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>
28 * Allocate a new EAP_PACKET
30 EAP_PACKET *eap_packet_alloc()
34 if ((rp = malloc(sizeof(EAP_PACKET))) == NULL) {
35 radlog(L_ERR, "out of memory");
38 memset(rp, 0, sizeof(EAP_PACKET));
45 void eap_packet_free(EAP_PACKET **eap_packet_ptr)
47 EAP_PACKET *eap_packet;
49 if (!eap_packet_ptr) return;
50 eap_packet = *eap_packet_ptr;
51 if (!eap_packet) return;
53 if (eap_packet->typedata) free(eap_packet->typedata);
54 if (eap_packet->rad_vps) pairfree(&(eap_packet->rad_vps));
58 *eap_packet_ptr = NULL;
62 * Allocate a new EAP_PACKET
64 EAP_DS *eap_ds_alloc()
68 if ((eap_ds = malloc(sizeof(EAP_DS))) == NULL) {
69 radlog(L_ERR, "out of memory");
72 memset(eap_ds, 0, sizeof(EAP_DS));
73 if ((eap_ds->response = eap_packet_alloc(sizeof(EAP_PACKET))) == NULL) {
77 if ((eap_ds->request = eap_packet_alloc(sizeof(EAP_PACKET))) == NULL) {
85 void eap_ds_free(EAP_DS **eap_ds_p)
89 if (!eap_ds_p) return;
93 if (eap_ds->response) eap_packet_free(&(eap_ds->response));
94 if (eap_ds->request) eap_packet_free(&(eap_ds->request));
96 if (eap_ds->username) pairfree(&(eap_ds->username));
97 if (eap_ds->password) pairfree(&(eap_ds->password));
103 void free_type_list(EAP_TYPES **i)
110 if(c->type->detach) (c->type->detach)(&(c->type_stuff));
111 if (c->handle) lt_dlclose(c->handle);
118 void node_free(EAP_LIST **node)
120 if (node == NULL) return;
121 if (*node == NULL) return;
122 if ((*node)->eap_ds) eap_ds_free(&((*node)->eap_ds));
124 (*node)->next = NULL;
129 void list_free(EAP_LIST **list)
131 EAP_LIST *node, *next;
145 int list_add(EAP_LIST **list, EAP_DS *eap_ds)
147 EAP_LIST *node, **last;
149 if (!eap_ds) return 0;
152 while (*last) *last = (*last)->next;
154 eap_ds->timestamp = time(NULL);
155 eap_ds->finished = 1;
157 node = malloc(sizeof(EAP_LIST));
159 radlog(L_ERR, "rlm_eap: out of memory");
164 node->eap_ds = eap_ds;
171 * List should contain only recent packets with life < X seconds.
173 void list_clean(EAP_LIST **first, time_t limit)
176 EAP_LIST *node, *next;
177 EAP_LIST **last = first;
181 for (node = *first; node; node = next) {
183 if ((now - node->eap_ds->timestamp) > limit) {
184 radlog(L_INFO, "rlm_eap: list_clean deleted one item");
188 last = &(node->next);
193 void remove_item(EAP_LIST **first, EAP_LIST *item)
196 EAP_LIST *node, *next;
197 EAP_LIST **last = first;
201 for (node = *first; node; node = next) {
204 radlog(L_INFO, "rlm_eap: remove_item deleted one item");
208 last = &(node->next);