This commit was generated by cvs2svn to compensate for changes in r2,
[btb/d2x.git] / arch / dos / bak / ipx.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13 /*
14  * $Source: /cvs/cvsroot/d2x/arch/dos/bak/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 22:00:03  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 //added on 10/29/98 by Kevin Bently to add ipx stuff
266 WORD ipx_num_outgoing_packets = 8;
267 ipx_packet * outpackets;
268 //end this section addition - Kevin Bently
269
270 WORD ipx_socket=0;
271 ubyte ipx_installed=0;
272 WORD ipx_vector_segment;
273 WORD ipx_vector_offset;
274 ubyte ipx_socket_life = 0;      // 0=closed at prog termination, 0xff=closed when requested.
275 DWORD ipx_network = 0;
276 local_address ipx_my_node;
277 WORD ipx_num_packets=32;                // 32 Ipx packets
278 ipx_packet * packets;
279 int neterrors = 0;
280 ushort ipx_packets_selector;
281
282 ecb_header * last_ecb=NULL;
283 int lastlen=0;
284
285 void got_new_packet( ecb_header * ecb );
286 void ipx_listen_for_packet(ecb_header * ecb );
287
288 void free_packet( int id )
289 {
290         packet_buffers[id].packetnum = -1;
291         packet_free_list[ --num_packets ] = id;
292         if (largest_packet_index==id)   
293                 while ((--largest_packet_index>0) && (packet_buffers[largest_packet_index].packetnum == -1 ));
294 }
295
296 int ipx_get_packet_data( ubyte * data )
297 {
298         int i, n, best, best_id, size;
299
300         for (i=1; i<ipx_num_packets; i++ )      {
301                 if ( !packets[i].ecb.in_use )   {
302                         got_new_packet( &packets[i].ecb );
303                         packets[i].ecb.in_use = 0;
304                         ipx_listen_for_packet(&packets[i].ecb);
305                 }                       
306         }
307
308         best = -1;
309         n = 0;
310         best_id = -1;
311
312         for (i=0; i<=largest_packet_index; i++ )        {
313                 if ( packet_buffers[i].packetnum > -1 ) {
314                         n++;
315                         if ( best == -1 || (packet_buffers[i].packetnum<best) ) {
316                                 best = packet_buffers[i].packetnum;
317                                 best_id = i;
318                         }
319                 }                       
320         }
321
322         //mprintf( (0, "Best id = %d, pn = %d, last_ecb = %x, len=%x, ne = %d\n", best_id, best, last_ecb, lastlen, neterrors ));
323         //mprintf( (1, "<%d> ", neterrors ));
324
325         if ( best_id < 0 ) return 0;
326
327         size = packet_size[best_id];
328         memcpy( data, packet_buffers[best_id].data, size );
329         free_packet(best_id);
330
331         return size;
332 }
333
334 #ifndef __GNUC__
335 unsigned int swap_short( unsigned int short )
336 #pragma aux swap_short parm [eax] = "xchg al,ah";
337 #else
338 static inline unsigned int swap_short( unsigned int sshort ) {
339         int __retval;
340         asm("xchg %%ah,%%al" : "=a" (__retval) : "a" (sshort));
341         return __retval;
342 }
343 #endif
344
345 void got_new_packet( ecb_header * ecb )
346 {
347         ipx_packet * p;
348         int id;
349         unsigned short datasize;
350
351         datasize = 0;
352         last_ecb = ecb;
353         p = (ipx_packet *)ecb;
354
355         if ( p->ecb.in_use ) { neterrors++; return; }
356         if      ( p->ecb.completion_code )      { neterrors++; return; }
357
358         //      Error( "Recieve error %d for completion code", p->ecb.completion_code );
359         
360         if ( memcmp( &p->ipx.source.node_id, &ipx_my_node, 6 ) )        {
361                 datasize=swap_short(p->ipx.length);
362                 lastlen=datasize;
363                 datasize -= sizeof(ipx_header);
364                 // Find slot to put packet in...
365                 if ( datasize > 0 && datasize <= sizeof(packet_data) )  {
366                         if ( num_packets >= MAX_PACKETS ) {
367                                 //printf( 1, "IPX: Packet buffer overrun!!!\n" );
368                                 neterrors++;
369                                 return;
370                         }               
371                         id = packet_free_list[ num_packets++ ];
372                         if (id > largest_packet_index ) largest_packet_index = id;
373                         packet_size[id] = datasize-sizeof(int);
374                         packet_buffers[id].packetnum =  p->pd.packetnum;
375                         if ( packet_buffers[id].packetnum < 0 ) { neterrors++; return; }
376                         memcpy( packet_buffers[id].data, p->pd.data, packet_size[id] );
377                 } else {
378                         neterrors++; return;
379                 }
380         } 
381         // Repost the ecb
382         p->ecb.in_use = 0;
383         //ipx_listen_for_packet(&p->ecb);
384 }
385
386 ubyte * ipx_get_my_local_address()
387 {
388         return ipx_my_node.address;
389 }
390
391 ubyte * ipx_get_my_server_address()
392 {
393         return (ubyte *)&ipx_network;
394 }
395
396 void ipx_listen_for_packet(ecb_header * ecb )   
397 {
398         dpmi_real_regs rregs;
399         ecb->in_use = 0x1d;
400         memset(&rregs,0,sizeof(dpmi_real_regs));
401         rregs.ebx = 4;  // Listen For Packet function
402         rregs.esi = DPMI_real_offset(ecb);
403         rregs.es = DPMI_real_segment(ecb);
404         dpmi_real_int386x( 0x7A, &rregs );
405 }
406
407 void ipx_cancel_listen_for_packet(ecb_header * ecb )    
408 {
409         dpmi_real_regs rregs;
410         memset(&rregs,0,sizeof(dpmi_real_regs));
411         rregs.ebx = 6;  // IPX Cancel event
412         rregs.esi = DPMI_real_offset(ecb);
413         rregs.es = DPMI_real_segment(ecb);
414         dpmi_real_int386x( 0x7A, &rregs );
415 }
416
417
418 void ipx_send_packet(ecb_header * ecb ) 
419 {
420         dpmi_real_regs rregs;
421         memset(&rregs,0,sizeof(dpmi_real_regs));
422         rregs.ebx = 3;  // Send Packet function
423         rregs.esi = DPMI_real_offset(ecb);
424         rregs.es = DPMI_real_segment(ecb);
425         dpmi_real_int386x( 0x7A, &rregs );
426 }
427
428 typedef struct {
429         ubyte   network[4];
430         ubyte           node[6];
431         ubyte           local_target[6];
432 } __pack__ net_xlat_info;
433
434 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
435 {
436         net_xlat_info * info;
437         dpmi_real_regs rregs;
438                 
439         // Get dos memory for call...
440         info = (net_xlat_info *)dpmi_get_temp_low_buffer( sizeof(net_xlat_info) );      
441         assert( info != NULL );
442         memcpy( info->network, server, 4 );
443         memcpy( info->node, node, 6 );
444         
445         memset(&rregs,0,sizeof(dpmi_real_regs));
446
447         rregs.ebx = 2;          // Get Local Target     
448         rregs.es = DPMI_real_segment(info);
449         rregs.esi = DPMI_real_offset(info->network);
450         rregs.edi = DPMI_real_offset(info->local_target);
451
452         dpmi_real_int386x( 0x7A, &rregs );
453
454         // Save the local target...
455         memcpy( local_target, info->local_target, 6 );
456 }
457
458 void ipx_close()
459 {
460         dpmi_real_regs rregs;
461         if ( ipx_installed )    {
462                 // When using VLM's instead of NETX, the sockets don't
463                 // seem to automatically get closed, so we must explicitly
464                 // close them at program termination.
465                 ipx_installed = 0;
466                 memset(&rregs,0,sizeof(dpmi_real_regs));
467                 rregs.edx = ipx_socket;
468                 rregs.ebx = 1;  // Close socket
469                 dpmi_real_int386x( 0x7A, &rregs );
470         }
471 }
472
473
474 //---------------------------------------------------------------
475 // Initializes all IPX internals. 
476 // If socket_number==0, then opens next available socket.
477 // Returns:     0  if successful.
478 //                              -1 if socket already open.
479 //                              -2      if socket table full.
480 //                              -3 if IPX not installed.
481 //                              -4 if couldn't allocate low dos memory
482 //                              -5 if error with getting internetwork address
483
484 int ipx_init( int socket_number, int show_address )
485 {
486         dpmi_real_regs rregs;
487         ubyte *ipx_real_buffer;
488         int i;
489
490         atexit(ipx_close);
491
492         ipx_packetnum = 0;
493
494         // init packet buffers.
495         for (i=0; i<MAX_PACKETS; i++ )  {
496                 packet_buffers[i].packetnum = -1;
497                 packet_free_list[i] = i;
498         }
499         num_packets = 0;
500         largest_packet_index = 0;
501
502         // Get the IPX vector
503         memset(&rregs,0,sizeof(dpmi_real_regs));
504         rregs.eax=0x00007a00;
505         dpmi_real_int386x( 0x2f, &rregs );
506
507         if ( (rregs.eax & 0xFF) != 0xFF )       {
508                 return 3;   
509         }
510         ipx_vector_offset = rregs.edi & 0xFFFF;
511         ipx_vector_segment = rregs.es;
512         //printf( "IPX entry point at %.4x:%.4x\n", ipx_vector_segment, ipx_vector_offset );
513
514         // Open a socket for IPX
515
516         memset(&rregs,0,sizeof(dpmi_real_regs));
517         swab( (char *)&socket_number,(char *)&ipx_socket, 2 );
518         rregs.edx = ipx_socket;
519         rregs.eax = ipx_socket_life;
520         rregs.ebx = 0;  // Open socket
521         dpmi_real_int386x( 0x7A, &rregs );
522         
523         ipx_socket = rregs.edx & 0xFFFF;
524         
525         if ( rregs.eax & 0xFF ) {
526                 //mprintf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
527                 return -2;
528         }
529         
530         ipx_installed = 1;
531
532         // Find our internetwork address
533         ipx_real_buffer = dpmi_get_temp_low_buffer( 1024 );     // 1k block
534         if ( ipx_real_buffer == NULL )  {
535                 //printf( "Error allocation realmode memory\n" );
536                 return -4;
537         }
538
539         memset(&rregs,0,sizeof(dpmi_real_regs));
540         rregs.ebx = 9;          // Get internetwork address
541         rregs.esi = DPMI_real_offset(ipx_real_buffer);
542         rregs.es = DPMI_real_segment(ipx_real_buffer);
543         dpmi_real_int386x( 0x7A, &rregs );
544
545         if ( rregs.eax & 0xFF ) {
546                 //printf( "Error getting internetwork address!\n" );
547                 return -2;
548         }
549
550         memcpy( &ipx_network, ipx_real_buffer, 4 );
551         memcpy( &ipx_my_node, &ipx_real_buffer[4], 6 );
552
553         Ipx_num_networks = 0;
554         memcpy( &Ipx_networks[Ipx_num_networks++], &ipx_network, 4 );
555
556         if ( show_address )     {
557                 printf( "My IPX addresss is " );
558                 printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
559                 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] );
560                 printf( "\n" );
561         }
562
563         packets = dpmi_real_malloc( sizeof(ipx_packet)*ipx_num_packets, &ipx_packets_selector );
564         if ( packets == NULL )  {
565                 //printf( "Couldn't allocate real memory for %d packets\n", ipx_num_packets );
566                 return -4;
567         }
568 #if 0 /* adb: not needed, fails with cwsdpmi */
569         if (!dpmi_lock_region( packets, sizeof(ipx_packet)*ipx_num_packets ))   {
570                 //printf( "Couldn't lock real memory for %d packets\n", ipx_num_packets );
571                 return -4;
572         }
573 #endif
574         memset( packets, 0, sizeof(ipx_packet)*ipx_num_packets );
575
576         for (i=1; i<ipx_num_packets; i++ )      {
577                 packets[i].ecb.in_use = 0x1d;
578                 //packets[i].ecb.in_use = 0;
579                 packets[i].ecb.socket_id = ipx_socket;
580                 packets[i].ecb.fragment_count = 1;
581                 packets[i].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[i].ipx);
582                 packets[i].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[i].ipx);
583                 packets[i].ecb.fragment_size = sizeof(ipx_packet)-sizeof(ecb_header);                   //-sizeof(ecb_header);
584
585                 ipx_listen_for_packet(&packets[i].ecb);
586         }
587
588 //added/replaced on 10/29/98 by Kevin Bently for new ipx stuff
589 //-killed-        packets[0].ecb.socket_id = ipx_socket;
590 //-killed-        packets[0].ecb.fragment_count = 1;
591 //-killed-        packets[0].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[0].ipx);
592 //-killed-        packets[0].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[0].ipx);
593 //-killed-        packets[0].ipx.packet_type = 4;         // IPX packet
594 //-killed-        packets[0].ipx.destination.socket_id = ipx_socket;
595 //-killed-//      memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
596 //-killed-        memset( packets[0].ipx.destination.network_id, 0, 4 );
597 outpackets = dpmi_real_malloc( sizeof(ipx_packet)*ipx_num_packets, &ipx_packets_selector);
598
599      for(i=0;i<ipx_num_outgoing_packets;i++)
600       {
601         outpackets[i].ecb.socket_id = ipx_socket;
602         outpackets[i].ecb.fragment_count = 1;
603         outpackets[i].ecb.fragment_pointer[0] = DPMI_real_offset(&outpackets[i].ipx);
604         outpackets[i].ecb.fragment_pointer[1] = DPMI_real_segment(&outpackets[i].ipx);
605         outpackets[i].ipx.packet_type = 4;              // IPX packet
606         outpackets[i].ipx.destination.socket_id = ipx_socket;
607         memset( outpackets[i].ipx.destination.network_id, 0, 4 );
608       }
609
610
611 //end this section replace - Kevin Bently
612         return 0;
613 }
614
615 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
616 {
617         int i;
618         ipx_packet *sendpacket = NULL;
619
620         assert(ipx_installed);
621
622         if ( datasize >= IPX_MAX_DATA_SIZE )    {
623                 printf( "Data too big\n" );
624                 exit(1);
625         }
626
627
628
629 //added/replaced on 10/29/98 by Kevin Bently for new ipx stuff
630 //-killed-        // Make sure no one is already sending something
631 //-killed-        while( packets[0].ecb.in_use )
632 //-killed-        {
633 //-killed-        }
634 //-killed- 
635 //-killed-        if (packets[0].ecb.completion_code)     {
636 //-killed-                printf( "Send error %d for completion code\n", packets[0].ecb.completion_code );
637 //-killed-               //killed on 10/25/98 by Victor Rachels to fix LAN crash
638 //-killed-               //-killed- exit(1);
639 //-killed-               //end this section kill - Victor 
640 //-killed-        }
641         while(!sendpacket)
642          for(i=0;i<ipx_num_outgoing_packets;i++)
643           if(!outpackets[i].ecb.in_use)
644            sendpacket= &outpackets[i];
645 //end this section replacement - Kevin Bently
646
647         // Fill in destination address
648         if ( memcmp( network, &ipx_network, 4 ) )
649                 memcpy( sendpacket->ipx.destination.network_id, network, 4 );
650         else
651                 memset( sendpacket->ipx.destination.network_id, 0, 4 );
652         memcpy( sendpacket->ipx.destination.node_id.address, address, 6 );
653         memcpy( sendpacket->ecb.immediate_address.address, immediate_address, 6 );
654         sendpacket->pd.packetnum = ipx_packetnum++;
655
656         // Fill in data to send
657         sendpacket->ecb.fragment_size = sizeof(ipx_header) + sizeof(int) + datasize;
658
659         assert( datasize > 1 );
660         assert( sendpacket->ecb.fragment_size <= 576 );
661
662         memcpy( sendpacket->pd.data, data, datasize );
663
664         // Send it
665         ipx_send_packet( &sendpacket->ecb );
666
667 }
668
669 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )       
670 {
671         int i, j;
672         ubyte broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
673         ubyte local_address[6];
674
675         // Set to all networks besides mine
676         for (i=0; i<Ipx_num_networks; i++ )     {
677                 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) )      {
678                         ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast, local_address );
679                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, local_address );
680                 } else {
681                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, broadcast );
682                 }
683         }
684
685         //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast, broadcast );
686
687         // Send directly to all users not on my network or in the network list.
688         for (i=0; i<Ipx_num_users; i++ )        {
689                 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) )  {
690                         for (j=0; j<Ipx_num_networks; j++ )             {
691                                 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
692                                         goto SkipUser;
693                         }
694                         ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
695 SkipUser:
696                         j = 0;
697                 }
698         }
699 }
700
701 // Sends a non-localized packet... needs 4 byte server, 6 byte address
702 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
703 {
704         ubyte local_address[6];
705
706         if ( (*(uint *)server) != 0 )   {
707                 ipx_get_local_target( server, address, local_address );
708                 ipx_send_packet_data( data, datasize, server, address, local_address );
709         } else {
710                 // Old method, no server info.
711                 ipx_send_packet_data( data, datasize, server, address, address );
712         }
713 }
714
715 int ipx_change_default_socket( ushort socket_number )
716 {
717         int i;
718         WORD new_ipx_socket;
719         dpmi_real_regs rregs;
720
721         if ( !ipx_installed ) return -3;
722
723         // Open a new socket    
724         memset(&rregs,0,sizeof(dpmi_real_regs));
725         swab( (char *)&socket_number,(char *)&new_ipx_socket, 2 );
726         rregs.edx = new_ipx_socket;
727         rregs.eax = ipx_socket_life;
728         rregs.ebx = 0;  // Open socket
729         dpmi_real_int386x( 0x7A, &rregs );
730         
731         new_ipx_socket = rregs.edx & 0xFFFF;
732         
733         if ( rregs.eax & 0xFF ) {
734                 //printf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
735                 return -2;
736         }
737
738         for (i=1; i<ipx_num_packets; i++ )      {
739                 ipx_cancel_listen_for_packet(&packets[i].ecb);
740         }
741
742         // Close existing socket...
743         memset(&rregs,0,sizeof(dpmi_real_regs));
744         rregs.edx = ipx_socket;
745         rregs.ebx = 1;  // Close socket
746         dpmi_real_int386x( 0x7A, &rregs );
747
748         ipx_socket = new_ipx_socket;
749
750         // Repost all listen requests on the new socket...      
751         for (i=1; i<ipx_num_packets; i++ )      {
752                 packets[i].ecb.in_use = 0;
753                 packets[i].ecb.socket_id = ipx_socket;
754                 ipx_listen_for_packet(&packets[i].ecb);
755         }
756
757         packets[0].ecb.socket_id = ipx_socket;
758         packets[0].ipx.destination.socket_id = ipx_socket;
759
760         ipx_packetnum = 0;
761         // init packet buffers.
762         for (i=0; i<MAX_PACKETS; i++ )  {
763                 packet_buffers[i].packetnum = -1;
764                 packet_free_list[i] = i;
765         }
766         num_packets = 0;
767         largest_packet_index = 0;
768
769         return 0;
770 }
771
772 void ipx_read_user_file(char * filename)
773 {
774         FILE * fp;
775         user_address tmp;
776         char temp_line[132], *p1;
777         int n, ln=0;
778
779         if (!filename) return;
780
781         Ipx_num_users = 0;
782
783         fp = fopen( filename, "rt" );
784         if ( !fp ) return;
785
786         printf( "Broadcast Users:\n" );
787
788         while (fgets(temp_line, 132, fp)) {
789                 ln++;
790                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
791                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
792                 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] );
793 //                n = sscanf( temp_line, "%2x%2x%2x%2x/%2x%2x%2x%2x%2x%2x",&tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3], &tmp.node[0], &tmp.node[1], &tmp.node[2],&tmp.node[3], &tmp.node[4], &tmp.node[5] );
794                 if ( n != 10 ) continue;
795                 if ( Ipx_num_users < MAX_USERS )        {
796                         ubyte * ipx_real_buffer = (ubyte *)&tmp;
797                         ipx_get_local_target( tmp.network, tmp.node, tmp.address );
798                         Ipx_users[Ipx_num_users++] = tmp;
799                         printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
800                         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] );
801                 } else {
802                         printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
803                         fclose(fp);
804                         return;
805                 }
806         }
807         fclose(fp);
808 }
809
810
811 void ipx_read_network_file(char * filename)
812 {
813         FILE * fp;
814         user_address tmp;
815         char temp_line[132], *p1;
816         int i, n, ln=0;
817
818         if (!filename) return;
819
820         fp = fopen( filename, "rt" );
821         if ( !fp ) return;
822
823         printf( "Using Networks:\n" );
824         for (i=0; i<Ipx_num_networks; i++ )             {
825                 ubyte * n1 = (ubyte *)&Ipx_networks[i];
826                 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
827         }
828
829         while (fgets(temp_line, 132, fp)) {
830                 ln++;
831                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
832                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
833                 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] );
834 //                n = sscanf( temp_line, "%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3] );
835                 if ( n != 4 ) continue;
836                 if ( Ipx_num_networks < MAX_NETWORKS  ) {
837                         int j;
838                         for (j=0; j<Ipx_num_networks; j++ )     
839                                 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
840                                         break;
841                         if ( j >= Ipx_num_networks )    {
842                                 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
843                                 printf("  %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
844                         }
845                 } else {
846                         printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
847                         fclose(fp);
848                         return;
849                 }
850         }
851         fclose(fp);
852
853 }
854
855 //---typedef struct rip_entry {
856 //---   uint            network;
857 //---   ushort  nhops;
858 //---   ushort  nticks;
859 //---} rip_entry;
860 //---
861 //---typedef struct rip_packet {
862 //---   ushort          operation;              //1=request, 2=response
863 //---   rip_entry       rip[50];
864 //---} rip_packet;
865 //---
866 //---
867 //---void  ipx_find_all_servers()
868 //---{
869 //---   int i;
870 //---   rip_packet * rp;
871 //---   assert(ipx_installed);
872 //---
873 //---   ipx_change_default_socket( 0x0453 );
874 //---   //      ipx_change_default_socket( 0x5304 );
875 //---
876 //---   // Make sure no one is already sending something
877 //---   while( packets[0].ecb.in_use )
878 //---   {
879 //---   }
880 //---   
881 //---   if (packets[0].ecb.completion_code)     {
882 //---           printf( "AAAA:Send error %d for completion code\n", packets[0].ecb.completion_code );
883 //---           //exit(1);
884 //---   }
885 //---
886 //---   rp = (rip_packet *)&packets[0].pd;
887 //---
888 //---   // Fill in destination address
889 //---   {
890 //---           char mzero1[] = {0,0,0,1};
891 //---           char mzero[] = {0,0,0,0,0,1};
892 //---           char immediate[6];
893 //---           //memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
894 //---           //memcpy( packets[0].ipx.destination.node_id.address, ipx_my_node.address, 6 );
895 //---
896 //---           memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
897 //---           memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
898 //---
899 //---           memcpy( packets[0].ipx.destination.socket_id, &ipx_socket, 2 );
900 //---           memcpy( packets[0].ipx.source.network_id, &ipx_network, 4 );
901 //---           memcpy( packets[0].ipx.source.node_id.address, ipx_my_node.address, 6 );
902 //---           memcpy( packets[0].ipx.source.socket_id, &ipx_socket, 2 );
903 //---           //memcpy( packets[0].ecb.immediate_address.address, ipx_my_node.address, 6 );
904 //---           //mzero1[3] = 1;
905 //---           //memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
906 //---           //mzero[5] = 1;
907 //---           //memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
908 //---           //ipx_get_local_target( mzero1, mzero, immediate );
909 //---           //memcpy( packets[0].ecb.immediate_address.address, mzero, 6 );
910 //---           //memcpy( packets[0].ecb.immediate_address.address, immediate, 6 );
911 //---           //mzero[5] = 0;
912 //---   }
913 //---
914 //---   packets[0].ipx.packet_type = 1;         // RIP packet
915 //---
916 //---   // Fill in data to send
917 //---   packets[0].ecb.fragment_size = sizeof(ipx_header) + sizeof(rip_packet);
918 //---   assert( packets[0].ecb.fragment_size <= 576 );
919 //---
920 //---   rp->operation = 0;              // Request
921 //---   for (i=0;i<50; i++)     {
922 //---           rp->rip[i].network = 0xFFFFFFFF;
923 //---           rp->rip[i].nhops = 0;
924 //---           rp->rip[i].nticks = 0;
925 //---   }
926 //---
927 //---   // Send it
928 //---   ipx_send_packet( &packets[0].ecb );
929 //---
930 //---   for (i=0;i<50; i++)     {
931 //---           if ( rp->rip[i].network != 0xFFFFFFFF )
932 //---                   printf( "Network = %8x, Hops=%d, Ticks=%d\n", rp->rip[i].network, rp->rip[i].nhops, rp->rip[i].nticks );
933 //---   }
934 //---}
935 //---
936 //---