ikev2: Compare initiator flag again, partially reverts 17ec1c74de
[strongswan.git] / src / libcharon / sa / ike_sa_id.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "ike_sa_id.h"
19
20 #include <stdio.h>
21 #include <encoding/payloads/ike_header.h>
22
23 typedef struct private_ike_sa_id_t private_ike_sa_id_t;
24
25 /**
26 * Private data of an ike_sa_id_t object.
27 */
28 struct private_ike_sa_id_t {
29 /**
30 * Public interface of ike_sa_id_t.
31 */
32 ike_sa_id_t public;
33
34 /**
35 * Major IKE version of IKE_SA.
36 */
37 u_int8_t ike_version;
38
39 /**
40 * SPI of initiator.
41 */
42 u_int64_t initiator_spi;
43
44 /**
45 * SPI of responder.
46 */
47 u_int64_t responder_spi;
48
49 /**
50 * Role for specific IKE_SA.
51 */
52 bool is_initiator_flag;
53 };
54
55 METHOD(ike_sa_id_t, get_ike_version, u_int8_t,
56 private_ike_sa_id_t *this)
57 {
58 return this->ike_version;
59 }
60
61 METHOD(ike_sa_id_t, set_responder_spi, void,
62 private_ike_sa_id_t *this, u_int64_t responder_spi)
63 {
64 this->responder_spi = responder_spi;
65 }
66
67 METHOD(ike_sa_id_t, set_initiator_spi, void,
68 private_ike_sa_id_t *this, u_int64_t initiator_spi)
69 {
70 this->initiator_spi = initiator_spi;
71 }
72
73 METHOD(ike_sa_id_t, get_initiator_spi, u_int64_t,
74 private_ike_sa_id_t *this)
75 {
76 return this->initiator_spi;
77 }
78
79 METHOD(ike_sa_id_t, get_responder_spi, u_int64_t,
80 private_ike_sa_id_t *this)
81 {
82 return this->responder_spi;
83 }
84
85 METHOD(ike_sa_id_t, equals, bool,
86 private_ike_sa_id_t *this, private_ike_sa_id_t *other)
87 {
88 if (other == NULL)
89 {
90 return FALSE;
91 }
92 return this->ike_version == other->ike_version &&
93 (this->ike_version == IKEV1_MAJOR_VERSION ||
94 this->is_initiator_flag == other->is_initiator_flag) &&
95 this->initiator_spi == other->initiator_spi &&
96 this->responder_spi == other->responder_spi;
97 }
98
99 METHOD(ike_sa_id_t, replace_values, void,
100 private_ike_sa_id_t *this, private_ike_sa_id_t *other)
101 {
102 this->ike_version = other->ike_version;
103 this->initiator_spi = other->initiator_spi;
104 this->responder_spi = other->responder_spi;
105 this->is_initiator_flag = other->is_initiator_flag;
106 }
107
108 METHOD(ike_sa_id_t, is_initiator, bool,
109 private_ike_sa_id_t *this)
110 {
111 return this->is_initiator_flag;
112 }
113
114 METHOD(ike_sa_id_t, switch_initiator, bool,
115 private_ike_sa_id_t *this)
116 {
117 this->is_initiator_flag = !this->is_initiator_flag;
118 return this->is_initiator_flag;
119 }
120
121 METHOD(ike_sa_id_t, clone_, ike_sa_id_t*,
122 private_ike_sa_id_t *this)
123 {
124 return ike_sa_id_create(this->ike_version, this->initiator_spi,
125 this->responder_spi, this->is_initiator_flag);
126 }
127
128 METHOD(ike_sa_id_t, destroy, void,
129 private_ike_sa_id_t *this)
130 {
131 free(this);
132 }
133
134 /*
135 * Described in header.
136 */
137 ike_sa_id_t * ike_sa_id_create(u_int8_t ike_version, u_int64_t initiator_spi,
138 u_int64_t responder_spi, bool is_initiator_flag)
139 {
140 private_ike_sa_id_t *this;
141
142 INIT(this,
143 .public = {
144 .get_ike_version = _get_ike_version,
145 .set_responder_spi = _set_responder_spi,
146 .set_initiator_spi = _set_initiator_spi,
147 .get_responder_spi = _get_responder_spi,
148 .get_initiator_spi = _get_initiator_spi,
149 .equals = (void*)_equals,
150 .replace_values = (void*)_replace_values,
151 .is_initiator = _is_initiator,
152 .switch_initiator = _switch_initiator,
153 .clone = _clone_,
154 .destroy = _destroy,
155 },
156 .ike_version = ike_version,
157 .initiator_spi = initiator_spi,
158 .responder_spi = responder_spi,
159 .is_initiator_flag = is_initiator_flag,
160 );
161
162 return &this->public;
163 }