ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/deliantra/server/socket/lowlevel.c
Revision: 1.3
Committed: Fri Jun 2 00:49:03 2006 UTC (20 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.2: +20 -0 lines
Log Message:
on linux, check wether the data sent/ack received difference grows larger than 20s, then disconnect. this should be a reliable way to detect dead connections without adding any bandwidth overhead

File Contents

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