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 #define MAXHOSTNAMELEN 256
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
37 #include <sys/filio.h>
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 union {int i;unsigned char d[4];} myAddr;
52 //=============================================================================
61 struct hostent *local;
62 char buff[MAXHOSTNAMELEN];
64 if (COM_CheckParm ("-noudp"))
68 if (WSAStartup (MAKEWORD(1, 1), &winsockdata))
70 Con_SafePrintf ("Winsock initialization failed.\n");
75 // loopback as a worst case fallback
76 myAddr.i = htonl(INADDR_ANY);
78 net_controlsocket = -1;
79 for (j = 0;net_controlsocket == -1;j++)
84 if ((i = COM_CheckParm("-ip")) != 0 && i < com_argc)
85 myAddr.i = inet_addr(com_argv[i+1]);
88 myAddr.i = htonl(INADDR_ANY);
91 if (gethostname(buff, MAXHOSTNAMELEN) != -1)
93 buff[MAXHOSTNAMELEN - 1] = 0;
94 local = gethostbyname(buff);
96 myAddr.i = *((int *)local->h_addr_list[0]);
104 Con_Printf("UDP_Init: Giving up, UDP networking support disabled.\n");
111 if (myAddr.i == htonl(INADDR_LOOPBACK))
112 sprintf(my_tcpip_address, "INADDR_LOOPBACK");
113 else if (myAddr.i == htonl(INADDR_ANY))
114 sprintf(my_tcpip_address, "INADDR_ANY");
115 else if (myAddr.i == htonl(INADDR_NONE))
116 sprintf(my_tcpip_address, "INADDR_NONE");
118 sprintf(my_tcpip_address, "%d.%d.%d.%d", myAddr.d[0], myAddr.d[1], myAddr.d[2], myAddr.d[3]);
119 Con_Printf("UDP_Init: Binding to IP Interface Address of %s... ", my_tcpip_address);
120 if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
121 Con_Printf("failed\n");
123 Con_Printf("succeeded\n");
126 ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
127 ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = htonl(INADDR_BROADCAST);
128 ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((unsigned short)net_hostport);
130 Con_Printf("UDP Initialized\n");
131 tcpipAvailable = true;
133 return net_controlsocket;
136 //=============================================================================
138 void UDP_Shutdown (void)
141 UDP_CloseSocket (net_controlsocket);
147 //=============================================================================
149 void UDP_Listen (qboolean state)
154 if (net_acceptsocket != -1)
156 if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == -1)
157 Sys_Error ("UDP_Listen: Unable to open accept socket\n");
162 if (net_acceptsocket == -1)
164 UDP_CloseSocket (net_acceptsocket);
165 net_acceptsocket = -1;
168 //=============================================================================
170 int UDP_OpenSocket (int port)
173 struct sockaddr_in address;
175 if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
181 if (ioctlsocket (newsocket, FIONBIO, &_true) == -1)
183 closesocket (newsocket);
186 if (ioctl (newsocket, FIONBIO, &_true) == -1)
190 Sys_Error("UDP_OpenSocket: unable to do a ioctl FIONBIO on the socket\n");
194 address.sin_family = AF_INET;
195 address.sin_addr.s_addr = myAddr.i;
196 address.sin_port = htons((unsigned short)port);
197 if (bind(newsocket, (void *)&address, sizeof(address)) == -1)
200 closesocket(newsocket);
204 Sys_Error ("UDP_OpenSocket: Unable to bind to %s", UDP_AddrToString((struct qsockaddr *)&address));
210 //=============================================================================
212 int UDP_CloseSocket (int socket)
214 if (net_broadcastsocket == socket)
215 net_broadcastsocket = 0;
217 return closesocket (socket);
219 return close (socket);
223 //=============================================================================
225 int UDP_Connect (int socket, struct qsockaddr *addr)
230 //=============================================================================
232 int UDP_CheckNewConnections (void)
236 unsigned long available;
237 struct sockaddr_in from;
241 if (net_acceptsocket == -1)
245 if (recvfrom (net_acceptsocket, buf, sizeof(buf), MSG_PEEK, NULL, NULL) >= 0)
246 return net_acceptsocket;
248 if (ioctl (net_acceptsocket, FIONREAD, &available) == -1)
249 Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
251 return net_acceptsocket;
252 recvfrom (net_acceptsocket, buf, 0, 0, (struct sockaddr *) &from, &fromlen);
257 //=============================================================================
259 int UDP_Recv (qbyte *buf, int len, struct qsockaddr *addr)
261 return UDP_Read (net_acceptsocket, buf, len, addr);
264 //=============================================================================
266 int UDP_Send (qbyte *buf, int len, struct qsockaddr *addr)
268 return UDP_Write (net_acceptsocket, buf, len, addr);
271 //=============================================================================
273 int UDP_Read (int socket, qbyte *buf, int len, struct qsockaddr *addr)
275 int addrlen = sizeof (struct qsockaddr);
278 ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
282 int e = WSAGetLastError();
283 if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
285 Con_Printf("UDP_Read(%i, %p, %i, <%s>): WSAGetLastError == %i\n", socket, buf, len, UDP_AddrToString(addr), e);
287 if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
289 Con_Printf("UDP_Read(%i, %p, %i, <%s>): errno == %i (%s)\n", socket, buf, len, UDP_AddrToString(addr), errno, strerror(errno));
292 else if (developer_networking.integer)
294 Con_Printf("UDP_Read(%i, %p, %i, <%s>) = %i\n", socket, buf, len, UDP_AddrToString(addr), ret);
295 Com_HexDumpToConsole(buf, ret);
301 //=============================================================================
303 int UDP_MakeSocketBroadcastCapable (int socket)
307 // make this socket broadcast capable
308 if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
310 net_broadcastsocket = socket;
315 //=============================================================================
317 int UDP_Broadcast (int socket, qbyte *buf, int len)
321 if (socket != net_broadcastsocket)
323 if (net_broadcastsocket != 0)
324 Sys_Error("Attempted to use multiple broadcasts sockets\n");
325 ret = UDP_MakeSocketBroadcastCapable (socket);
328 Con_Printf("Unable to make socket broadcast capable\n");
333 return UDP_Write (socket, buf, len, &broadcastaddr);
336 //=============================================================================
338 int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr)
342 if (developer_networking.integer)
344 Con_Printf("UDP_Write(%i, %p, %i, <%s>)\n", socket, buf, len, UDP_AddrToString(addr));
345 Com_HexDumpToConsole(buf, len);
348 ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
352 int e = WSAGetLastError();
353 if (e == WSAEWOULDBLOCK)
355 Con_Printf("UDP_Write(%i, %p, %i, <%s>): WSAGetLastError == %i\n", socket, buf, len, UDP_AddrToString(addr), e);
357 if (errno == EWOULDBLOCK)
359 Con_Printf("UDP_Write(%i, %p, %i, <%s>): errno == %i (%s)\n", socket, buf, len, UDP_AddrToString(addr), errno, strerror(errno));
365 //=============================================================================
367 char *UDP_AddrToString (const struct qsockaddr *addr)
369 static char buffer[22]; // only 22 needed (3 + 1 + 3 + 1 + 3 + 1 + 3 + 1 + 5 + null)
370 unsigned char *ip = (char *)(&((struct sockaddr_in *)addr)->sin_addr.s_addr);
371 sprintf(buffer, "%d.%d.%d.%d:%d", ip[0], ip[1], ip[2], ip[3], ntohs(((struct sockaddr_in *)addr)->sin_port));
375 //=============================================================================
377 int UDP_StringToAddr (const char *string, struct qsockaddr *addr)
379 int ha[4], hp, ipaddr, j, numbers;
383 colon = strrchr(string, ':');
386 hp = atoi(colon + 1);
390 numbers = sscanf(string, "%d.%d.%d.%d", &ha[0], &ha[1], &ha[2], &ha[3]);
391 for (ipaddr = 0, j = 0;j < numbers;j++)
392 ipaddr = (ipaddr << 8) | ha[j];
393 // if the address is incomplete take most important numbers from myAddr
395 ipaddr |= ntohl(myAddr.i) & (-1 << (numbers * 8));
397 addr->sa_family = AF_INET;
398 ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
399 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp);
400 if (ipaddr == INADDR_ANY)
406 //=============================================================================
408 int UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
411 int addrlen = sizeof(struct qsockaddr);
412 memset(addr, 0, sizeof(struct qsockaddr));
413 ret = getsockname(socket, (struct sockaddr *)addr, &addrlen);
417 int e = WSAGetLastError();
418 Con_Printf("UDP_GetSocketAddr: WASGetLastError == %i\n", e);
420 Con_Printf("UDP_GetSocketAddr: errno == %i (%s)\n", errno, strerror(errno));
426 //=============================================================================
428 int UDP_GetNameFromAddr (const struct qsockaddr *addr, char *name)
430 struct hostent *hostentry;
432 hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
435 strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
439 strcpy (name, UDP_AddrToString (addr));
443 //=============================================================================
445 int UDP_GetAddrFromName(const char *name, struct qsockaddr *addr)
447 struct hostent *hostentry;
449 if (name[0] >= '0' && name[0] <= '9')
450 return UDP_StringToAddr (name, addr);
452 hostentry = gethostbyname (name);
456 addr->sa_family = AF_INET;
457 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport);
458 ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
463 //=============================================================================
465 int UDP_AddrCompare (const struct qsockaddr *addr1, const struct qsockaddr *addr2)
467 if (addr1->sa_family != addr2->sa_family)
470 if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
473 if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
479 //=============================================================================
481 int UDP_GetSocketPort (struct qsockaddr *addr)
483 return ntohs(((struct sockaddr_in *)addr)->sin_port);
487 int UDP_SetSocketPort (struct qsockaddr *addr, int port)
489 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port);