UdpSocket.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // This is here so I can commit it to the repo and drive myself insane.
  49. //#define ZT_BREAK_UDP
  50. namespace ZeroTier {
  51. UdpSocket::~UdpSocket()
  52. {
  53. #ifdef __WINDOWS__
  54. ::closesocket(_sock);
  55. #else
  56. ::close(_sock);
  57. #endif
  58. }
  59. bool UdpSocket::send(const InetAddress &to,const void *msg,unsigned int msglen)
  60. {
  61. return sendWithHopLimit(to,msg,msglen,0);
  62. }
  63. bool UdpSocket::sendWithHopLimit(const InetAddress &to,const void *msg,unsigned int msglen,int hopLimit)
  64. {
  65. #ifdef ZT_BREAK_UDP
  66. return true;
  67. #else
  68. if (hopLimit <= 0)
  69. hopLimit = 255;
  70. if (to.isV6()) {
  71. #ifdef __WINDOWS__
  72. DWORD hltmp = (DWORD)hopLimit;
  73. setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,(const char *)&hltmp,sizeof(hltmp));
  74. return ((int)sendto(_sock,(const char *)msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  75. #else
  76. setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,&hopLimit,sizeof(hopLimit));
  77. return ((int)sendto(_sock,msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  78. #endif
  79. } else {
  80. #ifdef __WINDOWS__
  81. DWORD hltmp = (DWORD)hopLimit;
  82. setsockopt(_sock,IPPROTO_IP,IP_TTL,(const char *)&hltmp,sizeof(hltmp));
  83. return ((int)sendto(_sock,(const char *)msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  84. #else
  85. setsockopt(_sock,IPPROTO_IP,IP_TTL,&hopLimit,sizeof(hopLimit));
  86. return ((int)sendto(_sock,msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  87. #endif
  88. }
  89. #endif
  90. }
  91. bool UdpSocket::notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm)
  92. {
  93. Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> buf;
  94. InetAddress from;
  95. socklen_t salen = from.saddrSpaceLen();
  96. int n = (int)recvfrom(_sock,(char *)(buf.data()),ZT_SOCKET_MAX_MESSAGE_LEN,0,from.saddr(),&salen);
  97. if (n > 0) {
  98. buf.setSize((unsigned int)n);
  99. #ifndef ZT_BREAK_UDP
  100. sm->handleReceivedPacket(self,from,buf);
  101. #endif
  102. }
  103. return true;
  104. }
  105. bool UdpSocket::notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm)
  106. {
  107. return true;
  108. }
  109. } // namespace ZeroTier