]> git.ipfire.org Git - thirdparty/squid.git/blame - src/send-announce.cc
Polish: update Ip::Address to follow Squid coding guidelines
[thirdparty/squid.git] / src / send-announce.cc
CommitLineData
7d49daab 1/*
30a4f2a8 2 * DEBUG: section 27 Cache Announcer
3 * AUTHOR: Duane Wessels
4 *
2b6662ba 5 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 6 * ----------------------------------------------------------
30a4f2a8 7 *
2b6662ba 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.
30a4f2a8 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.
26ac0430 21 *
30a4f2a8 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.
26ac0430 26 *
30a4f2a8 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
cbdec147 29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 30 *
7d49daab 31 */
090089c4 32
582c2af2 33#include "squid.h"
e0d28505 34#include "comm/Connection.h"
438b04d4 35#include "disk.h"
a553a5a3 36#include "event.h"
c4ad1349 37#include "fd.h"
528b2c61 38#include "fde.h"
582c2af2 39#include "globals.h"
e0d28505 40#include "ICP.h"
714e68b7 41#include "ipcache.h"
4d5904f7 42#include "SquidConfig.h"
985c86bc 43#include "SquidTime.h"
4e540555 44#include "tools.h"
090089c4 45
b69f7771 46static IPH send_announce;
e924600d 47
b8d8561b 48void
79d39a72 49start_announce(void *datanotused)
e924600d 50{
9072f1fa 51 if (0 == Config.onoff.announce)
62e76326 52 return;
53
e0d28505 54 if (!Comm::IsConnOpen(icpOutgoingConn))
62e76326 55 return;
56
28c60158 57 ipcache_nbgethostbyname(Config.Announce.host, send_announce, NULL);
62e76326 58
52040193 59 eventAdd("send_announce", start_announce, NULL, (double) Config.Announce.period, 1);
e924600d 60}
61
62static void
3ff65596 63send_announce(const ipcache_addrs *ia, const DnsLookupDetails &, void *junk)
090089c4 64{
95d659f0 65 LOCAL_ARRAY(char, tbuf, 256);
66 LOCAL_ARRAY(char, sndbuf, BUFSIZ);
62e76326 67
48f44632 68 char *host = Config.Announce.host;
7d49daab 69 char *file = NULL;
f45dd259 70 unsigned short port = Config.Announce.port;
7d49daab 71 int l;
72 int n;
03a1ee42 73 int fd;
62e76326 74
e924600d 75 if (ia == NULL) {
e0236918 76 debugs(27, DBG_IMPORTANT, "send_announce: Unknown host '" << host << "'");
62e76326 77 return;
234967c9 78 }
62e76326 79
e0236918 80 debugs(27, DBG_IMPORTANT, "Sending Announcement to " << host);
234967c9 81 sndbuf[0] = '\0';
56878878 82 snprintf(tbuf, 256, "cache_version SQUID/%s\n", version_string);
7d49daab 83 strcat(sndbuf, tbuf);
7e3ce7b9 84 assert(Config.Sockaddr.http);
56878878 85 snprintf(tbuf, 256, "Running on %s %d %d\n",
62e76326 86 getMyHostname(),
87 getMyPort(),
88 (int) Config.Port.icp);
7d49daab 89 strcat(sndbuf, tbuf);
62e76326 90
b6f794d6 91 if (Config.adminEmail) {
62e76326 92 snprintf(tbuf, 256, "cache_admin: %s\n", Config.adminEmail);
93 strcat(sndbuf, tbuf);
234967c9 94 }
62e76326 95
042461c3 96 snprintf(tbuf, 256, "generated %d [%s]\n",
62e76326 97 (int) squid_curtime,
20efa1c2 98 Time::FormatHttpd(squid_curtime));
7d49daab 99 strcat(sndbuf, tbuf);
374b5a2d 100 l = strlen(sndbuf);
62e76326 101
79d39a72 102 if ((file = Config.Announce.file) != NULL) {
62e76326 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 {
e0236918 111 debugs(50, DBG_IMPORTANT, "send_announce: " << file << ": " << xstrerror());
62e76326 112 }
090089c4 113 }
62e76326 114
e0d28505 115 Ip::Address S = ia->in_addrs[0];
4dd643d5 116 S.port(port);
e0d28505 117 assert(Comm::IsConnOpen(icpOutgoingConn));
62e76326 118
e0d28505 119 if (comm_udp_sendto(icpOutgoingConn->fd, S, sndbuf, strlen(sndbuf) + 1) < 0)
e0236918 120 debugs(27, DBG_IMPORTANT, "ERROR: Failed to announce to " << S << " from " << icpOutgoingConn->local << ": " << xstrerror());
090089c4 121}