Logo AND Algorithmique Numérique Distribuée

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