iPXE
Data Structures | Macros | Functions | Variables
dns.c File Reference

DNS protocol. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/refcnt.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/resolv.h>
#include <ipxe/retry.h>
#include <ipxe/tcpip.h>
#include <ipxe/settings.h>
#include <ipxe/features.h>
#include <ipxe/job.h>
#include <ipxe/dhcp.h>
#include <ipxe/dhcpv6.h>
#include <ipxe/dns.h>

Go to the source code of this file.

Data Structures

struct  dns_request
 A DNS request. More...
 

Macros

#define ENXIO_NO_RECORD   __einfo_error ( EINFO_ENXIO_NO_RECORD )
 
#define EINFO_ENXIO_NO_RECORD   __einfo_uniqify ( EINFO_ENXIO, 0x01, "DNS name does not exist" )
 
#define ENXIO_NO_NAMESERVER   __einfo_error ( EINFO_ENXIO_NO_NAMESERVER )
 
#define EINFO_ENXIO_NO_NAMESERVER   __einfo_uniqify ( EINFO_ENXIO, 0x02, "No DNS servers available" )
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FEATURE (FEATURE_PROTOCOL, "DNS", DHCP_EB_FEATURE_DNS, 1)
 
int dns_encode (const char *string, struct dns_name *name)
 Encode a DNS name using RFC1035 encoding. More...
 
static int dns_label (struct dns_name *name, size_t offset)
 Find start of valid label within an RFC1035-encoded DNS name. More...
 
int dns_decode (struct dns_name *name, char *data, size_t len)
 Decode RFC1035-encoded DNS name. More...
 
int dns_compare (struct dns_name *first, struct dns_name *second)
 Compare DNS names for equality. More...
 
int dns_copy (struct dns_name *src, struct dns_name *dst)
 Copy a DNS name. More...
 
int dns_skip (struct dns_name *name)
 Skip RFC1035-encoded DNS name. More...
 
static int dns_skip_search (struct dns_name *name)
 Skip RFC1035-encoded DNS name in search list. More...
 
static const char * dns_name (struct dns_name *name)
 Transcribe DNS name (for debugging) More...
 
static const char * dns_type (uint16_t type)
 Name a DNS query type (for debugging) More...
 
static void dns_done (struct dns_request *dns, int rc)
 Mark DNS request as complete. More...
 
static void dns_resolved (struct dns_request *dns)
 Mark DNS request as resolved and complete. More...
 
static int dns_question (struct dns_request *dns)
 Construct DNS question. More...
 
static int dns_send_packet (struct dns_request *dns)
 Send DNS query. More...
 
static void dns_timer_expired (struct retry_timer *timer, int fail)
 Handle DNS retransmission timer expiry. More...
 
static int dns_xfer_deliver (struct dns_request *dns, struct io_buffer *iobuf, struct xfer_metadata *meta __unused)
 Receive new data. More...
 
static void dns_xfer_close (struct dns_request *dns, int rc)
 Receive new data. More...
 
static int dns_progress (struct dns_request *dns, struct job_progress *progress)
 Report job progress. More...
 
static int dns_resolv (struct interface *resolv, const char *name, struct sockaddr *sa)
 Resolve name using DNS. More...
 
struct resolver dns_resolver __resolver (RESOLV_NORMAL)
 DNS name resolver. More...
 
static int format_dnssl_setting (const struct setting_type *type __unused, const void *raw, size_t raw_len, char *buf, size_t len)
 Format DNS search list setting. More...
 
const struct setting dns_setting __setting (SETTING_IP4_EXTRA, dns)
 IPv4 DNS server setting. More...
 
const struct setting dns6_setting __setting (SETTING_IP6_EXTRA, dns6)
 IPv6 DNS server setting. More...
 
const struct setting dnssl_setting __setting (SETTING_IP_EXTRA, dnssl)
 DNS search list. More...
 
static void apply_dns_search (void)
 Apply DNS search list. More...
 
static int apply_dns_settings (void)
 Apply DNS settings. More...
 

Variables

union {
   struct sockaddr   sa
 
   struct sockaddr_tcpip   st
 
   struct sockaddr_in   sin
 
   struct sockaddr_in6   sin6
 
nameserver
 The DNS server. More...
 
static struct dns_name dns_search
 The DNS search list. More...
 
static struct interface_operation dns_socket_operations []
 DNS socket interface operations. More...
 
static struct interface_descriptor dns_socket_desc
 DNS socket interface descriptor. More...
 
static struct interface_operation dns_resolv_op []
 DNS resolver interface operations. More...
 
static struct interface_descriptor dns_resolv_desc
 DNS resolver interface descriptor. More...
 
const struct setting_type setting_type_dnssl __setting_type
 A DNS search list setting type. More...
 
struct settings_applicator dns_applicator __settings_applicator
 DNS settings applicator. More...
 

Detailed Description

DNS protocol.

Definition in file dns.c.

Macro Definition Documentation

◆ ENXIO_NO_RECORD

#define ENXIO_NO_RECORD   __einfo_error ( EINFO_ENXIO_NO_RECORD )

Definition at line 59 of file dns.c.

◆ EINFO_ENXIO_NO_RECORD

#define EINFO_ENXIO_NO_RECORD   __einfo_uniqify ( EINFO_ENXIO, 0x01, "DNS name does not exist" )

Definition at line 60 of file dns.c.

◆ ENXIO_NO_NAMESERVER

#define ENXIO_NO_NAMESERVER   __einfo_error ( EINFO_ENXIO_NO_NAMESERVER )

Definition at line 62 of file dns.c.

◆ EINFO_ENXIO_NO_NAMESERVER

#define EINFO_ENXIO_NO_NAMESERVER   __einfo_uniqify ( EINFO_ENXIO, 0x02, "No DNS servers available" )

Definition at line 63 of file dns.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FEATURE()

FEATURE ( FEATURE_PROTOCOL  ,
"DNS"  ,
DHCP_EB_FEATURE_DNS  ,
 
)

◆ dns_encode()

int dns_encode ( const char *  string,
struct dns_name name 
)

Encode a DNS name using RFC1035 encoding.

Parameters
stringDNS name as a string
nameDNS name to fill in
Return values
lenLength of DNS name, or negative error

Definition at line 88 of file dns.c.

88  {
89  uint8_t *start = ( name->data + name->offset );
90  uint8_t *end = ( name->data + name->len );
91  uint8_t *dst = start;
92  size_t len = 0;
93  char c;
94 
95  /* Encode name */
96  while ( ( c = *(string++) ) ) {
97 
98  /* Handle '.' separators */
99  if ( c == '.' ) {
100 
101  /* Reject consecutive '.' */
102  if ( ( len == 0 ) && ( dst != start ) )
103  return -EINVAL;
104 
105  /* Terminate if this is the trailing '.' */
106  if ( *string == '\0' )
107  break;
108 
109  /* Reject initial non-terminating '.' */
110  if ( len == 0 )
111  return -EINVAL;
112 
113  /* Reset length */
114  len = 0;
115 
116  } else {
117 
118  /* Increment length */
119  len++;
120 
121  /* Check for overflow */
122  if ( len > DNS_MAX_LABEL_LEN )
123  return -EINVAL;
124  }
125 
126  /* Copy byte, update length */
127  if ( ++dst < end ) {
128  *dst = c;
129  dst[-len] = len;
130  }
131  }
132 
133  /* Add terminating root marker */
134  if ( len )
135  dst++;
136  if ( dst < end )
137  *dst = '\0';
138  dst++;
139 
140  return ( dst - start );
141 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
const char * name
Definition: ath9k_hw.c:1984
#define DNS_MAX_LABEL_LEN
Maximum length of a single DNS label.
Definition: dns.h:53
static __always_inline void off_t int c
Definition: efi_uaccess.h:87
uint32_t start
Starting offset.
Definition: netvsc.h:12
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Length.
Definition: ena.h:14
uint32_t end
Ending offset.
Definition: netvsc.h:18

References c, DNS_MAX_LABEL_LEN, EINVAL, end, len, name, and start.

Referenced by apply_dns_search(), dns_encode_fail_okx(), dns_encode_okx(), and dns_resolv().

◆ dns_label()

static int dns_label ( struct dns_name name,
size_t  offset 
)
static

Find start of valid label within an RFC1035-encoded DNS name.

Parameters
nameDNS name
offsetCurrent offset
Return values
offsetOffset of label, or negative error

Definition at line 150 of file dns.c.

150  {
151  const uint8_t *byte;
152  const uint16_t *word;
153  size_t len;
154  size_t ptr;
155 
156  while ( 1 ) {
157 
158  /* Fail if we have overrun the DNS name */
159  if ( ( offset + sizeof ( *byte) ) > name->len )
160  return -EINVAL;
161  byte = ( name->data + offset );
162 
163  /* Follow compression pointer, if applicable */
164  if ( DNS_IS_COMPRESSED ( *byte ) ) {
165 
166  /* Fail if we have overrun the DNS name */
167  if ( ( offset + sizeof ( *word ) ) > name->len )
168  return -EINVAL;
169  word = ( name->data + offset );
170 
171  /* Extract pointer to new offset */
172  ptr = DNS_COMPRESSED_OFFSET ( ntohs ( *word ) );
173 
174  /* Fail if pointer does not point backwards.
175  * (This guarantees termination of the
176  * function.)
177  */
178  if ( ptr >= offset )
179  return -EINVAL;
180 
181  /* Continue from new offset */
182  offset = ptr;
183  continue;
184  }
185 
186  /* Fail if we have overrun the DNS name */
187  len = *byte;
188  if ( ( offset + sizeof ( *byte ) + len ) > name->len )
189  return -EINVAL;
190 
191  /* We have a valid label */
192  return offset;
193  }
194 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
const char * name
Definition: ath9k_hw.c:1984
unsigned short uint16_t
Definition: stdint.h:11
#define DNS_COMPRESSED_OFFSET(word)
Extract DNS compression pointer.
Definition: dns.h:42
#define ntohs(value)
Definition: byteswap.h:136
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
unsigned char uint8_t
Definition: stdint.h:10
unsigned char byte
Definition: smc9000.h:38
#define DNS_IS_COMPRESSED(byte)
Test for a DNS compression pointer.
Definition: dns.h:34
uint32_t len
Length.
Definition: ena.h:14
unsigned short word
Definition: smc9000.h:39

References DNS_COMPRESSED_OFFSET, DNS_IS_COMPRESSED, EINVAL, len, name, ntohs, and offset.

Referenced by dns_compare(), dns_copy(), dns_decode(), and dns_skip().

◆ dns_decode()

int dns_decode ( struct dns_name name,
char *  data,
size_t  len 
)

Decode RFC1035-encoded DNS name.

Parameters
nameDNS name
dataOutput buffer
lenLength of output buffer
Return values
lenLength of decoded DNS name, or negative error

Definition at line 204 of file dns.c.

204  {
205  unsigned int recursion_limit = name->len; /* Generous upper bound */
206  int offset = name->offset;
207  const uint8_t *label;
208  size_t decoded_len = 0;
209  size_t label_len;
210  size_t copy_len;
211 
212  while ( recursion_limit-- ) {
213 
214  /* Find valid DNS label */
215  offset = dns_label ( name, offset );
216  if ( offset < 0 )
217  return offset;
218 
219  /* Terminate if we have reached the root */
220  label = ( name->data + offset );
221  label_len = *(label++);
222  if ( label_len == 0 ) {
223  if ( decoded_len < len )
224  *data = '\0';
225  return decoded_len;
226  }
227 
228  /* Prepend '.' if applicable */
229  if ( decoded_len && ( decoded_len++ < len ) )
230  *(data++) = '.';
231 
232  /* Copy label to output buffer */
233  copy_len = ( ( decoded_len < len ) ? ( len - decoded_len ) : 0);
234  if ( copy_len > label_len )
235  copy_len = label_len;
236  memcpy ( data, label, copy_len );
237  data += copy_len;
238  decoded_len += label_len;
239 
240  /* Move to next label */
241  offset += ( sizeof ( *label ) + label_len );
242  }
243 
244  /* Recursion limit exceeded */
245  return -EINVAL;
246 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
const char * name
Definition: ath9k_hw.c:1984
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Length.
Definition: ena.h:14
static int dns_label(struct dns_name *name, size_t offset)
Find start of valid label within an RFC1035-encoded DNS name.
Definition: dns.c:150
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

References data, dns_label(), EINVAL, len, memcpy(), name, and offset.

Referenced by dns_decode_fail_okx(), dns_decode_okx(), dns_list_okx(), dns_name(), dns_progress(), and format_dnssl_setting().

◆ dns_compare()

int dns_compare ( struct dns_name first,
struct dns_name second 
)

Compare DNS names for equality.

Parameters
firstFirst DNS name
secondSecond DNS name
Return values
rcReturn status code

Definition at line 255 of file dns.c.

255  {
256  unsigned int recursion_limit = first->len; /* Generous upper bound */
257  int first_offset = first->offset;
258  int second_offset = second->offset;
259  const uint8_t *first_label;
260  const uint8_t *second_label;
261  size_t label_len;
262  size_t len;
263 
264  while ( recursion_limit-- ) {
265 
266  /* Find valid DNS labels */
267  first_offset = dns_label ( first, first_offset );
268  if ( first_offset < 0 )
269  return first_offset;
270  second_offset = dns_label ( second, second_offset );
271  if ( second_offset < 0 )
272  return second_offset;
273 
274  /* Compare label lengths */
275  first_label = ( first->data + first_offset );
276  second_label = ( second->data + second_offset );
277  label_len = *(first_label++);
278  if ( label_len != *(second_label++) )
279  return -ENOENT;
280  len = ( sizeof ( *first_label ) + label_len );
281 
282  /* Terminate if we have reached the root */
283  if ( label_len == 0 )
284  return 0;
285 
286  /* Compare label contents (case-insensitively) */
287  while ( label_len-- ) {
288  if ( tolower ( *(first_label++) ) !=
289  tolower ( *(second_label++) ) )
290  return -ENOENT;
291  }
292 
293  /* Move to next labels */
294  first_offset += len;
295  second_offset += len;
296  }
297 
298  /* Recursion limit exceeded */
299  return -EINVAL;
300 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
static __always_inline int off_t userptr_t second
Definition: efi_uaccess.h:80
#define ENOENT
No such file or directory.
Definition: errno.h:514
static int tolower(int character)
Convert character to lower case.
Definition: ctype.h:97
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Length.
Definition: ena.h:14
static int dns_label(struct dns_name *name, size_t offset)
Find start of valid label within an RFC1035-encoded DNS name.
Definition: dns.c:150
uint32_t first
Length to skip in first segment.
Definition: pccrc.h:23

References dns_label(), EINVAL, ENOENT, first, len, second, and tolower().

Referenced by dns_compare_fail_okx(), dns_compare_okx(), and dns_xfer_deliver().

◆ dns_copy()

int dns_copy ( struct dns_name src,
struct dns_name dst 
)

Copy a DNS name.

Parameters
srcSource DNS name
dstDestination DNS name
Return values
lenLength of copied DNS name, or negative error

Definition at line 309 of file dns.c.

309  {
310  unsigned int recursion_limit = src->len; /* Generous upper bound */
311  int src_offset = src->offset;
312  size_t dst_offset = dst->offset;
313  const uint8_t *label;
314  size_t label_len;
315  size_t copy_len;
316  size_t len;
317 
318  while ( recursion_limit-- ) {
319 
320  /* Find valid DNS label */
321  src_offset = dns_label ( src, src_offset );
322  if ( src_offset < 0 )
323  return src_offset;
324 
325  /* Copy as an uncompressed label */
326  label = ( src->data + src_offset );
327  label_len = *label;
328  len = ( sizeof ( *label ) + label_len );
329  copy_len = ( ( dst_offset < dst->len ) ?
330  ( dst->len - dst_offset ) : 0 );
331  if ( copy_len > len )
332  copy_len = len;
333  memcpy ( ( dst->data + dst_offset ), label, copy_len );
334  src_offset += len;
335  dst_offset += len;
336 
337  /* Terminate if we have reached the root */
338  if ( label_len == 0 )
339  return ( dst_offset - dst->offset );
340  }
341 
342  /* Recursion limit exceeded */
343  return -EINVAL;
344 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
void * data
Start of data.
Definition: dns.h:21
void * memcpy(void *dest, const void *src, size_t len) __nonnull
size_t offset
Offset of name within data.
Definition: dns.h:23
static __always_inline void off_t userptr_t src
Definition: efi_uaccess.h:66
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Length.
Definition: ena.h:14
static int dns_label(struct dns_name *name, size_t offset)
Find start of valid label within an RFC1035-encoded DNS name.
Definition: dns.c:150
size_t len
Total length of data.
Definition: dns.h:25

References dns_name::data, dns_label(), EINVAL, len, dns_name::len, memcpy(), dns_name::offset, and src.

Referenced by dns_copy_fail_okx(), dns_copy_okx(), dns_question(), and dns_xfer_deliver().

◆ dns_skip()

int dns_skip ( struct dns_name name)

Skip RFC1035-encoded DNS name.

Parameters
nameDNS name
Return values
offsetOffset to next name, or negative error

Definition at line 352 of file dns.c.

352  {
353  unsigned int recursion_limit = name->len; /* Generous upper bound */
354  int offset = name->offset;
355  int prev_offset;
356  const uint8_t *label;
357  size_t label_len;
358 
359  while ( recursion_limit-- ) {
360 
361  /* Find valid DNS label */
362  prev_offset = offset;
363  offset = dns_label ( name, prev_offset );
364  if ( offset < 0 )
365  return offset;
366 
367  /* Terminate if we have reached a compression pointer */
368  if ( offset != prev_offset )
369  return ( prev_offset + sizeof ( uint16_t ) );
370 
371  /* Skip this label */
372  label = ( name->data + offset );
373  label_len = *label;
374  offset += ( sizeof ( *label ) + label_len );
375 
376  /* Terminate if we have reached the root */
377  if ( label_len == 0 )
378  return offset;
379  }
380 
381  /* Recursion limit exceeded */
382  return -EINVAL;
383 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
const char * name
Definition: ath9k_hw.c:1984
unsigned short uint16_t
Definition: stdint.h:11
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
unsigned char uint8_t
Definition: stdint.h:10
static int dns_label(struct dns_name *name, size_t offset)
Find start of valid label within an RFC1035-encoded DNS name.
Definition: dns.c:150

References dns_label(), EINVAL, name, and offset.

Referenced by dns_list_okx(), dns_skip_search(), and dns_xfer_deliver().

◆ dns_skip_search()

static int dns_skip_search ( struct dns_name name)
static

Skip RFC1035-encoded DNS name in search list.

Parameters
nameDNS name
Return values
offsetOffset to next non-empty name, or negative error

Definition at line 391 of file dns.c.

391  {
392  int offset;
393 
394  /* Find next name */
395  offset = dns_skip ( name );
396  if ( offset < 0 )
397  return offset;
398 
399  /* Skip over any subsequent empty names (e.g. due to padding
400  * bytes used in the NDP DNSSL option).
401  */
402  while ( ( offset < ( ( int ) name->len ) ) &&
403  ( *( ( uint8_t * ) ( name->data + offset ) ) == 0 ) ) {
404  offset++;
405  }
406 
407  return offset;
408 }
const char * name
Definition: ath9k_hw.c:1984
int dns_skip(struct dns_name *name)
Skip RFC1035-encoded DNS name.
Definition: dns.c:352
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
unsigned char uint8_t
Definition: stdint.h:10

References dns_skip(), name, and offset.

Referenced by apply_dns_settings(), dns_xfer_deliver(), and format_dnssl_setting().

◆ dns_name()

static const char* dns_name ( struct dns_name name)
static

Transcribe DNS name (for debugging)

Parameters
nameDNS name
Return values
stringTranscribed DNS name

Definition at line 416 of file dns.c.

416  {
417  static char buf[256];
418  int len;
419 
420  len = dns_decode ( name, buf, ( sizeof ( buf ) - 1 /* NUL */ ) );
421  return ( ( len < 0 ) ? "<INVALID>" : buf );
422 }
const char * name
Definition: ath9k_hw.c:1984
uint32_t len
Length.
Definition: ena.h:14
int dns_decode(struct dns_name *name, char *data, size_t len)
Decode RFC1035-encoded DNS name.
Definition: dns.c:204

References dns_decode(), len, and name.

◆ dns_type()

static const char* dns_type ( uint16_t  type)
static

Name a DNS query type (for debugging)

Parameters
typeQuery type (in network byte order)
Return values
nameType name

Definition at line 430 of file dns.c.

430  {
431  switch ( type ) {
432  case htons ( DNS_TYPE_A ): return "A";
433  case htons ( DNS_TYPE_AAAA ): return "AAAA";
434  case htons ( DNS_TYPE_CNAME ): return "CNAME";
435  default: return "<UNKNOWN>";
436  }
437 }
#define DNS_TYPE_CNAME
Type of a DNS "NAME" record.
Definition: dns.h:129
uint8_t type
Type.
Definition: ena.h:16
#define DNS_TYPE_A
Type of a DNS "A" record.
Definition: dns.h:107
#define DNS_TYPE_AAAA
Type of a DNS "AAAA" record.
Definition: dns.h:118
return
Definition: natsemi.h:326
#define htons(value)
Definition: byteswap.h:135

References DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_CNAME, htons, and type.

Referenced by dns_question(), dns_send_packet(), and dns_xfer_deliver().

◆ dns_done()

static void dns_done ( struct dns_request dns,
int  rc 
)
static

Mark DNS request as complete.

Parameters
dnsDNS request
rcReturn status code

Definition at line 487 of file dns.c.

487  {
488 
489  /* Stop the retry timer */
490  stop_timer ( &dns->timer );
491 
492  /* Shut down interfaces */
493  intf_shutdown ( &dns->socket, rc );
494  intf_shutdown ( &dns->resolv, rc );
495 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:273
struct interface resolv
Name resolution interface.
Definition: dns.c:444
void stop_timer(struct retry_timer *timer)
Stop timer.
Definition: retry.c:117
struct interface socket
Data transfer interface.
Definition: dns.c:446
struct retry_timer timer
Retry timer.
Definition: dns.c:448

References intf_shutdown(), rc, dns_request::resolv, dns_request::socket, stop_timer(), and dns_request::timer.

Referenced by dns_resolved(), dns_timer_expired(), dns_xfer_close(), and dns_xfer_deliver().

◆ dns_resolved()

static void dns_resolved ( struct dns_request dns)
static

Mark DNS request as resolved and complete.

Parameters
dnsDNS request
rcReturn status code

Definition at line 503 of file dns.c.

503  {
504 
505  DBGC ( dns, "DNS %p found address %s\n",
506  dns, sock_ntoa ( &dns->address.sa ) );
507 
508  /* Return resolved address */
509  resolv_done ( &dns->resolv, &dns->address.sa );
510 
511  /* Mark operation as complete */
512  dns_done ( dns, 0 );
513 }
static void dns_done(struct dns_request *dns, int rc)
Mark DNS request as complete.
Definition: dns.c:487
void resolv_done(struct interface *intf, struct sockaddr *sa)
Name resolved.
Definition: resolv.c:55
#define DBGC(...)
Definition: compiler.h:505
struct sockaddr sa
Definition: dns.c:452
struct interface resolv
Name resolution interface.
Definition: dns.c:444
const char * sock_ntoa(struct sockaddr *sa)
Transcribe socket address.
Definition: socket.c:42
union dns_request::@3 address
Socket address to fill in with resolved address.

References dns_request::address, DBGC, dns_done(), dns_request::resolv, resolv_done(), dns_request::sa, and sock_ntoa().

Referenced by dns_xfer_deliver().

◆ dns_question()

static int dns_question ( struct dns_request dns)
static

Construct DNS question.

Parameters
dnsDNS request
Return values
rcReturn status code

Definition at line 521 of file dns.c.

521  {
522  static struct dns_name search_root = {
523  .data = "",
524  .len = 1,
525  };
526  struct dns_name *search = &dns->search;
527  int len;
528  size_t offset;
529 
530  /* Use root suffix if search list is empty */
531  if ( search->offset == search->len )
532  search = &search_root;
533 
534  /* Overwrite current suffix */
535  dns->name.offset = dns->offset;
536  len = dns_copy ( search, &dns->name );
537  if ( len < 0 )
538  return len;
539 
540  /* Sanity check */
541  offset = ( dns->name.offset + len );
542  if ( offset > dns->name.len ) {
543  DBGC ( dns, "DNS %p name is too long\n", dns );
544  return -EINVAL;
545  }
546 
547  /* Construct question */
548  dns->question = ( ( ( void * ) &dns->buf ) + offset );
549  dns->question->qtype = dns->qtype;
550  dns->question->qclass = htons ( DNS_CLASS_IN );
551 
552  /* Store length */
553  dns->len = ( offset + sizeof ( *(dns->question) ) );
554 
555  /* Restore name */
556  dns->name.offset = offsetof ( typeof ( dns->buf ), name );
557 
558  DBGC2 ( dns, "DNS %p question is %s type %s\n", dns,
559  dns_name ( &dns->name ), dns_type ( dns->question->qtype ) );
560 
561  return 0;
562 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
size_t offset
Offset of search suffix within current query.
Definition: dns.c:474
const char * name
Definition: ath9k_hw.c:1984
struct dns_question * question
Question within current query.
Definition: dns.c:470
#define DBGC(...)
Definition: compiler.h:505
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
void * data
Start of data.
Definition: dns.h:21
static const char * dns_type(uint16_t type)
Name a DNS query type (for debugging)
Definition: dns.c:430
uint16_t qtype
Query type.
Definition: dns.h:86
An RFC1035-encoded DNS name.
Definition: dns.h:19
size_t len
Length of current query.
Definition: dns.c:472
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
size_t offset
Offset of name within data.
Definition: dns.h:23
int dns_copy(struct dns_name *src, struct dns_name *dst)
Copy a DNS name.
Definition: dns.c:309
char name[DNS_MAX_NAME_LEN]
Name buffer.
Definition: dns.c:463
uint16_t qclass
Query class.
Definition: dns.h:88
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
struct dns_name search
Search list.
Definition: dns.c:476
#define DNS_CLASS_IN
DNS class "IN".
Definition: dns.h:92
uint16_t qtype
Initial query type.
Definition: dns.c:457
struct dns_request::@4 buf
Buffer for current query.
size_t len
Total length of data.
Definition: dns.h:25
#define htons(value)
Definition: byteswap.h:135

References dns_request::buf, dns_name::data, DBGC, DBGC2, DNS_CLASS_IN, dns_copy(), dns_type(), EINVAL, htons, len, dns_name::len, dns_request::len, dns_request::name, name, dns_name::offset, offset, dns_request::offset, offsetof, dns_question::qclass, dns_question::qtype, dns_request::qtype, dns_request::question, and dns_request::search.

◆ dns_send_packet()

static int dns_send_packet ( struct dns_request dns)
static

Send DNS query.

Parameters
dnsDNS request
Return values
rcReturn status code

Definition at line 570 of file dns.c.

570  {
571  struct dns_header *query = &dns->buf.query;
572 
573  /* Start retransmission timer */
574  start_timer ( &dns->timer );
575 
576  /* Generate query identifier */
577  query->id = random();
578 
579  /* Send query */
580  DBGC ( dns, "DNS %p sending query ID %#04x for %s type %s\n", dns,
581  ntohs ( query->id ), dns_name ( &dns->name ),
582  dns_type ( dns->question->qtype ) );
583 
584  /* Send the data */
585  return xfer_deliver_raw ( &dns->socket, query, dns->len );
586 }
struct dns_question * question
Question within current query.
Definition: dns.c:470
A DNS packet header.
Definition: dns.h:65
#define DBGC(...)
Definition: compiler.h:505
#define ntohs(value)
Definition: byteswap.h:136
int xfer_deliver_raw(struct interface *intf, const void *data, size_t len)
Deliver datagram as raw data without metadata.
Definition: xfer.c:287
static const char * dns_type(uint16_t type)
Name a DNS query type (for debugging)
Definition: dns.c:430
uint16_t qtype
Query type.
Definition: dns.h:86
An RFC1035-encoded DNS name.
Definition: dns.h:19
size_t len
Length of current query.
Definition: dns.c:472
struct dns_header query
Query header.
Definition: dns.c:461
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition: random.c:30
void start_timer(struct retry_timer *timer)
Start timer.
Definition: retry.c:93
char name[DNS_MAX_NAME_LEN]
Name buffer.
Definition: dns.c:463
struct interface socket
Data transfer interface.
Definition: dns.c:446
uint16_t id
Query identifier.
Definition: dns.h:67
struct retry_timer timer
Retry timer.
Definition: dns.c:448
struct dns_request::@4 buf
Buffer for current query.

References dns_request::buf, DBGC, dns_type(), dns_header::id, dns_request::len, dns_request::name, ntohs, dns_question::qtype, dns_request::query, dns_request::question, random(), dns_request::socket, start_timer(), dns_request::timer, and xfer_deliver_raw().

Referenced by dns_timer_expired(), and dns_xfer_deliver().

◆ dns_timer_expired()

static void dns_timer_expired ( struct retry_timer timer,
int  fail 
)
static

Handle DNS retransmission timer expiry.

Parameters
timerRetry timer
failFailure indicator

Definition at line 594 of file dns.c.

594  {
595  struct dns_request *dns =
596  container_of ( timer, struct dns_request, timer );
597 
598  if ( fail ) {
599  dns_done ( dns, -ETIMEDOUT );
600  } else {
601  dns_send_packet ( dns );
602  }
603 }
static void dns_done(struct dns_request *dns, int rc)
Mark DNS request as complete.
Definition: dns.c:487
A timer.
Definition: timer.h:28
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static int dns_send_packet(struct dns_request *dns)
Send DNS query.
Definition: dns.c:570
A DNS request.
Definition: dns.c:440
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669

References container_of, dns_done(), dns_send_packet(), and ETIMEDOUT.

Referenced by dns_resolv().

◆ dns_xfer_deliver()

static int dns_xfer_deliver ( struct dns_request dns,
struct io_buffer iobuf,
struct xfer_metadata *meta  __unused 
)
static

Receive new data.

Parameters
dnsDNS request
iobufI/O buffer
metaData transfer metadata
Return values
rcReturn status code

Definition at line 613 of file dns.c.

615  {
616  struct dns_header *response = iobuf->data;
617  struct dns_header *query = &dns->buf.query;
618  unsigned int qtype = dns->question->qtype;
619  struct dns_name buf;
620  union dns_rr *rr;
621  int offset;
622  size_t answer_offset;
623  size_t next_offset;
624  size_t rdlength;
625  size_t name_len;
626  int rc;
627 
628  /* Sanity check */
629  if ( iob_len ( iobuf ) < sizeof ( *response ) ) {
630  DBGC ( dns, "DNS %p received underlength packet length %zd\n",
631  dns, iob_len ( iobuf ) );
632  rc = -EINVAL;
633  goto done;
634  }
635 
636  /* Check response ID matches query ID */
637  if ( response->id != query->id ) {
638  DBGC ( dns, "DNS %p received unexpected response ID %#04x "
639  "(wanted %d)\n", dns, ntohs ( response->id ),
640  ntohs ( query->id ) );
641  rc = -EINVAL;
642  goto done;
643  }
644  DBGC ( dns, "DNS %p received response ID %#04x\n",
645  dns, ntohs ( response->id ) );
646 
647  /* Check that we have exactly one question */
648  if ( response->qdcount != htons ( 1 ) ) {
649  DBGC ( dns, "DNS %p received response with %d questions\n",
650  dns, ntohs ( response->qdcount ) );
651  rc = -EINVAL;
652  goto done;
653  }
654 
655  /* Skip question section */
656  buf.data = iobuf->data;
657  buf.offset = sizeof ( *response );
658  buf.len = iob_len ( iobuf );
659  offset = dns_skip ( &buf );
660  if ( offset < 0 ) {
661  rc = offset;
662  DBGC ( dns, "DNS %p received response with malformed "
663  "question: %s\n", dns, strerror ( rc ) );
664  goto done;
665  }
666  answer_offset = ( offset + sizeof ( struct dns_question ) );
667 
668  /* Search through response for useful answers. Do this
669  * multiple times, to take advantage of useful nameservers
670  * which send us e.g. the CNAME *and* the A record for the
671  * pointed-to name.
672  */
673  for ( buf.offset = answer_offset ; buf.offset != buf.len ;
674  buf.offset = next_offset ) {
675 
676  /* Check for valid name */
677  offset = dns_skip ( &buf );
678  if ( offset < 0 ) {
679  rc = offset;
680  DBGC ( dns, "DNS %p received response with malformed "
681  "answer: %s\n", dns, strerror ( rc ) );
682  goto done;
683  }
684 
685  /* Check for sufficient space for resource record */
686  rr = ( buf.data + offset );
687  if ( ( offset + sizeof ( rr->common ) ) > buf.len ) {
688  DBGC ( dns, "DNS %p received response with underlength "
689  "RR\n", dns );
690  rc = -EINVAL;
691  goto done;
692  }
693  rdlength = ntohs ( rr->common.rdlength );
694  next_offset = ( offset + sizeof ( rr->common ) + rdlength );
695  if ( next_offset > buf.len ) {
696  DBGC ( dns, "DNS %p received response with underlength "
697  "RR\n", dns );
698  rc = -EINVAL;
699  goto done;
700  }
701 
702  /* Skip non-matching names */
703  if ( dns_compare ( &buf, &dns->name ) != 0 ) {
704  DBGC2 ( dns, "DNS %p ignoring response for %s type "
705  "%s\n", dns, dns_name ( &buf ),
706  dns_type ( rr->common.type ) );
707  continue;
708  }
709 
710  /* Handle answer */
711  switch ( rr->common.type ) {
712 
713  case htons ( DNS_TYPE_AAAA ):
714 
715  /* Found the target AAAA record */
716  if ( rdlength < sizeof ( dns->address.sin6.sin6_addr )){
717  DBGC ( dns, "DNS %p received response with "
718  "underlength AAAA\n", dns );
719  rc = -EINVAL;
720  goto done;
721  }
723  memcpy ( &dns->address.sin6.sin6_addr,
724  &rr->aaaa.in6_addr,
725  sizeof ( dns->address.sin6.sin6_addr ) );
726  dns_resolved ( dns );
727  rc = 0;
728  goto done;
729 
730  case htons ( DNS_TYPE_A ):
731 
732  /* Found the target A record */
733  if ( rdlength < sizeof ( dns->address.sin.sin_addr ) ) {
734  DBGC ( dns, "DNS %p received response with "
735  "underlength A\n", dns );
736  rc = -EINVAL;
737  goto done;
738  }
739  dns->address.sin.sin_family = AF_INET;
740  dns->address.sin.sin_addr = rr->a.in_addr;
741  dns_resolved ( dns );
742  rc = 0;
743  goto done;
744 
745  case htons ( DNS_TYPE_CNAME ):
746 
747  /* Terminate the operation if we recurse too far */
748  if ( ++dns->recursion > DNS_MAX_CNAME_RECURSION ) {
749  DBGC ( dns, "DNS %p recursion exceeded\n",
750  dns );
751  rc = -ELOOP;
752  dns_done ( dns, rc );
753  goto done;
754  }
755 
756  /* Found a CNAME record; update query and recurse */
757  buf.offset = ( offset + sizeof ( rr->cname ) );
758  DBGC ( dns, "DNS %p found CNAME %s\n",
759  dns, dns_name ( &buf ) );
760  dns->search.offset = dns->search.len;
761  name_len = dns_copy ( &buf, &dns->name );
762  dns->offset = ( offsetof ( typeof ( dns->buf ), name ) +
763  name_len - 1 /* Strip root label */ );
764  if ( ( rc = dns_question ( dns ) ) != 0 ) {
765  dns_done ( dns, rc );
766  goto done;
767  }
768  next_offset = answer_offset;
769  break;
770 
771  default:
772  DBGC ( dns, "DNS %p got unknown record type %d\n",
773  dns, ntohs ( rr->common.type ) );
774  break;
775  }
776  }
777 
778  /* Stop the retry timer. After this point, each code path
779  * must either restart the timer by calling dns_send_packet(),
780  * or mark the DNS operation as complete by calling
781  * dns_done()
782  */
783  stop_timer ( &dns->timer );
784 
785  /* Determine what to do next based on the type of query we
786  * issued and the response we received
787  */
788  switch ( qtype ) {
789 
790  case htons ( DNS_TYPE_AAAA ):
791  /* We asked for an AAAA record and got nothing; try
792  * the A.
793  */
794  DBGC ( dns, "DNS %p found no AAAA record; trying A\n", dns );
795  dns->question->qtype = htons ( DNS_TYPE_A );
796  dns_send_packet ( dns );
797  rc = 0;
798  goto done;
799 
800  case htons ( DNS_TYPE_A ):
801  /* We asked for an A record and got nothing;
802  * try the CNAME.
803  */
804  DBGC ( dns, "DNS %p found no A record; trying CNAME\n", dns );
805  dns->question->qtype = htons ( DNS_TYPE_CNAME );
806  dns_send_packet ( dns );
807  rc = 0;
808  goto done;
809 
810  case htons ( DNS_TYPE_CNAME ):
811  /* We asked for a CNAME record. If we got a response
812  * (i.e. if the next AAAA/A query is already set up),
813  * then issue it.
814  */
815  if ( qtype == dns->qtype ) {
816  dns_send_packet ( dns );
817  rc = 0;
818  goto done;
819  }
820 
821  /* If we have already reached the end of the search list,
822  * then terminate lookup.
823  */
824  if ( dns->search.offset == dns->search.len ) {
825  DBGC ( dns, "DNS %p found no CNAME record\n", dns );
826  rc = -ENXIO_NO_RECORD;
827  dns_done ( dns, rc );
828  goto done;
829  }
830 
831  /* Move to next entry in search list. This can never fail,
832  * since we have already used this entry.
833  */
834  DBGC ( dns, "DNS %p found no CNAME record; trying next "
835  "suffix\n", dns );
836  dns->search.offset = dns_skip_search ( &dns->search );
837  if ( ( rc = dns_question ( dns ) ) != 0 ) {
838  dns_done ( dns, rc );
839  goto done;
840  }
841  dns_send_packet ( dns );
842  goto done;
843 
844  default:
845  assert ( 0 );
846  rc = -EINVAL;
847  dns_done ( dns, rc );
848  goto done;
849  }
850 
851  done:
852  /* Free I/O buffer */
853  free_iob ( iobuf );
854  return rc;
855 }
struct dns_rr_common common
Common fields.
Definition: dns.h:140
#define EINVAL
Invalid argument.
Definition: errno.h:428
#define DNS_TYPE_CNAME
Type of a DNS "NAME" record.
Definition: dns.h:129
size_t offset
Offset of search suffix within current query.
Definition: dns.c:474
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
uint16_t qdcount
Number of question records.
Definition: dns.h:71
#define AF_INET6
IPv6 Internet addresses.
Definition: socket.h:64
struct dns_question * question
Question within current query.
Definition: dns.c:470
int dns_skip(struct dns_name *name)
Skip RFC1035-encoded DNS name.
Definition: dns.c:352
static void dns_done(struct dns_request *dns, int rc)
Mark DNS request as complete.
Definition: dns.c:487
A DNS packet header.
Definition: dns.h:65
uint16_t type
Type.
Definition: dns.h:97
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
uint64_t address
Base address.
Definition: ena.h:24
#define DBGC(...)
Definition: compiler.h:505
struct in6_addr in6_addr
IPv6 address.
Definition: dns.h:125
struct in_addr in_addr
IPv4 address.
Definition: dns.h:114
#define ntohs(value)
Definition: byteswap.h:136
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
sa_family_t sin_family
Socket address family (part of struct sockaddr)
Definition: in.h:87
#define ELOOP
Too many levels of symbolic links.
Definition: errno.h:448
static const char * dns_type(uint16_t type)
Name a DNS query type (for debugging)
Definition: dns.c:430
#define DNS_TYPE_A
Type of a DNS "A" record.
Definition: dns.h:107
void * memcpy(void *dest, const void *src, size_t len) __nonnull
A DNS resource record.
Definition: dns.h:138
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint16_t qtype
Query type.
Definition: dns.h:86
An RFC1035-encoded DNS name.
Definition: dns.h:19
struct sockaddr_in6 sin6
Definition: dns.c:454
struct sockaddr_in sin
Definition: dns.c:70
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static int dns_send_packet(struct dns_request *dns)
Send DNS query.
Definition: dns.c:570
size_t offset
Offset of name within data.
Definition: dns.h:23
#define DNS_TYPE_AAAA
Type of a DNS "AAAA" record.
Definition: dns.h:118
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
struct dns_header query
Query header.
Definition: dns.c:461
int dns_copy(struct dns_name *src, struct dns_name *dst)
Copy a DNS name.
Definition: dns.c:309
#define DNS_MAX_CNAME_RECURSION
Maximum depth of CNAME recursion.
Definition: dns.h:62
struct dns_rr_a a
"A" record
Definition: dns.h:142
#define ENXIO_NO_RECORD
Definition: dns.c:59
char name[DNS_MAX_NAME_LEN]
Name buffer.
Definition: dns.c:463
struct in_addr sin_addr
IPv4 address.
Definition: in.h:98
void stop_timer(struct retry_timer *timer)
Stop timer.
Definition: retry.c:117
A DNS question.
Definition: dns.h:84
struct dns_rr_aaaa aaaa
"AAAA" record
Definition: dns.h:144
uint16_t id
Query identifier.
Definition: dns.h:67
#define DBGC2(...)
Definition: compiler.h:522
struct sockaddr_in6 sin6
Definition: dns.c:71
static int dns_skip_search(struct dns_name *name)
Skip RFC1035-encoded DNS name in search list.
Definition: dns.c:391
void * data
Start of data.
Definition: iobuf.h:44
struct dns_name search
Search list.
Definition: dns.c:476
struct retry_timer timer
Retry timer.
Definition: dns.c:448
int dns_compare(struct dns_name *first, struct dns_name *second)
Compare DNS names for equality.
Definition: dns.c:255
struct dns_request::@4 buf
Buffer for current query.
sa_family_t sin6_family
Socket address family (part of struct sockaddr)
Definition: in.h:120
size_t len
Total length of data.
Definition: dns.h:25
struct sockaddr_in sin
Definition: dns.c:453
uint16_t rdlength
Resource data length.
Definition: dns.h:103
#define htons(value)
Definition: byteswap.h:135
struct bofm_section_header done
Definition: bofm_test.c:46
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:63
struct in6_addr sin6_addr
IPv6 address.
Definition: in.h:132
union dns_request::@3 address
Socket address to fill in with resolved address.
if(natsemi->flags &NATSEMI_64BIT) return 1
struct dns_rr_cname cname
"CNAME" record
Definition: dns.h:146
static void dns_resolved(struct dns_request *dns)
Mark DNS request as resolved and complete.
Definition: dns.c:503

References dns_rr::a, dns_rr::aaaa, dns_request::address, AF_INET, AF_INET6, assert(), dns_request::buf, dns_rr::cname, dns_rr::common, dns_name::data, io_buffer::data, DBGC, DBGC2, dns_compare(), dns_copy(), dns_done(), DNS_MAX_CNAME_RECURSION, dns_resolved(), dns_send_packet(), dns_skip(), dns_skip_search(), dns_type(), DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_CNAME, done, EINVAL, ELOOP, ENXIO_NO_RECORD, free_iob(), htons, dns_header::id, dns_rr_aaaa::in6_addr, dns_rr_a::in_addr, iob_len(), dns_name::len, memcpy(), dns_request::name, name, ntohs, dns_name::offset, offset, dns_request::offset, offsetof, dns_header::qdcount, dns_question::qtype, dns_request::qtype, dns_request::query, dns_request::question, rc, dns_rr_common::rdlength, dns_request::recursion, dns_request::search, dns_request::sin, dns_request::sin6, sockaddr_in6::sin6_addr, sockaddr_in6::sin6_family, sockaddr_in::sin_addr, sockaddr_in::sin_family, stop_timer(), strerror(), dns_request::timer, and dns_rr_common::type.

◆ dns_xfer_close()

static void dns_xfer_close ( struct dns_request dns,
int  rc 
)
static

Receive new data.

Parameters
dnsDNS request
rcReason for close

Definition at line 863 of file dns.c.

863  {
864 
865  if ( ! rc )
866  rc = -ECONNABORTED;
867 
868  dns_done ( dns, rc );
869 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void dns_done(struct dns_request *dns, int rc)
Mark DNS request as complete.
Definition: dns.c:487
#define ECONNABORTED
Connection aborted.
Definition: errno.h:353

References dns_done(), ECONNABORTED, and rc.

◆ dns_progress()

static int dns_progress ( struct dns_request dns,
struct job_progress progress 
)
static

Report job progress.

Parameters
dnsDNS request
progressProgress report to fill in
Return values
ongoing_rcOngoing job status code (if known)

Definition at line 878 of file dns.c.

879  {
880  int len;
881 
882  /* Show current question as progress message */
883  len = dns_decode ( &dns->name, progress->message,
884  ( sizeof ( progress->message ) - 1 /* NUL */ ) );
885  if ( len < 0 ) {
886  /* Ignore undecodable names */
887  progress->message[0] = '\0';
888  }
889 
890  return 0;
891 }
char message[32]
Message (optional)
Definition: job.h:32
char name[DNS_MAX_NAME_LEN]
Name buffer.
Definition: dns.c:463
uint32_t len
Length.
Definition: ena.h:14
int dns_decode(struct dns_name *name, char *data, size_t len)
Decode RFC1035-encoded DNS name.
Definition: dns.c:204

References dns_decode(), len, job_progress::message, and dns_request::name.

◆ dns_resolv()

static int dns_resolv ( struct interface resolv,
const char *  name,
struct sockaddr sa 
)
static

Resolve name using DNS.

Parameters
resolvName resolution interface
nameName to resolve
saSocket address to fill in
Return values
rcReturn status code

Definition at line 921 of file dns.c.

922  {
923  struct dns_request *dns;
924  struct dns_header *query;
925  size_t search_len;
926  int name_len;
927  int rc;
928 
929  /* Fail immediately if no DNS servers */
930  if ( ! nameserver.sa.sa_family ) {
931  DBG ( "DNS not attempting to resolve \"%s\": "
932  "no DNS servers\n", name );
934  goto err_no_nameserver;
935  }
936 
937  /* Determine whether or not to use search list */
938  search_len = ( strchr ( name, '.' ) ? 0 : dns_search.len );
939 
940  /* Allocate DNS structure */
941  dns = zalloc ( sizeof ( *dns ) + search_len );
942  if ( ! dns ) {
943  rc = -ENOMEM;
944  goto err_alloc_dns;
945  }
946  ref_init ( &dns->refcnt, NULL );
947  intf_init ( &dns->resolv, &dns_resolv_desc, &dns->refcnt );
948  intf_init ( &dns->socket, &dns_socket_desc, &dns->refcnt );
949  timer_init ( &dns->timer, dns_timer_expired, &dns->refcnt );
950  memcpy ( &dns->address.sa, sa, sizeof ( dns->address.sa ) );
951  dns->search.data = ( ( ( void * ) dns ) + sizeof ( *dns ) );
952  dns->search.len = search_len;
953  memcpy ( dns->search.data, dns_search.data, search_len );
954 
955  /* Determine initial query type */
956  switch ( nameserver.sa.sa_family ) {
957  case AF_INET:
958  dns->qtype = htons ( DNS_TYPE_A );
959  break;
960  case AF_INET6:
961  dns->qtype = htons ( DNS_TYPE_AAAA );
962  break;
963  default:
964  rc = -ENOTSUP;
965  goto err_type;
966  }
967 
968  /* Construct query */
969  query = &dns->buf.query;
970  query->flags = htons ( DNS_FLAG_RD );
971  query->qdcount = htons ( 1 );
972  dns->name.data = &dns->buf;
973  dns->name.offset = offsetof ( typeof ( dns->buf ), name );
974  dns->name.len = offsetof ( typeof ( dns->buf ), padding );
975  name_len = dns_encode ( name, &dns->name );
976  if ( name_len < 0 ) {
977  rc = name_len;
978  goto err_encode;
979  }
980  dns->offset = ( offsetof ( typeof ( dns->buf ), name ) +
981  name_len - 1 /* Strip root label */ );
982  if ( ( rc = dns_question ( dns ) ) != 0 )
983  goto err_question;
984 
985  /* Open UDP connection */
986  if ( ( rc = xfer_open_socket ( &dns->socket, SOCK_DGRAM,
987  &nameserver.sa, NULL ) ) != 0 ) {
988  DBGC ( dns, "DNS %p could not open socket: %s\n",
989  dns, strerror ( rc ) );
990  goto err_open_socket;
991  }
992 
993  /* Start timer to trigger first packet */
994  start_timer_nodelay ( &dns->timer );
995 
996  /* Attach parent interface, mortalise self, and return */
997  intf_plug_plug ( &dns->resolv, resolv );
998  ref_put ( &dns->refcnt );
999  return 0;
1000 
1001  err_open_socket:
1002  err_question:
1003  err_encode:
1004  err_type:
1005  ref_put ( &dns->refcnt );
1006  err_alloc_dns:
1007  err_no_nameserver:
1008  return rc;
1009 }
size_t offset
Offset of search suffix within current query.
Definition: dns.c:474
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
uint16_t qdcount
Number of question records.
Definition: dns.h:71
#define ENXIO_NO_NAMESERVER
Definition: dns.c:62
#define AF_INET6
IPv6 Internet addresses.
Definition: socket.h:64
static void start_timer_nodelay(struct retry_timer *timer)
Start timer with no delay.
Definition: retry.h:99
struct sockaddr sa
Definition: dns.c:68
A DNS packet header.
Definition: dns.h:65
#define DNS_FLAG_RD
Recursion desired flag.
Definition: dns.h:81
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
int xfer_open_socket(struct interface *intf, int semantics, struct sockaddr *peer, struct sockaddr *local)
Open socket.
Definition: open.c:141
static void dns_timer_expired(struct retry_timer *timer, int fail)
Handle DNS retransmission timer expiry.
Definition: dns.c:594
#define SOCK_DGRAM
Definition: socket.h:29
#define DBGC(...)
Definition: compiler.h:505
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:102
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
void * data
Start of data.
Definition: dns.h:21
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
struct refcnt refcnt
Reference counter.
Definition: dns.c:442
#define DNS_TYPE_A
Type of a DNS "A" record.
Definition: dns.h:107
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static union @2 nameserver
The DNS server.
static struct dns_name dns_search
The DNS search list.
Definition: dns.c:79
struct sockaddr sa
Definition: dns.c:452
#define DNS_TYPE_AAAA
Type of a DNS "AAAA" record.
Definition: dns.h:118
struct interface resolv
Name resolution interface.
Definition: dns.c:444
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
uint16_t flags
Flags.
Definition: dns.h:69
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:241
struct dns_header query
Query header.
Definition: dns.c:461
int dns_encode(const char *string, struct dns_name *name)
Encode a DNS name using RFC1035 encoding.
Definition: dns.c:88
A DNS request.
Definition: dns.c:440
char name[DNS_MAX_NAME_LEN]
Name buffer.
Definition: dns.c:463
struct interface socket
Data transfer interface.
Definition: dns.c:446
A DNS question.
Definition: dns.h:84
static struct interface_descriptor dns_socket_desc
DNS socket interface descriptor.
Definition: dns.c:900
struct dns_name search
Search list.
Definition: dns.c:476
struct retry_timer timer
Retry timer.
Definition: dns.c:448
uint16_t qtype
Initial query type.
Definition: dns.c:457
static struct interface_descriptor dns_resolv_desc
DNS resolver interface descriptor.
Definition: dns.c:910
struct dns_request::@4 buf
Buffer for current query.
int resolv(struct interface *resolv, const char *name, struct sockaddr *sa)
Start name resolution.
Definition: resolv.c:257
size_t len
Total length of data.
Definition: dns.h:25
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:173
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
#define htons(value)
Definition: byteswap.h:135
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:63
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
union dns_request::@3 address
Socket address to fill in with resolved address.

References dns_request::address, AF_INET, AF_INET6, dns_request::buf, dns_name::data, DBG, DBGC, dns_encode(), DNS_FLAG_RD, dns_resolv_desc, dns_search, dns_socket_desc, dns_timer_expired(), DNS_TYPE_A, DNS_TYPE_AAAA, ENOMEM, ENOTSUP, ENXIO_NO_NAMESERVER, dns_header::flags, htons, intf_init(), intf_plug_plug(), dns_name::len, memcpy(), dns_request::name, name, nameserver, NULL, dns_request::offset, offsetof, dns_header::qdcount, dns_request::qtype, dns_request::query, rc, ref_init, ref_put, dns_request::refcnt, resolv(), dns_request::resolv, dns_request::sa, sa, dns_request::search, SOCK_DGRAM, dns_request::socket, start_timer_nodelay(), strchr(), strerror(), dns_request::timer, xfer_open_socket(), and zalloc().

◆ __resolver()

struct resolver dns_resolver __resolver ( RESOLV_NORMAL  )

DNS name resolver.

◆ format_dnssl_setting()

static int format_dnssl_setting ( const struct setting_type *type  __unused,
const void *  raw,
size_t  raw_len,
char *  buf,
size_t  len 
)
static

Format DNS search list setting.

Parameters
typeSetting type
rawRaw setting value
raw_lenLength of raw setting value
bufBuffer to contain formatted value
lenLength of buffer
Return values
lenLength of formatted value, or negative error

Definition at line 1034 of file dns.c.

1036  {
1037  struct dns_name name = {
1038  .data = ( ( void * ) raw ),
1039  .len = raw_len,
1040  };
1041  size_t remaining = len;
1042  size_t total = 0;
1043  int name_len;
1044 
1045  while ( name.offset < raw_len ) {
1046 
1047  /* Decode name */
1048  remaining = ( ( total < len ) ? ( len - total ) : 0 );
1049  name_len = dns_decode ( &name, ( buf + total ), remaining );
1050  if ( name_len < 0 )
1051  return name_len;
1052  total += name_len;
1053 
1054  /* Move to next name */
1055  name.offset = dns_skip_search ( &name );
1056 
1057  /* Add separator if applicable */
1058  if ( name.offset != raw_len ) {
1059  if ( total < len )
1060  buf[total] = ' ';
1061  total++;
1062  }
1063  }
1064 
1065  return total;
1066 }
const char * name
Definition: ath9k_hw.c:1984
An RFC1035-encoded DNS name.
Definition: dns.h:19
static size_t raw_len
Definition: base16.h:50
uint32_t len
Length.
Definition: ena.h:14
static int dns_skip_search(struct dns_name *name)
Skip RFC1035-encoded DNS name in search list.
Definition: dns.c:391
__be32 raw[7]
Definition: CIB_PRM.h:28
int dns_decode(struct dns_name *name, char *data, size_t len)
Decode RFC1035-encoded DNS name.
Definition: dns.c:204

References dns_decode(), dns_skip_search(), len, name, raw, and raw_len.

◆ __setting() [1/3]

const struct setting dns_setting __setting ( SETTING_IP4_EXTRA  ,
dns   
)

IPv4 DNS server setting.

◆ __setting() [2/3]

const struct setting dns6_setting __setting ( SETTING_IP6_EXTRA  ,
dns6   
)

IPv6 DNS server setting.

◆ __setting() [3/3]

const struct setting dnssl_setting __setting ( SETTING_IP_EXTRA  ,
dnssl   
)

DNS search list.

◆ apply_dns_search()

static void apply_dns_search ( void  )
static

Apply DNS search list.

Definition at line 1103 of file dns.c.

1103  {
1104  char *localdomain;
1105  int len;
1106 
1107  /* Free existing search list */
1108  free ( dns_search.data );
1109  memset ( &dns_search, 0, sizeof ( dns_search ) );
1110 
1111  /* Fetch DNS search list */
1112  len = fetch_setting_copy ( NULL, &dnssl_setting, NULL, NULL,
1113  &dns_search.data );
1114  if ( len >= 0 ) {
1115  dns_search.len = len;
1116  return;
1117  }
1118 
1119  /* If no DNS search list exists, try to fetch the local domain */
1120  fetch_string_setting_copy ( NULL, &domain_setting, &localdomain );
1121  if ( localdomain ) {
1122  len = dns_encode ( localdomain, &dns_search );
1123  if ( len >= 0 ) {
1124  dns_search.data = malloc ( len );
1125  if ( dns_search.data ) {
1126  dns_search.len = len;
1127  dns_encode ( localdomain, &dns_search );
1128  }
1129  }
1130  free ( localdomain );
1131  return;
1132  }
1133 }
void * data
Start of data.
Definition: dns.h:21
static struct dns_name dns_search
The DNS search list.
Definition: dns.c:79
int fetch_string_setting_copy(struct settings *settings, const struct setting *setting, char **data)
Fetch value of string setting.
Definition: settings.c:877
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
int dns_encode(const char *string, struct dns_name *name)
Encode a DNS name using RFC1035 encoding.
Definition: dns.c:88
int fetch_setting_copy(struct settings *settings, const struct setting *setting, struct settings **origin, struct setting *fetched, void **data)
Fetch copy of setting.
Definition: settings.c:789
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
uint32_t len
Length.
Definition: ena.h:14
size_t len
Total length of data.
Definition: dns.h:25
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
void * memset(void *dest, int character, size_t len) __nonnull

References dns_name::data, dns_encode(), dns_search, fetch_setting_copy(), fetch_string_setting_copy(), free, len, dns_name::len, malloc(), memset(), and NULL.

Referenced by apply_dns_settings().

◆ apply_dns_settings()

static int apply_dns_settings ( void  )
static

Apply DNS settings.

Return values
rcReturn status code

Definition at line 1140 of file dns.c.

1140  {
1141 
1142  /* Fetch DNS server address */
1143  nameserver.sa.sa_family = 0;
1144  if ( fetch_ipv6_setting ( NULL, &dns6_setting,
1145  &nameserver.sin6.sin6_addr ) >= 0 ) {
1146  nameserver.sin6.sin6_family = AF_INET6;
1147  } else if ( fetch_ipv4_setting ( NULL, &dns_setting,
1148  &nameserver.sin.sin_addr ) >= 0 ) {
1149  nameserver.sin.sin_family = AF_INET;
1150  }
1151  if ( nameserver.sa.sa_family ) {
1152  DBG ( "DNS using nameserver %s\n",
1153  sock_ntoa ( &nameserver.sa ) );
1154  }
1155 
1156  /* Fetch DNS search list */
1157  apply_dns_search();
1158  if ( DBG_LOG && ( dns_search.len != 0 ) ) {
1159  struct dns_name name;
1160  int offset;
1161 
1162  DBG ( "DNS search list:" );
1163  memcpy ( &name, &dns_search, sizeof ( name ) );
1164  while ( name.offset != name.len ) {
1165  DBG ( " %s", dns_name ( &name ) );
1166  offset = dns_skip_search ( &name );
1167  if ( offset < 0 )
1168  break;
1169  name.offset = offset;
1170  }
1171  DBG ( "\n" );
1172  }
1173 
1174  return 0;
1175 }
const char * name
Definition: ath9k_hw.c:1984
#define AF_INET6
IPv6 Internet addresses.
Definition: socket.h:64
int fetch_ipv4_setting(struct settings *settings, const struct setting *setting, struct in_addr *inp)
Fetch value of IPv4 address setting.
Definition: settings.c:916
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static union @2 nameserver
The DNS server.
static struct dns_name dns_search
The DNS search list.
Definition: dns.c:79
An RFC1035-encoded DNS name.
Definition: dns.h:19
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static void apply_dns_search(void)
Apply DNS search list.
Definition: dns.c:1103
const char * sock_ntoa(struct sockaddr *sa)
Transcribe socket address.
Definition: socket.c:42
static int dns_skip_search(struct dns_name *name)
Skip RFC1035-encoded DNS name in search list.
Definition: dns.c:391
size_t len
Total length of data.
Definition: dns.h:25
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
int fetch_ipv6_setting(struct settings *settings, const struct setting *setting, struct in6_addr *inp)
Fetch value of IPv6 address setting.
Definition: settings.c:954
#define DBG_LOG
Definition: compiler.h:317
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:63

References AF_INET, AF_INET6, apply_dns_search(), DBG, DBG_LOG, dns_search, dns_skip_search(), fetch_ipv4_setting(), fetch_ipv6_setting(), dns_name::len, memcpy(), name, nameserver, NULL, offset, and sock_ntoa().

Variable Documentation

◆ sa

struct sockaddr sa

◆ st

u8 st

◆ sin

struct sockaddr_in sin

◆ sin6

struct sockaddr_in6 sin6

◆ nameserver

union { ... } nameserver
Initial value:
= {
.st = {
.st_port = htons ( DNS_PORT ),
},
}
#define DNS_PORT
DNS server port.
Definition: dns.h:16
#define htons(value)
Definition: byteswap.h:135

The DNS server.

Referenced by apply_dns_settings(), and dns_resolv().

◆ dns_search

struct dns_name dns_search
static

The DNS search list.

Definition at line 79 of file dns.c.

Referenced by apply_dns_search(), apply_dns_settings(), and dns_resolv().

◆ dns_socket_operations

struct interface_operation dns_socket_operations[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
static void dns_xfer_close(struct dns_request *dns, int rc)
Receive new data.
Definition: dns.c:863
static int dns_xfer_deliver(struct dns_request *dns, struct io_buffer *iobuf, struct xfer_metadata *meta __unused)
Receive new data.
Definition: dns.c:613
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193
A DNS request.
Definition: dns.c:440

DNS socket interface operations.

Definition at line 894 of file dns.c.

◆ dns_socket_desc

struct interface_descriptor dns_socket_desc
static
Initial value:
=
static struct interface_operation dns_socket_operations[]
DNS socket interface operations.
Definition: dns.c:894
A DNS request.
Definition: dns.c:440
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:65

DNS socket interface descriptor.

Definition at line 900 of file dns.c.

Referenced by dns_resolv().

◆ dns_resolv_op

struct interface_operation dns_resolv_op[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
static void dns_done(struct dns_request *dns, int rc)
Mark DNS request as complete.
Definition: dns.c:487
static int dns_progress(struct dns_request *dns, struct job_progress *progress)
Report job progress.
Definition: dns.c:878
Job progress.
Definition: job.h:15
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
A DNS request.
Definition: dns.c:440

DNS resolver interface operations.

Definition at line 904 of file dns.c.

◆ dns_resolv_desc

struct interface_descriptor dns_resolv_desc
static
Initial value:
=
A DNS request.
Definition: dns.c:440
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:65
static struct interface_operation dns_resolv_op[]
DNS resolver interface operations.
Definition: dns.c:904
int resolv(struct interface *resolv, const char *name, struct sockaddr *sa)
Start name resolution.
Definition: resolv.c:257

DNS resolver interface descriptor.

Definition at line 910 of file dns.c.

Referenced by dns_resolv().

◆ __setting_type

const struct setting_type setting_type_dnssl __setting_type
Initial value:
= {
.name = "dnssl",
}
static int format_dnssl_setting(const struct setting_type *type __unused, const void *raw, size_t raw_len, char *buf, size_t len)
Format DNS search list setting.
Definition: dns.c:1034

A DNS search list setting type.

A string setting type.

Definition at line 1069 of file dns.c.

◆ __settings_applicator

struct settings_applicator dns_applicator __settings_applicator
Initial value:
= {
}
static int apply_dns_settings(void)
Apply DNS settings.
Definition: dns.c:1140

DNS settings applicator.

Definition at line 1178 of file dns.c.