Logo AND Algorithmique Numérique Distribuée

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