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