Logo AND Algorithmique Numérique Distribuée

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