Migrated ike_sa_id_t to INIT/METHOD macros.
[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 if ((this->is_initiator_flag == other->is_initiator_flag) &&
81 (this->initiator_spi == other->initiator_spi) &&
82 (this->responder_spi == other->responder_spi))
83 {
84 /* private_ike_sa_id's are equal */
85 return TRUE;
86 }
87 else
88 {
89 /* private_ike_sa_id's are not equal */
90 return FALSE;
91 }
92 }
93
94 METHOD(ike_sa_id_t, replace_values, void,
95 private_ike_sa_id_t *this, private_ike_sa_id_t *other)
96 {
97 this->initiator_spi = other->initiator_spi;
98 this->responder_spi = other->responder_spi;
99 this->is_initiator_flag = other->is_initiator_flag;
100 }
101
102 METHOD(ike_sa_id_t, is_initiator, bool,
103 private_ike_sa_id_t *this)
104 {
105 return this->is_initiator_flag;
106 }
107
108 METHOD(ike_sa_id_t, switch_initiator, bool,
109 private_ike_sa_id_t *this)
110 {
111 if (this->is_initiator_flag)
112 {
113 this->is_initiator_flag = FALSE;
114 }
115 else
116 {
117 this->is_initiator_flag = TRUE;
118 }
119 return this->is_initiator_flag;
120 }
121
122 METHOD(ike_sa_id_t, clone_, ike_sa_id_t*,
123 private_ike_sa_id_t *this)
124 {
125 return ike_sa_id_create(this->initiator_spi, this->responder_spi,
126 this->is_initiator_flag);
127 }
128
129 METHOD(ike_sa_id_t, destroy, void,
130 private_ike_sa_id_t *this)
131 {
132 free(this);
133 }
134
135 /*
136 * Described in header.
137 */
138 ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi,
139 bool is_initiator_flag)
140 {
141 private_ike_sa_id_t *this;
142
143 INIT(this,
144 .public = {
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 .initiator_spi = initiator_spi,
157 .responder_spi = responder_spi,
158 .is_initiator_flag = is_initiator_flag,
159 );
160
161 return &this->public;
162 }