]> icculus.org git repositories - btb/d2x.git/blob - arch/linux/linuxnet.c
revert to previous version, so linux joystick works
[btb/d2x.git] / arch / linux / linuxnet.c
1 /* $Id: linuxnet.c,v 1.8 2003-03-19 23:20:09 btb Exp $ */
2 /*
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.
13 */
14
15 /*
16  *
17  * Linux net
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #ifdef NETWORK
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <netinet/in.h> /* for htons & co. */
32
33 #include "pstypes.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         if (FindArg("-kali")) {
109                 printf("Using Kali for network games\n");
110                 driver = &ipx_kali;
111 //added on 12/20/98 by Jan Kratochvil for direct TCP/IP games
112         } else if (FindArg("-udp")) {
113                 printf("Using native TCP/IP (UDP) for network games\n");
114                 driver = &ipx_udp;
115 //end this section addition - JK
116         } else {
117                 printf("Using real IPX for network games\n");
118                 driver = &ipx_bsd;
119         }
120         if ((i = FindArg("-ipxnetwork")) && Args[i + 1]) {
121                 unsigned long n = strtol(Args[i + 1], NULL, 16);
122                 ipx_MyAddress[0] = n >> 24; ipx_MyAddress[1] = (n >> 16) & 255;
123                 ipx_MyAddress[2] = (n >> 8) & 255; ipx_MyAddress[3] = n & 255;
124                 printf("IPX: Using network %08x\n", (unsigned int)n);
125         }
126         if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
127                 return -3;
128         }
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         atexit(ipx_close);
135         return 0;
136 }
137
138 void ipx_close()
139 {
140         if (ipx_installed)
141                 driver->CloseSocket(&ipx_socket_data);
142         ipx_installed = 0;
143 }
144
145 int ipx_get_packet_data( ubyte * data )
146 {
147         struct ipx_recv_data rd;
148         char buf[MAX_IPX_DATA];
149 //killed 6-15-99 to get rid of compile warnings - OE
150 //      uint best_id = 0;
151 //      uint pkt_num;
152 //end kill - OE
153         int size;
154         int best_size = 0;
155 //edited 04/12/99 Matt Mueller - duh, we don't want to throw all that data away!
156         //--killed-- Like the original, only take latest packet, throw away rest
157         //do _NOT_ throw them away!
158         while (driver->PacketReady(&ipx_socket_data)) {
159                 if ((size = 
160                      driver->ReceivePacket(&ipx_socket_data, buf, 
161                       sizeof(buf), &rd)) > 4) {
162                      if (!memcmp(rd.src_network, ipx_MyAddress, 10)) 
163                         continue;       /* don't get own pkts */
164 //--killed--                 pkt_num = *(uint *)buf;
165 //--killed--                 if (pkt_num >= best_id) {
166                         memcpy(data, buf + 4, size - 4);
167                                 return size-4;
168 //--killed--                    best_id = pkt_num;
169 //--killed--                    best_size = size - 4;
170 //--killed--                 }
171 //end edit -MM
172                 }
173         }
174         return best_size;
175 }
176
177 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
178 {
179         u_char buf[MAX_IPX_DATA];
180         IPXPacket_t ipx_header;
181         
182         memcpy(ipx_header.Destination.Network, network, 4);
183         memcpy(ipx_header.Destination.Node, immediate_address, 6);
184         *(u_short *)ipx_header.Destination.Socket = htons(ipx_socket_data.socket);
185         ipx_header.PacketType = 4; /* Packet Exchange */
186         *(uint *)buf = ipx_packetnum++;
187     //ipx_packettotal+=datasize+4;
188     //if (f2i(Players[Player_num].time_level) && (f2i(Players[Player_num].time_level)%10!=ipx_lastspeed))
189         //{
190         //   ipx_lastspeed=f2i(Players[Player_num].time_level)%10;
191         //   printf("tot=%i,t2=%i,time=%i,avg=%i,a2=%i\n",ipx_packetnum,ipx_packettotal,(int)f2i(Players[Player_num].time_level),
192         //          ipx_packetnum/(int)f2i(Players[Player_num].time_level),
193         //          ipx_packettotal/(int)f2i(Players[Player_num].time_level));
194         //}    
195         memcpy(buf + 4, data, datasize);
196         driver->SendPacket(&ipx_socket_data, &ipx_header, buf, datasize + 4);
197 }
198
199 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
200 {
201         // let's hope Linux knows how to route it
202         memcpy( local_target, node, 6 );
203 }
204
205 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )       
206 {
207         int i, j;
208         ubyte broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
209         ubyte local_address[6];
210
211         // Set to all networks besides mine
212         for (i=0; i<Ipx_num_networks; i++ )     {
213                 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) )      {
214                         ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast, local_address );
215                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, local_address );
216                 } else {
217                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, broadcast );
218                 }
219         }
220
221         //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast, broadcast );
222
223         // Send directly to all users not on my network or in the network list.
224         for (i=0; i<Ipx_num_users; i++ )        {
225                 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) )  {
226                         for (j=0; j<Ipx_num_networks; j++ )             {
227                                 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
228                                         goto SkipUser;
229                         }
230                         ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
231 SkipUser:
232                         j = 0;
233                 }
234         }
235 }
236
237 // Sends a non-localized packet... needs 4 byte server, 6 byte address
238 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
239 {
240         ubyte local_address[6];
241
242         if ( (*(uint *)server) != 0 )   {
243                 ipx_get_local_target( server, address, local_address );
244                 ipx_send_packet_data( data, datasize, server, address, local_address );
245         } else {
246                 // Old method, no server info.
247                 ipx_send_packet_data( data, datasize, server, address, address );
248         }
249 }
250
251 int ipx_change_default_socket( ushort socket_number )
252 {
253         if ( !ipx_installed ) return -3;
254
255         driver->CloseSocket(&ipx_socket_data);
256         if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
257                 return -3;
258         }
259         return 0;
260 }
261
262 void ipx_read_user_file(char * filename)
263 {
264         FILE * fp;
265         user_address tmp;
266         char temp_line[132], *p1;
267         int n, ln=0, x;
268
269         if (!filename) return;
270
271         Ipx_num_users = 0;
272
273         fp = fopen( filename, "rt" );
274         if ( !fp ) return;
275
276         printf( "Broadcast Users:\n" );
277
278         while (fgets(temp_line, 132, fp)) {
279                 ln++;
280                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
281                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
282 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
283                 if (strlen(temp_line) >= 21 && temp_line[8] == '/') {
284                         for (n = 0; n < 4; n++) {
285                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
286                                         break;
287                                 tmp.network[n] = x;
288                         }
289                         if (n != 4)
290                                 continue;
291                         for (n = 0; n < 6; n++) {
292                                 if (sscanf(temp_line + 9 + n * 2, "%2x", &x) != 1)
293                                         break;
294                                 tmp.node[n] = x;
295                         }
296                         if (n != 6)
297                                 continue;
298                 } else
299                         continue;
300 #else
301                 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] );
302                 if ( n != 10 ) continue;
303 #endif
304                 if ( Ipx_num_users < MAX_USERS )        {
305                         ubyte * ipx_real_buffer = (ubyte *)&tmp;
306                         ipx_get_local_target( tmp.network, tmp.node, tmp.address );
307                         Ipx_users[Ipx_num_users++] = tmp;
308                         printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
309                         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] );
310                 } else {
311                         printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
312                         fclose(fp);
313                         return;
314                 }
315         }
316         fclose(fp);
317 }
318
319
320 void ipx_read_network_file(char * filename)
321 {
322         FILE * fp;
323         user_address tmp;
324         char temp_line[132], *p1;
325         int i, n, ln=0, x;
326
327         if (!filename) return;
328
329         fp = fopen( filename, "rt" );
330         if ( !fp ) return;
331
332         printf( "Using Networks:\n" );
333         for (i=0; i<Ipx_num_networks; i++ )             {
334                 ubyte * n1 = (ubyte *)&Ipx_networks[i];
335                 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
336         }
337
338         while (fgets(temp_line, 132, fp)) {
339                 ln++;
340                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
341                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
342 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
343                 if (strlen(temp_line) >= 8) {
344                         for (n = 0; n < 4; n++) {
345                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
346                                         break;
347                                 tmp.network[n] = x;
348                         }
349                         if (n != 4)
350                                 continue;
351                 } else
352                         continue;
353 #else
354                 n = sscanf( temp_line, "%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3] );
355                 if ( n != 4 ) continue;
356 #endif
357                 if ( Ipx_num_networks < MAX_NETWORKS  ) {
358                         int j;
359                         for (j=0; j<Ipx_num_networks; j++ )     
360                                 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
361                                         break;
362                         if ( j >= Ipx_num_networks )    {
363                                 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
364                                 printf("  %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
365                         }
366                 } else {
367                         printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
368                         fclose(fp);
369                         return;
370                 }
371         }
372         fclose(fp);
373 }
374 #endif //NETWORK