1 /* buf trp (transport) - buffered transport using the TCP one */
3 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include <string.h> /* memset */
13 #include "xbt/sysdep.h"
15 #include "gras/Transport/transport_private.h"
16 #include "gras/Msg/msg_interface.h" /* listener_close_socket */
18 /* FIXME maybe READV is sometime a good thing? */
26 #define MIN(a,b) ((a)<(b)?(a):(b))
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_tcp, gras_trp,
30 "TCP buffered transport");
33 *** Specific socket part
36 typedef enum { buffering_buf, buffering_iov } buffering_kind;
41 int pos; /* for receive; not exchanged over the net */
45 struct gras_trp_bufdata_ {
47 gras_trp_buf_t in_buf;
48 gras_trp_buf_t out_buf;
52 xbt_dynar_t out_buf_v;
60 /*****************************/
61 /****[ SOCKET MANAGEMENT ]****/
62 /*****************************/
63 /* we exchange port number on client side on socket creation,
64 so we need to be able to talk right now. */
65 static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock, const char *data,
66 unsigned long int size);
67 static int gras_trp_tcp_recv(gras_socket_t sock, char *data,
68 unsigned long int size);
71 static int _gras_tcp_proto_number(void);
73 static XBT_INLINE void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
77 struct sockaddr_in addr;
79 struct in_addr *haddr;
80 int size = sock->buf_size;
81 uint32_t myport = htonl(((gras_trp_procdata_t)
82 gras_libdata_by_id(gras_trp_libdata_id))->myport);
84 sock->incoming = 1; /* TCP sockets are duplex'ed */
86 sock->sd = socket(AF_INET, SOCK_STREAM, 0);
89 THROW1(system_error, 0, "Failed to create socket: %s",
90 sock_errstr(sock_errno));
94 (sock->sd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size))
95 || setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *) &size,
97 VERB1("setsockopt failed, cannot set buffer size: %s",
98 sock_errstr(sock_errno));
101 he = gethostbyname(sock->peer_name);
103 THROW2(system_error, 0, "Failed to lookup hostname %s: %s",
104 sock->peer_name, sock_errstr(sock_errno));
107 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
109 memset(&addr, 0, sizeof(struct sockaddr_in));
110 memcpy(&addr.sin_addr, haddr, sizeof(struct in_addr));
111 addr.sin_family = AF_INET;
112 addr.sin_port = htons(sock->peer_port);
114 if (connect(sock->sd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
116 THROW3(system_error, 0,
117 "Failed to connect socket to %s:%d (%s)",
118 sock->peer_name, sock->peer_port, sock_errstr(sock_errno));
121 gras_trp_tcp_send(sock, (char *) &myport, sizeof(uint32_t));
122 DEBUG1("peerport sent to %d", sock->peer_port);
124 VERB4("Connect to %s:%d (sd=%d, port %d here)",
125 sock->peer_name, sock->peer_port, sock->sd, sock->port);
129 * gras_trp_sock_socket_server:
131 * Open a socket used to receive messages.
133 static XBT_INLINE void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
136 int size = sock->buf_size;
138 struct sockaddr_in server;
140 sock->outgoing = 1; /* TCP => duplex mode */
142 server.sin_port = htons((u_short) sock->port);
143 server.sin_addr.s_addr = INADDR_ANY;
144 server.sin_family = AF_INET;
145 if ((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
146 THROW1(system_error, 0, "Socket allocation failed: %s",
147 sock_errstr(sock_errno));
150 (sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)))
151 THROW1(system_error, 0,
152 "setsockopt failed, cannot condition the socket: %s",
153 sock_errstr(sock_errno));
155 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF,
156 (char *) &size, sizeof(size))
157 || setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF,
158 (char *) &size, sizeof(size))) {
159 VERB1("setsockopt failed, cannot set buffer size: %s",
160 sock_errstr(sock_errno));
163 if (bind(sock->sd, (struct sockaddr *) &server, sizeof(server)) == -1) {
165 THROW2(system_error, 0,
166 "Cannot bind to port %d: %s", sock->port, sock_errstr(sock_errno));
169 DEBUG2("Listen on port %d (sd=%d)", sock->port, sock->sd);
170 if (listen(sock->sd, 5) < 0) {
172 THROW2(system_error, 0,
173 "Cannot listen on port %d: %s",
174 sock->port, sock_errstr(sock_errno));
177 VERB2("Openned a server socket on port %d (sd=%d)", sock->port, sock->sd);
180 static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock)
184 struct sockaddr_in peer_in;
185 socklen_t peer_in_len = sizeof(peer_in);
192 socklen_t s = sizeof(int);
197 gras_trp_socket_new(1, &res);
199 sd = accept(sock->sd, (struct sockaddr *) &peer_in, &peer_in_len);
200 tmp_errno = sock_errno;
203 gras_socket_close(sock);
204 THROW1(system_error, 0,
205 "Accept failed (%s). Droping server socket.",
206 sock_errstr(tmp_errno));
209 if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &i, s)
210 || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *) &i,
212 THROW1(system_error, 0,
213 "setsockopt failed, cannot condition the socket: %s",
214 sock_errstr(tmp_errno));
216 res->buf_size = sock->buf_size;
217 size = sock->buf_size;
218 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size))
219 || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)))
220 VERB1("setsockopt failed, cannot set buffer size: %s",
221 sock_errstr(tmp_errno));
223 res->plugin = sock->plugin;
224 res->incoming = sock->incoming;
225 res->outgoing = sock->outgoing;
230 gras_trp_tcp_recv(res, (char *) &hisport, sizeof(hisport));
231 res->peer_port = ntohl(hisport);
232 DEBUG1("peerport %d received", res->peer_port);
234 /* FIXME: Lock to protect inet_ntoa */
235 if (((struct sockaddr *) &peer_in)->sa_family != AF_INET) {
236 res->peer_name = (char *) strdup("unknown");
238 struct in_addr addrAsInAddr;
241 addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
243 tmp = inet_ntoa(addrAsInAddr);
245 res->peer_name = (char *) strdup(tmp);
247 res->peer_name = (char *) strdup("unknown");
251 VERB3("Accepted from %s:%d (sd=%d)", res->peer_name, res->peer_port, sd);
252 xbt_dynar_push(((gras_trp_procdata_t)
253 gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
259 static void gras_trp_sock_socket_close(gras_socket_t sock)
263 return; /* close only once */
265 VERB1("close tcp connection %d", sock->sd);
267 /* ask the listener to close the socket */
268 gras_msg_listener_close_socket(sock->sd);
271 /************************************/
272 /****[ end of SOCKET MANAGEMENT ]****/
273 /************************************/
276 /************************************/
277 /****[ UNBUFFERED DATA EXCHANGE ]****/
278 /************************************/
279 /* Temptation to merge this with file data exchange is great,
280 but doesn't work on BillWare (see tcp_write() in portable.h) */
281 static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock,
283 unsigned long int size)
289 status = tcp_write(sock->sd, data, (size_t) size);
290 DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
294 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
296 if (errno == EINTR || errno == EAGAIN)
300 THROW4(system_error, 0, "write(%d,%p,%ld) failed: %s",
301 sock->sd, data, size, sock_errstr(sock_errno));
308 THROW1(system_error, 0, "file descriptor closed (%s)",
309 sock_errstr(sock_errno));
314 static XBT_INLINE int
315 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
317 unsigned long int size,
318 unsigned long int bufsize)
324 data[0] = sock->recvd_val;
333 DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
334 sock->sd, data + got, bufsize, got,
335 hexa_str((unsigned char *) data, got, 0));
336 status = tcp_read(sock->sd, data + got, (size_t) bufsize);
339 THROW7(system_error, 0,
340 "read(%d,%p,%d) from %s:%d failed: %s; got %d so far", sock->sd,
341 data + got, (int) size, gras_socket_peer_name(sock),
342 gras_socket_peer_port(sock), sock_errstr(sock_errno), got);
344 DEBUG2("Got %d more bytes (%s)", status,
345 hexa_str((unsigned char *) data + got, status, 0));
351 THROW1(system_error, errno,
352 "Socket closed by remote side (got %d bytes before this)", got);
359 static int gras_trp_tcp_recv(gras_socket_t sock,
360 char *data, unsigned long int size)
362 return gras_trp_tcp_recv_withbuffer(sock, data, size, size);
366 /*******************************************/
367 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
368 /*******************************************/
370 /**********************************/
371 /****[ BUFFERED DATA EXCHANGE ]****/
372 /**********************************/
374 /* Make sure the data is sent */
375 static void gras_trp_bufiov_flush(gras_socket_t sock)
381 gras_trp_bufdata_t *data = sock->bufdata;
385 if (data->out == buffering_buf) {
386 if (XBT_LOG_ISENABLED(gras_trp_tcp, xbt_log_priority_debug))
387 hexa_print("chunk to send ",
388 (unsigned char *) data->out_buf.data, data->out_buf.size);
389 if ((data->out_buf.size - data->out_buf.pos) != 0) {
390 DEBUG3("Send the chunk (size=%d) to %s:%d", data->out_buf.size,
391 gras_socket_peer_name(sock), gras_socket_peer_port(sock));
392 gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
393 VERB1("Chunk sent (size=%d)", data->out_buf.size);
394 data->out_buf.size = 0;
398 if (data->out == buffering_iov) {
399 DEBUG0("Flush out iov");
400 vect = sock->bufdata->out_buf_v;
401 if ((size = xbt_dynar_length(vect))) {
402 DEBUG1("Flush %d chunks out of this socket", size);
403 writev(sock->sd, xbt_dynar_get_ptr(vect, 0), size);
404 xbt_dynar_reset(vect);
406 data->out_buf.size = 0; /* reset the buffer containing non-stable data */
409 if (data->in == buffering_iov) {
410 DEBUG0("Flush in iov");
411 vect = sock->bufdata->in_buf_v;
412 if ((size = xbt_dynar_length(vect))) {
413 DEBUG1("Get %d chunks from of this socket", size);
414 readv(sock->sd, xbt_dynar_get_ptr(vect, 0), size);
415 xbt_dynar_reset(vect);
422 gras_trp_buf_send(gras_socket_t sock,
424 unsigned long int size, int stable_ignored)
427 gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata;
432 while (chunk_pos < size) {
433 /* size of the chunk to receive in that shot */
435 min(size - chunk_pos, data->buffsize - data->out_buf.size);
436 DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
437 (int) data->out_buf.size,
438 ((int) data->out_buf.size) + thissize - 1, size,
439 hexa_str((unsigned char *) chunk, thissize, 0));
441 memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos,
444 data->out_buf.size += thissize;
445 chunk_pos += thissize;
446 DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
447 data->out_buf.size, size - chunk_pos, size,
448 hexa_str((unsigned char *) chunk, chunk_pos, 0));
450 if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
451 gras_trp_bufiov_flush(sock);
458 gras_trp_buf_recv(gras_socket_t sock, char *chunk, unsigned long int size)
461 gras_trp_bufdata_t *data = sock->bufdata;
462 long int chunk_pos = 0;
466 while (chunk_pos < size) {
467 /* size of the chunk to receive in that shot */
470 if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
472 DEBUG2("Get more data (size=%d,bufsize=%d)",
473 (int) MIN(size - chunk_pos, data->buffsize),
474 (int) data->buffsize);
478 gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
479 MIN(size - chunk_pos, data->buffsize),
482 data->in_buf.pos = 0;
485 thissize = min(size - chunk_pos, data->in_buf.size - data->in_buf.pos);
486 memcpy(chunk + chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
488 data->in_buf.pos += thissize;
489 chunk_pos += thissize;
490 DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
491 data->in_buf.pos, size - chunk_pos, size,
492 hexa_str((unsigned char *) chunk, chunk_pos, 0));
494 /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */
495 sock->moredata = (data->in_buf.size > data->in_buf.pos);
496 DEBUG1("There is %smore data", (sock->moredata ? "" : "no "));
502 /*****************************************/
503 /****[ end of BUFFERED DATA EXCHANGE ]****/
504 /*****************************************/
506 /********************************/
507 /****[ VECTOR DATA EXCHANGE ]****/
508 /********************************/
511 gras_trp_iov_send(gras_socket_t sock,
512 const char *chunk, unsigned long int size, int stable)
515 gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata;
518 DEBUG1("Buffer one chunk to be sent later (%s)",
519 hexa_str((char *) chunk, size, 0));
521 elm.iov_len = (size_t) size;
524 /* data storage won't last until flush. Save it in a buffer if we can */
526 if (size > data->buffsize - data->out_buf.size) {
528 flush the socket, using data in its actual storage */
529 elm.iov_base = (void *) chunk;
530 xbt_dynar_push(data->out_buf_v, &elm);
532 gras_trp_bufiov_flush(sock);
535 /* buffer big enough:
536 copy data into it, and chain it for upcoming writev */
537 memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
538 elm.iov_base = (void *) (data->out_buf.data + data->out_buf.size);
539 data->out_buf.size += size;
541 xbt_dynar_push(data->out_buf_v, &elm);
545 /* data storage stable. Chain it */
547 elm.iov_base = (void *) chunk;
548 xbt_dynar_push(data->out_buf_v, &elm);
553 gras_trp_iov_recv(gras_socket_t sock, char *chunk, unsigned long int size)
557 DEBUG0("Buffer one chunk to be received later");
558 elm.iov_base = (void *) chunk;
559 elm.iov_len = (size_t) size;
560 xbt_dynar_push(sock->bufdata->in_buf_v, &elm);
566 /***************************************/
567 /****[ end of VECTOR DATA EXCHANGE ]****/
568 /***************************************/
572 *** Prototypes of BUFFERED
575 void gras_trp_buf_socket_client(gras_trp_plugin_t self, gras_socket_t sock);
576 void gras_trp_buf_socket_server(gras_trp_plugin_t self, gras_socket_t sock);
577 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
579 void gras_trp_buf_socket_close(gras_socket_t sd);
582 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock)
584 gras_trp_bufdata_t *data = xbt_new(gras_trp_bufdata_t, 1);
586 data->buffsize = 100 * 1024; /* 100k */
588 data->in_buf.size = 0;
589 data->in_buf.data = xbt_malloc(data->buffsize);
590 data->in_buf.pos = 0; /* useless, indeed, since size==pos */
592 data->out_buf.size = 0;
593 data->out_buf.data = xbt_malloc(data->buffsize);
594 data->out_buf.pos = data->out_buf.size;
597 data->in_buf_v = data->out_buf_v = NULL;
598 data->in_buf_v = xbt_dynar_new(sizeof(struct iovec), NULL);
599 data->out_buf_v = xbt_dynar_new(sizeof(struct iovec), NULL);
600 data->out = buffering_iov;
602 data->out = buffering_buf;
605 data->in = buffering_buf;
607 sock->bufdata = data;
614 void gras_trp_tcp_setup(gras_trp_plugin_t plug)
617 plug->socket_client = gras_trp_buf_socket_client;
618 plug->socket_server = gras_trp_buf_socket_server;
619 plug->socket_accept = gras_trp_buf_socket_accept;
620 plug->socket_close = gras_trp_buf_socket_close;
623 plug->send = gras_trp_iov_send;
625 plug->send = gras_trp_buf_send;
627 plug->recv = gras_trp_buf_recv;
629 plug->raw_send = gras_trp_tcp_send;
630 plug->raw_recv = gras_trp_tcp_recv;
632 plug->flush = gras_trp_bufiov_flush;
638 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
639 /* OUT */ gras_socket_t sock)
642 gras_trp_sock_socket_client(NULL, sock);
643 gras_trp_buf_init_sock(sock);
647 * gras_trp_buf_socket_server:
649 * Open a socket used to receive messages.
651 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
652 /* OUT */ gras_socket_t sock)
655 gras_trp_sock_socket_server(NULL, sock);
656 gras_trp_buf_init_sock(sock);
659 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock)
661 return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
664 void gras_trp_buf_socket_close(gras_socket_t sock)
666 gras_trp_bufdata_t *data = sock->bufdata;
668 if (data->in_buf.size != data->in_buf.pos) {
669 WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
670 data->in_buf.size - data->in_buf.pos,
671 data->in_buf.size, data->in_buf.pos);
673 if (data->in_buf.data)
674 free(data->in_buf.data);
676 if (data->out_buf.size != data->out_buf.pos) {
677 DEBUG2("Flush the socket before closing (in=%d,out=%d)",
678 data->in_buf.size, data->out_buf.size);
679 gras_trp_bufiov_flush(sock);
681 if (data->out_buf.data)
682 free(data->out_buf.data);
685 if (data->in_buf_v) {
686 if (xbt_dynar_length(data->in_buf_v))
687 WARN0("Socket closed, but some bytes were unread");
688 xbt_dynar_free(&data->in_buf_v);
690 if (data->out_buf_v) {
691 if (xbt_dynar_length(data->out_buf_v)) {
692 DEBUG0("Flush the socket before closing");
693 gras_trp_bufiov_flush(sock);
695 xbt_dynar_free(&data->out_buf_v);
700 gras_trp_sock_socket_close(sock);
703 /****************************/
704 /****[ HELPER FUNCTIONS ]****/
705 /****************************/
708 * Returns the tcp protocol number from the network protocol data base.
710 * getprotobyname() is not thread safe. We need to lock it.
712 static int _gras_tcp_proto_number(void)
714 struct protoent *fetchedEntry;
715 static int returnValue = 0;
717 if (returnValue == 0) {
718 fetchedEntry = getprotobyname("tcp");
719 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
720 returnValue = fetchedEntry->p_proto;
726 #ifdef HAVE_WINSOCK_H
727 #define RETSTR( x ) case x: return #x
729 const char *gras_wsa_err2string(int err)
738 RETSTR(WSAEWOULDBLOCK);
739 RETSTR(WSAEINPROGRESS);
742 RETSTR(WSAEDESTADDRREQ);
744 RETSTR(WSAEPROTOTYPE);
745 RETSTR(WSAENOPROTOOPT);
746 RETSTR(WSAEPROTONOSUPPORT);
747 RETSTR(WSAESOCKTNOSUPPORT);
748 RETSTR(WSAEOPNOTSUPP);
749 RETSTR(WSAEPFNOSUPPORT);
750 RETSTR(WSAEAFNOSUPPORT);
751 RETSTR(WSAEADDRINUSE);
752 RETSTR(WSAEADDRNOTAVAIL);
754 RETSTR(WSAENETUNREACH);
755 RETSTR(WSAENETRESET);
756 RETSTR(WSAECONNABORTED);
757 RETSTR(WSAECONNRESET);
761 RETSTR(WSAESHUTDOWN);
762 RETSTR(WSAETOOMANYREFS);
763 RETSTR(WSAETIMEDOUT);
764 RETSTR(WSAECONNREFUSED);
766 RETSTR(WSAENAMETOOLONG);
767 RETSTR(WSAEHOSTDOWN);
768 RETSTR(WSAEHOSTUNREACH);
769 RETSTR(WSAENOTEMPTY);
775 RETSTR(WSASYSNOTREADY);
776 RETSTR(WSAVERNOTSUPPORTED);
777 RETSTR(WSANOTINITIALISED);
782 RETSTR(WSAECANCELLED);
783 RETSTR(WSAEINVALIDPROCTABLE);
784 RETSTR(WSAEINVALIDPROVIDER);
785 RETSTR(WSASYSCALLFAILURE);
786 RETSTR(WSASERVICE_NOT_FOUND);
787 RETSTR(WSATYPE_NOT_FOUND);
788 RETSTR(WSA_E_NO_MORE);
789 RETSTR(WSA_E_CANCELLED);
791 #endif /* HAVE_WINSOCK2 */
793 RETSTR(WSAHOST_NOT_FOUND);
794 RETSTR(WSATRY_AGAIN);
795 RETSTR(WSANO_RECOVERY);
798 return "unknown WSA error";
800 #endif /* HAVE_WINSOCK_H */
802 /***********************************/
803 /****[ end of HELPER FUNCTIONS ]****/
804 /***********************************/