splice.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. splice.c -- Splice two outgoing tinc connections together
  3. Copyright (C) 2018 Guus Sliepen <guus@tinc-vpn.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "../../src/system.h"
  17. #ifdef HAVE_WINDOWS
  18. static const char *winerror(int err) {
  19. static char buf[1024], *ptr;
  20. ptr = buf + snprintf(buf, sizeof(buf), "(%d) ", err);
  21. if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  22. NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
  23. strncpy(buf, "(unable to format errormessage)", sizeof(buf));
  24. };
  25. if((ptr = strchr(buf, '\r'))) {
  26. *ptr = '\0';
  27. }
  28. return buf;
  29. }
  30. #define strerror(x) ((x)>0?strerror(x):winerror(GetLastError()))
  31. #define sockerrno WSAGetLastError()
  32. #define sockstrerror(x) winerror(x)
  33. #else
  34. #define sockerrno errno
  35. #define sockstrerror(x) strerror(x)
  36. #endif
  37. int main(int argc, char *argv[]) {
  38. if(argc < 7) {
  39. fprintf(stderr, "Usage: %s name1 host1 port1 name2 host2 port2 [protocol]\n", argv[0]);
  40. return 1;
  41. }
  42. const char *protocol;
  43. if(argc >= 8) {
  44. protocol = argv[7];
  45. } else {
  46. protocol = "17.7";
  47. }
  48. #ifdef HAVE_WINDOWS
  49. static struct WSAData wsa_state;
  50. if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
  51. return 1;
  52. }
  53. #endif
  54. int sock[2];
  55. char buf[1024];
  56. const struct addrinfo hint = {
  57. .ai_family = AF_UNSPEC,
  58. .ai_socktype = SOCK_STREAM,
  59. .ai_protocol = IPPROTO_TCP,
  60. .ai_flags = 0,
  61. };
  62. for(int i = 0; i < 2; i++) {
  63. struct addrinfo *ai;
  64. if(getaddrinfo(argv[2 + 3 * i], argv[3 + 3 * i], &hint, &ai) || !ai) {
  65. fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno));
  66. return 1;
  67. }
  68. sock[i] = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  69. if(sock[i] == -1) {
  70. fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
  71. freeaddrinfo(ai);
  72. return 1;
  73. }
  74. if(connect(sock[i], ai->ai_addr, ai->ai_addrlen)) {
  75. fprintf(stderr, "Could not connect to %s: %s\n", argv[i + 3 * i], sockstrerror(sockerrno));
  76. freeaddrinfo(ai);
  77. return 1;
  78. }
  79. freeaddrinfo(ai);
  80. fprintf(stderr, "Connected to %s\n", argv[1 + 3 * i]);
  81. /* Pretend to be the other one */
  82. int len = snprintf(buf, sizeof buf, "0 %s %s\n", argv[4 - 3 * i], protocol);
  83. if(send(sock[i], buf, len, 0) != len) {
  84. fprintf(stderr, "Error sending data to %s: %s\n", argv[1 + 3 * i], sockstrerror(sockerrno));
  85. return 1;
  86. }
  87. /* Ignore the response */
  88. do {
  89. if(recv(sock[i], buf, 1, 0) != 1) {
  90. fprintf(stderr, "Error reading data from %s: %s\n", argv[1 + 3 * i], sockstrerror(sockerrno));
  91. return 1;
  92. }
  93. } while(*buf != '\n');
  94. }
  95. fprintf(stderr, "Splicing...\n");
  96. int nfds = (sock[0] > sock[1] ? sock[0] : sock[1]) + 1;
  97. while(true) {
  98. fd_set fds;
  99. FD_ZERO(&fds);
  100. FD_SET(sock[0], &fds);
  101. FD_SET(sock[1], &fds);
  102. if(select(nfds, &fds, NULL, NULL, NULL) <= 0) {
  103. return 1;
  104. }
  105. for(int i = 0; i < 2; i++) {
  106. if(FD_ISSET(sock[i], &fds)) {
  107. ssize_t len = recv(sock[i], buf, sizeof buf, 0);
  108. if(len < 0) {
  109. fprintf(stderr, "Error while reading from %s: %s\n", argv[1 + i * 3], sockstrerror(sockerrno));
  110. return 1;
  111. }
  112. if(len == 0) {
  113. fprintf(stderr, "Connection closed by %s\n", argv[1 + i * 3]);
  114. return 0;
  115. }
  116. if(send(sock[i ^ 1], buf, len, 0) != len) {
  117. fprintf(stderr, "Error while writing to %s: %s\n", argv[4 - i * 3], sockstrerror(sockerrno));
  118. return 1;
  119. }
  120. }
  121. }
  122. }
  123. return 0;
  124. }