mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-11 18:50:28 +09:00
Add more error checks to bind_socket().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@647 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
@@ -63,18 +63,27 @@ inline char *hstrerror(int h_errno_val) {
|
|||||||
|
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
// TODO: must use getaddrinfo
|
/* TODO FIXME: must use getaddrinfo */
|
||||||
static socket_t bind_socket(SSH_BIND *ssh_bind, const char *hostname,
|
static socket_t bind_socket(SSH_BIND *ssh_bind, const char *hostname,
|
||||||
int port) {
|
int port) {
|
||||||
struct sockaddr_in myaddr;
|
struct sockaddr_in myaddr;
|
||||||
int opt = 1;
|
|
||||||
socket_t s = socket(PF_INET, SOCK_STREAM, 0);
|
|
||||||
struct hostent *hp=NULL;
|
struct hostent *hp=NULL;
|
||||||
|
socket_t s;
|
||||||
|
int opt = 1;
|
||||||
|
|
||||||
|
s = socket(PF_INET, SOCK_STREAM, 0);
|
||||||
|
if (s < 0) {
|
||||||
|
ssh_set_error(ssh_bind, SSH_FATAL, "%s", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef HAVE_GETHOSTBYNAME
|
#ifdef HAVE_GETHOSTBYNAME
|
||||||
hp = gethostbyname(hostname);
|
hp = gethostbyname(hostname);
|
||||||
#endif
|
#endif
|
||||||
if(!hp){
|
|
||||||
ssh_set_error(ssh_bind,SSH_FATAL,"resolving %s: %s",hostname,hstrerror(h_errno));
|
if (hp == NULL) {
|
||||||
|
ssh_set_error(ssh_bind, SSH_FATAL,
|
||||||
|
"Resolving %s: %s", hostname, hstrerror(h_errno));
|
||||||
close(s);
|
close(s);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -83,13 +92,23 @@ static socket_t bind_socket(SSH_BIND *ssh_bind, const char *hostname,
|
|||||||
memcpy(&myaddr.sin_addr, hp->h_addr, hp->h_length);
|
memcpy(&myaddr.sin_addr, hp->h_addr, hp->h_length);
|
||||||
myaddr.sin_family = hp->h_addrtype;
|
myaddr.sin_family = hp->h_addrtype;
|
||||||
myaddr.sin_port = htons(port);
|
myaddr.sin_port = htons(port);
|
||||||
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
|
|
||||||
|
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0) {
|
||||||
|
ssh_set_error(ssh_bind, SSH_FATAL,
|
||||||
|
"Setting socket options failed: %s", hstrerror(h_errno));
|
||||||
|
close(s);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (bind(s, (struct sockaddr *) &myaddr, sizeof(myaddr)) < 0) {
|
if (bind(s, (struct sockaddr *) &myaddr, sizeof(myaddr)) < 0) {
|
||||||
ssh_set_error(ssh_bind,SSH_FATAL,"Binding to %s:%d : %s",hostname,port,
|
ssh_set_error(ssh_bind, SSH_FATAL, "Binding to %s:%d: %s",
|
||||||
|
hostname,
|
||||||
|
port,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
close(s);
|
close(s);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user