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.
20 #include <sys/types.h>
22 #include <netinet/in.h> /* for htons & co. */
32 //added 05/17/99 Matt Mueller - needed to redefine FD_* so that no asm is used
33 //#include "checker.h"
35 #define MAX_IPX_DATA 576
38 ipx_socket_t ipx_socket_data;
39 ubyte ipx_installed=0;
40 ushort ipx_socket = 0;
42 ubyte ipx_MyAddress[10];
43 int ipx_packetnum = 0; /* Sequence number */
44 //int ipx_packettotal=0,ipx_lastspeed=0;
46 /* User defined routing stuff */
47 typedef struct user_address {
53 int Ipx_num_users = 0;
54 user_address Ipx_users[MAX_USERS];
56 #define MAX_NETWORKS 64
57 int Ipx_num_networks = 0;
58 uint Ipx_networks[MAX_NETWORKS];
62 int ipx_general_PacketReady(ipx_socket_t *s) {
68 tv.tv_sec = tv.tv_usec = 0;
69 if (select(s->fd + 1, &set, NULL, NULL, &tv) > 0)
75 struct ipx_driver *driver = &ipx_bsd;
77 ubyte * ipx_get_my_server_address()
79 return (ubyte *)&ipx_network;
82 ubyte * ipx_get_my_local_address()
84 return (ubyte *)(ipx_MyAddress + 4);
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 )
100 /* DPH: killed kali for now
101 if (FindArg("-kali")) {
102 printf("Using Kali for network games\n");
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");
108 //end this section addition - JK
110 printf("Using real IPX for network games\n");
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);
119 if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
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 );
134 driver->CloseSocket(&ipx_socket_data);
138 int ipx_get_packet_data( ubyte * data )
140 struct ipx_recv_data rd;
141 char buf[MAX_IPX_DATA];
142 //killed 6-15-99 to get rid of compile warnings - OE
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)) {
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);
161 //--killed-- best_id = pkt_num;
162 //--killed-- best_size = size - 4;
170 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
172 u_char buf[MAX_IPX_DATA];
173 IPXPacket_t ipx_header;
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))
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));
188 memcpy(buf + 4, data, datasize);
189 driver->SendPacket(&ipx_socket_data, &ipx_header, buf, datasize + 4);
192 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
194 // let's hope Linux knows how to route it
195 memcpy( local_target, node, 6 );
198 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )
201 ubyte broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
202 ubyte local_address[6];
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 );
210 ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, broadcast );
214 //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast, broadcast );
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 ))
223 ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
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 )
233 ubyte local_address[6];
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 );
239 // Old method, no server info.
240 ipx_send_packet_data( data, datasize, server, address, address );
244 int ipx_change_default_socket( ushort socket_number )
246 if ( !ipx_installed ) return -3;
248 driver->CloseSocket(&ipx_socket_data);
249 if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
255 void ipx_read_user_file(char * filename)
259 char temp_line[132], *p1;
262 if (!filename) return;
266 fp = fopen( filename, "rt" );
269 printf( "Broadcast Users:\n" );
271 while (fgets(temp_line, 132, fp)) {
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)
284 for (n = 0; n < 6; n++) {
285 if (sscanf(temp_line + 9 + n * 2, "%2x", &x) != 1)
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;
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] );
304 printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
313 void ipx_read_network_file(char * filename)
317 char temp_line[132], *p1;
320 if (!filename) return;
322 fp = fopen( filename, "rt" );
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] );
331 while (fgets(temp_line, 132, fp)) {
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)
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;
350 if ( Ipx_num_networks < MAX_NETWORKS ) {
352 for (j=0; j<Ipx_num_networks; j++ )
353 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
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] );
360 printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
368 #endif // __ENV_LINUX__