Postfix3.3.1
dns_strrecord.c
[詳解]
1 /*++
2 /* NAME
3 /* dns_strrecord 3
4 /* SUMMARY
5 /* name service resource record printable forms
6 /* SYNOPSIS
7 /* #include <dns.h>
8 /*
9 /* char *dns_strrecord(buf, record)
10 /* VSTRING *buf;
11 /* DNS_RR *record;
12 /* DESCRIPTION
13 /* dns_strrecord() formats a DNS resource record as "name ttl
14 /* class type preference value", where the class field is
15 /* always "IN", the preference field exists only for MX records,
16 /* and all names end in ".". The result value is the payload
17 /* of the buffer argument.
18 /* LICENSE
19 /* .ad
20 /* .fi
21 /* The Secure Mailer license must be distributed with this software.
22 /* AUTHOR(S)
23 /* Wietse Venema
24 /* IBM T.J. Watson Research
25 /* P.O. Box 704
26 /* Yorktown Heights, NY 10598, USA
27 /*
28 /* Wietse Venema
29 /* Google, Inc.
30 /* 111 8th Avenue
31 /* New York, NY 10011, USA
32 /*--*/
33 
34 /* System library. */
35 
36 #include <sys_defs.h>
37 #include <string.h> /* memcpy */
38 
39 /* Utility library. */
40 
41 #include <vstring.h>
42 #include <msg.h>
43 
44 /* DNS library. */
45 
46 #include <dns.h>
47 
48 /* dns_strrecord - format resource record as generic string */
49 
50 char *dns_strrecord(VSTRING *buf, DNS_RR *rr)
51 {
52  const char myname[] = "dns_strrecord";
53  MAI_HOSTADDR_STR host;
54  UINT32_TYPE soa_buf[5];
55 
56  vstring_sprintf(buf, "%s. %u IN %s ",
57  rr->rname, rr->ttl, dns_strtype(rr->type));
58  switch (rr->type) {
59  case T_A:
60 #ifdef T_AAAA
61  case T_AAAA:
62 #endif
63  if (dns_rr_to_pa(rr, &host) == 0)
64  msg_fatal("%s: conversion error for resource record type %s: %m",
65  myname, dns_strtype(rr->type));
66  vstring_sprintf_append(buf, "%s", host.buf);
67  break;
68  case T_CNAME:
69  case T_DNAME:
70  case T_MB:
71  case T_MG:
72  case T_MR:
73  case T_NS:
74  case T_PTR:
75  vstring_sprintf_append(buf, "%s.", rr->data);
76  break;
77  case T_TXT:
78  vstring_sprintf_append(buf, "%s", rr->data);
79  break;
80  case T_MX:
81  vstring_sprintf_append(buf, "%u %s.", rr->pref, rr->data);
82  break;
83  case T_TLSA:
84  if (rr->data_len >= 3) {
85  uint8_t *ip = (uint8_t *) rr->data;
86  uint8_t usage = *ip++;
87  uint8_t selector = *ip++;
88  uint8_t mtype = *ip++;
89  unsigned i;
90 
91  /* /\.example\. \d+ IN TLSA \d+ \d+ \d+ [\da-f]*$/ IGNORE */
92  vstring_sprintf_append(buf, "%d %d %d ", usage, selector, mtype);
93  for (i = 3; i < rr->data_len; ++i)
94  vstring_sprintf_append(buf, "%02x", *ip++);
95  } else {
96  vstring_sprintf_append(buf, "[truncated record]");
97  }
98 
99  /*
100  * We use the SOA record TTL to determine the negative reply TTL. We
101  * save the time fields in the SOA record for debugging, but for now
102  * we don't bother saving the source host and mailbox information, as
103  * that would require changes to the DNS_RR structure. See also code
104  * in dns_get_rr().
105  */
106  case T_SOA:
107  memcpy(soa_buf, rr->data, sizeof(soa_buf));
108  vstring_sprintf_append(buf, "- - %u %u %u %u %u",
109  soa_buf[0], soa_buf[1], soa_buf[2],
110  soa_buf[3], soa_buf[4]);
111  break;
112  default:
113  msg_fatal("%s: don't know how to print type %s",
114  myname, dns_strtype(rr->type));
115  }
116  return (vstring_str(buf));
117 }
#define UINT32_TYPE
Definition: sys_defs.h:1720
unsigned short pref
Definition: dns.h:146
const char * dns_strtype(unsigned)
Definition: dns_strtype.c:187
#define vstring_str(vp)
Definition: vstring.h:71
char data[1]
Definition: dns.h:149
#define T_DNAME
Definition: dns.h:87
#define T_TXT
Definition: dns.h:115
const char * dns_rr_to_pa(DNS_RR *, MAI_HOSTADDR_STR *)
Definition: dns_rr_to_pa.c:53
char buf[MAI_HOSTADDR_STRSIZE]
Definition: myaddrinfo.h:146
VSTRING * vstring_sprintf_append(VSTRING *vp, const char *format,...)
Definition: vstring.c:624
size_t data_len
Definition: dns.h:148
VSTRING * vstring_sprintf(VSTRING *vp, const char *format,...)
Definition: vstring.c:602
#define T_TLSA
Definition: dns.h:81
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
char * dns_strrecord(VSTRING *buf, DNS_RR *rr)
Definition: dns_strrecord.c:50
char * rname
Definition: dns.h:141
unsigned int ttl
Definition: dns.h:144
unsigned short type
Definition: dns.h:142
Definition: dns.h:139