| 1 | /* |
| 2 | * query.c |
| 3 | * - overall query management (allocation, completion) |
| 4 | * - per-query memory management |
| 5 | * - query submission and cancellation (user-visible and internal) |
| 6 | */ |
| 7 | /* |
| 8 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2, or (at your option) |
| 13 | * any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software Foundation, |
| 22 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 | */ |
| 24 | |
| 25 | #include "internal.h" |
| 26 | |
| 27 | #include <stdlib.h> |
| 28 | #include <unistd.h> |
| 29 | #include <errno.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include <sys/time.h> |
| 33 | |
| 34 | #include "internal.h" |
| 35 | |
| 36 | static adns_query query_alloc(adns_state ads, const typeinfo *typei, |
| 37 | adns_queryflags flags, struct timeval now) { |
| 38 | /* Allocate a virgin query and return it. */ |
| 39 | adns_query qu; |
| 40 | |
| 41 | qu= malloc(sizeof(*qu)); if (!qu) return 0; |
| 42 | qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) { free(qu); return 0; } |
| 43 | |
| 44 | qu->ads= ads; |
| 45 | qu->state= query_udp; |
| 46 | qu->back= qu->next= qu->parent= 0; |
| 47 | LIST_INIT(qu->children); |
| 48 | LINK_INIT(qu->siblings); |
| 49 | LIST_INIT(qu->allocations); |
| 50 | qu->interim_allocd= 0; |
| 51 | qu->final_allocspace= 0; |
| 52 | |
| 53 | qu->typei= typei; |
| 54 | qu->query_dgram= 0; |
| 55 | qu->query_dglen= 0; |
| 56 | adns__vbuf_init(&qu->vb); |
| 57 | |
| 58 | qu->cname_dgram= 0; |
| 59 | qu->cname_dglen= qu->cname_begin= 0; |
| 60 | |
| 61 | adns__vbuf_init(&qu->search_vb); |
| 62 | qu->search_origlen= qu->search_pos= qu->search_doneabs= 0; |
| 63 | |
| 64 | qu->id= 0; |
| 65 | qu->flags= flags; |
| 66 | qu->udpretries= 0; |
| 67 | qu->udpnextserver= 0; |
| 68 | qu->udpsent= qu->tcpfailed= 0; |
| 69 | timerclear(&qu->timeout); |
| 70 | qu->expires= now.tv_sec + MAXTTLBELIEVE; |
| 71 | |
| 72 | memset(&qu->ctx,0,sizeof(qu->ctx)); |
| 73 | |
| 74 | qu->answer->status= adns_s_ok; |
| 75 | qu->answer->cname= 0; |
| 76 | qu->answer->type= typei->type; |
| 77 | qu->answer->expires= -1; |
| 78 | qu->answer->nrrs= 0; |
| 79 | qu->answer->rrs= 0; |
| 80 | qu->answer->rrsz= typei->rrsz; |
| 81 | |
| 82 | return qu; |
| 83 | } |
| 84 | |
| 85 | static void query_submit(adns_state ads, adns_query qu, |
| 86 | const typeinfo *typei, vbuf *qumsg_vb, int id, |
| 87 | adns_queryflags flags, struct timeval now) { |
| 88 | /* Fills in the query message in for a previously-allocated query, |
| 89 | * and submits it. Cannot fail. |
| 90 | */ |
| 91 | |
| 92 | qu->vb= *qumsg_vb; |
| 93 | adns__vbuf_init(qumsg_vb); |
| 94 | |
| 95 | qu->query_dgram= malloc(qu->vb.used); |
| 96 | if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; } |
| 97 | |
| 98 | qu->id= id; |
| 99 | qu->query_dglen= qu->vb.used; |
| 100 | memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used); |
| 101 | |
| 102 | adns__query_udp(qu,now); |
| 103 | adns__autosys(ads,now); |
| 104 | } |
| 105 | |
| 106 | adns_status adns__internal_submit(adns_state ads, adns_query *query_r, |
| 107 | const typeinfo *typei, vbuf *qumsg_vb, int id, |
| 108 | adns_queryflags flags, struct timeval now, |
| 109 | const qcontext *ctx) { |
| 110 | adns_query qu; |
| 111 | |
| 112 | qu= query_alloc(ads,typei,flags,now); |
| 113 | if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; } |
| 114 | *query_r= qu; |
| 115 | |
| 116 | memcpy(&qu->ctx,ctx,sizeof(qu->ctx)); |
| 117 | query_submit(ads,qu, typei,qumsg_vb,id,flags,now); |
| 118 | |
| 119 | return adns_s_ok; |
| 120 | } |
| 121 | |
| 122 | static void query_simple(adns_state ads, adns_query qu, |
| 123 | const char *owner, int ol, |
| 124 | const typeinfo *typei, adns_queryflags flags, |
| 125 | struct timeval now) { |
| 126 | vbuf vb; |
| 127 | int id; |
| 128 | adns_status stat; |
| 129 | |
| 130 | adns__vbuf_init(&vb); |
| 131 | |
| 132 | stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags); |
| 133 | if (stat) { adns__query_fail(qu,stat); return; } |
| 134 | |
| 135 | query_submit(ads,qu, typei,&vb,id, flags,now); |
| 136 | } |
| 137 | |
| 138 | void adns__search_next(adns_state ads, adns_query qu, struct timeval now) { |
| 139 | const char *nextentry; |
| 140 | adns_status stat; |
| 141 | |
| 142 | if (qu->search_doneabs<0) { |
| 143 | nextentry= 0; |
| 144 | qu->search_doneabs= 1; |
| 145 | } else { |
| 146 | if (qu->search_pos >= ads->nsearchlist) { |
| 147 | if (qu->search_doneabs) { |
| 148 | stat= adns_s_nxdomain; goto x_fail; |
| 149 | return; |
| 150 | } else { |
| 151 | nextentry= 0; |
| 152 | qu->search_doneabs= 1; |
| 153 | } |
| 154 | } else { |
| 155 | nextentry= ads->searchlist[qu->search_pos++]; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | qu->search_vb.used= qu->search_origlen; |
| 160 | if (nextentry) { |
| 161 | if (!adns__vbuf_append(&qu->search_vb,".",1) || |
| 162 | !adns__vbuf_appendstr(&qu->search_vb,nextentry)) { |
| 163 | stat= adns_s_nomemory; goto x_fail; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | free(qu->query_dgram); |
| 168 | qu->query_dgram= 0; qu->query_dglen= 0; |
| 169 | |
| 170 | query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, qu->typei, qu->flags, now); |
| 171 | return; |
| 172 | |
| 173 | x_fail: |
| 174 | adns__query_fail(qu,stat); |
| 175 | } |
| 176 | |
| 177 | int adns_submit(adns_state ads, |
| 178 | const char *owner, |
| 179 | adns_rrtype type, |
| 180 | adns_queryflags flags, |
| 181 | void *context, |
| 182 | adns_query *query_r) { |
| 183 | int r, ol, ndots; |
| 184 | adns_status stat; |
| 185 | const typeinfo *typei; |
| 186 | struct timeval now; |
| 187 | adns_query qu; |
| 188 | const char *p; |
| 189 | |
| 190 | typei= adns__findtype(type); |
| 191 | if (!typei) return adns_s_unknownrrtype; |
| 192 | |
| 193 | r= gettimeofday(&now,0); if (r) goto x_errno; |
| 194 | qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno; |
| 195 | |
| 196 | qu->ctx.ext= context; |
| 197 | qu->ctx.callback= 0; |
| 198 | memset(&qu->ctx.info,0,sizeof(qu->ctx.info)); |
| 199 | |
| 200 | *query_r= qu; |
| 201 | |
| 202 | ol= strlen(owner); |
| 203 | if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; } |
| 204 | if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; } |
| 205 | |
| 206 | if (ol>=2 && owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; } |
| 207 | |
| 208 | if (flags & adns_qf_search) { |
| 209 | r= adns__vbuf_append(&qu->search_vb,owner,ol); |
| 210 | if (!r) { stat= adns_s_nomemory; goto x_adnsfail; } |
| 211 | |
| 212 | for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++); |
| 213 | qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0; |
| 214 | qu->search_origlen= ol; |
| 215 | adns__search_next(ads,qu,now); |
| 216 | } else { |
| 217 | query_simple(ads,qu, owner,ol, typei,flags, now); |
| 218 | } |
| 219 | return 0; |
| 220 | |
| 221 | x_adnsfail: |
| 222 | adns__query_fail(qu,stat); |
| 223 | return 0; |
| 224 | |
| 225 | x_errno: |
| 226 | r= errno; |
| 227 | assert(r); |
| 228 | return r; |
| 229 | } |
| 230 | |
| 231 | int adns_synchronous(adns_state ads, |
| 232 | const char *owner, |
| 233 | adns_rrtype type, |
| 234 | adns_queryflags flags, |
| 235 | adns_answer **answer_r) { |
| 236 | adns_query qu; |
| 237 | int r; |
| 238 | |
| 239 | r= adns_submit(ads,owner,type,flags,0,&qu); |
| 240 | if (r) return r; |
| 241 | |
| 242 | r= adns_wait(ads,&qu,answer_r,0); |
| 243 | if (r) adns_cancel(qu); |
| 244 | |
| 245 | return r; |
| 246 | } |
| 247 | |
| 248 | static void *alloc_common(adns_query qu, size_t sz) { |
| 249 | allocnode *an; |
| 250 | |
| 251 | if (!sz) return qu; /* Any old pointer will do */ |
| 252 | assert(!qu->final_allocspace); |
| 253 | an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz)); |
| 254 | if (!an) return 0; |
| 255 | LIST_LINK_TAIL(qu->allocations,an); |
| 256 | return (byte*)an + MEM_ROUND(sizeof(*an)); |
| 257 | } |
| 258 | |
| 259 | void *adns__alloc_interim(adns_query qu, size_t sz) { |
| 260 | sz= MEM_ROUND(sz); |
| 261 | qu->interim_allocd += sz; |
| 262 | return alloc_common(qu,sz); |
| 263 | } |
| 264 | |
| 265 | void *adns__alloc_mine(adns_query qu, size_t sz) { |
| 266 | return alloc_common(qu,MEM_ROUND(sz)); |
| 267 | } |
| 268 | |
| 269 | void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) { |
| 270 | allocnode *an; |
| 271 | |
| 272 | if (!block) return; |
| 273 | an= (void*)((byte*)block - MEM_ROUND(sizeof(*an))); |
| 274 | |
| 275 | assert(!to->final_allocspace); |
| 276 | assert(!from->final_allocspace); |
| 277 | |
| 278 | LIST_UNLINK(from->allocations,an); |
| 279 | LIST_LINK_TAIL(to->allocations,an); |
| 280 | |
| 281 | from->interim_allocd -= sz; |
| 282 | to->interim_allocd += sz; |
| 283 | |
| 284 | if (to->expires > from->expires) to->expires= from->expires; |
| 285 | } |
| 286 | |
| 287 | void *adns__alloc_final(adns_query qu, size_t sz) { |
| 288 | /* When we're in the _final stage, we _subtract_ from interim_alloc'd |
| 289 | * each allocation, and use final_allocspace to point to the next free |
| 290 | * bit. |
| 291 | */ |
| 292 | void *rp; |
| 293 | |
| 294 | sz= MEM_ROUND(sz); |
| 295 | rp= qu->final_allocspace; |
| 296 | assert(rp); |
| 297 | qu->interim_allocd -= sz; |
| 298 | assert(qu->interim_allocd>=0); |
| 299 | qu->final_allocspace= (byte*)rp + sz; |
| 300 | return rp; |
| 301 | } |
| 302 | |
| 303 | static void cancel_children(adns_query qu) { |
| 304 | adns_query cqu, ncqu; |
| 305 | |
| 306 | for (cqu= qu->children.head; cqu; cqu= ncqu) { |
| 307 | ncqu= cqu->siblings.next; |
| 308 | adns_cancel(cqu); |
| 309 | } |
| 310 | LIST_INIT(qu->children); |
| 311 | } |
| 312 | |
| 313 | void adns__reset_cnameonly(adns_query qu) { |
| 314 | assert(!qu->final_allocspace); |
| 315 | cancel_children(qu); |
| 316 | qu->answer->nrrs= 0; |
| 317 | qu->answer->rrs= 0; |
| 318 | qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0; |
| 319 | } |
| 320 | |
| 321 | static void free_query_allocs(adns_query qu) { |
| 322 | allocnode *an, *ann; |
| 323 | |
| 324 | cancel_children(qu); |
| 325 | for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); } |
| 326 | adns__vbuf_free(&qu->vb); |
| 327 | } |
| 328 | |
| 329 | void adns_cancel(adns_query qu) { |
| 330 | switch (qu->state) { |
| 331 | case query_udp: case query_tcpwait: case query_tcpsent: |
| 332 | LIST_UNLINK(qu->ads->timew,qu); |
| 333 | break; |
| 334 | case query_child: |
| 335 | LIST_UNLINK(qu->ads->childw,qu); |
| 336 | break; |
| 337 | case query_done: |
| 338 | LIST_UNLINK(qu->ads->output,qu); |
| 339 | break; |
| 340 | default: |
| 341 | abort(); |
| 342 | } |
| 343 | free_query_allocs(qu); |
| 344 | free(qu->answer); |
| 345 | free(qu); |
| 346 | } |
| 347 | |
| 348 | void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) { |
| 349 | time_t max; |
| 350 | |
| 351 | assert(ttl <= MAXTTLBELIEVE); |
| 352 | max= now.tv_sec + ttl; |
| 353 | if (qu->expires < max) return; |
| 354 | qu->expires= max; |
| 355 | } |
| 356 | |
| 357 | static void makefinal_query(adns_query qu) { |
| 358 | adns_answer *ans; |
| 359 | int rrn; |
| 360 | |
| 361 | ans= qu->answer; |
| 362 | |
| 363 | if (qu->interim_allocd) { |
| 364 | ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd)); |
| 365 | if (!ans) goto x_nomem; |
| 366 | qu->answer= ans; |
| 367 | } |
| 368 | |
| 369 | qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans)); |
| 370 | adns__makefinal_str(qu,&ans->cname); |
| 371 | |
| 372 | if (ans->nrrs) { |
| 373 | adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz); |
| 374 | |
| 375 | for (rrn=0; rrn<ans->nrrs; rrn++) |
| 376 | qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz); |
| 377 | } |
| 378 | |
| 379 | free_query_allocs(qu); |
| 380 | return; |
| 381 | |
| 382 | x_nomem: |
| 383 | qu->answer->status= adns_s_nomemory; |
| 384 | qu->answer->cname= 0; |
| 385 | adns__reset_cnameonly(qu); |
| 386 | free_query_allocs(qu); |
| 387 | } |
| 388 | |
| 389 | void adns__query_done(adns_query qu) { |
| 390 | adns_answer *ans; |
| 391 | adns_query parent; |
| 392 | |
| 393 | qu->id= -1; |
| 394 | ans= qu->answer; |
| 395 | |
| 396 | if (ans->nrrs && qu->typei->diff_needswap) { |
| 397 | if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) { |
| 398 | adns__query_fail(qu,adns_s_nomemory); |
| 399 | return; |
| 400 | } |
| 401 | adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz, |
| 402 | qu->vb.buf, |
| 403 | (int(*)(void*, const void*, const void*))qu->typei->diff_needswap, |
| 404 | qu->ads); |
| 405 | } |
| 406 | |
| 407 | ans->expires= qu->expires; |
| 408 | parent= qu->parent; |
| 409 | if (parent) { |
| 410 | LIST_UNLINK_PART(parent->children,qu,siblings.); |
| 411 | qu->ctx.callback(parent,qu); |
| 412 | free_query_allocs(qu); |
| 413 | free(qu); |
| 414 | } else { |
| 415 | makefinal_query(qu); |
| 416 | LIST_LINK_TAIL(qu->ads->output,qu); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | void adns__query_fail(adns_query qu, adns_status stat) { |
| 421 | adns__reset_cnameonly(qu); |
| 422 | qu->answer->status= stat; |
| 423 | adns__query_done(qu); |
| 424 | } |
| 425 | |
| 426 | void adns__makefinal_str(adns_query qu, char **strp) { |
| 427 | int l; |
| 428 | char *before, *after; |
| 429 | |
| 430 | before= *strp; |
| 431 | if (!before) return; |
| 432 | l= strlen(before)+1; |
| 433 | after= adns__alloc_final(qu,l); |
| 434 | memcpy(after,before,l); |
| 435 | *strp= after; |
| 436 | } |
| 437 | |
| 438 | void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) { |
| 439 | void *before, *after; |
| 440 | |
| 441 | before= *blpp; |
| 442 | if (!before) return; |
| 443 | after= adns__alloc_final(qu,sz); |
| 444 | memcpy(after,before,sz); |
| 445 | *blpp= after; |
| 446 | } |