]> icculus.org git repositories - btb/d2x.git/blob - main/ipx_drv.c
Imported from d1x
[btb/d2x.git] / main / ipx_drv.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 <sys/types.h>
17 #include <sys/time.h>
18
19 #include "types.h"
20 #include "config.h"
21 #include "args.h"
22 #include "text.h"
23
24 #include "network.h"
25 #include "mono.h"
26
27 #include "ipx.h"
28 #include "ipx_drv.h"
29 //added 05/17/99 Matt Mueller - needed to redefine FD_* so that no asm is used
30 #include "checker.h"
31 //end addition -MM
32 #define MAX_IPX_DATA 576
33
34 ubyte broadcast_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
35 ubyte null_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
36
37 //int ipx_fd;
38 //ipx_socket_t ipx_socket_data;
39 ubyte ipx_installed=0;
40 ubyte ipx_atexit_installed=0;
41 //ushort ipx_socket = 0;
42 u_int32_t 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 void ipx_close(void);
61
62 int ipx_general_PacketReady(int fd) {
63         fd_set set;
64         struct timeval tv;
65         
66         FD_ZERO(&set);
67         FD_SET(fd, &set);
68         tv.tv_sec = tv.tv_usec = 0;
69         if (select(fd + 1, &set, NULL, NULL, &tv) > 0)
70                 return 1;
71         else
72                 return 0;
73 }
74
75 struct ipx_driver *driver = NULL;
76
77 ubyte * ipx_get_my_server_address()
78 {
79         return (ubyte *)&ipx_network;
80 }
81
82 ubyte * ipx_get_my_local_address()
83 {
84         return (ubyte *)(ipx_MyAddress + 4);
85 }
86
87 //---------------------------------------------------------------
88 // Initializes all IPX internals. 
89 // If socket_number==0, then opens next available socket.
90 // Returns:     0  if successful.
91 //                              -1 if socket already open.
92 //                              -2      if socket table full.
93 //                              -3 if IPX not installed.
94 //                              -4 if couldn't allocate low dos memory
95 //                              -5 if error with getting internetwork address
96 int ipx_init( int socket_number )
97 {
98         int i;
99         if (!driver) return -1;
100         memset(ipx_MyAddress,0,10);
101         if ((i = FindArg("-ipxnetwork")) && Args[i + 1]) {
102                 unsigned long n = strtol(Args[i + 1], NULL, 16);
103                 ipx_MyAddress[0] = n >> 24; ipx_MyAddress[1] = (n >> 16) & 255;
104                 ipx_MyAddress[2] = (n >> 8) & 255; ipx_MyAddress[3] = n & 255;
105                 printf("IPX: Using network %08x\n", (unsigned int)n);
106         }
107         if (driver->OpenSocket(socket_number)) {
108                 return -3;
109         }
110
111 //      if (driver->GetMyAddress)
112 //              driver->GetMyAddress();
113         memcpy(&ipx_network, ipx_MyAddress, 4);
114         Ipx_num_networks = 0;
115         memcpy( &Ipx_networks[Ipx_num_networks++], &ipx_network, 4 );
116         ipx_installed = 1;
117         if (ipx_atexit_installed==0){
118                 atexit(ipx_close);
119                 ipx_atexit_installed=1;
120         }
121         return 0;
122 }
123
124 void ipx_close()
125 {
126         if (ipx_installed)
127                 driver->CloseSocket();
128         ipx_installed = 0;
129 }
130
131 //########printfs??
132 int ipx_set_driver(char *arg)
133 {
134         ipx_close();
135
136         if (!FindArg( "-nonetwork" ))   {
137                 int socket=0;
138                 int ipx_error;
139                 int t;
140                 if (Inferno_verbose) printf( "\n%s ", TXT_INITIALIZING_NETWORK);
141                 if ((t=FindArg("-socket")))
142                         socket = atoi( Args[t+1] );
143
144                 if ((t=FindArg("-pps")) && (t = atoi(Args[t+1])) && (t >= 2) && (t <= 20)) {
145                         Network_initial_pps = t;
146                 }
147                 if ( FindArg("-shortpackets") )
148                         Network_initial_shortpackets = 1;
149
150 #ifdef SUPPORTS_NET_IP
151                 if (strcmp(arg,"ip")==0){
152                         driver=&ipx_ip;
153                 }else
154 #endif
155                         driver=arch_ipx_set_driver(arg);
156                 if ((ipx_error=ipx_init(IPX_DEFAULT_SOCKET+socket))==0) {
157                         if (Inferno_verbose) printf( "%s %d.\n", TXT_IPX_CHANNEL, socket );
158                         Network_active = 1;
159                 } else {
160                         switch( ipx_error )     {
161                                 case 3:         if (Inferno_verbose) printf( "%s\n", TXT_NO_NETWORK); break;
162                                 case -2: if (Inferno_verbose) printf( "%s 0x%x.\n", TXT_SOCKET_ERROR, IPX_DEFAULT_SOCKET+socket); break;
163                                 case -4: if (Inferno_verbose) printf( "%s\n", TXT_MEMORY_IPX ); break;
164                                 default:
165                                                          if (Inferno_verbose) printf( "%s %d", TXT_ERROR_IPX, ipx_error );
166                         }
167                         if (Inferno_verbose) printf( "%s\n",TXT_NETWORK_DISABLED);
168                         Network_active = 0;             // Assume no network
169                 }
170                 ipx_read_user_file( "descent.usr" );
171                 ipx_read_network_file( "descent.net" );
172                 //if ( FindArg( "-dynamicsockets" ))
173                 //        Network_allow_socket_changes = 1;
174                 //else
175                 //        Network_allow_socket_changes = 0;
176         } else {
177                 if (Inferno_verbose) printf( "%s\n", TXT_NETWORK_DISABLED);
178                 Network_active = 0;             // Assume no network
179         }
180
181         return ipx_installed?0:-1;
182 }
183
184 int ipx_get_packet_data( ubyte * data )
185 {
186         if (driver->GetPacketData)
187                 return driver->GetPacketData(data);
188         else {
189                 struct ipx_recv_data rd;
190                 char *buf;
191                 int size;
192                 if (driver->usepacketnum)
193                         buf=alloca(MAX_IPX_DATA);
194                 else
195                         buf=data;
196                 //edited 04/12/99 Matt Mueller - duh, we don't want to throw all that data away!
197                 while (driver->PacketReady()) {
198                         if ((size =     driver->ReceivePacket(buf, MAX_IPX_DATA, &rd)) > 4) {
199                                 if (!memcmp(rd.src_network, ipx_MyAddress, 10)) {
200                                         mprintf((0,"dumped my own packet\n"));
201                                         continue;       /* don't get own pkts */
202                                 }
203                                 if (driver->usepacketnum){
204                                         memcpy(data, buf + 4, size - 4);
205                                         return size-4;
206                                 }else{
207                                         return size;
208                                 }
209                         }
210                 }
211                 return 0;
212         }
213 }
214
215 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
216 {
217         if (driver->SendPacketData)
218                 driver->SendPacketData(data,datasize,network,address,immediate_address);
219         else{
220                 IPXPacket_t ipx_header;
221
222                 memcpy(ipx_header.Destination.Network, network, 4);
223                 memcpy(ipx_header.Destination.Node, immediate_address, 6);
224 //              *(ushort *)ipx_header.Destination.Socket = htons(ipx_socket_data.socket);
225                 ipx_header.PacketType = 4; /* Packet Exchange */
226
227                 if (driver->usepacketnum){
228                         ubyte buf[MAX_IPX_DATA];
229                         *(uint *)buf = ipx_packetnum++;
230
231                         memcpy(buf + 4, data, datasize);
232                         driver->SendPacket(&ipx_header, buf, datasize + 4);
233                 }else
234                         driver->SendPacket(&ipx_header, data, datasize);//we can save 4 bytes
235         }
236 }
237
238 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
239 {
240         if (driver->GetLocalTarget)
241                 driver->GetLocalTarget(server,node,local_target);
242         else
243         // let's hope Linux knows how to route it
244                 memcpy( local_target, node, 6 );
245 }
246
247 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )       
248 {
249         int i, j;
250         ubyte local_address[6];
251
252         // Set to all networks besides mine
253         for (i=0; i<Ipx_num_networks; i++ )     {
254                 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) )      {
255                         ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast_addr, local_address );
256                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast_addr, local_address );
257                 } else {
258                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast_addr, broadcast_addr );
259                 }
260         }
261
262         //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast_addr, broadcast_addr );
263
264         // Send directly to all users not on my network or in the network list.
265         for (i=0; i<Ipx_num_users; i++ )        {
266                 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) )  {
267                         for (j=0; j<Ipx_num_networks; j++ )             {
268                                 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
269                                         goto SkipUser;
270                         }
271                         ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
272 SkipUser:
273                         j = 0;
274                 }
275         }
276 }
277
278 // Sends a non-localized packet... needs 4 byte server, 6 byte address
279 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
280 {
281         ubyte local_address[6];
282
283         if ( (*(uint *)server) != 0 )   {
284                 ipx_get_local_target( server, address, local_address );
285                 ipx_send_packet_data( data, datasize, server, address, local_address );
286         } else {
287                 // Old method, no server info.
288                 ipx_send_packet_data( data, datasize, server, address, address );
289         }
290 }
291
292 int ipx_check_ready_to_join(ubyte *server, ubyte *node){
293         if (!driver->CheckReadyToJoin)
294                 return 1;
295         return driver->CheckReadyToJoin(server,node);
296 }
297
298
299 int ipx_change_default_socket( ushort socket_number )
300 {
301         if ( !ipx_installed ) return -3;
302
303         driver->CloseSocket();
304         if (driver->OpenSocket(socket_number)) {
305                 return -3;
306         }
307         return 0;
308 }
309
310 void ipx_read_user_file(char * filename)
311 {
312         FILE * fp;
313         user_address tmp;
314         char temp_line[132], *p1;
315         int n, ln=0, x;
316
317         if (!filename) return;
318
319         Ipx_num_users = 0;
320
321         fp = fopen( filename, "rt" );
322         if ( !fp ) return;
323
324         printf( "Broadcast Users:\n" );
325
326         while (fgets(temp_line, 132, fp)) {
327                 ln++;
328                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
329                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
330 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
331                 if (strlen(temp_line) >= 21 && temp_line[8] == '/') {
332                         for (n = 0; n < 4; n++) {
333                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
334                                         break;
335                                 tmp.network[n] = x;
336                         }
337                         if (n != 4)
338                                 continue;
339                         for (n = 0; n < 6; n++) {
340                                 if (sscanf(temp_line + 9 + n * 2, "%2x", &x) != 1)
341                                         break;
342                                 tmp.node[n] = x;
343                         }
344                         if (n != 6)
345                                 continue;
346                 } else
347                         continue;
348 #else
349                 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] );
350                 if ( n != 10 ) continue;
351 #endif
352                 if ( Ipx_num_users < MAX_USERS )        {
353                         ubyte * ipx_real_buffer = (ubyte *)&tmp;
354                         ipx_get_local_target( tmp.network, tmp.node, tmp.address );
355                         Ipx_users[Ipx_num_users++] = tmp;
356                         printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
357                         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] );
358                 } else {
359                         printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
360                         fclose(fp);
361                         return;
362                 }
363         }
364         fclose(fp);
365 }
366
367
368 void ipx_read_network_file(char * filename)
369 {
370         FILE * fp;
371         user_address tmp;
372         char temp_line[132], *p1;
373         int i, n, ln=0, x;
374
375         if (!filename) return;
376
377         fp = fopen( filename, "rt" );
378         if ( !fp ) return;
379
380         printf( "Using Networks:\n" );
381         for (i=0; i<Ipx_num_networks; i++ )             {
382                 ubyte * n1 = (ubyte *)&Ipx_networks[i];
383                 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
384         }
385
386         while (fgets(temp_line, 132, fp)) {
387                 ln++;
388                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
389                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
390 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
391                 if (strlen(temp_line) >= 8) {
392                         for (n = 0; n < 4; n++) {
393                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
394                                         break;
395                                 tmp.network[n] = x;
396                         }
397                         if (n != 4)
398                                 continue;
399                 } else
400                         continue;
401 #else
402                 n = sscanf( temp_line, "%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3] );
403                 if ( n != 4 ) continue;
404 #endif
405                 if ( Ipx_num_networks < MAX_NETWORKS  ) {
406                         int j;
407                         for (j=0; j<Ipx_num_networks; j++ )     
408                                 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
409                                         break;
410                         if ( j >= Ipx_num_networks )    {
411                                 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
412                                 printf("  %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
413                         }
414                 } else {
415                         printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
416                         fclose(fp);
417                         return;
418                 }
419         }
420         fclose(fp);
421 }