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