xref: /aosp_15_r20/external/cronet/net/base/network_interfaces.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
17 #include "net/base/winsock_init.h"
18 #endif
19 
20 namespace net {
21 
NetworkInterface()22 NetworkInterface::NetworkInterface()
23     : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) {
24 }
25 
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)26 NetworkInterface::NetworkInterface(const std::string& name,
27                                    const std::string& friendly_name,
28                                    uint32_t interface_index,
29                                    NetworkChangeNotifier::ConnectionType type,
30                                    const IPAddress& address,
31                                    uint32_t prefix_length,
32                                    int ip_address_attributes,
33                                    std::optional<Eui48MacAddress> mac_address)
34     : name(name),
35       friendly_name(friendly_name),
36       interface_index(interface_index),
37       type(type),
38       address(address),
39       prefix_length(prefix_length),
40       ip_address_attributes(ip_address_attributes),
41       mac_address(mac_address) {}
42 
43 NetworkInterface::NetworkInterface(const NetworkInterface& other) = default;
44 
45 NetworkInterface::~NetworkInterface() = default;
46 
47 ScopedWifiOptions::~ScopedWifiOptions() = default;
48 
GetHostName()49 std::string GetHostName() {
50 #if BUILDFLAG(IS_WIN)
51   EnsureWinsockInit();
52 #endif
53 
54   // Host names are limited to 255 bytes.
55   char buffer[256];
56   int result = gethostname(buffer, sizeof(buffer));
57   if (result != 0) {
58     DVLOG(1) << "gethostname() failed with " << result;
59     buffer[0] = '\0';
60   }
61   return std::string(buffer);
62 }
63 
64 }  // namespace net
65