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