Don't compare initiator flag in IKE_SA manager, pass initiator parameter to IKE_SA...
[strongswan.git] / src / libcharon / 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
17 #include "ike_sa_id.h"
18
19 #include <stdio.h>
20
21
22 typedef struct private_ike_sa_id_t private_ike_sa_id_t;
23
24 /**
25 * Private data of an ike_sa_id_t object.
26 */
27 struct private_ike_sa_id_t {
28 /**
29 * Public interface of ike_sa_id_t.
30 */
31 ike_sa_id_t public;
32
33 /**
34 * SPI of Initiator.
35 */
36 u_int64_t initiator_spi;
37
38 /**
39 * SPI of Responder.
40 */
41 u_int64_t responder_spi;
42
43 /**
44 * Role for specific IKE_SA.
45 */
46 bool is_initiator_flag;
47 };
48
49 METHOD(ike_sa_id_t, set_responder_spi, void,
50 private_ike_sa_id_t *this, u_int64_t responder_spi)
51 {
52 this->responder_spi = responder_spi;
53 }
54
55 METHOD(ike_sa_id_t, set_initiator_spi, void,
56 private_ike_sa_id_t *this, u_int64_t initiator_spi)
57 {
58 this->initiator_spi = initiator_spi;
59 }
60
61 METHOD(ike_sa_id_t, get_initiator_spi, u_int64_t,
62 private_ike_sa_id_t *this)
63 {
64 return this->initiator_spi;
65 }
66
67 METHOD(ike_sa_id_t, get_responder_spi, u_int64_t,
68 private_ike_sa_id_t *this)
69 {
70 return this->responder_spi;
71 }
72
73 METHOD(ike_sa_id_t, equals, bool,
74 private_ike_sa_id_t *this, private_ike_sa_id_t *other)
75 {
76 if (other == NULL)
77 {
78 return FALSE;
79 }
80 return this->initiator_spi == other->initiator_spi &&
81 this->responder_spi == other->responder_spi;
82 }
83
84 METHOD(ike_sa_id_t, replace_values, void,
85 private_ike_sa_id_t *this, private_ike_sa_id_t *other)
86 {
87 this->initiator_spi = other->initiator_spi;
88 this->responder_spi = other->responder_spi;
89 this->is_initiator_flag = other->is_initiator_flag;
90 }
91
92 METHOD(ike_sa_id_t, is_initiator, bool,
93 private_ike_sa_id_t *this)
94 {
95 return this->is_initiator_flag;
96 }
97
98 METHOD(ike_sa_id_t, switch_initiator, bool,
99 private_ike_sa_id_t *this)
100 {
101 if (this->is_initiator_flag)
102 {
103 this->is_initiator_flag = FALSE;
104 }
105 else
106 {
107 this->is_initiator_flag = TRUE;
108 }
109 return this->is_initiator_flag;
110 }
111
112 METHOD(ike_sa_id_t, clone_, ike_sa_id_t*,
113 private_ike_sa_id_t *this)
114 {
115 return ike_sa_id_create(this->initiator_spi, this->responder_spi,
116 this->is_initiator_flag);
117 }
118
119 METHOD(ike_sa_id_t, destroy, void,
120 private_ike_sa_id_t *this)
121 {
122 free(this);
123 }
124
125 /*
126 * Described in header.
127 */
128 ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi,
129 bool is_initiator_flag)
130 {
131 private_ike_sa_id_t *this;
132
133 INIT(this,
134 .public = {
135 .set_responder_spi = _set_responder_spi,
136 .set_initiator_spi = _set_initiator_spi,
137 .get_responder_spi = _get_responder_spi,
138 .get_initiator_spi = _get_initiator_spi,
139 .equals = (void*)_equals,
140 .replace_values = (void*)_replace_values,
141 .is_initiator = _is_initiator,
142 .switch_initiator = _switch_initiator,
143 .clone = _clone_,
144 .destroy = _destroy,
145 },
146 .initiator_spi = initiator_spi,
147 .responder_spi = responder_spi,
148 .is_initiator_flag = is_initiator_flag,
149 );
150
151 return &this->public;
152 }