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