]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/ModTcp.cc
Use Ip namespace in ModTcp
[thirdparty/squid.git] / src / log / ModTcp.cc
1 /*
2 * DEBUG: section 50 Log file handling
3 * AUTHOR: Dhaval Varia
4 * Developed based on ModUdp.* by Adrian Chadd
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
32 */
33
34 #include "squid.h"
35 #include "comm.h"
36 #include "log/File.h"
37 #include "log/ModTcp.h"
38 #include "Parsing.h"
39
40 /*
41 * This logfile TCP module is mostly inspired by a patch by Tim Starling
42 * from Wikimedia.
43 *
44 * It doesn't do any TCP 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_tcp_t;
54
55 static void
56 logfile_mod_tcp_write(Logfile * lf, const char *buf, size_t len)
57 {
58 l_tcp_t *ll = (l_tcp_t *) lf->data;
59 ssize_t s;
60 s = write(ll->fd, (char const *) buf, len);
61
62 fd_bytes(ll->fd, s, FD_WRITE);
63 #if 0
64 if (s < 0) {
65 debugs(1, 1, "logfile (tcp): got errno (" << errno << "):" << xstrerror());
66 }
67 if (s != len) {
68 debugs(1, 1, "logfile (tcp): len=" << len << ", wrote=" << s);
69 }
70 #endif
71
72 /* We don't worry about network errors for now */
73 }
74
75 static void
76 logfile_mod_tcp_flush(Logfile * lf)
77 {
78 l_tcp_t *ll = (l_tcp_t *) lf->data;
79 if (0 == ll->offset)
80 return;
81 logfile_mod_tcp_write(lf, ll->buf, (size_t) ll->offset);
82 ll->offset = 0;
83 }
84
85 static void
86 logfile_mod_tcp_writeline(Logfile * lf, const char *buf, size_t len)
87 {
88 l_tcp_t *ll = (l_tcp_t *) lf->data;
89
90 if (0 == ll->bufsz) {
91 /* buffering disabled */
92 logfile_mod_tcp_write(lf, buf, len);
93 return;
94 }
95 if (ll->offset > 0 && (ll->offset + len + 4) > ll->bufsz)
96 logfile_mod_tcp_flush(lf);
97
98 if (len > ll->bufsz) {
99 /* too big to fit in buffer */
100 logfile_mod_tcp_write(lf, buf, len);
101 return;
102 }
103 /* buffer it */
104 xmemcpy(ll->buf + ll->offset, buf, len);
105
106 ll->offset += len;
107
108 assert(ll->offset >= 0);
109
110 assert((size_t) ll->offset <= ll->bufsz);
111 }
112
113 static void
114 logfile_mod_tcp_linestart(Logfile * lf)
115 {
116 }
117
118 static void
119 logfile_mod_tcp_lineend(Logfile * lf)
120 {
121 }
122
123 static void
124 logfile_mod_tcp_rotate(Logfile * lf)
125 {
126 return;
127 }
128
129 static void
130 logfile_mod_tcp_close(Logfile * lf)
131 {
132 l_tcp_t *ll = (l_tcp_t *) lf->data;
133 lf->f_flush(lf);
134
135 if (ll->fd >= 0)
136 file_close(ll->fd);
137
138 if (ll->buf)
139 xfree(ll->buf);
140
141 xfree(lf->data);
142 lf->data = NULL;
143 }
144
145
146
147 /*
148 * This code expects the path to be //host:port
149 */
150 int
151 logfile_mod_tcp_open(Logfile * lf, const char *path, size_t bufsz, int fatal_flag)
152 {
153 debugs(5, 3, "Tcp Open called");
154 Ip::Address addr;
155
156 char *strAddr;
157
158 lf->f_close = logfile_mod_tcp_close;
159 lf->f_linewrite = logfile_mod_tcp_writeline;
160 lf->f_linestart = logfile_mod_tcp_linestart;
161 lf->f_lineend = logfile_mod_tcp_lineend;
162 lf->f_flush = logfile_mod_tcp_flush;
163 lf->f_rotate = logfile_mod_tcp_rotate;
164
165 l_tcp_t *ll = static_cast<l_tcp_t*>(xcalloc(1, sizeof(*ll)));
166 lf->data = ll;
167
168 if (strncmp(path, "//", 2) == 0) {
169 path += 2;
170 }
171 strAddr = xstrdup(path);
172
173 if (!GetHostWithPort(strAddr, &addr)) {
174 if (lf->flags.fatal) {
175 fatalf("Invalid TCP logging address '%s'\n", lf->path);
176 } else {
177 debugs(50, DBG_IMPORTANT, "Invalid TCP logging address '" << lf->path << "'");
178 safe_free(strAddr);
179 return FALSE;
180 }
181 }
182
183 safe_free(strAddr);
184
185 Ip::Address any_addr;
186 any_addr.SetAnyAddr();
187
188 #if USE_IPV6
189 // require the sending TCP port to be of the right family for the destination address.
190 if (addr.IsIPv4())
191 no_addr.SetIPv4();
192 #endif
193
194 ll->fd = comm_open(SOCK_STREAM, IPPROTO_TCP, any_addr, COMM_NONBLOCKING, "TCP log socket");
195 if (ll->fd < 0) {
196 if (lf->flags.fatal) {
197 fatalf("Unable to open TCP socket for logging\n");
198 } else {
199 debugs(50, DBG_IMPORTANT, "Unable to open TCP socket for logging");
200 return FALSE;
201 }
202 } else if (!comm_connect_addr(ll->fd, &addr)) {
203 if (lf->flags.fatal) {
204 fatalf("Unable to connect to %s for TCP log: %s\n", lf->path, xstrerror());
205 } else {
206 debugs(50, DBG_IMPORTANT, "Unable to connect to " << lf->path << " for TCP log: " << xstrerror());
207 return FALSE;
208 }
209 }
210 if (ll->fd == -1) {
211 if (ENOENT == errno && fatal_flag) {
212 fatalf("Cannot open '%s' because\n"
213 "\tthe parent directory does not exist.\n"
214 "\tPlease create the directory.\n", path);
215 } else if (EACCES == errno && fatal_flag) {
216 fatalf("Cannot open '%s' for writing.\n"
217 "\tThe parent directory must be writeable by the\n"
218 "\tuser '%s', which is the cache_effective_user\n"
219 "\tset in squid.conf.", path, Config.effectiveUser);
220 } else {
221 debugs(50, DBG_IMPORTANT, "logfileOpen (TCP): " << lf->path << ": " << xstrerror());
222 return 0;
223 }
224 }
225
226 bufsz = 65536;
227 if (bufsz > 0) {
228 ll->buf = static_cast<char*>(xmalloc(bufsz));
229 ll->bufsz = bufsz;
230 }
231
232 return 1;
233 }