]> git.ipfire.org Git - thirdparty/squid.git/blob - src/recv-announce.cc
Minor corrections of the formatting of the boilerplate section:
[thirdparty/squid.git] / src / recv-announce.cc
1
2 /*
3 * $Id: recv-announce.cc,v 1.22 2000/06/06 19:34:31 hno Exp $
4 *
5 * DEBUG: section 0 Announcement Server
6 * AUTHOR: Harvest Derived
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * the Regents of the University of California. Please see the
16 * COPYRIGHT file for full details. Squid incorporates software
17 * developed and/or copyrighted by other sources. Please see the
18 * CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include <stdio.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <netdb.h>
45 #include <unistd.h>
46 #include <signal.h>
47
48 #define RECV_BUF_SIZE 8192
49
50 extern void xmemcpy(void *from, void *to, int len);
51
52 /*
53 * This program must be run from inetd. First add something like this
54 * to /etc/services:
55 *
56 * cached_announce 3131/udp # cache announcements
57 *
58 * And then add something like this to /etc/inetd/conf:
59 *
60 * cached_announce dgram udp wait cached /tmp/recv-announce recv-announce /tmp/recv-announce.log
61 *
62 *
63 * A single instance of this process will continue to handle incoming
64 * requests. If it dies, or is killed, inetd should restart it when the
65 * next message arrives.
66 *
67 */
68
69 /*
70 * usage: recv-announce logfile
71 */
72
73 static void
74 sig_handle(void)
75 {
76 fflush(stdout);
77 close(2);
78 close(1);
79 close(0);
80 exit(0);
81 }
82
83
84 int
85 main(int argc, char *argv[])
86 {
87 char buf[RECV_BUF_SIZE];
88 struct sockaddr_in R;
89 int len;
90 struct hostent *hp = NULL;
91 char logfile[BUFSIZ];
92 char ip[4];
93
94 for (len = 0; len < 32; len++) {
95 signal(len, sig_handle);
96 }
97
98
99 if (argc > 1)
100 strcpy(logfile, argv[1]);
101 else
102 strcpy(logfile, "/tmp/recv-announce.log");
103
104 close(1);
105 if (open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0660) < 0) {
106 perror(logfile);
107 exit(1);
108 }
109 close(2);
110 dup(1);
111
112
113 for (;;) {
114 memset(buf, '\0', RECV_BUF_SIZE);
115 memset(&R, '\0', len = sizeof(R));
116
117 if (recvfrom(0, buf, RECV_BUF_SIZE, 0, &R, &len) < 0) {
118 perror("recv");
119 exit(2);
120 }
121 xmemcpy(ip, &R.sin_addr.s_addr, 4);
122 hp = gethostbyaddr(ip, 4, AF_INET);
123 printf("==============================================================================\n");
124 printf("Received from %s [%s]\n",
125 inet_ntoa(R.sin_addr),
126 (hp && hp->h_name) ? hp->h_name : "Unknown");
127 fputs(buf, stdout);
128 fflush(stdout);
129 }
130 return 0;
131 }