Added manager for DNS resolvers
[strongswan.git] / src / libstrongswan / resolver / resolver_manager.c
1 /*
2 * Copyright (C) 2011-2012 Reto Guadagnini
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "resolver_manager.h"
17
18 #include <utils/debug.h>
19
20 typedef struct private_resolver_manager_t private_resolver_manager_t;
21
22 /**
23 * private data of resolver_manager
24 */
25 struct private_resolver_manager_t {
26
27 /**
28 * public functions
29 */
30 resolver_manager_t public;
31
32 /**
33 * constructor function to create resolver instances
34 */
35 resolver_constructor_t constructor;
36 };
37
38 METHOD(resolver_manager_t, add_resolver, void,
39 private_resolver_manager_t *this, resolver_constructor_t constructor)
40 {
41 if (!this->constructor)
42 {
43 this->constructor = constructor;
44 }
45 }
46
47 METHOD(resolver_manager_t, remove_resolver, void,
48 private_resolver_manager_t *this, resolver_constructor_t constructor)
49 {
50 if (this->constructor == constructor)
51 {
52 this->constructor = NULL;
53 }
54 }
55
56 METHOD(resolver_manager_t, create, resolver_t*,
57 private_resolver_manager_t *this)
58 {
59 char *resolv_conf;
60 char *trust_anchor_file;
61
62 resolv_conf = lib->settings->get_str(lib->settings,
63 "libstrongswan.plugins.resolver."
64 "resolv_conf",
65 "/etc/resolv.conf");
66
67 trust_anchor_file = lib->settings->get_str(lib->settings,
68 "libstrongswan.plugins.resolver."
69 "trust_anchor",
70 "/etc/trust.anchors");
71
72 return this->constructor(resolv_conf, trust_anchor_file);
73 }
74
75 METHOD(resolver_manager_t, destroy, void,
76 private_resolver_manager_t *this)
77 {
78 free(this);
79 }
80
81 /*
82 * See header
83 */
84 resolver_manager_t *resolver_manager_create()
85 {
86 private_resolver_manager_t *this;
87
88 INIT(this,
89 .public = {
90 .add_resolver = _add_resolver,
91 .remove_resolver = _remove_resolver,
92 .create = _create,
93 .destroy = _destroy,
94 },
95 );
96
97 return &this->public;
98 }
99