-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror.c
431 lines (373 loc) · 11 KB
/
mirror.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/select.h>
#include <signal.h>
#define DEBUG
#define H_TEXT "mirror [-t|-u] [-s|-c] dst-addr port\n" \
"\n" \
"Parameters:\n" \
"-t TCP\n" \
"-u UDP\n" \
"-h This help text\n\n"
#define BUF_LEN 1024
#define DEBUG_NTOH(naddr,nport, msg, dir) { \
unsigned int server_addr_h = ntohl(naddr); \
unsigned short server_port_h = ntohs(nport); \
printf("message '%s' - %s: %d.%d.%d.%d:%d\n", \
msg, \
dir, \
server_addr_h >> 24 & 255, \
server_addr_h >> 16 & 255, \
server_addr_h >> 8 & 255, \
server_addr_h >> 0 & 255, \
server_port_h \
); \
}
void helptext();
void readargs(int argc, char *argv[], char *proto, char *mode, unsigned int *address, unsigned short *port, char * default_port);
void udp_server(const unsigned short port);
void tcp_server(const unsigned short port);
void udp_client(const unsigned int addr, const unsigned short port, int fd);
void tcp_client(const unsigned int addr, const unsigned short port, int fd);
void chomp(char *s);
int main(int argc, char *argv[]) {
char proto, mode;
unsigned int addr;
unsigned short port;
//read arguments (options)
readargs(argc, argv, &proto, &mode, &addr, &port, "10001");
if(mode == 's') {
if(proto == 'u') {
udp_server(port);
} else {
tcp_server(port);
}
} else {
if(proto == 'u') {
udp_client(addr, port, 0);
} else {
tcp_client(addr, port, 0);
}
}
return 0;
}
/*
* Read args - -t/-u necessary, -p xxxx, if not default port 10001
* -c destination
*/
void readargs(int argc, char *argv[], char *proto, char *mode, unsigned int *address, unsigned short *port, char * default_port) {
char c; //for getopt
int protocount = 0, modecount = 0; //count how often a proto occurs, must be 1
char *caddr = NULL; //character array for address
char *cport = default_port; //character array for port
unsigned int ip[4]; //helper that stores temp ip parts
while( (c = getopt(argc, argv, "tuhsc:p:")) != -1) {
switch(c) {
case 't':
*proto = 't';
protocount++;
break;
case 'u':
*proto = 'u';
protocount++;
break;
case 's':
*mode = 's';
modecount++;
break;
case 'c':
*mode = 'c';
caddr = optarg;
if ( sscanf(caddr, "%u.%u.%u.%u",
&ip[0],
&ip[1],
&ip[2],
&ip[3]) == EOF
) {
fprintf(stderr, "Not a valid IP address\n");
}
*address = ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3];
modecount++;
break;
case 'p':
cport = optarg;
break;
case 'h':
helptext();
break;
default:
break;
}
}
//convert port to int
*port = (unsigned short) atoi(cport);
if(*port < 10000) {
fprintf(stderr, "Port %d not allowed. Needs to be >= 10000\n", *port);
exit(1);
}
//make sure that either protocol is specified
if(protocount != 1) {
fprintf(stderr, "Please specify either TCP (-t), or UDP (-u)\n");
exit(1);
}
//make sure that either server or client is specified
if(modecount != 1) {
fprintf(stderr, "Please specify either server (-s), or client (-c) mode\n");
exit(1);
}
}
/*
* Shows the help text
*/
void helptext() {
printf(H_TEXT);
exit(1);
}
/*
* Create a UDP server
*/
void udp_server(const unsigned short port) {
printf("creating udp server on port %d\n", port);
int sock, addrlen;
char buf[BUF_LEN];
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
unsigned int client_addr_size;
unsigned short nport = htons(port); //port in network byte order
server_addr.sin_family = AF_INET;
server_addr.sin_port = nport;
server_addr.sin_addr.s_addr = INADDR_ANY;
if( (sock = socket(AF_INET, SOCK_DGRAM, 0) ) == - 1) { //open a UDP socket
perror("Creating socket");
exit(1);
}
if( bind(sock, (const struct sockaddr *) &server_addr, sizeof(server_addr)) == -1 ) {
perror("Binding socket to name");
exit(1);
}
for( ;; ) { //run until break
bzero(buf, sizeof(buf));
bzero(&client_addr, sizeof(client_addr));
client_addr_size = sizeof(client_addr);
addrlen = recvfrom(sock, buf, sizeof(buf), 0,
(struct sockaddr *) &client_addr, &client_addr_size
);
if( addrlen == -1 ) {
perror("receiving message");
exit(1);
}
chomp(buf);
#ifdef DEBUG
printf("<=== ");
DEBUG_NTOH(client_addr.sin_addr.s_addr, client_addr.sin_port, buf, "from host");
printf("<=== ");
DEBUG_NTOH(server_addr.sin_addr.s_addr, server_addr.sin_port, buf, "to host");
printf("===> ");
DEBUG_NTOH(server_addr.sin_addr.s_addr, server_addr.sin_port, buf, "from host");
printf("===> ");
DEBUG_NTOH(client_addr.sin_addr.s_addr, client_addr.sin_port, buf, "to host");
printf("\n");
#endif
//send back
if( sendto(
sock,
buf,
sizeof(buf),
0,
(const struct sockaddr *) &client_addr,
sizeof(client_addr)
) == -1 ) {
perror("mirroring message");
exit(1);
}
}
close(sock);
}
/*
* Create a TCP server
*/
void tcp_server(const unsigned short port) {
printf("creating tcp server on port %d\n", port);
int sock; //original, listening socket
int acc_sock; //accepted socket
unsigned short nport = htons(port);
struct sockaddr_in server_address;
unsigned int server_address_l = sizeof(server_address);
struct sockaddr_in client_address;
unsigned int client_address_l = sizeof(client_address);
char buf[BUF_LEN];
if( (sock = socket(AF_INET, SOCK_STREAM, 0) ) == -1) {
perror("Opening TCP socket");
exit(1);
}
server_address.sin_family = AF_INET;
server_address.sin_port = nport;
server_address.sin_addr.s_addr = INADDR_ANY;
if( bind(sock, (const struct sockaddr *) &server_address, server_address_l) == -1 ) {
perror("Binding socket");
exit(1);
}
if( listen(sock, 5) == -1 ) {
perror("Listening on socket");
exit(1);
}
/********************/
/* loop until break */
/********************/
for(;;) {
if( (acc_sock = accept(sock, (struct sockaddr *) &client_address, &client_address_l)) == -1 ) {
perror("Answering socket");
exit(1);
}
#ifdef DEBUG
printf("Socket opened - client info: \n");
DEBUG_NTOH(client_address.sin_addr.s_addr, client_address.sin_port, "", "-");
printf("Socket opened - server info: \n ");
DEBUG_NTOH(server_address.sin_addr.s_addr, server_address.sin_port, "", "-");
printf("\n");
#endif
bzero(buf, BUF_LEN); // reset buffer to 0
if( read(sock, buf, BUF_LEN) == -1 ) {
perror("Reading from client");
exit(1);
}
chomp(buf);
printf("%s\n", buf);
close(acc_sock);
}
/*******************/
/* end of for loop */
/*******************/
close(sock);
}
/*
* Create a UDP client
*/
void udp_client(const unsigned int addr, const unsigned short port, int fd) {
int sock;
//int addrlen;
char buf[BUF_LEN];
struct sockaddr_in server_addr;
unsigned int server_addr_size = sizeof(server_addr);
struct sockaddr_in client_addr;
unsigned int client_addr_size = sizeof(client_addr);
unsigned int naddr = htonl(addr);
unsigned short nport = htons(port); //port in network byte order
server_addr.sin_family = AF_INET;
server_addr.sin_port = nport;
server_addr.sin_addr.s_addr = naddr;
if( (sock = socket(AF_INET, SOCK_DGRAM, 0) ) == - 1) { //open a UDP socket
perror("Creating socket");
exit(1);
}
for(;;) {
bzero(buf, sizeof(buf));
if( read(fd, buf, sizeof(buf)) == -1 ) {
printf("reading input");
exit(1);
}
if( sendto(sock, buf, sizeof(buf), 0, (const struct sockaddr *) &server_addr, server_addr_size) == -1 ) {
perror("Sending message to server");
fprintf(stderr, "%d\n", errno);
fprintf(stderr, "%x\n", server_addr.sin_addr.s_addr);
exit(1);
}
if( getsockname(sock, (struct sockaddr *) &client_addr, &client_addr_size) == -1 ) {
perror("getting name of socket\n");
exit(1);
}
#ifdef DEBUG
chomp(buf);
printf("===> ");
DEBUG_NTOH(client_addr.sin_addr.s_addr, client_addr.sin_port,buf,"from host");
printf("===> ");
DEBUG_NTOH(naddr, nport,buf,"to host");
#endif
/*****************************************************************/
/* fork a child process so that the user can continue with input */
/*****************************************************************/
int pid;
signal(SIGCHLD, SIG_IGN); // ignore signals from children => avoid defunct processes
if( (pid = fork()) == -1 ) {
perror("Forking child process");
exit(1);
} else if( pid == 0 ) { //child
/****************************/
/* set timeout for recvfrom */
/****************************/
struct timeval tv;
tv.tv_sec = 5; //max 5 sec
tv.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof(struct timeval));
bzero(buf, sizeof(buf));
//now, receive
if( recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &server_addr, &server_addr_size) == -1 ) {
perror("Receiving answer message");
exit(1);
}
#ifdef DEBUG
printf("<=== ");
DEBUG_NTOH(server_addr.sin_addr.s_addr, server_addr.sin_port, buf, "from host");
printf("<=== ");
DEBUG_NTOH(client_addr.sin_addr.s_addr, client_addr.sin_port, buf, "to host");
#endif
exit(0);
} //end of forked child
} //end of loop
close(sock);
}
/*
* Create a TCP client
*/
void tcp_client(const unsigned int addr, const unsigned short port, int fd) {
printf("creating tcp client, dst %d.%d.%d.%d and dport %d\n", addr >> 24, (addr >> 16) & 255, (addr >> 8) & 255,addr & 255, port);
unsigned int naddr = htonl(addr);
unsigned short nport = htons(port);
int sock;
struct sockaddr_in client_address;
int client_address_l = sizeof(client_address);
struct sockaddr_in server_address;
int server_address_l = sizeof(server_address);
server_address.sin_family = AF_INET;
server_address.sin_port = nport;
server_address.sin_addr.s_addr = naddr;
char buf[BUF_LEN];
int char_count;
if( (sock = socket(AF_INET, SOCK_STREAM,0)) == -1 ) {
perror("Creating TCP socket");
exit(1);
}
if( connect(sock, (const struct sockaddr *) &server_address, server_address_l) == -1 ) {
perror("Connection to server");
exit(1);
}
//run until break
for(;;) {
bzero(buf, BUF_LEN);
if( (char_count = read(fd, buf, BUF_LEN)) == -1 ) {
perror("Reading from file descriptor into buffer");
exit(1);
}
chomp(buf);
if( send(sock, buf, char_count, 0) == -1 ) {
perror("Sending message to server");
exit(1);
}
printf("%s\n",buf);
}
sleep(20);
close(sock);
}
void chomp (char* s) {
int end = strlen(s) - 1;
if (end >= 0 && s[end] == '\n')
s[end] = '\0';
}