home *** CD-ROM | disk | FTP | other *** search
- /*
-
- hose.c, part of
- faucet and hose: network pipe utilities
- Copyright (C) 1992 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 by Robert Forsman <thoth@ufl.edu>\n";
- #include <stdio.h>
- #include <fcntl.h>
- #ifdef hpux
- #include <sgtty.h>
- #endif
- #include <signal.h>
- #include <sys/errno.h>
- #include <sys/param.h>
- #include <sys/file.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/un.h>
- #include <netinet/in.h>
- #include <netdb.h>
-
- #define DOSTDOUT (1<<0)
- #define DOSTDIN (1<<1)
- #define DOSTDERR (1<<2)
- #define DOUNIX (1<<3)
- #define DOJAM (1<<4)
- int doflags=0;
- char *localport=NULL;
- char *programname;
- extern int errno;
- extern char *sys_errlist[];
-
-
- int name_to_inet_port();
-
-
- int setup_socket(hostname,portname)
- char *hostname;
- char *portname;
-
- {
- int sock;
- struct sockaddr server;
- int length;
-
- sock = socket((doflags&DOUNIX)?AF_UNIX:AF_INET, SOCK_STREAM, 0);
- if (sock <0) {
- perror("opening stream socket");
- exit(1);
- }
-
- length = sizeof(server);
-
- if (localport != NULL &&
- !bindlocal(sock, localport, (doflags&DOUNIX)?AF_UNIX:AF_INET) ) {
- fprintf(stderr,"%s: error binding stream socket %s (%s)",
- programname,localport,sys_errlist[errno]);
- exit(1);
- }
-
- if (doflags&DOUNIX) {
- /* ignore the hostname parameter */
- ((struct sockaddr_un*)&server)->sun_family = AF_UNIX;
- strcpy( ((struct sockaddr_un*)&server)->sun_path, portname);
- } else {
- struct sockaddr_in *svr=(struct sockaddr_in *)&server;
-
- ((struct sockaddr_in*)&server)->sin_family = AF_INET;
-
- if (!convert_hostname(hostname, &svr->sin_addr)) {
- fprintf(stderr, "%s: could not translate %s to a host address\n",
- programname, hostname);
- exit(1);
- }
-
- svr->sin_port = name_to_inet_port(portname);
- if (svr->sin_port==0) {
- fprintf(stderr,"%s: bogus port number %s\n",programname,portname);
- exit(1);
- }
- }
-
- if (connect(sock,(struct sockaddr*)&server,sizeof(server)) < 0) {
- perror("connecting");
- exit(1);
- }
-
- return(sock);
- }
-
-
- void endjam()
- {
- doflags &= ~DOJAM;
- }
-
-
- main (argc,argv)
- int argc;
- char ** argv;
-
- {
- int rval,length;
- int jampipe[2];
-
- programname=argv[0];
-
- if (argc<4) {
- fprintf(stderr,"Usage : %s <hostname> <port> <command> (in|out|err)+ [unix] [localport <port>]}\n",programname);
- exit(1);
- }
- if (strcmp(argv[1],"-unix-")==0 || strcmp(programname,"uhose")==0 )
- doflags |= DOUNIX;
- for (length=4; length<argc; length++) {
- if (strcmp(argv[length],"in")==0)
- doflags |= DOSTDIN;
- else if (strcmp(argv[length],"out")==0)
- doflags |= DOSTDOUT;
- else if (strcmp(argv[length],"err")==0)
- doflags |= DOSTDERR;
- else if (strcmp(argv[length],"unix")==0)
- doflags |= DOUNIX;
- else if (strcmp(argv[length],"jam")==0)
- doflags |= DOJAM;
- else if (strcmp(argv[length],"localport")==0) {
- if (length+1<argc)
- localport=argv[++length];
- else
- fprintf(stderr,"%s: localport requires port name or number after.\n",
- programname);
- } else
- fprintf(stderr,"%s: Bogus extra command line flag \"%s\".\n",
- programname,argv[length]);
- }
-
- if ( ! (doflags&(DOSTDIN|DOSTDERR|DOSTDOUT)) ) {
- fprintf(stderr,"%s: Need at least one {in|out|err}.\n",programname);
- exit(1);
- }
-
- /* this wierd setup is to flood a socket with connections */
- if (doflags&DOJAM) {
- signal(SIGCHLD, endjam);
- if (0>pipe(jampipe)) {
- perror("opening jampipe");
- exit(1);
- }
- }
-
- 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(1);
- }
- }
-
- if (doflags&DOJAM)
- close (jampipe[0]);
-
- rval = setup_socket(argv[1],argv[2]);
-
- if (doflags&DOUNIX && localport!=NULL)
- unlink(localport);
-
- #if 0
- if (!fork()) {
- int sparefd;
- char *s;
-
- sparefd = dup(fileno(stderr));
- ioctl(sparefd,FIOCLEX,NULL);
-
- if (!(doflags & DOSTDIN))
- dup2(rval,fileno(stdin));
- if (!(doflags & DOSTDOUT))
- dup2(rval,fileno(stdin));
- if (!(doflags & DOSTDERR))
- dup2(rval,fileno(stderr));
- close(rval);
-
- execl("/bin/cat",NULL);
- s ="exec failed\n";
- write(sparefd,s,strlen(s));
- exit(1);
- }
- #endif
- {
- int sparefd;
- char *s;
-
- sparefd = dup(fileno(stderr));
- ioctl(sparefd,FIOCLEX,NULL);
-
- if (doflags & DOSTDIN)
- dup2(rval,fileno(stdin));
- if (doflags & DOSTDOUT)
- dup2(rval,fileno(stdout));
- if (doflags & DOSTDERR)
- dup2(rval,fileno(stderr));
- close(rval);
-
- if (doflags&DOJAM)
- close (jampipe[1]);
-
- execl("/bin/csh","csh","-c",argv[3],NULL);
- s ="exec failed\n";
- write(sparefd,s,strlen(s));
- exit(1);
- }
- }
-