]> icculus.org git repositories - btb/d2x.git/blob - arch/linux_net.c
build fixes
[btb/d2x.git] / arch / linux_net.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  * $Source: /cvs/cvsroot/d2x/arch/linux_net.c,v $
16  * $Revision: 1.3 $
17  * $Author: bradleyb $
18  * $Date: 2001-01-29 13:35:09 $
19  *
20  * Linux net
21  *
22  * $Log: not supported by cvs2svn $
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <conf.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.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 "config.h"
38 #include "args.h"
39
40 #include "ipx_drv.h"
41 #include "ipx_bsd.h"
42 #include "ipx_kali.h"
43 #include "ipx_udp.h"
44 //added 05/17/99 Matt Mueller - needed to redefine FD_* so that no asm is used
45 //#include "checker.h"
46 //end addition -MM
47 #define MAX_IPX_DATA 576
48
49 int ipx_fd;
50 ipx_socket_t ipx_socket_data;
51 ubyte ipx_installed=0;
52 ushort ipx_socket = 0;
53 uint ipx_network = 0;
54 ubyte ipx_MyAddress[10];
55 int ipx_packetnum = 0;                  /* Sequence number */
56 //int     ipx_packettotal=0,ipx_lastspeed=0;
57
58 /* User defined routing stuff */
59 typedef struct user_address {
60         ubyte network[4];
61         ubyte node[6];
62         ubyte address[6];
63 } user_address;
64 #define MAX_USERS 64
65 int Ipx_num_users = 0;
66 user_address Ipx_users[MAX_USERS];
67
68 #define MAX_NETWORKS 64
69 int Ipx_num_networks = 0;
70 uint Ipx_networks[MAX_NETWORKS];
71
72 void ipx_close(void);
73
74 int ipx_general_PacketReady(ipx_socket_t *s) {
75         fd_set set;
76         struct timeval tv;
77         
78         FD_ZERO(&set);
79         FD_SET(s->fd, &set);
80         tv.tv_sec = tv.tv_usec = 0;
81         if (select(s->fd + 1, &set, NULL, NULL, &tv) > 0)
82                 return 1;
83         else
84                 return 0;
85 }
86
87 struct ipx_driver *driver = &ipx_bsd;
88
89 ubyte * ipx_get_my_server_address()
90 {
91         return (ubyte *)&ipx_network;
92 }
93
94 ubyte * ipx_get_my_local_address()
95 {
96         return (ubyte *)(ipx_MyAddress + 4);
97 }
98
99 //---------------------------------------------------------------
100 // Initializes all IPX internals. 
101 // If socket_number==0, then opens next available socket.
102 // Returns:     0  if successful.
103 //                              -1 if socket already open.
104 //                              -2      if socket table full.
105 //                              -3 if IPX not installed.
106 //                              -4 if couldn't allocate low dos memory
107 //                              -5 if error with getting internetwork address
108 int ipx_init( int socket_number, int show_address )
109 {
110         int i;
111         
112 /* DPH: killed kali for now 
113         if (FindArg("-kali")) {
114                 printf("Using Kali for network games\n");
115                 driver = &ipx_kali;
116 //added on 12/20/98 by Jan Kratochvil for direct TCP/IP games
117         } else*/ if (FindArg("-udp")) {
118                 printf("Using native TCP/IP (UDP) for network games\n");
119                 driver = &ipx_udp;
120 //end this section addition - JK
121         } else {
122                 printf("Using real IPX for network games\n");
123                 driver = &ipx_bsd;
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 -3;
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 0;
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 = *(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         memcpy(ipx_header.Destination.Network, network, 4);
188         memcpy(ipx_header.Destination.Node, immediate_address, 6);
189         *(u_short *)ipx_header.Destination.Socket = htons(ipx_socket_data.socket);
190         ipx_header.PacketType = 4; /* Packet Exchange */
191         *(uint *)buf = ipx_packetnum++;
192     //ipx_packettotal+=datasize+4;
193     //if (f2i(Players[Player_num].time_level) && (f2i(Players[Player_num].time_level)%10!=ipx_lastspeed))
194         //{
195         //   ipx_lastspeed=f2i(Players[Player_num].time_level)%10;
196         //   printf("tot=%i,t2=%i,time=%i,avg=%i,a2=%i\n",ipx_packetnum,ipx_packettotal,(int)f2i(Players[Player_num].time_level),
197         //          ipx_packetnum/(int)f2i(Players[Player_num].time_level),
198         //          ipx_packettotal/(int)f2i(Players[Player_num].time_level));
199         //}    
200         memcpy(buf + 4, data, datasize);
201         driver->SendPacket(&ipx_socket_data, &ipx_header, buf, datasize + 4);
202 }
203
204 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
205 {
206         // let's hope Linux knows how to route it
207         memcpy( local_target, node, 6 );
208 }
209
210 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )       
211 {
212         int i, j;
213         ubyte broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
214         ubyte local_address[6];
215
216         // Set to all networks besides mine
217         for (i=0; i<Ipx_num_networks; i++ )     {
218                 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) )      {
219                         ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast, local_address );
220                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, local_address );
221                 } else {
222                         ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, broadcast );
223                 }
224         }
225
226         //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast, broadcast );
227
228         // Send directly to all users not on my network or in the network list.
229         for (i=0; i<Ipx_num_users; i++ )        {
230                 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) )  {
231                         for (j=0; j<Ipx_num_networks; j++ )             {
232                                 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
233                                         goto SkipUser;
234                         }
235                         ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
236 SkipUser:
237                         j = 0;
238                 }
239         }
240 }
241
242 // Sends a non-localized packet... needs 4 byte server, 6 byte address
243 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
244 {
245         ubyte local_address[6];
246
247         if ( (*(uint *)server) != 0 )   {
248                 ipx_get_local_target( server, address, local_address );
249                 ipx_send_packet_data( data, datasize, server, address, local_address );
250         } else {
251                 // Old method, no server info.
252                 ipx_send_packet_data( data, datasize, server, address, address );
253         }
254 }
255
256 int ipx_change_default_socket( ushort socket_number )
257 {
258         if ( !ipx_installed ) return -3;
259
260         driver->CloseSocket(&ipx_socket_data);
261         if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
262                 return -3;
263         }
264         return 0;
265 }
266
267 void ipx_read_user_file(char * filename)
268 {
269         FILE * fp;
270         user_address tmp;
271         char temp_line[132], *p1;
272         int n, ln=0, x;
273
274         if (!filename) return;
275
276         Ipx_num_users = 0;
277
278         fp = fopen( filename, "rt" );
279         if ( !fp ) return;
280
281         printf( "Broadcast Users:\n" );
282
283         while (fgets(temp_line, 132, fp)) {
284                 ln++;
285                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
286                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
287 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
288                 if (strlen(temp_line) >= 21 && temp_line[8] == '/') {
289                         for (n = 0; n < 4; n++) {
290                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
291                                         break;
292                                 tmp.network[n] = x;
293                         }
294                         if (n != 4)
295                                 continue;
296                         for (n = 0; n < 6; n++) {
297                                 if (sscanf(temp_line + 9 + n * 2, "%2x", &x) != 1)
298                                         break;
299                                 tmp.node[n] = x;
300                         }
301                         if (n != 6)
302                                 continue;
303                 } else
304                         continue;
305 #else
306                 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] );
307                 if ( n != 10 ) continue;
308 #endif
309                 if ( Ipx_num_users < MAX_USERS )        {
310                         ubyte * ipx_real_buffer = (ubyte *)&tmp;
311                         ipx_get_local_target( tmp.network, tmp.node, tmp.address );
312                         Ipx_users[Ipx_num_users++] = tmp;
313                         printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
314                         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] );
315                 } else {
316                         printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
317                         fclose(fp);
318                         return;
319                 }
320         }
321         fclose(fp);
322 }
323
324
325 void ipx_read_network_file(char * filename)
326 {
327         FILE * fp;
328         user_address tmp;
329         char temp_line[132], *p1;
330         int i, n, ln=0, x;
331
332         if (!filename) return;
333
334         fp = fopen( filename, "rt" );
335         if ( !fp ) return;
336
337         printf( "Using Networks:\n" );
338         for (i=0; i<Ipx_num_networks; i++ )             {
339                 ubyte * n1 = (ubyte *)&Ipx_networks[i];
340                 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
341         }
342
343         while (fgets(temp_line, 132, fp)) {
344                 ln++;
345                 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
346                 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
347 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
348                 if (strlen(temp_line) >= 8) {
349                         for (n = 0; n < 4; n++) {
350                                 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
351                                         break;
352                                 tmp.network[n] = x;
353                         }
354                         if (n != 4)
355                                 continue;
356                 } else
357                         continue;
358 #else
359                 n = sscanf( temp_line, "%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3] );
360                 if ( n != 4 ) continue;
361 #endif
362                 if ( Ipx_num_networks < MAX_NETWORKS  ) {
363                         int j;
364                         for (j=0; j<Ipx_num_networks; j++ )     
365                                 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
366                                         break;
367                         if ( j >= Ipx_num_networks )    {
368                                 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
369                                 printf("  %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
370                         }
371                 } else {
372                         printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
373                         fclose(fp);
374                         return;
375                 }
376         }
377         fclose(fp);
378 }