James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file re_dns.h Interface to DNS module |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | enum { |
| 9 | DNS_PORT = 53, |
| 10 | DNS_HEADER_SIZE = 12 |
| 11 | }; |
| 12 | |
| 13 | |
| 14 | /** DNS Opcodes */ |
| 15 | enum { |
| 16 | DNS_OPCODE_QUERY = 0, |
| 17 | DNS_OPCODE_IQUERY = 1, |
| 18 | DNS_OPCODE_STATUS = 2, |
| 19 | DNS_OPCODE_NOTIFY = 4 |
| 20 | }; |
| 21 | |
| 22 | |
| 23 | /** DNS Response codes */ |
| 24 | enum { |
| 25 | DNS_RCODE_OK = 0, |
| 26 | DNS_RCODE_FMT_ERR = 1, |
| 27 | DNS_RCODE_SRV_FAIL = 2, |
| 28 | DNS_RCODE_NAME_ERR = 3, |
| 29 | DNS_RCODE_NOT_IMPL = 4, |
| 30 | DNS_RCODE_REFUSED = 5, |
| 31 | DNS_RCODE_NOT_AUTH = 9 |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | /** DNS Resource Record types */ |
| 36 | enum { |
| 37 | DNS_TYPE_A = 0x0001, |
| 38 | DNS_TYPE_NS = 0x0002, |
| 39 | DNS_TYPE_CNAME = 0x0005, |
| 40 | DNS_TYPE_SOA = 0x0006, |
| 41 | DNS_TYPE_PTR = 0x000c, |
| 42 | DNS_TYPE_MX = 0x000f, |
| 43 | DNS_TYPE_AAAA = 0x001c, |
| 44 | DNS_TYPE_SRV = 0x0021, |
| 45 | DNS_TYPE_NAPTR = 0x0023, |
| 46 | DNS_QTYPE_IXFR = 0x00fb, |
| 47 | DNS_QTYPE_AXFR = 0x00fc, |
| 48 | DNS_QTYPE_ANY = 0x00ff |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | /** DNS Classes */ |
| 53 | enum { |
| 54 | DNS_CLASS_IN = 0x0001, |
| 55 | DNS_QCLASS_ANY = 0x00ff |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | /** Defines a DNS Header */ |
| 60 | struct dnshdr { |
| 61 | uint16_t id; |
| 62 | bool qr; |
| 63 | uint8_t opcode; |
| 64 | bool aa; |
| 65 | bool tc; |
| 66 | bool rd; |
| 67 | bool ra; |
| 68 | uint8_t z; |
| 69 | uint8_t rcode; |
| 70 | uint16_t nq; |
| 71 | uint16_t nans; |
| 72 | uint16_t nauth; |
| 73 | uint16_t nadd; |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | /** Defines a DNS Resource Record (RR) */ |
| 78 | struct dnsrr { |
| 79 | struct le le; |
| 80 | struct le le_priv; |
| 81 | char *name; |
| 82 | uint16_t type; |
| 83 | uint16_t dnsclass; |
| 84 | int64_t ttl; |
| 85 | uint16_t rdlen; |
| 86 | union { |
| 87 | struct { |
| 88 | uint32_t addr; |
| 89 | } a; |
| 90 | struct { |
| 91 | char *nsdname; |
| 92 | } ns; |
| 93 | struct { |
| 94 | char *cname; |
| 95 | } cname; |
| 96 | struct { |
| 97 | char *mname; |
| 98 | char *rname; |
| 99 | uint32_t serial; |
| 100 | uint32_t refresh; |
| 101 | uint32_t retry; |
| 102 | uint32_t expire; |
| 103 | uint32_t ttlmin; |
| 104 | } soa; |
| 105 | struct { |
| 106 | char *ptrdname; |
| 107 | } ptr; |
| 108 | struct { |
| 109 | uint16_t pref; |
| 110 | char *exchange; |
| 111 | } mx; |
| 112 | struct { |
| 113 | uint8_t addr[16]; |
| 114 | } aaaa; |
| 115 | struct { |
| 116 | uint16_t pri; |
| 117 | uint16_t weight; |
| 118 | uint16_t port; |
| 119 | char *target; |
| 120 | } srv; |
| 121 | struct { |
| 122 | uint16_t order; |
| 123 | uint16_t pref; |
| 124 | char *flags; |
| 125 | char *services; |
| 126 | char *regexp; |
| 127 | char *replace; |
| 128 | } naptr; |
| 129 | } rdata; |
| 130 | }; |
| 131 | |
| 132 | struct hash; |
| 133 | |
| 134 | /** |
| 135 | * Defines the DNS Query handler |
| 136 | * |
| 137 | * @param err 0 if success, otherwise errorcode |
| 138 | * @param hdr DNS Header |
| 139 | * @param ansl List of Answer records |
| 140 | * @param authl List of Authoritive records |
| 141 | * @param addl List of Additional records |
| 142 | * @param arg Handler argument |
| 143 | */ |
| 144 | typedef void(dns_query_h)(int err, const struct dnshdr *hdr, |
| 145 | struct list *ansl, struct list *authl, |
| 146 | struct list *addl, void *arg); |
| 147 | |
| 148 | /** |
| 149 | * Defines the DNS Resource Record list handler |
| 150 | * |
| 151 | * @param rr DNS Resource Record |
| 152 | * @param arg Handler argument |
| 153 | * |
| 154 | * @return True to stop traversing, False to continue |
| 155 | */ |
| 156 | typedef bool(dns_rrlist_h)(struct dnsrr *rr, void *arg); |
| 157 | |
| 158 | int dns_hdr_encode(struct mbuf *mb, const struct dnshdr *hdr); |
| 159 | int dns_hdr_decode(struct mbuf *mb, struct dnshdr *hdr); |
| 160 | const char *dns_hdr_opcodename(uint8_t opcode); |
| 161 | const char *dns_hdr_rcodename(uint8_t rcode); |
| 162 | struct dnsrr *dns_rr_alloc(void); |
| 163 | int dns_rr_encode(struct mbuf *mb, const struct dnsrr *rr, int64_t ttl_offs, |
| 164 | struct hash *ht_dname, size_t start); |
| 165 | int dns_rr_decode(struct mbuf *mb, struct dnsrr **rr, size_t start); |
| 166 | bool dns_rr_cmp(const struct dnsrr *rr1, const struct dnsrr *rr2, bool rdata); |
| 167 | const char *dns_rr_typename(uint16_t type); |
| 168 | const char *dns_rr_classname(uint16_t dnsclass); |
| 169 | int dns_rr_print(struct re_printf *pf, const struct dnsrr *rr); |
| 170 | int dns_dname_encode(struct mbuf *mb, const char *name, |
| 171 | struct hash *ht_dname, size_t start, bool comp); |
| 172 | int dns_dname_decode(struct mbuf *mb, char **name, size_t start); |
| 173 | int dns_cstr_encode(struct mbuf *mb, const char *str); |
| 174 | int dns_cstr_decode(struct mbuf *mb, char **str); |
| 175 | void dns_rrlist_sort(struct list *rrl, uint16_t type, size_t key); |
| 176 | void dns_rrlist_sort_addr(struct list *rrl, size_t key); |
| 177 | struct dnsrr *dns_rrlist_apply(struct list *rrl, const char *name, |
| 178 | uint16_t type, uint16_t dnsclass, |
| 179 | bool recurse, dns_rrlist_h *rrlh, void *arg); |
| 180 | struct dnsrr *dns_rrlist_apply2(struct list *rrl, const char *name, |
| 181 | uint16_t type1, uint16_t type2, |
| 182 | uint16_t dnsclass, bool recurse, |
| 183 | dns_rrlist_h *rrlh, void *arg); |
| 184 | struct dnsrr *dns_rrlist_find(struct list *rrl, const char *name, |
| 185 | uint16_t type, uint16_t dnsclass, bool recurse); |
| 186 | |
| 187 | |
| 188 | /* DNS Client */ |
| 189 | struct sa; |
| 190 | struct dnsc; |
| 191 | struct dns_query; |
| 192 | |
| 193 | /** DNS Client configuration */ |
| 194 | struct dnsc_conf { |
| 195 | uint32_t query_hash_size; |
| 196 | uint32_t tcp_hash_size; |
| 197 | uint32_t conn_timeout; /* in [ms] */ |
| 198 | uint32_t idle_timeout; /* in [ms] */ |
| 199 | }; |
| 200 | |
| 201 | int dnsc_alloc(struct dnsc **dcpp, const struct dnsc_conf *conf, |
| 202 | const struct sa *srvv, uint32_t srvc); |
| 203 | int dnsc_srv_set(struct dnsc *dnsc, const struct sa *srvv, uint32_t srvc); |
| 204 | int dnsc_query(struct dns_query **qp, struct dnsc *dnsc, const char *name, |
| 205 | uint16_t type, uint16_t dnsclass, |
| 206 | bool rd, dns_query_h *qh, void *arg); |
| 207 | int dnsc_query_srv(struct dns_query **qp, struct dnsc *dnsc, const char *name, |
| 208 | uint16_t type, uint16_t dnsclass, int proto, |
| 209 | const struct sa *srvv, const uint32_t *srvc, |
| 210 | bool rd, dns_query_h *qh, void *arg); |
| 211 | int dnsc_notify(struct dns_query **qp, struct dnsc *dnsc, const char *name, |
| 212 | uint16_t type, uint16_t dnsclass, const struct dnsrr *ans_rr, |
| 213 | int proto, const struct sa *srvv, const uint32_t *srvc, |
| 214 | dns_query_h *qh, void *arg); |
| 215 | |
| 216 | |
| 217 | /* DNS System functions */ |
| 218 | int dns_srv_get(char *domain, size_t dsize, struct sa *srvv, uint32_t *n); |