char *silc_net_localhost(void) { char hostname[256], ip_addr[64]; if (gethostname(hostname, sizeof(hostname))) return NULL; if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr))) return strdup(hostname); silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname)); return strdup(hostname); }
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(schedule, 0, silc_net_resolve_completion, r, 0, 1, SILC_TASK_TIMEOUT); silc_schedule_wakeup(schedule); return NULL; }
SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname, char **ip) { SilcSymbianSocket *s = (SilcSymbianSocket *)sock; TInetAddr addr; char host[256]; TBuf<64> tmp; if (hostname) *hostname = NULL; *ip = NULL; s->sock->LocalName(addr); addr.Output(tmp); *ip = (char *)silc_memdup(tmp.Ptr(), tmp.Length()); if (*ip == NULL) 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 = (char *)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; }
SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname, char **ip) { char host[1024]; int rval, len; #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; if (getnameinfo((struct sockaddr *)&local, len, s, sizeof(s), NULL, 0, NI_NUMERICHOST)) return FALSE; *ip = silc_memdup(s, strlen(s)); if (*ip == NULL) return FALSE; #else struct sockaddr_in local; char *host_ip; if (hostname) *hostname = NULL; *ip = NULL; 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; host_ip = inet_ntoa(local.sin_addr); if (!host_ip) return FALSE; *ip = silc_memdup(host_ip, strlen(host_ip)); if (*ip == NULL) return FALSE; #endif /* 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; }