summaryrefslogtreecommitdiff
path: root/src/charon/sa/authenticators/eap/eap_manager.c
blob: 44d84156c74353a8d4c3ffa0471633a66cc7d053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * 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.
 *
 * $Id: eap_manager.c 3589 2008-03-13 14:14:44Z martin $
 */

#include "eap_manager.h"

#include <pthread.h>

#include <utils/linked_list.h>

typedef struct private_eap_manager_t private_eap_manager_t;
typedef struct eap_entry_t eap_entry_t;

/**
 * EAP constructor entry
 */
struct eap_entry_t {
	
	/**
	 * EAP method type, vendor specific if vendor is set
	 */
	eap_type_t type;
	
	/**
	 * vendor ID, 0 for default EAP methods
	 */
	u_int32_t vendor;
	
	/**
	 * Role of the method returned by the constructor, EAP_SERVER or EAP_PEER
	 */
	eap_role_t role;
	
	/**
	 * constructor function to create instance
	 */
	eap_constructor_t constructor;
};

/**
 * private data of eap_manager
 */
struct private_eap_manager_t {

	/**
	 * public functions
	 */
	eap_manager_t public;
	
	/**
	 * list of eap_entry_t's
	 */
	linked_list_t *methods;
	
	/**
	 * mutex to lock methods
	 */
	pthread_mutex_t mutex;
};

/**
 * Implementation of eap_manager_t.add_method.
 */
static void add_method(private_eap_manager_t *this, eap_type_t type,
					   u_int32_t vendor, eap_role_t role,
					   eap_constructor_t constructor)
{
	eap_entry_t *entry = malloc_thing(eap_entry_t);
	
	entry->type = type;
	entry->vendor = vendor;
	entry->role = role;
	entry->constructor = constructor;

	pthread_mutex_lock(&this->mutex);
	this->methods->insert_last(this->methods, entry);
	pthread_mutex_unlock(&this->mutex);
}

/**
 * Implementation of eap_manager_t.remove_method.
 */
static void remove_method(private_eap_manager_t *this, eap_constructor_t constructor)
{
	enumerator_t *enumerator;
	eap_entry_t *entry;
	
	pthread_mutex_lock(&this->mutex);
	enumerator = this->methods->create_enumerator(this->methods);
	while (enumerator->enumerate(enumerator, &entry))
	{
		if (constructor == entry->constructor)
		{
			this->methods->remove_at(this->methods, enumerator);
			free(entry);
		}
	}
	enumerator->destroy(enumerator);
	pthread_mutex_unlock(&this->mutex);
}

/**
 * Implementation of eap_manager_t.create_instance.
 */
static eap_method_t* create_instance(private_eap_manager_t *this,
									 eap_type_t type, u_int32_t vendor,
									 eap_role_t role, identification_t *server,
									 identification_t *peer)
{
	enumerator_t *enumerator;
	eap_entry_t *entry;
	eap_method_t *method = NULL;
	
	pthread_mutex_lock(&this->mutex);
	enumerator = this->methods->create_enumerator(this->methods);
	while (enumerator->enumerate(enumerator, &entry))
	{
		if (type == entry->type && vendor == entry->vendor &&
			role == entry->role)
		{
			method = entry->constructor(server, peer);
			if (method)
			{
				break;
			}
		}
	}
	enumerator->destroy(enumerator);
	pthread_mutex_unlock(&this->mutex);
	return method;
}

/**
 * Implementation of 2008_t.destroy
 */
static void destroy(private_eap_manager_t *this)
{
	this->methods->destroy_function(this->methods, free);
	free(this);
}

/*
 * see header file
 */
eap_manager_t *eap_manager_create()
{
	private_eap_manager_t *this = malloc_thing(private_eap_manager_t);
	
	this->public.add_method = (void(*)(eap_manager_t*, eap_type_t type, u_int32_t vendor, eap_role_t role, eap_constructor_t constructor))add_method;
	this->public.remove_method = (void(*)(eap_manager_t*, eap_constructor_t constructor))remove_method;
	this->public.create_instance = (eap_method_t*(*)(eap_manager_t*, eap_type_t type, u_int32_t vendor, eap_role_t role, identification_t*,identification_t*))create_instance;
	this->public.destroy = (void(*)(eap_manager_t*))destroy;
	
	this->methods = linked_list_create();
	pthread_mutex_init(&this->mutex, NULL);
	
	return &this->public;
}