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