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