]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/ModTcp.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[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-old.h"
35 #include "comm.h"
36 #include "comm/Connection.h"
37 #include "log/File.h"
38 #include "log/ModTcp.h"
39 #include "Parsing.h"
40
41 /*
42 * This logfile TCP module is mostly inspired by a patch by Tim Starling
43 * from Wikimedia.
44 *
45 * It doesn't do any TCP buffering - it'd be quite a bit of work for
46 * something which the kernel could be doing for you!
47 */
48
49 typedef struct {
50 int fd;
51 char *buf;
52 size_t bufsz;
53 int offset;
54 } l_tcp_t;
55
56 static void
57 logfile_mod_tcp_write(Logfile * lf, const char *buf, size_t len)
58 {
59 l_tcp_t *ll = (l_tcp_t *) lf->data;
60 ssize_t s;
61 s = write(ll->fd, (char const *) buf, len);
62
63 fd_bytes(ll->fd, s, FD_WRITE);
64 #if 0
65 if (s < 0) {
66 debugs(1, 1, "logfile (tcp): got errno (" << errno << "):" << xstrerror());
67 }
68 if (s != len) {
69 debugs(1, 1, "logfile (tcp): len=" << len << ", wrote=" << s);
70 }
71 #endif
72
73 /* We don't worry about network errors for now */
74 }
75
76 static void
77 logfile_mod_tcp_flush(Logfile * lf)
78 {
79 l_tcp_t *ll = (l_tcp_t *) lf->data;
80 if (0 == ll->offset)
81 return;
82 logfile_mod_tcp_write(lf, ll->buf, (size_t) ll->offset);
83 ll->offset = 0;
84 }
85
86 static void
87 logfile_mod_tcp_writeline(Logfile * lf, const char *buf, size_t len)
88 {
89 l_tcp_t *ll = (l_tcp_t *) lf->data;
90
91 if (0 == ll->bufsz) {
92 /* buffering disabled */
93 logfile_mod_tcp_write(lf, buf, len);
94 return;
95 }
96 if (ll->offset > 0 && (ll->offset + len + 4) > ll->bufsz)
97 logfile_mod_tcp_flush(lf);
98
99 if (len > ll->bufsz) {
100 /* too big to fit in buffer */
101 logfile_mod_tcp_write(lf, buf, len);
102 return;
103 }
104 /* buffer it */
105 memcpy(ll->buf + ll->offset, buf, len);
106
107 ll->offset += len;
108
109 assert(ll->offset >= 0);
110
111 assert((size_t) ll->offset <= ll->bufsz);
112 }
113
114 static void
115 logfile_mod_tcp_linestart(Logfile * lf)
116 {
117 }
118
119 static void
120 logfile_mod_tcp_lineend(Logfile * lf)
121 {
122 logfile_mod_tcp_flush(lf);
123 }
124
125 static void
126 logfile_mod_tcp_rotate(Logfile * lf)
127 {
128 return;
129 }
130
131 static void
132 logfile_mod_tcp_close(Logfile * lf)
133 {
134 l_tcp_t *ll = (l_tcp_t *) lf->data;
135 lf->f_flush(lf);
136
137 if (ll->fd >= 0)
138 file_close(ll->fd);
139
140 if (ll->buf)
141 xfree(ll->buf);
142
143 xfree(lf->data);
144 lf->data = NULL;
145 }
146
147
148
149 /*
150 * This code expects the path to be //host:port
151 */
152 int
153 logfile_mod_tcp_open(Logfile * lf, const char *path, size_t bufsz, int fatal_flag)
154 {
155 debugs(5, 3, "Tcp Open called");
156 Ip::Address addr;
157
158 char *strAddr;
159
160 lf->f_close = logfile_mod_tcp_close;
161 lf->f_linewrite = logfile_mod_tcp_writeline;
162 lf->f_linestart = logfile_mod_tcp_linestart;
163 lf->f_lineend = logfile_mod_tcp_lineend;
164 lf->f_flush = logfile_mod_tcp_flush;
165 lf->f_rotate = logfile_mod_tcp_rotate;
166
167 l_tcp_t *ll = static_cast<l_tcp_t*>(xcalloc(1, sizeof(*ll)));
168 lf->data = ll;
169
170 if (strncmp(path, "//", 2) == 0) {
171 path += 2;
172 }
173 strAddr = xstrdup(path);
174
175 if (!GetHostWithPort(strAddr, &addr)) {
176 if (lf->flags.fatal) {
177 fatalf("Invalid TCP logging address '%s'\n", lf->path);
178 } else {
179 debugs(50, DBG_IMPORTANT, "Invalid TCP logging address '" << lf->path << "'");
180 safe_free(strAddr);
181 return FALSE;
182 }
183 }
184
185 safe_free(strAddr);
186
187 Ip::Address any_addr;
188 any_addr.SetAnyAddr();
189
190 // require the sending TCP port to be of the right family for the destination address.
191 if (addr.IsIPv4())
192 any_addr.SetIPv4();
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 }