… | |
… | |
72 | } |
72 | } |
73 | } |
73 | } |
74 | |
74 | |
75 | int val; |
75 | int val; |
76 | |
76 | |
77 | val = 0; |
|
|
78 | setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
77 | 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)); |
78 | val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
81 | #endif |
79 | #endif |
82 | } |
80 | } |
83 | |
81 | |
84 | /*********************************************************************** |
82 | /*********************************************************************** |
85 | * |
83 | * |
86 | * SockList functions/utilities |
84 | * SockList functions/utilities |
87 | * |
85 | * |
88 | **********************************************************************/ |
86 | **********************************************************************/ |
89 | |
87 | |
90 | void |
88 | SockList &SockList::operator <<(const data8 &v) |
91 | SockList_Init (SockList * sl) |
|
|
92 | { |
89 | { |
93 | sl->len = 0; |
90 | *this << uint8 (v.len); |
94 | sl->buf = NULL; |
|
|
95 | } |
|
|
96 | |
91 | |
97 | void |
92 | memcpy (buf + len, v.data, v.len); |
98 | SockList_AddInt (SockList * sl, uint32 data) |
93 | len += v.len; |
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 | } |
|
|
105 | |
94 | |
106 | void |
95 | return *this; |
107 | SockList_AddInt64 (SockList * sl, uint64 data) |
96 | } |
108 | { |
|
|
109 | sl->buf[sl->len++] = (char) ((data >> 56) & 0xff); |
|
|
110 | sl->buf[sl->len++] = (char) ((data >> 48) & 0xff); |
|
|
111 | sl->buf[sl->len++] = (char) ((data >> 40) & 0xff); |
|
|
112 | sl->buf[sl->len++] = (char) ((data >> 32) & 0xff); |
|
|
113 | |
97 | |
114 | sl->buf[sl->len++] = (char) ((data >> 24) & 0xff); |
98 | SockList &SockList::operator <<(const data16 &v) |
115 | sl->buf[sl->len++] = (char) ((data >> 16) & 0xff); |
99 | { |
116 | sl->buf[sl->len++] = (char) ((data >> 8) & 0xff); |
100 | *this << uint16 (v.len); |
117 | sl->buf[sl->len++] = (char) (data & 0xff); |
101 | |
|
|
102 | memcpy (buf + len, v.data, v.len); |
|
|
103 | len += v.len; |
|
|
104 | |
|
|
105 | return *this; |
|
|
106 | } |
|
|
107 | |
|
|
108 | SockList &SockList::operator <<(const char *v) |
|
|
109 | { |
|
|
110 | if (v) |
|
|
111 | { |
|
|
112 | int l = strlen (v); |
|
|
113 | memcpy (buf + len, v, l); |
|
|
114 | len += l; |
|
|
115 | } |
|
|
116 | |
|
|
117 | return *this; |
118 | } |
118 | } |
119 | |
119 | |
120 | /* Basically does the reverse of SockList_AddInt, but on |
120 | /* Basically does the reverse of SockList_AddInt, but on |
121 | * strings instead. Same for the GetShort, but for 16 bits. |
121 | * strings instead. Same for the GetShort, but for 16 bits. |
122 | */ |
122 | */ |
… | |
… | |
157 | abort (); |
157 | abort (); |
158 | } |
158 | } |
159 | /* We already have a partial packet */ |
159 | /* We already have a partial packet */ |
160 | if (sl->len < 2) |
160 | if (sl->len < 2) |
161 | { |
161 | { |
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 |
162 | do |
168 | { |
163 | { |
169 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
164 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
170 | } |
165 | } |
171 | while ((stat == -1) && (errno == EINTR)); |
166 | while ((stat == -1) && (errno == EINTR)); |
172 | #endif |
167 | |
173 | if (stat < 0) |
168 | if (stat < 0) |
174 | { |
169 | { |
175 | /* In non blocking mode, EAGAIN is set when there is no |
170 | /* In non blocking mode, EAGAIN is set when there is no |
176 | * data available. |
171 | * data available. |
177 | */ |
172 | */ |
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) |
173 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
191 | { |
174 | { |
192 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
175 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
193 | } |
176 | } |
194 | #endif |
|
|
195 | return 0; /*Error */ |
177 | return 0; /*Error */ |
196 | } |
178 | } |
197 | if (stat == 0) |
179 | if (stat == 0) |
198 | return -1; |
180 | return -1; |
199 | sl->len += stat; |
181 | 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); |
195 | 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 |
196 | /* Quick hack in case for 'oldsocketmode' input. If we are |
215 | * closing the socket anyways, then reading this extra 100 bytes |
197 | * closing the socket anyways, then reading this extra 100 bytes |
216 | * shouldn't hurt. |
198 | * shouldn't hurt. |
217 | */ |
199 | */ |
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); |
200 | read (fd, sl->buf + 2, 100); |
222 | #endif /* end win32 */ |
|
|
223 | |
201 | |
224 | /* return error so the socket is closed */ |
202 | /* return error so the socket is closed */ |
225 | return -1; |
203 | return -1; |
226 | } |
204 | } |
227 | do |
205 | do |
228 | { |
206 | { |
229 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
|
|
230 | stat = recv (fd, sl->buf + sl->len, toread, 0); |
|
|
231 | #else |
|
|
232 | do |
207 | do |
233 | { |
208 | { |
234 | stat = read (fd, sl->buf + sl->len, toread); |
209 | stat = read (fd, sl->buf + sl->len, toread); |
235 | } |
210 | } |
236 | while ((stat < 0) && (errno == EINTR)); |
211 | while ((stat < 0) && (errno == EINTR)); |
237 | #endif |
|
|
238 | if (stat < 0) |
212 | if (stat < 0) |
239 | { |
213 | { |
240 | |
214 | |
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) |
215 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
254 | { |
216 | { |
255 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
217 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
256 | } |
218 | } |
257 | #endif |
|
|
258 | return 0; /*Error */ |
219 | return 0; /*Error */ |
259 | } |
220 | } |
260 | if (stat == 0) |
221 | if (stat == 0) |
261 | return -1; |
222 | return -1; |
262 | sl->len += stat; |
223 | sl->len += stat; |
… | |
… | |
308 | |
269 | |
309 | end = ns->outputbuffer.start + ns->outputbuffer.len; |
270 | end = ns->outputbuffer.start + ns->outputbuffer.len; |
310 | /* The buffer is already in a wrapped state, so adjust end */ |
271 | /* The buffer is already in a wrapped state, so adjust end */ |
311 | if (end >= SOCKETBUFSIZE) |
272 | if (end >= SOCKETBUFSIZE) |
312 | end -= SOCKETBUFSIZE; |
273 | end -= SOCKETBUFSIZE; |
|
|
274 | |
313 | avail = SOCKETBUFSIZE - end; |
275 | avail = SOCKETBUFSIZE - end; |
314 | |
276 | |
315 | /* We can all fit it behind the current data without wrapping */ |
277 | /* We can all fit it behind the current data without wrapping */ |
316 | if (avail >= len) |
278 | if (avail >= len) |
317 | { |
|
|
318 | memcpy (ns->outputbuffer.data + end, buf, len); |
279 | memcpy (ns->outputbuffer.data + end, buf, len); |
319 | } |
|
|
320 | else |
280 | else |
321 | { |
281 | { |
322 | memcpy (ns->outputbuffer.data + end, buf, avail); |
282 | memcpy (ns->outputbuffer.data + end, buf, avail); |
323 | memcpy (ns->outputbuffer.data, buf + avail, len - avail); |
283 | memcpy (ns->outputbuffer.data, buf + avail, len - avail); |
324 | } |
284 | } |
|
|
285 | |
325 | ns->outputbuffer.len += len; |
286 | ns->outputbuffer.len += len; |
326 | #if 0 |
287 | #if 0 |
327 | LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); |
288 | LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); |
328 | #endif |
289 | #endif |
329 | } |
290 | } |
… | |
… | |
349 | { |
310 | { |
350 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
311 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
351 | if (ns->outputbuffer.len < max) |
312 | if (ns->outputbuffer.len < max) |
352 | max = ns->outputbuffer.len; |
313 | max = ns->outputbuffer.len; |
353 | |
314 | |
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 |
315 | do |
358 | { |
316 | { |
359 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
317 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
360 | } |
318 | } |
361 | while ((amt < 0) && (errno == EINTR)); |
319 | while ((amt < 0) && (errno == EINTR)); |
362 | #endif |
|
|
363 | |
320 | |
364 | if (amt < 0) |
321 | if (amt < 0) |
365 | { /* We got an error */ |
322 | { /* We got an error */ |
366 | |
323 | |
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) |
324 | if (errno != EWOULDBLOCK) |
373 | { |
325 | { |
374 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
326 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
375 | #endif |
|
|
376 | ns->status = Ns_Dead; |
327 | ns->status = Ns_Dead; |
377 | return; |
328 | return; |
378 | } |
329 | } |
379 | else |
330 | else |
380 | { /* EWOULDBLOCK */ |
331 | { /* EWOULDBLOCK */ |
… | |
… | |
424 | #endif |
375 | #endif |
425 | /* If we manage to write more than we wanted, take it as a bonus */ |
376 | /* If we manage to write more than we wanted, take it as a bonus */ |
426 | while (len > 0) |
377 | while (len > 0) |
427 | { |
378 | { |
428 | |
379 | |
429 | #ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */ |
|
|
430 | amt = send (ns->fd, pos, len, 0); |
|
|
431 | #else |
|
|
432 | do |
380 | do |
433 | { |
381 | { |
434 | amt = write (ns->fd, pos, len); |
382 | amt = write (ns->fd, pos, len); |
435 | } |
383 | } |
436 | while ((amt < 0) && (errno == EINTR)); |
384 | while ((amt < 0) && (errno == EINTR)); |
437 | #endif |
|
|
438 | |
385 | |
439 | if (amt < 0) |
386 | if (amt < 0) |
440 | { /* We got an error */ |
387 | { /* 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) |
388 | if (errno != EWOULDBLOCK) |
447 | { |
389 | { |
448 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
390 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
449 | errno, strerror (errno)); |
391 | errno, strerror (errno)); |
450 | #endif |
|
|
451 | ns->status = Ns_Dead; |
392 | ns->status = Ns_Dead; |
452 | return; |
393 | return; |
453 | } |
394 | } |
454 | else |
395 | else |
455 | { /* EWOULDBLOCK */ |
396 | { /* EWOULDBLOCK */ |
… | |
… | |
568 | cst_lst.obytes = 0; |
509 | cst_lst.obytes = 0; |
569 | cst_lst.max_conn = socket_info.nconns; |
510 | cst_lst.max_conn = socket_info.nconns; |
570 | cst_lst.time_start = now; |
511 | cst_lst.time_start = now; |
571 | } |
512 | } |
572 | #endif |
513 | #endif |
|
|
514 | |