f9b1df26b04d82f5b7d03ebbbe9c5168897759d4
[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 * $Id$
16 */
17
18 #include "migrate_job.h"
19
20 #include <daemon.h>
21
22 #include <config/child_cfg.h>
23
24
25 typedef struct private_migrate_job_t private_migrate_job_t;
26
27 /**
28 * Private data of a migrate_job_t object.
29 */
30 struct private_migrate_job_t {
31 /**
32 * Public migrate_job_t interface.
33 */
34 migrate_job_t public;
35
36 /**
37 * reqid of the CHILD_SA if it already exists
38 */
39 u_int32_t reqid;
40
41 /**
42 * source traffic selector
43 */
44 traffic_selector_t *src_ts;
45
46 /**
47 * destination traffic selector
48 */
49 traffic_selector_t *dst_ts;
50
51 /**
52 * local host address to be used for IKE
53 */
54 host_t *local;
55
56 /**
57 * remote host address to be used for IKE
58 */
59 host_t *remote;
60 };
61
62 /**
63 * Implementation of job_t.destroy.
64 */
65 static void destroy(private_migrate_job_t *this)
66 {
67 DESTROY_IF(this->src_ts);
68 DESTROY_IF(this->dst_ts);
69 DESTROY_IF(this->local);
70 DESTROY_IF(this->remote);
71 free(this);
72 }
73
74 /**
75 * Implementation of job_t.execute.
76 */
77 static void execute(private_migrate_job_t *this)
78 {
79 ike_sa_t *ike_sa = NULL;
80
81 if (this->reqid)
82 {
83 ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
84 this->reqid, TRUE);
85 }
86 if (ike_sa)
87 {
88 DBG2(DBG_JOB, "found CHILD_SA with reqid {%d}", this->reqid);
89 ike_sa->set_kmaddress(ike_sa, this->local, this->remote);
90 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
91 }
92 else
93 {
94 DBG1(DBG_JOB, "no CHILD_SA found with reqid {%d}", this->reqid);
95 }
96 destroy(this);
97 }
98
99 /*
100 * Described in header
101 */
102 migrate_job_t *migrate_job_create(u_int32_t reqid,
103 traffic_selector_t *src_ts,
104 traffic_selector_t *dst_ts,
105 policy_dir_t dir,
106 host_t *local, host_t *remote)
107 {
108 private_migrate_job_t *this = malloc_thing(private_migrate_job_t);
109
110 /* interface functions */
111 this->public.job_interface.execute = (void (*) (job_t *)) execute;
112 this->public.job_interface.destroy = (void (*)(job_t*)) destroy;
113
114 /* private variables */
115 this->reqid = reqid;
116 this->src_ts = (dir == POLICY_OUT) ? src_ts : dst_ts;
117 this->dst_ts = (dir == POLICY_OUT) ? dst_ts : src_ts;
118 this->local = local;
119 this->remote = remote;
120
121 return &this->public;
122 }