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 "net/base/net_util.h" | 5 #include "net/base/net_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 2124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2135 } | 2135 } |
2136 | 2136 |
2137 default: | 2137 default: |
2138 NOTREACHED(); | 2138 NOTREACHED(); |
2139 } | 2139 } |
2140 } | 2140 } |
2141 | 2141 |
2142 return false; | 2142 return false; |
2143 } | 2143 } |
2144 | 2144 |
2145 NetworkInterface::NetworkInterface() { | 2145 NetworkInterface::NetworkInterface() : network_prefix(0) { |
2146 } | 2146 } |
2147 | 2147 |
2148 NetworkInterface::NetworkInterface(const std::string& name, | 2148 NetworkInterface::NetworkInterface(const std::string& name, |
2149 const IPAddressNumber& address) | 2149 const IPAddressNumber& address, |
2150 : name(name), address(address) { | 2150 uint8 network_prefix) |
| 2151 : name(name), address(address), network_prefix(network_prefix) { |
2151 } | 2152 } |
2152 | 2153 |
2153 NetworkInterface::~NetworkInterface() { | 2154 NetworkInterface::~NetworkInterface() { |
2154 } | 2155 } |
2155 | 2156 |
| 2157 unsigned CommonPrefixLength(const IPAddressNumber& a1, |
| 2158 const IPAddressNumber& a2) { |
| 2159 DCHECK_EQ(a1.size(), a2.size()); |
| 2160 for (size_t i = 0; i < a1.size(); ++i) { |
| 2161 unsigned diff = a1[i] ^ a2[i]; |
| 2162 if (!diff) |
| 2163 continue; |
| 2164 for (unsigned j = 0; j < CHAR_BIT; ++j) { |
| 2165 if (diff & (1 << (CHAR_BIT - 1))) |
| 2166 return i * CHAR_BIT + j; |
| 2167 diff <<= 1; |
| 2168 } |
| 2169 NOTREACHED(); |
| 2170 } |
| 2171 return a1.size() * CHAR_BIT; |
| 2172 } |
| 2173 |
| 2174 unsigned MaskPrefixLength(const IPAddressNumber& mask) { |
| 2175 IPAddressNumber all_ones(mask.size(), 0xFF); |
| 2176 return CommonPrefixLength(mask, all_ones); |
| 2177 } |
| 2178 |
2156 } // namespace net | 2179 } // namespace net |
OLD | NEW |