network.c 35.3 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
/*
2
 * Copyright (c) 2000-2018 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
22 23
 */

24 25 26 27 28 29 30 31
/*
 * Network routines.
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
32 33
#include <net/if.h>
#include <ifaddrs.h>
34 35 36 37 38 39 40 41
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include "decls.h"
Mike Hibler's avatar
Mike Hibler committed
42
#include "utils.h"
43 44 45
#ifdef NO_SOCKET_TIMO
#include <sys/select.h>
#endif
Mike Hibler's avatar
Mike Hibler committed
46

47 48 49 50 51 52 53
#ifdef STATS
unsigned long nonetbufs;
#define DOSTAT(x)	(x)
#else
#define DOSTAT(x)
#endif

54
/* Max number of times to attempt bind to port before failing. */
55
#define MAXBINDATTEMPTS		1
56

57 58
#define IS_MCAST_ADDR(sa)	((ntohl((sa).s_addr) >> 28) == 14)

59 60 61
/* Max number of hops multicast hops. */
#define MCAST_TTL		5

62
static int		sock = -1;
63 64 65
#ifdef USE_REUSEADDR_COMPAT
static int		selfsock = -1;
#endif
66
struct in_addr		myipaddr;
67
static int		nobufdelay = -1;
68
int			broadcast = 0;
Mike Hibler's avatar
Mike Hibler committed
69 70 71
static int		isclient = 0;
static int		sndportnum;	/* kept in network order */

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/*
 * Convert a string to an IPv4 address.  We first try to interpret it as
 * an IPv4 address.  If that fails, we attempt to resolve it as a host name.
 * Return non-zero on success.
 */
int
GetIP(char *str, struct in_addr *in)
{
	struct hostent *he;

	if (inet_aton(str, in) == 0) {
		if ((he = gethostbyname(str)) == NULL)
			return 0;
		memcpy(in, he->h_addr, sizeof(*in));
	}

	return 1;
}

91 92 93 94 95 96
/*
 * Return the maximum size of a socket buffer.
 * Computes it dynamically on the first call.
 *
 * XXX assumes send/recv max sizes are the same.
 */
Mike Hibler's avatar
Mike Hibler committed
97 98 99 100 101 102 103 104 105 106
int
GetSockbufSize(void)
{

	static int sbsize = 0;

	if (sbsize == 0) {
		int sock;

		if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
107
			FrisPfatal("Could not allocate a socket");
Mike Hibler's avatar
Mike Hibler committed
108

109
		for (sbsize = sockbufsize; sbsize > 0; sbsize -= (16*1024)) {
Mike Hibler's avatar
Mike Hibler committed
110 111 112 113 114 115 116 117 118 119
			int i = sbsize;
			if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
				       &i, sizeof(i)) >= 0)
				break;
		}
		if (sbsize < 0) {
			int i = 0;
			unsigned int ilen = sizeof(i);
			if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF,
				       &i, &ilen) < 0)
120
				i = sockbufsize;
Mike Hibler's avatar
Mike Hibler committed
121 122
			sbsize = i;
		}
123 124 125 126 127
		else {
			int i = 0;
			unsigned int ilen = sizeof(i);
			if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF,
				       &i, &ilen) < 0)
128
				FrisPfatal("Could not read sockbuf size");
129 130 131 132 133 134
#ifdef linux
			/* In Linux, getsockopt returns 2 * actual value */
			if (i == 2 * sbsize)
				i = sbsize;
#endif
			if (i != sbsize) {
135 136
				FrisWarning("Actual socket buffer size is %d"
					    " (instead of %d)", i, sbsize);
137 138 139
				sbsize = i;
			}
		}
Mike Hibler's avatar
Mike Hibler committed
140
		close(sock);
141
		FrisLog("Maximum socket buffer size of %d bytes", sbsize);
Mike Hibler's avatar
Mike Hibler committed
142 143 144
	}
	return sbsize;
}
145

146 147 148 149 150 151 152 153 154 155 156 157 158
/*
 * Find the subnet broadcast address associated with the given interface
 * address. We use this to limit broadcasts to a single interface.
 * Returns zero if we successfully produced an address (in *bcaddr),
 * non-zero otherwise.
 */
static int
GetBcastAddr(struct in_addr *ifaddr, struct in_addr *bcaddr)
{
	struct ifaddrs *ifa, *nifa;
	struct sockaddr_in *sin;

	if (getifaddrs(&ifa) != 0) {
159
		FrisPwarning("Could not get interface list");
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		return 1;
	}

	for (nifa = ifa; nifa != NULL; nifa = nifa->ifa_next) {
		if (nifa->ifa_addr->sa_family != AF_INET)
			continue;
		if ((nifa->ifa_flags & (IFF_UP|IFF_BROADCAST)) !=
		    (IFF_UP|IFF_BROADCAST))
			continue;
		sin = (struct sockaddr_in *)nifa->ifa_addr;
		if (ifaddr->s_addr == sin->sin_addr.s_addr) {
			*bcaddr = ((struct sockaddr_in *)nifa->ifa_broadaddr)->sin_addr;
			freeifaddrs(ifa);
			return 0;
		}
	}
	freeifaddrs(ifa);

178
	FrisWarning("Could not find interface %s", inet_ntoa(*ifaddr));
179 180 181
	return 1;
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
/*
 * Bind one port from the given range.
 * If lo == hi == 0, then we let the kernel choose.
 * If lo == hi != 0, then we must get that port.
 * Otherwise we loop over the range til we get one.
 * Returns the port bound or 0 if unsuccessful.
 */
static in_port_t
BindPort(in_addr_t addr, in_port_t portlo, in_port_t porthi)
{
	struct sockaddr_in name;
	socklen_t sl = sizeof(name);

	name.sin_family      = AF_INET;
	name.sin_addr.s_addr = htonl(addr);
	name.sin_port        = htons(portlo);

	/*
	 * Let the kernel choose.
	 */
	if (portlo == 0) {
203 204 205
		if (bind(sock, (struct sockaddr *)&name, sl) != 0) {
			FrisPwarning("Bind to %s:%d failed",
				     inet_ntoa(name.sin_addr), portlo);
206
			return 0;
207
		}
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

		if (getsockname(sock, (struct sockaddr *)&name, &sl) < 0)
			FrisPfatal("could not determine bound port");
		return(ntohs(name.sin_port));
	}
	
	/*
	 * Specific port. Try a few times to get it.
	 */
	if (portlo == porthi) {
		int i = MAXBINDATTEMPTS;

		while (i) {
			if (bind(sock, (struct sockaddr *)&name, sl) == 0)
				return portlo;

			if (--i) {
				FrisPwarning("Bind to %s:%d failed. "
					     "Will try %d more times!",
					     inet_ntoa(name.sin_addr), portlo, i);
				sleep(5);
			}
		}
231 232
		FrisPwarning("Bind to %s:%d failed",
			     inet_ntoa(name.sin_addr), portlo);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
		return 0;
	}

	/*
	 * Port range, gotta loop through trying to grab one.
	 */
	while (portlo <= porthi) {
		name.sin_port = htons(portlo);
		if (bind(sock, (struct sockaddr *)&name, sl) == 0)
			return portlo;
		portlo++;
	}

	return 0;
}

249
static void
250
CommonInit(int portlo, int porthi, int dobind)
251 252 253 254
{
	int			i;
	char			buf[BUFSIZ];
	struct hostent		*he;
255

256
	sockbufsize = GetSockbufSize();
257
	if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
258
		FrisPfatal("Could not allocate a socket");
259

260
	i = sockbufsize;
Mike Hibler's avatar
Mike Hibler committed
261
	if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &i, sizeof(i)) < 0)
262
		FrisPwarning("Could not set send socket buffer size to %d", i);
263
    
264
	i = sockbufsize;
Mike Hibler's avatar
Mike Hibler committed
265
	if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &i, sizeof(i)) < 0)
266 267
		FrisPwarning("Could not set receive socket buffer size to %d",
			     i);
Mike Hibler's avatar
Mike Hibler committed
268

269 270 271
	/*
	 * At present, we use a multicast address in both directions.
	 */
272
	if (IS_MCAST_ADDR(mcastaddr)) {
273 274 275
		unsigned int loop = 0, ttl = MCAST_TTL;
		struct ip_mreq mreq;

276
		FrisLog("Using Multicast %s", inet_ntoa(mcastaddr));
277 278 279 280 281 282 283 284 285 286

		mreq.imr_multiaddr.s_addr = mcastaddr.s_addr;

		if (mcastif.s_addr)
			mreq.imr_interface.s_addr = mcastif.s_addr;
		else
			mreq.imr_interface.s_addr = htonl(INADDR_ANY);

		if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
			       &mreq, sizeof(mreq)) < 0)
287
			FrisPfatal("setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP)");
288 289 290

		if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL,
			       &ttl, sizeof(ttl)) < 0) 
291
			FrisPfatal("setsockopt(IPPROTO_IP, IP_MULTICAST_TTL)");
292 293 294 295

		/* Disable local echo */
		if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP,
			       &loop, sizeof(loop)) < 0)
296
			FrisPfatal("setsockopt(IPPROTO_IP, IP_MULTICAST_LOOP)");
297 298 299 300

		if (mcastif.s_addr &&
		    setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
			       &mcastif, sizeof(mcastif)) < 0) {
301
			FrisPfatal("setsockopt(IPPROTO_IP, IP_MULTICAST_IF)");
302
		}
303

304 305 306 307 308 309 310 311 312 313 314 315 316 317
#ifdef USE_REUSEADDR
		/*
		 * Allow use of the desired port in the presense of other
		 * non-MC use. Also allows for multiple clients of the same
		 * stream.
		 */
		if (isclient) {
			i = 1;
			if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
				       &i, sizeof(i)))
				FrisWarning("Could not set SO_REUSEADDR");
		}
#endif

318 319 320
#ifdef WITH_IGMP
		IGMPInit(&mcastif, &mcastaddr);
#endif
321
	}
322
	else if (broadcast) {
323
		FrisLog("Setting broadcast mode");
324

325
		/*
326 327 328 329 330
		 * If they are using the local broadcast address and they
		 * have specified an interface, attempt to limit broadcasts
		 * to that interface by using the subnet broadcast address.
		 * Otherwise we issue a dire warning about the consequences
		 * of broadcasting to all interfaces.
331
		 */
332 333 334 335
		if (ntohl(mcastaddr.s_addr) == INADDR_BROADCAST) {
			struct in_addr bcaddr;
			if (mcastif.s_addr &&
			    GetBcastAddr(&mcastif, &bcaddr) == 0) {
336 337
				FrisLog("Limiting broadcasts using %s",
					inet_ntoa(bcaddr));
338 339
				mcastaddr = bcaddr;
			} else
340 341
				FrisWarning("WARNING: will broadcast "
					    "to ALL configured interfaces!");
342
		}
343

344
		i = 1;
345 346
		if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
			       &i, sizeof(i)) < 0)
347
			FrisPfatal("setsockopt(SOL_SOCKET, SO_BROADCAST)");
348 349
	}

350 351 352 353 354 355 356 357
	if (dobind) {
		in_addr_t addr = INADDR_ANY;

#ifdef USE_REUSEADDR
		/*
		 * For REUSEADDR to work in the face of unrelated apps that
		 * bind INADDR_ANY:port, we must NOT also bind INADDR_ANY.
		 */
358
		if (isclient && IS_MCAST_ADDR(mcastaddr))
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
			addr = ntohl(mcastaddr.s_addr);
#endif
		portnum = BindPort(addr, portlo, porthi);

		/*
		 * Could not get a port.
		 * Note that we exit with a magic value. This is for server
		 * wrapper-scripts so that they can differentiate this case
		 * and try again with a different port.
		 *
		 * Note also that if portlo == 0, it cannot be a port
		 * conflict so we do not retry.
		 */
		if (portnum == 0) {
			FrisError("Could not bind %s:%d!\n",
				  inet_ntoa(mcastaddr), portnum);
			exit(portlo ? EADDRINUSE : -1);
		}
		FrisLog("Bound to %s:%d", inet_ntoa(mcastaddr), portnum);
	} else {
		portnum = portlo;
		FrisLog("NOT binding to %s:%d", inet_ntoa(mcastaddr), portnum);
	}
	sndportnum = htons(portnum);

384
#ifndef NO_SOCKET_TIMO
385 386 387
	/*
	 * We use a socket level timeout instead of polling for data.
	 */
388 389 390 391 392 393 394
	{
		struct timeval timeout;

		timeout.tv_sec  = 0;
		timeout.tv_usec = PKTRCV_TIMEOUT;
		if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
			       &timeout, sizeof(timeout)) < 0)
395
			FrisPfatal("setsockopt(SOL_SOCKET, SO_RCVTIMEO)");
396 397
	}
#endif
398 399

	/*
Mike Hibler's avatar
Mike Hibler committed
400 401 402
	 * If a specific interface IP is specified, use that to
	 * tag our outgoing packets.  Otherwise we use the IP address
	 * associated with our hostname.
403
	 */
Mike Hibler's avatar
Mike Hibler committed
404 405 406
	if (mcastif.s_addr)
		myipaddr.s_addr = mcastif.s_addr;
	else {
407
		if (gethostname(buf, sizeof(buf)) < 0)
408
			FrisPfatal("gethostname failed");
409

410
		if ((he = gethostbyname(buf)) == 0)
411
			FrisFatal("gethostbyname: %s", hstrerror(h_errno));
412

413 414
		memcpy((char *)&myipaddr, he->h_addr, sizeof(myipaddr));
	}
415 416

	/*
Mike Hibler's avatar
Mike Hibler committed
417
	 * Compute the out of buffer space delay.
418 419
	 */
	if (nobufdelay < 0)
Mike Hibler's avatar
Mike Hibler committed
420
		nobufdelay = sleeptime(100, NULL, 1);
421 422 423
}

int
424
ClientNetInit(int port)
425
{
426 427 428
	isclient = 1;
#ifdef SAME_HOST_HACK
	CommonInit(port, port, 0);
Mike Hibler's avatar
Mike Hibler committed
429
#else
430
	CommonInit(port, port, 1);
Mike Hibler's avatar
Mike Hibler committed
431
#endif
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

#ifdef USE_REUSEADDR_COMPAT
	/*
	 * Bind a unicast socket for our interface address and the port.
	 *
	 * XXX this is for backward compatibility with an older server and
	 * is used for two purposes.
	 *
	 * One, is so that we can receive a unicast JOIN reply from an old
	 * server (the new server always multicasts JOIN replies). The old
	 * client would see this reply because it would just bind the port
	 * using INADDR_ANY in CommonInit, insuring that it would get the
	 * JOIN reply. But with SO_REUSEADDR, we do not bind to INADDR_ANY,
	 * we bind explicitly to the MC address and port and thus would not
	 * see the reply without also binding the unicast (interface) address
	 * and port.
	 *
	 * Two, we send all our multicast packets (joins and block requests)
	 * out this interface so that the packets are stamped with the IP of
	 * the interface and not the MC IP. The old server required that the
	 * packet source IP match the source IP in the frisbee packet header
	 * (the new server allows packets that come from the MC address).
	 * We could just set the frisbee packet header srcip to be the MC
	 * address instead, but we use that address for logging and stats
	 * and want to keep it correct.
	 */
	if (myipaddr.s_addr && IS_MCAST_ADDR(mcastaddr)) {
		struct sockaddr_in name;
		int i;

		if ((selfsock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
			FrisPfatal("Could not allocate a socket");

		i = MCAST_TTL;
		if (setsockopt(selfsock, IPPROTO_IP, IP_MULTICAST_TTL,
			       &i, sizeof(i)) < 0) 
			FrisWarning("Could not set MC TTL");

		/* Disable local echo */
		i = 0;
		if (setsockopt(selfsock, IPPROTO_IP, IP_MULTICAST_LOOP,
			       &i, sizeof(i)) < 0)
			FrisWarning("Could not clear local echo");

		/* Make sure we use the correct interface */
		if (mcastif.s_addr &&
		    setsockopt(selfsock, IPPROTO_IP, IP_MULTICAST_IF,
			       &mcastif, sizeof(mcastif)) < 0) {
			FrisWarning("Could not set MCAST_IF");
		}

		/* Set REUSEADDR */
		i = 1;
		if (setsockopt(selfsock, SOL_SOCKET, SO_REUSEADDR,
			       &i, sizeof(i)))
			FrisWarning("Could not set SO_REUSEADDR");

		name.sin_family = AF_INET;
		name.sin_port = htons(portnum);
		name.sin_addr.s_addr = myipaddr.s_addr;
		if (bind(selfsock, (struct sockaddr *)&name, sizeof(name)) < 0)
			FrisPfatal("Could not bind to %s:%d",
				   inet_ntoa(name.sin_addr), portnum);

#ifndef NO_SOCKET_TIMO
		/*
		 * We use a socket level timeout instead of polling for data.
		 */
		{
			struct timeval timeout;

			timeout.tv_sec  = 0;
			timeout.tv_usec = PKTRCV_TIMEOUT;
			if (setsockopt(selfsock, SOL_SOCKET, SO_RCVTIMEO,
				       &timeout, sizeof(timeout)) < 0)
				FrisPfatal("setsockopt(SOL_SOCKET, SO_RCVTIMEO)");
		}
#endif
	}
#endif

513 514 515
	return 1;
}

516 517 518 519 520 521
unsigned long
ClientNetID(void)
{
	return ntohl(myipaddr.s_addr);
}

522
int
523
ServerNetInit(int portlo, int porthi)
524
{
Mike Hibler's avatar
Mike Hibler committed
525
	isclient = 0;
526
	CommonInit(portlo, porthi, 1);
527

528 529 530 531 532 533 534 535 536
#ifdef linux
	/*
	 * Enabled extended error reporting so that we get back ENOBUFS
	 * when we overrun the sent socket or NIC send buffers.
	 * For now we just do this on the client.
	 */
	{
		int i = 1;
		if (setsockopt(sock, SOL_IP, IP_RECVERR, &i, sizeof(i)) < 0)
537
			FrisPwarning("Could not enable extended errors");
538 539 540
	}
#endif

541 542 543
	return 1;
}

544 545 546 547 548 549 550 551 552 553
/*
 * XXX hack.
 *
 * Cisco switches without a multicast router defined have an unfortunate
 * habit of losing our IGMP membership.  This function allows us to send
 * a report message to remind the switch we are still around.
 *
 * We need a better way to do this!
 */
int
554
NetMCKeepAlive(void)
555 556 557 558 559 560 561 562 563
{
	struct ip_mreq mreq;

	if (broadcast || (ntohl(mcastaddr.s_addr) >> 28) != 14)
		return 0;

	if (sock == -1)
		return 1;

564 565 566 567 568 569
#ifdef WITH_IGMP
	/* Send a direct V2 report packet if possible */
	if (IGMPSendReport() == 0)
		return 0;
#endif

570 571 572 573 574 575 576 577 578 579 580
	mreq.imr_multiaddr.s_addr = mcastaddr.s_addr;
	if (mcastif.s_addr)
		mreq.imr_interface.s_addr = mcastif.s_addr;
	else
		mreq.imr_interface.s_addr = htonl(INADDR_ANY);

	if (setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
		       &mreq, sizeof(mreq)) < 0 ||
	    setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
		       &mreq, sizeof(mreq)) < 0)
		return 1;
581

582 583 584
	return 0;
}

585 586 587 588 589 590 591
/*
 * Look for a packet on the socket. Propogate the errors back to the caller
 * exactly as the system call does. Remember that we set up a socket timeout
 * above, so we will get EWOULDBLOCK errors when no data is available. 
 *
 * The amount of data received is determined from the datalen of the hdr.
 * All packets are actually the same size/structure. 
Mike Hibler's avatar
Mike Hibler committed
592
 *
593
 * Returns 0 for a good packet, 1 for a bad packet, -1 on timeout.
594 595 596 597 598
 */
int
PacketReceive(Packet_t *p)
{
	struct sockaddr_in from;
Mike Hibler's avatar
Mike Hibler committed
599 600
	int		   mlen;
	unsigned int	   alen;
601

602 603 604 605 606 607 608 609 610 611 612 613 614
#ifdef NO_SOCKET_TIMO
	fd_set		ready;
	struct timeval	tv;
	int		rv;

	tv.tv_sec = 0;
	tv.tv_usec = PKTRCV_TIMEOUT;
	FD_ZERO(&ready);
	FD_SET(sock, &ready);
	rv = select(sock+1, &ready, NULL, NULL, &tv);
	if (rv < 0) {
		if (errno == EINTR)
			return -1;
615
		FrisPfatal("PacketReceive(select)");
616 617 618 619
	}
	if (rv == 0)
		return -1;
#endif
620 621
	alen = sizeof(from);
	bzero(&from, alen);
622 623
	if ((mlen = recvfrom(sock, p, sizeof(*p), 0,
			     (struct sockaddr *)&from, &alen)) < 0) {
624
		if (errno == EWOULDBLOCK || errno == EINTR)
625
			return -1;
626
		FrisPfatal("PacketReceive(recvfrom)");
627
	}
628

Mike Hibler's avatar
Mike Hibler committed
629 630 631
	/*
	 * Basic integrity checks
	 */
632
	if ((uint32_t)mlen < sizeof(p->hdr) + p->hdr.datalen) {
633 634
		FrisLog("Bad message length (%d != %d)",
			mlen, p->hdr.datalen);
Mike Hibler's avatar
Mike Hibler committed
635 636
		return 1;
	}
Mike Hibler's avatar
Mike Hibler committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
#ifdef SAME_HOST_HACK
	/*
	 * If using a host alias for the client, a message may get
	 * the wrong IP, so rig the IP check to make it always work.
	 */
	if (p->hdr.srcip != from.sin_addr.s_addr)
		from.sin_addr.s_addr = p->hdr.srcip;

	/*
	 * Also, we aren't binding to a port on the client side, so the
	 * first message to the server will contain the actual port we
	 * will use from now on.
	 */
	if (!isclient && sndportnum == htons(portnum) &&
	    sndportnum != from.sin_port)
		sndportnum = from.sin_port;
#endif
654 655 656
	/*
	 * XXX accept packets from the MC address. This will be the case with
	 * newer clients that bind to the MC address instead of INADDR_ANY.
657 658 659 660 661
	 *
	 * Note that on a client, certain packets should only come from the
	 * server. These include: BLOCK replies and PROGRESS requests.
	 * Don't rewrite the address in these cases so that the following
	 * check will catch them (or a later caller check on hdr.srcip).
662
	 */
663 664 665 666 667 668 669 670 671
	if (from.sin_addr.s_addr == mcastaddr.s_addr) {
		if (isclient &&
		    (p->hdr.subtype == PKTSUBTYPE_BLOCK ||
		     (p->hdr.subtype == PKTSUBTYPE_PROGRESS &&
		      p->hdr.type == PKTTYPE_REQUEST)))
			;
		else
			from.sin_addr.s_addr = p->hdr.srcip;
	}
672

Mike Hibler's avatar
Mike Hibler committed
673
	if (p->hdr.srcip != from.sin_addr.s_addr) {
674 675
		FrisLog("Bad message source (%x != %x)",
			ntohl(from.sin_addr.s_addr), ntohl(p->hdr.srcip));
Mike Hibler's avatar
Mike Hibler committed
676 677
		return 1;
	}
Mike Hibler's avatar
Mike Hibler committed
678
	if (sndportnum != from.sin_port) {
679 680
		FrisLog("Bad message port (%d != %d)",
			ntohs(from.sin_port), ntohs(sndportnum));
Mike Hibler's avatar
Mike Hibler committed
681 682
		return 1;
	}
Mike Hibler's avatar
Mike Hibler committed
683
	return 0;
684 685
}

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
#ifdef USE_REUSEADDR_COMPAT
/*
 * Same as PacketReceive but read from unicast (self) socket.
 *
 * Returns 0 for a good packet, 1 for a bad packet, -1 on timeout.
 */
int
PacketRequest(Packet_t *p)
{
	struct sockaddr_in from;
	int		   mlen;
	unsigned int	   alen;

	if (selfsock < 0)
		return -1;

#ifdef NO_SOCKET_TIMO
	{
		fd_set		ready;
		struct timeval	tv;
		int		rv, maxfd;

		tv.tv_sec = 0;
		tv.tv_usec = PKTRCV_TIMEOUT;
		FD_ZERO(&ready);
		FD_SET(selfsock, &ready);
		rv = select(selfsock+1, &ready, NULL, NULL, &tv);
		if (rv < 0) {
			if (errno == EINTR)
				return -1;
			FrisPfatal("PacketRequest(select)");
		}
		if (rv == 0)
			return -1;
	}
#endif
	alen = sizeof(from);
	bzero(&from, alen);
	if ((mlen = recvfrom(selfsock, p, sizeof(*p), 0,
			     (struct sockaddr *)&from, &alen)) < 0) {
		if (errno == EWOULDBLOCK || errno == EINTR)
			return -1;
		FrisPfatal("PacketRequest(recvfrom)");
	}

	/*
	 * Basic integrity checks
	 */
	if (mlen < sizeof(p->hdr) + p->hdr.datalen) {
		FrisLog("Bad message length (%d != %d)",
			mlen, p->hdr.datalen);
		return 1;
	}
#ifdef SAME_HOST_HACK
	/*
	 * If using a host alias for the client, a message may get
	 * the wrong IP, so rig the IP check to make it always work.
	 */
	if (p->hdr.srcip != from.sin_addr.s_addr)
		from.sin_addr.s_addr = p->hdr.srcip;

	/*
	 * Also, we aren't binding to a port on the client side, so the
	 * first message to the server will contain the actual port we
	 * will use from now on.
	 */
	if (!isclient && sndportnum == htons(portnum) &&
	    sndportnum != from.sin_port)
		sndportnum = from.sin_port;
#endif
	if (p->hdr.srcip != from.sin_addr.s_addr) {
		FrisLog("Bad message source (%x != %x)",
			ntohl(from.sin_addr.s_addr), ntohl(p->hdr.srcip));
		return 1;
	}
	if (sndportnum != from.sin_port) {
		FrisLog("Bad message port (%d != %d)",
			ntohs(from.sin_port), ntohs(sndportnum));
		return 1;
	}
	return 0;
}
#endif

770 771 772 773
#ifndef MSG_DONTWAIT
#define MSG_DONTWAIT 0
#endif

774 775 776 777 778 779 780
/*
 * We use blocking sends since there is no point in giving up. All packets
 * go to the same place, whether client or server.
 *
 * The amount of data sent is determined from the datalen of the packet hdr.
 * All packets are actually the same size/structure. 
 */
781 782
void
PacketSend(Packet_t *p, int *resends)
783 784
{
	struct sockaddr_in to;
785
	int		   len, delays, rc;
786
	int		   fd = sock;
787

788
	len = sizeof(p->hdr) + p->hdr.datalen;
789
	p->hdr.srcip = myipaddr.s_addr;
790 791

	to.sin_family      = AF_INET;
Mike Hibler's avatar
Mike Hibler committed
792
	to.sin_port        = sndportnum;
793 794
	to.sin_addr.s_addr = mcastaddr.s_addr;

795
	delays = 0;
796 797 798 799 800 801
#ifdef USE_REUSEADDR_COMPAT
	/* send out selfsock so the source IP is ours and not the MC addr */
	if (selfsock >= 0)
		fd = selfsock;
#endif
	while ((rc = sendto(fd, (void *)p, len, MSG_DONTWAIT,
802 803
			    (struct sockaddr *)&to, sizeof(to))) <= 0) {
		if (rc < 0 && !(errno == ENOBUFS || errno == EAGAIN))
804
			FrisPfatal("PacketSend(sendto)");
805 806

		/*
807 808 809 810 811 812 813 814 815 816
		 * ENOBUFS (BSD) or EAGAIN (Linux, because we set DONTWAIT)
		 * means there was not enough socket space for the packet.
		 * Okay to sleep a bit to let things drain.
		 *
		 * Note that on BSD, ENOBUFS is also returned when the NIC
		 * send buffers are full, so we should never lose a packet
		 * on the send path.
		 *
		 * On Linux, we get this behavior as well by turning on
		 * the extended error message passing (IP_RECVERR).
817
		 */
818 819
		delays++;
		fsleep(nobufdelay);
820 821
	}

822 823 824
	DOSTAT(nonetbufs += delays);
	if (resends != 0)
		*resends = delays;
825 826 827 828 829 830 831 832
}

/*
 * Basically the same as above, but instead of sending to the multicast
 * group, send to the (unicast) IP in the packet header. This simplifies
 * the logic in a number of places, by avoiding having to deal with
 * multicast packets that are not destined for us, but for someone else.
 */
833
void
834
PacketReply(Packet_t *p, int firenforget)
835 836
{
	struct sockaddr_in to;
837 838
	int		len;
	int		fd = sock;
839 840 841 842

	len = sizeof(p->hdr) + p->hdr.datalen;

	to.sin_family      = AF_INET;
Mike Hibler's avatar
Mike Hibler committed
843
	to.sin_port        = sndportnum;
844
	to.sin_addr.s_addr = p->hdr.srcip;
845
	p->hdr.srcip       = myipaddr.s_addr;
846

847 848 849 850 851 852 853
#ifdef USE_REUSEADDR_COMPAT
	/* send out selfsock so the source IP is ours and not the MC addr */
	if (selfsock >= 0)
		fd = selfsock;
#endif

	while (sendto(fd, (void *)p, len, 0, 
854
		      (struct sockaddr *)&to, sizeof(to)) < 0) {
855
		if (errno != ENOBUFS && errno != EAGAIN)
856 857 858 859
			FrisPfatal("PacketReply(sendto)");

		if (firenforget)
			break;
860 861 862 863 864

		/*
		 * ENOBUFS means we ran out of mbufs. Okay to sleep a bit
		 * to let things drain.
		 */
865 866
		DOSTAT(nonetbufs++);
		fsleep(nobufdelay);
867
	}
868
}
869

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
int
PacketValid(Packet_t *p, int nchunks)
{
	switch (p->hdr.type) {
	case PKTTYPE_REQUEST:
	case PKTTYPE_REPLY:
		break;
	default:
		return 0;
	}

	switch (p->hdr.subtype) {
	case PKTSUBTYPE_BLOCK:
		if (p->hdr.datalen < sizeof(p->msg.block))
			return 0;
		if (p->msg.block.chunk < 0 ||
		    p->msg.block.chunk >= nchunks ||
		    p->msg.block.block < 0 ||
888
		    p->msg.block.block >= MAXCHUNKSIZE)
889 890 891 892 893 894 895 896
			return 0;
		break;
	case PKTSUBTYPE_REQUEST:
		if (p->hdr.datalen < sizeof(p->msg.request))
			return 0;
		if (p->msg.request.chunk < 0 ||
		    p->msg.request.chunk >= nchunks ||
		    p->msg.request.block < 0 ||
897
		    p->msg.request.block >= MAXCHUNKSIZE ||
898
		    p->msg.request.count < 0 ||
899
		    p->msg.request.block+p->msg.request.count > MAXCHUNKSIZE)
900 901
			return 0;
		break;
Mike Hibler's avatar
Mike Hibler committed
902 903 904 905 906 907 908
	case PKTSUBTYPE_PREQUEST:
		if (p->hdr.datalen < sizeof(p->msg.prequest))
			return 0;
		if (p->msg.prequest.chunk < 0 ||
		    p->msg.prequest.chunk >= nchunks)
			return 0;
		break;
909 910 911 912
	case PKTSUBTYPE_JOIN:
		if (p->hdr.datalen < sizeof(p->msg.join))
			return 0;
		break;
913 914 915 916
	case PKTSUBTYPE_JOIN2:
		if (p->hdr.datalen < sizeof(p->msg.join2))
			return 0;
		break;
917 918 919 920 921 922 923 924
	case PKTSUBTYPE_LEAVE:
		if (p->hdr.datalen < sizeof(p->msg.leave))
			return 0;
		break;
	case PKTSUBTYPE_LEAVE2:
		if (p->hdr.datalen < sizeof(p->msg.leave2))
			return 0;
		break;
925 926 927 928
	case PKTSUBTYPE_PROGRESS:
		if (p->hdr.datalen < sizeof(p->msg.progress.hdr))
			return 0;
		break;
929 930 931 932 933
	default:
		return 0;
	}

	return 1;
934
}
935

936 937
/*
 * Functions for communicating with the master server.
938 939 940 941 942 943 944 945 946
 *
 * TODO: protocol for negotiating the protocol version:
 * On the client, send a request with our current version and:
 *   - get a version error back: server must be V01, so redo with V01
 *   - otherwise header reply contains version
 *     if not our version, must be a lower version, so redo with that version
 * On the server:
 *   - version less than our current version, use that version
 *   - version greater than ours, reply with our version 
947
 */
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
#ifdef MASTER_SERVER
int
MsgSend(int msock, MasterMsg_t *msg, size_t size, int timo)
{
	void *buf = msg;
	int cc;
	struct timeval tv, now, then;
	fd_set wfds;

	if (timo) {
		tv.tv_sec = timo;
		tv.tv_usec = 0;
		gettimeofday(&then, NULL);
		timeradd(&then, &tv, &then);
	}
	while (size > 0) {
		if (timo) {
			gettimeofday(&now, NULL);
			if (timercmp(&now, &then, >=)) {
				cc = 0;
			} else {
				timersub(&then, &now, &tv);
				FD_ZERO(&wfds);
				FD_SET(msock, &wfds);
				cc = select(msock+1, NULL, &wfds, NULL, &tv);
			}
			if (cc <= 0) {
				if (cc == 0) {
					errno = ETIMEDOUT;
					cc = -1;
				}
				break;
			}
		}

		cc = write(msock, buf, size);
		if (cc <= 0)
			break;

		size -= cc;
		buf += cc;
	}

	if (size != 0) {
		char *estr = "master server message send";
		if (cc == 0)
			fprintf(stderr, "%s: Unexpected EOF\n", estr);
		else
			perror(estr);
		return 0;
	}
	return 1;
}

int
MsgReceive(int msock, MasterMsg_t *msg, size_t size, int timo)
{
	void *buf = msg;
	int cc;
	struct timeval tv, now, then;
	fd_set rfds;

	if (timo) {
		tv.tv_sec = timo;
		tv.tv_usec = 0;
		gettimeofday(&then, NULL);
		timeradd(&then, &tv, &then);
	}
	while (size > 0) {
		if (timo) {
			gettimeofday(&now, NULL);
			if (timercmp(&now, &then, >=)) {
				cc = 0;
			} else {
				timersub(&then, &now, &tv);
				FD_ZERO(&rfds);
				FD_SET(msock, &rfds);
				cc = select(msock+1, &rfds, NULL, NULL, &tv);
			}
			if (cc <= 0) {
				if (cc == 0) {
					errno = ETIMEDOUT;
					cc = -1;
				}
				break;
			}
		}

		cc = read(msock, buf, size);
		if (cc <= 0)
			break;

		size -= cc;
		buf += cc;
	}

	if (size != 0) {
		char *estr = "master server message receive";
		if (cc == 0)
			fprintf(stderr, "%s: Unexpected EOF\n", estr);
		else
			perror(estr);
		return 0;
	}
	return 1;
}

/*
 * Contact the master server to discover download information for imageid.
1057 1058 1059 1060 1061
 * 'sip' and 'sport' are the addr/port of the master server, 'method'
 * specifies the desired download method, 'askonly' is set to just ask
 * for information about the image (without starting a server), 'timeout'
 * is how long to wait for a response.
 *
1062 1063 1064 1065
 * If 'hostip' is not zero, then we are requesting information on behalf of
 * that node.  The calling node (us) must have "proxy" permission on the
 * server for this to work.
 *
1066 1067
 * On success, return non-zero with 'reply' filled in with the server's
 * response IN HOST ORDER.  On failure returns zero.
1068 1069
 */
int
1070 1071
ClientNetFindServer(in_addr_t sip, in_port_t sport,
		    in_addr_t hostip, char *imageid,
1072 1073
		    int method, int askonly, int timeout,
		    GetReply *reply, struct in_addr *myip)
1074 1075 1076
{
	struct sockaddr_in name;
	MasterMsg_t msg;
1077
	int msock, len;
1078 1079 1080 1081 1082
	
	if ((msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
		perror("Could not allocate socket for master server");
		return 0;
	}
1083 1084
	if (sport == 0)
		sport = MS_PORTNUM;
1085

1086 1087
	/* XXX need connection timeout! */

1088
	name.sin_family = AF_INET;
1089 1090
	name.sin_addr.s_addr = htonl(sip);
	name.sin_port = htons(sport);
1091
	if (connect(msock, (struct sockaddr *)&name, sizeof(name)) < 0) {
1092 1093 1094
		fprintf(stderr,
			"Connecting to master server %s:%d failed: %s",
			inet_ntoa(name.sin_addr), sport, strerror(errno));
1095 1096 1097 1098
		close(msock);
		return 0;
	}

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	/*
	 * XXX recover the IP address of the interface used to talk to
	 * the server.
	 */
	if (myip) {
		struct sockaddr_in me;
		socklen_t len = sizeof me;

		if (getsockname(msock, (struct sockaddr *)&me, &len) < 0) {
			perror("getsockname");
			close(msock);
			return 0;
		}
		*myip = me.sin_addr;
	}

1115
	memset(&msg, 0, sizeof msg);
1116 1117 1118
	strncpy((char *)msg.hdr.version, MS_MSGVERS_1,
		sizeof(msg.hdr.version));
	msg.hdr.type = htonl(MS_MSGTYPE_GETREQUEST);
1119
	msg.body.getrequest.hostip = htonl(hostip);
1120
	if (askonly) {
1121 1122 1123
		msg.body.getrequest.status = 1;
		msg.body.getrequest.methods = MS_METHOD_ANY;
	} else {
1124
		msg.body.getrequest.methods = method;
1125 1126 1127 1128 1129
	}
	len = strlen(imageid);
	if (len > MS_MAXIDLEN)
		len = MS_MAXIDLEN;
	msg.body.getrequest.idlen = htons(len);
1130
	strncpy((char *)msg.body.getrequest.imageid, imageid, MS_MAXIDLEN);
1131

1132
	len = sizeof msg.hdr + sizeof msg.body.getrequest;
1133 1134 1135 1136 1137 1138
	if (!MsgSend(msock, &msg, len, timeout)) {
		close(msock);
		return 0;
	}

	memset(&msg, 0, sizeof msg);
1139
	len = sizeof msg.hdr + sizeof msg.body.getreply;
1140 1141 1142 1143
	if (!MsgReceive(msock, &msg, len, timeout)) {
		close(msock);
		return 0;
	}
Mike Hibler's avatar
Mike Hibler committed
1144
	close(msock);
1145

1146 1147
	if (strncmp((char *)msg.hdr.version, MS_MSGVERS_1,
		    sizeof(msg.hdr.version))) {
1148 1149 1150
		fprintf(stderr,
			"Got incorrect version from master server %s:%d\n",
			inet_ntoa(name.sin_addr), sport);
1151 1152
		return 0;
	}
1153
	if (ntohl(msg.hdr.type) != MS_MSGTYPE_GETREPLY) {
1154 1155 1156
		fprintf(stderr,
			"Got incorrect reply from master server %s:%d\n",
			inet_ntoa(name.sin_addr), sport);
1157 1158 1159
		return 0;
	}

1160 1161 1162 1163 1164
	/*
	 * Convert the reply info to host order
	 */
	*reply = msg.body.getreply;
	reply->error = ntohs(reply->error);
Mike Hibler's avatar
Mike Hibler committed
1165
	reply->servaddr = ntohl(reply->servaddr);
1166 1167 1168
	reply->addr = ntohl(reply->addr);
	reply->port = ntohs(reply->port);
	reply->sigtype = ntohs(reply->sigtype);
1169 1170 1171 1172 1173
	if (reply->sigtype == MS_SIGTYPE_MTIME)
		*(uint32_t *)reply->signature =
			ntohl(*(uint32_t *)reply->signature);
	reply->hisize = ntohl(reply->hisize);
	reply->losize = ntohl(reply->losize);
1174
	return 1;
1175
}
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290

/*
 * Contact the master server to negotiate an upload for a 'file' to store
 * under the given 'imageid'.
 *
 * 'sip' and 'sport' are the addr/port of the master server, 'askonly' is
 * set to just see if the upload is allowed and to get characteristics of
 * any existing copy of the image, 'timeout' is how long to wait for a
 * response.
 *
 * If 'hostip' is not zero, then we are requesting information on behalf of
 * that node.  The calling node (us) must have "proxy" permission on the
 * server for this to work.
 *
 * On success, return non-zero with 'reply' filled in with the server's
 * response IN HOST ORDER.  On failure returns zero.
 */
int
ClientNetPutRequest(in_addr_t sip, in_port_t sport,
		    in_addr_t hostip, char *imageid,
		    uint64_t isize, uint32_t mtime,
		    int timeout, int askonly, int reqtimo, PutReply *reply)
{
	struct sockaddr_in name;
	MasterMsg_t msg;
	int msock, len;
	
	if ((msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
		perror("Could not allocate socket for master server");
		return 0;
	}
	if (sport == 0)
		sport = MS_PORTNUM;

	/* XXX need connection timeout! */

	name.sin_family = AF_INET;
	name.sin_addr.s_addr = htonl(sip);
	name.sin_port = htons(sport);
	if (connect(msock, (struct sockaddr *)&name, sizeof(name)) < 0) {
		fprintf(stderr,
			"Connecting to master server %s:%d failed: %s\n",
			inet_ntoa(name.sin_addr), sport, strerror(errno));
		close(msock);
		return 0;
	}

	memset(&msg, 0, sizeof msg);
	strncpy((char *)msg.hdr.version, MS_MSGVERS_1,
		sizeof(msg.hdr.version));
	msg.hdr.type = htonl(MS_MSGTYPE_PUTREQUEST);
	msg.body.putrequest.hostip = htonl(hostip);
	if (askonly)
		msg.body.putrequest.status = 1;
	len = strlen(imageid);
	if (len > MS_MAXIDLEN)
		len = MS_MAXIDLEN;
	msg.body.putrequest.idlen = htons(len);
	strncpy((char *)msg.body.putrequest.imageid, imageid, MS_MAXIDLEN);
	if (isize > 0) {
		msg.body.putrequest.hisize = htonl(isize >> 32);
		msg.body.putrequest.losize = htonl(isize);
	}
	if (mtime)
		msg.body.putrequest.mtime = htonl(mtime);
	/* XXX have the server wait longer than us so we timeout first */
	if (timeout)
		msg.body.putrequest.timeout = htonl(timeout+2);

	len = sizeof msg.hdr + sizeof msg.body.putrequest;
	if (!MsgSend(msock, &msg, len, reqtimo)) {
		close(msock);
		return 0;
	}

	memset(&msg, 0, sizeof msg);
	len = sizeof msg.hdr + sizeof msg.body.putreply;
	if (!MsgReceive(msock, &msg, len, reqtimo)) {
		close(msock);
		return 0;
	}
	close(msock);

	if (strncmp((char *)msg.hdr.version, MS_MSGVERS_1,
		    sizeof(msg.hdr.version))) {
		fprintf(stderr,
			"Got incorrect version from master server %s:%d\n",
			inet_ntoa(name.sin_addr), sport);
		return 0;
	}
	if (ntohl(msg.hdr.type) != MS_MSGTYPE_PUTREPLY) {
		fprintf(stderr,
			"Got incorrect reply from master server %s:%d\n",
			inet_ntoa(name.sin_addr), sport);
		return 0;
	}

	/*
	 * Convert the reply info to host order
	 */
	*reply = msg.body.putreply;
	reply->error = ntohs(reply->error);
	reply->addr = ntohl(reply->addr);
	reply->port = ntohs(reply->port);
	reply->sigtype = ntohs(reply->sigtype);
	if (reply->sigtype == MS_SIGTYPE_MTIME)
		*(uint32_t *)reply->signature =
			ntohl(*(uint32_t *)reply->signature);
	reply->hisize = ntohl(reply->hisize);
	reply->losize = ntohl(reply->losize);
	reply->himaxsize = ntohl(reply->himaxsize);
	reply->lomaxsize = ntohl(reply->lomaxsize);
	return 1;
}

1291
#endif
1292 1293 1294 1295 1296

/*
 * Functions for dealing with IGMP
 */
#ifdef WITH_IGMP
1297
#include <netinet/in_systm.h>	/* for older *BSD that needs n_long */
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411