Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge branches
[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   if (((gras_trp_tcp_sock_data_t)sock->data)->peer_name)
307     free(((gras_trp_tcp_sock_data_t)sock->data)->peer_name);
308   free(sock->data);
309
310   XBT_VERB("close tcp connection %d", sock->sd);
311
312   /* ask the listener to close the socket */
313   gras_msg_listener_close_socket(sock->sd);
314 }
315
316 /************************************/
317 /****[ end of SOCKET MANAGEMENT ]****/
318 /************************************/
319
320
321 /************************************/
322 /****[ UNBUFFERED DATA EXCHANGE ]****/
323 /************************************/
324 /* Temptation to merge this with file data exchange is great, 
325    but doesn't work on BillWare (see tcp_write() in portable.h) */
326 static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock,
327                                          const char *data,
328                                          unsigned long int size)
329 {
330
331   while (size) {
332     int status = 0;
333
334     status = tcp_write(sock->sd, data, (size_t) size);
335     XBT_DEBUG("write(%d, %p, %ld);", sock->sd, data, size);
336
337     if (status < 0) {
338 #ifdef EWOULDBLOCK
339       if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
340 #else
341       if (errno == EINTR || errno == EAGAIN)
342 #endif
343         continue;
344
345       THROWF(system_error, 0, "write(%d,%p,%ld) failed: %s",
346              sock->sd, data, size, sock_errstr(sock_errno));
347     }
348
349     if (status) {
350       size -= status;
351       data += status;
352     } else {
353       THROWF(system_error, 0, "file descriptor closed (%s)",
354              sock_errstr(sock_errno));
355     }
356   }
357 }
358
359 static XBT_INLINE int
360 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
361                              char *data,
362                              unsigned long int size,
363                              unsigned long int bufsize)
364 {
365
366   int got = 0;
367
368   if (sock->recvd) {
369     data[0] = sock->recvd_val;
370     sock->recvd = 0;
371     got++;
372     bufsize--;
373   }
374
375   while (size > got) {
376     int status = 0;
377
378     XBT_DEBUG("read(%d, %p, %ld) got %d so far (%s)",
379            sock->sd, data + got, bufsize, got,
380            hexa_str((unsigned char *) data, got, 0));
381     status = tcp_read(sock->sd, data + got, (size_t) bufsize);
382
383     if (status < 0) {
384       THROWF(system_error, 0,
385              "read(%d,%p,%d) from %s:%d failed: %s; got %d so far",
386              sock->sd, data + got, (int) size, gras_socket_peer_name(sock),
387              gras_socket_peer_port(sock), sock_errstr(sock_errno), got);
388     }
389     XBT_DEBUG("Got %d more bytes (%s)", status,
390            hexa_str((unsigned char *) data + got, status, 0));
391
392     if (status) {
393       bufsize -= status;
394       got += status;
395     } else {
396       THROWF(system_error, errno,
397              "Socket closed by remote side (got %d bytes before this)",
398              got);
399     }
400   }
401
402   return got;
403 }
404
405 static int gras_trp_tcp_recv(gras_socket_t sock,
406                              char *data, unsigned long int size)
407 {
408   return gras_trp_tcp_recv_withbuffer(sock, data, size, size);
409
410 }
411
412 /*******************************************/
413 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
414 /*******************************************/
415
416 /**********************************/
417 /****[ BUFFERED DATA EXCHANGE ]****/
418 /**********************************/
419
420 /* Make sure the data is sent */
421 static void gras_trp_bufiov_flush(gras_socket_t sock)
422 {
423 #ifdef HAVE_READV
424   xbt_dynar_t vect;
425   int size;
426 #endif
427   gras_trp_bufdata_t *data = sock->bufdata;
428   XBT_IN("");
429
430   XBT_DEBUG("Flush");
431   if (data->out == buffering_buf) {
432     if (XBT_LOG_ISENABLED(gras_trp_tcp, xbt_log_priority_debug))
433       hexa_print("chunk to send ",
434                  (unsigned char *) data->out_buf.data, data->out_buf.size);
435     if ((data->out_buf.size - data->out_buf.pos) != 0) {
436       XBT_DEBUG("Send the chunk (size=%d) to %s:%d", data->out_buf.size,
437              gras_socket_peer_name(sock), gras_socket_peer_port(sock));
438       gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
439       XBT_VERB("Chunk sent (size=%d)", data->out_buf.size);
440       data->out_buf.size = 0;
441     }
442   }
443 #ifdef HAVE_READV
444   if (data->out == buffering_iov) {
445     XBT_DEBUG("Flush out iov");
446     vect = sock->bufdata->out_buf_v;
447     if ((size = xbt_dynar_length(vect))) {
448       XBT_DEBUG("Flush %d chunks out of this socket", size);
449       writev(sock->sd, xbt_dynar_get_ptr(vect, 0), size);
450       xbt_dynar_reset(vect);
451     }
452     data->out_buf.size = 0;     /* reset the buffer containing non-stable data */
453   }
454
455   if (data->in == buffering_iov) {
456     XBT_DEBUG("Flush in iov");
457     vect = sock->bufdata->in_buf_v;
458     if ((size = xbt_dynar_length(vect))) {
459       XBT_DEBUG("Get %d chunks from of this socket", size);
460       readv(sock->sd, xbt_dynar_get_ptr(vect, 0), size);
461       xbt_dynar_reset(vect);
462     }
463   }
464 #endif
465 }
466
467 static void
468 gras_trp_buf_send(gras_socket_t sock,
469                   const char *chunk,
470                   unsigned long int size, int stable_ignored)
471 {
472
473   gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata;
474   int chunk_pos = 0;
475
476   XBT_IN("");
477
478   while (chunk_pos < size) {
479     /* size of the chunk to receive in that shot */
480     long int thissize =
481         min(size - chunk_pos, data->buffsize - data->out_buf.size);
482     XBT_DEBUG("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
483            (int) data->out_buf.size,
484            ((int) data->out_buf.size) + thissize - 1, size,
485            hexa_str((unsigned char *) chunk, thissize, 0));
486
487     memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos,
488            thissize);
489
490     data->out_buf.size += thissize;
491     chunk_pos += thissize;
492     XBT_DEBUG("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
493            data->out_buf.size, size - chunk_pos, size,
494            hexa_str((unsigned char *) chunk, chunk_pos, 0));
495
496     if (data->out_buf.size == data->buffsize)   /* out of space. Flush it */
497       gras_trp_bufiov_flush(sock);
498   }
499
500   XBT_OUT();
501 }
502
503 static int
504 gras_trp_buf_recv(gras_socket_t sock, char *chunk, unsigned long int size)
505 {
506
507   gras_trp_bufdata_t *data = sock->bufdata;
508   long int chunk_pos = 0;
509
510   XBT_IN("");
511
512   while (chunk_pos < size) {
513     /* size of the chunk to receive in that shot */
514     long int thissize;
515
516     if (data->in_buf.size == data->in_buf.pos) {        /* out of data. Get more */
517
518       XBT_DEBUG("Get more data (size=%d,bufsize=%d)",
519              (int) MIN(size - chunk_pos, data->buffsize),
520              (int) data->buffsize);
521
522
523       data->in_buf.size =
524           gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
525                                        MIN(size - chunk_pos,
526                                            data->buffsize),
527                                        data->buffsize);
528
529       data->in_buf.pos = 0;
530     }
531
532     thissize = min(size - chunk_pos, data->in_buf.size - data->in_buf.pos);
533     memcpy(chunk + chunk_pos, data->in_buf.data + data->in_buf.pos,
534            thissize);
535
536     data->in_buf.pos += thissize;
537     chunk_pos += thissize;
538     XBT_DEBUG("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
539            data->in_buf.pos, size - chunk_pos, size,
540            hexa_str((unsigned char *) chunk, chunk_pos, 0));
541   }
542   /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */
543   sock->moredata = (data->in_buf.size > data->in_buf.pos);
544   XBT_DEBUG("There is %smore data", (sock->moredata ? "" : "no "));
545
546   XBT_OUT();
547   return chunk_pos;
548 }
549
550 /*****************************************/
551 /****[ end of BUFFERED DATA EXCHANGE ]****/
552 /*****************************************/
553
554 /********************************/
555 /****[ VECTOR DATA EXCHANGE ]****/
556 /********************************/
557 #ifdef HAVE_READV
558 static void
559 gras_trp_iov_send(gras_socket_t sock,
560                   const char *chunk, unsigned long int size, int stable)
561 {
562   struct iovec elm;
563   gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata;
564
565
566   XBT_DEBUG("Buffer one chunk to be sent later (%s)",
567          hexa_str((char *) chunk, size, 0));
568
569   elm.iov_len = (size_t) size;
570
571   if (!stable) {
572     /* data storage won't last until flush. Save it in a buffer if we can */
573
574     if (size > data->buffsize - data->out_buf.size) {
575       /* buffer too small: 
576          flush the socket, using data in its actual storage */
577       elm.iov_base = (void *) chunk;
578       xbt_dynar_push(data->out_buf_v, &elm);
579
580       gras_trp_bufiov_flush(sock);
581       return;
582     } else {
583       /* buffer big enough: 
584          copy data into it, and chain it for upcoming writev */
585       memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
586       elm.iov_base = (void *) (data->out_buf.data + data->out_buf.size);
587       data->out_buf.size += size;
588
589       xbt_dynar_push(data->out_buf_v, &elm);
590     }
591
592   } else {
593     /* data storage stable. Chain it */
594
595     elm.iov_base = (void *) chunk;
596     xbt_dynar_push(data->out_buf_v, &elm);
597   }
598 }
599
600 static int
601 gras_trp_iov_recv(gras_socket_t sock, char *chunk, unsigned long int size)
602 {
603   struct iovec elm;
604
605   XBT_DEBUG("Buffer one chunk to be received later");
606   elm.iov_base = (void *) chunk;
607   elm.iov_len = (size_t) size;
608   xbt_dynar_push(sock->bufdata->in_buf_v, &elm);
609
610   return size;
611 }
612
613 #endif
614 /***************************************/
615 /****[ end of VECTOR DATA EXCHANGE ]****/
616 /***************************************/
617
618
619 /***
620  *** Prototypes of BUFFERED
621  ***/
622
623 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
624     const char *host,
625     int port,
626                                 gras_socket_t sock);
627 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
628                                 int port,
629                                 gras_socket_t sock);
630 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
631
632 void gras_trp_buf_socket_close(gras_socket_t sd);
633
634
635 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock)
636 {
637   gras_trp_bufdata_t *data = xbt_new(gras_trp_bufdata_t, 1);
638
639   data->buffsize = 100 * 1024;  /* 100k */
640
641   data->in_buf.size = 0;
642   data->in_buf.data = xbt_malloc(data->buffsize);
643   data->in_buf.pos = 0;         /* useless, indeed, since size==pos */
644
645   data->out_buf.size = 0;
646   data->out_buf.data = xbt_malloc(data->buffsize);
647   data->out_buf.pos = data->out_buf.size;
648
649 #ifdef HAVE_READV
650   data->in_buf_v = data->out_buf_v = NULL;
651   data->in_buf_v = xbt_dynar_new(sizeof(struct iovec), NULL);
652   data->out_buf_v = xbt_dynar_new(sizeof(struct iovec), NULL);
653   data->out = buffering_iov;
654 #else
655   data->out = buffering_buf;
656 #endif
657
658   data->in = buffering_buf;
659
660   sock->bufdata = data;
661   return sock;
662 }
663
664 /***
665  *** Info about who's speaking
666  ***/
667 static int gras_trp_tcp_my_port(gras_socket_t s) {
668   gras_trp_tcp_sock_data_t sockdata = s->data;
669   return sockdata->port;
670 }
671 static int gras_trp_tcp_peer_port(gras_socket_t s) {
672   gras_trp_tcp_sock_data_t sockdata = s->data;
673   return sockdata->peer_port;
674 }
675 static const char* gras_trp_tcp_peer_name(gras_socket_t s) {
676   gras_trp_tcp_sock_data_t sockdata = s->data;
677   return sockdata->peer_name;
678 }
679 static const char* gras_trp_tcp_peer_proc(gras_socket_t s) {
680   gras_trp_tcp_sock_data_t sockdata = s->data;
681   return sockdata->peer_proc;
682 }
683 static void gras_trp_tcp_peer_proc_set(gras_socket_t s,char *name) {
684   gras_trp_tcp_sock_data_t sockdata = s->data;
685   sockdata->peer_proc = xbt_strdup(name);
686 }
687
688 /***
689  *** Code
690  ***/
691 void gras_trp_tcp_setup(gras_trp_plugin_t plug)
692 {
693
694   plug->my_port = gras_trp_tcp_my_port;
695   plug->peer_port = gras_trp_tcp_peer_port;
696   plug->peer_name = gras_trp_tcp_peer_name;
697   plug->peer_proc = gras_trp_tcp_peer_proc;
698   plug->peer_proc_set = gras_trp_tcp_peer_proc_set;
699
700
701   plug->socket_client = gras_trp_buf_socket_client;
702   plug->socket_server = gras_trp_buf_socket_server;
703   plug->socket_accept = gras_trp_buf_socket_accept;
704   plug->socket_close = gras_trp_buf_socket_close;
705
706 #ifdef HAVE_READV
707   plug->send = gras_trp_iov_send;
708 #else
709   plug->send = gras_trp_buf_send;
710 #endif
711   plug->recv = gras_trp_buf_recv;
712
713   plug->raw_send = gras_trp_tcp_send;
714   plug->raw_recv = gras_trp_tcp_recv;
715
716   plug->flush = gras_trp_bufiov_flush;
717
718   plug->data = NULL;
719   plug->exit = NULL;
720 }
721
722 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
723     const char *host,
724     int port,
725                                 /* OUT */ gras_socket_t sock)
726 {
727
728   gras_trp_sock_socket_client(NULL, host,port,sock);
729   gras_trp_buf_init_sock(sock);
730 }
731
732 /**
733  * gras_trp_buf_socket_server:
734  *
735  * Open a socket used to receive messages.
736  */
737 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
738       int port,
739                                 /* OUT */ gras_socket_t sock)
740 {
741
742   gras_trp_sock_socket_server(NULL, port, sock);
743   gras_trp_buf_init_sock(sock);
744 }
745
746 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock)
747 {
748   return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
749 }
750
751 void gras_trp_buf_socket_close(gras_socket_t sock)
752 {
753   gras_trp_bufdata_t *data = sock->bufdata;
754
755   if (data->in_buf.size != data->in_buf.pos) {
756     XBT_WARN("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
757           data->in_buf.size - data->in_buf.pos,
758           data->in_buf.size, data->in_buf.pos);
759   }
760   if (data->in_buf.data)
761     free(data->in_buf.data);
762
763   if (data->out_buf.size != data->out_buf.pos) {
764     XBT_DEBUG("Flush the socket before closing (in=%d,out=%d)",
765            data->in_buf.size, data->out_buf.size);
766     gras_trp_bufiov_flush(sock);
767   }
768   if (data->out_buf.data)
769     free(data->out_buf.data);
770
771 #ifdef HAVE_READV
772   if (data->in_buf_v) {
773     if (xbt_dynar_length(data->in_buf_v))
774       XBT_WARN("Socket closed, but some bytes were unread");
775     xbt_dynar_free(&data->in_buf_v);
776   }
777   if (data->out_buf_v) {
778     if (xbt_dynar_length(data->out_buf_v)) {
779       XBT_DEBUG("Flush the socket before closing");
780       gras_trp_bufiov_flush(sock);
781     }
782     xbt_dynar_free(&data->out_buf_v);
783   }
784 #endif
785
786   free(data);
787   gras_trp_sock_socket_close(sock);
788 }
789
790 /****************************/
791 /****[ HELPER FUNCTIONS ]****/
792 /****************************/
793
794 /*
795  * Returns the tcp protocol number from the network protocol data base, or -1 if not found
796  *
797  * getprotobyname() is not thread safe. We need to lock it.
798  */
799 static int _gras_tcp_proto_number(void)
800 {
801   struct protoent *fetchedEntry;
802   static int returnValue = 0;
803
804   if (returnValue == 0) {
805     fetchedEntry = getprotobyname("tcp");
806     if (fetchedEntry == NULL) {
807       XBT_VERB("getprotobyname(tcp) gave NULL");
808       returnValue = -1;
809     } else {
810       returnValue = fetchedEntry->p_proto;
811     }
812   }
813
814   return returnValue;
815 }
816
817 #ifdef HAVE_WINSOCK_H
818 #define RETSTR( x ) case x: return #x
819
820 const char *gras_wsa_err2string(int err)
821 {
822   switch (err) {
823     RETSTR(WSAEINTR);
824     RETSTR(WSAEBADF);
825     RETSTR(WSAEACCES);
826     RETSTR(WSAEFAULT);
827     RETSTR(WSAEINVAL);
828     RETSTR(WSAEMFILE);
829     RETSTR(WSAEWOULDBLOCK);
830     RETSTR(WSAEINPROGRESS);
831     RETSTR(WSAEALREADY);
832     RETSTR(WSAENOTSOCK);
833     RETSTR(WSAEDESTADDRREQ);
834     RETSTR(WSAEMSGSIZE);
835     RETSTR(WSAEPROTOTYPE);
836     RETSTR(WSAENOPROTOOPT);
837     RETSTR(WSAEPROTONOSUPPORT);
838     RETSTR(WSAESOCKTNOSUPPORT);
839     RETSTR(WSAEOPNOTSUPP);
840     RETSTR(WSAEPFNOSUPPORT);
841     RETSTR(WSAEAFNOSUPPORT);
842     RETSTR(WSAEADDRINUSE);
843     RETSTR(WSAEADDRNOTAVAIL);
844     RETSTR(WSAENETDOWN);
845     RETSTR(WSAENETUNREACH);
846     RETSTR(WSAENETRESET);
847     RETSTR(WSAECONNABORTED);
848     RETSTR(WSAECONNRESET);
849     RETSTR(WSAENOBUFS);
850     RETSTR(WSAEISCONN);
851     RETSTR(WSAENOTCONN);
852     RETSTR(WSAESHUTDOWN);
853     RETSTR(WSAETOOMANYREFS);
854     RETSTR(WSAETIMEDOUT);
855     RETSTR(WSAECONNREFUSED);
856     RETSTR(WSAELOOP);
857     RETSTR(WSAENAMETOOLONG);
858     RETSTR(WSAEHOSTDOWN);
859     RETSTR(WSAEHOSTUNREACH);
860     RETSTR(WSAENOTEMPTY);
861     RETSTR(WSAEPROCLIM);
862     RETSTR(WSAEUSERS);
863     RETSTR(WSAEDQUOT);
864     RETSTR(WSAESTALE);
865     RETSTR(WSAEREMOTE);
866     RETSTR(WSASYSNOTREADY);
867     RETSTR(WSAVERNOTSUPPORTED);
868     RETSTR(WSANOTINITIALISED);
869     RETSTR(WSAEDISCON);
870
871 #ifdef HAVE_WINSOCK2
872     RETSTR(WSAENOMORE);
873     RETSTR(WSAECANCELLED);
874     RETSTR(WSAEINVALIDPROCTABLE);
875     RETSTR(WSAEINVALIDPROVIDER);
876     RETSTR(WSASYSCALLFAILURE);
877     RETSTR(WSASERVICE_NOT_FOUND);
878     RETSTR(WSATYPE_NOT_FOUND);
879     RETSTR(WSA_E_NO_MORE);
880     RETSTR(WSA_E_CANCELLED);
881     RETSTR(WSAEREFUSED);
882 #endif                          /* HAVE_WINSOCK2 */
883
884     RETSTR(WSAHOST_NOT_FOUND);
885     RETSTR(WSATRY_AGAIN);
886     RETSTR(WSANO_RECOVERY);
887     RETSTR(WSANO_DATA);
888   }
889   return "unknown WSA error";
890 }
891 #endif                          /* HAVE_WINSOCK_H */
892
893 /***********************************/
894 /****[ end of HELPER FUNCTIONS ]****/
895 /***********************************/