]> icculus.org git repositories - btb/d2x.git/blob - main/ipx_drv.c
new file
[btb/d2x.git] / main / ipx_drv.c
1 /*
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.
12 */
13
14 /*
15  * $Source: /cvs/cvsroot/d2x/main/ipx_drv.c,v $
16  * $Revision: 1.2 $
17  * $Author: bradleyb $
18  * $Date: 2001-10-19 04:10:27 $
19  *
20  * FIXME: put description here
21  *
22  * $Log: not supported by cvs2svn $
23  *
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include <conf.h>
28 #endif
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/time.h>
35
36 #include "pstypes.h"
37 #include "config.h"
38 #include "args.h"
39 #include "text.h"
40
41 #include "network.h"
42 #include "mono.h"
43
44 #include "ipx.h"
45 #include "ipx_drv.h"
46 //added 05/17/99 Matt Mueller - needed to redefine FD_* so that no asm is used
47 #include "checker.h"
48 //end addition -MM
49 #define MAX_IPX_DATA 576
50
51 ubyte broadcast_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
52 ubyte null_addr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
53
54 //int ipx_fd;
55 //ipx_socket_t ipx_socket_data;
56 ubyte ipx_installed=0;
57 ubyte ipx_atexit_installed=0;
58 //ushort ipx_socket = 0;
59 u_int32_t ipx_network = 0;
60 ubyte ipx_MyAddress[10];
61 int ipx_packetnum = 0;                  /* Sequence number */
62
63 /* User defined routing stuff */
64 typedef struct user_address {
65         ubyte network[4];
66         ubyte node[6];
67         ubyte address[6];
68 } user_address;
69 #define MAX_USERS 64
70 int Ipx_num_users = 0;
71 user_address Ipx_users[MAX_USERS];
72
73 #define MAX_NETWORKS 64
74 int Ipx_num_networks = 0;
75 uint Ipx_networks[MAX_NETWORKS];
76
77 void ipx_close(void);
78
79 int ipx_general_PacketReady(int fd) {
80         fd_set set;
81         struct timeval tv;
82         
83         FD_ZERO(&set);
84         FD_SET(fd, &set);
85         tv.tv_sec = tv.tv_usec = 0;
86         if (select(fd + 1, &set, NULL, NULL, &tv) > 0)
87                 return 1;
88         else
89                 return 0;
90 }
91
92 struct ipx_driver *driver = NULL;
93
94 ubyte * ipx_get_my_server_address()
95 {
96         return (ubyte *)&ipx_network;
97 }
98
99 ubyte * ipx_get_my_local_address()
100 {
101         return (ubyte *)(ipx_MyAddress + 4);
102 }
103
104 //---------------------------------------------------------------
105 // Initializes all IPX internals. 
106 // If socket_number==0, then opens next available socket.
107 // Returns:     0  if successful.
108 //                              -1 if socket already open.
109 //                              -2      if socket table full.
110 //                              -3 if IPX not installed.
111 //                              -4 if couldn't allocate low dos memory
112 //                              -5 if error with getting internetwork address
113 int ipx_init( int socket_number )
114 {
115         int i;
116         if (!driver) return -1;
117         memset(ipx_MyAddress,0,10);
118         if ((i = FindArg("-ipxnetwork")) && Args[i + 1]) {
119                 unsigned long n = strtol(Args[i + 1], NULL, 16);
120                 ipx_MyAddress[0] = n >> 24; ipx_MyAddress[1] = (n >> 16) & 255;
121                 ipx_MyAddress[2] = (n >> 8) & 255; ipx_MyAddress[3] = n & 255;
122                 printf("IPX: Using network %08x\n", (unsigned int)n);
123         }
124         if (driver->OpenSocket(socket_number)) {
125                 return -3;
126         }
127
128 //      if (driver->GetMyAddress)
129 //              driver->GetMyAddress();
130         memcpy(&ipx_network, ipx_MyAddress, 4);
131         Ipx_num_networks = 0;
132         memcpy( &Ipx_networks[Ipx_num_networks++], &ipx_network, 4 );
133         ipx_installed = 1;
134         if (ipx_atexit_installed==0){
135                 atexit(ipx_close);
136                 ipx_atexit_installed=1;
137         }
138         return 0;
139 }
140
141 void ipx_close()
142 {
143         if (ipx_installed)
144                 driver->CloseSocket();
145         ipx_installed = 0;
146 }
147
148 //########printfs??
149 int ipx_set_driver(char *arg)
150 {
151         ipx_close();
152
153         if (!FindArg( "-nonetwork" ))   {
154                 int socket=0;
155                 int ipx_error;
156                 int t;
157                 if (Inferno_verbose) printf( "\n%s ", TXT_INITIALIZING_NETWORK);
158                 if ((t=FindArg("-socket")))
159                         socket = atoi( Args[t+1] );
160
161                 if ((t=FindArg("-pps")) && (t = atoi(Args[t+1])) && (t >= 2) && (t <= 20)) {
162                         Network_initial_pps = t;
163                 }
164                 if ( FindArg("-shortpackets") )
165                         Network_initial_shortpackets = 1;
166
167 #ifdef SUPPORTS_NET_IP
168                 if (strcmp(arg,"ip")==0){
169                         driver=&ipx_ip;
170                 }else
171 #endif
172                         driver=arch_ipx_set_driver(arg);
173                 if ((ipx_error=ipx_init(IPX_DEFAULT_SOCKET+socket))==0) {
174                         if (Inferno_verbose) printf( "%s %d.\n", TXT_IPX_CHANNEL, socket );
175                         Network_active = 1;
176                 } else {
177                         switch( ipx_error )     {
178                                 case 3:         if (Inferno_verbose) printf( "%s\n", TXT_NO_NETWORK); break;
179                                 case -2: if (Inferno_verbose) printf( "%s 0x%x.\n", TXT_SOCKET_ERROR, IPX_DEFAULT_SOCKET+socket); break;
180                                 case -4: if (Inferno_verbose) printf( "%s\n", TXT_MEMORY_IPX ); break;
181                                 default:
182                                                          if (Inferno_verbose) printf( "%s %d", TXT_ERROR_IPX, ipx_error );
183                         }
184                         if (Inferno_verbose) printf( "%s\n",TXT_NETWORK_DISABLED);
185                         Network_active = 0;             // Assume no network
186                 }
187                 ipx_read_user_file( "descent.usr" );
188                 ipx_read_network_file( "descent.net" );
189                 //if ( FindArg( "-dynamicsockets" ))
190                 //        Network_allow_socket_changes = 1;
191                 //else
192                 //        Network_allow_socket_changes = 0;
193         } else {
194                 if (Inferno_verbose) printf( "%s\n", TXT_NETWORK_DISABLED);
195                 Network_active = 0;             // Assume no network
196         }
197
198         return ipx_installed?0:-1;
199 }
200
201 int ipx_get_packet_data( ubyte * data )
202 {
203         if (driver->GetPacketData)
204                 return driver->GetPacketData(data);
205         else {
206                 struct ipx_recv_data rd;
207                 char *buf;
208                 int size;
209                 if (driver->usepacketnum)
210                         buf=alloca(MAX_IPX_DATA);
211                 else
212                         buf=data;
213                 //edited 04/12/99 Matt Mueller - duh, we don't want to throw all that data away!
214                 while (driver->PacketReady()) {
215                         if ((size =     driver->ReceivePacket(buf, MAX_IPX_DATA, &rd)) > 4) {
216                                 if (!memcmp(rd.src_network, ipx_MyAddress, 10)) {
217                                         mprintf((0,"dumped my own packet\n"));
218                                         continue;       /* don't get own pkts */
219                                 }
220                                 if (driver->usepacketnum){
221                                         memcpy(data, buf + 4, size - 4);
222                                         return size-4;
223                                 }else{
224                                         return size;
225                                 }
226                         }
227                 }
228                 return 0;
229         }
230 }
231
232 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
233 {
234         if (driver->SendPacketData)
235                 driver->SendPacketData(data,datasize,network,address,immediate_address);
236         else{
237                 IPXPacket_t ipx_header;
238
239                 memcpy(ipx_header.Destination.Network, network, 4);
240                 memcpy(ipx_header.Destination.Node, immediate_address, 6);
241 //              *(ushort *)ipx_header.Destination.Socket = htons(ipx_socket_data.socket);
242                 ipx_header.PacketType = 4; /* Packet Exchange */
243
244                 if (driver->usepacketnum){
245                         ubyte buf[MAX_IPX_DATA];
246                         *(uint *)buf = ipx_packetnum++;
247
248                         memcpy(buf + 4, data, datasize);
249                         driver->SendPacket(&ipx_header, buf, datasize + 4);
250                 }else
251                         driver->SendPacket(&ipx_header, data, datasize);//we can save 4 bytes
252         }
253 }
254
255 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
256 {
257         if (driver->GetLocalTarget)
258                 driver->GetLocalTarget(server,node,local_target);
259         else
260         // let's hope Linux knows how to route it
261                 memcpy( local_target, node, 6 );
262 }
263
264 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )       
265 {
266         int i, j;
267         ubyte local_address[6];
268
269         // Set to all networks besides mine
270         for (i=0; i<Ipx_num_networks; i++ )     {
271                 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) )      {
272                         ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast_addr, local_address );
273                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast_addr, local_address );
274                 } else {
275                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast_addr, broadcast_addr );
276                 }
277         }
278
279         //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast_addr, broadcast_addr );
280
281         // Send directly to all users not on my network or in the network list.
282         for (i=0; i<Ipx_num_users; i++ )        {
283                 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) )  {
284                         for (j=0; j<Ipx_num_networks; j++ )             {
285                                 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
286                                         goto SkipUser;
287                         }
288                         ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
289 SkipUser:
290                         j = 0;
291                 }
292         }
293 }
294
295 // Sends a non-localized packet... needs 4 byte server, 6 byte address
296 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
297 {
298         ubyte local_address[6];
299
300         if ( (*(uint *)server) != 0 )   {
301                 ipx_get_local_target( server, address, local_address );
302                 ipx_send_packet_data( data, datasize, server, address, local_address );
303         } else {
304                 // Old method, no server info.
305                 ipx_send_packet_data( data, datasize, server, address, address );
306         }
307 }
308
309 int ipx_check_ready_to_join(ubyte *server, ubyte *node){
310         if (!driver->CheckReadyToJoin)
311                 return 1;
312         return driver->CheckReadyToJoin(server,node);
313 }
314
315
316 int ipx_change_default_socket( ushort socket_number )
317 {
318         if ( !ipx_installed ) return -3;
319
320         driver->CloseSocket();
321         if (driver->OpenSocket(socket_number)) {
322                 return -3;
323         }
324         return 0;
325 }
326
327 void ipx_read_user_file(char * filename)
328 {
329         FILE * fp;
330         user_address tmp;
331         char temp_line[132], *p1;
332         int n, ln=0, x;
333
334         if (!filename) return;
335
336         Ipx_num_users = 0;
337
338         fp = fopen( filename, "rt" );
339         if ( !fp ) return;
340
341         printf( "Broadcast Users:\n" );
342
343         while (fgets(temp_line, 132, fp)) {
344                 ln++;
345                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
346                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
347 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
348                 if (strlen(temp_line) >= 21 && temp_line[8] == '/') {
349                         for (n = 0; n < 4; n++) {
350                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
351                                         break;
352                                 tmp.network[n] = x;
353                         }
354                         if (n != 4)
355                                 continue;
356                         for (n = 0; n < 6; n++) {
357                                 if (sscanf(temp_line + 9 + n * 2, "%2x", &x) != 1)
358                                         break;
359                                 tmp.node[n] = x;
360                         }
361                         if (n != 6)
362                                 continue;
363                 } else
364                         continue;
365 #else
366                 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] );
367                 if ( n != 10 ) continue;
368 #endif
369                 if ( Ipx_num_users < MAX_USERS )        {
370                         ubyte * ipx_real_buffer = (ubyte *)&tmp;
371                         ipx_get_local_target( tmp.network, tmp.node, tmp.address );
372                         Ipx_users[Ipx_num_users++] = tmp;
373                         printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
374                         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] );
375                 } else {
376                         printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
377                         fclose(fp);
378                         return;
379                 }
380         }
381         fclose(fp);
382 }
383
384
385 void ipx_read_network_file(char * filename)
386 {
387         FILE * fp;
388         user_address tmp;
389         char temp_line[132], *p1;
390         int i, n, ln=0, x;
391
392         if (!filename) return;
393
394         fp = fopen( filename, "rt" );
395         if ( !fp ) return;
396
397         printf( "Using Networks:\n" );
398         for (i=0; i<Ipx_num_networks; i++ )             {
399                 ubyte * n1 = (ubyte *)&Ipx_networks[i];
400                 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
401         }
402
403         while (fgets(temp_line, 132, fp)) {
404                 ln++;
405                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
406                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
407 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
408                 if (strlen(temp_line) >= 8) {
409                         for (n = 0; n < 4; n++) {
410                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
411                                         break;
412                                 tmp.network[n] = x;
413                         }
414                         if (n != 4)
415                                 continue;
416                 } else
417                         continue;
418 #else
419                 n = sscanf( temp_line, "%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3] );
420                 if ( n != 4 ) continue;
421 #endif
422                 if ( Ipx_num_networks < MAX_NETWORKS  ) {
423                         int j;
424                         for (j=0; j<Ipx_num_networks; j++ )     
425                                 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
426                                         break;
427                         if ( j >= Ipx_num_networks )    {
428                                 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
429                                 printf("  %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
430                         }
431                 } else {
432                         printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
433                         fclose(fp);
434                         return;
435                 }
436         }
437         fclose(fp);
438 }