| 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, 1998 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 | |
| 31 | #include <sys/time.h> |
| 32 | |
| 33 | #include "internal.h" |
| 34 | |
| 35 | int adns__internal_submit(adns_state ads, adns_query *query_r, |
| 36 | const typeinfo *typei, vbuf *qumsg_vb, int id, |
| 37 | adns_queryflags flags, struct timeval now, |
| 38 | adns_status failstat, const qcontext *ctx) { |
| 39 | adns_query qu; |
| 40 | |
| 41 | qu= malloc(sizeof(*qu)); if (!qu) goto x_nomemory; |
| 42 | qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) goto x_freequ_nomemory; |
| 43 | |
| 44 | qu->ads= ads; |
| 45 | qu->state= query_udp; |
| 46 | qu->back= qu->next= qu->parent= 0; |
| 47 | LIST_INIT(qu->children); |
| 48 | qu->siblings.next= qu->siblings.back= 0; |
| 49 | qu->allocations= 0; |
| 50 | qu->interim_allocd= 0; |
| 51 | qu->final_allocspace= 0; |
| 52 | |
| 53 | qu->typei= typei; |
| 54 | adns__vbuf_init(&qu->vb); |
| 55 | |
| 56 | qu->cname_dgram= 0; |
| 57 | qu->cname_dglen= qu->cname_begin= 0; |
| 58 | |
| 59 | qu->id= id; |
| 60 | qu->flags= flags; |
| 61 | qu->udpretries= 0; |
| 62 | qu->udpnextserver= 0; |
| 63 | qu->udpsent= qu->tcpfailed= 0; |
| 64 | timerclear(&qu->timeout); |
| 65 | memcpy(&qu->context,ctx,sizeof(qu->context)); |
| 66 | |
| 67 | qu->answer->status= adns_s_ok; |
| 68 | qu->answer->cname= 0; |
| 69 | qu->answer->type= typei->type; |
| 70 | qu->answer->nrrs= 0; |
| 71 | qu->answer->rrs= 0; |
| 72 | qu->answer->rrsz= typei->rrsz; |
| 73 | |
| 74 | *query_r= qu; |
| 75 | |
| 76 | qu->query_dglen= qumsg_vb->used; |
| 77 | if (qumsg_vb->used) { |
| 78 | qu->query_dgram= malloc(qumsg_vb->used); |
| 79 | if (!qu->query_dgram) { |
| 80 | adns__query_fail(qu,adns_s_nolocalmem); |
| 81 | return adns_s_ok; |
| 82 | } |
| 83 | memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used); |
| 84 | } else { |
| 85 | qu->query_dgram= 0; |
| 86 | } |
| 87 | qu->vb= *qumsg_vb; |
| 88 | adns__vbuf_init(qumsg_vb); |
| 89 | |
| 90 | if (failstat) { |
| 91 | adns__query_fail(qu,failstat); |
| 92 | return adns_s_ok; |
| 93 | } |
| 94 | adns__query_udp(qu,now); |
| 95 | adns__autosys(ads,now); |
| 96 | |
| 97 | return adns_s_ok; |
| 98 | |
| 99 | x_freequ_nomemory: |
| 100 | free(qu); |
| 101 | x_nomemory: |
| 102 | adns__vbuf_free(qumsg_vb); |
| 103 | return adns_s_nolocalmem; |
| 104 | } |
| 105 | |
| 106 | int adns_submit(adns_state ads, |
| 107 | const char *owner, |
| 108 | adns_rrtype type, |
| 109 | adns_queryflags flags, |
| 110 | void *context, |
| 111 | adns_query *query_r) { |
| 112 | qcontext ctx; |
| 113 | int id, r, ol; |
| 114 | vbuf vb; |
| 115 | adns_status stat; |
| 116 | const typeinfo *typei; |
| 117 | struct timeval now; |
| 118 | |
| 119 | typei= adns__findtype(type); |
| 120 | if (!typei) return adns_s_notimplemented; |
| 121 | |
| 122 | ctx.ext= context; |
| 123 | r= gettimeofday(&now,0); if (r) return errno; |
| 124 | id= 0; |
| 125 | |
| 126 | adns__vbuf_init(&vb); |
| 127 | |
| 128 | ol= strlen(owner); |
| 129 | if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_invaliddomain; goto xit; } |
| 130 | |
| 131 | if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; } |
| 132 | |
| 133 | stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags); |
| 134 | |
| 135 | xit: |
| 136 | return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx); |
| 137 | } |
| 138 | |
| 139 | int adns_synchronous(adns_state ads, |
| 140 | const char *owner, |
| 141 | adns_rrtype type, |
| 142 | adns_queryflags flags, |
| 143 | adns_answer **answer_r) { |
| 144 | adns_query qu; |
| 145 | int r; |
| 146 | |
| 147 | r= adns_submit(ads,owner,type,flags,0,&qu); |
| 148 | if (r) return r; |
| 149 | |
| 150 | do { |
| 151 | r= adns_wait(ads,&qu,answer_r,0); |
| 152 | } while (r==EINTR); |
| 153 | if (r) adns_cancel(qu); |
| 154 | return r; |
| 155 | } |
| 156 | |
| 157 | void adns_cancel(adns_query query) { |
| 158 | abort(); /* fixme */ |
| 159 | } |
| 160 | |
| 161 | static void *alloc_common(adns_query qu, size_t sz) { |
| 162 | allocnode *an; |
| 163 | |
| 164 | if (!sz) return qu; /* Any old pointer will do */ |
| 165 | assert(!qu->final_allocspace); |
| 166 | an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz)); |
| 167 | if (!an) { |
| 168 | adns__query_fail(qu,adns_s_nolocalmem); |
| 169 | return 0; |
| 170 | } |
| 171 | an->next= qu->allocations; |
| 172 | qu->allocations= an; |
| 173 | return (byte*)an + MEM_ROUND(sizeof(*an)); |
| 174 | } |
| 175 | |
| 176 | void *adns__alloc_interim(adns_query qu, size_t sz) { |
| 177 | sz= MEM_ROUND(sz); |
| 178 | qu->interim_allocd += sz; |
| 179 | return alloc_common(qu,sz); |
| 180 | } |
| 181 | |
| 182 | void *adns__alloc_mine(adns_query qu, size_t sz) { |
| 183 | return alloc_common(qu,MEM_ROUND(sz)); |
| 184 | } |
| 185 | |
| 186 | void *adns__alloc_final(adns_query qu, size_t sz) { |
| 187 | /* When we're in the _final stage, we _subtract_ from interim_alloc'd |
| 188 | * each allocation, and use final_allocspace to point to the next free |
| 189 | * bit. |
| 190 | */ |
| 191 | void *rp; |
| 192 | |
| 193 | sz= MEM_ROUND(sz); |
| 194 | rp= qu->final_allocspace; |
| 195 | assert(rp); |
| 196 | qu->interim_allocd -= sz; |
| 197 | assert(qu->interim_allocd>=0); |
| 198 | qu->final_allocspace= (byte*)rp + sz; |
| 199 | return rp; |
| 200 | } |
| 201 | |
| 202 | void adns__reset_cnameonly(adns_query qu) { |
| 203 | assert(qu->final_allocspace); |
| 204 | qu->answer->nrrs= 0; |
| 205 | qu->answer->rrs= 0; |
| 206 | qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0; |
| 207 | } |
| 208 | |
| 209 | void adns__query_done(adns_query qu) { |
| 210 | adns_answer *ans; |
| 211 | allocnode *an, *ann; |
| 212 | int i; |
| 213 | |
| 214 | qu->answer= ans= realloc(qu->answer, |
| 215 | MEM_ROUND(MEM_ROUND(sizeof(*ans)) + |
| 216 | qu->interim_allocd)); |
| 217 | qu->final_allocspace= (byte*)qu->answer + MEM_ROUND(sizeof(*ans)); |
| 218 | |
| 219 | adns__makefinal_str(qu,&ans->cname); |
| 220 | if (ans->nrrs) { |
| 221 | adns__makefinal_block(qu,&ans->rrs.untyped,ans->rrsz*ans->nrrs); |
| 222 | for (i=0; i<ans->nrrs; i++) |
| 223 | qu->typei->makefinal(qu,ans->rrs.bytes+ans->rrsz*i); |
| 224 | } |
| 225 | |
| 226 | for (an= qu->allocations; an; an= ann) { ann= an->next; free(an); } |
| 227 | |
| 228 | adns__vbuf_free(&qu->vb); |
| 229 | |
| 230 | qu->id= -1; |
| 231 | LIST_LINK_TAIL(qu->ads->output,qu); |
| 232 | } |
| 233 | |
| 234 | void adns__query_fail(adns_query qu, adns_status stat) { |
| 235 | adns__reset_cnameonly(qu); |
| 236 | qu->answer->status= stat; |
| 237 | adns__query_done(qu); |
| 238 | } |
| 239 | |
| 240 | void adns__makefinal_str(adns_query qu, char **strp) { |
| 241 | int l; |
| 242 | char *before, *after; |
| 243 | |
| 244 | before= *strp; |
| 245 | l= strlen(before)+1; |
| 246 | after= adns__alloc_final(qu,l); |
| 247 | memcpy(after,before,l); |
| 248 | *strp= after; |
| 249 | } |
| 250 | |
| 251 | void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) { |
| 252 | void *after; |
| 253 | |
| 254 | after= adns__alloc_final(qu,sz); |
| 255 | memcpy(after,*blpp,sz); |
| 256 | *blpp= after; |
| 257 | } |
| 258 | |