Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenadnstest.c
Go to the documentation of this file.
00001 /* 00002 * adnstest.c 00003 * - simple test program, not part of the library 00004 */ 00005 /* 00006 * This file is 00007 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> 00008 * 00009 * It is part of adns, which is 00010 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> 00011 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at> 00012 * 00013 * This program is free software; you can redistribute it and/or modify 00014 * it under the terms of the GNU General Public License as published by 00015 * the Free Software Foundation; either version 2, or (at your option) 00016 * any later version. 00017 * 00018 * This program is distributed in the hope that it will be useful, 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 * GNU General Public License for more details. 00022 * 00023 * You should have received a copy of the GNU General Public License 00024 * along with this program; if not, write to the Free Software Foundation, 00025 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00026 */ 00027 00028 #ifdef ADNS_JGAA_WIN32 00029 # include "adns_win32.h" 00030 #else 00031 # include <stdio.h> 00032 # include <sys/time.h> 00033 # include <unistd.h> 00034 # include <assert.h> 00035 # include <stdlib.h> 00036 # include <string.h> 00037 # include <errno.h> 00038 #endif 00039 00040 #include "config.h" 00041 #include "adns.h" 00042 00043 #ifdef ADNS_REGRESS_TEST 00044 # include "hredirect.h" 00045 #endif 00046 00047 struct myctx { 00048 adns_query qu; 00049 int doneyet, found; 00050 const char *fdom; 00051 }; 00052 00053 static struct myctx *mcs; 00054 static adns_state ads; 00055 static adns_rrtype *types_a; 00056 00057 static void quitnow(int rc) NONRETURNING; 00058 static void quitnow(int rc) { 00059 free(mcs); 00060 free(types_a); 00061 if (ads) adns_finish(ads); 00062 00063 exit(rc); 00064 } 00065 00066 #ifndef HAVE_POLL 00067 #undef poll 00068 int poll(struct pollfd *ufds, int nfds, int timeout) { 00069 fputs("poll(2) not supported on this system\n",stderr); 00070 quitnow(5); 00071 return -1; /* compiler food */ 00072 } 00073 #define adns_beforepoll(a,b,c,d,e) 0 00074 #define adns_afterpoll(a,b,c,d) 0 00075 #endif 00076 00077 static void failure_status(const char *what, adns_status st) NONRETURNING; 00078 static void failure_status(const char *what, adns_status st) { 00079 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st)); 00080 quitnow(2); 00081 } 00082 00083 static void failure_errno(const char *what, int errnoval) NONRETURNING; 00084 static void failure_errno(const char *what, int errnoval) { 00085 fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval); 00086 quitnow(2); 00087 } 00088 00089 static void usageerr(const char *why) NONRETURNING; 00090 static void usageerr(const char *why) { 00091 fprintf(stderr, 00092 "bad usage: %s\n" 00093 "usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n" 00094 " [ :<typenum>,... ]\n" 00095 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n" 00096 "initflags: p use poll(2) instead of select(2)\n" 00097 " s use adns_wait with specified query, instead of 0\n" 00098 "queryflags: a print status abbrevs instead of strings\n" 00099 "exit status: 0 ok (though some queries may have failed)\n" 00100 " 1 used by test harness to indicate test failed\n" 00101 " 2 unable to submit or init or some such\n" 00102 " 3 unexpected failure\n" 00103 " 4 usage error\n" 00104 " 5 operation not supported on this system\n", 00105 why); 00106 quitnow(4); 00107 } 00108 00109 static const adns_rrtype defaulttypes[]= { 00110 adns_r_a, 00111 adns_r_ns_raw, 00112 adns_r_cname, 00113 adns_r_soa_raw, 00114 adns_r_ptr_raw, 00115 adns_r_hinfo, 00116 adns_r_mx_raw, 00117 adns_r_txt, 00118 adns_r_rp_raw, 00119 00120 adns_r_addr, 00121 adns_r_ns, 00122 adns_r_ptr, 00123 adns_r_mx, 00124 00125 adns_r_soa, 00126 adns_r_rp, 00127 00128 adns_r_none 00129 }; 00130 00131 static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) { 00132 fprintf(stdout, "%s(%s)%s%s", 00133 ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-", 00134 ri ? " " : "", ri ? adns_strerror(ri) : ""); 00135 } 00136 00137 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r, 00138 char *ownflags, int ownflags_l) { 00139 int qf; 00140 char *ep; 00141 00142 qf= strtoul(fdom,&ep,0); 00143 if (*ep == ',' && strchr(ep,'/')) { 00144 ep++; 00145 while (*ep != '/') { 00146 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); quitnow(3); } 00147 *ownflags++= *ep++; 00148 } 00149 } 00150 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; } 00151 else { *dom_r= ep+1; *qf_r= qf; } 00152 *ownflags= 0; 00153 } 00154 00155 static int consistsof(const char *string, const char *accept) { 00156 return strspn(string,accept) == strlen(string); 00157 } 00158 00159 int main(int argc, char *const *argv) { 00160 adns_query qu; 00161 struct myctx *mc, *mcw; 00162 void *mcr; 00163 adns_answer *ans; 00164 const char *initstring, *rrtn, *fmtn; 00165 const char *const *fdomlist, *domain; 00166 char *show, *cp; 00167 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum; 00168 adns_status ri; 00169 int r; 00170 const adns_rrtype *types; 00171 struct timeval now; 00172 char ownflags[10]; 00173 char *ep; 00174 const char *initflags, *owninitflags; 00175 00176 if (argv[0] && argv[1] && argv[1][0] == '-') { 00177 initflags= argv[1]+1; 00178 argv++; 00179 } else { 00180 initflags= ""; 00181 } 00182 if (argv[0] && argv[1] && argv[1][0] == '/') { 00183 initstring= argv[1]+1; 00184 argv++; 00185 } else { 00186 initstring= 0; 00187 } 00188 00189 initflagsnum= strtoul(initflags,&ep,0); 00190 if (*ep == ',') { 00191 owninitflags= ep+1; 00192 if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag"); 00193 } else if (!*ep) { 00194 owninitflags= ""; 00195 } else { 00196 usageerr("bad <initflagsnum>[,<owninitflags>]"); 00197 } 00198 00199 if (argv[0] && argv[1] && argv[1][0] == ':') { 00200 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++) 00201 if (ch==',') tc++; 00202 types_a= malloc(sizeof(*types_a)*(tc+1)); 00203 if (!types_a) { perror("malloc types"); quitnow(3); } 00204 for (cp= argv[1]+1, ti=0; ti<tc; ti++) { 00205 types_a[ti]= strtoul(cp,&cp,10); 00206 if ((ch= *cp)) { 00207 if (ch != ',') usageerr("unexpected char (not comma) in or between types"); 00208 cp++; 00209 } 00210 } 00211 types_a[ti]= adns_r_none; 00212 types= types_a; 00213 argv++; 00214 } else { 00215 types_a= 0; 00216 types= defaulttypes; 00217 } 00218 00219 if (!(argv[0] && argv[1])) usageerr("no query domains supplied"); 00220 fdomlist= (const char *const*)argv+1; 00221 00222 for (qc=0; fdomlist[qc]; qc++); 00223 for (tc=0; types[tc] != adns_r_none; tc++); 00224 mcs= malloc(tc ? sizeof(*mcs)*qc*tc : 1); 00225 if (!mcs) { perror("malloc mcs"); quitnow(3); } 00226 00227 setvbuf(stdout,0,_IOLBF,0); 00228 00229 if (initstring) { 00230 r= adns_init_strcfg(&ads, 00231 (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq) 00232 ^initflagsnum, 00233 stdout,initstring); 00234 } else { 00235 r= adns_init(&ads, 00236 (adns_if_debug|adns_if_noautosys)^initflagsnum, 00237 0); 00238 } 00239 if (r) failure_errno("init",r); 00240 00241 for (qi=0; qi<qc; qi++) { 00242 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags)); 00243 if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag"); 00244 for (ti=0; ti<tc; ti++) { 00245 mc= &mcs[qi*tc+ti]; 00246 mc->doneyet= 0; 00247 mc->fdom= fdomlist[qi]; 00248 00249 fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]); 00250 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu); 00251 if (r == ENOSYS) { 00252 fprintf(stdout," not implemented\n"); 00253 mc->qu= 0; 00254 mc->doneyet= 1; 00255 } else if (r) { 00256 failure_errno("submit",r); 00257 } else { 00258 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0); 00259 putc(' ',stdout); 00260 dumptype(ri,rrtn,fmtn); 00261 fprintf(stdout," submitted\n"); 00262 } 00263 } 00264 } 00265 00266 for (;;) { 00267 for (qi=0; qi<qc; qi++) { 00268 for (ti=0; ti<tc; ti++) { 00269 mc= &mcs[qi*tc+ti]; 00270 mc->found= 0; 00271 } 00272 } 00273 for (adns_forallqueries_begin(ads); 00274 (qu= adns_forallqueries_next(ads,&mcr)); 00275 ) { 00276 mc= mcr; 00277 assert(qu == mc->qu); 00278 assert(!mc->doneyet); 00279 mc->found= 1; 00280 } 00281 mcw= 0; 00282 for (qi=0; qi<qc; qi++) { 00283 for (ti=0; ti<tc; ti++) { 00284 mc= &mcs[qi*tc+ti]; 00285 if (mc->doneyet) continue; 00286 assert(mc->found); 00287 if (!mcw) mcw= mc; 00288 } 00289 } 00290 if (!mcw) break; 00291 00292 if (strchr(owninitflags,'s')) { 00293 qu= mcw->qu; 00294 mc= mcw; 00295 } else { 00296 qu= 0; 00297 mc= 0; 00298 } 00299 00300 #ifdef HAVE_POLL 00301 if (strchr(owninitflags,'p')) { 00302 r= adns_wait_poll(ads,&qu,&ans,&mcr); 00303 } else 00304 #endif 00305 { 00306 r= adns_wait(ads,&qu,&ans,&mcr); 00307 } 00308 if (r) failure_errno("wait/check",r); 00309 00310 if (mc) assert(mcr==mc); 00311 else mc= mcr; 00312 assert(qu==mc->qu); 00313 assert(!mc->doneyet); 00314 00315 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags)); 00316 00317 if (gettimeofday(&now,0)) { perror("gettimeofday"); quitnow(3); } 00318 00319 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0); 00320 fprintf(stdout, "%s flags %d type ",domain,qflags); 00321 dumptype(ri,rrtn,fmtn); 00322 fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n", 00323 ownflags[0] ? " ownflags=" : "", ownflags, 00324 strchr(ownflags,'a') 00325 ? adns_errabbrev(ans->status) 00326 : adns_strerror(ans->status), 00327 ans->nrrs, 00328 ans->cname ? ans->cname : "$", 00329 ans->owner ? ans->owner : "$", 00330 (long)ans->expires - (long)now.tv_sec); 00331 if (ans->nrrs) { 00332 assert(!ri); 00333 for (i=0; i<ans->nrrs; i++) { 00334 ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show); 00335 if (ri) failure_status("info",ri); 00336 fprintf(stdout," %s\n",show); 00337 free(show); 00338 } 00339 } 00340 free(ans); 00341 00342 mc->doneyet= 1; 00343 } 00344 00345 quitnow(0); 00346 } Generated on Wed May 23 2012 04:31:24 for ReactOS by
1.7.6.1
|