X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcnet.c;h=d083656eba63e379357d64acdc60848ec9aad701;hb=c257b555225193e54d85daf541d29578b3c93882;hp=07207030c044be91c64b6a814bc604bc1b681080;hpb=1a6aee188b05a46880d3362af24d9b36dadd12e3;p=silc.git diff --git a/lib/silcutil/silcnet.c b/lib/silcutil/silcnet.c index 07207030..d083656e 100644 --- a/lib/silcutil/silcnet.c +++ b/lib/silcutil/silcnet.c @@ -246,8 +246,12 @@ bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len) freeaddrinfo(ai); #else struct hostent *hp; + unsigned char a[16]; - hp = gethostbyaddr(addr, strlen(addr), AF_INET); + if (!silc_net_addr2bin(addr, a, sizeof(a))) + return FALSE; + + hp = gethostbyaddr(a, 4, AF_INET); if (!hp) return FALSE; if (name_len < strlen(hp->h_name)) @@ -288,7 +292,8 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) struct sockaddr_storage remote; char s[NI_MAXHOST]; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving remote hostname and IP address")); @@ -309,8 +314,10 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) return FALSE; #else struct sockaddr_in remote; + char *host_ip; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving remote hostname and IP address")); @@ -330,19 +337,22 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) return FALSE; #endif - /* Get host by address */ - if (!silc_net_gethostbyaddr(*ip, host, sizeof(host))) - return FALSE; + /* Do reverse lookup if we want hostname too. */ + if (hostname) { + /* Get host by address */ + if (!silc_net_gethostbyaddr(*ip, host, sizeof(host))) + return FALSE; - *hostname = silc_memdup(host, strlen(host)); - SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname)); + *hostname = silc_memdup(host, strlen(host)); + SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname)); - /* Reverse */ - if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) - return FALSE; + /* Reverse */ + if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) + return FALSE; - if (strcmp(*ip, host)) - return FALSE; + if (strcmp(*ip, host)) + return FALSE; + } SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip)); return TRUE; @@ -360,7 +370,8 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) struct sockaddr_storage local; char s[NI_MAXHOST]; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving local hostname and IP address")); @@ -381,8 +392,10 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) return FALSE; #else struct sockaddr_in local; + char *host_ip; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving local hostname and IP address")); @@ -402,19 +415,22 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) return FALSE; #endif - /* Get host by address */ - if (!silc_net_gethostbyaddr(*ip, host, sizeof(host))) - return FALSE; + /* Do reverse lookup if we want hostname too. */ + if (hostname) { + /* Get host by address */ + if (!silc_net_gethostbyaddr(*ip, host, sizeof(host))) + return FALSE; - *hostname = silc_memdup(host, strlen(host)); - SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname)); + *hostname = silc_memdup(host, strlen(host)); + SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname)); - /* Reverse */ - if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) - return FALSE; + /* Reverse */ + if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) + return FALSE; - if (strcmp(*ip, host)) - return FALSE; + if (strcmp(*ip, host)) + return FALSE; + } SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip)); return TRUE;