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