… | |
… | |
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); |
… | |
… | |
79 | #endif |
80 | #endif |
80 | } |
81 | } |
81 | |
82 | |
82 | /*********************************************************************** |
83 | /*********************************************************************** |
83 | * |
84 | * |
84 | * SockList functions/utilities |
85 | * packet functions/utilities |
85 | * |
86 | * |
86 | **********************************************************************/ |
87 | **********************************************************************/ |
87 | |
88 | |
88 | SockList &SockList::operator <<(const data8 &v) |
89 | packet &packet::operator <<(const data &v) |
89 | { |
90 | { |
90 | *this << uint8 (v.len); |
91 | if (room () < v.len) |
91 | |
92 | reset (); |
92 | memcpy (buf + len, v.data, v.len); |
93 | else |
93 | len += v.len; |
94 | { |
|
|
95 | if (v.len) |
|
|
96 | { |
|
|
97 | memcpy (cur, v.ptr, v.len); |
|
|
98 | cur += v.len; |
|
|
99 | } |
|
|
100 | } |
94 | |
101 | |
95 | return *this; |
102 | return *this; |
96 | } |
103 | } |
97 | |
104 | |
|
|
105 | packet &packet::operator <<(const data8 &v) |
|
|
106 | { |
|
|
107 | unsigned int len = min (v.len, 0x00FF); |
|
|
108 | return *this << uint8 (len) << data (v.ptr, len); |
|
|
109 | } |
|
|
110 | |
98 | SockList &SockList::operator <<(const data16 &v) |
111 | packet &packet::operator <<(const data16 &v) |
99 | { |
112 | { |
100 | *this << uint16 (v.len); |
113 | unsigned int len = min (v.len, 0xFFFF); |
101 | |
114 | return *this << uint16 (len) << data (v.ptr, len); |
102 | memcpy (buf + len, v.data, v.len); |
|
|
103 | len += v.len; |
|
|
104 | |
|
|
105 | return *this; |
|
|
106 | } |
115 | } |
107 | |
116 | |
108 | /* Basically does the reverse of SockList_AddInt, but on |
117 | packet &packet::operator <<(const char *v) |
109 | * strings instead. Same for the GetShort, but for 16 bits. |
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 | /****************************************************************************** |
110 | */ |
139 | * |
|
|
140 | * Start of read routines. |
|
|
141 | * |
|
|
142 | ******************************************************************************/ |
|
|
143 | |
111 | int |
144 | int |
112 | GetInt_String (unsigned char *data) |
145 | client_socket::read_packet () |
113 | { |
146 | { |
114 | return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); |
147 | for (;;) |
115 | } |
|
|
116 | |
|
|
117 | short |
|
|
118 | GetShort_String (unsigned char *data) |
|
|
119 | { |
|
|
120 | return ((data[0] << 8) + data[1]); |
|
|
121 | } |
|
|
122 | |
|
|
123 | /****************************************************************************** |
|
|
124 | * |
|
|
125 | * Start of read routines. |
|
|
126 | * |
|
|
127 | ******************************************************************************/ |
|
|
128 | |
|
|
129 | /** |
|
|
130 | * This reads from fd and puts the data in sl. We return true if we think |
|
|
131 | * we have a full packet, 0 if we have a partial packet. The only processing |
|
|
132 | * we do is remove the intial size value. len (As passed) is the size of the |
|
|
133 | * buffer allocated in the socklist. We make the assumption the buffer is |
|
|
134 | * at least 2 bytes long. |
|
|
135 | */ |
|
|
136 | |
|
|
137 | int |
|
|
138 | SockList_ReadPacket (int fd, SockList * sl, int len) |
|
|
139 | { |
|
|
140 | int stat, toread; |
|
|
141 | |
|
|
142 | /* Sanity check - shouldn't happen */ |
|
|
143 | if (sl->len < 0) |
|
|
144 | { |
|
|
145 | abort (); |
|
|
146 | } |
148 | { |
147 | /* We already have a partial packet */ |
149 | if (inbuf_len >= 2) |
148 | if (sl->len < 2) |
|
|
149 | { |
|
|
150 | do |
|
|
151 | { |
|
|
152 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
|
|
153 | } |
150 | { |
154 | while ((stat == -1) && (errno == EINTR)); |
151 | unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1]; |
155 | |
152 | |
156 | if (stat < 0) |
153 | if (inbuf_len >= 2 + pkt_len) |
|
|
154 | return pkt_len + 2; |
157 | { |
155 | } |
158 | /* In non blocking mode, EAGAIN is set when there is no |
156 | |
159 | * data available. |
157 | int amount = sizeof (inbuf) - inbuf_len; |
160 | */ |
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 | { |
161 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
174 | if (errno != EAGAIN && errno != EINTR) |
162 | { |
175 | { |
163 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
176 | LOG (llevError, "read error: %s", strerror (errno)); |
|
|
177 | return -1; |
164 | } |
178 | } |
165 | return 0; /*Error */ |
|
|
166 | } |
|
|
167 | if (stat == 0) |
|
|
168 | return -1; |
|
|
169 | sl->len += stat; |
|
|
170 | #ifdef CS_LOGSTATS |
|
|
171 | cst_tot.ibytes += stat; |
|
|
172 | cst_lst.ibytes += stat; |
|
|
173 | #endif |
|
|
174 | if (stat < 2) |
|
|
175 | return 0; /* Still don't have a full packet */ |
|
|
176 | } |
|
|
177 | /* Figure out how much more data we need to read. Add 2 from the |
|
|
178 | * end of this - size header information is not included. |
|
|
179 | */ |
|
|
180 | toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len; |
|
|
181 | if ((toread + sl->len) >= len) |
|
|
182 | { |
|
|
183 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
|
|
184 | /* Quick hack in case for 'oldsocketmode' input. If we are |
|
|
185 | * closing the socket anyways, then reading this extra 100 bytes |
|
|
186 | * shouldn't hurt. |
|
|
187 | */ |
|
|
188 | read (fd, sl->buf + 2, 100); |
|
|
189 | |
179 | |
190 | /* return error so the socket is closed */ |
|
|
191 | return -1; |
|
|
192 | } |
|
|
193 | do |
|
|
194 | { |
|
|
195 | do |
|
|
196 | { |
|
|
197 | stat = read (fd, sl->buf + sl->len, toread); |
|
|
198 | } |
|
|
199 | while ((stat < 0) && (errno == EINTR)); |
|
|
200 | if (stat < 0) |
|
|
201 | { |
|
|
202 | |
|
|
203 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
|
|
204 | { |
|
|
205 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
|
|
206 | } |
|
|
207 | return 0; /*Error */ |
|
|
208 | } |
|
|
209 | if (stat == 0) |
|
|
210 | return -1; |
|
|
211 | sl->len += stat; |
|
|
212 | #ifdef CS_LOGSTATS |
|
|
213 | cst_tot.ibytes += stat; |
|
|
214 | cst_lst.ibytes += stat; |
|
|
215 | #endif |
|
|
216 | toread -= stat; |
|
|
217 | if (toread == 0) |
|
|
218 | return 1; |
|
|
219 | if (toread < 0) |
|
|
220 | { |
|
|
221 | LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n"); |
|
|
222 | return 1; |
180 | return 0; |
223 | } |
181 | } |
|
|
182 | |
|
|
183 | inbuf_len += amount; |
|
|
184 | |
|
|
185 | cst_tot.ibytes += amount; |
|
|
186 | cst_lst.ibytes += amount; |
224 | } |
187 | } |
225 | while (toread > 0); |
188 | } |
226 | return 0; |
189 | |
|
|
190 | void |
|
|
191 | client_socket::skip_packet (int len) |
|
|
192 | { |
|
|
193 | inbuf_len -= len; |
|
|
194 | memmove (inbuf, inbuf + len, inbuf_len); |
227 | } |
195 | } |
228 | |
196 | |
229 | /******************************************************************************* |
197 | /******************************************************************************* |
230 | * |
198 | * |
231 | * Start of write related routines. |
199 | * Start of write related routines. |
… | |
… | |
238 | * 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 |
239 | * data, and len is the number of bytes to add. |
207 | * data, and len is the number of bytes to add. |
240 | */ |
208 | */ |
241 | |
209 | |
242 | static void |
210 | static void |
243 | add_to_buffer (NewSocket * ns, char *buf, int len) |
211 | add_to_buffer (client_socket *ns, char *buf, int len) |
244 | { |
212 | { |
245 | int avail, end; |
213 | int avail, end; |
246 | |
214 | |
247 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
215 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
248 | { |
216 | { |
… | |
… | |
282 | * |
250 | * |
283 | * 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 |
284 | * is called to write it out. |
252 | * is called to write it out. |
285 | */ |
253 | */ |
286 | void |
254 | void |
287 | write_socket_buffer (NewSocket * ns) |
255 | write_socket_buffer (client_socket * ns) |
288 | { |
256 | { |
289 | int amt, max; |
257 | int amt, max; |
290 | |
258 | |
291 | if (ns->outputbuffer.len == 0) |
259 | if (ns->outputbuffer.len == 0) |
292 | { |
260 | { |
… | |
… | |
341 | * 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 |
342 | * of bytes to write. IT doesn't return anything - rather, it |
310 | * of bytes to write. IT doesn't return anything - rather, it |
343 | * updates the ns structure if we get an error. |
311 | * updates the ns structure if we get an error. |
344 | */ |
312 | */ |
345 | void |
313 | void |
346 | Write_To_Socket (NewSocket * ns, char *buf, int len) |
314 | client_socket::send (void *buf_, int len) |
347 | { |
315 | { |
|
|
316 | char *buf = (char *)buf_; |
|
|
317 | char *pos = buf; |
348 | int amt = 0; |
318 | int amt = 0; |
349 | char *pos = buf; |
|
|
350 | |
319 | |
351 | if (ns->status == Ns_Dead || !buf) |
320 | if (status == Ns_Dead || !buf) |
352 | { |
321 | { |
353 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
322 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
354 | return; |
323 | return; |
355 | } |
324 | } |
356 | |
325 | |
357 | #ifndef __GNU__ /* This caused problems on Hurd */ |
326 | #ifndef __GNU__ /* This caused problems on Hurd */ |
358 | if (!ns->can_write) |
327 | if (!can_write) |
359 | { |
328 | { |
360 | add_to_buffer (ns, buf, len); |
329 | add_to_buffer (this, buf, len); |
361 | return; |
330 | return; |
362 | } |
331 | } |
363 | #endif |
332 | #endif |
|
|
333 | |
364 | /* 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 */ |
365 | while (len > 0) |
335 | while (len > 0) |
366 | { |
336 | { |
367 | |
|
|
368 | do |
337 | do |
369 | { |
338 | { |
370 | amt = write (ns->fd, pos, len); |
339 | amt = write (fd, pos, len); |
371 | } |
340 | } |
372 | while ((amt < 0) && (errno == EINTR)); |
341 | while ((amt < 0) && (errno == EINTR)); |
373 | |
342 | |
374 | if (amt < 0) |
343 | if (amt < 0) |
375 | { /* We got an error */ |
344 | { /* We got an error */ |
376 | if (errno != EWOULDBLOCK) |
345 | if (errno != EWOULDBLOCK) |
377 | { |
346 | { |
378 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
347 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
379 | errno, strerror (errno)); |
348 | errno, strerror (errno)); |
380 | ns->status = Ns_Dead; |
349 | status = Ns_Dead; |
381 | return; |
350 | return; |
382 | } |
351 | } |
383 | else |
352 | else |
384 | { /* EWOULDBLOCK */ |
353 | { /* EWOULDBLOCK */ |
385 | /* can't write it, so store it away. */ |
354 | /* can't write it, so store it away. */ |
386 | add_to_buffer (ns, pos, len); |
355 | add_to_buffer (this, pos, len); |
387 | ns->can_write = 0; |
356 | can_write = 0; |
388 | return; |
357 | return; |
389 | } |
358 | } |
390 | } |
359 | } |
391 | /* 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 |
392 | * an else if to make sure we don't reprocess it. |
361 | * an else if to make sure we don't reprocess it. |
393 | */ |
362 | */ |
394 | else if (amt == 0) |
363 | else if (amt == 0) |
395 | { |
|
|
396 | LOG (llevError, "Write_To_Socket: No data written out.\n"); |
364 | LOG (llevError, "Write_To_Socket: No data written out.\n"); |
397 | } |
365 | |
398 | len -= amt; |
366 | len -= amt; |
399 | pos += amt; |
367 | pos += amt; |
400 | #ifdef CS_LOGSTATS |
368 | #ifdef CS_LOGSTATS |
401 | cst_tot.obytes += amt; |
369 | cst_tot.obytes += amt; |
402 | cst_lst.obytes += amt; |
370 | cst_lst.obytes += amt; |
403 | #endif |
371 | #endif |
404 | } |
372 | } |
405 | } |
373 | } |
406 | |
374 | |
407 | |
|
|
408 | /** |
375 | /** |
409 | * 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 |
410 | * shortcut function. |
377 | * shortcut function. |
411 | */ |
378 | */ |
412 | void |
|
|
413 | cs_write_string (NewSocket * ns, const char *buf, int len) |
|
|
414 | { |
|
|
415 | SockList sl; |
|
|
416 | |
379 | |
417 | sl.len = len; |
380 | void |
418 | sl.buf = (unsigned char *) buf; |
381 | client_socket::send_packet (packet &sl) |
|
|
382 | { |
419 | Send_With_Handling (ns, &sl); |
383 | Send_With_Handling (this, &sl); |
420 | } |
384 | } |
421 | |
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 | } |
422 | |
400 | |
423 | /** |
401 | /** |
424 | * Calls Write_To_Socket to send data to the client. |
402 | * Calls Write_To_Socket to send data to the client. |
425 | * |
403 | * |
426 | * 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 |
427 | *, and we prepend the length information. |
405 | *, and we prepend the length information. |
428 | */ |
406 | */ |
429 | void |
407 | void |
430 | Send_With_Handling (NewSocket * ns, SockList * msg) |
408 | Send_With_Handling (client_socket *ns, packet *msg) |
431 | { |
409 | { |
432 | unsigned char sbuf[4]; |
410 | unsigned char sbuf[4]; |
433 | |
411 | |
434 | if (ns->status == Ns_Dead || !msg) |
412 | if (ns->status == Ns_Dead || !msg) |
435 | return; |
413 | return; |
436 | |
414 | |
437 | if (msg->len >= MAXSOCKBUF) |
415 | if (msg->length () >= MAXSOCKBUF) |
438 | { |
416 | { |
439 | 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 ()); |
440 | /* 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 |
441 | * it easier to debug. |
419 | * it easier to debug. |
442 | */ |
420 | */ |
443 | abort (); |
421 | abort (); |
444 | } |
422 | } |
|
|
423 | |
445 | sbuf[0] = ((uint32) (msg->len) >> 8) & 0xFF; |
424 | sbuf[0] = ((uint32) (msg->length ()) >> 8); |
446 | sbuf[1] = ((uint32) (msg->len)) & 0xFF; |
425 | sbuf[1] = ((uint32) (msg->length ()) ); |
|
|
426 | |
447 | if (ns->status != Ns_Old) |
427 | if (ns->status != Ns_Old) |
448 | Write_To_Socket (ns, (char *) sbuf, 2); |
428 | ns->send (sbuf, 2); |
449 | Write_To_Socket (ns, (char *) msg->buf, msg->len); |
|
|
450 | } |
|
|
451 | |
429 | |
452 | /** |
430 | ns->send (msg->buf, msg->length ()); |
453 | * Takes a string of data, and writes it out to the socket. A very handy |
|
|
454 | * shortcut function. |
|
|
455 | */ |
|
|
456 | void |
|
|
457 | Write_String_To_Socket (NewSocket * ns, char *buf, int len) |
|
|
458 | { |
|
|
459 | SockList sl; |
|
|
460 | |
|
|
461 | sl.len = len; |
|
|
462 | sl.buf = (unsigned char *) buf; |
|
|
463 | Send_With_Handling (ns, &sl); |
|
|
464 | } |
431 | } |
465 | |
|
|
466 | |
432 | |
467 | /****************************************************************************** |
433 | /****************************************************************************** |
468 | * |
434 | * |
469 | * statistics logging functions. |
435 | * statistics logging functions. |
470 | * |
436 | * |
… | |
… | |
493 | LOG (llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", |
459 | LOG (llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", |
494 | ctime (&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, |
460 | ctime (&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, |
495 | now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, cst_lst.max_conn, now - cst_lst.time_start); |
461 | now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, cst_lst.max_conn, now - cst_lst.time_start); |
496 | cst_lst.ibytes = 0; |
462 | cst_lst.ibytes = 0; |
497 | cst_lst.obytes = 0; |
463 | cst_lst.obytes = 0; |
498 | cst_lst.max_conn = socket_info.nconns; |
|
|
499 | cst_lst.time_start = now; |
464 | cst_lst.time_start = now; |
500 | } |
465 | } |
501 | #endif |
466 | #endif |
502 | |
467 | |