X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcnet.c;h=d083656eba63e379357d64acdc60848ec9aad701;hb=c257b555225193e54d85daf541d29578b3c93882;hp=c685e6375efc9a2c5b45bc08fe1fda234b73787d;hpb=7a89685f88a61fcdc4bd2ef5ee8aebc7de8eead6;p=silc.git diff --git a/lib/silcutil/silcnet.c b/lib/silcutil/silcnet.c index c685e637..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)) @@ -281,63 +285,76 @@ void silc_net_gethostbyaddr_async(const char *addr, bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) { - struct sockaddr_in remote; - struct hostent *dest; - char *host_ip = NULL; - char host_name[1024]; + char host[1024]; int rval, len; - int i; - *hostname = NULL; +#ifdef HAVE_IPV6 + struct sockaddr_storage remote; + char s[NI_MAXHOST]; + + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving remote hostname and IP address")); memset(&remote, 0, sizeof(remote)); + memset(&s, 0, sizeof(s)); len = sizeof(remote); rval = getpeername(sock, (struct sockaddr *)&remote, &len); if (rval < 0) return FALSE; - host_ip = inet_ntoa(remote.sin_addr); - if (!host_ip) + if (getnameinfo((struct sockaddr *)&remote, len, s, sizeof(s), NULL, 0, + NI_NUMERICHOST)) return FALSE; - *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char)); - memcpy(*ip, host_ip, strlen(host_ip)); - - /* Get host by address */ - dest = gethostbyaddr((char *)&remote.sin_addr, - sizeof(struct in_addr), AF_INET); - if (!dest) + *ip = silc_memdup(s, strlen(s)); + if (*ip == NULL) return FALSE; +#else + struct sockaddr_in remote; + char *host_ip; - /* Get same host by name to see that the remote host really is - the who it says it is */ - memset(host_name, 0, sizeof(host_name)); - memcpy(host_name, dest->h_name, strlen(dest->h_name)); + if (hostname) + *hostname = NULL; + *ip = NULL; - *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char)); - memcpy(*hostname, host_name, strlen(host_name)); - SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname)); + SILC_LOG_DEBUG(("Resolving remote hostname and IP address")); + + memset(&remote, 0, sizeof(remote)); + len = sizeof(remote); + rval = getpeername(sock, (struct sockaddr *)&remote, &len); + if (rval < 0) + return FALSE; - dest = gethostbyname(host_name); - if (!dest) + host_ip = inet_ntoa(remote.sin_addr); + if (!host_ip) return FALSE; - /* Find the address from list */ - for (i = 0; dest->h_addr_list[i]; i++) - if (!memcmp(dest->h_addr_list[i], &remote.sin_addr, - sizeof(struct in_addr))) - break; - if (!dest->h_addr_list[i]) + *ip = silc_memdup(host_ip, strlen(host_ip)); + if (*ip == NULL) return FALSE; +#endif - silc_free(*ip); - *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char)); - memcpy(*ip, host_ip, strlen(host_ip)); - SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip)); + /* 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)); + + /* Reverse */ + if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) + return FALSE; + + if (strcmp(*ip, host)) + return FALSE; + } + SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip)); return TRUE; } @@ -346,63 +363,76 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) { - struct sockaddr_in local; - struct hostent *dest; - char *host_ip = NULL; - char host_name[1024]; + char host[1024]; int rval, len; - int i; - *hostname = NULL; +#ifdef HAVE_IPV6 + struct sockaddr_storage local; + char s[NI_MAXHOST]; + + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving local hostname and IP address")); memset(&local, 0, sizeof(local)); + memset(&s, 0, sizeof(s)); len = sizeof(local); rval = getsockname(sock, (struct sockaddr *)&local, &len); if (rval < 0) return FALSE; - host_ip = inet_ntoa(local.sin_addr); - if (!host_ip) + if (getnameinfo((struct sockaddr *)&local, len, s, sizeof(s), NULL, 0, + NI_NUMERICHOST)) return FALSE; - *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char)); - memcpy(*ip, host_ip, strlen(host_ip)); - - /* Get host by address */ - dest = gethostbyaddr((char *)&local.sin_addr, - sizeof(struct in_addr), AF_INET); - if (!dest) + *ip = silc_memdup(s, strlen(s)); + if (*ip == NULL) return FALSE; +#else + struct sockaddr_in local; + char *host_ip; - /* Get same host by name to see that the local host really is - the who it says it is */ - memset(host_name, 0, sizeof(host_name)); - memcpy(host_name, dest->h_name, strlen(dest->h_name)); + if (hostname) + *hostname = NULL; + *ip = NULL; - *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char)); - memcpy(*hostname, host_name, strlen(host_name)); - SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname)); + SILC_LOG_DEBUG(("Resolving local hostname and IP address")); + + memset(&local, 0, sizeof(local)); + len = sizeof(local); + rval = getsockname(sock, (struct sockaddr *)&local, &len); + if (rval < 0) + return FALSE; - dest = gethostbyname(host_name); - if (!dest) + host_ip = inet_ntoa(local.sin_addr); + if (!host_ip) return FALSE; - /* Find the address from list */ - for (i = 0; dest->h_addr_list[i]; i++) - if (!memcmp(dest->h_addr_list[i], &local.sin_addr, - sizeof(struct in_addr))) - break; - if (!dest->h_addr_list[i]) + *ip = silc_memdup(host_ip, strlen(host_ip)); + if (*ip == NULL) return FALSE; +#endif - silc_free(*ip); - *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char)); - memcpy(*ip, host_ip, strlen(host_ip)); - SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip)); + /* 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)); + + /* Reverse */ + if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) + return FALSE; + + if (strcmp(*ip, host)) + return FALSE; + } + + SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip)); return TRUE; } @@ -421,7 +451,7 @@ SilcUInt16 silc_net_get_remote_port(int sock) return 0; if (getnameinfo((struct sockaddr *)&remote, len, NULL, 0, s, sizeof(s), - NI_NUMERICSERV)) + NI_NUMERICSERV)) return 0; return atoi(s); @@ -453,7 +483,7 @@ SilcUInt16 silc_net_get_local_port(int sock) return 0; if (getnameinfo((struct sockaddr *)&local, len, NULL, 0, s, sizeof(s), - NI_NUMERICSERV)) + NI_NUMERICSERV)) return 0; return atoi(s);