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,"setsockopt failed, cannot condition the socket: %s",
144 sock_errstr(sock_errno));
146 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
147 setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
148 WARN1("setsockopt failed, cannot set buffer size: %s",
149 sock_errstr(sock_errno));
152 if (bind(sock->sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
154 THROW2(system_error,0,"Cannot bind to port %d: %s",sock->port, sock_errstr(sock_errno));
157 DEBUG2("Listen on port %d (sd=%d)",sock->port, sock->sd);
158 if (listen(sock->sd, 5) < 0) {
160 THROW2(system_error,0,"Cannot listen on port %d: %s",sock->port,sock_errstr(sock_errno));
163 VERB2("Openned a server socket on port %d (sd=%d)",sock->port,sock->sd);
166 static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) {
169 struct sockaddr_in peer_in;
170 socklen_t peer_in_len = sizeof(peer_in);
177 socklen_t s = sizeof(int);
182 gras_trp_socket_new(1,&res);
184 sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
185 tmp_errno = sock_errno;
188 gras_socket_close(sock);
189 THROW1(system_error,0,
190 "Accept failed (%s). Droping server socket.", sock_errstr(tmp_errno));
193 if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s)
194 || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *)&i, s))
195 THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
196 sock_errstr(tmp_errno));
198 res->buf_size = sock->buf_size;
199 size = sock->buf_size;
200 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size))
201 || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size)))
202 WARN1("setsockopt failed, cannot set buffer size: %s", sock_errstr(tmp_errno));
204 res->plugin = sock->plugin;
205 res->incoming = sock->incoming;
206 res->outgoing = sock->outgoing;
211 gras_trp_tcp_recv(res,(char*)&hisport,sizeof(hisport));
212 res->peer_port = ntohl(hisport);
213 DEBUG1("peerport %d received",res->peer_port);
215 /* FIXME: Lock to protect inet_ntoa */
216 if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) {
217 res->peer_name = (char*)strdup("unknown");
219 struct in_addr addrAsInAddr;
222 addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
224 tmp = inet_ntoa(addrAsInAddr);
226 res->peer_name = (char*)strdup(tmp);
228 res->peer_name = (char*)strdup("unknown");
232 VERB3("Accepted from %s:%d (sd=%d)", res->peer_name,res->peer_port,sd);
238 static void gras_trp_sock_socket_close(gras_socket_t sock){
240 if (!sock) return; /* close only once */
242 VERB1("close tcp connection %d", sock->sd);
244 /* FIXME: no pipe in GRAS so far
245 if(!FD_ISSET(sd, &connectedPipes)) {
246 if(shutdown(sd, 2) < 0) {
249 ReleaseNWSLock(&lock);
251 / * The other side may have beaten us to the reset. * /
252 if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) {
253 WARN1("CloseSocket: shutdown error %d\n", tmp_errno);
259 /* close the socket */
260 if(tcp_close(sock->sd) < 0) {
261 WARN3("error while closing tcp socket %d: %d (%s)\n",
262 sock->sd, sock_errno, sock_errstr(sock_errno));
266 /************************************/
267 /****[ end of SOCKET MANAGEMENT ]****/
268 /************************************/
271 /************************************/
272 /****[ UNBUFFERED DATA EXCHANGE ]****/
273 /************************************/
274 /* Temptation to merge this with file data exchange is great,
275 but doesn't work on BillWare (see tcp_write() in portable.h) */
276 static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock,
278 unsigned long int size) {
283 status = tcp_write(sock->sd, data, (size_t)size);
284 DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
288 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
290 if (errno == EINTR || errno == EAGAIN)
294 THROW4(system_error,0,"write(%d,%p,%ld) failed: %s",
295 sock->sd, data, size,
296 sock_errstr(sock_errno));
303 THROW1(system_error,0,"file descriptor closed (%s)",
304 sock_errstr(sock_errno));
308 static XBT_INLINE int
309 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
311 unsigned long int size,
312 unsigned long int bufsize) {
319 DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
320 sock->sd, data+got, bufsize, got,
321 hexa_str((unsigned char*)data,got,0));
322 status = tcp_read(sock->sd, data+got, (size_t)bufsize);
325 THROW7(system_error,0,"read(%d,%p,%d) from %s:%d failed: %s; got %d so far",
326 sock->sd, data+got, (int)size,
327 gras_socket_peer_name(sock),gras_socket_peer_port(sock),
328 sock_errstr(sock_errno),
331 DEBUG2("Got %d more bytes (%s)",status,hexa_str((unsigned char*)data+got,status,0));
337 THROW1(system_error,0,"Socket closed by remote side (got %d bytes before this)",
345 static int gras_trp_tcp_recv(gras_socket_t sock,
347 unsigned long int size) {
348 return gras_trp_tcp_recv_withbuffer(sock,data,size,size);
351 /*******************************************/
352 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
353 /*******************************************/
355 /**********************************/
356 /****[ BUFFERED DATA EXCHANGE ]****/
357 /**********************************/
359 /* Make sure the data is sent */
361 gras_trp_bufiov_flush(gras_socket_t sock) {
366 gras_trp_bufdata_t *data=sock->bufdata;
370 if (data->out == buffering_buf) {
371 if (XBT_LOG_ISENABLED(gras_trp_tcp,xbt_log_priority_debug))
372 hexa_print("chunk to send ",
373 (unsigned char *) data->out_buf.data,data->out_buf.size);
374 if ((data->out_buf.size - data->out_buf.pos) != 0) {
375 DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size,
376 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
377 gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
378 VERB1("Chunk sent (size=%d)",data->out_buf.size);
379 data->out_buf.size = 0;
384 if (data->out == buffering_iov) {
385 DEBUG0("Flush out iov");
386 vect = sock->bufdata->out_buf_v;
387 if ((size = xbt_dynar_length(vect))) {
388 DEBUG1("Flush %d chunks out of this socket",size);
389 writev(sock->sd,xbt_dynar_get_ptr(vect,0),size);
390 xbt_dynar_reset(vect);
392 data->out_buf.size = 0; /* reset the buffer containing non-stable data */
395 if (data->in == buffering_iov) {
396 DEBUG0("Flush in iov");
397 vect = sock->bufdata->in_buf_v;
398 if ((size = xbt_dynar_length(vect))) {
399 DEBUG1("Get %d chunks from of this socket",size);
400 readv(sock->sd,xbt_dynar_get_ptr(vect,0),size);
401 xbt_dynar_reset(vect);
407 gras_trp_buf_send(gras_socket_t sock,
409 unsigned long int size,
410 int stable_ignored) {
412 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
417 while (chunk_pos < size) {
418 /* size of the chunk to receive in that shot */
419 long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size);
420 DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
421 (int)data->out_buf.size,
422 ((int)data->out_buf.size) + thissize -1,
424 hexa_str((unsigned char*)chunk,thissize,0));
426 memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
428 data->out_buf.size += thissize;
429 chunk_pos += thissize;
430 DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
431 data->out_buf.size,size-chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0));
433 if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
434 gras_trp_bufiov_flush(sock);
441 gras_trp_buf_recv(gras_socket_t sock,
443 unsigned long int size) {
445 gras_trp_bufdata_t *data=sock->bufdata;
446 long int chunk_pos = 0;
450 while (chunk_pos < size) {
451 /* size of the chunk to receive in that shot */
454 if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
456 DEBUG2("Get more data (size=%d,bufsize=%d)",
457 (int)MIN(size-chunk_pos,data->buffsize),
458 (int)data->buffsize);
462 gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
463 MIN(size-chunk_pos,data->buffsize),
469 thissize = min(size-chunk_pos , data->in_buf.size - data->in_buf.pos);
470 memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
472 data->in_buf.pos += thissize;
473 chunk_pos += thissize;
474 DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
475 data->in_buf.pos,size - chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0));
477 /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */
478 sock->moredata = (data->in_buf.size > data->in_buf.pos);
479 DEBUG1("There is %smore data",(sock->moredata?"":"no "));
485 /*****************************************/
486 /****[ end of BUFFERED DATA EXCHANGE ]****/
487 /*****************************************/
489 /********************************/
490 /****[ VECTOR DATA EXCHANGE ]****/
491 /********************************/
494 gras_trp_iov_send(gras_socket_t sock,
496 unsigned long int size,
499 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
502 DEBUG1("Buffer one chunk to be sent later (%s)",
503 hexa_str((char*)chunk,size,0));
505 elm.iov_len = (size_t)size;
508 /* data storage won't last until flush. Save it in a buffer if we can */
510 if (size > data->buffsize-data->out_buf.size) {
512 flush the socket, using data in its actual storage */
513 elm.iov_base = (void*)chunk;
514 xbt_dynar_push(data->out_buf_v,&elm);
516 gras_trp_bufiov_flush(sock);
519 /* buffer big enough:
520 copy data into it, and chain it for upcoming writev */
521 memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
522 elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size);
523 data->out_buf.size += size;
525 xbt_dynar_push(data->out_buf_v,&elm);
529 /* data storage stable. Chain it */
531 elm.iov_base = (void*)chunk;
532 xbt_dynar_push(data->out_buf_v,&elm);
536 gras_trp_iov_recv(gras_socket_t sock,
538 unsigned long int size) {
541 DEBUG0("Buffer one chunk to be received later");
542 elm.iov_base = (void*)chunk;
543 elm.iov_len = (size_t)size;
544 xbt_dynar_push(sock->bufdata->in_buf_v,&elm);
550 /***************************************/
551 /****[ end of VECTOR DATA EXCHANGE ]****/
552 /***************************************/
556 *** Prototypes of BUFFERED
559 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
561 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
563 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
565 void gras_trp_buf_socket_close(gras_socket_t sd);
568 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
569 gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
571 data->buffsize = 100 * 1024 ; /* 100k */
573 data->in_buf.size = 0;
574 data->in_buf.data = xbt_malloc(data->buffsize);
575 data->in_buf.pos = 0; /* useless, indeed, since size==pos */
577 data->out_buf.size = 0;
578 data->out_buf.data = xbt_malloc(data->buffsize);
579 data->out_buf.pos = data->out_buf.size;
582 data->in_buf_v = data->out_buf_v = NULL;
583 data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
584 data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
585 data->out = buffering_iov;
587 data->out = buffering_buf;
590 data->in = buffering_buf;
592 sock->bufdata = data;
600 gras_trp_tcp_setup(gras_trp_plugin_t plug) {
602 plug->socket_client = gras_trp_buf_socket_client;
603 plug->socket_server = gras_trp_buf_socket_server;
604 plug->socket_accept = gras_trp_buf_socket_accept;
605 plug->socket_close = gras_trp_buf_socket_close;
608 plug->send = gras_trp_iov_send;
610 plug->send = gras_trp_buf_send;
612 plug->recv = gras_trp_buf_recv;
614 plug->raw_send = gras_trp_tcp_send;
615 plug->raw_recv = gras_trp_tcp_recv;
617 plug->flush = gras_trp_bufiov_flush;
623 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
624 /* OUT */ gras_socket_t sock){
626 gras_trp_sock_socket_client(NULL,sock);
627 gras_trp_buf_init_sock(sock);
631 * gras_trp_buf_socket_server:
633 * Open a socket used to receive messages.
635 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
636 /* OUT */ gras_socket_t sock){
638 gras_trp_sock_socket_server(NULL,sock);
639 gras_trp_buf_init_sock(sock);
642 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock) {
643 return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
646 void gras_trp_buf_socket_close(gras_socket_t sock){
647 gras_trp_bufdata_t *data=sock->bufdata;
649 if (data->in_buf.size!=data->in_buf.pos) {
650 WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
651 data->in_buf.size - data->in_buf.pos,
652 data->in_buf.size, data->in_buf.pos);
654 if (data->in_buf.data)
655 free(data->in_buf.data);
657 if (data->out_buf.size!=data->out_buf.pos) {
658 DEBUG2("Flush the socket before closing (in=%d,out=%d)",
659 data->in_buf.size, data->out_buf.size);
660 gras_trp_bufiov_flush(sock);
662 if (data->out_buf.data)
663 free(data->out_buf.data);
666 if (data->in_buf_v) {
667 if (xbt_dynar_length(data->in_buf_v))
668 WARN0("Socket closed, but some bytes were unread");
669 xbt_dynar_free(&data->in_buf_v);
671 if (data->out_buf_v) {
672 if (xbt_dynar_length(data->out_buf_v)) {
673 DEBUG0("Flush the socket before closing");
674 gras_trp_bufiov_flush(sock);
676 xbt_dynar_free(&data->out_buf_v);
681 gras_trp_sock_socket_close(sock);
684 /****************************/
685 /****[ HELPER FUNCTIONS ]****/
686 /****************************/
689 * Returns the tcp protocol number from the network protocol data base.
691 * getprotobyname() is not thread safe. We need to lock it.
693 static int _gras_tcp_proto_number(void) {
694 struct protoent *fetchedEntry;
695 static int returnValue = 0;
697 if(returnValue == 0) {
698 fetchedEntry = getprotobyname("tcp");
699 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
700 returnValue = fetchedEntry->p_proto;
706 #ifdef HAVE_WINSOCK_H
707 #define RETSTR( x ) case x: return #x
709 const char *gras_wsa_err2string( int err ) {
717 RETSTR( WSAEWOULDBLOCK );
718 RETSTR( WSAEINPROGRESS );
719 RETSTR( WSAEALREADY );
720 RETSTR( WSAENOTSOCK );
721 RETSTR( WSAEDESTADDRREQ );
722 RETSTR( WSAEMSGSIZE );
723 RETSTR( WSAEPROTOTYPE );
724 RETSTR( WSAENOPROTOOPT );
725 RETSTR( WSAEPROTONOSUPPORT );
726 RETSTR( WSAESOCKTNOSUPPORT );
727 RETSTR( WSAEOPNOTSUPP );
728 RETSTR( WSAEPFNOSUPPORT );
729 RETSTR( WSAEAFNOSUPPORT );
730 RETSTR( WSAEADDRINUSE );
731 RETSTR( WSAEADDRNOTAVAIL );
732 RETSTR( WSAENETDOWN );
733 RETSTR( WSAENETUNREACH );
734 RETSTR( WSAENETRESET );
735 RETSTR( WSAECONNABORTED );
736 RETSTR( WSAECONNRESET );
737 RETSTR( WSAENOBUFS );
738 RETSTR( WSAEISCONN );
739 RETSTR( WSAENOTCONN );
740 RETSTR( WSAESHUTDOWN );
741 RETSTR( WSAETOOMANYREFS );
742 RETSTR( WSAETIMEDOUT );
743 RETSTR( WSAECONNREFUSED );
745 RETSTR( WSAENAMETOOLONG );
746 RETSTR( WSAEHOSTDOWN );
747 RETSTR( WSAEHOSTUNREACH );
748 RETSTR( WSAENOTEMPTY );
749 RETSTR( WSAEPROCLIM );
753 RETSTR( WSAEREMOTE );
754 RETSTR( WSASYSNOTREADY );
755 RETSTR( WSAVERNOTSUPPORTED );
756 RETSTR( WSANOTINITIALISED );
757 RETSTR( WSAEDISCON );
760 RETSTR( WSAENOMORE );
761 RETSTR( WSAECANCELLED );
762 RETSTR( WSAEINVALIDPROCTABLE );
763 RETSTR( WSAEINVALIDPROVIDER );
764 RETSTR( WSASYSCALLFAILURE );
765 RETSTR( WSASERVICE_NOT_FOUND );
766 RETSTR( WSATYPE_NOT_FOUND );
767 RETSTR( WSA_E_NO_MORE );
768 RETSTR( WSA_E_CANCELLED );
769 RETSTR( WSAEREFUSED );
770 #endif /* HAVE_WINSOCK2 */
772 RETSTR( WSAHOST_NOT_FOUND );
773 RETSTR( WSATRY_AGAIN );
774 RETSTR( WSANO_RECOVERY );
775 RETSTR( WSANO_DATA );
777 return "unknown WSA error";
779 #endif /* HAVE_WINSOCK_H */
781 /***********************************/
782 /****[ end of HELPER FUNCTIONS ]****/
783 /***********************************/