1 /* $Id: ipx.c,v 1.7 2004-08-28 23:17:45 schaffner Exp $ */
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.
17 * Routines for IPX communications, copied from d1x
26 #define _BORLAND_DOS_REGS 1
47 typedef unsigned char BYTE;
48 typedef unsigned short WORD;
49 typedef unsigned long DWORD;
51 typedef struct local_address {
53 } __pack__ local_address;
55 typedef struct net_address {
57 local_address node_id;
59 } __pack__ net_address;
61 typedef struct ipx_header {
64 BYTE transport_control;
66 net_address destination;
68 } __pack__ ipx_header;
70 typedef struct ecb_header {
76 BYTE ipx_reserved[14];
78 local_address immediate_address;
80 WORD fragment_pointer[2];
82 } __pack__ ecb_header;
84 typedef struct packet_data {
86 byte data[IPX_MAX_DATA_SIZE];
87 } __pack__ packet_data;
89 typedef struct ipx_packet {
93 } __pack__ ipx_packet;
95 static int ipx_packetnum = 0;
97 #define MAX_PACKETS 64
99 static packet_data packet_buffers[MAX_PACKETS];
100 static short packet_free_list[MAX_PACKETS];
101 static int num_packets = 0;
102 static int largest_packet_index = 0;
103 static short packet_size[MAX_PACKETS];
106 static ubyte ipx_installed=0;
107 WORD ipx_vector_segment;
108 WORD ipx_vector_offset;
109 ubyte ipx_socket_life = 0; // 0=closed at prog termination, 0xff=closed when requested.
110 //DWORD ipx_network = 0;
111 //local_address ipx_my_node;
112 #define ipx_my_node (ipx_MyAddress+4)
113 WORD ipx_num_packets=32; // 32 Ipx packets
114 ipx_packet * packets;
116 ushort ipx_packets_selector;
118 ecb_header * last_ecb=NULL;
121 static void got_new_packet( ecb_header * ecb );
122 static void ipx_listen_for_packet(ecb_header * ecb );
124 static void free_packet( int id )
126 packet_buffers[id].packetnum = -1;
127 packet_free_list[ --num_packets ] = id;
128 if (largest_packet_index==id)
129 while ((--largest_packet_index>0) && (packet_buffers[largest_packet_index].packetnum == -1 ));
132 static int ipx_dos_get_packet_data( ubyte * data )
134 int i, n, best, best_id, size;
136 for (i=1; i<ipx_num_packets; i++ ) {
137 if ( !packets[i].ecb.in_use ) {
138 got_new_packet( &packets[i].ecb );
139 packets[i].ecb.in_use = 0;
140 ipx_listen_for_packet(&packets[i].ecb);
148 for (i=0; i<=largest_packet_index; i++ ) {
149 if ( packet_buffers[i].packetnum > -1 ) {
151 if ( best == -1 || (packet_buffers[i].packetnum<best) ) {
152 best = packet_buffers[i].packetnum;
158 //mprintf( (0, "Best id = %d, pn = %d, last_ecb = %x, len=%x, ne = %d\n", best_id, best, last_ecb, lastlen, neterrors ));
159 //mprintf( (1, "<%d> ", neterrors ));
161 if ( best_id < 0 ) return 0;
163 size = packet_size[best_id];
164 memcpy( data, packet_buffers[best_id].data, size );
165 free_packet(best_id);
171 unsigned int swap_short( unsigned int short )
172 #pragma aux swap_short parm [eax] = "xchg al,ah";
174 static inline unsigned int swap_short( unsigned int sshort ) {
176 asm("xchg %%ah,%%al" : "=a" (__retval) : "a" (sshort));
181 static void got_new_packet( ecb_header * ecb )
185 unsigned short datasize;
189 p = (ipx_packet *)ecb;
191 if ( p->ecb.in_use ) { neterrors++; return; }
192 if ( p->ecb.completion_code ) { neterrors++; return; }
194 // Error( "Recieve error %d for completion code", p->ecb.completion_code );
196 if ( memcmp( &p->ipx.source.node_id, ipx_my_node, 6 ) ) {
197 datasize=swap_short(p->ipx.length);
199 datasize -= sizeof(ipx_header);
200 // Find slot to put packet in...
201 if ( datasize > 0 && datasize <= sizeof(packet_data) ) {
202 if ( num_packets >= MAX_PACKETS ) {
203 //printf( 1, "IPX: Packet buffer overrun!!!\n" );
207 id = packet_free_list[ num_packets++ ];
208 if (id > largest_packet_index ) largest_packet_index = id;
209 packet_size[id] = datasize-sizeof(int);
210 packet_buffers[id].packetnum = p->pd.packetnum;
211 if ( packet_buffers[id].packetnum < 0 ) { neterrors++; return; }
212 memcpy( packet_buffers[id].data, p->pd.data, packet_size[id] );
219 //ipx_listen_for_packet(&p->ecb);
222 /*ubyte * ipx_get_my_local_address()
224 return ipx_my_node.address;
227 ubyte * ipx_get_my_server_address()
229 return (ubyte *)&ipx_network;
232 static void ipx_listen_for_packet(ecb_header * ecb )
234 dpmi_real_regs rregs;
236 memset(&rregs,0,sizeof(dpmi_real_regs));
237 rregs.ebx = 4; // Listen For Packet function
238 rregs.esi = DPMI_real_offset(ecb);
239 rregs.es = DPMI_real_segment(ecb);
240 dpmi_real_int386x( 0x7A, &rregs );
243 /*static void ipx_cancel_listen_for_packet(ecb_header * ecb )
245 dpmi_real_regs rregs;
246 memset(&rregs,0,sizeof(dpmi_real_regs));
247 rregs.ebx = 6; // IPX Cancel event
248 rregs.esi = DPMI_real_offset(ecb);
249 rregs.es = DPMI_real_segment(ecb);
250 dpmi_real_int386x( 0x7A, &rregs );
254 static void ipx_send_packet(ecb_header * ecb )
256 dpmi_real_regs rregs;
257 memset(&rregs,0,sizeof(dpmi_real_regs));
258 rregs.ebx = 3; // Send Packet function
259 rregs.esi = DPMI_real_offset(ecb);
260 rregs.es = DPMI_real_segment(ecb);
261 dpmi_real_int386x( 0x7A, &rregs );
267 ubyte local_target[6];
268 } __pack__ net_xlat_info;
270 static void ipx_dos_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
272 net_xlat_info * info;
273 dpmi_real_regs rregs;
275 // Get dos memory for call...
276 info = (net_xlat_info *)dpmi_get_temp_low_buffer( sizeof(net_xlat_info) );
277 assert( info != NULL );
278 memcpy( info->network, server, 4 );
279 memcpy( info->node, node, 6 );
281 memset(&rregs,0,sizeof(dpmi_real_regs));
283 rregs.ebx = 2; // Get Local Target
284 rregs.es = DPMI_real_segment(info);
285 rregs.esi = DPMI_real_offset(info->network);
286 rregs.edi = DPMI_real_offset(info->local_target);
288 dpmi_real_int386x( 0x7A, &rregs );
290 // Save the local target...
291 memcpy( local_target, info->local_target, 6 );
294 static void ipx_close()
296 dpmi_real_regs rregs;
297 if ( ipx_installed ) {
298 // When using VLM's instead of NETX, the sockets don't
299 // seem to automatically get closed, so we must explicitly
300 // close them at program termination.
302 memset(&rregs,0,sizeof(dpmi_real_regs));
303 rregs.edx = ipx_socket;
304 rregs.ebx = 1; // Close socket
305 dpmi_real_int386x( 0x7A, &rregs );
309 static int ipx_init(int socket_number)
312 dpmi_real_regs rregs;
313 ubyte *ipx_real_buffer;
316 // atexit(ipx_close);
320 // init packet buffers.
321 for (i=0; i<MAX_PACKETS; i++ ) {
322 packet_buffers[i].packetnum = -1;
323 packet_free_list[i] = i;
326 largest_packet_index = 0;
328 // Get the IPX vector
329 memset(&rregs,0,sizeof(dpmi_real_regs));
330 rregs.eax=0x00007a00;
331 dpmi_real_int386x( 0x2f, &rregs );
333 if ( (rregs.eax & 0xFF) != 0xFF ) {
334 return IPX_NOT_INSTALLED;
336 ipx_vector_offset = rregs.edi & 0xFFFF;
337 ipx_vector_segment = rregs.es;
338 //printf( "IPX entry point at %.4x:%.4x\n", ipx_vector_segment, ipx_vector_offset );
340 // Open a socket for IPX
342 memset(&rregs,0,sizeof(dpmi_real_regs));
343 swab( (char *)&socket_number,(char *)&ipx_socket, 2 );
344 rregs.edx = ipx_socket;
345 rregs.eax = ipx_socket_life;
346 rregs.ebx = 0; // Open socket
347 dpmi_real_int386x( 0x7A, &rregs );
349 ipx_socket = rregs.edx & 0xFFFF;
351 if ( rregs.eax & 0xFF ) {
352 //mprintf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
353 return IPX_SOCKET_TABLE_FULL;
358 // Find our internetwork address
359 ipx_real_buffer = dpmi_get_temp_low_buffer( 1024 ); // 1k block
360 if ( ipx_real_buffer == NULL ) {
361 //printf( "Error allocation realmode memory\n" );
362 return IPX_NO_LOW_DOS_MEM;
365 memset(&rregs,0,sizeof(dpmi_real_regs));
366 rregs.ebx = 9; // Get internetwork address
367 rregs.esi = DPMI_real_offset(ipx_real_buffer);
368 rregs.es = DPMI_real_segment(ipx_real_buffer);
369 dpmi_real_int386x( 0x7A, &rregs );
371 if ( rregs.eax & 0xFF ) {
372 //printf( "Error getting internetwork address!\n" );
373 return IPX_SOCKET_TABLE_FULL;
376 /* memcpy( &ipx_network, ipx_real_buffer, 4 );
377 memcpy( ipx_my_node, &ipx_real_buffer[4], 6 );*/
378 memcpy(ipx_MyAddress,ipx_real_buffer,10);
380 if ( show_address ) {
381 printf( "My IPX addresss is " );
382 printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
383 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] );
387 packets = dpmi_real_malloc( sizeof(ipx_packet)*ipx_num_packets, &ipx_packets_selector );
388 if ( packets == NULL ) {
389 //printf( "Couldn't allocate real memory for %d packets\n", ipx_num_packets );
390 return IPX_NO_LOW_DOS_MEM;
392 #if 0 /* adb: not needed, fails with cwsdpmi */
393 if (!dpmi_lock_region( packets, sizeof(ipx_packet)*ipx_num_packets )) {
394 //printf( "Couldn't lock real memory for %d packets\n", ipx_num_packets );
395 return IPX_NO_LOW_DOS_MEM;
398 memset( packets, 0, sizeof(ipx_packet)*ipx_num_packets );
400 for (i=1; i<ipx_num_packets; i++ ) {
401 packets[i].ecb.in_use = 0x1d;
402 //packets[i].ecb.in_use = 0;
403 packets[i].ecb.socket_id = ipx_socket;
404 packets[i].ecb.fragment_count = 1;
405 packets[i].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[i].ipx);
406 packets[i].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[i].ipx);
407 packets[i].ecb.fragment_size = sizeof(ipx_packet)-sizeof(ecb_header); //-sizeof(ecb_header);
409 ipx_listen_for_packet(&packets[i].ecb);
412 packets[0].ecb.socket_id = ipx_socket;
413 packets[0].ecb.fragment_count = 1;
414 packets[0].ecb.fragment_pointer[0] = DPMI_real_offset(&packets[0].ipx);
415 packets[0].ecb.fragment_pointer[1] = DPMI_real_segment(&packets[0].ipx);
416 packets[0].ipx.packet_type = 4; // IPX packet
417 packets[0].ipx.destination.socket_id = ipx_socket;
418 // memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
419 memset( packets[0].ipx.destination.network_id, 0, 4 );
424 static void ipx_dos_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
426 assert(ipx_installed);
428 if ( datasize >= IPX_MAX_DATA_SIZE ) {
429 printf( "Data too big\n" );
430 //added/replaced on 11/8/98 by Victor Rachels to stop crappage
433 //end this section replacement - VR
436 // Make sure no one is already sending something
437 while( packets[0].ecb.in_use )
441 if (packets[0].ecb.completion_code) {
442 // printf( "Send error %d for completion code\n", packets[0].ecb.completion_code );
443 //added/replaced on 11/8/98 by Victor Rachels to stop crappage
446 //end this section replacement - VR
450 // Fill in destination address
451 if ( memcmp( network, &ipx_network, 4 ) )
452 memcpy( packets[0].ipx.destination.network_id, network, 4 );
454 memset( packets[0].ipx.destination.network_id, 0, 4 );
455 memcpy( packets[0].ipx.destination.node_id.address, address, 6 );
456 memcpy( packets[0].ecb.immediate_address.address, immediate_address, 6 );
457 packets[0].pd.packetnum = ipx_packetnum++;
459 // Fill in data to send
460 packets[0].ecb.fragment_size = sizeof(ipx_header) + sizeof(int) + datasize;
462 assert( datasize > 1 );
463 assert( packets[0].ecb.fragment_size <= 576 );
465 memcpy( packets[0].pd.data, data, datasize );
468 ipx_send_packet( &packets[0].ecb );
472 /*static int ipx_change_default_socket( ushort socket_number )
476 dpmi_real_regs rregs;
478 if ( !ipx_installed ) return -3;
481 memset(&rregs,0,sizeof(dpmi_real_regs));
482 swab( (char *)&socket_number,(char *)&new_ipx_socket, 2 );
483 rregs.edx = new_ipx_socket;
484 rregs.eax = ipx_socket_life;
485 rregs.ebx = 0; // Open socket
486 dpmi_real_int386x( 0x7A, &rregs );
488 new_ipx_socket = rregs.edx & 0xFFFF;
490 if ( rregs.eax & 0xFF ) {
491 //printf( (1, "IPX error opening channel %d\n", socket_number-IPX_DEFAULT_SOCKET ));
495 for (i=1; i<ipx_num_packets; i++ ) {
496 ipx_cancel_listen_for_packet(&packets[i].ecb);
499 // Close existing socket...
500 memset(&rregs,0,sizeof(dpmi_real_regs));
501 rregs.edx = ipx_socket;
502 rregs.ebx = 1; // Close socket
503 dpmi_real_int386x( 0x7A, &rregs );
505 ipx_socket = new_ipx_socket;
507 // Repost all listen requests on the new socket...
508 for (i=1; i<ipx_num_packets; i++ ) {
509 packets[i].ecb.in_use = 0;
510 packets[i].ecb.socket_id = ipx_socket;
511 ipx_listen_for_packet(&packets[i].ecb);
514 packets[0].ecb.socket_id = ipx_socket;
515 packets[0].ipx.destination.socket_id = ipx_socket;
518 // init packet buffers.
519 for (i=0; i<MAX_PACKETS; i++ ) {
520 packet_buffers[i].packetnum = -1;
521 packet_free_list[i] = i;
524 largest_packet_index = 0;
530 struct ipx_driver ipx_dos = {
539 ipx_dos_get_local_target,
540 ipx_dos_get_packet_data,
541 ipx_dos_send_packet_data
544 void arch_ipx_set_driver(int ipx_driver)
547 if (ipx_driver != IPX_DRIVER_IPX)
548 Warning("Unknown network driver! Defaulting to real IPX");
552 //---typedef struct rip_entry {
558 //---typedef struct rip_packet {
559 //--- ushort operation; //1=request, 2=response
560 //--- rip_entry rip[50];
564 //---void ipx_find_all_servers()
567 //--- rip_packet * rp;
568 //--- assert(ipx_installed);
570 //--- ipx_change_default_socket( 0x0453 );
571 //--- // ipx_change_default_socket( 0x5304 );
573 //--- // Make sure no one is already sending something
574 //--- while( packets[0].ecb.in_use )
578 //--- if (packets[0].ecb.completion_code) {
579 //--- printf( "AAAA:Send error %d for completion code\n", packets[0].ecb.completion_code );
583 //--- rp = (rip_packet *)&packets[0].pd;
585 //--- // Fill in destination address
587 //--- char mzero1[] = {0,0,0,1};
588 //--- char mzero[] = {0,0,0,0,0,1};
589 //--- char immediate[6];
590 //--- //memcpy( packets[0].ipx.destination.network_id, &ipx_network, 4 );
591 //--- //memcpy( packets[0].ipx.destination.node_id.address, ipx_my_node.address, 6 );
593 //--- memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
594 //--- memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
596 //--- memcpy( packets[0].ipx.destination.socket_id, &ipx_socket, 2 );
597 //--- memcpy( packets[0].ipx.source.network_id, &ipx_network, 4 );
598 //--- memcpy( packets[0].ipx.source.node_id.address, ipx_my_node.address, 6 );
599 //--- memcpy( packets[0].ipx.source.socket_id, &ipx_socket, 2 );
600 //--- //memcpy( packets[0].ecb.immediate_address.address, ipx_my_node.address, 6 );
601 //--- //mzero1[3] = 1;
602 //--- //memcpy( packets[0].ipx.destination.network_id, mzero1, 4 );
603 //--- //mzero[5] = 1;
604 //--- //memcpy( packets[0].ipx.destination.node_id.address, mzero, 6 );
605 //--- //ipx_get_local_target( mzero1, mzero, immediate );
606 //--- //memcpy( packets[0].ecb.immediate_address.address, mzero, 6 );
607 //--- //memcpy( packets[0].ecb.immediate_address.address, immediate, 6 );
608 //--- //mzero[5] = 0;
611 //--- packets[0].ipx.packet_type = 1; // RIP packet
613 //--- // Fill in data to send
614 //--- packets[0].ecb.fragment_size = sizeof(ipx_header) + sizeof(rip_packet);
615 //--- assert( packets[0].ecb.fragment_size <= 576 );
617 //--- rp->operation = 0; // Request
618 //--- for (i=0;i<50; i++) {
619 //--- rp->rip[i].network = 0xFFFFFFFF;
620 //--- rp->rip[i].nhops = 0;
621 //--- rp->rip[i].nticks = 0;
625 //--- ipx_send_packet( &packets[0].ecb );
627 //--- for (i=0;i<50; i++) {
628 //--- if ( rp->rip[i].network != 0xFFFFFFFF )
629 //--- printf( "Network = %8x, Hops=%d, Ticks=%d\n", rp->rip[i].network, rp->rip[i].nhops, rp->rip[i].nticks );