This commit was generated by cvs2svn to compensate for changes in r2,
[btb/d2x.git] / arch / dos / 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  * $Source: /cvs/cvsroot/d2x/arch/dos/ipx.c,v $
15  * $Revision: 1.1.1.1 $
16  * $Author: bradleyb $
17  * $Date: 2001-01-19 03:30:15 $
18  * 
19  * Routines for IPX communications.
20  * 
21  * $Log: not supported by cvs2svn $
22  * Revision 1.1.1.1  1999/06/14 21:58:22  donut
23  * Import of d1x 1.37 source.
24  *
25  * Revision 2.10  1995/03/29  17:27:55  john
26  * Added code to not duplicate broadcasts.
27  * 
28  * Revision 2.9  1995/03/29  15:32:07  john
29  * NEatened.
30  * 
31  * Revision 2.8  1995/03/29  15:07:31  john
32  * Added network support.
33  * 
34  * Revision 2.7  1995/03/29  11:22:27  john
35  * Added broadcasting over a net.
36  * 
37  * Revision 2.6  1995/03/28  20:05:03  john
38  * Took away alternate server stuff.
39  * 
40  * Revision 2.5  1995/03/23  19:14:29  john
41  * Added better MAX_USERS message.
42  * 
43  * Revision 2.4  1995/03/23  19:00:02  john
44  * Added user list capabitly.
45  * 
46  * Revision 2.3  1995/03/23  12:27:05  john
47  * Move IPX into bios lib.
48  * 
49  * Revision 2.2  1995/03/22  19:08:15  john
50  * Added code to fix sending packets over router... now
51  * we just need to make broadcasts go over router!!
52  * 
53  * Revision 2.1  1995/03/21  08:40:16  john
54  * Ifdef'd out the NETWORK code.
55  * 
56  * Revision 2.0  1995/02/27  11:30:45  john
57  * New version 2.0, which has no anonymous unions, builds with
58  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
59  * 
60  * Revision 1.31  1995/02/16  17:35:03  john
61  * Added code to allow dynamic socket changing.
62  * 
63  * Revision 1.30  1995/02/01  21:03:45  john
64  * Lintified.
65  * 
66  * Revision 1.29  1995/01/22  14:31:05  john
67  * Added key.h
68  * 
69  * Revision 1.28  1995/01/22  14:29:26  john
70  * Added code to bypass broadcast messages.
71  * 
72  * Revision 1.27  1995/01/03  13:46:16  john
73  * Added code that should make ipx work over different servers,
74  * but ifdef'd it out with SHAREWARE in ipx.c.  I haven't tested
75  * this, and I hope it doesn't introduce net bugs.
76  * 
77  * Revision 1.26  1994/12/14  11:54:37  rob
78  * Reduce # of ipx packets.
79  * 
80  * Revision 1.25  1994/12/11  00:43:28  john
81  * Locked down DOS ipx buffers... probably not necessary, tho.
82  * 
83  * Revision 1.24  1994/12/08  15:49:58  rob
84  * Bumped up the # of ipx packets.
85  * 
86  * Revision 1.23  1994/12/04  15:23:54  rob
87  * Reduced packet # constants.
88  * 
89  * Revision 1.22  1994/11/27  23:12:39  matt
90  * Made changes for new mprintf calling convention
91  * 
92  * Revision 1.21  1994/11/19  15:15:19  mike
93  * remove unused code and data
94  * 
95  * Revision 1.20  1994/11/10  11:06:07  rob
96  * Raised number of ipx packets to 64 and buffers to 128 (for 8-way games)
97  * Removed (commented out) all the spx support stuff.
98  * 
99  * Revision 1.19  1994/11/07  17:50:12  rob
100  * Bumped up max_packets and ipx_num_packets to double their previous
101  * values.
102  * 
103  * Revision 1.18  1994/11/07  15:39:01  rob
104  * Removed temporary hacked ipx_send_object function.
105  * Replaced with a more general thing.
106  * 
107  * Revision 1.17  1994/11/05  16:08:40  rob
108  * Made a change necessary for object syncing on rejoin.
109  * 
110  * Revision 1.16  1994/11/01  15:57:12  rob
111  * Added support for SPX sockets for critical messages.
112  * 
113  * Revision 1.15  1994/10/31  19:21:01  rob
114  * Added a routine for sending objects via ipx.  Its a hack and shoukld
115  * be replaced by something more general time permitting.
116  * 
117  * Revision 1.14  1994/10/20  14:57:42  john
118  * Some test stuff.
119  * 
120  * Revision 1.13  1994/10/20  11:22:45  john
121  * Tweaked the number of waiting packets. Added code to print 
122  * total packets missed, and to limit the packet send rate to
123  * 10 packets per second unless firing.
124  * 
125  * Revision 1.12  1994/10/03  20:50:11  john
126  * Started adding sound pause functions.
127  * 
128  * Revision 1.11  1994/09/15  08:54:24  mike
129  * comment out a mprintf.
130  * 
131  * Revision 1.10  1994/09/12  19:38:20  john
132  * Made some stuff that prints to the DOS screen go to the
133  * mono instead, since it really is debugging info.
134  * 
135  * Revision 1.9  1994/09/07  13:32:03  john
136  * Made ipx_close explicitly close the socket we were using,
137  * because it seems that VLM's don't automatically close them
138  * like the older NETX stuff did (Going from Novell 3.11 driver
139  * to 3.12 anyway).
140  * 
141  * Revision 1.8  1994/09/06  19:29:20  john
142  * Added trial version of rejoin function.
143  * 
144  * Revision 1.7  1994/08/09  19:31:50  john
145  * Networking changes.
146  * 
147  * Revision 1.6  1994/08/05  16:11:41  john
148  * Psuedo working version of networking.
149  * 
150  * Revision 1.5  1994/08/04  19:17:21  john
151  * Inbetween version of network stuff.
152  * 
153  * Revision 1.4  1994/07/29  16:08:57  john
154  * *** empty log message ***
155  * 
156  * Revision 1.3  1994/07/25  12:33:31  john
157  * Network "pinging" in.
158  * 
159  * Revision 1.2  1994/07/20  15:58:28  john
160  * First installment of ipx stuff.
161  * 
162  * Revision 1.1  1994/07/19  15:42:00  john
163  * Initial revision
164  * 
165  * 
166  */
167
168 #ifdef RCS
169 static char rcsid[] = "$Id: ipx.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
170 #endif
171
172 #ifdef __GNUC__
173 #define _BORLAND_DOS_REGS 1
174 #define far
175 #endif
176
177 #include <i86.h>
178 #include <dos.h>
179 #include <stdio.h>
180 #include <string.h>
181 #include <malloc.h>
182 #include <stdlib.h>
183 #include <conio.h>
184 #include <assert.h>
185
186 #include "types.h"
187 #include "timer.h"
188 #include "ipx.h"
189 #include "error.h"
190 #include "u_dpmi.h"
191 #include "key.h"
192
193 typedef unsigned char BYTE;
194 typedef unsigned short WORD;
195 typedef unsigned long DWORD;
196
197 typedef struct local_address {
198         ubyte address[6];
199 } __pack__ local_address;
200
201 typedef struct net_address {
202         BYTE                            network_id[4];                  
203         local_address   node_id;
204         WORD                            socket_id;
205 } __pack__ net_address;
206
207 typedef struct ipx_header {
208         WORD                    checksum;
209         WORD                    length;
210         BYTE                    transport_control;
211         BYTE                    packet_type;
212         net_address     destination;
213         net_address     source;
214 } __pack__ ipx_header;
215
216 typedef struct ecb_header {
217         WORD                    link[2];
218         WORD                    esr_address[2];
219         BYTE                    in_use;
220         BYTE                    completion_code;
221         WORD                    socket_id;
222         BYTE                    ipx_reserved[14];        
223         WORD                    connection_id;
224         local_address immediate_address;
225         WORD                    fragment_count;
226         WORD                    fragment_pointer[2];
227         WORD                    fragment_size;
228 } __pack__ ecb_header;
229
230 typedef struct packet_data {
231         int                     packetnum;
232         byte                    data[IPX_MAX_DATA_SIZE];
233 } __pack__ packet_data;
234
235 typedef struct ipx_packet {
236         ecb_header      ecb;
237         ipx_header      ipx;
238         packet_data     pd;
239 } __pack__ ipx_packet;
240
241 typedef struct user_address {
242         ubyte network[4];
243         ubyte node[6];
244         ubyte address[6];
245 } __pack__ user_address;
246
247 #define MAX_USERS 64
248 int Ipx_num_users = 0;
249 user_address Ipx_users[MAX_USERS];
250
251 #define MAX_NETWORKS 64
252 int Ipx_num_networks = 0;
253 uint Ipx_networks[MAX_NETWORKS];
254
255 int ipx_packetnum = 0;
256
257 #define MAX_PACKETS 64
258
259 static packet_data packet_buffers[MAX_PACKETS];
260 static short packet_free_list[MAX_PACKETS];
261 static int num_packets = 0;
262 static int largest_packet_index = 0;
263 static short packet_size[MAX_PACKETS];
264
265 WORD ipx_socket=0;
266 ubyte ipx_installed=0;
267 WORD ipx_vector_segment;
268 WORD ipx_vector_offset;
269 ubyte ipx_socket_life = 0;      // 0=closed at prog termination, 0xff=closed when requested.
270 DWORD ipx_network = 0;
271 local_address ipx_my_node;
272 WORD ipx_num_packets=32;                // 32 Ipx packets
273 ipx_packet * packets;
274 int neterrors = 0;
275 ushort ipx_packets_selector;
276
277 ecb_header * last_ecb=NULL;
278 int lastlen=0;
279
280 void got_new_packet( ecb_header * ecb );
281 void ipx_listen_for_packet(ecb_header * ecb );
282
283 void free_packet( int id )
284 {
285         packet_buffers[id].packetnum = -1;
286         packet_free_list[ --num_packets ] = id;
287         if (largest_packet_index==id)   
288                 while ((--largest_packet_index>0) && (packet_buffers[largest_packet_index].packetnum == -1 ));
289 }
290
291 int ipx_get_packet_data( ubyte * data )
292 {
293         int i, n, best, best_id, size;
294
295         for (i=1; i<ipx_num_packets; i++ )      {
296                 if ( !packets[i].ecb.in_use )   {
297                         got_new_packet( &packets[i].ecb );
298                         packets[i].ecb.in_use = 0;
299                         ipx_listen_for_packet(&packets[i].ecb);
300                 }                       
301         }
302
303         best = -1;
304         n = 0;
305         best_id = -1;
306
307         for (i=0; i<=largest_packet_index; i++ )        {
308                 if ( packet_buffers[i].packetnum > -1 ) {
309                         n++;
310                         if ( best == -1 || (packet_buffers[i].packetnum<best) ) {
311                                 best = packet_buffers[i].packetnum;
312                                 best_id = i;
313                         }
314                 }                       
315         }
316
317         //mprintf( (0, "Best id = %d, pn = %d, last_ecb = %x, len=%x, ne = %d\n", best_id, best, last_ecb, lastlen, neterrors ));
318         //mprintf( (1, "<%d> ", neterrors ));
319
320         if ( best_id < 0 ) return 0;
321
322         size = packet_size[best_id];
323         memcpy( data, packet_buffers[best_id].data, size );
324         free_packet(best_id);
325
326         return size;
327 }
328
329 #ifndef __GNUC__
330 unsigned int swap_short( unsigned int short )
331 #pragma aux swap_short parm [eax] = "xchg al,ah";
332 #else
333 static inline unsigned int swap_short( unsigned int sshort ) {
334         int __retval;
335         asm("xchg %%ah,%%al" : "=a" (__retval) : "a" (sshort));
336         return __retval;
337 }
338 #endif
339
340 void got_new_packet( ecb_header * ecb )
341 {
342         ipx_packet * p;
343         int id;
344         unsigned short datasize;
345
346         datasize = 0;
347         last_ecb = ecb;
348         p = (ipx_packet *)ecb;
349
350         if ( p->ecb.in_use ) { neterrors++; return; }
351         if      ( p->ecb.completion_code )      { neterrors++; return; }
352
353         //      Error( "Recieve error %d for completion code", p->ecb.completion_code );
354         
355         if ( memcmp( &p->ipx.source.node_id, &ipx_my_node, 6 ) )        {
356                 datasize=swap_short(p->ipx.length);
357                 lastlen=datasize;
358                 datasize -= sizeof(ipx_header);
359                 // Find slot to put packet in...
360                 if ( datasize > 0 && datasize <= sizeof(packet_data) )  {
361                         if ( num_packets >= MAX_PACKETS ) {
362                                 //printf( 1, "IPX: Packet buffer overrun!!!\n" );
363                                 neterrors++;
364                                 return;
365                         }               
366                         id = packet_free_list[ num_packets++ ];
367                         if (id > largest_packet_index ) largest_packet_index = id;
368                         packet_size[id] = datasize-sizeof(int);
369                         packet_buffers[id].packetnum =  p->pd.packetnum;
370                         if ( packet_buffers[id].packetnum < 0 ) { neterrors++; return; }
371                         memcpy( packet_buffers[id].data, p->pd.data, packet_size[id] );
372                 } else {
373                         neterrors++; return;
374                 }
375         } 
376         // Repost the ecb
377         p->ecb.in_use = 0;
378         //ipx_listen_for_packet(&p->ecb);
379 }
380
381 ubyte * ipx_get_my_local_address()
382 {
383         return ipx_my_node.address;
384 }
385
386 ubyte * ipx_get_my_server_address()
387 {
388         return (ubyte *)&ipx_network;
389 }
390
391 void ipx_listen_for_packet(ecb_header * ecb )   
392 {
393         dpmi_real_regs rregs;
394         ecb->in_use = 0x1d;
395         memset(&rregs,0,sizeof(dpmi_real_regs));
396         rregs.ebx = 4;  // Listen For Packet function
397         rregs.esi = DPMI_real_offset(ecb);
398         rregs.es = DPMI_real_segment(ecb);
399         dpmi_real_int386x( 0x7A, &rregs );
400 }
401
402 void ipx_cancel_listen_for_packet(ecb_header * ecb )    
403 {
404         dpmi_real_regs rregs;
405         memset(&rregs,0,sizeof(dpmi_real_regs));
406         rregs.ebx = 6;  // IPX Cancel event
407         rregs.esi = DPMI_real_offset(ecb);
408         rregs.es = DPMI_real_segment(ecb);
409         dpmi_real_int386x( 0x7A, &rregs );
410 }
411
412
413 void ipx_send_packet(ecb_header * ecb ) 
414 {
415         dpmi_real_regs rregs;
416         memset(&rregs,0,sizeof(dpmi_real_regs));
417         rregs.ebx = 3;  // Send Packet function
418         rregs.esi = DPMI_real_offset(ecb);
419         rregs.es = DPMI_real_segment(ecb);
420         dpmi_real_int386x( 0x7A, &rregs );
421 }
422
423 typedef struct {
424         ubyte   network[4];
425         ubyte           node[6];
426         ubyte           local_target[6];
427 } __pack__ net_xlat_info;
428
429 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
430 {
431         net_xlat_info * info;
432         dpmi_real_regs rregs;
433                 
434         // Get dos memory for call...
435         info = (net_xlat_info *)dpmi_get_temp_low_buffer( sizeof(net_xlat_info) );      
436         assert( info != NULL );
437         memcpy( info->network, server, 4 );
438         memcpy( info->node, node, 6 );
439         
440         memset(&rregs,0,sizeof(dpmi_real_regs));
441
442         rregs.ebx = 2;          // Get Local Target     
443         rregs.es = DPMI_real_segment(info);
444         rregs.esi = DPMI_real_offset(info->network);
445         rregs.edi = DPMI_real_offset(info->local_target);
446
447         dpmi_real_int386x( 0x7A, &rregs );
448
449         // Save the local target...
450         memcpy( local_target, info->local_target, 6 );
451 }
452
453 void ipx_close()
454 {
455         dpmi_real_regs rregs;
456         if ( ipx_installed )    {
457                 // When using VLM's instead of NETX, the sockets don't
458                 // seem to automatically get closed, so we must explicitly
459                 // close them at program termination.
460                 ipx_installed = 0;
461                 memset(&rregs,0,sizeof(dpmi_real_regs));
462                 rregs.edx = ipx_socket;
463                 rregs.ebx = 1;  // Close socket
464                 dpmi_real_int386x( 0x7A, &rregs );
465         }
466 }
467
468
469 //---------------------------------------------------------------
470 // Initializes all IPX internals. 
471 // If socket_number==0, then opens next available socket.
472 // Returns:     0  if successful.
473 //                              -1 if socket already open.
474 //                              -2      if socket table full.
475 //                              -3 if IPX not installed.
476 //                              -4 if couldn't allocate low dos memory
477 //                              -5 if error with getting internetwork address
478
479 int ipx_init( int socket_number, int show_address )
480 {
481         dpmi_real_regs rregs;
482         ubyte *ipx_real_buffer;
483         int i;
484
485         atexit(ipx_close);
486
487         ipx_packetnum = 0;
488
489         // init packet buffers.
490         for (i=0; i<MAX_PACKETS; i++ )  {
491                 packet_buffers[i].packetnum = -1;
492                 packet_free_list[i] = i;
493         }
494         num_packets = 0;
495         largest_packet_index = 0;
496
497         // Get the IPX vector
498         memset(&rregs,0,sizeof(dpmi_real_regs));
499         rregs.eax=0x00007a00;
500         dpmi_real_int386x( 0x2f, &rregs );
501
502         if ( (rregs.eax & 0xFF) != 0xFF )       {
503                 return 3;   
504         }
505         ipx_vector_offset = rregs.edi & 0xFFFF;
506         ipx_vector_segment = rregs.es;
507         //printf( "IPX entry point at %.4x:%.4x\n", ipx_vector_segment, ipx_vector_offset );
508
509         // Open a socket for IPX
510
511         memset(&rregs,0,sizeof(dpmi_real_regs));
512         swab( (char *)&socket_number,(char *)&ipx_socket, 2 );
513         rregs.edx = ipx_socket;
514         rregs.eax = ipx_socket_life;
515         rregs.ebx = 0;  // Open socket
516         dpmi_real_int386x( 0x7A, &rregs );
517         
518         ipx_socket = rregs.edx & 0xFFFF;
519         
520         if ( rregs.eax & 0xFF ) {
521                 //mprintf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
522                 return -2;
523         }
524         
525         ipx_installed = 1;
526
527         // Find our internetwork address
528         ipx_real_buffer = dpmi_get_temp_low_buffer( 1024 );     // 1k block
529         if ( ipx_real_buffer == NULL )  {
530                 //printf( "Error allocation realmode memory\n" );
531                 return -4;
532         }
533
534         memset(&rregs,0,sizeof(dpmi_real_regs));
535         rregs.ebx = 9;          // Get internetwork address
536         rregs.esi = DPMI_real_offset(ipx_real_buffer);
537         rregs.es = DPMI_real_segment(ipx_real_buffer);
538         dpmi_real_int386x( 0x7A, &rregs );
539
540         if ( rregs.eax & 0xFF ) {
541                 //printf( "Error getting internetwork address!\n" );
542                 return -2;
543         }
544
545         memcpy( &ipx_network, ipx_real_buffer, 4 );
546         memcpy( &ipx_my_node, &ipx_real_buffer[4], 6 );
547
548         Ipx_num_networks = 0;
549         memcpy( &Ipx_networks[Ipx_num_networks++], &ipx_network, 4 );
550
551         if ( show_address )     {
552                 printf( "My IPX addresss is " );
553                 printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
554                 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] );
555                 printf( "\n" );
556         }
557
558         packets = dpmi_real_malloc( sizeof(ipx_packet)*ipx_num_packets, &ipx_packets_selector );
559         if ( packets == NULL )  {
560                 //printf( "Couldn't allocate real memory for %d packets\n", ipx_num_packets );
561                 return -4;
562         }
563 #if 0 /* adb: not needed, fails with cwsdpmi */
564         if (!dpmi_lock_region( packets, sizeof(ipx_packet)*ipx_num_packets ))   {
565                 //printf( "Couldn't lock real memory for %d packets\n", ipx_num_packets );
566                 return -4;
567         }
568 #endif
569         memset( packets, 0, sizeof(ipx_packet)*ipx_num_packets );
570
571         for (i=1; i<ipx_num_packets; i++ )      {
572                 packets[i].ecb.in_use = 0x1d;
573                 //packets[i].ecb.in_use = 0;
574                 packets[i].ecb.socket_id = ipx_socket;
575                 packets[i].ecb.fragment_count = 1;
576                 packets[i].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[i].ipx);
577                 packets[i].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[i].ipx);
578                 packets[i].ecb.fragment_size = sizeof(ipx_packet)-sizeof(ecb_header);                   //-sizeof(ecb_header);
579
580                 ipx_listen_for_packet(&packets[i].ecb);
581         }
582
583         packets[0].ecb.socket_id = ipx_socket;
584         packets[0].ecb.fragment_count = 1;
585         packets[0].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[0].ipx);
586         packets[0].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[0].ipx);
587         packets[0].ipx.packet_type = 4;         // IPX packet
588         packets[0].ipx.destination.socket_id = ipx_socket;
589 //      memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
590         memset( packets[0].ipx.destination.network_id, 0, 4 );
591
592         return 0;
593 }
594
595 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
596 {
597         assert(ipx_installed);
598
599         if ( datasize >= IPX_MAX_DATA_SIZE )    {
600                 printf( "Data too big\n" );
601 //added/replaced on 11/8/98 by Victor Rachels to stop crappage
602                 return;
603 //                exit(1);
604 //end this section replacement - VR
605         }
606
607         // Make sure no one is already sending something
608         while( packets[0].ecb.in_use )
609         {
610         }
611         
612         if (packets[0].ecb.completion_code)     {
613 //                printf( "Send error %d for completion code\n", packets[0].ecb.completion_code );
614 //added/replaced on 11/8/98 by Victor Rachels to stop crappage
615                 return;
616         //        exit(1);
617 //end this section replacement - VR
618
619         }
620
621         // Fill in destination address
622         if ( memcmp( network, &ipx_network, 4 ) )
623                 memcpy( packets[0].ipx.destination.network_id, network, 4 );
624         else
625                 memset( packets[0].ipx.destination.network_id, 0, 4 );
626         memcpy( packets[0].ipx.destination.node_id.address, address, 6 );
627         memcpy( packets[0].ecb.immediate_address.address, immediate_address, 6 );
628         packets[0].pd.packetnum = ipx_packetnum++;
629
630         // Fill in data to send
631         packets[0].ecb.fragment_size = sizeof(ipx_header) + sizeof(int) + datasize;
632
633         assert( datasize > 1 );
634         assert( packets[0].ecb.fragment_size <= 576 );
635
636         memcpy( packets[0].pd.data, data, datasize );
637
638         // Send it
639         ipx_send_packet( &packets[0].ecb );
640
641 }
642
643 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )       
644 {
645         int i, j;
646         ubyte broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
647         ubyte local_address[6];
648
649         // Set to all networks besides mine
650         for (i=0; i<Ipx_num_networks; i++ )     {
651                 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) )      {
652                         ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast, local_address );
653                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, local_address );
654                 } else {
655                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, broadcast );
656                 }
657         }
658
659         //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast, broadcast );
660
661         // Send directly to all users not on my network or in the network list.
662         for (i=0; i<Ipx_num_users; i++ )        {
663                 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) )  {
664                         for (j=0; j<Ipx_num_networks; j++ )             {
665                                 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
666                                         goto SkipUser;
667                         }
668                         ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
669 SkipUser:
670                         j = 0;
671                 }
672         }
673 }
674
675 // Sends a non-localized packet... needs 4 byte server, 6 byte address
676 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
677 {
678         ubyte local_address[6];
679
680         if ( (*(uint *)server) != 0 )   {
681                 ipx_get_local_target( server, address, local_address );
682                 ipx_send_packet_data( data, datasize, server, address, local_address );
683         } else {
684                 // Old method, no server info.
685                 ipx_send_packet_data( data, datasize, server, address, address );
686         }
687 }
688
689 int ipx_change_default_socket( ushort socket_number )
690 {
691         int i;
692         WORD new_ipx_socket;
693         dpmi_real_regs rregs;
694
695         if ( !ipx_installed ) return -3;
696
697         // Open a new socket    
698         memset(&rregs,0,sizeof(dpmi_real_regs));
699         swab( (char *)&socket_number,(char *)&new_ipx_socket, 2 );
700         rregs.edx = new_ipx_socket;
701         rregs.eax = ipx_socket_life;
702         rregs.ebx = 0;  // Open socket
703         dpmi_real_int386x( 0x7A, &rregs );
704         
705         new_ipx_socket = rregs.edx & 0xFFFF;
706         
707         if ( rregs.eax & 0xFF ) {
708                 //printf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
709                 return -2;
710         }
711
712         for (i=1; i<ipx_num_packets; i++ )      {
713                 ipx_cancel_listen_for_packet(&packets[i].ecb);
714         }
715
716         // Close existing socket...
717         memset(&rregs,0,sizeof(dpmi_real_regs));
718         rregs.edx = ipx_socket;
719         rregs.ebx = 1;  // Close socket
720         dpmi_real_int386x( 0x7A, &rregs );
721
722         ipx_socket = new_ipx_socket;
723
724         // Repost all listen requests on the new socket...      
725         for (i=1; i<ipx_num_packets; i++ )      {
726                 packets[i].ecb.in_use = 0;
727                 packets[i].ecb.socket_id = ipx_socket;
728                 ipx_listen_for_packet(&packets[i].ecb);
729         }
730
731         packets[0].ecb.socket_id = ipx_socket;
732         packets[0].ipx.destination.socket_id = ipx_socket;
733
734         ipx_packetnum = 0;
735         // init packet buffers.
736         for (i=0; i<MAX_PACKETS; i++ )  {
737                 packet_buffers[i].packetnum = -1;
738                 packet_free_list[i] = i;
739         }
740         num_packets = 0;
741         largest_packet_index = 0;
742
743         return 0;
744 }
745
746 void ipx_read_user_file(char * filename)
747 {
748         FILE * fp;
749         user_address tmp;
750         char temp_line[132], *p1;
751         int n, ln=0;
752
753         if (!filename) return;
754
755         Ipx_num_users = 0;
756
757         fp = fopen( filename, "rt" );
758         if ( !fp ) return;
759
760         printf( "Broadcast Users:\n" );
761
762         while (fgets(temp_line, 132, fp)) {
763                 ln++;
764                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
765                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
766                 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] );
767                 if ( n != 10 ) continue;
768                 if ( Ipx_num_users < MAX_USERS )        {
769                         ubyte * ipx_real_buffer = (ubyte *)&tmp;
770                         ipx_get_local_target( tmp.network, tmp.node, tmp.address );
771                         Ipx_users[Ipx_num_users++] = tmp;
772                         printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
773                         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] );
774                 } else {
775                         printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
776                         fclose(fp);
777                         return;
778                 }
779         }
780         fclose(fp);
781 }
782
783
784 void ipx_read_network_file(char * filename)
785 {
786         FILE * fp;
787         user_address tmp;
788         char temp_line[132], *p1;
789         int i, n, ln=0;
790
791         if (!filename) return;
792
793         fp = fopen( filename, "rt" );
794         if ( !fp ) return;
795
796         printf( "Using Networks:\n" );
797         for (i=0; i<Ipx_num_networks; i++ )             {
798                 ubyte * n1 = (ubyte *)&Ipx_networks[i];
799                 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
800         }
801
802         while (fgets(temp_line, 132, fp)) {
803                 ln++;
804                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
805                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
806                 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] );
807                 if ( n != 4 ) continue;
808                 if ( Ipx_num_networks < MAX_NETWORKS  ) {
809                         int j;
810                         for (j=0; j<Ipx_num_networks; j++ )     
811                                 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
812                                         break;
813                         if ( j >= Ipx_num_networks )    {
814                                 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
815                                 printf("  %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
816                         }
817                 } else {
818                         printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
819                         fclose(fp);
820                         return;
821                 }
822         }
823         fclose(fp);
824
825 }
826
827 //---typedef struct rip_entry {
828 //---   uint            network;
829 //---   ushort  nhops;
830 //---   ushort  nticks;
831 //---} rip_entry;
832 //---
833 //---typedef struct rip_packet {
834 //---   ushort          operation;              //1=request, 2=response
835 //---   rip_entry       rip[50];
836 //---} rip_packet;
837 //---
838 //---
839 //---void  ipx_find_all_servers()
840 //---{
841 //---   int i;
842 //---   rip_packet * rp;
843 //---   assert(ipx_installed);
844 //---
845 //---   ipx_change_default_socket( 0x0453 );
846 //---   //      ipx_change_default_socket( 0x5304 );
847 //---
848 //---   // Make sure no one is already sending something
849 //---   while( packets[0].ecb.in_use )
850 //---   {
851 //---   }
852 //---   
853 //---   if (packets[0].ecb.completion_code)     {
854 //---           printf( "AAAA:Send error %d for completion code\n", packets[0].ecb.completion_code );
855 //---           //exit(1);
856 //---   }
857 //---
858 //---   rp = (rip_packet *)&packets[0].pd;
859 //---
860 //---   // Fill in destination address
861 //---   {
862 //---           char mzero1[] = {0,0,0,1};
863 //---           char mzero[] = {0,0,0,0,0,1};
864 //---           char immediate[6];
865 //---           //memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
866 //---           //memcpy( packets[0].ipx.destination.node_id.address, ipx_my_node.address, 6 );
867 //---
868 //---           memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
869 //---           memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
870 //---
871 //---           memcpy( packets[0].ipx.destination.socket_id, &ipx_socket, 2 );
872 //---           memcpy( packets[0].ipx.source.network_id, &ipx_network, 4 );
873 //---           memcpy( packets[0].ipx.source.node_id.address, ipx_my_node.address, 6 );
874 //---           memcpy( packets[0].ipx.source.socket_id, &ipx_socket, 2 );
875 //---           //memcpy( packets[0].ecb.immediate_address.address, ipx_my_node.address, 6 );
876 //---           //mzero1[3] = 1;
877 //---           //memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
878 //---           //mzero[5] = 1;
879 //---           //memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
880 //---           //ipx_get_local_target( mzero1, mzero, immediate );
881 //---           //memcpy( packets[0].ecb.immediate_address.address, mzero, 6 );
882 //---           //memcpy( packets[0].ecb.immediate_address.address, immediate, 6 );
883 //---           //mzero[5] = 0;
884 //---   }
885 //---
886 //---   packets[0].ipx.packet_type = 1;         // RIP packet
887 //---
888 //---   // Fill in data to send
889 //---   packets[0].ecb.fragment_size = sizeof(ipx_header) + sizeof(rip_packet);
890 //---   assert( packets[0].ecb.fragment_size <= 576 );
891 //---
892 //---   rp->operation = 0;              // Request
893 //---   for (i=0;i<50; i++)     {
894 //---           rp->rip[i].network = 0xFFFFFFFF;
895 //---           rp->rip[i].nhops = 0;
896 //---           rp->rip[i].nticks = 0;
897 //---   }
898 //---
899 //---   // Send it
900 //---   ipx_send_packet( &packets[0].ecb );
901 //---
902 //---   for (i=0;i<50; i++)     {
903 //---           if ( rp->rip[i].network != 0xFFFFFFFF )
904 //---                   printf( "Network = %8x, Hops=%d, Ticks=%d\n", rp->rip[i].network, rp->rip[i].nhops, rp->rip[i].nticks );
905 //---   }
906 //---}
907 //---
908 //---