… | |
… | |
86 | * |
86 | * |
87 | **********************************************************************/ |
87 | **********************************************************************/ |
88 | |
88 | |
89 | packet &packet::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 | |
… | |
… | |
115 | } |
120 | } |
116 | |
121 | |
117 | void |
122 | void |
118 | packet::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. |
… | |
… | |
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 | NewSocket::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; |
… | |
… | |
400 | unsigned char sbuf[4]; |
410 | unsigned char sbuf[4]; |
401 | |
411 | |
402 | if (ns->status == Ns_Dead || !msg) |
412 | if (ns->status == Ns_Dead || !msg) |
403 | return; |
413 | return; |
404 | |
414 | |
405 | if (msg->len >= MAXSOCKBUF) |
415 | if (msg->length () >= MAXSOCKBUF) |
406 | { |
416 | { |
407 | 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 ()); |
408 | /* 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 |
409 | * it easier to debug. |
419 | * it easier to debug. |
410 | */ |
420 | */ |
411 | abort (); |
421 | abort (); |
412 | } |
422 | } |
|
|
423 | |
413 | sbuf[0] = ((uint32) (msg->len) >> 8) & 0xFF; |
424 | sbuf[0] = ((uint32) (msg->length ()) >> 8); |
414 | sbuf[1] = ((uint32) (msg->len)) & 0xFF; |
425 | sbuf[1] = ((uint32) (msg->length ()) ); |
|
|
426 | |
415 | if (ns->status != Ns_Old) |
427 | if (ns->status != Ns_Old) |
416 | Write_To_Socket (ns, (char *) sbuf, 2); |
428 | ns->send (sbuf, 2); |
417 | Write_To_Socket (ns, (char *) msg->buf, msg->len); |
429 | |
|
|
430 | ns->send (msg->buf, msg->length ()); |
418 | } |
431 | } |
419 | |
432 | |
420 | /****************************************************************************** |
433 | /****************************************************************************** |
421 | * |
434 | * |
422 | * statistics logging functions. |
435 | * statistics logging functions. |