… | |
… | |
33 | */ |
33 | */ |
34 | |
34 | |
35 | using namespace std; |
35 | using namespace std; |
36 | |
36 | |
37 | #include <global.h> |
37 | #include <global.h> |
38 | #include <newclient.h> |
|
|
39 | #include <sproto.h> |
38 | #include <sproto.h> |
40 | #include <cstdarg> |
39 | #include <cstdarg> |
41 | |
40 | |
42 | #ifdef __linux__ |
41 | #ifdef __linux__ |
43 | # include <sys/types.h> |
42 | # include <sys/types.h> |
… | |
… | |
51 | // easily die in 20 seconds... |
50 | // easily die in 20 seconds... |
52 | #define SOCKET_TIMEOUT1 10 |
51 | #define SOCKET_TIMEOUT1 10 |
53 | #define SOCKET_TIMEOUT2 20 |
52 | #define SOCKET_TIMEOUT2 20 |
54 | |
53 | |
55 | void |
54 | void |
56 | Socket_Flush (client_socket * ns) |
55 | client::flush () |
57 | { |
56 | { |
58 | #ifdef __linux__ |
57 | #ifdef __linux__ |
59 | // check time of last ack, and, if too old, kill connection |
58 | // check time of last ack, and, if too old, kill connection |
60 | struct tcp_info tcpi; |
59 | struct tcp_info tcpi; |
61 | socklen_t len = sizeof (tcpi); |
60 | socklen_t len = sizeof (tcpi); |
62 | |
61 | |
63 | if (!getsockopt (ns->fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) |
62 | if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) |
64 | { |
63 | { |
65 | unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; |
64 | unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; |
|
|
65 | |
|
|
66 | rtt = tcpi.tcpi_rtt; |
|
|
67 | rttvar = tcpi.tcpi_rttvar; |
66 | |
68 | |
67 | if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s |
69 | if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s |
68 | && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s |
70 | && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s |
69 | { |
71 | { |
70 | LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", ns->fd, |
72 | LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
71 | (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked); |
73 | (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked); |
72 | ns->status = Ns_Dead; |
74 | status = Ns_Dead; |
73 | } |
75 | } |
74 | } |
76 | } |
75 | |
|
|
76 | int val; |
|
|
77 | |
|
|
78 | val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
|
|
79 | val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
|
|
80 | #endif |
77 | #endif |
|
|
78 | |
|
|
79 | /** |
|
|
80 | * Writes data to socket. |
|
|
81 | * |
|
|
82 | * When the socket is clear to write, and we have backlogged data, this |
|
|
83 | * is called to write it out. |
|
|
84 | */ |
|
|
85 | |
|
|
86 | if (!outputbuffer.len || socket_ev.poll () & PE_W) |
|
|
87 | return; |
|
|
88 | |
|
|
89 | write_outputbuffer (); |
|
|
90 | } |
|
|
91 | |
|
|
92 | void |
|
|
93 | client::write_outputbuffer () |
|
|
94 | { |
|
|
95 | while (outputbuffer.len) |
|
|
96 | { |
|
|
97 | int res = write (fd, outputbuffer.data + outputbuffer.start, |
|
|
98 | min (outputbuffer.len, SOCKETBUFSIZE - outputbuffer.start)); |
|
|
99 | |
|
|
100 | if (res > 0) |
|
|
101 | { |
|
|
102 | outputbuffer.start += res; |
|
|
103 | /* wrap back to start of buffer */ |
|
|
104 | if (outputbuffer.start == SOCKETBUFSIZE) |
|
|
105 | outputbuffer.start = 0; |
|
|
106 | |
|
|
107 | outputbuffer.len -= res; |
|
|
108 | #ifdef CS_LOGSTATS |
|
|
109 | cst_tot.obytes += res; |
|
|
110 | cst_lst.obytes += res; |
|
|
111 | #endif |
|
|
112 | } |
|
|
113 | else if (res == 0) |
|
|
114 | { |
|
|
115 | LOG (llevError, "socket write failed, connection closed.\n"); |
|
|
116 | status = Ns_Dead; |
|
|
117 | return; |
|
|
118 | } |
|
|
119 | else if (errno == EINTR) |
|
|
120 | { |
|
|
121 | // just retry |
|
|
122 | } |
|
|
123 | else if (errno == EAGAIN) |
|
|
124 | { |
|
|
125 | // delay till ready |
|
|
126 | socket_ev.poll (socket_ev.poll () | PE_W); |
|
|
127 | socket_ev.start (); |
|
|
128 | return; |
|
|
129 | } |
|
|
130 | else |
|
|
131 | { |
|
|
132 | LOG (llevError, "socket write failed: %s\n", strerror (errno)); |
|
|
133 | status = Ns_Dead; |
|
|
134 | return; |
|
|
135 | } |
|
|
136 | } |
|
|
137 | |
|
|
138 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
81 | } |
139 | } |
82 | |
140 | |
83 | /*********************************************************************** |
141 | /*********************************************************************** |
84 | * |
142 | * |
85 | * packet functions/utilities |
143 | * packet functions/utilities |
… | |
… | |
140 | * Start of read routines. |
198 | * Start of read routines. |
141 | * |
199 | * |
142 | ******************************************************************************/ |
200 | ******************************************************************************/ |
143 | |
201 | |
144 | int |
202 | int |
145 | client_socket::read_packet () |
203 | client::read_packet () |
146 | { |
204 | { |
147 | for (;;) |
205 | for (;;) |
148 | { |
206 | { |
149 | if (inbuf_len >= 2) |
207 | if (inbuf_len >= 2) |
150 | { |
208 | { |
… | |
… | |
171 | } |
229 | } |
172 | else if (amount < 0) |
230 | else if (amount < 0) |
173 | { |
231 | { |
174 | if (errno != EAGAIN && errno != EINTR) |
232 | if (errno != EAGAIN && errno != EINTR) |
175 | { |
233 | { |
176 | LOG (llevError, "read error: %s", strerror (errno)); |
234 | LOG (llevError, "read error: %s\n", strerror (errno)); |
177 | return -1; |
235 | return -1; |
178 | } |
236 | } |
179 | |
237 | |
180 | return 0; |
238 | return 0; |
181 | } |
239 | } |
… | |
… | |
186 | cst_lst.ibytes += amount; |
244 | cst_lst.ibytes += amount; |
187 | } |
245 | } |
188 | } |
246 | } |
189 | |
247 | |
190 | void |
248 | void |
191 | client_socket::skip_packet (int len) |
249 | client::skip_packet (int len) |
192 | { |
250 | { |
193 | inbuf_len -= len; |
251 | inbuf_len -= len; |
194 | memmove (inbuf, inbuf + len, inbuf_len); |
252 | memmove (inbuf, inbuf + len, inbuf_len); |
195 | } |
253 | } |
196 | |
254 | |
… | |
… | |
204 | * Adds data to a socket buffer for whatever reason. |
262 | * Adds data to a socket buffer for whatever reason. |
205 | * |
263 | * |
206 | * ns is the socket we are adding the data to, buf is the start of the |
264 | * ns is the socket we are adding the data to, buf is the start of the |
207 | * data, and len is the number of bytes to add. |
265 | * data, and len is the number of bytes to add. |
208 | */ |
266 | */ |
209 | |
267 | void |
210 | static void |
268 | client::send (void *buf_, int len) |
211 | add_to_buffer (client_socket *ns, char *buf, int len) |
|
|
212 | { |
269 | { |
|
|
270 | char *buf = (char *)buf_; |
|
|
271 | char *pos = buf; |
|
|
272 | int amt = 0; |
|
|
273 | |
|
|
274 | if (status == Ns_Dead || !buf) |
|
|
275 | { |
|
|
276 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
|
|
277 | return; |
|
|
278 | } |
|
|
279 | |
|
|
280 | if ((len + outputbuffer.len) > SOCKETBUFSIZE) |
|
|
281 | { |
|
|
282 | LOG (llevDebug, "Socket on fd %d has overrun internal buffer - marking as dead\n", fd); |
|
|
283 | status = Ns_Dead; |
|
|
284 | return; |
|
|
285 | } |
|
|
286 | |
213 | int avail, end; |
287 | int avail, end; |
214 | |
|
|
215 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
|
|
216 | { |
|
|
217 | LOG (llevDebug, "Socket on fd %d has overrun internal buffer - marking as dead\n", ns->fd); |
|
|
218 | ns->status = Ns_Dead; |
|
|
219 | return; |
|
|
220 | } |
|
|
221 | |
288 | |
222 | /* data + end is where we start putting the new data. The last byte |
289 | /* data + end is where we start putting the new data. The last byte |
223 | * currently in use is actually data + end -1 |
290 | * currently in use is actually data + end -1 |
224 | */ |
291 | */ |
225 | |
|
|
226 | end = ns->outputbuffer.start + ns->outputbuffer.len; |
292 | end = outputbuffer.start + outputbuffer.len; |
227 | /* The buffer is already in a wrapped state, so adjust end */ |
293 | /* The buffer is already in a wrapped state, so adjust end */ |
228 | if (end >= SOCKETBUFSIZE) |
294 | if (end >= SOCKETBUFSIZE) |
229 | end -= SOCKETBUFSIZE; |
295 | end -= SOCKETBUFSIZE; |
230 | |
296 | |
231 | avail = SOCKETBUFSIZE - end; |
297 | avail = SOCKETBUFSIZE - end; |
232 | |
298 | |
233 | /* We can all fit it behind the current data without wrapping */ |
299 | /* We can all fit it behind the current data without wrapping */ |
234 | if (avail >= len) |
300 | if (avail >= len) |
235 | memcpy (ns->outputbuffer.data + end, buf, len); |
301 | memcpy (outputbuffer.data + end, buf, len); |
236 | else |
302 | else |
237 | { |
303 | { |
238 | memcpy (ns->outputbuffer.data + end, buf, avail); |
304 | memcpy (outputbuffer.data + end, buf, avail); |
239 | memcpy (ns->outputbuffer.data, buf + avail, len - avail); |
305 | memcpy (outputbuffer.data, buf + avail, len - avail); |
240 | } |
306 | } |
241 | |
307 | |
242 | ns->outputbuffer.len += len; |
308 | outputbuffer.len += len; |
243 | #if 0 |
|
|
244 | LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); |
|
|
245 | #endif |
|
|
246 | } |
309 | } |
247 | |
310 | |
248 | /** |
|
|
249 | * Writes data to socket. |
|
|
250 | * |
|
|
251 | * When the socket is clear to write, and we have backlogged data, this |
|
|
252 | * is called to write it out. |
|
|
253 | */ |
|
|
254 | void |
311 | void |
255 | write_socket_buffer (client_socket * ns) |
312 | client::socket_cb (iow &w, int got) |
256 | { |
313 | { |
257 | int amt, max; |
314 | write_outputbuffer (); |
258 | |
315 | |
259 | if (ns->outputbuffer.len == 0) |
316 | if (!outputbuffer.len) |
260 | { |
317 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
261 | LOG (llevDebug, "write_socket_buffer called when there is no data, fd=%d\n", ns->fd); |
|
|
262 | return; |
|
|
263 | } |
|
|
264 | |
|
|
265 | do |
|
|
266 | { |
|
|
267 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
|
|
268 | if (ns->outputbuffer.len < max) |
|
|
269 | max = ns->outputbuffer.len; |
|
|
270 | |
|
|
271 | do |
|
|
272 | { |
|
|
273 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
|
|
274 | } |
|
|
275 | while ((amt < 0) && (errno == EINTR)); |
|
|
276 | |
|
|
277 | if (amt < 0) |
|
|
278 | { /* We got an error */ |
|
|
279 | |
|
|
280 | if (errno != EWOULDBLOCK) |
|
|
281 | { |
|
|
282 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
|
|
283 | ns->status = Ns_Dead; |
|
|
284 | return; |
|
|
285 | } |
|
|
286 | else |
|
|
287 | { /* EWOULDBLOCK */ |
|
|
288 | /* can't write it, so store it away. */ |
|
|
289 | ns->can_write = 0; |
|
|
290 | return; |
|
|
291 | } |
|
|
292 | } |
|
|
293 | ns->outputbuffer.start += amt; |
|
|
294 | /* wrap back to start of buffer */ |
|
|
295 | if (ns->outputbuffer.start == SOCKETBUFSIZE) |
|
|
296 | ns->outputbuffer.start = 0; |
|
|
297 | ns->outputbuffer.len -= amt; |
|
|
298 | #ifdef CS_LOGSTATS |
|
|
299 | cst_tot.obytes += amt; |
|
|
300 | cst_lst.obytes += amt; |
|
|
301 | #endif |
|
|
302 | } |
|
|
303 | while (ns->outputbuffer.len > 0); |
|
|
304 | } |
|
|
305 | |
|
|
306 | /** |
|
|
307 | * This writes data to the socket. - It is very low level - |
|
|
308 | * all we try and do is write out the data to the socket |
|
|
309 | * provided (ns). buf is the data to write, len is the number |
|
|
310 | * of bytes to write. IT doesn't return anything - rather, it |
|
|
311 | * updates the ns structure if we get an error. |
|
|
312 | */ |
|
|
313 | void |
|
|
314 | client_socket::send (void *buf_, int len) |
|
|
315 | { |
|
|
316 | char *buf = (char *)buf_; |
|
|
317 | char *pos = buf; |
|
|
318 | int amt = 0; |
|
|
319 | |
|
|
320 | if (status == Ns_Dead || !buf) |
|
|
321 | { |
|
|
322 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
|
|
323 | return; |
|
|
324 | } |
|
|
325 | |
|
|
326 | #ifndef __GNU__ /* This caused problems on Hurd */ |
|
|
327 | if (!can_write) |
|
|
328 | { |
|
|
329 | add_to_buffer (this, buf, len); |
|
|
330 | return; |
|
|
331 | } |
|
|
332 | #endif |
|
|
333 | |
|
|
334 | /* If we manage to write more than we wanted, take it as a bonus */ |
|
|
335 | while (len > 0) |
|
|
336 | { |
|
|
337 | do |
|
|
338 | { |
|
|
339 | amt = write (fd, pos, len); |
|
|
340 | } |
|
|
341 | while ((amt < 0) && (errno == EINTR)); |
|
|
342 | |
|
|
343 | if (amt < 0) |
|
|
344 | { /* We got an error */ |
|
|
345 | if (errno != EWOULDBLOCK) |
|
|
346 | { |
|
|
347 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
|
|
348 | errno, strerror (errno)); |
|
|
349 | status = Ns_Dead; |
|
|
350 | return; |
|
|
351 | } |
|
|
352 | else |
|
|
353 | { /* EWOULDBLOCK */ |
|
|
354 | /* can't write it, so store it away. */ |
|
|
355 | add_to_buffer (this, pos, len); |
|
|
356 | can_write = 0; |
|
|
357 | return; |
|
|
358 | } |
|
|
359 | } |
|
|
360 | /* amt gets set to 0 above in blocking code, so we do this as |
|
|
361 | * an else if to make sure we don't reprocess it. |
|
|
362 | */ |
|
|
363 | else if (amt == 0) |
|
|
364 | LOG (llevError, "Write_To_Socket: No data written out.\n"); |
|
|
365 | |
|
|
366 | len -= amt; |
|
|
367 | pos += amt; |
|
|
368 | #ifdef CS_LOGSTATS |
|
|
369 | cst_tot.obytes += amt; |
|
|
370 | cst_lst.obytes += amt; |
|
|
371 | #endif |
|
|
372 | } |
|
|
373 | } |
318 | } |
374 | |
319 | |
375 | /** |
320 | /** |
376 | * Takes a string of data, and writes it out to the socket. A very handy |
321 | * Takes a string of data, and writes it out to the socket. A very handy |
377 | * shortcut function. |
322 | * shortcut function. |
378 | */ |
323 | */ |
379 | |
|
|
380 | void |
324 | void |
381 | client_socket::send_packet (packet &sl) |
325 | client::send_packet (packet &sl) |
382 | { |
326 | { |
383 | Send_With_Handling (this, &sl); |
|
|
384 | } |
|
|
385 | |
|
|
386 | void |
|
|
387 | client_socket::send_packet (const char *buf, int len) |
|
|
388 | { |
|
|
389 | packet sl; |
|
|
390 | |
|
|
391 | sl << data (buf, len); |
|
|
392 | send_packet (sl); |
|
|
393 | } |
|
|
394 | |
|
|
395 | void |
|
|
396 | client_socket::send_packet (const char *buf) |
|
|
397 | { |
|
|
398 | send_packet (buf, strlen (buf)); |
|
|
399 | } |
|
|
400 | |
|
|
401 | /** |
|
|
402 | * Calls Write_To_Socket to send data to the client. |
|
|
403 | * |
|
|
404 | * The only difference in this function is that we take a packet |
|
|
405 | *, and we prepend the length information. |
|
|
406 | */ |
|
|
407 | void |
|
|
408 | Send_With_Handling (client_socket *ns, packet *msg) |
|
|
409 | { |
|
|
410 | unsigned char sbuf[4]; |
|
|
411 | |
|
|
412 | if (ns->status == Ns_Dead || !msg) |
327 | if (status == Ns_Dead) |
413 | return; |
328 | return; |
414 | |
329 | |
415 | if (msg->length () >= MAXSOCKBUF) |
330 | if (sl.length () >= MAXSOCKBUF) |
416 | { |
331 | { |
417 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", msg->length ()); |
332 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); |
418 | /* Almost certainly we've overflowed a buffer, so quite now to make |
333 | /* Almost certainly we've overflowed a buffer, so quit now to make |
419 | * it easier to debug. |
334 | * it easier to debug. |
420 | */ |
335 | */ |
421 | abort (); |
336 | abort (); |
422 | } |
337 | } |
423 | |
338 | |
424 | sbuf[0] = ((uint32) (msg->length ()) >> 8); |
339 | if (!sl.length ()) |
425 | sbuf[1] = ((uint32) (msg->length ()) ); |
340 | return; |
426 | |
341 | |
427 | if (ns->status != Ns_Old) |
342 | assert (sl.hdrlen == 2); |
428 | ns->send (sbuf, 2); |
|
|
429 | |
343 | |
430 | ns->send (msg->buf, msg->length ()); |
344 | sl.buf_ [0] = sl.length () >> 8; |
|
|
345 | sl.buf_ [1] = sl.length () ; |
|
|
346 | |
|
|
347 | send (sl.buf_, sl.length () + sl.hdrlen); |
|
|
348 | } |
|
|
349 | |
|
|
350 | void |
|
|
351 | client::send_packet (const char *buf, int len) |
|
|
352 | { |
|
|
353 | packet sl; |
|
|
354 | |
|
|
355 | sl << data (buf, len); |
|
|
356 | send_packet (sl); |
|
|
357 | } |
|
|
358 | |
|
|
359 | void |
|
|
360 | client::send_packet (const char *buf) |
|
|
361 | { |
|
|
362 | send_packet (buf, strlen (buf)); |
431 | } |
363 | } |
432 | |
364 | |
433 | /****************************************************************************** |
365 | /****************************************************************************** |
434 | * |
366 | * |
435 | * statistics logging functions. |
367 | * statistics logging functions. |