UdpSocket.cpp 6.1 KB

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