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