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->buf_size;
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->buf_size;
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->buf_size = sock->buf_size;
183 size = sock->buf_size;
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,
295 hexa_str((unsigned char*)data,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((unsigned char*)data+got,status));
309 THROW1(system_error,0,"Socket closed by remote side (got %d bytes before this)",
316 static int gras_trp_tcp_recv(gras_socket_t sock,
318 unsigned long int size) {
319 return gras_trp_tcp_recv_withbuffer(sock,data,size,size);
322 /*******************************************/
323 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
324 /*******************************************/
326 /**********************************/
327 /****[ BUFFERED DATA EXCHANGE ]****/
328 /**********************************/
330 /* Make sure the data is sent */
332 gras_trp_bufiov_flush(gras_socket_t sock) {
337 gras_trp_bufdata_t *data=sock->bufdata;
341 if (data->out == buffering_buf) {
342 if (XBT_LOG_ISENABLED(trp_tcp,xbt_log_priority_debug))
343 hexa_print("chunk to send ",
344 (unsigned char *) data->out_buf.data,data->out_buf.size);
345 if ((data->out_buf.size - data->out_buf.pos) != 0) {
346 DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size,
347 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
348 gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
349 VERB1("Chunk sent (size=%d)",data->out_buf.size);
350 data->out_buf.size = 0;
355 if (data->out == buffering_iov) {
356 DEBUG0("Flush out iov");
357 vect = sock->bufdata->out_buf_v;
358 if ((size = xbt_dynar_length(vect))) {
359 DEBUG1("Flush %d chunks out of this socket",size);
360 writev(sock->sd,xbt_dynar_get_ptr(vect,0),size);
361 xbt_dynar_reset(vect);
363 data->out_buf.size = 0; /* reset the buffer containing non-stable data */
366 if (data->in == buffering_iov) {
367 DEBUG0("Flush in iov");
368 vect = sock->bufdata->in_buf_v;
369 if ((size = xbt_dynar_length(vect))) {
370 DEBUG1("Get %d chunks from of this socket",size);
371 readv(sock->sd,xbt_dynar_get_ptr(vect,0),size);
372 xbt_dynar_reset(vect);
378 gras_trp_buf_send(gras_socket_t sock,
380 unsigned long int size,
381 int stable_ignored) {
383 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
388 while (chunk_pos < size) {
389 /* size of the chunk to receive in that shot */
390 long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size);
391 DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
392 (int)data->out_buf.size,
393 ((int)data->out_buf.size) + thissize -1,
395 hexa_str((unsigned char*)chunk,thissize));
397 memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
399 data->out_buf.size += thissize;
400 chunk_pos += thissize;
401 DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
402 data->out_buf.size,size-chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos));
404 if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
405 gras_trp_bufiov_flush(sock);
412 gras_trp_buf_recv(gras_socket_t sock,
414 unsigned long int size) {
416 gras_trp_bufdata_t *data=sock->bufdata;
417 long int chunk_pos = 0;
421 while (chunk_pos < size) {
422 /* size of the chunk to receive in that shot */
425 if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
427 DEBUG2("Get more data (size=%d,bufsize=%d)",
428 (int)MIN(size-chunk_pos,data->buffsize),
429 (int)data->buffsize);
433 gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
434 MIN(size-chunk_pos,data->buffsize),
440 thissize = min(size-chunk_pos , data->in_buf.size - data->in_buf.pos);
441 memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
443 data->in_buf.pos += thissize;
444 chunk_pos += thissize;
445 DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
446 data->in_buf.pos,size - chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos));
453 /*****************************************/
454 /****[ end of BUFFERED DATA EXCHANGE ]****/
455 /*****************************************/
457 /********************************/
458 /****[ VECTOR DATA EXCHANGE ]****/
459 /********************************/
462 gras_trp_iov_send(gras_socket_t sock,
464 unsigned long int size,
467 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
470 DEBUG1("Buffer one chunk to be sent later (%s)",
471 hexa_str((char*)chunk,size));
473 elm.iov_len = (size_t)size;
476 /* data storage won't last until flush. Save it in a buffer if we can */
478 if (size > data->buffsize-data->out_buf.size) {
480 flush the socket, using data in its actual storage */
481 elm.iov_base = (void*)chunk;
482 xbt_dynar_push(data->out_buf_v,&elm);
484 gras_trp_bufiov_flush(sock);
487 /* buffer big enough:
488 copy data into it, and chain it for upcoming writev */
489 memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
490 elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size);
491 data->out_buf.size += size;
493 xbt_dynar_push(data->out_buf_v,&elm);
497 /* data storage stable. Chain it */
499 elm.iov_base = (void*)chunk;
500 xbt_dynar_push(data->out_buf_v,&elm);
504 gras_trp_iov_recv(gras_socket_t sock,
506 unsigned long int size) {
509 DEBUG0("Buffer one chunk to be received later");
510 elm.iov_base = (void*)chunk;
511 elm.iov_len = (size_t)size;
512 xbt_dynar_push(sock->bufdata->in_buf_v,&elm);
518 /***************************************/
519 /****[ end of VECTOR DATA EXCHANGE ]****/
520 /***************************************/
524 *** Prototypes of BUFFERED
527 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
529 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
531 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
533 void gras_trp_buf_socket_close(gras_socket_t sd);
536 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
537 gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
539 data->buffsize = 100 * 1024 ; /* 100k */
541 data->in_buf.size = 0;
542 data->in_buf.data = xbt_malloc(data->buffsize);
543 data->in_buf.pos = 0; /* useless, indeed, since size==pos */
545 data->out_buf.size = 0;
546 data->out_buf.data = xbt_malloc(data->buffsize);
547 data->out_buf.pos = data->out_buf.size;
550 data->in_buf_v = data->out_buf_v = NULL;
551 data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
552 data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
553 data->out = buffering_iov;
555 data->out = buffering_buf;
558 data->in = buffering_buf;
560 sock->bufdata = data;
568 gras_trp_tcp_setup(gras_trp_plugin_t plug) {
570 plug->socket_client = gras_trp_buf_socket_client;
571 plug->socket_server = gras_trp_buf_socket_server;
572 plug->socket_accept = gras_trp_buf_socket_accept;
573 plug->socket_close = gras_trp_buf_socket_close;
576 plug->send = gras_trp_iov_send;
578 plug->send = gras_trp_buf_send;
580 plug->recv = gras_trp_buf_recv;
582 plug->raw_send = gras_trp_tcp_send;
583 plug->raw_recv = gras_trp_tcp_recv;
585 plug->flush = gras_trp_bufiov_flush;
591 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
592 /* OUT */ gras_socket_t sock){
594 gras_trp_sock_socket_client(NULL,sock);
595 gras_trp_buf_init_sock(sock);
599 * gras_trp_buf_socket_server:
601 * Open a socket used to receive messages.
603 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
604 /* OUT */ gras_socket_t sock){
606 gras_trp_sock_socket_server(NULL,sock);
607 gras_trp_buf_init_sock(sock);
610 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock) {
611 return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
614 void gras_trp_buf_socket_close(gras_socket_t sock){
615 gras_trp_bufdata_t *data=sock->bufdata;
617 if (data->in_buf.size!=data->in_buf.pos) {
618 WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
619 data->in_buf.size - data->in_buf.pos,
620 data->in_buf.size, data->in_buf.pos);
622 if (data->in_buf.data)
623 free(data->in_buf.data);
625 if (data->out_buf.size!=data->out_buf.pos) {
626 DEBUG2("Flush the socket before closing (in=%d,out=%d)",
627 data->in_buf.size, data->out_buf.size);
628 gras_trp_bufiov_flush(sock);
630 if (data->out_buf.data)
631 free(data->out_buf.data);
634 if (data->in_buf_v) {
635 if (xbt_dynar_length(data->in_buf_v))
636 WARN0("Socket closed, but some bytes were unread");
637 xbt_dynar_free(&data->in_buf_v);
639 if (data->out_buf_v) {
640 if (xbt_dynar_length(data->out_buf_v)) {
641 DEBUG0("Flush the socket before closing");
642 gras_trp_bufiov_flush(sock);
644 xbt_dynar_free(&data->out_buf_v);
649 gras_trp_sock_socket_close(sock);
652 /****************************/
653 /****[ HELPER FUNCTIONS ]****/
654 /****************************/
657 * Returns the tcp protocol number from the network protocol data base.
659 * getprotobyname() is not thread safe. We need to lock it.
661 static int _gras_tcp_proto_number(void) {
662 struct protoent *fetchedEntry;
663 static int returnValue = 0;
665 if(returnValue == 0) {
666 fetchedEntry = getprotobyname("tcp");
667 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
668 returnValue = fetchedEntry->p_proto;
674 #ifdef HAVE_WINSOCK_H
675 #define RETSTR( x ) case x: return #x
677 const char *gras_wsa_err2string( int err ) {
685 RETSTR( WSAEWOULDBLOCK );
686 RETSTR( WSAEINPROGRESS );
687 RETSTR( WSAEALREADY );
688 RETSTR( WSAENOTSOCK );
689 RETSTR( WSAEDESTADDRREQ );
690 RETSTR( WSAEMSGSIZE );
691 RETSTR( WSAEPROTOTYPE );
692 RETSTR( WSAENOPROTOOPT );
693 RETSTR( WSAEPROTONOSUPPORT );
694 RETSTR( WSAESOCKTNOSUPPORT );
695 RETSTR( WSAEOPNOTSUPP );
696 RETSTR( WSAEPFNOSUPPORT );
697 RETSTR( WSAEAFNOSUPPORT );
698 RETSTR( WSAEADDRINUSE );
699 RETSTR( WSAEADDRNOTAVAIL );
700 RETSTR( WSAENETDOWN );
701 RETSTR( WSAENETUNREACH );
702 RETSTR( WSAENETRESET );
703 RETSTR( WSAECONNABORTED );
704 RETSTR( WSAECONNRESET );
705 RETSTR( WSAENOBUFS );
706 RETSTR( WSAEISCONN );
707 RETSTR( WSAENOTCONN );
708 RETSTR( WSAESHUTDOWN );
709 RETSTR( WSAETOOMANYREFS );
710 RETSTR( WSAETIMEDOUT );
711 RETSTR( WSAECONNREFUSED );
713 RETSTR( WSAENAMETOOLONG );
714 RETSTR( WSAEHOSTDOWN );
715 RETSTR( WSAEHOSTUNREACH );
716 RETSTR( WSAENOTEMPTY );
717 RETSTR( WSAEPROCLIM );
721 RETSTR( WSAEREMOTE );
722 RETSTR( WSASYSNOTREADY );
723 RETSTR( WSAVERNOTSUPPORTED );
724 RETSTR( WSANOTINITIALISED );
725 RETSTR( WSAEDISCON );
728 RETSTR( WSAENOMORE );
729 RETSTR( WSAECANCELLED );
730 RETSTR( WSAEINVALIDPROCTABLE );
731 RETSTR( WSAEINVALIDPROVIDER );
732 RETSTR( WSASYSCALLFAILURE );
733 RETSTR( WSASERVICE_NOT_FOUND );
734 RETSTR( WSATYPE_NOT_FOUND );
735 RETSTR( WSA_E_NO_MORE );
736 RETSTR( WSA_E_CANCELLED );
737 RETSTR( WSAEREFUSED );
738 #endif /* HAVE_WINSOCK2 */
740 RETSTR( WSAHOST_NOT_FOUND );
741 RETSTR( WSATRY_AGAIN );
742 RETSTR( WSANO_RECOVERY );
743 RETSTR( WSANO_DATA );
745 return "unknown WSA error";
747 #endif /* HAVE_WINSOCK_H */
749 /***********************************/
750 /****[ end of HELPER FUNCTIONS ]****/
751 /***********************************/