ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/lowlevel.C
(Generate patch)

Comparing deliantra/server/socket/lowlevel.C (file contents):
Revision 1.5 by root, Sun Sep 10 13:43:33 2006 UTC vs.
Revision 1.15 by root, Thu Dec 14 01:59:10 2006 UTC

1
2/*
3 * static char *rcsid_sockets_c =
4 * "$Id: lowlevel.C,v 1.5 2006/09/10 13:43:33 root Exp $";
5 */
6
7/* 1/*
8 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
9 3
10 Copyright (C) 1992 Frank Tore Johansen 4 Copyright (C) 1992 Frank Tore Johansen
11 5
41using namespace std; 35using namespace std;
42 36
43#include <global.h> 37#include <global.h>
44#include <newclient.h> 38#include <newclient.h>
45#include <sproto.h> 39#include <sproto.h>
40#include <cstdarg>
46 41
47#ifdef __linux__ 42#ifdef __linux__
48# include <sys/types.h> 43# include <sys/types.h>
49# include <sys/socket.h> 44# include <sys/socket.h>
50# include <netinet/in.h> 45# include <netinet/in.h>
78 } 73 }
79 } 74 }
80 75
81 int val; 76 int val;
82 77
83 val = 0;
84 setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); 78 val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
85 val = 1;
86 setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); 79 val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
87#endif 80#endif
88} 81}
89 82
90/*********************************************************************** 83/***********************************************************************
91 * 84 *
92 * SockList functions/utilities 85 * packet functions/utilities
93 * 86 *
94 **********************************************************************/ 87 **********************************************************************/
95 88
96void 89packet &packet::operator <<(const data &v)
97SockList_Init (SockList * sl)
98{ 90{
99 sl->len = 0; 91 if (room () < v.len)
100 sl->buf = NULL; 92 reset ();
101} 93 else
94 {
95 if (v.len)
96 {
97 memcpy (cur, v.ptr, v.len);
98 cur += v.len;
99 }
100 }
102 101
103void 102 return *this;
104SockList_AddInt (SockList * sl, uint32 data)
105{
106 sl->buf[sl->len++] = (data >> 24) & 0xff;
107 sl->buf[sl->len++] = (data >> 16) & 0xff;
108 sl->buf[sl->len++] = (data >> 8) & 0xff;
109 sl->buf[sl->len++] = data & 0xff;
110} 103}
111 104
112void 105packet &packet::operator <<(const data8 &v)
113SockList_AddInt64 (SockList * sl, uint64 data)
114{ 106{
115 sl->buf[sl->len++] = (char) ((data >> 56) & 0xff); 107 unsigned int len = min (v.len, 0x00FF);
116 sl->buf[sl->len++] = (char) ((data >> 48) & 0xff); 108 return *this << uint8 (len) << data (v.ptr, len);
117 sl->buf[sl->len++] = (char) ((data >> 40) & 0xff);
118 sl->buf[sl->len++] = (char) ((data >> 32) & 0xff);
119
120 sl->buf[sl->len++] = (char) ((data >> 24) & 0xff);
121 sl->buf[sl->len++] = (char) ((data >> 16) & 0xff);
122 sl->buf[sl->len++] = (char) ((data >> 8) & 0xff);
123 sl->buf[sl->len++] = (char) (data & 0xff);
124} 109}
125 110
126/* Basically does the reverse of SockList_AddInt, but on 111packet &packet::operator <<(const data16 &v)
127 * strings instead. Same for the GetShort, but for 16 bits. 112{
113 unsigned int len = min (v.len, 0xFFFF);
114 return *this << uint16 (len) << data (v.ptr, len);
115}
116
117packet &packet::operator <<(const char *v)
118{
119 return *this << data (v, strlen (v ? v : 0));
120}
121
122void
123packet::printf (const char *format, ...)
124{
125 int size = room ();
126
127 va_list ap;
128 va_start (ap, format);
129 int len = vsnprintf ((char *)cur, size, format, ap);
130 va_end (ap);
131
132 if (len >= size)
133 return reset ();
134
135 cur += len;
136}
137
138/******************************************************************************
128 */ 139 *
140 * Start of read routines.
141 *
142 ******************************************************************************/
143
129int 144int
130GetInt_String (unsigned char *data) 145NewSocket::read_packet ()
131{ 146{
132 return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); 147 for (;;)
133}
134
135short
136GetShort_String (unsigned char *data)
137{
138 return ((data[0] << 8) + data[1]);
139}
140
141/******************************************************************************
142 *
143 * Start of read routines.
144 *
145 ******************************************************************************/
146
147/**
148 * This reads from fd and puts the data in sl. We return true if we think
149 * we have a full packet, 0 if we have a partial packet. The only processing
150 * we do is remove the intial size value. len (As passed) is the size of the
151 * buffer allocated in the socklist. We make the assumption the buffer is
152 * at least 2 bytes long.
153 */
154
155int
156SockList_ReadPacket (int fd, SockList * sl, int len)
157{
158 int stat, toread;
159
160 /* Sanity check - shouldn't happen */
161 if (sl->len < 0)
162 {
163 abort ();
164 } 148 {
165 /* We already have a partial packet */ 149 if (inbuf_len >= 2)
166 if (sl->len < 2)
167 {
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 {
175 stat = read (fd, sl->buf + sl->len, 2 - sl->len);
176 } 150 {
177 while ((stat == -1) && (errno == EINTR)); 151 unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1];
178#endif 152
179 if (stat < 0) 153 if (inbuf_len >= 2 + pkt_len)
154 return pkt_len + 2;
180 { 155 }
181 /* In non blocking mode, EAGAIN is set when there is no 156
182 * data available. 157 int amount = sizeof (inbuf) - inbuf_len;
183 */ 158
184#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ 159 if (amount <= 0)
185 if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK) 160 {
161 LOG (llevError, "packet too large");//TODO
162 return -1;
163 }
164
165 amount = read (fd, inbuf + inbuf_len, amount);
166
167 if (!amount)
168 {
169 status = Ns_Dead;
170 return -1;
171 }
172 else if (amount < 0)
173 {
174 if (errno != EAGAIN && errno != EINTR)
186 { 175 {
187 if (WSAGetLastError () == WSAECONNRESET) 176 LOG (llevError, "read error: %s", strerror (errno));
188 LOG (llevDebug, "Connection closed by client\n"); 177 return -1;
189 else
190 {
191 LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ());
192 }
193 return -1; /* kick this user! */
194 } 178 }
195#else
196 if (errno != EAGAIN && errno != EWOULDBLOCK)
197 {
198 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
199 }
200#endif
201 return 0; /*Error */
202 }
203 if (stat == 0)
204 return -1;
205 sl->len += stat;
206#ifdef CS_LOGSTATS
207 cst_tot.ibytes += stat;
208 cst_lst.ibytes += stat;
209#endif
210 if (stat < 2)
211 return 0; /* Still don't have a full packet */
212 }
213 /* Figure out how much more data we need to read. Add 2 from the
214 * end of this - size header information is not included.
215 */
216 toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len;
217 if ((toread + sl->len) >= len)
218 {
219 LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len);
220 /* Quick hack in case for 'oldsocketmode' input. If we are
221 * closing the socket anyways, then reading this extra 100 bytes
222 * shouldn't hurt.
223 */
224#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
225 recv (fd, sl->buf + 2, 100, 0);
226#else
227 read (fd, sl->buf + 2, 100);
228#endif /* end win32 */
229 179
230 /* return error so the socket is closed */
231 return -1;
232 }
233 do
234 {
235#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
236 stat = recv (fd, sl->buf + sl->len, toread, 0);
237#else
238 do
239 {
240 stat = read (fd, sl->buf + sl->len, toread);
241 }
242 while ((stat < 0) && (errno == EINTR));
243#endif
244 if (stat < 0)
245 {
246
247#ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */
248 if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK)
249 {
250 if (WSAGetLastError () == WSAECONNRESET)
251 LOG (llevDebug, "Connection closed by client\n");
252 else
253 {
254 LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ());
255 }
256 return -1; /* kick this user! */
257 }
258#else
259 if (errno != EAGAIN && errno != EWOULDBLOCK)
260 {
261 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
262 }
263#endif
264 return 0; /*Error */
265 }
266 if (stat == 0)
267 return -1;
268 sl->len += stat;
269#ifdef CS_LOGSTATS
270 cst_tot.ibytes += stat;
271 cst_lst.ibytes += stat;
272#endif
273 toread -= stat;
274 if (toread == 0)
275 return 1;
276 if (toread < 0)
277 {
278 LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n");
279 return 1; 180 return 0;
280 } 181 }
182
183 inbuf_len += amount;
184
185 cst_tot.ibytes += amount;
186 cst_lst.ibytes += amount;
281 } 187 }
282 while (toread > 0); 188}
283 return 0; 189
190void
191NewSocket::skip_packet (int len)
192{
193 inbuf_len -= len;
194 memmove (inbuf, inbuf + len, inbuf_len);
284} 195}
285 196
286/******************************************************************************* 197/*******************************************************************************
287 * 198 *
288 * Start of write related routines. 199 * Start of write related routines.
295 * ns is the socket we are adding the data to, buf is the start of the 206 * ns is the socket we are adding the data to, buf is the start of the
296 * data, and len is the number of bytes to add. 207 * data, and len is the number of bytes to add.
297 */ 208 */
298 209
299static void 210static void
300add_to_buffer (NewSocket * ns, char *buf, int len) 211add_to_buffer (NewSocket *ns, char *buf, int len)
301{ 212{
302 int avail, end; 213 int avail, end;
303 214
304 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) 215 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE)
305 { 216 {
314 225
315 end = ns->outputbuffer.start + ns->outputbuffer.len; 226 end = ns->outputbuffer.start + ns->outputbuffer.len;
316 /* The buffer is already in a wrapped state, so adjust end */ 227 /* The buffer is already in a wrapped state, so adjust end */
317 if (end >= SOCKETBUFSIZE) 228 if (end >= SOCKETBUFSIZE)
318 end -= SOCKETBUFSIZE; 229 end -= SOCKETBUFSIZE;
230
319 avail = SOCKETBUFSIZE - end; 231 avail = SOCKETBUFSIZE - end;
320 232
321 /* We can all fit it behind the current data without wrapping */ 233 /* We can all fit it behind the current data without wrapping */
322 if (avail >= len) 234 if (avail >= len)
323 {
324 memcpy (ns->outputbuffer.data + end, buf, len); 235 memcpy (ns->outputbuffer.data + end, buf, len);
325 }
326 else 236 else
327 { 237 {
328 memcpy (ns->outputbuffer.data + end, buf, avail); 238 memcpy (ns->outputbuffer.data + end, buf, avail);
329 memcpy (ns->outputbuffer.data, buf + avail, len - avail); 239 memcpy (ns->outputbuffer.data, buf + avail, len - avail);
330 } 240 }
241
331 ns->outputbuffer.len += len; 242 ns->outputbuffer.len += len;
332#if 0 243#if 0
333 LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); 244 LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start);
334#endif 245#endif
335} 246}
355 { 266 {
356 max = SOCKETBUFSIZE - ns->outputbuffer.start; 267 max = SOCKETBUFSIZE - ns->outputbuffer.start;
357 if (ns->outputbuffer.len < max) 268 if (ns->outputbuffer.len < max)
358 max = ns->outputbuffer.len; 269 max = ns->outputbuffer.len;
359 270
360#ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */
361 amt = send (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max, 0);
362#else
363 do 271 do
364 { 272 {
365 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); 273 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max);
366 } 274 }
367 while ((amt < 0) && (errno == EINTR)); 275 while ((amt < 0) && (errno == EINTR));
368#endif
369 276
370 if (amt < 0) 277 if (amt < 0)
371 { /* We got an error */ 278 { /* We got an error */
372 279
373#ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */
374 if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK)
375 {
376 LOG (llevError, "New socket write failed (wsb) (%d).\n", WSAGetLastError ());
377#else
378 if (errno != EWOULDBLOCK) 280 if (errno != EWOULDBLOCK)
379 { 281 {
380 LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); 282 LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno));
381#endif
382 ns->status = Ns_Dead; 283 ns->status = Ns_Dead;
383 return; 284 return;
384 } 285 }
385 else 286 else
386 { /* EWOULDBLOCK */ 287 { /* EWOULDBLOCK */
408 * 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
409 * of bytes to write. IT doesn't return anything - rather, it 310 * of bytes to write. IT doesn't return anything - rather, it
410 * updates the ns structure if we get an error. 311 * updates the ns structure if we get an error.
411 */ 312 */
412void 313void
413Write_To_Socket (NewSocket * ns, char *buf, int len) 314NewSocket::send (void *buf_, int len)
414{ 315{
316 char *buf = (char *)buf_;
317 char *pos = buf;
415 int amt = 0; 318 int amt = 0;
416 char *pos = buf;
417 319
418 if (ns->status == Ns_Dead || !buf) 320 if (status == Ns_Dead || !buf)
419 { 321 {
420 LOG (llevDebug, "Write_To_Socket called with dead socket\n"); 322 LOG (llevDebug, "Write_To_Socket called with dead socket\n");
421 return; 323 return;
422 } 324 }
423 325
424#ifndef __GNU__ /* This caused problems on Hurd */ 326#ifndef __GNU__ /* This caused problems on Hurd */
425 if (!ns->can_write) 327 if (!can_write)
426 { 328 {
427 add_to_buffer (ns, buf, len); 329 add_to_buffer (this, buf, len);
428 return; 330 return;
429 } 331 }
430#endif 332#endif
333
431 /* 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 */
432 while (len > 0) 335 while (len > 0)
433 { 336 {
434
435#ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */
436 amt = send (ns->fd, pos, len, 0);
437#else
438 do 337 do
439 { 338 {
440 amt = write (ns->fd, pos, len); 339 amt = write (fd, pos, len);
441 } 340 }
442 while ((amt < 0) && (errno == EINTR)); 341 while ((amt < 0) && (errno == EINTR));
443#endif
444 342
445 if (amt < 0) 343 if (amt < 0)
446 { /* We got an error */ 344 { /* We got an error */
447#ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */
448 if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK)
449 {
450 LOG (llevError, "New socket write failed WTS (%d).\n", WSAGetLastError ());
451#else
452 if (errno != EWOULDBLOCK) 345 if (errno != EWOULDBLOCK)
453 { 346 {
454 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ 347 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */
455 errno, strerror (errno)); 348 errno, strerror (errno));
456#endif
457 ns->status = Ns_Dead; 349 status = Ns_Dead;
458 return; 350 return;
459 } 351 }
460 else 352 else
461 { /* EWOULDBLOCK */ 353 { /* EWOULDBLOCK */
462 /* can't write it, so store it away. */ 354 /* can't write it, so store it away. */
463 add_to_buffer (ns, pos, len); 355 add_to_buffer (this, pos, len);
464 ns->can_write = 0; 356 can_write = 0;
465 return; 357 return;
466 } 358 }
467 } 359 }
468 /* 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
469 * an else if to make sure we don't reprocess it. 361 * an else if to make sure we don't reprocess it.
470 */ 362 */
471 else if (amt == 0) 363 else if (amt == 0)
472 {
473 LOG (llevError, "Write_To_Socket: No data written out.\n"); 364 LOG (llevError, "Write_To_Socket: No data written out.\n");
474 } 365
475 len -= amt; 366 len -= amt;
476 pos += amt; 367 pos += amt;
477#ifdef CS_LOGSTATS 368#ifdef CS_LOGSTATS
478 cst_tot.obytes += amt; 369 cst_tot.obytes += amt;
479 cst_lst.obytes += amt; 370 cst_lst.obytes += amt;
480#endif 371#endif
481 } 372 }
482} 373}
483 374
484
485/** 375/**
486 * Takes a string of data, and writes it out to the socket. A very handy 376 * Takes a string of data, and writes it out to the socket. A very handy
487 * shortcut function. 377 * shortcut function.
488 */ 378 */
489void
490cs_write_string (NewSocket * ns, const char *buf, int len)
491{
492 SockList sl;
493 379
494 sl.len = len; 380void
495 sl.buf = (unsigned char *) buf; 381NewSocket::send_packet (packet &sl)
382{
496 Send_With_Handling (ns, &sl); 383 Send_With_Handling (this, &sl);
497} 384}
498 385
386void
387NewSocket::send_packet (const char *buf, int len)
388{
389 packet sl;
390
391 sl << data (buf, len);
392 send_packet (sl);
393}
394
395void
396NewSocket::send_packet (const char *buf)
397{
398 send_packet (buf, strlen (buf));
399}
499 400
500/** 401/**
501 * Calls Write_To_Socket to send data to the client. 402 * Calls Write_To_Socket to send data to the client.
502 * 403 *
503 * The only difference in this function is that we take a SockList 404 * The only difference in this function is that we take a packet
504 *, and we prepend the length information. 405 *, and we prepend the length information.
505 */ 406 */
506void 407void
507Send_With_Handling (NewSocket * ns, SockList * msg) 408Send_With_Handling (NewSocket *ns, packet *msg)
508{ 409{
509 unsigned char sbuf[4]; 410 unsigned char sbuf[4];
510 411
511 if (ns->status == Ns_Dead || !msg) 412 if (ns->status == Ns_Dead || !msg)
512 return; 413 return;
513 414
514 if (msg->len >= MAXSOCKBUF) 415 if (msg->length () >= MAXSOCKBUF)
515 { 416 {
516 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 ());
517 /* 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
518 * it easier to debug. 419 * it easier to debug.
519 */ 420 */
520 abort (); 421 abort ();
521 } 422 }
423
522 sbuf[0] = ((uint32) (msg->len) >> 8) & 0xFF; 424 sbuf[0] = ((uint32) (msg->length ()) >> 8);
523 sbuf[1] = ((uint32) (msg->len)) & 0xFF; 425 sbuf[1] = ((uint32) (msg->length ()) );
426
524 if (ns->status != Ns_Old) 427 if (ns->status != Ns_Old)
525 Write_To_Socket (ns, (char *) sbuf, 2); 428 ns->send (sbuf, 2);
526 Write_To_Socket (ns, (char *) msg->buf, msg->len);
527}
528 429
529/** 430 ns->send (msg->buf, msg->length ());
530 * Takes a string of data, and writes it out to the socket. A very handy
531 * shortcut function.
532 */
533void
534Write_String_To_Socket (NewSocket * ns, char *buf, int len)
535{
536 SockList sl;
537
538 sl.len = len;
539 sl.buf = (unsigned char *) buf;
540 Send_With_Handling (ns, &sl);
541} 431}
542
543 432
544/****************************************************************************** 433/******************************************************************************
545 * 434 *
546 * statistics logging functions. 435 * statistics logging functions.
547 * 436 *
574 cst_lst.obytes = 0; 463 cst_lst.obytes = 0;
575 cst_lst.max_conn = socket_info.nconns; 464 cst_lst.max_conn = socket_info.nconns;
576 cst_lst.time_start = now; 465 cst_lst.time_start = now;
577} 466}
578#endif 467#endif
468

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines