1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-29 01:03:57 +03:00

server: ssh_bind_accept_fd

This function will not call accept() but use function parameter
instead
This commit is contained in:
Aris Adamantiadis
2011-10-13 22:23:48 +02:00
parent 85cc582d4a
commit 551a0c855b
2 changed files with 85 additions and 52 deletions

View File

@@ -224,6 +224,21 @@ LIBSSH_API void ssh_bind_fd_toaccept(ssh_bind ssh_bind_o);
*/
LIBSSH_API int ssh_bind_accept(ssh_bind ssh_bind_o, ssh_session session);
/**
* @brief Accept an incoming ssh connection on the given file descriptor
* and initialize the session.
*
* @param ssh_bind_o The ssh server bind to accept a connection.
* @param session A preallocated ssh session
* @param fd A file descriptor of an already established TCP
* inbound connection
* @see ssh_new
* @see ssh_bind_accept
* @return SSH_OK when a connection is established
*/
LIBSSH_API int ssh_bind_accept_fd(ssh_bind ssh_bind_o, ssh_session session,
socket_t fd);
/**
* @brief Handles the key exchange and set up encryption
*

View File

@@ -331,30 +331,14 @@ void ssh_bind_free(ssh_bind sshbind){
SAFE_FREE(sshbind);
}
int ssh_bind_accept(ssh_bind sshbind, ssh_session session) {
socket_t fd = SSH_INVALID_SOCKET;
int ssh_bind_accept_fd(ssh_bind sshbind, ssh_session session, socket_t fd){
int i;
if (sshbind->bindfd == SSH_INVALID_SOCKET) {
ssh_set_error(sshbind, SSH_FATAL,
"Can't accept new clients on a not bound socket.");
return SSH_ERROR;
}
if (session == NULL){
ssh_set_error(sshbind, SSH_FATAL,"session is null");
return SSH_ERROR;
}
fd = accept(sshbind->bindfd, NULL, NULL);
if (fd == SSH_INVALID_SOCKET) {
ssh_set_error(sshbind, SSH_FATAL,
"Accepting a new connection: %s",
strerror(errno));
return SSH_ERROR;
}
session->server = 1;
session->version = 2;
@@ -404,10 +388,44 @@ int ssh_bind_accept(ssh_bind sshbind, ssh_session session) {
return SSH_ERROR;
}
}
return SSH_OK;
}
int ssh_bind_accept(ssh_bind sshbind, ssh_session session) {
socket_t fd = SSH_INVALID_SOCKET;
int rc;
if (sshbind->bindfd == SSH_INVALID_SOCKET) {
ssh_set_error(sshbind, SSH_FATAL,
"Can't accept new clients on a not bound socket.");
return SSH_ERROR;
}
if (session == NULL){
ssh_set_error(sshbind, SSH_FATAL,"session is null");
return SSH_ERROR;
}
fd = accept(sshbind->bindfd, NULL, NULL);
if (fd == SSH_INVALID_SOCKET) {
ssh_set_error(sshbind, SSH_FATAL,
"Accepting a new connection: %s",
strerror(errno));
return SSH_ERROR;
}
rc = ssh_bind_accept_fd(sshbind, session, fd);
if(rc == SSH_ERROR){
#ifdef _WIN32
closesocket(fd);
#else
close(fd);
#endif
if (session->socket)
ssh_socket_close(session->socket);
}
return rc;
}
/**
* @}