InetAddress.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdint.h>
  30. #include <string>
  31. #include "Constants.hpp"
  32. #include "InetAddress.hpp"
  33. #include "Utils.hpp"
  34. namespace ZeroTier {
  35. const InetAddress InetAddress::LO4("127.0.0.1",0);
  36. const InetAddress InetAddress::LO6("::1",0);
  37. void InetAddress::set(const std::string &ip,unsigned int port)
  38. throw()
  39. {
  40. memset(&_sa,0,sizeof(_sa));
  41. if (ip.find(':') != std::string::npos) {
  42. _sa.sin6.sin6_family = AF_INET6;
  43. _sa.sin6.sin6_port = htons((uint16_t)port);
  44. if (inet_pton(AF_INET6,ip.c_str(),(void *)&(_sa.sin6.sin6_addr.s6_addr)) <= 0)
  45. _sa.saddr.sa_family = 0;
  46. } else {
  47. _sa.sin.sin_family = AF_INET;
  48. _sa.sin.sin_port = htons((uint16_t)port);
  49. if (inet_pton(AF_INET,ip.c_str(),(void *)&(_sa.sin.sin_addr.s_addr)) <= 0)
  50. _sa.saddr.sa_family = 0;
  51. }
  52. }
  53. std::string InetAddress::toString() const
  54. {
  55. char buf[128],buf2[128];
  56. switch(_sa.saddr.sa_family) {
  57. case AF_INET:
  58. #ifdef __WINDOWS__
  59. if (inet_ntop(AF_INET,(PVOID)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) {
  60. #else
  61. if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) {
  62. #endif
  63. Utils::snprintf(buf2,sizeof(buf2),"%s/%u",buf,(unsigned int)ntohs(_sa.sin.sin_port));
  64. return std::string(buf2);
  65. }
  66. break;
  67. case AF_INET6:
  68. #ifdef __WINDOWS__
  69. if (inet_ntop(AF_INET6,(PVOID)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) {
  70. #else
  71. if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) {
  72. #endif
  73. Utils::snprintf(buf2,sizeof(buf2),"%s/%u",buf,(unsigned int)ntohs(_sa.sin6.sin6_port));
  74. return std::string(buf2);
  75. }
  76. break;
  77. }
  78. return std::string();
  79. }
  80. void InetAddress::fromString(const std::string &ipSlashPort)
  81. {
  82. std::size_t slashAt = ipSlashPort.find('/');
  83. if ((slashAt == std::string::npos)||(slashAt >= ipSlashPort.length()))
  84. set(ipSlashPort,0);
  85. else {
  86. long p = strtol(ipSlashPort.substr(slashAt+1).c_str(),(char **)0,10);
  87. if ((p > 0)&&(p <= 0xffff))
  88. set(ipSlashPort.substr(0,slashAt),(unsigned int)p);
  89. else set(ipSlashPort.substr(0,slashAt),0);
  90. }
  91. }
  92. std::string InetAddress::toIpString() const
  93. {
  94. char buf[128];
  95. switch(_sa.saddr.sa_family) {
  96. case AF_INET:
  97. #ifdef __WINDOWS__
  98. if (inet_ntop(AF_INET,(PVOID)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf)))
  99. return std::string(buf);
  100. #else
  101. if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf)))
  102. return std::string(buf);
  103. #endif
  104. break;
  105. case AF_INET6:
  106. #ifdef __WINDOWS__
  107. if (inet_ntop(AF_INET6,(PVOID)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf)))
  108. return std::string(buf);
  109. #else
  110. if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf)))
  111. return std::string(buf);
  112. #endif
  113. break;
  114. }
  115. return std::string();
  116. }
  117. bool InetAddress::operator==(const InetAddress &a) const
  118. throw()
  119. {
  120. if (_sa.saddr.sa_family == AF_INET) {
  121. if (a._sa.saddr.sa_family == AF_INET)
  122. return ((_sa.sin.sin_addr.s_addr == a._sa.sin.sin_addr.s_addr)&&(_sa.sin.sin_port == a._sa.sin.sin_port));
  123. return false;
  124. } else if (_sa.saddr.sa_family == AF_INET6) {
  125. if (a._sa.saddr.sa_family == AF_INET6) {
  126. if (_sa.sin6.sin6_port == a._sa.sin6.sin6_port)
  127. return (!memcmp(_sa.sin6.sin6_addr.s6_addr,a._sa.sin6.sin6_addr.s6_addr,sizeof(_sa.sin6.sin6_addr.s6_addr)));
  128. }
  129. return false;
  130. } else if (!_sa.saddr.sa_family)
  131. return (!a._sa.saddr.sa_family);
  132. return (!memcmp(&_sa,&a._sa,sizeof(_sa)));
  133. }
  134. bool InetAddress::operator<(const InetAddress &a) const
  135. throw()
  136. {
  137. if (_sa.saddr.sa_family == AF_INET) {
  138. if (a._sa.saddr.sa_family == AF_INET)
  139. return ((ntohl(_sa.sin.sin_addr.s_addr < ntohl(a._sa.sin.sin_addr.s_addr)))||((_sa.sin.sin_addr.s_addr == a._sa.sin.sin_addr.s_addr)&&(ntohs(_sa.sin.sin_port) < ntohs(a._sa.sin.sin_port))));
  140. else if (a._sa.saddr.sa_family == AF_INET6)
  141. return true;
  142. } else if (_sa.saddr.sa_family == AF_INET6) {
  143. if (a._sa.saddr.sa_family == AF_INET6) {
  144. int cmp = memcmp(_sa.sin6.sin6_addr.s6_addr,a._sa.sin6.sin6_addr.s6_addr,16);
  145. return ((cmp < 0)||((!cmp)&&(ntohs(_sa.sin6.sin6_port) < ntohs(a._sa.sin6.sin6_port))));
  146. } else if (a._sa.saddr.sa_family == AF_INET)
  147. return false;
  148. }
  149. return (_sa.saddr.sa_family < a._sa.saddr.sa_family);
  150. }
  151. } // namespace ZeroTier