]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/plugins/unbound/unbound_rr.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libstrongswan / plugins / unbound / unbound_rr.c
1 /*
2 * Copyright (C) 2012 Reto Guadagnini
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <resolver/rr.h>
18
19 #include <library.h>
20 #include <utils/debug.h>
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "unbound_rr.h"
26
27 typedef struct private_unbound_rr_t private_unbound_rr_t;
28
29 /**
30 * private data of an unbound_rr_t object.
31 */
32 struct private_unbound_rr_t {
33
34 /**
35 * Public data
36 */
37 unbound_rr_t public;
38
39 /**
40 * Owner name
41 */
42 char* name;
43
44 /**
45 * Type
46 */
47 rr_type_t type;
48
49 /**
50 * Class
51 */
52 rr_class_t class;
53
54 /**
55 * TTL
56 */
57 uint32_t ttl;
58
59 /**
60 * Size of the rdata field in octets
61 */
62 uint16_t size;
63
64 /**
65 * RDATA field (array of bytes in network order)
66 */
67 u_char *rdata;
68 };
69
70 METHOD(rr_t, get_name, char *,
71 private_unbound_rr_t *this)
72 {
73 return this->name;
74 }
75
76 METHOD(rr_t, get_type, rr_type_t,
77 private_unbound_rr_t *this)
78 {
79 return this->type;
80 }
81
82 METHOD(rr_t, get_class, rr_class_t,
83 private_unbound_rr_t *this)
84 {
85 return this->class;
86 }
87
88 METHOD(rr_t, get_ttl, uint32_t,
89 private_unbound_rr_t *this)
90 {
91 return this->ttl;
92 }
93
94 METHOD(rr_t, get_rdata, chunk_t,
95 private_unbound_rr_t *this)
96 {
97 return chunk_create(this->rdata, this->size);
98 }
99
100 METHOD(rr_t, destroy, void,
101 private_unbound_rr_t *this)
102 {
103 free(this->name);
104 free(this->rdata);
105 free(this);
106 }
107
108 /*
109 * Described in header.
110 */
111 unbound_rr_t *unbound_rr_create_frm_ldns_rr(ldns_rr *rr)
112 {
113 private_unbound_rr_t *this;
114 ldns_status status;
115 ldns_buffer *buf;
116 int i;
117
118 INIT(this,
119 .public = {
120 .interface = {
121 .get_name = _get_name,
122 .get_type = _get_type,
123 .get_class = _get_class,
124 .get_ttl = _get_ttl,
125 .get_rdata = _get_rdata,
126 .destroy = _destroy,
127 },
128 },
129 );
130
131 this->name = ldns_rdf2str(ldns_rr_owner(rr));
132 if (!this->name)
133 {
134 DBG1(DBG_LIB, "failed to parse the owner name of a DNS RR");
135 _destroy(this);
136 return NULL;
137 }
138
139 this->type = (rr_type_t)ldns_rr_get_type(rr);
140 this->class = (rr_class_t)ldns_rr_get_class(rr);
141 this->ttl = ldns_rr_ttl(rr);
142 for(i = 0; i < ldns_rr_rd_count(rr); i++)
143 {
144 this->size += ldns_rdf_size(ldns_rr_rdf(rr, i));
145 }
146
147 /**
148 * The ldns library splits the RDATA field of a RR in various rdf.
149 * Here we reassemble these rdf to get the RDATA field of the RR.
150 */
151 buf = ldns_buffer_new(LDNS_MIN_BUFLEN);
152 /* The buffer will be resized automatically by ldns_rr_rdata2buffer_wire() */
153 status = ldns_rr_rdata2buffer_wire(buf, rr);
154
155 if (status != LDNS_STATUS_OK)
156 {
157 DBG1(DBG_LIB, "failed to get the RDATA field of a DNS RR");
158 ldns_buffer_free(buf);
159 _destroy(this);
160 return NULL;
161 }
162
163 this->rdata = ldns_buffer_export(buf);
164 ldns_buffer_free(buf);
165
166 return &this->public;
167 }