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