1 // Copyright 2012 The Chromium Authors
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
7 #include "base/logging.h"
8 #include "build/build_config.h"
9
10 #if BUILDFLAG(IS_POSIX)
11 #include <unistd.h>
12 #endif
13
14 #if BUILDFLAG(IS_WIN)
15 #include <winsock2.h>
16 #include "net/base/winsock_init.h"
17 #endif
18
19 namespace net {
20
NetworkInterface()21 NetworkInterface::NetworkInterface()
22 : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) {
23 }
24
NetworkInterface(const std::string & name,const std::string & friendly_name,uint32_t interface_index,NetworkChangeNotifier::ConnectionType type,const IPAddress & address,uint32_t prefix_length,int ip_address_attributes,std::optional<Eui48MacAddress> mac_address)25 NetworkInterface::NetworkInterface(const std::string& name,
26 const std::string& friendly_name,
27 uint32_t interface_index,
28 NetworkChangeNotifier::ConnectionType type,
29 const IPAddress& address,
30 uint32_t prefix_length,
31 int ip_address_attributes,
32 std::optional<Eui48MacAddress> mac_address)
33 : name(name),
34 friendly_name(friendly_name),
35 interface_index(interface_index),
36 type(type),
37 address(address),
38 prefix_length(prefix_length),
39 ip_address_attributes(ip_address_attributes),
40 mac_address(mac_address) {}
41
42 NetworkInterface::NetworkInterface(const NetworkInterface& other) = default;
43
44 NetworkInterface::~NetworkInterface() = default;
45
46 ScopedWifiOptions::~ScopedWifiOptions() = default;
47
GetHostName()48 std::string GetHostName() {
49 #if BUILDFLAG(IS_WIN)
50 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);
61 }
62
63 } // namespace net
64