]> git.ipfire.org Git - thirdparty/squid.git/blame - src/recv-announce.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / recv-announce.cc
CommitLineData
30a4f2a8 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
e25c139f 3 *
bbc27441
AJ
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.
30a4f2a8 7 */
bbc27441
AJ
8
9/* DEBUG: section 00 Announcement Server */
10
f7f3304a 11#include "squid.h"
ed43818f 12
074d6a40
AJ
13#include <csignal>
14#include <cstring>
090089c4 15#include <fcntl.h>
090089c4 16#include <sys/socket.h>
17#include <netinet/in.h>
30a4f2a8 18#include <arpa/inet.h>
090089c4 19#include <netdb.h>
30a4f2a8 20#include <unistd.h>
090089c4 21
e6ccf245 22#define RECV_BUF_SIZE 8192
f587ac0a 23
090089c4 24/*
25 * This program must be run from inetd. First add something like this
26 * to /etc/services:
26ac0430 27 *
c5c666ab 28 * cached_announce 3131/udp # cache announcements
26ac0430 29 *
090089c4 30 * And then add something like this to /etc/inetd/conf:
26ac0430 31 *
090089c4 32 * cached_announce dgram udp wait cached /tmp/recv-announce recv-announce /tmp/recv-announce.log
26ac0430
AJ
33 *
34 *
090089c4 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.
26ac0430 38 *
090089c4 39 */
40
62e76326 41/*
090089c4 42 * usage: recv-announce logfile
43 */
44
24382924 45static void
e6ccf245 46sig_handle(int)
090089c4 47{
48 fflush(stdout);
49 close(2);
50 close(1);
51 close(0);
24885773 52 exit(EXIT_SUCCESS);
090089c4 53}
54
b8d8561b 55int
56main(int argc, char *argv[])
090089c4 57{
58 char buf[RECV_BUF_SIZE];
62e76326 59
090089c4 60 struct sockaddr_in R;
e6ccf245 61 socklen_t len;
62e76326 62
090089c4 63 struct hostent *hp = NULL;
5cf4e671 64 const char *logfile;
090089c4 65 char ip[4];
66
5db6bf73 67 for (len = 0; len < 32; ++len) {
62e76326 68 signal(len, sig_handle);
090089c4 69 }
70
090089c4 71 if (argc > 1)
5cf4e671 72 logfile = argv[1];
090089c4 73 else
5cf4e671 74 logfile = "/tmp/recv-announce.log";
090089c4 75
76 close(1);
62e76326 77
090089c4 78 if (open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0660) < 0) {
62e76326 79 perror(logfile);
24885773 80 exit(EXIT_FAILURE);
090089c4 81 }
62e76326 82
090089c4 83 close(2);
84 dup(1);
85
2f0b84f7
AJ
86 IPAddress ipa;
87 chat tmp[MAX_HOSTNAMELEN];
16300b58 88 for (;;) {
62e76326 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");
24885773 94 exit(EXIT_FAILURE);
62e76326 95 }
96
41d00cd3 97 memcpy(ip, &R.sin_addr.s_addr, 4);
62e76326 98 hp = gethostbyaddr(ip, 4, AF_INET);
2f0b84f7 99 ipa = R.sin_addr;
62e76326 100 printf("==============================================================================\n");
101 printf("Received from %s [%s]\n",
4dd643d5 102 ipa.toStr(tmp,MAX_HOSTNAMELEN),
62e76326 103 (hp && hp->h_name) ? hp->h_name : "Unknown");
104 fputs(buf, stdout);
105 fflush(stdout);
090089c4 106 }
62e76326 107
24885773 108 return EXIT_SUCCESS;
090089c4 109}
f53969cc 110