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