]> git.ipfire.org Git - thirdparty/squid.git/blob - src/recv-announce.cc
SourceFormat: Main reformat push
[thirdparty/squid.git] / src / recv-announce.cc
1
2 /*
3 * $Id: recv-announce.cc,v 1.26 2003/02/21 22:50:10 robertc Exp $
4 *
5 * DEBUG: section 0 Announcement Server
6 * AUTHOR: Harvest Derived
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the 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 #include "config.h"
49
50 #define RECV_BUF_SIZE 8192
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(int)
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
89 struct sockaddr_in R;
90 socklen_t len;
91
92 struct hostent *hp = NULL;
93 char logfile[BUFSIZ];
94 char ip[4];
95
96 for (len = 0; len < 32; len++) {
97 signal(len, sig_handle);
98 }
99
100
101 if (argc > 1)
102 strcpy(logfile, argv[1]);
103 else
104 strcpy(logfile, "/tmp/recv-announce.log");
105
106 close(1);
107
108 if (open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0660) < 0) {
109 perror(logfile);
110 exit(1);
111 }
112
113 close(2);
114 dup(1);
115
116
117 for (;;) {
118 memset(buf, '\0', RECV_BUF_SIZE);
119 memset(&R, '\0', len = sizeof(R));
120
121 if (recvfrom(0, buf, RECV_BUF_SIZE, 0, (sockaddr *)&R, &len) < 0) {
122 perror("recv");
123 exit(2);
124 }
125
126 xmemcpy(ip, &R.sin_addr.s_addr, 4);
127 hp = gethostbyaddr(ip, 4, AF_INET);
128 printf("==============================================================================\n");
129 printf("Received from %s [%s]\n",
130 inet_ntoa(R.sin_addr),
131 (hp && hp->h_name) ? hp->h_name : "Unknown");
132 fputs(buf, stdout);
133 fflush(stdout);
134 }
135
136 return 0;
137 }