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