Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
dns_test.c File Reference
#include "asterisk.h"
#include "asterisk/dns_core.h"
#include "asterisk/dns_test.h"
#include "asterisk/utils.h"
Include dependency graph for dns_test.c:

Go to the source code of this file.

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...
 
static int generate_dns_answer (int ttl, char *buf)
 Generate a DNS answer and write it to a buffer. More...
 
static int generate_dns_header (unsigned short num_records, char *buf)
 Generate a DNS header and write it to a buffer. More...
 
static int generate_dns_question (char *buf)
 Generate a DNS question and write it to a buffer. More...
 

Variables

const char DNS_HEADER []
 
const char DNS_QUESTION []
 
const char NAPTR_ANSWER []
 

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 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 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

◆ generate_dns_answer()

static int generate_dns_answer ( int  ttl,
char *  buf 
)
static

Generate a DNS answer and write it to a buffer.

The DNS answer is the third (and in our case final) part of a DNS response. The DNS answer generated here is only partial. The record-specific data is generated by a separate function. DNS answers in our tests may have variable TTLs, but the rest is hard-coded.

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
bufThe buffer to write the answer into
Return values
Thenumber of bytes written to the buffer

Definition at line 135 of file dns_test.c.

References ARRAY_LEN, and NAPTR_ANSWER.

Referenced by ast_dns_test_generate_result().

136 {
137  int net_ttl = htonl(ttl);
138 
140  /* Overwrite TTL if one is provided */
141  if (ttl) {
142  memcpy(&buf[6], &net_ttl, sizeof(int));
143  }
144 
145  return ARRAY_LEN(NAPTR_ANSWER);
146 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
const char NAPTR_ANSWER[]
Definition: dns_test.c:108

◆ generate_dns_header()

static int generate_dns_header ( unsigned short  num_records,
char *  buf 
)
static

Generate a DNS header and write it to a buffer.

The DNS header is the first part of a DNS request or response. In our case, the only part of the header that a test can affect is the number of answers. The rest of the DNS header is based on hard-coded values.

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
num_recordsThe number of DNS records in this DNS response
bufThe buffer to write the header into
Return values
Thenumber of bytes written to the buffer

Definition at line 64 of file dns_test.c.

References ARRAY_LEN, and DNS_HEADER.

Referenced by ast_dns_test_generate_result().

65 {
66  unsigned short net_num_records = htons(num_records);
67 
69  /* Overwrite the ANCOUNT with the actual number of answers */
70  memcpy(&buf[6], &net_num_records, sizeof(num_records));
71 
72  return ARRAY_LEN(DNS_HEADER);
73 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
const char DNS_HEADER[]
Definition: dns_test.c:32

◆ generate_dns_question()

static int generate_dns_question ( char *  buf)
static

Generate a DNS question and write it to a buffer.

The DNS question is the second part of a DNS request or response. All DNS questions in this file are for the same domain and thus the DNS question is a hard-coded value.

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
bufThe buffer to write the question into
Return values
Thenumber of bytes written to the buffer

Definition at line 102 of file dns_test.c.

References ARRAY_LEN, and DNS_QUESTION.

Referenced by ast_dns_test_generate_result().

103 {
105  return ARRAY_LEN(DNS_QUESTION);
106 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
const char DNS_QUESTION[]
Definition: dns_test.c:75

Variable Documentation

◆ DNS_HEADER

const char DNS_HEADER[]

Definition at line 32 of file dns_test.c.

Referenced by generate_dns_header().

◆ DNS_QUESTION

const char DNS_QUESTION[]

Definition at line 75 of file dns_test.c.

Referenced by generate_dns_question().

◆ NAPTR_ANSWER

const char NAPTR_ANSWER[]

Definition at line 108 of file dns_test.c.

Referenced by generate_dns_answer().