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