]> git.ipfire.org Git - thirdparty/squid.git/blob - src/send-announce.cc
Prep for 3.3.12 and 3.4.4
[thirdparty/squid.git] / src / send-announce.cc
1 /*
2 * DEBUG: section 27 Cache Announcer
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 #include "squid.h"
34 #include "comm/Connection.h"
35 #include "disk.h"
36 #include "event.h"
37 #include "fd.h"
38 #include "fde.h"
39 #include "globals.h"
40 #include "ICP.h"
41 #include "ipcache.h"
42 #include "SquidConfig.h"
43 #include "SquidTime.h"
44 #include "tools.h"
45
46 static IPH send_announce;
47
48 void
49 start_announce(void *datanotused)
50 {
51 if (0 == Config.onoff.announce)
52 return;
53
54 if (!Comm::IsConnOpen(icpOutgoingConn))
55 return;
56
57 ipcache_nbgethostbyname(Config.Announce.host, send_announce, NULL);
58
59 eventAdd("send_announce", start_announce, NULL, (double) Config.Announce.period, 1);
60 }
61
62 static void
63 send_announce(const ipcache_addrs *ia, const DnsLookupDetails &, void *junk)
64 {
65 LOCAL_ARRAY(char, tbuf, 256);
66 LOCAL_ARRAY(char, sndbuf, BUFSIZ);
67
68 char *host = Config.Announce.host;
69 char *file = NULL;
70 unsigned short port = Config.Announce.port;
71 int l;
72 int n;
73 int fd;
74
75 if (ia == NULL) {
76 debugs(27, DBG_IMPORTANT, "send_announce: Unknown host '" << host << "'");
77 return;
78 }
79
80 debugs(27, DBG_IMPORTANT, "Sending Announcement to " << host);
81 sndbuf[0] = '\0';
82 snprintf(tbuf, 256, "cache_version SQUID/%s\n", version_string);
83 strcat(sndbuf, tbuf);
84 assert(Config.Sockaddr.http);
85 snprintf(tbuf, 256, "Running on %s %d %d\n",
86 getMyHostname(),
87 getMyPort(),
88 (int) Config.Port.icp);
89 strcat(sndbuf, tbuf);
90
91 if (Config.adminEmail) {
92 snprintf(tbuf, 256, "cache_admin: %s\n", Config.adminEmail);
93 strcat(sndbuf, tbuf);
94 }
95
96 snprintf(tbuf, 256, "generated %d [%s]\n",
97 (int) squid_curtime,
98 Time::FormatHttpd(squid_curtime));
99 strcat(sndbuf, tbuf);
100 l = strlen(sndbuf);
101
102 if ((file = Config.Announce.file) != NULL) {
103 fd = file_open(file, O_RDONLY | O_TEXT);
104
105 if (fd > -1 && (n = FD_READ_METHOD(fd, sndbuf + l, BUFSIZ - l - 1)) > 0) {
106 fd_bytes(fd, n, FD_READ);
107 l += n;
108 sndbuf[l] = '\0';
109 file_close(fd);
110 } else {
111 debugs(50, DBG_IMPORTANT, "send_announce: " << file << ": " << xstrerror());
112 }
113 }
114
115 Ip::Address S = ia->in_addrs[0];
116 S.port(port);
117 assert(Comm::IsConnOpen(icpOutgoingConn));
118
119 if (comm_udp_sendto(icpOutgoingConn->fd, S, sndbuf, strlen(sndbuf) + 1) < 0)
120 debugs(27, DBG_IMPORTANT, "ERROR: Failed to announce to " << S << " from " << icpOutgoingConn->local << ": " << xstrerror());
121 }