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