home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume26 / faucet / part01 / hose.c < prev    next >
C/C++ Source or Header  |  1993-01-29  |  5KB  |  197 lines

  1. /*
  2.  
  3.     hose.c, part of
  4.     faucet and hose: network pipe utilities
  5.     Copyright (C) 1992 Robert Forsman
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.     */
  22.  
  23. static char info[] = "hose: a network utility for sockets\nWritten 1992 by Robert Forsman <thoth@ufl.edu>\n";
  24. #include    <stdio.h>
  25. #include    <fcntl.h>
  26. #ifdef hpux
  27. #include    <sgtty.h>
  28. #endif
  29. #include    <sys/errno.h>
  30. #include    <sys/param.h>
  31. #include    <sys/file.h>
  32. #include    <sys/ioctl.h>
  33. #include    <sys/socket.h>
  34. #include    <sys/un.h>
  35. #include    <netinet/in.h>
  36. #include    <netdb.h>
  37.  
  38. #define    DOSTDOUT    (1<<0)
  39. #define    DOSTDIN        (1<<1)
  40. #define    DOSTDERR    (1<<2)
  41. #define    DOUNIX        (1<<3)
  42. int    doflags=0;
  43. char    *localport=NULL;
  44. char    *programname;
  45. extern int    errno;
  46. extern char *sys_errlist[];
  47.  
  48.  
  49. int name_to_inet_port();
  50.  
  51.  
  52. int setup_socket(hostname,portname)
  53. char    *hostname;
  54. char    *portname;
  55.  
  56. {
  57.   int    sock;
  58.   struct sockaddr    server;
  59.   int length;
  60.   
  61.   sock = socket((doflags&DOUNIX)?AF_UNIX:AF_INET, SOCK_STREAM, 0);
  62.   if (sock <0) {
  63.     perror("opening stream socket");
  64.     exit(1);
  65.   }
  66.   
  67.   length = sizeof(server);
  68.   
  69.   if (localport != NULL &&
  70.       !bindlocal(sock, localport, (doflags&DOUNIX)?AF_UNIX:AF_INET) ) {
  71.     fprintf(stderr,"%s: error binding stream socket %s (%s)",
  72.         programname,localport,sys_errlist[errno]);
  73.     exit(1);
  74.   }
  75.   
  76.   if (doflags&DOUNIX) {
  77.     /* ignore the hostname parameter */
  78.     ((struct sockaddr_un*)&server)->sun_family = AF_UNIX;
  79.     strcpy( ((struct sockaddr_un*)&server)->sun_path, portname);
  80.   } else {
  81.     struct sockaddr_in *svr=(struct sockaddr_in *)&server;
  82.     
  83.     ((struct sockaddr_in*)&server)->sin_family = AF_INET;
  84.     
  85.     if (!convert_hostname(hostname, &svr->sin_addr)) {
  86.       fprintf(stderr, "%s: could not translate %s to a host address\n",
  87.           programname, hostname);
  88.       exit(1);
  89.     }
  90.     
  91.     svr->sin_port = name_to_inet_port(portname);
  92.     if (svr->sin_port==0) {
  93.       fprintf(stderr,"%s: bogus port number %s\n",programname,portname);
  94.       exit(1);
  95.     }
  96.   }
  97.   
  98.   if (connect(sock,(struct sockaddr*)&server,sizeof(server)) < 0) {
  99.     perror("connecting");
  100.     exit(1);
  101.   }
  102.   
  103.   return(sock);
  104. }
  105.  
  106.  
  107.  
  108.  
  109. main (argc,argv)
  110.      int argc;
  111.      char ** argv;
  112.      
  113. {
  114.   int    rval,length;
  115.   
  116.   programname=argv[0];
  117.   
  118.   if (argc<4) {
  119.     fprintf(stderr,"Usage : %s <hostname> <port> <command> (in|out|err)+ [unix] [localport <port>]}\n",programname);
  120.     exit(1);
  121.   }
  122.   if (strcmp(argv[1],"-unix-")==0 || strcmp(programname,"uhose")==0 )
  123.     doflags |= DOUNIX;
  124.   for (length=4; length<argc; length++) {
  125.     if (strcmp(argv[length],"in")==0)
  126.       doflags |= DOSTDIN;
  127.     else if (strcmp(argv[length],"out")==0)
  128.       doflags |= DOSTDOUT;
  129.     else if (strcmp(argv[length],"err")==0)
  130.       doflags |= DOSTDERR;
  131.     else if (strcmp(argv[length],"unix")==0)
  132.       doflags |= DOUNIX;
  133.     else if (strcmp(argv[length],"localport")==0) {
  134.       if (length+1<argc)
  135.     localport=argv[++length];
  136.       else
  137.     fprintf(stderr,"%s: localport requires port name or number after.\n",
  138.         programname);
  139.     } else
  140.       fprintf(stderr,"%s: Bogus extra command line flag \"%s\".\n",
  141.           programname,argv[length]);
  142.   }
  143.   
  144.   if ( ! (doflags&(DOSTDIN|DOSTDERR|DOSTDOUT)) ) {
  145.     fprintf(stderr,"%s: Need at least one {in|out|err}.\n",programname);
  146.     exit(1);
  147.   }
  148.   
  149.   rval = setup_socket(argv[1],argv[2]);
  150.   
  151.   if (doflags&DOUNIX && localport!=NULL)
  152.     unlink(localport);
  153.   
  154. #if 0
  155.   if (!fork()) {
  156.     int sparefd;
  157.     char *s;
  158.     
  159.     sparefd = dup(fileno(stderr));
  160.     ioctl(sparefd,FIOCLEX,NULL);
  161.     
  162.     if (!(doflags & DOSTDIN))
  163.       dup2(rval,fileno(stdin));
  164.     if (!(doflags & DOSTDOUT))
  165.       dup2(rval,fileno(stdin));
  166.     if (!(doflags & DOSTDERR))
  167.       dup2(rval,fileno(stderr));
  168.     close(rval);
  169.     
  170.     execl("/bin/cat",NULL);
  171.     s ="exec failed\n";
  172.     write(sparefd,s,strlen(s));
  173.     exit(1);
  174.   }
  175. #endif
  176.   {
  177.     int sparefd;
  178.     char *s;
  179.     
  180.     sparefd = dup(fileno(stderr));
  181.     ioctl(sparefd,FIOCLEX,NULL);
  182.     
  183.     if (doflags & DOSTDIN)
  184.       dup2(rval,fileno(stdin));
  185.     if (doflags & DOSTDOUT)
  186.       dup2(rval,fileno(stdout));
  187.     if (doflags & DOSTDERR)
  188.       dup2(rval,fileno(stderr));
  189.     close(rval);
  190.     
  191.     execl("/bin/csh","csh","-c",argv[3],NULL);
  192.     s ="exec failed\n";
  193.     write(sparefd,s,strlen(s));
  194.     exit(1);
  195.   }
  196. }
  197.