1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-12-17 06:18:58 +03:00

reformat: bind.c

reformat: remove unneeded free

Signed-off-by: Gauravsingh Sisodia <xaerru@gmail.com>
Reviewed-by: Sahana Prasad <sahana@redhat.com>
This commit is contained in:
Gauravsingh Sisodia
2024-03-08 17:41:55 +00:00
committed by Sahana Prasad
parent fcd63abb6a
commit b9d4e11456

View File

@@ -225,90 +225,105 @@ static int ssh_bind_import_keys(ssh_bind sshbind) {
return SSH_OK; return SSH_OK;
} }
int ssh_bind_listen(ssh_bind sshbind) { int ssh_bind_listen(ssh_bind sshbind)
const char *host; {
socket_t fd; const char *host = NULL;
int rc; socket_t fd;
int rc;
if (sshbind->rsa == NULL && /* Apply global bind configurations, if it hasn't been applied before */
sshbind->ecdsa == NULL && rc = ssh_bind_options_parse_config(sshbind, NULL);
sshbind->ed25519 == NULL) { if (rc != 0) {
rc = ssh_bind_import_keys(sshbind); ssh_set_error(sshbind, SSH_FATAL, "Could not parse global config");
if (rc != SSH_OK) { return SSH_ERROR;
return SSH_ERROR; }
}
}
if (sshbind->bindfd == SSH_INVALID_SOCKET) { /* Set default hostkey paths if no hostkey was found before */
host = sshbind->bindaddr; if (sshbind->ecdsakey == NULL &&
if (host == NULL) { sshbind->rsakey == NULL &&
host = "0.0.0.0"; sshbind->ed25519key == NULL) {
}
fd = bind_socket(sshbind, host, sshbind->bindport); sshbind->ecdsakey = strdup("/etc/ssh/ssh_host_ecdsa_key");
if (fd == SSH_INVALID_SOCKET) { sshbind->rsakey = strdup("/etc/ssh/ssh_host_rsa_key");
ssh_key_free(sshbind->rsa); sshbind->ed25519key = strdup("/etc/ssh/ssh_host_ed25519_key");
sshbind->rsa = NULL; }
/* XXX should this clear also other structures that were allocated */
return -1;
}
if (listen(fd, 10) < 0) { if (sshbind->rsa == NULL &&
char err_msg[SSH_ERRNO_MSG_MAX] = {0}; sshbind->ecdsa == NULL &&
ssh_set_error(sshbind, SSH_FATAL, sshbind->ed25519 == NULL) {
"Listening to socket %d: %s", rc = ssh_bind_import_keys(sshbind);
fd, ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX)); if (rc != SSH_OK) {
CLOSE_SOCKET(fd); return SSH_ERROR;
ssh_key_free(sshbind->rsa); }
sshbind->rsa = NULL; }
/* XXX should this clear also other structures that were allocated */
return -1;
}
sshbind->bindfd = fd; if (sshbind->bindfd == SSH_INVALID_SOCKET) {
host = sshbind->bindaddr;
if (host == NULL) {
host = "0.0.0.0";
}
fd = bind_socket(sshbind, host, sshbind->bindport);
if (fd == SSH_INVALID_SOCKET) {
return SSH_ERROR;
}
if (listen(fd, 10) < 0) {
char err_msg[SSH_ERRNO_MSG_MAX] = {0};
ssh_set_error(sshbind,
SSH_FATAL,
"Listening to socket %d: %s",
fd,
ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
CLOSE_SOCKET(fd);
return SSH_ERROR;
}
sshbind->bindfd = fd;
} else { } else {
SSH_LOG(SSH_LOG_DEBUG, "Using app-provided bind socket"); SSH_LOG(SSH_LOG_DEBUG, "Using app-provided bind socket");
} }
return 0; return 0;
} }
int ssh_bind_set_callbacks(ssh_bind sshbind, ssh_bind_callbacks callbacks, int ssh_bind_set_callbacks(ssh_bind sshbind, ssh_bind_callbacks callbacks, void *userdata)
void *userdata){ {
if (sshbind == NULL) { if (sshbind == NULL) {
return SSH_ERROR; return SSH_ERROR;
} }
if (callbacks == NULL) { if (callbacks == NULL) {
ssh_set_error_invalid(sshbind); ssh_set_error_invalid(sshbind);
return SSH_ERROR; return SSH_ERROR;
} }
if(callbacks->size <= 0 || callbacks->size > 1024 * sizeof(void *)){ if (callbacks->size <= 0 || callbacks->size > 1024 * sizeof(void *)) {
ssh_set_error(sshbind,SSH_FATAL, ssh_set_error(sshbind,
"Invalid callback passed in (badly initialized)"); SSH_FATAL,
return SSH_ERROR; "Invalid callback passed in (badly initialized)");
} return SSH_ERROR;
sshbind->bind_callbacks = callbacks; }
sshbind->bind_callbacks_userdata=userdata; sshbind->bind_callbacks = callbacks;
return 0; sshbind->bind_callbacks_userdata = userdata;
return 0;
} }
/** @internal /** @internal
* @brief callback being called by poll when an event happens * @brief callback being called by poll when an event happens
* *
*/ */
static int ssh_bind_poll_callback(ssh_poll_handle sshpoll, static int ssh_bind_poll_callback(ssh_poll_handle sshpoll, socket_t fd, int revents, void *user)
socket_t fd, int revents, void *user){ {
ssh_bind sshbind=(ssh_bind)user; ssh_bind sshbind = (ssh_bind)user;
(void)sshpoll; (void)sshpoll;
(void)fd; (void)fd;
if(revents & POLLIN){ if (revents & POLLIN) {
/* new incoming connection */ /* new incoming connection */
if(ssh_callbacks_exists(sshbind->bind_callbacks,incoming_connection)){ if (ssh_callbacks_exists(sshbind->bind_callbacks, incoming_connection)) {
sshbind->bind_callbacks->incoming_connection(sshbind, sshbind->bind_callbacks->incoming_connection(sshbind,
sshbind->bind_callbacks_userdata); sshbind->bind_callbacks_userdata);
}
} }
} return 0;
return 0;
} }
/** @internal /** @internal
@@ -336,20 +351,24 @@ ssh_poll_handle ssh_bind_get_poll(ssh_bind sshbind)
return sshbind->poll; return sshbind->poll;
} }
void ssh_bind_set_blocking(ssh_bind sshbind, int blocking) { void ssh_bind_set_blocking(ssh_bind sshbind, int blocking)
sshbind->blocking = blocking ? 1 : 0; {
sshbind->blocking = blocking ? 1 : 0;
} }
socket_t ssh_bind_get_fd(ssh_bind sshbind) { socket_t ssh_bind_get_fd(ssh_bind sshbind)
return sshbind->bindfd; {
return sshbind->bindfd;
} }
void ssh_bind_set_fd(ssh_bind sshbind, socket_t fd) { void ssh_bind_set_fd(ssh_bind sshbind, socket_t fd)
sshbind->bindfd = fd; {
sshbind->bindfd = fd;
} }
void ssh_bind_fd_toaccept(ssh_bind sshbind) { void ssh_bind_fd_toaccept(ssh_bind sshbind)
sshbind->toaccept = 1; {
sshbind->toaccept = 1;
} }
void ssh_bind_free(ssh_bind sshbind){ void ssh_bind_free(ssh_bind sshbind){