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);
245 static void gras_trp_sock_socket_close(gras_socket_t sock){
247 if (!sock) return; /* close only once */
249 VERB1("close tcp connection %d", sock->sd);
251 /* FIXME: no pipe in GRAS so far
252 if(!FD_ISSET(sd, &connectedPipes)) {
253 if(shutdown(sd, 2) < 0) {
256 ReleaseNWSLock(&lock);
258 / * The other side may have beaten us to the reset. * /
259 if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) {
260 WARN1("CloseSocket: shutdown error %d\n", tmp_errno);
266 /* close the socket */
267 if(tcp_close(sock->sd) < 0) {
268 WARN3("error while closing tcp socket %d: %d (%s)\n",
269 sock->sd, sock_errno, sock_errstr(sock_errno));
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) {
290 status = tcp_write(sock->sd, data, (size_t)size);
291 DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
295 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
297 if (errno == EINTR || errno == EAGAIN)
301 THROW4(system_error,0,"write(%d,%p,%ld) failed: %s",
302 sock->sd, data, size,
303 sock_errstr(sock_errno));
310 THROW1(system_error,0,"file descriptor closed (%s)",
311 sock_errstr(sock_errno));
315 static XBT_INLINE int
316 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
318 unsigned long int size,
319 unsigned long int bufsize) {
326 DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
327 sock->sd, data+got, bufsize, got,
328 hexa_str((unsigned char*)data,got,0));
329 status = tcp_read(sock->sd, data+got, (size_t)bufsize);
332 THROW7(system_error,0,"read(%d,%p,%d) from %s:%d failed: %s; got %d so far",
333 sock->sd, data+got, (int)size,
334 gras_socket_peer_name(sock),gras_socket_peer_port(sock),
335 sock_errstr(sock_errno),
338 DEBUG2("Got %d more bytes (%s)",status,hexa_str((unsigned char*)data+got,status,0));
344 THROW1(system_error,0,"Socket closed by remote side (got %d bytes before this)",
352 static int gras_trp_tcp_recv(gras_socket_t sock,
354 unsigned long int size) {
355 return gras_trp_tcp_recv_withbuffer(sock,data,size,size);
358 /*******************************************/
359 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
360 /*******************************************/
362 /**********************************/
363 /****[ BUFFERED DATA EXCHANGE ]****/
364 /**********************************/
366 /* Make sure the data is sent */
368 gras_trp_bufiov_flush(gras_socket_t sock) {
373 gras_trp_bufdata_t *data=sock->bufdata;
377 if (data->out == buffering_buf) {
378 if (XBT_LOG_ISENABLED(gras_trp_tcp,xbt_log_priority_debug))
379 hexa_print("chunk to send ",
380 (unsigned char *) data->out_buf.data,data->out_buf.size);
381 if ((data->out_buf.size - data->out_buf.pos) != 0) {
382 DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size,
383 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
384 gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
385 VERB1("Chunk sent (size=%d)",data->out_buf.size);
386 data->out_buf.size = 0;
391 if (data->out == buffering_iov) {
392 DEBUG0("Flush out iov");
393 vect = sock->bufdata->out_buf_v;
394 if ((size = xbt_dynar_length(vect))) {
395 DEBUG1("Flush %d chunks out of this socket",size);
396 writev(sock->sd,xbt_dynar_get_ptr(vect,0),size);
397 xbt_dynar_reset(vect);
399 data->out_buf.size = 0; /* reset the buffer containing non-stable data */
402 if (data->in == buffering_iov) {
403 DEBUG0("Flush in iov");
404 vect = sock->bufdata->in_buf_v;
405 if ((size = xbt_dynar_length(vect))) {
406 DEBUG1("Get %d chunks from of this socket",size);
407 readv(sock->sd,xbt_dynar_get_ptr(vect,0),size);
408 xbt_dynar_reset(vect);
414 gras_trp_buf_send(gras_socket_t sock,
416 unsigned long int size,
417 int stable_ignored) {
419 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
424 while (chunk_pos < size) {
425 /* size of the chunk to receive in that shot */
426 long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size);
427 DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
428 (int)data->out_buf.size,
429 ((int)data->out_buf.size) + thissize -1,
431 hexa_str((unsigned char*)chunk,thissize,0));
433 memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
435 data->out_buf.size += thissize;
436 chunk_pos += thissize;
437 DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
438 data->out_buf.size,size-chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0));
440 if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
441 gras_trp_bufiov_flush(sock);
448 gras_trp_buf_recv(gras_socket_t sock,
450 unsigned long int size) {
452 gras_trp_bufdata_t *data=sock->bufdata;
453 long int chunk_pos = 0;
457 while (chunk_pos < size) {
458 /* size of the chunk to receive in that shot */
461 if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
463 DEBUG2("Get more data (size=%d,bufsize=%d)",
464 (int)MIN(size-chunk_pos,data->buffsize),
465 (int)data->buffsize);
469 gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
470 MIN(size-chunk_pos,data->buffsize),
476 thissize = min(size-chunk_pos , data->in_buf.size - data->in_buf.pos);
477 memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
479 data->in_buf.pos += thissize;
480 chunk_pos += thissize;
481 DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
482 data->in_buf.pos,size - chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0));
484 /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */
485 sock->moredata = (data->in_buf.size > data->in_buf.pos);
486 DEBUG1("There is %smore data",(sock->moredata?"":"no "));
492 /*****************************************/
493 /****[ end of BUFFERED DATA EXCHANGE ]****/
494 /*****************************************/
496 /********************************/
497 /****[ VECTOR DATA EXCHANGE ]****/
498 /********************************/
501 gras_trp_iov_send(gras_socket_t sock,
503 unsigned long int size,
506 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
509 DEBUG1("Buffer one chunk to be sent later (%s)",
510 hexa_str((char*)chunk,size,0));
512 elm.iov_len = (size_t)size;
515 /* data storage won't last until flush. Save it in a buffer if we can */
517 if (size > data->buffsize-data->out_buf.size) {
519 flush the socket, using data in its actual storage */
520 elm.iov_base = (void*)chunk;
521 xbt_dynar_push(data->out_buf_v,&elm);
523 gras_trp_bufiov_flush(sock);
526 /* buffer big enough:
527 copy data into it, and chain it for upcoming writev */
528 memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
529 elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size);
530 data->out_buf.size += size;
532 xbt_dynar_push(data->out_buf_v,&elm);
536 /* data storage stable. Chain it */
538 elm.iov_base = (void*)chunk;
539 xbt_dynar_push(data->out_buf_v,&elm);
543 gras_trp_iov_recv(gras_socket_t sock,
545 unsigned long int size) {
548 DEBUG0("Buffer one chunk to be received later");
549 elm.iov_base = (void*)chunk;
550 elm.iov_len = (size_t)size;
551 xbt_dynar_push(sock->bufdata->in_buf_v,&elm);
557 /***************************************/
558 /****[ end of VECTOR DATA EXCHANGE ]****/
559 /***************************************/
563 *** Prototypes of BUFFERED
566 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
568 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
570 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
572 void gras_trp_buf_socket_close(gras_socket_t sd);
575 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
576 gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
578 data->buffsize = 100 * 1024 ; /* 100k */
580 data->in_buf.size = 0;
581 data->in_buf.data = xbt_malloc(data->buffsize);
582 data->in_buf.pos = 0; /* useless, indeed, since size==pos */
584 data->out_buf.size = 0;
585 data->out_buf.data = xbt_malloc(data->buffsize);
586 data->out_buf.pos = data->out_buf.size;
589 data->in_buf_v = data->out_buf_v = NULL;
590 data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
591 data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
592 data->out = buffering_iov;
594 data->out = buffering_buf;
597 data->in = buffering_buf;
599 sock->bufdata = data;
607 gras_trp_tcp_setup(gras_trp_plugin_t plug) {
609 plug->socket_client = gras_trp_buf_socket_client;
610 plug->socket_server = gras_trp_buf_socket_server;
611 plug->socket_accept = gras_trp_buf_socket_accept;
612 plug->socket_close = gras_trp_buf_socket_close;
615 plug->send = gras_trp_iov_send;
617 plug->send = gras_trp_buf_send;
619 plug->recv = gras_trp_buf_recv;
621 plug->raw_send = gras_trp_tcp_send;
622 plug->raw_recv = gras_trp_tcp_recv;
624 plug->flush = gras_trp_bufiov_flush;
630 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
631 /* OUT */ gras_socket_t sock){
633 gras_trp_sock_socket_client(NULL,sock);
634 gras_trp_buf_init_sock(sock);
638 * gras_trp_buf_socket_server:
640 * Open a socket used to receive messages.
642 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
643 /* OUT */ gras_socket_t sock){
645 gras_trp_sock_socket_server(NULL,sock);
646 gras_trp_buf_init_sock(sock);
649 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock) {
650 return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
653 void gras_trp_buf_socket_close(gras_socket_t sock){
654 gras_trp_bufdata_t *data=sock->bufdata;
656 if (data->in_buf.size!=data->in_buf.pos) {
657 WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
658 data->in_buf.size - data->in_buf.pos,
659 data->in_buf.size, data->in_buf.pos);
661 if (data->in_buf.data)
662 free(data->in_buf.data);
664 if (data->out_buf.size!=data->out_buf.pos) {
665 DEBUG2("Flush the socket before closing (in=%d,out=%d)",
666 data->in_buf.size, data->out_buf.size);
667 gras_trp_bufiov_flush(sock);
669 if (data->out_buf.data)
670 free(data->out_buf.data);
673 if (data->in_buf_v) {
674 if (xbt_dynar_length(data->in_buf_v))
675 WARN0("Socket closed, but some bytes were unread");
676 xbt_dynar_free(&data->in_buf_v);
678 if (data->out_buf_v) {
679 if (xbt_dynar_length(data->out_buf_v)) {
680 DEBUG0("Flush the socket before closing");
681 gras_trp_bufiov_flush(sock);
683 xbt_dynar_free(&data->out_buf_v);
688 gras_trp_sock_socket_close(sock);
691 /****************************/
692 /****[ HELPER FUNCTIONS ]****/
693 /****************************/
696 * Returns the tcp protocol number from the network protocol data base.
698 * getprotobyname() is not thread safe. We need to lock it.
700 static int _gras_tcp_proto_number(void) {
701 struct protoent *fetchedEntry;
702 static int returnValue = 0;
704 if(returnValue == 0) {
705 fetchedEntry = getprotobyname("tcp");
706 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
707 returnValue = fetchedEntry->p_proto;
713 #ifdef HAVE_WINSOCK_H
714 #define RETSTR( x ) case x: return #x
716 const char *gras_wsa_err2string( int err ) {
724 RETSTR( WSAEWOULDBLOCK );
725 RETSTR( WSAEINPROGRESS );
726 RETSTR( WSAEALREADY );
727 RETSTR( WSAENOTSOCK );
728 RETSTR( WSAEDESTADDRREQ );
729 RETSTR( WSAEMSGSIZE );
730 RETSTR( WSAEPROTOTYPE );
731 RETSTR( WSAENOPROTOOPT );
732 RETSTR( WSAEPROTONOSUPPORT );
733 RETSTR( WSAESOCKTNOSUPPORT );
734 RETSTR( WSAEOPNOTSUPP );
735 RETSTR( WSAEPFNOSUPPORT );
736 RETSTR( WSAEAFNOSUPPORT );
737 RETSTR( WSAEADDRINUSE );
738 RETSTR( WSAEADDRNOTAVAIL );
739 RETSTR( WSAENETDOWN );
740 RETSTR( WSAENETUNREACH );
741 RETSTR( WSAENETRESET );
742 RETSTR( WSAECONNABORTED );
743 RETSTR( WSAECONNRESET );
744 RETSTR( WSAENOBUFS );
745 RETSTR( WSAEISCONN );
746 RETSTR( WSAENOTCONN );
747 RETSTR( WSAESHUTDOWN );
748 RETSTR( WSAETOOMANYREFS );
749 RETSTR( WSAETIMEDOUT );
750 RETSTR( WSAECONNREFUSED );
752 RETSTR( WSAENAMETOOLONG );
753 RETSTR( WSAEHOSTDOWN );
754 RETSTR( WSAEHOSTUNREACH );
755 RETSTR( WSAENOTEMPTY );
756 RETSTR( WSAEPROCLIM );
760 RETSTR( WSAEREMOTE );
761 RETSTR( WSASYSNOTREADY );
762 RETSTR( WSAVERNOTSUPPORTED );
763 RETSTR( WSANOTINITIALISED );
764 RETSTR( WSAEDISCON );
767 RETSTR( WSAENOMORE );
768 RETSTR( WSAECANCELLED );
769 RETSTR( WSAEINVALIDPROCTABLE );
770 RETSTR( WSAEINVALIDPROVIDER );
771 RETSTR( WSASYSCALLFAILURE );
772 RETSTR( WSASERVICE_NOT_FOUND );
773 RETSTR( WSATYPE_NOT_FOUND );
774 RETSTR( WSA_E_NO_MORE );
775 RETSTR( WSA_E_CANCELLED );
776 RETSTR( WSAEREFUSED );
777 #endif /* HAVE_WINSOCK2 */
779 RETSTR( WSAHOST_NOT_FOUND );
780 RETSTR( WSATRY_AGAIN );
781 RETSTR( WSANO_RECOVERY );
782 RETSTR( WSANO_DATA );
784 return "unknown WSA error";
786 #endif /* HAVE_WINSOCK_H */
788 /***********************************/
789 /****[ end of HELPER FUNCTIONS ]****/
790 /***********************************/