changed [4856] to dynamically choose traffic selector family
[strongswan.git] / src / charon / plugins / load_tester / load_tester_config.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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 * $Id$
16 */
17
18 #include "load_tester_config.h"
19
20 #include <daemon.h>
21
22 typedef struct private_load_tester_config_t private_load_tester_config_t;
23
24 /**
25 * Private data of an load_tester_config_t object
26 */
27 struct private_load_tester_config_t {
28
29 /**
30 * Public part
31 */
32 load_tester_config_t public;
33
34 /**
35 * peer config
36 */
37 peer_cfg_t *peer_cfg;
38
39 /**
40 * virtual IP, if any
41 */
42 host_t *vip;
43
44 /**
45 * Remote address
46 */
47 char *remote;
48
49 /**
50 * IP address pool
51 */
52 char *pool;
53
54 /**
55 * IKE proposal
56 */
57 proposal_t *proposal;
58
59 /**
60 * Authentication method to use
61 */
62 auth_class_t class;
63
64 /**
65 * incremental numbering of generated configs
66 */
67 u_int num;
68 };
69
70 /**
71 * Generate a new initiator config, num = 0 for responder config
72 */
73 static peer_cfg_t* generate_config(private_load_tester_config_t *this, uint num)
74 {
75 ike_cfg_t *ike_cfg;
76 child_cfg_t *child_cfg;
77 peer_cfg_t *peer_cfg;
78 traffic_selector_t *ts;
79 auth_info_t *auth;
80 identification_t *local, *remote;
81 proposal_t *proposal;
82 char buf[128];
83
84 if (num)
85 { /* initiator */
86 snprintf(buf, sizeof(buf), "CN=cli-%d, OU=load-test, O=strongSwan", num);
87 local = identification_create_from_string(buf);
88 snprintf(buf, sizeof(buf), "CN=srv, OU=load-test, O=strongSwan", num);
89 remote = identification_create_from_string(buf);
90 }
91 else
92 { /* responder */
93 local = identification_create_from_string(
94 "CN=srv, OU=load-test, O=strongSwan");
95 remote = identification_create_from_string(
96 "CN=*, OU=load-test, O=strongSwan");
97 }
98
99 ike_cfg = ike_cfg_create(FALSE, FALSE, "0.0.0.0", this->remote);
100 ike_cfg->add_proposal(ike_cfg, this->proposal->clone(this->proposal));
101 peer_cfg = peer_cfg_create("load-test", 2, ike_cfg, local, remote,
102 CERT_SEND_IF_ASKED, UNIQUE_NO, 1, 0, 0, /* keytries, rekey, reauth */
103 0, 0, FALSE, 0, /* jitter, overtime, mobike, dpddelay */
104 this->vip ? this->vip->clone(this->vip) : NULL,
105 this->pool, FALSE, NULL, NULL);
106 auth = peer_cfg->get_auth(peer_cfg);
107 auth->add_item(auth, AUTHN_AUTH_CLASS, &this->class);
108 child_cfg = child_cfg_create("load-test", 600, 400, 100, NULL, TRUE,
109 MODE_TUNNEL, ACTION_NONE, ACTION_NONE, FALSE);
110 proposal = proposal_create_from_string(PROTO_ESP, "aes128-sha1");
111 child_cfg->add_proposal(child_cfg, proposal);
112 ts = traffic_selector_create_dynamic(0, 0, 65535);
113 child_cfg->add_traffic_selector(child_cfg, TRUE, ts);
114 ts = traffic_selector_create_dynamic(0, 0, 65535);
115 child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
116 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
117 return peer_cfg;
118 }
119
120 /**
121 * Implementation of backend_t.create_peer_cfg_enumerator.
122 */
123 static enumerator_t* create_peer_cfg_enumerator(private_load_tester_config_t *this,
124 identification_t *me,
125 identification_t *other)
126 {
127 return enumerator_create_single(this->peer_cfg, NULL);
128 }
129
130 /**
131 * Implementation of backend_t.create_ike_cfg_enumerator.
132 */
133 static enumerator_t* create_ike_cfg_enumerator(private_load_tester_config_t *this,
134 host_t *me, host_t *other)
135 {
136 ike_cfg_t *ike_cfg;
137
138 ike_cfg = this->peer_cfg->get_ike_cfg(this->peer_cfg);
139 return enumerator_create_single(ike_cfg, NULL);
140 }
141
142 /**
143 * implements backend_t.get_peer_cfg_by_name.
144 */
145 static peer_cfg_t *get_peer_cfg_by_name(private_load_tester_config_t *this,
146 char *name)
147 {
148 if (streq(name, "load-test"))
149 {
150 return generate_config(this, this->num++);
151 }
152 return NULL;
153 }
154
155 /**
156 * Implementation of load_tester_config_t.destroy.
157 */
158 static void destroy(private_load_tester_config_t *this)
159 {
160 this->peer_cfg->destroy(this->peer_cfg);
161 DESTROY_IF(this->proposal);
162 DESTROY_IF(this->vip);
163 free(this);
164 }
165
166 /**
167 * Described in header.
168 */
169 load_tester_config_t *load_tester_config_create()
170 {
171 private_load_tester_config_t *this = malloc_thing(private_load_tester_config_t);
172 char *authstr;
173
174 this->public.backend.create_peer_cfg_enumerator = (enumerator_t*(*)(backend_t*, identification_t *me, identification_t *other))create_peer_cfg_enumerator;
175 this->public.backend.create_ike_cfg_enumerator = (enumerator_t*(*)(backend_t*, host_t *me, host_t *other))create_ike_cfg_enumerator;
176 this->public.backend.get_peer_cfg_by_name = (peer_cfg_t* (*)(backend_t*,char*))get_peer_cfg_by_name;
177 this->public.destroy = (void(*)(load_tester_config_t*))destroy;
178
179 this->vip = NULL;
180 if (lib->settings->get_bool(lib->settings,
181 "charon.plugins.load_tester.request_virtual_ip", FALSE))
182 {
183 this->vip = host_create_from_string("0.0.0.0", 0);
184 }
185 this->pool = lib->settings->get_str(lib->settings,
186 "charon.plugins.load_tester.pool", NULL);
187 this->remote = lib->settings->get_str(lib->settings,
188 "charon.plugins.load_tester.remote", "127.0.0.1");
189
190 this->proposal = proposal_create_from_string(PROTO_IKE,
191 lib->settings->get_str(lib->settings,
192 "charon.plugins.load_tester.proposal", "aes128-sha1-modp768"));
193 if (!this->proposal)
194 { /* fallback */
195 this->proposal = proposal_create_from_string(PROTO_IKE,
196 "aes128-sha1-modp768");
197 }
198 authstr = lib->settings->get_str(lib->settings,
199 "charon.plugins.load_tester.auth", "pubkey");
200 if (streq(authstr, "psk"))
201 {
202 this->class = AUTH_CLASS_PSK;
203 }
204 else
205 {
206 this->class = AUTH_CLASS_PUBKEY;
207 }
208
209 this->num = 1;
210 this->peer_cfg = generate_config(this, 0);
211
212 return &this->public;
213 }
214