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.4 by root, Wed Aug 30 16:30:37 2006 UTC vs.
Revision 1.5 by root, Sun Sep 10 13:43:33 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines