attribute_manager supports attribute_handler's to handle configuration attributes...
[strongswan.git] / src / charon / plugins / attr / attr_provider.c
1 /*
2 * Copyright (C) 2009 Martin Willi
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 * $Id$
16 */
17
18 #include "attr_provider.h"
19
20 #include <time.h>
21
22 #include <daemon.h>
23
24 #define SERVER_MAX 2
25
26 typedef struct private_attr_provider_t private_attr_provider_t;
27 typedef struct attribute_entry_t attribute_entry_t;
28
29 /**
30 * private data of attr_provider
31 */
32 struct private_attr_provider_t {
33
34 /**
35 * public functions
36 */
37 attr_provider_t public;
38
39 /**
40 * List of attributes, attribute_entry_t
41 */
42 linked_list_t *attributes;
43 };
44
45 struct attribute_entry_t {
46 /** type of attribute */
47 configuration_attribute_type_t type;
48 /** attribute value */
49 chunk_t value;
50 };
51
52 /**
53 * convert enumerator value from attribute_entry
54 */
55 static bool attr_enum_filter(void *null, attribute_entry_t **in,
56 configuration_attribute_type_t *type, void* none, chunk_t *value)
57 {
58 *type = (*in)->type;
59 *value = (*in)->value;
60 return TRUE;
61 }
62
63 /**
64 * Implementation of attribute_provider_t.create_attribute_enumerator
65 */
66 static enumerator_t* create_attribute_enumerator(
67 private_attr_provider_t *this, identification_t *id)
68 {
69 return enumerator_create_filter(
70 this->attributes->create_enumerator(this->attributes),
71 (void*)attr_enum_filter, NULL, NULL);
72 }
73
74 /**
75 * Implementation of attr_provider_t.destroy
76 */
77 static void destroy(private_attr_provider_t *this)
78 {
79 attribute_entry_t *entry;
80
81 while (this->attributes->remove_last(this->attributes,
82 (void**)&entry) == SUCCESS)
83 {
84 free(entry->value.ptr);
85 free(entry);
86 }
87 this->attributes->destroy(this->attributes);
88 free(this);
89 }
90
91 /**
92 * Add an attribute entry to the list
93 */
94 static void add_entry(private_attr_provider_t *this, char *key, int nr,
95 configuration_attribute_type_t type)
96 {
97 attribute_entry_t *entry;
98 host_t *host;
99 char *str;
100
101 str = lib->settings->get_str(lib->settings, "charon.%s%d", NULL, key, nr);
102 if (str)
103 {
104 host = host_create_from_string(str, 0);
105 if (host)
106 {
107 entry = malloc_thing(attribute_entry_t);
108
109 if (host->get_family(host) == AF_INET6)
110 {
111 switch (type)
112 {
113 case INTERNAL_IP4_DNS:
114 type = INTERNAL_IP6_DNS;
115 break;
116 case INTERNAL_IP4_NBNS:
117 type = INTERNAL_IP6_NBNS;
118 break;
119 default:
120 break;
121 }
122 }
123 entry->type = type;
124 entry->value = chunk_clone(host->get_address(host));
125 host->destroy(host);
126 this->attributes->insert_last(this->attributes, entry);
127 }
128 }
129 }
130
131 /*
132 * see header file
133 */
134 attr_provider_t *attr_provider_create(database_t *db)
135 {
136 private_attr_provider_t *this;
137 int i;
138
139 this = malloc_thing(private_attr_provider_t);
140
141 this->public.provider.acquire_address = (host_t*(*)(attribute_provider_t *this, char*, identification_t *, host_t *))return_null;
142 this->public.provider.release_address = (bool(*)(attribute_provider_t *this, char*,host_t *, identification_t*))return_false;
143 this->public.provider.create_attribute_enumerator = (enumerator_t*(*)(attribute_provider_t*, identification_t *id))create_attribute_enumerator;
144 this->public.destroy = (void(*)(attr_provider_t*))destroy;
145
146 this->attributes = linked_list_create();
147
148 for (i = 1; i <= SERVER_MAX; i++)
149 {
150 add_entry(this, "dns", i, INTERNAL_IP4_DNS);
151 add_entry(this, "nbns", i, INTERNAL_IP4_NBNS);
152 }
153
154 return &this->public;
155 }
156