home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / netpipes / part01 / hose.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-07  |  5.5 KB  |  234 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    <signal.h>
  30. #include    <sys/errno.h>
  31. #include    <sys/param.h>
  32. #include    <sys/file.h>
  33. #include    <sys/ioctl.h>
  34. #include    <sys/socket.h>
  35. #include    <sys/un.h>
  36. #include    <netinet/in.h>
  37. #include    <netdb.h>
  38.  
  39. #define    DOSTDOUT    (1<<0)
  40. #define    DOSTDIN        (1<<1)
  41. #define    DOSTDERR    (1<<2)
  42. #define    DOUNIX        (1<<3)
  43. #define    DOJAM        (1<<4)
  44. int    doflags=0;
  45. char    *localport=NULL;
  46. char    *programname;
  47. extern int    errno;
  48. extern char *sys_errlist[];
  49.  
  50.  
  51. int name_to_inet_port();
  52.  
  53.  
  54. int setup_socket(hostname,portname)
  55. char    *hostname;
  56. char    *portname;
  57.  
  58. {
  59.   int    sock;
  60.   struct sockaddr    server;
  61.   int length;
  62.   
  63.   sock = socket((doflags&DOUNIX)?AF_UNIX:AF_INET, SOCK_STREAM, 0);
  64.   if (sock <0) {
  65.     perror("opening stream socket");
  66.     exit(1);
  67.   }
  68.   
  69.   length = sizeof(server);
  70.   
  71.   if (localport != NULL &&
  72.       !bindlocal(sock, localport, (doflags&DOUNIX)?AF_UNIX:AF_INET) ) {
  73.     fprintf(stderr,"%s: error binding stream socket %s (%s)",
  74.         programname,localport,sys_errlist[errno]);
  75.     exit(1);
  76.   }
  77.   
  78.   if (doflags&DOUNIX) {
  79.     /* ignore the hostname parameter */
  80.     ((struct sockaddr_un*)&server)->sun_family = AF_UNIX;
  81.     strcpy( ((struct sockaddr_un*)&server)->sun_path, portname);
  82.   } else {
  83.     struct sockaddr_in *svr=(struct sockaddr_in *)&server;
  84.     
  85.     ((struct sockaddr_in*)&server)->sin_family = AF_INET;
  86.     
  87.     if (!convert_hostname(hostname, &svr->sin_addr)) {
  88.       fprintf(stderr, "%s: could not translate %s to a host address\n",
  89.           programname, hostname);
  90.       exit(1);
  91.     }
  92.     
  93.     svr->sin_port = name_to_inet_port(portname);
  94.     if (svr->sin_port==0) {
  95.       fprintf(stderr,"%s: bogus port number %s\n",programname,portname);
  96.       exit(1);
  97.     }
  98.   }
  99.   
  100.   if (connect(sock,(struct sockaddr*)&server,sizeof(server)) < 0) {
  101.     perror("connecting");
  102.     exit(1);
  103.   }
  104.   
  105.   return(sock);
  106. }
  107.  
  108.  
  109. void endjam()
  110. {
  111.   doflags &= ~DOJAM;
  112. }
  113.  
  114.  
  115. main (argc,argv)
  116.      int argc;
  117.      char ** argv;
  118.      
  119. {
  120.   int    rval,length;
  121.   int    jampipe[2];
  122.   
  123.   programname=argv[0];
  124.   
  125.   if (argc<4) {
  126.     fprintf(stderr,"Usage : %s <hostname> <port> <command> (in|out|err)+ [unix] [localport <port>]}\n",programname);
  127.     exit(1);
  128.   }
  129.   if (strcmp(argv[1],"-unix-")==0 || strcmp(programname,"uhose")==0 )
  130.     doflags |= DOUNIX;
  131.   for (length=4; length<argc; length++) {
  132.     if (strcmp(argv[length],"in")==0)
  133.       doflags |= DOSTDIN;
  134.     else if (strcmp(argv[length],"out")==0)
  135.       doflags |= DOSTDOUT;
  136.     else if (strcmp(argv[length],"err")==0)
  137.       doflags |= DOSTDERR;
  138.     else if (strcmp(argv[length],"unix")==0)
  139.       doflags |= DOUNIX;
  140.     else if (strcmp(argv[length],"jam")==0)
  141.       doflags |= DOJAM;
  142.     else if (strcmp(argv[length],"localport")==0) {
  143.       if (length+1<argc)
  144.     localport=argv[++length];
  145.       else
  146.     fprintf(stderr,"%s: localport requires port name or number after.\n",
  147.         programname);
  148.     } else
  149.       fprintf(stderr,"%s: Bogus extra command line flag \"%s\".\n",
  150.           programname,argv[length]);
  151.   }
  152.   
  153.   if ( ! (doflags&(DOSTDIN|DOSTDERR|DOSTDOUT)) ) {
  154.     fprintf(stderr,"%s: Need at least one {in|out|err}.\n",programname);
  155.     exit(1);
  156.   }
  157.  
  158.   /* this wierd setup is to flood a socket with connections */
  159.   if (doflags&DOJAM) {
  160.     signal(SIGCHLD, endjam);
  161.     if (0>pipe(jampipe)) {
  162.       perror("opening jampipe");
  163.       exit(1);
  164.     }
  165.   }
  166.  
  167.   while ( (doflags & DOJAM) && fork() ) {
  168.     char    ch;
  169.     close (jampipe[1]);
  170.     while (1==read(jampipe[0], &ch, 1))
  171.       ;
  172.     close (jampipe[0]);
  173.     jampipe[0] = -1;
  174.     if (0>pipe(jampipe)) {
  175.       perror("opening jampipe");
  176.       exit(1);
  177.     }
  178.   }
  179.  
  180.   if (doflags&DOJAM)
  181.     close (jampipe[0]);
  182.  
  183.   rval = setup_socket(argv[1],argv[2]);
  184.   
  185.   if (doflags&DOUNIX && localport!=NULL)
  186.     unlink(localport);
  187.   
  188. #if 0
  189.   if (!fork()) {
  190.     int sparefd;
  191.     char *s;
  192.     
  193.     sparefd = dup(fileno(stderr));
  194.     ioctl(sparefd,FIOCLEX,NULL);
  195.     
  196.     if (!(doflags & DOSTDIN))
  197.       dup2(rval,fileno(stdin));
  198.     if (!(doflags & DOSTDOUT))
  199.       dup2(rval,fileno(stdin));
  200.     if (!(doflags & DOSTDERR))
  201.       dup2(rval,fileno(stderr));
  202.     close(rval);
  203.     
  204.     execl("/bin/cat",NULL);
  205.     s ="exec failed\n";
  206.     write(sparefd,s,strlen(s));
  207.     exit(1);
  208.   }
  209. #endif
  210.   {
  211.     int sparefd;
  212.     char *s;
  213.     
  214.     sparefd = dup(fileno(stderr));
  215.     ioctl(sparefd,FIOCLEX,NULL);
  216.     
  217.     if (doflags & DOSTDIN)
  218.       dup2(rval,fileno(stdin));
  219.     if (doflags & DOSTDOUT)
  220.       dup2(rval,fileno(stdout));
  221.     if (doflags & DOSTDERR)
  222.       dup2(rval,fileno(stderr));
  223.     close(rval);
  224.     
  225.     if (doflags&DOJAM)
  226.       close (jampipe[1]);
  227.  
  228.     execl("/bin/csh","csh","-c",argv[3],NULL);
  229.     s ="exec failed\n";
  230.     write(sparefd,s,strlen(s));
  231.     exit(1);
  232.   }
  233. }
  234.