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 "transport_private.h"
26 #define MIN(a,b) ((a)<(b)?(a):(b))
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_tcp,transport,
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 static int _gras_tcp_proto_number(void);
65 static inline void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
68 struct sockaddr_in addr;
70 struct in_addr *haddr;
71 int size = sock->bufSize * 1024;
73 sock->incoming = 1; /* TCP sockets are duplex'ed */
75 sock->sd = socket (AF_INET, SOCK_STREAM, 0);
78 THROW1(system_error,0, "Failed to create socket: %s", sock_errstr);
81 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
82 setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
83 WARN1("setsockopt failed, cannot set buffer size: %s",sock_errstr);
86 he = gethostbyname (sock->peer_name);
88 THROW2(system_error,0, "Failed to lookup hostname %s: %s",
89 sock->peer_name, sock_errstr);
92 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
94 memset(&addr, 0, sizeof(struct sockaddr_in));
95 memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
96 addr.sin_family = AF_INET;
97 addr.sin_port = htons (sock->peer_port);
99 if (connect (sock->sd, (struct sockaddr*) &addr, sizeof (addr)) < 0) {
101 THROW3(system_error,0,
102 "Failed to connect socket to %s:%d (%s)",
103 sock->peer_name, sock->peer_port, sock_errstr);
105 VERB4("Connect to %s:%d (sd=%d, port %d here)",
106 sock->peer_name, sock->peer_port, sock->sd, sock->port);
110 * gras_trp_sock_socket_server:
112 * Open a socket used to receive messages.
114 static inline void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
116 int size = sock->bufSize * 1024;
118 struct sockaddr_in server;
120 sock->outgoing = 1; /* TCP => duplex mode */
122 server.sin_port = htons((u_short)sock->port);
123 server.sin_addr.s_addr = INADDR_ANY;
124 server.sin_family = AF_INET;
125 if((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
126 THROW1(system_error,0,"Socket allocation failed: %s", sock_errstr);
128 if (setsockopt(sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)))
129 THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
132 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
133 setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
134 WARN1("setsockopt failed, cannot set buffer size: %s",
138 if (bind(sock->sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
140 THROW2(system_error,0,"Cannot bind to port %d: %s",sock->port, sock_errstr);
143 DEBUG2("Listen on port %d (sd=%d)",sock->port, sock->sd);
144 if (listen(sock->sd, 5) < 0) {
146 THROW2(system_error,0,"Cannot listen on port %d: %s",sock->port,sock_errstr);
149 VERB2("Openned a server socket on port %d (sd=%d)",sock->port,sock->sd);
152 static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) {
155 struct sockaddr_in peer_in;
156 socklen_t peer_in_len = sizeof(peer_in);
163 socklen_t s = sizeof(int);
166 gras_trp_socket_new(1,&res);
168 sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
172 gras_socket_close(sock);
173 THROW1(system_error,0,
174 "Accept failed (%s). Droping server socket.", sock_errstr);
177 if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s)
178 || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *)&i, s))
179 THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
182 res->bufSize = sock->bufSize;
183 size = sock->bufSize * 1024;
184 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size))
185 || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size)))
186 WARN1("setsockopt failed, cannot set buffer size: %s", sock_errstr);
188 res->plugin = sock->plugin;
189 res->incoming = sock->incoming;
190 res->outgoing = sock->outgoing;
194 res->peer_port = peer_in.sin_port;
196 /* FIXME: Lock to protect inet_ntoa */
197 if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) {
198 res->peer_name = (char*)strdup("unknown");
200 struct in_addr addrAsInAddr;
203 addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
205 tmp = inet_ntoa(addrAsInAddr);
207 res->peer_name = (char*)strdup(tmp);
209 res->peer_name = (char*)strdup("unknown");
213 VERB3("Accepted from %s:%d (sd=%d)", res->peer_name,res->peer_port,sd);
219 static void gras_trp_sock_socket_close(gras_socket_t sock){
221 if (!sock) return; /* close only once */
223 VERB1("close tcp connection %d", sock->sd);
225 /* FIXME: no pipe in GRAS so far
226 if(!FD_ISSET(sd, &connectedPipes)) {
227 if(shutdown(sd, 2) < 0) {
230 ReleaseNWSLock(&lock);
232 / * The other side may have beaten us to the reset. * /
233 if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) {
234 WARN1("CloseSocket: shutdown error %d\n", tmp_errno);
240 /* close the socket */
241 if(tcp_close(sock->sd) < 0) {
242 WARN3("error while closing tcp socket %d: %d (%s)\n",
243 sock->sd, sock_errno, sock_errstr);
247 /************************************/
248 /****[ end of SOCKET MANAGEMENT ]****/
249 /************************************/
252 /************************************/
253 /****[ UNBUFFERED DATA EXCHANGE ]****/
254 /************************************/
255 /* Temptation to merge this with file data exchange is great,
256 but doesn't work on BillWare (see tcp_write() in portable.h) */
257 static inline void gras_trp_tcp_send(gras_socket_t sock,
259 unsigned long int size) {
264 status = tcp_write(sock->sd, data, (size_t)size);
265 DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
268 THROW4(system_error,0,"write(%d,%p,%ld) failed: %s",
269 sock->sd, data, size,
277 THROW1(system_error,0,"file descriptor closed (%s)",
283 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
285 unsigned long int size,
286 unsigned long int bufsize) {
293 DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
294 sock->sd, data+got, bufsize, got,
296 status = tcp_read(sock->sd, data+got, (size_t)bufsize);
299 THROW4(system_error,0,"read(%d,%p,%d) failed: %s",
300 sock->sd, data+got, (int)size,
303 DEBUG2("Got %d more bytes (%s)",status,hexa_str(data+got,status));
309 THROW0(system_error,0,"Socket closed by remote side");
315 static int gras_trp_tcp_recv(gras_socket_t sock,
317 unsigned long int size) {
318 return gras_trp_tcp_recv_withbuffer(sock,data,size,size);
321 /*******************************************/
322 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
323 /*******************************************/
325 /**********************************/
326 /****[ BUFFERED DATA EXCHANGE ]****/
327 /**********************************/
329 /* Make sure the data is sent */
331 gras_trp_bufiov_flush(gras_socket_t sock) {
336 gras_trp_bufdata_t *data=sock->bufdata;
340 if (data->out == buffering_buf) {
341 if (XBT_LOG_ISENABLED(trp_tcp,xbt_log_priority_debug))
342 hexa_print("chunk to send ",
343 (unsigned char *) data->out_buf.data,data->out_buf.size);
344 if ((data->out_buf.size - data->out_buf.pos) != 0) {
345 DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size,
346 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
347 gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
348 VERB1("Chunk sent (size=%d)",data->out_buf.size);
349 data->out_buf.size = 0;
354 if (data->out == buffering_iov) {
355 DEBUG0("Flush out iov");
356 vect = sock->bufdata->out_buf_v;
357 if ((size = xbt_dynar_length(vect))) {
358 DEBUG1("Flush %d chunks out of this socket",size);
359 writev(sock->sd,xbt_dynar_get_ptr(vect,0),size);
360 xbt_dynar_reset(vect);
362 data->out_buf.size = 0; /* reset the buffer containing non-stable data */
365 if (data->in == buffering_iov) {
366 DEBUG0("Flush in iov");
367 vect = sock->bufdata->in_buf_v;
368 if ((size = xbt_dynar_length(vect))) {
369 DEBUG1("Get %d chunks from of this socket",size);
370 readv(sock->sd,xbt_dynar_get_ptr(vect,0),size);
371 xbt_dynar_reset(vect);
377 gras_trp_buf_send(gras_socket_t sock,
379 unsigned long int size,
380 int stable_ignored) {
382 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
387 while (chunk_pos < size) {
388 /* size of the chunk to receive in that shot */
389 long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size);
390 DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
391 (int)data->out_buf.size,
392 ((int)data->out_buf.size) + thissize -1,
394 hexa_str((char*)chunk,thissize));
396 memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
398 data->out_buf.size += thissize;
399 chunk_pos += thissize;
400 DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
401 data->out_buf.size,size-chunk_pos,size,hexa_str((char*)chunk,chunk_pos));
403 if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
404 gras_trp_bufiov_flush(sock);
411 gras_trp_buf_recv(gras_socket_t sock,
413 unsigned long int size) {
415 gras_trp_bufdata_t *data=sock->bufdata;
416 long int chunk_pos = 0;
420 while (chunk_pos < size) {
421 /* size of the chunk to receive in that shot */
424 if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
426 DEBUG2("Get more data (size=%d,bufsize=%d)",
427 (int)MIN(size-chunk_pos,data->buffsize),
428 (int)data->buffsize);
432 gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
433 MIN(size-chunk_pos,data->buffsize),
439 thissize = min(size-chunk_pos , data->in_buf.size - data->in_buf.pos);
440 memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
442 data->in_buf.pos += thissize;
443 chunk_pos += thissize;
444 DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
445 data->in_buf.pos,size - chunk_pos,size,hexa_str(chunk,chunk_pos));
452 /*****************************************/
453 /****[ end of BUFFERED DATA EXCHANGE ]****/
454 /*****************************************/
456 /********************************/
457 /****[ VECTOR DATA EXCHANGE ]****/
458 /********************************/
461 gras_trp_iov_send(gras_socket_t sock,
463 unsigned long int size,
466 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
469 DEBUG1("Buffer one chunk to be sent later (%s)",
470 hexa_str((char*)chunk,size));
472 elm.iov_len = (size_t)size;
475 /* data storage won't last until flush. Save it in a buffer if we can */
477 if (size > data->buffsize-data->out_buf.size) {
479 flush the socket, using data in its actual storage */
480 elm.iov_base = (void*)chunk;
481 xbt_dynar_push(data->out_buf_v,&elm);
483 gras_trp_bufiov_flush(sock);
486 /* buffer big enough:
487 copy data into it, and chain it for upcoming writev */
488 memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
489 elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size);
490 data->out_buf.size += size;
492 xbt_dynar_push(data->out_buf_v,&elm);
496 /* data storage stable. Chain it */
498 elm.iov_base = (void*)chunk;
499 xbt_dynar_push(data->out_buf_v,&elm);
503 gras_trp_iov_recv(gras_socket_t sock,
505 unsigned long int size) {
508 DEBUG0("Buffer one chunk to be received later");
509 elm.iov_base = (void*)chunk;
510 elm.iov_len = (size_t)size;
511 xbt_dynar_push(sock->bufdata->in_buf_v,&elm);
517 /***************************************/
518 /****[ end of VECTOR DATA EXCHANGE ]****/
519 /***************************************/
523 *** Prototypes of BUFFERED
526 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
528 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
530 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
532 void gras_trp_buf_socket_close(gras_socket_t sd);
535 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
536 gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
538 data->buffsize = 100 * 1024 ; /* 100k */
540 data->in_buf.size = 0;
541 data->in_buf.data = xbt_malloc(data->buffsize);
542 data->in_buf.pos = 0; /* useless, indeed, since size==pos */
544 data->out_buf.size = 0;
545 data->out_buf.data = xbt_malloc(data->buffsize);
546 data->out_buf.pos = data->out_buf.size;
549 data->in_buf_v = data->out_buf_v = NULL;
550 data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
551 data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
552 data->out = buffering_iov;
554 data->out = buffering_buf;
557 data->in = buffering_buf;
559 sock->bufdata = data;
567 gras_trp_buf_setup(gras_trp_plugin_t plug) {
569 plug->socket_client = gras_trp_buf_socket_client;
570 plug->socket_server = gras_trp_buf_socket_server;
571 plug->socket_accept = gras_trp_buf_socket_accept;
572 plug->socket_close = gras_trp_buf_socket_close;
575 plug->send = gras_trp_iov_send;
577 plug->send = gras_trp_buf_send;
579 plug->recv = gras_trp_buf_recv;
581 plug->raw_send = gras_trp_tcp_send;
582 plug->raw_recv = gras_trp_tcp_recv;
584 plug->flush = gras_trp_bufiov_flush;
590 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
591 /* OUT */ gras_socket_t sock){
593 gras_trp_sock_socket_client(NULL,sock);
594 gras_trp_buf_init_sock(sock);
598 * gras_trp_buf_socket_server:
600 * Open a socket used to receive messages.
602 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
603 /* OUT */ gras_socket_t sock){
605 gras_trp_sock_socket_server(NULL,sock);
606 gras_trp_buf_init_sock(sock);
609 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock) {
610 return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
613 void gras_trp_buf_socket_close(gras_socket_t sock){
614 gras_trp_bufdata_t *data=sock->bufdata;
616 if (data->in_buf.size!=data->in_buf.pos) {
617 WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
618 data->in_buf.size - data->in_buf.pos,
619 data->in_buf.size, data->in_buf.pos);
621 if (data->in_buf.data)
622 free(data->in_buf.data);
624 if (data->out_buf.size!=data->out_buf.pos) {
625 DEBUG2("Flush the socket before closing (in=%d,out=%d)",
626 data->in_buf.size, data->out_buf.size);
627 gras_trp_bufiov_flush(sock);
629 if (data->out_buf.data)
630 free(data->out_buf.data);
633 if (data->in_buf_v) {
634 if (xbt_dynar_length(data->in_buf_v))
635 WARN0("Socket closed, but some bytes were unread");
636 xbt_dynar_free(&data->in_buf_v);
638 if (data->out_buf_v) {
639 if (xbt_dynar_length(data->out_buf_v)) {
640 DEBUG0("Flush the socket before closing");
641 gras_trp_bufiov_flush(sock);
643 xbt_dynar_free(&data->out_buf_v);
648 gras_trp_sock_socket_close(sock);
651 /****************************/
652 /****[ HELPER FUNCTIONS ]****/
653 /****************************/
656 * Returns the tcp protocol number from the network protocol data base.
658 * getprotobyname() is not thread safe. We need to lock it.
660 static int _gras_tcp_proto_number(void) {
661 struct protoent *fetchedEntry;
662 static int returnValue = 0;
664 if(returnValue == 0) {
665 fetchedEntry = getprotobyname("tcp");
666 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
667 returnValue = fetchedEntry->p_proto;
673 #ifdef HAVE_WINSOCK_H
674 #define RETSTR( x ) case x: return #x
676 const char *gras_wsa_err2string( int err ) {
684 RETSTR( WSAEWOULDBLOCK );
685 RETSTR( WSAEINPROGRESS );
686 RETSTR( WSAEALREADY );
687 RETSTR( WSAENOTSOCK );
688 RETSTR( WSAEDESTADDRREQ );
689 RETSTR( WSAEMSGSIZE );
690 RETSTR( WSAEPROTOTYPE );
691 RETSTR( WSAENOPROTOOPT );
692 RETSTR( WSAEPROTONOSUPPORT );
693 RETSTR( WSAESOCKTNOSUPPORT );
694 RETSTR( WSAEOPNOTSUPP );
695 RETSTR( WSAEPFNOSUPPORT );
696 RETSTR( WSAEAFNOSUPPORT );
697 RETSTR( WSAEADDRINUSE );
698 RETSTR( WSAEADDRNOTAVAIL );
699 RETSTR( WSAENETDOWN );
700 RETSTR( WSAENETUNREACH );
701 RETSTR( WSAENETRESET );
702 RETSTR( WSAECONNABORTED );
703 RETSTR( WSAECONNRESET );
704 RETSTR( WSAENOBUFS );
705 RETSTR( WSAEISCONN );
706 RETSTR( WSAENOTCONN );
707 RETSTR( WSAESHUTDOWN );
708 RETSTR( WSAETOOMANYREFS );
709 RETSTR( WSAETIMEDOUT );
710 RETSTR( WSAECONNREFUSED );
712 RETSTR( WSAENAMETOOLONG );
713 RETSTR( WSAEHOSTDOWN );
714 RETSTR( WSAEHOSTUNREACH );
715 RETSTR( WSAENOTEMPTY );
716 RETSTR( WSAEPROCLIM );
720 RETSTR( WSAEREMOTE );
721 RETSTR( WSASYSNOTREADY );
722 RETSTR( WSAVERNOTSUPPORTED );
723 RETSTR( WSANOTINITIALISED );
724 RETSTR( WSAEDISCON );
727 RETSTR( WSAENOMORE );
728 RETSTR( WSAECANCELLED );
729 RETSTR( WSAEINVALIDPROCTABLE );
730 RETSTR( WSAEINVALIDPROVIDER );
731 RETSTR( WSASYSCALLFAILURE );
732 RETSTR( WSASERVICE_NOT_FOUND );
733 RETSTR( WSATYPE_NOT_FOUND );
734 RETSTR( WSA_E_NO_MORE );
735 RETSTR( WSA_E_CANCELLED );
736 RETSTR( WSAEREFUSED );
737 #endif /* HAVE_WINSOCK2 */
739 RETSTR( WSAHOST_NOT_FOUND );
740 RETSTR( WSATRY_AGAIN );
741 RETSTR( WSANO_RECOVERY );
742 RETSTR( WSANO_DATA );
744 return "unknown WSA error";
746 #endif /* HAVE_WINSOCK_H */
748 /***********************************/
749 /****[ end of HELPER FUNCTIONS ]****/
750 /***********************************/