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.
27 extern cvar_t hostname;
29 #define MAXHOSTNAMELEN 256
31 static int net_acceptsocket = -1; // socket for fielding new connections
32 static int net_controlsocket;
33 static struct qsockaddr broadcastaddr;
35 extern qboolean winsock_initialized;
36 extern WSADATA winsockdata;
39 static int ipxsocket[IPXSOCKETS];
40 static int sequence[IPXSOCKETS];
42 //=============================================================================
47 char buff[MAXHOSTNAMELEN];
48 struct qsockaddr addr;
51 WORD wVersionRequested;
53 if (!COM_CheckParm ("-ipx")) // LordHavoc: changed -noipx to -ipx at Dabb's request, apparently crashs on computers without MS clients installed
56 // make sure LoadLibrary has happened successfully
57 if (!winsock_lib_initialized)
60 if (winsock_initialized == 0)
62 wVersionRequested = MAKEWORD(1, 1);
64 r = pWSAStartup (MAKEWORD(1, 1), &winsockdata);
68 Con_Printf ("Winsock initialization failed.\n");
72 winsock_initialized++;
74 for (i = 0; i < IPXSOCKETS; i++)
77 // determine my name & address
78 if (pgethostname(buff, MAXHOSTNAMELEN) == 0)
80 // if the quake hostname isn't set, set it to the machine name
81 if (strcmp(hostname.string, "UNNAMED") == 0)
83 // see if it's a text IP address (well, close enough)
84 for (p = buff; *p; p++)
85 if ((*p < '0' || *p > '9') && *p != '.')
88 // if it is a real name, strip off the domain; we only want the host
91 for (i = 0; i < 15; i++)
96 Cvar_Set ("hostname", buff);
100 if ((net_controlsocket = WIPX_OpenSocket (0)) == -1)
102 Con_Printf("WIPX_Init: Unable to open control socket\n");
103 if (--winsock_initialized == 0)
108 ((struct sockaddr_ipx *)&broadcastaddr)->sa_family = AF_IPX;
109 memset(((struct sockaddr_ipx *)&broadcastaddr)->sa_netnum, 0, 4);
110 memset(((struct sockaddr_ipx *)&broadcastaddr)->sa_nodenum, 0xff, 6);
111 ((struct sockaddr_ipx *)&broadcastaddr)->sa_socket = htons((unsigned short)net_hostport);
113 WIPX_GetSocketAddr (net_controlsocket, &addr);
114 strcpy(my_ipx_address, WIPX_AddrToString (&addr));
115 p = strrchr (my_ipx_address, ':');
119 Con_Printf("Winsock IPX Initialized\n");
122 return net_controlsocket;
125 //=============================================================================
127 void WIPX_Shutdown (void)
130 WIPX_CloseSocket (net_controlsocket);
131 if (--winsock_initialized == 0)
135 //=============================================================================
137 void WIPX_Listen (qboolean state)
142 if (net_acceptsocket != -1)
144 if ((net_acceptsocket = WIPX_OpenSocket (net_hostport)) == -1)
145 Sys_Error ("WIPX_Listen: Unable to open accept socket\n");
150 if (net_acceptsocket == -1)
152 WIPX_CloseSocket (net_acceptsocket);
153 net_acceptsocket = -1;
156 //=============================================================================
158 int WIPX_OpenSocket (int port)
162 struct sockaddr_ipx address;
165 for (handle = 0; handle < IPXSOCKETS; handle++)
166 if (ipxsocket[handle] == 0)
168 if (handle == IPXSOCKETS)
171 if ((newsocket = psocket (AF_IPX, SOCK_DGRAM, NSPROTO_IPX)) == INVALID_SOCKET)
174 if (pioctlsocket (newsocket, FIONBIO, &_true) == -1)
177 if (psetsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&_true, sizeof(_true)) < 0)
180 address.sa_family = AF_IPX;
181 memset(address.sa_netnum, 0, 4);
182 memset(address.sa_nodenum, 0, 6);;
183 address.sa_socket = htons((unsigned short)port);
184 if( bind (newsocket, (void *)&address, sizeof(address)) == 0)
186 ipxsocket[handle] = newsocket;
187 sequence[handle] = 0;
191 Sys_Error ("Winsock IPX bind failed\n");
193 pclosesocket (newsocket);
197 //=============================================================================
199 int WIPX_CloseSocket (int handle)
201 int socket = ipxsocket[handle];
204 ret = pclosesocket (socket);
205 ipxsocket[handle] = 0;
210 //=============================================================================
212 int WIPX_Connect (int handle, struct qsockaddr *addr)
217 //=============================================================================
219 int WIPX_CheckNewConnections (void)
221 unsigned long available;
223 if (net_acceptsocket == -1)
226 if (pioctlsocket (ipxsocket[net_acceptsocket], FIONREAD, &available) == -1)
227 Sys_Error ("WIPX: ioctlsocket (FIONREAD) failed\n");
229 return net_acceptsocket;
233 //=============================================================================
235 static byte packetBuffer[NET_DATAGRAMSIZE + 4];
237 int WIPX_Read (int handle, byte *buf, int len, struct qsockaddr *addr)
239 int addrlen = sizeof (struct qsockaddr);
240 int socket = ipxsocket[handle];
244 ret = precvfrom (socket, packetBuffer, len+4, 0, (struct sockaddr *)addr, &addrlen);
247 errno = pWSAGetLastError();
249 if (errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED)
257 // remove sequence number, it's only needed for DOS IPX
259 memcpy(buf, packetBuffer+4, ret);
264 //=============================================================================
266 int WIPX_Broadcast (int handle, byte *buf, int len)
268 return WIPX_Write (handle, buf, len, &broadcastaddr);
271 //=============================================================================
273 int WIPX_Write (int handle, byte *buf, int len, struct qsockaddr *addr)
275 int socket = ipxsocket[handle];
278 // build packet with sequence number
279 *(int *)(&packetBuffer[0]) = sequence[handle];
281 memcpy(&packetBuffer[4], buf, len);
284 ret = psendto (socket, packetBuffer, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
286 if (pWSAGetLastError() == WSAEWOULDBLOCK)
292 //=============================================================================
294 char *WIPX_AddrToString (struct qsockaddr *addr)
298 sprintf(buf, "%02x%02x%02x%02x:%02x%02x%02x%02x%02x%02x:%u",
299 ((struct sockaddr_ipx *)addr)->sa_netnum[0] & 0xff,
300 ((struct sockaddr_ipx *)addr)->sa_netnum[1] & 0xff,
301 ((struct sockaddr_ipx *)addr)->sa_netnum[2] & 0xff,
302 ((struct sockaddr_ipx *)addr)->sa_netnum[3] & 0xff,
303 ((struct sockaddr_ipx *)addr)->sa_nodenum[0] & 0xff,
304 ((struct sockaddr_ipx *)addr)->sa_nodenum[1] & 0xff,
305 ((struct sockaddr_ipx *)addr)->sa_nodenum[2] & 0xff,
306 ((struct sockaddr_ipx *)addr)->sa_nodenum[3] & 0xff,
307 ((struct sockaddr_ipx *)addr)->sa_nodenum[4] & 0xff,
308 ((struct sockaddr_ipx *)addr)->sa_nodenum[5] & 0xff,
309 ntohs(((struct sockaddr_ipx *)addr)->sa_socket)
314 //=============================================================================
316 int WIPX_StringToAddr (char *string, struct qsockaddr *addr)
322 memset(addr, 0, sizeof(struct qsockaddr));
323 addr->sa_family = AF_IPX;
325 #define DO(src,dest) \
326 buf[0] = string[src]; \
327 buf[1] = string[src + 1]; \
328 if (sscanf (buf, "%x", &val) != 1) \
330 ((struct sockaddr_ipx *)addr)->dest = val
336 DO(9, sa_nodenum[0]);
337 DO(11, sa_nodenum[1]);
338 DO(13, sa_nodenum[2]);
339 DO(15, sa_nodenum[3]);
340 DO(17, sa_nodenum[4]);
341 DO(19, sa_nodenum[5]);
344 sscanf (&string[22], "%u", &val);
345 ((struct sockaddr_ipx *)addr)->sa_socket = htons((unsigned short)val);
350 //=============================================================================
352 int WIPX_GetSocketAddr (int handle, struct qsockaddr *addr)
355 int socket = ipxsocket[handle];
356 int addrlen = sizeof(struct qsockaddr);
358 memset(addr, 0, sizeof(struct qsockaddr));
359 if(pgetsockname(socket, (struct sockaddr *)addr, &addrlen) != 0)
361 errno = pWSAGetLastError();
367 //=============================================================================
369 int WIPX_GetNameFromAddr (struct qsockaddr *addr, char *name)
371 strcpy(name, WIPX_AddrToString(addr));
375 //=============================================================================
377 int WIPX_GetAddrFromName(char *name, struct qsockaddr *addr)
386 sprintf(buf, "00000000:%s:%u", name, net_hostport);
387 return WIPX_StringToAddr (buf, addr);
391 sprintf(buf, "%s:%u", name, net_hostport);
392 return WIPX_StringToAddr (buf, addr);
394 if (n > 21 && n <= 27)
395 return WIPX_StringToAddr (name, addr);
400 //=============================================================================
402 int WIPX_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
404 if (addr1->sa_family != addr2->sa_family)
407 if (*((struct sockaddr_ipx *)addr1)->sa_netnum && *((struct sockaddr_ipx *)addr2)->sa_netnum)
408 if (memcmp(((struct sockaddr_ipx *)addr1)->sa_netnum, ((struct sockaddr_ipx *)addr2)->sa_netnum, 4) != 0)
410 if (memcmp(((struct sockaddr_ipx *)addr1)->sa_nodenum, ((struct sockaddr_ipx *)addr2)->sa_nodenum, 6) != 0)
413 if (((struct sockaddr_ipx *)addr1)->sa_socket != ((struct sockaddr_ipx *)addr2)->sa_socket)
419 //=============================================================================
421 int WIPX_GetSocketPort (struct qsockaddr *addr)
423 return ntohs(((struct sockaddr_ipx *)addr)->sa_socket);
427 int WIPX_SetSocketPort (struct qsockaddr *addr, int port)
429 ((struct sockaddr_ipx *)addr)->sa_socket = htons((unsigned short)port);
433 //=============================================================================