1 | |
1 | |
2 | /* |
2 | /* |
3 | * static char *rcsid_sockets_c = |
3 | * static char *rcsid_sockets_c = |
4 | * "$Id: lowlevel.C,v 1.1 2006/08/13 17:16:06 elmex Exp $"; |
4 | * "$Id: lowlevel.C,v 1.4 2006/08/30 16:30:37 root Exp $"; |
5 | */ |
5 | */ |
6 | |
6 | |
7 | /* |
7 | /* |
8 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
9 | |
9 | |
… | |
… | |
70 | |
70 | |
71 | if (tcpi.tcpi_unacked |
71 | if (tcpi.tcpi_unacked |
72 | && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s |
72 | && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s |
73 | && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s |
73 | && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s |
74 | { |
74 | { |
75 | LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", ns->fd, |
75 | LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", ns->fd, |
76 | (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked); |
76 | (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked); |
77 | ns->status = Ns_Dead; |
77 | ns->status = Ns_Dead; |
78 | } |
78 | } |
79 | } |
79 | } |
80 | |
80 | |
… | |
… | |
93 | void SockList_Init(SockList *sl) |
93 | void SockList_Init(SockList *sl) |
94 | { |
94 | { |
95 | sl->len=0; |
95 | sl->len=0; |
96 | sl->buf=NULL; |
96 | sl->buf=NULL; |
97 | } |
97 | } |
98 | |
|
|
99 | void SockList_AddChar(SockList *sl, char c) |
|
|
100 | { |
|
|
101 | sl->buf[sl->len]=c; |
|
|
102 | sl->len++; |
|
|
103 | } |
|
|
104 | |
|
|
105 | void SockList_AddShort(SockList *sl, uint16 data) |
|
|
106 | { |
|
|
107 | sl->buf[sl->len++]= (data>>8)&0xff; |
|
|
108 | sl->buf[sl->len++] = data & 0xff; |
|
|
109 | } |
|
|
110 | |
|
|
111 | |
98 | |
112 | void SockList_AddInt(SockList *sl, uint32 data) |
99 | void SockList_AddInt(SockList *sl, uint32 data) |
113 | { |
100 | { |
114 | sl->buf[sl->len++]= (data>>24)&0xff; |
101 | sl->buf[sl->len++]= (data>>24)&0xff; |
115 | sl->buf[sl->len++]= (data>>16)&0xff; |
102 | sl->buf[sl->len++]= (data>>16)&0xff; |
… | |
… | |
160 | { |
147 | { |
161 | int stat,toread; |
148 | int stat,toread; |
162 | |
149 | |
163 | /* Sanity check - shouldn't happen */ |
150 | /* Sanity check - shouldn't happen */ |
164 | if (sl->len < 0) { |
151 | if (sl->len < 0) { |
165 | abort(); |
152 | abort(); |
166 | } |
153 | } |
167 | /* We already have a partial packet */ |
154 | /* We already have a partial packet */ |
168 | if (sl->len<2) { |
155 | if (sl->len<2) { |
169 | #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */ |
156 | #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */ |
170 | |
157 | |
171 | stat=recv(fd, sl->buf + sl->len, 2-sl->len,0); |
158 | stat=recv(fd, sl->buf + sl->len, 2-sl->len,0); |
172 | |
159 | |
173 | #else |
160 | #else |
174 | do { |
161 | do { |
175 | stat=read(fd, sl->buf + sl->len, 2-sl->len); |
162 | stat=read(fd, sl->buf + sl->len, 2-sl->len); |
176 | } while ((stat==-1) && (errno==EINTR)); |
163 | } while ((stat==-1) && (errno==EINTR)); |
177 | #endif |
164 | #endif |
178 | if (stat<0) { |
165 | if (stat<0) { |
179 | /* In non blocking mode, EAGAIN is set when there is no |
166 | /* In non blocking mode, EAGAIN is set when there is no |
180 | * data available. |
167 | * data available. |
181 | */ |
168 | */ |
182 | #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ |
169 | #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ |
183 | if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) { |
170 | if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) { |
184 | if(WSAGetLastError() == WSAECONNRESET) |
171 | if(WSAGetLastError() == WSAECONNRESET) |
185 | LOG(llevDebug,"Connection closed by client\n"); |
172 | LOG(llevDebug,"Connection closed by client\n"); |
186 | else |
173 | else |
187 | { |
174 | { |
188 | LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError()); |
175 | LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError()); |
189 | } |
176 | } |
190 | return -1; /* kick this user! */ |
177 | return -1; /* kick this user! */ |
191 | } |
178 | } |
192 | #else |
179 | #else |
193 | if (errno != EAGAIN && errno !=EWOULDBLOCK) { |
180 | if (errno != EAGAIN && errno !=EWOULDBLOCK) { |
194 | LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror_local(errno)); |
181 | LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror(errno)); |
195 | } |
182 | } |
196 | #endif |
183 | #endif |
197 | return 0; /*Error */ |
184 | return 0; /*Error */ |
198 | } |
185 | } |
199 | if (stat==0) return -1; |
186 | if (stat==0) return -1; |
200 | sl->len += stat; |
187 | sl->len += stat; |
201 | #ifdef CS_LOGSTATS |
188 | #ifdef CS_LOGSTATS |
202 | cst_tot.ibytes += stat; |
189 | cst_tot.ibytes += stat; |
203 | cst_lst.ibytes += stat; |
190 | cst_lst.ibytes += stat; |
204 | #endif |
191 | #endif |
205 | if (stat<2) return 0; /* Still don't have a full packet */ |
192 | if (stat<2) return 0; /* Still don't have a full packet */ |
206 | } |
193 | } |
207 | /* Figure out how much more data we need to read. Add 2 from the |
194 | /* Figure out how much more data we need to read. Add 2 from the |
208 | * end of this - size header information is not included. |
195 | * end of this - size header information is not included. |
209 | */ |
196 | */ |
210 | toread = 2+(sl->buf[0] << 8) + sl->buf[1] - sl->len; |
197 | toread = 2+(sl->buf[0] << 8) + sl->buf[1] - sl->len; |
211 | if ((toread + sl->len) >= len) { |
198 | if ((toread + sl->len) >= len) { |
212 | LOG(llevError,"SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", |
199 | LOG(llevError,"SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", |
213 | toread + sl->len, len); |
200 | toread + sl->len, len); |
214 | /* Quick hack in case for 'oldsocketmode' input. If we are |
201 | /* Quick hack in case for 'oldsocketmode' input. If we are |
215 | * closing the socket anyways, then reading this extra 100 bytes |
202 | * closing the socket anyways, then reading this extra 100 bytes |
216 | * shouldn't hurt. |
203 | * shouldn't hurt. |
217 | */ |
204 | */ |
218 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
205 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
219 | recv(fd, sl->buf+2, 100, 0); |
206 | recv(fd, sl->buf+2, 100, 0); |
220 | #else |
207 | #else |
221 | read(fd, sl->buf+2, 100); |
208 | read(fd, sl->buf+2, 100); |
222 | #endif /* end win32 */ |
209 | #endif /* end win32 */ |
223 | |
210 | |
224 | /* return error so the socket is closed */ |
211 | /* return error so the socket is closed */ |
225 | return -1; |
212 | return -1; |
226 | } |
213 | } |
227 | do { |
214 | do { |
228 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
215 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
229 | stat = recv(fd, sl->buf+ sl->len, toread, 0); |
216 | stat = recv(fd, sl->buf+ sl->len, toread, 0); |
230 | #else |
217 | #else |
231 | do { |
218 | do { |
232 | stat = read(fd, sl->buf+ sl->len, toread); |
219 | stat = read(fd, sl->buf+ sl->len, toread); |
233 | } while ((stat<0) && (errno==EINTR)); |
220 | } while ((stat<0) && (errno==EINTR)); |
234 | #endif |
221 | #endif |
235 | if (stat<0) { |
222 | if (stat<0) { |
236 | |
223 | |
237 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */ |
224 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */ |
238 | if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) { |
225 | if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) { |
239 | if(WSAGetLastError() == WSAECONNRESET) |
226 | if(WSAGetLastError() == WSAECONNRESET) |
240 | LOG(llevDebug,"Connection closed by client\n"); |
227 | LOG(llevDebug,"Connection closed by client\n"); |
241 | else |
228 | else |
242 | { |
229 | { |
243 | LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError()); |
230 | LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError()); |
244 | } |
231 | } |
245 | return -1; /* kick this user! */ |
232 | return -1; /* kick this user! */ |
246 | } |
233 | } |
247 | #else |
234 | #else |
248 | if (errno != EAGAIN && errno !=EWOULDBLOCK) { |
235 | if (errno != EAGAIN && errno !=EWOULDBLOCK) { |
249 | LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror_local(errno)); |
236 | LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror(errno)); |
250 | } |
237 | } |
251 | #endif |
238 | #endif |
252 | return 0; /*Error */ |
239 | return 0; /*Error */ |
253 | } |
240 | } |
254 | if (stat==0) return -1; |
241 | if (stat==0) return -1; |
255 | sl->len += stat; |
242 | sl->len += stat; |
256 | #ifdef CS_LOGSTATS |
243 | #ifdef CS_LOGSTATS |
257 | cst_tot.ibytes += stat; |
244 | cst_tot.ibytes += stat; |
258 | cst_lst.ibytes += stat; |
245 | cst_lst.ibytes += stat; |
259 | #endif |
246 | #endif |
260 | toread -= stat; |
247 | toread -= stat; |
261 | if (toread==0) return 1; |
248 | if (toread==0) return 1; |
262 | if (toread < 0) { |
249 | if (toread < 0) { |
263 | LOG(llevError,"SockList_ReadPacket: Read more bytes than desired.\n"); |
250 | LOG(llevError,"SockList_ReadPacket: Read more bytes than desired.\n"); |
264 | return 1; |
251 | return 1; |
265 | } |
252 | } |
266 | } while (toread>0); |
253 | } while (toread>0); |
267 | return 0; |
254 | return 0; |
268 | } |
255 | } |
269 | |
256 | |
270 | /******************************************************************************* |
257 | /******************************************************************************* |
… | |
… | |
283 | static void add_to_buffer(NewSocket *ns, char *buf, int len) |
270 | static void add_to_buffer(NewSocket *ns, char *buf, int len) |
284 | { |
271 | { |
285 | int avail, end; |
272 | int avail, end; |
286 | |
273 | |
287 | if ((len+ns->outputbuffer.len)>SOCKETBUFSIZE) { |
274 | if ((len+ns->outputbuffer.len)>SOCKETBUFSIZE) { |
288 | LOG(llevDebug,"Socket on fd %d has overrun internal buffer - marking as dead\n", |
275 | LOG(llevDebug,"Socket on fd %d has overrun internal buffer - marking as dead\n", |
289 | ns->fd); |
276 | ns->fd); |
290 | ns->status = Ns_Dead; |
277 | ns->status = Ns_Dead; |
291 | return; |
278 | return; |
292 | } |
279 | } |
293 | |
280 | |
294 | /* data + end is where we start putting the new data. The last byte |
281 | /* data + end is where we start putting the new data. The last byte |
295 | * currently in use is actually data + end -1 |
282 | * currently in use is actually data + end -1 |
296 | */ |
283 | */ |
… | |
… | |
300 | if (end>=SOCKETBUFSIZE) end-=SOCKETBUFSIZE; |
287 | if (end>=SOCKETBUFSIZE) end-=SOCKETBUFSIZE; |
301 | avail=SOCKETBUFSIZE - end; |
288 | avail=SOCKETBUFSIZE - end; |
302 | |
289 | |
303 | /* We can all fit it behind the current data without wrapping */ |
290 | /* We can all fit it behind the current data without wrapping */ |
304 | if (avail >=len ) { |
291 | if (avail >=len ) { |
305 | memcpy(ns->outputbuffer.data + end, buf, len); |
292 | memcpy(ns->outputbuffer.data + end, buf, len); |
306 | } |
293 | } |
307 | else { |
294 | else { |
308 | memcpy(ns->outputbuffer.data + end, buf, avail); |
295 | memcpy(ns->outputbuffer.data + end, buf, avail); |
309 | memcpy(ns->outputbuffer.data, buf+avail, len-avail); |
296 | memcpy(ns->outputbuffer.data, buf+avail, len-avail); |
310 | } |
297 | } |
311 | ns->outputbuffer.len += len; |
298 | ns->outputbuffer.len += len; |
312 | #if 0 |
299 | #if 0 |
313 | LOG(llevDebug,"Added %d to output buffer, total length now %d, start=%d\n", len, |
300 | LOG(llevDebug,"Added %d to output buffer, total length now %d, start=%d\n", len, |
314 | ns->outputbuffer.len, ns->outputbuffer.start); |
301 | ns->outputbuffer.len, ns->outputbuffer.start); |
315 | #endif |
302 | #endif |
316 | } |
303 | } |
317 | |
304 | |
318 | /** |
305 | /** |
319 | * Writes data to socket. |
306 | * Writes data to socket. |
… | |
… | |
324 | void write_socket_buffer(NewSocket *ns) |
311 | void write_socket_buffer(NewSocket *ns) |
325 | { |
312 | { |
326 | int amt, max; |
313 | int amt, max; |
327 | |
314 | |
328 | if (ns->outputbuffer.len==0) { |
315 | if (ns->outputbuffer.len==0) { |
329 | LOG(llevDebug,"write_socket_buffer called when there is no data, fd=%d\n", |
316 | LOG(llevDebug,"write_socket_buffer called when there is no data, fd=%d\n", |
330 | ns->fd); |
317 | ns->fd); |
331 | return; |
318 | return; |
332 | } |
319 | } |
333 | |
320 | |
334 | do { |
321 | do { |
335 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
322 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
336 | if (ns->outputbuffer.len<max) max = ns->outputbuffer.len; |
323 | if (ns->outputbuffer.len<max) max = ns->outputbuffer.len; |
337 | |
324 | |
338 | #ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */ |
325 | #ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */ |
339 | amt=send(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max,0); |
326 | amt=send(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max,0); |
340 | #else |
327 | #else |
341 | do { |
328 | do { |
342 | amt=write(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
329 | amt=write(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
343 | } while ((amt<0) && (errno==EINTR)); |
330 | } while ((amt<0) && (errno==EINTR)); |
344 | #endif |
331 | #endif |
345 | |
332 | |
346 | if (amt < 0) { /* We got an error */ |
333 | if (amt < 0) { /* We got an error */ |
347 | |
334 | |
348 | #ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */ |
335 | #ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */ |
349 | if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) { |
336 | if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) { |
350 | LOG(llevError,"New socket write failed (wsb) (%d).\n", WSAGetLastError()); |
337 | LOG(llevError,"New socket write failed (wsb) (%d).\n", WSAGetLastError()); |
351 | #else |
338 | #else |
352 | if (errno !=EWOULDBLOCK) { |
339 | if (errno !=EWOULDBLOCK) { |
353 | LOG(llevError,"New socket write failed (wsb) (%d: %s).\n", |
340 | LOG(llevError,"New socket write failed (wsb) (%d: %s).\n", |
354 | errno, strerror_local(errno)); |
341 | errno, strerror(errno)); |
355 | #endif |
342 | #endif |
356 | ns->status=Ns_Dead; |
343 | ns->status=Ns_Dead; |
357 | return; |
344 | return; |
358 | } |
345 | } |
359 | else { /* EWOULDBLOCK */ |
346 | else { /* EWOULDBLOCK */ |
360 | /* can't write it, so store it away. */ |
347 | /* can't write it, so store it away. */ |
361 | ns->can_write=0; |
348 | ns->can_write=0; |
362 | return; |
349 | return; |
363 | } |
350 | } |
364 | } |
351 | } |
365 | ns->outputbuffer.start += amt; |
352 | ns->outputbuffer.start += amt; |
366 | /* wrap back to start of buffer */ |
353 | /* wrap back to start of buffer */ |
367 | if (ns->outputbuffer.start==SOCKETBUFSIZE) ns->outputbuffer.start=0; |
354 | if (ns->outputbuffer.start==SOCKETBUFSIZE) ns->outputbuffer.start=0; |
368 | ns->outputbuffer.len -= amt; |
355 | ns->outputbuffer.len -= amt; |
369 | #ifdef CS_LOGSTATS |
356 | #ifdef CS_LOGSTATS |
370 | cst_tot.obytes += amt; |
357 | cst_tot.obytes += amt; |
371 | cst_lst.obytes += amt; |
358 | cst_lst.obytes += amt; |
372 | #endif |
359 | #endif |
373 | } while (ns->outputbuffer.len>0); |
360 | } while (ns->outputbuffer.len>0); |
374 | } |
361 | } |
375 | |
362 | |
376 | /** |
363 | /** |
… | |
… | |
384 | { |
371 | { |
385 | int amt=0; |
372 | int amt=0; |
386 | char *pos=buf; |
373 | char *pos=buf; |
387 | |
374 | |
388 | if (ns->status == Ns_Dead || !buf) { |
375 | if (ns->status == Ns_Dead || !buf) { |
389 | LOG(llevDebug,"Write_To_Socket called with dead socket\n"); |
376 | LOG(llevDebug,"Write_To_Socket called with dead socket\n"); |
390 | return; |
377 | return; |
391 | } |
378 | } |
392 | |
379 | |
393 | #ifndef __GNU__ /* This caused problems on Hurd */ |
380 | #ifndef __GNU__ /* This caused problems on Hurd */ |
394 | if (!ns->can_write) { |
381 | if (!ns->can_write) { |
395 | add_to_buffer(ns, buf, len); |
382 | add_to_buffer(ns, buf, len); |
396 | return; |
383 | return; |
397 | } |
384 | } |
398 | #endif |
385 | #endif |
399 | /* If we manage to write more than we wanted, take it as a bonus */ |
386 | /* If we manage to write more than we wanted, take it as a bonus */ |
400 | while (len>0) { |
387 | while (len>0) { |
401 | |
388 | |
402 | #ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */ |
389 | #ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */ |
403 | amt=send(ns->fd, pos, len,0); |
390 | amt=send(ns->fd, pos, len,0); |
404 | #else |
391 | #else |
405 | do { |
392 | do { |
406 | amt=write(ns->fd, pos, len); |
393 | amt=write(ns->fd, pos, len); |
407 | } while ((amt<0) && (errno==EINTR)); |
394 | } while ((amt<0) && (errno==EINTR)); |
408 | #endif |
395 | #endif |
409 | |
396 | |
410 | if (amt < 0) { /* We got an error */ |
397 | if (amt < 0) { /* We got an error */ |
411 | #ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */ |
398 | #ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */ |
412 | if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) { |
399 | if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) { |
413 | LOG(llevError,"New socket write failed WTS (%d).\n",WSAGetLastError()); |
400 | LOG(llevError,"New socket write failed WTS (%d).\n",WSAGetLastError()); |
414 | #else |
401 | #else |
415 | if (errno !=EWOULDBLOCK) { |
402 | if (errno !=EWOULDBLOCK) { |
416 | LOG(llevError,"New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
403 | LOG(llevError,"New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
417 | errno, strerror_local(errno)); |
404 | errno, strerror(errno)); |
418 | #endif |
405 | #endif |
419 | ns->status=Ns_Dead; |
406 | ns->status=Ns_Dead; |
420 | return; |
407 | return; |
421 | } |
408 | } |
422 | else { /* EWOULDBLOCK */ |
409 | else { /* EWOULDBLOCK */ |
423 | /* can't write it, so store it away. */ |
410 | /* can't write it, so store it away. */ |
424 | add_to_buffer(ns, pos, len); |
411 | add_to_buffer(ns, pos, len); |
425 | ns->can_write=0; |
412 | ns->can_write=0; |
426 | return; |
413 | return; |
427 | } |
414 | } |
428 | } |
415 | } |
429 | /* amt gets set to 0 above in blocking code, so we do this as |
416 | /* amt gets set to 0 above in blocking code, so we do this as |
430 | * an else if to make sure we don't reprocess it. |
417 | * an else if to make sure we don't reprocess it. |
431 | */ |
418 | */ |
432 | else if (amt==0) { |
419 | else if (amt==0) { |
433 | LOG(llevError,"Write_To_Socket: No data written out.\n"); |
420 | LOG(llevError,"Write_To_Socket: No data written out.\n"); |
434 | } |
421 | } |
435 | len -= amt; |
422 | len -= amt; |
436 | pos += amt; |
423 | pos += amt; |
437 | #ifdef CS_LOGSTATS |
424 | #ifdef CS_LOGSTATS |
438 | cst_tot.obytes += amt; |
425 | cst_tot.obytes += amt; |
439 | cst_lst.obytes += amt; |
426 | cst_lst.obytes += amt; |
440 | #endif |
427 | #endif |
441 | } |
428 | } |
442 | } |
429 | } |
443 | |
430 | |
444 | |
431 | |
… | |
… | |
465 | void Send_With_Handling(NewSocket *ns,SockList *msg) |
452 | void Send_With_Handling(NewSocket *ns,SockList *msg) |
466 | { |
453 | { |
467 | unsigned char sbuf[4]; |
454 | unsigned char sbuf[4]; |
468 | |
455 | |
469 | if (ns->status == Ns_Dead || !msg) |
456 | if (ns->status == Ns_Dead || !msg) |
470 | return; |
457 | return; |
471 | |
458 | |
472 | if (msg->len >= MAXSOCKBUF) { |
459 | if (msg->len >= MAXSOCKBUF) { |
473 | LOG(llevError,"Trying to send a buffer beyond properly size, len =%d\n", |
460 | LOG(llevError,"Trying to send a buffer beyond properly size, len =%d\n", |
474 | msg->len); |
461 | msg->len); |
475 | /* Almost certainly we've overflowed a buffer, so quite now to make |
462 | /* Almost certainly we've overflowed a buffer, so quite now to make |
476 | * it easier to debug. |
463 | * it easier to debug. |
477 | */ |
464 | */ |
478 | abort(); |
465 | abort(); |
479 | } |
466 | } |
480 | sbuf[0] = ((uint32)(msg->len) >> 8) & 0xFF; |
467 | sbuf[0] = ((uint32)(msg->len) >> 8) & 0xFF; |
481 | sbuf[1] = ((uint32)(msg->len)) & 0xFF; |
468 | sbuf[1] = ((uint32)(msg->len)) & 0xFF; |
482 | if (ns->status != Ns_Old) |
469 | if (ns->status != Ns_Old) |
483 | Write_To_Socket(ns, (char *) sbuf, 2); |
470 | Write_To_Socket(ns, (char *) sbuf, 2); |
484 | Write_To_Socket(ns, (char*)msg->buf, msg->len); |
471 | Write_To_Socket(ns, (char*)msg->buf, msg->len); |
485 | } |
472 | } |
486 | |
473 | |
487 | /** |
474 | /** |
488 | * Takes a string of data, and writes it out to the socket. A very handy |
475 | * Takes a string of data, and writes it out to the socket. A very handy |
… | |
… | |
520 | /* If no connections recently, don't both to log anything */ |
507 | /* If no connections recently, don't both to log anything */ |
521 | if (cst_lst.ibytes==0 && cst_lst.obytes==0) return; |
508 | if (cst_lst.ibytes==0 && cst_lst.obytes==0) return; |
522 | |
509 | |
523 | /* CSSTAT is put in so scripts can easily find the line */ |
510 | /* CSSTAT is put in so scripts can easily find the line */ |
524 | LOG(llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", |
511 | LOG(llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", |
525 | ctime(&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, |
512 | ctime(&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, |
526 | now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, |
513 | now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, |
527 | cst_lst.max_conn, now - cst_lst.time_start); |
514 | cst_lst.max_conn, now - cst_lst.time_start); |
528 | cst_lst.ibytes=0; |
515 | cst_lst.ibytes=0; |
529 | cst_lst.obytes=0; |
516 | cst_lst.obytes=0; |
530 | cst_lst.max_conn=socket_info.nconns; |
517 | cst_lst.max_conn=socket_info.nconns; |
531 | cst_lst.time_start=now; |
518 | cst_lst.time_start=now; |
532 | } |
519 | } |