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