Store the major IKE version on ike_sa_id_t.
[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
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->initiator_spi == other->initiator_spi &&
94 this->responder_spi == other->responder_spi;
95 }
96
97 METHOD(ike_sa_id_t, replace_values, void,
98 private_ike_sa_id_t *this, private_ike_sa_id_t *other)
99 {
100 this->ike_version = other->ike_version;
101 this->initiator_spi = other->initiator_spi;
102 this->responder_spi = other->responder_spi;
103 this->is_initiator_flag = other->is_initiator_flag;
104 }
105
106 METHOD(ike_sa_id_t, is_initiator, bool,
107 private_ike_sa_id_t *this)
108 {
109 return this->is_initiator_flag;
110 }
111
112 METHOD(ike_sa_id_t, switch_initiator, bool,
113 private_ike_sa_id_t *this)
114 {
115 this->is_initiator_flag = !this->is_initiator_flag;
116 return this->is_initiator_flag;
117 }
118
119 METHOD(ike_sa_id_t, clone_, ike_sa_id_t*,
120 private_ike_sa_id_t *this)
121 {
122 return ike_sa_id_create(this->ike_version, this->initiator_spi,
123 this->responder_spi, this->is_initiator_flag);
124 }
125
126 METHOD(ike_sa_id_t, destroy, void,
127 private_ike_sa_id_t *this)
128 {
129 free(this);
130 }
131
132 /*
133 * Described in header.
134 */
135 ike_sa_id_t * ike_sa_id_create(u_int8_t ike_version, u_int64_t initiator_spi,
136 u_int64_t responder_spi, bool is_initiator_flag)
137 {
138 private_ike_sa_id_t *this;
139
140 INIT(this,
141 .public = {
142 .get_ike_version = _get_ike_version,
143 .set_responder_spi = _set_responder_spi,
144 .set_initiator_spi = _set_initiator_spi,
145 .get_responder_spi = _get_responder_spi,
146 .get_initiator_spi = _get_initiator_spi,
147 .equals = (void*)_equals,
148 .replace_values = (void*)_replace_values,
149 .is_initiator = _is_initiator,
150 .switch_initiator = _switch_initiator,
151 .clone = _clone_,
152 .destroy = _destroy,
153 },
154 .ike_version = ike_version,
155 .initiator_spi = initiator_spi,
156 .responder_spi = responder_spi,
157 .is_initiator_flag = is_initiator_flag,
158 );
159
160 return &this->public;
161 }