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"
19 /* FIXME maybe READV is sometime a good thing? */
27 #define MIN(a,b) ((a)<(b)?(a):(b))
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_tcp,gras_trp,
31 "TCP buffered transport");
34 *** Specific socket part
37 typedef enum { buffering_buf, buffering_iov } buffering_kind;
42 int pos; /* for receive; not exchanged over the net */
46 struct gras_trp_bufdata_{
48 gras_trp_buf_t in_buf;
49 gras_trp_buf_t out_buf;
53 xbt_dynar_t out_buf_v;
61 /*****************************/
62 /****[ SOCKET MANAGEMENT ]****/
63 /*****************************/
64 /* we exchange port number on client side on socket creation,
65 so we need to be able to talk right now. */
66 static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock, const char *data,
67 unsigned long int size);
68 static int gras_trp_tcp_recv(gras_socket_t sock, char *data,
69 unsigned long int size);
72 static int _gras_tcp_proto_number(void);
74 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) gras_libdata_by_id(gras_trp_libdata_id))->myport);
83 sock->incoming = 1; /* TCP sockets are duplex'ed */
85 sock->sd = socket (AF_INET, SOCK_STREAM, 0);
88 THROW1(system_error,0, "Failed to create socket: %s", sock_errstr(sock_errno));
91 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
92 setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
93 WARN1("setsockopt failed, cannot set buffer size: %s",sock_errstr(sock_errno));
96 he = gethostbyname (sock->peer_name);
98 THROW2(system_error,0, "Failed to lookup hostname %s: %s",
99 sock->peer_name, sock_errstr(sock_errno));
102 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
104 memset(&addr, 0, sizeof(struct sockaddr_in));
105 memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
106 addr.sin_family = AF_INET;
107 addr.sin_port = htons (sock->peer_port);
109 if (connect (sock->sd, (struct sockaddr*) &addr, sizeof (addr)) < 0) {
111 THROW3(system_error,0,
112 "Failed to connect socket to %s:%d (%s)",
113 sock->peer_name, sock->peer_port, sock_errstr(sock_errno));
116 gras_trp_tcp_send(sock,(char*)&myport,sizeof(uint32_t));
117 DEBUG1("peerport sent to %d", sock->peer_port);
119 VERB4("Connect to %s:%d (sd=%d, port %d here)",
120 sock->peer_name, sock->peer_port, sock->sd, sock->port);
124 * gras_trp_sock_socket_server:
126 * Open a socket used to receive messages.
128 static XBT_INLINE void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
130 int size = sock->buf_size;
132 struct sockaddr_in server;
134 sock->outgoing = 1; /* TCP => duplex mode */
136 server.sin_port = htons((u_short)sock->port);
137 server.sin_addr.s_addr = INADDR_ANY;
138 server.sin_family = AF_INET;
139 if((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
140 THROW1(system_error,0,"Socket allocation failed: %s", sock_errstr(sock_errno));
142 if (setsockopt(sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)))
143 THROW1(system_error,0,
144 "setsockopt failed, cannot condition the socket: %s",
145 sock_errstr(sock_errno));
147 if ( setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF,
148 (char *)&size, sizeof(size))
149 || setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF,
150 (char *)&size, sizeof(size))) {
151 WARN1("setsockopt failed, cannot set buffer size: %s",
152 sock_errstr(sock_errno));
155 if (bind(sock->sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
157 THROW2(system_error,0,
158 "Cannot bind to port %d: %s",
159 sock->port, sock_errstr(sock_errno));
162 DEBUG2("Listen on port %d (sd=%d)",sock->port, sock->sd);
163 if (listen(sock->sd, 5) < 0) {
165 THROW2(system_error,0,
166 "Cannot listen on port %d: %s",
167 sock->port,sock_errstr(sock_errno));
170 VERB2("Openned a server socket on port %d (sd=%d)",sock->port,sock->sd);
173 static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) {
176 struct sockaddr_in peer_in;
177 socklen_t peer_in_len = sizeof(peer_in);
184 socklen_t s = sizeof(int);
189 gras_trp_socket_new(1,&res);
191 sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
192 tmp_errno = sock_errno;
195 gras_socket_close(sock);
196 THROW1(system_error,0,
197 "Accept failed (%s). Droping server socket.", sock_errstr(tmp_errno));
200 if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s)
201 || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *)&i, s))
202 THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
203 sock_errstr(tmp_errno));
205 res->buf_size = sock->buf_size;
206 size = sock->buf_size;
207 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size))
208 || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size)))
209 WARN1("setsockopt failed, cannot set buffer size: %s", sock_errstr(tmp_errno));
211 res->plugin = sock->plugin;
212 res->incoming = sock->incoming;
213 res->outgoing = sock->outgoing;
218 gras_trp_tcp_recv(res,(char*)&hisport,sizeof(hisport));
219 res->peer_port = ntohl(hisport);
220 DEBUG1("peerport %d received",res->peer_port);
222 /* FIXME: Lock to protect inet_ntoa */
223 if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) {
224 res->peer_name = (char*)strdup("unknown");
226 struct in_addr addrAsInAddr;
229 addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
231 tmp = inet_ntoa(addrAsInAddr);
233 res->peer_name = (char*)strdup(tmp);
235 res->peer_name = (char*)strdup("unknown");
239 VERB3("Accepted from %s:%d (sd=%d)", res->peer_name,res->peer_port,sd);
240 xbt_dynar_push(((gras_trp_procdata_t)
241 gras_libdata_by_id(gras_trp_libdata_id))->sockets,&res);
247 static void gras_trp_sock_socket_close(gras_socket_t sock){
249 if (!sock) return; /* close only once */
251 VERB1("close tcp connection %d", sock->sd);
253 /* FIXME: no pipe in GRAS so far
254 if(!FD_ISSET(sd, &connectedPipes)) {
255 if(shutdown(sd, 2) < 0) {
258 ReleaseNWSLock(&lock);
260 / * The other side may have beaten us to the reset. * /
261 if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) {
262 WARN1("CloseSocket: shutdown error %d\n", tmp_errno);
268 /* close the socket */
269 if(tcp_close(sock->sd) < 0) {
270 WARN3("error while closing tcp socket %d: %d (%s)\n",
271 sock->sd, sock_errno, sock_errstr(sock_errno));
275 /************************************/
276 /****[ end of SOCKET MANAGEMENT ]****/
277 /************************************/
280 /************************************/
281 /****[ UNBUFFERED DATA EXCHANGE ]****/
282 /************************************/
283 /* Temptation to merge this with file data exchange is great,
284 but doesn't work on BillWare (see tcp_write() in portable.h) */
285 static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock,
287 unsigned long int size) {
292 status = tcp_write(sock->sd, data, (size_t)size);
293 DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
297 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
299 if (errno == EINTR || errno == EAGAIN)
303 THROW4(system_error,0,"write(%d,%p,%ld) failed: %s",
304 sock->sd, data, size,
305 sock_errstr(sock_errno));
312 THROW1(system_error,0,"file descriptor closed (%s)",
313 sock_errstr(sock_errno));
317 static XBT_INLINE int
318 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
320 unsigned long int size,
321 unsigned long int bufsize) {
328 DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
329 sock->sd, data+got, bufsize, got,
330 hexa_str((unsigned char*)data,got,0));
331 status = tcp_read(sock->sd, data+got, (size_t)bufsize);
334 THROW7(system_error,0,"read(%d,%p,%d) from %s:%d failed: %s; got %d so far",
335 sock->sd, data+got, (int)size,
336 gras_socket_peer_name(sock),gras_socket_peer_port(sock),
337 sock_errstr(sock_errno),
340 DEBUG2("Got %d more bytes (%s)",status,hexa_str((unsigned char*)data+got,status,0));
346 THROW1(system_error,0,"Socket closed by remote side (got %d bytes before this)",
354 static int gras_trp_tcp_recv(gras_socket_t sock,
356 unsigned long int size) {
357 return gras_trp_tcp_recv_withbuffer(sock,data,size,size);
360 /*******************************************/
361 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
362 /*******************************************/
364 /**********************************/
365 /****[ BUFFERED DATA EXCHANGE ]****/
366 /**********************************/
368 /* Make sure the data is sent */
370 gras_trp_bufiov_flush(gras_socket_t sock) {
375 gras_trp_bufdata_t *data=sock->bufdata;
379 if (data->out == buffering_buf) {
380 if (XBT_LOG_ISENABLED(gras_trp_tcp,xbt_log_priority_debug))
381 hexa_print("chunk to send ",
382 (unsigned char *) data->out_buf.data,data->out_buf.size);
383 if ((data->out_buf.size - data->out_buf.pos) != 0) {
384 DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size,
385 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
386 gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
387 VERB1("Chunk sent (size=%d)",data->out_buf.size);
388 data->out_buf.size = 0;
393 if (data->out == buffering_iov) {
394 DEBUG0("Flush out iov");
395 vect = sock->bufdata->out_buf_v;
396 if ((size = xbt_dynar_length(vect))) {
397 DEBUG1("Flush %d chunks out of this socket",size);
398 writev(sock->sd,xbt_dynar_get_ptr(vect,0),size);
399 xbt_dynar_reset(vect);
401 data->out_buf.size = 0; /* reset the buffer containing non-stable data */
404 if (data->in == buffering_iov) {
405 DEBUG0("Flush in iov");
406 vect = sock->bufdata->in_buf_v;
407 if ((size = xbt_dynar_length(vect))) {
408 DEBUG1("Get %d chunks from of this socket",size);
409 readv(sock->sd,xbt_dynar_get_ptr(vect,0),size);
410 xbt_dynar_reset(vect);
416 gras_trp_buf_send(gras_socket_t sock,
418 unsigned long int size,
419 int stable_ignored) {
421 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
426 while (chunk_pos < size) {
427 /* size of the chunk to receive in that shot */
428 long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size);
429 DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
430 (int)data->out_buf.size,
431 ((int)data->out_buf.size) + thissize -1,
433 hexa_str((unsigned char*)chunk,thissize,0));
435 memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
437 data->out_buf.size += thissize;
438 chunk_pos += thissize;
439 DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
440 data->out_buf.size,size-chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0));
442 if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
443 gras_trp_bufiov_flush(sock);
450 gras_trp_buf_recv(gras_socket_t sock,
452 unsigned long int size) {
454 gras_trp_bufdata_t *data=sock->bufdata;
455 long int chunk_pos = 0;
459 while (chunk_pos < size) {
460 /* size of the chunk to receive in that shot */
463 if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
465 DEBUG2("Get more data (size=%d,bufsize=%d)",
466 (int)MIN(size-chunk_pos,data->buffsize),
467 (int)data->buffsize);
471 gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
472 MIN(size-chunk_pos,data->buffsize),
478 thissize = min(size-chunk_pos , data->in_buf.size - data->in_buf.pos);
479 memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
481 data->in_buf.pos += thissize;
482 chunk_pos += thissize;
483 DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
484 data->in_buf.pos,size - chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0));
486 /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */
487 sock->moredata = (data->in_buf.size > data->in_buf.pos);
488 DEBUG1("There is %smore data",(sock->moredata?"":"no "));
494 /*****************************************/
495 /****[ end of BUFFERED DATA EXCHANGE ]****/
496 /*****************************************/
498 /********************************/
499 /****[ VECTOR DATA EXCHANGE ]****/
500 /********************************/
503 gras_trp_iov_send(gras_socket_t sock,
505 unsigned long int size,
508 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
511 DEBUG1("Buffer one chunk to be sent later (%s)",
512 hexa_str((char*)chunk,size,0));
514 elm.iov_len = (size_t)size;
517 /* data storage won't last until flush. Save it in a buffer if we can */
519 if (size > data->buffsize-data->out_buf.size) {
521 flush the socket, using data in its actual storage */
522 elm.iov_base = (void*)chunk;
523 xbt_dynar_push(data->out_buf_v,&elm);
525 gras_trp_bufiov_flush(sock);
528 /* buffer big enough:
529 copy data into it, and chain it for upcoming writev */
530 memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
531 elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size);
532 data->out_buf.size += size;
534 xbt_dynar_push(data->out_buf_v,&elm);
538 /* data storage stable. Chain it */
540 elm.iov_base = (void*)chunk;
541 xbt_dynar_push(data->out_buf_v,&elm);
545 gras_trp_iov_recv(gras_socket_t sock,
547 unsigned long int size) {
550 DEBUG0("Buffer one chunk to be received later");
551 elm.iov_base = (void*)chunk;
552 elm.iov_len = (size_t)size;
553 xbt_dynar_push(sock->bufdata->in_buf_v,&elm);
559 /***************************************/
560 /****[ end of VECTOR DATA EXCHANGE ]****/
561 /***************************************/
565 *** Prototypes of BUFFERED
568 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
570 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
572 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
574 void gras_trp_buf_socket_close(gras_socket_t sd);
577 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
578 gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
580 data->buffsize = 100 * 1024 ; /* 100k */
582 data->in_buf.size = 0;
583 data->in_buf.data = xbt_malloc(data->buffsize);
584 data->in_buf.pos = 0; /* useless, indeed, since size==pos */
586 data->out_buf.size = 0;
587 data->out_buf.data = xbt_malloc(data->buffsize);
588 data->out_buf.pos = data->out_buf.size;
591 data->in_buf_v = data->out_buf_v = NULL;
592 data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
593 data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
594 data->out = buffering_iov;
596 data->out = buffering_buf;
599 data->in = buffering_buf;
601 sock->bufdata = data;
609 gras_trp_tcp_setup(gras_trp_plugin_t plug) {
611 plug->socket_client = gras_trp_buf_socket_client;
612 plug->socket_server = gras_trp_buf_socket_server;
613 plug->socket_accept = gras_trp_buf_socket_accept;
614 plug->socket_close = gras_trp_buf_socket_close;
617 plug->send = gras_trp_iov_send;
619 plug->send = gras_trp_buf_send;
621 plug->recv = gras_trp_buf_recv;
623 plug->raw_send = gras_trp_tcp_send;
624 plug->raw_recv = gras_trp_tcp_recv;
626 plug->flush = gras_trp_bufiov_flush;
632 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
633 /* OUT */ gras_socket_t sock){
635 gras_trp_sock_socket_client(NULL,sock);
636 gras_trp_buf_init_sock(sock);
640 * gras_trp_buf_socket_server:
642 * Open a socket used to receive messages.
644 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
645 /* OUT */ gras_socket_t sock){
647 gras_trp_sock_socket_server(NULL,sock);
648 gras_trp_buf_init_sock(sock);
651 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock) {
652 return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
655 void gras_trp_buf_socket_close(gras_socket_t sock){
656 gras_trp_bufdata_t *data=sock->bufdata;
658 if (data->in_buf.size!=data->in_buf.pos) {
659 WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
660 data->in_buf.size - data->in_buf.pos,
661 data->in_buf.size, data->in_buf.pos);
663 if (data->in_buf.data)
664 free(data->in_buf.data);
666 if (data->out_buf.size!=data->out_buf.pos) {
667 DEBUG2("Flush the socket before closing (in=%d,out=%d)",
668 data->in_buf.size, data->out_buf.size);
669 gras_trp_bufiov_flush(sock);
671 if (data->out_buf.data)
672 free(data->out_buf.data);
675 if (data->in_buf_v) {
676 if (xbt_dynar_length(data->in_buf_v))
677 WARN0("Socket closed, but some bytes were unread");
678 xbt_dynar_free(&data->in_buf_v);
680 if (data->out_buf_v) {
681 if (xbt_dynar_length(data->out_buf_v)) {
682 DEBUG0("Flush the socket before closing");
683 gras_trp_bufiov_flush(sock);
685 xbt_dynar_free(&data->out_buf_v);
690 gras_trp_sock_socket_close(sock);
693 /****************************/
694 /****[ HELPER FUNCTIONS ]****/
695 /****************************/
698 * Returns the tcp protocol number from the network protocol data base.
700 * getprotobyname() is not thread safe. We need to lock it.
702 static int _gras_tcp_proto_number(void) {
703 struct protoent *fetchedEntry;
704 static int returnValue = 0;
706 if(returnValue == 0) {
707 fetchedEntry = getprotobyname("tcp");
708 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
709 returnValue = fetchedEntry->p_proto;
715 #ifdef HAVE_WINSOCK_H
716 #define RETSTR( x ) case x: return #x
718 const char *gras_wsa_err2string( int err ) {
726 RETSTR( WSAEWOULDBLOCK );
727 RETSTR( WSAEINPROGRESS );
728 RETSTR( WSAEALREADY );
729 RETSTR( WSAENOTSOCK );
730 RETSTR( WSAEDESTADDRREQ );
731 RETSTR( WSAEMSGSIZE );
732 RETSTR( WSAEPROTOTYPE );
733 RETSTR( WSAENOPROTOOPT );
734 RETSTR( WSAEPROTONOSUPPORT );
735 RETSTR( WSAESOCKTNOSUPPORT );
736 RETSTR( WSAEOPNOTSUPP );
737 RETSTR( WSAEPFNOSUPPORT );
738 RETSTR( WSAEAFNOSUPPORT );
739 RETSTR( WSAEADDRINUSE );
740 RETSTR( WSAEADDRNOTAVAIL );
741 RETSTR( WSAENETDOWN );
742 RETSTR( WSAENETUNREACH );
743 RETSTR( WSAENETRESET );
744 RETSTR( WSAECONNABORTED );
745 RETSTR( WSAECONNRESET );
746 RETSTR( WSAENOBUFS );
747 RETSTR( WSAEISCONN );
748 RETSTR( WSAENOTCONN );
749 RETSTR( WSAESHUTDOWN );
750 RETSTR( WSAETOOMANYREFS );
751 RETSTR( WSAETIMEDOUT );
752 RETSTR( WSAECONNREFUSED );
754 RETSTR( WSAENAMETOOLONG );
755 RETSTR( WSAEHOSTDOWN );
756 RETSTR( WSAEHOSTUNREACH );
757 RETSTR( WSAENOTEMPTY );
758 RETSTR( WSAEPROCLIM );
762 RETSTR( WSAEREMOTE );
763 RETSTR( WSASYSNOTREADY );
764 RETSTR( WSAVERNOTSUPPORTED );
765 RETSTR( WSANOTINITIALISED );
766 RETSTR( WSAEDISCON );
769 RETSTR( WSAENOMORE );
770 RETSTR( WSAECANCELLED );
771 RETSTR( WSAEINVALIDPROCTABLE );
772 RETSTR( WSAEINVALIDPROVIDER );
773 RETSTR( WSASYSCALLFAILURE );
774 RETSTR( WSASERVICE_NOT_FOUND );
775 RETSTR( WSATYPE_NOT_FOUND );
776 RETSTR( WSA_E_NO_MORE );
777 RETSTR( WSA_E_CANCELLED );
778 RETSTR( WSAEREFUSED );
779 #endif /* HAVE_WINSOCK2 */
781 RETSTR( WSAHOST_NOT_FOUND );
782 RETSTR( WSATRY_AGAIN );
783 RETSTR( WSANO_RECOVERY );
784 RETSTR( WSANO_DATA );
786 return "unknown WSA error";
788 #endif /* HAVE_WINSOCK_H */
790 /***********************************/
791 /****[ end of HELPER FUNCTIONS ]****/
792 /***********************************/