2 * Copyright (C) 2010 Tobias Brunner
3 * Copyright (C) 2009 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
17 #include "attr_provider.h"
23 #include <utils/linked_list.h>
27 typedef struct private_attr_provider_t private_attr_provider_t
;
28 typedef struct attribute_entry_t attribute_entry_t
;
31 * private data of attr_provider
33 struct private_attr_provider_t
{
38 attr_provider_t
public;
41 * List of attributes, attribute_entry_t
43 linked_list_t
*attributes
;
46 struct attribute_entry_t
{
47 /** type of attribute */
48 configuration_attribute_type_t type
;
49 /** attribute value */
54 * convert enumerator value from attribute_entry
56 static bool attr_enum_filter(void *null
, attribute_entry_t
**in
,
57 configuration_attribute_type_t
*type
, void* none
, chunk_t
*value
)
60 *value
= (*in
)->value
;
65 * Implementation of attribute_provider_t.create_attribute_enumerator
67 static enumerator_t
* create_attribute_enumerator(private_attr_provider_t
*this,
68 identification_t
*id
, host_t
*vip
)
72 return enumerator_create_filter(
73 this->attributes
->create_enumerator(this->attributes
),
74 (void*)attr_enum_filter
, NULL
, NULL
);
76 return enumerator_create_empty();
80 * Implementation of attr_provider_t.destroy
82 static void destroy(private_attr_provider_t
*this)
84 attribute_entry_t
*entry
;
86 while (this->attributes
->remove_last(this->attributes
,
87 (void**)&entry
) == SUCCESS
)
89 free(entry
->value
.ptr
);
92 this->attributes
->destroy(this->attributes
);
97 * Add an attribute entry to the list
99 static void add_legacy_entry(private_attr_provider_t
*this, char *key
, int nr
,
100 configuration_attribute_type_t type
)
102 attribute_entry_t
*entry
;
106 str
= lib
->settings
->get_str(lib
->settings
, "%s.%s%d", NULL
, hydra
->daemon
,
110 host
= host_create_from_string(str
, 0);
113 entry
= malloc_thing(attribute_entry_t
);
115 if (host
->get_family(host
) == AF_INET6
)
119 case INTERNAL_IP4_DNS
:
120 type
= INTERNAL_IP6_DNS
;
122 case INTERNAL_IP4_NBNS
:
123 type
= INTERNAL_IP6_NBNS
;
130 entry
->value
= chunk_clone(host
->get_address(host
));
132 this->attributes
->insert_last(this->attributes
, entry
);
138 * Key to attribute type mappings, for v4 and v6 attributes
142 configuration_attribute_type_t v4
;
143 configuration_attribute_type_t v6
;
145 {"address", INTERNAL_IP4_ADDRESS
, INTERNAL_IP6_ADDRESS
},
146 {"dns", INTERNAL_IP4_DNS
, INTERNAL_IP6_DNS
},
147 {"nbns", INTERNAL_IP4_NBNS
, INTERNAL_IP6_NBNS
},
148 {"dhcp", INTERNAL_IP4_DHCP
, INTERNAL_IP6_DHCP
},
149 {"netmask", INTERNAL_IP4_NETMASK
, INTERNAL_IP6_NETMASK
},
150 {"server", INTERNAL_IP4_SERVER
, INTERNAL_IP6_SERVER
},
151 {"subnet", INTERNAL_IP4_SUBNET
, INTERNAL_IP6_SUBNET
},
155 * Load (numerical) entries from the plugins.attr namespace
157 static void load_entries(private_attr_provider_t
*this)
159 enumerator_t
*enumerator
, *tokens
;
160 char *key
, *value
, *token
;
162 enumerator
= lib
->settings
->create_key_value_enumerator(lib
->settings
,
163 "%s.plugins.attr", hydra
->daemon
);
164 while (enumerator
->enumerate(enumerator
, &key
, &value
))
166 configuration_attribute_type_t type
;
167 attribute_entry_t
*entry
;
173 tokens
= enumerator_create_token(value
, ",", " ");
174 while (tokens
->enumerate(tokens
, &token
))
176 pos
= strchr(token
, '/');
182 host
= host_create_from_string(token
, 0);
185 DBG1(DBG_CFG
, "invalid host in key %s: %s", key
, token
);
190 for (i
= 0; i
< countof(keys
); i
++)
192 if (streq(key
, keys
[i
].name
))
194 if (host
->get_family(host
) == AF_INET
)
206 DBG1(DBG_CFG
, "mapping attribute type %s failed", key
);
210 entry
= malloc_thing(attribute_entry_t
);
214 entry
->value
= chunk_clone(host
->get_address(host
));
218 if (host
->get_family(host
) == AF_INET
)
219 { /* IPv4 attributes contain a subnet mask */
223 netmask
= htonl((0xFFFFFFFF >> mask
) << mask
);
224 entry
->value
= chunk_cat("cc", host
->get_address(host
),
225 chunk_from_thing(netmask
));
228 { /* IPv6 addresses the prefix only */
229 entry
->value
= chunk_cat("cc", host
->get_address(host
),
230 chunk_from_chars(mask
));
234 this->attributes
->insert_last(this->attributes
, entry
);
236 tokens
->destroy(tokens
);
238 enumerator
->destroy(enumerator
);
244 attr_provider_t
*attr_provider_create(database_t
*db
)
246 private_attr_provider_t
*this;
249 this = malloc_thing(private_attr_provider_t
);
251 this->public.provider
.acquire_address
= (host_t
*(*)(attribute_provider_t
*this, char*, identification_t
*, host_t
*))return_null
;
252 this->public.provider
.release_address
= (bool(*)(attribute_provider_t
*this, char*,host_t
*, identification_t
*))return_false
;
253 this->public.provider
.create_attribute_enumerator
= (enumerator_t
*(*)(attribute_provider_t
*, identification_t
*id
, host_t
*vip
))create_attribute_enumerator
;
254 this->public.destroy
= (void(*)(attr_provider_t
*))destroy
;
256 this->attributes
= linked_list_create();
258 for (i
= 1; i
<= SERVER_MAX
; i
++)
260 add_legacy_entry(this, "dns", i
, INTERNAL_IP4_DNS
);
261 add_legacy_entry(this, "nbns", i
, INTERNAL_IP4_NBNS
);
266 return &this->public;