ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/lowlevel.c
Revision: 1.1
Committed: Fri Feb 3 07:14:44 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Branch point for: UPSTREAM
Log Message:
Initial revision

File Contents

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