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