blob: 2db51a9843a25d58b1ad4b62dbd081aeba8628a9 [file] [log] [blame]
eromanc69886a42015-06-03 18:19:521// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/base/network_interfaces.h"
6
Hans Wennborg725d0432020-06-18 13:54:167#include "base/logging.h"
tfarinae5f66e62016-02-12 02:48:408#include "build/build_config.h"
9
Xiaohan Wang2a6845b2022-01-08 04:40:5710#if BUILDFLAG(IS_POSIX)
tfarinae5f66e62016-02-12 02:48:4011#include <unistd.h>
12#endif
13
Xiaohan Wang2a6845b2022-01-08 04:40:5714#if BUILDFLAG(IS_WIN)
tfarinae5f66e62016-02-12 02:48:4015#include <winsock2.h>
16#include "net/base/winsock_init.h"
17#endif
18
eromanc69886a42015-06-03 18:19:5219namespace net {
20
21NetworkInterface::NetworkInterface()
22 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) {
23}
24
25NetworkInterface::NetworkInterface(const std::string& name,
26 const std::string& friendly_name,
wtc69f8ea82015-06-04 00:08:1327 uint32_t interface_index,
eromanc69886a42015-06-03 18:19:5228 NetworkChangeNotifier::ConnectionType type,
martijn6e030652016-03-03 19:49:2629 const IPAddress& address,
30 uint32_t prefix_length,
Erik Oveliusb51918572022-09-02 18:52:1231 int ip_address_attributes,
32 absl::optional<Eui48MacAddress> mac_address)
eromanc69886a42015-06-03 18:19:5233 : name(name),
34 friendly_name(friendly_name),
35 interface_index(interface_index),
36 type(type),
37 address(address),
38 prefix_length(prefix_length),
Erik Oveliusb51918572022-09-02 18:52:1239 ip_address_attributes(ip_address_attributes),
40 mac_address(mac_address) {}
eromanc69886a42015-06-03 18:19:5241
vmpstracd23b72016-02-26 21:08:5542NetworkInterface::NetworkInterface(const NetworkInterface& other) = default;
43
Chris Watkins68b15032017-12-01 03:07:1344NetworkInterface::~NetworkInterface() = default;
eromanc69886a42015-06-03 18:19:5245
Chris Watkins68b15032017-12-01 03:07:1346ScopedWifiOptions::~ScopedWifiOptions() = default;
eromanc69886a42015-06-03 18:19:5247
tfarinae5f66e62016-02-12 02:48:4048std::string GetHostName() {
Xiaohan Wang2a6845b2022-01-08 04:40:5749#if BUILDFLAG(IS_WIN)
tfarinae5f66e62016-02-12 02:48:4050 EnsureWinsockInit();
51#endif
52
53 // Host names are limited to 255 bytes.
54 char buffer[256];
55 int result = gethostname(buffer, sizeof(buffer));
56 if (result != 0) {
57 DVLOG(1) << "gethostname() failed with " << result;
58 buffer[0] = '\0';
59 }
60 return std::string(buffer);
tfarinae5f66e62016-02-12 02:48:4061}
62
eromanc69886a42015-06-03 18:19:5263} // namespace net