Moving charon to libcharon.
[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 METHOD(sender_t, send_, void,
73 private_sender_t *this, packet_t *packet)
74 {
75 host_t *src, *dst;
76
77 src = packet->get_source(packet);
78 dst = packet->get_destination(packet);
79 DBG1(DBG_NET, "sending packet: from %#H to %#H", src, dst);
80
81 if (this->send_delay)
82 {
83 usleep(this->send_delay * 1000);
84 }
85
86 this->mutex->lock(this->mutex);
87 this->list->insert_last(this->list, packet);
88 this->got->signal(this->got);
89 this->mutex->unlock(this->mutex);
90 }
91
92 /**
93 * Job callback function to send packets
94 */
95 static job_requeue_t send_packets(private_sender_t * this)
96 {
97 packet_t *packet;
98 bool oldstate;
99
100 this->mutex->lock(this->mutex);
101 while (this->list->get_count(this->list) == 0)
102 {
103 /* add cleanup handler, wait for packet, remove cleanup handler */
104 thread_cleanup_push((thread_cleanup_t)this->mutex->unlock, this->mutex);
105 oldstate = thread_cancelability(TRUE);
106
107 this->got->wait(this->got, this->mutex);
108
109 thread_cancelability(oldstate);
110 thread_cleanup_pop(FALSE);
111 }
112 this->list->remove_first(this->list, (void**)&packet);
113 this->sent->signal(this->sent);
114 this->mutex->unlock(this->mutex);
115
116 charon->socket->send(charon->socket, packet);
117 packet->destroy(packet);
118 return JOB_REQUEUE_DIRECT;
119 }
120
121 METHOD(sender_t, destroy, void,
122 private_sender_t *this)
123 {
124 /* send all packets in the queue */
125 this->mutex->lock(this->mutex);
126 while (this->list->get_count(this->list))
127 {
128 this->sent->wait(this->sent, this->mutex);
129 }
130 this->mutex->unlock(this->mutex);
131 this->job->cancel(this->job);
132 this->list->destroy(this->list);
133 this->got->destroy(this->got);
134 this->sent->destroy(this->sent);
135 this->mutex->destroy(this->mutex);
136 free(this);
137 }
138
139 /*
140 * Described in header.
141 */
142 sender_t * sender_create()
143 {
144 private_sender_t *this;
145
146 INIT(this,
147 .public = {
148 .send = _send_,
149 .destroy = _destroy,
150 },
151 .list = linked_list_create(),
152 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
153 .got = condvar_create(CONDVAR_TYPE_DEFAULT),
154 .sent = condvar_create(CONDVAR_TYPE_DEFAULT),
155 .job = callback_job_create((callback_job_cb_t)send_packets,
156 this, NULL, NULL),
157 .send_delay = lib->settings->get_int(lib->settings,
158 "charon.send_delay", 0),
159 );
160
161 charon->processor->queue_job(charon->processor, (job_t*)this->job);
162
163 return &this->public;
164 }
165