static jdns_dnsparams_t *dnsparams_get_winsys() { jdns_dnsparams_t *params; GetNetworkParamsFunc myGetNetworkParams; DWORD ret; HINSTANCE lib; jdns_address_t *addr; jdns_string_t *str; IP_ADDR_STRING *ipstr; lib = LoadLibraryA("iphlpapi"); if(!lib) return 0; params = 0; myGetNetworkParams = (GetNetworkParamsFunc)GetProcAddress(lib, "GetNetworkParams"); if(myGetNetworkParams) { ULONG bufsize = 0; ret = myGetNetworkParams(0, &bufsize); if(ret == ERROR_BUFFER_OVERFLOW) { FIXED_INFO *info = (FIXED_INFO *)jdns_alloc((int)bufsize); ret = myGetNetworkParams(info, &bufsize); if(ret == ERROR_SUCCESS) { params = jdns_dnsparams_new(); ipstr = &info->DnsServerList; while(ipstr) { addr = jdns_address_new(); if(jdns_address_set_cstr(addr, (char *)ipstr->IpAddress.String)) jdns_dnsparams_append_nameserver(params, addr, JDNS_UNICAST_PORT); jdns_address_delete(addr); ipstr = ipstr->Next; } str = jdns_string_new(); jdns_string_set_cstr(str, info->DomainName); if(str->size > 0) jdns_dnsparams_append_domain(params, str); jdns_string_delete(str); } jdns_free(info); } } FreeLibrary(lib); return params; }
static jdns_dnsparams_t *dnsparams_get_unixsys() { int n; jdns_dnsparams_t *params; #ifdef JDNS_MODERN_RES_API struct __res_state res; memset(&res, 0, sizeof(struct __res_state)); n = res_ninit(&res); #define RESVAR res #else n = my_res_init(); #define RESVAR _res #endif params = jdns_dnsparams_new(); // error initializing? if(n == -1) return params; #ifdef USE_INDEP_EXT for(n = 0; n < MAXNS && n < RESVAR.nscount; ++n) { struct sockaddr_in *sa = (struct sockaddr_in*)&(_res_ext.nsaddr_list[n]); jdns_address_t *addr = jdns_address_new(); if (sa->sin_family = AF_INET6) { struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; jdns_address_set_ipv6(addr, sa6->sin6_addr.s6_addr); } else { jdns_address_set_ipv4(addr, ntohl(sa->sin_addr.s_addr)); } jdns_dnsparams_append_nameserver(params, addr, JDNS_UNICAST_PORT); jdns_address_delete(addr); } #else // nameservers - ipv6 #ifdef __GLIBC__ for(n = 0; n < MAXNS; ++n) #else for(n = 0; n < MAXNS && n < RESVAR._u._ext.nscount; ++n) #endif { jdns_address_t *addr; struct sockaddr_in6 *sa6; #ifdef USE_EXTEXT // seems _ext.ext can be null in some cases... if(RESVAR._u._ext.ext == NULL) break; sa6 = ((struct sockaddr_in6 *)RESVAR._u._ext.ext) + n; #else sa6 = RESVAR._u._ext.nsaddrs[n]; #endif if(sa6 == NULL) continue; addr = jdns_address_new(); jdns_address_set_ipv6(addr, sa6->sin6_addr.s6_addr); jdns_dnsparams_append_nameserver(params, addr, JDNS_UNICAST_PORT); jdns_address_delete(addr); } // nameservers - ipv4 #ifdef __GLIBC__ int ns4count = RESVAR.nscount - RESVAR._u._ext.nscount6; for(n = 0; n < MAXNS && n < ns4count; ++n) #else for(n = 0; n < MAXNS && n < RESVAR.nscount; ++n) #endif { jdns_address_t *addr = jdns_address_new(); jdns_address_set_ipv4(addr, ntohl(RESVAR.nsaddr_list[n].sin_addr.s_addr)); jdns_dnsparams_append_nameserver(params, addr, JDNS_UNICAST_PORT); jdns_address_delete(addr); } #endif // domain name if(strlen(RESVAR.defdname) > 0) { jdns_string_t *str; jdns_string_t *p; str = jdns_string_new(); jdns_string_set_cstr(str, RESVAR.defdname); p = string_tolower(str); jdns_string_delete(str); str = p; jdns_dnsparams_append_domain(params, str); jdns_string_delete(str); } // search list #ifdef MAXDFLSRCH for(n = 0; n < MAXDFLSRCH && RESVAR.dnsrch[n]; ++n) { if(strlen(RESVAR.dnsrch[n]) > 0) { jdns_string_t *str; jdns_string_t *p; str = jdns_string_new(); jdns_string_set_cstr(str, RESVAR.dnsrch[n]); p = string_tolower(str); jdns_string_delete(str); str = p; // don't add dups if(!dnsparams_have_domain(params, str)) jdns_dnsparams_append_domain(params, str); jdns_string_delete(str); } } #endif return params; }
static jdns_dnsparams_t *dnsparams_get_unixfiles() { FILE *f; jdns_dnsparams_t *params; params = jdns_dnsparams_new(); f = jdns_fopen("/etc/resolv.conf", "r"); if(!f) return params; while(1) { int n; jdns_string_t *line, *simp; jdns_stringlist_t *parts; line = file_nextline(f); if(!line) break; // truncate at comment n = string_indexOf(line, '#', 0); if(n != -1) { line->size = n; line->data[n] = 0; } simp = string_simplify(line); jdns_string_delete(line); parts = string_split(simp, ' '); jdns_string_delete(simp); if(parts->count < 2) { jdns_stringlist_delete(parts); continue; } simp = string_tolower(parts->item[0]); if(strcmp((char *)simp->data, "nameserver") == 0) { jdns_address_t *addr = jdns_address_new(); jdns_address_set_cstr(addr, (const char *)parts->item[1]->data); jdns_dnsparams_append_nameserver(params, addr, JDNS_UNICAST_PORT); jdns_address_delete(addr); } else if(strcmp((char *)simp->data, "search") == 0) { for(n = 1; n < parts->count; ++n) { jdns_dnsparams_append_domain(params, parts->item[n]); } } else if(strcmp((char *)simp->data, "domain") == 0) { jdns_dnsparams_append_domain(params, parts->item[1]); } jdns_string_delete(simp); jdns_stringlist_delete(parts); } fclose(f); return params; }
static jdns_dnsparams_t *dnsparams_get_winreg() { int n; jdns_dnsparams_t *params; HKEY key; int ret; char sep; jdns_string_t *str_domain, *str_nameserver, *str_searchlist; jdns_stringlist_t *list_nameserver, *list_searchlist; sep = ' '; ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\Tcpip\\Parameters", 0, KEY_READ, &key); if(ret != ERROR_SUCCESS) { sep = ','; ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\VxD\\MSTCP", 0, KEY_READ, &key); if(ret != ERROR_SUCCESS) return 0; } str_domain = reg_readString(key, "DhcpDomain"); if(!str_domain) str_domain = reg_readString(key, "Domain"); str_nameserver = reg_readString(key, "DhcpNameServer"); if(!str_nameserver) str_nameserver = reg_readString(key, "NameServer"); str_searchlist = reg_readString(key, "SearchList"); RegCloseKey(key); list_nameserver = 0; if(str_nameserver) { list_nameserver = string_split(str_nameserver, sep); jdns_string_delete(str_nameserver); } list_searchlist = 0; if(str_searchlist) { // lowercase the string jdns_string_t *p = string_tolower(str_searchlist); jdns_string_delete(str_searchlist); str_searchlist = p; list_searchlist = string_split(str_searchlist, sep); jdns_string_delete(str_searchlist); } params = jdns_dnsparams_new(); if(list_nameserver) { // qt seems to do a strange thing here by running each name // server address through the q3dns setLabel function, and // then pulls the result as a list of addresses. i have // no idea why they do this, or how one IP address would // turn into anything else, let alone several addresses. // so, uh, we're not going to do that. for(n = 0; n < list_nameserver->count; ++n) { jdns_address_t *addr = jdns_address_new(); if(jdns_address_set_cstr(addr, (char *)list_nameserver->item[n]->data)) jdns_dnsparams_append_nameserver(params, addr, JDNS_UNICAST_PORT); jdns_address_delete(addr); } jdns_stringlist_delete(list_nameserver); } if(str_domain) { if(str_domain->size > 0) jdns_dnsparams_append_domain(params, str_domain); jdns_string_delete(str_domain); } if(list_searchlist) { for(n = 0; n < list_searchlist->count; ++n) { if(list_searchlist->item[n]->size > 0) jdns_dnsparams_append_domain(params, list_searchlist->item[n]); } jdns_stringlist_delete(list_searchlist); } return params; }
static jdns_dnshostlist_t *read_hosts_file(const char *path) { jdns_dnshostlist_t *out; FILE *f; out = jdns_dnshostlist_new(); f = jdns_fopen(path, "r"); if(!f) return out; while(1) { jdns_string_t *line, *simp; jdns_stringlist_t *parts; jdns_address_t *addr; int n; line = file_nextline(f); if(!line) break; // truncate at comment n = string_indexOf(line, '#', 0); if(n != -1) { line->size = n; line->data[n] = 0; } simp = string_simplify(line); jdns_string_delete(line); parts = string_split(simp, ' '); jdns_string_delete(simp); if(parts->count < 2) { jdns_stringlist_delete(parts); continue; } addr = jdns_address_new(); if(!jdns_address_set_cstr(addr, (const char *)parts->item[0]->data)) { jdns_address_delete(addr); jdns_stringlist_delete(parts); continue; } for(n = 1; n < parts->count; ++n) { jdns_dnshost_t *h = jdns_dnshost_new(); h->name = jdns_string_copy(parts->item[n]); h->address = jdns_address_copy(addr); jdns_dnshostlist_append(out, h); jdns_dnshost_delete(h); } jdns_address_delete(addr); jdns_stringlist_delete(parts); } fclose(f); return out; }
static jdns_dnsparams_t *dnsparams_get_unixsys() { int n; jdns_dnsparams_t *params; #ifdef JDNS_MODERN_RES_API struct __res_state res; memset(&res, 0, sizeof(struct __res_state)); n = res_ninit(&res); #define RESVAR res #else n = my_res_init(); #define RESVAR _res #endif params = jdns_dnsparams_new(); // error initializing? if(n == -1) return params; // nameservers - ipv6 for(n = 0; n < MAXNS && n < RESVAR._u._ext.nscount; ++n) { jdns_address_t *addr; struct sockaddr_in6 *sa6; #ifdef USE_EXTEXT sa6 = ((struct sockaddr_in6 *)RESVAR._u._ext.ext) + n; #else sa6 = RESVAR._u._ext.nsaddrs[n]; #endif if(sa6 == NULL) continue; addr = jdns_address_new(); jdns_address_set_ipv6(addr, sa6->sin6_addr.s6_addr); jdns_dnsparams_append_nameserver(params, addr, JDNS_UNICAST_PORT); jdns_address_delete(addr); } // nameservers - ipv4 for(n = 0; n < MAXNS && n < RESVAR.nscount; ++n) { jdns_address_t *addr = jdns_address_new(); jdns_address_set_ipv4(addr, ntohl(RESVAR.nsaddr_list[n].sin_addr.s_addr)); jdns_dnsparams_append_nameserver(params, addr, JDNS_UNICAST_PORT); jdns_address_delete(addr); } // domain name if(strlen(RESVAR.defdname) > 0) { jdns_string_t *str; jdns_string_t *p; str = jdns_string_new(); jdns_string_set_cstr(str, RESVAR.defdname); p = string_tolower(str); jdns_string_delete(str); str = p; jdns_dnsparams_append_domain(params, str); jdns_string_delete(str); } // search list #ifdef MAXDFLSRCH for(n = 0; n < MAXDFLSRCH && RESVAR.dnsrch[n]; ++n) { if(strlen(RESVAR.dnsrch[n]) > 0) { jdns_string_t *str; jdns_string_t *p; str = jdns_string_new(); jdns_string_set_cstr(str, RESVAR.dnsrch[n]); p = string_tolower(str); jdns_string_delete(str); str = p; // don't add dups if(!dnsparams_have_domain(params, str)) jdns_dnsparams_append_domain(params, str); jdns_string_delete(str); } } #endif return params; }