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