OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/cros/network_ip_config.h" | 5 #include "chrome/browser/chromeos/cros/network_ip_config.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "base/string_tokenizer.h" | 8 #include "base/string_tokenizer.h" |
8 | 9 |
9 namespace chromeos { | 10 namespace chromeos { |
10 | 11 |
| 12 namespace { |
| 13 #define ENUM_CASE(x) case x: return std::string(#x) |
| 14 std::string IPConfigTypeAsString(IPConfigType type) { |
| 15 switch (type) { |
| 16 ENUM_CASE(IPCONFIG_TYPE_UNKNOWN); |
| 17 ENUM_CASE(IPCONFIG_TYPE_IPV4); |
| 18 ENUM_CASE(IPCONFIG_TYPE_IPV6); |
| 19 ENUM_CASE(IPCONFIG_TYPE_DHCP); |
| 20 ENUM_CASE(IPCONFIG_TYPE_BOOTP); |
| 21 ENUM_CASE(IPCONFIG_TYPE_ZEROCONF); |
| 22 ENUM_CASE(IPCONFIG_TYPE_DHCP6); |
| 23 ENUM_CASE(IPCONFIG_TYPE_PPP); |
| 24 } |
| 25 NOTREACHED() << "Unhandled enum value " << type; |
| 26 return std::string(); |
| 27 } |
| 28 #undef ENUM_CASE |
| 29 } // namespace |
| 30 |
11 NetworkIPConfig::NetworkIPConfig( | 31 NetworkIPConfig::NetworkIPConfig( |
12 const std::string& device_path, IPConfigType type, | 32 const std::string& device_path, IPConfigType type, |
13 const std::string& address, const std::string& netmask, | 33 const std::string& address, const std::string& netmask, |
14 const std::string& gateway, const std::string& name_servers) | 34 const std::string& gateway, const std::string& name_servers) |
15 : device_path(device_path), | 35 : device_path(device_path), |
16 type(type), | 36 type(type), |
17 address(address), | 37 address(address), |
18 netmask(netmask), | 38 netmask(netmask), |
19 gateway(gateway), | 39 gateway(gateway), |
20 name_servers(name_servers) { | 40 name_servers(name_servers) { |
21 } | 41 } |
22 | 42 |
23 NetworkIPConfig::~NetworkIPConfig() {} | 43 NetworkIPConfig::~NetworkIPConfig() {} |
24 | 44 |
| 45 std::string NetworkIPConfig::ToString() const { |
| 46 return std::string("path: ") + device_path |
| 47 + " type: " + IPConfigTypeAsString(type) |
| 48 + " address: " + address |
| 49 + " netmask: " + netmask |
| 50 + " gateway: " + gateway |
| 51 + " name_servers: " + name_servers; |
| 52 } |
| 53 |
25 int32 NetworkIPConfig::GetPrefixLength() const { | 54 int32 NetworkIPConfig::GetPrefixLength() const { |
26 int count = 0; | 55 int count = 0; |
27 int prefixlen = 0; | 56 int prefixlen = 0; |
28 StringTokenizer t(netmask, "."); | 57 StringTokenizer t(netmask, "."); |
29 while (t.GetNext()) { | 58 while (t.GetNext()) { |
30 // If there are more than 4 numbers, then it's invalid. | 59 // If there are more than 4 numbers, then it's invalid. |
31 if (count == 4) { | 60 if (count == 4) { |
32 return -1; | 61 return -1; |
33 } | 62 } |
34 std::string token = t.token(); | 63 std::string token = t.token(); |
(...skipping 26 matching lines...) Expand all Loading... |
61 return -1; | 90 return -1; |
62 } | 91 } |
63 count++; | 92 count++; |
64 } | 93 } |
65 if (count < 4) | 94 if (count < 4) |
66 return -1; | 95 return -1; |
67 return prefixlen; | 96 return prefixlen; |
68 } | 97 } |
69 | 98 |
70 } // namespace chromeos | 99 } // namespace chromeos |
OLD | NEW |