UdpSocket.cpp 3.2 KB

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