… | |
… | |
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; |
… | |
… | |
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 */ |