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));
}
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));
}
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));
}
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));
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));
}
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));
}
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);
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));
xbt_dynar_push(((gras_trp_procdata_t)
gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
- XBT_OUT;
+ XBT_OUT();
return res;
}
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);
XBT_VERB("close tcp connection %d", sock->sd);
#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));
}
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));
}
}
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);
bufsize -= status;
got += status;
} else {
- THROW1(system_error, errno,
+ THROWF(system_error, errno,
"Socket closed by remote side (got %d bytes before this)",
got);
}
int size;
#endif
gras_trp_bufdata_t *data = sock->bufdata;
- XBT_IN;
+ XBT_IN("");
XBT_DEBUG("Flush");
if (data->out == buffering_buf) {
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 */
gras_trp_bufiov_flush(sock);
}
- XBT_OUT;
+ XBT_OUT();
}
static int
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 */
sock->moredata = (data->in_buf.size > data->in_buf.pos);
XBT_DEBUG("There is %smore data", (sock->moredata ? "" : "no "));
- XBT_OUT;
+ XBT_OUT();
return chunk_pos;
}
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) {
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))
+ 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)) {
+ if (!xbt_dynar_is_empty(data->out_buf_v)) {
XBT_DEBUG("Flush the socket before closing");
gras_trp_bufiov_flush(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.
*/
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;