summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/dhcp/dhcp_transaction.c
blob: 87711799c8a5625cc9482a9ad361c1b21be4282b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (C) 2010 Martin Willi
 * Copyright (C) 2010 revosec AG
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "dhcp_transaction.h"

#include <collections/linked_list.h>

typedef struct private_dhcp_transaction_t private_dhcp_transaction_t;

/**
 * Private data of an dhcp_transaction_t object.
 */
struct private_dhcp_transaction_t {

	/**
	 * Public dhcp_transaction_t interface.
	 */
	dhcp_transaction_t public;

	/**
	 * DHCP transaction ID
	 */
	uint32_t id;

	/**
	 * Peer identity
	 */
	identification_t *identity;

	/**
	 * received DHCP address
	 */
	host_t *address;

	/**
	 * discovered DHCP server address
	 */
	host_t *server;

	/**
	 * List of added attributes, as attribute_entry_t
	 */
	linked_list_t *attributes;
};

/**
 * Entry for an added attribute
 */
typedef struct {
	configuration_attribute_type_t type;
	chunk_t data;
} attribute_entry_t;

METHOD(dhcp_transaction_t, get_id, uint32_t,
	private_dhcp_transaction_t *this)
{
	return this->id;
}

METHOD(dhcp_transaction_t, get_identity, identification_t*,
	private_dhcp_transaction_t *this)
{
	return this->identity;
}

METHOD(dhcp_transaction_t, set_address, void,
	private_dhcp_transaction_t *this, host_t *address)
{
	DESTROY_IF(this->address);
	this->address = address;
}

METHOD(dhcp_transaction_t, get_address, host_t*,
	private_dhcp_transaction_t *this)
{
	return this->address;
}

METHOD(dhcp_transaction_t, set_server, void,
	private_dhcp_transaction_t *this, host_t *server)
{
	DESTROY_IF(this->server);
	this->server = server;
}

METHOD(dhcp_transaction_t, get_server, host_t*,
	private_dhcp_transaction_t *this)
{
	return this->server;
}

METHOD(dhcp_transaction_t, add_attribute, void,
	private_dhcp_transaction_t *this, configuration_attribute_type_t type,
	chunk_t data)
{
	attribute_entry_t *entry;

	INIT(entry,
		.type = type,
		.data = chunk_clone(data),
	);
	this->attributes->insert_last(this->attributes, entry);
}

CALLBACK(attribute_filter, bool,
	void *null, enumerator_t *orig, va_list args)
{
	configuration_attribute_type_t *type;
	attribute_entry_t *entry;
	chunk_t *data;

	VA_ARGS_VGET(args, type, data);

	if (orig->enumerate(orig, &entry))
	{
		*type = entry->type;
		*data = entry->data;
		return TRUE;
	}
	return FALSE;
}

METHOD(dhcp_transaction_t, create_attribute_enumerator, enumerator_t*,
	private_dhcp_transaction_t *this)
{
	return enumerator_create_filter(
						this->attributes->create_enumerator(this->attributes),
						attribute_filter, NULL, NULL);
}

/**
 * Clean up an attribute entry
 */
static void attribute_entry_destroy(attribute_entry_t *entry)
{
	free(entry->data.ptr);
	free(entry);
}

METHOD(dhcp_transaction_t, destroy, void,
	private_dhcp_transaction_t *this)
{
	this->identity->destroy(this->identity);
	DESTROY_IF(this->address);
	DESTROY_IF(this->server);
	this->attributes->destroy_function(this->attributes,
									   (void*)attribute_entry_destroy);
	free(this);
}

/**
 * See header
 */
dhcp_transaction_t *dhcp_transaction_create(uint32_t id,
											identification_t *identity)
{
	private_dhcp_transaction_t *this;

	INIT(this,
		.public = {
			.get_id = _get_id,
			.get_identity = _get_identity,
			.set_address = _set_address,
			.get_address = _get_address,
			.set_server = _set_server,
			.get_server = _get_server,
			.add_attribute = _add_attribute,
			.create_attribute_enumerator = _create_attribute_enumerator,
			.destroy = _destroy,
		},
		.id = id,
		.identity = identity->clone(identity),
		.attributes = linked_list_create(),
	);

	return &this->public;
}