network_interfaces.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include "net/base/network_interfaces.h"
  5. #include "base/logging.h"
  6. #include "build/build_config.h"
  7. #if BUILDFLAG(IS_POSIX)
  8. #include <unistd.h>
  9. #endif
  10. #if BUILDFLAG(IS_WIN)
  11. #include <winsock2.h>
  12. #include "net/base/winsock_init.h"
  13. #endif
  14. namespace net {
  15. NetworkInterface::NetworkInterface()
  16. : type(NetworkChangeNotifier::CONNECTION_UNKNOWN), prefix_length(0) {
  17. }
  18. NetworkInterface::NetworkInterface(const std::string& name,
  19. const std::string& friendly_name,
  20. uint32_t interface_index,
  21. NetworkChangeNotifier::ConnectionType type,
  22. const IPAddress& address,
  23. uint32_t prefix_length,
  24. int ip_address_attributes)
  25. : name(name),
  26. friendly_name(friendly_name),
  27. interface_index(interface_index),
  28. type(type),
  29. address(address),
  30. prefix_length(prefix_length),
  31. ip_address_attributes(ip_address_attributes) {}
  32. NetworkInterface::NetworkInterface(const NetworkInterface& other) = default;
  33. NetworkInterface::~NetworkInterface() = default;
  34. ScopedWifiOptions::~ScopedWifiOptions() = default;
  35. std::string GetHostName() {
  36. #if BUILDFLAG(IS_WIN)
  37. EnsureWinsockInit();
  38. #endif
  39. // Host names are limited to 255 bytes.
  40. char buffer[256];
  41. int result = gethostname(buffer, sizeof(buffer));
  42. if (result != 0) {
  43. DVLOG(1) << "gethostname() failed with " << result;
  44. buffer[0] = '\0';
  45. }
  46. return std::string(buffer);
  47. }
  48. } // namespace net