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"
24 #define MIN(a,b) ((a)<(b)?(a):(b))
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_tcp,transport,
28 "TCP buffered transport");
31 *** Specific socket part
34 typedef enum { buffering_buf, buffering_iov } buffering_kind;
39 int pos; /* for receive; not exchanged over the net */
43 struct gras_trp_bufdata_{
45 gras_trp_buf_t in_buf;
46 gras_trp_buf_t out_buf;
50 xbt_dynar_t out_buf_v;
58 /*****************************/
59 /****[ SOCKET MANAGEMENT ]****/
60 /*****************************/
61 static int _gras_tcp_proto_number(void);
63 static inline void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
66 struct sockaddr_in addr;
68 struct in_addr *haddr;
69 int size = sock->bufSize * 1024;
71 sock->incoming = 1; /* TCP sockets are duplex'ed */
73 sock->sd = socket (AF_INET, SOCK_STREAM, 0);
76 THROW1(system_error,0, "Failed to create socket: %s", sock_errstr);
79 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
80 setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
81 WARN1("setsockopt failed, cannot set buffer size: %s",sock_errstr);
84 he = gethostbyname (sock->peer_name);
86 THROW2(system_error,0, "Failed to lookup hostname %s: %s",
87 sock->peer_name, sock_errstr);
90 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
92 memset(&addr, 0, sizeof(struct sockaddr_in));
93 memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
94 addr.sin_family = AF_INET;
95 addr.sin_port = htons (sock->peer_port);
97 if (connect (sock->sd, (struct sockaddr*) &addr, sizeof (addr)) < 0) {
99 THROW3(system_error,0,
100 "Failed to connect socket to %s:%d (%s)",
101 sock->peer_name, sock->peer_port, sock_errstr);
103 VERB4("Connect to %s:%d (sd=%d, port %d here)",
104 sock->peer_name, sock->peer_port, sock->sd, sock->port);
108 * gras_trp_sock_socket_server:
110 * Open a socket used to receive messages.
112 static inline void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
114 int size = sock->bufSize * 1024;
116 struct sockaddr_in server;
118 sock->outgoing = 1; /* TCP => duplex mode */
120 server.sin_port = htons((u_short)sock->port);
121 server.sin_addr.s_addr = INADDR_ANY;
122 server.sin_family = AF_INET;
123 if((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
124 THROW1(system_error,0,"Socket allocation failed: %s", sock_errstr);
126 if (setsockopt(sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)))
127 THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
130 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
131 setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
132 WARN1("setsockopt failed, cannot set buffer size: %s",
136 if (bind(sock->sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
138 THROW2(system_error,0,"Cannot bind to port %d: %s",sock->port, sock_errstr);
141 DEBUG2("Listen on port %d (sd=%d)",sock->port, sock->sd);
142 if (listen(sock->sd, 5) < 0) {
144 THROW2(system_error,0,"Cannot listen on port %d: %s",sock->port,sock_errstr);
147 VERB2("Openned a server socket on port %d (sd=%d)",sock->port,sock->sd);
150 static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) {
153 struct sockaddr_in peer_in;
154 socklen_t peer_in_len = sizeof(peer_in);
161 socklen_t s = sizeof(int);
164 gras_trp_socket_new(1,&res);
166 sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
170 gras_socket_close(sock);
171 THROW1(system_error,0,
172 "Accept failed (%s). Droping server socket.", sock_errstr);
175 if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s)
176 || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *)&i, s))
177 THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
180 res->bufSize = sock->bufSize;
181 size = sock->bufSize * 1024;
182 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size))
183 || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size)))
184 WARN1("setsockopt failed, cannot set buffer size: %s", sock_errstr);
186 res->plugin = sock->plugin;
187 res->incoming = sock->incoming;
188 res->outgoing = sock->outgoing;
192 res->peer_port = peer_in.sin_port;
194 /* FIXME: Lock to protect inet_ntoa */
195 if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) {
196 res->peer_name = (char*)strdup("unknown");
198 struct in_addr addrAsInAddr;
201 addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
203 tmp = inet_ntoa(addrAsInAddr);
205 res->peer_name = (char*)strdup(tmp);
207 res->peer_name = (char*)strdup("unknown");
211 VERB3("Accepted from %s:%d (sd=%d)", res->peer_name,res->peer_port,sd);
217 static void gras_trp_sock_socket_close(gras_socket_t sock){
219 if (!sock) return; /* close only once */
221 VERB1("close tcp connection %d", sock->sd);
223 /* FIXME: no pipe in GRAS so far
224 if(!FD_ISSET(sd, &connectedPipes)) {
225 if(shutdown(sd, 2) < 0) {
228 ReleaseNWSLock(&lock);
230 / * The other side may have beaten us to the reset. * /
231 if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) {
232 WARN1("CloseSocket: shutdown error %d\n", tmp_errno);
238 /* close the socket */
239 if(tcp_close(sock->sd) < 0) {
240 WARN3("error while closing tcp socket %d: %d (%s)\n",
241 sock->sd, sock_errno, sock_errstr);
245 /************************************/
246 /****[ end of SOCKET MANAGEMENT ]****/
247 /************************************/
250 /************************************/
251 /****[ UNBUFFERED DATA EXCHANGE ]****/
252 /************************************/
253 /* Temptation to merge this with file data exchange is great,
254 but doesn't work on BillWare (see tcp_write() in portable.h) */
255 static inline void gras_trp_tcp_send(gras_socket_t sock,
257 unsigned long int size) {
262 status = tcp_write(sock->sd, data, (size_t)size);
263 DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
266 THROW4(system_error,0,"write(%d,%p,%ld) failed: %s",
267 sock->sd, data, size,
275 THROW1(system_error,0,"file descriptor closed (%s)",
281 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
283 unsigned long int size,
284 unsigned long int bufsize) {
291 DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
292 sock->sd, data+got, bufsize, got,
294 status = tcp_read(sock->sd, data+got, (size_t)bufsize);
297 THROW4(system_error,0,"read(%d,%p,%d) failed: %s",
298 sock->sd, data+got, (int)size,
301 DEBUG2("Got %d more bytes (%s)",status,hexa_str(data+got,status));
307 THROW0(system_error,0,"Socket closed by remote side");
313 static int gras_trp_tcp_recv(gras_socket_t sock,
315 unsigned long int size) {
316 return gras_trp_tcp_recv_withbuffer(sock,data,size,size);
319 /*******************************************/
320 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
321 /*******************************************/
323 /**********************************/
324 /****[ BUFFERED DATA EXCHANGE ]****/
325 /**********************************/
327 /* Make sure the data is sent */
329 gras_trp_bufiov_flush(gras_socket_t sock) {
334 gras_trp_bufdata_t *data=sock->bufdata;
338 if (data->out == buffering_buf) {
339 if (XBT_LOG_ISENABLED(trp_tcp,xbt_log_priority_debug))
340 hexa_print("chunk to send ",
341 (unsigned char *) data->out_buf.data,data->out_buf.size);
342 if ((data->out_buf.size - data->out_buf.pos) != 0) {
343 DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size,
344 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
345 gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
346 VERB1("Chunk sent (size=%d)",data->out_buf.size);
347 data->out_buf.size = 0;
352 if (data->out == buffering_iov) {
353 vect = sock->bufdata->out_buf_v;
354 if ((size = xbt_dynar_length(vect))) {
355 DEBUG1("Flush %d chunks out of this socket",size);
356 writev(sock->sd,xbt_dynar_get_ptr(vect,0),size);
357 xbt_dynar_reset(vect);
359 data->out_buf.size = 0; /* reset the buffer containing non-stable data */
362 if (data->in == buffering_iov) {
363 vect = sock->bufdata->in_buf_v;
364 if ((size = xbt_dynar_length(vect))) {
365 DEBUG1("Get %d chunks from of this socket",size);
366 readv(sock->sd,xbt_dynar_get_ptr(vect,0),size);
367 xbt_dynar_reset(vect);
373 gras_trp_buf_send(gras_socket_t sock,
375 unsigned long int size,
376 int stable_ignored) {
378 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
383 while (chunk_pos < size) {
384 /* size of the chunk to receive in that shot */
385 long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size);
386 DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
387 (int)data->out_buf.size,
388 ((int)data->out_buf.size) + thissize -1,
390 hexa_str((char*)chunk,thissize));
392 memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
394 data->out_buf.size += thissize;
395 chunk_pos += thissize;
396 DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
397 data->out_buf.size,size-chunk_pos,size,hexa_str((char*)chunk,chunk_pos));
399 if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
400 gras_trp_bufiov_flush(sock);
407 gras_trp_buf_recv(gras_socket_t sock,
409 unsigned long int size) {
411 gras_trp_bufdata_t *data=sock->bufdata;
412 long int chunk_pos = 0;
416 while (chunk_pos < size) {
417 /* size of the chunk to receive in that shot */
420 if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
422 DEBUG2("Get more data (size=%d,bufsize=%d)",
423 (int)MIN(size-chunk_pos,data->buffsize),
424 (int)data->buffsize);
428 gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
429 MIN(size-chunk_pos,data->buffsize),
435 thissize = min(size-chunk_pos , data->in_buf.size - data->in_buf.pos);
436 memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
438 data->in_buf.pos += thissize;
439 chunk_pos += thissize;
440 DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
441 data->in_buf.pos,size - chunk_pos,size,hexa_str(chunk,chunk_pos));
448 /*****************************************/
449 /****[ end of BUFFERED DATA EXCHANGE ]****/
450 /*****************************************/
452 /********************************/
453 /****[ VECTOR DATA EXCHANGE ]****/
454 /********************************/
457 gras_trp_iov_send(gras_socket_t sock,
459 unsigned long int size,
462 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
465 DEBUG1("Buffer one chunk to be sent later (%s)",
466 hexa_str((char*)chunk,size));
468 elm.iov_len = (size_t)size;
471 /* data storage won't last until flush. Save it in a buffer if we can */
473 if (size > data->buffsize-data->out_buf.size) {
475 flush the socket, using data in its actual storage */
476 elm.iov_base = (void*)chunk;
477 xbt_dynar_push(data->out_buf_v,&elm);
479 gras_trp_bufiov_flush(sock);
482 /* buffer big enough:
483 copy data into it, and chain it for upcoming writev */
484 memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
485 elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size);
486 data->out_buf.size += size;
488 xbt_dynar_push(data->out_buf_v,&elm);
492 /* data storage stable. Chain it */
494 elm.iov_base = (void*)chunk;
495 xbt_dynar_push(data->out_buf_v,&elm);
499 gras_trp_iov_recv(gras_socket_t sock,
501 unsigned long int size) {
504 DEBUG0("Buffer one chunk to be received later");
505 elm.iov_base = (void*)chunk;
506 elm.iov_len = (size_t)size;
507 xbt_dynar_push(sock->bufdata->in_buf_v,&elm);
513 /***************************************/
514 /****[ end of VECTOR DATA EXCHANGE ]****/
515 /***************************************/
519 *** Prototypes of BUFFERED
522 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
524 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
526 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
528 void gras_trp_buf_socket_close(gras_socket_t sd);
531 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
532 gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
534 data->buffsize = 100 * 1024 ; /* 100k */
536 data->in_buf.size = 0;
537 data->in_buf.data = xbt_malloc(data->buffsize);
538 data->in_buf.pos = 0; /* useless, indeed, since size==pos */
540 data->out_buf.size = 0;
541 data->out_buf.data = xbt_malloc(data->buffsize);
542 data->out_buf.pos = data->out_buf.size;
545 data->in_buf_v = data->out_buf_v = NULL;
546 data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
547 data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
548 data->out = buffering_iov;
550 data->out = buffering_buf;
553 data->in = buffering_buf;
555 sock->bufdata = data;
563 gras_trp_buf_setup(gras_trp_plugin_t plug) {
565 plug->socket_client = gras_trp_buf_socket_client;
566 plug->socket_server = gras_trp_buf_socket_server;
567 plug->socket_accept = gras_trp_buf_socket_accept;
568 plug->socket_close = gras_trp_buf_socket_close;
571 plug->send = gras_trp_iov_send;
573 plug->send = gras_trp_buf_send;
575 plug->recv = gras_trp_buf_recv;
577 plug->raw_send = gras_trp_tcp_send;
578 plug->raw_recv = gras_trp_tcp_recv;
580 plug->flush = gras_trp_bufiov_flush;
586 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
587 /* OUT */ gras_socket_t sock){
589 gras_trp_sock_socket_client(NULL,sock);
590 gras_trp_buf_init_sock(sock);
594 * gras_trp_buf_socket_server:
596 * Open a socket used to receive messages.
598 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
599 /* OUT */ gras_socket_t sock){
601 gras_trp_sock_socket_server(NULL,sock);
602 gras_trp_buf_init_sock(sock);
605 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock) {
606 return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
609 void gras_trp_buf_socket_close(gras_socket_t sock){
610 gras_trp_bufdata_t *data=sock->bufdata;
612 if (data->in_buf.size!=data->in_buf.pos) {
613 WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
614 data->in_buf.size - data->in_buf.pos,
615 data->in_buf.size, data->in_buf.pos);
617 if (data->in_buf.data)
618 free(data->in_buf.data);
620 if (data->out_buf.size!=data->out_buf.pos) {
621 DEBUG2("Flush the socket before closing (in=%d,out=%d)",
622 data->in_buf.size, data->out_buf.size);
623 gras_trp_bufiov_flush(sock);
625 if (data->out_buf.data)
626 free(data->out_buf.data);
629 if (data->in_buf_v) {
630 if (xbt_dynar_length(data->in_buf_v))
631 WARN0("Socket closed, but some bytes were unread");
632 xbt_dynar_free(&data->in_buf_v);
634 if (data->out_buf_v) {
635 if (xbt_dynar_length(data->out_buf_v)) {
636 DEBUG0("Flush the socket before closing");
637 gras_trp_bufiov_flush(sock);
639 xbt_dynar_free(&data->out_buf_v);
644 gras_trp_sock_socket_close(sock);
647 /****************************/
648 /****[ HELPER FUNCTIONS ]****/
649 /****************************/
652 * Returns the tcp protocol number from the network protocol data base.
654 * getprotobyname() is not thread safe. We need to lock it.
656 static int _gras_tcp_proto_number(void) {
657 struct protoent *fetchedEntry;
658 static int returnValue = 0;
660 if(returnValue == 0) {
661 fetchedEntry = getprotobyname("tcp");
662 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
663 returnValue = fetchedEntry->p_proto;
669 #ifdef HAVE_WINSOCK_H
670 #define RETSTR( x ) case x: return #x
672 const char *gras_wsa_err2string( int err ) {
680 RETSTR( WSAEWOULDBLOCK );
681 RETSTR( WSAEINPROGRESS );
682 RETSTR( WSAEALREADY );
683 RETSTR( WSAENOTSOCK );
684 RETSTR( WSAEDESTADDRREQ );
685 RETSTR( WSAEMSGSIZE );
686 RETSTR( WSAEPROTOTYPE );
687 RETSTR( WSAENOPROTOOPT );
688 RETSTR( WSAEPROTONOSUPPORT );
689 RETSTR( WSAESOCKTNOSUPPORT );
690 RETSTR( WSAEOPNOTSUPP );
691 RETSTR( WSAEPFNOSUPPORT );
692 RETSTR( WSAEAFNOSUPPORT );
693 RETSTR( WSAEADDRINUSE );
694 RETSTR( WSAEADDRNOTAVAIL );
695 RETSTR( WSAENETDOWN );
696 RETSTR( WSAENETUNREACH );
697 RETSTR( WSAENETRESET );
698 RETSTR( WSAECONNABORTED );
699 RETSTR( WSAECONNRESET );
700 RETSTR( WSAENOBUFS );
701 RETSTR( WSAEISCONN );
702 RETSTR( WSAENOTCONN );
703 RETSTR( WSAESHUTDOWN );
704 RETSTR( WSAETOOMANYREFS );
705 RETSTR( WSAETIMEDOUT );
706 RETSTR( WSAECONNREFUSED );
708 RETSTR( WSAENAMETOOLONG );
709 RETSTR( WSAEHOSTDOWN );
710 RETSTR( WSAEHOSTUNREACH );
711 RETSTR( WSAENOTEMPTY );
712 RETSTR( WSAEPROCLIM );
716 RETSTR( WSAEREMOTE );
717 RETSTR( WSASYSNOTREADY );
718 RETSTR( WSAVERNOTSUPPORTED );
719 RETSTR( WSANOTINITIALISED );
720 RETSTR( WSAEDISCON );
723 RETSTR( WSAENOMORE );
724 RETSTR( WSAECANCELLED );
725 RETSTR( WSAEINVALIDPROCTABLE );
726 RETSTR( WSAEINVALIDPROVIDER );
727 RETSTR( WSASYSCALLFAILURE );
728 RETSTR( WSASERVICE_NOT_FOUND );
729 RETSTR( WSATYPE_NOT_FOUND );
730 RETSTR( WSA_E_NO_MORE );
731 RETSTR( WSA_E_CANCELLED );
732 RETSTR( WSAEREFUSED );
733 #endif /* HAVE_WINSOCK2 */
735 RETSTR( WSAHOST_NOT_FOUND );
736 RETSTR( WSATRY_AGAIN );
737 RETSTR( WSANO_RECOVERY );
738 RETSTR( WSANO_DATA );
740 return "unknown WSA error";
742 #endif /* HAVE_WINSOCK_H */
744 /***********************************/
745 /****[ end of HELPER FUNCTIONS ]****/
746 /***********************************/