Removed strayed code fragment
[strongswan.git] / src / charon / processing / jobs / migrate_job.c
1 /*
2 * Copyright (C) 2008 Andreas Steffen
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
16 #include "migrate_job.h"
17
18 #include <daemon.h>
19
20 #include <config/child_cfg.h>
21
22
23 typedef struct private_migrate_job_t private_migrate_job_t;
24
25 /**
26 * Private data of a migrate_job_t object.
27 */
28 struct private_migrate_job_t {
29 /**
30 * Public migrate_job_t interface.
31 */
32 migrate_job_t public;
33
34 /**
35 * reqid of the CHILD_SA if it already exists
36 */
37 u_int32_t reqid;
38
39 /**
40 * source traffic selector
41 */
42 traffic_selector_t *src_ts;
43
44 /**
45 * destination traffic selector
46 */
47 traffic_selector_t *dst_ts;
48
49 /**
50 * local host address to be used for IKE
51 */
52 host_t *local;
53
54 /**
55 * remote host address to be used for IKE
56 */
57 host_t *remote;
58 };
59
60 /**
61 * Implementation of job_t.destroy.
62 */
63 static void destroy(private_migrate_job_t *this)
64 {
65 DESTROY_IF(this->src_ts);
66 DESTROY_IF(this->dst_ts);
67 DESTROY_IF(this->local);
68 DESTROY_IF(this->remote);
69 free(this);
70 }
71
72 /**
73 * Implementation of job_t.execute.
74 */
75 static void execute(private_migrate_job_t *this)
76 {
77 ike_sa_t *ike_sa = NULL;
78
79 if (this->reqid)
80 {
81 ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
82 this->reqid, TRUE);
83 }
84 if (ike_sa)
85 {
86 iterator_t *children;
87 child_sa_t *child_sa;
88 host_t *host;
89
90 children = ike_sa->create_child_sa_iterator(ike_sa);
91 while (children->iterate(children, (void**)&child_sa))
92 {
93 if (child_sa->get_reqid(child_sa) == this->reqid)
94 {
95 break;
96 }
97 }
98 children->destroy(children);
99 DBG2(DBG_JOB, "found CHILD_SA with reqid {%d}", this->reqid);
100
101 ike_sa->set_kmaddress(ike_sa, this->local, this->remote);
102
103 host = this->local->clone(this->local);
104 host->set_port(host, IKEV2_UDP_PORT);
105 ike_sa->set_my_host(ike_sa, host);
106
107 host = this->remote->clone(this->remote);
108 host->set_port(host, IKEV2_UDP_PORT);
109 ike_sa->set_other_host(ike_sa, host);
110
111 if (child_sa->update(child_sa, this->local, this->remote,
112 ike_sa->get_virtual_ip(ike_sa, TRUE),
113 ike_sa->has_condition(ike_sa, COND_NAT_ANY)) == NOT_SUPPORTED)
114 {
115 ike_sa->rekey_child_sa(ike_sa, child_sa->get_protocol(child_sa),
116 child_sa->get_spi(child_sa, TRUE));
117 }
118 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
119 }
120 else
121 {
122 DBG1(DBG_JOB, "no CHILD_SA found with reqid {%d}", this->reqid);
123 }
124 destroy(this);
125 }
126
127 /*
128 * Described in header
129 */
130 migrate_job_t *migrate_job_create(u_int32_t reqid,
131 traffic_selector_t *src_ts,
132 traffic_selector_t *dst_ts,
133 policy_dir_t dir,
134 host_t *local, host_t *remote)
135 {
136 private_migrate_job_t *this = malloc_thing(private_migrate_job_t);
137
138 /* interface functions */
139 this->public.job_interface.execute = (void (*) (job_t *)) execute;
140 this->public.job_interface.destroy = (void (*)(job_t*)) destroy;
141
142 /* private variables */
143 this->reqid = reqid;
144 this->src_ts = (dir == POLICY_OUT) ? src_ts : dst_ts;
145 this->dst_ts = (dir == POLICY_OUT) ? dst_ts : src_ts;
146 this->local = local;
147 this->remote = remote;
148
149 return &this->public;
150 }