Logo AND Algorithmique Numérique Distribuée

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