- added clone functionality
[strongswan.git] / Source / charon / ike_sa_id.c
1 /**
2 * @file ihe_sa_id.c
3 *
4 * @brief Class for identification of an IKE_SA
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stdlib.h>
24 #include <freeswan.h>
25 #include <pluto/constants.h>
26 #include <pluto/defs.h>
27
28 #include "types.h"
29 #include "ike_sa_id.h"
30
31 /**
32 * Private data of an ike_sa_id object
33 */
34 typedef struct private_ike_sa_id_s private_ike_sa_id_t;
35
36 struct private_ike_sa_id_s {
37 /**
38 * Public part of a ike_sa_id object
39 */
40 ike_sa_id_t public;
41
42
43 /* Private values */
44
45 /**
46 * SPI of Initiator
47 */
48 spi_t initiator_spi;
49
50 /**
51 * SPI of Responder
52 */
53 spi_t responder_spi;
54
55 /**
56 * Role for specific IKE_SA
57 */
58 ike_sa_role_t role;
59
60 };
61
62
63 /**
64 * @brief implements function set_responder_spi of ike_sa_id_t
65 */
66 static status_t set_responder_spi (private_ike_sa_id_t *this, spi_t responder_spi)
67 {
68 if (this == NULL)
69 {
70 return FAILED;
71 }
72 this->responder_spi = responder_spi;
73 return SUCCESS;
74 }
75
76 /**
77 * @brief implements function initiator_spi_is_set of ike_sa_id_t
78 */
79 static bool initiator_spi_is_set (private_ike_sa_id_t *this)
80 {
81 return (!((this->initiator_spi.high == 0) && (this->initiator_spi.low == 0)));
82 }
83
84 /**
85 * @brief implements function responder_spi_is_set of ike_sa_id_t
86 */
87 static bool responder_spi_is_set (private_ike_sa_id_t *this)
88 {
89 return (!((this->responder_spi.high == 0) && (this->responder_spi.low == 0)));
90 }
91
92 /**
93 * @brief implements function equals of ike_sa_id_t
94 */
95 static status_t equals (private_ike_sa_id_t *this,private_ike_sa_id_t *other, bool *are_equal)
96 {
97 if ((this == NULL)||(other == NULL))
98 {
99 return FAILED;
100 }
101 if ( (this->role == other->role) &&
102 (this->initiator_spi.high == other->initiator_spi.high) &&
103 (this->initiator_spi.low == other->initiator_spi.low) &&
104 (this->responder_spi.high == other->responder_spi.high) &&
105 (this->responder_spi.low == other->responder_spi.low))
106 {
107 /* private_ike_sa_id's are equal */
108 *are_equal = TRUE;
109 }
110 else
111 {
112 /* private_ike_sa_id's are not equal */
113 *are_equal = FALSE;
114 }
115
116 return SUCCESS;
117 }
118
119 static status_t clone (private_ike_sa_id_t *this,ike_sa_id_t **clone_of_this)
120 {
121 if ((this == NULL) || (clone_of_this == NULL))
122 {
123 return FAILED;
124 }
125
126 *clone_of_this = ike_sa_id_create(this->initiator_spi, this->responder_spi, this->role);
127
128 return (*clone_of_this == NULL) ? FAILED : SUCCESS;
129 }
130
131 /**
132 * @brief implements function destroy of ike_sa_id_t
133 */
134 static status_t destroy (private_ike_sa_id_t *this)
135 {
136 if (this == NULL)
137 {
138 return FAILED;
139 }
140 pfree(this);
141 return SUCCESS;
142 }
143
144 /*
145 * Described in Header-File
146 */
147 ike_sa_id_t * ike_sa_id_create(spi_t initiator_spi, spi_t responder_spi, ike_sa_role_t role)
148 {
149 private_ike_sa_id_t *this = alloc_thing(private_ike_sa_id_t, "private_ike_sa_id_t");
150 if (this == NULL)
151 {
152 return NULL;
153 }
154
155 /* Public functions */
156 this->public.set_responder_spi = (status_t(*)(ike_sa_id_t*,spi_t))set_responder_spi;
157 this->public.responder_spi_is_set = (bool(*)(ike_sa_id_t*))responder_spi_is_set;
158 this->public.initiator_spi_is_set = (bool(*)(ike_sa_id_t*))initiator_spi_is_set;
159 this->public.equals = (status_t(*)(ike_sa_id_t*,ike_sa_id_t*,bool*))equals;
160 this->public.clone = (status_t(*)(ike_sa_id_t*,ike_sa_id_t**))clone;
161 this->public.destroy = (status_t(*)(ike_sa_id_t*))destroy;
162
163 /* private data */
164 this->initiator_spi = initiator_spi;
165 this->responder_spi = responder_spi;
166 this->role = role;
167
168 return (&this->public);
169 }