-
Notifications
You must be signed in to change notification settings - Fork 31
/
peer.c
401 lines (358 loc) · 6.92 KB
/
peer.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
#include <stdio.h>
#ifdef _WIN32
#include <conio.h>
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
// MinGW: Build with -lwsock32
#else
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <termios.h>
#define SOCKET int
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#define closesocket close
#define ioctlsocket ioctl
#define SOCKADDR struct sockaddr
#define SOCKADDR_IN struct sockaddr_in
static void Sleep(u_int msec)
{
struct timespec ts;
ts.tv_sec = msec / 1000;
ts.tv_nsec = (long)(msec % 1000) * 1000000;
nanosleep(&ts, NULL);
}
// kbhit() and getch() for Unix:
static int __conio_initialized;
static struct termios tty, otty;
static void makecooked(void)
{
tcsetattr(0, TCSANOW, &otty);
}
static void sighand(int num)
{
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
signal(SIGHUP, SIG_IGN);
exit(0);
}
static void exitfn(void)
{
if (__conio_initialized)
{
makecooked();
}
printf("\n");
}
static void makeraw(void)
{
static int first_call = 1;
if (first_call)
{
first_call = 0;
if (tcgetattr(0, &tty))
{
// input terminal
fprintf(stderr, "cannot get terminal attributes: %s\n", strerror(errno));
exit(1);
}
otty = tty; // save it
#ifdef NO_MAKERAW
// makeraw code from NetBSD 1.6.2 (/usr/src/lib/libc/termios/cfmakeraw.c)
tty.c_iflag &= ~(IMAXBEL | IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_oflag &= ~OPOST;
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_cflag &= ~(CSIZE | PARENB);
tty.c_cflag |= CS8;
#else
cfmakeraw(&tty);
#endif
tty.c_lflag |= ISIG; // enable signals from ^C etc.
tty.c_oflag |= ONLCR | OPOST; // translate '\n' to '\r\n' on output
}
tcsetattr(0, TCSANOW, &tty);
}
static void __init_conio(void)
{
if (!__conio_initialized)
{
signal(SIGINT, sighand);
signal(SIGTERM, sighand);
signal(SIGHUP, sighand);
atexit(exitfn);
makeraw();
__conio_initialized = 1;
}
}
static void __makeraw(void)
{
if (!__conio_initialized)
{
__init_conio();
}
else
{
makeraw();
}
}
static void __makecooked(void)
{
if (__conio_initialized)
{
makecooked();
}
}
static char getch(void)
{
char c;
__makeraw();
read(0, &c, 1);
return c;
}
static unsigned char kbhit(void)
{
int retval;
fd_set fds;
struct timeval tv;
__makeraw();
again:
FD_ZERO(&fds);
FD_SET(0, &fds);
tv.tv_sec = tv.tv_usec = 0;
retval = select(1, &fds, NULL, NULL, &tv);
if (retval == -1)
{
if (errno == EINTR)
{
goto again;
}
__makecooked();
exit(1);
}
if (FD_ISSET(0, &fds))
{
return 1;
}
return 0;
}
#endif // _WIN32
#define LEN 200
static void dump(unsigned char *buf, unsigned len)
{
unsigned i;
for (i = 0; i < len; ++i)
{
if ((i % 24) == 0)
{
printf("\n$%04X:", i);
}
printf(" %02X", buf[i]);
}
printf(".\n");
}
void main(void)
{
printf("Init\n");
#ifdef _WIN32
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
return;
}
#endif
SOCKET udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (udp == INVALID_SOCKET)
{
return;
}
SOCKET srv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (srv == INVALID_SOCKET)
{
return;
}
u_long arg = 1;
if (ioctlsocket(udp, FIONBIO, &arg) == SOCKET_ERROR)
{
return;
}
if (ioctlsocket(srv, FIONBIO, &arg) == SOCKET_ERROR)
{
return;
}
SOCKADDR_IN local;
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons(6502);
if (bind(udp, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR)
{
return;
}
if (bind(srv, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR)
{
return;
}
if (listen(srv, 1) == SOCKET_ERROR)
{
return;
}
SOCKADDR_IN remote;
remote.sin_addr.s_addr = INADDR_NONE;
SOCKET tcp = INVALID_SOCKET;
printf("(U)DP, (T)CP or e(X)it\n");
char key;
do
{
int len;
unsigned char buf[1500];
if (kbhit())
{
key = getch();
}
else
{
key = '\0';
}
if (key == 'u' || key == 'U')
{
if (remote.sin_addr.s_addr == INADDR_NONE)
{
printf("Peer Unknown As Yet\n");
}
else
{
int i;
len = LEN;
for (i = 0; i < len; ++i)
{
buf[i] = i;
}
printf("Send Len %d To %s", len, inet_ntoa(remote.sin_addr));
if (sendto(udp, buf, len, 0, (SOCKADDR *)&remote, sizeof(remote)) == SOCKET_ERROR)
{
return;
}
printf(".\n");
}
}
if (key == 't' || key == 'T')
{
if (tcp == INVALID_SOCKET)
{
printf("No Connection\n");
}
else
{
int i;
len = LEN;
for (i = 0; i < len; ++i)
{
buf[i] = i;
}
printf("Send Len %d", len);
if (send(tcp, buf, len, 0) == SOCKET_ERROR)
{
return;
}
printf(".\n");
}
}
unsigned remote_size = sizeof(remote);
len = recvfrom(udp, buf, sizeof(buf), 0, (SOCKADDR *)&remote, &remote_size);
if (len == SOCKET_ERROR)
{
#ifdef _WIN32
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
return;
}
#else
if (errno != EAGAIN && errno != EWOULDBLOCK)
{
return;
}
#endif
}
else if (len)
{
printf("Recv Len %d From %s", len, inet_ntoa(remote.sin_addr));
dump(buf, len);
}
if (tcp == INVALID_SOCKET)
{
SOCKADDR_IN conn;
unsigned conn_size = sizeof(conn);
tcp = accept(srv, (SOCKADDR *)&conn, &conn_size);
if (tcp == INVALID_SOCKET)
{
#ifdef _WIN32
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
return;
}
#else
if (errno != EAGAIN && errno != EWOULDBLOCK)
{
return;
}
#endif
}
else
{
printf("Connect From %s\n", inet_ntoa(conn.sin_addr));
u_long arg = 1;
if (ioctlsocket(tcp, FIONBIO, &arg) == SOCKET_ERROR)
{
return;
}
}
}
else
{
len = recv(tcp, buf, sizeof(buf), 0);
if (len == SOCKET_ERROR)
{
#ifdef _WIN32
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
return;
}
#else
if (errno != EAGAIN && errno != EWOULDBLOCK)
{
return;
}
#endif
}
else if (len)
{
printf("Recv Len %d", len);
dump(buf, len);
}
else
{
printf("Disconnect\n");
closesocket(tcp);
tcp = INVALID_SOCKET;
}
}
Sleep(10);
}
while (key != 'x' && key != 'X');
closesocket(udp);
closesocket(tcp);
closesocket(srv);
#ifdef _WIN32
WSACleanup();
#endif
printf("Done\n");
}