75635d2e32a7bd2ca72286ad946143bd69f1fb9b
[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 * The packets are stored in a linked list
43 */
44 linked_list_t *list;
45
46 /**
47 * mutex to synchronize access to list
48 */
49 mutex_t *mutex;
50
51 /**
52 * condvar to signal for packets added to list
53 */
54 condvar_t *got;
55
56 /**
57 * condvar to signal for packets sent
58 */
59 condvar_t *sent;
60
61 /**
62 * Delay for sending outgoing packets, to simulate larger RTT
63 */
64 int send_delay;
65
66 /**
67 * Specific message type to delay, 0 for any
68 */
69 int send_delay_type;
70
71 /**
72 * Delay request messages?
73 */
74 bool send_delay_request;
75
76 /**
77 * Delay response messages?
78 */
79 bool send_delay_response;
80 };
81
82 METHOD(sender_t, send_, void,
83 private_sender_t *this, packet_t *packet)
84 {
85 host_t *src, *dst;
86
87 src = packet->get_source(packet);
88 dst = packet->get_destination(packet);
89 DBG1(DBG_NET, "sending packet: from %#H to %#H", src, dst);
90
91 if (this->send_delay)
92 {
93 message_t *message;
94
95 message = message_create_from_packet(packet->clone(packet));
96 if (message->parse_header(message) == SUCCESS)
97 {
98 if (this->send_delay_type == 0 ||
99 this->send_delay_type == message->get_exchange_type(message))
100 {
101 if ((message->get_request(message) && this->send_delay_request) ||
102 (!message->get_request(message) && this->send_delay_response))
103 {
104 DBG1(DBG_NET, "using send delay: %dms", this->send_delay);
105 usleep(this->send_delay * 1000);
106 }
107 }
108 }
109 message->destroy(message);
110 }
111
112 this->mutex->lock(this->mutex);
113 this->list->insert_last(this->list, packet);
114 this->got->signal(this->got);
115 this->mutex->unlock(this->mutex);
116 }
117
118 /**
119 * Job callback function to send packets
120 */
121 static job_requeue_t send_packets(private_sender_t * this)
122 {
123 packet_t *packet;
124 bool oldstate;
125
126 this->mutex->lock(this->mutex);
127 while (this->list->get_count(this->list) == 0)
128 {
129 /* add cleanup handler, wait for packet, remove cleanup handler */
130 thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
131 oldstate = thread_cancelability(TRUE);
132
133 this->got->wait(this->got, this->mutex);
134
135 thread_cancelability(oldstate);
136 thread_cleanup_pop(FALSE);
137 }
138 this->list->remove_first(this->list, (void**)&packet);
139 this->sent->signal(this->sent);
140 this->mutex->unlock(this->mutex);
141
142 charon->socket->send(charon->socket, packet);
143 packet->destroy(packet);
144 return JOB_REQUEUE_DIRECT;
145 }
146
147 METHOD(sender_t, flush, void,
148 private_sender_t *this)
149 {
150 /* send all packets in the queue */
151 this->mutex->lock(this->mutex);
152 while (this->list->get_count(this->list))
153 {
154 this->sent->wait(this->sent, this->mutex);
155 }
156 this->mutex->unlock(this->mutex);
157 }
158
159 METHOD(sender_t, destroy, void,
160 private_sender_t *this)
161 {
162 this->list->destroy_offset(this->list, offsetof(packet_t, destroy));
163 this->got->destroy(this->got);
164 this->sent->destroy(this->sent);
165 this->mutex->destroy(this->mutex);
166 free(this);
167 }
168
169 /*
170 * Described in header.
171 */
172 sender_t * sender_create()
173 {
174 private_sender_t *this;
175
176 INIT(this,
177 .public = {
178 .send = _send_,
179 .flush = _flush,
180 .destroy = _destroy,
181 },
182 .list = linked_list_create(),
183 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
184 .got = condvar_create(CONDVAR_TYPE_DEFAULT),
185 .sent = condvar_create(CONDVAR_TYPE_DEFAULT),
186 .send_delay = lib->settings->get_int(lib->settings,
187 "%s.send_delay", 0, charon->name),
188 .send_delay_type = lib->settings->get_int(lib->settings,
189 "%s.send_delay_type", 0, charon->name),
190 .send_delay_request = lib->settings->get_bool(lib->settings,
191 "%s.send_delay_request", TRUE, charon->name),
192 .send_delay_response = lib->settings->get_bool(lib->settings,
193 "%s.send_delay_response", TRUE, charon->name),
194 );
195
196 lib->processor->queue_job(lib->processor,
197 (job_t*)callback_job_create_with_prio((callback_job_cb_t)send_packets,
198 this, NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
199
200 return &this->public;
201 }
202