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