1 |
|
2 |
/* |
3 |
* static char *rcsid_sockets_c = |
4 |
* "$Id$"; |
5 |
*/ |
6 |
|
7 |
/* |
8 |
CrossFire, A Multiplayer game for X-windows |
9 |
|
10 |
Copyright (C) 1992 Frank Tore Johansen |
11 |
|
12 |
This program is free software; you can redistribute it and/or modify |
13 |
it under the terms of the GNU General Public License as published by |
14 |
the Free Software Foundation; either version 2 of the License, or |
15 |
(at your option) any later version. |
16 |
|
17 |
This program is distributed in the hope that it will be useful, |
18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 |
GNU General Public License for more details. |
21 |
|
22 |
You should have received a copy of the GNU General Public License |
23 |
along with this program; if not, write to the Free Software |
24 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 |
|
26 |
The author can be reached via e-mail to mark@pyramid.com |
27 |
*/ |
28 |
|
29 |
/** |
30 |
* \file |
31 |
* Low-level socket-related functions. |
32 |
* |
33 |
* \date 2003-12-02 |
34 |
* |
35 |
* Contains some base functions that both the client and server |
36 |
* can use. As such, depending what we are being compiled for will |
37 |
* determine what we can include. the client is designed have |
38 |
* CFCLIENT defined as part of its compile flags. |
39 |
*/ |
40 |
|
41 |
#include <global.h> |
42 |
#include <newclient.h> |
43 |
#include <sproto.h> |
44 |
|
45 |
#ifdef __linux__ |
46 |
# include <sys/types.h> |
47 |
# include <sys/socket.h> |
48 |
# include <netinet/in.h> |
49 |
# define TCP_HZ 1000 // sorry... |
50 |
# include <netinet/tcp.h> |
51 |
#endif |
52 |
|
53 |
// use a really low timeout, as it doesn't cost any bandwidth, and you can |
54 |
// easily die in 20 seconds... |
55 |
#define SOCKET_TIMEOUT1 10 |
56 |
#define SOCKET_TIMEOUT2 20 |
57 |
|
58 |
void Socket_Flush (NewSocket *ns) |
59 |
{ |
60 |
#ifdef __linux__ |
61 |
// check time of last ack, and, if too old, kill connection |
62 |
struct tcp_info tcpi; |
63 |
socklen_t len = sizeof (tcpi); |
64 |
|
65 |
if (!getsockopt (ns->fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) |
66 |
{ |
67 |
unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; |
68 |
|
69 |
if (tcpi.tcpi_unacked |
70 |
&& SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s |
71 |
&& SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s |
72 |
{ |
73 |
LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", ns->fd, |
74 |
(unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked); |
75 |
ns->status = Ns_Dead; |
76 |
} |
77 |
} |
78 |
|
79 |
int val; |
80 |
val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
81 |
val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
82 |
#endif |
83 |
} |
84 |
|
85 |
/*********************************************************************** |
86 |
* |
87 |
* SockList functions/utilities |
88 |
* |
89 |
**********************************************************************/ |
90 |
|
91 |
void SockList_Init(SockList *sl) |
92 |
{ |
93 |
sl->len=0; |
94 |
sl->buf=NULL; |
95 |
} |
96 |
|
97 |
void SockList_AddChar(SockList *sl, char c) |
98 |
{ |
99 |
sl->buf[sl->len]=c; |
100 |
sl->len++; |
101 |
} |
102 |
|
103 |
void SockList_AddShort(SockList *sl, uint16 data) |
104 |
{ |
105 |
sl->buf[sl->len++]= (data>>8)&0xff; |
106 |
sl->buf[sl->len++] = data & 0xff; |
107 |
} |
108 |
|
109 |
|
110 |
void SockList_AddInt(SockList *sl, uint32 data) |
111 |
{ |
112 |
sl->buf[sl->len++]= (data>>24)&0xff; |
113 |
sl->buf[sl->len++]= (data>>16)&0xff; |
114 |
sl->buf[sl->len++]= (data>>8)&0xff; |
115 |
sl->buf[sl->len++] = data & 0xff; |
116 |
} |
117 |
|
118 |
void SockList_AddInt64(SockList *sl, uint64 data) |
119 |
{ |
120 |
sl->buf[sl->len++]= ( char )( (data>>56)&0xff ); |
121 |
sl->buf[sl->len++]= ( char )( (data>>48)&0xff ); |
122 |
sl->buf[sl->len++]= ( char )( (data>>40)&0xff ); |
123 |
sl->buf[sl->len++]= ( char )( (data>>32)&0xff ); |
124 |
|
125 |
sl->buf[sl->len++]= ( char )( (data>>24)&0xff ); |
126 |
sl->buf[sl->len++]= ( char )( (data>>16)&0xff ); |
127 |
sl->buf[sl->len++]= ( char )( (data>>8)&0xff ); |
128 |
sl->buf[sl->len++] =( char )( data & 0xff ); |
129 |
} |
130 |
|
131 |
/* Basically does the reverse of SockList_AddInt, but on |
132 |
* strings instead. Same for the GetShort, but for 16 bits. |
133 |
*/ |
134 |
int GetInt_String(unsigned char *data) |
135 |
{ |
136 |
return ((data[0]<<24) + (data[1]<<16) + (data[2]<<8) + data[3]); |
137 |
} |
138 |
|
139 |
short GetShort_String(unsigned char *data) { |
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 SockList_ReadPacket(int fd, SockList *sl, int len) |
158 |
{ |
159 |
int stat,toread; |
160 |
extern int errno; |
161 |
|
162 |
/* Sanity check - shouldn't happen */ |
163 |
if (sl->len < 0) { |
164 |
abort(); |
165 |
} |
166 |
/* We already have a partial packet */ |
167 |
if (sl->len<2) { |
168 |
#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */ |
169 |
|
170 |
stat=recv(fd, sl->buf + sl->len, 2-sl->len,0); |
171 |
|
172 |
#else |
173 |
do { |
174 |
stat=read(fd, sl->buf + sl->len, 2-sl->len); |
175 |
} while ((stat==-1) && (errno==EINTR)); |
176 |
#endif |
177 |
if (stat<0) { |
178 |
/* In non blocking mode, EAGAIN is set when there is no |
179 |
* data available. |
180 |
*/ |
181 |
#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ |
182 |
if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) { |
183 |
if(WSAGetLastError() == WSAECONNRESET) |
184 |
LOG(llevDebug,"Connection closed by client\n"); |
185 |
else |
186 |
{ |
187 |
LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError()); |
188 |
} |
189 |
return -1; /* kick this user! */ |
190 |
} |
191 |
#else |
192 |
if (errno != EAGAIN && errno !=EWOULDBLOCK) { |
193 |
LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror_local(errno)); |
194 |
} |
195 |
#endif |
196 |
return 0; /*Error */ |
197 |
} |
198 |
if (stat==0) return -1; |
199 |
sl->len += stat; |
200 |
#ifdef CS_LOGSTATS |
201 |
cst_tot.ibytes += stat; |
202 |
cst_lst.ibytes += stat; |
203 |
#endif |
204 |
if (stat<2) return 0; /* Still don't have a full packet */ |
205 |
} |
206 |
/* Figure out how much more data we need to read. Add 2 from the |
207 |
* end of this - size header information is not included. |
208 |
*/ |
209 |
toread = 2+(sl->buf[0] << 8) + sl->buf[1] - sl->len; |
210 |
if ((toread + sl->len) >= len) { |
211 |
LOG(llevError,"SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", |
212 |
toread + sl->len, len); |
213 |
/* Quick hack in case for 'oldsocketmode' input. If we are |
214 |
* closing the socket anyways, then reading this extra 100 bytes |
215 |
* shouldn't hurt. |
216 |
*/ |
217 |
#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
218 |
recv(fd, sl->buf+2, 100, 0); |
219 |
#else |
220 |
read(fd, sl->buf+2, 100); |
221 |
#endif /* end win32 */ |
222 |
|
223 |
/* return error so the socket is closed */ |
224 |
return -1; |
225 |
} |
226 |
do { |
227 |
#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
228 |
stat = recv(fd, sl->buf+ sl->len, toread, 0); |
229 |
#else |
230 |
do { |
231 |
stat = read(fd, sl->buf+ sl->len, toread); |
232 |
} while ((stat<0) && (errno==EINTR)); |
233 |
#endif |
234 |
if (stat<0) { |
235 |
|
236 |
#ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */ |
237 |
if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) { |
238 |
if(WSAGetLastError() == WSAECONNRESET) |
239 |
LOG(llevDebug,"Connection closed by client\n"); |
240 |
else |
241 |
{ |
242 |
LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError()); |
243 |
} |
244 |
return -1; /* kick this user! */ |
245 |
} |
246 |
#else |
247 |
if (errno != EAGAIN && errno !=EWOULDBLOCK) { |
248 |
LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror_local(errno)); |
249 |
} |
250 |
#endif |
251 |
return 0; /*Error */ |
252 |
} |
253 |
if (stat==0) return -1; |
254 |
sl->len += stat; |
255 |
#ifdef CS_LOGSTATS |
256 |
cst_tot.ibytes += stat; |
257 |
cst_lst.ibytes += stat; |
258 |
#endif |
259 |
toread -= stat; |
260 |
if (toread==0) return 1; |
261 |
if (toread < 0) { |
262 |
LOG(llevError,"SockList_ReadPacket: Read more bytes than desired.\n"); |
263 |
return 1; |
264 |
} |
265 |
} while (toread>0); |
266 |
return 0; |
267 |
} |
268 |
|
269 |
/******************************************************************************* |
270 |
* |
271 |
* Start of write related routines. |
272 |
* |
273 |
******************************************************************************/ |
274 |
|
275 |
/** |
276 |
* Adds data to a socket buffer for whatever reason. |
277 |
* |
278 |
* ns is the socket we are adding the data to, buf is the start of the |
279 |
* data, and len is the number of bytes to add. |
280 |
*/ |
281 |
|
282 |
static void add_to_buffer(NewSocket *ns, unsigned char *buf, int len) |
283 |
{ |
284 |
int avail, end; |
285 |
|
286 |
if ((len+ns->outputbuffer.len)>SOCKETBUFSIZE) { |
287 |
LOG(llevDebug,"Socket on fd %d has overrun internal buffer - marking as dead\n", |
288 |
ns->fd); |
289 |
ns->status = Ns_Dead; |
290 |
return; |
291 |
} |
292 |
|
293 |
/* data + end is where we start putting the new data. The last byte |
294 |
* currently in use is actually data + end -1 |
295 |
*/ |
296 |
|
297 |
end=ns->outputbuffer.start + ns->outputbuffer.len; |
298 |
/* The buffer is already in a wrapped state, so adjust end */ |
299 |
if (end>=SOCKETBUFSIZE) end-=SOCKETBUFSIZE; |
300 |
avail=SOCKETBUFSIZE - end; |
301 |
|
302 |
/* We can all fit it behind the current data without wrapping */ |
303 |
if (avail >=len ) { |
304 |
memcpy(ns->outputbuffer.data + end, buf, len); |
305 |
} |
306 |
else { |
307 |
memcpy(ns->outputbuffer.data + end, buf, avail); |
308 |
memcpy(ns->outputbuffer.data, buf+avail, len-avail); |
309 |
} |
310 |
ns->outputbuffer.len += len; |
311 |
#if 0 |
312 |
LOG(llevDebug,"Added %d to output buffer, total length now %d, start=%d\n", len, |
313 |
ns->outputbuffer.len, ns->outputbuffer.start); |
314 |
#endif |
315 |
} |
316 |
|
317 |
/** |
318 |
* Writes data to socket. |
319 |
* |
320 |
* When the socket is clear to write, and we have backlogged data, this |
321 |
* is called to write it out. |
322 |
*/ |
323 |
void write_socket_buffer(NewSocket *ns) |
324 |
{ |
325 |
int amt, max; |
326 |
|
327 |
if (ns->outputbuffer.len==0) { |
328 |
LOG(llevDebug,"write_socket_buffer called when there is no data, fd=%d\n", |
329 |
ns->fd); |
330 |
return; |
331 |
} |
332 |
|
333 |
do { |
334 |
max = SOCKETBUFSIZE - ns->outputbuffer.start; |
335 |
if (ns->outputbuffer.len<max) max = ns->outputbuffer.len; |
336 |
|
337 |
#ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */ |
338 |
amt=send(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max,0); |
339 |
#else |
340 |
do { |
341 |
amt=write(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
342 |
} while ((amt<0) && (errno==EINTR)); |
343 |
#endif |
344 |
|
345 |
if (amt < 0) { /* We got an error */ |
346 |
|
347 |
#ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */ |
348 |
if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) { |
349 |
LOG(llevError,"New socket write failed (wsb) (%d).\n", WSAGetLastError()); |
350 |
#else |
351 |
if (errno !=EWOULDBLOCK) { |
352 |
LOG(llevError,"New socket write failed (wsb) (%d: %s).\n", |
353 |
errno, strerror_local(errno)); |
354 |
#endif |
355 |
ns->status=Ns_Dead; |
356 |
return; |
357 |
} |
358 |
else { /* EWOULDBLOCK */ |
359 |
/* can't write it, so store it away. */ |
360 |
ns->can_write=0; |
361 |
return; |
362 |
} |
363 |
} |
364 |
ns->outputbuffer.start += amt; |
365 |
/* wrap back to start of buffer */ |
366 |
if (ns->outputbuffer.start==SOCKETBUFSIZE) ns->outputbuffer.start=0; |
367 |
ns->outputbuffer.len -= amt; |
368 |
#ifdef CS_LOGSTATS |
369 |
cst_tot.obytes += amt; |
370 |
cst_lst.obytes += amt; |
371 |
#endif |
372 |
} while (ns->outputbuffer.len>0); |
373 |
} |
374 |
|
375 |
/** |
376 |
* This writes data to the socket. - It is very low level - |
377 |
* all we try and do is write out the data to the socket |
378 |
* provided (ns). buf is the data to write, len is the number |
379 |
* of bytes to write. IT doesn't return anything - rather, it |
380 |
* updates the ns structure if we get an error. |
381 |
*/ |
382 |
void Write_To_Socket(NewSocket *ns, unsigned char *buf, int len) |
383 |
{ |
384 |
int amt=0; |
385 |
unsigned char *pos=buf; |
386 |
|
387 |
if (ns->status == Ns_Dead || !buf) { |
388 |
LOG(llevDebug,"Write_To_Socket called with dead socket\n"); |
389 |
return; |
390 |
} |
391 |
|
392 |
#ifndef __GNU__ /* This caused problems on Hurd */ |
393 |
if (!ns->can_write) { |
394 |
add_to_buffer(ns, buf, len); |
395 |
return; |
396 |
} |
397 |
#endif |
398 |
/* If we manage to write more than we wanted, take it as a bonus */ |
399 |
while (len>0) { |
400 |
|
401 |
#ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */ |
402 |
amt=send(ns->fd, pos, len,0); |
403 |
#else |
404 |
do { |
405 |
amt=write(ns->fd, pos, len); |
406 |
} while ((amt<0) && (errno==EINTR)); |
407 |
#endif |
408 |
|
409 |
if (amt < 0) { /* We got an error */ |
410 |
#ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */ |
411 |
if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) { |
412 |
LOG(llevError,"New socket write failed WTS (%d).\n",WSAGetLastError()); |
413 |
#else |
414 |
if (errno !=EWOULDBLOCK) { |
415 |
LOG(llevError,"New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
416 |
errno, strerror_local(errno)); |
417 |
#endif |
418 |
ns->status=Ns_Dead; |
419 |
return; |
420 |
} |
421 |
else { /* EWOULDBLOCK */ |
422 |
/* can't write it, so store it away. */ |
423 |
add_to_buffer(ns, pos, len); |
424 |
ns->can_write=0; |
425 |
return; |
426 |
} |
427 |
} |
428 |
/* amt gets set to 0 above in blocking code, so we do this as |
429 |
* an else if to make sure we don't reprocess it. |
430 |
*/ |
431 |
else if (amt==0) { |
432 |
LOG(llevError,"Write_To_Socket: No data written out.\n"); |
433 |
} |
434 |
len -= amt; |
435 |
pos += amt; |
436 |
#ifdef CS_LOGSTATS |
437 |
cst_tot.obytes += amt; |
438 |
cst_lst.obytes += amt; |
439 |
#endif |
440 |
} |
441 |
} |
442 |
|
443 |
|
444 |
/** |
445 |
* Takes a string of data, and writes it out to the socket. A very handy |
446 |
* shortcut function. |
447 |
*/ |
448 |
void cs_write_string(NewSocket *ns, const char *buf, int len) |
449 |
{ |
450 |
SockList sl; |
451 |
|
452 |
sl.len = len; |
453 |
sl.buf = (unsigned char*)buf; |
454 |
Send_With_Handling(ns, &sl); |
455 |
} |
456 |
|
457 |
|
458 |
/** |
459 |
* Calls Write_To_Socket to send data to the client. |
460 |
* |
461 |
* The only difference in this function is that we take a SockList |
462 |
*, and we prepend the length information. |
463 |
*/ |
464 |
void Send_With_Handling(NewSocket *ns,SockList *msg) |
465 |
{ |
466 |
unsigned char sbuf[4]; |
467 |
|
468 |
if (ns->status == Ns_Dead || !msg) |
469 |
return; |
470 |
|
471 |
if (msg->len >= MAXSOCKBUF) { |
472 |
LOG(llevError,"Trying to send a buffer beyond properly size, len =%d\n", |
473 |
msg->len); |
474 |
/* Almost certainly we've overflowed a buffer, so quite now to make |
475 |
* it easier to debug. |
476 |
*/ |
477 |
abort(); |
478 |
} |
479 |
sbuf[0] = ((uint32)(msg->len) >> 8) & 0xFF; |
480 |
sbuf[1] = ((uint32)(msg->len)) & 0xFF; |
481 |
if (ns->status != Ns_Old) |
482 |
Write_To_Socket(ns, sbuf, 2); |
483 |
Write_To_Socket(ns, msg->buf, msg->len); |
484 |
} |
485 |
|
486 |
/** |
487 |
* Takes a string of data, and writes it out to the socket. A very handy |
488 |
* shortcut function. |
489 |
*/ |
490 |
void Write_String_To_Socket(NewSocket *ns, char *buf, int len) |
491 |
{ |
492 |
SockList sl; |
493 |
|
494 |
sl.len = len; |
495 |
sl.buf = (uint8*)buf; |
496 |
Send_With_Handling(ns, &sl); |
497 |
} |
498 |
|
499 |
|
500 |
/****************************************************************************** |
501 |
* |
502 |
* statistics logging functions. |
503 |
* |
504 |
******************************************************************************/ |
505 |
|
506 |
#ifdef CS_LOGSTATS |
507 |
/* cst_tot is for the life of the server, cst_last is for the last series of |
508 |
* stats |
509 |
*/ |
510 |
CS_Stats cst_tot, cst_lst; |
511 |
|
512 |
/** |
513 |
* Writes out the gathered stats. We clear cst_lst. |
514 |
*/ |
515 |
void write_cs_stats(void) |
516 |
{ |
517 |
time_t now=time(NULL); |
518 |
|
519 |
/* If no connections recently, don't both to log anything */ |
520 |
if (cst_lst.ibytes==0 && cst_lst.obytes==0) return; |
521 |
|
522 |
/* CSSTAT is put in so scripts can easily find the line */ |
523 |
LOG(llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", |
524 |
ctime(&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, |
525 |
now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, |
526 |
cst_lst.max_conn, now - cst_lst.time_start); |
527 |
cst_lst.ibytes=0; |
528 |
cst_lst.obytes=0; |
529 |
cst_lst.max_conn=socket_info.nconns; |
530 |
cst_lst.time_start=now; |
531 |
} |
532 |
#endif |