2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
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 "dhcp_transaction.h"
18 #include <utils/linked_list.h>
20 typedef struct private_dhcp_transaction_t private_dhcp_transaction_t
;
23 * Private data of an dhcp_transaction_t object.
25 struct private_dhcp_transaction_t
{
28 * Public dhcp_transaction_t interface.
30 dhcp_transaction_t
public;
40 identification_t
*identity
;
43 * received DHCP address
48 * discovered DHCP server address
53 * List of added attributes, as attribute_entry_t
55 linked_list_t
*attributes
;
59 * Entry for an added attribute
62 configuration_attribute_type_t type
;
66 METHOD(dhcp_transaction_t
, get_id
, u_int32_t
,
67 private_dhcp_transaction_t
*this)
72 METHOD(dhcp_transaction_t
, get_identity
, identification_t
*,
73 private_dhcp_transaction_t
*this)
75 return this->identity
;
78 METHOD(dhcp_transaction_t
, set_address
, void,
79 private_dhcp_transaction_t
*this, host_t
*address
)
81 DESTROY_IF(this->address
);
82 this->address
= address
;
85 METHOD(dhcp_transaction_t
, get_address
, host_t
*,
86 private_dhcp_transaction_t
*this)
91 METHOD(dhcp_transaction_t
, set_server
, void,
92 private_dhcp_transaction_t
*this, host_t
*server
)
94 DESTROY_IF(this->server
);
95 this->server
= server
;
98 METHOD(dhcp_transaction_t
, get_server
, host_t
*,
99 private_dhcp_transaction_t
*this)
104 METHOD(dhcp_transaction_t
, add_attribute
, void,
105 private_dhcp_transaction_t
*this, configuration_attribute_type_t type
,
108 attribute_entry_t
*entry
;
112 .data
= chunk_clone(data
),
114 this->attributes
->insert_last(this->attributes
, entry
);
118 * Filter function to map entries to type/data
120 static bool attribute_filter(void *null
, attribute_entry_t
**entry
,
121 configuration_attribute_type_t
*type
,
122 void **dummy
, chunk_t
*data
)
124 *type
= (*entry
)->type
;
125 *data
= (*entry
)->data
;
129 METHOD(dhcp_transaction_t
, create_attribute_enumerator
, enumerator_t
*,
130 private_dhcp_transaction_t
*this)
132 return enumerator_create_filter(
133 this->attributes
->create_enumerator(this->attributes
),
134 (void*)attribute_filter
, NULL
, NULL
);
138 * Clean up an attribute entry
140 static void attribute_entry_destroy(attribute_entry_t
*entry
)
142 free(entry
->data
.ptr
);
146 METHOD(dhcp_transaction_t
, destroy
, void,
147 private_dhcp_transaction_t
*this)
149 this->identity
->destroy(this->identity
);
150 DESTROY_IF(this->address
);
151 DESTROY_IF(this->server
);
152 this->attributes
->destroy_function(this->attributes
,
153 (void*)attribute_entry_destroy
);
160 dhcp_transaction_t
*dhcp_transaction_create(u_int32_t id
,
161 identification_t
*identity
)
163 private_dhcp_transaction_t
*this;
168 .get_identity
= _get_identity
,
169 .set_address
= _set_address
,
170 .get_address
= _get_address
,
171 .set_server
= _set_server
,
172 .get_server
= _get_server
,
173 .add_attribute
= _add_attribute
,
174 .create_attribute_enumerator
= _create_attribute_enumerator
,
178 .identity
= identity
->clone(identity
),
179 .attributes
= linked_list_create(),
182 return &this->public;