]> icculus.org git repositories - divverent/darkplaces.git/blob - net_udp.c
increased MAX_SURFVERTS to hold as many as the surf mesh splitter in model_brush...
[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 (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_Read (int socket, qbyte *buf, int len, struct qsockaddr *addr)
248 {
249         int addrlen = sizeof (struct qsockaddr);
250         int ret;
251
252         ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
253         if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED))
254                 return 0;
255         return ret;
256 }
257
258 //=============================================================================
259
260 int UDP_MakeSocketBroadcastCapable (int socket)
261 {
262         int                             i = 1;
263
264         // make this socket broadcast capable
265         if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
266                 return -1;
267         net_broadcastsocket = socket;
268
269         return 0;
270 }
271
272 //=============================================================================
273
274 int UDP_Broadcast (int socket, qbyte *buf, int len)
275 {
276         int ret;
277
278         if (socket != net_broadcastsocket)
279         {
280                 if (net_broadcastsocket != 0)
281                         Sys_Error("Attempted to use multiple broadcasts sockets\n");
282                 ret = UDP_MakeSocketBroadcastCapable (socket);
283                 if (ret == -1)
284                 {
285                         Con_Printf("Unable to make socket broadcast capable\n");
286                         return ret;
287                 }
288         }
289
290         return UDP_Write (socket, buf, len, &broadcastaddr);
291 }
292
293 //=============================================================================
294
295 int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr)
296 {
297         int ret;
298
299         ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
300         if (ret == -1 && errno == EWOULDBLOCK)
301                 return 0;
302         return ret;
303 }
304
305 //=============================================================================
306
307 char *UDP_AddrToString (struct qsockaddr *addr)
308 {
309         static char buffer[22];
310         int haddr;
311
312         haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
313         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));
314         return buffer;
315 }
316
317 //=============================================================================
318
319 int UDP_StringToAddr (char *string, struct qsockaddr *addr)
320 {
321         int ha1, ha2, ha3, ha4, hp;
322         int ipaddr;
323
324         sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
325         ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
326
327         addr->sa_family = AF_INET;
328         ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
329         ((struct sockaddr_in *)addr)->sin_port = htons(hp);
330         return 0;
331 }
332
333 //=============================================================================
334
335 unsigned long inet_addr(const char *cp);
336 int UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
337 {
338         int addrlen = sizeof(struct qsockaddr);
339         unsigned int a;
340
341         memset(addr, 0, sizeof(struct qsockaddr));
342         getsockname(socket, (struct sockaddr *)addr, &addrlen);
343         a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
344         if (a == 0 || a == inet_addr("127.0.0.1"))
345                 ((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr;
346
347         return 0;
348 }
349
350 //=============================================================================
351
352 int UDP_GetNameFromAddr (struct qsockaddr *addr, char *name)
353 {
354         struct hostent *hostentry;
355
356         hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
357         if (hostentry)
358         {
359                 strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
360                 return 0;
361         }
362
363         strcpy (name, UDP_AddrToString (addr));
364         return 0;
365 }
366
367 //=============================================================================
368
369 int UDP_GetAddrFromName(char *name, struct qsockaddr *addr)
370 {
371         struct hostent *hostentry;
372
373         if (name[0] >= '0' && name[0] <= '9')
374                 return PartialIPAddress (name, addr);
375         
376         hostentry = gethostbyname (name);
377         if (!hostentry)
378                 return -1;
379
380         addr->sa_family = AF_INET;
381         ((struct sockaddr_in *)addr)->sin_port = htons(net_hostport);   
382         ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
383
384         return 0;
385 }
386
387 //=============================================================================
388
389 int UDP_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
390 {
391         if (addr1->sa_family != addr2->sa_family)
392                 return -1;
393
394         if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
395                 return -1;
396
397         if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
398                 return 1;
399
400         return 0;
401 }
402
403 //=============================================================================
404
405 int UDP_GetSocketPort (struct qsockaddr *addr)
406 {
407         return ntohs(((struct sockaddr_in *)addr)->sin_port);
408 }
409
410
411 int UDP_SetSocketPort (struct qsockaddr *addr, int port)
412 {
413         ((struct sockaddr_in *)addr)->sin_port = htons(port);
414         return 0;
415 }
416
417 //=============================================================================