Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix several glitches, add a 'proc' field to the sockets. I want to know the name...
[simgrid.git] / src / gras / Transport / transport_plugin_tcp.c
1 /* $Id$ */
2
3 /* buf trp (transport) - buffered transport using the TCP one               */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include <stdlib.h>
11 #include <string.h>       /* memset */
12
13 #include "portable.h"
14 #include "xbt/misc.h"
15 #include "xbt/sysdep.h"
16 #include "xbt/ex.h"
17 #include "transport_private.h"
18
19 #undef HAVE_READV
20
21 #ifdef HAVE_READV
22 #include <sys/uio.h>
23 #endif       
24
25 #ifndef MIN
26 #define MIN(a,b) ((a)<(b)?(a):(b))
27 #endif
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_tcp,transport,
30       "TCP buffered transport");
31
32 /***
33  *** Specific socket part
34  ***/
35
36 typedef enum { buffering_buf, buffering_iov } buffering_kind;
37
38 typedef struct {
39   int size;
40   char *data;
41   int pos; /* for receive; not exchanged over the net */
42 } gras_trp_buf_t;
43
44
45 struct gras_trp_bufdata_{
46   int buffsize;
47   gras_trp_buf_t in_buf;
48   gras_trp_buf_t out_buf;
49
50 #ifdef HAVE_READV
51   xbt_dynar_t in_buf_v;
52   xbt_dynar_t out_buf_v;
53 #endif
54
55   buffering_kind in;
56   buffering_kind out;
57 };
58
59
60 /*****************************/
61 /****[ SOCKET MANAGEMENT ]****/
62 /*****************************/
63 static int _gras_tcp_proto_number(void);
64
65 static inline void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
66                                                gras_socket_t sock){
67   
68   struct sockaddr_in addr;
69   struct hostent *he;
70   struct in_addr *haddr;
71   int size = sock->buf_size; 
72
73   sock->incoming = 1; /* TCP sockets are duplex'ed */
74
75   sock->sd = socket (AF_INET, SOCK_STREAM, 0);
76   
77   if (sock->sd < 0) {
78     THROW1(system_error,0, "Failed to create socket: %s", sock_errstr);
79   }
80
81   if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
82       setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
83      WARN1("setsockopt failed, cannot set buffer size: %s",sock_errstr);
84   }
85   
86   he = gethostbyname (sock->peer_name);
87   if (he == NULL) {
88     THROW2(system_error,0, "Failed to lookup hostname %s: %s",
89            sock->peer_name, sock_errstr);
90   }
91   
92   haddr = ((struct in_addr *) (he->h_addr_list)[0]);
93   
94   memset(&addr, 0, sizeof(struct sockaddr_in));
95   memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
96   addr.sin_family = AF_INET;
97   addr.sin_port = htons (sock->peer_port);
98
99   if (connect (sock->sd, (struct sockaddr*) &addr, sizeof (addr)) < 0) {
100     tcp_close(sock->sd);
101     THROW3(system_error,0,
102            "Failed to connect socket to %s:%d (%s)",
103            sock->peer_name, sock->peer_port, sock_errstr);
104   }
105   VERB4("Connect to %s:%d (sd=%d, port %d here)",
106         sock->peer_name, sock->peer_port, sock->sd, sock->port);
107 }
108
109 /**
110  * gras_trp_sock_socket_server:
111  *
112  * Open a socket used to receive messages.
113  */
114 static inline void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
115                                                gras_socket_t sock){
116   int size = sock->buf_size; 
117   int on = 1;
118   struct sockaddr_in server;
119
120   sock->outgoing  = 1; /* TCP => duplex mode */
121
122   server.sin_port = htons((u_short)sock->port);
123   server.sin_addr.s_addr = INADDR_ANY;
124   server.sin_family = AF_INET;
125   if((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
126     THROW1(system_error,0,"Socket allocation failed: %s", sock_errstr);
127
128   if (setsockopt(sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)))
129      THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
130             sock_errstr);
131    
132   if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
133       setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
134      WARN1("setsockopt failed, cannot set buffer size: %s",
135            sock_errstr);
136   }
137         
138   if (bind(sock->sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
139     tcp_close(sock->sd);
140     THROW2(system_error,0,"Cannot bind to port %d: %s",sock->port, sock_errstr);
141   }
142
143   DEBUG2("Listen on port %d (sd=%d)",sock->port, sock->sd);
144   if (listen(sock->sd, 5) < 0) {
145     tcp_close(sock->sd);
146     THROW2(system_error,0,"Cannot listen on port %d: %s",sock->port,sock_errstr);
147   }
148
149   VERB2("Openned a server socket on port %d (sd=%d)",sock->port,sock->sd);
150 }
151
152 static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) {
153   gras_socket_t res;
154   
155   struct sockaddr_in peer_in;
156   socklen_t peer_in_len = sizeof(peer_in);
157
158   int sd;
159   int tmp_errno;
160   int size;
161
162   int i = 1;
163   socklen_t s = sizeof(int);
164                 
165   XBT_IN;
166   gras_trp_socket_new(1,&res);
167
168   sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
169   tmp_errno = errno;
170
171   if (sd == -1) {
172     gras_socket_close(sock);
173     THROW1(system_error,0,
174            "Accept failed (%s). Droping server socket.", sock_errstr);
175   }
176   
177   if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s) 
178       || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *)&i, s))
179     THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
180            sock_errstr);
181
182   res->buf_size = sock->buf_size;
183   size = sock->buf_size;
184   if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size))
185       || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size)))
186     WARN1("setsockopt failed, cannot set buffer size: %s", sock_errstr);
187      
188   res->plugin    = sock->plugin;
189   res->incoming  = sock->incoming;
190   res->outgoing  = sock->outgoing;
191   res->accepting = 0;
192   res->sd        = sd;
193   res->port      = -1;
194   res->peer_port = peer_in.sin_port;
195
196   /* FIXME: Lock to protect inet_ntoa */
197   if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) {
198     res->peer_name = (char*)strdup("unknown");
199   } else {
200     struct in_addr addrAsInAddr;
201     char *tmp;
202     
203     addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
204     
205     tmp = inet_ntoa(addrAsInAddr);
206     if (tmp != NULL) {
207       res->peer_name = (char*)strdup(tmp);
208     } else {
209       res->peer_name = (char*)strdup("unknown");
210     }
211   }
212   
213   VERB3("Accepted from %s:%d (sd=%d)", res->peer_name,res->peer_port,sd);
214   
215   XBT_OUT;
216   return res;
217 }
218
219 static void gras_trp_sock_socket_close(gras_socket_t sock){
220   
221   if (!sock) return; /* close only once */
222
223   VERB1("close tcp connection %d", sock->sd);
224
225   /* FIXME: no pipe in GRAS so far  
226   if(!FD_ISSET(sd, &connectedPipes)) {
227     if(shutdown(sd, 2) < 0) {
228       GetNWSLock(&lock);
229       tmp_errno = errno;
230       ReleaseNWSLock(&lock);
231       
232       / * The other side may have beaten us to the reset. * /
233       if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) {
234         WARN1("CloseSocket: shutdown error %d\n", tmp_errno);
235       }
236     }
237   } */
238
239    
240   /* close the socket */
241   if(tcp_close(sock->sd) < 0) {
242     WARN3("error while closing tcp socket %d: %d (%s)\n", 
243              sock->sd, sock_errno, sock_errstr);
244   }
245
246 }
247 /************************************/
248 /****[ end of SOCKET MANAGEMENT ]****/
249 /************************************/
250
251
252 /************************************/
253 /****[ UNBUFFERED DATA EXCHANGE ]****/
254 /************************************/
255 /* Temptation to merge this with file data exchange is great, 
256    but doesn't work on BillWare (see tcp_write() in portable.h) */
257 static inline void gras_trp_tcp_send(gras_socket_t sock,
258                                      const char *data,
259                                      unsigned long int size) {
260   
261   while (size) {
262     int status = 0;
263     
264     status = tcp_write(sock->sd, data, (size_t)size);
265     DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
266     
267     if (status < 0) {
268       THROW4(system_error,0,"write(%d,%p,%ld) failed: %s",
269              sock->sd, data, size,
270              sock_errstr);
271     }
272     
273     if (status) {
274       size  -= status;
275       data  += status;
276     } else {
277       THROW1(system_error,0,"file descriptor closed (%s)",
278              sock_errstr);
279     }
280   }
281 }
282 static inline int 
283 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
284                                    char *data,
285                                    unsigned long int size,
286                                    unsigned long int bufsize) {
287
288   int got = 0;
289
290   while (size>got) {
291     int status = 0;
292     
293     DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
294           sock->sd, data+got, bufsize, got,
295           hexa_str((unsigned char*)data,got));
296     status = tcp_read(sock->sd, data+got, (size_t)bufsize);
297     
298     if (status < 0) {
299       THROW4(system_error,0,"read(%d,%p,%d) failed: %s",
300              sock->sd, data+got, (int)size,
301              sock_errstr);
302     }
303     DEBUG2("Got %d more bytes (%s)",status,hexa_str((unsigned char*)data+got,status));
304     
305     if (status) {
306       bufsize -= status;
307       got     += status;
308     } else {
309       THROW1(system_error,0,"Socket closed by remote side (got %d bytes before this)",
310              got);
311     }
312   }
313   return got;
314 }
315
316 static int gras_trp_tcp_recv(gras_socket_t sock,
317                                    char *data,
318                                    unsigned long int size) {
319   return gras_trp_tcp_recv_withbuffer(sock,data,size,size);
320
321 }
322 /*******************************************/
323 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
324 /*******************************************/
325
326 /**********************************/
327 /****[ BUFFERED DATA EXCHANGE ]****/
328 /**********************************/
329
330 /* Make sure the data is sent */
331 static void
332 gras_trp_bufiov_flush(gras_socket_t sock) {
333 #ifdef HAVE_READV
334   xbt_dynar_t vect;
335   int size;
336 #endif
337   gras_trp_bufdata_t *data=sock->bufdata;
338   XBT_IN;    
339   
340   DEBUG0("Flush");
341   if (data->out == buffering_buf) {
342     if (XBT_LOG_ISENABLED(trp_tcp,xbt_log_priority_debug))
343       hexa_print("chunk to send ",
344                  (unsigned char *) data->out_buf.data,data->out_buf.size);
345     if ((data->out_buf.size - data->out_buf.pos) != 0) { 
346       DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size,
347              gras_socket_peer_name(sock),gras_socket_peer_port(sock));
348       gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
349       VERB1("Chunk sent (size=%d)",data->out_buf.size);
350       data->out_buf.size = 0;
351     }
352   }
353
354 #ifdef HAVE_READV
355   if (data->out == buffering_iov) {
356     DEBUG0("Flush out iov");
357     vect = sock->bufdata->out_buf_v;
358     if ((size = xbt_dynar_length(vect))) {
359       DEBUG1("Flush %d chunks out of this socket",size);
360       writev(sock->sd,xbt_dynar_get_ptr(vect,0),size);
361       xbt_dynar_reset(vect);
362     }
363     data->out_buf.size = 0; /* reset the buffer containing non-stable data */
364   }
365
366   if (data->in == buffering_iov) {
367     DEBUG0("Flush in iov");
368     vect = sock->bufdata->in_buf_v;
369     if ((size = xbt_dynar_length(vect))) {
370       DEBUG1("Get %d chunks from of this socket",size);
371       readv(sock->sd,xbt_dynar_get_ptr(vect,0),size);
372       xbt_dynar_reset(vect);
373     }
374   }
375 #endif
376 }
377 static void
378 gras_trp_buf_send(gras_socket_t sock,
379                   const char *chunk,
380                   unsigned long int size,
381                   int stable_ignored) {
382
383   gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
384   int chunk_pos=0;
385
386   XBT_IN;
387
388   while (chunk_pos < size) {
389     /* size of the chunk to receive in that shot */
390     long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size);
391     DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
392            (int)data->out_buf.size,
393            ((int)data->out_buf.size) + thissize -1,
394            size,
395            hexa_str((unsigned char*)chunk,thissize));
396
397     memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
398
399     data->out_buf.size += thissize;
400     chunk_pos      += thissize;
401     DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
402            data->out_buf.size,size-chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos));
403
404     if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
405       gras_trp_bufiov_flush(sock);
406   }
407
408   XBT_OUT;
409 }
410
411 static int
412 gras_trp_buf_recv(gras_socket_t sock,
413                   char *chunk,
414                   unsigned long int size) {
415
416   gras_trp_bufdata_t *data=sock->bufdata;
417   long int chunk_pos = 0;
418    
419   XBT_IN;
420
421   while (chunk_pos < size) {
422     /* size of the chunk to receive in that shot */
423     long int thissize;
424
425     if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
426
427       DEBUG2("Get more data (size=%d,bufsize=%d)",
428              (int)MIN(size-chunk_pos,data->buffsize),
429              (int)data->buffsize);
430
431        
432       data->in_buf.size = 
433         gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data, 
434                                      MIN(size-chunk_pos,data->buffsize),
435                                      data->buffsize);
436        
437       data->in_buf.pos=0;
438     }
439      
440     thissize = min(size-chunk_pos ,  data->in_buf.size - data->in_buf.pos);
441     memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
442
443     data->in_buf.pos += thissize;
444     chunk_pos        += thissize;
445     DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
446            data->in_buf.pos,size - chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos));
447   }
448
449   XBT_OUT;
450   return chunk_pos;
451 }
452
453 /*****************************************/
454 /****[ end of BUFFERED DATA EXCHANGE ]****/
455 /*****************************************/
456
457 /********************************/
458 /****[ VECTOR DATA EXCHANGE ]****/
459 /********************************/
460 #ifdef HAVE_READV
461 static void
462 gras_trp_iov_send(gras_socket_t sock,
463                   const char *chunk,
464                   unsigned long int size,
465                   int stable) {
466   struct iovec elm;
467   gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
468     
469
470   DEBUG1("Buffer one chunk to be sent later (%s)",
471         hexa_str((char*)chunk,size));
472
473   elm.iov_len = (size_t)size;
474
475   if (!stable) {
476     /* data storage won't last until flush. Save it in a buffer if we can */
477
478     if (size > data->buffsize-data->out_buf.size) {
479       /* buffer too small: 
480          flush the socket, using data in its actual storage */
481       elm.iov_base = (void*)chunk;
482       xbt_dynar_push(data->out_buf_v,&elm);
483
484       gras_trp_bufiov_flush(sock);      
485       return;
486     } else {
487       /* buffer big enough: 
488          copy data into it, and chain it for upcoming writev */
489       memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
490       elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size);
491       data->out_buf.size += size;
492
493       xbt_dynar_push(data->out_buf_v,&elm);
494     }
495
496   } else {
497     /* data storage stable. Chain it */
498     
499     elm.iov_base = (void*)chunk;
500     xbt_dynar_push(data->out_buf_v,&elm);
501   }
502 }
503 static int
504 gras_trp_iov_recv(gras_socket_t sock,
505                   char *chunk,
506                   unsigned long int size) {
507   struct iovec elm;
508
509   DEBUG0("Buffer one chunk to be received later");
510   elm.iov_base = (void*)chunk;
511   elm.iov_len = (size_t)size;
512   xbt_dynar_push(sock->bufdata->in_buf_v,&elm);
513
514   return size;
515 }
516
517 #endif
518 /***************************************/
519 /****[ end of VECTOR DATA EXCHANGE ]****/
520 /***************************************/
521
522
523 /***
524  *** Prototypes of BUFFERED
525  ***/
526    
527 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
528                                 gras_socket_t sock);
529 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
530                                 gras_socket_t sock);
531 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
532
533 void         gras_trp_buf_socket_close(gras_socket_t sd);
534   
535
536 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
537   gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
538   
539   data->buffsize = 100 * 1024 ; /* 100k */ 
540
541   data->in_buf.size  = 0;
542   data->in_buf.data  = xbt_malloc(data->buffsize);
543   data->in_buf.pos   = 0; /* useless, indeed, since size==pos */
544    
545   data->out_buf.size = 0;
546   data->out_buf.data = xbt_malloc(data->buffsize);
547   data->out_buf.pos  = data->out_buf.size;
548
549 #ifdef HAVE_READV
550   data->in_buf_v = data->out_buf_v = NULL;
551   data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
552   data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
553   data->out = buffering_iov;
554 #else
555   data->out = buffering_buf;
556 #endif
557    
558   data->in = buffering_buf;
559
560   sock->bufdata = data;
561   return sock;
562 }
563
564 /***
565  *** Code
566  ***/
567 void
568 gras_trp_tcp_setup(gras_trp_plugin_t plug) {
569
570   plug->socket_client = gras_trp_buf_socket_client;
571   plug->socket_server = gras_trp_buf_socket_server;
572   plug->socket_accept = gras_trp_buf_socket_accept;
573   plug->socket_close  = gras_trp_buf_socket_close;
574
575 #ifdef HAVE_READV
576   plug->send = gras_trp_iov_send;
577 #else
578   plug->send = gras_trp_buf_send;
579 #endif
580   plug->recv = gras_trp_buf_recv;
581
582   plug->raw_send    = gras_trp_tcp_send;
583   plug->raw_recv    = gras_trp_tcp_recv;
584
585   plug->flush         = gras_trp_bufiov_flush;
586
587   plug->data = NULL;
588   plug->exit = NULL;
589 }
590
591 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
592                                 /* OUT */ gras_socket_t sock){
593
594   gras_trp_sock_socket_client(NULL,sock);
595   gras_trp_buf_init_sock(sock);
596 }
597
598 /**
599  * gras_trp_buf_socket_server:
600  *
601  * Open a socket used to receive messages.
602  */
603 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
604                                 /* OUT */ gras_socket_t sock){
605
606   gras_trp_sock_socket_server(NULL,sock);
607   gras_trp_buf_init_sock(sock);
608 }
609
610 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock) {
611   return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
612 }
613
614 void gras_trp_buf_socket_close(gras_socket_t sock){
615   gras_trp_bufdata_t *data=sock->bufdata;
616
617   if (data->in_buf.size!=data->in_buf.pos) {
618      WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
619            data->in_buf.size - data->in_buf.pos,
620            data->in_buf.size, data->in_buf.pos);
621   }
622   if (data->in_buf.data)
623     free(data->in_buf.data);
624    
625   if (data->out_buf.size!=data->out_buf.pos) {
626     DEBUG2("Flush the socket before closing (in=%d,out=%d)",
627            data->in_buf.size, data->out_buf.size);
628     gras_trp_bufiov_flush(sock);
629   }   
630   if (data->out_buf.data)
631     free(data->out_buf.data);
632
633 #ifdef HAVE_READV
634   if (data->in_buf_v) {
635     if (xbt_dynar_length(data->in_buf_v)) 
636       WARN0("Socket closed, but some bytes were unread");
637     xbt_dynar_free(&data->in_buf_v);
638   }
639   if (data->out_buf_v) {
640     if (xbt_dynar_length(data->out_buf_v)) {
641       DEBUG0("Flush the socket before closing");
642       gras_trp_bufiov_flush(sock);
643     }
644     xbt_dynar_free(&data->out_buf_v);
645   }
646 #endif
647
648   free(data);
649   gras_trp_sock_socket_close(sock);
650 }
651
652 /****************************/
653 /****[ HELPER FUNCTIONS ]****/
654 /****************************/
655
656 /*
657  * Returns the tcp protocol number from the network protocol data base.
658  *
659  * getprotobyname() is not thread safe. We need to lock it.
660  */
661 static int _gras_tcp_proto_number(void) {
662   struct protoent *fetchedEntry;
663   static int returnValue = 0;
664   
665   if(returnValue == 0) {
666     fetchedEntry = getprotobyname("tcp");
667     xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
668     returnValue = fetchedEntry->p_proto;
669   }
670   
671   return returnValue;
672 }
673
674 #ifdef HAVE_WINSOCK_H
675 #define RETSTR( x ) case x: return #x
676
677 const char *gras_wsa_err2string( int err ) {
678    switch( err ) {
679       RETSTR( WSAEINTR );
680       RETSTR( WSAEBADF );
681       RETSTR( WSAEACCES );
682       RETSTR( WSAEFAULT );
683       RETSTR( WSAEINVAL );
684       RETSTR( WSAEMFILE );
685       RETSTR( WSAEWOULDBLOCK );
686       RETSTR( WSAEINPROGRESS );
687       RETSTR( WSAEALREADY );
688       RETSTR( WSAENOTSOCK );
689       RETSTR( WSAEDESTADDRREQ );
690       RETSTR( WSAEMSGSIZE );
691       RETSTR( WSAEPROTOTYPE );
692       RETSTR( WSAENOPROTOOPT );
693       RETSTR( WSAEPROTONOSUPPORT );
694       RETSTR( WSAESOCKTNOSUPPORT );
695       RETSTR( WSAEOPNOTSUPP );
696       RETSTR( WSAEPFNOSUPPORT );
697       RETSTR( WSAEAFNOSUPPORT );
698       RETSTR( WSAEADDRINUSE );
699       RETSTR( WSAEADDRNOTAVAIL );
700       RETSTR( WSAENETDOWN );
701       RETSTR( WSAENETUNREACH );
702       RETSTR( WSAENETRESET );
703       RETSTR( WSAECONNABORTED );
704       RETSTR( WSAECONNRESET );
705       RETSTR( WSAENOBUFS );
706       RETSTR( WSAEISCONN );
707       RETSTR( WSAENOTCONN );
708       RETSTR( WSAESHUTDOWN );
709       RETSTR( WSAETOOMANYREFS );
710       RETSTR( WSAETIMEDOUT );
711       RETSTR( WSAECONNREFUSED );
712       RETSTR( WSAELOOP );
713       RETSTR( WSAENAMETOOLONG );
714       RETSTR( WSAEHOSTDOWN );
715       RETSTR( WSAEHOSTUNREACH );
716       RETSTR( WSAENOTEMPTY );
717       RETSTR( WSAEPROCLIM );
718       RETSTR( WSAEUSERS );
719       RETSTR( WSAEDQUOT );
720       RETSTR( WSAESTALE );
721       RETSTR( WSAEREMOTE );
722       RETSTR( WSASYSNOTREADY );
723       RETSTR( WSAVERNOTSUPPORTED );
724       RETSTR( WSANOTINITIALISED );
725       RETSTR( WSAEDISCON );
726       
727 #ifdef HAVE_WINSOCK2
728       RETSTR( WSAENOMORE );
729       RETSTR( WSAECANCELLED );
730       RETSTR( WSAEINVALIDPROCTABLE );
731       RETSTR( WSAEINVALIDPROVIDER );
732       RETSTR( WSASYSCALLFAILURE );
733       RETSTR( WSASERVICE_NOT_FOUND );
734       RETSTR( WSATYPE_NOT_FOUND );
735       RETSTR( WSA_E_NO_MORE );
736       RETSTR( WSA_E_CANCELLED );
737       RETSTR( WSAEREFUSED );
738 #endif /* HAVE_WINSOCK2 */
739
740       RETSTR( WSAHOST_NOT_FOUND );
741       RETSTR( WSATRY_AGAIN );
742       RETSTR( WSANO_RECOVERY );
743       RETSTR( WSANO_DATA );
744    }
745    return "unknown WSA error";
746 }
747 #endif /* HAVE_WINSOCK_H */
748
749 /***********************************/
750 /****[ end of HELPER FUNCTIONS ]****/
751 /***********************************/