network.c 16.4 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2011 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
4 5 6
 * All rights reserved.
 */

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Network routines.
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#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
23
#include "utils.h"
24 25 26
#ifdef NO_SOCKET_TIMO
#include <sys/select.h>
#endif
Mike Hibler's avatar
Mike Hibler committed
27

28 29 30 31 32 33 34
#ifdef STATS
unsigned long nonetbufs;
#define DOSTAT(x)	(x)
#else
#define DOSTAT(x)
#endif

35
/* Max number of times to attempt bind to port before failing. */
36
#define MAXBINDATTEMPTS		1
37 38 39 40

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

41
static int		sock = -1;
42
struct in_addr		myipaddr;
43
static int		nobufdelay = -1;
44
int			broadcast = 0;
Mike Hibler's avatar
Mike Hibler committed
45 46 47
static int		isclient = 0;
static int		sndportnum;	/* kept in network order */

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*
 * 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;
}

Mike Hibler's avatar
Mike Hibler committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
int
GetSockbufSize(void)
{

	static int sbsize = 0;

	if (sbsize == 0) {
#if DYN_SOCKBUFSIZE > 0
		int sock;

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

		for (sbsize = SOCKBUFSIZE; sbsize > 0; sbsize -= (16*1024)) {
			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)
				i = SOCKBUFSIZE;
			sbsize = i;
		}
		close(sock);
#else
		sbsize = SOCKBUFSIZE;
#endif
		log("Maximum socket buffer size of %d bytes", sbsize);
	}
	return sbsize;
}
102 103

static void
Mike Hibler's avatar
Mike Hibler committed
104
CommonInit(int dobind)
105 106 107 108 109 110 111 112 113
{
	struct sockaddr_in	name;
	int			i;
	char			buf[BUFSIZ];
	struct hostent		*he;
	
	if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
		pfatal("Could not allocate a socket");

Mike Hibler's avatar
Mike Hibler committed
114
	i = GetSockbufSize();
Mike Hibler's avatar
Mike Hibler committed
115
	if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &i, sizeof(i)) < 0)
Mike Hibler's avatar
Mike Hibler committed
116
		pwarning("Could not increase send socket buffer size to %d", i);
117
    
Mike Hibler's avatar
Mike Hibler committed
118
	i = GetSockbufSize();
Mike Hibler's avatar
Mike Hibler committed
119
	if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &i, sizeof(i)) < 0)
Mike Hibler's avatar
Mike Hibler committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
		pwarning("Could not increase recv socket buffer size to %d", i);

	if (dobind) {
		name.sin_family      = AF_INET;
		name.sin_port	     = htons(portnum);
		name.sin_addr.s_addr = htonl(INADDR_ANY);

		i = MAXBINDATTEMPTS;
		while (i) {
			if (bind(sock, (struct sockaddr *)&name, sizeof(name)) == 0)
				break;

			/*
			 * 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.
			 */
			if (--i == 0) {
				error("Could not bind to port %d!\n", portnum);
				exit(EADDRINUSE);
			}

			pwarning("Bind to port %d failed. Will try %d more times!",
				 portnum, i);
			sleep(5);
146
		}
Mike Hibler's avatar
Mike Hibler committed
147 148 149
		log("Bound to port %d", portnum);
	} else {
		log("NOT binding to port %d", portnum);
150
	}
Mike Hibler's avatar
Mike Hibler committed
151
	sndportnum = htons(portnum);
152 153 154 155 156 157 158 159

	/*
	 * At present, we use a multicast address in both directions.
	 */
	if ((ntohl(mcastaddr.s_addr) >> 28) == 14) {
		unsigned int loop = 0, ttl = MCAST_TTL;
		struct ip_mreq mreq;

160
		log("Using Multicast %s", inet_ntoa(mcastaddr));
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

		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)
			pfatal("setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP)");

		if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL,
			       &ttl, sizeof(ttl)) < 0) 
			pfatal("setsockopt(IPPROTO_IP, IP_MULTICAST_TTL)");

		/* Disable local echo */
		if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP,
			       &loop, sizeof(loop)) < 0)
			pfatal("setsockopt(IPPROTO_IP, IP_MULTICAST_LOOP)");

		if (mcastif.s_addr &&
		    setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
			       &mcastif, sizeof(mcastif)) < 0) {
			pfatal("setsockopt(IPPROTO_IP, IP_MULTICAST_IF)");
		}
	}
188
	else if (broadcast) {
189 190 191 192
		/*
		 * Otherwise, we use a broadcast addr. 
		 */
		i = 1;
193 194

		log("Setting broadcast mode\n");
195 196 197 198 199 200
		
		if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
			       &i, sizeof(i)) < 0)
			pfatal("setsockopt(SOL_SOCKET, SO_BROADCAST)");
	}

201
#ifndef NO_SOCKET_TIMO
202 203 204
	/*
	 * We use a socket level timeout instead of polling for data.
	 */
205 206 207 208 209 210 211 212 213 214
	{
		struct timeval timeout;

		timeout.tv_sec  = 0;
		timeout.tv_usec = PKTRCV_TIMEOUT;
		if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
			       &timeout, sizeof(timeout)) < 0)
			pfatal("setsockopt(SOL_SOCKET, SO_RCVTIMEO)");
	}
#endif
215 216

	/*
Mike Hibler's avatar
Mike Hibler committed
217 218 219
	 * If a specific interface IP is specified, use that to
	 * tag our outgoing packets.  Otherwise we use the IP address
	 * associated with our hostname.
220
	 */
Mike Hibler's avatar
Mike Hibler committed
221 222 223
	if (mcastif.s_addr)
		myipaddr.s_addr = mcastif.s_addr;
	else {
224 225
		if (gethostname(buf, sizeof(buf)) < 0)
			pfatal("gethostname failed");
226

227 228
		if ((he = gethostbyname(buf)) == 0)
			fatal("gethostbyname: %s", hstrerror(h_errno));
229

230 231
		memcpy((char *)&myipaddr, he->h_addr, sizeof(myipaddr));
	}
232 233

	/*
Mike Hibler's avatar
Mike Hibler committed
234
	 * Compute the out of buffer space delay.
235 236
	 */
	if (nobufdelay < 0)
Mike Hibler's avatar
Mike Hibler committed
237
		nobufdelay = sleeptime(100, NULL, 1);
238 239 240 241 242
}

int
ClientNetInit(void)
{
Mike Hibler's avatar
Mike Hibler committed
243 244 245 246 247 248
#ifndef SAME_HOST_HACK
	CommonInit(1);
#else
	CommonInit(0);
#endif
	isclient = 1;
249 250 251 252
	
	return 1;
}

253 254 255 256 257 258
unsigned long
ClientNetID(void)
{
	return ntohl(myipaddr.s_addr);
}

259 260 261
int
ServerNetInit(void)
{
Mike Hibler's avatar
Mike Hibler committed
262 263
	CommonInit(1);
	isclient = 0;
264 265 266 267

	return 1;
}

268 269 270 271 272 273 274 275 276 277
/*
 * 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
278
NetMCKeepAlive(void)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
{
	struct ip_mreq mreq;

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

	if (sock == -1)
		return 1;

	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;
	return 0;
}

302 303 304 305 306 307 308
/*
 * 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
309 310
 *
 * Returns 0 for a good packet, 1 for a back packet, -1 on timeout.
311 312 313 314 315
 */
int
PacketReceive(Packet_t *p)
{
	struct sockaddr_in from;
Mike Hibler's avatar
Mike Hibler committed
316 317
	int		   mlen;
	unsigned int	   alen;
318

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
#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;
		pfatal("PacketReceive(select)");
	}
	if (rv == 0)
		return -1;
#endif
337 338
	alen = sizeof(from);
	bzero(&from, alen);
339 340 341 342 343 344
	if ((mlen = recvfrom(sock, p, sizeof(*p), 0,
			     (struct sockaddr *)&from, &alen)) < 0) {
		if (errno == EWOULDBLOCK)
			return -1;
		pfatal("PacketReceive(recvfrom)");
	}
345

Mike Hibler's avatar
Mike Hibler committed
346 347 348 349 350 351 352 353
	/*
	 * Basic integrity checks
	 */
	if (mlen < sizeof(p->hdr) + p->hdr.datalen) {
		log("Bad message length (%d != %d)",
		    mlen, p->hdr.datalen);
		return 1;
	}
Mike Hibler's avatar
Mike Hibler committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
#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
Mike Hibler's avatar
Mike Hibler committed
371 372 373 374 375
	if (p->hdr.srcip != from.sin_addr.s_addr) {
		log("Bad message source (%x != %x)",
		    ntohl(from.sin_addr.s_addr), ntohl(p->hdr.srcip));
		return 1;
	}
Mike Hibler's avatar
Mike Hibler committed
376 377 378 379 380
	if (sndportnum != from.sin_port) {
		log("Bad message port (%d != %d)",
		    ntohs(from.sin_port), ntohs(sndportnum));
		return 1;
	}
Mike Hibler's avatar
Mike Hibler committed
381
	return 0;
382 383 384 385 386 387 388 389 390
}

/*
 * 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. 
 */
391 392
void
PacketSend(Packet_t *p, int *resends)
393 394
{
	struct sockaddr_in to;
395 396
	int		   len, delays;

397
	len = sizeof(p->hdr) + p->hdr.datalen;
398
	p->hdr.srcip = myipaddr.s_addr;
399 400

	to.sin_family      = AF_INET;
Mike Hibler's avatar
Mike Hibler committed
401
	to.sin_port        = sndportnum;
402 403
	to.sin_addr.s_addr = mcastaddr.s_addr;

404
	delays = 0;
405 406 407 408 409 410 411 412 413
	while (sendto(sock, (void *)p, len, 0, 
		      (struct sockaddr *)&to, sizeof(to)) < 0) {
		if (errno != ENOBUFS)
			pfatal("PacketSend(sendto)");

		/*
		 * ENOBUFS means we ran out of mbufs. Okay to sleep a bit
		 * to let things drain.
		 */
414 415
		delays++;
		fsleep(nobufdelay);
416 417
	}

418 419 420
	DOSTAT(nonetbufs += delays);
	if (resends != 0)
		*resends = delays;
421 422 423 424 425 426 427 428
}

/*
 * 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.
 */
429
void
430 431 432 433 434 435 436 437
PacketReply(Packet_t *p)
{
	struct sockaddr_in to;
	int		   len;

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

	to.sin_family      = AF_INET;
Mike Hibler's avatar
Mike Hibler committed
438
	to.sin_port        = sndportnum;
439
	to.sin_addr.s_addr = p->hdr.srcip;
440
	p->hdr.srcip       = myipaddr.s_addr;
441 442 443 444 445 446 447 448 449 450

	while (sendto(sock, (void *)p, len, 0, 
		      (struct sockaddr *)&to, sizeof(to)) < 0) {
		if (errno != ENOBUFS)
			pfatal("PacketSend(sendto)");

		/*
		 * ENOBUFS means we ran out of mbufs. Okay to sleep a bit
		 * to let things drain.
		 */
451 452
		DOSTAT(nonetbufs++);
		fsleep(nobufdelay);
453
	}
454
}
455

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
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 ||
474
		    p->msg.block.block >= MAXCHUNKSIZE)
475 476 477 478 479 480 481 482
			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 ||
483
		    p->msg.request.block >= MAXCHUNKSIZE ||
484
		    p->msg.request.count < 0 ||
485
		    p->msg.request.block+p->msg.request.count > MAXCHUNKSIZE)
486 487
			return 0;
		break;
Mike Hibler's avatar
Mike Hibler committed
488 489 490 491 492 493 494
	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;
495 496 497 498
	case PKTSUBTYPE_JOIN:
		if (p->hdr.datalen < sizeof(p->msg.join))
			return 0;
		break;
499 500 501 502
	case PKTSUBTYPE_JOIN2:
		if (p->hdr.datalen < sizeof(p->msg.join2))
			return 0;
		break;
503 504 505 506 507 508 509 510 511 512 513 514 515
	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;
	default:
		return 0;
	}

	return 1;
516
}
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

#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.
627 628 629 630 631
 * '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.
 *
632 633 634 635
 * 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.
 *
636 637
 * On success, return non-zero with 'reply' filled in with the server's
 * response IN HOST ORDER.  On failure returns zero.
638 639
 */
int
640 641
ClientNetFindServer(in_addr_t sip, in_port_t sport,
		    in_addr_t hostip, char *imageid,
642 643
		    int method, int askonly, int timeout,
		    GetReply *reply, struct in_addr *myip)
644 645 646
{
	struct sockaddr_in name;
	MasterMsg_t msg;
647
	int msock, len;
648 649 650 651 652
	
	if ((msock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
		perror("Could not allocate socket for master server");
		return 0;
	}
653 654
	if (sport == 0)
		sport = MS_PORTNUM;
655 656

	name.sin_family = AF_INET;
657 658
	name.sin_addr.s_addr = htonl(sip);
	name.sin_port = htons(sport);
659 660 661 662 663 664
	if (connect(msock, (struct sockaddr *)&name, sizeof(name)) < 0) {
		perror("Connecting to master server");
		close(msock);
		return 0;
	}

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
	/*
	 * 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;
	}

681
	memset(&msg, 0, sizeof msg);
682 683 684
	strncpy((char *)msg.hdr.version, MS_MSGVERS_1,
		sizeof(msg.hdr.version));
	msg.hdr.type = htonl(MS_MSGTYPE_GETREQUEST);
685
	msg.body.getrequest.hostip = htonl(hostip);
686
	if (askonly) {
687 688 689
		msg.body.getrequest.status = 1;
		msg.body.getrequest.methods = MS_METHOD_ANY;
	} else {
690
		msg.body.getrequest.methods = method;
691 692 693 694 695
	}
	len = strlen(imageid);
	if (len > MS_MAXIDLEN)
		len = MS_MAXIDLEN;
	msg.body.getrequest.idlen = htons(len);
696
	strncpy((char *)msg.body.getrequest.imageid, imageid, MS_MAXIDLEN);
697

698
	len = sizeof msg.hdr + sizeof msg.body.getrequest;
699 700 701 702 703 704
	if (!MsgSend(msock, &msg, len, timeout)) {
		close(msock);
		return 0;
	}

	memset(&msg, 0, sizeof msg);
705
	len = sizeof msg.hdr + sizeof msg.body.getreply;
706 707 708 709
	if (!MsgReceive(msock, &msg, len, timeout)) {
		close(msock);
		return 0;
	}
Mike Hibler's avatar
Mike Hibler committed
710
	close(msock);
711

712 713
	if (strncmp((char *)msg.hdr.version, MS_MSGVERS_1,
		    sizeof(msg.hdr.version))) {
714 715 716
		fprintf(stderr, "Got incorrect version from master server\n");
		return 0;
	}
717
	if (ntohl(msg.hdr.type) != MS_MSGTYPE_GETREPLY) {
718 719 720 721
		fprintf(stderr, "Got incorrect reply from master server\n");
		return 0;
	}

722 723 724 725 726
	/*
	 * Convert the reply info to host order
	 */
	*reply = msg.body.getreply;
	reply->error = ntohs(reply->error);
727
	reply->servaddr = ntohl(reply->servaddr);
728 729 730
	reply->addr = ntohl(reply->addr);
	reply->port = ntohs(reply->port);
	reply->sigtype = ntohs(reply->sigtype);
731 732 733 734 735
	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);
736
	return 1;
737 738 739
}
#endif