1 /* $Id: winnet.c,v 1.10 2003-10-12 09:17:47 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * Win32 lower-level network code.
18 * implements functions declared in include/ipx.h
36 #include "ipx_mcast4.h"
37 #include "../../main/player.h" /* for Players */
38 #include "../../main/multi.h" /* for NetPlayers */
40 extern struct ipx_driver ipx_win;
42 #define MAX_IPX_DATA 576
45 ipx_socket_t ipx_socket_data;
46 ubyte ipx_installed=0;
47 ushort ipx_socket = 0;
49 ubyte ipx_MyAddress[10];
50 int ipx_packetnum = 0; /* Sequence number */
52 /* User defined routing stuff */
53 typedef struct user_address {
59 int Ipx_num_users = 0;
60 user_address Ipx_users[MAX_USERS];
62 #define MAX_NETWORKS 64
63 int Ipx_num_networks = 0;
64 uint Ipx_networks[MAX_NETWORKS];
66 int ipx_general_PacketReady(ipx_socket_t *s) {
72 tv.tv_sec = tv.tv_usec = 0;
73 if (select(FD_SETSIZE, &set, NULL, NULL, &tv) > 0)
79 struct ipx_driver *driver = &ipx_win;
81 ubyte * ipx_get_my_server_address()
83 return (ubyte *)&ipx_network;
86 ubyte * ipx_get_my_local_address()
88 return (ubyte *)(ipx_MyAddress + 4);
91 void arch_ipx_set_driver(int ipx_driver)
94 case IPX_DRIVER_IPX: driver = &ipx_win; break;
95 case IPX_DRIVER_UDP: driver = &ipx_udp; break;
96 case IPX_DRIVER_MCAST4: driver = &ipx_mcast4; break;
101 int ipx_init(int socket_number)
105 WORD wVersionRequested;
108 wVersionRequested = MAKEWORD(2, 0);
109 if (WSAStartup( wVersionRequested, &wsaData))
111 return IPX_SOCKET_ALREADY_OPEN;
115 if ( LOBYTE( wsaData.wVersion ) != 2 ||
116 HIBYTE( wsaData.wVersion ) != 0 ) {
117 /* We couldn't find a usable WinSock DLL. */
119 return IPX_SOCKET_TABLE_FULL;
123 if ((i = FindArg("-ipxnetwork")) && Args[i + 1]) {
124 unsigned long n = strtol(Args[i + 1], NULL, 16);
125 ipx_MyAddress[0] = (unsigned char)n >> 24; ipx_MyAddress[1] = (unsigned char)(n >> 16) & 255;
126 ipx_MyAddress[2] = (unsigned char)(n >> 8) & 255; ipx_MyAddress[3] = (unsigned char)n & 255;
127 printf("IPX: Using network %08x\n", (int) n);
129 if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
130 return IPX_NOT_INSTALLED;
132 driver->GetMyAddress();
133 memcpy(&ipx_network, ipx_MyAddress, 4);
134 Ipx_num_networks = 0;
135 memcpy( &Ipx_networks[Ipx_num_networks++], &ipx_network, 4 );
138 printf("ipx succesfully installed\n");
146 driver->CloseSocket(&ipx_socket_data);
151 int ipx_get_packet_data( ubyte * data )
153 struct ipx_recv_data rd;
154 char buf[MAX_IPX_DATA];
155 //killed 6-15-99 to get rid of compile warnings - OE
161 //edited 04/12/99 Matt Mueller - duh, we don't want to throw all that data away!
162 //--killed-- Like the original, only take latest packet, throw away rest
163 //do _NOT_ throw them away!
164 while (driver->PacketReady(&ipx_socket_data)) {
166 driver->ReceivePacket(&ipx_socket_data, buf,
167 sizeof(buf), &rd)) > 4) {
168 if (!memcmp(rd.src_network, ipx_MyAddress, 10))
169 continue; /* don't get own pkts */
170 //--killed-- pkt_num = *(uint *)buf;
171 //--killed-- if (pkt_num >= best_id) {
172 memcpy(data, buf + 4, size - 4);
174 //--killed-- best_id = pkt_num;
175 //--killed-- best_size = size - 4;
183 void ipx_send_packet_data( ubyte * data, int datasize, ubyte *network, ubyte *address, ubyte *immediate_address )
185 u_char buf[MAX_IPX_DATA];
186 IPXPacket_t ipx_header;
188 memcpy(ipx_header.Destination.Network, network, 4);
189 memcpy(ipx_header.Destination.Node, immediate_address, 6);
190 *(u_short *)ipx_header.Destination.Socket = htons(ipx_socket_data.socket);
191 ipx_header.PacketType = 4; /* Packet Exchange */
192 *(uint *)buf = ipx_packetnum++;
193 memcpy(buf + 4, data, datasize);
194 driver->SendPacket(&ipx_socket_data, &ipx_header, buf, datasize + 4);
197 void ipx_get_local_target( ubyte * server, ubyte * node, ubyte * local_target )
199 // let's hope Linux knows how to route it
200 memcpy( local_target, node, 6 );
203 void ipx_send_broadcast_packet_data( ubyte * data, int datasize )
206 ubyte broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
207 ubyte local_address[6];
209 // Set to all networks besides mine
210 for (i=0; i<Ipx_num_networks; i++ ) {
211 if ( memcmp( &Ipx_networks[i], &ipx_network, 4 ) ) {
212 ipx_get_local_target( (ubyte *)&Ipx_networks[i], broadcast, local_address );
213 ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, local_address );
215 ipx_send_packet_data( data, datasize, (ubyte *)&Ipx_networks[i], broadcast, broadcast );
219 //OLDipx_send_packet_data( data, datasize, (ubyte *)&ipx_network, broadcast, broadcast );
221 // Send directly to all users not on my network or in the network list.
222 for (i=0; i<Ipx_num_users; i++ ) {
223 if ( memcmp( Ipx_users[i].network, &ipx_network, 4 ) ) {
224 for (j=0; j<Ipx_num_networks; j++ ) {
225 if (!memcmp( Ipx_users[i].network, &Ipx_networks[j], 4 ))
228 ipx_send_packet_data( data, datasize, Ipx_users[i].network, Ipx_users[i].node, Ipx_users[i].address );
235 // Sends a non-localized packet... needs 4 byte server, 6 byte address
236 void ipx_send_internetwork_packet_data( ubyte * data, int datasize, ubyte * server, ubyte *address )
238 ubyte local_address[6];
240 if ( (*(uint *)server) != 0 ) {
241 ipx_get_local_target( server, address, local_address );
242 ipx_send_packet_data( data, datasize, server, address, local_address );
244 // Old method, no server info.
245 ipx_send_packet_data( data, datasize, server, address, address );
249 int ipx_change_default_socket( ushort socket_number )
251 if ( !ipx_installed ) return -3;
253 driver->CloseSocket(&ipx_socket_data);
254 if (driver->OpenSocket(&ipx_socket_data, socket_number)) {
260 void ipx_read_user_file(char * filename)
264 char temp_line[132], *p1;
267 if (!filename) return;
271 fp = fopen( filename, "rt" );
274 printf( "Broadcast Users:\n" );
276 while (fgets(temp_line, 132, fp)) {
278 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
279 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
280 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
281 if (strlen(temp_line) >= 21 && temp_line[8] == '/') {
282 for (n = 0; n < 4; n++) {
283 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
289 for (n = 0; n < 6; n++) {
290 if (sscanf(temp_line + 9 + n * 2, "%2x", &x) != 1)
299 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] );
300 if ( n != 10 ) continue;
302 if ( Ipx_num_users < MAX_USERS ) {
303 ubyte * ipx_real_buffer = (ubyte *)&tmp;
304 ipx_get_local_target( tmp.network, tmp.node, tmp.address );
305 Ipx_users[Ipx_num_users++] = tmp;
306 printf( "%02X%02X%02X%02X/", ipx_real_buffer[0],ipx_real_buffer[1],ipx_real_buffer[2],ipx_real_buffer[3] );
307 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] );
309 printf( "Too many addresses in %s! (Limit of %d)\n", filename, MAX_USERS );
318 void ipx_read_network_file(char * filename)
322 char temp_line[132], *p1;
325 if (!filename) return;
327 fp = fopen( filename, "rt" );
330 printf( "Using Networks:\n" );
331 for (i=0; i<Ipx_num_networks; i++ ) {
332 ubyte * n1 = (ubyte *)&Ipx_networks[i];
333 printf("* %02x%02x%02x%02x\n", n1[0], n1[1], n1[2], n1[3] );
336 while (fgets(temp_line, 132, fp)) {
338 p1 = strchr(temp_line,'\n'); if (p1) *p1 = '\0';
339 p1 = strchr(temp_line,';'); if (p1) *p1 = '\0';
340 #if 1 // adb: replaced sscanf(..., "%2x...", (char *)...) with better, but longer code
341 if (strlen(temp_line) >= 8) {
342 for (n = 0; n < 4; n++) {
343 if (sscanf(temp_line + n * 2, "%2x", &x) != 1)
352 n = sscanf( temp_line, "%2x%2x%2x%2x", &tmp.network[0], &tmp.network[1], &tmp.network[2], &tmp.network[3] );
353 if ( n != 4 ) continue;
355 if ( Ipx_num_networks < MAX_NETWORKS ) {
357 for (j=0; j<Ipx_num_networks; j++ )
358 if ( !memcmp( &Ipx_networks[j], tmp.network, 4 ) )
360 if ( j >= Ipx_num_networks ) {
361 memcpy( &Ipx_networks[Ipx_num_networks++], tmp.network, 4 );
362 printf(" %02x%02x%02x%02x\n", tmp.network[0], tmp.network[1], tmp.network[2], tmp.network[3] );
365 printf( "Too many networks in %s! (Limit of %d)\n", filename, MAX_NETWORKS );
373 // Initalizes the protocol-specific member of the netgame packet.
374 void ipx_init_netgame_aux_data(ubyte buf[])
376 if(driver->InitNetgameAuxData)
377 driver->InitNetgameAuxData(&ipx_socket_data, buf);
380 // Handles the protocol-specific member of the netgame packet.
381 int ipx_handle_netgame_aux_data(const ubyte buf[])
383 if(driver->HandleNetgameAuxData)
384 return driver->HandleNetgameAuxData(&ipx_socket_data, buf);
388 // Notifies the protocol that we're done with a particular game
389 void ipx_handle_leave_game()
391 if(driver->HandleLeaveGame)
392 driver->HandleLeaveGame(&ipx_socket_data);
395 // Send a packet to every member of the game.
396 int ipx_send_game_packet(ubyte *data, int datasize)
398 if(driver->SendGamePacket) {
399 u_char buf[MAX_IPX_DATA];
401 *(uint *)buf = ipx_packetnum++;
402 memcpy(buf + 4, data, datasize);
403 *(uint *)data = ipx_packetnum++;
404 return driver->SendGamePacket(&ipx_socket_data, buf, datasize + 4);
406 // Loop through all the players unicasting the packet.
409 //printf("Sending game packet: N_players = %i\n", N_players);
411 for(i=0; i<N_players; i++) {
412 if(Players[i].connected && (i != Player_num))
413 ipx_send_packet_data(data, datasize, NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node,Players[i].net_address);