/* * 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 */ #include "config.h" #define PORT TINYPORT #include <sys/param.h> #include <sys/socket.h> #include <sys/time.h> #include <sys/resource.h> #include <netinet/in.h> #include <netdb.h> #include <signal.h> #include <stdio.h> #include <ctype.h> /* extern char **environ; */ char *Name; /* name of this program for error messages */ char msg[2048]; int main(argc, argv) char *argv[]; { int s, ns, foo; static struct sockaddr_in sin = { AF_INET }; char *host, *inet_ntoa(); char tmp[80]; long ct; setbuf(stdout,NULL); 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[2048] = '\0'; signal(SIGHUP, SIG_IGN); /* get socket, bind port to it */ s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) { perror("announce: socket");; exit(1); } if (bind(s, &sin, sizeof sin) < 0) { perror("bind"); exit(1); } 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); } if (listen(s, 1) < 0) { /* start listening on port */ perror("announce: listen"); exit(1); } foo = sizeof sin; for(;;) { /* loop forever, accepting requests & printing msg */ ns = accept(s, &sin, &foo); if (ns < 0) { perror("announce: accept"); exit(1); } host = inet_ntoa(sin.sin_addr.s_addr); ct = time(0L); fprintf(stderr, "CONNECTION made from %s at %s", host, ctime(&ct)); write(ns, msg, strlen(msg)); sleep(5); close(ns); } }/* main */