InetAddress.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. namespace ZeroTier {
  34. const InetAddress InetAddress::LO4("127.0.0.1",0);
  35. const InetAddress InetAddress::LO6("::1",0);
  36. void InetAddress::set(const std::string &ip,unsigned int port)
  37. throw()
  38. {
  39. memset(&_sa,0,sizeof(_sa));
  40. if (ip.find(':') != std::string::npos) {
  41. _sa.sin6.sin6_family = AF_INET6;
  42. _sa.sin6.sin6_port = htons((uint16_t)port);
  43. if (inet_pton(AF_INET6,ip.c_str(),(void *)&(_sa.sin6.sin6_addr.s6_addr)) <= 0)
  44. _sa.saddr.sa_family = 0;
  45. } else {
  46. _sa.sin.sin_family = AF_INET;
  47. _sa.sin.sin_port = htons((uint16_t)port);
  48. if (inet_pton(AF_INET,ip.c_str(),(void *)&(_sa.sin.sin_addr.s_addr)) <= 0)
  49. _sa.saddr.sa_family = 0;
  50. }
  51. }
  52. std::string InetAddress::toString() const
  53. {
  54. char buf[128],buf2[128];
  55. switch(_sa.saddr.sa_family) {
  56. case AF_INET:
  57. #ifdef __WINDOWS__
  58. if (inet_ntop(AF_INET,(PVOID)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) {
  59. #else
  60. if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) {
  61. #endif
  62. sprintf(buf2,"%s/%u",buf,(unsigned int)ntohs(_sa.sin.sin_port));
  63. return std::string(buf2);
  64. }
  65. break;
  66. case AF_INET6:
  67. #ifdef __WINDOWS__
  68. if (inet_ntop(AF_INET6,(PVOID)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) {
  69. #else
  70. if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) {
  71. #endif
  72. sprintf(buf2,"%s/%u",buf,(unsigned int)ntohs(_sa.sin6.sin6_port));
  73. return std::string(buf2);
  74. }
  75. break;
  76. }
  77. return std::string();
  78. }
  79. void InetAddress::fromString(const std::string &ipSlashPort)
  80. {
  81. std::size_t slashAt = ipSlashPort.find('/');
  82. if ((slashAt == std::string::npos)||(slashAt >= ipSlashPort.length()))
  83. set(ipSlashPort,0);
  84. else {
  85. long p = strtol(ipSlashPort.substr(slashAt+1).c_str(),(char **)0,10);
  86. if ((p > 0)&&(p <= 0xffff))
  87. set(ipSlashPort.substr(0,slashAt),(unsigned int)p);
  88. else set(ipSlashPort.substr(0,slashAt),0);
  89. }
  90. }
  91. std::string InetAddress::toIpString() const
  92. {
  93. char buf[128];
  94. switch(_sa.saddr.sa_family) {
  95. case AF_INET:
  96. #ifdef __WINDOWS__
  97. if (inet_ntop(AF_INET,(PVOID)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf)))
  98. return std::string(buf);
  99. #else
  100. if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf)))
  101. return std::string(buf);
  102. #endif
  103. break;
  104. case AF_INET6:
  105. #ifdef __WINDOWS__
  106. if (inet_ntop(AF_INET6,(PVOID)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf)))
  107. return std::string(buf);
  108. #else
  109. if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf)))
  110. return std::string(buf);
  111. #endif
  112. break;
  113. }
  114. return std::string();
  115. }
  116. bool InetAddress::operator==(const InetAddress &a) const
  117. throw()
  118. {
  119. if (_sa.saddr.sa_family == AF_INET) {
  120. if (a._sa.saddr.sa_family == AF_INET)
  121. return ((_sa.sin.sin_addr.s_addr == a._sa.sin.sin_addr.s_addr)&&(_sa.sin.sin_port == a._sa.sin.sin_port));
  122. return false;
  123. } else if (_sa.saddr.sa_family == AF_INET6) {
  124. if (a._sa.saddr.sa_family == AF_INET6) {
  125. if (_sa.sin6.sin6_port == a._sa.sin6.sin6_port)
  126. return (!memcmp(_sa.sin6.sin6_addr.s6_addr,a._sa.sin6.sin6_addr.s6_addr,sizeof(_sa.sin6.sin6_addr.s6_addr)));
  127. }
  128. return false;
  129. } else if (!_sa.saddr.sa_family)
  130. return (!a._sa.saddr.sa_family);
  131. return (!memcmp(&_sa,&a._sa,sizeof(_sa)));
  132. }
  133. bool InetAddress::operator<(const InetAddress &a) const
  134. throw()
  135. {
  136. if (_sa.saddr.sa_family == AF_INET) {
  137. if (a._sa.saddr.sa_family == AF_INET)
  138. 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))));
  139. else if (a._sa.saddr.sa_family == AF_INET6)
  140. return true;
  141. } else if (_sa.saddr.sa_family == AF_INET6) {
  142. if (a._sa.saddr.sa_family == AF_INET6) {
  143. int cmp = memcmp(_sa.sin6.sin6_addr.s6_addr,a._sa.sin6.sin6_addr.s6_addr,16);
  144. return ((cmp < 0)||((!cmp)&&(ntohs(_sa.sin6.sin6_port) < ntohs(a._sa.sin6.sin6_port))));
  145. } else if (a._sa.saddr.sa_family == AF_INET)
  146. return false;
  147. }
  148. return (_sa.saddr.sa_family < a._sa.saddr.sa_family);
  149. }
  150. } // namespace ZeroTier