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