mux/src/tools/announce.c

Go to the documentation of this file.
00001 /*
00002  *      announce - sits listening on a port, and whenever anyone connects
00003  *                 announces a message and disconnects them
00004  *
00005  *      Usage:  announce [port] < message_file
00006  *
00007  *      Author: Lawrence Brown <lpb@cs.adfa.oz.au>      Aug 90
00008  *
00009  *      Bits of code are adapted from the Berkeley telnetd sources
00010  */
00011 
00012 #define PORT    2860
00013 
00014 #include <sys/param.h>
00015 #include <sys/socket.h>
00016 #include <sys/types.h>
00017 #include <sys/time.h>
00018 #include <sys/resource.h>
00019 #include <netinet/in.h>
00020 #include <netdb.h>
00021 #include <signal.h>
00022 #include <stdio.h>
00023 #include <ctype.h>
00024 
00025 char   *Name;        // name of this program for error messages.
00026 char    msg[8192];
00027 size_t  nmsg;
00028 
00029 int main(int argc, char *argv[])
00030 {
00031     int    s;
00032     int    ns;
00033     int    foo;
00034     static struct sockaddr_in sin = {AF_INET};
00035     char   *host;
00036     char   *inet_ntoa();
00037     long   ct;
00038     int    ch;
00039     char   *p;
00040     int    opt;
00041 
00042     // Save program name for error messages.
00043     //
00044     Name = argv[0];
00045 
00046     // Assume PORT, but let command-line override.
00047     //
00048     sin.sin_port = htons((u_short) PORT);
00049     argc--;
00050     argv++;
00051     if (argc > 0)
00052     {
00053         // unless specified on command-line.
00054         //
00055         sin.sin_port = atoi(*argv);
00056         sin.sin_port = htons((u_short) sin.sin_port);
00057     }
00058 
00059     // Read in message and translate CRLF/NL to something reasonable.
00060     //
00061     p = msg;
00062     while (  (ch = getchar()) != EOF
00063           && p + 2 < msg + sizeof(msg))
00064     {
00065         if (ch != '\r')
00066         {
00067             if (ch == '\n')
00068             {
00069                 *p++ = '\r';
00070             }
00071             *p++ = ch;
00072         }
00073     }
00074     *p = '\0';
00075     nmsg = p - msg;
00076 
00077     signal(SIGHUP, SIG_IGN);
00078     s = socket(AF_INET, SOCK_STREAM, 0);
00079     if (s < 0)
00080     {
00081         perror("announce: socket");
00082         exit(1);
00083     }
00084     opt = 1;
00085     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0)
00086     {   
00087         perror("setsockopt");
00088     }
00089     if (bind(s, (struct sockaddr *)&sin, sizeof sin) < 0)
00090     {
00091         perror("bind");
00092         exit(1);
00093     }
00094     if ((foo = fork()) != 0)
00095     {
00096         fprintf(stderr, "announce: pid %d running on port %d\n", foo,
00097                 ntohs((u_short) sin.sin_port));
00098         _exit(0);
00099     }
00100     else
00101     {
00102         setpriority(PRIO_PROCESS, getpid(), 10);
00103     }
00104     if (listen(s, 1) < 0)
00105     {
00106         // start listening on port.
00107         //
00108         perror("announce: listen");
00109         _exit(1);
00110     }
00111     foo = sizeof sin;
00112     for (;;)
00113     {
00114         // loop forever, accepting requests & printing msg.
00115         //
00116         ns = accept(s, (struct sockaddr *)&sin, &foo);
00117         if (ns < 0)
00118         {
00119             perror("announce: accept");
00120             _exit(1);
00121         }
00122         host = inet_ntoa(sin.sin_addr);
00123         ct = time(0L);
00124         fprintf(stderr, "CONNECTION made from %s at %s",
00125                 host, ctime(&ct));
00126         write(ns, msg, nmsg);
00127         sleep(5);
00128         close(ns);
00129     }
00130 }
00131 

Generated on Mon May 28 04:40:12 2007 for MUX by  doxygen 1.4.7