X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/1a2592601e29b027872580cbfc364194f8b01ba6..18c57b27dd71ee7839c6a90368c845886a87d638:/src/gras/Transport/transport_plugin_tcp.c diff --git a/src/gras/Transport/transport_plugin_tcp.c b/src/gras/Transport/transport_plugin_tcp.c index 6c22cb7ace..b74bf905e9 100644 --- a/src/gras/Transport/transport_plugin_tcp.c +++ b/src/gras/Transport/transport_plugin_tcp.c @@ -105,7 +105,7 @@ void gras_trp_sock_socket_client(gras_trp_plugin_t ignored, sock->sd = socket(AF_INET, SOCK_STREAM, 0); if (sock->sd < 0) { - THROW1(system_error, 0, "Failed to create socket: %s", + THROWF(system_error, 0, "Failed to create socket: %s", sock_errstr(sock_errno)); } @@ -113,13 +113,13 @@ void gras_trp_sock_socket_client(gras_trp_plugin_t ignored, (sock->sd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) || setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size))) { - VERB1("setsockopt failed, cannot set buffer size: %s", + XBT_VERB("setsockopt failed, cannot set buffer size: %s", sock_errstr(sock_errno)); } he = gethostbyname(sockdata->peer_name); if (he == NULL) { - THROW2(system_error, 0, "Failed to lookup hostname %s: %s", + THROWF(system_error, 0, "Failed to lookup hostname %s: %s", sockdata->peer_name, sock_errstr(sock_errno)); } @@ -132,15 +132,15 @@ void gras_trp_sock_socket_client(gras_trp_plugin_t ignored, if (connect(sock->sd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { tcp_close(sock->sd); - THROW3(system_error, 0, + THROWF(system_error, 0, "Failed to connect socket to %s:%d (%s)", sockdata->peer_name, sockdata->peer_port, sock_errstr(sock_errno)); } gras_trp_tcp_send(sock, (char *) &myport, sizeof(uint32_t)); - DEBUG1("peerport sent to %d", sockdata->peer_port); + XBT_DEBUG("peerport sent to %d", sockdata->peer_port); - VERB4("Connect to %s:%d (sd=%d, port %d here)", + XBT_VERB("Connect to %s:%d (sd=%d, port %d here)", sockdata->peer_name, sockdata->peer_port, sock->sd, sockdata->port); } @@ -171,12 +171,12 @@ void gras_trp_sock_socket_server(gras_trp_plugin_t ignored, server.sin_addr.s_addr = INADDR_ANY; server.sin_family = AF_INET; if ((sock->sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - THROW1(system_error, 0, "Socket allocation failed: %s", + THROWF(system_error, 0, "Socket allocation failed: %s", sock_errstr(sock_errno)); if (setsockopt (sock->sd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on))) - THROW1(system_error, 0, + THROWF(system_error, 0, "setsockopt failed, cannot condition the socket: %s", sock_errstr(sock_errno)); @@ -184,26 +184,26 @@ void gras_trp_sock_socket_server(gras_trp_plugin_t ignored, (char *) &size, sizeof(size)) || setsockopt(sock->sd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size))) { - VERB1("setsockopt failed, cannot set buffer size: %s", + XBT_VERB("setsockopt failed, cannot set buffer size: %s", sock_errstr(sock_errno)); } if (bind(sock->sd, (struct sockaddr *) &server, sizeof(server)) == -1) { tcp_close(sock->sd); - THROW2(system_error, 0, + THROWF(system_error, 0, "Cannot bind to port %d: %s", sockdata->port, sock_errstr(sock_errno)); } - DEBUG2("Listen on port %d (sd=%d)", sockdata->port, sock->sd); + XBT_DEBUG("Listen on port %d (sd=%d)", sockdata->port, sock->sd); if (listen(sock->sd, 5) < 0) { tcp_close(sock->sd); - THROW2(system_error, 0, + THROWF(system_error, 0, "Cannot listen on port %d: %s", sockdata->port, sock_errstr(sock_errno)); } - VERB2("Openned a server socket on port %d (sd=%d)", sockdata->port, + XBT_VERB("Openned a server socket on port %d (sd=%d)", sockdata->port, sock->sd); } @@ -223,7 +223,9 @@ static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) uint32_t hisport; - XBT_IN; + int failed=0; + + XBT_IN(""); gras_trp_socket_new(1, &res); sd = accept(sock->sd, (struct sockaddr *) &peer_in, &peer_in_len); @@ -231,15 +233,20 @@ static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) if (sd == -1) { gras_socket_close(sock); - THROW1(system_error, 0, + THROWF(system_error, 0, "Accept failed (%s). Droping server socket.", sock_errstr(tmp_errno)); } - if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &i, s) - || setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *) &i, - s)) - THROW1(system_error, 0, + if (_gras_tcp_proto_number()!=-1) + if (setsockopt(sd, _gras_tcp_proto_number(), TCP_NODELAY, (char *) &i,s)) + failed=1; + + if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char *) &i, s)) + failed=1; + + if (failed) + THROWF(system_error, 0, "setsockopt failed, cannot condition the socket: %s", sock_errstr(tmp_errno)); @@ -248,7 +255,7 @@ static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) || setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size))) - VERB1("setsockopt failed, cannot set buffer size: %s", + XBT_VERB("setsockopt failed, cannot set buffer size: %s", sock_errstr(tmp_errno)); res->plugin = sock->plugin; @@ -263,7 +270,7 @@ static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) gras_trp_tcp_recv(res, (char *) &hisport, sizeof(hisport)); sockdata->peer_port = ntohl(hisport); - DEBUG1("peerport %d received", sockdata->peer_port); + XBT_DEBUG("peerport %d received", sockdata->peer_port); /* FIXME: Lock to protect inet_ntoa */ if (((struct sockaddr *) &peer_in)->sa_family != AF_INET) { @@ -282,11 +289,11 @@ static gras_socket_t gras_trp_sock_socket_accept(gras_socket_t sock) } } - VERB3("Accepted from %s:%d (sd=%d)", sockdata->peer_name, sockdata->peer_port, sd); + XBT_VERB("Accepted from %s:%d (sd=%d)", sockdata->peer_name, sockdata->peer_port, sd); xbt_dynar_push(((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res); - XBT_OUT; + XBT_OUT(); return res; } @@ -296,11 +303,10 @@ static void gras_trp_sock_socket_close(gras_socket_t sock) if (!sock) return; /* close only once */ - if (((gras_trp_tcp_sock_data_t)sock->data)->peer_name) - free(((gras_trp_tcp_sock_data_t)sock->data)->peer_name); + free(((gras_trp_tcp_sock_data_t)sock->data)->peer_name); free(sock->data); - VERB1("close tcp connection %d", sock->sd); + XBT_VERB("close tcp connection %d", sock->sd); /* ask the listener to close the socket */ gras_msg_listener_close_socket(sock->sd); @@ -325,7 +331,7 @@ static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock, int status = 0; status = tcp_write(sock->sd, data, (size_t) size); - DEBUG3("write(%d, %p, %ld);", sock->sd, data, size); + XBT_DEBUG("write(%d, %p, %ld);", sock->sd, data, size); if (status < 0) { #ifdef EWOULDBLOCK @@ -335,7 +341,7 @@ static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock, #endif continue; - THROW4(system_error, 0, "write(%d,%p,%ld) failed: %s", + THROWF(system_error, 0, "write(%d,%p,%ld) failed: %s", sock->sd, data, size, sock_errstr(sock_errno)); } @@ -343,7 +349,7 @@ static XBT_INLINE void gras_trp_tcp_send(gras_socket_t sock, size -= status; data += status; } else { - THROW1(system_error, 0, "file descriptor closed (%s)", + THROWF(system_error, 0, "file descriptor closed (%s)", sock_errstr(sock_errno)); } } @@ -368,25 +374,25 @@ gras_trp_tcp_recv_withbuffer(gras_socket_t sock, while (size > got) { int status = 0; - DEBUG5("read(%d, %p, %ld) got %d so far (%s)", + XBT_DEBUG("read(%d, %p, %ld) got %d so far (%s)", sock->sd, data + got, bufsize, got, hexa_str((unsigned char *) data, got, 0)); status = tcp_read(sock->sd, data + got, (size_t) bufsize); if (status < 0) { - THROW7(system_error, 0, + THROWF(system_error, 0, "read(%d,%p,%d) from %s:%d failed: %s; got %d so far", sock->sd, data + got, (int) size, gras_socket_peer_name(sock), gras_socket_peer_port(sock), sock_errstr(sock_errno), got); } - DEBUG2("Got %d more bytes (%s)", status, + XBT_DEBUG("Got %d more bytes (%s)", status, hexa_str((unsigned char *) data + got, status, 0)); if (status) { bufsize -= status; got += status; } else { - THROW1(system_error, errno, + THROWF(system_error, errno, "Socket closed by remote side (got %d bytes before this)", got); } @@ -418,27 +424,27 @@ static void gras_trp_bufiov_flush(gras_socket_t sock) int size; #endif gras_trp_bufdata_t *data = sock->bufdata; - XBT_IN; + XBT_IN(""); - DEBUG0("Flush"); + XBT_DEBUG("Flush"); if (data->out == buffering_buf) { if (XBT_LOG_ISENABLED(gras_trp_tcp, xbt_log_priority_debug)) hexa_print("chunk to send ", (unsigned char *) data->out_buf.data, data->out_buf.size); if ((data->out_buf.size - data->out_buf.pos) != 0) { - DEBUG3("Send the chunk (size=%d) to %s:%d", data->out_buf.size, + XBT_DEBUG("Send the chunk (size=%d) to %s:%d", data->out_buf.size, gras_socket_peer_name(sock), gras_socket_peer_port(sock)); gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size); - VERB1("Chunk sent (size=%d)", data->out_buf.size); + XBT_VERB("Chunk sent (size=%d)", data->out_buf.size); data->out_buf.size = 0; } } #ifdef HAVE_READV if (data->out == buffering_iov) { - DEBUG0("Flush out iov"); + XBT_DEBUG("Flush out iov"); vect = sock->bufdata->out_buf_v; if ((size = xbt_dynar_length(vect))) { - DEBUG1("Flush %d chunks out of this socket", size); + XBT_DEBUG("Flush %d chunks out of this socket", size); writev(sock->sd, xbt_dynar_get_ptr(vect, 0), size); xbt_dynar_reset(vect); } @@ -446,10 +452,10 @@ static void gras_trp_bufiov_flush(gras_socket_t sock) } if (data->in == buffering_iov) { - DEBUG0("Flush in iov"); + XBT_DEBUG("Flush in iov"); vect = sock->bufdata->in_buf_v; if ((size = xbt_dynar_length(vect))) { - DEBUG1("Get %d chunks from of this socket", size); + XBT_DEBUG("Get %d chunks from of this socket", size); readv(sock->sd, xbt_dynar_get_ptr(vect, 0), size); xbt_dynar_reset(vect); } @@ -466,13 +472,13 @@ gras_trp_buf_send(gras_socket_t sock, gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata; int chunk_pos = 0; - XBT_IN; + XBT_IN(""); while (chunk_pos < size) { /* size of the chunk to receive in that shot */ long int thissize = min(size - chunk_pos, data->buffsize - data->out_buf.size); - DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)", + XBT_DEBUG("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)", (int) data->out_buf.size, ((int) data->out_buf.size) + thissize - 1, size, hexa_str((unsigned char *) chunk, thissize, 0)); @@ -482,7 +488,7 @@ gras_trp_buf_send(gras_socket_t sock, data->out_buf.size += thissize; chunk_pos += thissize; - DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)", + XBT_DEBUG("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)", data->out_buf.size, size - chunk_pos, size, hexa_str((unsigned char *) chunk, chunk_pos, 0)); @@ -490,7 +496,7 @@ gras_trp_buf_send(gras_socket_t sock, gras_trp_bufiov_flush(sock); } - XBT_OUT; + XBT_OUT(); } static int @@ -500,7 +506,7 @@ gras_trp_buf_recv(gras_socket_t sock, char *chunk, unsigned long int size) gras_trp_bufdata_t *data = sock->bufdata; long int chunk_pos = 0; - XBT_IN; + XBT_IN(""); while (chunk_pos < size) { /* size of the chunk to receive in that shot */ @@ -508,7 +514,7 @@ gras_trp_buf_recv(gras_socket_t sock, char *chunk, unsigned long int size) if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */ - DEBUG2("Get more data (size=%d,bufsize=%d)", + XBT_DEBUG("Get more data (size=%d,bufsize=%d)", (int) MIN(size - chunk_pos, data->buffsize), (int) data->buffsize); @@ -528,15 +534,15 @@ gras_trp_buf_recv(gras_socket_t sock, char *chunk, unsigned long int size) data->in_buf.pos += thissize; chunk_pos += thissize; - DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)", + XBT_DEBUG("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)", data->in_buf.pos, size - chunk_pos, size, hexa_str((unsigned char *) chunk, chunk_pos, 0)); } /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */ sock->moredata = (data->in_buf.size > data->in_buf.pos); - DEBUG1("There is %smore data", (sock->moredata ? "" : "no ")); + XBT_DEBUG("There is %smore data", (sock->moredata ? "" : "no ")); - XBT_OUT; + XBT_OUT(); return chunk_pos; } @@ -556,7 +562,7 @@ gras_trp_iov_send(gras_socket_t sock, gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata; - DEBUG1("Buffer one chunk to be sent later (%s)", + XBT_DEBUG("Buffer one chunk to be sent later (%s)", hexa_str((char *) chunk, size, 0)); elm.iov_len = (size_t) size; @@ -595,7 +601,7 @@ gras_trp_iov_recv(gras_socket_t sock, char *chunk, unsigned long int size) { struct iovec elm; - DEBUG0("Buffer one chunk to be received later"); + XBT_DEBUG("Buffer one chunk to be received later"); elm.iov_base = (void *) chunk; elm.iov_len = (size_t) size; xbt_dynar_push(sock->bufdata->in_buf_v, &elm); @@ -746,30 +752,28 @@ void gras_trp_buf_socket_close(gras_socket_t sock) gras_trp_bufdata_t *data = sock->bufdata; if (data->in_buf.size != data->in_buf.pos) { - WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)", + XBT_WARN("Socket closed, but %d bytes were unread (size=%d,pos=%d)", data->in_buf.size - data->in_buf.pos, data->in_buf.size, data->in_buf.pos); } - if (data->in_buf.data) - free(data->in_buf.data); + free(data->in_buf.data); if (data->out_buf.size != data->out_buf.pos) { - DEBUG2("Flush the socket before closing (in=%d,out=%d)", + XBT_DEBUG("Flush the socket before closing (in=%d,out=%d)", data->in_buf.size, data->out_buf.size); gras_trp_bufiov_flush(sock); } - if (data->out_buf.data) - free(data->out_buf.data); + free(data->out_buf.data); #ifdef HAVE_READV if (data->in_buf_v) { - if (xbt_dynar_length(data->in_buf_v)) - WARN0("Socket closed, but some bytes were unread"); + if (!xbt_dynar_is_empty(data->in_buf_v)) + XBT_WARN("Socket closed, but some bytes were unread"); xbt_dynar_free(&data->in_buf_v); } if (data->out_buf_v) { - if (xbt_dynar_length(data->out_buf_v)) { - DEBUG0("Flush the socket before closing"); + if (!xbt_dynar_is_empty(data->out_buf_v)) { + XBT_DEBUG("Flush the socket before closing"); gras_trp_bufiov_flush(sock); } xbt_dynar_free(&data->out_buf_v); @@ -785,7 +789,7 @@ void gras_trp_buf_socket_close(gras_socket_t sock) /****************************/ /* - * Returns the tcp protocol number from the network protocol data base. + * Returns the tcp protocol number from the network protocol data base, or -1 if not found * * getprotobyname() is not thread safe. We need to lock it. */ @@ -796,8 +800,12 @@ static int _gras_tcp_proto_number(void) if (returnValue == 0) { fetchedEntry = getprotobyname("tcp"); - xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL"); - returnValue = fetchedEntry->p_proto; + if (fetchedEntry == NULL) { + XBT_VERB("getprotobyname(tcp) gave NULL"); + returnValue = -1; + } else { + returnValue = fetchedEntry->p_proto; + } } return returnValue;