]> icculus.org git repositories - divverent/darkplaces.git/blob - net_udp.c
works with NULL worldmodel
[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 // net_udp.c
21
22 #include "quakedef.h"
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netdb.h>
28 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 #include <errno.h>
31
32 #ifdef __sun__
33 #include <sys/filio.h>
34 #endif
35
36 #ifdef NeXT
37 #include <libc.h>
38 #endif
39
40 extern int gethostname (char *, int);
41 extern int close (int);
42
43 static int net_acceptsocket = -1;               // socket for fielding new connections
44 static int net_controlsocket;
45 static int net_broadcastsocket = 0;
46 static struct qsockaddr broadcastaddr;
47
48 static unsigned long myAddr;
49
50 #include "net_udp.h"
51
52 //=============================================================================
53
54 int UDP_Init (void)
55 {
56         struct hostent *local;
57         char    buff[MAXHOSTNAMELEN];
58         struct qsockaddr addr;
59         char *colon;
60         
61         if (COM_CheckParm ("-noudp"))
62                 return -1;
63
64         // determine my name & address
65         gethostname(buff, MAXHOSTNAMELEN);
66         local = gethostbyname(buff);
67         if (!local)
68                 myAddr = htonl(INADDR_LOOPBACK);  // default to the loopback address
69         else
70                 myAddr = *(int *)local->h_addr_list[0];
71
72         // if the quake hostname isn't set, set it to the machine name
73         if (strcmp(hostname.string, "UNNAMED") == 0)
74         {
75                 buff[15] = 0;
76                 Cvar_Set ("hostname", buff);
77         }
78
79         if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
80                 Sys_Error("UDP_Init: Unable to open control socket\n");
81
82         ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
83         ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = htonl(INADDR_BROADCAST);
84         ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons(net_hostport);
85
86         UDP_GetSocketAddr (net_controlsocket, &addr);
87         strcpy(my_tcpip_address,  UDP_AddrToString (&addr));
88         colon = strrchr (my_tcpip_address, ':');
89         if (colon)
90                 *colon = 0;
91
92         Con_Printf("UDP Initialized\n");
93         tcpipAvailable = true;
94
95         return net_controlsocket;
96 }
97
98 //=============================================================================
99
100 void UDP_Shutdown (void)
101 {
102         UDP_Listen (false);
103         UDP_CloseSocket (net_controlsocket);
104 }
105
106 //=============================================================================
107
108 void UDP_Listen (qboolean state)
109 {
110         // enable listening
111         if (state)
112         {
113                 if (net_acceptsocket != -1)
114                         return;
115                 if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == -1)
116                         Sys_Error ("UDP_Listen: Unable to open accept socket\n");
117                 return;
118         }
119
120         // disable listening
121         if (net_acceptsocket == -1)
122                 return;
123         UDP_CloseSocket (net_acceptsocket);
124         net_acceptsocket = -1;
125 }
126
127 //=============================================================================
128
129 int UDP_OpenSocket (int port)
130 {
131         int newsocket;
132         struct sockaddr_in address;
133         qboolean _true = true;
134
135         if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
136                 return -1;
137
138         if (ioctl (newsocket, FIONBIO, (char *)&_true) == -1)
139                 goto ErrorReturn;
140
141         address.sin_family = AF_INET;
142         address.sin_addr.s_addr = INADDR_ANY;
143         address.sin_port = htons(port);
144         if( bind (newsocket, (void *)&address, sizeof(address)) == -1)
145                 goto ErrorReturn;
146
147         return newsocket;
148
149 ErrorReturn:
150         close (newsocket);
151         return -1;
152 }
153
154 //=============================================================================
155
156 int UDP_CloseSocket (int socket)
157 {
158         if (socket == net_broadcastsocket)
159                 net_broadcastsocket = 0;
160         return close (socket);
161 }
162
163
164 //=============================================================================
165 /*
166 ============
167 PartialIPAddress
168
169 this lets you type only as much of the net address as required, using
170 the local network components to fill in the rest
171 ============
172 */
173 static int PartialIPAddress (const char *in, struct qsockaddr *hostaddr)
174 {
175         char buff[256];
176         char *b;
177         int addr;
178         int num;
179         int mask;
180         int run;
181         int port;
182         
183         buff[0] = '.';
184         b = buff;
185         strcpy(buff+1, in);
186         if (buff[1] == '.')
187                 b++;
188
189         addr = 0;
190         mask=-1;
191         while (*b == '.')
192         {
193                 b++;
194                 num = 0;
195                 run = 0;
196                 while (!( *b < '0' || *b > '9'))
197                 {
198                   num = num*10 + *b++ - '0';
199                   if (++run > 3)
200                         return -1;
201                 }
202                 if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
203                         return -1;
204                 if (num < 0 || num > 255)
205                         return -1;
206                 mask<<=8;
207                 addr = (addr<<8) + num;
208         }
209         
210         if (*b++ == ':')
211                 port = atoi(b);
212         else
213                 port = net_hostport;
214
215         hostaddr->sa_family = AF_INET;
216         ((struct sockaddr_in *)hostaddr)->sin_port = htons((short)port);        
217         ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr = (myAddr & htonl(mask)) | htonl(addr);
218         
219         return 0;
220 }
221 //=============================================================================
222
223 int UDP_Connect (int socket, struct qsockaddr *addr)
224 {
225         return 0;
226 }
227
228 //=============================================================================
229
230 int UDP_CheckNewConnections (void)
231 {
232         unsigned long   available;
233         struct sockaddr_in      from;
234         socklen_t                       fromlen;
235         char buff[1];
236
237         if (net_acceptsocket == -1)
238                 return -1;
239
240         if (ioctl (net_acceptsocket, FIONREAD, &available) == -1)
241                 Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
242         if (available)
243                 return net_acceptsocket;
244         recvfrom (net_acceptsocket, buff, 0, 0, (struct sockaddr *) &from, &fromlen);
245         return -1;
246 }
247
248 //=============================================================================
249
250 int UDP_Recv (qbyte *buf, int len, struct qsockaddr *addr)
251 {
252         return UDP_Read (net_acceptsocket, buf, len, addr);
253 }
254
255 //=============================================================================
256
257 int UDP_Send (qbyte *buf, int len, struct qsockaddr *addr)
258 {
259         return UDP_Write (net_acceptsocket, buf, len, addr);
260 }
261
262 //=============================================================================
263
264 int UDP_Read (int socket, qbyte *buf, int len, struct qsockaddr *addr)
265 {
266         int addrlen = sizeof (struct qsockaddr);
267         int ret;
268
269         ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
270         if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED))
271                 return 0;
272         return ret;
273 }
274
275 //=============================================================================
276
277 int UDP_MakeSocketBroadcastCapable (int socket)
278 {
279         int                             i = 1;
280
281         // make this socket broadcast capable
282         if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
283                 return -1;
284         net_broadcastsocket = socket;
285
286         return 0;
287 }
288
289 //=============================================================================
290
291 int UDP_Broadcast (int socket, qbyte *buf, int len)
292 {
293         int ret;
294
295         if (socket != net_broadcastsocket)
296         {
297                 if (net_broadcastsocket != 0)
298                         Sys_Error("Attempted to use multiple broadcasts sockets\n");
299                 ret = UDP_MakeSocketBroadcastCapable (socket);
300                 if (ret == -1)
301                 {
302                         Con_Printf("Unable to make socket broadcast capable\n");
303                         return ret;
304                 }
305         }
306
307         return UDP_Write (socket, buf, len, &broadcastaddr);
308 }
309
310 //=============================================================================
311
312 int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr)
313 {
314         int ret;
315
316         ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
317         if (ret == -1 && errno == EWOULDBLOCK)
318                 return 0;
319         return ret;
320 }
321
322 //=============================================================================
323
324 char *UDP_AddrToString (const struct qsockaddr *addr)
325 {
326         static char buffer[22];
327         int haddr;
328
329         haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
330         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));
331         return buffer;
332 }
333
334 //=============================================================================
335
336 int UDP_StringToAddr (const char *string, struct qsockaddr *addr)
337 {
338         int ha1, ha2, ha3, ha4, hp;
339         int ipaddr;
340
341         sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
342         ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
343
344         addr->sa_family = AF_INET;
345         ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
346         ((struct sockaddr_in *)addr)->sin_port = htons(hp);
347         return 0;
348 }
349
350 //=============================================================================
351
352 unsigned long inet_addr(const char *cp);
353 int UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
354 {
355         int addrlen = sizeof(struct qsockaddr);
356         unsigned int a;
357
358         memset(addr, 0, sizeof(struct qsockaddr));
359         getsockname(socket, (struct sockaddr *)addr, &addrlen);
360         a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
361         if (a == 0 || a == inet_addr("127.0.0.1"))
362                 ((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr;
363
364         return 0;
365 }
366
367 //=============================================================================
368
369 int UDP_GetNameFromAddr (const struct qsockaddr *addr, char *name)
370 {
371         struct hostent *hostentry;
372
373         hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
374         if (hostentry)
375         {
376                 strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
377                 return 0;
378         }
379
380         strcpy (name, UDP_AddrToString (addr));
381         return 0;
382 }
383
384 //=============================================================================
385
386 int UDP_GetAddrFromName(const char *name, struct qsockaddr *addr)
387 {
388         struct hostent *hostentry;
389
390         if (name[0] >= '0' && name[0] <= '9')
391                 return PartialIPAddress (name, addr);
392         
393         hostentry = gethostbyname (name);
394         if (!hostentry)
395                 return -1;
396
397         addr->sa_family = AF_INET;
398         ((struct sockaddr_in *)addr)->sin_port = htons(net_hostport);   
399         ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
400
401         return 0;
402 }
403
404 //=============================================================================
405
406 int UDP_AddrCompare (const struct qsockaddr *addr1, const struct qsockaddr *addr2)
407 {
408         if (addr1->sa_family != addr2->sa_family)
409                 return -1;
410
411         if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
412                 return -1;
413
414         if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
415                 return 1;
416
417         return 0;
418 }
419
420 //=============================================================================
421
422 int UDP_GetSocketPort (struct qsockaddr *addr)
423 {
424         return ntohs(((struct sockaddr_in *)addr)->sin_port);
425 }
426
427
428 int UDP_SetSocketPort (struct qsockaddr *addr, int port)
429 {
430         ((struct sockaddr_in *)addr)->sin_port = htons(port);
431         return 0;
432 }
433