Logo AND Algorithmique Numérique Distribuée

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