aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/sa/transactions/dead_peer_detection.c
blob: c7f5db604d63a6d38e6a2b0bebe47bd4f269eee9 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
 * @file dead_peer_detection.c
 *
 * @brief Implementation of the dead_peer_detection transaction.
 *
 */

/*
 * Copyright (C) 2006 Martin Willi
 * 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 "dead_peer_detection.h"

#include <daemon.h>


typedef struct private_dead_peer_detection_t private_dead_peer_detection_t;

/**
 * Private members of a dead_peer_detection_t object..
 */
struct private_dead_peer_detection_t {
	
	/**
	 * Public methods and transaction_t interface.
	 */
	dead_peer_detection_t public;
	
	/**
	 * Assigned IKE_SA.
	 */
	ike_sa_t *ike_sa;
	
	/**
	 * Message sent by our peer, if already generated
	 */
	message_t *message;
	
	/**
	 * Message ID this transaction uses
	 */
	u_int32_t message_id;
	
	/**
	 * Times we did send the request
	 */
	u_int32_t requested;
	
	/**
	 * Assigned logger.
	 */
	logger_t *logger;
};

/**
 * Implementation of transaction_t.get_message_id.
 */
static u_int32_t get_message_id(private_dead_peer_detection_t *this)
{
	return this->message_id;
}

/**
 * Implementation of transaction_t.requested.
 */
static u_int32_t requested(private_dead_peer_detection_t *this)
{
	return this->requested++;
}

/**
 * Implementation of transaction_t.get_request.
 */
static status_t get_request(private_dead_peer_detection_t *this, message_t **result)
{
	message_t *request;
	connection_t *connection;
	host_t *me, *other;
	
	/* check if we already have built a message (retransmission) */
	if (this->message)
	{
		*result = this->message;
		return SUCCESS;
	}
	
	connection = this->ike_sa->get_connection(this->ike_sa);
	me = connection->get_my_host(connection);
	other = connection->get_other_host(connection);
	
	/* build the request */
	request = message_create();
	request->set_source(request, me->clone(me));
	request->set_destination(request, other->clone(other));
	request->set_exchange_type(request, INFORMATIONAL);
	request->set_request(request, TRUE);
	this->message_id = this->ike_sa->get_next_message_id(this->ike_sa);
	request->set_message_id(request, this->message_id);
	request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
	/* apply for caller */
	*result = request;
	/* store for retransmission */
	this->message = request;
	
	return SUCCESS;
}

/**
 * Implementation of transaction_t.get_response.
 */
static status_t get_response(private_dead_peer_detection_t *this, message_t *request, 
							 message_t **result, transaction_t **next)
{
	host_t *me, *other;
	message_t *response;
	connection_t *connection;
	
	/* check if we already have built a response (retransmission) */
	if (this->message)
	{
		*result = this->message;
		return SUCCESS;
	}
	
	connection = this->ike_sa->get_connection(this->ike_sa);
	me = connection->get_my_host(connection);
	other = connection->get_other_host(connection);
	this->message_id = request->get_message_id(request);
	
	/* set up response */
	response = message_create();
	response->set_source(response, me->clone(me));
	response->set_destination(response, other->clone(other));
	response->set_exchange_type(response, INFORMATIONAL);
	response->set_request(response, FALSE);
	response->set_message_id(response, this->message_id);
	response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
	this->message = response;
	*result = response;
	
	return SUCCESS;
}


/**
 * Implementation of transaction_t.conclude
 */
static status_t conclude(private_dead_peer_detection_t *this, message_t *response, 
						 transaction_t **transaction)
{
	return SUCCESS;
}

/**
 * implements transaction_t.destroy
 */
static void destroy(private_dead_peer_detection_t *this)
{
	if (this->message)
	{
		this->message->destroy(this->message);
	}
	free(this);
}

/*
 * Described in header.
 */
dead_peer_detection_t *dead_peer_detection_create(ike_sa_t *ike_sa)
{
	private_dead_peer_detection_t *this = malloc_thing(private_dead_peer_detection_t);
	
	/* transaction interface functions */
	this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
	this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
	this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
	this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
	this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
	this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
	
	/* private data */
	this->ike_sa = ike_sa;
	this->message_id = 0;
	this->message = NULL;
	this->requested = 0;
	this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
	
	return &this->public;
}