/* * announce - sits listening on a port, and whenever anyone connects * announces a message and disconnects them * * Usage: announce [port] < message_file * * Author: Lawrence Brown <lpb@cs.adfa.oz.au> Aug 90 * * Bits of code are adapted from the Berkeley telnetd sources */ #define PORT 4201 #include "os.h" extern char **environ; char *Name; /* name of this program for error messages */ char msg[2048]; int main (int argc, char **argv) { SOCKET s, ns; int foo; static struct sockaddr_in sin; char *host; char tmp[80]; long ct; WIN32STARTUP sin.sin_family = AF_INET; Name = argv[0]; /* save name of program for error messages */ sin.sin_port = htons ((u_short) PORT); /* Assume PORT */ argc--, argv++; if (argc > 0) { /* unless specified on command-line */ sin.sin_port = atoi (*argv); sin.sin_port = htons ((u_short) sin.sin_port); } strcpy (msg, ""); strcpy (tmp, ""); while (1) { if ((gets (tmp)) == NULL) break; strcat (tmp, "\r\n"); strcat (msg, tmp); } msg[2047] = '\0'; #ifndef WIN32 signal (SIGHUP, SIG_IGN); /* get socket, bind port to it */ #endif s = socket (AF_INET, SOCK_STREAM, 0); if (s == INVALID_SOCKET) { perror ("announce: socket");; WIN32CLEANUP exit (1); } if (bind (s, (struct sockaddr *) &sin, sizeof sin) < 0) { perror ("bind"); WIN32CLEANUP exit (1); } #ifndef WIN32 if ((foo = fork ()) != 0) { fprintf (stderr, "announce: pid %d running on port %d\n", foo, ntohs ((u_short) sin.sin_port)); exit (0); } else { setpriority (PRIO_PROCESS, getpid (), 10); } #endif if (listen (s, 1) == SOCKET_ERROR) { /* start listening on port */ perror ("announce: listen"); WIN32CLEANUP exit (1); } foo = sizeof sin; for (;;) { /* loop forever, accepting requests & printing msg */ ns = accept (s, (struct sockaddr *) &sin, &foo); if (ns == INVALID_SOCKET) { perror ("announce: accept"); exit (1); } host = inet_ntoa (sin.sin_addr); ct = time (0L); fprintf (stderr, "CONNECTION made from %s at %s", host, ctime (&ct)); send (ns, msg, strlen (msg), 0); #ifdef WIN32 Sleep (5000); #else sleep (5); #endif close (ns); } WIN32CLEANUP return 0; } /* main */