mediation connections should now properly rekey
[strongswan.git] / src / charon / sa / ike_sa_id.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include "ike_sa_id.h"
20
21 #include <stdio.h>
22
23
24 typedef struct private_ike_sa_id_t private_ike_sa_id_t;
25
26 /**
27 * Private data of an ike_sa_id_t object.
28 */
29 struct private_ike_sa_id_t {
30 /**
31 * Public interface of ike_sa_id_t.
32 */
33 ike_sa_id_t public;
34
35 /**
36 * SPI of Initiator.
37 */
38 u_int64_t initiator_spi;
39
40 /**
41 * SPI of Responder.
42 */
43 u_int64_t responder_spi;
44
45 /**
46 * Role for specific IKE_SA.
47 */
48 bool is_initiator_flag;
49 };
50
51 /**
52 * Implementation of ike_sa_id_t.set_responder_spi.
53 */
54 static void set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi)
55 {
56 this->responder_spi = responder_spi;
57 }
58
59 /**
60 * Implementation of ike_sa_id_t.set_initiator_spi.
61 */
62 static void set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi)
63 {
64 this->initiator_spi = initiator_spi;
65 }
66
67 /**
68 * Implementation of ike_sa_id_t.get_initiator_spi.
69 */
70 static u_int64_t get_initiator_spi (private_ike_sa_id_t *this)
71 {
72 return this->initiator_spi;
73 }
74
75 /**
76 * Implementation of ike_sa_id_t.get_responder_spi.
77 */
78 static u_int64_t get_responder_spi (private_ike_sa_id_t *this)
79 {
80 return this->responder_spi;
81 }
82
83 /**
84 * Implementation of ike_sa_id_t.equals.
85 */
86 static bool equals (private_ike_sa_id_t *this, private_ike_sa_id_t *other)
87 {
88 if (other == NULL)
89 {
90 return FALSE;
91 }
92 if ((this->is_initiator_flag == other->is_initiator_flag) &&
93 (this->initiator_spi == other->initiator_spi) &&
94 (this->responder_spi == other->responder_spi))
95 {
96 /* private_ike_sa_id's are equal */
97 return TRUE;
98 }
99 else
100 {
101 /* private_ike_sa_id's are not equal */
102 return FALSE;
103 }
104 }
105
106 /**
107 * Implementation of ike_sa_id_t.replace_values.
108 */
109 static void replace_values(private_ike_sa_id_t *this, private_ike_sa_id_t *other)
110 {
111 this->initiator_spi = other->initiator_spi;
112 this->responder_spi = other->responder_spi;
113 this->is_initiator_flag = other->is_initiator_flag;
114 }
115
116 /**
117 * Implementation of ike_sa_id_t.is_initiator.
118 */
119 static bool is_initiator(private_ike_sa_id_t *this)
120 {
121 return this->is_initiator_flag;
122 }
123
124 /**
125 * Implementation of ike_sa_id_t.switch_initiator.
126 */
127 static bool switch_initiator(private_ike_sa_id_t *this)
128 {
129 if (this->is_initiator_flag)
130 {
131 this->is_initiator_flag = FALSE;
132 }
133 else
134 {
135 this->is_initiator_flag = TRUE;
136 }
137 return this->is_initiator_flag;
138 }
139
140 /**
141 * Implementation of ike_sa_id_t.clone.
142 */
143 static ike_sa_id_t* clone_(private_ike_sa_id_t *this)
144 {
145 return ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag);
146 }
147
148 /**
149 * Implementation of ike_sa_id_t.destroy.
150 */
151 static void destroy(private_ike_sa_id_t *this)
152 {
153 free(this);
154 }
155
156 /*
157 * Described in header.
158 */
159 ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator_flag)
160 {
161 private_ike_sa_id_t *this = malloc_thing(private_ike_sa_id_t);
162
163 /* public functions */
164 this->public.set_responder_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi;
165 this->public.set_initiator_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi;
166 this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi;
167 this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi;
168 this->public.equals = (bool(*)(ike_sa_id_t*,ike_sa_id_t*)) equals;
169 this->public.replace_values = (void(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values;
170 this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator;
171 this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator;
172 this->public.clone = (ike_sa_id_t*(*)(ike_sa_id_t*)) clone_;
173 this->public.destroy = (void(*)(ike_sa_id_t*))destroy;
174
175 /* private data */
176 this->initiator_spi = initiator_spi;
177 this->responder_spi = responder_spi;
178 this->is_initiator_flag = is_initiator_flag;
179
180 return &this->public;
181 }