X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcnet.c;h=d083656eba63e379357d64acdc60848ec9aad701;hb=c257b555225193e54d85daf541d29578b3c93882;hp=630f93292d485b5ebc39ef35b21c161f20f7ffd3;hpb=7910ce2a5c55211a4ea09a52832c6ed32c0b64fd;p=silc.git diff --git a/lib/silcutil/silcnet.c b/lib/silcutil/silcnet.c index 630f9329..d083656e 100644 --- a/lib/silcutil/silcnet.c +++ b/lib/silcutil/silcnet.c @@ -115,14 +115,15 @@ SILC_TASK_CALLBACK(silc_net_resolve_completion) static void *silc_net_gethostbyname_thread(void *context) { SilcNetResolveContext r = (SilcNetResolveContext)context; + SilcSchedule schedule = r->schedule; char tmp[64]; if (silc_net_gethostbyname(r->input, r->prefer_ipv6, tmp, sizeof(tmp))) r->result = strdup(tmp); - silc_schedule_task_add(r->schedule, 0, silc_net_resolve_completion, r, 0, 1, + silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL); - silc_schedule_wakeup(r->schedule); + silc_schedule_wakeup(schedule); return NULL; } @@ -131,21 +132,22 @@ static void *silc_net_gethostbyname_thread(void *context) static void *silc_net_gethostbyaddr_thread(void *context) { SilcNetResolveContext r = (SilcNetResolveContext)context; + SilcSchedule schedule = r->schedule; char tmp[256]; if (silc_net_gethostbyaddr(r->input, tmp, sizeof(tmp))) r->result = strdup(tmp); - silc_schedule_task_add(r->schedule, 0, silc_net_resolve_completion, r, 0, 1, + silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL); - silc_schedule_wakeup(r->schedule); + silc_schedule_wakeup(schedule); return NULL; } /* Resolves IP address for hostname. */ bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address, - uint32 address_len) + SilcUInt32 address_len) { #ifdef HAVE_IPV6 struct addrinfo hints, *ai, *tmp, *ip4 = NULL, *ip6 = NULL; @@ -226,7 +228,7 @@ void silc_net_gethostbyname_async(const char *name, /* Resolves hostname by IP address. */ -bool silc_net_gethostbyaddr(const char *addr, char *name, uint32 name_len) +bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len) { #ifdef HAVE_IPV6 struct addrinfo req, *ai; @@ -244,8 +246,12 @@ bool silc_net_gethostbyaddr(const char *addr, char *name, uint32 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)) @@ -279,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; } @@ -344,69 +363,82 @@ 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; } /* Return remote port by socket. */ -uint16 silc_net_get_remote_port(int sock) +SilcUInt16 silc_net_get_remote_port(int sock) { #ifdef HAVE_IPV6 struct sockaddr_storage remote; @@ -419,7 +451,7 @@ uint16 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); @@ -438,7 +470,7 @@ uint16 silc_net_get_remote_port(int sock) /* Return local port by socket. */ -uint16 silc_net_get_local_port(int sock) +SilcUInt16 silc_net_get_local_port(int sock) { #ifdef HAVE_IPV6 struct sockaddr_storage local; @@ -451,7 +483,7 @@ uint16 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);