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