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