tmcc.c 15.6 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
4 5
 * All rights reserved.
 */
6 7 8 9 10 11 12 13 14 15 16

#ifdef _WIN32
/*Windows version should be linked to: WS2_32*/
/*g++ -Wall -o tmcc tmcc.c -D_WIN32 -lWS2_32*/
#include <winsock2.h>
#include <unistd.h>
#include <getopt.h>
typedef int socklen_t;
#define ECONNREFUSED WSAECONNREFUSED
#endif

17
#include <sys/types.h>
18
#ifndef _WIN32
19 20
#include <sys/socket.h>
#include <netinet/in.h>
21
#endif
22 23
#include <sys/un.h>
#include <sys/fcntl.h>
24 25 26 27 28 29
#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
30 31
#include <stdlib.h>
#include <string.h>
32 33 34
#include <sys/time.h>
#include <time.h>
#include <assert.h>
35
#include <sys/types.h>
36
#ifndef _WIN32
37 38 39
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
40
#endif
41
#include "decls.h"
42
#include "ssl.h"
43 44 45
#ifndef STANDALONE
#include "config.h"
#endif
46
#ifndef _WIN32
47
#undef  BOSSNODE
48 49 50
#ifndef BOSSNODE
#include <resolv.h>
#endif
51
#endif
52

53 54
#ifndef KEYFILE
#define KEYFILE		"/etc/emulab.pkey"
55
#endif
56
#define UNIXSOCKVAR	"TMCCUNIXPATH"
57

58 59 60 61 62
/*
 * We search a couple of dirs for the bossnode file.
 */
static char *bossnodedirs[] = {
	"/etc/testbed",
63
	"/etc/emulab",
64 65 66 67 68 69
	"/etc/rc.d/testbed",
	"/usr/local/etc/testbed",
	"/usr/local/etc/emulab",
	0
};

70 71 72 73 74 75 76
/*
 * Need to try several ports cause of firewalling. 
 */
static int portlist[] = {
	TBSERVER_PORT,
	TBSERVER_PORT2,
};
77 78 79 80 81 82
/* Locals */
static int	numports = sizeof(portlist)/sizeof(int);
static int	debug = 0;
static char    *logfile = NULL;

/* Forward decls */
83
static int	getbossnode(char **, int *);
84 85 86 87 88
static int	doudp(char *, int, struct in_addr, int);
static int	dotcp(char *, int, struct in_addr);
static int	dounix(char *, int, char *);
static void	beproxy(char *, struct in_addr, char *);
static int	dooutput(int, char *, int);
89

90
char *usagestr = 
91 92
 "usage: tmcc [options] <command>\n"
 " -d		   Turn on debugging\n"
93
 " -s server	   Specify a tmcd server to connect to\n"
94
 " -p portnum	   Specify a port number to connect to\n"
95
 " -v versnum	   Specify a version number for tmcd\n"
96
 " -n vnodeid	   Specify the vnodeid\n"
97
 " -k keyfile	   Specify the private keyfile\n"
98
 " -u		   Use UDP instead of TCP\n"
99
 " -l path	   Use named unix domain socket instead of TCP\n"
100
 " -t timeout	   Timeout waiting for the controller.\n"
101
 " -x path	   Be a tmcc proxy, using the named unix domain socket\n"
102 103 104 105 106 107 108 109 110
 "\n";

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

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/*
 * We cannot let remote nodes hang, but they can be slow. If we get connected
 * we give it an extra timeout, and if we make any progress at all, keep
 * giving it extra timeouts.
 */
static int connected = 0;
static int progress  = 0;
static int waitfor   = 0;

static void
tooktoolong()
{
	static int	lastprogress = 0;

	/* If we made progress, keep going (reset timer too) */
	if (connected && progress > lastprogress) {
		lastprogress = progress;
		alarm(waitfor);
		return;
	}

	fprintf(stderr, "Timed out cause there was no progress!\n");
	exit(-1);
}
135

Mike Hibler's avatar
Mike Hibler committed
136
int
137 138
main(int argc, char **argv)
{
139
	int			n, ch;
140 141
	struct hostent		*he;
	struct in_addr		serverip;
142
	char			buf[MYBUFSIZE], *bp;
143
	FILE			*fp;
144 145 146 147 148 149 150 151
	int			useudp    = 0;
	char			*unixpath = NULL;
	char			*bossnode = NULL;
	int			version   = CURRENT_VERSION;
	char			*vnodeid  = NULL;
	char			*keyfile  = NULL;
	char			*privkey  = NULL;
	char			*proxypath= NULL;
152 153 154
#ifdef _WIN32
        WSADATA wsaData;
#endif
155 156

	while ((ch = getopt(argc, argv, "v:s:p:un:t:k:x:l:do:")) != -1)
157
		switch(ch) {
158 159 160
		case 'd':
			debug++;
			break;
161
		case 'p':
162 163
			portlist[0] = atoi(optarg);
			numports    = 1;
164
			break;
165 166 167
		case 's':
			bossnode = optarg;
			break;
168 169 170
		case 'n':
			vnodeid = optarg;
			break;
171 172 173
		case 'k':
			keyfile = optarg;
			break;
174 175 176
		case 'v':
			version = atoi(optarg);
			break;
177 178 179
		case 't':
			waitfor = atoi(optarg);
			break;
180 181 182
		case 'u':
			useudp = 1;
			break;
183 184 185 186 187 188 189 190 191
		case 'x':
			proxypath = optarg;
			break;
		case 'l':
			unixpath  = optarg;
			break;
		case 'o':
			logfile  = optarg;
			break;
192 193 194 195
		default:
			usage();
		}

196
	argv += optind;
197
	argc -= optind;
198
	if (!proxypath && (argc < 1 || argc > 5)) {
199
		usage();
200
	}
201 202
	if (unixpath && proxypath)
		usage();
203

204
	/*
205 206 207
	 * Allow for environment to specify a unix socket, which is a
	 * connection to a tmcc proxy. Note that a specific -l option
	 * overrides the environment variable.
208
	 */
209 210
	if (!unixpath && !proxypath && (bp = getenv(UNIXSOCKVAR)))
		unixpath = bp;
211

212 213 214 215
	if (unixpath && (keyfile || bossnode)) {
		fprintf(stderr,
			"You may not use the -k or -s with the -l option\n");
		usage();
216
	}
217

218 219 220 221 222 223 224 225
#ifdef _WIN32
        /*Windows requires us to start up the version of the network API that we want*/
	if(WSAStartup( MAKEWORD( 2, 2 ), &wsaData )) {
	        fprintf(stderr,"WSAStartup failed\n");
		exit(1);
	}
#endif

226 227 228 229 230 231 232 233 234 235
	if (!bossnode) {
		int	port = 0;
		
		getbossnode(&bossnode, &port);
		/* In other words, do not override command line port spec! */
		if (port && numports > 1) {
			portlist[0] = port;
			numports    = 1;
		}
	}
236
	
237 238 239 240 241 242 243 244
	he = gethostbyname(bossnode);
	if (he)
		memcpy((char *)&serverip, he->h_addr, he->h_length);
	else {
		fprintf(stderr, "gethostbyname(%s) failed\n", bossnode); 
		exit(1);
	}

245 246 247 248 249 250 251 252
	/*
	 * Handle built-in "bossinfo" command
	 */
	if (!proxypath && (strcmp(argv[0], "bossinfo") == 0)) {
		printf("%s %s\n", bossnode, inet_ntoa(serverip));
		exit(0);
	}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	/*
	 * Grab the key. Its not an error if we do not find it.
	 */
	if (keyfile) {
		/* Well, if one was specifed it has to exist. */
		if (access(keyfile, R_OK) < 0) {
			perror("keyfile"); 
			exit(1);
		}
	}
	else
		keyfile = KEYFILE;

	if ((fp = fopen(keyfile, "r")) != NULL) {
	    if (fgets(buf, sizeof(buf), fp)) {
		if ((bp = strchr(buf, '\n')))
		    *bp = (char) NULL;
		privkey = strdup(buf);
	    }
	    fclose(fp);
	}

275 276 277 278 279 280 281 282 283 284 285 286 287
	/*
	 * Build up the command request string.
	 */
	buf[0] = '\0';
	
	/* Tack on vnodeid */
	if (vnodeid) {
		sprintf(&buf[strlen(buf)], "VNODEID=%s ", vnodeid);
	}

	/* Tack on privkey */
	if (privkey) {
		sprintf(&buf[strlen(buf)], "PRIVKEY=%s ", privkey);
288 289
	}

290
	/*
291 292
	 * If specified udp with unixpath, then pass through a UDP flag
	 * to the proxy, which will do what it thinks is appropriate.
293
	 */
294 295 296 297 298 299 300 301 302 303
	if (useudp && unixpath) {
		sprintf(&buf[strlen(buf)], "USEUDP=1 ");
		useudp = 0;
	}

	/*
	 * In proxy mode ...
	 */
	if (proxypath) {
		beproxy(proxypath, serverip, buf);
304 305 306
		exit(0);
	}

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	/* Tack on version number */
	sprintf(&buf[strlen(buf)], "VERSION=%d ", version);

	/*
	 * Since we've gone through a getopt() pass, argv[0] is now the
	 * first argument
	 */
	n = strlen(buf);
	while (argc && n < sizeof(buf)) {
		n += snprintf(&buf[n], sizeof(buf) - (n + 1), "%s ", argv[0]);
		argc--;
		argv++;
	}
	if (n >= sizeof(buf)) {
		fprintf(stderr, "Command too large!\n");
		exit(-1);
	}
	buf[n] = '\0';

	/*
	 * When a timeout is requested, just let the signal kill us.
	 */
	if (waitfor) {
330
		signal(SIGALRM, tooktoolong);
331
		alarm(waitfor);
332
	}
333 334 335 336 337 338 339 340 341 342 343 344 345

	if (useudp)
		n = doudp(buf, fileno(stdout), serverip, portlist[0]);
	else if (unixpath)
		n = dounix(buf, fileno(stdout), unixpath);
	else
		n = dotcp(buf, fileno(stdout), serverip);
	exit(n);
}

/*
 * Find the bossnode name if one was not specified on the command line.
 */
346 347
static int
getbossnode(char **bossnode, int *portp)
348 349
{
#ifdef BOSSNODE
350 351
	*bossnode = strdup(BOSSNODE);
	return 0;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
#else
	struct hostent	*he;
	FILE		*fp;
	char		buf[BUFSIZ], **cp = bossnodedirs, *bp;

	/*
	 * Search for the file.
	 */
	while (*cp) {
		sprintf(buf, "%s/%s", *cp, BOSSNODE_FILENAME);

		if (access(buf, R_OK) == 0)
			break;
		cp++;
	}
	if (*cp) {
		if ((fp = fopen(buf, "r")) != NULL) {
			if (fgets(buf, sizeof(buf), fp)) {
				if ((bp = strchr(buf, '\n')))
					*bp = (char) NULL;
				fclose(fp);
373 374 375 376 377 378 379 380 381
				/*
				 * Look for port spec
				 */
				if ((bp = strchr(buf, ':'))) {
					*bp++  = NULL;
					*portp = atoi(bp);
				}
				*bossnode = strdup(buf);
				return 0;
382 383 384 385 386
			}
			fclose(fp);
		}
	}
	
387
#ifndef _WIN32
388 389 390 391 392 393
	/*
	 * Nameserver goo 
	 */
	res_init();
	he = gethostbyaddr((char *)&_res.nsaddr.sin_addr,
			   sizeof(struct in_addr), AF_INET);
394 395 396
	if (he && he->h_name) 
		*bossnode = strdup(he->h_name);
	else
397
#endif
398 399
		*bossnode = strdup("UNKNOWN");
	return 0;
400
#endif
401 402 403 404 405 406 407 408 409 410 411 412
}

/*
 * TCP version, which uses ssl if compiled in.
 */
static int
dotcp(char *data, int outfd, struct in_addr serverip)
{
	int			n, sock, cc;
	struct sockaddr_in	name;
	char			*bp, buf[MYBUFSIZE];
	
413 414 415
#ifdef  WITHSSL
	if (tmcd_client_sslinit()) {
		printf("SSL initialization failed!\n");
416
		return -1;
417 418
	}
#endif
419
	while (1) {
420 421 422 423 424
		for (n = 0; n < numports; n++) {
			/* Create socket from which to read. */
			sock = socket(AF_INET, SOCK_STREAM, 0);
			if (sock < 0) {
				perror("creating stream socket:");
425
				return -1;
426
			}
427

428 429 430 431
			/* Create name. */
			name.sin_family = AF_INET;
			name.sin_addr   = serverip;
			name.sin_port   = htons(portlist[n]);
432

433 434 435 436 437 438 439
			if (CONNECT(sock, (struct sockaddr *) &name,
				    sizeof(name)) == 0) {
				goto foundit;
			}
			if (errno != ECONNREFUSED) {
				perror("connecting stream socket");
				CLOSE(sock);
440
				return -1;
441
			}
442
			CLOSE(sock);
443
		}
444 445 446
		if (debug) 
			fprintf(stderr,
				"Connection to TMCD refused. Waiting ...\n");
447
		sleep(10);
448
	}
449
 foundit:
450
	connected = 1;
451

452 453
	n = 1;
	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &n, sizeof(n)) < 0) {
454
		perror("setsockopt SO_KEEPALIVE");
455
		goto bad;
456
	}
457

458
	/*
459
	 * Write the command to the socket and wait for the response.
460
	 */
461 462
	bp = data;
	n  = strlen(data);
463
	while (n) {
464
		if ((cc = WRITE(sock, bp, n)) <= 0) {
465 466
			if (cc < 0) {
				perror("Writing to socket:");
467
				goto bad;
468 469
			}
			fprintf(stderr, "write aborted");
470
			goto bad;
471 472 473 474 475 476
		}
		bp += cc;
		n  -= cc;
	}

	while (1) {
477
		if ((cc = READ(sock, buf, sizeof(buf) - 1)) <= 0) {
478 479
			if (cc < 0) {
				perror("Reading from socket:");
480
				goto bad;
481 482 483
			}
			break;
		}
484
		progress += cc;
485 486
		if (dooutput(outfd, buf, cc) < 0)
			goto bad;
487
	}
488
	CLOSE(sock);
489
	return 0;
490 491
 bad:
	CLOSE(sock);
492
	return -1;
493 494
}

Mike Hibler's avatar
Mike Hibler committed
495 496 497
/*
 * Not very robust, send a single request, read a single reply. 
 */
498 499
static int
doudp(char *data, int outfd, struct in_addr serverip, int portnum)
Mike Hibler's avatar
Mike Hibler committed
500 501 502
{
	int			sock, length, n, cc;
	struct sockaddr_in	name, client;
503
	char			buf[MYBUFSIZE];
Mike Hibler's avatar
Mike Hibler committed
504 505 506 507 508

	/* Create socket from which to read. */
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) {
		perror("creating dgram socket:");
509
		return -1;
Mike Hibler's avatar
Mike Hibler committed
510 511 512 513 514
	}

	/* Create name. */
	name.sin_family = AF_INET;
	name.sin_addr   = serverip;
515
	name.sin_port   = htons(portnum);
Mike Hibler's avatar
Mike Hibler committed
516 517 518 519

	/*
	 * Write the command to the socket and wait for the response
	 */
520 521
	n  = strlen(data);
	cc = sendto(sock, data, n, 0, (struct sockaddr *)&name, sizeof(name));
Mike Hibler's avatar
Mike Hibler committed
522 523 524
	if (cc != n) {
		if (cc < 0) {
			perror("Writing to socket:");
525
			return -1;
Mike Hibler's avatar
Mike Hibler committed
526 527
		}
		fprintf(stderr, "short write (%d != %d)\n", cc, n);
528
		return -1;
Mike Hibler's avatar
Mike Hibler committed
529
	}
530
	connected = 1;
Mike Hibler's avatar
Mike Hibler committed
531

532
	cc = recvfrom(sock, buf, sizeof(buf) - 1, 0,
Mike Hibler's avatar
Mike Hibler committed
533
		      (struct sockaddr *)&client, &length);
534
	progress += cc;
535

Mike Hibler's avatar
Mike Hibler committed
536 537
	if (cc < 0) {
		perror("Reading from socket:");
538
		return -1;
Mike Hibler's avatar
Mike Hibler committed
539
	}
540 541 542 543 544
	close(sock);
	if (dooutput(outfd, buf, cc) < 0)
		return -1;
	return 0;
}
Mike Hibler's avatar
Mike Hibler committed
545

546 547 548 549 550 551
/*
 * Unix domain version.
 */
static int
dounix(char *data, int outfd, char *unixpath)
{
552 553
#if defined(linux) || defined(_WIN32)
	fprintf(stderr, "unix domain socket mode not supported on this platform!\n");
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
	return -1;
#else
	int			n, sock, cc, length;
	struct sockaddr_un	sunaddr;
	char			*bp, buf[MYBUFSIZE];

	sunaddr.sun_family = AF_UNIX;
	strlcpy(sunaddr.sun_path, unixpath, sizeof(sunaddr.sun_path));
	sunaddr.sun_len = length = SUN_LEN(&sunaddr)+1;

	/* Create socket from which to read. */
	sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sock < 0) {
		perror("creating stream socket:");
		return -1;
	}

	if (connect(sock, (struct sockaddr *) &sunaddr, length) < 0) {
		perror("connecting unix domain socket");
		close(sock);
		return -1;
	}
576
	connected = 1;
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

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

	while (1) {
		if ((cc = read(sock, buf, sizeof(buf) - 1)) <= 0) {
			if (cc < 0) {
				perror("Reading from socket:");
				goto bad;
			}
			break;
		}
604
		progress += cc;
605 606 607 608 609 610
		if (dooutput(outfd, buf, cc) < 0)
			goto bad;
	}
	close(sock);
	return 0;
 bad:
Mike Hibler's avatar
Mike Hibler committed
611
	close(sock);
612 613
	return -1;
#endif
Mike Hibler's avatar
Mike Hibler committed
614
}
615 616 617 618 619 620 621 622 623 624 625 626 627 628

/*
 * Be a proxy. Basically, listen for requests from the localhost and
 * forward. At the moment this is strictly for the benefit of jailed
 * environments on FreeBSD, hence we use a unix domain socket to provide
 * the security (the socket is visible to the jail only).
 *
 * Note that only TCP traffic goes through the proxy. The jailed tmcc
 * can speak directly to tmcd over udp since it never returns anything
 * sensitive that way.
 */
static void
beproxy(char *localpath, struct in_addr serverip, char *partial)
{
629 630
#if defined(linux) || defined(_WIN32)
	fprintf(stderr, "proxy mode not supported on this platform!\n");
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 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
	exit(-1);
#else
	int			sock, newsock, cc, length;
	struct sockaddr_un	sunaddr, client;
	char			command[MYBUFSIZE], buf[MYBUFSIZE];
	char			*bp, *cp;
	
	sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sock < 0) {
		perror("creating unix domain socket");
		exit(-1);
	}

	unlink(localpath);
	memset(&sunaddr, 0, sizeof(sunaddr));
	sunaddr.sun_family = AF_UNIX;
	strlcpy(sunaddr.sun_path, localpath, sizeof(sunaddr.sun_path));
	sunaddr.sun_len = SUN_LEN(&sunaddr) + 1;
	if (bind(sock, (struct sockaddr *)&sunaddr, sunaddr.sun_len) < 0) {
		perror("binding unix domain socket");
		exit(-1);
	}
	if (listen(sock, 5) < 0) {
		perror("listen on unix domain socket");
		exit(-1);
	}

	if (logfile) {
		int	fd;

		/*
		 * Detach, but do not daemonize. The caller tracks the
		 * pid and kills us. Needs to be fixed at some point ...
		 */
		if ((fd = open("/dev/null", O_RDONLY, 0)) < 0) {
			perror("Could not open /dev/null");
			exit(-1);
		}
		(void)dup2(fd, STDIN_FILENO);
		if (fd != STDIN_FILENO)
			(void)close(fd);
		
		if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND,0640)) < 0) {
			perror("Could not open logfile");
			exit(-1);
		}
		(void)dup2(fd, STDOUT_FILENO);
		(void)dup2(fd, STDERR_FILENO);
		if (fd > STDERR_FILENO)
			(void)close(fd);
	}

	/*
	 * Wait for TCP connections.
	 */
	while (1) {
		int	rval, useudp = 0;
		
		length  = sizeof(client);
		newsock = accept(sock, (struct sockaddr *)&client, &length);
		if (newsock < 0) {
			perror("accepting Unix domain connection");
			continue;
		}

		/*
		 * Read in the command request.
		 */
		if ((cc = read(newsock, buf, sizeof(buf) - 1)) <= 0) {
			if (cc < 0)
				perror("Reading Unix domain request");
			fprintf(stderr, "Unix domain connection aborted\n");
			close(newsock);
			continue;
		}
		buf[cc] = '\0';

		/*
		 * Do not allow PRIVKEY or VNODE options to be specified
		 * by the proxy user. 
		 */
		strcpy(command, partial);
		bp = cp = buf;
		while ((bp = strsep(&cp, " ")) != NULL) {
			if (strstr(bp, "PRIVKEY=") ||
			    strstr(bp, "VNODE=")) {
				if (debug)
					fprintf(stderr,
						"Ignoring option: %s\n", bp);
				continue;
			}
			if (strstr(bp, "USEUDP=1")) {
				useudp = 1;
				continue;
			}
			strcat(command, bp);
			strcat(command, " ");
		}

		if (debug) {
			fprintf(stderr, "%s\n", command);
			fflush(stderr);
		}

		if (useudp)
			rval = doudp(command, newsock, serverip, portlist[0]);
		else
			rval = dotcp(command, newsock, serverip);
		
		if (rval < 0 && debug) {
			fprintf(stderr, "Request failed!\n");
			fflush(stderr);
		}
		close(newsock);
	}
Mike Hibler's avatar
Mike Hibler committed
746
#endif
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
}

/*
 * Little helper to write tmcd response to output. 
 */
static int
dooutput(int fd, char *buf, int len)
{
	int		cc, count = len;

	if (debug) {
		write(fileno(stderr), buf, len);
	}
	
	while (count) {
		if ((cc = write(fd, buf, count)) <= 0) {
			if (cc < 0) {
				perror("Writing to output stream:");
				return -1;
			}
			fprintf(stderr, "write to socket aborted");
			return -1;
		}
		buf   += cc;
		count -= cc;
	}
	return len;
}