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"
20 #define MIN(a,b) ((a)<(b)?(a):(b))
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_tcp,transport,
24 "TCP buffered transport");
27 *** Specific socket part
30 typedef enum { buffering_buf, buffering_iov } buffering_kind;
35 int pos; /* for receive; not exchanged over the net */
39 struct gras_trp_bufdata_{
41 gras_trp_buf_t in_buf;
42 gras_trp_buf_t out_buf;
46 xbt_dynar_t out_buf_v;
54 /*****************************/
55 /****[ SOCKET MANAGEMENT ]****/
56 /*****************************/
57 static int _gras_tcp_proto_number(void);
59 static inline void gras_trp_sock_socket_client(gras_trp_plugin_t ignored,
62 struct sockaddr_in addr;
64 struct in_addr *haddr;
65 int size = sock->bufSize * 1024;
67 sock->incoming = 1; /* TCP sockets are duplex'ed */
69 sock->sd = socket (AF_INET, SOCK_STREAM, 0);
72 THROW1(system_error,0, "Failed to create socket: %s", sock_errstr);
75 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
76 setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
77 WARN1("setsockopt failed, cannot set buffer size: %s",sock_errstr);
80 he = gethostbyname (sock->peer_name);
82 THROW2(system_error,0, "Failed to lookup hostname %s: %s",
83 sock->peer_name, sock_errstr);
86 haddr = ((struct in_addr *) (he->h_addr_list)[0]);
88 memset(&addr, 0, sizeof(struct sockaddr_in));
89 memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
90 addr.sin_family = AF_INET;
91 addr.sin_port = htons (sock->peer_port);
93 if (connect (sock->sd, (struct sockaddr*) &addr, sizeof (addr)) < 0) {
95 THROW3(system_error,0,
96 "Failed to connect socket to %s:%d (%s)",
97 sock->peer_name, sock->peer_port, sock_errstr);
99 VERB4("Connect to %s:%d (sd=%d, port %d here)",
100 sock->peer_name, sock->peer_port, sock->sd, sock->port);
104 * gras_trp_sock_socket_server:
106 * Open a socket used to receive messages.
108 static inline void gras_trp_sock_socket_server(gras_trp_plugin_t ignored,
110 int size = sock->bufSize * 1024;
112 struct sockaddr_in server;
114 sock->outgoing = 1; /* TCP => duplex mode */
116 server.sin_port = htons((u_short)sock->port);
117 server.sin_addr.s_addr = INADDR_ANY;
118 server.sin_family = AF_INET;
119 if((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
120 THROW1(system_error,0,"Socket allocation failed: %s", sock_errstr);
122 if (setsockopt(sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)))
123 THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
126 if (setsockopt(sock->sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size)) ||
127 setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size))) {
128 WARN1("setsockopt failed, cannot set buffer size: %s",
132 if (bind(sock->sd, (struct sockaddr *)&server, sizeof(server)) == -1) {
134 THROW2(system_error,0,"Cannot bind to port %d: %s",sock->port, sock_errstr);
137 DEBUG2("Listen on port %d (sd=%d)",sock->port, sock->sd);
138 if (listen(sock->sd, 5) < 0) {
140 THROW2(system_error,0,"Cannot listen on port %d: %s",sock->port,sock_errstr);
143 VERB2("Openned a server socket on port %d (sd=%d)",sock->port,sock->sd);
146 static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) {
149 struct sockaddr_in peer_in;
150 socklen_t peer_in_len = sizeof(peer_in);
157 socklen_t s = sizeof(int);
160 gras_trp_socket_new(1,&res);
162 sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
166 gras_socket_close(sock);
167 THROW1(system_error,0,
168 "Accept failed (%s). Droping server socket.", sock_errstr);
171 if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, s)
172 || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *)&i, s))
173 THROW1(system_error,0,"setsockopt failed, cannot condition the socket: %s",
176 res->bufSize = sock->bufSize;
177 size = sock->bufSize * 1024;
178 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof(size))
179 || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *)&size, sizeof(size)))
180 WARN1("setsockopt failed, cannot set buffer size: %s", sock_errstr);
182 res->plugin = sock->plugin;
183 res->incoming = sock->incoming;
184 res->outgoing = sock->outgoing;
188 res->peer_port = peer_in.sin_port;
190 /* FIXME: Lock to protect inet_ntoa */
191 if (((struct sockaddr *)&peer_in)->sa_family != AF_INET) {
192 res->peer_name = (char*)strdup("unknown");
194 struct in_addr addrAsInAddr;
197 addrAsInAddr.s_addr = peer_in.sin_addr.s_addr;
199 tmp = inet_ntoa(addrAsInAddr);
201 res->peer_name = (char*)strdup(tmp);
203 res->peer_name = (char*)strdup("unknown");
207 VERB3("Accepted from %s:%d (sd=%d)", res->peer_name,res->peer_port,sd);
213 static void gras_trp_sock_socket_close(gras_socket_t sock){
215 if (!sock) return; /* close only once */
217 VERB1("close tcp connection %d", sock->sd);
219 /* FIXME: no pipe in GRAS so far
220 if(!FD_ISSET(sd, &connectedPipes)) {
221 if(shutdown(sd, 2) < 0) {
224 ReleaseNWSLock(&lock);
226 / * The other side may have beaten us to the reset. * /
227 if ((tmp_errno!=ENOTCONN) && (tmp_errno!=ECONNRESET)) {
228 WARN1("CloseSocket: shutdown error %d\n", tmp_errno);
234 /* close the socket */
235 if(tcp_close(sock->sd) < 0) {
236 WARN3("error while closing tcp socket %d: %d (%s)\n",
237 sock->sd, sock_errno, sock_errstr);
241 /************************************/
242 /****[ end of SOCKET MANAGEMENT ]****/
243 /************************************/
246 /************************************/
247 /****[ UNBUFFERED DATA EXCHANGE ]****/
248 /************************************/
249 /* Temptation to merge this with file data exchange is great,
250 but doesn't work on BillWare (see tcp_write() in portable.h) */
251 static inline void gras_trp_tcp_send(gras_socket_t sock,
253 unsigned long int size) {
258 status = tcp_write(sock->sd, data, (size_t)size);
259 DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
262 THROW4(system_error,0,"write(%d,%p,%ld) failed: %s",
263 sock->sd, data, size,
271 THROW1(system_error,0,"file descriptor closed (%s)",
277 gras_trp_tcp_recv_withbuffer(gras_socket_t sock,
279 unsigned long int size,
280 unsigned long int bufsize) {
287 DEBUG5("read(%d, %p, %ld) got %d so far (%s)",
288 sock->sd, data+got, bufsize, got,
290 status = tcp_read(sock->sd, data+got, (size_t)bufsize);
293 THROW4(system_error,0,"read(%d,%p,%d) failed: %s",
294 sock->sd, data+got, (int)size,
297 DEBUG2("Got %d more bytes (%s)",status,hexa_str(data+got,status));
303 THROW0(system_error,0,"Socket closed by remote side");
309 static int gras_trp_tcp_recv(gras_socket_t sock,
311 unsigned long int size) {
312 return gras_trp_tcp_recv_withbuffer(sock,data,size,size);
315 /*******************************************/
316 /****[ end of UNBUFFERED DATA EXCHANGE ]****/
317 /*******************************************/
319 /**********************************/
320 /****[ BUFFERED DATA EXCHANGE ]****/
321 /**********************************/
323 /* Make sure the data is sent */
325 gras_trp_bufiov_flush(gras_socket_t sock) {
330 gras_trp_bufdata_t *data=sock->bufdata;
334 if (data->out == buffering_buf) {
335 if (XBT_LOG_ISENABLED(trp_tcp,xbt_log_priority_debug))
336 hexa_print("chunk to send ",
337 (unsigned char *) data->out_buf.data,data->out_buf.size);
338 if ((data->out_buf.size - data->out_buf.pos) != 0) {
339 DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size,
340 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
341 gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
342 VERB1("Chunk sent (size=%d)",data->out_buf.size);
343 data->out_buf.size = 0;
348 if (data->out == buffering_iov) {
349 vect = sock->bufdata->out_buf_v;
350 if ((size = xbt_dynar_length(vect))) {
351 DEBUG1("Flush %d chunks out of this socket",size);
352 writev(sock->sd,xbt_dynar_get_ptr(vect,0),size);
353 xbt_dynar_reset(vect);
355 data->out_buf.size = 0; /* reset the buffer containing non-stable data */
358 if (data->in == buffering_iov) {
359 vect = sock->bufdata->in_buf_v;
360 if ((size = xbt_dynar_length(vect))) {
361 DEBUG1("Get %d chunks from of this socket",size);
362 readv(sock->sd,xbt_dynar_get_ptr(vect,0),size);
363 xbt_dynar_reset(vect);
369 gras_trp_buf_send(gras_socket_t sock,
371 unsigned long int size,
372 int stable_ignored) {
374 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
379 while (chunk_pos < size) {
380 /* size of the chunk to receive in that shot */
381 long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size);
382 DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
383 (int)data->out_buf.size,
384 ((int)data->out_buf.size) + thissize -1,
386 hexa_str((char*)chunk,thissize));
388 memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
390 data->out_buf.size += thissize;
391 chunk_pos += thissize;
392 DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
393 data->out_buf.size,size-chunk_pos,size,hexa_str((char*)chunk,chunk_pos));
395 if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
396 gras_trp_bufiov_flush(sock);
403 gras_trp_buf_recv(gras_socket_t sock,
405 unsigned long int size) {
407 gras_trp_bufdata_t *data=sock->bufdata;
408 long int chunk_pos = 0;
412 while (chunk_pos < size) {
413 /* size of the chunk to receive in that shot */
416 if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
418 DEBUG2("Get more data (size=%d,bufsize=%d)",
419 (int)MIN(size-chunk_pos,data->buffsize),
420 (int)data->buffsize);
424 gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
425 MIN(size-chunk_pos,data->buffsize),
431 thissize = min(size-chunk_pos , data->in_buf.size - data->in_buf.pos);
432 memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
434 data->in_buf.pos += thissize;
435 chunk_pos += thissize;
436 DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
437 data->in_buf.pos,size - chunk_pos,size,hexa_str(chunk,chunk_pos));
444 /*****************************************/
445 /****[ end of BUFFERED DATA EXCHANGE ]****/
446 /*****************************************/
448 /********************************/
449 /****[ VECTOR DATA EXCHANGE ]****/
450 /********************************/
453 gras_trp_iov_send(gras_socket_t sock,
455 unsigned long int size,
458 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
461 DEBUG1("Buffer one chunk to be sent later (%s)",
462 hexa_str((char*)chunk,size));
464 elm.iov_len = (size_t)size;
467 /* data storage won't last until flush. Save it in a buffer if we can */
469 if (size > data->buffsize-data->out_buf.size) {
471 flush the socket, using data in its actual storage */
472 elm.iov_base = (void*)chunk;
473 xbt_dynar_push(data->out_buf_v,&elm);
475 gras_trp_bufiov_flush(sock);
478 /* buffer big enough:
479 copy data into it, and chain it for upcoming writev */
480 memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
481 elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size);
482 data->out_buf.size += size;
484 xbt_dynar_push(data->out_buf_v,&elm);
488 /* data storage stable. Chain it */
490 elm.iov_base = (void*)chunk;
491 xbt_dynar_push(data->out_buf_v,&elm);
495 gras_trp_iov_recv(gras_socket_t sock,
497 unsigned long int size) {
500 DEBUG0("Buffer one chunk to be received later");
501 elm.iov_base = (void*)chunk;
502 elm.iov_len = (size_t)size;
503 xbt_dynar_push(sock->bufdata->in_buf_v,&elm);
509 /***************************************/
510 /****[ end of VECTOR DATA EXCHANGE ]****/
511 /***************************************/
515 *** Prototypes of BUFFERED
518 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
520 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
522 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
524 void gras_trp_buf_socket_close(gras_socket_t sd);
527 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
528 gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
530 data->buffsize = 100 * 1024 ; /* 100k */
532 data->in_buf.size = 0;
533 data->in_buf.data = xbt_malloc(data->buffsize);
534 data->in_buf.pos = 0; /* useless, indeed, since size==pos */
536 data->out_buf.size = 0;
537 data->out_buf.data = xbt_malloc(data->buffsize);
538 data->out_buf.pos = data->out_buf.size;
540 data->in_buf_v = data->out_buf_v = NULL;
542 data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
543 data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
546 data->in = buffering_buf;
547 data->out = buffering_iov;
548 /*data->out = buffering_buf;*/
550 sock->bufdata = data;
558 gras_trp_buf_setup(gras_trp_plugin_t plug) {
560 plug->socket_client = gras_trp_buf_socket_client;
561 plug->socket_server = gras_trp_buf_socket_server;
562 plug->socket_accept = gras_trp_buf_socket_accept;
563 plug->socket_close = gras_trp_buf_socket_close;
565 plug->send = gras_trp_iov_send;
566 /*plug->send = gras_trp_buf_send;*/
567 plug->recv = gras_trp_buf_recv;
569 plug->raw_send = gras_trp_tcp_send;
570 plug->raw_recv = gras_trp_tcp_recv;
572 plug->flush = gras_trp_bufiov_flush;
578 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
579 /* OUT */ gras_socket_t sock){
581 gras_trp_sock_socket_client(NULL,sock);
582 gras_trp_buf_init_sock(sock);
586 * gras_trp_buf_socket_server:
588 * Open a socket used to receive messages.
590 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
591 /* OUT */ gras_socket_t sock){
593 gras_trp_sock_socket_server(NULL,sock);
594 gras_trp_buf_init_sock(sock);
597 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock) {
598 return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
601 void gras_trp_buf_socket_close(gras_socket_t sock){
602 gras_trp_bufdata_t *data=sock->bufdata;
604 if (data->in_buf.size!=data->in_buf.pos) {
605 WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
606 data->in_buf.size - data->in_buf.pos,
607 data->in_buf.size, data->in_buf.pos);
609 if (data->in_buf.data)
610 free(data->in_buf.data);
612 if (data->out_buf.size!=data->out_buf.pos) {
613 DEBUG2("Flush the socket before closing (in=%d,out=%d)",
614 data->in_buf.size, data->out_buf.size);
615 gras_trp_bufiov_flush(sock);
617 if (data->out_buf.data)
618 free(data->out_buf.data);
621 if (data->in_buf_v) {
622 if (xbt_dynar_length(data->in_buf_v))
623 WARN0("Socket closed, but some bytes were unread");
624 xbt_dynar_free(&data->in_buf_v);
626 if (data->out_buf_v) {
627 if (xbt_dynar_length(data->out_buf_v)) {
628 DEBUG0("Flush the socket before closing");
629 gras_trp_bufiov_flush(sock);
631 xbt_dynar_free(&data->out_buf_v);
636 gras_trp_sock_socket_close(sock);
639 /****************************/
640 /****[ HELPER FUNCTIONS ]****/
641 /****************************/
644 * Returns the tcp protocol number from the network protocol data base.
646 * getprotobyname() is not thread safe. We need to lock it.
648 static int _gras_tcp_proto_number(void) {
649 struct protoent *fetchedEntry;
650 static int returnValue = 0;
652 if(returnValue == 0) {
653 fetchedEntry = getprotobyname("tcp");
654 xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
655 returnValue = fetchedEntry->p_proto;
661 #ifdef HAVE_WINSOCK_H
662 #define RETSTR( x ) case x: return #x
664 const char *gras_wsa_err2string( int err ) {
672 RETSTR( WSAEWOULDBLOCK );
673 RETSTR( WSAEINPROGRESS );
674 RETSTR( WSAEALREADY );
675 RETSTR( WSAENOTSOCK );
676 RETSTR( WSAEDESTADDRREQ );
677 RETSTR( WSAEMSGSIZE );
678 RETSTR( WSAEPROTOTYPE );
679 RETSTR( WSAENOPROTOOPT );
680 RETSTR( WSAEPROTONOSUPPORT );
681 RETSTR( WSAESOCKTNOSUPPORT );
682 RETSTR( WSAEOPNOTSUPP );
683 RETSTR( WSAEPFNOSUPPORT );
684 RETSTR( WSAEAFNOSUPPORT );
685 RETSTR( WSAEADDRINUSE );
686 RETSTR( WSAEADDRNOTAVAIL );
687 RETSTR( WSAENETDOWN );
688 RETSTR( WSAENETUNREACH );
689 RETSTR( WSAENETRESET );
690 RETSTR( WSAECONNABORTED );
691 RETSTR( WSAECONNRESET );
692 RETSTR( WSAENOBUFS );
693 RETSTR( WSAEISCONN );
694 RETSTR( WSAENOTCONN );
695 RETSTR( WSAESHUTDOWN );
696 RETSTR( WSAETOOMANYREFS );
697 RETSTR( WSAETIMEDOUT );
698 RETSTR( WSAECONNREFUSED );
700 RETSTR( WSAENAMETOOLONG );
701 RETSTR( WSAEHOSTDOWN );
702 RETSTR( WSAEHOSTUNREACH );
703 RETSTR( WSAENOTEMPTY );
704 RETSTR( WSAEPROCLIM );
708 RETSTR( WSAEREMOTE );
709 RETSTR( WSASYSNOTREADY );
710 RETSTR( WSAVERNOTSUPPORTED );
711 RETSTR( WSANOTINITIALISED );
712 RETSTR( WSAEDISCON );
715 RETSTR( WSAENOMORE );
716 RETSTR( WSAECANCELLED );
717 RETSTR( WSAEINVALIDPROCTABLE );
718 RETSTR( WSAEINVALIDPROVIDER );
719 RETSTR( WSASYSCALLFAILURE );
720 RETSTR( WSASERVICE_NOT_FOUND );
721 RETSTR( WSATYPE_NOT_FOUND );
722 RETSTR( WSA_E_NO_MORE );
723 RETSTR( WSA_E_CANCELLED );
724 RETSTR( WSAEREFUSED );
725 #endif /* HAVE_WINSOCK2 */
727 RETSTR( WSAHOST_NOT_FOUND );
728 RETSTR( WSATRY_AGAIN );
729 RETSTR( WSANO_RECOVERY );
730 RETSTR( WSANO_DATA );
732 return "unknown WSA error";
734 #endif /* HAVE_WINSOCK_H */
736 /***********************************/
737 /****[ end of HELPER FUNCTIONS ]****/
738 /***********************************/