UdpSocket.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. int localPort,
  48. bool ipv6,
  49. void (*packetHandler)(UdpSocket *,void *,const InetAddress &,const void *,unsigned int),
  50. void *arg)
  51. throw(std::runtime_error) :
  52. Thread(),
  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. memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
  82. if (::bind(_sock,(const struct sockaddr *)&sin6,sizeof(sin6))) {
  83. ::close(_sock);
  84. throw std::runtime_error("unable to bind to port");
  85. }
  86. } else {
  87. _sock = socket(AF_INET,SOCK_DGRAM,0);
  88. if (_sock <= 0)
  89. throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
  90. no = 0; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(void *)&no,sizeof(no));
  91. #ifdef IP_DONTFRAG
  92. no = 0; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAG,&no,sizeof(no));
  93. #endif
  94. #ifdef IP_MTU_DISCOVER
  95. no = 0; setsockopt(_sock,IPPROTO_IP,IP_MTU_DISCOVER,&no,sizeof(no));
  96. #endif
  97. struct sockaddr_in sin;
  98. memset(&sin,0,sizeof(sin));
  99. sin.sin_family = AF_INET;
  100. sin.sin_port = htons(localPort);
  101. sin.sin_addr.s_addr = INADDR_ANY;
  102. if (::bind(_sock,(const struct sockaddr *)&sin,sizeof(sin))) {
  103. ::close(_sock);
  104. throw std::runtime_error("unable to bind to port");
  105. }
  106. }
  107. start();
  108. }
  109. UdpSocket::~UdpSocket()
  110. {
  111. close(_sock);
  112. }
  113. bool UdpSocket::send(const InetAddress &to,const void *data,unsigned int len,int hopLimit)
  114. throw()
  115. {
  116. Mutex::Lock _l(_sendLock);
  117. if (to.isV6()) {
  118. if (!_v6)
  119. return false;
  120. setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,&hopLimit,sizeof(hopLimit));
  121. return ((int)sendto(_sock,data,len,0,to.saddr(),to.saddrLen()) == (int)len);
  122. } else {
  123. if (_v6)
  124. return false;
  125. setsockopt(_sock,IPPROTO_IP,IP_TTL,&hopLimit,sizeof(hopLimit));
  126. return ((int)sendto(_sock,data,len,0,to.saddr(),to.saddrLen()) == (int)len);
  127. }
  128. }
  129. void UdpSocket::main()
  130. throw()
  131. {
  132. char buf[32768];
  133. InetAddress from;
  134. socklen_t salen;
  135. int n;
  136. for(;;) {
  137. salen = from.saddrSpaceLen();
  138. n = (int)recvfrom(_sock,buf,sizeof(buf),0,from.saddr(),&salen);
  139. if (n < 0) {
  140. if ((errno != EINTR)&&(errno != ETIMEDOUT))
  141. break;
  142. } else if (n > 0)
  143. _packetHandler(this,_arg,from,buf,(unsigned int)n);
  144. }
  145. }
  146. } // namespace ZeroTier