mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-08-10 06:23:01 +03:00
bind: Correctly close sockets and invalidate them.
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
@@ -109,11 +109,15 @@
|
||||
struct timeval;
|
||||
int gettimeofday(struct timeval *__p, void *__t);
|
||||
|
||||
#define _XCLOSESOCKET closesocket
|
||||
|
||||
#else /* _WIN32 */
|
||||
|
||||
#include <unistd.h>
|
||||
#define PRIdS "zd"
|
||||
|
||||
#define _XCLOSESOCKET close
|
||||
|
||||
#endif /* _WIN32 */
|
||||
|
||||
#include "libssh/libssh.h"
|
||||
@@ -332,5 +336,7 @@ int match_hostname(const char *host, const char *pattern, unsigned int len);
|
||||
#define __VA_NARG__(...) (-1)
|
||||
#endif
|
||||
|
||||
#define CLOSE_SOCKET(s) do { if ((s) != SSH_INVALID_SOCKET) { _XCLOSESOCKET(s); (s) = SSH_INVALID_SOCKET;} } while(0)
|
||||
|
||||
#endif /* _LIBSSH_PRIV_H */
|
||||
/* vim: set ts=4 sw=4 et cindent: */
|
||||
|
18
src/bind.c
18
src/bind.c
@@ -109,7 +109,7 @@ static socket_t bind_socket(ssh_bind sshbind, const char *hostname,
|
||||
"Setting socket options failed: %s",
|
||||
strerror(errno));
|
||||
freeaddrinfo (ai);
|
||||
close(s);
|
||||
CLOSE_SOCKET(s);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ static socket_t bind_socket(ssh_bind sshbind, const char *hostname,
|
||||
port,
|
||||
strerror(errno));
|
||||
freeaddrinfo (ai);
|
||||
close(s);
|
||||
CLOSE_SOCKET(s);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -259,7 +259,7 @@ int ssh_bind_listen(ssh_bind sshbind) {
|
||||
ssh_set_error(sshbind, SSH_FATAL,
|
||||
"Listening to socket %d: %s",
|
||||
fd, strerror(errno));
|
||||
close(fd);
|
||||
CLOSE_SOCKET(fd);
|
||||
ssh_key_free(sshbind->dsa);
|
||||
sshbind->dsa = NULL;
|
||||
ssh_key_free(sshbind->rsa);
|
||||
@@ -350,11 +350,7 @@ void ssh_bind_free(ssh_bind sshbind){
|
||||
}
|
||||
|
||||
if (sshbind->bindfd >= 0) {
|
||||
#ifdef _WIN32
|
||||
closesocket(sshbind->bindfd);
|
||||
#else
|
||||
close(sshbind->bindfd);
|
||||
#endif
|
||||
CLOSE_SOCKET(sshbind->bindfd);
|
||||
}
|
||||
sshbind->bindfd = SSH_INVALID_SOCKET;
|
||||
|
||||
@@ -499,11 +495,7 @@ int ssh_bind_accept(ssh_bind sshbind, ssh_session session) {
|
||||
rc = ssh_bind_accept_fd(sshbind, session, fd);
|
||||
|
||||
if(rc == SSH_ERROR){
|
||||
#ifdef _WIN32
|
||||
closesocket(fd);
|
||||
#else
|
||||
close(fd);
|
||||
#endif
|
||||
CLOSE_SOCKET(fd);
|
||||
ssh_socket_free(session->socket);
|
||||
}
|
||||
return rc;
|
||||
|
Reference in New Issue
Block a user