Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include <arpa/inet.h> | 21 #include <arpa/inet.h> |
| 22 #include <netdb.h> | 22 #include <netdb.h> |
| 23 #include <unistd.h> | 23 #include <unistd.h> |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 #include <stdio.h> | 26 #include <stdio.h> |
| 27 | 27 |
| 28 #include "webrtc/base/ipaddress.h" | 28 #include "webrtc/base/ipaddress.h" |
| 29 #include "webrtc/base/byteorder.h" | 29 #include "webrtc/base/byteorder.h" |
| 30 #include "webrtc/base/checks.h" | 30 #include "webrtc/base/checks.h" |
| 31 #include "webrtc/base/logging.h" | |
| 31 #include "webrtc/base/nethelpers.h" | 32 #include "webrtc/base/nethelpers.h" |
| 32 #include "webrtc/base/logging.h" | 33 #include "webrtc/base/stringutils.h" |
| 33 #include "webrtc/base/win32.h" | 34 #include "webrtc/base/win32.h" |
| 34 | 35 |
| 35 namespace rtc { | 36 namespace rtc { |
| 36 | 37 |
| 37 // Prefixes used for categorizing IPv6 addresses. | 38 // Prefixes used for categorizing IPv6 addresses. |
| 38 static const in6_addr kV4MappedPrefix = {{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 39 static const in6_addr kV4MappedPrefix = {{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 39 0xFF, 0xFF, 0}}}; | 40 0xFF, 0xFF, 0}}}; |
| 40 static const in6_addr k6To4Prefix = {{{0x20, 0x02, 0}}}; | 41 static const in6_addr k6To4Prefix = {{{0x20, 0x02, 0}}}; |
| 41 static const in6_addr kTeredoPrefix = {{{0x20, 0x01, 0x00, 0x00}}}; | 42 static const in6_addr kTeredoPrefix = {{{0x20, 0x01, 0x00, 0x00}}}; |
| 42 static const in6_addr kV4CompatibilityPrefix = {{{0}}}; | 43 static const in6_addr kV4CompatibilityPrefix = {{{0}}}; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 | 130 |
| 130 std::string IPAddress::ToString() const { | 131 std::string IPAddress::ToString() const { |
| 131 if (family_ != AF_INET && family_ != AF_INET6) { | 132 if (family_ != AF_INET && family_ != AF_INET6) { |
| 132 return std::string(); | 133 return std::string(); |
| 133 } | 134 } |
| 134 char buf[INET6_ADDRSTRLEN] = {0}; | 135 char buf[INET6_ADDRSTRLEN] = {0}; |
| 135 const void* src = &u_.ip4; | 136 const void* src = &u_.ip4; |
| 136 if (family_ == AF_INET6) { | 137 if (family_ == AF_INET6) { |
| 137 src = &u_.ip6; | 138 src = &u_.ip6; |
| 138 } | 139 } |
| 139 if (!rtc::inet_ntop(family_, src, buf, sizeof(buf))) { | 140 if (!rtc::inet_ntop(family_, src, buf, sizeof(buf))) { |
|
pthatcher1
2016/01/13 22:25:23
Would it be a good idea to remove the dependency o
Sergey Ulanov
2016/01/13 22:47:42
Maybe, but I think it should be done separately fr
| |
| 140 return std::string(); | 141 return std::string(); |
| 141 } | 142 } |
| 142 return std::string(buf); | 143 return std::string(buf); |
| 143 } | 144 } |
| 144 | 145 |
| 145 std::string IPAddress::ToSensitiveString() const { | 146 std::string IPAddress::ToSensitiveString() const { |
| 146 #if !defined(NDEBUG) | 147 #if !defined(NDEBUG) |
| 147 // Return non-stripped in debug. | 148 // Return non-stripped in debug. |
| 148 return ToString(); | 149 return ToString(); |
| 149 #else | 150 #else |
| 150 switch (family_) { | 151 switch (family_) { |
| 151 case AF_INET: { | 152 case AF_INET: { |
| 152 std::string address = ToString(); | 153 std::string address = ToString(); |
| 153 size_t find_pos = address.rfind('.'); | 154 size_t find_pos = address.rfind('.'); |
| 154 if (find_pos == std::string::npos) | 155 if (find_pos == std::string::npos) |
| 155 return std::string(); | 156 return std::string(); |
| 156 address.resize(find_pos); | 157 address.resize(find_pos); |
| 157 address += ".x"; | 158 address += ".x"; |
| 158 return address; | 159 return address; |
| 159 } | 160 } |
| 160 case AF_INET6: { | 161 case AF_INET6: { |
| 161 // Remove the last 5 groups (80 bits). | 162 std::string result; |
| 162 std::string address = TruncateIP(*this, 128 - 80).ToString(); | 163 result.resize(40); |
|
pthatcher1
2016/01/13 22:25:23
Why 40?
Truncated, it could only be 25. Are you
Sergey Ulanov
2016/01/13 22:47:42
Changed it to INET6_ADDRSTRLEN. (it's set to 46, f
| |
| 163 | 164 in6_addr addr = ipv6_address(); |
| 164 // If all three remaining groups are written out explicitly in the string, | 165 size_t len = |
| 165 // remove one of the two trailing colons before appending the stripped | 166 rtc::sprintfn(&(result[0]), result.size(), "%x:%x:%x:x:x:x:x:x", |
| 166 // groups as "x"s. There should be max 4 colons (2 between the 3 groups + | 167 (addr.s6_addr[0] << 8) + addr.s6_addr[1], |
| 167 // 2 trailing) in the truncated address string. | 168 (addr.s6_addr[2] << 8) + addr.s6_addr[3], |
| 168 size_t number_of_colons = std::count(address.begin(), address.end(), ':'); | 169 (addr.s6_addr[4] << 8) + addr.s6_addr[5]); |
|
pthatcher1
2016/01/13 22:25:23
Would it be worth the little extra code to make ":
Sergey Ulanov
2016/01/13 22:47:42
I don't think so. It would make this code more com
| |
| 169 RTC_CHECK_LE(number_of_colons, 4u); | 170 result.resize(len); |
| 170 if (number_of_colons > 3) | 171 return result; |
| 171 address.resize(address.length() - 1); | |
| 172 | |
| 173 return address + "x:x:x:x:x"; | |
| 174 } | 172 } |
| 175 } | 173 } |
| 176 return std::string(); | 174 return std::string(); |
| 177 #endif | 175 #endif |
| 178 } | 176 } |
| 179 | 177 |
| 180 IPAddress IPAddress::Normalized() const { | 178 IPAddress IPAddress::Normalized() const { |
| 181 if (family_ != AF_INET6) { | 179 if (family_ != AF_INET6) { |
| 182 return *this; | 180 return *this; |
| 183 } | 181 } |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 518 if (family == AF_INET) { | 516 if (family == AF_INET) { |
| 519 return rtc::IPAddress(INADDR_ANY); | 517 return rtc::IPAddress(INADDR_ANY); |
| 520 } | 518 } |
| 521 if (family == AF_INET6) { | 519 if (family == AF_INET6) { |
| 522 return rtc::IPAddress(in6addr_any); | 520 return rtc::IPAddress(in6addr_any); |
| 523 } | 521 } |
| 524 return rtc::IPAddress(); | 522 return rtc::IPAddress(); |
| 525 } | 523 } |
| 526 | 524 |
| 527 } // Namespace rtc | 525 } // Namespace rtc |
| OLD | NEW |