void quitnow(int rc) { if (ads) adns_finish(ads); free(buf); free(ov_id); exit(rc); }
int main(int argc, char *argv[]) { adns_state adns; adns_query query; adns_answer *answer; progname= strrchr(*argv, '/'); if (progname) progname++; else progname= *argv; if (argc != 2) { fprintf(stderr, "usage: %s <domain>\n", progname); exit(1); } errno= adns_init(&adns, adns_if_debug, 0); if (errno) aargh("adns_init"); errno= adns_submit(adns, argv[1], adns_r_ptr, adns_qf_quoteok_cname|adns_qf_cname_loose, NULL, &query); if (errno) aargh("adns_submit"); errno= adns_wait(adns, &query, &answer, NULL); if (errno) aargh("adns_init"); printf("%s\n", answer->status == adns_s_ok ? *answer->rrs.str : "dunno"); adns_finish(adns); return 0; }
void torque_dns_shutdown(dns_state *dctx){ #ifndef LIBTORQUE_WITHOUT_ADNS adns_finish(*dctx); #else memset(dctx,0,sizeof(*dctx)); #endif }
static apr_status_t kdldap_servers_cleanup(void *data) { kdldap_servers *self = (kdldap_servers *)data; if (self->use_dns && self->adns_init) adns_finish(self->adns_state); return APR_SUCCESS; }
static void do_adns_free(adns_state s) { adns_forallqueries_begin(s); if (adns_forallqueries_next(s, NULL)) werror("Dropping queries on the floor.\n"); adns_finish(s); }
static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) { int eof, err, len; adns_state adns; adns_answer *answer; logline *head, *tail, *line; adns_initflags initflags; initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0; if (config_text) { errno= adns_init_strcfg(&adns, initflags, stderr, config_text); } else { errno= adns_init(&adns, initflags, 0); } if (errno) aargh("adns_init"); head= tail= readline(inf, adns, opts); len= 1; eof= 0; while (head) { while (head) { if (opts & OPT_DEBUG) msg("%d in queue; checking %.*s", len, (int)(head->rest-head->addr), guard_null(head->addr)); if (eof || len >= maxpending) { if (opts & OPT_POLL) err= adns_wait_poll(adns, &head->query, &answer, NULL); else err= adns_wait(adns, &head->query, &answer, NULL); } else { err= adns_check(adns, &head->query, &answer, NULL); } if (err == EAGAIN) break; if (err) { fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err)); exit(1); } printline(outf, head->start, head->addr, head->rest, answer->status == adns_s_ok ? *answer->rrs.str : NULL, head->fullip, head->is_v6, opts); line= head; head= head->next; free(line); free(answer); len--; } if (!eof) { line= readline(inf, adns, opts); if (line) { if (!head) head= line; else tail->next= line; tail= line; len++; } else { eof= 1; } } } adns_finish(adns); }
static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) { int eof, err, len; adns_state adns; adns_answer *answer; logline *head, *tail, *line; adns_initflags initflags; initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0; if (config_text) { errno= adns_init_strcfg(&adns, initflags, stderr, config_text); } else { errno= adns_init(&adns, initflags, 0); } if (errno) aargh("adns_init"); head= tail= readline(inf, adns, opts); len= 1; eof= 0; while (head) { while (head) { if (head->query) { if (opts & OPT_DEBUG) msg("%d in queue; checking %.*s", len, (int)(head->rest-head->name), guard_null(head->name)); if (eof || len >= maxpending) { if (opts & OPT_POLL) err= adns_wait_poll(adns, &head->query, &answer, NULL); else err= adns_wait(adns, &head->query, &answer, NULL); } else { err= adns_check(adns, &head->query, &answer, NULL); } if (err == EAGAIN) break; if (err) { fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err)); exit(1); } if (answer->status == adns_s_ok) { const char *addr; int ok = 0; fprintf(outf, "%.*s", (int)(head->rest-head->start), head->start); while(answer->nrrs--) { addr= inet_ntoa(answer->rrs.inaddr[answer->nrrs]); ok |= !strncmp(addr, head->addr, strlen(addr)); fprintf(outf, " [%s]", addr); } fprintf(outf, "%s%s", ok ? " OK" : "", head->rest); } else { if (opts & OPT_DEBUG) msg("query failed"); fputs(head->start, outf); } free(answer); len--; } else { if (opts & OPT_DEBUG) msg("%d in queue; no query on this line", len); fputs(head->start, outf); } line= head; head= head->next; free(line); } if (!eof) { line= readline(inf, adns, opts); if (line) { if (!head) head= line; else tail->next= line; tail= line; if (line->query) len++; } else { eof= 1; } } } adns_finish(adns); }
/* This function resolves the srv name specified and copies the addresses and * ports found to 'addr_array' and 'port_array'. Up to '*nb_addr' addresses and * ports will be copied. On success, '*nb_addr' is set to the number of * addresses actually copied. This function returns -1 on failure, 0 on success. */ static int knp_query_resolve_srv_name(char *srv_name, unsigned long *addr_array, unsigned int *port_array, int *nb_addr) { int error = 0; adns_state state; adns_answer *answer = NULL; /* Try. */ do { int r; int i, j; int nb_out = 0; /* Initialize the ADNS state. */ r = adns_init(&state, 0, NULL); if (r) { kmo_seterror("cannot resolve %s: %s", srv_name, kmo_syserror(r)); error = -1; state = NULL; /* Don't call adns_finish(): ADNS sucks. */ break; } /* Perform the query. */ r = adns_synchronous(state, srv_name, adns_r_srv, 0, &answer); if (r) { kmo_seterror("cannot resolve %s: %s", srv_name, kmo_syserror(r)); error = -1; break; } /* The query failed. */ if (answer->status != adns_s_ok) { kmo_seterror("cannot resolve %s: %s", srv_name, adns_strerror(answer->status)); error = -1; break; } /* We got the results. */ for (i = 0; i < answer->nrrs; i++) { unsigned int port = answer->rrs.srvha[i].port; /* No addresses returned so resolve the SRV host name. */ if (! answer->rrs.srvha[i].ha.naddrs) { int nb = *nb_addr - nb_out; /* Ignore errors, since it's likely the DNS that's badly * configured if this function fails. */ if (ksock_get_host_addr_list(answer->rrs.srvha[i].ha.host, addr_array + nb_out, &nb)) { continue; } for (j = 0; j < nb; j++) { port_array[nb_out + j] = port; } nb_out += nb; } /* Use the returned addresses. */ else { for (j = 0; j < answer->rrs.srvha[i].ha.naddrs; j++) { unsigned long addr = answer->rrs.srvha[i].ha.addrs[j].addr.inet.sin_addr.s_addr; if (nb_out == *nb_addr) { goto out; } addr_array[nb_out] = addr; port_array[nb_out] = port; nb_out++; } } } out: *nb_addr = nb_out; if (nb_out == 0) { kmo_seterror("cannot resolve %s: no addresses found for service", srv_name); error = -1; break; } } while (0); free(answer); adns_finish(state); return error; }
int getsrv (const char *name,struct srventry **list) { int srvcount=0; u16 count; int i, rc; *list = NULL; #ifdef USE_ADNS { adns_state state; adns_answer *answer = NULL; rc = adns_init (&state, adns_if_noerrprint, NULL); if (rc) { log_error ("error initializing adns: %s\n", strerror (errno)); return -1; } rc = adns_synchronous (state, name, adns_r_srv, adns_qf_quoteok_query, &answer); if (rc) { log_error ("DNS query failed: %s\n", strerror (errno)); adns_finish (state); return -1; } if (answer->status != adns_s_ok || answer->type != adns_r_srv || !answer->nrrs) { /* log_error ("DNS query returned an error or no records: %s (%s)\n", */ /* adns_strerror (answer->status), */ /* adns_errabbrev (answer->status)); */ adns_free (answer); adns_finish (state); return 0; } for (count = 0; count < answer->nrrs; count++) { struct srventry *srv = NULL; struct srventry *newlist; if (strlen (answer->rrs.srvha[count].ha.host) >= MAXDNAME) { log_info ("hostname in SRV record too long - skipped\n"); continue; } newlist = xtryrealloc (*list, (srvcount+1)*sizeof(struct srventry)); if (!newlist) goto fail; *list = newlist; memset (&(*list)[srvcount], 0, sizeof(struct srventry)); srv = &(*list)[srvcount]; srvcount++; srv->priority = answer->rrs.srvha[count].priority; srv->weight = answer->rrs.srvha[count].weight; srv->port = answer->rrs.srvha[count].port; strcpy (srv->target, answer->rrs.srvha[count].ha.host); } adns_free (answer); adns_finish (state); } #else /*!USE_ADNS*/ { unsigned char answer[2048]; HEADER *header = (HEADER *)answer; unsigned char *pt, *emsg; int r; u16 dlen; r = res_query (name, C_IN, T_SRV, answer, sizeof answer); if (r < sizeof (HEADER) || r > sizeof answer) return -1; if (header->rcode != NOERROR || !(count=ntohs (header->ancount))) return 0; /* Error or no record found. */ emsg = &answer[r]; pt = &answer[sizeof(HEADER)]; /* Skip over the query */ rc = dn_skipname (pt, emsg); if (rc == -1) goto fail; pt += rc + QFIXEDSZ; while (count-- > 0 && pt < emsg) { struct srventry *srv=NULL; u16 type,class; struct srventry *newlist; newlist = xtryrealloc (*list, (srvcount+1)*sizeof(struct srventry)); if (!newlist) goto fail; *list = newlist; memset(&(*list)[srvcount],0,sizeof(struct srventry)); srv=&(*list)[srvcount]; srvcount++; rc = dn_skipname(pt,emsg); /* the name we just queried for */ if (rc == -1) goto fail; pt+=rc; /* Truncated message? */ if((emsg-pt)<16) goto fail; type=*pt++ << 8; type|=*pt++; /* We asked for SRV and got something else !? */ if(type!=T_SRV) goto fail; class=*pt++ << 8; class|=*pt++; /* We asked for IN and got something else !? */ if(class!=C_IN) goto fail; pt+=4; /* ttl */ dlen=*pt++ << 8; dlen|=*pt++; srv->priority=*pt++ << 8; srv->priority|=*pt++; srv->weight=*pt++ << 8; srv->weight|=*pt++; srv->port=*pt++ << 8; srv->port|=*pt++; /* Get the name. 2782 doesn't allow name compression, but dn_expand still works to pull the name out of the packet. */ rc = dn_expand(answer,emsg,pt,srv->target,MAXDNAME); if (rc == 1 && srv->target[0] == 0) /* "." */ { xfree(*list); *list = NULL; return 0; } if (rc == -1) goto fail; pt += rc; /* Corrupt packet? */ if (dlen != rc+6) goto fail; } } #endif /*!USE_ADNS*/ /* Now we have an array of all the srv records. */ /* Order by priority */ qsort(*list,srvcount,sizeof(struct srventry),priosort); /* For each priority, move the zero-weighted items first. */ for (i=0; i < srvcount; i++) { int j; for (j=i;j < srvcount && (*list)[i].priority == (*list)[j].priority; j++) { if((*list)[j].weight==0) { /* Swap j with i */ if(j!=i) { struct srventry temp; memcpy (&temp,&(*list)[j],sizeof(struct srventry)); memcpy (&(*list)[j],&(*list)[i],sizeof(struct srventry)); memcpy (&(*list)[i],&temp,sizeof(struct srventry)); } break; } } } /* Run the RFC-2782 weighting algorithm. We don't need very high quality randomness for this, so regular libc srand/rand is sufficient. Fixme: It is a bit questionaly to reinitalize srand - better use a gnupg fucntion for this. */ srand(time(NULL)*getpid()); for (i=0; i < srvcount; i++) { int j; float prio_count=0,chose; for (j=i; j < srvcount && (*list)[i].priority == (*list)[j].priority; j++) { prio_count+=(*list)[j].weight; (*list)[j].run_count=prio_count; } chose=prio_count*rand()/RAND_MAX; for (j=i;j<srvcount && (*list)[i].priority==(*list)[j].priority;j++) { if (chose<=(*list)[j].run_count) { /* Swap j with i */ if(j!=i) { struct srventry temp; memcpy(&temp,&(*list)[j],sizeof(struct srventry)); memcpy(&(*list)[j],&(*list)[i],sizeof(struct srventry)); memcpy(&(*list)[i],&temp,sizeof(struct srventry)); } break; } } } return srvcount; fail: xfree(*list); *list=NULL; return -1; }
Modnsctx::~Modnsctx(){ adns_finish(adns); }
DNS_STATUS WINAPI DnsQuery_A(LPCSTR Name, WORD Type, DWORD Options, PIP4_ARRAY Servers, PDNS_RECORD *QueryResultSet, PVOID *Reserved) { adns_state astate; int quflags = 0; int adns_error; adns_answer *answer; LPSTR CurrentName; unsigned i, CNameLoop; *QueryResultSet = 0; switch(Type) { case DNS_TYPE_A: adns_error = adns_init(&astate, adns_if_noenv | adns_if_noerrprint | adns_if_noserverwarn, 0); if(adns_error != adns_s_ok) return DnsIntTranslateAdnsToDNS_STATUS(adns_error); if (Servers) { for(i = 0; i < Servers->AddrCount; i++) { adns_addserver(astate, *((struct in_addr *)&Servers->AddrArray[i])); } } /* * adns doesn't resolve chained CNAME records (a CNAME which points to * another CNAME pointing to another... pointing to an A record), according * to a mailing list thread the authors believe that chained CNAME records * are invalid and the DNS entries should be fixed. That's a nice academic * standpoint, but there certainly are chained CNAME records out there, * even some fairly major ones (at the time of this writing * download.mozilla.org is a chained CNAME). Everyone else seems to resolve * these fine, so we should too. So we loop here to try to resolve CNAME * chains ourselves. Of course, there must be a limit to protect against * CNAME loops. */ #define CNAME_LOOP_MAX 16 CurrentName = (LPSTR) Name; for (CNameLoop = 0; CNameLoop < CNAME_LOOP_MAX; CNameLoop++) { adns_error = adns_synchronous(astate, CurrentName, adns_r_addr, quflags, &answer); if(adns_error != adns_s_ok) { adns_finish(astate); if (CurrentName != Name) RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName); return DnsIntTranslateAdnsToDNS_STATUS(adns_error); } if(answer && answer->rrs.addr) { if (CurrentName != Name) RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName); *QueryResultSet = (PDNS_RECORD)RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(DNS_RECORD)); if (NULL == *QueryResultSet) { adns_finish( astate ); return ERROR_OUTOFMEMORY; } (*QueryResultSet)->pNext = NULL; (*QueryResultSet)->wType = Type; (*QueryResultSet)->wDataLength = sizeof(DNS_A_DATA); (*QueryResultSet)->Data.A.IpAddress = answer->rrs.addr->addr.inet.sin_addr.s_addr; adns_finish(astate); (*QueryResultSet)->pName = xstrsave( Name ); return NULL != (*QueryResultSet)->pName ? ERROR_SUCCESS : ERROR_OUTOFMEMORY; } if (NULL == answer || adns_s_prohibitedcname != answer->status || NULL == answer->cname) { adns_finish(astate); if (CurrentName != Name) RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName); return ERROR_FILE_NOT_FOUND; } if (CurrentName != Name) RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName); CurrentName = xstrsave(answer->cname); if (!CurrentName) { adns_finish(astate); return ERROR_OUTOFMEMORY; } } adns_finish(astate); RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentName); return ERROR_FILE_NOT_FOUND; default: return ERROR_OUTOFMEMORY; /* XXX arty: find a better error code. */ } }