2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "tnc_pdp_connections.h"
18 #include <utils/linked_list.h>
21 typedef struct private_tnc_pdp_connections_t private_tnc_pdp_connections_t
;
22 typedef struct entry_t entry_t
;
25 * Private data of tnc_pdp_connections_t
27 struct private_tnc_pdp_connections_t
{
30 * Implements tnc_pdp_connections_t interface
32 tnc_pdp_connections_t
public;
35 * List of TNC PEP RADIUS Connections
41 * Data entry for a TNC PEP RADIUS connection
46 * NAS identifier of PEP
51 * User name of TNC Client
62 * Free the memory allocated to a data entry
64 static void free_entry(entry_t
*this)
66 this->method
->destroy(this->method
);
67 free(this->nas_id
.ptr
);
68 free(this->user_name
.ptr
);
73 * Find a matching data entry
75 static bool equals_entry( entry_t
*this, chunk_t nas_id
, chunk_t user_name
)
77 bool no_nas_id
= !this->nas_id
.ptr
&& !nas_id
.ptr
;
79 return (chunk_equals(this->nas_id
, nas_id
) || no_nas_id
) &&
80 chunk_equals(this->user_name
, user_name
);
84 * Find a matching data entry
86 static void dbg_nas_user(chunk_t nas_id
, chunk_t user_name
, bool not, char *op
)
90 DBG1(DBG_CFG
, "%s RADIUS connection for user '%.*s' NAS '%.*s'",
91 not ?
"could not find" : op
, user_name
.len
, user_name
.ptr
,
92 nas_id
.len
, nas_id
.ptr
);
96 DBG1(DBG_CFG
, "%s RADIUS connection for user '%.*s'",
97 not ?
"could not find" : op
, user_name
.len
, user_name
.ptr
);
101 METHOD(tnc_pdp_connections_t
, add
, void,
102 private_tnc_pdp_connections_t
*this, chunk_t nas_id
, chunk_t user_name
,
103 eap_method_t
*method
)
105 enumerator_t
*enumerator
;
109 enumerator
= this->list
->create_enumerator(this->list
);
110 while (enumerator
->enumerate(enumerator
, &entry
))
112 if (equals_entry(entry
, nas_id
, user_name
))
115 entry
->method
->destroy(entry
->method
);
116 DBG1(DBG_CFG
, "removed stale RADIUS connection");
117 entry
->method
= method
;
121 enumerator
->destroy(enumerator
);
125 entry
= malloc_thing(entry_t
);
126 entry
->nas_id
= chunk_clone(nas_id
);
127 entry
->user_name
= chunk_clone(user_name
);
128 entry
->method
= method
;
129 this->list
->insert_last(this->list
, entry
);
131 dbg_nas_user(nas_id
, user_name
, FALSE
, "created");
134 METHOD(tnc_pdp_connections_t
, remove_
, void,
135 private_tnc_pdp_connections_t
*this, chunk_t nas_id
, chunk_t user_name
)
137 enumerator_t
*enumerator
;
140 enumerator
= this->list
->create_enumerator(this->list
);
141 while (enumerator
->enumerate(enumerator
, &entry
))
143 if (equals_entry(entry
, nas_id
, user_name
))
146 this->list
->remove_at(this->list
, enumerator
);
147 dbg_nas_user(nas_id
, user_name
, FALSE
, "removed");
151 enumerator
->destroy(enumerator
);
154 METHOD(tnc_pdp_connections_t
, get_method
, eap_method_t
*,
155 private_tnc_pdp_connections_t
*this, chunk_t nas_id
, chunk_t user_name
)
157 enumerator_t
*enumerator
;
159 eap_method_t
*found
= NULL
;
161 enumerator
= this->list
->create_enumerator(this->list
);
162 while (enumerator
->enumerate(enumerator
, &entry
))
164 if (equals_entry(entry
, nas_id
, user_name
))
166 found
= entry
->method
;
170 enumerator
->destroy(enumerator
);
172 dbg_nas_user(nas_id
, user_name
, !found
, "found");
176 METHOD(tnc_pdp_connections_t
, destroy
, void,
177 private_tnc_pdp_connections_t
*this)
179 this->list
->destroy_function(this->list
, (void*)free_entry
);
186 tnc_pdp_connections_t
*tnc_pdp_connections_create(void)
188 private_tnc_pdp_connections_t
*this;
194 .get_method
= _get_method
,
197 .list
= linked_list_create(),
200 return &this->public;