Logo AND Algorithmique Numérique Distribuée

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