]> icculus.org git repositories - btb/d2x.git/blob - arch/win32/winnet.c
Make network architecture choosable from game menu, add support for UDP/IP on Mac...
[btb/d2x.git] / arch / win32 / winnet.c
1 /* $Id: winnet.c,v 1.6 2003-10-03 07:58:14 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Win32 lower-level network code.
18  * implements functions declared in include/ipx.h
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <winsock.h>
30
31 #include "args.h"
32 #include "ipx_drv.h"
33
34 extern struct ipx_driver ipx_win;
35
36 #define MAX_IPX_DATA 576
37
38 int ipx_fd;
39 ipx_socket_t ipx_socket_data;
40 ubyte ipx_installed=0;
41 ushort ipx_socket = 0;
42 uint ipx_network = 0;
43 ubyte ipx_MyAddress[10];
44 int ipx_packetnum = 0;                  /* Sequence number */
45
46 /* User defined routing stuff */
47 typedef struct user_address {
48         ubyte network[4];
49         ubyte node[6];
50         ubyte address[6];
51 } user_address;
52 #define MAX_USERS 64
53 int Ipx_num_users = 0;
54 user_address Ipx_users[MAX_USERS];
55
56 #define MAX_NETWORKS 64
57 int Ipx_num_networks = 0;
58 uint Ipx_networks[MAX_NETWORKS];
59
60 int ipx_general_PacketReady(ipx_socket_t *s) {
61         fd_set set;
62         struct timeval tv;
63         
64         FD_ZERO(&set);
65         FD_SET(s->fd, &set);
66         tv.tv_sec = tv.tv_usec = 0;
67         if (select(FD_SETSIZE, &set, NULL, NULL, &tv) > 0)
68                 return 1;
69         else
70                 return 0;
71 }
72
73 struct ipx_driver *driver = &ipx_win;
74
75 ubyte * ipx_get_my_server_address()
76 {
77         return (ubyte *)&ipx_network;
78 }
79
80 ubyte * ipx_get_my_local_address()
81 {
82         return (ubyte *)(ipx_MyAddress + 4);
83 }
84
85 void arch_ipx_set_driver(int ipx_driver)
86 {
87         if (ipx_driver != IPX_DRIVER_IPX)
88                 Warning("Unknown network driver! Defaulting to real IPX");
89
90         driver = &ipx_win;
91 }
92
93 int ipx_init(int socket_number)
94 {
95         int i;
96
97         WORD wVersionRequested;
98         WSADATA wsaData;
99
100         wVersionRequested = MAKEWORD(2, 0);
101         if (WSAStartup( wVersionRequested, &wsaData))
102         {
103           return IPX_SOCKET_ALREADY_OPEN;
104         }
105
106 #if 0
107         if ( LOBYTE( wsaData.wVersion ) != 2 ||
108           HIBYTE( wsaData.wVersion ) != 0 ) {
109            /* We couldn't find a usable WinSock DLL. */
110            WSACleanup( );
111            return IPX_SOCKET_TABLE_FULL;
112         }
113 #endif
114
115         if ((i = FindArg("-ipxnetwork")) && Args[i + 1]) {
116                 unsigned long n = strtol(Args[i + 1], NULL, 16);
117                 ipx_MyAddress[0] = (unsigned char)n >> 24; ipx_MyAddress[1] = (unsigned char)(n >> 16) & 255;
118                 ipx_MyAddress[2] = (unsigned char)(n >> 8) & 255; ipx_MyAddress[3] = (unsigned char)n & 255;
119                 printf("IPX: Using network %08x\n", (int) n);
120         }
121         if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
122                 return IPX_NOT_INSTALLED;
123         }
124         driver->GetMyAddress();
125         memcpy(&ipx_network, ipx_MyAddress, 4);
126         Ipx_num_networks = 0;
127         memcpy( &Ipx_networks[Ipx_num_networks++], &ipx_network, 4 );
128         ipx_installed = 1;
129         atexit(ipx_close);
130         printf("ipx succesfully installed\n");
131         return IPX_INIT_OK;
132 }
133
134 void ipx_close()
135 {
136         if (ipx_installed) {
137                 WSACleanup();
138                 driver->CloseSocket(&ipx_socket_data);
139         }
140         ipx_installed = 0;
141 }
142
143 int ipx_get_packet_data( ubyte * data )
144 {
145         struct ipx_recv_data rd;
146         char buf[MAX_IPX_DATA];
147         uint best_id = 0;
148         uint pkt_num;
149         int size;
150         int best_size = 0;
151         
152         // Like the original, only take latest packet, throw away rest
153         while (driver->PacketReady(&ipx_socket_data)) {
154                 if ((size = 
155                      driver->ReceivePacket(&ipx_socket_data, buf, 
156                       sizeof(buf), &rd)) > 4) {
157                      if (!memcmp(rd.src_network, ipx_MyAddress, 10)) 
158                         continue;       /* don't get own pkts */
159                      pkt_num = *(uint *)buf;
160                      if (pkt_num >= best_id) {
161                         memcpy(data, buf + 4, size - 4);
162                         best_id = pkt_num;
163                         best_size = size - 4;
164                      }
165                 }
166         }
167         return best_size;
168 }
169
170 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
171 {
172         u_char buf[MAX_IPX_DATA];
173         IPXPacket_t ipx_header;
174         
175         memcpy(ipx_header.Destination.Network, network, 4);
176         memcpy(ipx_header.Destination.Node, immediate_address, 6);
177         *(u_short *)ipx_header.Destination.Socket = htons(ipx_socket_data.socket);
178         ipx_header.PacketType = 4; /* Packet Exchange */
179         *(uint *)buf = ipx_packetnum++;
180         memcpy(buf + 4, data, datasize);
181         driver->SendPacket(&ipx_socket_data, &ipx_header, buf, datasize + 4);
182 }
183
184 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
185 {
186         // let's hope Linux knows how to route it
187         memcpy( local_target, node, 6 );
188 }
189
190 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )       
191 {
192         int i, j;
193         ubyte broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
194         ubyte local_address[6];
195
196         // Set to all networks besides mine
197         for (i=0; i<Ipx_num_networks; i++ )     {
198                 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) )      {
199                         ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast, local_address );
200                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, local_address );
201                 } else {
202                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, broadcast );
203                 }
204         }
205
206         //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast, broadcast );
207
208         // Send directly to all users not on my network or in the network list.
209         for (i=0; i<Ipx_num_users; i++ )        {
210                 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) )  {
211                         for (j=0; j<Ipx_num_networks; j++ )             {
212                                 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
213                                         goto SkipUser;
214                         }
215                         ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
216 SkipUser:
217                         j = 0;
218                 }
219         }
220 }
221
222 // Sends a non-localized packet... needs 4 byte server, 6 byte address
223 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
224 {
225         ubyte local_address[6];
226
227         if ( (*(uint *)server) != 0 )   {
228                 ipx_get_local_target( server, address, local_address );
229                 ipx_send_packet_data( data, datasize, server, address, local_address );
230         } else {
231                 // Old method, no server info.
232                 ipx_send_packet_data( data, datasize, server, address, address );
233         }
234 }
235
236 int ipx_change_default_socket( ushort socket_number )
237 {
238         if ( !ipx_installed ) return -3;
239
240         driver->CloseSocket(&ipx_socket_data);
241         if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
242                 return -3;
243         }
244         return 0;
245 }
246
247 void ipx_read_user_file(char * filename)
248 {
249         FILE * fp;
250         user_address tmp;
251         char temp_line[132], *p1;
252         int n, ln=0, x;
253
254         if (!filename) return;
255
256         Ipx_num_users = 0;
257
258         fp = fopen( filename, "rt" );
259         if ( !fp ) return;
260
261         printf( "Broadcast Users:\n" );
262
263         while (fgets(temp_line, 132, fp)) {
264                 ln++;
265                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
266                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
267 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
268                 if (strlen(temp_line) >= 21 && temp_line[8] == '/') {
269                         for (n = 0; n < 4; n++) {
270                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
271                                         break;
272                                 tmp.network[n] = x;
273                         }
274                         if (n != 4)
275                                 continue;
276                         for (n = 0; n < 6; n++) {
277                                 if (sscanf(temp_line + 9 + n * 2, "%2x", &x) != 1)
278                                         break;
279                                 tmp.node[n] = x;
280                         }
281                         if (n != 6)
282                                 continue;
283                 } else
284                         continue;
285 #else
286                 n = sscanf( temp_line, "%2x%2x%2x%2x/%2x%2x%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3], &tmp.node[0], &tmp.node[1], &tmp.node[2],&tmp.node[3], &tmp.node[4], &tmp.node[5] );
287                 if ( n != 10 ) continue;
288 #endif
289                 if ( Ipx_num_users < MAX_USERS )        {
290                         ubyte * ipx_real_buffer = (ubyte *)&tmp;
291                         ipx_get_local_target( tmp.network, tmp.node, tmp.address );
292                         Ipx_users[Ipx_num_users++] = tmp;
293                         printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
294                         printf( "%02X%02X%02X%02X%02X%02X\n", ipx_real_buffer[4],ipx_real_buffer[5],ipx_real_buffer[6],ipx_real_buffer[7],ipx_real_buffer[8],ipx_real_buffer[9] );
295                 } else {
296                         printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
297                         fclose(fp);
298                         return;
299                 }
300         }
301         fclose(fp);
302 }
303
304
305 void ipx_read_network_file(char * filename)
306 {
307         FILE * fp;
308         user_address tmp;
309         char temp_line[132], *p1;
310         int i, n, ln=0, x;
311
312         if (!filename) return;
313
314         fp = fopen( filename, "rt" );
315         if ( !fp ) return;
316
317         printf( "Using Networks:\n" );
318         for (i=0; i<Ipx_num_networks; i++ )             {
319                 ubyte * n1 = (ubyte *)&Ipx_networks[i];
320                 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
321         }
322
323         while (fgets(temp_line, 132, fp)) {
324                 ln++;
325                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
326                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
327 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
328                 if (strlen(temp_line) >= 8) {
329                         for (n = 0; n < 4; n++) {
330                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
331                                         break;
332                                 tmp.network[n] = x;
333                         }
334                         if (n != 4)
335                                 continue;
336                 } else
337                         continue;
338 #else
339                 n = sscanf( temp_line, "%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3] );
340                 if ( n != 4 ) continue;
341 #endif
342                 if ( Ipx_num_networks < MAX_NETWORKS  ) {
343                         int j;
344                         for (j=0; j<Ipx_num_networks; j++ )     
345                                 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
346                                         break;
347                         if ( j >= Ipx_num_networks )    {
348                                 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
349                                 printf("  %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
350                         }
351                 } else {
352                         printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
353                         fclose(fp);
354                         return;
355                 }
356         }
357         fclose(fp);
358 }