… | |
… | |
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; |
118 | } |
106 | } |
119 | |
107 | |
120 | /* Basically does the reverse of SockList_AddInt, but on |
108 | /* Basically does the reverse of SockList_AddInt, but on |
121 | * strings instead. Same for the GetShort, but for 16 bits. |
109 | * strings instead. Same for the GetShort, but for 16 bits. |
122 | */ |
110 | */ |
… | |
… | |
157 | abort (); |
145 | abort (); |
158 | } |
146 | } |
159 | /* We already have a partial packet */ |
147 | /* We already have a partial packet */ |
160 | if (sl->len < 2) |
148 | if (sl->len < 2) |
161 | { |
149 | { |
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 |
150 | do |
168 | { |
151 | { |
169 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
152 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
170 | } |
153 | } |
171 | while ((stat == -1) && (errno == EINTR)); |
154 | while ((stat == -1) && (errno == EINTR)); |
172 | #endif |
155 | |
173 | if (stat < 0) |
156 | if (stat < 0) |
174 | { |
157 | { |
175 | /* In non blocking mode, EAGAIN is set when there is no |
158 | /* In non blocking mode, EAGAIN is set when there is no |
176 | * data available. |
159 | * data available. |
177 | */ |
160 | */ |
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) |
161 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
191 | { |
162 | { |
192 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
163 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
193 | } |
164 | } |
194 | #endif |
|
|
195 | return 0; /*Error */ |
165 | return 0; /*Error */ |
196 | } |
166 | } |
197 | if (stat == 0) |
167 | if (stat == 0) |
198 | return -1; |
168 | return -1; |
199 | sl->len += stat; |
169 | 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); |
183 | 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 |
184 | /* Quick hack in case for 'oldsocketmode' input. If we are |
215 | * closing the socket anyways, then reading this extra 100 bytes |
185 | * closing the socket anyways, then reading this extra 100 bytes |
216 | * shouldn't hurt. |
186 | * shouldn't hurt. |
217 | */ |
187 | */ |
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); |
188 | read (fd, sl->buf + 2, 100); |
222 | #endif /* end win32 */ |
|
|
223 | |
189 | |
224 | /* return error so the socket is closed */ |
190 | /* return error so the socket is closed */ |
225 | return -1; |
191 | return -1; |
226 | } |
192 | } |
227 | do |
193 | do |
228 | { |
194 | { |
229 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
|
|
230 | stat = recv (fd, sl->buf + sl->len, toread, 0); |
|
|
231 | #else |
|
|
232 | do |
195 | do |
233 | { |
196 | { |
234 | stat = read (fd, sl->buf + sl->len, toread); |
197 | stat = read (fd, sl->buf + sl->len, toread); |
235 | } |
198 | } |
236 | while ((stat < 0) && (errno == EINTR)); |
199 | while ((stat < 0) && (errno == EINTR)); |
237 | #endif |
|
|
238 | if (stat < 0) |
200 | if (stat < 0) |
239 | { |
201 | { |
240 | |
202 | |
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) |
203 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
254 | { |
204 | { |
255 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
205 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
256 | } |
206 | } |
257 | #endif |
|
|
258 | return 0; /*Error */ |
207 | return 0; /*Error */ |
259 | } |
208 | } |
260 | if (stat == 0) |
209 | if (stat == 0) |
261 | return -1; |
210 | return -1; |
262 | sl->len += stat; |
211 | sl->len += stat; |
… | |
… | |
349 | { |
298 | { |
350 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
299 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
351 | if (ns->outputbuffer.len < max) |
300 | if (ns->outputbuffer.len < max) |
352 | max = ns->outputbuffer.len; |
301 | max = ns->outputbuffer.len; |
353 | |
302 | |
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 |
303 | do |
358 | { |
304 | { |
359 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
305 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
360 | } |
306 | } |
361 | while ((amt < 0) && (errno == EINTR)); |
307 | while ((amt < 0) && (errno == EINTR)); |
362 | #endif |
|
|
363 | |
308 | |
364 | if (amt < 0) |
309 | if (amt < 0) |
365 | { /* We got an error */ |
310 | { /* We got an error */ |
366 | |
311 | |
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) |
312 | if (errno != EWOULDBLOCK) |
373 | { |
313 | { |
374 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
314 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
375 | #endif |
|
|
376 | ns->status = Ns_Dead; |
315 | ns->status = Ns_Dead; |
377 | return; |
316 | return; |
378 | } |
317 | } |
379 | else |
318 | else |
380 | { /* EWOULDBLOCK */ |
319 | { /* EWOULDBLOCK */ |
… | |
… | |
424 | #endif |
363 | #endif |
425 | /* If we manage to write more than we wanted, take it as a bonus */ |
364 | /* If we manage to write more than we wanted, take it as a bonus */ |
426 | while (len > 0) |
365 | while (len > 0) |
427 | { |
366 | { |
428 | |
367 | |
429 | #ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */ |
|
|
430 | amt = send (ns->fd, pos, len, 0); |
|
|
431 | #else |
|
|
432 | do |
368 | do |
433 | { |
369 | { |
434 | amt = write (ns->fd, pos, len); |
370 | amt = write (ns->fd, pos, len); |
435 | } |
371 | } |
436 | while ((amt < 0) && (errno == EINTR)); |
372 | while ((amt < 0) && (errno == EINTR)); |
437 | #endif |
|
|
438 | |
373 | |
439 | if (amt < 0) |
374 | if (amt < 0) |
440 | { /* We got an error */ |
375 | { /* 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) |
376 | if (errno != EWOULDBLOCK) |
447 | { |
377 | { |
448 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
378 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
449 | errno, strerror (errno)); |
379 | errno, strerror (errno)); |
450 | #endif |
|
|
451 | ns->status = Ns_Dead; |
380 | ns->status = Ns_Dead; |
452 | return; |
381 | return; |
453 | } |
382 | } |
454 | else |
383 | else |
455 | { /* EWOULDBLOCK */ |
384 | { /* EWOULDBLOCK */ |
… | |
… | |
568 | cst_lst.obytes = 0; |
497 | cst_lst.obytes = 0; |
569 | cst_lst.max_conn = socket_info.nconns; |
498 | cst_lst.max_conn = socket_info.nconns; |
570 | cst_lst.time_start = now; |
499 | cst_lst.time_start = now; |
571 | } |
500 | } |
572 | #endif |
501 | #endif |
|
|
502 | |