Review Board 1.7.16


Refactor duplicated DNS routines into common sections

Review Request #4598 - Created April 6, 2015 and submitted

Mark Michelson
/trunk/
Reviewers
asterisk-dev
Asterisk
Josh worked on SRV in one branch, and I worked on NAPTR in a separate branch. Independently we kept coming to realizations that something that one of us had developed independently would be needed by the other person. We decided to simply have copies of common functionality in our branches. After merging, we would perform a refactor to remove duplication.

This changeset introduces no new DNS functionality. Instead, it takes some duplicated code and places them into common areas of the DNS core.
All DNS unit tests continue to pass.
/trunk/include/asterisk/dns_internal.h
Revision 434218 New Change
1
/*
1
/*
2
 * Asterisk -- An open source telephony toolkit.
2
 * Asterisk -- An open source telephony toolkit.
3
 *
3
 *
4
 * Copyright (C) 2015, Digium, Inc.
4
 * Copyright (C) 2015, Digium, Inc.
5
 *
5
 *
6
 * Joshua Colp <jcolp@digium.com>
6
 * Joshua Colp <jcolp@digium.com>
7
 *
7
 *
8
 * See http://www.asterisk.org for more information about
8
 * See http://www.asterisk.org for more information about
9
 * the Asterisk project. Please do not directly contact
9
 * the Asterisk project. Please do not directly contact
10
 * any of the maintainers of this project for assistance;
10
 * any of the maintainers of this project for assistance;
11
 * the project provides a web site, mailing lists and IRC
11
 * the project provides a web site, mailing lists and IRC
12
 * channels for your use.
12
 * channels for your use.
13
 *
13
 *
14
 * This program is free software, distributed under the terms of
14
 * This program is free software, distributed under the terms of
15
 * the GNU General Public License Version 2. See the LICENSE file
15
 * the GNU General Public License Version 2. See the LICENSE file
16
 * at the top of the source tree.
16
 * at the top of the source tree.
17
 */
17
 */
18

    
   
18

   
19
/*! \file
19
/*! \file
20
 *
20
 *
21
 * \brief Internal DNS structure definitions
21
 * \brief Internal DNS structure definitions
22
 *
22
 *
23
 * \author Joshua Colp <jcolp@digium.com>
23
 * \author Joshua Colp <jcolp@digium.com>
24
 */
24
 */
25

    
   
25

   
26
/*! \brief Generic DNS record information */
26
/*! \brief Generic DNS record information */
27
struct ast_dns_record {
27
struct ast_dns_record {
28
	/*! \brief Resource record type */
28
	/*! \brief Resource record type */
29
	int rr_type;
29
	int rr_type;
30
	/*! \brief Resource record class */
30
	/*! \brief Resource record class */
31
	int rr_class;
31
	int rr_class;
32
	/*! \brief Time-to-live of the record */
32
	/*! \brief Time-to-live of the record */
33
	int ttl;
33
	int ttl;
34
	/*! \brief The size of the raw DNS record */
34
	/*! \brief The size of the raw DNS record */
35
	size_t data_len;
35
	size_t data_len;
36
	/*! \brief Linked list information */
36
	/*! \brief Linked list information */
37
	AST_LIST_ENTRY(ast_dns_record) list;
37
	AST_LIST_ENTRY(ast_dns_record) list;
38
	/*! \brief pointer to record-specific data.
38
	/*! \brief pointer to record-specific data.
39
	 *
39
	 *
40
	 * For certain "subclasses" of DNS records, the
40
	 * For certain "subclasses" of DNS records, the
41
	 * location of the raw DNS data will differ from
41
	 * location of the raw DNS data will differ from
42
	 * the generic case. This pointer will reliably
42
	 * the generic case. This pointer will reliably
43
	 * be set to point to the raw DNS data, no matter
43
	 * be set to point to the raw DNS data, no matter
44
	 * where in the structure it may lie.
44
	 * where in the structure it may lie.
45
	 */
45
	 */
46
	char *data_ptr;
46
	char *data_ptr;
47
	/*! \brief The raw DNS record */
47
	/*! \brief The raw DNS record */
48
	char data[0];
48
	char data[0];
49
};
49
};
50

    
   
50

   
51
/*! \brief An SRV record */
51
/*! \brief An SRV record */
52
struct ast_dns_srv_record {
52
struct ast_dns_srv_record {
53
	/*! \brief Generic DNS record information */
53
	/*! \brief Generic DNS record information */
54
	struct ast_dns_record generic;
54
	struct ast_dns_record generic;
55
	/*! \brief The hostname in the SRV record */
55
	/*! \brief The hostname in the SRV record */
56
	const char *host;
56
	const char *host;
57
	/*! \brief The priority of the SRV record */
57
	/*! \brief The priority of the SRV record */
58
	unsigned short priority;
58
	unsigned short priority;
59
	/*! \brief The weight of the SRV record */
59
	/*! \brief The weight of the SRV record */
60
	unsigned short weight;
60
	unsigned short weight;
61
	/*! \brief The port in the SRV record */
61
	/*! \brief The port in the SRV record */
62
	unsigned short port;
62
	unsigned short port;
63
	/*! \brief The running weight sum */
63
	/*! \brief The running weight sum */
64
	unsigned int weight_sum;
64
	unsigned int weight_sum;
65
	/*! \brief Additional data */
65
	/*! \brief Additional data */
66
	char data[0];
66
	char data[0];
67
};
67
};
68

    
   
68

   
69
/*! \brief A NAPTR record */
69
/*! \brief A NAPTR record */
70
struct ast_dns_naptr_record {
70
struct ast_dns_naptr_record {
71
	/*! \brief Generic DNS record information */
71
	/*! \brief Generic DNS record information */
72
	struct ast_dns_record generic;
72
	struct ast_dns_record generic;
73
	/*! \brief The flags from the NAPTR record */
73
	/*! \brief The flags from the NAPTR record */
74
	const char *flags;
74
	const char *flags;
75
	/*! \brief The service from the NAPTR record */
75
	/*! \brief The service from the NAPTR record */
76
	const char *service;
76
	const char *service;
77
	/*! \brief The regular expression from the NAPTR record */
77
	/*! \brief The regular expression from the NAPTR record */
78
	const char *regexp;
78
	const char *regexp;
79
	/*! \brief The replacement from the NAPTR record */
79
	/*! \brief The replacement from the NAPTR record */
80
	const char *replacement;
80
	const char *replacement;
81
	/*! \brief The order for the NAPTR record */
81
	/*! \brief The order for the NAPTR record */
82
	unsigned short order;
82
	unsigned short order;
83
	/*! \brief The preference of the NAPTR record */
83
	/*! \brief The preference of the NAPTR record */
84
	unsigned short preference;
84
	unsigned short preference;
85
	/*! \brief Buffer for NAPTR-specific data
85
	/*! \brief Buffer for NAPTR-specific data
86
	 *
86
	 *
87
	 * This includes the raw NAPTR record, as well as
87
	 * This includes the raw NAPTR record, as well as
88
	 * the area where the flags, service, regexp, and
88
	 * the area where the flags, service, regexp, and
89
	 * replacement strings are stored.
89
	 * replacement strings are stored.
90
	 */
90
	 */
91
	char data[0];
91
	char data[0];
92
};
92
};
93

    
   
93

   
94
/*! \brief The result of a DNS query */
94
/*! \brief The result of a DNS query */
95
struct ast_dns_result {
95
struct ast_dns_result {
96
	/*! \brief Whether the result is secure */
96
	/*! \brief Whether the result is secure */
97
	unsigned int secure;
97
	unsigned int secure;
98
	/*! \brief Whether the result is bogus */
98
	/*! \brief Whether the result is bogus */
99
	unsigned int bogus;
99
	unsigned int bogus;
100
	/*! \brief Optional rcode, set if an error occurred */
100
	/*! \brief Optional rcode, set if an error occurred */
101
	unsigned int rcode;
101
	unsigned int rcode;
102
	/*! \brief Records returned */
102
	/*! \brief Records returned */
103
	AST_LIST_HEAD_NOLOCK(dns_records, ast_dns_record) records;
103
	AST_LIST_HEAD_NOLOCK(dns_records, ast_dns_record) records;
104
	/*! \brief The canonical name */
104
	/*! \brief The canonical name */
105
	const char *canonical;
105
	const char *canonical;
106
	/*! \brief The raw DNS answer */
106
	/*! \brief The raw DNS answer */
107
	const char *answer;
107
	const char *answer;
108
	/*! \brief The size of the raw DNS answer */
108
	/*! \brief The size of the raw DNS answer */
109
	size_t answer_size;
109
	size_t answer_size;
110
	/*! \brief Buffer for dynamic data */
110
	/*! \brief Buffer for dynamic data */
111
	char buf[0];
111
	char buf[0];
112
};
112
};
113

    
   
113

   
114
/*! \brief A DNS query */
114
/*! \brief A DNS query */
115
struct ast_dns_query {
115
struct ast_dns_query {
116
	/*! \brief Callback to invoke upon completion */
116
	/*! \brief Callback to invoke upon completion */
117
	ast_dns_resolve_callback callback;
117
	ast_dns_resolve_callback callback;
118
	/*! \brief User-specific data */
118
	/*! \brief User-specific data */
119
	void *user_data;
119
	void *user_data;
120
	/*! \brief The resolver in use for this query */
120
	/*! \brief The resolver in use for this query */
121
	struct ast_dns_resolver *resolver;
121
	struct ast_dns_resolver *resolver;
122
	/*! \brief Resolver-specific data */
122
	/*! \brief Resolver-specific data */
123
	void *resolver_data;
123
	void *resolver_data;
124
	/*! \brief Result of the DNS query */
124
	/*! \brief Result of the DNS query */
125
	struct ast_dns_result *result;
125
	struct ast_dns_result *result;
126
	/*! \brief Resource record type */
126
	/*! \brief Resource record type */
127
	int rr_type;
127
	int rr_type;
128
	/*! \brief Resource record class */
128
	/*! \brief Resource record class */
129
	int rr_class;
129
	int rr_class;
130
	/*! \brief The name of what is being resolved */
130
	/*! \brief The name of what is being resolved */
131
	char name[0];
131
	char name[0];
132
};
132
};
133

    
   
133

   
134
/*! \brief A recurring DNS query */
134
/*! \brief A recurring DNS query */
135
struct ast_dns_query_recurring {
135
struct ast_dns_query_recurring {
136
	/*! \brief Callback to invoke upon completion */
136
	/*! \brief Callback to invoke upon completion */
137
	ast_dns_resolve_callback callback;
137
	ast_dns_resolve_callback callback;
138
	/*! \brief User-specific data */
138
	/*! \brief User-specific data */
139
	void *user_data;
139
	void *user_data;
140
	/*! \brief Current active query */
140
	/*! \brief Current active query */
141
	struct ast_dns_query_active *active;
141
	struct ast_dns_query_active *active;
142
	/*! \brief The recurring query has been cancelled */
142
	/*! \brief The recurring query has been cancelled */
143
	unsigned int cancelled;
143
	unsigned int cancelled;
144
	/*! \brief Scheduled timer for next resolution */
144
	/*! \brief Scheduled timer for next resolution */
145
	int timer;
145
	int timer;
146
	/*! \brief Resource record type */
146
	/*! \brief Resource record type */
147
	int rr_type;
147
	int rr_type;
148
	/*! \brief Resource record class */
148
	/*! \brief Resource record class */
149
	int rr_class;
149
	int rr_class;
150
	/*! \brief The name of what is being resolved */
150
	/*! \brief The name of what is being resolved */
151
	char name[0];
151
	char name[0];
152
};
152
};
153

    
   
153

   
154
/*! \brief An active DNS query */
154
/*! \brief An active DNS query */
155
struct ast_dns_query_active {
155
struct ast_dns_query_active {
156
	/*! \brief The underlying DNS query */
156
	/*! \brief The underlying DNS query */
157
	struct ast_dns_query *query;
157
	struct ast_dns_query *query;
158
};
158
};
159

    
   
159

   
160
struct ast_sched_context;
160
struct ast_sched_context;
161

    
   
161

   
162
/*!
162
/*!
163
 * \brief Retrieve the DNS scheduler context
163
 * \brief Retrieve the DNS scheduler context
164
 *
164
 *
165
 * \return scheduler context
165
 * \return scheduler context
166
 */
166
 */
167
struct ast_sched_context *ast_dns_get_sched(void);
167
struct ast_sched_context *ast_dns_get_sched(void);
168

    
   
168

   
169
/*!
169
/*!
170
 * \brief Allocate and parse a DNS NAPTR record
170
 * \brief Allocate and parse a DNS NAPTR record
171
 *
171
 *
172
 * \param query The DNS query
172
 * \param query The DNS query
173
 * \param data This specific NAPTR record
173
 * \param data This specific NAPTR record
174
 * \param size The size of the NAPTR record
174
 * \param size The size of the NAPTR record
175
 *
175
 *
176
 * \retval non-NULL success
176
 * \retval non-NULL success
177
 * \retval NULL failure
177
 * \retval NULL failure
178
 */
178
 */
179
struct ast_dns_record *dns_naptr_alloc(struct ast_dns_query *query, const char *data, const size_t size);
179
struct ast_dns_record *dns_naptr_alloc(struct ast_dns_query *query, const char *data, const size_t size);
180

    
   
180

   
181
/*!
181
/*!
182
 * \brief Sort the NAPTR records on a result
182
 * \brief Sort the NAPTR records on a result
183
 *
183
 *
184
 * \param result The DNS result
184
 * \param result The DNS result
185
 */
185
 */
186
void dns_naptr_sort(struct ast_dns_result *result);
186
void dns_naptr_sort(struct ast_dns_result *result);
187

    
   
187

   
188
/*!
188
/*!
189
 * \brief Allocate and parse a DNS SRV record
189
 * \brief Allocate and parse a DNS SRV record
190
 *
190
 *
191
 * \param query The DNS query
191
 * \param query The DNS query
192
 * \param data This specific SRV record
192
 * \param data This specific SRV record
193
 * \param size The size of the SRV record
193
 * \param size The size of the SRV record
194
 *
194
 *
195
 * \retval non-NULL success
195
 * \retval non-NULL success
196
 * \retval NULL failure
196
 * \retval NULL failure
197
 */
197
 */
198
struct ast_dns_record *ast_dns_srv_alloc(struct ast_dns_query *query, const char *data, const size_t size);
198
struct ast_dns_record *dns_srv_alloc(struct ast_dns_query *query, const char *data, const size_t size);
199

    
   
199

   
200
/*!
200
/*!
201
 * \brief Sort the SRV records on a result
201
 * \brief Sort the SRV records on a result
202
 *
202
 *
203
 * \param result The DNS result
203
 * \param result The DNS result
204
 */
204
 */
205
void ast_dns_srv_sort(struct ast_dns_result *result);
205
void dns_srv_sort(struct ast_dns_result *result);
206

    
   
206

   

    
   
207
/*!

    
   
208
 * \brief Find the location of a DNS record within the entire DNS answer

    
   
209
 *

    
   
210
 * The DNS record that has been returned by the resolver may be a copy of the record that was

    
   
211
 * found in the complete DNS response. If so, then some DNS record types (specifically those that

    
   
212
 * parse domains) will need to locate the DNS record within the complete DNS response. This is so

    
   
213
 * that if the domain contains pointers to other sections of the DNS response, then the referenced

    
   
214
 * domains may be located.

    
   
215
 *

    
   
216
 * \param record The DNS record returned by a resolver implementation

    
   
217
 * \param record_size The size of the DNS record in bytes

    
   
218
 * \param response The complete DNS answer

    
   
219
 * \param response_size The size of the complete DNS response

    
   
220
 */

    
   
221
char *dns_find_record(const char *record, size_t record_size, const char *response, size_t response_size);

    
   
222

   

    
   
223
/*!

    
   
224
 * \brief Parse a 16-bit unsigned value from a DNS record

    
   
225
 *

    
   
226
 * \param cur Pointer to the location of the 16-bit value in the DNS record

    
   
227
 * \param[out] val The parsed 16-bit unsigned integer

    
   
228
 * \return The number of bytes consumed while parsing

    
   
229
 */

    
   
230
int dns_parse_short(unsigned char *cur, uint16_t *val);

    
   
231

   

    
   
232
/*!

    
   
233
 * \brief Parse a DNS string from a DNS record

    
   
234
 *

    
   
235
 * A DNS string consists of an 8-bit size, followed by the

    
   
236
 * string value (not NULL-terminated).

    
   
237
 *

    
   
238
 * \param cur Pointer to the location of the DNS string

    
   
239
 * \param[out] size The parsed size of the DNS string

    
   
240
 * \param[out] val The contained string (not NULL-terminated)

    
   
241
 * \return The number of bytes consumed while parsing

    
   
242
 */

    
   
243
int dns_parse_string(char *cur, uint8_t *size, char **val);
/trunk/include/asterisk/dns_test.h
New File
 
/trunk/main/dns_core.c
Revision 434218 New Change
 
/trunk/main/dns_naptr.c
Revision 434218 New Change
 
/trunk/main/dns_srv.c
Revision 434218 New Change
 
/trunk/main/dns_test.c
New File
 
/trunk/tests/test_dns_naptr.c
Revision 434218 New Change
 
/trunk/tests/test_dns_srv.c
Revision 434218 New Change
 
  1. /trunk/include/asterisk/dns_internal.h: Loading...
  2. /trunk/include/asterisk/dns_test.h: Loading...
  3. /trunk/main/dns_core.c: Loading...
  4. /trunk/main/dns_naptr.c: Loading...
  5. /trunk/main/dns_srv.c: Loading...
  6. /trunk/main/dns_test.c: Loading...
  7. /trunk/tests/test_dns_naptr.c: Loading...
  8. /trunk/tests/test_dns_srv.c: Loading...

https://reviewboard.asterisk.org/ runs on a server provided by Digium, Inc. and uses bandwidth donated to the open source Asterisk community by API Digital Communications in Huntsville, AL USA.
Please report problems with this site to asteriskteam@digium.com.