]> git.ipfire.org Git - thirdparty/squid.git/blob - src/send-announce.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / send-announce.cc
1 /*
2 * Copyright (C) 1996-2018 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 27 Cache Announcer */
10
11 #include "squid.h"
12 #include "anyp/PortCfg.h"
13 #include "comm/Connection.h"
14 #include "event.h"
15 #include "fd.h"
16 #include "fde.h"
17 #include "fs_io.h"
18 #include "globals.h"
19 #include "ICP.h"
20 #include "ipcache.h"
21 #include "SquidConfig.h"
22 #include "SquidTime.h"
23 #include "tools.h"
24
25 static IPH send_announce;
26
27 void
28 start_announce(void *)
29 {
30 if (0 == Config.onoff.announce)
31 return;
32
33 if (!Comm::IsConnOpen(icpOutgoingConn))
34 return;
35
36 ipcache_nbgethostbyname(Config.Announce.host, send_announce, NULL);
37
38 eventAdd("send_announce", start_announce, NULL, (double) Config.Announce.period, 1);
39 }
40
41 static void
42 send_announce(const ipcache_addrs *ia, const Dns::LookupDetails &, void *)
43 {
44 LOCAL_ARRAY(char, tbuf, 256);
45 LOCAL_ARRAY(char, sndbuf, BUFSIZ);
46
47 char *host = Config.Announce.host;
48 char *file = NULL;
49 unsigned short port = Config.Announce.port;
50 int l;
51 int n;
52 int fd;
53
54 if (ia == NULL) {
55 debugs(27, DBG_IMPORTANT, "send_announce: Unknown host '" << host << "'");
56 return;
57 }
58
59 debugs(27, DBG_IMPORTANT, "Sending Announcement to " << host);
60 sndbuf[0] = '\0';
61 snprintf(tbuf, 256, "cache_version SQUID/%s\n", version_string);
62 strcat(sndbuf, tbuf);
63 assert(HttpPortList != NULL);
64 snprintf(tbuf, 256, "Running on %s %d %d\n",
65 getMyHostname(),
66 getMyPort(),
67 (int) Config.Port.icp);
68 strcat(sndbuf, tbuf);
69
70 if (Config.adminEmail) {
71 snprintf(tbuf, 256, "cache_admin: %s\n", Config.adminEmail);
72 strcat(sndbuf, tbuf);
73 }
74
75 snprintf(tbuf, 256, "generated %d [%s]\n",
76 (int) squid_curtime,
77 Time::FormatHttpd(squid_curtime));
78 strcat(sndbuf, tbuf);
79 l = strlen(sndbuf);
80
81 if ((file = Config.Announce.file) != NULL) {
82 fd = file_open(file, O_RDONLY | O_TEXT);
83
84 if (fd > -1 && (n = FD_READ_METHOD(fd, sndbuf + l, BUFSIZ - l - 1)) > 0) {
85 fd_bytes(fd, n, FD_READ);
86 l += n;
87 sndbuf[l] = '\0';
88 file_close(fd);
89 } else {
90 int xerrno = errno;
91 debugs(50, DBG_IMPORTANT, "send_announce: " << file << ": " << xstrerr(xerrno));
92 }
93 }
94
95 Ip::Address S = ia->current();
96 S.port(port);
97 assert(Comm::IsConnOpen(icpOutgoingConn));
98
99 if (comm_udp_sendto(icpOutgoingConn->fd, S, sndbuf, strlen(sndbuf) + 1) < 0) {
100 int xerrno = errno;
101 debugs(27, DBG_IMPORTANT, "ERROR: Failed to announce to " << S << " from " << icpOutgoingConn->local << ": " << xstrerr(xerrno));
102 }
103 }
104