You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

570 lines
15 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #if defined(WEBRTC_POSIX)
  11. #include <netinet/in.h>
  12. #include <sys/socket.h>
  13. #ifdef OPENBSD
  14. #include <netinet/in_systm.h>
  15. #endif
  16. #ifndef __native_client__
  17. #include <netinet/ip.h>
  18. #endif
  19. #include <netdb.h>
  20. #endif
  21. #include "rtc_base/byte_order.h"
  22. #include "rtc_base/ip_address.h"
  23. #include "rtc_base/net_helpers.h"
  24. #include "rtc_base/string_utils.h"
  25. #if defined(WEBRTC_WIN)
  26. #include "rtc_base/win32.h"
  27. #endif // WEBRTC_WIN
  28. namespace rtc {
  29. // Prefixes used for categorizing IPv6 addresses.
  30. static const in6_addr kV4MappedPrefix = {
  31. {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0}}};
  32. static const in6_addr k6To4Prefix = {{{0x20, 0x02, 0}}};
  33. static const in6_addr kTeredoPrefix = {{{0x20, 0x01, 0x00, 0x00}}};
  34. static const in6_addr kV4CompatibilityPrefix = {{{0}}};
  35. static const in6_addr k6BonePrefix = {{{0x3f, 0xfe, 0}}};
  36. static const in6_addr kPrivateNetworkPrefix = {{{0xFD}}};
  37. static bool IPIsHelper(const IPAddress& ip,
  38. const in6_addr& tomatch,
  39. int length);
  40. static in_addr ExtractMappedAddress(const in6_addr& addr);
  41. uint32_t IPAddress::v4AddressAsHostOrderInteger() const {
  42. if (family_ == AF_INET) {
  43. return NetworkToHost32(u_.ip4.s_addr);
  44. } else {
  45. return 0;
  46. }
  47. }
  48. bool IPAddress::IsNil() const {
  49. return IPIsUnspec(*this);
  50. }
  51. size_t IPAddress::Size() const {
  52. switch (family_) {
  53. case AF_INET:
  54. return sizeof(in_addr);
  55. case AF_INET6:
  56. return sizeof(in6_addr);
  57. }
  58. return 0;
  59. }
  60. bool IPAddress::operator==(const IPAddress& other) const {
  61. if (family_ != other.family_) {
  62. return false;
  63. }
  64. if (family_ == AF_INET) {
  65. return memcmp(&u_.ip4, &other.u_.ip4, sizeof(u_.ip4)) == 0;
  66. }
  67. if (family_ == AF_INET6) {
  68. return memcmp(&u_.ip6, &other.u_.ip6, sizeof(u_.ip6)) == 0;
  69. }
  70. return family_ == AF_UNSPEC;
  71. }
  72. bool IPAddress::operator!=(const IPAddress& other) const {
  73. return !((*this) == other);
  74. }
  75. bool IPAddress::operator>(const IPAddress& other) const {
  76. return (*this) != other && !((*this) < other);
  77. }
  78. bool IPAddress::operator<(const IPAddress& other) const {
  79. // IPv4 is 'less than' IPv6
  80. if (family_ != other.family_) {
  81. if (family_ == AF_UNSPEC) {
  82. return true;
  83. }
  84. if (family_ == AF_INET && other.family_ == AF_INET6) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. // Comparing addresses of the same family.
  90. switch (family_) {
  91. case AF_INET: {
  92. return NetworkToHost32(u_.ip4.s_addr) <
  93. NetworkToHost32(other.u_.ip4.s_addr);
  94. }
  95. case AF_INET6: {
  96. return memcmp(&u_.ip6.s6_addr, &other.u_.ip6.s6_addr, 16) < 0;
  97. }
  98. }
  99. // Catches AF_UNSPEC and invalid addresses.
  100. return false;
  101. }
  102. in6_addr IPAddress::ipv6_address() const {
  103. return u_.ip6;
  104. }
  105. in_addr IPAddress::ipv4_address() const {
  106. return u_.ip4;
  107. }
  108. std::string IPAddress::ToString() const {
  109. if (family_ != AF_INET && family_ != AF_INET6) {
  110. return std::string();
  111. }
  112. char buf[INET6_ADDRSTRLEN] = {0};
  113. const void* src = &u_.ip4;
  114. if (family_ == AF_INET6) {
  115. src = &u_.ip6;
  116. }
  117. if (!rtc::inet_ntop(family_, src, buf, sizeof(buf))) {
  118. return std::string();
  119. }
  120. return std::string(buf);
  121. }
  122. std::string IPAddress::ToSensitiveString() const {
  123. #if !defined(NDEBUG)
  124. // Return non-stripped in debug.
  125. return ToString();
  126. #else
  127. switch (family_) {
  128. case AF_INET: {
  129. std::string address = ToString();
  130. size_t find_pos = address.rfind('.');
  131. if (find_pos == std::string::npos)
  132. return std::string();
  133. address.resize(find_pos);
  134. address += ".x";
  135. return address;
  136. }
  137. case AF_INET6: {
  138. std::string result;
  139. result.resize(INET6_ADDRSTRLEN);
  140. in6_addr addr = ipv6_address();
  141. size_t len = snprintf(&(result[0]), result.size(), "%x:%x:%x:x:x:x:x:x",
  142. (addr.s6_addr[0] << 8) + addr.s6_addr[1],
  143. (addr.s6_addr[2] << 8) + addr.s6_addr[3],
  144. (addr.s6_addr[4] << 8) + addr.s6_addr[5]);
  145. result.resize(len);
  146. return result;
  147. }
  148. }
  149. return std::string();
  150. #endif
  151. }
  152. IPAddress IPAddress::Normalized() const {
  153. if (family_ != AF_INET6) {
  154. return *this;
  155. }
  156. if (!IPIsV4Mapped(*this)) {
  157. return *this;
  158. }
  159. in_addr addr = ExtractMappedAddress(u_.ip6);
  160. return IPAddress(addr);
  161. }
  162. IPAddress IPAddress::AsIPv6Address() const {
  163. if (family_ != AF_INET) {
  164. return *this;
  165. }
  166. in6_addr v6addr = kV4MappedPrefix;
  167. ::memcpy(&v6addr.s6_addr[12], &u_.ip4.s_addr, sizeof(u_.ip4.s_addr));
  168. return IPAddress(v6addr);
  169. }
  170. bool InterfaceAddress::operator==(const InterfaceAddress& other) const {
  171. return ipv6_flags_ == other.ipv6_flags() &&
  172. static_cast<const IPAddress&>(*this) == other;
  173. }
  174. bool InterfaceAddress::operator!=(const InterfaceAddress& other) const {
  175. return !((*this) == other);
  176. }
  177. const InterfaceAddress& InterfaceAddress::operator=(
  178. const InterfaceAddress& other) {
  179. ipv6_flags_ = other.ipv6_flags_;
  180. static_cast<IPAddress&>(*this) = other;
  181. return *this;
  182. }
  183. std::string InterfaceAddress::ToString() const {
  184. std::string result = IPAddress::ToString();
  185. if (family() == AF_INET6)
  186. result += "|flags:0x" + rtc::ToHex(ipv6_flags());
  187. return result;
  188. }
  189. static bool IPIsPrivateNetworkV4(const IPAddress& ip) {
  190. uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger();
  191. return ((ip_in_host_order >> 24) == 10) ||
  192. ((ip_in_host_order >> 20) == ((172 << 4) | 1)) ||
  193. ((ip_in_host_order >> 16) == ((192 << 8) | 168));
  194. }
  195. static bool IPIsPrivateNetworkV6(const IPAddress& ip) {
  196. return IPIsHelper(ip, kPrivateNetworkPrefix, 8);
  197. }
  198. bool IPIsPrivateNetwork(const IPAddress& ip) {
  199. switch (ip.family()) {
  200. case AF_INET: {
  201. return IPIsPrivateNetworkV4(ip);
  202. }
  203. case AF_INET6: {
  204. return IPIsPrivateNetworkV6(ip);
  205. }
  206. }
  207. return false;
  208. }
  209. static bool IPIsSharedNetworkV4(const IPAddress& ip) {
  210. uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger();
  211. return (ip_in_host_order >> 22) == ((100 << 2) | 1);
  212. }
  213. bool IPIsSharedNetwork(const IPAddress& ip) {
  214. if (ip.family() == AF_INET) {
  215. return IPIsSharedNetworkV4(ip);
  216. }
  217. return false;
  218. }
  219. in_addr ExtractMappedAddress(const in6_addr& in6) {
  220. in_addr ipv4;
  221. ::memcpy(&ipv4.s_addr, &in6.s6_addr[12], sizeof(ipv4.s_addr));
  222. return ipv4;
  223. }
  224. bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out) {
  225. if (!info || !info->ai_addr) {
  226. return false;
  227. }
  228. if (info->ai_addr->sa_family == AF_INET) {
  229. sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(info->ai_addr);
  230. *out = IPAddress(addr->sin_addr);
  231. return true;
  232. } else if (info->ai_addr->sa_family == AF_INET6) {
  233. sockaddr_in6* addr = reinterpret_cast<sockaddr_in6*>(info->ai_addr);
  234. *out = IPAddress(addr->sin6_addr);
  235. return true;
  236. }
  237. return false;
  238. }
  239. bool IPFromString(const std::string& str, IPAddress* out) {
  240. if (!out) {
  241. return false;
  242. }
  243. in_addr addr;
  244. if (rtc::inet_pton(AF_INET, str.c_str(), &addr) == 0) {
  245. in6_addr addr6;
  246. if (rtc::inet_pton(AF_INET6, str.c_str(), &addr6) == 0) {
  247. *out = IPAddress();
  248. return false;
  249. }
  250. *out = IPAddress(addr6);
  251. } else {
  252. *out = IPAddress(addr);
  253. }
  254. return true;
  255. }
  256. bool IPFromString(const std::string& str, int flags, InterfaceAddress* out) {
  257. IPAddress ip;
  258. if (!IPFromString(str, &ip)) {
  259. return false;
  260. }
  261. *out = InterfaceAddress(ip, flags);
  262. return true;
  263. }
  264. bool IPIsAny(const IPAddress& ip) {
  265. switch (ip.family()) {
  266. case AF_INET:
  267. return ip == IPAddress(INADDR_ANY);
  268. case AF_INET6:
  269. return ip == IPAddress(in6addr_any) || ip == IPAddress(kV4MappedPrefix);
  270. case AF_UNSPEC:
  271. return false;
  272. }
  273. return false;
  274. }
  275. static bool IPIsLoopbackV4(const IPAddress& ip) {
  276. uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger();
  277. return ((ip_in_host_order >> 24) == 127);
  278. }
  279. static bool IPIsLoopbackV6(const IPAddress& ip) {
  280. return ip == IPAddress(in6addr_loopback);
  281. }
  282. bool IPIsLoopback(const IPAddress& ip) {
  283. switch (ip.family()) {
  284. case AF_INET: {
  285. return IPIsLoopbackV4(ip);
  286. }
  287. case AF_INET6: {
  288. return IPIsLoopbackV6(ip);
  289. }
  290. }
  291. return false;
  292. }
  293. bool IPIsPrivate(const IPAddress& ip) {
  294. return IPIsLinkLocal(ip) || IPIsLoopback(ip) || IPIsPrivateNetwork(ip) ||
  295. IPIsSharedNetwork(ip);
  296. }
  297. bool IPIsUnspec(const IPAddress& ip) {
  298. return ip.family() == AF_UNSPEC;
  299. }
  300. size_t HashIP(const IPAddress& ip) {
  301. switch (ip.family()) {
  302. case AF_INET: {
  303. return ip.ipv4_address().s_addr;
  304. }
  305. case AF_INET6: {
  306. in6_addr v6addr = ip.ipv6_address();
  307. const uint32_t* v6_as_ints =
  308. reinterpret_cast<const uint32_t*>(&v6addr.s6_addr);
  309. return v6_as_ints[0] ^ v6_as_ints[1] ^ v6_as_ints[2] ^ v6_as_ints[3];
  310. }
  311. }
  312. return 0;
  313. }
  314. IPAddress TruncateIP(const IPAddress& ip, int length) {
  315. if (length < 0) {
  316. return IPAddress();
  317. }
  318. if (ip.family() == AF_INET) {
  319. if (length > 31) {
  320. return ip;
  321. }
  322. if (length == 0) {
  323. return IPAddress(INADDR_ANY);
  324. }
  325. int mask = (0xFFFFFFFF << (32 - length));
  326. uint32_t host_order_ip = NetworkToHost32(ip.ipv4_address().s_addr);
  327. in_addr masked;
  328. masked.s_addr = HostToNetwork32(host_order_ip & mask);
  329. return IPAddress(masked);
  330. } else if (ip.family() == AF_INET6) {
  331. if (length > 127) {
  332. return ip;
  333. }
  334. if (length == 0) {
  335. return IPAddress(in6addr_any);
  336. }
  337. in6_addr v6addr = ip.ipv6_address();
  338. int position = length / 32;
  339. int inner_length = 32 - (length - (position * 32));
  340. // Note: 64bit mask constant needed to allow possible 32-bit left shift.
  341. uint32_t inner_mask = 0xFFFFFFFFLL << inner_length;
  342. uint32_t* v6_as_ints = reinterpret_cast<uint32_t*>(&v6addr.s6_addr);
  343. for (int i = 0; i < 4; ++i) {
  344. if (i == position) {
  345. uint32_t host_order_inner = NetworkToHost32(v6_as_ints[i]);
  346. v6_as_ints[i] = HostToNetwork32(host_order_inner & inner_mask);
  347. } else if (i > position) {
  348. v6_as_ints[i] = 0;
  349. }
  350. }
  351. return IPAddress(v6addr);
  352. }
  353. return IPAddress();
  354. }
  355. int CountIPMaskBits(IPAddress mask) {
  356. uint32_t word_to_count = 0;
  357. int bits = 0;
  358. switch (mask.family()) {
  359. case AF_INET: {
  360. word_to_count = NetworkToHost32(mask.ipv4_address().s_addr);
  361. break;
  362. }
  363. case AF_INET6: {
  364. in6_addr v6addr = mask.ipv6_address();
  365. const uint32_t* v6_as_ints =
  366. reinterpret_cast<const uint32_t*>(&v6addr.s6_addr);
  367. int i = 0;
  368. for (; i < 4; ++i) {
  369. if (v6_as_ints[i] != 0xFFFFFFFF) {
  370. break;
  371. }
  372. }
  373. if (i < 4) {
  374. word_to_count = NetworkToHost32(v6_as_ints[i]);
  375. }
  376. bits = (i * 32);
  377. break;
  378. }
  379. default: {
  380. return 0;
  381. }
  382. }
  383. if (word_to_count == 0) {
  384. return bits;
  385. }
  386. // Public domain bit-twiddling hack from:
  387. // http://graphics.stanford.edu/~seander/bithacks.html
  388. // Counts the trailing 0s in the word.
  389. unsigned int zeroes = 32;
  390. // This could also be written word_to_count &= -word_to_count, but
  391. // MSVC emits warning C4146 when negating an unsigned number.
  392. word_to_count &= ~word_to_count + 1; // Isolate lowest set bit.
  393. if (word_to_count)
  394. zeroes--;
  395. if (word_to_count & 0x0000FFFF)
  396. zeroes -= 16;
  397. if (word_to_count & 0x00FF00FF)
  398. zeroes -= 8;
  399. if (word_to_count & 0x0F0F0F0F)
  400. zeroes -= 4;
  401. if (word_to_count & 0x33333333)
  402. zeroes -= 2;
  403. if (word_to_count & 0x55555555)
  404. zeroes -= 1;
  405. return bits + (32 - zeroes);
  406. }
  407. bool IPIsHelper(const IPAddress& ip, const in6_addr& tomatch, int length) {
  408. // Helper method for checking IP prefix matches (but only on whole byte
  409. // lengths). Length is in bits.
  410. in6_addr addr = ip.ipv6_address();
  411. return ::memcmp(&addr, &tomatch, (length >> 3)) == 0;
  412. }
  413. bool IPIs6Bone(const IPAddress& ip) {
  414. return IPIsHelper(ip, k6BonePrefix, 16);
  415. }
  416. bool IPIs6To4(const IPAddress& ip) {
  417. return IPIsHelper(ip, k6To4Prefix, 16);
  418. }
  419. static bool IPIsLinkLocalV4(const IPAddress& ip) {
  420. uint32_t ip_in_host_order = ip.v4AddressAsHostOrderInteger();
  421. return ((ip_in_host_order >> 16) == ((169 << 8) | 254));
  422. }
  423. static bool IPIsLinkLocalV6(const IPAddress& ip) {
  424. // Can't use the helper because the prefix is 10 bits.
  425. in6_addr addr = ip.ipv6_address();
  426. return (addr.s6_addr[0] == 0xFE) && ((addr.s6_addr[1] & 0xC0) == 0x80);
  427. }
  428. bool IPIsLinkLocal(const IPAddress& ip) {
  429. switch (ip.family()) {
  430. case AF_INET: {
  431. return IPIsLinkLocalV4(ip);
  432. }
  433. case AF_INET6: {
  434. return IPIsLinkLocalV6(ip);
  435. }
  436. }
  437. return false;
  438. }
  439. // According to http://www.ietf.org/rfc/rfc2373.txt, Appendix A, page 19. An
  440. // address which contains MAC will have its 11th and 12th bytes as FF:FE as well
  441. // as the U/L bit as 1.
  442. bool IPIsMacBased(const IPAddress& ip) {
  443. in6_addr addr = ip.ipv6_address();
  444. return ((addr.s6_addr[8] & 0x02) && addr.s6_addr[11] == 0xFF &&
  445. addr.s6_addr[12] == 0xFE);
  446. }
  447. bool IPIsSiteLocal(const IPAddress& ip) {
  448. // Can't use the helper because the prefix is 10 bits.
  449. in6_addr addr = ip.ipv6_address();
  450. return addr.s6_addr[0] == 0xFE && (addr.s6_addr[1] & 0xC0) == 0xC0;
  451. }
  452. bool IPIsULA(const IPAddress& ip) {
  453. // Can't use the helper because the prefix is 7 bits.
  454. in6_addr addr = ip.ipv6_address();
  455. return (addr.s6_addr[0] & 0xFE) == 0xFC;
  456. }
  457. bool IPIsTeredo(const IPAddress& ip) {
  458. return IPIsHelper(ip, kTeredoPrefix, 32);
  459. }
  460. bool IPIsV4Compatibility(const IPAddress& ip) {
  461. return IPIsHelper(ip, kV4CompatibilityPrefix, 96);
  462. }
  463. bool IPIsV4Mapped(const IPAddress& ip) {
  464. return IPIsHelper(ip, kV4MappedPrefix, 96);
  465. }
  466. int IPAddressPrecedence(const IPAddress& ip) {
  467. // Precedence values from RFC 3484-bis. Prefers native v4 over 6to4/Teredo.
  468. if (ip.family() == AF_INET) {
  469. return 30;
  470. } else if (ip.family() == AF_INET6) {
  471. if (IPIsLoopback(ip)) {
  472. return 60;
  473. } else if (IPIsULA(ip)) {
  474. return 50;
  475. } else if (IPIsV4Mapped(ip)) {
  476. return 30;
  477. } else if (IPIs6To4(ip)) {
  478. return 20;
  479. } else if (IPIsTeredo(ip)) {
  480. return 10;
  481. } else if (IPIsV4Compatibility(ip) || IPIsSiteLocal(ip) || IPIs6Bone(ip)) {
  482. return 1;
  483. } else {
  484. // A 'normal' IPv6 address.
  485. return 40;
  486. }
  487. }
  488. return 0;
  489. }
  490. IPAddress GetLoopbackIP(int family) {
  491. if (family == AF_INET) {
  492. return rtc::IPAddress(INADDR_LOOPBACK);
  493. }
  494. if (family == AF_INET6) {
  495. return rtc::IPAddress(in6addr_loopback);
  496. }
  497. return rtc::IPAddress();
  498. }
  499. IPAddress GetAnyIP(int family) {
  500. if (family == AF_INET) {
  501. return rtc::IPAddress(INADDR_ANY);
  502. }
  503. if (family == AF_INET6) {
  504. return rtc::IPAddress(in6addr_any);
  505. }
  506. return rtc::IPAddress();
  507. }
  508. } // namespace rtc