send-announce.cc
Go to the documentation of this file.
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 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 
26 
27 void
29 {
30  if (0 == Config.onoff.announce)
31  return;
32 
34  return;
35 
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);
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);
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 
struct SquidConfig::@106 Announce
void start_announce(void *)
#define assert(EX)
Definition: assert.h:17
char * adminEmail
Definition: SquidConfig.h:182
unsigned short port
Definition: SquidConfig.h:226
int file_open(const char *path, int mode)
Definition: fs_io.cc:46
encapsulates DNS lookup results
Definition: LookupDetails.h:20
AnyP::PortCfgPointer HttpPortList
list of Squid http(s)_port configured
Definition: PortCfg.cc:21
char * file
Definition: SquidConfig.h:224
const char * getMyHostname(void)
Definition: tools.cc:412
int comm_udp_sendto(int fd, const Ip::Address &to_addr, const void *buf, int len)
Definition: comm.cc:927
Definition: enums.h:23
time_t squid_curtime
Definition: stub_time.cc:17
void fd_bytes(int fd, int len, unsigned int type)
Definition: fd.cc:261
const char * xstrerr(int error)
Definition: xstrerror.cc:83
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:24
int getMyPort(void)
Definition: tools.cc:1003
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
#define DBG_IMPORTANT
Definition: Debug.h:45
struct SquidConfig::@99 Port
unsigned short icp
Definition: SquidConfig.h:131
Comm::ConnectionPointer icpOutgoingConn
Definition: icp_v2.cc:73
void ipcache_nbgethostbyname(const char *name, IPH *handler, void *handlerData)
Definition: ipcache.cc:600
static int port
Definition: ldap_backend.cc:69
time_t period
Definition: SquidConfig.h:225
#define LOCAL_ARRAY(type, name, size)
Definition: leakcheck.h:18
#define BUFSIZ
Definition: defines.h:20
#define O_TEXT
Definition: defines.h:201
struct SquidConfig::@112 onoff
unsigned short port() const
Definition: Address.cc:786
Ip::Address local
Definition: Connection.h:135
void eventAdd(const char *name, EVH *func, void *arg, double when, int weight, bool cbdata)
Definition: event.cc:109
#define FD_READ_METHOD(fd, buf, len)
Definition: fde.h:161
const char * FormatHttpd(time_t t)
Definition: stub_time.cc:25
void IPH(const ipcache_addrs *, const Dns::LookupDetails &details, void *)
Definition: ipcache.h:219
static IPH send_announce
void file_close(int fd)
Definition: fs_io.cc:76
const Ip::Address & current() const
Definition: ipcache.h:59
const char * version_string
class SquidConfig Config
Definition: SquidConfig.cc:12
#define NULL
Definition: types.h:166
char * host
Definition: SquidConfig.h:223

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors