1 /* $OpenBSD: asr_private.h,v 1.26 2014/04/17 15:28:26 guenther 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 ASR_BUFSIZ 1024 21 22 #define QR_MASK (0x1 << 15) 23 #define OPCODE_MASK (0xf << 11) 24 #define AA_MASK (0x1 << 10) 25 #define TC_MASK (0x1 << 9) 26 #define RD_MASK (0x1 << 8) 27 #define RA_MASK (0x1 << 7) 28 #define Z_MASK (0x7 << 4) 29 #define RCODE_MASK (0xf) 30 31 #define OPCODE(v) ((v) & OPCODE_MASK) 32 #define RCODE(v) ((v) & RCODE_MASK) 33 34 35 struct asr_pack { 36 char *buf; 37 size_t len; 38 size_t offset; 39 const char *err; 40 }; 41 42 struct asr_unpack { 43 const char *buf; 44 size_t len; 45 size_t offset; 46 const char *err; 47 }; 48 49 struct asr_dns_header { 50 uint16_t id; 51 uint16_t flags; 52 uint16_t qdcount; 53 uint16_t ancount; 54 uint16_t nscount; 55 uint16_t arcount; 56 }; 57 58 struct asr_dns_query { 59 char q_dname[MAXDNAME]; 60 uint16_t q_type; 61 uint16_t q_class; 62 }; 63 64 struct asr_dns_rr { 65 char rr_dname[MAXDNAME]; 66 uint16_t rr_type; 67 uint16_t rr_class; 68 uint32_t rr_ttl; 69 union { 70 struct { 71 char cname[MAXDNAME]; 72 } cname; 73 struct { 74 uint16_t preference; 75 char exchange[MAXDNAME]; 76 } mx; 77 struct { 78 char nsname[MAXDNAME]; 79 } ns; 80 struct { 81 char ptrname[MAXDNAME]; 82 } ptr; 83 struct { 84 char mname[MAXDNAME]; 85 char rname[MAXDNAME]; 86 uint32_t serial; 87 uint32_t refresh; 88 uint32_t retry; 89 uint32_t expire; 90 uint32_t minimum; 91 } soa; 92 struct { 93 struct in_addr addr; 94 } in_a; 95 struct { 96 struct in6_addr addr6; 97 } in_aaaa; 98 struct { 99 uint16_t rdlen; 100 const void *rdata; 101 } other; 102 } rr; 103 }; 104 105 106 #define ASR_MAXNS 5 107 #define ASR_MAXDB 3 108 #define ASR_MAXDOM 10 109 110 enum async_type { 111 ASR_SEND, 112 ASR_SEARCH, 113 ASR_GETRRSETBYNAME, 114 ASR_GETHOSTBYNAME, 115 ASR_GETHOSTBYADDR, 116 ASR_GETNETBYNAME, 117 ASR_GETNETBYADDR, 118 ASR_GETADDRINFO, 119 ASR_GETNAMEINFO, 120 }; 121 122 #define ASR_DB_FILE 'f' 123 #define ASR_DB_DNS 'b' 124 125 struct asr_ctx { 126 int ac_refcount; 127 int ac_options; 128 int ac_ndots; 129 char *ac_domain; 130 int ac_domcount; 131 char *ac_dom[ASR_MAXDOM]; 132 int ac_dbcount; 133 char ac_db[ASR_MAXDB + 1]; 134 int ac_family[3]; 135 136 int ac_nscount; 137 int ac_nstimeout; 138 int ac_nsretries; 139 struct sockaddr *ac_ns[ASR_MAXNS]; 140 141 }; 142 143 struct asr { 144 pid_t a_pid; 145 time_t a_mtime; 146 time_t a_rtime; 147 struct asr_ctx *a_ctx; 148 }; 149 150 #define ASYNC_COND 0 151 #define ASYNC_DONE 1 152 153 #define ASYNC_DOM_FQDN 0x00000001 154 #define ASYNC_DOM_NDOTS 0x00000002 155 #define ASYNC_DOM_DOMAIN 0x00000004 156 #define ASYNC_DOM_ASIS 0x00000008 157 158 #define ASYNC_NODATA 0x00000100 159 #define ASYNC_AGAIN 0x00000200 160 161 #define ASYNC_EXTOBUF 0x00002000 162 163 #define ASYNC_NO_INET 0x00010000 164 #define ASYNC_NO_INET6 0x00020000 165 166 struct asr_query { 167 int (*as_run)(struct asr_query *, struct asr_result *); 168 struct asr_ctx *as_ctx; 169 int as_type; 170 int as_state; 171 172 /* cond */ 173 int as_timeout; 174 int as_fd; 175 176 /* loop indices in ctx */ 177 int as_dom_step; 178 int as_dom_idx; 179 int as_dom_flags; 180 int as_family_idx; 181 int as_db_idx; 182 183 int as_count; 184 185 union { 186 struct { 187 int flags; 188 uint16_t reqid; 189 int class; 190 int type; 191 char *dname; /* not fqdn! */ 192 int rcode; /* response code */ 193 int ancount; /* answer count */ 194 195 int nsidx; 196 int nsloop; 197 198 /* io buffers for query/response */ 199 unsigned char *obuf; 200 size_t obuflen; 201 size_t obufsize; 202 unsigned char *ibuf; 203 size_t ibuflen; 204 size_t ibufsize; 205 size_t datalen; /* for tcp io */ 206 uint16_t pktlen; 207 } dns; 208 209 struct { 210 int flags; 211 int class; 212 int type; 213 char *name; 214 struct asr_query *subq; 215 int saved_h_errno; 216 } search; 217 218 struct { 219 int flags; 220 int class; 221 int type; 222 char *name; 223 struct asr_query *subq; 224 } rrset; 225 226 struct { 227 char *name; 228 int family; 229 struct asr_query *subq; 230 char addr[16]; 231 int addrlen; 232 int subq_h_errno; 233 } hostnamadr; 234 235 struct { 236 char *name; 237 int family; 238 struct asr_query *subq; 239 in_addr_t addr; 240 } netnamadr; 241 242 struct { 243 char *hostname; 244 char *servname; 245 int port_tcp; 246 int port_udp; 247 union { 248 struct sockaddr sa; 249 struct sockaddr_in sain; 250 struct sockaddr_in6 sain6; 251 } sa; 252 253 struct addrinfo hints; 254 char *fqdn; 255 struct addrinfo *aifirst; 256 struct addrinfo *ailast; 257 struct asr_query *subq; 258 int flags; 259 } ai; 260 261 struct { 262 char *hostname; 263 char *servname; 264 size_t hostnamelen; 265 size_t servnamelen; 266 union { 267 struct sockaddr sa; 268 struct sockaddr_in sain; 269 struct sockaddr_in6 sain6; 270 } sa; 271 int flags; 272 struct asr_query *subq; 273 } ni; 274 #define MAXTOKEN 10 275 } as; 276 277 }; 278 279 #define AS_DB(p) ((p)->as_ctx->ac_db[(p)->as_db_idx - 1]) 280 #define AS_FAMILY(p) ((p)->as_ctx->ac_family[(p)->as_family_idx]) 281 282 enum asr_state { 283 ASR_STATE_INIT, 284 ASR_STATE_NEXT_DOMAIN, 285 ASR_STATE_NEXT_DB, 286 ASR_STATE_SAME_DB, 287 ASR_STATE_NEXT_FAMILY, 288 ASR_STATE_NEXT_NS, 289 ASR_STATE_UDP_SEND, 290 ASR_STATE_UDP_RECV, 291 ASR_STATE_TCP_WRITE, 292 ASR_STATE_TCP_READ, 293 ASR_STATE_PACKET, 294 ASR_STATE_SUBQUERY, 295 ASR_STATE_NOT_FOUND, 296 ASR_STATE_HALT, 297 }; 298 299 300 /* asr_utils.c */ 301 void _asr_pack_init(struct asr_pack *, char *, size_t); 302 int _asr_pack_header(struct asr_pack *, const struct asr_dns_header *); 303 int _asr_pack_query(struct asr_pack *, uint16_t, uint16_t, const char *); 304 void _asr_unpack_init(struct asr_unpack *, const char *, size_t); 305 int _asr_unpack_header(struct asr_unpack *, struct asr_dns_header *); 306 int _asr_unpack_query(struct asr_unpack *, struct asr_dns_query *); 307 int _asr_unpack_rr(struct asr_unpack *, struct asr_dns_rr *); 308 int _asr_sockaddr_from_str(struct sockaddr *, int, const char *); 309 ssize_t _asr_dname_from_fqdn(const char *, char *, size_t); 310 ssize_t _asr_addr_as_fqdn(const char *, int, char *, size_t); 311 312 /* asr.c */ 313 void _asr_resolver_done(void *); 314 struct asr_ctx *_asr_use_resolver(void *); 315 struct asr_ctx *_asr_no_resolver(void); 316 void _asr_ctx_unref(struct asr_ctx *); 317 struct asr_query *_asr_async_new(struct asr_ctx *, int); 318 void _asr_async_free(struct asr_query *); 319 size_t _asr_make_fqdn(const char *, const char *, char *, size_t); 320 char *_asr_strdname(const char *, char *, size_t); 321 int _asr_iter_db(struct asr_query *); 322 int _asr_parse_namedb_line(FILE *, char **, int, char *, size_t); 323 324 /* *_async.c */ 325 struct asr_query *_res_query_async_ctx(const char *, int, int, struct asr_ctx *); 326 struct asr_query *_res_search_async_ctx(const char *, int, int, struct asr_ctx *); 327 struct asr_query *_gethostbyaddr_async_ctx(const void *, socklen_t, int, 328 struct asr_ctx *); 329 330 int _asr_iter_domain(struct asr_query *, const char *, char *, size_t); 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 *_asr_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