2 * Copyright (C) 2008 Thomas Kallenberg
3 * Copyright (C) 2008 Tobias Brunner
4 * Copyright (C) 2008 Martin Willi
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 #include "uci_config.h"
21 #include "uci_parser.h"
25 typedef struct private_uci_config_t private_uci_config_t
;
28 * Private data of an uci_config_t object
30 struct private_uci_config_t
{
44 * enumerator implementation for create_peer_cfg_enumerator
47 /** implements enumerator */
49 /** currently enumerated peer config */
51 /** inner uci_parser section enumerator */
56 * Implementation of peer_enumerator_t.public.enumerate
58 static bool peer_enumerator_enumerate(peer_enumerator_t
*this, peer_cfg_t
**cfg
)
60 char *name
, *local_id
, *remote_ip
;
61 child_cfg_t
*child_cfg
;
67 remote_ip
= "0.0.0.0";
69 if (this->inner
->enumerate(this->inner
, &name
, &local_id
, &remote_ip
))
71 DESTROY_IF(this->peer_cfg
);
72 ike_cfg
= ike_cfg_create(FALSE
, FALSE
, "0.0.0.0", remote_ip
);
73 ike_cfg
->add_proposal(ike_cfg
, proposal_create_default(PROTO_IKE
));
74 this->peer_cfg
= peer_cfg_create(
76 identification_create_from_string(local_id
),
77 identification_create_from_encoding(ID_ANY
, chunk_empty
),
78 CERT_SEND_IF_ASKED
, UNIQUE_NO
, CONF_AUTH_PSK
,
79 0, 0, /* EAP method, vendor */
80 1, 3600*12, 0, /* keytries, rekey, reauth */
81 3600, 1800, /* jitter, overtime */
82 TRUE
, 60, /* mobike, dpddelay */
83 NULL
, NULL
, /* vip, pool */
84 FALSE
, NULL
, NULL
); /* mediation, med by, peer id */
85 child_cfg
= child_cfg_create(
86 name
, 3600*4, 3600*3, 360, NULL
, TRUE
,
87 MODE_TUNNEL
, ACTION_NONE
, ACTION_NONE
, FALSE
);
88 child_cfg
->add_proposal(child_cfg
, proposal_create_default(PROTO_ESP
));
89 child_cfg
->add_traffic_selector(child_cfg
, TRUE
,
90 traffic_selector_create_dynamic(0, 0, 65535));
91 child_cfg
->add_traffic_selector(child_cfg
, FALSE
,
92 traffic_selector_create_dynamic(0, 0, 65535));
93 this->peer_cfg
->add_child_cfg(this->peer_cfg
, child_cfg
);
94 *cfg
= this->peer_cfg
;
101 * Implementation of peer_enumerator_t.public.destroy
103 static void peer_enumerator_destroy(peer_enumerator_t
*this)
105 DESTROY_IF(this->peer_cfg
);
106 this->inner
->destroy(this->inner
);
111 * Implementation of backend_t.create_peer_cfg_enumerator.
113 static enumerator_t
* create_peer_cfg_enumerator(private_uci_config_t
*this,
114 identification_t
*me
,
115 identification_t
*other
)
117 peer_enumerator_t
*e
= malloc_thing(peer_enumerator_t
);
119 e
->public.enumerate
= (void*)peer_enumerator_enumerate
;
120 e
->public.destroy
= (void*)peer_enumerator_destroy
;
122 e
->inner
= this->parser
->create_section_enumerator(this->parser
,
123 "local_id", "remote_ip", NULL
);
133 * enumerator implementation for create_ike_cfg_enumerator
136 /** implements enumerator */
138 /** currently enumerated ike config */
140 /** inner uci_parser section enumerator */
145 * Implementation of peer_enumerator_t.public.enumerate
147 static bool ike_enumerator_enumerate(ike_enumerator_t
*this, ike_cfg_t
**cfg
)
149 char *name
, *remote_ip
;
153 remote_ip
= "0.0.0.0";
155 if (this->inner
->enumerate(this->inner
, &name
, &remote_ip
))
157 DESTROY_IF(this->ike_cfg
);
158 this->ike_cfg
= ike_cfg_create(FALSE
, FALSE
, "0.0.0.0", remote_ip
);
159 this->ike_cfg
->add_proposal(this->ike_cfg
,
160 proposal_create_default(PROTO_IKE
));
162 *cfg
= this->ike_cfg
;
169 * Implementation of ike_enumerator_t.public.destroy
171 static void ike_enumerator_destroy(ike_enumerator_t
*this)
173 DESTROY_IF(this->ike_cfg
);
174 this->inner
->destroy(this->inner
);
179 * Implementation of backend_t.create_ike_cfg_enumerator.
181 static enumerator_t
* create_ike_cfg_enumerator(private_uci_config_t
*this,
182 host_t
*me
, host_t
*other
)
184 ike_enumerator_t
*e
= malloc_thing(ike_enumerator_t
);
186 e
->public.enumerate
= (void*)ike_enumerator_enumerate
;
187 e
->public.destroy
= (void*)ike_enumerator_destroy
;
189 e
->inner
= this->parser
->create_section_enumerator(this->parser
,
200 * implements backend_t.get_peer_cfg_by_name.
202 static peer_cfg_t
*get_peer_cfg_by_name(private_uci_config_t
*this, char *name
)
204 enumerator_t
*enumerator
;
205 peer_cfg_t
*current
, *found
= NULL
;
207 enumerator
= create_peer_cfg_enumerator(this, NULL
, NULL
);
210 while (enumerator
->enumerate(enumerator
, ¤t
))
212 if (streq(name
, current
->get_name(current
)))
214 found
= current
->get_ref(current
);
218 enumerator
->destroy(enumerator
);
224 * Implementation of uci_config_t.destroy.
226 static void destroy(private_uci_config_t
*this)
232 * Described in header.
234 uci_config_t
*uci_config_create(uci_parser_t
*parser
)
236 private_uci_config_t
*this = malloc_thing(private_uci_config_t
);
238 this->public.backend
.create_peer_cfg_enumerator
= (enumerator_t
*(*)(backend_t
*, identification_t
*me
, identification_t
*other
))create_peer_cfg_enumerator
;
239 this->public.backend
.create_ike_cfg_enumerator
= (enumerator_t
*(*)(backend_t
*, host_t
*me
, host_t
*other
))create_ike_cfg_enumerator
;
240 this->public.backend
.get_peer_cfg_by_name
= (peer_cfg_t
* (*)(backend_t
*,char*))get_peer_cfg_by_name
;
241 this->public.destroy
= (void(*)(uci_config_t
*))destroy
;
243 this->parser
= parser
;
245 return &this->public;