1 /* $OpenBSD: asr_private.h,v 1.27 2015/02/14 20:15:05 jca Exp $ */ 2 /* 3 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <stdio.h> 19 20 #define QR_MASK (0x1 << 15) 21 #define OPCODE_MASK (0xf << 11) 22 #define AA_MASK (0x1 << 10) 23 #define TC_MASK (0x1 << 9) 24 #define RD_MASK (0x1 << 8) 25 #define RA_MASK (0x1 << 7) 26 #define Z_MASK (0x7 << 4) 27 #define RCODE_MASK (0xf) 28 29 #define OPCODE(v) ((v) & OPCODE_MASK) 30 #define RCODE(v) ((v) & RCODE_MASK) 31 32 33 struct asr_pack { 34 char *buf; 35 size_t len; 36 size_t offset; 37 const char *err; 38 }; 39 40 struct asr_unpack { 41 const char *buf; 42 size_t len; 43 size_t offset; 44 const char *err; 45 }; 46 47 struct asr_dns_header { 48 uint16_t id; 49 uint16_t flags; 50 uint16_t qdcount; 51 uint16_t ancount; 52 uint16_t nscount; 53 uint16_t arcount; 54 }; 55 56 struct asr_dns_query { 57 char q_dname[MAXDNAME]; 58 uint16_t q_type; 59 uint16_t q_class; 60 }; 61 62 struct asr_dns_rr { 63 char rr_dname[MAXDNAME]; 64 uint16_t rr_type; 65 uint16_t rr_class; 66 uint32_t rr_ttl; 67 union { 68 struct { 69 char cname[MAXDNAME]; 70 } cname; 71 struct { 72 uint16_t preference; 73 char exchange[MAXDNAME]; 74 } mx; 75 struct { 76 char nsname[MAXDNAME]; 77 } ns; 78 struct { 79 char ptrname[MAXDNAME]; 80 } ptr; 81 struct { 82 char mname[MAXDNAME]; 83 char rname[MAXDNAME]; 84 uint32_t serial; 85 uint32_t refresh; 86 uint32_t retry; 87 uint32_t expire; 88 uint32_t minimum; 89 } soa; 90 struct { 91 struct in_addr addr; 92 } in_a; 93 struct { 94 struct in6_addr addr6; 95 } in_aaaa; 96 struct { 97 uint16_t rdlen; 98 const void *rdata; 99 } other; 100 } rr; 101 }; 102 103 104 #define ASR_MAXNS 5 105 #define ASR_MAXDB 3 106 #define ASR_MAXDOM 10 107 108 enum async_type { 109 ASR_SEND, 110 ASR_SEARCH, 111 ASR_GETRRSETBYNAME, 112 ASR_GETHOSTBYNAME, 113 ASR_GETHOSTBYADDR, 114 ASR_GETNETBYNAME, 115 ASR_GETNETBYADDR, 116 ASR_GETADDRINFO, 117 ASR_GETNAMEINFO, 118 }; 119 120 #define ASR_DB_FILE 'f' 121 #define ASR_DB_DNS 'b' 122 #define ASR_DB_YP 'y' 123 124 struct asr_ctx { 125 int ac_refcount; 126 int ac_options; 127 int ac_ndots; 128 char *ac_domain; 129 int ac_domcount; 130 char *ac_dom[ASR_MAXDOM]; 131 int ac_dbcount; 132 char ac_db[ASR_MAXDB + 1]; 133 int ac_family[3]; 134 135 char *ac_hostfile; 136 137 int ac_nscount; 138 int ac_nstimeout; 139 int ac_nsretries; 140 struct sockaddr *ac_ns[ASR_MAXNS]; 141 142 }; 143 144 struct asr { 145 char *a_path; 146 time_t a_mtime; 147 time_t a_rtime; 148 struct asr_ctx *a_ctx; 149 }; 150 151 #define ASYNC_COND 0 152 #define ASYNC_DONE 1 153 154 #define ASYNC_DOM_FQDN 0x00000001 155 #define ASYNC_DOM_NDOTS 0x00000002 156 #define ASYNC_DOM_DOMAIN 0x00000004 157 #define ASYNC_DOM_ASIS 0x00000008 158 159 #define ASYNC_NODATA 0x00000100 160 #define ASYNC_AGAIN 0x00000200 161 162 #define ASYNC_EXTOBUF 0x00002000 163 164 #define ASYNC_NO_INET 0x00010000 165 #define ASYNC_NO_INET6 0x00020000 166 167 struct asr_query { 168 int (*as_run)(struct asr_query *, struct asr_result *); 169 struct asr_ctx *as_ctx; 170 int as_type; 171 int as_state; 172 173 /* cond */ 174 int as_timeout; 175 int as_fd; 176 177 /* loop indices in ctx */ 178 int as_dom_step; 179 int as_dom_idx; 180 int as_dom_flags; 181 int as_family_idx; 182 int as_db_idx; 183 184 int as_count; 185 186 union { 187 struct { 188 int flags; 189 uint16_t reqid; 190 int class; 191 int type; 192 char *dname; /* not fqdn! */ 193 int rcode; /* response code */ 194 int ancount; /* answer count */ 195 196 int nsidx; 197 int nsloop; 198 199 /* io buffers for query/response */ 200 unsigned char *obuf; 201 size_t obuflen; 202 size_t obufsize; 203 unsigned char *ibuf; 204 size_t ibuflen; 205 size_t ibufsize; 206 size_t datalen; /* for tcp io */ 207 uint16_t pktlen; 208 } dns; 209 210 struct { 211 int flags; 212 int class; 213 int type; 214 char *name; 215 struct asr_query *subq; 216 int saved_h_errno; 217 } search; 218 219 struct { 220 int flags; 221 int class; 222 int type; 223 char *name; 224 struct asr_query *subq; 225 } rrset; 226 227 struct { 228 char *name; 229 int family; 230 struct asr_query *subq; 231 char addr[16]; 232 int addrlen; 233 int subq_h_errno; 234 } hostnamadr; 235 236 struct { 237 char *name; 238 int family; 239 struct asr_query *subq; 240 in_addr_t addr; 241 } netnamadr; 242 243 struct { 244 char *hostname; 245 char *servname; 246 int port_tcp; 247 int port_udp; 248 union { 249 struct sockaddr sa; 250 struct sockaddr_in sain; 251 struct sockaddr_in6 sain6; 252 } sa; 253 254 struct addrinfo hints; 255 char *fqdn; 256 struct addrinfo *aifirst; 257 struct addrinfo *ailast; 258 struct asr_query *subq; 259 int flags; 260 } ai; 261 262 struct { 263 char *hostname; 264 char *servname; 265 size_t hostnamelen; 266 size_t servnamelen; 267 union { 268 struct sockaddr sa; 269 struct sockaddr_in sain; 270 struct sockaddr_in6 sain6; 271 } sa; 272 int flags; 273 struct asr_query *subq; 274 } ni; 275 #define MAXTOKEN 10 276 } as; 277 278 }; 279 280 #define AS_DB(p) ((p)->as_ctx->ac_db[(p)->as_db_idx - 1]) 281 #define AS_FAMILY(p) ((p)->as_ctx->ac_family[(p)->as_family_idx]) 282 283 enum asr_state { 284 ASR_STATE_INIT, 285 ASR_STATE_NEXT_DOMAIN, 286 ASR_STATE_NEXT_DB, 287 ASR_STATE_SAME_DB, 288 ASR_STATE_NEXT_FAMILY, 289 ASR_STATE_NEXT_NS, 290 ASR_STATE_UDP_SEND, 291 ASR_STATE_UDP_RECV, 292 ASR_STATE_TCP_WRITE, 293 ASR_STATE_TCP_READ, 294 ASR_STATE_PACKET, 295 ASR_STATE_SUBQUERY, 296 ASR_STATE_NOT_FOUND, 297 ASR_STATE_HALT, 298 }; 299 300 301 /* asr_utils.c */ 302 void asr_pack_init(struct asr_pack *, char *, size_t); 303 int asr_pack_header(struct asr_pack *, const struct asr_dns_header *); 304 int asr_pack_query(struct asr_pack *, uint16_t, uint16_t, const char *); 305 void asr_unpack_init(struct asr_unpack *, const char *, size_t); 306 int asr_unpack_header(struct asr_unpack *, struct asr_dns_header *); 307 int asr_unpack_query(struct asr_unpack *, struct asr_dns_query *); 308 int asr_unpack_rr(struct asr_unpack *, struct asr_dns_rr *); 309 int asr_sockaddr_from_str(struct sockaddr *, int, const char *); 310 ssize_t asr_dname_from_fqdn(const char *, char *, size_t); 311 ssize_t asr_addr_as_fqdn(const char *, int, char *, size_t); 312 313 /* asr.c */ 314 void *asr_resolver(const char *); 315 void asr_resolver_done(void *); 316 struct asr_ctx *asr_use_resolver(void *); 317 void asr_ctx_unref(struct asr_ctx *); 318 struct asr_query *asr_async_new(struct asr_ctx *, int); 319 void asr_async_free(struct asr_query *); 320 size_t asr_make_fqdn(const char *, const char *, char *, size_t); 321 char *asr_strdname(const char *, char *, size_t); 322 int asr_iter_db(struct asr_query *); 323 int asr_parse_namedb_line(FILE *, char **, int); 324 char *asr_hostalias(struct asr_ctx *, const char *, char *, size_t); 325 326 /* *_async.c */ 327 struct asr_query *res_query_async_ctx(const char *, int, int, struct asr_ctx *); 328 struct asr_query *res_search_async_ctx(const char *, int, int, struct asr_ctx *); 329 struct asr_query *gethostbyaddr_async_ctx(const void *, socklen_t, int, 330 struct asr_ctx *); 331 332 #ifdef DEBUG 333 334 #define DPRINT(...) do { if(asr_debug) { \ 335 fprintf(asr_debug, __VA_ARGS__); \ 336 } } while (0) 337 #define DPRINT_PACKET(n, p, s) do { if(asr_debug) { \ 338 fprintf(asr_debug, "----- %s -----\n", n); \ 339 asr_dump_packet(asr_debug, (p), (s)); \ 340 fprintf(asr_debug, "--------------\n"); \ 341 } } while (0) 342 343 const char *asr_querystr(int); 344 const char *asr_statestr(int); 345 const char *asr_transitionstr(int); 346 const char *print_sockaddr(const struct sockaddr *, char *, size_t); 347 void asr_dump_config(FILE *, struct asr *); 348 void asr_dump_packet(FILE *, const void *, size_t); 349 350 extern FILE * asr_debug; 351 352 #else /* DEBUG */ 353 354 #define DPRINT(...) 355 #define DPRINT_PACKET(...) 356 357 #endif /* DEBUG */ 358 359 #define async_set_state(a, s) do { \ 360 DPRINT("asr: [%s@%p] %s -> %s\n", \ 361 asr_querystr((a)->as_type), \ 362 as, \ 363 asr_statestr((a)->as_state), \ 364 asr_statestr((s))); \ 365 (a)->as_state = (s); } while (0) 366