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.
16 * Routines for IPX communications, copied from d1x
25 #define _BORLAND_DOS_REGS 1
46 typedef unsigned char BYTE;
47 typedef unsigned short WORD;
48 typedef unsigned long DWORD;
50 typedef struct local_address {
52 } __pack__ local_address;
54 typedef struct net_address {
56 local_address node_id;
58 } __pack__ net_address;
60 typedef struct ipx_header {
63 BYTE transport_control;
65 net_address destination;
67 } __pack__ ipx_header;
69 typedef struct ecb_header {
75 BYTE ipx_reserved[14];
77 local_address immediate_address;
79 WORD fragment_pointer[2];
81 } __pack__ ecb_header;
83 typedef struct packet_data {
85 byte data[IPX_MAX_DATA_SIZE];
86 } __pack__ packet_data;
88 typedef struct ipx_packet {
92 } __pack__ ipx_packet;
94 static int ipx_packetnum = 0;
96 #define MAX_PACKETS 64
98 static packet_data packet_buffers[MAX_PACKETS];
99 static short packet_free_list[MAX_PACKETS];
100 static int num_packets = 0;
101 static int largest_packet_index = 0;
102 static short packet_size[MAX_PACKETS];
105 static ubyte ipx_installed=0;
106 WORD ipx_vector_segment;
107 WORD ipx_vector_offset;
108 ubyte ipx_socket_life = 0; // 0=closed at prog termination, 0xff=closed when requested.
109 //DWORD ipx_network = 0;
110 //local_address ipx_my_node;
111 #define ipx_my_node (ipx_MyAddress+4)
112 WORD ipx_num_packets=32; // 32 Ipx packets
113 ipx_packet * packets;
115 ushort ipx_packets_selector;
117 ecb_header * last_ecb=NULL;
120 static void got_new_packet( ecb_header * ecb );
121 static void ipx_listen_for_packet(ecb_header * ecb );
123 static void free_packet( int id )
125 packet_buffers[id].packetnum = -1;
126 packet_free_list[ --num_packets ] = id;
127 if (largest_packet_index==id)
128 while ((--largest_packet_index>0) && (packet_buffers[largest_packet_index].packetnum == -1 ));
131 static int ipx_dos_get_packet_data( ubyte * data )
133 int i, n, best, best_id, size;
135 for (i=1; i<ipx_num_packets; i++ ) {
136 if ( !packets[i].ecb.in_use ) {
137 got_new_packet( &packets[i].ecb );
138 packets[i].ecb.in_use = 0;
139 ipx_listen_for_packet(&packets[i].ecb);
147 for (i=0; i<=largest_packet_index; i++ ) {
148 if ( packet_buffers[i].packetnum > -1 ) {
150 if ( best == -1 || (packet_buffers[i].packetnum<best) ) {
151 best = packet_buffers[i].packetnum;
157 //mprintf( (0, "Best id = %d, pn = %d, last_ecb = %x, len=%x, ne = %d\n", best_id, best, last_ecb, lastlen, neterrors ));
158 //mprintf( (1, "<%d> ", neterrors ));
160 if ( best_id < 0 ) return 0;
162 size = packet_size[best_id];
163 memcpy( data, packet_buffers[best_id].data, size );
164 free_packet(best_id);
170 unsigned int swap_short( unsigned int short )
171 #pragma aux swap_short parm [eax] = "xchg al,ah";
173 static inline unsigned int swap_short( unsigned int sshort ) {
175 asm("xchg %%ah,%%al" : "=a" (__retval) : "a" (sshort));
180 static void got_new_packet( ecb_header * ecb )
184 unsigned short datasize;
188 p = (ipx_packet *)ecb;
190 if ( p->ecb.in_use ) { neterrors++; return; }
191 if ( p->ecb.completion_code ) { neterrors++; return; }
193 // Error( "Recieve error %d for completion code", p->ecb.completion_code );
195 if ( memcmp( &p->ipx.source.node_id, ipx_my_node, 6 ) ) {
196 datasize=swap_short(p->ipx.length);
198 datasize -= sizeof(ipx_header);
199 // Find slot to put packet in...
200 if ( datasize > 0 && datasize <= sizeof(packet_data) ) {
201 if ( num_packets >= MAX_PACKETS ) {
202 //printf( 1, "IPX: Packet buffer overrun!!!\n" );
206 id = packet_free_list[ num_packets++ ];
207 if (id > largest_packet_index ) largest_packet_index = id;
208 packet_size[id] = datasize-sizeof(int);
209 packet_buffers[id].packetnum = p->pd.packetnum;
210 if ( packet_buffers[id].packetnum < 0 ) { neterrors++; return; }
211 memcpy( packet_buffers[id].data, p->pd.data, packet_size[id] );
218 //ipx_listen_for_packet(&p->ecb);
221 /*ubyte * ipx_get_my_local_address()
223 return ipx_my_node.address;
226 ubyte * ipx_get_my_server_address()
228 return (ubyte *)&ipx_network;
231 static void ipx_listen_for_packet(ecb_header * ecb )
233 dpmi_real_regs rregs;
235 memset(&rregs,0,sizeof(dpmi_real_regs));
236 rregs.ebx = 4; // Listen For Packet function
237 rregs.esi = DPMI_real_offset(ecb);
238 rregs.es = DPMI_real_segment(ecb);
239 dpmi_real_int386x( 0x7A, &rregs );
242 /*static void ipx_cancel_listen_for_packet(ecb_header * ecb )
244 dpmi_real_regs rregs;
245 memset(&rregs,0,sizeof(dpmi_real_regs));
246 rregs.ebx = 6; // IPX Cancel event
247 rregs.esi = DPMI_real_offset(ecb);
248 rregs.es = DPMI_real_segment(ecb);
249 dpmi_real_int386x( 0x7A, &rregs );
253 static void ipx_send_packet(ecb_header * ecb )
255 dpmi_real_regs rregs;
256 memset(&rregs,0,sizeof(dpmi_real_regs));
257 rregs.ebx = 3; // Send Packet function
258 rregs.esi = DPMI_real_offset(ecb);
259 rregs.es = DPMI_real_segment(ecb);
260 dpmi_real_int386x( 0x7A, &rregs );
266 ubyte local_target[6];
267 } __pack__ net_xlat_info;
269 static void ipx_dos_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
271 net_xlat_info * info;
272 dpmi_real_regs rregs;
274 // Get dos memory for call...
275 info = (net_xlat_info *)dpmi_get_temp_low_buffer( sizeof(net_xlat_info) );
276 assert( info != NULL );
277 memcpy( info->network, server, 4 );
278 memcpy( info->node, node, 6 );
280 memset(&rregs,0,sizeof(dpmi_real_regs));
282 rregs.ebx = 2; // Get Local Target
283 rregs.es = DPMI_real_segment(info);
284 rregs.esi = DPMI_real_offset(info->network);
285 rregs.edi = DPMI_real_offset(info->local_target);
287 dpmi_real_int386x( 0x7A, &rregs );
289 // Save the local target...
290 memcpy( local_target, info->local_target, 6 );
293 static void ipx_close()
295 dpmi_real_regs rregs;
296 if ( ipx_installed ) {
297 // When using VLM's instead of NETX, the sockets don't
298 // seem to automatically get closed, so we must explicitly
299 // close them at program termination.
301 memset(&rregs,0,sizeof(dpmi_real_regs));
302 rregs.edx = ipx_socket;
303 rregs.ebx = 1; // Close socket
304 dpmi_real_int386x( 0x7A, &rregs );
308 static int ipx_init(int socket_number)
311 dpmi_real_regs rregs;
312 ubyte *ipx_real_buffer;
315 // atexit(ipx_close);
319 // init packet buffers.
320 for (i=0; i<MAX_PACKETS; i++ ) {
321 packet_buffers[i].packetnum = -1;
322 packet_free_list[i] = i;
325 largest_packet_index = 0;
327 // Get the IPX vector
328 memset(&rregs,0,sizeof(dpmi_real_regs));
329 rregs.eax=0x00007a00;
330 dpmi_real_int386x( 0x2f, &rregs );
332 if ( (rregs.eax & 0xFF) != 0xFF ) {
333 return IPX_NOT_INSTALLED;
335 ipx_vector_offset = rregs.edi & 0xFFFF;
336 ipx_vector_segment = rregs.es;
337 //printf( "IPX entry point at %.4x:%.4x\n", ipx_vector_segment, ipx_vector_offset );
339 // Open a socket for IPX
341 memset(&rregs,0,sizeof(dpmi_real_regs));
342 swab( (char *)&socket_number,(char *)&ipx_socket, 2 );
343 rregs.edx = ipx_socket;
344 rregs.eax = ipx_socket_life;
345 rregs.ebx = 0; // Open socket
346 dpmi_real_int386x( 0x7A, &rregs );
348 ipx_socket = rregs.edx & 0xFFFF;
350 if ( rregs.eax & 0xFF ) {
351 //mprintf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
352 return IPX_SOCKET_TABLE_FULL;
357 // Find our internetwork address
358 ipx_real_buffer = dpmi_get_temp_low_buffer( 1024 ); // 1k block
359 if ( ipx_real_buffer == NULL ) {
360 //printf( "Error allocation realmode memory\n" );
361 return IPX_NO_LOW_DOS_MEM;
364 memset(&rregs,0,sizeof(dpmi_real_regs));
365 rregs.ebx = 9; // Get internetwork address
366 rregs.esi = DPMI_real_offset(ipx_real_buffer);
367 rregs.es = DPMI_real_segment(ipx_real_buffer);
368 dpmi_real_int386x( 0x7A, &rregs );
370 if ( rregs.eax & 0xFF ) {
371 //printf( "Error getting internetwork address!\n" );
372 return IPX_SOCKET_TABLE_FULL;
375 /* memcpy( &ipx_network, ipx_real_buffer, 4 );
376 memcpy( ipx_my_node, &ipx_real_buffer[4], 6 );*/
377 memcpy(ipx_MyAddress,ipx_real_buffer,10);
379 if ( show_address ) {
380 printf( "My IPX addresss is " );
381 printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
382 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] );
386 packets = dpmi_real_malloc( sizeof(ipx_packet)*ipx_num_packets, &ipx_packets_selector );
387 if ( packets == NULL ) {
388 //printf( "Couldn't allocate real memory for %d packets\n", ipx_num_packets );
389 return IPX_NO_LOW_DOS_MEM;
391 #if 0 /* adb: not needed, fails with cwsdpmi */
392 if (!dpmi_lock_region( packets, sizeof(ipx_packet)*ipx_num_packets )) {
393 //printf( "Couldn't lock real memory for %d packets\n", ipx_num_packets );
394 return IPX_NO_LOW_DOS_MEM;
397 memset( packets, 0, sizeof(ipx_packet)*ipx_num_packets );
399 for (i=1; i<ipx_num_packets; i++ ) {
400 packets[i].ecb.in_use = 0x1d;
401 //packets[i].ecb.in_use = 0;
402 packets[i].ecb.socket_id = ipx_socket;
403 packets[i].ecb.fragment_count = 1;
404 packets[i].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[i].ipx);
405 packets[i].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[i].ipx);
406 packets[i].ecb.fragment_size = sizeof(ipx_packet)-sizeof(ecb_header); //-sizeof(ecb_header);
408 ipx_listen_for_packet(&packets[i].ecb);
411 packets[0].ecb.socket_id = ipx_socket;
412 packets[0].ecb.fragment_count = 1;
413 packets[0].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[0].ipx);
414 packets[0].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[0].ipx);
415 packets[0].ipx.packet_type = 4; // IPX packet
416 packets[0].ipx.destination.socket_id = ipx_socket;
417 // memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
418 memset( packets[0].ipx.destination.network_id, 0, 4 );
423 static void ipx_dos_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
425 assert(ipx_installed);
427 if ( datasize >= IPX_MAX_DATA_SIZE ) {
428 printf( "Data too big\n" );
429 //added/replaced on 11/8/98 by Victor Rachels to stop crappage
432 //end this section replacement - VR
435 // Make sure no one is already sending something
436 while( packets[0].ecb.in_use )
440 if (packets[0].ecb.completion_code) {
441 // printf( "Send error %d for completion code\n", packets[0].ecb.completion_code );
442 //added/replaced on 11/8/98 by Victor Rachels to stop crappage
445 //end this section replacement - VR
449 // Fill in destination address
450 if ( memcmp( network, &ipx_network, 4 ) )
451 memcpy( packets[0].ipx.destination.network_id, network, 4 );
453 memset( packets[0].ipx.destination.network_id, 0, 4 );
454 memcpy( packets[0].ipx.destination.node_id.address, address, 6 );
455 memcpy( packets[0].ecb.immediate_address.address, immediate_address, 6 );
456 packets[0].pd.packetnum = ipx_packetnum++;
458 // Fill in data to send
459 packets[0].ecb.fragment_size = sizeof(ipx_header) + sizeof(int) + datasize;
461 assert( datasize > 1 );
462 assert( packets[0].ecb.fragment_size <= 576 );
464 memcpy( packets[0].pd.data, data, datasize );
467 ipx_send_packet( &packets[0].ecb );
471 /*static int ipx_change_default_socket( ushort socket_number )
475 dpmi_real_regs rregs;
477 if ( !ipx_installed ) return -3;
480 memset(&rregs,0,sizeof(dpmi_real_regs));
481 swab( (char *)&socket_number,(char *)&new_ipx_socket, 2 );
482 rregs.edx = new_ipx_socket;
483 rregs.eax = ipx_socket_life;
484 rregs.ebx = 0; // Open socket
485 dpmi_real_int386x( 0x7A, &rregs );
487 new_ipx_socket = rregs.edx & 0xFFFF;
489 if ( rregs.eax & 0xFF ) {
490 //printf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
494 for (i=1; i<ipx_num_packets; i++ ) {
495 ipx_cancel_listen_for_packet(&packets[i].ecb);
498 // Close existing socket...
499 memset(&rregs,0,sizeof(dpmi_real_regs));
500 rregs.edx = ipx_socket;
501 rregs.ebx = 1; // Close socket
502 dpmi_real_int386x( 0x7A, &rregs );
504 ipx_socket = new_ipx_socket;
506 // Repost all listen requests on the new socket...
507 for (i=1; i<ipx_num_packets; i++ ) {
508 packets[i].ecb.in_use = 0;
509 packets[i].ecb.socket_id = ipx_socket;
510 ipx_listen_for_packet(&packets[i].ecb);
513 packets[0].ecb.socket_id = ipx_socket;
514 packets[0].ipx.destination.socket_id = ipx_socket;
517 // init packet buffers.
518 for (i=0; i<MAX_PACKETS; i++ ) {
519 packet_buffers[i].packetnum = -1;
520 packet_free_list[i] = i;
523 largest_packet_index = 0;
529 struct ipx_driver ipx_dos = {
538 ipx_dos_get_local_target,
539 ipx_dos_get_packet_data,
540 ipx_dos_send_packet_data
543 void arch_ipx_set_driver(int ipx_driver)
546 if (ipx_driver != IPX_DRIVER_IPX)
547 Warning("Unknown network driver! Defaulting to real IPX");
551 //---typedef struct rip_entry {
557 //---typedef struct rip_packet {
558 //--- ushort operation; //1=request, 2=response
559 //--- rip_entry rip[50];
563 //---void ipx_find_all_servers()
566 //--- rip_packet * rp;
567 //--- assert(ipx_installed);
569 //--- ipx_change_default_socket( 0x0453 );
570 //--- // ipx_change_default_socket( 0x5304 );
572 //--- // Make sure no one is already sending something
573 //--- while( packets[0].ecb.in_use )
577 //--- if (packets[0].ecb.completion_code) {
578 //--- printf( "AAAA:Send error %d for completion code\n", packets[0].ecb.completion_code );
582 //--- rp = (rip_packet *)&packets[0].pd;
584 //--- // Fill in destination address
586 //--- char mzero1[] = {0,0,0,1};
587 //--- char mzero[] = {0,0,0,0,0,1};
588 //--- char immediate[6];
589 //--- //memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
590 //--- //memcpy( packets[0].ipx.destination.node_id.address, ipx_my_node.address, 6 );
592 //--- memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
593 //--- memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
595 //--- memcpy( packets[0].ipx.destination.socket_id, &ipx_socket, 2 );
596 //--- memcpy( packets[0].ipx.source.network_id, &ipx_network, 4 );
597 //--- memcpy( packets[0].ipx.source.node_id.address, ipx_my_node.address, 6 );
598 //--- memcpy( packets[0].ipx.source.socket_id, &ipx_socket, 2 );
599 //--- //memcpy( packets[0].ecb.immediate_address.address, ipx_my_node.address, 6 );
600 //--- //mzero1[3] = 1;
601 //--- //memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
602 //--- //mzero[5] = 1;
603 //--- //memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
604 //--- //ipx_get_local_target( mzero1, mzero, immediate );
605 //--- //memcpy( packets[0].ecb.immediate_address.address, mzero, 6 );
606 //--- //memcpy( packets[0].ecb.immediate_address.address, immediate, 6 );
607 //--- //mzero[5] = 0;
610 //--- packets[0].ipx.packet_type = 1; // RIP packet
612 //--- // Fill in data to send
613 //--- packets[0].ecb.fragment_size = sizeof(ipx_header) + sizeof(rip_packet);
614 //--- assert( packets[0].ecb.fragment_size <= 576 );
616 //--- rp->operation = 0; // Request
617 //--- for (i=0;i<50; i++) {
618 //--- rp->rip[i].network = 0xFFFFFFFF;
619 //--- rp->rip[i].nhops = 0;
620 //--- rp->rip[i].nticks = 0;
624 //--- ipx_send_packet( &packets[0].ecb );
626 //--- for (i=0;i<50; i++) {
627 //--- if ( rp->rip[i].network != 0xFFFFFFFF )
628 //--- printf( "Network = %8x, Hops=%d, Ticks=%d\n", rp->rip[i].network, rp->rip[i].nhops, rp->rip[i].nticks );