/* o * announce - sits listening on a port, and whenever anyone connects * announces a message and disconnects them * * Usage: announce [port] < message_file * * Author: Lawrence Brown Aug 90 * * Bits of code are adapted from the Berkeley telnetd sources */ #define PORT 2323 #include #include #include #include #include #include #include #include #include extern char **environ; extern int errno; char *Name; /* name of this program for error messages */ char msg[2048]; int main(int argc, char **argv) { int s, ns, foo; static struct sockaddr_in sin = {AF_INET}; char *host, *inet_ntoa(); char tmp[80]; long ct; 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((long int *)0); fprintf(stderr, "CONNECTION made from %s at %s", host, ctime(&ct)); write(ns, msg, strlen(msg)); sleep(5); close(ns); } } /* main */