UdpSocket.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. _packetHandler(packetHandler),
  54. _arg(arg),
  55. _localPort(localPort),
  56. _sock(0),
  57. _v6(ipv6)
  58. {
  59. int yes,no;
  60. if ((localPort <= 0)||(localPort > 0xffff))
  61. throw std::runtime_error("port is out of range");
  62. if (ipv6) {
  63. _sock = socket(AF_INET6,SOCK_DGRAM,0);
  64. if (_sock <= 0)
  65. throw std::runtime_error("unable to create IPv6 SOCK_DGRAM socket");
  66. yes = 1; setsockopt(_sock,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&yes,sizeof(yes));
  67. no = 0; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(void *)&no,sizeof(no));
  68. #ifdef IP_DONTFRAG
  69. no = 0; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAG,&no,sizeof(no));
  70. #endif
  71. #ifdef IP_MTU_DISCOVER
  72. no = 0; setsockopt(_sock,IPPROTO_IP,IP_MTU_DISCOVER,&no,sizeof(no));
  73. #endif
  74. #ifdef IPV6_MTU_DISCOVER
  75. no = 0; setsockopt(_sock,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&no,sizeof(no));
  76. #endif
  77. struct sockaddr_in6 sin6;
  78. memset(&sin6,0,sizeof(sin6));
  79. sin6.sin6_family = AF_INET6;
  80. sin6.sin6_port = htons(localPort);
  81. if (localOnly)
  82. memcpy(&(sin6.sin6_addr.s6_addr),InetAddress::LO6.rawIpData(),16);
  83. else memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
  84. if (::bind(_sock,(const struct sockaddr *)&sin6,sizeof(sin6))) {
  85. ::close(_sock);
  86. throw std::runtime_error("unable to bind to port");
  87. }
  88. } else {
  89. _sock = socket(AF_INET,SOCK_DGRAM,0);
  90. if (_sock <= 0)
  91. throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
  92. no = 0; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(void *)&no,sizeof(no));
  93. #ifdef IP_DONTFRAG
  94. no = 0; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAG,&no,sizeof(no));
  95. #endif
  96. #ifdef IP_MTU_DISCOVER
  97. no = 0; setsockopt(_sock,IPPROTO_IP,IP_MTU_DISCOVER,&no,sizeof(no));
  98. #endif
  99. struct sockaddr_in sin;
  100. memset(&sin,0,sizeof(sin));
  101. sin.sin_family = AF_INET;
  102. sin.sin_port = htons(localPort);
  103. if (localOnly)
  104. memcpy(&(sin.sin_addr.s_addr),InetAddress::LO4.rawIpData(),4);
  105. else sin.sin_addr.s_addr = INADDR_ANY;
  106. if (::bind(_sock,(const struct sockaddr *)&sin,sizeof(sin))) {
  107. ::close(_sock);
  108. throw std::runtime_error("unable to bind to port");
  109. }
  110. }
  111. _thread = Thread::start(this);
  112. }
  113. UdpSocket::~UdpSocket()
  114. {
  115. int s = _sock;
  116. _sock = 0;
  117. if (s > 0) {
  118. ::shutdown(s,SHUT_RDWR);
  119. ::close(s);
  120. }
  121. Thread::join(_thread);
  122. }
  123. bool UdpSocket::send(const InetAddress &to,const void *data,unsigned int len,int hopLimit)
  124. throw()
  125. {
  126. Mutex::Lock _l(_sendLock);
  127. if (to.isV6()) {
  128. if (!_v6)
  129. return false;
  130. setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,&hopLimit,sizeof(hopLimit));
  131. return ((int)sendto(_sock,data,len,0,to.saddr(),to.saddrLen()) == (int)len);
  132. } else {
  133. if (_v6)
  134. return false;
  135. setsockopt(_sock,IPPROTO_IP,IP_TTL,&hopLimit,sizeof(hopLimit));
  136. return ((int)sendto(_sock,data,len,0,to.saddr(),to.saddrLen()) == (int)len);
  137. }
  138. }
  139. void UdpSocket::threadMain()
  140. throw()
  141. {
  142. char buf[65536];
  143. InetAddress from;
  144. socklen_t salen;
  145. int n;
  146. while (_sock > 0) {
  147. salen = from.saddrSpaceLen();
  148. n = (int)recvfrom(_sock,buf,sizeof(buf),0,from.saddr(),&salen);
  149. if (n < 0) {
  150. if ((errno != EINTR)&&(errno != ETIMEDOUT))
  151. break;
  152. } else if (n > 0) {
  153. try {
  154. _packetHandler(this,_arg,from,buf,(unsigned int)n);
  155. } catch ( ... ) {} // should never be thrown from here anyway...
  156. }
  157. }
  158. }
  159. } // namespace ZeroTier