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