implement -nomusic for linux hmiplay (d1x r1.8)
[btb/d2x.git] / arch / dos / ipx.c
1 /* $Id: ipx.c,v 1.6 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, copied from d1x
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 HAVE_CONFIG_H
164 #include <conf.h>
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 "pstypes.h"
182 #include "timer.h"
183 #include "ipx.h"
184 #include "error.h"
185 #include "u_dpmi.h"
186 #include "key.h"
187 #include "ipx_drv.h"
188
189 typedef unsigned char BYTE;
190 typedef unsigned short WORD;
191 typedef unsigned long DWORD;
192
193 typedef struct local_address {
194         ubyte address[6];
195 } __pack__ local_address;
196
197 typedef struct net_address {
198         BYTE                            network_id[4];                  
199         local_address   node_id;
200         WORD                            socket_id;
201 } __pack__ net_address;
202
203 typedef struct ipx_header {
204         WORD                    checksum;
205         WORD                    length;
206         BYTE                    transport_control;
207         BYTE                    packet_type;
208         net_address     destination;
209         net_address     source;
210 } __pack__ ipx_header;
211
212 typedef struct ecb_header {
213         WORD                    link[2];
214         WORD                    esr_address[2];
215         BYTE                    in_use;
216         BYTE                    completion_code;
217         WORD                    socket_id;
218         BYTE                    ipx_reserved[14];        
219         WORD                    connection_id;
220         local_address immediate_address;
221         WORD                    fragment_count;
222         WORD                    fragment_pointer[2];
223         WORD                    fragment_size;
224 } __pack__ ecb_header;
225
226 typedef struct packet_data {
227         int                     packetnum;
228         byte                    data[IPX_MAX_DATA_SIZE];
229 } __pack__ packet_data;
230
231 typedef struct ipx_packet {
232         ecb_header      ecb;
233         ipx_header      ipx;
234         packet_data     pd;
235 } __pack__ ipx_packet;
236
237 static int ipx_packetnum = 0;
238
239 #define MAX_PACKETS 64
240
241 static packet_data packet_buffers[MAX_PACKETS];
242 static short packet_free_list[MAX_PACKETS];
243 static int num_packets = 0;
244 static int largest_packet_index = 0;
245 static short packet_size[MAX_PACKETS];
246
247 WORD ipx_socket=0;
248 static ubyte ipx_installed=0;
249 WORD ipx_vector_segment;
250 WORD ipx_vector_offset;
251 ubyte ipx_socket_life = 0;      // 0=closed at prog termination, 0xff=closed when requested.
252 //DWORD ipx_network = 0;
253 //local_address ipx_my_node;
254 #define ipx_my_node (ipx_MyAddress+4)
255 WORD ipx_num_packets=32;                // 32 Ipx packets
256 ipx_packet * packets;
257 int neterrors = 0;
258 ushort ipx_packets_selector;
259
260 ecb_header * last_ecb=NULL;
261 int lastlen=0;
262
263 static void got_new_packet( ecb_header * ecb );
264 static void ipx_listen_for_packet(ecb_header * ecb );
265
266 static void free_packet( int id )
267 {
268         packet_buffers[id].packetnum = -1;
269         packet_free_list[ --num_packets ] = id;
270         if (largest_packet_index==id)   
271                 while ((--largest_packet_index>0) && (packet_buffers[largest_packet_index].packetnum == -1 ));
272 }
273
274 static int ipx_dos_get_packet_data( ubyte * data )
275 {
276         int i, n, best, best_id, size;
277
278         for (i=1; i<ipx_num_packets; i++ )      {
279                 if ( !packets[i].ecb.in_use )   {
280                         got_new_packet( &packets[i].ecb );
281                         packets[i].ecb.in_use = 0;
282                         ipx_listen_for_packet(&packets[i].ecb);
283                 }                       
284         }
285
286         best = -1;
287         n = 0;
288         best_id = -1;
289
290         for (i=0; i<=largest_packet_index; i++ )        {
291                 if ( packet_buffers[i].packetnum > -1 ) {
292                         n++;
293                         if ( best == -1 || (packet_buffers[i].packetnum<best) ) {
294                                 best = packet_buffers[i].packetnum;
295                                 best_id = i;
296                         }
297                 }                       
298         }
299
300         //mprintf( (0, "Best id = %d, pn = %d, last_ecb = %x, len=%x, ne = %d\n", best_id, best, last_ecb, lastlen, neterrors ));
301         //mprintf( (1, "<%d> ", neterrors ));
302
303         if ( best_id < 0 ) return 0;
304
305         size = packet_size[best_id];
306         memcpy( data, packet_buffers[best_id].data, size );
307         free_packet(best_id);
308
309         return size;
310 }
311
312 #ifndef __GNUC__
313 unsigned int swap_short( unsigned int short )
314 #pragma aux swap_short parm [eax] = "xchg al,ah";
315 #else
316 static inline unsigned int swap_short( unsigned int sshort ) {
317         int __retval;
318         asm("xchg %%ah,%%al" : "=a" (__retval) : "a" (sshort));
319         return __retval;
320 }
321 #endif
322
323 static void got_new_packet( ecb_header * ecb )
324 {
325         ipx_packet * p;
326         int id;
327         unsigned short datasize;
328
329         datasize = 0;
330         last_ecb = ecb;
331         p = (ipx_packet *)ecb;
332
333         if ( p->ecb.in_use ) { neterrors++; return; }
334         if      ( p->ecb.completion_code )      { neterrors++; return; }
335
336         //      Error( "Recieve error %d for completion code", p->ecb.completion_code );
337         
338         if ( memcmp( &p->ipx.source.node_id, ipx_my_node, 6 ) ) {
339                 datasize=swap_short(p->ipx.length);
340                 lastlen=datasize;
341                 datasize -= sizeof(ipx_header);
342                 // Find slot to put packet in...
343                 if ( datasize > 0 && datasize <= sizeof(packet_data) )  {
344                         if ( num_packets >= MAX_PACKETS ) {
345                                 //printf( 1, "IPX: Packet buffer overrun!!!\n" );
346                                 neterrors++;
347                                 return;
348                         }               
349                         id = packet_free_list[ num_packets++ ];
350                         if (id > largest_packet_index ) largest_packet_index = id;
351                         packet_size[id] = datasize-sizeof(int);
352                         packet_buffers[id].packetnum =  p->pd.packetnum;
353                         if ( packet_buffers[id].packetnum < 0 ) { neterrors++; return; }
354                         memcpy( packet_buffers[id].data, p->pd.data, packet_size[id] );
355                 } else {
356                         neterrors++; return;
357                 }
358         } 
359         // Repost the ecb
360         p->ecb.in_use = 0;
361         //ipx_listen_for_packet(&p->ecb);
362 }
363
364 /*ubyte * ipx_get_my_local_address()
365 {
366         return ipx_my_node.address;
367 }
368
369 ubyte * ipx_get_my_server_address()
370 {
371         return (ubyte *)&ipx_network;
372 }*/
373
374 static void ipx_listen_for_packet(ecb_header * ecb )    
375 {
376         dpmi_real_regs rregs;
377         ecb->in_use = 0x1d;
378         memset(&rregs,0,sizeof(dpmi_real_regs));
379         rregs.ebx = 4;  // Listen For Packet function
380         rregs.esi = DPMI_real_offset(ecb);
381         rregs.es = DPMI_real_segment(ecb);
382         dpmi_real_int386x( 0x7A, &rregs );
383 }
384
385 /*static void ipx_cancel_listen_for_packet(ecb_header * ecb )   
386 {
387         dpmi_real_regs rregs;
388         memset(&rregs,0,sizeof(dpmi_real_regs));
389         rregs.ebx = 6;  // IPX Cancel event
390         rregs.esi = DPMI_real_offset(ecb);
391         rregs.es = DPMI_real_segment(ecb);
392         dpmi_real_int386x( 0x7A, &rregs );
393 }
394 */
395
396 static void ipx_send_packet(ecb_header * ecb )  
397 {
398         dpmi_real_regs rregs;
399         memset(&rregs,0,sizeof(dpmi_real_regs));
400         rregs.ebx = 3;  // Send Packet function
401         rregs.esi = DPMI_real_offset(ecb);
402         rregs.es = DPMI_real_segment(ecb);
403         dpmi_real_int386x( 0x7A, &rregs );
404 }
405
406 typedef struct {
407         ubyte   network[4];
408         ubyte           node[6];
409         ubyte           local_target[6];
410 } __pack__ net_xlat_info;
411
412 static void ipx_dos_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
413 {
414         net_xlat_info * info;
415         dpmi_real_regs rregs;
416                 
417         // Get dos memory for call...
418         info = (net_xlat_info *)dpmi_get_temp_low_buffer( sizeof(net_xlat_info) );      
419         assert( info != NULL );
420         memcpy( info->network, server, 4 );
421         memcpy( info->node, node, 6 );
422         
423         memset(&rregs,0,sizeof(dpmi_real_regs));
424
425         rregs.ebx = 2;          // Get Local Target     
426         rregs.es = DPMI_real_segment(info);
427         rregs.esi = DPMI_real_offset(info->network);
428         rregs.edi = DPMI_real_offset(info->local_target);
429
430         dpmi_real_int386x( 0x7A, &rregs );
431
432         // Save the local target...
433         memcpy( local_target, info->local_target, 6 );
434 }
435
436 static void ipx_close()
437 {
438         dpmi_real_regs rregs;
439         if ( ipx_installed )    {
440                 // When using VLM's instead of NETX, the sockets don't
441                 // seem to automatically get closed, so we must explicitly
442                 // close them at program termination.
443                 ipx_installed = 0;
444                 memset(&rregs,0,sizeof(dpmi_real_regs));
445                 rregs.edx = ipx_socket;
446                 rregs.ebx = 1;  // Close socket
447                 dpmi_real_int386x( 0x7A, &rregs );
448         }
449 }
450
451 static int ipx_init(int socket_number)
452 {
453         int show_address=0;
454         dpmi_real_regs rregs;
455         ubyte *ipx_real_buffer;
456         int i;
457
458 //      atexit(ipx_close);
459
460         ipx_packetnum = 0;
461
462         // init packet buffers.
463         for (i=0; i<MAX_PACKETS; i++ )  {
464                 packet_buffers[i].packetnum = -1;
465                 packet_free_list[i] = i;
466         }
467         num_packets = 0;
468         largest_packet_index = 0;
469
470         // Get the IPX vector
471         memset(&rregs,0,sizeof(dpmi_real_regs));
472         rregs.eax=0x00007a00;
473         dpmi_real_int386x( 0x2f, &rregs );
474
475         if ( (rregs.eax & 0xFF) != 0xFF )       {
476                 return IPX_NOT_INSTALLED;   
477         }
478         ipx_vector_offset = rregs.edi & 0xFFFF;
479         ipx_vector_segment = rregs.es;
480         //printf( "IPX entry point at %.4x:%.4x\n", ipx_vector_segment, ipx_vector_offset );
481
482         // Open a socket for IPX
483
484         memset(&rregs,0,sizeof(dpmi_real_regs));
485         swab( (char *)&socket_number,(char *)&ipx_socket, 2 );
486         rregs.edx = ipx_socket;
487         rregs.eax = ipx_socket_life;
488         rregs.ebx = 0;  // Open socket
489         dpmi_real_int386x( 0x7A, &rregs );
490         
491         ipx_socket = rregs.edx & 0xFFFF;
492         
493         if ( rregs.eax & 0xFF ) {
494                 //mprintf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
495                 return IPX_SOCKET_TABLE_FULL;
496         }
497         
498         ipx_installed = 1;
499
500         // Find our internetwork address
501         ipx_real_buffer = dpmi_get_temp_low_buffer( 1024 );     // 1k block
502         if ( ipx_real_buffer == NULL )  {
503                 //printf( "Error allocation realmode memory\n" );
504                 return IPX_NO_LOW_DOS_MEM;
505         }
506
507         memset(&rregs,0,sizeof(dpmi_real_regs));
508         rregs.ebx = 9;          // Get internetwork address
509         rregs.esi = DPMI_real_offset(ipx_real_buffer);
510         rregs.es = DPMI_real_segment(ipx_real_buffer);
511         dpmi_real_int386x( 0x7A, &rregs );
512
513         if ( rregs.eax & 0xFF ) {
514                 //printf( "Error getting internetwork address!\n" );
515                 return IPX_SOCKET_TABLE_FULL;
516         }
517
518 /*      memcpy( &ipx_network, ipx_real_buffer, 4 );
519         memcpy( ipx_my_node, &ipx_real_buffer[4], 6 );*/
520         memcpy(ipx_MyAddress,ipx_real_buffer,10);
521
522         if ( show_address )     {
523                 printf( "My IPX addresss is " );
524                 printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
525                 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] );
526                 printf( "\n" );
527         }
528
529         packets = dpmi_real_malloc( sizeof(ipx_packet)*ipx_num_packets, &ipx_packets_selector );
530         if ( packets == NULL )  {
531                 //printf( "Couldn't allocate real memory for %d packets\n", ipx_num_packets );
532                 return IPX_NO_LOW_DOS_MEM;
533         }
534 #if 0 /* adb: not needed, fails with cwsdpmi */
535         if (!dpmi_lock_region( packets, sizeof(ipx_packet)*ipx_num_packets ))   {
536                 //printf( "Couldn't lock real memory for %d packets\n", ipx_num_packets );
537                 return IPX_NO_LOW_DOS_MEM;
538         }
539 #endif
540         memset( packets, 0, sizeof(ipx_packet)*ipx_num_packets );
541
542         for (i=1; i<ipx_num_packets; i++ )      {
543                 packets[i].ecb.in_use = 0x1d;
544                 //packets[i].ecb.in_use = 0;
545                 packets[i].ecb.socket_id = ipx_socket;
546                 packets[i].ecb.fragment_count = 1;
547                 packets[i].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[i].ipx);
548                 packets[i].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[i].ipx);
549                 packets[i].ecb.fragment_size = sizeof(ipx_packet)-sizeof(ecb_header);                   //-sizeof(ecb_header);
550
551                 ipx_listen_for_packet(&packets[i].ecb);
552         }
553
554         packets[0].ecb.socket_id = ipx_socket;
555         packets[0].ecb.fragment_count = 1;
556         packets[0].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[0].ipx);
557         packets[0].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[0].ipx);
558         packets[0].ipx.packet_type = 4;         // IPX packet
559         packets[0].ipx.destination.socket_id = ipx_socket;
560 //      memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
561         memset( packets[0].ipx.destination.network_id, 0, 4 );
562
563         return IPX_INIT_OK;
564 }
565
566 static void ipx_dos_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
567 {
568         assert(ipx_installed);
569
570         if ( datasize >= IPX_MAX_DATA_SIZE )    {
571                 printf( "Data too big\n" );
572 //added/replaced on 11/8/98 by Victor Rachels to stop crappage
573                 return;
574 //                exit(1);
575 //end this section replacement - VR
576         }
577
578         // Make sure no one is already sending something
579         while( packets[0].ecb.in_use )
580         {
581         }
582         
583         if (packets[0].ecb.completion_code)     {
584 //                printf( "Send error %d for completion code\n", packets[0].ecb.completion_code );
585 //added/replaced on 11/8/98 by Victor Rachels to stop crappage
586                 return;
587         //        exit(1);
588 //end this section replacement - VR
589
590         }
591
592         // Fill in destination address
593         if ( memcmp( network, &ipx_network, 4 ) )
594                 memcpy( packets[0].ipx.destination.network_id, network, 4 );
595         else
596                 memset( packets[0].ipx.destination.network_id, 0, 4 );
597         memcpy( packets[0].ipx.destination.node_id.address, address, 6 );
598         memcpy( packets[0].ecb.immediate_address.address, immediate_address, 6 );
599         packets[0].pd.packetnum = ipx_packetnum++;
600
601         // Fill in data to send
602         packets[0].ecb.fragment_size = sizeof(ipx_header) + sizeof(int) + datasize;
603
604         assert( datasize > 1 );
605         assert( packets[0].ecb.fragment_size <= 576 );
606
607         memcpy( packets[0].pd.data, data, datasize );
608
609         // Send it
610         ipx_send_packet( &packets[0].ecb );
611
612 }
613
614 /*static int ipx_change_default_socket( ushort socket_number )
615 {
616         int i;
617         WORD new_ipx_socket;
618         dpmi_real_regs rregs;
619
620         if ( !ipx_installed ) return -3;
621
622         // Open a new socket    
623         memset(&rregs,0,sizeof(dpmi_real_regs));
624         swab( (char *)&socket_number,(char *)&new_ipx_socket, 2 );
625         rregs.edx = new_ipx_socket;
626         rregs.eax = ipx_socket_life;
627         rregs.ebx = 0;  // Open socket
628         dpmi_real_int386x( 0x7A, &rregs );
629         
630         new_ipx_socket = rregs.edx & 0xFFFF;
631         
632         if ( rregs.eax & 0xFF ) {
633                 //printf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
634                 return -2;
635         }
636
637         for (i=1; i<ipx_num_packets; i++ )      {
638                 ipx_cancel_listen_for_packet(&packets[i].ecb);
639         }
640
641         // Close existing socket...
642         memset(&rregs,0,sizeof(dpmi_real_regs));
643         rregs.edx = ipx_socket;
644         rregs.ebx = 1;  // Close socket
645         dpmi_real_int386x( 0x7A, &rregs );
646
647         ipx_socket = new_ipx_socket;
648
649         // Repost all listen requests on the new socket...      
650         for (i=1; i<ipx_num_packets; i++ )      {
651                 packets[i].ecb.in_use = 0;
652                 packets[i].ecb.socket_id = ipx_socket;
653                 ipx_listen_for_packet(&packets[i].ecb);
654         }
655
656         packets[0].ecb.socket_id = ipx_socket;
657         packets[0].ipx.destination.socket_id = ipx_socket;
658
659         ipx_packetnum = 0;
660         // init packet buffers.
661         for (i=0; i<MAX_PACKETS; i++ )  {
662                 packet_buffers[i].packetnum = -1;
663                 packet_free_list[i] = i;
664         }
665         num_packets = 0;
666         largest_packet_index = 0;
667
668         return 0;
669 }
670 */
671
672 struct ipx_driver ipx_dos = {
673 //      NULL,
674         ipx_init,
675         ipx_close,
676         NULL,
677         NULL,
678         NULL,
679         NULL,
680         1,
681         ipx_dos_get_local_target,
682         ipx_dos_get_packet_data,
683         ipx_dos_send_packet_data
684 };
685
686 void arch_ipx_set_driver(int ipx_driver)
687 {
688         driver = &ipx_dos;
689         if (ipx_driver != IPX_DRIVER_IPX)
690                 Warning("Unknown network driver! Defaulting to real IPX");
691 }
692
693
694 //---typedef struct rip_entry {
695 //---   uint            network;
696 //---   ushort  nhops;
697 //---   ushort  nticks;
698 //---} rip_entry;
699 //---
700 //---typedef struct rip_packet {
701 //---   ushort          operation;              //1=request, 2=response
702 //---   rip_entry       rip[50];
703 //---} rip_packet;
704 //---
705 //---
706 //---void  ipx_find_all_servers()
707 //---{
708 //---   int i;
709 //---   rip_packet * rp;
710 //---   assert(ipx_installed);
711 //---
712 //---   ipx_change_default_socket( 0x0453 );
713 //---   //      ipx_change_default_socket( 0x5304 );
714 //---
715 //---   // Make sure no one is already sending something
716 //---   while( packets[0].ecb.in_use )
717 //---   {
718 //---   }
719 //---   
720 //---   if (packets[0].ecb.completion_code)     {
721 //---           printf( "AAAA:Send error %d for completion code\n", packets[0].ecb.completion_code );
722 //---           //exit(1);
723 //---   }
724 //---
725 //---   rp = (rip_packet *)&packets[0].pd;
726 //---
727 //---   // Fill in destination address
728 //---   {
729 //---           char mzero1[] = {0,0,0,1};
730 //---           char mzero[] = {0,0,0,0,0,1};
731 //---           char immediate[6];
732 //---           //memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
733 //---           //memcpy( packets[0].ipx.destination.node_id.address, ipx_my_node.address, 6 );
734 //---
735 //---           memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
736 //---           memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
737 //---
738 //---           memcpy( packets[0].ipx.destination.socket_id, &ipx_socket, 2 );
739 //---           memcpy( packets[0].ipx.source.network_id, &ipx_network, 4 );
740 //---           memcpy( packets[0].ipx.source.node_id.address, ipx_my_node.address, 6 );
741 //---           memcpy( packets[0].ipx.source.socket_id, &ipx_socket, 2 );
742 //---           //memcpy( packets[0].ecb.immediate_address.address, ipx_my_node.address, 6 );
743 //---           //mzero1[3] = 1;
744 //---           //memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
745 //---           //mzero[5] = 1;
746 //---           //memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
747 //---           //ipx_get_local_target( mzero1, mzero, immediate );
748 //---           //memcpy( packets[0].ecb.immediate_address.address, mzero, 6 );
749 //---           //memcpy( packets[0].ecb.immediate_address.address, immediate, 6 );
750 //---           //mzero[5] = 0;
751 //---   }
752 //---
753 //---   packets[0].ipx.packet_type = 1;         // RIP packet
754 //---
755 //---   // Fill in data to send
756 //---   packets[0].ecb.fragment_size = sizeof(ipx_header) + sizeof(rip_packet);
757 //---   assert( packets[0].ecb.fragment_size <= 576 );
758 //---
759 //---   rp->operation = 0;              // Request
760 //---   for (i=0;i<50; i++)     {
761 //---           rp->rip[i].network = 0xFFFFFFFF;
762 //---           rp->rip[i].nhops = 0;
763 //---           rp->rip[i].nticks = 0;
764 //---   }
765 //---
766 //---   // Send it
767 //---   ipx_send_packet( &packets[0].ecb );
768 //---
769 //---   for (i=0;i<50; i++)     {
770 //---           if ( rp->rip[i].network != 0xFFFFFFFF )
771 //---                   printf( "Network = %8x, Hops=%d, Ticks=%d\n", rp->rip[i].network, rp->rip[i].nhops, rp->rip[i].nticks );
772 //---   }
773 //---}
774 //---
775 //---