Logo AND Algorithmique Numérique Distribuée

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