1 /* $OpenBSD: asr_private.h,v 1.30 2015/06/04 19:23:17 eric 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 int ac_nscount; 136 int ac_nstimeout; 137 int ac_nsretries; 138 struct sockaddr *ac_ns[ASR_MAXNS]; 139 140 }; 141 142 struct asr { 143 char *a_path; 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(const char *); 314 void asr_resolver_done(void *); 315 struct asr_ctx *asr_use_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 char *asr_hostalias(struct asr_ctx *, const char *, char *, size_t); 324 325 /* *_async.c */ 326 struct asr_query *res_query_async_ctx(const char *, int, int, struct asr_ctx *); 327 struct asr_query *res_search_async_ctx(const char *, int, int, struct asr_ctx *); 328 struct asr_query *gethostbyaddr_async_ctx(const void *, socklen_t, int, 329 struct asr_ctx *); 330 331 #ifdef DEBUG 332 333 #define DPRINT(...) do { if(asr_debug) { \ 334 fprintf(asr_debug, __VA_ARGS__); \ 335 } } while (0) 336 #define DPRINT_PACKET(n, p, s) do { if(asr_debug) { \ 337 fprintf(asr_debug, "----- %s -----\n", n); \ 338 asr_dump_packet(asr_debug, (p), (s)); \ 339 fprintf(asr_debug, "--------------\n"); \ 340 } } while (0) 341 342 const char *asr_querystr(int); 343 const char *asr_statestr(int); 344 const char *asr_transitionstr(int); 345 const char *print_sockaddr(const struct sockaddr *, char *, size_t); 346 void asr_dump_config(FILE *, struct asr *); 347 void asr_dump_packet(FILE *, const void *, size_t); 348 349 extern FILE * asr_debug; 350 351 #else /* DEBUG */ 352 353 #define DPRINT(...) 354 #define DPRINT_PACKET(...) 355 356 #endif /* DEBUG */ 357 358 #define async_set_state(a, s) do { \ 359 DPRINT("asr: [%s@%p] %s -> %s\n", \ 360 asr_querystr((a)->as_type), \ 361 as, \ 362 asr_statestr((a)->as_state), \ 363 asr_statestr((s))); \ 364 (a)->as_state = (s); } while (0) 365