]>
Commit | Line | Data |
---|---|---|
74b2466e LP |
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" | |
322345fd | 28 | #include "hashmap.h" |
623a4c97 | 29 | #include "in-addr-util.h" |
74b2466e LP |
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, | |
322345fd | 37 | DNS_CLASS_ANY = 0xFF, |
b93312f5 ZJS |
38 | _DNS_CLASS_MAX, |
39 | _DNS_CLASS_INVALID = -1 | |
74b2466e LP |
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, | |
0dae31d4 | 54 | DNS_TYPE_LOC = 0x1D, |
74b2466e | 55 | DNS_TYPE_SRV = 0x21, |
322345fd | 56 | DNS_TYPE_DNAME = 0x27, |
623a4c97 | 57 | DNS_TYPE_SSHFP = 0x2C, |
74b2466e | 58 | |
9de3e329 ZJS |
59 | DNS_TYPE_SPF = 0x63, |
60 | ||
74b2466e LP |
61 | /* Special records */ |
62 | DNS_TYPE_ANY = 0xFF, | |
63 | DNS_TYPE_OPT = 0x29, /* EDNS0 option */ | |
64 | DNS_TYPE_TKEY = 0xF9, | |
65 | DNS_TYPE_TSIG = 0xFA, | |
66 | DNS_TYPE_IXFR = 0xFB, | |
67 | DNS_TYPE_AXFR = 0xFC, | |
b93312f5 ZJS |
68 | _DNS_TYPE_MAX, |
69 | _DNS_TYPE_INVALID = -1 | |
74b2466e LP |
70 | }; |
71 | ||
72 | struct DnsResourceKey { | |
faa133f3 LP |
73 | unsigned n_ref; |
74 | uint16_t class, type; | |
75 | char *_name; /* don't access directy, use DNS_RESOURCE_KEY_NAME()! */ | |
74b2466e LP |
76 | }; |
77 | ||
78 | struct DnsResourceRecord { | |
79 | unsigned n_ref; | |
faa133f3 | 80 | DnsResourceKey *key; |
74b2466e | 81 | uint32_t ttl; |
0dae31d4 | 82 | bool unparseable; |
74b2466e LP |
83 | union { |
84 | struct { | |
85 | void *data; | |
86 | uint16_t size; | |
87 | } generic; | |
88 | ||
89 | /* struct { */ | |
90 | /* uint16_t priority; */ | |
91 | /* uint16_t weight; */ | |
92 | /* uint16_t port; */ | |
93 | /* char *name; */ | |
94 | /* } srv; */ | |
95 | ||
96 | struct { | |
97 | char *name; | |
8ac4e9e1 | 98 | } ptr, ns, cname, dname; |
74b2466e LP |
99 | |
100 | struct { | |
101 | char *cpu; | |
102 | char *os; | |
103 | } hinfo; | |
104 | ||
2e276efc ZJS |
105 | struct { |
106 | char **strings; | |
107 | } txt; | |
74b2466e LP |
108 | |
109 | struct { | |
110 | struct in_addr in_addr; | |
111 | } a; | |
112 | ||
113 | struct { | |
114 | struct in6_addr in6_addr; | |
115 | } aaaa; | |
7e8e0422 LP |
116 | |
117 | struct { | |
118 | char *mname; | |
119 | char *rname; | |
120 | uint32_t serial; | |
121 | uint32_t refresh; | |
122 | uint32_t retry; | |
123 | uint32_t expire; | |
124 | uint32_t minimum; | |
125 | } soa; | |
946c7094 ZJS |
126 | |
127 | struct { | |
128 | uint16_t priority; | |
129 | char *exchange; | |
130 | } mx; | |
0dae31d4 ZJS |
131 | |
132 | struct { | |
133 | uint8_t version; | |
134 | uint8_t size; | |
135 | uint8_t horiz_pre; | |
136 | uint8_t vert_pre; | |
137 | uint32_t latitude; | |
138 | uint32_t longitude; | |
139 | uint32_t altitude; | |
140 | } loc; | |
74b2466e LP |
141 | }; |
142 | }; | |
143 | ||
faa133f3 LP |
144 | static inline const char* DNS_RESOURCE_KEY_NAME(const DnsResourceKey *key) { |
145 | if (_unlikely_(!key)) | |
146 | return NULL; | |
147 | ||
148 | if (key->_name) | |
149 | return key->_name; | |
150 | ||
151 | return (char*) key + sizeof(DnsResourceKey); | |
152 | } | |
74b2466e | 153 | |
faa133f3 LP |
154 | DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name); |
155 | DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name); | |
156 | DnsResourceKey* dns_resource_key_ref(DnsResourceKey *key); | |
157 | DnsResourceKey* dns_resource_key_unref(DnsResourceKey *key); | |
158 | int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b); | |
159 | int dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr); | |
160 | int dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr); | |
322345fd LP |
161 | unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]); |
162 | int dns_resource_key_compare_func(const void *a, const void *b); | |
2d4c5cbc | 163 | int dns_resource_key_to_string(const DnsResourceKey *key, char **ret); |
faa133f3 | 164 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref); |
322345fd | 165 | |
faa133f3 | 166 | DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key); |
8bf52d3d | 167 | DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name); |
74b2466e LP |
168 | DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr); |
169 | DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr); | |
623a4c97 | 170 | int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name); |
322345fd | 171 | int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b); |
2d4c5cbc | 172 | int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret); |
faa133f3 | 173 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceRecord*, dns_resource_record_unref); |
322345fd LP |
174 | |
175 | const char *dns_type_to_string(uint16_t type); | |
2d4c5cbc LP |
176 | int dns_type_from_string(const char *name, uint16_t *type); |
177 | ||
322345fd | 178 | const char *dns_class_to_string(uint16_t type); |
2d4c5cbc | 179 | int dns_class_from_string(const char *name, uint16_t *class); |