-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_udp.c
557 lines (436 loc) · 13.9 KB
/
net_udp.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// net_udp.c
#include "quakedef.h"
#include "unixquake.h"
#include "net_udp.h"
// socket for fielding new connections
static sys_socket_t net_acceptsocket = INVALID_SOCKET;
static sys_socket_t net_controlsocket;
static sys_socket_t net_broadcastsocket = 0;
static struct sockaddr_in broadcastaddr;
/*
There are three addresses that we may use in different ways:
myAddr - This is the "default" address returned by the OS
localAddr - This is an address to advertise in CCREP_SERVER_INFO
and CCREP_ACCEPT response packets, rather than the
default address (sometimes the default address is not
suitable for LAN clients; i.e. loopback address). Set
on the command line using the "-localip" option.
bindAddr - The address to which we bind our network socket. The
default is INADDR_ANY, but in some cases we may want
to only listen on a particular address. Set on the
command line using the "-ip" option.
*/
static struct in_addr myAddr;
static struct in_addr localAddr;
static struct in_addr bindAddr;
static char ifname[IFNAMSIZ];
//=============================================================================
static int udp_scan_iface(sys_socket_t net_socket)
{
struct ifconf ifc;
struct ifreq *ifr;
char buf[8192];
int i, n;
struct sockaddr_in *iaddr;
struct in_addr addr;
if (COM_CheckParm("-noifscan"))
return -1;
ifc.ifc_len = sizeof (buf);
ifc.ifc_buf = buf;
if (ioctl(net_socket, SIOCGIFCONF, &ifc) == -1)
{
Con_Printf("udp_scan_iface: SIOCGIFCONF failed\n");
return -1;
}
ifr = ifc.ifc_req;
n = ifc.ifc_len / sizeof(struct ifreq);
for (i = 0; i < n; i++)
{
if (ioctl(net_socket, SIOCGIFADDR, &ifr[i]) == -1)
continue;
iaddr = (struct sockaddr_in *)&ifr[i].ifr_addr;
Con_DPrintf("%s: %s\n", ifr[i].ifr_name, inet_ntoa(iaddr->sin_addr));
addr.s_addr = iaddr->sin_addr.s_addr;
if (addr.s_addr != htonl(INADDR_LOOPBACK))
{
myAddr.s_addr = addr.s_addr;
strcpy(ifname, ifr[i].ifr_name);
return 0;
}
}
return -1;
}
sys_socket_t UDP_Init (void)
{
int i, err;
struct hostent *local;
char buff[MAXHOSTNAMELEN];
struct qsockaddr addr;
char *colon;
if (COM_CheckParm ("-noudp"))
return INVALID_SOCKET;
// determine my name & address
myAddr.s_addr = htonl(INADDR_LOOPBACK);
err = gethostname(buff, MAXHOSTNAMELEN);
if (err)
{
Con_SafePrintf("UDP_Init: gethostname failed (errno: %i)\n", errno);
}
else
{
buff[MAXHOSTNAMELEN - 1] = 0;
local = gethostbyname(buff);
if (!local)
{
Con_Warning("UDP_Init: gethostbyname failed (errno: %i)\n", errno);
}
else if (local->h_addrtype != AF_INET)
{
Con_Printf("UDP_Init: address from gethostbyname not IPv4\n");
}
else
{
myAddr = *(struct in_addr *)local->h_addr_list[0];
}
}
i = COM_CheckParm("-ip");
if (i && i < com_argc - 1)
{
bindAddr.s_addr = inet_addr(com_argv[i + 1]);
if (bindAddr.s_addr == htonl(INADDR_NONE))
Sys_Error("UDP_Init: %s is not a valid IP address", com_argv[i + 1]);
Con_Printf("Binding to IP Interface Address of %s\n", com_argv[i + 1]);
}
else
{
bindAddr.s_addr = htonl(INADDR_NONE);
}
i = COM_CheckParm("-localip");
if (i && i < com_argc - 1)
{
localAddr.s_addr = inet_addr(com_argv[i + 1]);
if (localAddr.s_addr == htonl(INADDR_NONE))
Sys_Error("UDP_Init: %s is not a valid IP address", com_argv[i + 1]);
Con_Printf("Advertising %s as the local IP in response packets\n", com_argv[i + 1]);
}
else
{
localAddr.s_addr = htonl(INADDR_NONE);
}
net_controlsocket = UDP_OpenSocket(0);
if (net_controlsocket == INVALID_SOCKET)
{
Con_Printf("UDP_Init: Unable to open control socket, UDP disabled\n");
return INVALID_SOCKET;
}
// myAddr may resolve to 127.0.0.1, see if we can do any better
memset (ifname, 0, sizeof(ifname));
if (myAddr.s_addr == htonl(INADDR_LOOPBACK))
{
if (udp_scan_iface(net_controlsocket) == 0)
Con_Printf ("UDP, Local address: %s (%s)\n", inet_ntoa(myAddr), ifname);
}
if (ifname[0] == 0)
{
Con_Printf ("UDP, Local address: %s\n", inet_ntoa(myAddr));
}
broadcastaddr.sin_family = AF_INET;
broadcastaddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
broadcastaddr.sin_port = htons((unsigned short)net_hostport);
UDP_GetSocketAddr (net_controlsocket, &addr);
strcpy(my_tcpip_address, UDP_AddrToString (&addr));
colon = strrchr (my_tcpip_address, ':');
if (colon)
*colon = 0;
Con_Printf("UDP initialized (%s)\n", my_tcpip_address);
tcpipAvailable = true;
return net_controlsocket;
}
//=============================================================================
void UDP_Shutdown (void)
{
UDP_Listen (false);
UDP_CloseSocket (net_controlsocket);
}
//=============================================================================
void UDP_Listen (qboolean state)
{
// enable listening
if (state)
{
if (net_acceptsocket != INVALID_SOCKET)
return;
if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == INVALID_SOCKET)
Sys_Error ("UDP_Listen: Unable to open accept socket");
return;
}
// disable listening
if (net_acceptsocket == INVALID_SOCKET)
return;
UDP_CloseSocket (net_acceptsocket);
net_acceptsocket = INVALID_SOCKET;
}
//=============================================================================
sys_socket_t UDP_OpenSocket (int port)
{
sys_socket_t newsocket;
struct sockaddr_in address;
unsigned long _true = 1; // was int
if ((newsocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
return INVALID_SOCKET;
if (ioctl(newsocket, FIONBIO, &_true) == SOCKET_ERROR)
goto ErrorReturn;
address.sin_family = AF_INET;
if (bindAddr.s_addr != htonl(INADDR_NONE))
address.sin_addr.s_addr = bindAddr.s_addr;
else
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons((unsigned short)port);
if (bind(newsocket, (struct sockaddr *)&address, sizeof(address)) == -1)
goto ErrorReturn;
else
return newsocket;
ErrorReturn:
if (tcpipAvailable)
Sys_Error("UDP_OpenSocket: Unable to bind to %s", UDP_AddrToString((struct qsockaddr *)&address));
else // we are still in init phase, no need to error
Con_Printf("UDP_OpenSocket: Unable to bind to %s\n", UDP_AddrToString((struct qsockaddr *)&address));
UDP_CloseSocket (newsocket);
return INVALID_SOCKET;
}
//=============================================================================
int UDP_CloseSocket (sys_socket_t net_socket)
{
if (net_socket == net_broadcastsocket)
net_broadcastsocket = 0;
return close(net_socket);
}
//=============================================================================
/*
============
PartialIPAddress
this lets you type only as much of the net address as required, using
the local network components to fill in the rest
============
*/
static int PartialIPAddress (char *in, struct qsockaddr *hostaddr)
{
char buff[256];
char *b;
int addr;
int num;
int mask;
int run;
int port;
buff[0] = '.';
b = buff;
strcpy(buff + 1, in);
if (buff[1] == '.')
b++;
addr = 0;
mask = -1;
while (*b == '.')
{
b++;
num = 0;
run = 0;
while (!( *b < '0' || *b > '9'))
{
num = num * 10 + *b++ - '0';
if (++run > 3)
return -1;
}
if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
return -1;
if (num < 0 || num > 255)
return -1;
mask <<= 8;
addr = (addr << 8) + num;
}
if (*b++ == ':')
port = atoi(b);
else
port = net_hostport;
hostaddr->qsa_family = AF_INET;
((struct sockaddr_in *)hostaddr)->sin_port = htons((unsigned short)port);
((struct sockaddr_in *)hostaddr)->sin_addr.s_addr = (myAddr.s_addr & htonl(mask)) | htonl(addr);
return 0;
}
//=============================================================================
sys_socket_t UDP_CheckNewConnections (void)
{
char buf[4096];
int ret;
if (net_acceptsocket == INVALID_SOCKET)
return INVALID_SOCKET;
// ProQuake fix
ret = (int)recvfrom(net_acceptsocket, buf, sizeof(buf), MSG_PEEK, NULL, NULL);
if (ret != SOCKET_ERROR)
return net_acceptsocket;
return INVALID_SOCKET;
}
//=============================================================================
int UDP_Read (sys_socket_t net_socket, byte *buf, int len, struct qsockaddr *addr)
{
socklen_t addrlen = sizeof(struct qsockaddr);
int ret;
ret = (int)recvfrom(net_socket, (char *)buf, len, 0, (struct sockaddr *)addr, &addrlen);
if (ret == SOCKET_ERROR)
{
if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
return 0;
}
return ret;
}
//=============================================================================
int UDP_MakeSocketBroadcastCapable (sys_socket_t net_socket)
{
int i = 1;
// make this socket broadcast capable
if (setsockopt(net_socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) == SOCKET_ERROR)
return -1;
net_broadcastsocket = net_socket;
return 0;
}
//=============================================================================
int UDP_Broadcast (sys_socket_t net_socket, byte *buf, int len)
{
int ret;
if (net_socket != net_broadcastsocket)
{
if (net_broadcastsocket != 0)
Sys_Error("UDP_Broadcast: Attempted to use multiple broadcasts sockets");
ret = UDP_MakeSocketBroadcastCapable (net_socket);
if (ret == -1)
{
Con_Printf("Unable to make socket broadcast capable\n");
return ret;
}
}
return UDP_Write (net_socket, buf, len, (struct qsockaddr *)&broadcastaddr);
}
//=============================================================================
int UDP_Write (sys_socket_t net_socket, byte *buf, int len, struct qsockaddr *addr)
{
int ret;
ret = (int)sendto(net_socket, (char *)buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
if (ret == SOCKET_ERROR)
if (errno == EWOULDBLOCK)
return 0;
return ret;
}
//=============================================================================
char *UDP_AddrToString (struct qsockaddr *addr)
{
static char buffer[22];
int haddr;
haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
sprintf(buffer, "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff, (haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff, ntohs(((struct sockaddr_in *)addr)->sin_port));
return buffer;
}
//=============================================================================
int UDP_StringToAddr (char *string, struct qsockaddr *addr)
{
int ha1, ha2, ha3, ha4, hp;
int ipaddr;
sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
addr->qsa_family = AF_INET;
((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp);
return 0;
}
//=============================================================================
int UDP_GetSocketAddr (sys_socket_t net_socket, struct qsockaddr *addr)
{
struct sockaddr_in *address = (struct sockaddr_in *)addr;
socklen_t addrlen = sizeof(struct qsockaddr);
struct in_addr a;
memset(addr, 0, sizeof(struct qsockaddr));
getsockname(net_socket, (struct sockaddr *)addr, &addrlen);
/*
The returned IP is embedded in our response to a broadcast request for
server info from clients. The server admin may wish to advertise a
specific IP for various reasons, so allow the "default" address
returned by the OS to be overridden.
*/
if (localAddr.s_addr != htonl(INADDR_NONE))
address->sin_addr.s_addr = localAddr.s_addr;
else
{
a = address->sin_addr;
if (!a.s_addr || a.s_addr == htonl(INADDR_LOOPBACK))
address->sin_addr.s_addr = myAddr.s_addr;
}
return 0;
}
//=============================================================================
int UDP_GetNameFromAddr (struct qsockaddr *addr, char *name)
{
/* ProQuake connect speedup fix (verified for w95 -> XP) */
/* // pq comment out this block
struct hostent *hostentry;
hostentry = gethostbyaddr((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
if (hostentry)
{
strncpy(name, (char *)hostentry->h_name, NET_NAMELEN - 1);
return 0;
}
*/
strcpy(name, UDP_AddrToString (addr));
return 0;
}
//=============================================================================
int UDP_GetAddrFromName(char *name, struct qsockaddr *addr)
{
struct hostent *hostentry;
if (name[0] >= '0' && name[0] <= '9')
return PartialIPAddress (name, addr);
hostentry = gethostbyname(name);
if (!hostentry)
return -1;
addr->qsa_family = AF_INET;
((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport);
((struct sockaddr_in *)addr)->sin_addr.s_addr = *(in_addr_t *)hostentry->h_addr_list[0];
return 0;
}
//=============================================================================
int UDP_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
{
if (addr1->qsa_family != addr2->qsa_family)
return -1;
if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
return -1;
if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
return 1;
return 0;
}
//=============================================================================
int UDP_GetSocketPort (struct qsockaddr *addr)
{
return ntohs(((struct sockaddr_in *)addr)->sin_port);
}
int UDP_SetSocketPort (struct qsockaddr *addr, int port)
{
((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port);
return 0;
}
//=============================================================================
int UDP_GetDefaultMTU (void)
{
return (sv.protocol == PROTOCOL_NETQUAKE) ? DATAGRAM_MTU_NQ : DATAGRAM_MTU;
}