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 = NULL;
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.d[0] = 127;myAddr.d[1] = 0;myAddr.d[2] = 0;myAddr.d[3] = 1;
78 if ((i = COM_CheckParm("-ip")) != 0 && i < com_argc)
80 myAddr.i = inet_addr(com_argv[i+1]);
81 Con_Printf("Binding to IP Interface Address of %i.%i.%i.%i\n", myAddr.d[0], myAddr.d[1], myAddr.d[2], myAddr.d[3]);
83 else if (gethostname(buff, MAXHOSTNAMELEN) != -1)
85 buff[MAXHOSTNAMELEN - 1] = 0;
86 local = gethostbyname(buff);
88 myAddr.i = *((int *)local->h_addr_list[0]);
91 sprintf(my_tcpip_address, "%d.%d.%d.%d", myAddr.d[0], myAddr.d[1], myAddr.d[2], myAddr.d[3]);
93 if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
95 Con_Printf("UDP_Init: Unable to open control socket\n");
102 ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
103 ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = htonl(INADDR_BROADCAST);
104 ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((unsigned short)net_hostport);
106 Con_Printf("UDP Initialized\n");
107 tcpipAvailable = true;
109 return net_controlsocket;
112 //=============================================================================
114 void UDP_Shutdown (void)
117 UDP_CloseSocket (net_controlsocket);
123 //=============================================================================
125 void UDP_Listen (qboolean state)
130 if (net_acceptsocket != -1)
132 if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == -1)
133 Sys_Error ("UDP_Listen: Unable to open accept socket\n");
138 if (net_acceptsocket == -1)
140 UDP_CloseSocket (net_acceptsocket);
141 net_acceptsocket = -1;
144 //=============================================================================
146 int UDP_OpenSocket (int port)
149 struct sockaddr_in address;
151 if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
157 if (ioctlsocket (newsocket, FIONBIO, &_true) == -1)
159 closesocket (newsocket);
162 if (ioctl (newsocket, FIONBIO, &_true) == -1)
166 Sys_Error("UDP_OpenSocket: unable to do a ioctl FIONBIO on the socket\n");
170 address.sin_family = AF_INET;
171 address.sin_addr.s_addr = myAddr.i;
172 address.sin_port = htons((unsigned short)port);
173 if (bind(newsocket, (void *)&address, sizeof(address)) == -1)
176 closesocket(newsocket);
180 Sys_Error ("UDP_OpenSocket: Unable to bind to %s", UDP_AddrToString((struct qsockaddr *)&address));
186 //=============================================================================
188 int UDP_CloseSocket (int socket)
190 if (net_broadcastsocket == socket)
191 net_broadcastsocket = 0;
193 return closesocket (socket);
195 return close (socket);
199 //=============================================================================
201 int UDP_Connect (int socket, struct qsockaddr *addr)
206 //=============================================================================
208 int UDP_CheckNewConnections (void)
212 unsigned long available;
213 struct sockaddr_in from;
217 if (net_acceptsocket == -1)
221 if (recvfrom (net_acceptsocket, buf, sizeof(buf), MSG_PEEK, NULL, NULL) >= 0)
222 return net_acceptsocket;
224 if (ioctl (net_acceptsocket, FIONREAD, &available) == -1)
225 Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
227 return net_acceptsocket;
228 recvfrom (net_acceptsocket, buf, 0, 0, (struct sockaddr *) &from, &fromlen);
233 //=============================================================================
235 int UDP_Recv (qbyte *buf, int len, struct qsockaddr *addr)
237 return UDP_Read (net_acceptsocket, buf, len, addr);
240 //=============================================================================
242 int UDP_Send (qbyte *buf, int len, struct qsockaddr *addr)
244 return UDP_Write (net_acceptsocket, buf, len, addr);
247 //=============================================================================
249 int UDP_Read (int socket, qbyte *buf, int len, struct qsockaddr *addr)
251 int addrlen = sizeof (struct qsockaddr);
254 ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
258 int e = WSAGetLastError();
259 if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
261 Con_Printf("UDP_Read(%i, %p, %i, <%s>): WSAGetLastError == %i\n", socket, buf, len, UDP_AddrToString(addr), e);
263 if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
265 Con_Printf("UDP_Read(%i, %p, %i, <%s>): errno == %i (%s)\n", socket, buf, len, UDP_AddrToString(addr), errno, strerror(errno));
268 else if (developer_networking.integer)
270 Con_Printf("UDP_Read(%i, %p, %i, <%s>) = %i\n", socket, buf, len, UDP_AddrToString(addr), ret);
271 Com_HexDumpToConsole(buf, ret);
277 //=============================================================================
279 int UDP_MakeSocketBroadcastCapable (int socket)
283 // make this socket broadcast capable
284 if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
286 net_broadcastsocket = socket;
291 //=============================================================================
293 int UDP_Broadcast (int socket, qbyte *buf, int len)
297 if (socket != net_broadcastsocket)
299 if (net_broadcastsocket != 0)
300 Sys_Error("Attempted to use multiple broadcasts sockets\n");
301 ret = UDP_MakeSocketBroadcastCapable (socket);
304 Con_Printf("Unable to make socket broadcast capable\n");
309 return UDP_Write (socket, buf, len, &broadcastaddr);
312 //=============================================================================
314 int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr)
318 if (developer_networking.integer)
320 Con_Printf("UDP_Write(%i, %p, %i, <%s>)\n", socket, buf, len, UDP_AddrToString(addr));
321 Com_HexDumpToConsole(buf, len);
324 ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
328 int e = WSAGetLastError();
329 if (e == WSAEWOULDBLOCK)
331 Con_Printf("UDP_Write(%i, %p, %i, <%s>): WSAGetLastError == %i\n", socket, buf, len, UDP_AddrToString(addr), e);
333 if (errno == EWOULDBLOCK)
335 Con_Printf("UDP_Write(%i, %p, %i, <%s>): errno == %i (%s)\n", socket, buf, len, UDP_AddrToString(addr), errno, strerror(errno));
341 //=============================================================================
343 char *UDP_AddrToString (const struct qsockaddr *addr)
345 static char buffer[22]; // only 22 needed (3 + 1 + 3 + 1 + 3 + 1 + 3 + 1 + 5 + null)
346 unsigned char *ip = (char *)(&((struct sockaddr_in *)addr)->sin_addr.s_addr);
347 sprintf(buffer, "%d.%d.%d.%d:%d", ip[0], ip[1], ip[2], ip[3], ntohs(((struct sockaddr_in *)addr)->sin_port));
351 //=============================================================================
353 int UDP_StringToAddr (const char *string, struct qsockaddr *addr)
355 int ha[4], hp, ipaddr, j, numbers;
359 colon = strrchr(string, ':');
362 hp = atoi(colon + 1);
366 numbers = sscanf(string, "%d.%d.%d.%d", &ha[0], &ha[1], &ha[2], &ha[3]);
367 for (ipaddr = 0, j = 0;j < numbers;j++)
368 ipaddr = (ipaddr << 8) | ha[j];
369 // if the address is incomplete take most important numbers from myAddr
371 ipaddr |= ntohl(myAddr.i) & (-1 << (numbers * 8));
373 addr->sa_family = AF_INET;
374 ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
375 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp);
376 if (ipaddr == INADDR_ANY)
382 //=============================================================================
384 int UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
387 int addrlen = sizeof(struct qsockaddr);
388 memset(addr, 0, sizeof(struct qsockaddr));
389 ret = getsockname(socket, (struct sockaddr *)addr, &addrlen);
393 int e = WSAGetLastError();
394 Con_Printf("UDP_GetSocketAddr: WASGetLastError == %i\n", e);
396 Con_Printf("UDP_GetSocketAddr: errno == %i (%s)\n", errno, strerror(errno));
402 //=============================================================================
404 int UDP_GetNameFromAddr (const struct qsockaddr *addr, char *name)
406 struct hostent *hostentry;
408 hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
411 strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
415 strcpy (name, UDP_AddrToString (addr));
419 //=============================================================================
421 int UDP_GetAddrFromName(const char *name, struct qsockaddr *addr)
423 struct hostent *hostentry;
425 if (name[0] >= '0' && name[0] <= '9')
426 return UDP_StringToAddr (name, addr);
428 hostentry = gethostbyname (name);
432 addr->sa_family = AF_INET;
433 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport);
434 ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
439 //=============================================================================
441 int UDP_AddrCompare (const struct qsockaddr *addr1, const struct qsockaddr *addr2)
443 if (addr1->sa_family != addr2->sa_family)
446 if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
449 if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
455 //=============================================================================
457 int UDP_GetSocketPort (struct qsockaddr *addr)
459 return ntohs(((struct sockaddr_in *)addr)->sin_port);
463 int UDP_SetSocketPort (struct qsockaddr *addr, int port)
465 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port);