]> icculus.org git repositories - btb/d2x.git/blob - arch/linux/linuxnet.c
fixed networking fuck-up?
[btb/d2x.git] / arch / linux / linuxnet.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  *
16  * Linux net
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #ifdef NETWORK
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <netinet/in.h> /* for htons & co. */
31
32 #include "pstypes.h"
33 #include "config.h"
34 #include "args.h"
35
36 #include "ipx_drv.h"
37 #include "ipx_bsd.h"
38 #include "ipx_kali.h"
39 #include "ipx_udp.h"
40 //added 05/17/99 Matt Mueller - needed to redefine FD_* so that no asm is used
41 //#include "checker.h"
42 //end addition -MM
43 #define MAX_IPX_DATA 576
44
45 int ipx_fd;
46 ipx_socket_t ipx_socket_data;
47 ubyte ipx_installed=0;
48 ushort ipx_socket = 0;
49 uint ipx_network = 0;
50 ubyte ipx_MyAddress[10];
51 int ipx_packetnum = 0;                  /* Sequence number */
52 //int     ipx_packettotal=0,ipx_lastspeed=0;
53
54 /* User defined routing stuff */
55 typedef struct user_address {
56         ubyte network[4];
57         ubyte node[6];
58         ubyte address[6];
59 } user_address;
60 #define MAX_USERS 64
61 int Ipx_num_users = 0;
62 user_address Ipx_users[MAX_USERS];
63
64 #define MAX_NETWORKS 64
65 int Ipx_num_networks = 0;
66 uint Ipx_networks[MAX_NETWORKS];
67
68 void ipx_close(void);
69
70 int ipx_general_PacketReady(ipx_socket_t *s) {
71         fd_set set;
72         struct timeval tv;
73         
74         FD_ZERO(&set);
75         FD_SET(s->fd, &set);
76         tv.tv_sec = tv.tv_usec = 0;
77         if (select(s->fd + 1, &set, NULL, NULL, &tv) > 0)
78                 return 1;
79         else
80                 return 0;
81 }
82
83 struct ipx_driver *driver = &ipx_bsd;
84
85 ubyte * ipx_get_my_server_address()
86 {
87         return (ubyte *)&ipx_network;
88 }
89
90 ubyte * ipx_get_my_local_address()
91 {
92         return (ubyte *)(ipx_MyAddress + 4);
93 }
94
95 //---------------------------------------------------------------
96 // Initializes all IPX internals. 
97 // If socket_number==0, then opens next available socket.
98 // Returns:     0  if successful.
99 //                              -1 if socket already open.
100 //                              -2      if socket table full.
101 //                              -3 if IPX not installed.
102 //                              -4 if couldn't allocate low dos memory
103 //                              -5 if error with getting internetwork address
104 int ipx_init( int socket_number, int show_address )
105 {
106         int i;
107         
108 /* DPH: killed kali for now 
109         if (FindArg("-kali")) {
110                 printf("Using Kali for network games\n");
111                 driver = &ipx_kali;
112 //added on 12/20/98 by Jan Kratochvil for direct TCP/IP games
113         } else*/ if (FindArg("-udp")) {
114                 printf("Using native TCP/IP (UDP) for network games\n");
115                 driver = &ipx_udp;
116 //end this section addition - JK
117         } else {
118                 printf("Using real IPX for network games\n");
119                 driver = &ipx_bsd;
120         }
121         if ((i = FindArg("-ipxnetwork")) && Args[i + 1]) {
122                 unsigned long n = strtol(Args[i + 1], NULL, 16);
123                 ipx_MyAddress[0] = n >> 24; ipx_MyAddress[1] = (n >> 16) & 255;
124                 ipx_MyAddress[2] = (n >> 8) & 255; ipx_MyAddress[3] = n & 255;
125                 printf("IPX: Using network %08x\n", (unsigned int)n);
126         }
127         if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
128                 return -3;
129         }
130         driver->GetMyAddress();
131         memcpy(&ipx_network, ipx_MyAddress, 4);
132         Ipx_num_networks = 0;
133         memcpy( &Ipx_networks[Ipx_num_networks++], &ipx_network, 4 );
134         ipx_installed = 1;
135         atexit(ipx_close);
136         return 0;
137 }
138
139 void ipx_close()
140 {
141         if (ipx_installed)
142                 driver->CloseSocket(&ipx_socket_data);
143         ipx_installed = 0;
144 }
145
146 int ipx_get_packet_data( ubyte * data )
147 {
148         struct ipx_recv_data rd;
149         char buf[MAX_IPX_DATA];
150 //killed 6-15-99 to get rid of compile warnings - OE
151 //      uint best_id = 0;
152 //      uint pkt_num;
153 //end kill - OE
154         int size;
155         int best_size = 0;
156 //edited 04/12/99 Matt Mueller - duh, we don't want to throw all that data away!
157         //--killed-- Like the original, only take latest packet, throw away rest
158         //do _NOT_ throw them away!
159         while (driver->PacketReady(&ipx_socket_data)) {
160                 if ((size = 
161                      driver->ReceivePacket(&ipx_socket_data, buf, 
162                       sizeof(buf), &rd)) > 4) {
163                      if (!memcmp(rd.src_network, ipx_MyAddress, 10)) 
164                         continue;       /* don't get own pkts */
165 //--killed--                 pkt_num = *(uint *)buf;
166 //--killed--                 if (pkt_num >= best_id) {
167                         memcpy(data, buf + 4, size - 4);
168                                 return size-4;
169 //--killed--                    best_id = pkt_num;
170 //--killed--                    best_size = size - 4;
171 //--killed--                 }
172 //end edit -MM
173                 }
174         }
175         return best_size;
176 }
177
178 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
179 {
180         u_char buf[MAX_IPX_DATA];
181         IPXPacket_t ipx_header;
182         
183         memcpy(ipx_header.Destination.Network, network, 4);
184         memcpy(ipx_header.Destination.Node, immediate_address, 6);
185         *(u_short *)ipx_header.Destination.Socket = htons(ipx_socket_data.socket);
186         ipx_header.PacketType = 4; /* Packet Exchange */
187         *(uint *)buf = ipx_packetnum++;
188     //ipx_packettotal+=datasize+4;
189     //if (f2i(Players[Player_num].time_level) && (f2i(Players[Player_num].time_level)%10!=ipx_lastspeed))
190         //{
191         //   ipx_lastspeed=f2i(Players[Player_num].time_level)%10;
192         //   printf("tot=%i,t2=%i,time=%i,avg=%i,a2=%i\n",ipx_packetnum,ipx_packettotal,(int)f2i(Players[Player_num].time_level),
193         //          ipx_packetnum/(int)f2i(Players[Player_num].time_level),
194         //          ipx_packettotal/(int)f2i(Players[Player_num].time_level));
195         //}    
196         memcpy(buf + 4, data, datasize);
197         driver->SendPacket(&ipx_socket_data, &ipx_header, buf, datasize + 4);
198 }
199
200 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
201 {
202         // let's hope Linux knows how to route it
203         memcpy( local_target, node, 6 );
204 }
205
206 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )       
207 {
208         int i, j;
209         ubyte broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
210         ubyte local_address[6];
211
212         // Set to all networks besides mine
213         for (i=0; i<Ipx_num_networks; i++ )     {
214                 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) )      {
215                         ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast, local_address );
216                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, local_address );
217                 } else {
218                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, broadcast );
219                 }
220         }
221
222         //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast, broadcast );
223
224         // Send directly to all users not on my network or in the network list.
225         for (i=0; i<Ipx_num_users; i++ )        {
226                 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) )  {
227                         for (j=0; j<Ipx_num_networks; j++ )             {
228                                 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
229                                         goto SkipUser;
230                         }
231                         ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
232 SkipUser:
233                         j = 0;
234                 }
235         }
236 }
237
238 // Sends a non-localized packet... needs 4 byte server, 6 byte address
239 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
240 {
241         ubyte local_address[6];
242
243         if ( (*(uint *)server) != 0 )   {
244                 ipx_get_local_target( server, address, local_address );
245                 ipx_send_packet_data( data, datasize, server, address, local_address );
246         } else {
247                 // Old method, no server info.
248                 ipx_send_packet_data( data, datasize, server, address, address );
249         }
250 }
251
252 int ipx_change_default_socket( ushort socket_number )
253 {
254         if ( !ipx_installed ) return -3;
255
256         driver->CloseSocket(&ipx_socket_data);
257         if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
258                 return -3;
259         }
260         return 0;
261 }
262
263 void ipx_read_user_file(char * filename)
264 {
265         FILE * fp;
266         user_address tmp;
267         char temp_line[132], *p1;
268         int n, ln=0, x;
269
270         if (!filename) return;
271
272         Ipx_num_users = 0;
273
274         fp = fopen( filename, "rt" );
275         if ( !fp ) return;
276
277         printf( "Broadcast Users:\n" );
278
279         while (fgets(temp_line, 132, fp)) {
280                 ln++;
281                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
282                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
283 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
284                 if (strlen(temp_line) >= 21 && temp_line[8] == '/') {
285                         for (n = 0; n < 4; n++) {
286                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
287                                         break;
288                                 tmp.network[n] = x;
289                         }
290                         if (n != 4)
291                                 continue;
292                         for (n = 0; n < 6; n++) {
293                                 if (sscanf(temp_line + 9 + n * 2, "%2x", &x) != 1)
294                                         break;
295                                 tmp.node[n] = x;
296                         }
297                         if (n != 6)
298                                 continue;
299                 } else
300                         continue;
301 #else
302                 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] );
303                 if ( n != 10 ) continue;
304 #endif
305                 if ( Ipx_num_users < MAX_USERS )        {
306                         ubyte * ipx_real_buffer = (ubyte *)&tmp;
307                         ipx_get_local_target( tmp.network, tmp.node, tmp.address );
308                         Ipx_users[Ipx_num_users++] = tmp;
309                         printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
310                         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] );
311                 } else {
312                         printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
313                         fclose(fp);
314                         return;
315                 }
316         }
317         fclose(fp);
318 }
319
320
321 void ipx_read_network_file(char * filename)
322 {
323         FILE * fp;
324         user_address tmp;
325         char temp_line[132], *p1;
326         int i, n, ln=0, x;
327
328         if (!filename) return;
329
330         fp = fopen( filename, "rt" );
331         if ( !fp ) return;
332
333         printf( "Using Networks:\n" );
334         for (i=0; i<Ipx_num_networks; i++ )             {
335                 ubyte * n1 = (ubyte *)&Ipx_networks[i];
336                 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
337         }
338
339         while (fgets(temp_line, 132, fp)) {
340                 ln++;
341                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
342                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
343 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
344                 if (strlen(temp_line) >= 8) {
345                         for (n = 0; n < 4; n++) {
346                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
347                                         break;
348                                 tmp.network[n] = x;
349                         }
350                         if (n != 4)
351                                 continue;
352                 } else
353                         continue;
354 #else
355                 n = sscanf( temp_line, "%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3] );
356                 if ( n != 4 ) continue;
357 #endif
358                 if ( Ipx_num_networks < MAX_NETWORKS  ) {
359                         int j;
360                         for (j=0; j<Ipx_num_networks; j++ )     
361                                 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
362                                         break;
363                         if ( j >= Ipx_num_networks )    {
364                                 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
365                                 printf("  %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
366                         }
367                 } else {
368                         printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
369                         fclose(fp);
370                         return;
371                 }
372         }
373         fclose(fp);
374 }
375 #endif //NETWORK