session MAINTENANCE socket functions reorganized
Some will be used in both client and server for
Call Home.
diff --git a/src/session_client.c b/src/session_client.c
index 20a0660..1d61341 100644
--- a/src/session_client.c
+++ b/src/session_client.c
@@ -352,7 +352,7 @@
}
int
-nc_connect_getsocket(const char* host, uint16_t port)
+nc_sock_connect(const char* host, uint16_t port)
{
int i, sock = -1;
struct addrinfo hints, *res_list, *res;
@@ -1405,95 +1405,8 @@
/* CALL HOME */
-static int
-get_listen_socket(const char *address, uint16_t port)
-{
- int sock;
- const int optVal = 1;
- const socklen_t optLen = sizeof(optVal);
- char is_ipv4;
- struct sockaddr_storage saddr;
-
- struct sockaddr_in* saddr4;
- struct sockaddr_in6* saddr6;
-
- if (!address || !port) {
- return -1;
- }
-
- if (strchr(address, ':') == NULL) {
- is_ipv4 = 1;
- } else {
- is_ipv4 = 0;
- }
-
- sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
- if (sock == -1) {
- ERR("Could not create socket (%s)", strerror(errno));
- return -1;
- }
-
- if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
- ERR("Could not set socket SO_REUSEADDR option (%s)", strerror(errno));
- close(sock);
- return -1;
- }
-
- /* TODO may be needed
- if (fcntl(sock, F_SETFD, FD_CLOEXEC) != 0) {
- nc_verb_error("%s: fcntl failed (%s)", __func__, strerror(errno));
- continue;
- }*/
-
- bzero(&saddr, sizeof(struct sockaddr_storage));
- if (is_ipv4) {
- saddr4 = (struct sockaddr_in *)&saddr;
-
- saddr4->sin_family = AF_INET;
- saddr4->sin_port = htons(port);
-
- if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
- ERR("Failed to convert \"%s\" to IPv4 address.", address);
- close(sock);
- return -1;
- }
-
- if (bind(sock, (struct sockaddr*)saddr4, sizeof(struct sockaddr_in)) == -1) {
- ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
- close(sock);
- return -1;
- }
-
- } else {
- saddr6 = (struct sockaddr_in6 *)&saddr;
-
- saddr6->sin6_family = AF_INET6;
- saddr6->sin6_port = htons(port);
-
- if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
- ERR("Failed to convert \"%s\" to IPv6 address.", address);
- close(sock);
- return -1;
- }
-
- if (bind(sock, (struct sockaddr*)saddr6, sizeof(struct sockaddr_in6)) == -1) {
- ERR("Could not bind \"%s\" port %d (%s)", address, port, strerror(errno));
- close(sock);
- return -1;
- }
- }
-
- if (listen(sock, NC_REVERSE_QUEUE)) {
- ERR("Unable to start listening on \"%s\" port %d (%s)", address, port, strerror(errno));
- close(sock);
- return -1;
- }
-
- return sock;
-}
-
int
-nc_callhome_accept_connection(uint16_t port, int timeout, uint16_t *server_port, char **server_host)
+nc_sock_accept(uint16_t port, int timeout, char **peer_host, uint16_t *peer_port)
{
struct pollfd reverse_listen_socket = {-1, POLLIN, 0};
int sock;
@@ -1501,7 +1414,7 @@
socklen_t addr_size = sizeof(remote);
int status;
- reverse_listen_socket.fd = get_listen_socket("::0", port);
+ reverse_listen_socket.fd = nc_sock_listen("::0", port);
if (reverse_listen_socket.fd == -1) {
goto fail;
}
@@ -1541,21 +1454,21 @@
/* fill some server info, if interested */
if (remote.ss_family == AF_INET) {
struct sockaddr_in *remote_in = (struct sockaddr_in *)&remote;
- if (server_port) {
- *server_port = ntohs(remote_in->sin_port);
+ if (peer_port) {
+ *peer_port = ntohs(remote_in->sin_port);
}
- if (server_host) {
- *server_host = malloc(INET6_ADDRSTRLEN);
- inet_ntop(AF_INET, &(remote_in->sin_addr), *server_host, INET6_ADDRSTRLEN);
+ if (peer_host) {
+ *peer_host = malloc(INET6_ADDRSTRLEN);
+ inet_ntop(AF_INET, &(remote_in->sin_addr), *peer_host, INET6_ADDRSTRLEN);
}
} else if (remote.ss_family == AF_INET6) {
struct sockaddr_in6 *remote_in = (struct sockaddr_in6 *)&remote;
- if (server_port) {
- *server_port = ntohs(remote_in->sin6_port);
+ if (peer_port) {
+ *peer_port = ntohs(remote_in->sin6_port);
}
- if (server_host) {
- *server_host = malloc(INET6_ADDRSTRLEN);
- inet_ntop(AF_INET6, &(remote_in->sin6_addr), *server_host, INET6_ADDRSTRLEN);
+ if (peer_host) {
+ *peer_host = malloc(INET6_ADDRSTRLEN);
+ inet_ntop(AF_INET6, &(remote_in->sin6_addr), *peer_host, INET6_ADDRSTRLEN);
}
}
diff --git a/src/session_client_ssh.c b/src/session_client_ssh.c
index 613a5de..086b830 100644
--- a/src/session_client_ssh.c
+++ b/src/session_client_ssh.c
@@ -943,7 +943,7 @@
}
/* create and assign communication socket */
- sock = nc_connect_getsocket(host, port);
+ sock = nc_sock_connect(host, port);
if (sock == -1) {
goto fail;
}
@@ -1029,7 +1029,7 @@
ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host);
/* create and connect socket */
- sock = nc_connect_getsocket(host, port);
+ sock = nc_sock_connect(host, port);
if (sock == -1) {
goto fail;
}
@@ -1192,7 +1192,7 @@
port = NC_PORT_CH_SSH;
}
- sock = nc_callhome_accept_connection(port, timeout, NULL, &server_host);
+ sock = nc_sock_accept(port, timeout, &server_host, NULL);
if (sock == -1) {
return NULL;
}
diff --git a/src/session_client_tls.c b/src/session_client_tls.c
index c88cb03..085c71e 100644
--- a/src/session_client_tls.c
+++ b/src/session_client_tls.c
@@ -255,7 +255,7 @@
}
/* create and assign socket */
- sock = nc_connect_getsocket(host, port);
+ sock = nc_sock_connect(host, port);
if (sock == -1) {
goto fail;
}
@@ -378,7 +378,7 @@
port = NC_PORT_CH_TLS;
}
- sock = nc_callhome_accept_connection(port, timeout, NULL, &server_host);
+ sock = nc_sock_accept(port, timeout, &server_host, NULL);
if (sock == -1) {
return NULL;
}
diff --git a/src/session_p.h b/src/session_p.h
index 48b5237..c4ea43b 100644
--- a/src/session_p.h
+++ b/src/session_p.h
@@ -259,15 +259,6 @@
int nc_ctx_check_and_fill(struct nc_session *session);
/**
- * @brief Create and connect a socket.
- *
- * @param[in] host Hostname to connect to.
- * @param[in] port Port to connect on.
- * @return Connected socket or -1 on error.
- */
-int nc_connect_getsocket(const char *host, unsigned short port);
-
-/**
* @brief Perform NETCONF handshake on \p session.
*
* @param[in] session NETCONF session to use.
@@ -276,15 +267,24 @@
int nc_handshake(struct nc_session *session);
/**
- * @brief Accept a new (Call Home) connection.
+ * @brief Create a socket connection.
+ *
+ * @param[in] host Hostname to connect to.
+ * @param[in] port Port to connect on.
+ * @return Connected socket or -1 on error.
+ */
+int nc_sock_connect(const char *host, uint16_t port);
+
+/**
+ * @brief Accept a new socket connection.
*
* @param[in] port Port to listen on.
- * @param[in] timeout Timeout in msec.
- * @param[out] server_port Port the new connection is connected on. Can be NULL.
- * @param[out] server_host Host the new connection was initiated from. Can be NULL.
+ * @param[in] timeout Timeout in milliseconds.
+ * @param[out] peer_host Host the new connection was initiated from. Can be NULL.
+ * @param[out] peer_port Port the new connection is connected on. Can be NULL.
* @return Connected socket with the new connection, -1 on error.
*/
-int nc_callhome_accept_connection(uint16_t port, int32_t timeout, uint16_t *server_port, char **server_host);
+int nc_sock_accept(uint16_t port, int timeout, char **peer_host, uint16_t *peer_port);
/**
* @brief Create a listening socket.
@@ -293,7 +293,7 @@
* @param[in] port Port to listen on.
* @return Listening socket, -1 on error.
*/
-int nc_sock_listen(const char *address, uint32_t port);
+int nc_sock_listen(const char *address, uint16_t port);
/**
* @brief Accept a new connection on a listening socket.
@@ -306,7 +306,7 @@
* @param[out] port Port of the new connection. Can be NULL.
* @return Accepted socket of the new connection, -1 on error.
*/
-int nc_sock_accept(struct nc_bind *binds, uint16_t bind_count, int timeout, NC_TRANSPORT_IMPL *ti, char **host, uint16_t *port);
+int nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, NC_TRANSPORT_IMPL *ti, char **host, uint16_t *port);
#ifdef ENABLE_SSH
diff --git a/src/session_server.c b/src/session_server.c
index e3eeb15..bc75fe1 100644
--- a/src/session_server.c
+++ b/src/session_server.c
@@ -53,7 +53,7 @@
}
int
-nc_sock_listen(const char *address, uint32_t port)
+nc_sock_listen(const char *address, uint16_t port)
{
const int optVal = 1;
const socklen_t optLen = sizeof(optVal);
@@ -131,7 +131,7 @@
}
int
-nc_sock_accept(struct nc_bind *binds, uint16_t bind_count, int timeout, NC_TRANSPORT_IMPL *ti, char **host, uint16_t *port)
+nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, NC_TRANSPORT_IMPL *ti, char **host, uint16_t *port)
{
uint16_t i;
struct pollfd *pfd;
@@ -865,7 +865,7 @@
/* LOCK */
pthread_mutex_lock(&server_opts.bind_lock);
- ret = nc_sock_accept(server_opts.binds, server_opts.bind_count, timeout, &ti, &host, &port);
+ ret = nc_sock_accept_binds(server_opts.binds, server_opts.bind_count, timeout, &ti, &host, &port);
/* UNLOCK */
pthread_mutex_unlock(&server_opts.bind_lock);