00001
00002
00003
00004
00005
00006
00007
00008
00009
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;
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
00043
00044 Name = argv[0];
00045
00046
00047
00048 sin.sin_port = htons((u_short) PORT);
00049 argc--;
00050 argv++;
00051 if (argc > 0)
00052 {
00053
00054
00055 sin.sin_port = atoi(*argv);
00056 sin.sin_port = htons((u_short) sin.sin_port);
00057 }
00058
00059
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
00107
00108 perror("announce: listen");
00109 _exit(1);
00110 }
00111 foo = sizeof sin;
00112 for (;;)
00113 {
00114
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