tmcc.c 5.39 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h>
Mike Hibler's avatar
Mike Hibler committed
10 11
#include <stdlib.h>
#include <string.h>
12 13 14
#include <sys/time.h>
#include <time.h>
#include <assert.h>
15 16 17 18
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
19 20 21 22
#include "decls.h"

void		sigcatcher(int foo);

23 24 25 26 27 28 29 30 31 32 33 34 35
char *usagestr = 
 "usage: tmcc [-p #] <command>\n"
 " -p portnum	   Specify a port number to connect to\n"
 "\n";

void
usage()
{
	fprintf(stderr, usagestr);
	exit(1);
}


Mike Hibler's avatar
Mike Hibler committed
36
int
37 38
main(int argc, char **argv)
{
39
	int			sock, data, n, cc, ch, portnum;
Mike Hibler's avatar
Mike Hibler committed
40 41
	struct sockaddr_in	name;
#if 0
42
	struct timeval		tv;
Mike Hibler's avatar
Mike Hibler committed
43
#endif
44 45
	struct hostent		*he;
	struct in_addr		serverip;
Mike Hibler's avatar
Mike Hibler committed
46
	char			buf[MYBUFSIZE], *bp, *response = "";
47
#ifdef UDP
Mike Hibler's avatar
Mike Hibler committed
48 49 50
	int			useudp = 0;
	void			doudp(int argc, char **argv);
#endif
51

52 53
	portnum = TBSERVER_PORT;

54
	while ((ch = getopt(argc, argv, "p:u")) != -1)
55 56 57 58
		switch(ch) {
		case 'p':
			portnum = atoi(optarg);
			break;
59 60 61 62 63
#ifdef UDP
		case 'u':
			useudp = 1;
			break;
#endif
64 65 66 67 68
		default:
			usage();
		}

	argc -= optind;
69
	if (argc < 1 || argc > 3) {
70
		usage();
71
	}
72
	argv += optind;
73

74 75 76 77 78 79 80 81 82 83
#ifdef UDP
	if (useudp) {
		doudp(argc, argv);
		/*
		 * Never returns.
		 */
		abort();
	}
#endif

84
	he = gethostbyname(BOSSNODE);
85 86 87
	if (he)
		memcpy((char *)&serverip, he->h_addr, he->h_length);
	else {
88
		fprintf(stderr, "gethostbyname(%s) failed\n", BOSSNODE); 
89 90 91
		exit(1);
	}

92 93 94 95 96 97 98 99 100 101
	while (1) {
		/* Create socket from which to read. */
		sock = socket(AF_INET, SOCK_STREAM, 0);
		if (sock < 0) {
			perror("creating stream socket:");
			exit(1);
		}

		/* Create name. */
		name.sin_family = AF_INET;
102
		name.sin_addr   = serverip;
103
		name.sin_port = htons(portnum);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

		if (connect(sock,
			    (struct sockaddr *) &name, sizeof(name)) == 0) {
			break;
		}
		if (errno == ECONNREFUSED) {
			fprintf(stderr, "Connection to TMCD refused "
				"Sleeping a little while ...\n");
			sleep(10);
		}
		else {
			perror("connecting stream socket");
			exit(1);
		}
		close(sock);
	}

	data = 1;
	if (setsockopt(sock, SOL_SOCKET,
		       SO_KEEPALIVE, &data, sizeof(data)) < 0) {
		perror("setsockopt SO_KEEPALIVE");
		close(sock);
		exit(1);
	}
128
#if 0
129 130 131 132
#ifndef linux
	tv.tv_sec  = 30;
	tv.tv_usec = 0;
	if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
133
		perror("setsockopt SO_RCVTIMEO");
134 135 136
		close(sock);
		exit(1);
	}
137
#endif
138
#endif
139 140 141 142
	/*
	 * Since we've gone through a getopt() pass, argv[0] is now the
	 * first argument
	 */
143
	switch(argc) {
144
	case 1:
145
		n = snprintf(buf, sizeof(buf) - 1, "%s", argv[0]);
146 147
		break;
	case 2:
148
		n = snprintf(buf, sizeof(buf) - 1, "%s %s", argv[0], argv[1]);
149 150 151
		break;
	case 3:
		n = snprintf(buf, sizeof(buf) - 1, "%s %s %s",
152
			     argv[0], argv[1], argv[2]);
153 154 155 156 157 158
		break;
	default:
		fprintf(stderr, "Too many command arguments!\n");
		exit(1);
	}
		
159
	if (n >= sizeof(buf)) {
Mike Hibler's avatar
Mike Hibler committed
160
		fprintf(stderr, "Command too large!\n");
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		exit(1);
	}

	/*
	 * Write the command to the socket and wait for the response
	 */
	bp = buf;
	while (n) {
		if ((cc = write(sock, bp, n)) <= 0) {
			if (cc < 0) {
				perror("Writing to socket:");
				exit(1);
			}
			fprintf(stderr, "write aborted");
			exit(1);
		}
		bp += cc;
		n  -= cc;
	}

	while (1) {
		if ((cc = read(sock, buf, sizeof(buf))) <= 0) {
			if (cc < 0) {
				perror("Reading from socket:");
				exit(1);
			}
			break;
		}
		buf[cc] = '\0';
		bp = (char *) malloc(strlen(response) + cc + 1);
		assert(bp);
		strcpy(bp, response);
		strcat(bp, buf);
		response = bp;
	}
	printf("%s", response);

	close(sock);
	exit(0);
}

void
sigcatcher(int foo)
{
}

207
#ifdef UDP
Mike Hibler's avatar
Mike Hibler committed
208 209 210 211 212 213 214 215 216 217
/*
 * Not very robust, send a single request, read a single reply. 
 */
void
doudp(int argc, char **argv)
{
	int			sock, length, n, cc;
	struct sockaddr_in	name, client;
	struct hostent		*he;
	struct in_addr		serverip;
Mike Hibler's avatar
Mike Hibler committed
218
	char			buf[MYBUFSIZE], *bp, *response = "";
Mike Hibler's avatar
Mike Hibler committed
219

220
	he = gethostbyname(BOSSNODE);
Mike Hibler's avatar
Mike Hibler committed
221 222 223
	if (he)
		memcpy((char *)&serverip, he->h_addr, he->h_length);
	else {
224
		fprintf(stderr, "gethostbyname(%s) failed\n", BOSSNODE); 
Mike Hibler's avatar
Mike Hibler committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
		exit(1);
	}

	/* Create socket from which to read. */
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) {
		perror("creating dgram socket:");
		exit(1);
	}

	/* Create name. */
	name.sin_family = AF_INET;
	name.sin_addr   = serverip;
	name.sin_port = htons(TBSERVER_PORT);

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	/*
	 * Since we've gone through a getopt() pass, argv[0] is now the
	 * first argument
	 */
	switch(argc) {
	case 1:
		n = snprintf(buf, sizeof(buf) - 1, "%s", argv[0]);
		break;
	case 2:
		n = snprintf(buf, sizeof(buf) - 1, "%s %s", argv[0], argv[1]);
		break;
	case 3:
		n = snprintf(buf, sizeof(buf) - 1, "%s %s %s",
			     argv[0], argv[1], argv[2]);
		break;
	default:
		fprintf(stderr, "Too many command arguments!\n");
		exit(1);
	}
		
Mike Hibler's avatar
Mike Hibler committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	if (n >= sizeof(buf)) {
		fprintf(stderr, "Command too large!\n");
		exit(1);
	}

	/*
	 * Write the command to the socket and wait for the response
	 */
	cc = sendto(sock, buf, n, 0, (struct sockaddr *)&name, sizeof(name));
	if (cc != n) {
		if (cc < 0) {
			perror("Writing to socket:");
			exit(1);
		}
		fprintf(stderr, "short write (%d != %d)\n", cc, n);
		exit(1);
	}

	cc = recvfrom(sock, buf, sizeof(buf), 0,
		      (struct sockaddr *)&client, &length);
	if (cc < 0) {
		perror("Reading from socket:");
		exit(1);
	}
	buf[cc] = '\0';
	bp = (char *) malloc(strlen(response) + cc + 1);
	assert(bp);
	strcpy(bp, response);
	strcat(bp, buf);
	response = bp;
	printf("%s", response);

	close(sock);
	exit(0);
}
#endif