root.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  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. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "node/Constants.hpp"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <string.h>
  31. #include <fcntl.h>
  32. #include <signal.h>
  33. #include <sys/stat.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <sys/select.h>
  37. #include <sys/time.h>
  38. #include <sys/un.h>
  39. #include <sys/ioctl.h>
  40. #include <arpa/inet.h>
  41. #include <netinet/in.h>
  42. #include <netinet/ip.h>
  43. #include <netinet/ip6.h>
  44. #include <netinet/tcp.h>
  45. #include <string>
  46. #include <thread>
  47. #include <map>
  48. #include <vector>
  49. #include <iostream>
  50. #include "include/ZeroTierOne.h"
  51. static int bindSocket(struct sockaddr *bindAddr)
  52. {
  53. int s = socket(bindAddr->sa_family,SOCK_DGRAM,0);
  54. if (s < 0) {
  55. close(s);
  56. return -1;
  57. }
  58. int f = 131072;
  59. setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char *)&f,sizeof(f));
  60. f = 131072;
  61. setsockopt(s,SOL_SOCKET,SO_SNDBUF,(const char *)&f,sizeof(f));
  62. if (bindAddr->sa_family == AF_INET6) {
  63. f = 1; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
  64. #ifdef IPV6_MTU_DISCOVER
  65. f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&f,sizeof(f));
  66. #endif
  67. #ifdef IPV6_DONTFRAG
  68. f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_DONTFRAG,&f,sizeof(f));
  69. #endif
  70. }
  71. f = 1; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  72. f = 1; setsockopt(s,SOL_SOCKET,SO_REUSEPORT,(void *)&f,sizeof(f));
  73. f = 1; setsockopt(s,SOL_SOCKET,SO_BROADCAST,(void *)&f,sizeof(f));
  74. #ifdef IP_DONTFRAG
  75. f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
  76. #endif
  77. #ifdef IP_MTU_DISCOVER
  78. f = IP_PMTUDISC_DONT; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
  79. #endif
  80. #ifdef SO_NO_CHECK
  81. if (bindAddr->sa_family == AF_INET) {
  82. f = 1; setsockopt(s,SOL_SOCKET,SO_NO_CHECK,(void *)&f,sizeof(f));
  83. }
  84. #endif
  85. if (bind(s,bindAddr,(bindAddr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6))) {
  86. close(s);
  87. return -1;
  88. }
  89. return s;
  90. }
  91. int main(int argc,char **argv)
  92. {
  93. unsigned int ncores = std::thread::hardware_concurrency();
  94. if (ncores == 0) ncores = 1;
  95. std::vector<int> sockets;
  96. std::vector<std::thread> threads;
  97. for(unsigned int tn=0;tn<ncores;++tn) {
  98. struct sockaddr_in6 in6;
  99. memset(&in6,0,sizeof(in6));
  100. in6.sin6_family = AF_INET6;
  101. in6.sin6_port = htons(ZT_DEFAULT_PORT);
  102. const int s6 = bindSocket((struct sockaddr *)&in6);
  103. if (s6 < 0) {
  104. std::cout << "ERROR: unable to bind to port " << ZT_DEFAULT_PORT << ZT_EOL_S;
  105. exit(1);
  106. }
  107. struct sockaddr_in in4;
  108. memset(&in4,0,sizeof(in4));
  109. in4.sin_family = AF_INET;
  110. in4.sin_port = htons(ZT_DEFAULT_PORT);
  111. const int s4 = bindSocket((struct sockaddr *)&in4);
  112. if (s4 < 0) {
  113. std::cout << "ERROR: unable to bind to port " << ZT_DEFAULT_PORT << ZT_EOL_S;
  114. exit(1);
  115. }
  116. sockets.push_back(s6);
  117. sockets.push_back(s4);
  118. threads.push_back(std::thread([s6]() {
  119. struct sockaddr_in6 in6;
  120. char buf[10000];
  121. memset(&in6,0,sizeof(in6));
  122. for(;;) {
  123. socklen_t sl = sizeof(in6);
  124. const int pl = (int)recvfrom(s6,buf,sizeof(buf),0,(struct sockaddr *)&in6,&sl);
  125. if (pl > 0) {
  126. } else break;
  127. }
  128. }));
  129. threads.push_back(std::thread([s4]() {
  130. struct sockaddr_in in4;
  131. char buf[10000];
  132. memset(&in4,0,sizeof(in4));
  133. for(;;) {
  134. socklen_t sl = sizeof(in4);
  135. const int pl = (int)recvfrom(s4,buf,sizeof(buf),0,(struct sockaddr *)&in4,&sl);
  136. if (pl > 0) {
  137. } else break;
  138. }
  139. }));
  140. }
  141. return 0;
  142. }