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