… | |
… | |
35 | using namespace std; |
35 | using namespace std; |
36 | |
36 | |
37 | #include <global.h> |
37 | #include <global.h> |
38 | #include <newclient.h> |
38 | #include <newclient.h> |
39 | #include <sproto.h> |
39 | #include <sproto.h> |
|
|
40 | #include <cstdarg> |
40 | |
41 | |
41 | #ifdef __linux__ |
42 | #ifdef __linux__ |
42 | # include <sys/types.h> |
43 | # include <sys/types.h> |
43 | # include <sys/socket.h> |
44 | # include <sys/socket.h> |
44 | # include <netinet/in.h> |
45 | # include <netinet/in.h> |
… | |
… | |
72 | } |
73 | } |
73 | } |
74 | } |
74 | |
75 | |
75 | int val; |
76 | int val; |
76 | |
77 | |
77 | val = 0; |
|
|
78 | setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
78 | val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
79 | val = 1; |
|
|
80 | setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
79 | val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
81 | #endif |
80 | #endif |
82 | } |
81 | } |
83 | |
82 | |
84 | /*********************************************************************** |
83 | /*********************************************************************** |
85 | * |
84 | * |
86 | * SockList functions/utilities |
85 | * SockList functions/utilities |
87 | * |
86 | * |
88 | **********************************************************************/ |
87 | **********************************************************************/ |
89 | |
88 | |
90 | void |
89 | SockList &SockList::operator <<(const data &v) |
91 | SockList_Init (SockList * sl) |
|
|
92 | { |
90 | { |
93 | sl->len = 0; |
91 | if (v.len) |
94 | sl->buf = NULL; |
92 | { |
95 | } |
93 | memcpy (buf + len, v.ptr, v.len); |
|
|
94 | len += v.len; |
|
|
95 | } |
96 | |
96 | |
97 | void |
97 | return *this; |
98 | SockList_AddInt (SockList * sl, uint32 data) |
|
|
99 | { |
|
|
100 | sl->buf[sl->len++] = (data >> 24) & 0xff; |
|
|
101 | sl->buf[sl->len++] = (data >> 16) & 0xff; |
|
|
102 | sl->buf[sl->len++] = (data >> 8) & 0xff; |
|
|
103 | sl->buf[sl->len++] = data & 0xff; |
|
|
104 | } |
98 | } |
105 | |
99 | |
106 | void |
100 | SockList &SockList::operator <<(const data8 &v) |
107 | SockList_AddInt64 (SockList * sl, uint64 data) |
|
|
108 | { |
101 | { |
109 | sl->buf[sl->len++] = (char) ((data >> 56) & 0xff); |
102 | unsigned int len = min (v.len, 0x00FF); |
110 | sl->buf[sl->len++] = (char) ((data >> 48) & 0xff); |
103 | return *this << uint8 (len) << data (v.ptr, len); |
111 | sl->buf[sl->len++] = (char) ((data >> 40) & 0xff); |
104 | } |
112 | sl->buf[sl->len++] = (char) ((data >> 32) & 0xff); |
|
|
113 | |
105 | |
114 | sl->buf[sl->len++] = (char) ((data >> 24) & 0xff); |
106 | SockList &SockList::operator <<(const data16 &v) |
115 | sl->buf[sl->len++] = (char) ((data >> 16) & 0xff); |
107 | { |
116 | sl->buf[sl->len++] = (char) ((data >> 8) & 0xff); |
108 | unsigned int len = min (v.len, 0xFFFF); |
117 | sl->buf[sl->len++] = (char) (data & 0xff); |
109 | return *this << uint16 (len) << data (v.ptr, len); |
|
|
110 | } |
|
|
111 | |
|
|
112 | SockList &SockList::operator <<(const char *v) |
|
|
113 | { |
|
|
114 | return *this << data (v, strlen (v ? v : 0)); |
|
|
115 | } |
|
|
116 | |
|
|
117 | void |
|
|
118 | SockList::printf (const char *format, ...) |
|
|
119 | { |
|
|
120 | va_list ap; |
|
|
121 | va_start (ap, format); |
|
|
122 | |
|
|
123 | len += vsprintf ((char *)buf + len, format, ap); |
|
|
124 | |
|
|
125 | va_end (ap); |
118 | } |
126 | } |
119 | |
127 | |
120 | /* Basically does the reverse of SockList_AddInt, but on |
128 | /* Basically does the reverse of SockList_AddInt, but on |
121 | * strings instead. Same for the GetShort, but for 16 bits. |
129 | * strings instead. Same for the GetShort, but for 16 bits. |
122 | */ |
130 | */ |
… | |
… | |
145 | * buffer allocated in the socklist. We make the assumption the buffer is |
153 | * buffer allocated in the socklist. We make the assumption the buffer is |
146 | * at least 2 bytes long. |
154 | * at least 2 bytes long. |
147 | */ |
155 | */ |
148 | |
156 | |
149 | int |
157 | int |
150 | SockList_ReadPacket (int fd, SockList * sl, int len) |
158 | SockList_ReadPacket (int fd, SockList *sl, int len) |
151 | { |
159 | { |
152 | int stat, toread; |
160 | int stat, toread; |
153 | |
161 | |
154 | /* Sanity check - shouldn't happen */ |
162 | /* Sanity check - shouldn't happen */ |
155 | if (sl->len < 0) |
163 | if (sl->len < 0) |
… | |
… | |
157 | abort (); |
165 | abort (); |
158 | } |
166 | } |
159 | /* We already have a partial packet */ |
167 | /* We already have a partial packet */ |
160 | if (sl->len < 2) |
168 | if (sl->len < 2) |
161 | { |
169 | { |
162 | #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */ |
|
|
163 | |
|
|
164 | stat = recv (fd, sl->buf + sl->len, 2 - sl->len, 0); |
|
|
165 | |
|
|
166 | #else |
|
|
167 | do |
170 | do |
168 | { |
171 | { |
169 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
172 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
170 | } |
173 | } |
171 | while ((stat == -1) && (errno == EINTR)); |
174 | while ((stat == -1) && (errno == EINTR)); |
172 | #endif |
175 | |
173 | if (stat < 0) |
176 | if (stat < 0) |
174 | { |
177 | { |
175 | /* In non blocking mode, EAGAIN is set when there is no |
178 | /* In non blocking mode, EAGAIN is set when there is no |
176 | * data available. |
179 | * data available. |
177 | */ |
180 | */ |
178 | #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ |
|
|
179 | if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
180 | { |
|
|
181 | if (WSAGetLastError () == WSAECONNRESET) |
|
|
182 | LOG (llevDebug, "Connection closed by client\n"); |
|
|
183 | else |
|
|
184 | { |
|
|
185 | LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ()); |
|
|
186 | } |
|
|
187 | return -1; /* kick this user! */ |
|
|
188 | } |
|
|
189 | #else |
|
|
190 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
181 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
191 | { |
182 | { |
192 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
183 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
193 | } |
184 | } |
194 | #endif |
|
|
195 | return 0; /*Error */ |
185 | return 0; /*Error */ |
196 | } |
186 | } |
197 | if (stat == 0) |
187 | if (stat == 0) |
198 | return -1; |
188 | return -1; |
199 | sl->len += stat; |
189 | sl->len += stat; |
… | |
… | |
213 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
203 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
214 | /* Quick hack in case for 'oldsocketmode' input. If we are |
204 | /* Quick hack in case for 'oldsocketmode' input. If we are |
215 | * closing the socket anyways, then reading this extra 100 bytes |
205 | * closing the socket anyways, then reading this extra 100 bytes |
216 | * shouldn't hurt. |
206 | * shouldn't hurt. |
217 | */ |
207 | */ |
218 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
|
|
219 | recv (fd, sl->buf + 2, 100, 0); |
|
|
220 | #else |
|
|
221 | read (fd, sl->buf + 2, 100); |
208 | read (fd, sl->buf + 2, 100); |
222 | #endif /* end win32 */ |
|
|
223 | |
209 | |
224 | /* return error so the socket is closed */ |
210 | /* return error so the socket is closed */ |
225 | return -1; |
211 | return -1; |
226 | } |
212 | } |
227 | do |
213 | do |
228 | { |
214 | { |
229 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
|
|
230 | stat = recv (fd, sl->buf + sl->len, toread, 0); |
|
|
231 | #else |
|
|
232 | do |
215 | do |
233 | { |
216 | { |
234 | stat = read (fd, sl->buf + sl->len, toread); |
217 | stat = read (fd, sl->buf + sl->len, toread); |
235 | } |
218 | } |
236 | while ((stat < 0) && (errno == EINTR)); |
219 | while ((stat < 0) && (errno == EINTR)); |
237 | #endif |
|
|
238 | if (stat < 0) |
220 | if (stat < 0) |
239 | { |
221 | { |
240 | |
222 | |
241 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */ |
|
|
242 | if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
243 | { |
|
|
244 | if (WSAGetLastError () == WSAECONNRESET) |
|
|
245 | LOG (llevDebug, "Connection closed by client\n"); |
|
|
246 | else |
|
|
247 | { |
|
|
248 | LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ()); |
|
|
249 | } |
|
|
250 | return -1; /* kick this user! */ |
|
|
251 | } |
|
|
252 | #else |
|
|
253 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
223 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
254 | { |
224 | { |
255 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
225 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
256 | } |
226 | } |
257 | #endif |
|
|
258 | return 0; /*Error */ |
227 | return 0; /*Error */ |
259 | } |
228 | } |
260 | if (stat == 0) |
229 | if (stat == 0) |
261 | return -1; |
230 | return -1; |
262 | sl->len += stat; |
231 | sl->len += stat; |
… | |
… | |
349 | { |
318 | { |
350 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
319 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
351 | if (ns->outputbuffer.len < max) |
320 | if (ns->outputbuffer.len < max) |
352 | max = ns->outputbuffer.len; |
321 | max = ns->outputbuffer.len; |
353 | |
322 | |
354 | #ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */ |
|
|
355 | amt = send (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max, 0); |
|
|
356 | #else |
|
|
357 | do |
323 | do |
358 | { |
324 | { |
359 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
325 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
360 | } |
326 | } |
361 | while ((amt < 0) && (errno == EINTR)); |
327 | while ((amt < 0) && (errno == EINTR)); |
362 | #endif |
|
|
363 | |
328 | |
364 | if (amt < 0) |
329 | if (amt < 0) |
365 | { /* We got an error */ |
330 | { /* We got an error */ |
366 | |
331 | |
367 | #ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */ |
|
|
368 | if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
369 | { |
|
|
370 | LOG (llevError, "New socket write failed (wsb) (%d).\n", WSAGetLastError ()); |
|
|
371 | #else |
|
|
372 | if (errno != EWOULDBLOCK) |
332 | if (errno != EWOULDBLOCK) |
373 | { |
333 | { |
374 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
334 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
375 | #endif |
|
|
376 | ns->status = Ns_Dead; |
335 | ns->status = Ns_Dead; |
377 | return; |
336 | return; |
378 | } |
337 | } |
379 | else |
338 | else |
380 | { /* EWOULDBLOCK */ |
339 | { /* EWOULDBLOCK */ |
… | |
… | |
424 | #endif |
383 | #endif |
425 | /* If we manage to write more than we wanted, take it as a bonus */ |
384 | /* If we manage to write more than we wanted, take it as a bonus */ |
426 | while (len > 0) |
385 | while (len > 0) |
427 | { |
386 | { |
428 | |
387 | |
429 | #ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */ |
|
|
430 | amt = send (ns->fd, pos, len, 0); |
|
|
431 | #else |
|
|
432 | do |
388 | do |
433 | { |
389 | { |
434 | amt = write (ns->fd, pos, len); |
390 | amt = write (ns->fd, pos, len); |
435 | } |
391 | } |
436 | while ((amt < 0) && (errno == EINTR)); |
392 | while ((amt < 0) && (errno == EINTR)); |
437 | #endif |
|
|
438 | |
393 | |
439 | if (amt < 0) |
394 | if (amt < 0) |
440 | { /* We got an error */ |
395 | { /* We got an error */ |
441 | #ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */ |
|
|
442 | if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
443 | { |
|
|
444 | LOG (llevError, "New socket write failed WTS (%d).\n", WSAGetLastError ()); |
|
|
445 | #else |
|
|
446 | if (errno != EWOULDBLOCK) |
396 | if (errno != EWOULDBLOCK) |
447 | { |
397 | { |
448 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
398 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
449 | errno, strerror (errno)); |
399 | errno, strerror (errno)); |
450 | #endif |
|
|
451 | ns->status = Ns_Dead; |
400 | ns->status = Ns_Dead; |
452 | return; |
401 | return; |
453 | } |
402 | } |
454 | else |
403 | else |
455 | { /* EWOULDBLOCK */ |
404 | { /* EWOULDBLOCK */ |
… | |
… | |
479 | /** |
428 | /** |
480 | * Takes a string of data, and writes it out to the socket. A very handy |
429 | * Takes a string of data, and writes it out to the socket. A very handy |
481 | * shortcut function. |
430 | * shortcut function. |
482 | */ |
431 | */ |
483 | void |
432 | void |
484 | cs_write_string (NewSocket * ns, const char *buf, int len) |
433 | cs_write_string (NewSocket *ns, const char *buf, int len) |
485 | { |
434 | { |
486 | SockList sl; |
435 | SockList sl; |
487 | |
436 | |
488 | sl.len = len; |
437 | sl.len = len; |
489 | sl.buf = (unsigned char *) buf; |
438 | sl.buf = (unsigned char *) buf; |
… | |
… | |
496 | * |
445 | * |
497 | * The only difference in this function is that we take a SockList |
446 | * The only difference in this function is that we take a SockList |
498 | *, and we prepend the length information. |
447 | *, and we prepend the length information. |
499 | */ |
448 | */ |
500 | void |
449 | void |
501 | Send_With_Handling (NewSocket * ns, SockList * msg) |
450 | Send_With_Handling (NewSocket *ns, SockList *msg) |
502 | { |
451 | { |
503 | unsigned char sbuf[4]; |
452 | unsigned char sbuf[4]; |
504 | |
453 | |
505 | if (ns->status == Ns_Dead || !msg) |
454 | if (ns->status == Ns_Dead || !msg) |
506 | return; |
455 | return; |
… | |
… | |
523 | /** |
472 | /** |
524 | * Takes a string of data, and writes it out to the socket. A very handy |
473 | * Takes a string of data, and writes it out to the socket. A very handy |
525 | * shortcut function. |
474 | * shortcut function. |
526 | */ |
475 | */ |
527 | void |
476 | void |
528 | Write_String_To_Socket (NewSocket * ns, char *buf, int len) |
477 | Write_String_To_Socket (NewSocket *ns, char *buf, int len) |
529 | { |
478 | { |
530 | SockList sl; |
479 | SockList sl; |
531 | |
480 | |
532 | sl.len = len; |
481 | sl.len = len; |
533 | sl.buf = (unsigned char *) buf; |
482 | sl.buf = (unsigned char *) buf; |
… | |
… | |
568 | cst_lst.obytes = 0; |
517 | cst_lst.obytes = 0; |
569 | cst_lst.max_conn = socket_info.nconns; |
518 | cst_lst.max_conn = socket_info.nconns; |
570 | cst_lst.time_start = now; |
519 | cst_lst.time_start = now; |
571 | } |
520 | } |
572 | #endif |
521 | #endif |
|
|
522 | |