UdpSocket.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifdef ZT_BREAK_UDP
  62. return true;
  63. #else
  64. if (to.isV6()) {
  65. #ifdef __WINDOWS__
  66. return ((int)sendto(_sock,(const char *)msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  67. #else
  68. return ((int)sendto(_sock,msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  69. #endif
  70. } else {
  71. #ifdef __WINDOWS__
  72. return ((int)sendto(_sock,(const char *)msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  73. #else
  74. return ((int)sendto(_sock,msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  75. #endif
  76. }
  77. #endif
  78. }
  79. bool UdpSocket::notifyAvailableForRead(const SharedPtr<Socket> &self,SocketManager *sm)
  80. {
  81. Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> buf;
  82. InetAddress from;
  83. socklen_t salen = from.saddrSpaceLen();
  84. int n = (int)recvfrom(_sock,(char *)(buf.data()),ZT_SOCKET_MAX_MESSAGE_LEN,0,from.saddr(),&salen);
  85. if (n > 0) {
  86. buf.setSize((unsigned int)n);
  87. #ifndef ZT_BREAK_UDP
  88. sm->handleReceivedPacket(self,from,buf);
  89. #endif
  90. }
  91. return true;
  92. }
  93. bool UdpSocket::notifyAvailableForWrite(const SharedPtr<Socket> &self,SocketManager *sm)
  94. {
  95. return true;
  96. }
  97. } // namespace ZeroTier