]> icculus.org git repositories - btb/d2x.git/blob - arch/win32/ipx_udp.c
92bed29f8f783dfcf7676b0ac7b7848300e04f3a
[btb/d2x.git] / arch / win32 / ipx_udp.c
1 /* $Id: ipx_udp.c,v 1.3 2003-10-12 09:17:47 btb Exp $ */
2 /*
3  *
4  * IPX driver for native Linux TCP/IP networking (UDP implementation)
5  *   Version 0.99.2
6  * Contact Jan [Lace] Kratochvil <short@ucw.cz> for assistance
7  * (no "It somehow doesn't work! What should I do?" complaints, please)
8  * Special thanks to Vojtech Pavlik <vojtech@ucw.cz> for testing.
9  *
10  * Also you may see KIX - KIX kix out KaliNix (in Linux-Linux only):
11  *   http://atrey.karlin.mff.cuni.cz/~short/sw/kix.c.gz
12  *
13  * Primarily based on ipx_kali.c - "IPX driver for KaliNix interface"
14  *   which is probably mostly by Jay Cotton <jay@kali.net>.
15  * Parts shamelessly stolen from my KIX v0.99.2 and GGN v0.100
16  *
17  * Changes:
18  * --------
19  * 0.99.1 - now the default broadcast list also contains all point-to-point
20  *          links with their destination peer addresses
21  * 0.99.2 - commented a bit :-) 
22  *        - now adds to broadcast list each host it gets some packet from
23  *          which is already not covered by local physical ethernet broadcast
24  *        - implemented short-signature packet format
25  *        - compatibility mode for old D1X releases due to the previous bullet
26  *
27  * Configuration:
28  * --------------
29  * No network server software is needed, neither KIX nor KaliNIX.
30  *
31  *    NOTE: with the change to allow the user to choose the network
32  *    driver from the game menu, the following is not anymore precise:
33  *    the command line argument "-udp" is only necessary to supply udp
34  *    options!
35  *
36  * Add command line argument "-udp". In default operation D1X will send
37  * broadcasts too all the local interfaces found. But you may also use
38  * additional parameter specified after "-udp" to modify the default
39  * broadcasting style and UDP port numbers binding:
40  *
41  * ./d1x -udp [@SHIFT]=HOST_LIST   Broadcast ONLY to HOST_LIST
42  * ./d1x -udp [@SHIFT]+HOST_LIST   Broadcast both to local ifaces & to HOST_LIST
43  *
44  * HOST_LIST is a comma (',') separated list of HOSTs:
45  * HOST is an IPv4 address (so-called quad like 192.168.1.2) or regular hostname
46  * HOST can also be in form 'address:SHIFT'
47  * SHIFT sets the UDP port base offset (e.g. +2), can be used to run multiple
48  *       clients on one host simultaneously. This SHIFT has nothing to do
49  *       with the dynamic-sockets (PgUP/PgDOWN) option in Descent, it's another
50  *       higher-level differentiation option.
51  *
52  * Examples:
53  * ---------
54  * ./d1x -udp
55  *  - Run D1X to participate in normal local network (Linux only, of course)
56  *
57  * ./d1x -udp @1=localhost:2 & ./d1x -udp @2=localhost:1
58  *  - Run two clients simultaneously fighting each other (only each other)
59  *
60  * ./d1x -udp =192.168.4.255
61  *  - Run distant Descent which will participate with remote network
62  *    192.168.4.0 with netmask 255.255.255.0 (broadcast has 192.168.4.255)
63  *  - You'll have to also setup hosts in that network accordingly:
64  * ./d1x -udp +UPPER_DISTANT_MACHINE_NAME
65  *
66  * Have fun!
67  *
68  */
69
70 #ifdef HAVE_CONFIG_H
71 #include <conf.h>
72 #endif
73
74 #include <stdio.h>
75 #include <string.h>
76 #include <unistd.h>
77 #include <stdarg.h>
78 #include <stdlib.h>
79 #include <ctype.h>
80 #include <malloc.h>
81
82 #include <winsock2.h>
83
84 #include "ipx_drv.h"
85 #include "args.h"
86
87 extern unsigned char ipx_MyAddress[10];
88
89 // #define UDPDEBUG
90
91 #define UDP_BASEPORT 28342
92 #define PORTSHIFT_TOLERANCE 0x100
93 #define MAX_PACKETSIZE 8192
94
95 /* Packet format: first is the signature { 0xD1,'X' } which can be also
96  * { 'D','1','X','u','d','p'} for old-fashioned packets.
97  * Then follows virtual socket number (as changed during PgDOWN/PgUP)
98  * in network-byte-order as 2 bytes (u_short). After such 4/8 byte header
99  * follows raw data as communicated with D1X network core functions.
100  */
101
102 // Length HAS TO BE 6!
103 #define D1Xudp "D1Xudp"
104 // Length HAS TO BE 2!
105 #define D1Xid "\xD1X"
106
107 static int open_sockets = 0;
108 static int dynamic_socket = 0x401;
109 static const int val_one=1;
110
111 /* OUR port. Can be changed by "@X[+=]..." argument (X is the shift value)
112  */
113
114 static int baseport=UDP_BASEPORT;
115
116 /* Have we some old D1X in network and have we to maintain compatibility?
117  * FIXME: Following scenario is braindead:
118  *                                           A  (NEW) , B  (OLD) , C  (NEW)
119  *   host A) We start new D1X.               A-newcomm, B-none   , C-none
120  *   host B) We start OLD D1X.               A-newcomm, B-oldcomm, C-none
121  *   Now host A hears host B and switches:   A-oldcomm, B-oldcomm, C-none
122  *   host C) We start new D1X.               A-oldcomm, B-oldcomm, C-newcomm
123  *   Now host C hears host A/B and switches: A-oldcomm, B-oldcomm, C-oldcomm
124  *   Now host B finishes:                    A-oldcomm, B-none   , C-oldcomm
125  *
126  * But right now we have hosts A and C, both new code equipped but
127  * communicating wastefully by the OLD protocol! Bummer.
128  */
129
130 static char compatibility=0;
131
132 static int have_empty_address() {
133         int i;
134
135         for (i = 0; i < 10 && !ipx_MyAddress[i]; i++) ;
136         return i == 10;
137 }
138
139 #define MSGHDR "IPX_udp: "
140
141 static void msg(const char *fmt,...)
142 {
143         va_list ap;
144
145         fputs(MSGHDR,stdout);
146         va_start(ap,fmt);
147         vprintf(fmt,ap);
148         va_end(ap);
149         putchar('\n');
150 }
151
152 static void chk(void *p)
153 {
154         if (p) return;
155         msg("FATAL: Virtual memory exhausted!");
156         exit(EXIT_FAILURE);
157 }
158
159 #define FAIL(m...) do { msg(#m); return -1; } while (0)
160
161 /* Find as much as MAX_BRDINTERFACES during local iface autoconfiguration.
162  * Note that more interfaces can be added during manual configuration
163  * or host-received-packet autoconfiguration
164  */
165
166 #define MAX_BRDINTERFACES 16
167
168 /* We require the interface to be UP and RUNNING to accept it.
169  */
170
171 #define IF_REQFLAGS (IFF_UP|IFF_RUNNING)
172
173 /* We reject any interfaces declared as LOOPBACK type.
174  */
175 #define IF_NOTFLAGS (IFF_LOOPBACK)
176
177 static struct sockaddr_in *broads,broadmasks[MAX_BRDINTERFACES];
178 static int broadnum,masksnum,broadsize;
179
180 /* We'll check whether the "broads" array of destination addresses is now
181  * full and so needs expanding.
182  */
183
184 static void chkbroadsize(void)
185 {
186         if (broadnum<broadsize) return;
187         broadsize=broadsize?broadsize*2:8;
188         chk(broads=realloc(broads,sizeof(*broads)*broadsize));
189 }
190
191 /* This function is called during init and has to grab all system interfaces
192  * and collect their broadcast-destination addresses (and their netmasks).
193  * Typically it founds only one ethernet card and so returns address in
194  * the style "192.168.1.255" with netmask "255.255.255.0".
195  * Broadcast addresses are filled into "broads", netmasks to "broadmasks".
196  */
197
198 /* Stolen from my GGN */
199 static int addiflist(void)
200 {
201 #if 0
202         unsigned cnt=MAX_BRDINTERFACES,i,j;
203         WSADATA info;
204         INTERFACE_INFO *ifo;
205         SOCKET sock;
206         struct sockaddr_in *sinp,*sinmp;
207
208         free(broads);
209         if ((sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))<0)
210                 FAIL("Creating socket() failure during broadcast detection: %m");
211
212 #ifdef SIOCGIFCOUNT
213         if (ioctl(sock,SIOCGIFCOUNT,&cnt))
214                 { /* msg("Getting iterface count error: %m"); */ }
215         else
216                 cnt=cnt*2+2;
217 #endif
218
219         memset(&ifo[0], 0, sizeof(ifo));
220         chk(ifo = alloca(cnt * sizeof(INTERFACE_INFO)));
221
222         if (wsaioctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, &ifo[0], cnt * sizeof(INTERFACE_INFO), &br, NULL, NULL)) != 0) {
223                 closesocket(sock);
224                 FAIL("ioctl(SIOCGIFCONF) failure during broadcast detection: %m");
225                 }
226 #if 0
227         cnt=ifconf.ifc_len/sizeof(struct ifreq);
228 #endif
229         chk(broads=malloc(cnt*sizeof(*broads)));
230         broadsize=cnt;
231         for (i=j=0;i<cnt;i++) {
232                 if (ioctl(sock,SIOCGIFFLAGS,ifconf.ifc_req+i)) {
233                         closesocket(sock);
234                         FAIL("ioctl(udp,\"%s\",SIOCGIFFLAGS) error: %m",ifconf.ifc_req[i].ifr_name);
235                         }
236                 if (((ifconf.ifc_req[i].ifr_flags&IF_REQFLAGS)!=IF_REQFLAGS)||
237                                  (ifconf.ifc_req[i].ifr_flags&IF_NOTFLAGS))
238                         continue;
239                 if (ioctl(sock,(ifconf.ifc_req[i].ifr_flags&IFF_BROADCAST?SIOCGIFBRDADDR:SIOCGIFDSTADDR),ifconf.ifc_req+i)) {
240                         closesocket(sock);
241                         FAIL("ioctl(udp,\"%s\",SIOCGIF{DST/BRD}ADDR) error: %m",ifconf.ifc_req[i].ifr_name);
242                         }
243
244                 sinp = (struct sockaddr_in *)&ifconf.ifc_req[i].ifr_broadaddr;
245 #if 0 // old, not portable code
246                 sinmp = (struct sockaddr_in *)&ifconf.ifc_req[i].ifr_netmask;
247 #else // portable code
248                 if (ioctl(sock, SIOCGIFNETMASK, ifconf.ifc_req+i)) {
249                         closesocket(sock);
250                         FAIL("ioctl(udp,\"%s\",SIOCGIFNETMASK) error: %m", ifconf.ifc_req[i].ifr_name);
251                 }
252                 sinmp = (struct sockaddr_in *)&ifconf.ifc_req[i].ifr_addr;
253 #endif
254                 if (sinp->sin_family!=AF_INET || sinmp->sin_family!=AF_INET) continue;
255                 broads[j]=*sinp;
256                 broads[j].sin_port=UDP_BASEPORT; //FIXME: No possibility to override from cmdline
257                 broadmasks[j]=*sinmp;
258                 j++;
259                 }
260         broadnum=j;
261         masksnum=j;
262 #endif
263         return(0);
264 }
265
266 #define addreq(a,b) ((a).sin_port==(b).sin_port&&(a).sin_addr.s_addr==(b).sin_addr.s_addr)
267
268 /* Previous function addiflist() can (and probably will) report multiple
269  * same addresses. On some Linux boxes is present both device "eth0" and
270  * "dummy0" with the same IP addreesses - we'll filter it here.
271  */
272
273 static void unifyiflist(void)
274 {
275 int d=0,s,i;
276
277         for (s=0;s<broadnum;s++) {
278                 for (i=0;i<s;i++)
279                         if (addreq(broads[s],broads[i])) break;
280                 if (i>=s) broads[d++]=broads[s];
281                 }
282         broadnum=d;
283 }
284
285 static unsigned char qhbuf[6];
286
287 /* Parse PORTSHIFT numeric parameter
288  */
289
290 static void portshift(const char *cs)
291 {
292 long port;
293 unsigned short ports=0;
294
295         port=atol(cs);
296         if (port<-PORTSHIFT_TOLERANCE || port>+PORTSHIFT_TOLERANCE)
297                 msg("Invalid portshift in \"%s\", tolerance is +/-%d",cs,PORTSHIFT_TOLERANCE);
298         else ports=htons(port);
299         memcpy(qhbuf+4,&ports,2);
300 }
301
302 /* Do hostname resolve on name "buf" and return the address in buffer "qhbuf".
303  */
304 static unsigned char *queryhost(char *buf)
305 {
306 struct hostent *he;
307 char *s;
308 char c=0;
309
310         if ((s=strrchr(buf,':'))) {
311           c=*s;
312           *s='\0';
313           portshift(s+1);
314           }
315         else memset(qhbuf+4,0,2);
316         he=gethostbyname((char *)buf);
317         if (s) *s=c;
318         if (!he) {
319                 msg("Error resolving my hostname \"%s\"",buf);
320                 return(NULL);
321                 }
322         if (he->h_addrtype!=AF_INET || he->h_length!=4) {
323           msg("Error parsing resolved my hostname \"%s\"",buf);
324                 return(NULL);
325                 }
326         if (!*he->h_addr_list) {
327           msg("My resolved hostname \"%s\" address list empty",buf);
328                 return(NULL);
329                 }
330         memcpy(qhbuf,(*he->h_addr_list),4);
331         return(qhbuf);
332 }
333
334 /* Dump raw form of IP address/port by fancy output to user
335  */
336 static void dumpraddr(unsigned char *a)
337 {
338 short port;
339         printf("[%u.%u.%u.%u]",a[0],a[1],a[2],a[3]);
340         port=(signed short)ntohs(*(unsigned short *)(a+4));
341         if (port) printf(":%+d",port);
342 }
343
344 /* Like dumpraddr() but for structure "sockaddr_in"
345  */
346
347 static void dumpaddr(struct sockaddr_in *sin)
348 {
349 unsigned short ports;
350
351         memcpy(qhbuf+0,&sin->sin_addr,4);
352         ports=htons(((short)ntohs(sin->sin_port))-UDP_BASEPORT);
353         memcpy(qhbuf+4,&ports,2);
354         dumpraddr(qhbuf);
355 }
356
357 /* Startup... Uninteresting parsing...
358  */
359
360 int ipx_udp_GetMyAddress(void) {
361
362 char buf[256];
363 int i;
364 char *s,*s2,*ns;
365
366         if (!have_empty_address())
367                 return 0;
368
369         if (!((i=FindArg("-udp")) && (s=Args[i+1]) && (*s=='=' || *s=='+' || *s=='@'))) s=NULL;
370
371         if (gethostname(buf,sizeof(buf))) FAIL("Error getting my hostname");
372         if (!(queryhost(buf))) FAIL("Querying my own hostname \"%s\"",buf);
373
374         if (s) while (*s=='@') {
375                 portshift(++s);
376                 while (isdigit(*s)) s++;
377                 }
378
379         memset(ipx_MyAddress+0,0,4);
380         memcpy(ipx_MyAddress+4,qhbuf,6);
381         baseport+=(short)ntohs(*(unsigned short *)(qhbuf+4));
382
383         if (!s || (s && !*s)) addiflist();
384         else {
385                 if (*s=='+') addiflist();
386                 s++;
387                 for (;;) {
388 struct sockaddr_in *sin;
389                         while (isspace(*s)) s++;
390                         if (!*s) break;
391                         for (s2=s;*s2 && *s2!=',';s2++);
392                         chk(ns=malloc(s2-s+1));
393                         memcpy(ns,s,s2-s);
394                         ns[s2-s]='\0';
395                         if (!queryhost(ns)) msg("Ignored broadcast-destination \"%s\" as being invalid",ns);
396                         free(ns);
397                         chkbroadsize();
398                         sin=broads+(broadnum++);
399                         sin->sin_family=AF_INET;
400                         memcpy(&sin->sin_addr,qhbuf+0,4);
401                         sin->sin_port=htons(((short)ntohs(*(unsigned short *)(qhbuf+4)))+UDP_BASEPORT);
402                         s=s2+(*s2==',');
403                         }
404                 }
405
406         unifyiflist();
407
408         printf(MSGHDR "Using TCP/IP address ");
409         dumpraddr(ipx_MyAddress+4);
410         putchar('\n');
411         if (broadnum) {
412                 printf(MSGHDR "Using %u broadcast-dest%s:",broadnum,(broadnum==1?"":"s"));
413                 for (i=0;i<broadnum;i++) {
414                         putchar(' ');
415                         dumpaddr(broads+i);
416                         }
417                 putchar('\n');
418                 }
419
420         return 0;
421 }
422
423 /* We should probably avoid such insanity here as during PgUP/PgDOWN
424  * (virtual port number change) we wastefully destroy/create the same
425  * socket here (binded always to "baseport"). FIXME.
426  * "open_sockets" can be only 0 or 1 anyway.
427  */
428
429 static int ipx_udp_OpenSocket(ipx_socket_t *sk, int port) {
430         struct sockaddr_in sin;
431
432         if (!open_sockets)
433                 if (have_empty_address())
434                 if (ipx_udp_GetMyAddress() < 0) FAIL("Error getting my address");
435
436         msg("OpenSocket on D1X socket port %d",port);
437
438         if (!port)
439                 port = dynamic_socket++;
440
441         if ((sk->fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) < 0) {
442                 sk->fd = -1;
443                 FAIL("socket() creation failed on port %d: %m",port);
444                 }
445         if (setsockopt(sk->fd, SOL_SOCKET, SO_BROADCAST, (char *)&val_one, sizeof(val_one))) {
446                 if (closesocket(sk->fd)) msg("closesocket() failed during error recovery: %m");
447                 sk->fd=-1;
448                 FAIL("setsockopt(SO_BROADCAST) failed: %m");
449                 }
450         sin.sin_family=AF_INET;
451         sin.sin_addr.s_addr=htonl(INADDR_ANY);
452         sin.sin_port=htons(baseport);
453         if (bind(sk->fd,(struct sockaddr *)&sin,sizeof(sin))) {
454                 if (closesocket(sk->fd)) msg("closesocket() failed during error recovery: %m");
455                 sk->fd=-1;
456                 FAIL("bind() to UDP port %d failed: %m",baseport);
457                 }
458
459         open_sockets++;
460         sk->socket = port;
461         return 0;
462 }
463
464 /* The same comment as in previous "ipx_udp_OpenSocket"...
465  */
466
467 static void ipx_udp_CloseSocket(ipx_socket_t *mysock) {
468         if (!open_sockets) {
469                 msg("close w/o open");
470                 return;
471         }
472         msg("CloseSocket on D1X socket port %d",mysock->socket);
473         if (closesocket(mysock->fd))
474                 msg("closesocket() failed on CloseSocket D1X socket port %d: %m",mysock->socket);
475         mysock->fd=-1;
476         if (--open_sockets) {
477                 msg("(closesocket) %d sockets left", open_sockets);
478                 return;
479         }
480 }
481
482 /* Here we'll send the packet to our host. If it is unicast packet, send
483  * it to IP address/port as retrieved from IPX address. Otherwise (broadcast)
484  * we'll repeat the same data to each host in our broadcasting list.
485  */
486
487 static int ipx_udp_SendPacket(ipx_socket_t *mysock, IPXPacket_t *IPXHeader,
488  u_char *data, int dataLen) {
489         struct sockaddr_in toaddr,*dest;
490         int i=dataLen;
491         int bcast;
492         char *buf;
493
494 #ifdef UDPDEBUG
495         msg("SendPacket enter, dataLen=%d",dataLen);
496 #endif
497         if (dataLen<0 || dataLen>MAX_PACKETSIZE) return -1;
498         chk(buf=alloca(8+dataLen));
499         if (compatibility) memcpy(buf+0,D1Xudp,6),buf+=6;
500         else               memcpy(buf+0,D1Xid ,2),buf+=2;
501         memcpy(buf+0,IPXHeader->Destination.Socket,2);
502         memcpy(buf+2,data,dataLen);
503  
504         toaddr.sin_family=AF_INET;
505         memcpy(&toaddr.sin_addr,IPXHeader->Destination.Node+0,4);
506         toaddr.sin_port=htons(((short)ntohs(*(unsigned short *)(IPXHeader->Destination.Node+4)))+UDP_BASEPORT);
507
508         for (bcast=(toaddr.sin_addr.s_addr==htonl(INADDR_BROADCAST)?0:-1);bcast<broadnum;bcast++) {
509                 if (bcast>=0) dest=broads+bcast;
510                 else dest=&toaddr;
511         
512 #ifdef UDPDEBUG
513                 printf(MSGHDR "sendto((%d),Node=[4] %02X %02X,Socket=%02X %02X,s_port=%u,",
514                         dataLen,
515                         IPXHeader->Destination.Node  [4],IPXHeader->Destination.Node  [5],
516                         IPXHeader->Destination.Socket[0],IPXHeader->Destination.Socket[1],
517                         ntohs(dest->sin_port));
518                 dumpaddr(dest);
519                 puts(").");
520 #endif
521                 i=sendto(mysock->fd,buf-(compatibility?6:2),(compatibility?8:4)+dataLen,
522                         0,(struct sockaddr *)dest,sizeof(*dest));
523                 if (bcast==-1) return (i<8?-1:i-8);
524                 }
525         return(dataLen);
526 }
527
528 /* Here we will receive new packet to the given buffer. Both formats of packets
529  * are supported, we fallback to old format when first obsolete packet is seen.
530  * If the (valid) packet is received from unknown host, we will add it to our
531  * broadcasting list. FIXME: For now such autoconfigured hosts are NEVER removed.
532  */
533
534 static int ipx_udp_ReceivePacket(ipx_socket_t *s, char *outbuf, int outbufsize, 
535  struct ipx_recv_data *rd) {
536         int size;
537         struct sockaddr_in fromaddr;
538         int fromaddrsize=sizeof(fromaddr);
539         unsigned short ports;
540         size_t offs;
541         int i;
542
543         if ((size=recvfrom(s->fd,outbuf,outbufsize,0,(struct sockaddr *)&fromaddr,&fromaddrsize))<0)
544                 return -1;
545 #ifdef UDPDEBUG
546         printf(MSGHDR "recvfrom((%d-8=%d),",size,size-8);
547         dumpaddr(&fromaddr);
548         puts(").");
549 #endif
550         if (fromaddr.sin_family!=AF_INET) return -1;
551         if (size<4) return -1;
552         if (memcmp(outbuf+0,D1Xid,2)) {
553                 if (size<8 || memcmp(outbuf+0,D1Xudp,6)) return -1;
554                 if (!compatibility) {
555                         compatibility=1;
556                         fputs(MSGHDR "Received obsolete packet from ",stdout);
557                         dumpaddr(&fromaddr);
558                         puts(", upgrade that machine.\n" MSGHDR "Turning on compatibility mode...");
559                         }
560                 offs=6;
561                 }
562         else offs=2;
563
564         /* Lace: (dst_socket & src_socket) should be network-byte-order by comment in include/ipx_drv.h */
565         /*       This behaviour presented here is broken. It is not used anywhere, so why bother? */
566         rd->src_socket = ntohs(*(unsigned short *)(outbuf+offs));
567         if (rd->src_socket != s->socket) {
568 #ifdef UDPDEBUG
569                 msg(" - pkt was dropped (dst=%d,my=%d)",rd->src_socket,s->socket);
570 #endif
571                 return -1;
572                 }
573         rd->dst_socket = s->socket;
574
575         // check if we already have sender of this packet in broadcast list
576         for (i=0;i<broadnum;i++) {
577                 if (i>=masksnum) {
578                         if (addreq(fromaddr,broads[i])) break; 
579                         }
580                 else {
581                         if (fromaddr.sin_port==broads[i].sin_port
582                         &&( fromaddr.sin_addr.s_addr & broadmasks[i].sin_addr.s_addr)
583                         ==(broads[i].sin_addr.s_addr & broadmasks[i].sin_addr.s_addr)) break;
584                         }
585                 }
586         if (i>=broadnum) { // we don't have sender of this packet in our broadcast list
587                 chkbroadsize();
588                 broads[broadnum++]=fromaddr;
589                 fputs(MSGHDR "Adding host ",stdout);
590                 dumpaddr(&fromaddr);
591                 puts(" to broadcasting address list");
592                 }
593
594         memmove(outbuf,outbuf+offs+2,size-(offs+2));
595         size-=offs+2;
596
597         memcpy(rd->src_node+0,&fromaddr.sin_addr,4);
598         ports=htons(ntohs(fromaddr.sin_port)-UDP_BASEPORT);
599         memcpy(rd->src_node+4,&ports,2);
600         memset(rd->src_network, 0, 4);
601         rd->pkt_type = 0;
602 #ifdef UDPDEBUG
603         printf(MSGHDR "ReceivePacket: size=%d,from=",size);
604         dumpraddr(rd->src_node);
605         putchar('\n');
606 #endif
607
608         return size;
609 }
610
611 struct ipx_driver ipx_udp = {
612         ipx_udp_GetMyAddress,
613         ipx_udp_OpenSocket,
614         ipx_udp_CloseSocket,
615         ipx_udp_SendPacket,
616         ipx_udp_ReceivePacket,
617         ipx_general_PacketReady,
618         NULL,   // InitNetgameAuxData
619         NULL,   // HandleNetgameAuxData
620         NULL,   // HandleLeaveGame
621         NULL    // SendGamePacke
622 };