2 * mem.c Memory allocation, deallocation stuff.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000,2001 The FreeRADIUS server project
21 * Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
27 * Allocate a new EAP_PACKET
29 EAP_PACKET *eap_packet_alloc(void)
33 rp = rad_malloc(sizeof(EAP_PACKET));
34 memset(rp, 0, sizeof(EAP_PACKET));
41 void eap_packet_free(EAP_PACKET **eap_packet_ptr)
43 EAP_PACKET *eap_packet;
45 if (!eap_packet_ptr) return;
46 eap_packet = *eap_packet_ptr;
47 if (!eap_packet) return;
49 if (eap_packet->type.data) {
51 * This is just a pointer in the packet
52 * so we do not free it but we NULL it
53 free(eap_packet->type.data);
55 eap_packet->type.data = NULL;
58 if (eap_packet->packet) {
59 free(eap_packet->packet);
60 eap_packet->packet = NULL;
65 *eap_packet_ptr = NULL;
69 * Allocate a new EAP_PACKET
71 EAP_DS *eap_ds_alloc(void)
75 eap_ds = rad_malloc(sizeof(EAP_DS));
76 memset(eap_ds, 0, sizeof(EAP_DS));
77 if ((eap_ds->response = eap_packet_alloc()) == NULL) {
81 if ((eap_ds->request = eap_packet_alloc()) == NULL) {
89 void eap_ds_free(EAP_DS **eap_ds_p)
93 if (!eap_ds_p) return;
97 if (eap_ds->response) eap_packet_free(&(eap_ds->response));
98 if (eap_ds->request) eap_packet_free(&(eap_ds->request));
105 * Allocate a new EAP_HANDLER
107 EAP_HANDLER *eap_handler_alloc(void)
109 EAP_HANDLER *handler;
111 if ((handler = malloc(sizeof(EAP_HANDLER))) == NULL) {
112 radlog(L_ERR, "out of memory");
115 handler = rad_malloc(sizeof(EAP_HANDLER));
119 void eap_handler_free(EAP_HANDLER **handler_p)
121 EAP_HANDLER *handler;
123 if ((handler_p == NULL) || (*handler_p == NULL))
125 handler = *handler_p;
132 if (handler->identity) {
133 free(handler->identity);
134 handler->identity = NULL;
137 if (handler->username) pairfree(&(handler->username));
138 if (handler->configured) pairfree(&(handler->configured));
140 if (handler->prev_eapds) eap_ds_free(&(handler->prev_eapds));
141 if (handler->eap_ds) eap_ds_free(&(handler->eap_ds));
143 if ((handler->opaque) && (handler->free_opaque))
144 handler->free_opaque(&handler->opaque);
145 else if ((handler->opaque) && (handler->free_opaque == NULL))
146 radlog(L_ERR, "Possible memory leak ...");
148 handler->opaque = NULL;
149 handler->free_opaque = NULL;
150 handler->next = NULL;
155 void eaptype_freelist(EAP_TYPES **i)
162 if(c->type->detach) (c->type->detach)(&(c->type_stuff));
163 if (c->handle) lt_dlclose(c->handle);
170 void eaplist_free(EAP_HANDLER **list)
172 EAP_HANDLER *node, *next;
178 eap_handler_free(&node);
185 int eaplist_add(EAP_HANDLER **list, EAP_HANDLER *node)
189 if (node == NULL) return 0;
192 while (*last) last = &((*last)->next);
194 node->timestamp = time(NULL);
203 * List should contain only recent packets with life < x seconds.
205 void eaplist_clean(EAP_HANDLER **first, time_t limit)
208 EAP_HANDLER *node, *next;
209 EAP_HANDLER **last = first;
213 for (node = *first; node; node = next) {
215 if ((now - node->timestamp) > limit) {
216 radlog(L_INFO, "rlm_eap: list_clean deleted one item");
218 eap_handler_free(&node);
220 last = &(node->next);
226 * If the present EAP-Response is a reply to the previous
227 * EAP-Request sent by us, then return the EAP_HANDLER
228 * only after releasing from the eaplist
229 * Also since we fill the eap_ds with the present EAP-Response
230 * we got to free the prev_eapds & move the eap_ds to prev_eapds
232 EAP_HANDLER *eaplist_isreply(EAP_HANDLER **first, unsigned char id[])
234 EAP_HANDLER *node, *next, *ret = NULL;
235 EAP_HANDLER **last = first;
237 for (node = *first; node; node = next) {
239 if (memcmp(node->id, id, id[0]) == 0) {
240 radlog(L_INFO, "rlm_eap: Request found, released from the list");
241 /* detach the node from the list */
245 /* clean up the unwanted stuff before returning */
246 eap_ds_free(&(node->prev_eapds));
247 node->prev_eapds = node->eap_ds;
253 last = &(node->next);
258 radlog(L_INFO, "rlm_eap: Request not found in the list");
263 EAP_HANDLER *eaplist_findhandler(EAP_HANDLER *list, unsigned char id[])
270 * Match is identified by the same IDs
272 if (memcmp(node->id, id, id[0]) == 0) {
273 radlog(L_INFO, "rlm_eap: EAP Handler found in the list ");