UdpSocket.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 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 <time.h>
  32. #include <errno.h>
  33. #include <sys/types.h>
  34. #include "Constants.hpp"
  35. #include "UdpSocket.hpp"
  36. #include "SocketManager.hpp"
  37. #ifdef __WINDOWS__
  38. #include <WinSock2.h>
  39. #include <WS2tcpip.h>
  40. #include <Windows.h>
  41. #else
  42. #include <unistd.h>
  43. #include <sys/socket.h>
  44. #include <arpa/inet.h>
  45. #include <signal.h>
  46. #endif
  47. // Uncomment to intentionally break UDP in order to test TCP fallback
  48. #define ZT_BREAK_UDP
  49. namespace ZeroTier {
  50. UdpSocket::~UdpSocket()
  51. {
  52. #ifdef __WINDOWS__
  53. ::closesocket(_sock);
  54. #else
  55. ::close(_sock);
  56. #endif
  57. }
  58. bool UdpSocket::send(const InetAddress &to,const void *msg,unsigned int msglen)
  59. {
  60. return sendWithHopLimit(to,msg,msglen,0);
  61. }
  62. bool UdpSocket::sendWithHopLimit(const InetAddress &to,const void *msg,unsigned int msglen,int hopLimit)
  63. {
  64. #ifdef ZT_BREAK_UDP
  65. return true;
  66. #else
  67. if (hopLimit <= 0)
  68. hopLimit = 255;
  69. if (to.isV6()) {
  70. #ifdef __WINDOWS__
  71. DWORD hltmp = (DWORD)hopLimit;
  72. setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,(const char *)&hltmp,sizeof(hltmp));
  73. return ((int)sendto(_sock,(const char *)msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  74. #else
  75. setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,&hopLimit,sizeof(hopLimit));
  76. return ((int)sendto(_sock,msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  77. #endif
  78. } else {
  79. #ifdef __WINDOWS__
  80. DWORD hltmp = (DWORD)hopLimit;
  81. setsockopt(_sock,IPPROTO_IP,IP_TTL,(const char *)&hltmp,sizeof(hltmp));
  82. return ((int)sendto(_sock,(const char *)msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  83. #else
  84. setsockopt(_sock,IPPROTO_IP,IP_TTL,&hopLimit,sizeof(hopLimit));
  85. return ((int)sendto(_sock,msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  86. #endif
  87. }
  88. #endif
  89. }
  90. bool UdpSocket::notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm)
  91. {
  92. Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> buf;
  93. InetAddress from;
  94. socklen_t salen = from.saddrSpaceLen();
  95. int n = (int)recvfrom(_sock,(char *)(buf.data()),ZT_SOCKET_MAX_MESSAGE_LEN,0,from.saddr(),&salen);
  96. if (n > 0) {
  97. buf.setSize((unsigned int)n);
  98. #ifndef ZT_BREAK_UDP
  99. sm->handleReceivedPacket(self,from,buf);
  100. #endif
  101. }
  102. return true;
  103. }
  104. bool UdpSocket::notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm)
  105. {
  106. return true;
  107. }
  108. } // namespace ZeroTier