InetAddress.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include <string>
  33. #include "InetAddress.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. if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf))) {
  59. sprintf(buf2,"%s/%u",buf,(unsigned int)ntohs(_sa.sin.sin_port));
  60. return std::string(buf2);
  61. }
  62. break;
  63. case AF_INET6:
  64. if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf))) {
  65. sprintf(buf2,"%s/%u",buf,(unsigned int)ntohs(_sa.sin6.sin6_port));
  66. return std::string(buf2);
  67. }
  68. break;
  69. }
  70. return std::string();
  71. }
  72. void InetAddress::fromString(const std::string &ipSlashPort)
  73. {
  74. std::size_t slashAt = ipSlashPort.find('/');
  75. if ((slashAt == std::string::npos)||(slashAt >= ipSlashPort.length()))
  76. set(ipSlashPort,0);
  77. else {
  78. long p = strtol(ipSlashPort.substr(slashAt+1).c_str(),(char **)0,10);
  79. if ((p > 0)&&(p <= 0xffff))
  80. set(ipSlashPort.substr(0,slashAt),(unsigned int)p);
  81. else set(ipSlashPort.substr(0,slashAt),0);
  82. }
  83. }
  84. std::string InetAddress::toIpString() const
  85. {
  86. char buf[128];
  87. switch(_sa.saddr.sa_family) {
  88. case AF_INET:
  89. if (inet_ntop(AF_INET,(const void *)&(_sa.sin.sin_addr.s_addr),buf,sizeof(buf)))
  90. return std::string(buf);
  91. break;
  92. case AF_INET6:
  93. if (inet_ntop(AF_INET6,(const void *)&(_sa.sin6.sin6_addr.s6_addr),buf,sizeof(buf)))
  94. return std::string(buf);
  95. break;
  96. }
  97. return std::string();
  98. }
  99. bool InetAddress::operator==(const InetAddress &a) const
  100. throw()
  101. {
  102. if (_sa.saddr.sa_family == AF_INET) {
  103. if (a._sa.saddr.sa_family == AF_INET)
  104. return ((_sa.sin.sin_addr.s_addr == a._sa.sin.sin_addr.s_addr)&&(_sa.sin.sin_port == a._sa.sin.sin_port));
  105. return false;
  106. } else if (_sa.saddr.sa_family == AF_INET6) {
  107. if (a._sa.saddr.sa_family == AF_INET6) {
  108. if (_sa.sin6.sin6_port == a._sa.sin6.sin6_port)
  109. return (!memcmp(_sa.sin6.sin6_addr.s6_addr,a._sa.sin6.sin6_addr.s6_addr,sizeof(_sa.sin6.sin6_addr.s6_addr)));
  110. }
  111. return false;
  112. } else if (!_sa.saddr.sa_family)
  113. return (!a._sa.saddr.sa_family);
  114. return (!memcmp(&_sa,&a._sa,sizeof(_sa)));
  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 ((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))));
  122. else if (a._sa.saddr.sa_family == AF_INET6)
  123. return true;
  124. } else if (_sa.saddr.sa_family == AF_INET6) {
  125. if (a._sa.saddr.sa_family == AF_INET6) {
  126. int cmp = memcmp(_sa.sin6.sin6_addr.s6_addr,a._sa.sin6.sin6_addr.s6_addr,16);
  127. return ((cmp < 0)||((!cmp)&&(ntohs(_sa.sin6.sin6_port) < ntohs(a._sa.sin6.sin6_port))));
  128. } else if (a._sa.saddr.sa_family == AF_INET)
  129. return false;
  130. }
  131. return (_sa.saddr.sa_family < a._sa.saddr.sa_family);
  132. }
  133. } // namespace ZeroTier