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