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