home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / d / d-linux.zip / dm-dist / sign.c < prev    next >
C/C++ Source or Header  |  1991-03-01  |  3KB  |  231 lines

  1. /* Present a message on a port */
  2.  
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/wait.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <sys/time.h>
  11. #include <fcntl.h>
  12. #include <ctype.h>
  13. #include <string.h>
  14.  
  15. void watch(int port, char *text);
  16. void wave(int sock, char *text);
  17. int new_connection(int s);
  18. int init_socket(int port);
  19. int write_to_descriptor(int desc, char *txt);
  20. void nonblock(int s);
  21.  
  22.  
  23.  
  24.  
  25. main(int argc, char **argv)
  26. {
  27.     int port;
  28.     char txt[2048], buf[83];
  29.     FILE *fl;
  30.  
  31.     if (argc != 3)
  32.     {
  33.         fputs("Usage: sign (<filename> | - ) <port #>\n", stderr);
  34.         exit();
  35.     }
  36.     if (!strcmp(argv[1], "-"))
  37.     {
  38.         fl = stdin;
  39.         puts("Input text (terminate with ^D)");
  40.     }
  41.     else if (!(fl = fopen(argv[1], "r")))
  42.     {
  43.         perror(argv[1]);
  44.         exit();
  45.     }
  46.     for (;;)
  47.     {
  48.         fgets(buf, 81, fl);
  49.         if (feof(fl))
  50.             break;
  51.         strcat(buf, "\r");
  52.         if (strlen(buf) + strlen(txt) > 2048)
  53.         {
  54.             fputs("String too long\n", stderr);
  55.             exit();
  56.         }
  57.         strcat(txt, buf);
  58.     }
  59.     if ((port = atoi(argv[2])) <= 1024)
  60.     {
  61.         fputs("Illegal port #\n", stderr);
  62.         exit();
  63.     }
  64.     watch(port, txt);
  65. }
  66.  
  67.  
  68.  
  69.  
  70. void watch(int port, char *text)
  71. {
  72.     int mother;
  73.     fd_set input_set;
  74.  
  75.     mother = init_socket(port);
  76.  
  77.     FD_ZERO(&input_set);
  78.     for(;;)
  79.     {
  80.         FD_SET(mother, &input_set);
  81.         if (select(64, &input_set, 0, 0, 0) < 0)
  82.         {
  83.             perror("select");
  84.             exit();
  85.         }
  86.         if (FD_ISSET(mother, &input_set))
  87.             wave(mother, text);
  88.     }
  89. }
  90.  
  91.  
  92.  
  93. void wave(int sock, char *text)
  94. {
  95.     int s;
  96.  
  97.     if ((s = new_connection(sock)) < 0)
  98.         return;
  99.  
  100.     write_to_descriptor(s, text);
  101.     sleep(6);
  102.     close(s);
  103. }
  104.  
  105.  
  106.  
  107. int new_connection(int s)
  108. {
  109.     struct sockaddr_in isa;
  110.     /* struct sockaddr peer; */
  111.     int i;
  112.     int t;
  113.     char buf[100];
  114.  
  115.     i = sizeof(isa);
  116.     getsockname(s, &isa, &i);
  117.  
  118.  
  119.     if ((t = accept(s, &isa, &i)) < 0)
  120.     {
  121.         perror("Accept");
  122.         return(-1);
  123.     }
  124.     nonblock(t);
  125.  
  126.     /*
  127.  
  128.     i = sizeof(peer);
  129.     if (!getpeername(t, &peer, &i))
  130.     {
  131.         *(peer.sa_data + 49) = '\0';
  132.         sprintf(buf, "New connection from addr %s\n", peer.sa_data);
  133.         log(buf);
  134.     }
  135.  
  136.     */
  137.  
  138.     return(t);
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. int init_socket(int port)
  147. {
  148.     int s;
  149.     char *opt;
  150.     char hostname[1024];
  151.     struct sockaddr_in sa;
  152.     struct hostent *hp;
  153.     struct linger ld;
  154.  
  155.     bzero(&sa, sizeof(struct sockaddr_in));
  156.     gethostname(hostname, 1023);
  157.     hp = gethostbyname(hostname);
  158.     if (hp == NULL)
  159.     {
  160.         perror("gethostbyname");
  161.         exit();
  162.     }
  163.     sa.sin_family = hp->h_addrtype;
  164.     sa.sin_port    = htons(port);
  165.     s = socket(AF_INET, SOCK_STREAM, 0);
  166.     if (s < 0) 
  167.     {
  168.         perror("Init-socket");
  169.         exit();
  170.      }
  171.     if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR,
  172.         (char *) &opt, sizeof (opt)) < 0) 
  173.     {
  174.         perror ("setsockopt REUSEADDR");
  175.         exit ();
  176.     }
  177.  
  178.     ld.l_onoff = 1;
  179.     ld.l_linger = 1000;
  180.     if (setsockopt(s, SOL_SOCKET, SO_LINGER, &ld, sizeof(ld)) < 0)
  181.     {
  182.         perror("setsockopt LINGER");
  183.         exit();
  184.     }
  185.     if (bind(s, &sa, sizeof(sa), 0) < 0)
  186.     {
  187.         perror("bind");
  188.         close(s);
  189.         exit();
  190.     }
  191.     listen(s, 5);
  192.     return(s);
  193. }
  194.  
  195.  
  196.  
  197.  
  198. int write_to_descriptor(int desc, char *txt)
  199. {
  200.     int sofar, thisround, total;
  201.  
  202.     total = strlen(txt);
  203.     sofar = 0;
  204.  
  205.     do
  206.     {
  207.         thisround = write(desc, txt + sofar, total - sofar);
  208.         if (thisround < 0)
  209.         {
  210.             perror("Write to socket");
  211.             return(-1);
  212.         }
  213.         sofar += thisround;
  214.     } 
  215.     while (sofar < total);
  216.  
  217.     return(0);
  218. }
  219.  
  220.  
  221.  
  222.  
  223. void nonblock(int s)
  224. {
  225.     if (fcntl(s, F_SETFL, FNDELAY) == -1)
  226.     {
  227.         perror("Noblock");
  228.         exit();
  229.     }
  230. }
  231.