Logo AND Algorithmique Numérique Distribuée

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