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 #include <sys/types.h>
29 //added 05/17/99 Matt Mueller - needed to redefine FD_* so that no asm is used
32 #define MAX_IPX_DATA 576
34 ubyte broadcast_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
35 ubyte null_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
38 //ipx_socket_t ipx_socket_data;
39 ubyte ipx_installed=0;
40 ubyte ipx_atexit_installed=0;
41 //ushort ipx_socket = 0;
42 u_int32_t ipx_network = 0;
43 ubyte ipx_MyAddress[10];
44 int ipx_packetnum = 0; /* Sequence number */
46 /* User defined routing stuff */
47 typedef struct user_address {
53 int Ipx_num_users = 0;
54 user_address Ipx_users[MAX_USERS];
56 #define MAX_NETWORKS 64
57 int Ipx_num_networks = 0;
58 uint Ipx_networks[MAX_NETWORKS];
62 int ipx_general_PacketReady(int fd) {
68 tv.tv_sec = tv.tv_usec = 0;
69 if (select(fd + 1, &set, NULL, NULL, &tv) > 0)
75 struct ipx_driver *driver = NULL;
77 ubyte * ipx_get_my_server_address()
79 return (ubyte *)&ipx_network;
82 ubyte * ipx_get_my_local_address()
84 return (ubyte *)(ipx_MyAddress + 4);
87 //---------------------------------------------------------------
88 // Initializes all IPX internals.
89 // If socket_number==0, then opens next available socket.
90 // Returns: 0 if successful.
91 // -1 if socket already open.
92 // -2 if socket table full.
93 // -3 if IPX not installed.
94 // -4 if couldn't allocate low dos memory
95 // -5 if error with getting internetwork address
96 int ipx_init( int socket_number )
99 if (!driver) return -1;
100 memset(ipx_MyAddress,0,10);
101 if ((i = FindArg("-ipxnetwork")) && Args[i + 1]) {
102 unsigned long n = strtol(Args[i + 1], NULL, 16);
103 ipx_MyAddress[0] = n >> 24; ipx_MyAddress[1] = (n >> 16) & 255;
104 ipx_MyAddress[2] = (n >> 8) & 255; ipx_MyAddress[3] = n & 255;
105 printf("IPX: Using network %08x\n", (unsigned int)n);
107 if (driver->OpenSocket(socket_number)) {
111 // if (driver->GetMyAddress)
112 // driver->GetMyAddress();
113 memcpy(&ipx_network, ipx_MyAddress, 4);
114 Ipx_num_networks = 0;
115 memcpy( &Ipx_networks[Ipx_num_networks++], &ipx_network, 4 );
117 if (ipx_atexit_installed==0){
119 ipx_atexit_installed=1;
127 driver->CloseSocket();
132 int ipx_set_driver(char *arg)
136 if (!FindArg( "-nonetwork" )) {
140 if (Inferno_verbose) printf( "\n%s ", TXT_INITIALIZING_NETWORK);
141 if ((t=FindArg("-socket")))
142 socket = atoi( Args[t+1] );
144 if ((t=FindArg("-pps")) && (t = atoi(Args[t+1])) && (t >= 2) && (t <= 20)) {
145 Network_initial_pps = t;
147 if ( FindArg("-shortpackets") )
148 Network_initial_shortpackets = 1;
150 #ifdef SUPPORTS_NET_IP
151 if (strcmp(arg,"ip")==0){
155 driver=arch_ipx_set_driver(arg);
156 if ((ipx_error=ipx_init(IPX_DEFAULT_SOCKET+socket))==0) {
157 if (Inferno_verbose) printf( "%s %d.\n", TXT_IPX_CHANNEL, socket );
160 switch( ipx_error ) {
161 case 3: if (Inferno_verbose) printf( "%s\n", TXT_NO_NETWORK); break;
162 case -2: if (Inferno_verbose) printf( "%s 0x%x.\n", TXT_SOCKET_ERROR, IPX_DEFAULT_SOCKET+socket); break;
163 case -4: if (Inferno_verbose) printf( "%s\n", TXT_MEMORY_IPX ); break;
165 if (Inferno_verbose) printf( "%s %d", TXT_ERROR_IPX, ipx_error );
167 if (Inferno_verbose) printf( "%s\n",TXT_NETWORK_DISABLED);
168 Network_active = 0; // Assume no network
170 ipx_read_user_file( "descent.usr" );
171 ipx_read_network_file( "descent.net" );
172 //if ( FindArg( "-dynamicsockets" ))
173 // Network_allow_socket_changes = 1;
175 // Network_allow_socket_changes = 0;
177 if (Inferno_verbose) printf( "%s\n", TXT_NETWORK_DISABLED);
178 Network_active = 0; // Assume no network
181 return ipx_installed?0:-1;
184 int ipx_get_packet_data( ubyte * data )
186 if (driver->GetPacketData)
187 return driver->GetPacketData(data);
189 struct ipx_recv_data rd;
192 if (driver->usepacketnum)
193 buf=alloca(MAX_IPX_DATA);
196 //edited 04/12/99 Matt Mueller - duh, we don't want to throw all that data away!
197 while (driver->PacketReady()) {
198 if ((size = driver->ReceivePacket(buf, MAX_IPX_DATA, &rd)) > 4) {
199 if (!memcmp(rd.src_network, ipx_MyAddress, 10)) {
200 mprintf((0,"dumped my own packet\n"));
201 continue; /* don't get own pkts */
203 if (driver->usepacketnum){
204 memcpy(data, buf + 4, size - 4);
215 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
217 if (driver->SendPacketData)
218 driver->SendPacketData(data,datasize,network,address,immediate_address);
220 IPXPacket_t ipx_header;
222 memcpy(ipx_header.Destination.Network, network, 4);
223 memcpy(ipx_header.Destination.Node, immediate_address, 6);
224 // *(ushort *)ipx_header.Destination.Socket = htons(ipx_socket_data.socket);
225 ipx_header.PacketType = 4; /* Packet Exchange */
227 if (driver->usepacketnum){
228 ubyte buf[MAX_IPX_DATA];
229 *(uint *)buf = ipx_packetnum++;
231 memcpy(buf + 4, data, datasize);
232 driver->SendPacket(&ipx_header, buf, datasize + 4);
234 driver->SendPacket(&ipx_header, data, datasize);//we can save 4 bytes
238 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
240 if (driver->GetLocalTarget)
241 driver->GetLocalTarget(server,node,local_target);
243 // let's hope Linux knows how to route it
244 memcpy( local_target, node, 6 );
247 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )
250 ubyte local_address[6];
252 // Set to all networks besides mine
253 for (i=0; i<Ipx_num_networks; i++ ) {
254 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) ) {
255 ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast_addr, local_address );
256 ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast_addr, local_address );
258 ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast_addr, broadcast_addr );
262 //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast_addr, broadcast_addr );
264 // Send directly to all users not on my network or in the network list.
265 for (i=0; i<Ipx_num_users; i++ ) {
266 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) ) {
267 for (j=0; j<Ipx_num_networks; j++ ) {
268 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
271 ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
278 // Sends a non-localized packet... needs 4 byte server, 6 byte address
279 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
281 ubyte local_address[6];
283 if ( (*(uint *)server) != 0 ) {
284 ipx_get_local_target( server, address, local_address );
285 ipx_send_packet_data( data, datasize, server, address, local_address );
287 // Old method, no server info.
288 ipx_send_packet_data( data, datasize, server, address, address );
292 int ipx_check_ready_to_join(ubyte *server, ubyte *node){
293 if (!driver->CheckReadyToJoin)
295 return driver->CheckReadyToJoin(server,node);
299 int ipx_change_default_socket( ushort socket_number )
301 if ( !ipx_installed ) return -3;
303 driver->CloseSocket();
304 if (driver->OpenSocket(socket_number)) {
310 void ipx_read_user_file(char * filename)
314 char temp_line[132], *p1;
317 if (!filename) return;
321 fp = fopen( filename, "rt" );
324 printf( "Broadcast Users:\n" );
326 while (fgets(temp_line, 132, fp)) {
328 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
329 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
330 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
331 if (strlen(temp_line) >= 21 && temp_line[8] == '/') {
332 for (n = 0; n < 4; n++) {
333 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
339 for (n = 0; n < 6; n++) {
340 if (sscanf(temp_line + 9 + n * 2, "%2x", &x) != 1)
349 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] );
350 if ( n != 10 ) continue;
352 if ( Ipx_num_users < MAX_USERS ) {
353 ubyte * ipx_real_buffer = (ubyte *)&tmp;
354 ipx_get_local_target( tmp.network, tmp.node, tmp.address );
355 Ipx_users[Ipx_num_users++] = tmp;
356 printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
357 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] );
359 printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
368 void ipx_read_network_file(char * filename)
372 char temp_line[132], *p1;
375 if (!filename) return;
377 fp = fopen( filename, "rt" );
380 printf( "Using Networks:\n" );
381 for (i=0; i<Ipx_num_networks; i++ ) {
382 ubyte * n1 = (ubyte *)&Ipx_networks[i];
383 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
386 while (fgets(temp_line, 132, fp)) {
388 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
389 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
390 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
391 if (strlen(temp_line) >= 8) {
392 for (n = 0; n < 4; n++) {
393 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
402 n = sscanf( temp_line, "%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3] );
403 if ( n != 4 ) continue;
405 if ( Ipx_num_networks < MAX_NETWORKS ) {
407 for (j=0; j<Ipx_num_networks; j++ )
408 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
410 if ( j >= Ipx_num_networks ) {
411 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
412 printf(" %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
415 printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );