summaryrefslogtreecommitdiff
path: root/src/libcharon/sa/ike_sa_id.c
blob: bea4c21249b85960920a4c5e6a32398349362dad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2005-2006 Martin Willi
 * Copyright (C) 2005 Jan Hutter
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "ike_sa_id.h"

#include <stdio.h>


typedef struct private_ike_sa_id_t private_ike_sa_id_t;

/**
 * Private data of an ike_sa_id_t object.
 */
struct private_ike_sa_id_t {
	/**
	 * Public interface of ike_sa_id_t.
	 */
	ike_sa_id_t public;

	 /**
	  * SPI of Initiator.
	  */
	u_int64_t initiator_spi;

	 /**
	  * SPI of Responder.
	  */
	u_int64_t responder_spi;

	/**
	 * Role for specific IKE_SA.
	 */
	bool is_initiator_flag;
};

METHOD(ike_sa_id_t, set_responder_spi, void,
	private_ike_sa_id_t *this, u_int64_t responder_spi)
{
	this->responder_spi = responder_spi;
}

METHOD(ike_sa_id_t, set_initiator_spi, void,
	private_ike_sa_id_t *this, u_int64_t initiator_spi)
{
	this->initiator_spi = initiator_spi;
}

METHOD(ike_sa_id_t, get_initiator_spi, u_int64_t,
	private_ike_sa_id_t *this)
{
	return this->initiator_spi;
}

METHOD(ike_sa_id_t, get_responder_spi, u_int64_t,
	private_ike_sa_id_t *this)
{
	return this->responder_spi;
}

METHOD(ike_sa_id_t, equals, bool,
	private_ike_sa_id_t *this, private_ike_sa_id_t *other)
{
	if (other == NULL)
	{
		return FALSE;
	}
	if ((this->is_initiator_flag == other->is_initiator_flag) &&
		(this->initiator_spi == other->initiator_spi) &&
		(this->responder_spi == other->responder_spi))
	{
		/* private_ike_sa_id's are equal */
		return TRUE;
	}
	else
	{
		/* private_ike_sa_id's are not equal */
		return FALSE;
	}
}

METHOD(ike_sa_id_t, replace_values, void,
	private_ike_sa_id_t *this, private_ike_sa_id_t *other)
{
	this->initiator_spi = other->initiator_spi;
	this->responder_spi = other->responder_spi;
	this->is_initiator_flag = other->is_initiator_flag;
}

METHOD(ike_sa_id_t, is_initiator, bool,
	private_ike_sa_id_t *this)
{
	return this->is_initiator_flag;
}

METHOD(ike_sa_id_t, switch_initiator, bool,
	private_ike_sa_id_t *this)
{
	if (this->is_initiator_flag)
	{
		this->is_initiator_flag = FALSE;
	}
	else
	{
		this->is_initiator_flag = TRUE;
	}
	return this->is_initiator_flag;
}

METHOD(ike_sa_id_t, clone_, ike_sa_id_t*,
	private_ike_sa_id_t *this)
{
	return ike_sa_id_create(this->initiator_spi, this->responder_spi,
							this->is_initiator_flag);
}

METHOD(ike_sa_id_t, destroy, void,
	private_ike_sa_id_t *this)
{
	free(this);
}

/*
 * Described in header.
 */
ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi,
							   bool is_initiator_flag)
{
	private_ike_sa_id_t *this;

	INIT(this,
		.public = {
			.set_responder_spi = _set_responder_spi,
			.set_initiator_spi = _set_initiator_spi,
			.get_responder_spi = _get_responder_spi,
			.get_initiator_spi = _get_initiator_spi,
			.equals = (void*)_equals,
			.replace_values = (void*)_replace_values,
			.is_initiator = _is_initiator,
			.switch_initiator = _switch_initiator,
			.clone = _clone_,
			.destroy = _destroy,
		},
		.initiator_spi = initiator_spi,
		.responder_spi = responder_spi,
		.is_initiator_flag = is_initiator_flag,
	);

	return &this->public;
}