]> git.ipfire.org Git - thirdparty/squid.git/blob - src/recv-announce.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / recv-announce.cc
1 /*
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 00 Announcement Server */
10
11 #include "squid.h"
12
13 #include <csignal>
14 #include <cstring>
15 #include <fcntl.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <netdb.h>
20 #include <unistd.h>
21
22 #define RECV_BUF_SIZE 8192
23
24 /*
25 * This program must be run from inetd. First add something like this
26 * to /etc/services:
27 *
28 * cached_announce 3131/udp # cache announcements
29 *
30 * And then add something like this to /etc/inetd/conf:
31 *
32 * cached_announce dgram udp wait cached /tmp/recv-announce recv-announce /tmp/recv-announce.log
33 *
34 *
35 * A single instance of this process will continue to handle incoming
36 * requests. If it dies, or is killed, inetd should restart it when the
37 * next message arrives.
38 *
39 */
40
41 /*
42 * usage: recv-announce logfile
43 */
44
45 static void
46 sig_handle(int)
47 {
48 fflush(stdout);
49 close(2);
50 close(1);
51 close(0);
52 exit(0);
53 }
54
55 int
56 main(int argc, char *argv[])
57 {
58 char buf[RECV_BUF_SIZE];
59
60 struct sockaddr_in R;
61 socklen_t len;
62
63 struct hostent *hp = NULL;
64 const char *logfile;
65 char ip[4];
66
67 for (len = 0; len < 32; ++len) {
68 signal(len, sig_handle);
69 }
70
71 if (argc > 1)
72 logfile = argv[1];
73 else
74 logfile = "/tmp/recv-announce.log";
75
76 close(1);
77
78 if (open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0660) < 0) {
79 perror(logfile);
80 exit(1);
81 }
82
83 close(2);
84 dup(1);
85
86 IPAddress ipa;
87 chat tmp[MAX_HOSTNAMELEN];
88 for (;;) {
89 memset(buf, '\0', RECV_BUF_SIZE);
90 memset(&R, '\0', len = sizeof(R));
91
92 if (recvfrom(0, buf, RECV_BUF_SIZE, 0, (sockaddr *)&R, &len) < 0) {
93 perror("recv");
94 exit(2);
95 }
96
97 memcpy(ip, &R.sin_addr.s_addr, 4);
98 hp = gethostbyaddr(ip, 4, AF_INET);
99 ipa = R.sin_addr;
100 printf("==============================================================================\n");
101 printf("Received from %s [%s]\n",
102 ipa.toStr(tmp,MAX_HOSTNAMELEN),
103 (hp && hp->h_name) ? hp->h_name : "Unknown");
104 fputs(buf, stdout);
105 fflush(stdout);
106 }
107
108 return 0;
109 }
110