Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Typedefs | Functions
dns_test.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_dns_test_string
 Representation of a string in DNS. More...
 

Typedefs

typedef int(* record_fn) (void *record, char *buf)
 Callback to write specific DNS record to an answer. More...
 

Functions

int ast_dns_test_generate_result (struct ast_dns_query *query, void *records, size_t num_records, size_t record_size, record_fn generate, char *buffer)
 Generate a full DNS response for the given DNS records. More...
 
int ast_dns_test_write_domain (const char *string, char *buf)
 Write a DNS domain to a buffer. More...
 
int ast_dns_test_write_string (const struct ast_dns_test_string *string, char *buf)
 Write a DNS string to a buffer. More...
 

Typedef Documentation

◆ record_fn

typedef int(* record_fn) (void *record, char *buf)

Callback to write specific DNS record to an answer.

When generating a DNS result, the type of DNS record being generated will need to be performed by individual test cases. This is a callback that tests can define to write a specific type of DNS record to the provided buffer.

There is no buffer size passed to this function. Tests are expected to use a buffer that is sufficiently large for their tests.

Parameters
recordPointer to test-specific DNS record data
bufThe buffer into which to write the DNS record
Returns
The number of bytes written to the buffer

Definition at line 85 of file dns_test.h.

Function Documentation

◆ ast_dns_test_generate_result()

int ast_dns_test_generate_result ( struct ast_dns_query query,
void *  records,
size_t  num_records,
size_t  record_size,
record_fn  generate,
char *  buffer 
)

Generate a full DNS response for the given DNS records.

This function takes care of generating the DNS header, question, and answer sections of a DNS response. In order to place test-specific record data into the DNS answers, a callback is provided as a parameter to this function so that the necessary records can be encoded properly by the tests.

There is no buffer size passed to this function. Tests are expected to use a buffer that is sufficiently large for their tests.

Parameters
queryThe DNS query that is being processed
recordsAn array of test-specific representations of DNS records
num_recordsThe number of elements in the records array
record_sizeThe size of each element in the records array
generateThe test-specific encoder for DNS records
bufferThe buffer into which to write the DNS response

Definition at line 222 of file dns_test.c.

References ast_dns_test_write_domain(), ast_dns_test_write_string(), buf, generate_dns_answer(), generate_dns_header(), generate_dns_question(), and records.

Referenced by naptr_thread(), and srv_thread().

224 {
225  char *ptr = buffer;
226  char *record_iter;
227 
228  ptr += generate_dns_header(num_records, ptr);
229  ptr += generate_dns_question(ptr);
230 
231  for (record_iter = records; record_iter < (char *) records + num_records * record_size; record_iter += record_size) {
232  unsigned short rdlength;
233  unsigned short net_rdlength;
234 
235  /* XXX Do we even want to override TTL? */
236  ptr += generate_dns_answer(0, ptr);
237  rdlength = generate(record_iter, ptr + 2);
238  net_rdlength = htons(rdlength);
239  memcpy(ptr, &net_rdlength, 2);
240  ptr += 2;
241  ptr += rdlength;
242  }
243 
244  return ptr - buffer;
245 }
static int records
Definition: cdr_mysql.c:84
static int generate_dns_question(char *buf)
Generate a DNS question and write it to a buffer.
Definition: dns_test.c:102
static int generate_dns_header(unsigned short num_records, char *buf)
Generate a DNS header and write it to a buffer.
Definition: dns_test.c:64
static int generate_dns_answer(int ttl, char *buf)
Generate a DNS answer and write it to a buffer.
Definition: dns_test.c:135

◆ ast_dns_test_write_domain()

int ast_dns_test_write_domain ( const char *  string,
char *  buf 
)

Write a DNS domain to a buffer.

A DNS domain consists of a series of labels separated by dots. Each of these labels gets written as a DNS string. A DNS domain ends with a NULL label, which is essentially a zero-length DNS string.

There is no buffer size passed to this function. Tests are expected to use a buffer that is sufficiently large for their tests.

Parameters
stringThe DNS domain to write
bufThe buffer to write the domain into
Returns
The number of bytes written to the buffer

A DNS domain consists of a series of labels separated by dots. Each of these labels gets written as a DNS string. A DNS domain ends with a NULL label, which is essentially a zero-length DNS string.

There is no buffer size passed to this function since we provide the data ourselves and have sized the buffer to be way larger than necessary for the tests.

Parameters
stringThe DNS domain to write
bufThe buffer to write the domain into
Returns
The number of bytes written to the buffer

Definition at line 196 of file dns_test.c.

References ast_dns_test_write_string(), ast_strdupa, ast_strlen_zero, buf, copy(), ast_dns_test_string::len, strsep(), and ast_dns_test_string::val.

Referenced by ast_dns_test_generate_result(), generate_naptr_record(), and generate_srv_record().

197 {
198  char *copy = ast_strdupa(string);
199  char *part;
200  char *ptr = buf;
201  static const struct ast_dns_test_string null_label = {
202  .len = 0,
203  .val = "",
204  };
205 
206  while (1) {
207  struct ast_dns_test_string dns_str;
208  part = strsep(&copy, ".");
209  if (ast_strlen_zero(part)) {
210  break;
211  }
212  dns_str.len = strlen(part);
213  dns_str.val = part;
214 
215  ptr += ast_dns_test_write_string(&dns_str, ptr);
216  }
217  ptr += ast_dns_test_write_string(&null_label, ptr);
218 
219  return ptr - buf;
220 }
Representation of a string in DNS.
Definition: dns_test.h:33
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int copy(char *infile, char *outfile)
Utility function to copy a file.
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
int ast_dns_test_write_string(const struct ast_dns_test_string *string, char *buf)
Write a DNS string to a buffer.
Definition: dns_test.c:162
char * strsep(char **str, const char *delims)

◆ ast_dns_test_write_string()

int ast_dns_test_write_string ( const struct ast_dns_test_string string,
char *  buf 
)

Write a DNS string to a buffer.

This writes the DNS string to the buffer and returns the total number of bytes written to the buffer.

There is no buffer size passed to this function. Tests are expected to use a buffer that is sufficiently large for their tests.

Parameters
stringThe string to write
bufThe buffer to write the string into
Returns
The number of bytes written to the buffer

This writes the DNS string to the buffer and returns the total number of bytes written to the buffer.

There is no buffer size passed to this function since we provide the data ourselves and have sized the buffer to be way larger than necessary for the tests.

Parameters
stringThe string to write
bufThe buffer to write the string into
Returns
The number of bytes written to the buffer

Definition at line 162 of file dns_test.c.

References len(), and ast_dns_test_string::val.

Referenced by ast_dns_test_generate_result(), ast_dns_test_write_domain(), and generate_naptr_record().

163 {
164  uint8_t len = string->len;
165  size_t actual_len = strlen(string->val);
166  buf[0] = len;
167  /*
168  * We use the actual length of the string instead of
169  * the stated value since sometimes we're going to lie about
170  * the length of the string
171  */
172  if (actual_len) {
173  memcpy(&buf[1], string->val, strlen(string->val));
174  }
175 
176  return actual_len + 1;
177 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
const char * val
Definition: dns_test.h:35