]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/ModUdp.cc
a1d94720229b43d1dd3a8ac44dd63e68852e8203
[thirdparty/squid.git] / src / log / ModUdp.cc
1 /*
2 * Copyright (C) 1996-2015 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 50 Log file handling */
10
11 #include "squid.h"
12 #include "comm.h"
13 #include "comm/Connection.h"
14 #include "disk.h"
15 #include "fatal.h"
16 #include "fd.h"
17 #include "log/File.h"
18 #include "log/ModUdp.h"
19 #include "Parsing.h"
20 #include "SquidConfig.h"
21
22 #include <cerrno>
23
24 /*
25 * This logfile UDP module is mostly inspired by a patch by Tim Starling
26 * from Wikimedia.
27 *
28 * It doesn't do any UDP buffering - it'd be quite a bit of work for
29 * something which the kernel could be doing for you!
30 */
31
32 typedef struct {
33 int fd;
34 char *buf;
35 size_t bufsz;
36 int offset;
37 } l_udp_t;
38
39 static void
40 logfile_mod_udp_write(Logfile * lf, const char *buf, size_t len)
41 {
42 l_udp_t *ll = (l_udp_t *) lf->data;
43 ssize_t s;
44 s = write(ll->fd, (char const *) buf, len);
45 fd_bytes(ll->fd, s, FD_WRITE);
46 #if 0
47 if (s < 0) {
48 debugs(1, DBG_IMPORTANT, "logfile (udp): got errno (" << errno << "):" << xstrerror());
49 }
50 if (s != len) {
51 debugs(1, DBG_IMPORTANT, "logfile (udp): len=" << len << ", wrote=" << s);
52 }
53 #endif
54
55 /* We don't worry about network errors for now */
56 }
57
58 static void
59 logfile_mod_udp_flush(Logfile * lf)
60 {
61 l_udp_t *ll = (l_udp_t *) lf->data;
62 if (0 == ll->offset)
63 return;
64 logfile_mod_udp_write(lf, ll->buf, (size_t) ll->offset);
65 ll->offset = 0;
66 }
67
68 static void
69 logfile_mod_udp_writeline(Logfile * lf, const char *buf, size_t len)
70 {
71 l_udp_t *ll = (l_udp_t *) lf->data;
72
73 if (0 == ll->bufsz) {
74 /* buffering disabled */
75 logfile_mod_udp_write(lf, buf, len);
76 return;
77 }
78 if (ll->offset > 0 && (ll->offset + len + 4) > ll->bufsz)
79 logfile_mod_udp_flush(lf);
80
81 if (len > ll->bufsz) {
82 /* too big to fit in buffer */
83 logfile_mod_udp_write(lf, buf, len);
84 return;
85 }
86 /* buffer it */
87 memcpy(ll->buf + ll->offset, buf, len);
88
89 ll->offset += len;
90
91 assert(ll->offset >= 0);
92
93 assert((size_t) ll->offset <= ll->bufsz);
94 }
95
96 static void
97 logfile_mod_udp_linestart(Logfile *)
98 {
99 }
100
101 static void
102 logfile_mod_udp_lineend(Logfile *)
103 {
104 }
105
106 static void
107 logfile_mod_udp_rotate(Logfile *)
108 {
109 }
110
111 static void
112 logfile_mod_udp_close(Logfile * lf)
113 {
114 l_udp_t *ll = (l_udp_t *) lf->data;
115 lf->f_flush(lf);
116
117 if (ll->fd >= 0)
118 file_close(ll->fd);
119
120 if (ll->buf)
121 xfree(ll->buf);
122
123 xfree(lf->data);
124 lf->data = NULL;
125 }
126
127 /*
128 * This code expects the path to be //host:port
129 */
130 int
131 logfile_mod_udp_open(Logfile * lf, const char *path, size_t bufsz, int fatal_flag)
132 {
133 Ip::Address addr;
134 char *strAddr;
135
136 lf->f_close = logfile_mod_udp_close;
137 lf->f_linewrite = logfile_mod_udp_writeline;
138 lf->f_linestart = logfile_mod_udp_linestart;
139 lf->f_lineend = logfile_mod_udp_lineend;
140 lf->f_flush = logfile_mod_udp_flush;
141 lf->f_rotate = logfile_mod_udp_rotate;
142
143 l_udp_t *ll = static_cast<l_udp_t*>(xcalloc(1, sizeof(*ll)));
144 lf->data = ll;
145
146 if (strncmp(path, "//", 2) == 0) {
147 path += 2;
148 }
149 strAddr = xstrdup(path);
150 if (!GetHostWithPort(strAddr, &addr)) {
151 if (lf->flags.fatal) {
152 fatalf("Invalid UDP logging address '%s'\n", lf->path);
153 } else {
154 debugs(50, DBG_IMPORTANT, "Invalid UDP logging address '" << lf->path << "'");
155 safe_free(strAddr);
156 return FALSE;
157 }
158 }
159 safe_free(strAddr);
160
161 Ip::Address any_addr;
162 any_addr.setAnyAddr();
163
164 // require the sending UDP port to be of the right family for the destination address.
165 if (addr.isIPv4())
166 any_addr.setIPv4();
167
168 ll->fd = comm_open(SOCK_DGRAM, IPPROTO_UDP, any_addr, COMM_NONBLOCKING, "UDP log socket");
169 if (ll->fd < 0) {
170 if (lf->flags.fatal) {
171 fatalf("Unable to open UDP socket for logging\n");
172 } else {
173 debugs(50, DBG_IMPORTANT, "Unable to open UDP socket for logging");
174 return FALSE;
175 }
176 } else if (!comm_connect_addr(ll->fd, addr)) {
177 if (lf->flags.fatal) {
178 fatalf("Unable to connect to %s for UDP log: %s\n", lf->path, xstrerror());
179 } else {
180 debugs(50, DBG_IMPORTANT, "Unable to connect to " << lf->path << " for UDP log: " << xstrerror());
181 return FALSE;
182 }
183 }
184 if (ll->fd == -1) {
185 if (ENOENT == errno && fatal_flag) {
186 fatalf("Cannot open '%s' because\n"
187 "\tthe parent directory does not exist.\n"
188 "\tPlease create the directory.\n", path);
189 } else if (EACCES == errno && fatal_flag) {
190 fatalf("Cannot open '%s' for writing.\n"
191 "\tThe parent directory must be writeable by the\n"
192 "\tuser '%s', which is the cache_effective_user\n"
193 "\tset in squid.conf.", path, Config.effectiveUser);
194 } else {
195 debugs(50, DBG_IMPORTANT, "logfileOpen (UDP): " << lf->path << ": " << xstrerror());
196 return 0;
197 }
198 }
199 /* Force buffer size to something roughly fitting inside an MTU */
200 /*
201 * XXX note the receive side needs to receive the whole packet at once;
202 * applications like netcat have a small default receive buffer and will
203 * truncate!
204 */
205 bufsz = 1400;
206 if (bufsz > 0) {
207 ll->buf = static_cast<char*>(xmalloc(bufsz));
208 ll->bufsz = bufsz;
209 }
210
211 return 1;
212 }
213