2 * Copyright (C) 2011-2012 Reto Guadagnini
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include <ldns/ldns.h>
22 #include <utils/debug.h>
24 #include "unbound_resolver.h"
25 #include "unbound_response.h"
27 typedef struct private_resolver_t private_resolver_t
;
30 * private data of a unbound_resolver_t object.
32 struct private_resolver_t
{
40 * private unbound resolver handle (unbound context)
46 * query method implementation
48 METHOD(resolver_t
, query
, resolver_response_t
*,
49 private_resolver_t
*this, char *domain
, rr_class_t rr_class
,
52 unbound_response_t
*response
= NULL
;
53 struct ub_result
*result
= NULL
;
56 ub_retval
= ub_resolve(this->ctx
, domain
, rr_type
, rr_class
, &result
);
59 DBG1(DBG_LIB
, "unbound resolver error: %s", ub_strerror(ub_retval
));
60 ub_resolve_free(result
);
64 response
= unbound_response_create_frm_libub_response(result
);
67 DBG1(DBG_LIB
, "unbound_resolver: Could not create response.");
68 ub_resolve_free(result
);
71 ub_resolve_free(result
);
72 return (resolver_response_t
*)response
;
76 * destroy method implementation
78 METHOD(resolver_t
, destroy
, void,
79 private_resolver_t
*this)
81 ub_ctx_delete(this->ctx
);
86 * Described in header.
88 resolver_t
*unbound_resolver_create(char *resolv_conf
, char *ta_file
)
90 private_resolver_t
*this;
100 DBG1(DBG_LIB
, "creating an unbound_resolver instance");
102 this->ctx
= ub_ctx_create();
105 DBG1(DBG_LIB
, "failed to create an unbound resolver context");
110 ub_retval
= ub_ctx_resolvconf(this->ctx
, resolv_conf
);
113 DBG1(DBG_LIB
, "failed to read the resolver configuration file. "
114 "Unbound error: %s. errno says: %s", ub_strerror(ub_retval
),
120 ub_retval
= ub_ctx_add_ta_file(this->ctx
, ta_file
);
123 DBG1(DBG_LIB
, "failed to load trusted anchors from file %s. "
124 "Unbound error: %s. errno says: %s",
125 ta_file
, ub_strerror(ub_retval
), strerror(errno
));
128 DBG1(DBG_LIB
, "unbound resolver instance created");
129 return &this->public;