Logo AND Algorithmique Numérique Distribuée

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