chiark / gitweb /
afd623a3829c81cb116e1bd4f1408a2e39069126
[elogind.git] / src / resolve / resolved-dns-rr.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2014 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22  ***/
23
24 #include <inttypes.h>
25 #include <netinet/in.h>
26
27 #include "util.h"
28 #include "hashmap.h"
29 #include "in-addr-util.h"
30
31 typedef struct DnsResourceKey DnsResourceKey;
32 typedef struct DnsResourceRecord DnsResourceRecord;
33
34 /* DNS record classes, see RFC 1035 */
35 enum {
36         DNS_CLASS_IN   = 0x01,
37         DNS_CLASS_ANY  = 0xFF,
38         _DNS_CLASS_MAX,
39         _DNS_CLASS_INVALID = -1
40 };
41
42 /* DNS record types, see RFC 1035 */
43 enum {
44         /* Normal records */
45         DNS_TYPE_A     = 0x01,
46         DNS_TYPE_NS    = 0x02,
47         DNS_TYPE_CNAME = 0x05,
48         DNS_TYPE_SOA   = 0x06,
49         DNS_TYPE_PTR   = 0x0C,
50         DNS_TYPE_HINFO = 0x0D,
51         DNS_TYPE_MX    = 0x0F,
52         DNS_TYPE_TXT   = 0x10,
53         DNS_TYPE_AAAA  = 0x1C,
54         DNS_TYPE_SRV   = 0x21,
55         DNS_TYPE_DNAME = 0x27,
56         DNS_TYPE_SSHFP = 0x2C,
57
58         DNS_TYPE_SPF   = 0x63,
59
60         /* Special records */
61         DNS_TYPE_ANY   = 0xFF,
62         DNS_TYPE_OPT   = 0x29,      /* EDNS0 option */
63         DNS_TYPE_TKEY  = 0xF9,
64         DNS_TYPE_TSIG  = 0xFA,
65         DNS_TYPE_IXFR  = 0xFB,
66         DNS_TYPE_AXFR  = 0xFC,
67         _DNS_TYPE_MAX,
68         _DNS_TYPE_INVALID = -1
69 };
70
71 struct DnsResourceKey {
72         unsigned n_ref;
73         uint16_t class, type;
74         char *_name; /* don't access directy, use DNS_RESOURCE_KEY_NAME()! */
75 };
76
77 struct DnsResourceRecord {
78         unsigned n_ref;
79         DnsResourceKey *key;
80         uint32_t ttl;
81         union {
82                 struct {
83                         void *data;
84                         uint16_t size;
85                 } generic;
86
87                 /* struct { */
88                 /*         uint16_t priority; */
89                 /*         uint16_t weight; */
90                 /*         uint16_t port; */
91                 /*         char *name; */
92                 /* } srv; */
93
94                 struct {
95                         char *name;
96                 } ptr, ns, cname;
97
98                 struct {
99                         char *cpu;
100                         char *os;
101                 } hinfo;
102
103                 struct {
104                         char **strings;
105                 } txt;
106
107                 struct {
108                         struct in_addr in_addr;
109                 } a;
110
111                 struct {
112                         struct in6_addr in6_addr;
113                 } aaaa;
114
115                 struct {
116                         char *mname;
117                         char *rname;
118                         uint32_t serial;
119                         uint32_t refresh;
120                         uint32_t retry;
121                         uint32_t expire;
122                         uint32_t minimum;
123                 } soa;
124
125                 struct {
126                         uint16_t priority;
127                         char *exchange;
128                 } mx;
129         };
130 };
131
132 static inline const char* DNS_RESOURCE_KEY_NAME(const DnsResourceKey *key) {
133         if (_unlikely_(!key))
134                 return NULL;
135
136         if (key->_name)
137                 return key->_name;
138
139         return (char*) key + sizeof(DnsResourceKey);
140 }
141
142 DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name);
143 DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name);
144 DnsResourceKey* dns_resource_key_ref(DnsResourceKey *key);
145 DnsResourceKey* dns_resource_key_unref(DnsResourceKey *key);
146 int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b);
147 int dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr);
148 int dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr);
149 unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]);
150 int dns_resource_key_compare_func(const void *a, const void *b);
151 int dns_resource_key_to_string(const DnsResourceKey *key, char **ret);
152 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref);
153
154 DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key);
155 DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name);
156 DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr);
157 DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr);
158 int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name);
159 int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b);
160 int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret);
161 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceRecord*, dns_resource_record_unref);
162
163 const char *dns_type_to_string(uint16_t type);
164 int dns_type_from_string(const char *name, uint16_t *type);
165
166 const char *dns_class_to_string(uint16_t type);
167 int dns_class_from_string(const char *name, uint16_t *class);