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