1 // 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
7 #include "base/logging.h"
8 #include "build/build_config.h"
9
10 #if defined(OS_POSIX)
11 #include <unistd.h>
12 #endif
13
14 #if defined(OS_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)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 : name(name),
33 friendly_name(friendly_name),
34 interface_index(interface_index),
35 type(type),
36 address(address),
37 prefix_length(prefix_length),
38 ip_address_attributes(ip_address_attributes) {}
39
40 NetworkInterface::NetworkInterface(const NetworkInterface& other) = default;
41
42 NetworkInterface::~NetworkInterface() = default;
43
44 ScopedWifiOptions::~ScopedWifiOptions() = default;
45
GetHostName()46 std::string GetHostName() {
47 #if defined(OS_NACL)
48 NOTIMPLEMENTED();
49 return std::string();
50 #else // defined(OS_NACL)
51 #if defined(OS_WIN)
52 EnsureWinsockInit();
53 #endif
54
55 // Host names are limited to 255 bytes.
56 char buffer[256];
57 int result = gethostname(buffer, sizeof(buffer));
58 if (result != 0) {
59 DVLOG(1) << "gethostname() failed with " << result;
60 buffer[0] = '\0';
61 }
62 return std::string(buffer);
63 #endif // !defined(OS_NACL)
64 }
65
66 } // namespace net
67