summaryrefslogtreecommitdiff
path: root/src/charon/sa/ike_sa_id.c
blob: 94c5405f2fd649ece124ad8160e7a8d5575daa03 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * 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;
};

/**
 * Implementation of ike_sa_id_t.set_responder_spi.
 */
static void set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi)
{
	this->responder_spi = responder_spi;
}

/**
 * Implementation of ike_sa_id_t.set_initiator_spi.
 */
static void set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi)
{
	this->initiator_spi = initiator_spi;
}

/**
 * Implementation of ike_sa_id_t.get_initiator_spi.
 */
static u_int64_t get_initiator_spi (private_ike_sa_id_t *this)
{
	return this->initiator_spi;
}

/**
 * Implementation of ike_sa_id_t.get_responder_spi.
 */
static u_int64_t get_responder_spi (private_ike_sa_id_t *this)
{
	return this->responder_spi;
}

/**
 * Implementation of ike_sa_id_t.equals.
 */
static bool equals (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;
	}
}

/**
 * Implementation of ike_sa_id_t.replace_values.
 */
static void replace_values(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;
}

/**
 * Implementation of ike_sa_id_t.is_initiator.
 */
static bool is_initiator(private_ike_sa_id_t *this)
{
	return this->is_initiator_flag;
}

/**
 * Implementation of ike_sa_id_t.switch_initiator.
 */
static bool switch_initiator(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;
}

/**
 * Implementation of ike_sa_id_t.clone.
 */
static ike_sa_id_t* clone_(private_ike_sa_id_t *this)
{
	return ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag);
}

/**
 * Implementation of ike_sa_id_t.destroy.
 */
static void destroy(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 = malloc_thing(private_ike_sa_id_t);

	/* public functions */
	this->public.set_responder_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi;
	this->public.set_initiator_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi;
	this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi;
	this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi;
	this->public.equals = (bool(*)(ike_sa_id_t*,ike_sa_id_t*)) equals;
	this->public.replace_values = (void(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values;
	this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator;
	this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator;
	this->public.clone = (ike_sa_id_t*(*)(ike_sa_id_t*)) clone_;
	this->public.destroy = (void(*)(ike_sa_id_t*))destroy;

	/* private data */
	this->initiator_spi = initiator_spi;
	this->responder_spi = responder_spi;
	this->is_initiator_flag = is_initiator_flag;

	return &this->public;
}