1
0

UdpSocket.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <stdlib.h>
  30. #include <fcntl.h>
  31. #include <errno.h>
  32. #include <sys/socket.h>
  33. #include <sys/types.h>
  34. #include <arpa/inet.h>
  35. #ifdef _WIN32
  36. #include <Windows.h>
  37. #else
  38. #include <unistd.h>
  39. #include <signal.h>
  40. #endif
  41. #include "UdpSocket.hpp"
  42. #include "RuntimeEnvironment.hpp"
  43. #include "Logger.hpp"
  44. #include "Switch.hpp"
  45. namespace ZeroTier {
  46. UdpSocket::UdpSocket(
  47. bool localOnly,
  48. int localPort,
  49. bool ipv6,
  50. void (*packetHandler)(UdpSocket *,void *,const InetAddress &,const void *,unsigned int),
  51. void *arg)
  52. throw(std::runtime_error) :
  53. Thread(),
  54. _packetHandler(packetHandler),
  55. _arg(arg),
  56. _localPort(localPort),
  57. _sock(0),
  58. _v6(ipv6)
  59. {
  60. int yes,no;
  61. if ((localPort <= 0)||(localPort > 0xffff))
  62. throw std::runtime_error("port is out of range");
  63. if (ipv6) {
  64. _sock = socket(AF_INET6,SOCK_DGRAM,0);
  65. if (_sock <= 0)
  66. throw std::runtime_error("unable to create IPv6 SOCK_DGRAM socket");
  67. yes = 1; setsockopt(_sock,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&yes,sizeof(yes));
  68. no = 0; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(void *)&no,sizeof(no));
  69. #ifdef IP_DONTFRAG
  70. no = 0; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAG,&no,sizeof(no));
  71. #endif
  72. #ifdef IP_MTU_DISCOVER
  73. no = 0; setsockopt(_sock,IPPROTO_IP,IP_MTU_DISCOVER,&no,sizeof(no));
  74. #endif
  75. #ifdef IPV6_MTU_DISCOVER
  76. no = 0; setsockopt(_sock,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&no,sizeof(no));
  77. #endif
  78. struct sockaddr_in6 sin6;
  79. memset(&sin6,0,sizeof(sin6));
  80. sin6.sin6_family = AF_INET6;
  81. sin6.sin6_port = htons(localPort);
  82. if (localOnly)
  83. memcpy(&(sin6.sin6_addr.s6_addr),InetAddress::LO6.rawIpBytes(),16);
  84. else memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
  85. if (::bind(_sock,(const struct sockaddr *)&sin6,sizeof(sin6))) {
  86. ::close(_sock);
  87. throw std::runtime_error("unable to bind to port");
  88. }
  89. } else {
  90. _sock = socket(AF_INET,SOCK_DGRAM,0);
  91. if (_sock <= 0)
  92. throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
  93. no = 0; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(void *)&no,sizeof(no));
  94. #ifdef IP_DONTFRAG
  95. no = 0; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAG,&no,sizeof(no));
  96. #endif
  97. #ifdef IP_MTU_DISCOVER
  98. no = 0; setsockopt(_sock,IPPROTO_IP,IP_MTU_DISCOVER,&no,sizeof(no));
  99. #endif
  100. struct sockaddr_in sin;
  101. memset(&sin,0,sizeof(sin));
  102. sin.sin_family = AF_INET;
  103. sin.sin_port = htons(localPort);
  104. if (localOnly)
  105. memcpy(&(sin.sin_addr.s_addr),InetAddress::LO4.rawIpBytes(),4);
  106. else sin.sin_addr.s_addr = INADDR_ANY;
  107. if (::bind(_sock,(const struct sockaddr *)&sin,sizeof(sin))) {
  108. ::close(_sock);
  109. throw std::runtime_error("unable to bind to port");
  110. }
  111. }
  112. start();
  113. }
  114. UdpSocket::~UdpSocket()
  115. {
  116. close(_sock);
  117. }
  118. bool UdpSocket::send(const InetAddress &to,const void *data,unsigned int len,int hopLimit)
  119. throw()
  120. {
  121. Mutex::Lock _l(_sendLock);
  122. if (to.isV6()) {
  123. if (!_v6)
  124. return false;
  125. setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,&hopLimit,sizeof(hopLimit));
  126. return ((int)sendto(_sock,data,len,0,to.saddr(),to.saddrLen()) == (int)len);
  127. } else {
  128. if (_v6)
  129. return false;
  130. setsockopt(_sock,IPPROTO_IP,IP_TTL,&hopLimit,sizeof(hopLimit));
  131. return ((int)sendto(_sock,data,len,0,to.saddr(),to.saddrLen()) == (int)len);
  132. }
  133. }
  134. void UdpSocket::main()
  135. throw()
  136. {
  137. char buf[32768];
  138. InetAddress from;
  139. socklen_t salen;
  140. int n;
  141. for(;;) {
  142. salen = from.saddrSpaceLen();
  143. n = (int)recvfrom(_sock,buf,sizeof(buf),0,from.saddr(),&salen);
  144. if (n < 0) {
  145. if ((errno != EINTR)&&(errno != ETIMEDOUT))
  146. break;
  147. } else if (n > 0)
  148. _packetHandler(this,_arg,from,buf,(unsigned int)n);
  149. }
  150. }
  151. } // namespace ZeroTier