aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/config/init_config.c
blob: 495feb7ab94b7a04b75b06a6994c6b948125ebaa (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 * @file init_config.c
 * 
 * @brief Implementation of init_config_t.
 *  
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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 "init_config.h"

#include <utils/allocator.h>
#include <utils/linked_list.h>

typedef struct private_init_config_t private_init_config_t;

/**
 * Private data of an init_config_t object
 */
struct private_init_config_t {

	/**
	 * Public part
	 */
	init_config_t public;

	/**
	 * Host information of my host.
	 */
	host_t *my_host;

	/**
	 * Host information of other host.
	 */	
	host_t *other_host;
	
	/**
	 * Supported proposals
	 */
	linked_list_t *proposals;
};

/**
 * Implementation of init_config_t.get_my_host.
 */
static host_t * get_my_host (private_init_config_t *this)
{
	return this->my_host->clone(this->my_host);
}

/**
 * Implementation of init_config_t.get_other_host.
 */
static host_t * get_other_host (private_init_config_t *this)
{
	return this->other_host->clone(this->other_host);
}

/**
 * Implementation of init_config_t.get_dh_group_number.
 */
static diffie_hellman_group_t get_dh_group_number (private_init_config_t *this,size_t priority)
{
	ike_proposal_t *ike_proposal;
	
	if ((this->proposals->get_count(this->proposals) == 0) || (this->proposals->get_count(this->proposals) < priority))
	{
		return MODP_UNDEFINED;
	}
	
	this->proposals->get_at_position(this->proposals,(priority -1),(void **) &ike_proposal);
	
	return (ike_proposal->diffie_hellman_group);
}

/**
 * Implementation of init_config_t.get_proposals.
 */
static size_t get_proposals (private_init_config_t *this,ike_proposal_t **proposals)
{
	iterator_t *iterator;
	ike_proposal_t *current_proposal;
	int i = 0;
	ike_proposal_t *proposal_array;
	
	proposal_array = allocator_alloc(this->proposals->get_count(this->proposals) * sizeof(ike_proposal_t));
		
	iterator = this->proposals->create_iterator(this->proposals,TRUE);
	
	while (iterator->has_next(iterator))
	{
		iterator->current(iterator,(void **) &current_proposal);
		proposal_array[i] = (*current_proposal);
		i++;
	}
	iterator->destroy(iterator);
	
	*proposals = proposal_array;
	return this->proposals->get_count(this->proposals);
}
	
/**
 * Implementation of init_config_t.select_proposal.
 */
static status_t select_proposal (private_init_config_t *this, ike_proposal_t *proposals, size_t proposal_count, ike_proposal_t *selected_proposal)
{
	iterator_t * my_iterator;
	int i;
	ike_proposal_t *my_current_proposal;

	my_iterator = this->proposals->create_iterator(this->proposals,TRUE);

	
	for (i = 0; i < proposal_count; i++)
	{
		my_iterator->reset(my_iterator);
		while (my_iterator->has_next(my_iterator))
		{
			my_iterator->current(my_iterator,(void **) &my_current_proposal);
		
			if (memcmp(my_current_proposal,&proposals[i],sizeof(ike_proposal_t)) == 0)
			{
				/* found a matching proposal */
				*selected_proposal = *my_current_proposal;
				my_iterator->destroy(my_iterator);
				return SUCCESS;
			}
		}				
	}
	
	my_iterator->destroy(my_iterator);
	return NOT_FOUND;
}

/**
 * Implementation of init_config_t.destroy.
 */
static void add_proposal (private_init_config_t *this,size_t priority, ike_proposal_t proposal)
{
	ike_proposal_t * new_proposal = allocator_alloc(sizeof(ike_proposal_t));
	
	*new_proposal = proposal;
	 
	
	if (priority > this->proposals->get_count(this->proposals))
	{
		this->proposals->insert_last(this->proposals,new_proposal);
		return;
	}
	
	this->proposals->insert_at_position(this->proposals,(priority - 1),new_proposal);
}

/**
 * Implementation of init_config_t.destroy.
 */
static void destroy (private_init_config_t *this)
{
	ike_proposal_t *proposal;
	
	while (this->proposals->get_count(this->proposals) > 0)
	{
		this->proposals->remove_first(this->proposals,(void **) &proposal);
		allocator_free(proposal);
	}
	this->proposals->destroy(this->proposals);
	
	this->my_host->destroy(this->my_host);
	this->other_host->destroy(this->other_host);
	
	allocator_free(this);
}

/**
 * Described in header.
 */
init_config_t * init_config_create(char * my_ip, char *other_ip, u_int16_t my_port, u_int16_t other_port)
{
	private_init_config_t *this = allocator_alloc_thing(private_init_config_t);

	/* public functions */
	this->public.get_my_host = (host_t*(*)(init_config_t*))get_my_host;
	this->public.get_other_host = (host_t*(*)(init_config_t*))get_other_host;
	this->public.get_dh_group_number = (diffie_hellman_group_t (*)(init_config_t*,size_t))get_dh_group_number;
	this->public.get_proposals = (size_t(*)(init_config_t*,ike_proposal_t**))get_proposals;
	this->public.select_proposal = (status_t(*)(init_config_t*,ike_proposal_t*,size_t,ike_proposal_t*))select_proposal;
	this->public.add_proposal = (void(*)(init_config_t*, size_t, ike_proposal_t)) add_proposal;
	this->public.destroy = (void(*)(init_config_t*))destroy;
	
	/* private variables */
	this->my_host = host_create(AF_INET,my_ip, my_port);
	this->other_host = host_create(AF_INET,other_ip, other_port);
	
	this->proposals = linked_list_create();

	return (&this->public);
}