Added a dedicated sender flush method, delay sender destruction until users gone
[strongswan.git] / src / libcharon / network / sender.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <unistd.h>
18 #include <stdlib.h>
19
20 #include "sender.h"
21
22 #include <daemon.h>
23 #include <network/socket.h>
24 #include <processing/jobs/callback_job.h>
25 #include <threading/thread.h>
26 #include <threading/condvar.h>
27 #include <threading/mutex.h>
28
29
30 typedef struct private_sender_t private_sender_t;
31
32 /**
33 * Private data of a sender_t object.
34 */
35 struct private_sender_t {
36 /**
37 * Public part of a sender_t object.
38 */
39 sender_t public;
40
41 /**
42 * Sender threads job.
43 */
44 callback_job_t *job;
45
46 /**
47 * The packets are stored in a linked list
48 */
49 linked_list_t *list;
50
51 /**
52 * mutex to synchronize access to list
53 */
54 mutex_t *mutex;
55
56 /**
57 * condvar to signal for packets added to list
58 */
59 condvar_t *got;
60
61 /**
62 * condvar to signal for packets sent
63 */
64 condvar_t *sent;
65
66 /**
67 * Delay for sending outgoing packets, to simulate larger RTT
68 */
69 int send_delay;
70
71 /**
72 * Specific message type to delay, 0 for any
73 */
74 int send_delay_type;
75
76 /**
77 * Delay request messages?
78 */
79 bool send_delay_request;
80
81 /**
82 * Delay response messages?
83 */
84 bool send_delay_response;
85 };
86
87 METHOD(sender_t, send_, void,
88 private_sender_t *this, packet_t *packet)
89 {
90 host_t *src, *dst;
91
92 src = packet->get_source(packet);
93 dst = packet->get_destination(packet);
94 DBG1(DBG_NET, "sending packet: from %#H to %#H", src, dst);
95
96 if (this->send_delay)
97 {
98 message_t *message;
99
100 message = message_create_from_packet(packet->clone(packet));
101 if (message->parse_header(message) == SUCCESS)
102 {
103 if (this->send_delay_type == 0 ||
104 this->send_delay_type == message->get_exchange_type(message))
105 {
106 if ((message->get_request(message) && this->send_delay_request) ||
107 (!message->get_request(message) && this->send_delay_response))
108 {
109 DBG1(DBG_NET, "using send delay: %dms", this->send_delay);
110 usleep(this->send_delay * 1000);
111 }
112 }
113 }
114 message->destroy(message);
115 }
116
117 this->mutex->lock(this->mutex);
118 this->list->insert_last(this->list, packet);
119 this->got->signal(this->got);
120 this->mutex->unlock(this->mutex);
121 }
122
123 /**
124 * Job callback function to send packets
125 */
126 static job_requeue_t send_packets(private_sender_t * this)
127 {
128 packet_t *packet;
129 bool oldstate;
130
131 this->mutex->lock(this->mutex);
132 while (this->list->get_count(this->list) == 0)
133 {
134 /* add cleanup handler, wait for packet, remove cleanup handler */
135 thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
136 oldstate = thread_cancelability(TRUE);
137
138 this->got->wait(this->got, this->mutex);
139
140 thread_cancelability(oldstate);
141 thread_cleanup_pop(FALSE);
142 }
143 this->list->remove_first(this->list, (void**)&packet);
144 this->sent->signal(this->sent);
145 this->mutex->unlock(this->mutex);
146
147 charon->socket->send(charon->socket, packet);
148 packet->destroy(packet);
149 return JOB_REQUEUE_DIRECT;
150 }
151
152 METHOD(sender_t, flush, void,
153 private_sender_t *this)
154 {
155 /* send all packets in the queue */
156 this->mutex->lock(this->mutex);
157 while (this->list->get_count(this->list))
158 {
159 this->sent->wait(this->sent, this->mutex);
160 }
161 this->mutex->unlock(this->mutex);
162 }
163
164 METHOD(sender_t, destroy, void,
165 private_sender_t *this)
166 {
167 this->job->cancel(this->job);
168 this->list->destroy_offset(this->list, offsetof(packet_t, destroy));
169 this->got->destroy(this->got);
170 this->sent->destroy(this->sent);
171 this->mutex->destroy(this->mutex);
172 free(this);
173 }
174
175 /*
176 * Described in header.
177 */
178 sender_t * sender_create()
179 {
180 private_sender_t *this;
181
182 INIT(this,
183 .public = {
184 .send = _send_,
185 .flush = _flush,
186 .destroy = _destroy,
187 },
188 .list = linked_list_create(),
189 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
190 .got = condvar_create(CONDVAR_TYPE_DEFAULT),
191 .sent = condvar_create(CONDVAR_TYPE_DEFAULT),
192 .job = callback_job_create_with_prio((callback_job_cb_t)send_packets,
193 this, NULL, NULL, JOB_PRIO_CRITICAL),
194 .send_delay = lib->settings->get_int(lib->settings,
195 "charon.send_delay", 0),
196 .send_delay_type = lib->settings->get_int(lib->settings,
197 "charon.send_delay_type", 0),
198 .send_delay_request = lib->settings->get_bool(lib->settings,
199 "charon.send_delay_request", TRUE),
200 .send_delay_response = lib->settings->get_int(lib->settings,
201 "charon.send_delay_response", TRUE),
202 );
203
204 lib->processor->queue_job(lib->processor, (job_t*)this->job);
205
206 return &this->public;
207 }
208