bootinfoclient.c 5.04 KB
Newer Older
1
/*
2
 * Copyright (c) 2000-2012 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/>.
 * 
 * }}}
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <netdb.h>
#include <err.h>
#include <errno.h>
#include "bootwhat.h"
#include "bootinfo.h"
#include "config.h"

int		debug = 0;
static char	*progname;

void
usage()
{
	fprintf(stderr,
47
		"Usage: %s <options> [-d] [-k hostkey] [-s bossnode]\n"
48 49
		"options:\n"
		"-d         - Turn on debugging\n"
50
		"-k         - Specify a widearea hostkey to be used instead of IP for identification\n"
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
		"-s         - Boss Node\n",
		progname);
	exit(-1);
}

int
main(int argc, char **argv)
{
	int			sock, c, cc, pollmode = 0;
	char			*bossnode = BOSSNODE;
	struct sockaddr_in	name, target;
	boot_info_t		boot_info, boot_reply;
	boot_what_t	       *boot_whatp = (boot_what_t *) &boot_reply.data;
	extern char		build_info[];
	struct hostent	       *he;
	struct timeval		timeout;
67
	char* 			hostkey = NULL;
68 69 70

	progname = argv[0];

71
	while ((c = getopt(argc, argv, "dhvk:s:")) != -1) {
72 73 74 75 76 77 78
		switch (c) {
		case 'd':
			debug++;
			break;
		case 's':
			bossnode = optarg;
			break;
79 80 81
		case 'k':
			hostkey = optarg;
			break;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		case 'v':
		    	fprintf(stderr, "%s\n", build_info);
			exit(0);
			break;
		case 'h':
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc)
		usage();

	if (debug)
		printf("%s\n", build_info);

101 102 103 104 105 106
	if((hostkey != NULL) && ((strlen(hostkey) > HOSTKEY_LENGTH) || (strlen(hostkey) == 0))) {
		warn("provided hostkey [%s] is not valid (too big or small)", hostkey);
		exit(1);
	}


107 108 109 110 111 112 113 114 115 116 117 118 119 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 146 147 148 149 150 151
	/* Make sure we can map target */
	if ((he = gethostbyname(bossnode)) == NULL) {
		warn("gethostbyname(%s)", bossnode);
		exit(1);
	}

	bzero(&target, sizeof(target));
	memcpy((char *)&target.sin_addr, he->h_addr, he->h_length);
	target.sin_family = AF_INET;
	target.sin_port   = htons((u_short) BOOTWHAT_DSTPORT);

	/* Create socket */
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) {
		warn("opening datagram socket");
		exit(1);
	}

	/*
	 * We use a socket level timeout instead of polling for data.
	 */
	timeout.tv_sec  = 5;
	timeout.tv_usec = 0;
	
	if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
		       &timeout, sizeof(timeout)) < 0) {
		warn("setsockopt(SOL_SOCKET, SO_RCVTIMEO)");
		exit(1);
	}

	/* Bind our side of it */
	name.sin_family = AF_INET;
	name.sin_addr.s_addr = INADDR_ANY;
	name.sin_port = htons((u_short) BOOTWHAT_SRCPORT);
	if (bind(sock, (struct sockaddr *) &name, sizeof(name))) {
		warn("binding datagram socket");
		exit(1);
	}
	
	/*
	 * Create a bootinfo request packet and send it.
	 */
	bzero(&boot_info, sizeof(boot_info));
	boot_info.version = BIVERSION_CURRENT;
	boot_info.opcode  = BIOPCODE_BOOTWHAT_REQUEST;
152 153 154 155
	if(hostkey != NULL) {
		strcpy(boot_info.data, hostkey);
		boot_info.opcode = BIOPCODE_BOOTWHAT_KEYED_REQUEST;
	}
156 157 158

	while (1) {
		struct sockaddr_in	client;
159
		socklen_t		length;
160 161 162 163 164 165 166 167
		
		cc = sendto(sock, &boot_info, sizeof(boot_info), 0,
			    (struct sockaddr *)&target, sizeof(target));
		if (cc != sizeof(boot_info)) {
			if (cc < 0) {
				warn("Writing to socket");
				goto again;
			}
168
			fprintf(stderr, "short write (%d != %lu)\n",
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
				cc, sizeof(boot_info));
			goto again;
		}
	poll:
		length = sizeof(client);
		bzero(&client, sizeof(client));
		cc = recvfrom(sock, &boot_reply, sizeof(boot_reply), 0,
			      (struct sockaddr *)&client, &length);
		if (cc < 0) {
			if (errno != EWOULDBLOCK)
				warn("Reading from socket");
			if (pollmode)
				goto poll;
			goto again;
		}
		pollmode = 0;

		switch (boot_whatp->type) {
		case BIBOOTWHAT_TYPE_PART:
188 189 190 191
			printf("partition:%d", boot_whatp->what.partition);
			if (boot_whatp->cmdline[0])
				printf(" %s", boot_whatp->cmdline);
			printf("\n");
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
			goto done;
			break;
		case BIBOOTWHAT_TYPE_WAIT:
			if (debug)
				printf("wait: now polling\n");
			pollmode = 1;
			goto poll;
			break;
		case BIBOOTWHAT_TYPE_REBOOT:
			printf("reboot\n");
			goto done;
			break;
		case BIBOOTWHAT_TYPE_AUTO:
			if (debug)
				printf("query: will query again\n");
			goto again;
			break;
		case BIBOOTWHAT_TYPE_MFS:
			printf("mfs:%s\n", boot_whatp->what.mfs);
			goto done;
			break;
		}
	again:
		if (debug)
			fprintf(stderr, "Trying again in 5 seconds\n");
		sleep(5);
	}
 done:
	close(sock);
	exit(0);
}