Moving charon to libcharon.
[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 /**
50 * Implementation of ike_sa_id_t.set_responder_spi.
51 */
52 static void set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi)
53 {
54 this->responder_spi = responder_spi;
55 }
56
57 /**
58 * Implementation of ike_sa_id_t.set_initiator_spi.
59 */
60 static void set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi)
61 {
62 this->initiator_spi = initiator_spi;
63 }
64
65 /**
66 * Implementation of ike_sa_id_t.get_initiator_spi.
67 */
68 static u_int64_t get_initiator_spi (private_ike_sa_id_t *this)
69 {
70 return this->initiator_spi;
71 }
72
73 /**
74 * Implementation of ike_sa_id_t.get_responder_spi.
75 */
76 static u_int64_t get_responder_spi (private_ike_sa_id_t *this)
77 {
78 return this->responder_spi;
79 }
80
81 /**
82 * Implementation of ike_sa_id_t.equals.
83 */
84 static bool equals (private_ike_sa_id_t *this, private_ike_sa_id_t *other)
85 {
86 if (other == NULL)
87 {
88 return FALSE;
89 }
90 if ((this->is_initiator_flag == other->is_initiator_flag) &&
91 (this->initiator_spi == other->initiator_spi) &&
92 (this->responder_spi == other->responder_spi))
93 {
94 /* private_ike_sa_id's are equal */
95 return TRUE;
96 }
97 else
98 {
99 /* private_ike_sa_id's are not equal */
100 return FALSE;
101 }
102 }
103
104 /**
105 * Implementation of ike_sa_id_t.replace_values.
106 */
107 static void replace_values(private_ike_sa_id_t *this, private_ike_sa_id_t *other)
108 {
109 this->initiator_spi = other->initiator_spi;
110 this->responder_spi = other->responder_spi;
111 this->is_initiator_flag = other->is_initiator_flag;
112 }
113
114 /**
115 * Implementation of ike_sa_id_t.is_initiator.
116 */
117 static bool is_initiator(private_ike_sa_id_t *this)
118 {
119 return this->is_initiator_flag;
120 }
121
122 /**
123 * Implementation of ike_sa_id_t.switch_initiator.
124 */
125 static bool switch_initiator(private_ike_sa_id_t *this)
126 {
127 if (this->is_initiator_flag)
128 {
129 this->is_initiator_flag = FALSE;
130 }
131 else
132 {
133 this->is_initiator_flag = TRUE;
134 }
135 return this->is_initiator_flag;
136 }
137
138 /**
139 * Implementation of ike_sa_id_t.clone.
140 */
141 static ike_sa_id_t* clone_(private_ike_sa_id_t *this)
142 {
143 return ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag);
144 }
145
146 /**
147 * Implementation of ike_sa_id_t.destroy.
148 */
149 static void destroy(private_ike_sa_id_t *this)
150 {
151 free(this);
152 }
153
154 /*
155 * Described in header.
156 */
157 ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator_flag)
158 {
159 private_ike_sa_id_t *this = malloc_thing(private_ike_sa_id_t);
160
161 /* public functions */
162 this->public.set_responder_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi;
163 this->public.set_initiator_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi;
164 this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi;
165 this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi;
166 this->public.equals = (bool(*)(ike_sa_id_t*,ike_sa_id_t*)) equals;
167 this->public.replace_values = (void(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values;
168 this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator;
169 this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator;
170 this->public.clone = (ike_sa_id_t*(*)(ike_sa_id_t*)) clone_;
171 this->public.destroy = (void(*)(ike_sa_id_t*))destroy;
172
173 /* private data */
174 this->initiator_spi = initiator_spi;
175 this->responder_spi = responder_spi;
176 this->is_initiator_flag = is_initiator_flag;
177
178 return &this->public;
179 }