2008-04-09 12:54:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Martin Willi
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "attribute_manager.h"
|
|
|
|
|
2009-10-13 11:46:27 +00:00
|
|
|
#include <debug.h>
|
2008-04-09 12:54:47 +00:00
|
|
|
#include <utils/linked_list.h>
|
2009-12-08 15:53:01 +00:00
|
|
|
#include <threading/rwlock.h>
|
2008-04-09 12:54:47 +00:00
|
|
|
|
|
|
|
typedef struct private_attribute_manager_t private_attribute_manager_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* private data of attribute_manager
|
|
|
|
*/
|
|
|
|
struct private_attribute_manager_t {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* public functions
|
|
|
|
*/
|
|
|
|
attribute_manager_t public;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-04-09 12:54:47 +00:00
|
|
|
/**
|
|
|
|
* list of registered providers
|
|
|
|
*/
|
|
|
|
linked_list_t *providers;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-04-24 14:13:52 +00:00
|
|
|
/**
|
|
|
|
* list of registered handlers
|
|
|
|
*/
|
|
|
|
linked_list_t *handlers;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-04-09 12:54:47 +00:00
|
|
|
/**
|
2008-11-27 15:22:41 +00:00
|
|
|
* rwlock provider list
|
2008-04-09 12:54:47 +00:00
|
|
|
*/
|
2008-11-27 15:22:41 +00:00
|
|
|
rwlock_t *lock;
|
2008-04-09 12:54:47 +00:00
|
|
|
};
|
|
|
|
|
2009-11-17 13:51:50 +00:00
|
|
|
/**
|
|
|
|
* Data to pass to enumerator filters
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/** server/peer identity */
|
|
|
|
identification_t *id;
|
|
|
|
/** requesting/assigned virtual IP */
|
|
|
|
host_t *vip;
|
|
|
|
} enum_data_t;
|
|
|
|
|
2008-04-09 12:54:47 +00:00
|
|
|
/**
|
|
|
|
* Implementation of attribute_manager_t.acquire_address.
|
|
|
|
*/
|
|
|
|
static host_t* acquire_address(private_attribute_manager_t *this,
|
|
|
|
char *pool, identification_t *id,
|
2009-04-14 10:34:24 +00:00
|
|
|
host_t *requested)
|
2008-04-09 12:54:47 +00:00
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
attribute_provider_t *current;
|
|
|
|
host_t *host = NULL;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-11-27 15:22:41 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
2008-04-09 12:54:47 +00:00
|
|
|
enumerator = this->providers->create_enumerator(this->providers);
|
|
|
|
while (enumerator->enumerate(enumerator, ¤t))
|
|
|
|
{
|
2009-04-14 10:34:24 +00:00
|
|
|
host = current->acquire_address(current, pool, id, requested);
|
2008-04-09 12:54:47 +00:00
|
|
|
if (host)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
2008-11-27 15:22:41 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-03-03 14:56:17 +00:00
|
|
|
if (!host)
|
|
|
|
{
|
2010-03-31 15:28:46 +00:00
|
|
|
DBG1(DBG_LIB, "acquiring address from pool '%s' failed", pool);
|
2009-03-03 14:56:17 +00:00
|
|
|
}
|
2008-04-09 12:54:47 +00:00
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of attribute_manager_t.release_address.
|
|
|
|
*/
|
|
|
|
static void release_address(private_attribute_manager_t *this,
|
2008-12-05 09:40:50 +00:00
|
|
|
char *pool, host_t *address, identification_t *id)
|
2008-04-09 12:54:47 +00:00
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
attribute_provider_t *current;
|
2009-03-03 14:56:17 +00:00
|
|
|
bool found = FALSE;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-11-27 15:22:41 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
2008-04-09 12:54:47 +00:00
|
|
|
enumerator = this->providers->create_enumerator(this->providers);
|
|
|
|
while (enumerator->enumerate(enumerator, ¤t))
|
|
|
|
{
|
2008-12-05 09:40:50 +00:00
|
|
|
if (current->release_address(current, pool, address, id))
|
2008-04-09 12:54:47 +00:00
|
|
|
{
|
2009-03-03 14:56:17 +00:00
|
|
|
found = TRUE;
|
2008-04-09 12:54:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
2008-11-27 15:22:41 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-03-03 14:56:17 +00:00
|
|
|
if (!found)
|
|
|
|
{
|
2010-03-31 15:28:46 +00:00
|
|
|
DBG1(DBG_LIB, "releasing address to pool '%s' failed", pool);
|
2009-03-03 14:56:17 +00:00
|
|
|
}
|
2008-04-09 12:54:47 +00:00
|
|
|
}
|
|
|
|
|
2009-04-24 14:13:52 +00:00
|
|
|
/**
|
2009-11-17 13:51:50 +00:00
|
|
|
* inner enumerator constructor for responder attributes
|
2009-04-24 14:13:52 +00:00
|
|
|
*/
|
2009-11-17 13:51:50 +00:00
|
|
|
static enumerator_t *responder_enum_create(attribute_provider_t *provider,
|
|
|
|
enum_data_t *data)
|
2009-04-24 14:13:52 +00:00
|
|
|
{
|
2009-11-17 13:51:50 +00:00
|
|
|
return provider->create_attribute_enumerator(provider, data->id, data->vip);
|
2009-04-24 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-11-17 13:51:50 +00:00
|
|
|
* Implementation of attribute_manager_t.create_responder_enumerator
|
2009-04-24 14:13:52 +00:00
|
|
|
*/
|
2009-11-17 13:51:50 +00:00
|
|
|
static enumerator_t* create_responder_enumerator(
|
|
|
|
private_attribute_manager_t *this, identification_t *id, host_t *vip)
|
2009-04-24 14:13:52 +00:00
|
|
|
{
|
2009-11-17 13:51:50 +00:00
|
|
|
enum_data_t *data = malloc_thing(enum_data_t);
|
|
|
|
|
|
|
|
data->id = id;
|
|
|
|
data->vip = vip;
|
2009-04-24 14:13:52 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
|
|
|
return enumerator_create_cleaner(
|
|
|
|
enumerator_create_nested(
|
|
|
|
this->providers->create_enumerator(this->providers),
|
2009-11-17 13:51:50 +00:00
|
|
|
(void*)responder_enum_create, data, free),
|
2009-04-24 14:13:52 +00:00
|
|
|
(void*)this->lock->unlock, this->lock);
|
|
|
|
}
|
|
|
|
|
2008-04-09 12:54:47 +00:00
|
|
|
/**
|
|
|
|
* Implementation of attribute_manager_t.add_provider.
|
|
|
|
*/
|
|
|
|
static void add_provider(private_attribute_manager_t *this,
|
|
|
|
attribute_provider_t *provider)
|
|
|
|
{
|
2008-11-27 15:22:41 +00:00
|
|
|
this->lock->write_lock(this->lock);
|
2008-04-09 12:54:47 +00:00
|
|
|
this->providers->insert_last(this->providers, provider);
|
2008-11-27 15:22:41 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2008-04-09 12:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of attribute_manager_t.remove_provider.
|
|
|
|
*/
|
|
|
|
static void remove_provider(private_attribute_manager_t *this,
|
|
|
|
attribute_provider_t *provider)
|
|
|
|
{
|
2008-11-27 15:22:41 +00:00
|
|
|
this->lock->write_lock(this->lock);
|
2008-04-09 12:54:47 +00:00
|
|
|
this->providers->remove(this->providers, provider, NULL);
|
2008-11-27 15:22:41 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2008-04-09 12:54:47 +00:00
|
|
|
}
|
|
|
|
|
2009-04-24 14:13:52 +00:00
|
|
|
/**
|
|
|
|
* Implementation of attribute_manager_t.handle
|
|
|
|
*/
|
|
|
|
static attribute_handler_t* handle(private_attribute_manager_t *this,
|
2009-11-17 13:51:50 +00:00
|
|
|
identification_t *server, attribute_handler_t *handler,
|
|
|
|
configuration_attribute_type_t type, chunk_t data)
|
2009-04-24 14:13:52 +00:00
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
attribute_handler_t *current, *handled = NULL;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-04-24 14:13:52 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
2009-11-17 13:51:50 +00:00
|
|
|
|
|
|
|
/* try to find the passed handler */
|
2009-04-24 14:13:52 +00:00
|
|
|
enumerator = this->handlers->create_enumerator(this->handlers);
|
|
|
|
while (enumerator->enumerate(enumerator, ¤t))
|
|
|
|
{
|
2009-11-17 13:51:50 +00:00
|
|
|
if (current == handler && current->handle(current, server, type, data))
|
2009-04-24 14:13:52 +00:00
|
|
|
{
|
|
|
|
handled = current;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
2009-11-17 13:51:50 +00:00
|
|
|
if (!handled)
|
|
|
|
{ /* handler requesting this attribute not found, try any other */
|
|
|
|
enumerator = this->handlers->create_enumerator(this->handlers);
|
|
|
|
while (enumerator->enumerate(enumerator, ¤t))
|
|
|
|
{
|
|
|
|
if (current->handle(current, server, type, data))
|
|
|
|
{
|
|
|
|
handled = current;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
}
|
2009-04-24 14:13:52 +00:00
|
|
|
this->lock->unlock(this->lock);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-04-24 14:13:52 +00:00
|
|
|
if (!handled)
|
|
|
|
{
|
2010-03-31 15:28:46 +00:00
|
|
|
DBG1(DBG_LIB, "handling %N attribute failed",
|
2009-04-24 14:13:52 +00:00
|
|
|
configuration_attribute_type_names, type);
|
|
|
|
}
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of attribute_manager_t.release
|
|
|
|
*/
|
|
|
|
static void release(private_attribute_manager_t *this,
|
2009-10-13 11:46:27 +00:00
|
|
|
attribute_handler_t *handler,
|
|
|
|
identification_t *server,
|
|
|
|
configuration_attribute_type_t type, chunk_t data)
|
2009-04-24 14:13:52 +00:00
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
attribute_handler_t *current;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-04-24 14:13:52 +00:00
|
|
|
this->lock->read_lock(this->lock);
|
|
|
|
enumerator = this->handlers->create_enumerator(this->handlers);
|
|
|
|
while (enumerator->enumerate(enumerator, ¤t))
|
|
|
|
{
|
|
|
|
if (current == handler)
|
|
|
|
{
|
2009-10-13 11:46:27 +00:00
|
|
|
current->release(current, server, type, data);
|
2009-04-24 14:13:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
this->lock->unlock(this->lock);
|
|
|
|
}
|
|
|
|
|
2009-11-17 13:51:50 +00:00
|
|
|
/**
|
2009-11-17 16:51:30 +00:00
|
|
|
* Enumerator implementation to enumerate nested initiator attributes
|
2009-11-17 13:51:50 +00:00
|
|
|
*/
|
2009-11-17 16:51:30 +00:00
|
|
|
typedef struct {
|
|
|
|
/** implements enumerator_t */
|
|
|
|
enumerator_t public;
|
|
|
|
/** back ref */
|
|
|
|
private_attribute_manager_t *this;
|
|
|
|
/** currently processing handler */
|
|
|
|
attribute_handler_t *handler;
|
|
|
|
/** outer enumerator over handlers */
|
|
|
|
enumerator_t *outer;
|
|
|
|
/** inner enumerator over current handlers attributes */
|
|
|
|
enumerator_t *inner;
|
|
|
|
/** server ID we want attributes for */
|
|
|
|
identification_t *id;
|
|
|
|
/** virtual IP we are requesting along with attriubutes */
|
|
|
|
host_t *vip;
|
|
|
|
} initiator_enumerator_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enumerator implementation for initiator attributes
|
|
|
|
*/
|
|
|
|
static bool initiator_enumerate(initiator_enumerator_t *this,
|
|
|
|
attribute_handler_t **handler,
|
|
|
|
configuration_attribute_type_t *type,
|
|
|
|
chunk_t *value)
|
2009-11-17 13:51:50 +00:00
|
|
|
{
|
2009-11-17 16:51:30 +00:00
|
|
|
/* enumerate inner attributes using outer handler enumerator */
|
|
|
|
while (!this->inner || !this->inner->enumerate(this->inner, type, value))
|
|
|
|
{
|
|
|
|
if (!this->outer->enumerate(this->outer, &this->handler))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
DESTROY_IF(this->inner);
|
|
|
|
this->inner = this->handler->create_attribute_enumerator(this->handler,
|
|
|
|
this->id, this->vip);
|
|
|
|
}
|
|
|
|
/* inject the handler as additional attribute */
|
|
|
|
*handler = this->handler;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup function of initiator attribute enumerator
|
|
|
|
*/
|
|
|
|
static void initiator_destroy(initiator_enumerator_t *this)
|
|
|
|
{
|
|
|
|
this->this->lock->unlock(this->this->lock);
|
|
|
|
this->outer->destroy(this->outer);
|
|
|
|
DESTROY_IF(this->inner);
|
|
|
|
free(this);
|
2009-11-17 13:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of attribute_manager_t.create_initiator_enumerator
|
|
|
|
*/
|
|
|
|
static enumerator_t* create_initiator_enumerator(
|
|
|
|
private_attribute_manager_t *this, identification_t *id, host_t *vip)
|
|
|
|
{
|
2009-11-17 16:51:30 +00:00
|
|
|
initiator_enumerator_t *enumerator = malloc_thing(initiator_enumerator_t);
|
2009-11-17 13:51:50 +00:00
|
|
|
|
|
|
|
this->lock->read_lock(this->lock);
|
2009-11-17 16:51:30 +00:00
|
|
|
enumerator->public.enumerate = (void*)initiator_enumerate;
|
|
|
|
enumerator->public.destroy = (void*)initiator_destroy;
|
|
|
|
enumerator->this = this;
|
|
|
|
enumerator->id = id;
|
|
|
|
enumerator->vip = vip;
|
|
|
|
enumerator->outer = this->handlers->create_enumerator(this->handlers);
|
|
|
|
enumerator->inner = NULL;
|
|
|
|
enumerator->handler = NULL;
|
|
|
|
|
|
|
|
return &enumerator->public;
|
2009-11-17 13:51:50 +00:00
|
|
|
}
|
|
|
|
|
2009-04-24 14:13:52 +00:00
|
|
|
/**
|
|
|
|
* Implementation of attribute_manager_t.add_handler
|
|
|
|
*/
|
|
|
|
static void add_handler(private_attribute_manager_t *this,
|
|
|
|
attribute_handler_t *handler)
|
|
|
|
{
|
|
|
|
this->lock->write_lock(this->lock);
|
|
|
|
this->handlers->insert_last(this->handlers, handler);
|
|
|
|
this->lock->unlock(this->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of attribute_manager_t.remove_handler
|
|
|
|
*/
|
|
|
|
static void remove_handler(private_attribute_manager_t *this,
|
|
|
|
attribute_handler_t *handler)
|
|
|
|
{
|
|
|
|
this->lock->write_lock(this->lock);
|
|
|
|
this->handlers->remove(this->handlers, handler, NULL);
|
|
|
|
this->lock->unlock(this->lock);
|
|
|
|
}
|
|
|
|
|
2008-04-09 12:54:47 +00:00
|
|
|
/**
|
|
|
|
* Implementation of attribute_manager_t.destroy
|
|
|
|
*/
|
|
|
|
static void destroy(private_attribute_manager_t *this)
|
|
|
|
{
|
|
|
|
this->providers->destroy(this->providers);
|
2009-04-24 14:13:52 +00:00
|
|
|
this->handlers->destroy(this->handlers);
|
2008-11-27 15:22:41 +00:00
|
|
|
this->lock->destroy(this->lock);
|
2008-04-09 12:54:47 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* see header file
|
|
|
|
*/
|
|
|
|
attribute_manager_t *attribute_manager_create()
|
|
|
|
{
|
|
|
|
private_attribute_manager_t *this = malloc_thing(private_attribute_manager_t);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-04-14 10:34:24 +00:00
|
|
|
this->public.acquire_address = (host_t*(*)(attribute_manager_t*, char*, identification_t*,host_t*))acquire_address;
|
2008-12-05 09:40:50 +00:00
|
|
|
this->public.release_address = (void(*)(attribute_manager_t*, char *, host_t*, identification_t*))release_address;
|
2009-11-17 13:51:50 +00:00
|
|
|
this->public.create_responder_enumerator = (enumerator_t*(*)(attribute_manager_t*, identification_t*, host_t*))create_responder_enumerator;
|
2008-04-09 12:54:47 +00:00
|
|
|
this->public.add_provider = (void(*)(attribute_manager_t*, attribute_provider_t *provider))add_provider;
|
|
|
|
this->public.remove_provider = (void(*)(attribute_manager_t*, attribute_provider_t *provider))remove_provider;
|
2009-11-17 13:51:50 +00:00
|
|
|
this->public.handle = (attribute_handler_t*(*)(attribute_manager_t*,identification_t*, attribute_handler_t*, configuration_attribute_type_t, chunk_t))handle;
|
2009-10-13 11:46:27 +00:00
|
|
|
this->public.release = (void(*)(attribute_manager_t*, attribute_handler_t*, identification_t*, configuration_attribute_type_t, chunk_t))release;
|
2009-11-17 13:51:50 +00:00
|
|
|
this->public.create_initiator_enumerator = (enumerator_t*(*)(attribute_manager_t*, identification_t*, host_t*))create_initiator_enumerator;
|
2009-10-13 11:46:27 +00:00
|
|
|
this->public.add_handler = (void(*)(attribute_manager_t*, attribute_handler_t*))add_handler;
|
|
|
|
this->public.remove_handler = (void(*)(attribute_manager_t*, attribute_handler_t*))remove_handler;
|
2008-04-09 12:54:47 +00:00
|
|
|
this->public.destroy = (void(*)(attribute_manager_t*))destroy;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-04-09 12:54:47 +00:00
|
|
|
this->providers = linked_list_create();
|
2009-04-24 14:13:52 +00:00
|
|
|
this->handlers = linked_list_create();
|
2009-08-14 11:30:59 +00:00
|
|
|
this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-04-09 12:54:47 +00:00
|
|
|
return &this->public;
|
|
|
|
}
|
|
|
|
|