]>
Commit | Line | Data |
---|---|---|
1 | /* sar.h - srvx asynchronous resolver | |
2 | * Copyright 2005, 2007 Michael Poole <mdpoole@troilus.org> | |
3 | * | |
4 | * This file is part of srvx. | |
5 | * | |
6 | * srvx is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with srvx; if not, write to the Free Software Foundation, | |
18 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
19 | */ | |
20 | ||
21 | #if !defined(SRVX_SAR_H) | |
22 | #define SRVX_SAR_H | |
23 | ||
24 | #include "common.h" | |
25 | ||
26 | #define SAI_NUMERICHOST 0x01 /* simply translate address from text form */ | |
27 | #define SAI_CANONNAME 0x02 /* fill in canonical name of host */ | |
28 | #define SAI_PASSIVE 0x04 /* if node==NULL, use unspecified address */ | |
29 | #define SAI_V4MAPPED 0x08 /* accept v4-mapped IPv6 addresses */ | |
30 | #define SAI_ALL 0x10 /* return both IPv4 and IPv6 addresses */ | |
31 | #define SAI_NOSRV 0x20 /* suppress SRV even if default is to use it */ | |
32 | #define SAI_FORCESRV 0x40 /* force SRV request even if questionable */ | |
33 | ||
34 | #define SNI_NOFQDN 0x01 /* omit domain name for local hosts */ | |
35 | #define SNI_NUMERICHOST 0x02 /* do not resolve address, just translate to text */ | |
36 | #define SNI_NAMEREQD 0x04 /* indicate error if no name exists */ | |
37 | #define SNI_NUMERICSERV 0x08 /* return service in numeric form */ | |
38 | #define SNI_DGRAM 0x10 /* return service names for UDP use */ | |
39 | #define SNI_PARANOID 0x20 /* confirm forward resolution of name */ | |
40 | ||
41 | enum sar_errcode { | |
42 | SAI_SUCCESS, | |
43 | SAI_FAMILY, | |
44 | SAI_SOCKTYPE, | |
45 | SAI_BADFLAGS, | |
46 | SAI_NONAME, | |
47 | SAI_SERVICE, | |
48 | SAI_ADDRFAMILY, | |
49 | SAI_NODATA, | |
50 | SAI_MEMORY, | |
51 | SAI_FAIL, | |
52 | SAI_AGAIN, | |
53 | SAI_MISMATCH, | |
54 | SAI_SYSTEM | |
55 | }; | |
56 | ||
57 | struct sockaddr; | |
58 | struct addrinfo; | |
59 | struct sar_request; | |
60 | ||
61 | void sar_init(void); | |
62 | const char *sar_strerror(enum sar_errcode errcode); | |
63 | ||
64 | int sar_get_port(const struct sockaddr *sa, unsigned int socklen); | |
65 | int sar_set_port(struct sockaddr *sa, unsigned int socklen, unsigned short port); | |
66 | unsigned int sar_pton(struct sockaddr *sa, unsigned int socklen, unsigned int *bits, const char *input); | |
67 | typedef void (*sar_addr_cb)(void *ctx, struct addrinfo *res, enum sar_errcode errcode); | |
68 | struct sar_request *sar_getaddr(const char *node, const char *service, const struct addrinfo *hints, sar_addr_cb cb, void *cb_ctx); | |
69 | void sar_free(struct addrinfo *ai); | |
70 | ||
71 | /** Maximum value returnable by sar_ntop(). */ | |
72 | #define SAR_NTOP_MAX 40 | |
73 | unsigned int sar_ntop(char *output, unsigned int out_size, const struct sockaddr *sa, unsigned int socklen); | |
74 | typedef void (*sar_name_cb)(void *ctx, const char *host, const char *serv, enum sar_errcode errcode); | |
75 | struct sar_request *sar_getname(const struct sockaddr *sa, unsigned int salen, int flags, sar_name_cb cb, void *cb_ctx); | |
76 | ||
77 | /** Generic DNS lookup support. */ | |
78 | ||
79 | /** DNS message (request and response) header. */ | |
80 | struct dns_header { | |
81 | uint16_t id; | |
82 | uint16_t flags; | |
83 | #define REQ_FLAG_QR 0x8000 /* response */ | |
84 | #define REQ_FLAG_OPCODE_MASK 0x7800 /* opcode mask */ | |
85 | #define REQ_FLAG_OPCODE_SHIFT 11 /* opcode shift count */ | |
86 | #define REQ_OPCODE_QUERY (0 << REQ_FLAG_OPCODE_SHIFT) | |
87 | #define REQ_FLAG_AA 0x0400 /* authoritative answer */ | |
88 | #define REQ_FLAG_TC 0x0200 /* truncated message */ | |
89 | #define REQ_FLAG_RD 0x0100 /* recursion desired */ | |
90 | #define REQ_FLAG_RA 0x0080 /* recursion available */ | |
91 | /* 0x0040 bit currently reserved and must be zero; 0x0020 and 0x0010 | |
92 | * used by DNSSEC. */ | |
93 | #define REQ_FLAG_RCODE_MASK 0x000f /* response code mask */ | |
94 | #define REQ_FLAG_RCODE_SHIFT 0 /* response code shift count */ | |
95 | #define RCODE_NO_ERROR 0 | |
96 | #define RCODE_FORMAT_ERROR 1 | |
97 | #define RCODE_SERVER_FAILURE 2 | |
98 | #define RCODE_NAME_ERROR 3 /* aka NXDOMAIN (since RFC2308) */ | |
99 | #define RCODE_NOT_IMPLEMENTED 4 | |
100 | #define RCODE_REFUSED 5 | |
101 | #define RCODE_BAD_OPT_VERSION 16 /* RFC 2671 */ | |
102 | uint16_t qdcount; /* count of questions */ | |
103 | uint16_t ancount; /* count of answer RRs */ | |
104 | uint16_t nscount; /* count of NS (authority) RRs */ | |
105 | uint16_t arcount; /* count of additional RRs */ | |
106 | }; | |
107 | ||
108 | /** DNS resource record. */ | |
109 | struct dns_rr { | |
110 | uint16_t type; | |
111 | uint16_t class; | |
112 | uint32_t ttl; | |
113 | uint16_t rdlength; | |
114 | uint16_t rd_start; | |
115 | char *name; | |
116 | }; | |
117 | ||
118 | #define REQ_TYPE_A 1 | |
119 | #define REQ_TYPE_NS 2 | |
120 | #define REQ_TYPE_CNAME 5 | |
121 | #define REQ_TYPE_SOA 6 | |
122 | #define REQ_TYPE_PTR 12 | |
123 | #define REQ_TYPE_MX 15 | |
124 | #define REQ_TYPE_TXT 16 | |
125 | #define REQ_TYPE_AAAA 28 /* RFC 3596 */ | |
126 | #define REQ_TYPE_SRV 33 /* RFC 2782 */ | |
127 | #define REQ_TYPE_OPT 41 /* RFC 2671 */ | |
128 | #define REQ_QTYPE_ALL 255 | |
129 | #define REQ_CLASS_IN 1 | |
130 | #define REQ_QCLASS_ALL 255 | |
131 | ||
132 | struct sar_request; | |
133 | typedef void (*sar_request_ok_cb)(struct sar_request *req, struct dns_header *hdr, struct dns_rr *rr, unsigned char *raw, unsigned int raw_size); | |
134 | typedef void (*sar_request_fail_cb)(struct sar_request *req, unsigned int rcode); | |
135 | ||
136 | /** Pending request structure. | |
137 | * User code should treat this structure as opaque. | |
138 | */ | |
139 | struct sar_request { | |
140 | int id; | |
141 | time_t expiry; | |
142 | sar_request_ok_cb cb_ok; | |
143 | sar_request_fail_cb cb_fail; | |
144 | unsigned char *body; | |
145 | unsigned int body_len; | |
146 | unsigned char retries; | |
147 | char id_text[6]; | |
148 | }; | |
149 | ||
150 | const char *sar_rcode_text(unsigned int rcode); | |
151 | struct sar_request *sar_request_alloc(unsigned int data_len, sar_request_ok_cb ok_cb, sar_request_fail_cb fail_cb); | |
152 | unsigned int sar_request_build(struct sar_request *req, ...); | |
153 | void sar_request_send(struct sar_request *req); | |
154 | struct sar_request *sar_request_simple(unsigned int data_len, sar_request_ok_cb ok_cb, sar_request_fail_cb fail_cb, ...); | |
155 | void sar_request_abort(struct sar_request *req); | |
156 | char *sar_extract_name(const unsigned char *buf, unsigned int size, unsigned int *ppos); | |
157 | ||
158 | #endif /* !defined(SRVX_SAR_H) */ |