… | |
… | |
35 | using namespace std; |
35 | using namespace std; |
36 | |
36 | |
37 | #include <global.h> |
37 | #include <global.h> |
38 | #include <newclient.h> |
38 | #include <newclient.h> |
39 | #include <sproto.h> |
39 | #include <sproto.h> |
|
|
40 | #include <cstdarg> |
40 | |
41 | |
41 | #ifdef __linux__ |
42 | #ifdef __linux__ |
42 | # include <sys/types.h> |
43 | # include <sys/types.h> |
43 | # include <sys/socket.h> |
44 | # include <sys/socket.h> |
44 | # include <netinet/in.h> |
45 | # include <netinet/in.h> |
… | |
… | |
50 | // easily die in 20 seconds... |
51 | // easily die in 20 seconds... |
51 | #define SOCKET_TIMEOUT1 10 |
52 | #define SOCKET_TIMEOUT1 10 |
52 | #define SOCKET_TIMEOUT2 20 |
53 | #define SOCKET_TIMEOUT2 20 |
53 | |
54 | |
54 | void |
55 | void |
55 | Socket_Flush (NewSocket * ns) |
56 | Socket_Flush (client_socket * ns) |
56 | { |
57 | { |
57 | #ifdef __linux__ |
58 | #ifdef __linux__ |
58 | // check time of last ack, and, if too old, kill connection |
59 | // check time of last ack, and, if too old, kill connection |
59 | struct tcp_info tcpi; |
60 | struct tcp_info tcpi; |
60 | socklen_t len = sizeof (tcpi); |
61 | socklen_t len = sizeof (tcpi); |
… | |
… | |
72 | } |
73 | } |
73 | } |
74 | } |
74 | |
75 | |
75 | int val; |
76 | int val; |
76 | |
77 | |
77 | val = 0; |
|
|
78 | setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
78 | val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
79 | val = 1; |
|
|
80 | setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
79 | val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
81 | #endif |
80 | #endif |
82 | } |
81 | } |
83 | |
82 | |
84 | /*********************************************************************** |
83 | /*********************************************************************** |
85 | * |
84 | * |
86 | * SockList functions/utilities |
85 | * packet functions/utilities |
87 | * |
86 | * |
88 | **********************************************************************/ |
87 | **********************************************************************/ |
89 | |
88 | |
90 | void |
89 | packet &packet::operator <<(const data &v) |
91 | SockList_Init (SockList * sl) |
|
|
92 | { |
90 | { |
93 | sl->len = 0; |
91 | if (room () < v.len) |
94 | sl->buf = NULL; |
92 | reset (); |
95 | } |
93 | else |
|
|
94 | { |
|
|
95 | if (v.len) |
|
|
96 | { |
|
|
97 | memcpy (cur, v.ptr, v.len); |
|
|
98 | cur += v.len; |
|
|
99 | } |
|
|
100 | } |
96 | |
101 | |
97 | void |
102 | return *this; |
98 | SockList_AddInt (SockList * sl, uint32 data) |
|
|
99 | { |
|
|
100 | sl->buf[sl->len++] = (data >> 24) & 0xff; |
|
|
101 | sl->buf[sl->len++] = (data >> 16) & 0xff; |
|
|
102 | sl->buf[sl->len++] = (data >> 8) & 0xff; |
|
|
103 | sl->buf[sl->len++] = data & 0xff; |
|
|
104 | } |
103 | } |
105 | |
104 | |
106 | void |
105 | packet &packet::operator <<(const data8 &v) |
107 | SockList_AddInt64 (SockList * sl, uint64 data) |
|
|
108 | { |
106 | { |
109 | sl->buf[sl->len++] = (char) ((data >> 56) & 0xff); |
107 | unsigned int len = min (v.len, 0x00FF); |
110 | sl->buf[sl->len++] = (char) ((data >> 48) & 0xff); |
108 | return *this << uint8 (len) << data (v.ptr, len); |
111 | sl->buf[sl->len++] = (char) ((data >> 40) & 0xff); |
|
|
112 | sl->buf[sl->len++] = (char) ((data >> 32) & 0xff); |
|
|
113 | |
|
|
114 | sl->buf[sl->len++] = (char) ((data >> 24) & 0xff); |
|
|
115 | sl->buf[sl->len++] = (char) ((data >> 16) & 0xff); |
|
|
116 | sl->buf[sl->len++] = (char) ((data >> 8) & 0xff); |
|
|
117 | sl->buf[sl->len++] = (char) (data & 0xff); |
|
|
118 | } |
109 | } |
119 | |
110 | |
120 | /* Basically does the reverse of SockList_AddInt, but on |
111 | packet &packet::operator <<(const data16 &v) |
121 | * strings instead. Same for the GetShort, but for 16 bits. |
112 | { |
|
|
113 | unsigned int len = min (v.len, 0xFFFF); |
|
|
114 | return *this << uint16 (len) << data (v.ptr, len); |
|
|
115 | } |
|
|
116 | |
|
|
117 | packet &packet::operator <<(const char *v) |
|
|
118 | { |
|
|
119 | return *this << data (v, strlen (v ? v : 0)); |
|
|
120 | } |
|
|
121 | |
|
|
122 | void |
|
|
123 | packet::printf (const char *format, ...) |
|
|
124 | { |
|
|
125 | int size = room (); |
|
|
126 | |
|
|
127 | va_list ap; |
|
|
128 | va_start (ap, format); |
|
|
129 | int len = vsnprintf ((char *)cur, size, format, ap); |
|
|
130 | va_end (ap); |
|
|
131 | |
|
|
132 | if (len >= size) |
|
|
133 | return reset (); |
|
|
134 | |
|
|
135 | cur += len; |
|
|
136 | } |
|
|
137 | |
|
|
138 | /****************************************************************************** |
122 | */ |
139 | * |
|
|
140 | * Start of read routines. |
|
|
141 | * |
|
|
142 | ******************************************************************************/ |
|
|
143 | |
123 | int |
144 | int |
124 | GetInt_String (unsigned char *data) |
145 | client_socket::read_packet () |
125 | { |
146 | { |
126 | return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); |
147 | for (;;) |
127 | } |
|
|
128 | |
|
|
129 | short |
|
|
130 | GetShort_String (unsigned char *data) |
|
|
131 | { |
|
|
132 | return ((data[0] << 8) + data[1]); |
|
|
133 | } |
|
|
134 | |
|
|
135 | /****************************************************************************** |
|
|
136 | * |
|
|
137 | * Start of read routines. |
|
|
138 | * |
|
|
139 | ******************************************************************************/ |
|
|
140 | |
|
|
141 | /** |
|
|
142 | * This reads from fd and puts the data in sl. We return true if we think |
|
|
143 | * we have a full packet, 0 if we have a partial packet. The only processing |
|
|
144 | * we do is remove the intial size value. len (As passed) is the size of the |
|
|
145 | * buffer allocated in the socklist. We make the assumption the buffer is |
|
|
146 | * at least 2 bytes long. |
|
|
147 | */ |
|
|
148 | |
|
|
149 | int |
|
|
150 | SockList_ReadPacket (int fd, SockList * sl, int len) |
|
|
151 | { |
|
|
152 | int stat, toread; |
|
|
153 | |
|
|
154 | /* Sanity check - shouldn't happen */ |
|
|
155 | if (sl->len < 0) |
|
|
156 | { |
|
|
157 | abort (); |
|
|
158 | } |
148 | { |
159 | /* We already have a partial packet */ |
149 | if (inbuf_len >= 2) |
160 | if (sl->len < 2) |
|
|
161 | { |
|
|
162 | do |
|
|
163 | { |
|
|
164 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
|
|
165 | } |
150 | { |
166 | while ((stat == -1) && (errno == EINTR)); |
151 | unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1]; |
167 | |
152 | |
168 | if (stat < 0) |
153 | if (inbuf_len >= 2 + pkt_len) |
|
|
154 | return pkt_len + 2; |
169 | { |
155 | } |
170 | /* In non blocking mode, EAGAIN is set when there is no |
156 | |
171 | * data available. |
157 | int amount = sizeof (inbuf) - inbuf_len; |
172 | */ |
158 | |
|
|
159 | if (amount <= 0) |
|
|
160 | { |
|
|
161 | LOG (llevError, "packet too large");//TODO |
|
|
162 | return -1; |
|
|
163 | } |
|
|
164 | |
|
|
165 | amount = read (fd, inbuf + inbuf_len, amount); |
|
|
166 | |
|
|
167 | if (!amount) |
|
|
168 | { |
|
|
169 | status = Ns_Dead; |
|
|
170 | return -1; |
|
|
171 | } |
|
|
172 | else if (amount < 0) |
|
|
173 | { |
173 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
174 | if (errno != EAGAIN && errno != EINTR) |
174 | { |
175 | { |
175 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
176 | LOG (llevError, "read error: %s", strerror (errno)); |
|
|
177 | return -1; |
176 | } |
178 | } |
177 | return 0; /*Error */ |
|
|
178 | } |
|
|
179 | if (stat == 0) |
|
|
180 | return -1; |
|
|
181 | sl->len += stat; |
|
|
182 | #ifdef CS_LOGSTATS |
|
|
183 | cst_tot.ibytes += stat; |
|
|
184 | cst_lst.ibytes += stat; |
|
|
185 | #endif |
|
|
186 | if (stat < 2) |
|
|
187 | return 0; /* Still don't have a full packet */ |
|
|
188 | } |
|
|
189 | /* Figure out how much more data we need to read. Add 2 from the |
|
|
190 | * end of this - size header information is not included. |
|
|
191 | */ |
|
|
192 | toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len; |
|
|
193 | if ((toread + sl->len) >= len) |
|
|
194 | { |
|
|
195 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
|
|
196 | /* Quick hack in case for 'oldsocketmode' input. If we are |
|
|
197 | * closing the socket anyways, then reading this extra 100 bytes |
|
|
198 | * shouldn't hurt. |
|
|
199 | */ |
|
|
200 | read (fd, sl->buf + 2, 100); |
|
|
201 | |
179 | |
202 | /* return error so the socket is closed */ |
|
|
203 | return -1; |
|
|
204 | } |
|
|
205 | do |
|
|
206 | { |
|
|
207 | do |
|
|
208 | { |
|
|
209 | stat = read (fd, sl->buf + sl->len, toread); |
|
|
210 | } |
|
|
211 | while ((stat < 0) && (errno == EINTR)); |
|
|
212 | if (stat < 0) |
|
|
213 | { |
|
|
214 | |
|
|
215 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
|
|
216 | { |
|
|
217 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
|
|
218 | } |
|
|
219 | return 0; /*Error */ |
|
|
220 | } |
|
|
221 | if (stat == 0) |
|
|
222 | return -1; |
|
|
223 | sl->len += stat; |
|
|
224 | #ifdef CS_LOGSTATS |
|
|
225 | cst_tot.ibytes += stat; |
|
|
226 | cst_lst.ibytes += stat; |
|
|
227 | #endif |
|
|
228 | toread -= stat; |
|
|
229 | if (toread == 0) |
|
|
230 | return 1; |
|
|
231 | if (toread < 0) |
|
|
232 | { |
|
|
233 | LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n"); |
|
|
234 | return 1; |
180 | return 0; |
235 | } |
181 | } |
|
|
182 | |
|
|
183 | inbuf_len += amount; |
|
|
184 | |
|
|
185 | cst_tot.ibytes += amount; |
|
|
186 | cst_lst.ibytes += amount; |
236 | } |
187 | } |
237 | while (toread > 0); |
188 | } |
238 | return 0; |
189 | |
|
|
190 | void |
|
|
191 | client_socket::skip_packet (int len) |
|
|
192 | { |
|
|
193 | inbuf_len -= len; |
|
|
194 | memmove (inbuf, inbuf + len, inbuf_len); |
239 | } |
195 | } |
240 | |
196 | |
241 | /******************************************************************************* |
197 | /******************************************************************************* |
242 | * |
198 | * |
243 | * Start of write related routines. |
199 | * Start of write related routines. |
… | |
… | |
250 | * 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 |
251 | * data, and len is the number of bytes to add. |
207 | * data, and len is the number of bytes to add. |
252 | */ |
208 | */ |
253 | |
209 | |
254 | static void |
210 | static void |
255 | add_to_buffer (NewSocket * ns, char *buf, int len) |
211 | add_to_buffer (client_socket *ns, char *buf, int len) |
256 | { |
212 | { |
257 | int avail, end; |
213 | int avail, end; |
258 | |
214 | |
259 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
215 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
260 | { |
216 | { |
… | |
… | |
294 | * |
250 | * |
295 | * 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 |
296 | * is called to write it out. |
252 | * is called to write it out. |
297 | */ |
253 | */ |
298 | void |
254 | void |
299 | write_socket_buffer (NewSocket * ns) |
255 | write_socket_buffer (client_socket * ns) |
300 | { |
256 | { |
301 | int amt, max; |
257 | int amt, max; |
302 | |
258 | |
303 | if (ns->outputbuffer.len == 0) |
259 | if (ns->outputbuffer.len == 0) |
304 | { |
260 | { |
… | |
… | |
353 | * 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 |
354 | * of bytes to write. IT doesn't return anything - rather, it |
310 | * of bytes to write. IT doesn't return anything - rather, it |
355 | * updates the ns structure if we get an error. |
311 | * updates the ns structure if we get an error. |
356 | */ |
312 | */ |
357 | void |
313 | void |
358 | Write_To_Socket (NewSocket * ns, char *buf, int len) |
314 | client_socket::send (void *buf_, int len) |
359 | { |
315 | { |
|
|
316 | char *buf = (char *)buf_; |
|
|
317 | char *pos = buf; |
360 | int amt = 0; |
318 | int amt = 0; |
361 | char *pos = buf; |
|
|
362 | |
319 | |
363 | if (ns->status == Ns_Dead || !buf) |
320 | if (status == Ns_Dead || !buf) |
364 | { |
321 | { |
365 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
322 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
366 | return; |
323 | return; |
367 | } |
324 | } |
368 | |
325 | |
369 | #ifndef __GNU__ /* This caused problems on Hurd */ |
326 | #ifndef __GNU__ /* This caused problems on Hurd */ |
370 | if (!ns->can_write) |
327 | if (!can_write) |
371 | { |
328 | { |
372 | add_to_buffer (ns, buf, len); |
329 | add_to_buffer (this, buf, len); |
373 | return; |
330 | return; |
374 | } |
331 | } |
375 | #endif |
332 | #endif |
|
|
333 | |
376 | /* 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 */ |
377 | while (len > 0) |
335 | while (len > 0) |
378 | { |
336 | { |
379 | |
|
|
380 | do |
337 | do |
381 | { |
338 | { |
382 | amt = write (ns->fd, pos, len); |
339 | amt = write (fd, pos, len); |
383 | } |
340 | } |
384 | while ((amt < 0) && (errno == EINTR)); |
341 | while ((amt < 0) && (errno == EINTR)); |
385 | |
342 | |
386 | if (amt < 0) |
343 | if (amt < 0) |
387 | { /* We got an error */ |
344 | { /* We got an error */ |
388 | if (errno != EWOULDBLOCK) |
345 | if (errno != EWOULDBLOCK) |
389 | { |
346 | { |
390 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
347 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
391 | errno, strerror (errno)); |
348 | errno, strerror (errno)); |
392 | ns->status = Ns_Dead; |
349 | status = Ns_Dead; |
393 | return; |
350 | return; |
394 | } |
351 | } |
395 | else |
352 | else |
396 | { /* EWOULDBLOCK */ |
353 | { /* EWOULDBLOCK */ |
397 | /* can't write it, so store it away. */ |
354 | /* can't write it, so store it away. */ |
398 | add_to_buffer (ns, pos, len); |
355 | add_to_buffer (this, pos, len); |
399 | ns->can_write = 0; |
356 | can_write = 0; |
400 | return; |
357 | return; |
401 | } |
358 | } |
402 | } |
359 | } |
403 | /* 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 |
404 | * an else if to make sure we don't reprocess it. |
361 | * an else if to make sure we don't reprocess it. |
405 | */ |
362 | */ |
406 | else if (amt == 0) |
363 | else if (amt == 0) |
407 | { |
|
|
408 | LOG (llevError, "Write_To_Socket: No data written out.\n"); |
364 | LOG (llevError, "Write_To_Socket: No data written out.\n"); |
409 | } |
365 | |
410 | len -= amt; |
366 | len -= amt; |
411 | pos += amt; |
367 | pos += amt; |
412 | #ifdef CS_LOGSTATS |
368 | #ifdef CS_LOGSTATS |
413 | cst_tot.obytes += amt; |
369 | cst_tot.obytes += amt; |
414 | cst_lst.obytes += amt; |
370 | cst_lst.obytes += amt; |
415 | #endif |
371 | #endif |
416 | } |
372 | } |
417 | } |
373 | } |
418 | |
374 | |
419 | |
|
|
420 | /** |
375 | /** |
421 | * Takes a string of data, and writes it out to the socket. A very handy |
376 | * Takes a string of data, and writes it out to the socket. A very handy |
422 | * shortcut function. |
377 | * shortcut function. |
423 | */ |
378 | */ |
424 | void |
|
|
425 | cs_write_string (NewSocket * ns, const char *buf, int len) |
|
|
426 | { |
|
|
427 | SockList sl; |
|
|
428 | |
379 | |
429 | sl.len = len; |
380 | void |
430 | sl.buf = (unsigned char *) buf; |
381 | client_socket::send_packet (packet &sl) |
|
|
382 | { |
431 | Send_With_Handling (ns, &sl); |
383 | Send_With_Handling (this, &sl); |
432 | } |
384 | } |
433 | |
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 | } |
434 | |
400 | |
435 | /** |
401 | /** |
436 | * Calls Write_To_Socket to send data to the client. |
402 | * Calls Write_To_Socket to send data to the client. |
437 | * |
403 | * |
438 | * The only difference in this function is that we take a SockList |
404 | * The only difference in this function is that we take a packet |
439 | *, and we prepend the length information. |
405 | *, and we prepend the length information. |
440 | */ |
406 | */ |
441 | void |
407 | void |
442 | Send_With_Handling (NewSocket * ns, SockList * msg) |
408 | Send_With_Handling (client_socket *ns, packet *msg) |
443 | { |
409 | { |
444 | unsigned char sbuf[4]; |
410 | unsigned char sbuf[4]; |
445 | |
411 | |
446 | if (ns->status == Ns_Dead || !msg) |
412 | if (ns->status == Ns_Dead || !msg) |
447 | return; |
413 | return; |
448 | |
414 | |
449 | if (msg->len >= MAXSOCKBUF) |
415 | if (msg->length () >= MAXSOCKBUF) |
450 | { |
416 | { |
451 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", msg->len); |
417 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", msg->length ()); |
452 | /* Almost certainly we've overflowed a buffer, so quite now to make |
418 | /* Almost certainly we've overflowed a buffer, so quite now to make |
453 | * it easier to debug. |
419 | * it easier to debug. |
454 | */ |
420 | */ |
455 | abort (); |
421 | abort (); |
456 | } |
422 | } |
|
|
423 | |
457 | sbuf[0] = ((uint32) (msg->len) >> 8) & 0xFF; |
424 | sbuf[0] = ((uint32) (msg->length ()) >> 8); |
458 | sbuf[1] = ((uint32) (msg->len)) & 0xFF; |
425 | sbuf[1] = ((uint32) (msg->length ()) ); |
459 | if (ns->status != Ns_Old) |
|
|
460 | Write_To_Socket (ns, (char *) sbuf, 2); |
|
|
461 | Write_To_Socket (ns, (char *) msg->buf, msg->len); |
|
|
462 | } |
|
|
463 | |
426 | |
464 | /** |
427 | //TODO: single write, or writev |
465 | * Takes a string of data, and writes it out to the socket. A very handy |
428 | ns->send (sbuf, 2); |
466 | * shortcut function. |
429 | ns->send (msg->buf, msg->length ()); |
467 | */ |
|
|
468 | void |
|
|
469 | Write_String_To_Socket (NewSocket * ns, char *buf, int len) |
|
|
470 | { |
|
|
471 | SockList sl; |
|
|
472 | |
|
|
473 | sl.len = len; |
|
|
474 | sl.buf = (unsigned char *) buf; |
|
|
475 | Send_With_Handling (ns, &sl); |
|
|
476 | } |
430 | } |
477 | |
|
|
478 | |
431 | |
479 | /****************************************************************************** |
432 | /****************************************************************************** |
480 | * |
433 | * |
481 | * statistics logging functions. |
434 | * statistics logging functions. |
482 | * |
435 | * |
… | |
… | |
505 | LOG (llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", |
458 | LOG (llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", |
506 | ctime (&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, |
459 | ctime (&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, |
507 | now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, cst_lst.max_conn, now - cst_lst.time_start); |
460 | now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, cst_lst.max_conn, now - cst_lst.time_start); |
508 | cst_lst.ibytes = 0; |
461 | cst_lst.ibytes = 0; |
509 | cst_lst.obytes = 0; |
462 | cst_lst.obytes = 0; |
510 | cst_lst.max_conn = socket_info.nconns; |
|
|
511 | cst_lst.time_start = now; |
463 | cst_lst.time_start = now; |
512 | } |
464 | } |
513 | #endif |
465 | #endif |
|
|
466 | |