| 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/logging.h" |
| 8 #include "base/string_tokenizer.h" | |
| 9 | 8 |
| 10 namespace chromeos { | 9 namespace chromeos { |
| 11 | 10 |
| 12 namespace { | 11 namespace { |
| 13 #define ENUM_CASE(x) case x: return std::string(#x) | 12 #define ENUM_CASE(x) case x: return std::string(#x) |
| 14 std::string IPConfigTypeAsString(IPConfigType type) { | 13 std::string IPConfigTypeAsString(IPConfigType type) { |
| 15 switch (type) { | 14 switch (type) { |
| 16 ENUM_CASE(IPCONFIG_TYPE_UNKNOWN); | 15 ENUM_CASE(IPCONFIG_TYPE_UNKNOWN); |
| 17 ENUM_CASE(IPCONFIG_TYPE_IPV4); | 16 ENUM_CASE(IPCONFIG_TYPE_IPV4); |
| 18 ENUM_CASE(IPCONFIG_TYPE_IPV6); | 17 ENUM_CASE(IPCONFIG_TYPE_IPV6); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 44 | 43 |
| 45 std::string NetworkIPConfig::ToString() const { | 44 std::string NetworkIPConfig::ToString() const { |
| 46 return std::string("path: ") + device_path | 45 return std::string("path: ") + device_path |
| 47 + " type: " + IPConfigTypeAsString(type) | 46 + " type: " + IPConfigTypeAsString(type) |
| 48 + " address: " + address | 47 + " address: " + address |
| 49 + " netmask: " + netmask | 48 + " netmask: " + netmask |
| 50 + " gateway: " + gateway | 49 + " gateway: " + gateway |
| 51 + " name_servers: " + name_servers; | 50 + " name_servers: " + name_servers; |
| 52 } | 51 } |
| 53 | 52 |
| 54 int32 NetworkIPConfig::GetPrefixLength() const { | |
| 55 int count = 0; | |
| 56 int prefixlen = 0; | |
| 57 StringTokenizer t(netmask, "."); | |
| 58 while (t.GetNext()) { | |
| 59 // If there are more than 4 numbers, then it's invalid. | |
| 60 if (count == 4) { | |
| 61 return -1; | |
| 62 } | |
| 63 std::string token = t.token(); | |
| 64 // If we already found the last mask and the current one is not | |
| 65 // "0" then the netmask is invalid. For example, 255.224.255.0 | |
| 66 if (prefixlen / 8 != count) { | |
| 67 if (token != "0") { | |
| 68 return -1; | |
| 69 } | |
| 70 } else if (token == "255") { | |
| 71 prefixlen += 8; | |
| 72 } else if (token == "254") { | |
| 73 prefixlen += 7; | |
| 74 } else if (token == "252") { | |
| 75 prefixlen += 6; | |
| 76 } else if (token == "248") { | |
| 77 prefixlen += 5; | |
| 78 } else if (token == "240") { | |
| 79 prefixlen += 4; | |
| 80 } else if (token == "224") { | |
| 81 prefixlen += 3; | |
| 82 } else if (token == "192") { | |
| 83 prefixlen += 2; | |
| 84 } else if (token == "128") { | |
| 85 prefixlen += 1; | |
| 86 } else if (token == "0") { | |
| 87 prefixlen += 0; | |
| 88 } else { | |
| 89 // mask is not a valid number. | |
| 90 return -1; | |
| 91 } | |
| 92 count++; | |
| 93 } | |
| 94 if (count < 4) | |
| 95 return -1; | |
| 96 return prefixlen; | |
| 97 } | |
| 98 | |
| 99 } // namespace chromeos | 53 } // namespace chromeos |
| OLD | NEW |