Moved data structures to new collections subfolder
[strongswan.git] / src / libcharon / plugins / dhcp / dhcp_transaction.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "dhcp_transaction.h"
17
18 #include <collections/linked_list.h>
19
20 typedef struct private_dhcp_transaction_t private_dhcp_transaction_t;
21
22 /**
23 * Private data of an dhcp_transaction_t object.
24 */
25 struct private_dhcp_transaction_t {
26
27 /**
28 * Public dhcp_transaction_t interface.
29 */
30 dhcp_transaction_t public;
31
32 /**
33 * DHCP transaction ID
34 */
35 u_int32_t id;
36
37 /**
38 * Peer identity
39 */
40 identification_t *identity;
41
42 /**
43 * received DHCP address
44 */
45 host_t *address;
46
47 /**
48 * discovered DHCP server address
49 */
50 host_t *server;
51
52 /**
53 * List of added attributes, as attribute_entry_t
54 */
55 linked_list_t *attributes;
56 };
57
58 /**
59 * Entry for an added attribute
60 */
61 typedef struct {
62 configuration_attribute_type_t type;
63 chunk_t data;
64 } attribute_entry_t;
65
66 METHOD(dhcp_transaction_t, get_id, u_int32_t,
67 private_dhcp_transaction_t *this)
68 {
69 return this->id;
70 }
71
72 METHOD(dhcp_transaction_t, get_identity, identification_t*,
73 private_dhcp_transaction_t *this)
74 {
75 return this->identity;
76 }
77
78 METHOD(dhcp_transaction_t, set_address, void,
79 private_dhcp_transaction_t *this, host_t *address)
80 {
81 DESTROY_IF(this->address);
82 this->address = address;
83 }
84
85 METHOD(dhcp_transaction_t, get_address, host_t*,
86 private_dhcp_transaction_t *this)
87 {
88 return this->address;
89 }
90
91 METHOD(dhcp_transaction_t, set_server, void,
92 private_dhcp_transaction_t *this, host_t *server)
93 {
94 DESTROY_IF(this->server);
95 this->server = server;
96 }
97
98 METHOD(dhcp_transaction_t, get_server, host_t*,
99 private_dhcp_transaction_t *this)
100 {
101 return this->server;
102 }
103
104 METHOD(dhcp_transaction_t, add_attribute, void,
105 private_dhcp_transaction_t *this, configuration_attribute_type_t type,
106 chunk_t data)
107 {
108 attribute_entry_t *entry;
109
110 INIT(entry,
111 .type = type,
112 .data = chunk_clone(data),
113 );
114 this->attributes->insert_last(this->attributes, entry);
115 }
116
117 /**
118 * Filter function to map entries to type/data
119 */
120 static bool attribute_filter(void *null, attribute_entry_t **entry,
121 configuration_attribute_type_t *type,
122 void **dummy, chunk_t *data)
123 {
124 *type = (*entry)->type;
125 *data = (*entry)->data;
126 return TRUE;
127 }
128
129 METHOD(dhcp_transaction_t, create_attribute_enumerator, enumerator_t*,
130 private_dhcp_transaction_t *this)
131 {
132 return enumerator_create_filter(
133 this->attributes->create_enumerator(this->attributes),
134 (void*)attribute_filter, NULL, NULL);
135 }
136
137 /**
138 * Clean up an attribute entry
139 */
140 static void attribute_entry_destroy(attribute_entry_t *entry)
141 {
142 free(entry->data.ptr);
143 free(entry);
144 }
145
146 METHOD(dhcp_transaction_t, destroy, void,
147 private_dhcp_transaction_t *this)
148 {
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);
154 free(this);
155 }
156
157 /**
158 * See header
159 */
160 dhcp_transaction_t *dhcp_transaction_create(u_int32_t id,
161 identification_t *identity)
162 {
163 private_dhcp_transaction_t *this;
164
165 INIT(this,
166 .public = {
167 .get_id = _get_id,
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,
175 .destroy = _destroy,
176 },
177 .id = id,
178 .identity = identity->clone(identity),
179 .attributes = linked_list_create(),
180 );
181
182 return &this->public;
183 }
184