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