eroman | c69886a4 | 2015-06-03 18:19:52 | [diff] [blame] | 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 | |
Hans Wennborg | 725d043 | 2020-06-18 13:54:16 | [diff] [blame] | 7 | #include "base/logging.h" |
tfarina | e5f66e6 | 2016-02-12 02:48:40 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 10 | #if BUILDFLAG(IS_POSIX) |
tfarina | e5f66e6 | 2016-02-12 02:48:40 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | #endif |
| 13 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 14 | #if BUILDFLAG(IS_WIN) |
tfarina | e5f66e6 | 2016-02-12 02:48:40 | [diff] [blame] | 15 | #include <winsock2.h> |
| 16 | #include "net/base/winsock_init.h" |
| 17 | #endif |
| 18 | |
eroman | c69886a4 | 2015-06-03 18:19:52 | [diff] [blame] | 19 | namespace net { |
| 20 | |
| 21 | NetworkInterface::NetworkInterface() |
| 22 | : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) { |
| 23 | } |
| 24 | |
| 25 | NetworkInterface::NetworkInterface(const std::string& name, |
| 26 | const std::string& friendly_name, |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 27 | uint32_t interface_index, |
eroman | c69886a4 | 2015-06-03 18:19:52 | [diff] [blame] | 28 | NetworkChangeNotifier::ConnectionType type, |
martijn | 6e03065 | 2016-03-03 19:49:26 | [diff] [blame] | 29 | const IPAddress& address, |
| 30 | uint32_t prefix_length, |
Erik Ovelius | b5191857 | 2022-09-02 18:52:12 | [diff] [blame] | 31 | int ip_address_attributes, |
| 32 | absl::optional<Eui48MacAddress> mac_address) |
eroman | c69886a4 | 2015-06-03 18:19:52 | [diff] [blame] | 33 | : name(name), |
| 34 | friendly_name(friendly_name), |
| 35 | interface_index(interface_index), |
| 36 | type(type), |
| 37 | address(address), |
| 38 | prefix_length(prefix_length), |
Erik Ovelius | b5191857 | 2022-09-02 18:52:12 | [diff] [blame] | 39 | ip_address_attributes(ip_address_attributes), |
| 40 | mac_address(mac_address) {} |
eroman | c69886a4 | 2015-06-03 18:19:52 | [diff] [blame] | 41 | |
vmpstr | acd23b7 | 2016-02-26 21:08:55 | [diff] [blame] | 42 | NetworkInterface::NetworkInterface(const NetworkInterface& other) = default; |
| 43 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 44 | NetworkInterface::~NetworkInterface() = default; |
eroman | c69886a4 | 2015-06-03 18:19:52 | [diff] [blame] | 45 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 46 | ScopedWifiOptions::~ScopedWifiOptions() = default; |
eroman | c69886a4 | 2015-06-03 18:19:52 | [diff] [blame] | 47 | |
tfarina | e5f66e6 | 2016-02-12 02:48:40 | [diff] [blame] | 48 | std::string GetHostName() { |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 49 | #if BUILDFLAG(IS_WIN) |
tfarina | e5f66e6 | 2016-02-12 02:48:40 | [diff] [blame] | 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); |
tfarina | e5f66e6 | 2016-02-12 02:48:40 | [diff] [blame] | 61 | } |
| 62 | |
eroman | c69886a4 | 2015-06-03 18:19:52 | [diff] [blame] | 63 | } // namespace net |