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