2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
28 #include <sys/param.h>
29 #include <sys/ioctl.h>
33 #include <sys/filio.h>
40 extern int gethostname (char *, int);
41 extern int close (int);
43 extern cvar_t hostname;
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;
50 static unsigned long myAddr;
54 //=============================================================================
58 struct hostent *local;
59 char buff[MAXHOSTNAMELEN];
60 struct qsockaddr addr;
63 if (COM_CheckParm ("-noudp"))
66 // determine my name & address
67 gethostname(buff, MAXHOSTNAMELEN);
68 local = gethostbyname(buff);
69 myAddr = *(int *)local->h_addr_list[0];
71 // if the quake hostname isn't set, set it to the machine name
72 if (strcmp(hostname.string, "UNNAMED") == 0)
75 Cvar_Set ("hostname", buff);
78 if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
79 Sys_Error("UDP_Init: Unable to open control socket\n");
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);
85 UDP_GetSocketAddr (net_controlsocket, &addr);
86 strcpy(my_tcpip_address, UDP_AddrToString (&addr));
87 colon = strrchr (my_tcpip_address, ':');
91 Con_Printf("UDP Initialized\n");
92 tcpipAvailable = true;
94 return net_controlsocket;
97 //=============================================================================
99 void UDP_Shutdown (void)
102 UDP_CloseSocket (net_controlsocket);
105 //=============================================================================
107 void UDP_Listen (qboolean state)
112 if (net_acceptsocket != -1)
114 if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == -1)
115 Sys_Error ("UDP_Listen: Unable to open accept socket\n");
120 if (net_acceptsocket == -1)
122 UDP_CloseSocket (net_acceptsocket);
123 net_acceptsocket = -1;
126 //=============================================================================
128 int UDP_OpenSocket (int port)
131 struct sockaddr_in address;
132 qboolean _true = true;
134 if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
137 if (ioctl (newsocket, FIONBIO, (char *)&_true) == -1)
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)
153 //=============================================================================
155 int UDP_CloseSocket (int socket)
157 if (socket == net_broadcastsocket)
158 net_broadcastsocket = 0;
159 return close (socket);
163 //=============================================================================
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
172 static int PartialIPAddress (char *in, struct qsockaddr *hostaddr)
195 while (!( *b < '0' || *b > '9'))
197 num = num*10 + *b++ - '0';
201 if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
203 if (num < 0 || num > 255)
206 addr = (addr<<8) + num;
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);
220 //=============================================================================
222 int UDP_Connect (int socket, struct qsockaddr *addr)
227 //=============================================================================
229 int UDP_CheckNewConnections (void)
231 unsigned long available;
232 struct sockaddr_in from;
236 if (net_acceptsocket == -1)
239 if (ioctl (net_acceptsocket, FIONREAD, &available) == -1)
240 Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
242 return net_acceptsocket;
243 recvfrom (net_acceptsocket, buff, 0, 0, (struct sockaddr *) &from, &fromlen);
247 //=============================================================================
249 int UDP_Read (int socket, byte *buf, int len, struct qsockaddr *addr)
251 int addrlen = sizeof (struct qsockaddr);
254 ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
255 if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED))
260 //=============================================================================
262 int UDP_MakeSocketBroadcastCapable (int socket)
266 // make this socket broadcast capable
267 if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
269 net_broadcastsocket = socket;
274 //=============================================================================
276 int UDP_Broadcast (int socket, byte *buf, int len)
280 if (socket != net_broadcastsocket)
282 if (net_broadcastsocket != 0)
283 Sys_Error("Attempted to use multiple broadcasts sockets\n");
284 ret = UDP_MakeSocketBroadcastCapable (socket);
287 Con_Printf("Unable to make socket broadcast capable\n");
292 return UDP_Write (socket, buf, len, &broadcastaddr);
295 //=============================================================================
297 int UDP_Write (int socket, byte *buf, int len, struct qsockaddr *addr)
301 ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
302 if (ret == -1 && errno == EWOULDBLOCK)
307 //=============================================================================
309 char *UDP_AddrToString (struct qsockaddr *addr)
311 static char buffer[22];
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));
319 //=============================================================================
321 int UDP_StringToAddr (char *string, struct qsockaddr *addr)
323 int ha1, ha2, ha3, ha4, hp;
326 sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
327 ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
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);
335 //=============================================================================
337 unsigned long inet_addr(const char *cp);
338 int UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
340 int addrlen = sizeof(struct qsockaddr);
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;
352 //=============================================================================
354 int UDP_GetNameFromAddr (struct qsockaddr *addr, char *name)
356 struct hostent *hostentry;
358 hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
361 strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
365 strcpy (name, UDP_AddrToString (addr));
369 //=============================================================================
371 int UDP_GetAddrFromName(char *name, struct qsockaddr *addr)
373 struct hostent *hostentry;
375 if (name[0] >= '0' && name[0] <= '9')
376 return PartialIPAddress (name, addr);
378 hostentry = gethostbyname (name);
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];
389 //=============================================================================
391 int UDP_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
393 if (addr1->sa_family != addr2->sa_family)
396 if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
399 if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
405 //=============================================================================
407 int UDP_GetSocketPort (struct qsockaddr *addr)
409 return ntohs(((struct sockaddr_in *)addr)->sin_port);
413 int UDP_SetSocketPort (struct qsockaddr *addr, int port)
415 ((struct sockaddr_in *)addr)->sin_port = htons(port);
419 //=============================================================================