aboutsummaryrefslogtreecommitdiff
path: root/libssh
diff options
context:
space:
mode:
authorAndreas Schneider <mail@cynapses.org>2009-04-29 11:45:56 +0000
committerAndreas Schneider <mail@cynapses.org>2009-04-29 11:45:56 +0000
commitafeaea318cbf46810c24a08367f233deaab2f91c (patch)
tree7a757fd8de0da143c2f6312a8941552f57a510ee /libssh
parent29035f952cdd8917dd1436cb9ee95561ee3f7e0e (diff)
downloadlibssh-afeaea318cbf46810c24a08367f233deaab2f91c.tar.gz
libssh-afeaea318cbf46810c24a08367f233deaab2f91c.tar.xz
libssh-afeaea318cbf46810c24a08367f233deaab2f91c.zip
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
Diffstat (limited to 'libssh')
-rw-r--r--libssh/server.c65
1 files changed, 42 insertions, 23 deletions
diff --git a/libssh/server.c b/libssh/server.c
index 6cf6a78..e470c7c 100644
--- a/libssh/server.c
+++ b/libssh/server.c
@@ -63,34 +63,53 @@ inline char *hstrerror(int h_errno_val) {
#endif /* _WIN32 */
-// TODO: must use getaddrinfo
+/* TODO FIXME: must use getaddrinfo */
static socket_t bind_socket(SSH_BIND *ssh_bind, const char *hostname,
int port) {
- struct sockaddr_in myaddr;
- int opt = 1;
- socket_t s = socket(PF_INET, SOCK_STREAM, 0);
- struct hostent *hp=NULL;
+ struct sockaddr_in myaddr;
+ 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
- hp=gethostbyname(hostname);
+ hp = gethostbyname(hostname);
#endif
- if(!hp){
- ssh_set_error(ssh_bind,SSH_FATAL,"resolving %s: %s",hostname,hstrerror(h_errno));
- close(s);
- return -1;
- }
- memset(&myaddr, 0, sizeof(myaddr));
- memcpy(&myaddr.sin_addr,hp->h_addr,hp->h_length);
- myaddr.sin_family=hp->h_addrtype;
- myaddr.sin_port = htons(port);
- setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
- if (bind(s, (struct sockaddr *) &myaddr, sizeof(myaddr)) < 0) {
- ssh_set_error(ssh_bind,SSH_FATAL,"Binding to %s:%d : %s",hostname,port,
- strerror(errno));
- close(s);
- return -1;
- }
- return s;
+ if (hp == NULL) {
+ ssh_set_error(ssh_bind, SSH_FATAL,
+ "Resolving %s: %s", hostname, hstrerror(h_errno));
+ close(s);
+ return -1;
+ }
+
+ memset(&myaddr, 0, sizeof(myaddr));
+ memcpy(&myaddr.sin_addr, hp->h_addr, hp->h_length);
+ myaddr.sin_family = hp->h_addrtype;
+ myaddr.sin_port = htons(port);
+
+ 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) {
+ ssh_set_error(ssh_bind, SSH_FATAL, "Binding to %s:%d: %s",
+ hostname,
+ port,
+ strerror(errno));
+ close(s);
+ return -1;
+ }
+
+ return s;
}
SSH_BIND *ssh_bind_new(void){