]> icculus.org git repositories - divverent/darkplaces.git/blob - net_udp.c
always do r_shadow_realtime_dlight if r_shadow_realtime_world is on
[divverent/darkplaces.git] / net_udp.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 #include "quakedef.h"
21 #include "net_udp.h"
22 #ifdef WIN32
23 #include "winquake.h"
24 #define MAXHOSTNAMELEN          256
25 #else
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <netdb.h>
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <errno.h>
35
36 #ifdef __sun__
37 #include <sys/filio.h>
38 #endif
39
40 #ifdef NeXT
41 #include <libc.h>
42 #endif
43 #endif
44
45 static int net_acceptsocket = -1;               // socket for fielding new connections
46 static int net_controlsocket;
47 static int net_broadcastsocket = 0;
48 static struct qsockaddr broadcastaddr;
49
50 static union {int i;unsigned char d[4];} myAddr;
51
52 //=============================================================================
53
54 #ifdef WIN32
55 WSADATA         winsockdata;
56 #endif
57
58 int UDP_Init (void)
59 {
60         int i;
61         struct hostent *local = NULL;
62         char buff[MAXHOSTNAMELEN];
63
64         if (COM_CheckParm ("-noudp"))
65                 return -1;
66
67 #ifdef WIN32
68         if (WSAStartup (MAKEWORD(1, 1), &winsockdata))
69         {
70                 Con_SafePrintf ("Winsock initialization failed.\n");
71                 return -1;
72         }
73 #endif
74
75         // loopback as a worst case fallback
76         myAddr.d[0] = 127;myAddr.d[1] = 0;myAddr.d[2] = 0;myAddr.d[3] = 1;
77
78         if ((i = COM_CheckParm("-ip")) != 0 && i < com_argc)
79         {
80                 myAddr.i = inet_addr(com_argv[i+1]);
81                 Con_Printf("Binding to IP Interface Address of %i.%i.%i.%i\n", myAddr.d[0], myAddr.d[1], myAddr.d[2], myAddr.d[3]);
82         }
83         else if (gethostname(buff, MAXHOSTNAMELEN) != -1)
84         {
85                 buff[MAXHOSTNAMELEN - 1] = 0;
86                 local = gethostbyname(buff);
87                 if (local != NULL)
88                         myAddr.i = *((int *)local->h_addr_list[0]);
89         }
90
91         sprintf(my_tcpip_address, "%d.%d.%d.%d", myAddr.d[0], myAddr.d[1], myAddr.d[2], myAddr.d[3]);
92
93         if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
94         {
95                 Con_Printf("UDP_Init: Unable to open control socket\n");
96 #ifdef WIN32
97                 WSACleanup ();
98 #endif
99                 return -1;
100         }
101
102         ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
103         ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = htonl(INADDR_BROADCAST);
104         ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((unsigned short)net_hostport);
105
106         Con_Printf("UDP Initialized\n");
107         tcpipAvailable = true;
108
109         return net_controlsocket;
110 }
111
112 //=============================================================================
113
114 void UDP_Shutdown (void)
115 {
116         UDP_Listen (false);
117         UDP_CloseSocket (net_controlsocket);
118 #ifdef WIN32
119         WSACleanup ();
120 #endif
121 }
122
123 //=============================================================================
124
125 void UDP_Listen (qboolean state)
126 {
127         // enable listening
128         if (state)
129         {
130                 if (net_acceptsocket != -1)
131                         return;
132                 if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == -1)
133                         Sys_Error ("UDP_Listen: Unable to open accept socket\n");
134                 return;
135         }
136
137         // disable listening
138         if (net_acceptsocket == -1)
139                 return;
140         UDP_CloseSocket (net_acceptsocket);
141         net_acceptsocket = -1;
142 }
143
144 //=============================================================================
145
146 int UDP_OpenSocket (int port)
147 {
148         int newsocket;
149         struct sockaddr_in address;
150
151         if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
152                 return -1;
153
154         {
155 #ifdef WIN32
156                 u_long _true = 1;
157                 if (ioctlsocket (newsocket, FIONBIO, &_true) == -1)
158                 {
159                         closesocket (newsocket);
160 #else
161                 char _true = 1;
162                 if (ioctl (newsocket, FIONBIO, &_true) == -1)
163                 {
164                         close (newsocket);
165 #endif
166                         Sys_Error("UDP_OpenSocket: unable to do a ioctl FIONBIO on the socket\n");
167                 }
168         }
169
170         address.sin_family = AF_INET;
171         address.sin_addr.s_addr = myAddr.i;
172         address.sin_port = htons((unsigned short)port);
173         if (bind(newsocket, (void *)&address, sizeof(address)) == -1)
174         {
175 #ifdef WIN32
176                 closesocket(newsocket);
177 #else
178                 close(newsocket);
179 #endif
180                 Sys_Error ("UDP_OpenSocket: Unable to bind to %s", UDP_AddrToString((struct qsockaddr *)&address));
181         }
182
183         return newsocket;
184 }
185
186 //=============================================================================
187
188 int UDP_CloseSocket (int socket)
189 {
190         if (net_broadcastsocket == socket)
191                 net_broadcastsocket = 0;
192 #ifdef WIN32
193         return closesocket (socket);
194 #else
195         return close (socket);
196 #endif
197 }
198
199 //=============================================================================
200
201 int UDP_Connect (int socket, struct qsockaddr *addr)
202 {
203         return 0;
204 }
205
206 //=============================================================================
207
208 int UDP_CheckNewConnections (void)
209 {
210         char buf[4096];
211 #ifndef WIN32
212         unsigned long   available;
213         struct sockaddr_in      from;
214         socklen_t                       fromlen;
215 #endif
216
217         if (net_acceptsocket == -1)
218                 return -1;
219
220 #ifdef WIN32
221         if (recvfrom (net_acceptsocket, buf, sizeof(buf), MSG_PEEK, NULL, NULL) >= 0)
222                 return net_acceptsocket;
223 #else
224         if (ioctl (net_acceptsocket, FIONREAD, &available) == -1)
225                 Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
226         if (available)
227                 return net_acceptsocket;
228         recvfrom (net_acceptsocket, buf, 0, 0, (struct sockaddr *) &from, &fromlen);
229 #endif
230         return -1;
231 }
232
233 //=============================================================================
234
235 int UDP_Recv (qbyte *buf, int len, struct qsockaddr *addr)
236 {
237         return UDP_Read (net_acceptsocket, buf, len, addr);
238 }
239
240 //=============================================================================
241
242 int UDP_Send (qbyte *buf, int len, struct qsockaddr *addr)
243 {
244         return UDP_Write (net_acceptsocket, buf, len, addr);
245 }
246
247 //=============================================================================
248
249 int UDP_Read (int socket, qbyte *buf, int len, struct qsockaddr *addr)
250 {
251         int addrlen = sizeof (struct qsockaddr);
252         int ret;
253
254         ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
255         if (ret == -1)
256         {
257 #ifdef WIN32
258                 int e = WSAGetLastError();
259                 if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
260                         return 0;
261                 Con_Printf("UDP_Read(%i, %p, %i, <%s>): WSAGetLastError == %i\n", socket, buf, len, UDP_AddrToString(addr), e);
262 #else
263                 if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
264                         return 0;
265                 Con_Printf("UDP_Read(%i, %p, %i, <%s>): errno == %i (%s)\n", socket, buf, len, UDP_AddrToString(addr), errno, strerror(errno));
266 #endif
267         }
268         else if (developer_networking.integer)
269         {
270                 Con_Printf("UDP_Read(%i, %p, %i, <%s>) = %i\n", socket, buf, len, UDP_AddrToString(addr), ret);
271                 Com_HexDumpToConsole(buf, ret);
272         }
273
274         return ret;
275 }
276
277 //=============================================================================
278
279 int UDP_MakeSocketBroadcastCapable (int socket)
280 {
281         int i = 1;
282
283         // make this socket broadcast capable
284         if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
285                 return -1;
286         net_broadcastsocket = socket;
287
288         return 0;
289 }
290
291 //=============================================================================
292
293 int UDP_Broadcast (int socket, qbyte *buf, int len)
294 {
295         int ret;
296
297         if (socket != net_broadcastsocket)
298         {
299                 if (net_broadcastsocket != 0)
300                         Sys_Error("Attempted to use multiple broadcasts sockets\n");
301                 ret = UDP_MakeSocketBroadcastCapable (socket);
302                 if (ret == -1)
303                 {
304                         Con_Printf("Unable to make socket broadcast capable\n");
305                         return ret;
306                 }
307         }
308
309         return UDP_Write (socket, buf, len, &broadcastaddr);
310 }
311
312 //=============================================================================
313
314 int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr)
315 {
316         int ret;
317
318         if (developer_networking.integer)
319         {
320                 Con_Printf("UDP_Write(%i, %p, %i, <%s>)\n", socket, buf, len, UDP_AddrToString(addr));
321                 Com_HexDumpToConsole(buf, len);
322         }
323
324         ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
325         if (ret == -1)
326         {
327 #ifdef WIN32
328                 int e = WSAGetLastError();
329                 if (e == WSAEWOULDBLOCK)
330                         return 0;
331                 Con_Printf("UDP_Write(%i, %p, %i, <%s>): WSAGetLastError == %i\n", socket, buf, len, UDP_AddrToString(addr), e);
332 #else
333                 if (errno == EWOULDBLOCK)
334                         return 0;
335                 Con_Printf("UDP_Write(%i, %p, %i, <%s>): errno == %i (%s)\n", socket, buf, len, UDP_AddrToString(addr), errno, strerror(errno));
336 #endif
337         }
338         return ret;
339 }
340
341 //=============================================================================
342
343 char *UDP_AddrToString (const struct qsockaddr *addr)
344 {
345         static char buffer[22]; // only 22 needed (3 + 1 + 3 + 1 + 3 + 1 + 3 + 1 + 5 + null)
346         unsigned char *ip = (char *)(&((struct sockaddr_in *)addr)->sin_addr.s_addr);
347         sprintf(buffer, "%d.%d.%d.%d:%d", ip[0], ip[1], ip[2], ip[3], ntohs(((struct sockaddr_in *)addr)->sin_port));
348         return buffer;
349 }
350
351 //=============================================================================
352
353 int UDP_StringToAddr (const char *string, struct qsockaddr *addr)
354 {
355         int ha[4], hp, ipaddr, j, numbers;
356         const char *colon;
357
358         hp = net_hostport;
359         colon = strrchr(string, ':');
360         if (colon)
361         {
362                 hp = atoi(colon + 1);
363                 if (hp == 0)
364                         hp = net_hostport;
365         }
366         numbers = sscanf(string, "%d.%d.%d.%d", &ha[0], &ha[1], &ha[2], &ha[3]);
367         for (ipaddr = 0, j = 0;j < numbers;j++)
368                 ipaddr = (ipaddr << 8) | ha[j];
369         // if the address is incomplete take most important numbers from myAddr
370         if (numbers < 4)
371                 ipaddr |= ntohl(myAddr.i) & (-1 << (numbers * 8));
372
373         addr->sa_family = AF_INET;
374         ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
375         ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp);
376         if (ipaddr == INADDR_ANY)
377                 return -1;
378         else
379                 return 0;
380 }
381
382 //=============================================================================
383
384 int UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
385 {
386         int ret;
387         int addrlen = sizeof(struct qsockaddr);
388         memset(addr, 0, sizeof(struct qsockaddr));
389         ret = getsockname(socket, (struct sockaddr *)addr, &addrlen);
390         if (ret == -1)
391         {
392 #ifdef WIN32
393                 int e = WSAGetLastError();
394                 Con_Printf("UDP_GetSocketAddr: WASGetLastError == %i\n", e);
395 #else
396                 Con_Printf("UDP_GetSocketAddr: errno == %i (%s)\n", errno, strerror(errno));
397 #endif
398         }
399         return ret;
400 }
401
402 //=============================================================================
403
404 int UDP_GetNameFromAddr (const struct qsockaddr *addr, char *name)
405 {
406         struct hostent *hostentry;
407
408         hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
409         if (hostentry)
410         {
411                 strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
412                 return 0;
413         }
414
415         strcpy (name, UDP_AddrToString (addr));
416         return 0;
417 }
418
419 //=============================================================================
420
421 int UDP_GetAddrFromName(const char *name, struct qsockaddr *addr)
422 {
423         struct hostent *hostentry;
424
425         if (name[0] >= '0' && name[0] <= '9')
426                 return UDP_StringToAddr (name, addr);
427
428         hostentry = gethostbyname (name);
429         if (!hostentry)
430                 return -1;
431
432         addr->sa_family = AF_INET;
433         ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport);
434         ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
435
436         return 0;
437 }
438
439 //=============================================================================
440
441 int UDP_AddrCompare (const struct qsockaddr *addr1, const struct qsockaddr *addr2)
442 {
443         if (addr1->sa_family != addr2->sa_family)
444                 return -1;
445
446         if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
447                 return -1;
448
449         if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
450                 return 1;
451
452         return 0;
453 }
454
455 //=============================================================================
456
457 int UDP_GetSocketPort (struct qsockaddr *addr)
458 {
459         return ntohs(((struct sockaddr_in *)addr)->sin_port);
460 }
461
462
463 int UDP_SetSocketPort (struct qsockaddr *addr, int port)
464 {
465         ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port);
466         return 0;
467 }
468