Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
06609af3a989cb0fcfea73b8674e8cb88d7b1aed
[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->bufSize * 1024; 
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->bufSize * 1024; 
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->bufSize = sock->bufSize;
183   size = sock->bufSize * 1024;
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       THROW0(system_error,0,"Socket closed by remote side");
310     }
311   }
312   return got;
313 }
314
315 static int gras_trp_tcp_recv(gras_socket_t sock,
316                                    char *data,
317                                    unsigned long int size) {
318   return gras_trp_tcp_recv_withbuffer(sock,data,size,size);
319
320 }
321 /*******************************************/
322 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
323 /*******************************************/
324
325 /**********************************/
326 /****[ BUFFERED DATA EXCHANGE ]****/
327 /**********************************/
328
329 /* Make sure the data is sent */
330 static void
331 gras_trp_bufiov_flush(gras_socket_t sock) {
332 #ifdef HAVE_READV
333   xbt_dynar_t vect;
334   int size;
335 #endif
336   gras_trp_bufdata_t *data=sock->bufdata;
337   XBT_IN;    
338   
339   DEBUG0("Flush");
340   if (data->out == buffering_buf) {
341     if (XBT_LOG_ISENABLED(trp_tcp,xbt_log_priority_debug))
342       hexa_print("chunk to send ",
343                  (unsigned char *) data->out_buf.data,data->out_buf.size);
344     if ((data->out_buf.size - data->out_buf.pos) != 0) { 
345       DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size,
346              gras_socket_peer_name(sock),gras_socket_peer_port(sock));
347       gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
348       VERB1("Chunk sent (size=%d)",data->out_buf.size);
349       data->out_buf.size = 0;
350     }
351   }
352
353 #ifdef HAVE_READV
354   if (data->out == buffering_iov) {
355     DEBUG0("Flush out iov");
356     vect = sock->bufdata->out_buf_v;
357     if ((size = xbt_dynar_length(vect))) {
358       DEBUG1("Flush %d chunks out of this socket",size);
359       writev(sock->sd,xbt_dynar_get_ptr(vect,0),size);
360       xbt_dynar_reset(vect);
361     }
362     data->out_buf.size = 0; /* reset the buffer containing non-stable data */
363   }
364
365   if (data->in == buffering_iov) {
366     DEBUG0("Flush in iov");
367     vect = sock->bufdata->in_buf_v;
368     if ((size = xbt_dynar_length(vect))) {
369       DEBUG1("Get %d chunks from of this socket",size);
370       readv(sock->sd,xbt_dynar_get_ptr(vect,0),size);
371       xbt_dynar_reset(vect);
372     }
373   }
374 #endif
375 }
376 static void
377 gras_trp_buf_send(gras_socket_t sock,
378                   const char *chunk,
379                   unsigned long int size,
380                   int stable_ignored) {
381
382   gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
383   int chunk_pos=0;
384
385   XBT_IN;
386
387   while (chunk_pos < size) {
388     /* size of the chunk to receive in that shot */
389     long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size);
390     DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
391            (int)data->out_buf.size,
392            ((int)data->out_buf.size) + thissize -1,
393            size,
394            hexa_str((unsigned char*)chunk,thissize));
395
396     memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
397
398     data->out_buf.size += thissize;
399     chunk_pos      += thissize;
400     DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
401            data->out_buf.size,size-chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos));
402
403     if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
404       gras_trp_bufiov_flush(sock);
405   }
406
407   XBT_OUT;
408 }
409
410 static int
411 gras_trp_buf_recv(gras_socket_t sock,
412                   char *chunk,
413                   unsigned long int size) {
414
415   gras_trp_bufdata_t *data=sock->bufdata;
416   long int chunk_pos = 0;
417    
418   XBT_IN;
419
420   while (chunk_pos < size) {
421     /* size of the chunk to receive in that shot */
422     long int thissize;
423
424     if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
425
426       DEBUG2("Get more data (size=%d,bufsize=%d)",
427              (int)MIN(size-chunk_pos,data->buffsize),
428              (int)data->buffsize);
429
430        
431       data->in_buf.size = 
432         gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data, 
433                                      MIN(size-chunk_pos,data->buffsize),
434                                      data->buffsize);
435        
436       data->in_buf.pos=0;
437     }
438      
439     thissize = min(size-chunk_pos ,  data->in_buf.size - data->in_buf.pos);
440     memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
441
442     data->in_buf.pos += thissize;
443     chunk_pos        += thissize;
444     DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
445            data->in_buf.pos,size - chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos));
446   }
447
448   XBT_OUT;
449   return chunk_pos;
450 }
451
452 /*****************************************/
453 /****[ end of BUFFERED DATA EXCHANGE ]****/
454 /*****************************************/
455
456 /********************************/
457 /****[ VECTOR DATA EXCHANGE ]****/
458 /********************************/
459 #ifdef HAVE_READV
460 static void
461 gras_trp_iov_send(gras_socket_t sock,
462                   const char *chunk,
463                   unsigned long int size,
464                   int stable) {
465   struct iovec elm;
466   gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
467     
468
469   DEBUG1("Buffer one chunk to be sent later (%s)",
470         hexa_str((char*)chunk,size));
471
472   elm.iov_len = (size_t)size;
473
474   if (!stable) {
475     /* data storage won't last until flush. Save it in a buffer if we can */
476
477     if (size > data->buffsize-data->out_buf.size) {
478       /* buffer too small: 
479          flush the socket, using data in its actual storage */
480       elm.iov_base = (void*)chunk;
481       xbt_dynar_push(data->out_buf_v,&elm);
482
483       gras_trp_bufiov_flush(sock);      
484       return;
485     } else {
486       /* buffer big enough: 
487          copy data into it, and chain it for upcoming writev */
488       memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
489       elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size);
490       data->out_buf.size += size;
491
492       xbt_dynar_push(data->out_buf_v,&elm);
493     }
494
495   } else {
496     /* data storage stable. Chain it */
497     
498     elm.iov_base = (void*)chunk;
499     xbt_dynar_push(data->out_buf_v,&elm);
500   }
501 }
502 static int
503 gras_trp_iov_recv(gras_socket_t sock,
504                   char *chunk,
505                   unsigned long int size) {
506   struct iovec elm;
507
508   DEBUG0("Buffer one chunk to be received later");
509   elm.iov_base = (void*)chunk;
510   elm.iov_len = (size_t)size;
511   xbt_dynar_push(sock->bufdata->in_buf_v,&elm);
512
513   return size;
514 }
515
516 #endif
517 /***************************************/
518 /****[ end of VECTOR DATA EXCHANGE ]****/
519 /***************************************/
520
521
522 /***
523  *** Prototypes of BUFFERED
524  ***/
525    
526 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
527                                 gras_socket_t sock);
528 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
529                                 gras_socket_t sock);
530 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
531
532 void         gras_trp_buf_socket_close(gras_socket_t sd);
533   
534
535 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
536   gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
537   
538   data->buffsize = 100 * 1024 ; /* 100k */ 
539
540   data->in_buf.size  = 0;
541   data->in_buf.data  = xbt_malloc(data->buffsize);
542   data->in_buf.pos   = 0; /* useless, indeed, since size==pos */
543    
544   data->out_buf.size = 0;
545   data->out_buf.data = xbt_malloc(data->buffsize);
546   data->out_buf.pos  = data->out_buf.size;
547
548 #ifdef HAVE_READV
549   data->in_buf_v = data->out_buf_v = NULL;
550   data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
551   data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
552   data->out = buffering_iov;
553 #else
554   data->out = buffering_buf;
555 #endif
556    
557   data->in = buffering_buf;
558
559   sock->bufdata = data;
560   return sock;
561 }
562
563 /***
564  *** Code
565  ***/
566 void
567 gras_trp_tcp_setup(gras_trp_plugin_t plug) {
568
569   plug->socket_client = gras_trp_buf_socket_client;
570   plug->socket_server = gras_trp_buf_socket_server;
571   plug->socket_accept = gras_trp_buf_socket_accept;
572   plug->socket_close  = gras_trp_buf_socket_close;
573
574 #ifdef HAVE_READV
575   plug->send = gras_trp_iov_send;
576 #else
577   plug->send = gras_trp_buf_send;
578 #endif
579   plug->recv = gras_trp_buf_recv;
580
581   plug->raw_send    = gras_trp_tcp_send;
582   plug->raw_recv    = gras_trp_tcp_recv;
583
584   plug->flush         = gras_trp_bufiov_flush;
585
586   plug->data = NULL;
587   plug->exit = NULL;
588 }
589
590 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
591                                 /* OUT */ gras_socket_t sock){
592
593   gras_trp_sock_socket_client(NULL,sock);
594   gras_trp_buf_init_sock(sock);
595 }
596
597 /**
598  * gras_trp_buf_socket_server:
599  *
600  * Open a socket used to receive messages.
601  */
602 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
603                                 /* OUT */ gras_socket_t sock){
604
605   gras_trp_sock_socket_server(NULL,sock);
606   gras_trp_buf_init_sock(sock);
607 }
608
609 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock) {
610   return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
611 }
612
613 void gras_trp_buf_socket_close(gras_socket_t sock){
614   gras_trp_bufdata_t *data=sock->bufdata;
615
616   if (data->in_buf.size!=data->in_buf.pos) {
617      WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
618            data->in_buf.size - data->in_buf.pos,
619            data->in_buf.size, data->in_buf.pos);
620   }
621   if (data->in_buf.data)
622     free(data->in_buf.data);
623    
624   if (data->out_buf.size!=data->out_buf.pos) {
625     DEBUG2("Flush the socket before closing (in=%d,out=%d)",
626            data->in_buf.size, data->out_buf.size);
627     gras_trp_bufiov_flush(sock);
628   }   
629   if (data->out_buf.data)
630     free(data->out_buf.data);
631
632 #ifdef HAVE_READV
633   if (data->in_buf_v) {
634     if (xbt_dynar_length(data->in_buf_v)) 
635       WARN0("Socket closed, but some bytes were unread");
636     xbt_dynar_free(&data->in_buf_v);
637   }
638   if (data->out_buf_v) {
639     if (xbt_dynar_length(data->out_buf_v)) {
640       DEBUG0("Flush the socket before closing");
641       gras_trp_bufiov_flush(sock);
642     }
643     xbt_dynar_free(&data->out_buf_v);
644   }
645 #endif
646
647   free(data);
648   gras_trp_sock_socket_close(sock);
649 }
650
651 /****************************/
652 /****[ HELPER FUNCTIONS ]****/
653 /****************************/
654
655 /*
656  * Returns the tcp protocol number from the network protocol data base.
657  *
658  * getprotobyname() is not thread safe. We need to lock it.
659  */
660 static int _gras_tcp_proto_number(void) {
661   struct protoent *fetchedEntry;
662   static int returnValue = 0;
663   
664   if(returnValue == 0) {
665     fetchedEntry = getprotobyname("tcp");
666     xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
667     returnValue = fetchedEntry->p_proto;
668   }
669   
670   return returnValue;
671 }
672
673 #ifdef HAVE_WINSOCK_H
674 #define RETSTR( x ) case x: return #x
675
676 const char *gras_wsa_err2string( int err ) {
677    switch( err ) {
678       RETSTR( WSAEINTR );
679       RETSTR( WSAEBADF );
680       RETSTR( WSAEACCES );
681       RETSTR( WSAEFAULT );
682       RETSTR( WSAEINVAL );
683       RETSTR( WSAEMFILE );
684       RETSTR( WSAEWOULDBLOCK );
685       RETSTR( WSAEINPROGRESS );
686       RETSTR( WSAEALREADY );
687       RETSTR( WSAENOTSOCK );
688       RETSTR( WSAEDESTADDRREQ );
689       RETSTR( WSAEMSGSIZE );
690       RETSTR( WSAEPROTOTYPE );
691       RETSTR( WSAENOPROTOOPT );
692       RETSTR( WSAEPROTONOSUPPORT );
693       RETSTR( WSAESOCKTNOSUPPORT );
694       RETSTR( WSAEOPNOTSUPP );
695       RETSTR( WSAEPFNOSUPPORT );
696       RETSTR( WSAEAFNOSUPPORT );
697       RETSTR( WSAEADDRINUSE );
698       RETSTR( WSAEADDRNOTAVAIL );
699       RETSTR( WSAENETDOWN );
700       RETSTR( WSAENETUNREACH );
701       RETSTR( WSAENETRESET );
702       RETSTR( WSAECONNABORTED );
703       RETSTR( WSAECONNRESET );
704       RETSTR( WSAENOBUFS );
705       RETSTR( WSAEISCONN );
706       RETSTR( WSAENOTCONN );
707       RETSTR( WSAESHUTDOWN );
708       RETSTR( WSAETOOMANYREFS );
709       RETSTR( WSAETIMEDOUT );
710       RETSTR( WSAECONNREFUSED );
711       RETSTR( WSAELOOP );
712       RETSTR( WSAENAMETOOLONG );
713       RETSTR( WSAEHOSTDOWN );
714       RETSTR( WSAEHOSTUNREACH );
715       RETSTR( WSAENOTEMPTY );
716       RETSTR( WSAEPROCLIM );
717       RETSTR( WSAEUSERS );
718       RETSTR( WSAEDQUOT );
719       RETSTR( WSAESTALE );
720       RETSTR( WSAEREMOTE );
721       RETSTR( WSASYSNOTREADY );
722       RETSTR( WSAVERNOTSUPPORTED );
723       RETSTR( WSANOTINITIALISED );
724       RETSTR( WSAEDISCON );
725       
726 #ifdef HAVE_WINSOCK2
727       RETSTR( WSAENOMORE );
728       RETSTR( WSAECANCELLED );
729       RETSTR( WSAEINVALIDPROCTABLE );
730       RETSTR( WSAEINVALIDPROVIDER );
731       RETSTR( WSASYSCALLFAILURE );
732       RETSTR( WSASERVICE_NOT_FOUND );
733       RETSTR( WSATYPE_NOT_FOUND );
734       RETSTR( WSA_E_NO_MORE );
735       RETSTR( WSA_E_CANCELLED );
736       RETSTR( WSAEREFUSED );
737 #endif /* HAVE_WINSOCK2 */
738
739       RETSTR( WSAHOST_NOT_FOUND );
740       RETSTR( WSATRY_AGAIN );
741       RETSTR( WSANO_RECOVERY );
742       RETSTR( WSANO_DATA );
743    }
744    return "unknown WSA error";
745 }
746 #endif /* HAVE_WINSOCK_H */
747
748 /***********************************/
749 /****[ end of HELPER FUNCTIONS ]****/
750 /***********************************/