1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 102 103 104 105 106 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 152 153 154 155 156 157 158 159 160 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 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 513 514 515 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 627 628 629 630 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
|
/*
$Id: hose.c,v 1.25 1998/10/28 15:52:23 thoth Exp $, part of
faucet and hose: network pipe utilities
Copyright (C) 1992-98 Robert Forsman
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
static char info[] = "hose: a network utility for sockets\nWritten 1992-98 by Robert Forsman <thoth@purplefrog.com>\n$Id: hose.c,v 1.25 1998/10/28 15:52:23 thoth Exp $\n";
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef hpux
#include <sgtty.h>
#endif
#include <signal.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/file.h>
#ifdef USE_IOCTL
#include <sys/ioctl.h>
/* find the FIOCLEX ioctl */
#ifdef linux
#include <sys/termios.h>
#else /* defined(linux) */
#ifdef sco
#include <sgtty.h>
#else /* defined(sco) */
#include <sys/filio.h>
#endif /* defined(sco) */
#endif /* defined(linux) */
#else /* defined(USE_IOCTL) */
#include <fcntl.h>
#endif /* defined(USE_IOCTL) */
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#ifndef NOUNIXSOCKETS
#include <sys/un.h>
#endif
#include <netinet/in.h>
#include <sys/time.h>
#include <netdb.h>
#include <sys/wait.h>
#ifdef AIX
#include <sys/select.h>
#endif
#include "memmove.h"
#include "common.h"
#ifndef NOUNIXSOCKETS
#define DOUNIX (1<<0)
#endif
#define DOVERBOSE (1<<1)
#define DOJAM (1<<2)
#define DOSLAVE (1<<3)
#define DONETSLAVE_CT (1<<4)
#define DONETSLAVE_CF (1<<5)
#define DONETSLAVE (DONETSLAVE_CT | DONETSLAVE_CF)
#define EXITCODE_CONNECTION 127
#define EXITCODE_ARGS 126
#define EXITCODE_FAILED_SYSCALL 125
#define EXITCODE_PIPE 124
long doflags=0;
int retry=0; /* how many times to retry after ECONNREFUSED */
unsigned delay=5; /* how long to wait between each retry */
int shutdn=0; /* should we fork, wait and shutdown? */
char *localport=NULL; /* local port name */
char *localaddr=NULL; /* local internet address */
extern int errno;
int name_to_inet_port();
void usage () {
fprintf(stderr,"Usage : %s <hostname> <port> (--in|--out|--err|--fd N|--slave|--netslave|--netslave1|--netslave2)+ [--verb(|ose)] [--unix] [--localport <port>] [--localhost <inet-addr>] [--retry n] [--delay n] [--shutdown [r|w][a]] [--noreuseaddr] -[i][o][e][#3[,4[,5...]]][s][v][q][u] [-p <local port>] [-h <local host>] <command> [ args ... ]\n",progname);
}
int setup_socket(hostname,portname, reuseaddr)
char *hostname;
char *portname;
int reuseaddr;
{
int sock = -1;
struct sockaddr_storage ** addresses=0;
#ifdef DOUNIX
struct sockaddr_un unix_addr;
#endif
struct sockaddr_storage inet_addr;
int num_addresses;
int length;
int tries;
int cstat;
int port = 0;
#ifdef DOUNIX
if (doflags&DOUNIX) {
unix_addr.sun_family = AF_UNIX;
strncpy( unix_addr.sun_path, portname, sizeof(unix_addr.sun_path));
unix_addr.sun_path[sizeof(unix_addr.sun_path) - 1] = 0;
length = sizeof(struct sockaddr_un);
num_addresses = 1;
} else
#endif
{
if (0==(addresses = convert_hostname(hostname, &num_addresses))) {
fprintf(stderr, "%s: could not translate %s to a host address\n",
progname, hostname);
exit(EXITCODE_CONNECTION);
}
if( (port = name_to_inet_port(portname)) == 0 ) {
fprintf(stderr,"%s: bogus port number %s\n",progname,portname);
exit(EXITCODE_CONNECTION);
}
}
for (tries = 0; retry<0 || tries <= retry; tries++) {
int j;
int family;
/* multi-homed hosts are a little tricky */
for ( j=0; j<num_addresses; j++) {
#ifdef DOUNIX
if (doflags&DOUNIX)
family = AF_UNIX;
else
#endif
family = addresses[j]->ss_family;
sock = socket(family, SOCK_STREAM, 0);
if (sock <0) {
perror("opening stream socket");
exit(EXITCODE_CONNECTION);
}
if ((localport) && !bindlocal(sock, localport, localaddr,
family, reuseaddr) ) {
fprintf(stderr,"%s: error binding stream socket %s (%s)\n",
progname,localport,strerror(errno));
exit(EXITCODE_CONNECTION);
}
#ifdef DOUNIX
if (!(doflags&DOUNIX))
#endif
{
memcpy(&inet_addr, addresses[j], sizeof(struct sockaddr_storage));
set_port(&inet_addr, port);
}
if (doflags&DOVERBOSE) {
fprintf(stderr, "%s: attempting to connect to ", progname);
#ifdef DOUNIX
if (doflags&DOUNIX) {
fprintf(stderr, "UNIX %s\n", unix_addr.sun_path);
} else
#endif
{
printhost(stderr, (struct sockaddr *) &inet_addr);
fprintf(stderr, " port %d\n", get_port(&inet_addr));
}
}
length = (inet_addr.ss_family == AF_INET6)
? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
#ifdef DOUNIX
cstat=connect(sock, (doflags&DOUNIX) ? ((struct sockaddr*)&unix_addr)
: ((struct sockaddr*)&inet_addr),
length);
#else
cstat=connect(sock, ((struct sockaddr*)&inet_addr), length);
#endif
if (cstat==0)
break; /* success */
if ( errno == ECONNREFUSED || errno == ETIMEDOUT) {
close(sock);
sock = -1;
continue;
} else {
perror("connecting");
exit(EXITCODE_CONNECTION);
}
}
if (tries<retry) {
/* failed, retry all addresses after a delay */
if (doflags&DOVERBOSE) {
fprintf(stderr, "sleeping before retry...");
fflush(stdout);
}
sleep(delay);
if (doflags&DOVERBOSE)
fprintf(stderr, "\n");
}
}
if (sock < 0) {
fprintf(stderr, "%s: Retries exhausted, failing connect to %s port %s\n",
progname, hostname, portname);
exit(EXITCODE_CONNECTION);
}
return(sock);
}
/*
copy bytes from stdin to the socket and
copy bytes from the socket to stdout.
*/
void copyio(sock, aggressive_close)
int sock;
int aggressive_close;
{
fd_set readfds, writefds;
#define BSIZE 4096
char tosockbuf[BSIZE], fromsockbuf[BSIZE];
int tosocklen, fromsocklen;
int rval;
int exitval = 0;
tosocklen = fromsocklen = 0;
while (tosocklen>=0 || fromsocklen>=0) {
/********************/
FD_ZERO(&readfds);
FD_ZERO(&writefds);
if (tosocklen>=0) {
if (tosocklen==0) {
FD_SET(0, &readfds);
} else {
FD_SET(sock, &writefds);
}
}
if (fromsocklen>=0) {
if (fromsocklen==0) {
FD_SET(sock, &readfds);
} else {
FD_SET(1, &writefds);
}
}
if ( ( (aggressive_close&DONETSLAVE_CF)
&& fromsocklen < 0
&& tosocklen<1 )
||
( (aggressive_close&DONETSLAVE_CT)
&& tosocklen < 0
&& fromsocklen<1 )
) {
/* One direction is closed and the other's buffer is empty.
Exit. */
close(sock);
break;
}
if ( ( (aggressive_close&DONETSLAVE_CF)
&& fromsocklen < 0 )
||
( (aggressive_close&DONETSLAVE_CT)
&& tosocklen < 0 )
) {
/* One direction is closed but the other's buffer is not empty.
Don't accept any more input while we flush the buffer. */
FD_ZERO(&readfds);
}
/********************/
rval=select(sock+1, &readfds, &writefds,
(fd_set*)0, (struct timeval*)0);
/********************/
if (rval<0) {
if (errno != EINTR) {
perror("during copyio() select(2)");
exit(EXITCODE_PIPE);
}
} else if (rval==0) {
break;
}
/********************/
if (FD_ISSET(1, &writefds)) {
rval = write(1, fromsockbuf, fromsocklen);
if (rval<0) {
perror("during copyio() write(2)(1)");
exitval = EXITCODE_PIPE;
fromsocklen = -1;
shutdown(sock, 0);
} else {
memmove(fromsockbuf, fromsockbuf+rval, fromsocklen-rval);
fromsocklen -= rval;
}
}
if (FD_ISSET(sock, &writefds)) {
rval = write(sock, tosockbuf, tosocklen);
if (rval<0) {
perror("during copyio() write(2)(sock)");
exitval = EXITCODE_PIPE;
tosocklen = -1;
shutdown(sock, 1);
} else {
memmove(tosockbuf, tosockbuf+rval, tosocklen-rval);
tosocklen -= rval;
}
}
if (FD_ISSET(0, &readfds)) {
tosocklen = read(0, tosockbuf, BSIZE);
if (tosocklen<0) {
perror("during copyio() read(2)(0)");
exitval = EXITCODE_PIPE;
tosocklen = -1;
} else if (tosocklen==0) {
tosocklen = -1;
if (aggressive_close == 0)
shutdown(sock, 1);
}
}
if (FD_ISSET(sock, &readfds)) {
fromsocklen = read(sock, fromsockbuf, BSIZE);
if (fromsocklen<0) {
perror("during copyio() read(2)(0)");
exitval = EXITCODE_PIPE;
fromsocklen = -1;
} else if (fromsocklen==0) {
fromsocklen = -1;
if (aggressive_close == 0)
shutdown(sock, 0);
}
}
}
exit(exitval);
}
void endjam()
{
doflags &= ~DOJAM;
}
/**********************************************************************/
/* since we have flag processing for long and short, we do the same thing
in two separate pieces of code. The non-trivial ones we encapsulate
in a small function */
void flag_in()
{
add_fd(0);
if (how_shutdown == 0) /* make sure we can read from the socket */
how_shutdown = -1;
else if (how_shutdown==-2)
how_shutdown = 1;
}
void flag_out()
{
add_fd(1);
if (how_shutdown == 1) /* make sure we can write to the socket */
how_shutdown = -1;
else if (how_shutdown==-2)
how_shutdown = 0;
}
void flag_err()
{
add_fd(2);
if (how_shutdown == 1) /* make sure we can write to the socket */
how_shutdown = -1;
else if (how_shutdown==-2)
how_shutdown = 0;
}
int flag_scan_comma_fds(s)
char *s;
{
int rval=0;
while (1) {
int fd;
int n;
if (1 != sscanf(s, "%i%n", &fd, &n)) {
fprintf(stderr, "%s: parse error in file descriptor list at 's'\n", progname);
usage();
exit(EXITCODE_ARGS);
}
add_fd(fd);
rval +=n;
s += n;
if (*s == ',') {
rval++;
s++;
} else {
break;
}
}
return rval;
}
/**********************************************************************/
int main (argc,argv)
int argc;
char ** argv;
{
int sock,i;
int jampipe[2];
char **cmd;
int reuseaddr =1;
set_progname(argv[0]);
if (argc<4) {
usage();
exit(EXITCODE_ARGS);
}
if (strcmp(argv[1],"-unix-")==0 || strcmp(progname,"uhose")==0 ){
#ifdef DOUNIX
doflags |= DOUNIX;
#else
fprintf(stderr, "%s: unix-domain sockets are not supported in this binary.\n", progname);
exit(EXITCODE_ARGS);
#endif
}
for (i=3; i<argc; i++) {
char *arg;
if (argv[i][0]!='-')
break;
arg = argv[i]+1;
if (*arg == '-') arg++;
if (strcmp(arg,"in")==0) {
flag_in();
} else if (strcmp(arg,"out")==0) {
flag_out();
} else if (strcmp(arg,"err")==0) {
flag_err();
} else if (strncmp(arg,"fd",2)==0) {
int fd;
if (arg[2])
fd = atoi(arg+2);
else if (i+1<argc) {
fd = atoi(argv[++i]);
} else {
fprintf(stderr, "%s: --fd requires numeric file descriptor argument.\n", progname);
usage();
exit(EXITCODE_ARGS);
}
add_fd(fd);
how_shutdown = -1;
} else if (strcmp(arg,"slave")==0) {
if (doflags & DOSLAVE) {
fprintf(stderr, "%s: only one --slave or --netslave permitted.\n",
progname);
usage();
exit(EXITCODE_ARGS);
}
doflags |= DOSLAVE;
} else if (strcmp(arg,"netslave")==0) {
if (doflags & DOSLAVE) {
fprintf(stderr, "%s: only one --slave or --netslave permitted.\n",
progname);
usage();
exit(EXITCODE_ARGS);
}
doflags |= DOSLAVE|DONETSLAVE;
} else if (strcmp(arg,"netslave1")==0) {
if (doflags & DOSLAVE) {
fprintf(stderr, "%s: only one --slave or --netslave permitted.\n",
progname);
usage();
exit(EXITCODE_ARGS);
}
doflags |= DOSLAVE|DONETSLAVE_CT;
} else if (strcmp(arg,"netslave2")==0) {
if (doflags & DOSLAVE) {
fprintf(stderr, "%s: only one --slave or --netslave permitted.\n",
progname);
usage();
exit(EXITCODE_ARGS);
}
doflags |= DOSLAVE|DONETSLAVE_CF;
} else if (strcmp(arg,"unix")==0) {
#ifdef DOUNIX
doflags |= DOUNIX;
#else
fprintf(stderr, "%s: unix-domain sockets are not supported in this binary.\n", progname);
exit(EXITCODE_ARGS);
#endif
} else if (strcmp(arg,"verbose")==0 ||
strcmp(arg,"verb")==0)
doflags |= DOVERBOSE;
else if (strcmp(arg,"jam")==0)
doflags |= DOJAM;
else if (strcmp(arg,"localport")==0) {
if (i+1<argc)
localport=argv[++i];
else {
fprintf(stderr,
"%s: -localport requires port name or number argument.\n",
progname);
usage();
exit(EXITCODE_ARGS);
}
} else if (strcmp(arg,"localhost")==0) {
if (i+1<argc)
localaddr=argv[++i];
else {
fprintf(stderr,
"%s: -localhost requires internet name or number.\n",
progname);
usage();
exit(EXITCODE_ARGS);
}
} else if (strcmp(arg,"retry")==0) {
if (i+1<argc)
retry=atoi(argv[++i]);
else
fprintf(stderr,"%s: retry requires count argument.\n",
progname);
} else if (strcmp(arg,"delay")==0) {
if (i+1<argc)
delay=atoi(argv[++i]);
else {
fprintf(stderr,"%s: delay requires time argument in seconds.\n",
progname);
usage();
exit(EXITCODE_ARGS);
}
} else if (strcmp(arg,"shutdown")==0) {
int err=1;
if (i+1<argc) {
arg = argv[++i];
err=0;
if (0==strcmp(arg, "r")) {
how_shutdown = 1;
} else if (0==strcmp(arg, "w")) {
how_shutdown = 0;
} else if (0==strcmp(arg, "ra")) {
how_shutdown = 1;
shutdn = 1;
} else if (0==strcmp(arg, "wa")) {
how_shutdown = 0;
shutdn = 1;
} else if (0==strcmp(arg, "a")) {
shutdn = 1;
} else {
err = 1;
}
}
if (err) {
fprintf(stderr,"%s: shutdown requires \"r\", \"w\" \"ra\", \"wa\", or \"a\" string.\n",
progname);
usage();
exit(EXITCODE_ARGS);
}
} else if (strcmp(arg,"noreuseaddr")==0) {
reuseaddr=0;
} else {
int j;
for (j=0; arg[j]; j++) {
switch (arg[j]) {
case 'i': flag_in(); break;
case 'o': flag_out(); break;
case 'e': flag_err(); break;
case '#':
j += flag_scan_comma_fds(arg+j+1);
break;
case 's':
if (doflags & DOSLAVE) {
fprintf(stderr, "%s: only one --slave or --netslave permitted.\n", progname);
usage();
exit(EXITCODE_ARGS);
}
doflags |= DOSLAVE;
break;
case 'v': doflags |= DOVERBOSE; break;
case 'q': doflags &= ~DOVERBOSE; break;
case 'u':
#ifdef DOUNIX
doflags |= DOUNIX;
#else
fprintf(stderr, "%s: unix-domain sockets are not supported in this binary.\n", progname);
exit(EXITCODE_ARGS);
#endif
break;
case 'p':
if (i+1<argc)
localport=argv[++i];
else
fprintf(stderr,
"%s: localport requires port name or number.\n",
progname);
break;
case 'h':
if (i+1<argc)
localaddr=argv[++i];
else
fprintf(stderr,
"%s: localhost requires host name or number.\n",
progname);
break;
default:
fprintf(stderr,
"%s: Unrecognized flag '%c' in argument -%s.\n",
progname, arg[j], arg);
usage();
exit(EXITCODE_ARGS);
}
}
}
}
cmd = argv+i;
if (doflags&DOVERBOSE) {
emit_version("hose", 1992);
}
if ( nfds==0 && !(doflags&DOSLAVE) ) {
fprintf(stderr,"%s: Need at least one {in|out|err|fd #|slave}.\n",progname);
usage();
exit(EXITCODE_ARGS);
}
if (doflags&DOSLAVE) {
if (*cmd) {
fprintf(stderr, "%s: you must not specify a subcommand (%s) when using \nthe -slave or -netslave option.\n", progname, *cmd);
usage();
exit (EXITCODE_ARGS);
} else if (nfds>0) {
fprintf(stderr, "%s: --in, --out, --err, and --fd are mutually exclusive \nwith --slave and --netslave.\n", progname);
}
} else {
if (!*cmd) {
fprintf(stderr, "%s: No subcommand specified.\n", progname);
usage();
exit (EXITCODE_ARGS);
}
}
/* this wierd setup is to flood a socket with connections */
if (doflags&DOJAM) {
signal(SIGCHLD, endjam);
if (0>pipe(jampipe)) {
perror("opening jampipe");
exit(EXITCODE_ARGS);
}
}
fflush(stdout);
fflush(stderr);
while ( (doflags & DOJAM) && fork() ) {
char ch;
close (jampipe[1]);
while (1==read(jampipe[0], &ch, 1))
;
close (jampipe[0]);
jampipe[0] = -1;
if (0>pipe(jampipe)) {
perror("opening jampipe");
exit(EXITCODE_FAILED_SYSCALL);
}
}
if (doflags&DOJAM)
close (jampipe[0]);
reserve_fds(0);
sock = setup_socket(argv[1],argv[2], reuseaddr);
#ifdef DOUNIX
if (doflags&DOUNIX && localport!=NULL)
unlink(localport);
#endif
if (doflags &DOSLAVE) {
copyio(sock, doflags & DONETSLAVE );
}
fflush(stdout);
fflush(stderr);
/* if we're to shutdown(2) the socket when the subprocess exits we
need to fork */
i = shutdn ? fork() : 0;
if (i) {
/* we are supposed to shutdown(2) the socket and we are the parent */
int status;
int pid;
pid = wait(&status);
if (pid != -1 && i!=pid)
fprintf(stderr, "Strange, wait returned a child I don't know about. I'm an unwed father!\n");
shutdown(sock, 2); /* shut the socket down nicely? */
close(sock);
exit( (status&0xff) ? EXITCODE_FAILED_SYSCALL : ((status>>8)&0xff));
} else {
int sparefd;
char *s;
sparefd = dup(fileno(stderr));
#ifdef USE_IOCTL
ioctl(sparefd,FIOCLEX,NULL);
#else
fcntl(sparefd,F_SETFD,FD_CLOEXEC);
#endif
dup_n(sock); /* dup the socket onto all the chosen file descriptors */
close(sock);
if (doflags&DOJAM)
close (jampipe[1]);
execvp(cmd[0], cmd);
s ="exec failed for ";
write(sparefd,s,strlen(s));
write(sparefd,cmd[0],strlen(cmd[0]));
write(sparefd,"\n",1);
exit(EXITCODE_FAILED_SYSCALL);
}
/* NOTREACHED */
}
|