UdpSocket.cpp 6.5 KB

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