3 /* buf trp (transport) - buffered transport using the TCP one */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
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. */
11 #include <string.h> /* memset */
15 #include "xbt/sysdep.h"
17 #include "gras/Transport/transport_private.h"
18 #include "gras/Msg/msg_interface.h" /* listener_close_socket */
20 /* FIXME maybe READV is sometime a good thing? */
28 #define MIN(a,b) ((a)<(b)?(a):(b))
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_tcp, gras_trp,
32 "TCP buffered transport");
35 *** Specific socket part
38 typedef enum { buffering_buf, buffering_iov } buffering_kind;
43 int pos; /* for receive; not exchanged over the net */
47 struct gras_trp_bufdata_ {
49 gras_trp_buf_t in_buf;
50 gras_trp_buf_t out_buf;
54 xbt_dynar_t out_buf_v;
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);
73 static int _gras_tcp_proto_number(void);
75 static XBT_INLINE void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
79 struct sockaddr_in addr;
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);
86 sock->incoming = 1; /* TCP sockets are duplex'ed */
88 sock->sd = socket(AF_INET, SOCK_STREAM, 0);
91 THROW1(system_error, 0, "Failed to create socket: %s",
92 sock_errstr(sock_errno));
96 (sock->sd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size))
97 || setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *) &size,
99 VERB1("setsockopt failed, cannot set buffer size: %s",
100 sock_errstr(sock_errno));
103 he = gethostbyname(sock->peer_name);
105 THROW2(system_error, 0, "Failed to lookup hostname %s: %s",
106 sock->peer_name, sock_errstr(sock_errno));
109 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
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);
116 if (connect(sock->sd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
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));
123 gras_trp_tcp_send(sock, (char *) &myport, sizeof(uint32_t));
124 DEBUG1("peerport sent to %d", sock->peer_port);
126 VERB4("Connect to %s:%d (sd=%d, port %d here)",
127 sock->peer_name, sock->peer_port, sock->sd, sock->port);
131 * gras_trp_sock_socket_server:
133 * Open a socket used to receive messages.
135 static XBT_INLINE void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
138 int size = sock->buf_size;
140 struct sockaddr_in server;
142 sock->outgoing = 1; /* TCP => duplex mode */
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));
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));
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));
165 if (bind(sock->sd, (struct sockaddr *) &server, sizeof(server)) == -1) {
167 THROW2(system_error, 0,
168 "Cannot bind to port %d: %s", sock->port, sock_errstr(sock_errno));
171 DEBUG2("Listen on port %d (sd=%d)", sock->port, sock->sd);
172 if (listen(sock->sd, 5) < 0) {
174 THROW2(system_error, 0,
175 "Cannot listen on port %d: %s",
176 sock->port, sock_errstr(sock_errno));
179 VERB2("Openned a server socket on port %d (sd=%d)", sock->port, sock->sd);
182 static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock)
186 struct sockaddr_in peer_in;
187 socklen_t peer_in_len = sizeof(peer_in);
194 socklen_t s = sizeof(int);
199 gras_trp_socket_new(1, &res);
201 sd = accept(sock->sd, (struct sockaddr *) &peer_in, &peer_in_len);
202 tmp_errno = sock_errno;
205 gras_socket_close(sock);
206 THROW1(system_error, 0,
207 "Accept failed (%s). Droping server socket.",
208 sock_errstr(tmp_errno));
211 if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &i, s)
212 || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *) &i,
214 THROW1(system_error, 0,
215 "setsockopt failed, cannot condition the socket: %s",
216 sock_errstr(tmp_errno));
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));
225 res->plugin = sock->plugin;
226 res->incoming = sock->incoming;
227 res->outgoing = sock->outgoing;
232 gras_trp_tcp_recv(res, (char *) &hisport, sizeof(hisport));
233 res->peer_port = ntohl(hisport);
234 DEBUG1("peerport %d received", res->peer_port);
236 /* FIXME: Lock to protect inet_ntoa */
237 if (((struct sockaddr *) &peer_in)->sa_family != AF_INET) {
238 res->peer_name = (char *) strdup("unknown");
240 struct in_addr addrAsInAddr;
243 addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
245 tmp = inet_ntoa(addrAsInAddr);
247 res->peer_name = (char *) strdup(tmp);
249 res->peer_name = (char *) strdup("unknown");
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);
261 static void gras_trp_sock_socket_close(gras_socket_t sock)
265 return; /* close only once */
267 VERB1("close tcp connection %d", sock->sd);
269 /* ask the listener to close the socket */
270 gras_msg_listener_close_socket(sock->sd);
273 /************************************/
274 /****[ end of SOCKET MANAGEMENT ]****/
275 /************************************/
278 /************************************/
279 /****[ UNBUFFERED DATA EXCHANGE ]****/
280 /************************************/
281 /* Temptation to merge this with file data exchange is great,
282 but doesn't work on BillWare (see tcp_write() in portable.h) */
283 static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock,
285 unsigned long int size)
291 status = tcp_write(sock->sd, data, (size_t) size);
292 DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
296 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
298 if (errno == EINTR || errno == EAGAIN)
302 THROW4(system_error, 0, "write(%d,%p,%ld) failed: %s",
303 sock->sd, data, size, sock_errstr(sock_errno));
310 THROW1(system_error, 0, "file descriptor closed (%s)",
311 sock_errstr(sock_errno));
316 static XBT_INLINE int
317 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
319 unsigned long int size,
320 unsigned long int bufsize)
326 data[0] = sock->recvd_val;
335 DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
336 sock->sd, data + got, bufsize, got,
337 hexa_str((unsigned char *) data, got, 0));
338 status = tcp_read(sock->sd, data + got, (size_t) bufsize);
341 THROW7(system_error, 0,
342 "read(%d,%p,%d) from %s:%d failed: %s; got %d so far", sock->sd,
343 data + got, (int) size, gras_socket_peer_name(sock),
344 gras_socket_peer_port(sock), sock_errstr(sock_errno), got);
346 DEBUG2("Got %d more bytes (%s)", status,
347 hexa_str((unsigned char *) data + got, status, 0));
353 THROW1(system_error, errno,
354 "Socket closed by remote side (got %d bytes before this)", got);
361 static int gras_trp_tcp_recv(gras_socket_t sock,
362 char *data, unsigned long int size)
364 return gras_trp_tcp_recv_withbuffer(sock, data, size, size);
368 /*******************************************/
369 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
370 /*******************************************/
372 /**********************************/
373 /****[ BUFFERED DATA EXCHANGE ]****/
374 /**********************************/
376 /* Make sure the data is sent */
377 static void gras_trp_bufiov_flush(gras_socket_t sock)
383 gras_trp_bufdata_t *data = sock->bufdata;
387 if (data->out == buffering_buf) {
388 if (XBT_LOG_ISENABLED(gras_trp_tcp, xbt_log_priority_debug))
389 hexa_print("chunk to send ",
390 (unsigned char *) data->out_buf.data, data->out_buf.size);
391 if ((data->out_buf.size - data->out_buf.pos) != 0) {
392 DEBUG3("Send the chunk (size=%d) to %s:%d", data->out_buf.size,
393 gras_socket_peer_name(sock), gras_socket_peer_port(sock));
394 gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
395 VERB1("Chunk sent (size=%d)", data->out_buf.size);
396 data->out_buf.size = 0;
400 if (data->out == buffering_iov) {
401 DEBUG0("Flush out iov");
402 vect = sock->bufdata->out_buf_v;
403 if ((size = xbt_dynar_length(vect))) {
404 DEBUG1("Flush %d chunks out of this socket", size);
405 writev(sock->sd, xbt_dynar_get_ptr(vect, 0), size);
406 xbt_dynar_reset(vect);
408 data->out_buf.size = 0; /* reset the buffer containing non-stable data */
411 if (data->in == buffering_iov) {
412 DEBUG0("Flush in iov");
413 vect = sock->bufdata->in_buf_v;
414 if ((size = xbt_dynar_length(vect))) {
415 DEBUG1("Get %d chunks from of this socket", size);
416 readv(sock->sd, xbt_dynar_get_ptr(vect, 0), size);
417 xbt_dynar_reset(vect);
424 gras_trp_buf_send(gras_socket_t sock,
426 unsigned long int size, int stable_ignored)
429 gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata;
434 while (chunk_pos < size) {
435 /* size of the chunk to receive in that shot */
437 min(size - chunk_pos, data->buffsize - data->out_buf.size);
438 DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
439 (int) data->out_buf.size,
440 ((int) data->out_buf.size) + thissize - 1, size,
441 hexa_str((unsigned char *) chunk, thissize, 0));
443 memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos,
446 data->out_buf.size += thissize;
447 chunk_pos += thissize;
448 DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
449 data->out_buf.size, size - chunk_pos, size,
450 hexa_str((unsigned char *) chunk, chunk_pos, 0));
452 if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
453 gras_trp_bufiov_flush(sock);
460 gras_trp_buf_recv(gras_socket_t sock, char *chunk, unsigned long int size)
463 gras_trp_bufdata_t *data = sock->bufdata;
464 long int chunk_pos = 0;
468 while (chunk_pos < size) {
469 /* size of the chunk to receive in that shot */
472 if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
474 DEBUG2("Get more data (size=%d,bufsize=%d)",
475 (int) MIN(size - chunk_pos, data->buffsize),
476 (int) data->buffsize);
480 gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
481 MIN(size - chunk_pos, data->buffsize),
484 data->in_buf.pos = 0;
487 thissize = min(size - chunk_pos, data->in_buf.size - data->in_buf.pos);
488 memcpy(chunk + chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
490 data->in_buf.pos += thissize;
491 chunk_pos += thissize;
492 DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
493 data->in_buf.pos, size - chunk_pos, size,
494 hexa_str((unsigned char *) chunk, chunk_pos, 0));
496 /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */
497 sock->moredata = (data->in_buf.size > data->in_buf.pos);
498 DEBUG1("There is %smore data", (sock->moredata ? "" : "no "));
504 /*****************************************/
505 /****[ end of BUFFERED DATA EXCHANGE ]****/
506 /*****************************************/
508 /********************************/
509 /****[ VECTOR DATA EXCHANGE ]****/
510 /********************************/
513 gras_trp_iov_send(gras_socket_t sock,
514 const char *chunk, unsigned long int size, int stable)
517 gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata;
520 DEBUG1("Buffer one chunk to be sent later (%s)",
521 hexa_str((char *) chunk, size, 0));
523 elm.iov_len = (size_t) size;
526 /* data storage won't last until flush. Save it in a buffer if we can */
528 if (size > data->buffsize - data->out_buf.size) {
530 flush the socket, using data in its actual storage */
531 elm.iov_base = (void *) chunk;
532 xbt_dynar_push(data->out_buf_v, &elm);
534 gras_trp_bufiov_flush(sock);
537 /* buffer big enough:
538 copy data into it, and chain it for upcoming writev */
539 memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
540 elm.iov_base = (void *) (data->out_buf.data + data->out_buf.size);
541 data->out_buf.size += size;
543 xbt_dynar_push(data->out_buf_v, &elm);
547 /* data storage stable. Chain it */
549 elm.iov_base = (void *) chunk;
550 xbt_dynar_push(data->out_buf_v, &elm);
555 gras_trp_iov_recv(gras_socket_t sock, char *chunk, unsigned long int size)
559 DEBUG0("Buffer one chunk to be received later");
560 elm.iov_base = (void *) chunk;
561 elm.iov_len = (size_t) size;
562 xbt_dynar_push(sock->bufdata->in_buf_v, &elm);
568 /***************************************/
569 /****[ end of VECTOR DATA EXCHANGE ]****/
570 /***************************************/
574 *** Prototypes of BUFFERED
577 void gras_trp_buf_socket_client(gras_trp_plugin_t self, gras_socket_t sock);
578 void gras_trp_buf_socket_server(gras_trp_plugin_t self, gras_socket_t sock);
579 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
581 void gras_trp_buf_socket_close(gras_socket_t sd);
584 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock)
586 gras_trp_bufdata_t *data = xbt_new(gras_trp_bufdata_t, 1);
588 data->buffsize = 100 * 1024; /* 100k */
590 data->in_buf.size = 0;
591 data->in_buf.data = xbt_malloc(data->buffsize);
592 data->in_buf.pos = 0; /* useless, indeed, since size==pos */
594 data->out_buf.size = 0;
595 data->out_buf.data = xbt_malloc(data->buffsize);
596 data->out_buf.pos = data->out_buf.size;
599 data->in_buf_v = data->out_buf_v = NULL;
600 data->in_buf_v = xbt_dynar_new(sizeof(struct iovec), NULL);
601 data->out_buf_v = xbt_dynar_new(sizeof(struct iovec), NULL);
602 data->out = buffering_iov;
604 data->out = buffering_buf;
607 data->in = buffering_buf;
609 sock->bufdata = data;
616 void gras_trp_tcp_setup(gras_trp_plugin_t plug)
619 plug->socket_client = gras_trp_buf_socket_client;
620 plug->socket_server = gras_trp_buf_socket_server;
621 plug->socket_accept = gras_trp_buf_socket_accept;
622 plug->socket_close = gras_trp_buf_socket_close;
625 plug->send = gras_trp_iov_send;
627 plug->send = gras_trp_buf_send;
629 plug->recv = gras_trp_buf_recv;
631 plug->raw_send = gras_trp_tcp_send;
632 plug->raw_recv = gras_trp_tcp_recv;
634 plug->flush = gras_trp_bufiov_flush;
640 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
641 /* OUT */ gras_socket_t sock)
644 gras_trp_sock_socket_client(NULL, sock);
645 gras_trp_buf_init_sock(sock);
649 * gras_trp_buf_socket_server:
651 * Open a socket used to receive messages.
653 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
654 /* OUT */ gras_socket_t sock)
657 gras_trp_sock_socket_server(NULL, sock);
658 gras_trp_buf_init_sock(sock);
661 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock)
663 return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
666 void gras_trp_buf_socket_close(gras_socket_t sock)
668 gras_trp_bufdata_t *data = sock->bufdata;
670 if (data->in_buf.size != data->in_buf.pos) {
671 WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
672 data->in_buf.size - data->in_buf.pos,
673 data->in_buf.size, data->in_buf.pos);
675 if (data->in_buf.data)
676 free(data->in_buf.data);
678 if (data->out_buf.size != data->out_buf.pos) {
679 DEBUG2("Flush the socket before closing (in=%d,out=%d)",
680 data->in_buf.size, data->out_buf.size);
681 gras_trp_bufiov_flush(sock);
683 if (data->out_buf.data)
684 free(data->out_buf.data);
687 if (data->in_buf_v) {
688 if (xbt_dynar_length(data->in_buf_v))
689 WARN0("Socket closed, but some bytes were unread");
690 xbt_dynar_free(&data->in_buf_v);
692 if (data->out_buf_v) {
693 if (xbt_dynar_length(data->out_buf_v)) {
694 DEBUG0("Flush the socket before closing");
695 gras_trp_bufiov_flush(sock);
697 xbt_dynar_free(&data->out_buf_v);
702 gras_trp_sock_socket_close(sock);
705 /****************************/
706 /****[ HELPER FUNCTIONS ]****/
707 /****************************/
710 * Returns the tcp protocol number from the network protocol data base.
712 * getprotobyname() is not thread safe. We need to lock it.
714 static int _gras_tcp_proto_number(void)
716 struct protoent *fetchedEntry;
717 static int returnValue = 0;
719 if (returnValue == 0) {
720 fetchedEntry = getprotobyname("tcp");
721 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
722 returnValue = fetchedEntry->p_proto;
728 #ifdef HAVE_WINSOCK_H
729 #define RETSTR( x ) case x: return #x
731 const char *gras_wsa_err2string(int err)
740 RETSTR(WSAEWOULDBLOCK);
741 RETSTR(WSAEINPROGRESS);
744 RETSTR(WSAEDESTADDRREQ);
746 RETSTR(WSAEPROTOTYPE);
747 RETSTR(WSAENOPROTOOPT);
748 RETSTR(WSAEPROTONOSUPPORT);
749 RETSTR(WSAESOCKTNOSUPPORT);
750 RETSTR(WSAEOPNOTSUPP);
751 RETSTR(WSAEPFNOSUPPORT);
752 RETSTR(WSAEAFNOSUPPORT);
753 RETSTR(WSAEADDRINUSE);
754 RETSTR(WSAEADDRNOTAVAIL);
756 RETSTR(WSAENETUNREACH);
757 RETSTR(WSAENETRESET);
758 RETSTR(WSAECONNABORTED);
759 RETSTR(WSAECONNRESET);
763 RETSTR(WSAESHUTDOWN);
764 RETSTR(WSAETOOMANYREFS);
765 RETSTR(WSAETIMEDOUT);
766 RETSTR(WSAECONNREFUSED);
768 RETSTR(WSAENAMETOOLONG);
769 RETSTR(WSAEHOSTDOWN);
770 RETSTR(WSAEHOSTUNREACH);
771 RETSTR(WSAENOTEMPTY);
777 RETSTR(WSASYSNOTREADY);
778 RETSTR(WSAVERNOTSUPPORTED);
779 RETSTR(WSANOTINITIALISED);
784 RETSTR(WSAECANCELLED);
785 RETSTR(WSAEINVALIDPROCTABLE);
786 RETSTR(WSAEINVALIDPROVIDER);
787 RETSTR(WSASYSCALLFAILURE);
788 RETSTR(WSASERVICE_NOT_FOUND);
789 RETSTR(WSATYPE_NOT_FOUND);
790 RETSTR(WSA_E_NO_MORE);
791 RETSTR(WSA_E_CANCELLED);
793 #endif /* HAVE_WINSOCK2 */
795 RETSTR(WSAHOST_NOT_FOUND);
796 RETSTR(WSATRY_AGAIN);
797 RETSTR(WSANO_RECOVERY);
800 return "unknown WSA error";
802 #endif /* HAVE_WINSOCK_H */
804 /***********************************/
805 /****[ end of HELPER FUNCTIONS ]****/
806 /***********************************/