… | |
… | |
51 | // easily die in 20 seconds... |
51 | // easily die in 20 seconds... |
52 | #define SOCKET_TIMEOUT1 10 |
52 | #define SOCKET_TIMEOUT1 10 |
53 | #define SOCKET_TIMEOUT2 20 |
53 | #define SOCKET_TIMEOUT2 20 |
54 | |
54 | |
55 | void |
55 | void |
56 | Socket_Flush (NewSocket * ns) |
56 | Socket_Flush (client_socket * ns) |
57 | { |
57 | { |
58 | #ifdef __linux__ |
58 | #ifdef __linux__ |
59 | // check time of last ack, and, if too old, kill connection |
59 | // check time of last ack, and, if too old, kill connection |
60 | struct tcp_info tcpi; |
60 | struct tcp_info tcpi; |
61 | socklen_t len = sizeof (tcpi); |
61 | socklen_t len = sizeof (tcpi); |
… | |
… | |
80 | #endif |
80 | #endif |
81 | } |
81 | } |
82 | |
82 | |
83 | /*********************************************************************** |
83 | /*********************************************************************** |
84 | * |
84 | * |
85 | * SockList functions/utilities |
85 | * packet functions/utilities |
86 | * |
86 | * |
87 | **********************************************************************/ |
87 | **********************************************************************/ |
88 | |
88 | |
89 | SockList &SockList::operator <<(const data &v) |
89 | packet &packet::operator <<(const data &v) |
90 | { |
90 | { |
|
|
91 | if (room () < v.len) |
|
|
92 | reset (); |
|
|
93 | else |
|
|
94 | { |
91 | if (v.len) |
95 | if (v.len) |
92 | { |
96 | { |
93 | memcpy (buf + len, v.ptr, v.len); |
97 | memcpy (cur, v.ptr, v.len); |
94 | len += v.len; |
98 | cur += v.len; |
|
|
99 | } |
95 | } |
100 | } |
96 | |
101 | |
97 | return *this; |
102 | return *this; |
98 | } |
103 | } |
99 | |
104 | |
100 | SockList &SockList::operator <<(const data8 &v) |
105 | packet &packet::operator <<(const data8 &v) |
101 | { |
106 | { |
102 | unsigned int len = min (v.len, 0x00FF); |
107 | unsigned int len = min (v.len, 0x00FF); |
103 | return *this << uint8 (len) << data (v.ptr, len); |
108 | return *this << uint8 (len) << data (v.ptr, len); |
104 | } |
109 | } |
105 | |
110 | |
106 | SockList &SockList::operator <<(const data16 &v) |
111 | packet &packet::operator <<(const data16 &v) |
107 | { |
112 | { |
108 | unsigned int len = min (v.len, 0xFFFF); |
113 | unsigned int len = min (v.len, 0xFFFF); |
109 | return *this << uint16 (len) << data (v.ptr, len); |
114 | return *this << uint16 (len) << data (v.ptr, len); |
110 | } |
115 | } |
111 | |
116 | |
112 | SockList &SockList::operator <<(const char *v) |
117 | packet &packet::operator <<(const char *v) |
113 | { |
118 | { |
114 | return *this << data (v, strlen (v ? v : 0)); |
119 | return *this << data (v, strlen (v ? v : 0)); |
115 | } |
120 | } |
116 | |
121 | |
117 | void |
122 | void |
118 | SockList::printf (const char *format, ...) |
123 | packet::printf (const char *format, ...) |
119 | { |
124 | { |
|
|
125 | int size = room (); |
|
|
126 | |
120 | va_list ap; |
127 | va_list ap; |
121 | va_start (ap, format); |
128 | va_start (ap, format); |
122 | |
129 | int len = vsnprintf ((char *)cur, size, format, ap); |
123 | len += vsnprintf ((char *)buf + len, MAXSOCKBUF, format, ap); |
|
|
124 | |
|
|
125 | va_end (ap); |
130 | va_end (ap); |
|
|
131 | |
|
|
132 | if (len >= size) |
|
|
133 | return reset (); |
|
|
134 | |
|
|
135 | cur += len; |
126 | } |
136 | } |
127 | |
137 | |
128 | /****************************************************************************** |
138 | /****************************************************************************** |
129 | * |
139 | * |
130 | * Start of read routines. |
140 | * Start of read routines. |
131 | * |
141 | * |
132 | ******************************************************************************/ |
142 | ******************************************************************************/ |
133 | |
143 | |
134 | int |
144 | int |
135 | NewSocket::read_packet () |
145 | client_socket::read_packet () |
136 | { |
146 | { |
137 | for (;;) |
147 | for (;;) |
138 | { |
148 | { |
139 | if (inbuf_len >= 2) |
149 | if (inbuf_len >= 2) |
140 | { |
150 | { |
… | |
… | |
176 | cst_lst.ibytes += amount; |
186 | cst_lst.ibytes += amount; |
177 | } |
187 | } |
178 | } |
188 | } |
179 | |
189 | |
180 | void |
190 | void |
181 | NewSocket::skip_packet (int len) |
191 | client_socket::skip_packet (int len) |
182 | { |
192 | { |
183 | inbuf_len -= len; |
193 | inbuf_len -= len; |
184 | memmove (inbuf, inbuf + len, inbuf_len); |
194 | memmove (inbuf, inbuf + len, inbuf_len); |
185 | } |
195 | } |
186 | |
196 | |
… | |
… | |
196 | * ns is the socket we are adding the data to, buf is the start of the |
206 | * ns is the socket we are adding the data to, buf is the start of the |
197 | * data, and len is the number of bytes to add. |
207 | * data, and len is the number of bytes to add. |
198 | */ |
208 | */ |
199 | |
209 | |
200 | static void |
210 | static void |
201 | add_to_buffer (NewSocket *ns, char *buf, int len) |
211 | add_to_buffer (client_socket *ns, char *buf, int len) |
202 | { |
212 | { |
203 | int avail, end; |
213 | int avail, end; |
204 | |
214 | |
205 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
215 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
206 | { |
216 | { |
… | |
… | |
240 | * |
250 | * |
241 | * When the socket is clear to write, and we have backlogged data, this |
251 | * When the socket is clear to write, and we have backlogged data, this |
242 | * is called to write it out. |
252 | * is called to write it out. |
243 | */ |
253 | */ |
244 | void |
254 | void |
245 | write_socket_buffer (NewSocket * ns) |
255 | write_socket_buffer (client_socket * ns) |
246 | { |
256 | { |
247 | int amt, max; |
257 | int amt, max; |
248 | |
258 | |
249 | if (ns->outputbuffer.len == 0) |
259 | if (ns->outputbuffer.len == 0) |
250 | { |
260 | { |
… | |
… | |
299 | * provided (ns). buf is the data to write, len is the number |
309 | * provided (ns). buf is the data to write, len is the number |
300 | * of bytes to write. IT doesn't return anything - rather, it |
310 | * of bytes to write. IT doesn't return anything - rather, it |
301 | * updates the ns structure if we get an error. |
311 | * updates the ns structure if we get an error. |
302 | */ |
312 | */ |
303 | void |
313 | void |
304 | Write_To_Socket (NewSocket * ns, char *buf, int len) |
314 | client_socket::send (void *buf_, int len) |
305 | { |
315 | { |
|
|
316 | char *buf = (char *)buf_; |
|
|
317 | char *pos = buf; |
306 | int amt = 0; |
318 | int amt = 0; |
307 | char *pos = buf; |
|
|
308 | |
319 | |
309 | if (ns->status == Ns_Dead || !buf) |
320 | if (status == Ns_Dead || !buf) |
310 | { |
321 | { |
311 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
322 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
312 | return; |
323 | return; |
313 | } |
324 | } |
314 | |
325 | |
315 | #ifndef __GNU__ /* This caused problems on Hurd */ |
326 | #ifndef __GNU__ /* This caused problems on Hurd */ |
316 | if (!ns->can_write) |
327 | if (!can_write) |
317 | { |
328 | { |
318 | add_to_buffer (ns, buf, len); |
329 | add_to_buffer (this, buf, len); |
319 | return; |
330 | return; |
320 | } |
331 | } |
321 | #endif |
332 | #endif |
|
|
333 | |
322 | /* If we manage to write more than we wanted, take it as a bonus */ |
334 | /* If we manage to write more than we wanted, take it as a bonus */ |
323 | while (len > 0) |
335 | while (len > 0) |
324 | { |
336 | { |
325 | |
|
|
326 | do |
337 | do |
327 | { |
338 | { |
328 | amt = write (ns->fd, pos, len); |
339 | amt = write (fd, pos, len); |
329 | } |
340 | } |
330 | while ((amt < 0) && (errno == EINTR)); |
341 | while ((amt < 0) && (errno == EINTR)); |
331 | |
342 | |
332 | if (amt < 0) |
343 | if (amt < 0) |
333 | { /* We got an error */ |
344 | { /* We got an error */ |
334 | if (errno != EWOULDBLOCK) |
345 | if (errno != EWOULDBLOCK) |
335 | { |
346 | { |
336 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
347 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
337 | errno, strerror (errno)); |
348 | errno, strerror (errno)); |
338 | ns->status = Ns_Dead; |
349 | status = Ns_Dead; |
339 | return; |
350 | return; |
340 | } |
351 | } |
341 | else |
352 | else |
342 | { /* EWOULDBLOCK */ |
353 | { /* EWOULDBLOCK */ |
343 | /* can't write it, so store it away. */ |
354 | /* can't write it, so store it away. */ |
344 | add_to_buffer (ns, pos, len); |
355 | add_to_buffer (this, pos, len); |
345 | ns->can_write = 0; |
356 | can_write = 0; |
346 | return; |
357 | return; |
347 | } |
358 | } |
348 | } |
359 | } |
349 | /* amt gets set to 0 above in blocking code, so we do this as |
360 | /* amt gets set to 0 above in blocking code, so we do this as |
350 | * an else if to make sure we don't reprocess it. |
361 | * an else if to make sure we don't reprocess it. |
351 | */ |
362 | */ |
352 | else if (amt == 0) |
363 | else if (amt == 0) |
353 | { |
|
|
354 | LOG (llevError, "Write_To_Socket: No data written out.\n"); |
364 | LOG (llevError, "Write_To_Socket: No data written out.\n"); |
355 | } |
365 | |
356 | len -= amt; |
366 | len -= amt; |
357 | pos += amt; |
367 | pos += amt; |
358 | #ifdef CS_LOGSTATS |
368 | #ifdef CS_LOGSTATS |
359 | cst_tot.obytes += amt; |
369 | cst_tot.obytes += amt; |
360 | cst_lst.obytes += amt; |
370 | cst_lst.obytes += amt; |
361 | #endif |
371 | #endif |
362 | } |
372 | } |
363 | } |
373 | } |
364 | |
374 | |
|
|
375 | void |
|
|
376 | client_socket::socket_cb (iow &w, int got) |
|
|
377 | { |
|
|
378 | //printf ("iow got %x\n", got); |
|
|
379 | w.stop (); |
|
|
380 | } |
|
|
381 | |
365 | /** |
382 | /** |
366 | * Takes a string of data, and writes it out to the socket. A very handy |
383 | * Takes a string of data, and writes it out to the socket. A very handy |
367 | * shortcut function. |
384 | * shortcut function. |
368 | */ |
385 | */ |
369 | |
|
|
370 | void |
386 | void |
371 | NewSocket::send_packet (SockList &sl) |
387 | client_socket::send_packet (packet &sl) |
372 | { |
388 | { |
373 | Send_With_Handling (this, &sl); |
|
|
374 | } |
|
|
375 | |
|
|
376 | void |
|
|
377 | NewSocket::send_packet (const char *buf, int len) |
|
|
378 | { |
|
|
379 | SockList sl (MAXSOCKBUF); |
|
|
380 | |
|
|
381 | sl << data (buf, len); |
|
|
382 | send_packet (sl); |
|
|
383 | sl.free (); |
|
|
384 | } |
|
|
385 | |
|
|
386 | void |
|
|
387 | NewSocket::send_packet (const char *buf) |
|
|
388 | { |
|
|
389 | send_packet (buf, strlen (buf)); |
|
|
390 | } |
|
|
391 | |
|
|
392 | /** |
|
|
393 | * Calls Write_To_Socket to send data to the client. |
|
|
394 | * |
|
|
395 | * The only difference in this function is that we take a SockList |
|
|
396 | *, and we prepend the length information. |
|
|
397 | */ |
|
|
398 | void |
|
|
399 | Send_With_Handling (NewSocket *ns, SockList *msg) |
|
|
400 | { |
|
|
401 | unsigned char sbuf[4]; |
|
|
402 | |
|
|
403 | if (ns->status == Ns_Dead || !msg) |
389 | if (status == Ns_Dead) |
404 | return; |
390 | return; |
405 | |
391 | |
406 | if (msg->len >= MAXSOCKBUF) |
392 | if (sl.length () >= MAXSOCKBUF) |
407 | { |
393 | { |
408 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", msg->len); |
394 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); |
409 | /* Almost certainly we've overflowed a buffer, so quite now to make |
395 | /* Almost certainly we've overflowed a buffer, so quit now to make |
410 | * it easier to debug. |
396 | * it easier to debug. |
411 | */ |
397 | */ |
412 | abort (); |
398 | abort (); |
413 | } |
399 | } |
414 | sbuf[0] = ((uint32) (msg->len) >> 8) & 0xFF; |
400 | |
415 | sbuf[1] = ((uint32) (msg->len)) & 0xFF; |
401 | if (!sl.length ()) |
416 | if (ns->status != Ns_Old) |
402 | return; |
417 | Write_To_Socket (ns, (char *) sbuf, 2); |
403 | |
418 | Write_To_Socket (ns, (char *) msg->buf, msg->len); |
404 | assert (sl.hdrlen == 2); |
|
|
405 | |
|
|
406 | sl.buf_ [0] = sl.length () >> 8; |
|
|
407 | sl.buf_ [1] = sl.length () ; |
|
|
408 | |
|
|
409 | send (sl.buf_, sl.length () + sl.hdrlen); |
|
|
410 | } |
|
|
411 | |
|
|
412 | void |
|
|
413 | client_socket::send_packet (const char *buf, int len) |
|
|
414 | { |
|
|
415 | packet sl; |
|
|
416 | |
|
|
417 | sl << data (buf, len); |
|
|
418 | send_packet (sl); |
|
|
419 | } |
|
|
420 | |
|
|
421 | void |
|
|
422 | client_socket::send_packet (const char *buf) |
|
|
423 | { |
|
|
424 | send_packet (buf, strlen (buf)); |
419 | } |
425 | } |
420 | |
426 | |
421 | /****************************************************************************** |
427 | /****************************************************************************** |
422 | * |
428 | * |
423 | * statistics logging functions. |
429 | * statistics logging functions. |
… | |
… | |
447 | LOG (llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", |
453 | LOG (llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", |
448 | ctime (&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, |
454 | ctime (&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, |
449 | now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, cst_lst.max_conn, now - cst_lst.time_start); |
455 | now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, cst_lst.max_conn, now - cst_lst.time_start); |
450 | cst_lst.ibytes = 0; |
456 | cst_lst.ibytes = 0; |
451 | cst_lst.obytes = 0; |
457 | cst_lst.obytes = 0; |
452 | cst_lst.max_conn = socket_info.nconns; |
|
|
453 | cst_lst.time_start = now; |
458 | cst_lst.time_start = now; |
454 | } |
459 | } |
455 | #endif |
460 | #endif |
456 | |
461 | |