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++)
88 if ((i = COM_CheckParm("-ip")) != 0 && i < com_argc)
89 myAddr.i = inet_addr(com_argv[i+1]);
92 myAddr.i = htonl(INADDR_ANY);
95 if (gethostname(buff, MAXHOSTNAMELEN) != -1)
97 buff[MAXHOSTNAMELEN - 1] = 0;
98 local = gethostbyname(buff);
100 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))
113 if (myAddr.i == htonl(INADDR_LOOPBACK))
114 sprintf(my_tcpip_address, "INADDR_LOOPBACK");
115 else if (myAddr.i == htonl(INADDR_ANY))
116 sprintf(my_tcpip_address, "INADDR_ANY");
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");
127 ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
128 ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = htonl(INADDR_BROADCAST);
129 ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((unsigned short)net_hostport);
131 Con_Printf("UDP Initialized\n");
132 tcpipAvailable = true;
134 return net_controlsocket;
137 //=============================================================================
139 void UDP_Shutdown (void)
142 UDP_CloseSocket (net_controlsocket);
148 //=============================================================================
150 void UDP_Listen (qboolean state)
155 if (net_acceptsocket != -1)
157 if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == -1)
158 Sys_Error ("UDP_Listen: Unable to open accept socket\n");
163 if (net_acceptsocket == -1)
165 UDP_CloseSocket (net_acceptsocket);
166 net_acceptsocket = -1;
169 //=============================================================================
171 int UDP_OpenSocket (int port)
174 struct sockaddr_in address;
176 if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
182 if (ioctlsocket (newsocket, FIONBIO, &_true) == -1)
184 closesocket (newsocket);
187 if (ioctl (newsocket, FIONBIO, &_true) == -1)
191 Sys_Error("UDP_OpenSocket: unable to do a ioctl FIONBIO on the socket\n");
195 address.sin_family = AF_INET;
196 address.sin_addr.s_addr = myAddr.i;
197 address.sin_port = htons((unsigned short)port);
198 if (bind(newsocket, (void *)&address, sizeof(address)) == -1)
201 closesocket(newsocket);
205 Sys_Error ("UDP_OpenSocket: Unable to bind to %s", UDP_AddrToString((struct qsockaddr *)&address));
211 //=============================================================================
213 int UDP_CloseSocket (int socket)
215 if (net_broadcastsocket == socket)
216 net_broadcastsocket = 0;
218 return closesocket (socket);
220 return close (socket);
224 //=============================================================================
226 int UDP_Connect (int socket, struct qsockaddr *addr)
231 //=============================================================================
233 int UDP_CheckNewConnections (void)
237 unsigned long available;
238 struct sockaddr_in from;
242 if (net_acceptsocket == -1)
246 if (recvfrom (net_acceptsocket, buf, sizeof(buf), MSG_PEEK, NULL, NULL) >= 0)
247 return net_acceptsocket;
249 if (ioctl (net_acceptsocket, FIONREAD, &available) == -1)
250 Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
252 return net_acceptsocket;
253 recvfrom (net_acceptsocket, buf, 0, 0, (struct sockaddr *) &from, &fromlen);
258 //=============================================================================
260 int UDP_Recv (qbyte *buf, int len, struct qsockaddr *addr)
262 return UDP_Read (net_acceptsocket, buf, len, addr);
265 //=============================================================================
267 int UDP_Send (qbyte *buf, int len, struct qsockaddr *addr)
269 return UDP_Write (net_acceptsocket, buf, len, addr);
272 //=============================================================================
274 int UDP_Read (int socket, qbyte *buf, int len, struct qsockaddr *addr)
276 int addrlen = sizeof (struct qsockaddr);
279 ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
283 int e = WSAGetLastError();
284 if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
286 Con_Printf("UDP_Read(%i, %p, %i, <%s>): WSAGetLastError == %i\n", socket, buf, len, UDP_AddrToString(addr), e);
288 if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
290 Con_Printf("UDP_Read(%i, %p, %i, <%s>): errno == %i (%s)\n", socket, buf, len, UDP_AddrToString(addr), errno, strerror(errno));
293 else if (developer_networking.integer)
295 Con_Printf("UDP_Read(%i, %p, %i, <%s>) = %i\n", socket, buf, len, UDP_AddrToString(addr), ret);
296 Com_HexDumpToConsole(buf, ret);
302 //=============================================================================
304 int UDP_MakeSocketBroadcastCapable (int socket)
308 // make this socket broadcast capable
309 if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
311 net_broadcastsocket = socket;
316 //=============================================================================
318 int UDP_Broadcast (int socket, qbyte *buf, int len)
322 if (socket != net_broadcastsocket)
324 if (net_broadcastsocket != 0)
325 Sys_Error("Attempted to use multiple broadcasts sockets\n");
326 ret = UDP_MakeSocketBroadcastCapable (socket);
329 Con_Printf("Unable to make socket broadcast capable\n");
334 return UDP_Write (socket, buf, len, &broadcastaddr);
337 //=============================================================================
339 int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr)
343 if (developer_networking.integer)
345 Con_Printf("UDP_Write(%i, %p, %i, <%s>)\n", socket, buf, len, UDP_AddrToString(addr));
346 Com_HexDumpToConsole(buf, len);
349 ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
353 int e = WSAGetLastError();
354 if (e == WSAEWOULDBLOCK)
356 Con_Printf("UDP_Write(%i, %p, %i, <%s>): WSAGetLastError == %i\n", socket, buf, len, UDP_AddrToString(addr), e);
358 if (errno == EWOULDBLOCK)
360 Con_Printf("UDP_Write(%i, %p, %i, <%s>): errno == %i (%s)\n", socket, buf, len, UDP_AddrToString(addr), errno, strerror(errno));
366 //=============================================================================
368 char *UDP_AddrToString (const struct qsockaddr *addr)
370 static char buffer[22]; // only 22 needed (3 + 1 + 3 + 1 + 3 + 1 + 3 + 1 + 5 + null)
371 unsigned char *ip = (char *)(&((struct sockaddr_in *)addr)->sin_addr.s_addr);
372 sprintf(buffer, "%d.%d.%d.%d:%d", ip[0], ip[1], ip[2], ip[3], ntohs(((struct sockaddr_in *)addr)->sin_port));
376 //=============================================================================
378 int UDP_StringToAddr (const char *string, struct qsockaddr *addr)
380 int ha[4], hp, ipaddr, j, numbers;
384 colon = strrchr(string, ':');
387 hp = atoi(colon + 1);
391 numbers = sscanf(string, "%d.%d.%d.%d", &ha[0], &ha[1], &ha[2], &ha[3]);
392 for (ipaddr = 0, j = 0;j < numbers;j++)
393 ipaddr = (ipaddr << 8) | ha[j];
394 // if the address is incomplete take most important numbers from myAddr
396 ipaddr |= ntohl(myAddr.i) & (-1 << (numbers * 8));
398 addr->sa_family = AF_INET;
399 ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
400 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp);
401 if (ipaddr == INADDR_ANY)
407 //=============================================================================
409 int UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
412 int addrlen = sizeof(struct qsockaddr);
413 memset(addr, 0, sizeof(struct qsockaddr));
414 ret = getsockname(socket, (struct sockaddr *)addr, &addrlen);
418 int e = WSAGetLastError();
419 Con_Printf("UDP_GetSocketAddr: WASGetLastError == %i\n", e);
421 Con_Printf("UDP_GetSocketAddr: errno == %i (%s)\n", errno, strerror(errno));
427 //=============================================================================
429 int UDP_GetNameFromAddr (const struct qsockaddr *addr, char *name)
431 struct hostent *hostentry;
433 hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
436 strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
440 strcpy (name, UDP_AddrToString (addr));
444 //=============================================================================
446 int UDP_GetAddrFromName(const char *name, struct qsockaddr *addr)
448 struct hostent *hostentry;
450 if (name[0] >= '0' && name[0] <= '9')
451 return UDP_StringToAddr (name, addr);
453 hostentry = gethostbyname (name);
457 addr->sa_family = AF_INET;
458 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport);
459 ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
464 //=============================================================================
466 int UDP_AddrCompare (const struct qsockaddr *addr1, const struct qsockaddr *addr2)
468 if (addr1->sa_family != addr2->sa_family)
471 if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
474 if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
480 //=============================================================================
482 int UDP_GetSocketPort (struct qsockaddr *addr)
484 return ntohs(((struct sockaddr_in *)addr)->sin_port);
488 int UDP_SetSocketPort (struct qsockaddr *addr, int port)
490 ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port);