118d01185f7997753b5ded9d03f4404827d8131f
[tinc] / test / splice.c
1 /*
2     splice.c -- Splice two outgoing tinc connections together
3     Copyright (C) 2018 Guus Sliepen <guus@tinc-vpn.org>
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 2 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 along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "../src/system.h"
21
22 #ifdef HAVE_MINGW
23 static const char *winerror(int err) {
24         static char buf[1024], *ptr;
25
26         ptr = buf + snprintf(buf, sizeof(buf), "(%d) ", err);
27
28         if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
29                           NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
30                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
31         };
32
33         if((ptr = strchr(buf, '\r'))) {
34                 *ptr = '\0';
35         }
36
37         return buf;
38 }
39
40 #define strerror(x) ((x)>0?strerror(x):winerror(GetLastError()))
41 #define sockerrno WSAGetLastError()
42 #define sockstrerror(x) winerror(x)
43 #else
44 #define sockerrno errno
45 #define sockstrerror(x) strerror(x)
46 #endif
47
48 int main(int argc, char *argv[]) {
49         if(argc < 7) {
50                 fprintf(stderr, "Usage: %s name1 host1 port1 name2 host2 port2 [protocol]\n", argv[0]);
51                 return 1;
52         }
53
54         const char *protocol;
55
56         if(argc >= 8) {
57                 protocol = argv[7];
58         } else {
59                 protocol = "17.7";
60         }
61
62 #ifdef HAVE_MINGW
63         static struct WSAData wsa_state;
64
65         if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
66                 return 1;
67         }
68
69 #endif
70         int sock[2];
71         char buf[1024];
72
73         const struct addrinfo hint = {
74                 .ai_family = AF_UNSPEC,
75                 .ai_socktype = SOCK_STREAM,
76                 .ai_protocol = IPPROTO_TCP,
77                 .ai_flags = 0,
78         };
79
80         for(int i = 0; i < 2; i++) {
81                 struct addrinfo *ai;
82
83                 if(getaddrinfo(argv[2 + 3 * i], argv[3 + 3 * i], &hint, &ai) || !ai) {
84                         fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno));
85                         return 1;
86                 }
87
88                 sock[i] = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
89
90                 if(sock[i] == -1) {
91                         fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
92                         freeaddrinfo(ai);
93                         return 1;
94                 }
95
96                 if(connect(sock[i], ai->ai_addr, ai->ai_addrlen)) {
97                         fprintf(stderr, "Could not connect to %s: %s\n", argv[i + 3 * i], sockstrerror(sockerrno));
98                         freeaddrinfo(ai);
99                         return 1;
100                 }
101
102                 freeaddrinfo(ai);
103
104                 fprintf(stderr, "Connected to %s\n", argv[1 + 3 * i]);
105
106                 /* Pretend to be the other one */
107                 int len = snprintf(buf, sizeof buf, "0 %s %s\n", argv[4 - 3 * i], protocol);
108
109                 if(send(sock[i], buf, len, 0) != len) {
110                         fprintf(stderr, "Error sending data to %s: %s\n", argv[1 + 3 * i], sockstrerror(sockerrno));
111                         return 1;
112                 }
113
114                 /* Ignore the response */
115                 do {
116                         if(recv(sock[i], buf, 1, 0) != 1) {
117                                 fprintf(stderr, "Error reading data from %s: %s\n", argv[1 + 3 * i], sockstrerror(sockerrno));
118                                 return 1;
119                         }
120                 } while(*buf != '\n');
121         }
122
123         fprintf(stderr, "Splicing...\n");
124
125         int nfds = (sock[0] > sock[1] ? sock[0] : sock[1]) + 1;
126
127         while(true) {
128                 fd_set fds;
129                 FD_ZERO(&fds);
130                 FD_SET(sock[0], &fds);
131                 FD_SET(sock[1], &fds);
132
133                 if(select(nfds, &fds, NULL, NULL, NULL) <= 0) {
134                         return 1;
135                 }
136
137                 for(int i = 0; i < 2; i++) {
138                         if(FD_ISSET(sock[i], &fds)) {
139                                 ssize_t len = recv(sock[i], buf, sizeof buf, 0);
140
141                                 if(len < 0) {
142                                         fprintf(stderr, "Error while reading from %s: %s\n", argv[1 + i * 3], sockstrerror(sockerrno));
143                                         return 1;
144                                 }
145
146                                 if(len == 0) {
147                                         fprintf(stderr, "Connection closed by %s\n", argv[1 + i * 3]);
148                                         return 0;
149                                 }
150
151                                 if(send(sock[i ^ 1], buf, len, 0) != len) {
152                                         fprintf(stderr, "Error while writing to %s: %s\n", argv[4 - i * 3], sockstrerror(sockerrno));
153                                         return 1;
154                                 }
155                         }
156                 }
157         }
158
159         return 0;
160 }