… | |
… | |
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 | |
|
|
123 | len += vsprintf ((char *)buf + len, format, ap); |
129 | int len = vsnprintf ((char *)cur, size, format, ap); |
124 | |
|
|
125 | va_end (ap); |
130 | va_end (ap); |
126 | } |
|
|
127 | |
131 | |
128 | /* Basically does the reverse of SockList_AddInt, but on |
132 | if (len >= size) |
129 | * strings instead. Same for the GetShort, but for 16 bits. |
133 | return reset (); |
|
|
134 | |
|
|
135 | cur += len; |
|
|
136 | } |
|
|
137 | |
|
|
138 | /****************************************************************************** |
130 | */ |
139 | * |
|
|
140 | * Start of read routines. |
|
|
141 | * |
|
|
142 | ******************************************************************************/ |
|
|
143 | |
131 | int |
144 | int |
132 | GetInt_String (unsigned char *data) |
145 | client_socket::read_packet () |
133 | { |
146 | { |
134 | return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); |
147 | for (;;) |
135 | } |
|
|
136 | |
|
|
137 | short |
|
|
138 | GetShort_String (unsigned char *data) |
|
|
139 | { |
|
|
140 | return ((data[0] << 8) + data[1]); |
|
|
141 | } |
|
|
142 | |
|
|
143 | /****************************************************************************** |
|
|
144 | * |
|
|
145 | * Start of read routines. |
|
|
146 | * |
|
|
147 | ******************************************************************************/ |
|
|
148 | |
|
|
149 | /** |
|
|
150 | * This reads from fd and puts the data in sl. We return true if we think |
|
|
151 | * we have a full packet, 0 if we have a partial packet. The only processing |
|
|
152 | * we do is remove the intial size value. len (As passed) is the size of the |
|
|
153 | * buffer allocated in the socklist. We make the assumption the buffer is |
|
|
154 | * at least 2 bytes long. |
|
|
155 | */ |
|
|
156 | |
|
|
157 | int |
|
|
158 | SockList_ReadPacket (int fd, SockList *sl, int len) |
|
|
159 | { |
|
|
160 | int stat, toread; |
|
|
161 | |
|
|
162 | /* Sanity check - shouldn't happen */ |
|
|
163 | if (sl->len < 0) |
|
|
164 | { |
|
|
165 | abort (); |
|
|
166 | } |
148 | { |
167 | /* We already have a partial packet */ |
149 | if (inbuf_len >= 2) |
168 | if (sl->len < 2) |
|
|
169 | { |
|
|
170 | do |
|
|
171 | { |
|
|
172 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
|
|
173 | } |
150 | { |
174 | while ((stat == -1) && (errno == EINTR)); |
151 | unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1]; |
175 | |
152 | |
176 | if (stat < 0) |
153 | if (inbuf_len >= 2 + pkt_len) |
|
|
154 | return pkt_len + 2; |
177 | { |
155 | } |
178 | /* In non blocking mode, EAGAIN is set when there is no |
156 | |
179 | * data available. |
157 | int amount = sizeof (inbuf) - inbuf_len; |
180 | */ |
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 | { |
181 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
174 | if (errno != EAGAIN && errno != EINTR) |
182 | { |
175 | { |
183 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
176 | LOG (llevError, "read error: %s", strerror (errno)); |
|
|
177 | return -1; |
184 | } |
178 | } |
185 | return 0; /*Error */ |
|
|
186 | } |
|
|
187 | if (stat == 0) |
|
|
188 | return -1; |
|
|
189 | sl->len += stat; |
|
|
190 | #ifdef CS_LOGSTATS |
|
|
191 | cst_tot.ibytes += stat; |
|
|
192 | cst_lst.ibytes += stat; |
|
|
193 | #endif |
|
|
194 | if (stat < 2) |
|
|
195 | return 0; /* Still don't have a full packet */ |
|
|
196 | } |
|
|
197 | /* Figure out how much more data we need to read. Add 2 from the |
|
|
198 | * end of this - size header information is not included. |
|
|
199 | */ |
|
|
200 | toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len; |
|
|
201 | if ((toread + sl->len) >= len) |
|
|
202 | { |
|
|
203 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
|
|
204 | /* Quick hack in case for 'oldsocketmode' input. If we are |
|
|
205 | * closing the socket anyways, then reading this extra 100 bytes |
|
|
206 | * shouldn't hurt. |
|
|
207 | */ |
|
|
208 | read (fd, sl->buf + 2, 100); |
|
|
209 | |
179 | |
210 | /* return error so the socket is closed */ |
|
|
211 | return -1; |
|
|
212 | } |
|
|
213 | do |
|
|
214 | { |
|
|
215 | do |
|
|
216 | { |
|
|
217 | stat = read (fd, sl->buf + sl->len, toread); |
|
|
218 | } |
|
|
219 | while ((stat < 0) && (errno == EINTR)); |
|
|
220 | if (stat < 0) |
|
|
221 | { |
|
|
222 | |
|
|
223 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
|
|
224 | { |
|
|
225 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
|
|
226 | } |
|
|
227 | return 0; /*Error */ |
|
|
228 | } |
|
|
229 | if (stat == 0) |
|
|
230 | return -1; |
|
|
231 | sl->len += stat; |
|
|
232 | #ifdef CS_LOGSTATS |
|
|
233 | cst_tot.ibytes += stat; |
|
|
234 | cst_lst.ibytes += stat; |
|
|
235 | #endif |
|
|
236 | toread -= stat; |
|
|
237 | if (toread == 0) |
|
|
238 | return 1; |
|
|
239 | if (toread < 0) |
|
|
240 | { |
|
|
241 | LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n"); |
|
|
242 | return 1; |
180 | return 0; |
243 | } |
181 | } |
|
|
182 | |
|
|
183 | inbuf_len += amount; |
|
|
184 | |
|
|
185 | cst_tot.ibytes += amount; |
|
|
186 | cst_lst.ibytes += amount; |
244 | } |
187 | } |
245 | while (toread > 0); |
188 | } |
246 | return 0; |
189 | |
|
|
190 | void |
|
|
191 | client_socket::skip_packet (int len) |
|
|
192 | { |
|
|
193 | inbuf_len -= len; |
|
|
194 | memmove (inbuf, inbuf + len, inbuf_len); |
247 | } |
195 | } |
248 | |
196 | |
249 | /******************************************************************************* |
197 | /******************************************************************************* |
250 | * |
198 | * |
251 | * Start of write related routines. |
199 | * Start of write related routines. |
… | |
… | |
258 | * 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 |
259 | * data, and len is the number of bytes to add. |
207 | * data, and len is the number of bytes to add. |
260 | */ |
208 | */ |
261 | |
209 | |
262 | static void |
210 | static void |
263 | add_to_buffer (NewSocket * ns, char *buf, int len) |
211 | add_to_buffer (client_socket *ns, char *buf, int len) |
264 | { |
212 | { |
265 | int avail, end; |
213 | int avail, end; |
266 | |
214 | |
267 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
215 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
268 | { |
216 | { |
… | |
… | |
302 | * |
250 | * |
303 | * 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 |
304 | * is called to write it out. |
252 | * is called to write it out. |
305 | */ |
253 | */ |
306 | void |
254 | void |
307 | write_socket_buffer (NewSocket * ns) |
255 | write_socket_buffer (client_socket * ns) |
308 | { |
256 | { |
309 | int amt, max; |
257 | int amt, max; |
310 | |
258 | |
311 | if (ns->outputbuffer.len == 0) |
259 | if (ns->outputbuffer.len == 0) |
312 | { |
260 | { |
… | |
… | |
361 | * 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 |
362 | * of bytes to write. IT doesn't return anything - rather, it |
310 | * of bytes to write. IT doesn't return anything - rather, it |
363 | * updates the ns structure if we get an error. |
311 | * updates the ns structure if we get an error. |
364 | */ |
312 | */ |
365 | void |
313 | void |
366 | Write_To_Socket (NewSocket * ns, char *buf, int len) |
314 | client_socket::send (void *buf_, int len) |
367 | { |
315 | { |
|
|
316 | char *buf = (char *)buf_; |
|
|
317 | char *pos = buf; |
368 | int amt = 0; |
318 | int amt = 0; |
369 | char *pos = buf; |
|
|
370 | |
319 | |
371 | if (ns->status == Ns_Dead || !buf) |
320 | if (status == Ns_Dead || !buf) |
372 | { |
321 | { |
373 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
322 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
374 | return; |
323 | return; |
375 | } |
324 | } |
376 | |
325 | |
377 | #ifndef __GNU__ /* This caused problems on Hurd */ |
326 | #ifndef __GNU__ /* This caused problems on Hurd */ |
378 | if (!ns->can_write) |
327 | if (!can_write) |
379 | { |
328 | { |
380 | add_to_buffer (ns, buf, len); |
329 | add_to_buffer (this, buf, len); |
381 | return; |
330 | return; |
382 | } |
331 | } |
383 | #endif |
332 | #endif |
|
|
333 | |
384 | /* 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 */ |
385 | while (len > 0) |
335 | while (len > 0) |
386 | { |
336 | { |
387 | |
|
|
388 | do |
337 | do |
389 | { |
338 | { |
390 | amt = write (ns->fd, pos, len); |
339 | amt = write (fd, pos, len); |
391 | } |
340 | } |
392 | while ((amt < 0) && (errno == EINTR)); |
341 | while ((amt < 0) && (errno == EINTR)); |
393 | |
342 | |
394 | if (amt < 0) |
343 | if (amt < 0) |
395 | { /* We got an error */ |
344 | { /* We got an error */ |
396 | if (errno != EWOULDBLOCK) |
345 | if (errno != EWOULDBLOCK) |
397 | { |
346 | { |
398 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
347 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
399 | errno, strerror (errno)); |
348 | errno, strerror (errno)); |
400 | ns->status = Ns_Dead; |
349 | status = Ns_Dead; |
401 | return; |
350 | return; |
402 | } |
351 | } |
403 | else |
352 | else |
404 | { /* EWOULDBLOCK */ |
353 | { /* EWOULDBLOCK */ |
405 | /* can't write it, so store it away. */ |
354 | /* can't write it, so store it away. */ |
406 | add_to_buffer (ns, pos, len); |
355 | add_to_buffer (this, pos, len); |
407 | ns->can_write = 0; |
356 | can_write = 0; |
408 | return; |
357 | return; |
409 | } |
358 | } |
410 | } |
359 | } |
411 | /* 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 |
412 | * an else if to make sure we don't reprocess it. |
361 | * an else if to make sure we don't reprocess it. |
413 | */ |
362 | */ |
414 | else if (amt == 0) |
363 | else if (amt == 0) |
415 | { |
|
|
416 | LOG (llevError, "Write_To_Socket: No data written out.\n"); |
364 | LOG (llevError, "Write_To_Socket: No data written out.\n"); |
417 | } |
365 | |
418 | len -= amt; |
366 | len -= amt; |
419 | pos += amt; |
367 | pos += amt; |
420 | #ifdef CS_LOGSTATS |
368 | #ifdef CS_LOGSTATS |
421 | cst_tot.obytes += amt; |
369 | cst_tot.obytes += amt; |
422 | cst_lst.obytes += amt; |
370 | cst_lst.obytes += amt; |
423 | #endif |
371 | #endif |
424 | } |
372 | } |
425 | } |
373 | } |
426 | |
374 | |
|
|
375 | void |
|
|
376 | client_socket::socket_cb (iow &w, int got) |
|
|
377 | { |
|
|
378 | //printf ("iow got %x\n", got); |
|
|
379 | w.stop (); |
|
|
380 | } |
427 | |
381 | |
428 | /** |
382 | /** |
429 | * 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 |
430 | * shortcut function. |
384 | * shortcut function. |
431 | */ |
385 | */ |
432 | void |
386 | void |
433 | cs_write_string (NewSocket *ns, const char *buf, int len) |
387 | client_socket::send_packet (packet &sl) |
434 | { |
388 | { |
435 | SockList sl; |
|
|
436 | |
|
|
437 | sl.len = len; |
|
|
438 | sl.buf = (unsigned char *) buf; |
|
|
439 | Send_With_Handling (ns, &sl); |
|
|
440 | } |
|
|
441 | |
|
|
442 | |
|
|
443 | /** |
|
|
444 | * Calls Write_To_Socket to send data to the client. |
|
|
445 | * |
|
|
446 | * The only difference in this function is that we take a SockList |
|
|
447 | *, and we prepend the length information. |
|
|
448 | */ |
|
|
449 | void |
|
|
450 | Send_With_Handling (NewSocket *ns, SockList *msg) |
|
|
451 | { |
|
|
452 | unsigned char sbuf[4]; |
|
|
453 | |
|
|
454 | if (ns->status == Ns_Dead || !msg) |
389 | if (status == Ns_Dead) |
455 | return; |
390 | return; |
456 | |
391 | |
457 | if (msg->len >= MAXSOCKBUF) |
392 | if (sl.length () >= MAXSOCKBUF) |
458 | { |
393 | { |
459 | 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 ()); |
460 | /* 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 |
461 | * it easier to debug. |
396 | * it easier to debug. |
462 | */ |
397 | */ |
463 | abort (); |
398 | abort (); |
464 | } |
399 | } |
465 | sbuf[0] = ((uint32) (msg->len) >> 8) & 0xFF; |
|
|
466 | sbuf[1] = ((uint32) (msg->len)) & 0xFF; |
|
|
467 | if (ns->status != Ns_Old) |
|
|
468 | Write_To_Socket (ns, (char *) sbuf, 2); |
|
|
469 | Write_To_Socket (ns, (char *) msg->buf, msg->len); |
|
|
470 | } |
|
|
471 | |
400 | |
472 | /** |
401 | if (!sl.length ()) |
473 | * Takes a string of data, and writes it out to the socket. A very handy |
402 | return; |
474 | * shortcut function. |
|
|
475 | */ |
|
|
476 | void |
|
|
477 | Write_String_To_Socket (NewSocket *ns, char *buf, int len) |
|
|
478 | { |
|
|
479 | SockList sl; |
|
|
480 | |
403 | |
481 | sl.len = len; |
404 | assert (sl.hdrlen == 2); |
482 | sl.buf = (unsigned char *) buf; |
|
|
483 | Send_With_Handling (ns, &sl); |
|
|
484 | } |
|
|
485 | |
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)); |
|
|
425 | } |
486 | |
426 | |
487 | /****************************************************************************** |
427 | /****************************************************************************** |
488 | * |
428 | * |
489 | * statistics logging functions. |
429 | * statistics logging functions. |
490 | * |
430 | * |
… | |
… | |
513 | 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", |
514 | 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, |
515 | 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); |
516 | cst_lst.ibytes = 0; |
456 | cst_lst.ibytes = 0; |
517 | cst_lst.obytes = 0; |
457 | cst_lst.obytes = 0; |
518 | cst_lst.max_conn = socket_info.nconns; |
|
|
519 | cst_lst.time_start = now; |
458 | cst_lst.time_start = now; |
520 | } |
459 | } |
521 | #endif |
460 | #endif |
522 | |
461 | |