UdpSocket.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/types.h>
  33. #include <sys/stat.h>
  34. #ifdef _WIN32
  35. #include <Windows.h>
  36. #include <WinSock2.h>
  37. #include <WS2tcpip.h>
  38. #else
  39. #include <sys/socket.h>
  40. #include <arpa/inet.h>
  41. #include <unistd.h>
  42. #include <signal.h>
  43. #endif
  44. #include "UdpSocket.hpp"
  45. #include "RuntimeEnvironment.hpp"
  46. #include "Logger.hpp"
  47. #include "Switch.hpp"
  48. namespace ZeroTier {
  49. UdpSocket::UdpSocket(
  50. bool localOnly,
  51. int localPort,
  52. bool ipv6,
  53. void (*packetHandler)(UdpSocket *,void *,const InetAddress &,const void *,unsigned int),
  54. void *arg)
  55. throw(std::runtime_error) :
  56. _packetHandler(packetHandler),
  57. _arg(arg),
  58. _localPort(localPort),
  59. _sock(0),
  60. _v6(ipv6)
  61. {
  62. #ifdef __WINDOWS__
  63. BOOL yes,no;
  64. #else
  65. int yes,no;
  66. #endif
  67. if ((localPort <= 0)||(localPort > 0xffff))
  68. throw std::runtime_error("port is out of range");
  69. if (ipv6) {
  70. _sock = socket(AF_INET6,SOCK_DGRAM,0);
  71. if (_sock <= 0)
  72. throw std::runtime_error("unable to create IPv6 SOCK_DGRAM socket");
  73. #ifdef __WINDOWS__
  74. yes = TRUE; setsockopt(_sock,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&yes,sizeof(yes));
  75. no = FALSE; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(const char *)&no,sizeof(no));
  76. no = FALSE; setsockopt(_sock,IPPROTO_IPV6,IPV6_DONTFRAG,(const char *)&no,sizeof(no));
  77. #else
  78. yes = 1; setsockopt(_sock,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&yes,sizeof(yes));
  79. no = 0; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(void *)&no,sizeof(no));
  80. #ifdef IP_DONTFRAG
  81. no = 0; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAG,&no,sizeof(no));
  82. #endif
  83. #ifdef IP_MTU_DISCOVER
  84. no = 0; setsockopt(_sock,IPPROTO_IP,IP_MTU_DISCOVER,&no,sizeof(no));
  85. #endif
  86. #ifdef IPV6_MTU_DISCOVER
  87. no = 0; setsockopt(_sock,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&no,sizeof(no));
  88. #endif
  89. #endif
  90. struct sockaddr_in6 sin6;
  91. memset(&sin6,0,sizeof(sin6));
  92. sin6.sin6_family = AF_INET6;
  93. sin6.sin6_port = htons(localPort);
  94. if (localOnly)
  95. memcpy(&(sin6.sin6_addr.s6_addr),InetAddress::LO6.rawIpData(),16);
  96. else memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
  97. if (::bind(_sock,(const struct sockaddr *)&sin6,sizeof(sin6))) {
  98. #ifdef __WINDOWS__
  99. ::closesocket(_sock);
  100. #else
  101. ::close(_sock);
  102. #endif
  103. throw std::runtime_error("unable to bind to port");
  104. }
  105. } else {
  106. _sock = socket(AF_INET,SOCK_DGRAM,0);
  107. if (_sock <= 0)
  108. throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
  109. #ifdef __WINDOWS__
  110. no = FALSE; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(const char *)&no,sizeof(no));
  111. no = FALSE; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAGMENT,(const char *)&no,sizeof(no));
  112. #else
  113. no = 0; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(void *)&no,sizeof(no));
  114. #ifdef IP_DONTFRAG
  115. no = 0; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAG,&no,sizeof(no));
  116. #endif
  117. #ifdef IP_MTU_DISCOVER
  118. no = 0; setsockopt(_sock,IPPROTO_IP,IP_MTU_DISCOVER,&no,sizeof(no));
  119. #endif
  120. #endif
  121. struct sockaddr_in sin;
  122. memset(&sin,0,sizeof(sin));
  123. sin.sin_family = AF_INET;
  124. sin.sin_port = htons(localPort);
  125. if (localOnly)
  126. memcpy(&(sin.sin_addr.s_addr),InetAddress::LO4.rawIpData(),4);
  127. else sin.sin_addr.s_addr = INADDR_ANY;
  128. if (::bind(_sock,(const struct sockaddr *)&sin,sizeof(sin))) {
  129. #ifdef __WINDOWS__
  130. ::closesocket(_sock);
  131. #else
  132. ::close(_sock);
  133. #endif
  134. throw std::runtime_error("unable to bind to port");
  135. }
  136. }
  137. _thread = Thread::start(this);
  138. }
  139. UdpSocket::~UdpSocket()
  140. {
  141. int s = _sock;
  142. _sock = 0;
  143. if (s > 0) {
  144. #ifdef __WINDOWS__
  145. ::shutdown(s,SD_BOTH);
  146. ::closesocket(s);
  147. #else
  148. ::shutdown(s,SHUT_RDWR);
  149. ::close(s);
  150. #endif
  151. }
  152. Thread::join(_thread);
  153. }
  154. bool UdpSocket::send(const InetAddress &to,const void *data,unsigned int len,int hopLimit)
  155. throw()
  156. {
  157. Mutex::Lock _l(_sendLock);
  158. if (to.isV6()) {
  159. if (!_v6)
  160. return false;
  161. #ifdef __WINDOWS__
  162. DWORD hltmp = (DWORD)hopLimit;
  163. setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,(const char *)&hltmp,sizeof(hltmp));
  164. return ((int)sendto(_sock,(const char *)data,len,0,to.saddr(),to.saddrLen()) == (int)len);
  165. #else
  166. setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,&hopLimit,sizeof(hopLimit));
  167. return ((int)sendto(_sock,data,len,0,to.saddr(),to.saddrLen()) == (int)len);
  168. #endif
  169. } else {
  170. if (_v6)
  171. return false;
  172. #ifdef __WINDOWS__
  173. DWORD hltmp = (DWORD)hopLimit;
  174. setsockopt(_sock,IPPROTO_IP,IP_TTL,(const char *)&hltmp,sizeof(hltmp));
  175. return ((int)sendto(_sock,(const char *)data,len,0,to.saddr(),to.saddrLen()) == (int)len);
  176. #else
  177. setsockopt(_sock,IPPROTO_IP,IP_TTL,&hopLimit,sizeof(hopLimit));
  178. return ((int)sendto(_sock,data,len,0,to.saddr(),to.saddrLen()) == (int)len);
  179. #endif
  180. }
  181. }
  182. void UdpSocket::threadMain()
  183. throw()
  184. {
  185. char buf[65536];
  186. InetAddress from;
  187. socklen_t salen;
  188. int n;
  189. while (_sock > 0) {
  190. salen = from.saddrSpaceLen();
  191. n = (int)recvfrom(_sock,buf,sizeof(buf),0,from.saddr(),&salen);
  192. if (n < 0) {
  193. if ((errno != EINTR)&&(errno != ETIMEDOUT))
  194. break;
  195. } else if (n > 0) {
  196. try {
  197. _packetHandler(this,_arg,from,buf,(unsigned int)n);
  198. } catch ( ... ) {} // should never be thrown from here anyway...
  199. }
  200. }
  201. }
  202. } // namespace ZeroTier