void dns_lookup(CLIENT_ENTRY *entry, const char *domain, int port) { const char *myname = "dns_lookup"; SERVICE *service = entry->service; if (acl_is_ip(domain)) { entry->ip_idx = 0; ACL_SAFE_STRNCPY(entry->dns_ctx.ip[0], domain, sizeof(entry->dns_ctx.ip[0])); entry->dns_ctx.port[0] = port; entry->dns_ctx.ip_cnt = 1; entry->nslookup_notify_fn(entry, NSLOOKUP_OK); return; } else if (service->dns_handle) { inner_nslookup(service, entry, domain, port); } else if (service->dns_server) { thrpool_nslookup(service, entry, domain, port); } else { acl_msg_fatal("%s(%d): no dns lookup set", myname, __LINE__); } }
ACL_DNS_DB *acl_gethostbyname(const char *name, int *h_error) { char myname[] = "acl_gethostbyname"; ACL_DNS_DB *h_dns_db = NULL; ACL_HOSTNAME *h_host; /* #ifndef SUNOS5 */ struct hostent *h_addrp = NULL; /* #endif */ #ifdef ACL_UNIX # ifndef ACL_MACOSX struct hostent h_buf; int errnum = 0; # endif #endif char **pptr, buf[4096]; int n; #undef ERETURN #define ERETURN(_x_) do { \ if (h_dns_db) \ acl_netdb_free(h_dns_db); \ return (_x_); \ } while (0) if (name == NULL) { acl_msg_error("%s, %s(%d): input error", __FILE__, myname, __LINE__); ERETURN (NULL); } if (h_error) *h_error = 0; /* lookup the local dns cache first */ h_dns_db = acl_netdb_cache_lookup(name); if (h_dns_db) return (h_dns_db); h_dns_db = acl_netdb_new(name); if (h_dns_db == NULL) { acl_msg_error("%s, %s(%d): acl_netdb_new error(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); return (NULL); } if (acl_is_ip(name) == 0) { h_host = acl_mycalloc(1, sizeof(ACL_HOSTNAME)); if (h_host == NULL) { acl_msg_error("%s, %s(%d): calloc error(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); ERETURN (NULL); } h_host->saddr.sin_addr.s_addr = inet_addr(name); ACL_SAFE_STRNCPY(h_host->ip, name, sizeof(h_host->ip)); if (acl_array_append(h_dns_db->h_db, h_host) < 0) { acl_msg_error("%s, %s(%d): array append error(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); ERETURN (NULL); } h_dns_db->size++; return (h_dns_db); } memset(buf, 0, sizeof(buf)); h_addrp = NULL; #if defined(ACL_MS_WINDOWS) || defined(ACL_MACOSX) h_addrp = gethostbyname(name); if (h_addrp == NULL) { acl_msg_error("%s, %s(%d): gethostbyname error(%s), addr=%s", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf)), name); ERETURN (NULL); } #elif defined(ACL_UNIX) memset(&h_buf, 0, sizeof(h_buf)); # if defined(LINUX2) || defined(ACL_FREEBSD) n = gethostbyname_r(name, &h_buf, buf, sizeof(buf), &h_addrp, &errnum); if (n) { if (h_error) *h_error = errnum; ERETURN (NULL); } # elif defined(SUNOS5) h_addrp = gethostbyname_r(name, &h_buf, buf, sizeof(buf), &errnum); if (h_addrp == NULL) { if (h_error) *h_error = errnum; ERETURN (NULL); } # else # error "unknown OS type" # endif #else # error "unknown OS type" #endif if (h_addrp == NULL || h_addrp->h_addr_list == NULL) { acl_msg_error("%s, %s(%d): null result return(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); ERETURN (NULL); } for (pptr = h_addrp->h_addr_list; *pptr != NULL; pptr++) { h_host = acl_mycalloc(1, sizeof(ACL_HOSTNAME)); if (h_host == NULL) { acl_msg_error("%s, %s(%d): calloc error(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); ERETURN (NULL); } memset(&h_host->saddr, 0, sizeof(h_host->saddr)); n = (int) sizeof(h_host->saddr.sin_addr) > h_addrp->h_length ? h_addrp->h_length : (int) sizeof(h_host->saddr.sin_addr); memcpy(&h_host->saddr.sin_addr, *pptr, n); /* bugifx: 2009.12.8 * this is not thread safe * ACL_SAFE_STRNCPY(h_host->ip, inet_ntoa(h_host->saddr.sin_addr), sizeof(h_host->ip)); */ acl_inet_ntoa(h_host->saddr.sin_addr, h_host->ip, sizeof(h_host->ip)); if (acl_array_append(h_dns_db->h_db, h_host) < 0) { acl_msg_error("%s, %s(%d): array append error(%s)", __FILE__, myname, __LINE__, acl_last_strerror(buf, sizeof(buf))); ERETURN (NULL); } h_dns_db->size++; } acl_netdb_cache_push(h_dns_db, 0); return (h_dns_db); }
ACL_IFCONF *acl_get_ifaddrs() { const char *myname = "acl_get_ifaddrs"; IP_ADAPTER_INFO info_temp, *infos, *info; ACL_IFCONF *ifconf; ULONG len = 0; int j; if (GetAdaptersInfo(&info_temp, &len) != ERROR_BUFFER_OVERFLOW) { acl_msg_error("%s(%d): GetAdaptersInfo eror(%s)", myname, __LINE__, acl_last_serror()); return (NULL); } infos = (IP_ADAPTER_INFO *) acl_mymalloc(len); if (GetAdaptersInfo(infos, &len) != NO_ERROR) { acl_msg_error("%s(%d): GetAdaptersInfo eror(%s)", myname, __LINE__, acl_last_serror()); acl_myfree(infos); return (NULL); } ifconf = (ACL_IFCONF*) acl_mymalloc(sizeof(ACL_IFCONF)); ifconf->length = len / sizeof(IP_ADAPTER_INFO) + 1; ifconf->addrs = (ACL_IFADDR*) acl_mycalloc(ifconf->length, sizeof(ACL_IFADDR)); for (info = infos, j = 0; info != NULL; info = info->Next) { if (info->Type == MIB_IF_TYPE_LOOPBACK) continue; if (strcmp(info->IpAddressList.IpAddress.String, "0.0.0.0") == 0) continue; if (acl_is_ip(info->IpAddressList.IpAddress.String) < 0) continue; ifconf->addrs[j].name = acl_mystrdup(info->AdapterName); ifconf->addrs[j].desc = acl_mystrdup(info->Description); snprintf(ifconf->addrs[j].ip, sizeof(ifconf->addrs[j].ip), "%s", info->IpAddressList.IpAddress.String); ifconf->addrs[j].addr = inet_addr(ifconf->addrs[j].ip); j++; if (j == ifconf->length) { ifconf->length *= 2; ifconf->addrs = (ACL_IFADDR*) acl_myrealloc(ifconf->addrs, ifconf->length * sizeof(ACL_IFADDR)); } } acl_myfree(infos); if (j == 0) { acl_myfree(ifconf->addrs); acl_myfree(ifconf); return (NULL); } ifconf->length = j; /* reset the ifconf->length */ /* set the iterator callback */ ifconf->iter_head = ifaddrs_iter_head; ifconf->iter_next = ifaddrs_iter_next; ifconf->iter_tail = ifaddrs_iter_tail; ifconf->iter_prev = ifaddrs_iter_prev; return (ifconf); }
ACL_IFCONF *acl_get_ifaddrs() { const char *myname = "acl_get_ifaddrs"; IP_ADAPTER_INFO info_temp, *infos, *info; ACL_IFCONF *ifconf; ULONG len = 0; int j; HMODULE hInst; PGAINFO *pGAInfo; hInst = LoadLibrary("iphlpapi.dll"); if(!hInst) { MessageBox(0, "iphlpapi.dll not supported in this platform!", "Error", 0); return (NULL); } pGAInfo = (PGAINFO*) GetProcAddress(hInst,"GetAdaptersInfo"); if (pGAInfo == NULL) { MessageBox(0, "can't find GetAdaptersInfo function!", "Error", 0); return (NULL); } if (pGAInfo(&info_temp, &len) != ERROR_BUFFER_OVERFLOW) { acl_msg_error("%s(%d): GetAdaptersInfo eror(%s)", myname, __LINE__, acl_last_serror()); FreeLibrary(hInst); return (NULL); } infos = (IP_ADAPTER_INFO *) acl_mymalloc(len); if (pGAInfo(infos, &len) != NO_ERROR) { acl_msg_error("%s(%d): GetAdaptersInfo eror(%s)", myname, __LINE__, acl_last_serror()); acl_myfree(infos); FreeLibrary(hInst); return (NULL); } ifconf = (ACL_IFCONF*) acl_mymalloc(sizeof(ACL_IFCONF)); ifconf->length = len / sizeof(IP_ADAPTER_INFO) + 1; ifconf->addrs = (ACL_IFADDR*) acl_mycalloc(ifconf->length, sizeof(ACL_IFADDR)); for (info = infos, j = 0; info != NULL; info = info->Next) { if (info->Type == MIB_IF_TYPE_LOOPBACK) continue; if (strcmp(info->IpAddressList.IpAddress.String, "0.0.0.0") == 0) continue; if (acl_is_ip(info->IpAddressList.IpAddress.String) < 0) continue; ifconf->addrs[j].name = acl_mystrdup(info->AdapterName); ifconf->addrs[j].desc = acl_mystrdup(info->Description); snprintf(ifconf->addrs[j].ip, sizeof(ifconf->addrs[j].ip), "%s", info->IpAddressList.IpAddress.String); ifconf->addrs[j].addr = inet_addr(ifconf->addrs[j].ip); j++; if (j == ifconf->length) { ifconf->length *= 2; ifconf->addrs = (ACL_IFADDR*) acl_myrealloc(ifconf->addrs, ifconf->length * sizeof(ACL_IFADDR)); } } acl_myfree(infos); if (j == 0) { acl_myfree(ifconf->addrs); acl_myfree(ifconf); FreeLibrary(hInst); return (NULL); } ifconf->length = j; /* reset the ifconf->length */ /* set the iterator callback */ ifconf->iter_head = ifaddrs_iter_head; ifconf->iter_next = ifaddrs_iter_next; ifconf->iter_tail = ifaddrs_iter_tail; ifconf->iter_prev = ifaddrs_iter_prev; FreeLibrary(hInst); return (ifconf); }