]> icculus.org git repositories - btb/d2x.git/blob - arch/dos/bak/ipx.c
remove rcs tags
[btb/d2x.git] / arch / dos / bak / ipx.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
14 /*
15  *
16  * Routines for IPX communications.
17  *
18  */
19
20 #ifdef __GNUC__
21 #define _BORLAND_DOS_REGS 1
22 #define far
23 #endif
24
25 #include <i86.h>
26 #include <dos.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <malloc.h>
30 #include <stdlib.h>
31 #include <conio.h>
32 #include <assert.h>
33
34 #include "types.h"
35 #include "timer.h"
36 #include "ipx.h"
37 #include "error.h"
38 #include "u_dpmi.h"
39 #include "key.h"
40
41 typedef unsigned char BYTE;
42 typedef unsigned short WORD;
43 typedef unsigned long DWORD;
44
45 typedef struct local_address {
46         ubyte address[6];
47 } __pack__ local_address;
48
49 typedef struct net_address {
50         BYTE                            network_id[4];                  
51         local_address   node_id;
52         WORD                            socket_id;
53 } __pack__ net_address;
54
55 typedef struct ipx_header {
56         WORD                    checksum;
57         WORD                    length;
58         BYTE                    transport_control;
59         BYTE                    packet_type;
60         net_address     destination;
61         net_address     source;
62 } __pack__ ipx_header;
63
64 typedef struct ecb_header {
65         WORD                    link[2];
66         WORD                    esr_address[2];
67         BYTE                    in_use;
68         BYTE                    completion_code;
69         WORD                    socket_id;
70         BYTE                    ipx_reserved[14];        
71         WORD                    connection_id;
72         local_address immediate_address;
73         WORD                    fragment_count;
74         WORD                    fragment_pointer[2];
75         WORD                    fragment_size;
76 } __pack__ ecb_header;
77
78 typedef struct packet_data {
79         int                     packetnum;
80         byte                    data[IPX_MAX_DATA_SIZE];
81 } __pack__ packet_data;
82
83 typedef struct ipx_packet {
84         ecb_header      ecb;
85         ipx_header      ipx;
86         packet_data     pd;
87 } __pack__ ipx_packet;
88
89 typedef struct user_address {
90         ubyte network[4];
91         ubyte node[6];
92         ubyte address[6];
93 } __pack__ user_address;
94
95 #define MAX_USERS 64
96 int Ipx_num_users = 0;
97 user_address Ipx_users[MAX_USERS];
98
99 #define MAX_NETWORKS 64
100 int Ipx_num_networks = 0;
101 uint Ipx_networks[MAX_NETWORKS];
102
103 int ipx_packetnum = 0;
104
105 #define MAX_PACKETS 64
106
107 static packet_data packet_buffers[MAX_PACKETS];
108 static short packet_free_list[MAX_PACKETS];
109 static int num_packets = 0;
110 static int largest_packet_index = 0;
111 static short packet_size[MAX_PACKETS];
112
113 //added on 10/29/98 by Kevin Bently to add ipx stuff
114 WORD ipx_num_outgoing_packets = 8;
115 ipx_packet * outpackets;
116 //end this section addition - Kevin Bently
117
118 WORD ipx_socket=0;
119 ubyte ipx_installed=0;
120 WORD ipx_vector_segment;
121 WORD ipx_vector_offset;
122 ubyte ipx_socket_life = 0;      // 0=closed at prog termination, 0xff=closed when requested.
123 DWORD ipx_network = 0;
124 local_address ipx_my_node;
125 WORD ipx_num_packets=32;                // 32 Ipx packets
126 ipx_packet * packets;
127 int neterrors = 0;
128 ushort ipx_packets_selector;
129
130 ecb_header * last_ecb=NULL;
131 int lastlen=0;
132
133 void got_new_packet( ecb_header * ecb );
134 void ipx_listen_for_packet(ecb_header * ecb );
135
136 void free_packet( int id )
137 {
138         packet_buffers[id].packetnum = -1;
139         packet_free_list[ --num_packets ] = id;
140         if (largest_packet_index==id)   
141                 while ((--largest_packet_index>0) && (packet_buffers[largest_packet_index].packetnum == -1 ));
142 }
143
144 int ipx_get_packet_data( ubyte * data )
145 {
146         int i, n, best, best_id, size;
147
148         for (i=1; i<ipx_num_packets; i++ )      {
149                 if ( !packets[i].ecb.in_use )   {
150                         got_new_packet( &packets[i].ecb );
151                         packets[i].ecb.in_use = 0;
152                         ipx_listen_for_packet(&packets[i].ecb);
153                 }                       
154         }
155
156         best = -1;
157         n = 0;
158         best_id = -1;
159
160         for (i=0; i<=largest_packet_index; i++ )        {
161                 if ( packet_buffers[i].packetnum > -1 ) {
162                         n++;
163                         if ( best == -1 || (packet_buffers[i].packetnum<best) ) {
164                                 best = packet_buffers[i].packetnum;
165                                 best_id = i;
166                         }
167                 }                       
168         }
169
170         //mprintf( (0, "Best id = %d, pn = %d, last_ecb = %x, len=%x, ne = %d\n", best_id, best, last_ecb, lastlen, neterrors ));
171         //mprintf( (1, "<%d> ", neterrors ));
172
173         if ( best_id < 0 ) return 0;
174
175         size = packet_size[best_id];
176         memcpy( data, packet_buffers[best_id].data, size );
177         free_packet(best_id);
178
179         return size;
180 }
181
182 #ifndef __GNUC__
183 unsigned int swap_short( unsigned int short )
184 #pragma aux swap_short parm [eax] = "xchg al,ah";
185 #else
186 static inline unsigned int swap_short( unsigned int sshort ) {
187         int __retval;
188         asm("xchg %%ah,%%al" : "=a" (__retval) : "a" (sshort));
189         return __retval;
190 }
191 #endif
192
193 void got_new_packet( ecb_header * ecb )
194 {
195         ipx_packet * p;
196         int id;
197         unsigned short datasize;
198
199         datasize = 0;
200         last_ecb = ecb;
201         p = (ipx_packet *)ecb;
202
203         if ( p->ecb.in_use ) { neterrors++; return; }
204         if      ( p->ecb.completion_code )      { neterrors++; return; }
205
206         //      Error( "Recieve error %d for completion code", p->ecb.completion_code );
207         
208         if ( memcmp( &p->ipx.source.node_id, &ipx_my_node, 6 ) )        {
209                 datasize=swap_short(p->ipx.length);
210                 lastlen=datasize;
211                 datasize -= sizeof(ipx_header);
212                 // Find slot to put packet in...
213                 if ( datasize > 0 && datasize <= sizeof(packet_data) )  {
214                         if ( num_packets >= MAX_PACKETS ) {
215                                 //printf( 1, "IPX: Packet buffer overrun!!!\n" );
216                                 neterrors++;
217                                 return;
218                         }               
219                         id = packet_free_list[ num_packets++ ];
220                         if (id > largest_packet_index ) largest_packet_index = id;
221                         packet_size[id] = datasize-sizeof(int);
222                         packet_buffers[id].packetnum =  p->pd.packetnum;
223                         if ( packet_buffers[id].packetnum < 0 ) { neterrors++; return; }
224                         memcpy( packet_buffers[id].data, p->pd.data, packet_size[id] );
225                 } else {
226                         neterrors++; return;
227                 }
228         } 
229         // Repost the ecb
230         p->ecb.in_use = 0;
231         //ipx_listen_for_packet(&p->ecb);
232 }
233
234 ubyte * ipx_get_my_local_address()
235 {
236         return ipx_my_node.address;
237 }
238
239 ubyte * ipx_get_my_server_address()
240 {
241         return (ubyte *)&ipx_network;
242 }
243
244 void ipx_listen_for_packet(ecb_header * ecb )   
245 {
246         dpmi_real_regs rregs;
247         ecb->in_use = 0x1d;
248         memset(&rregs,0,sizeof(dpmi_real_regs));
249         rregs.ebx = 4;  // Listen For Packet function
250         rregs.esi = DPMI_real_offset(ecb);
251         rregs.es = DPMI_real_segment(ecb);
252         dpmi_real_int386x( 0x7A, &rregs );
253 }
254
255 void ipx_cancel_listen_for_packet(ecb_header * ecb )    
256 {
257         dpmi_real_regs rregs;
258         memset(&rregs,0,sizeof(dpmi_real_regs));
259         rregs.ebx = 6;  // IPX Cancel event
260         rregs.esi = DPMI_real_offset(ecb);
261         rregs.es = DPMI_real_segment(ecb);
262         dpmi_real_int386x( 0x7A, &rregs );
263 }
264
265
266 void ipx_send_packet(ecb_header * ecb ) 
267 {
268         dpmi_real_regs rregs;
269         memset(&rregs,0,sizeof(dpmi_real_regs));
270         rregs.ebx = 3;  // Send Packet function
271         rregs.esi = DPMI_real_offset(ecb);
272         rregs.es = DPMI_real_segment(ecb);
273         dpmi_real_int386x( 0x7A, &rregs );
274 }
275
276 typedef struct {
277         ubyte   network[4];
278         ubyte           node[6];
279         ubyte           local_target[6];
280 } __pack__ net_xlat_info;
281
282 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
283 {
284         net_xlat_info * info;
285         dpmi_real_regs rregs;
286                 
287         // Get dos memory for call...
288         info = (net_xlat_info *)dpmi_get_temp_low_buffer( sizeof(net_xlat_info) );      
289         assert( info != NULL );
290         memcpy( info->network, server, 4 );
291         memcpy( info->node, node, 6 );
292         
293         memset(&rregs,0,sizeof(dpmi_real_regs));
294
295         rregs.ebx = 2;          // Get Local Target     
296         rregs.es = DPMI_real_segment(info);
297         rregs.esi = DPMI_real_offset(info->network);
298         rregs.edi = DPMI_real_offset(info->local_target);
299
300         dpmi_real_int386x( 0x7A, &rregs );
301
302         // Save the local target...
303         memcpy( local_target, info->local_target, 6 );
304 }
305
306 void ipx_close()
307 {
308         dpmi_real_regs rregs;
309         if ( ipx_installed )    {
310                 // When using VLM's instead of NETX, the sockets don't
311                 // seem to automatically get closed, so we must explicitly
312                 // close them at program termination.
313                 ipx_installed = 0;
314                 memset(&rregs,0,sizeof(dpmi_real_regs));
315                 rregs.edx = ipx_socket;
316                 rregs.ebx = 1;  // Close socket
317                 dpmi_real_int386x( 0x7A, &rregs );
318         }
319 }
320
321
322 //---------------------------------------------------------------
323 // Initializes all IPX internals. 
324 // If socket_number==0, then opens next available socket.
325 // Returns one of the constants defined in include/ipx.h
326
327 int ipx_init( int socket_number, int show_address )
328 {
329         dpmi_real_regs rregs;
330         ubyte *ipx_real_buffer;
331         int i;
332
333         atexit(ipx_close);
334
335         ipx_packetnum = 0;
336
337         // init packet buffers.
338         for (i=0; i<MAX_PACKETS; i++ )  {
339                 packet_buffers[i].packetnum = -1;
340                 packet_free_list[i] = i;
341         }
342         num_packets = 0;
343         largest_packet_index = 0;
344
345         // Get the IPX vector
346         memset(&rregs,0,sizeof(dpmi_real_regs));
347         rregs.eax=0x00007a00;
348         dpmi_real_int386x( 0x2f, &rregs );
349
350         if ( (rregs.eax & 0xFF) != 0xFF )       {
351                 return IPX_NOT_INSTALLED;   
352         }
353         ipx_vector_offset = rregs.edi & 0xFFFF;
354         ipx_vector_segment = rregs.es;
355         //printf( "IPX entry point at %.4x:%.4x\n", ipx_vector_segment, ipx_vector_offset );
356
357         // Open a socket for IPX
358
359         memset(&rregs,0,sizeof(dpmi_real_regs));
360         swab( (char *)&socket_number,(char *)&ipx_socket, 2 );
361         rregs.edx = ipx_socket;
362         rregs.eax = ipx_socket_life;
363         rregs.ebx = 0;  // Open socket
364         dpmi_real_int386x( 0x7A, &rregs );
365         
366         ipx_socket = rregs.edx & 0xFFFF;
367         
368         if ( rregs.eax & 0xFF ) {
369                 //mprintf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
370                 return IPX_SOCKET_TABLE_FULL;
371         }
372         
373         ipx_installed = 1;
374
375         // Find our internetwork address
376         ipx_real_buffer = dpmi_get_temp_low_buffer( 1024 );     // 1k block
377         if ( ipx_real_buffer == NULL )  {
378                 //printf( "Error allocation realmode memory\n" );
379                 return IPX_NO_LOW_DOS_MEM;
380         }
381
382         memset(&rregs,0,sizeof(dpmi_real_regs));
383         rregs.ebx = 9;          // Get internetwork address
384         rregs.esi = DPMI_real_offset(ipx_real_buffer);
385         rregs.es = DPMI_real_segment(ipx_real_buffer);
386         dpmi_real_int386x( 0x7A, &rregs );
387
388         if ( rregs.eax & 0xFF ) {
389                 //printf( "Error getting internetwork address!\n" );
390                 return IPX_SOCKET_TABLE_FULL;
391         }
392
393         memcpy( &ipx_network, ipx_real_buffer, 4 );
394         memcpy( &ipx_my_node, &ipx_real_buffer[4], 6 );
395
396         Ipx_num_networks = 0;
397         memcpy( &Ipx_networks[Ipx_num_networks++], &ipx_network, 4 );
398
399         if ( show_address )     {
400                 printf( "My IPX addresss is " );
401                 printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
402                 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] );
403                 printf( "\n" );
404         }
405
406         packets = dpmi_real_malloc( sizeof(ipx_packet)*ipx_num_packets, &ipx_packets_selector );
407         if ( packets == NULL )  {
408                 //printf( "Couldn't allocate real memory for %d packets\n", ipx_num_packets );
409                 return IPX_NO_LOW_DOS_MEM;
410         }
411 #if 0 /* adb: not needed, fails with cwsdpmi */
412         if (!dpmi_lock_region( packets, sizeof(ipx_packet)*ipx_num_packets ))   {
413                 //printf( "Couldn't lock real memory for %d packets\n", ipx_num_packets );
414                 return IPX_NO_LOW_DOS_MEM;
415         }
416 #endif
417         memset( packets, 0, sizeof(ipx_packet)*ipx_num_packets );
418
419         for (i=1; i<ipx_num_packets; i++ )      {
420                 packets[i].ecb.in_use = 0x1d;
421                 //packets[i].ecb.in_use = 0;
422                 packets[i].ecb.socket_id = ipx_socket;
423                 packets[i].ecb.fragment_count = 1;
424                 packets[i].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[i].ipx);
425                 packets[i].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[i].ipx);
426                 packets[i].ecb.fragment_size = sizeof(ipx_packet)-sizeof(ecb_header);                   //-sizeof(ecb_header);
427
428                 ipx_listen_for_packet(&packets[i].ecb);
429         }
430
431 //added/replaced on 10/29/98 by Kevin Bently for new ipx stuff
432 //-killed-        packets[0].ecb.socket_id = ipx_socket;
433 //-killed-        packets[0].ecb.fragment_count = 1;
434 //-killed-        packets[0].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[0].ipx);
435 //-killed-        packets[0].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[0].ipx);
436 //-killed-        packets[0].ipx.packet_type = 4;         // IPX packet
437 //-killed-        packets[0].ipx.destination.socket_id = ipx_socket;
438 //-killed-//      memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
439 //-killed-        memset( packets[0].ipx.destination.network_id, 0, 4 );
440 outpackets = dpmi_real_malloc( sizeof(ipx_packet)*ipx_num_packets, &ipx_packets_selector);
441
442      for(i=0;i<ipx_num_outgoing_packets;i++)
443       {
444         outpackets[i].ecb.socket_id = ipx_socket;
445         outpackets[i].ecb.fragment_count = 1;
446         outpackets[i].ecb.fragment_pointer[0] = DPMI_real_offset(&outpackets[i].ipx);
447         outpackets[i].ecb.fragment_pointer[1] = DPMI_real_segment(&outpackets[i].ipx);
448         outpackets[i].ipx.packet_type = 4;              // IPX packet
449         outpackets[i].ipx.destination.socket_id = ipx_socket;
450         memset( outpackets[i].ipx.destination.network_id, 0, 4 );
451       }
452
453
454 //end this section replace - Kevin Bently
455         return IPX_INIT_OK;
456 }
457
458 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
459 {
460         int i;
461         ipx_packet *sendpacket = NULL;
462
463         assert(ipx_installed);
464
465         if ( datasize >= IPX_MAX_DATA_SIZE )    {
466                 printf( "Data too big\n" );
467                 exit(1);
468         }
469
470
471
472 //added/replaced on 10/29/98 by Kevin Bently for new ipx stuff
473 //-killed-        // Make sure no one is already sending something
474 //-killed-        while( packets[0].ecb.in_use )
475 //-killed-        {
476 //-killed-        }
477 //-killed- 
478 //-killed-        if (packets[0].ecb.completion_code)     {
479 //-killed-                printf( "Send error %d for completion code\n", packets[0].ecb.completion_code );
480 //-killed-               //killed on 10/25/98 by Victor Rachels to fix LAN crash
481 //-killed-               //-killed- exit(1);
482 //-killed-               //end this section kill - Victor 
483 //-killed-        }
484         while(!sendpacket)
485          for(i=0;i<ipx_num_outgoing_packets;i++)
486           if(!outpackets[i].ecb.in_use)
487            sendpacket= &outpackets[i];
488 //end this section replacement - Kevin Bently
489
490         // Fill in destination address
491         if ( memcmp( network, &ipx_network, 4 ) )
492                 memcpy( sendpacket->ipx.destination.network_id, network, 4 );
493         else
494                 memset( sendpacket->ipx.destination.network_id, 0, 4 );
495         memcpy( sendpacket->ipx.destination.node_id.address, address, 6 );
496         memcpy( sendpacket->ecb.immediate_address.address, immediate_address, 6 );
497         sendpacket->pd.packetnum = ipx_packetnum++;
498
499         // Fill in data to send
500         sendpacket->ecb.fragment_size = sizeof(ipx_header) + sizeof(int) + datasize;
501
502         assert( datasize > 1 );
503         assert( sendpacket->ecb.fragment_size <= 576 );
504
505         memcpy( sendpacket->pd.data, data, datasize );
506
507         // Send it
508         ipx_send_packet( &sendpacket->ecb );
509
510 }
511
512 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )       
513 {
514         int i, j;
515         ubyte broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
516         ubyte local_address[6];
517
518         // Set to all networks besides mine
519         for (i=0; i<Ipx_num_networks; i++ )     {
520                 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) )      {
521                         ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast, local_address );
522                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, local_address );
523                 } else {
524                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, broadcast );
525                 }
526         }
527
528         //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast, broadcast );
529
530         // Send directly to all users not on my network or in the network list.
531         for (i=0; i<Ipx_num_users; i++ )        {
532                 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) )  {
533                         for (j=0; j<Ipx_num_networks; j++ )             {
534                                 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
535                                         goto SkipUser;
536                         }
537                         ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
538 SkipUser:
539                         j = 0;
540                 }
541         }
542 }
543
544 // Sends a non-localized packet... needs 4 byte server, 6 byte address
545 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
546 {
547         ubyte local_address[6];
548
549         if ( (*(uint *)server) != 0 )   {
550                 ipx_get_local_target( server, address, local_address );
551                 ipx_send_packet_data( data, datasize, server, address, local_address );
552         } else {
553                 // Old method, no server info.
554                 ipx_send_packet_data( data, datasize, server, address, address );
555         }
556 }
557
558 int ipx_change_default_socket( ushort socket_number )
559 {
560         int i;
561         WORD new_ipx_socket;
562         dpmi_real_regs rregs;
563
564         if ( !ipx_installed ) return -3;
565
566         // Open a new socket    
567         memset(&rregs,0,sizeof(dpmi_real_regs));
568         swab( (char *)&socket_number,(char *)&new_ipx_socket, 2 );
569         rregs.edx = new_ipx_socket;
570         rregs.eax = ipx_socket_life;
571         rregs.ebx = 0;  // Open socket
572         dpmi_real_int386x( 0x7A, &rregs );
573         
574         new_ipx_socket = rregs.edx & 0xFFFF;
575         
576         if ( rregs.eax & 0xFF ) {
577                 //printf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
578                 return -2;
579         }
580
581         for (i=1; i<ipx_num_packets; i++ )      {
582                 ipx_cancel_listen_for_packet(&packets[i].ecb);
583         }
584
585         // Close existing socket...
586         memset(&rregs,0,sizeof(dpmi_real_regs));
587         rregs.edx = ipx_socket;
588         rregs.ebx = 1;  // Close socket
589         dpmi_real_int386x( 0x7A, &rregs );
590
591         ipx_socket = new_ipx_socket;
592
593         // Repost all listen requests on the new socket...      
594         for (i=1; i<ipx_num_packets; i++ )      {
595                 packets[i].ecb.in_use = 0;
596                 packets[i].ecb.socket_id = ipx_socket;
597                 ipx_listen_for_packet(&packets[i].ecb);
598         }
599
600         packets[0].ecb.socket_id = ipx_socket;
601         packets[0].ipx.destination.socket_id = ipx_socket;
602
603         ipx_packetnum = 0;
604         // init packet buffers.
605         for (i=0; i<MAX_PACKETS; i++ )  {
606                 packet_buffers[i].packetnum = -1;
607                 packet_free_list[i] = i;
608         }
609         num_packets = 0;
610         largest_packet_index = 0;
611
612         return 0;
613 }
614
615 void ipx_read_user_file(char * filename)
616 {
617         FILE * fp;
618         user_address tmp;
619         char temp_line[132], *p1;
620         int n, ln=0;
621
622         if (!filename) return;
623
624         Ipx_num_users = 0;
625
626         fp = fopen( filename, "rt" );
627         if ( !fp ) return;
628
629         printf( "Broadcast Users:\n" );
630
631         while (fgets(temp_line, 132, fp)) {
632                 ln++;
633                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
634                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
635                 n = sscanf( temp_line, "%2x%2x%2x%2x/%2x%2x%2x%2x%2x%2x",(unsigned int *)&tmp.network[0],(unsigned int *) &tmp.network[1],(unsigned int *) &tmp.network[2],(unsigned int *) &tmp.network[3],(unsigned int *) &tmp.node[0],(unsigned int *) &tmp.node[1],(unsigned int *) &tmp.node[2],(unsigned int *)&tmp.node[3],(unsigned int *) &tmp.node[4],(unsigned int *) &tmp.node[5] );
636 //                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] );
637                 if ( n != 10 ) continue;
638                 if ( Ipx_num_users < MAX_USERS )        {
639                         ubyte * ipx_real_buffer = (ubyte *)&tmp;
640                         ipx_get_local_target( tmp.network, tmp.node, tmp.address );
641                         Ipx_users[Ipx_num_users++] = tmp;
642                         printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
643                         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] );
644                 } else {
645                         printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
646                         fclose(fp);
647                         return;
648                 }
649         }
650         fclose(fp);
651 }
652
653
654 void ipx_read_network_file(char * filename)
655 {
656         FILE * fp;
657         user_address tmp;
658         char temp_line[132], *p1;
659         int i, n, ln=0;
660
661         if (!filename) return;
662
663         fp = fopen( filename, "rt" );
664         if ( !fp ) return;
665
666         printf( "Using Networks:\n" );
667         for (i=0; i<Ipx_num_networks; i++ )             {
668                 ubyte * n1 = (ubyte *)&Ipx_networks[i];
669                 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
670         }
671
672         while (fgets(temp_line, 132, fp)) {
673                 ln++;
674                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
675                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
676                 n = sscanf( temp_line, "%2x%2x%2x%2x",(unsigned int *) &tmp.network[0],(unsigned int *) &tmp.network[1],(unsigned int *) &tmp.network[2],(unsigned int *) &tmp.network[3] );
677 //                n = sscanf( temp_line, "%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3] );
678                 if ( n != 4 ) continue;
679                 if ( Ipx_num_networks < MAX_NETWORKS  ) {
680                         int j;
681                         for (j=0; j<Ipx_num_networks; j++ )     
682                                 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
683                                         break;
684                         if ( j >= Ipx_num_networks )    {
685                                 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
686                                 printf("  %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
687                         }
688                 } else {
689                         printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
690                         fclose(fp);
691                         return;
692                 }
693         }
694         fclose(fp);
695
696 }
697
698 //---typedef struct rip_entry {
699 //---   uint            network;
700 //---   ushort  nhops;
701 //---   ushort  nticks;
702 //---} rip_entry;
703 //---
704 //---typedef struct rip_packet {
705 //---   ushort          operation;              //1=request, 2=response
706 //---   rip_entry       rip[50];
707 //---} rip_packet;
708 //---
709 //---
710 //---void  ipx_find_all_servers()
711 //---{
712 //---   int i;
713 //---   rip_packet * rp;
714 //---   assert(ipx_installed);
715 //---
716 //---   ipx_change_default_socket( 0x0453 );
717 //---   //      ipx_change_default_socket( 0x5304 );
718 //---
719 //---   // Make sure no one is already sending something
720 //---   while( packets[0].ecb.in_use )
721 //---   {
722 //---   }
723 //---   
724 //---   if (packets[0].ecb.completion_code)     {
725 //---           printf( "AAAA:Send error %d for completion code\n", packets[0].ecb.completion_code );
726 //---           //exit(1);
727 //---   }
728 //---
729 //---   rp = (rip_packet *)&packets[0].pd;
730 //---
731 //---   // Fill in destination address
732 //---   {
733 //---           char mzero1[] = {0,0,0,1};
734 //---           char mzero[] = {0,0,0,0,0,1};
735 //---           char immediate[6];
736 //---           //memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
737 //---           //memcpy( packets[0].ipx.destination.node_id.address, ipx_my_node.address, 6 );
738 //---
739 //---           memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
740 //---           memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
741 //---
742 //---           memcpy( packets[0].ipx.destination.socket_id, &ipx_socket, 2 );
743 //---           memcpy( packets[0].ipx.source.network_id, &ipx_network, 4 );
744 //---           memcpy( packets[0].ipx.source.node_id.address, ipx_my_node.address, 6 );
745 //---           memcpy( packets[0].ipx.source.socket_id, &ipx_socket, 2 );
746 //---           //memcpy( packets[0].ecb.immediate_address.address, ipx_my_node.address, 6 );
747 //---           //mzero1[3] = 1;
748 //---           //memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
749 //---           //mzero[5] = 1;
750 //---           //memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
751 //---           //ipx_get_local_target( mzero1, mzero, immediate );
752 //---           //memcpy( packets[0].ecb.immediate_address.address, mzero, 6 );
753 //---           //memcpy( packets[0].ecb.immediate_address.address, immediate, 6 );
754 //---           //mzero[5] = 0;
755 //---   }
756 //---
757 //---   packets[0].ipx.packet_type = 1;         // RIP packet
758 //---
759 //---   // Fill in data to send
760 //---   packets[0].ecb.fragment_size = sizeof(ipx_header) + sizeof(rip_packet);
761 //---   assert( packets[0].ecb.fragment_size <= 576 );
762 //---
763 //---   rp->operation = 0;              // Request
764 //---   for (i=0;i<50; i++)     {
765 //---           rp->rip[i].network = 0xFFFFFFFF;
766 //---           rp->rip[i].nhops = 0;
767 //---           rp->rip[i].nticks = 0;
768 //---   }
769 //---
770 //---   // Send it
771 //---   ipx_send_packet( &packets[0].ecb );
772 //---
773 //---   for (i=0;i<50; i++)     {
774 //---           if ( rp->rip[i].network != 0xFFFFFFFF )
775 //---                   printf( "Network = %8x, Hops=%d, Ticks=%d\n", rp->rip[i].network, rp->rip[i].nhops, rp->rip[i].nticks );
776 //---   }
777 //---}
778 //---
779 //---