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