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 "chromeos/network/network_util.h" | 5 #include "chromeos/network/network_util.h" |
6 | 6 |
7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
8 #include "base/strings/string_tokenizer.h" | 8 #include "base/strings/string_tokenizer.h" |
9 | 9 |
10 namespace chromeos { | 10 namespace chromeos { |
(...skipping 25 matching lines...) Expand all Loading... |
36 if (prefix_length >= 8) { | 36 if (prefix_length >= 8) { |
37 prefix_length -= 8; | 37 prefix_length -= 8; |
38 } else { | 38 } else { |
39 remainder = prefix_length; | 39 remainder = prefix_length; |
40 prefix_length = 0; | 40 prefix_length = 0; |
41 } | 41 } |
42 if (i > 0) | 42 if (i > 0) |
43 netmask += "."; | 43 netmask += "."; |
44 int value = remainder == 0 ? 0 : | 44 int value = remainder == 0 ? 0 : |
45 ((2L << (remainder - 1)) - 1) << (8 - remainder); | 45 ((2L << (remainder - 1)) - 1) << (8 - remainder); |
46 netmask += StringPrintf("%d", value); | 46 netmask += base::StringPrintf("%d", value); |
47 } | 47 } |
48 return netmask; | 48 return netmask; |
49 } | 49 } |
50 | 50 |
51 int32 NetmaskToPrefixLength(const std::string& netmask) { | 51 int32 NetmaskToPrefixLength(const std::string& netmask) { |
52 int count = 0; | 52 int count = 0; |
53 int prefix_length = 0; | 53 int prefix_length = 0; |
54 base::StringTokenizer t(netmask, "."); | 54 base::StringTokenizer t(netmask, "."); |
55 while (t.GetNext()) { | 55 while (t.GetNext()) { |
56 // If there are more than 4 numbers, then it's invalid. | 56 // If there are more than 4 numbers, then it's invalid. |
(...skipping 30 matching lines...) Expand all Loading... |
87 } | 87 } |
88 count++; | 88 count++; |
89 } | 89 } |
90 if (count < 4) | 90 if (count < 4) |
91 return -1; | 91 return -1; |
92 return prefix_length; | 92 return prefix_length; |
93 } | 93 } |
94 | 94 |
95 } // namespace network_util | 95 } // namespace network_util |
96 } // namespace chromeos | 96 } // namespace chromeos |
OLD | NEW |