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