942401a8268d977da19cf68c980806e61bb92a6a
[strongswan.git] / src / charon / 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 * $Id$
17 */
18
19 #include <stdlib.h>
20 #include <pthread.h>
21
22 #include "sender.h"
23
24 #include <daemon.h>
25 #include <network/socket.h>
26 #include <processing/jobs/callback_job.h>
27
28
29 typedef struct private_sender_t private_sender_t;
30
31 /**
32 * Private data of a sender_t object.
33 */
34 struct private_sender_t {
35 /**
36 * Public part of a sender_t object.
37 */
38 sender_t public;
39
40 /**
41 * Sender threads job.
42 */
43 callback_job_t *job;
44
45 /**
46 * The packets are stored in a linked list
47 */
48 linked_list_t *list;
49
50 /**
51 * mutex to synchronize access to list
52 */
53 pthread_mutex_t mutex;
54
55 /**
56 * condvar to signal for packets in list
57 */
58 pthread_cond_t condvar;
59 };
60
61 /**
62 * implements sender_t.send
63 */
64 static void send_(private_sender_t *this, packet_t *packet)
65 {
66 host_t *src, *dst;
67
68 src = packet->get_source(packet);
69 dst = packet->get_destination(packet);
70 DBG1(DBG_NET, "sending packet: from %#H to %#H", src, dst);
71
72 pthread_mutex_lock(&this->mutex);
73 this->list->insert_last(this->list, packet);
74 pthread_mutex_unlock(&this->mutex);
75 pthread_cond_signal(&this->condvar);
76 }
77
78 /**
79 * Implementation of private_sender_t.send_packets.
80 */
81 static job_requeue_t send_packets(private_sender_t * this)
82 {
83 packet_t *packet;
84 int oldstate;
85
86 pthread_mutex_lock(&this->mutex);
87 while (this->list->get_count(this->list) == 0)
88 {
89 /* add cleanup handler, wait for packet, remove cleanup handler */
90 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&this->mutex);
91 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
92
93 pthread_cond_wait(&this->condvar, &this->mutex);
94
95 pthread_setcancelstate(oldstate, NULL);
96 pthread_cleanup_pop(0);
97 }
98 this->list->remove_first(this->list, (void**)&packet);
99 pthread_mutex_unlock(&this->mutex);
100
101 charon->socket->send(charon->socket, packet);
102 packet->destroy(packet);
103 return JOB_REQUEUE_DIRECT;
104 }
105
106 /**
107 * Implementation of sender_t.destroy.
108 */
109 static void destroy(private_sender_t *this)
110 {
111 /* send all packets in the queue */
112 while (this->list->get_count(this->list))
113 {
114 sched_yield();
115 }
116 this->job->cancel(this->job);
117 this->list->destroy(this->list);
118 free(this);
119 }
120
121 /*
122 * Described in header.
123 */
124 sender_t * sender_create()
125 {
126 private_sender_t *this = malloc_thing(private_sender_t);
127
128 this->public.send = (void(*)(sender_t*,packet_t*))send_;
129 this->public.destroy = (void(*)(sender_t*)) destroy;
130
131 this->list = linked_list_create();
132 pthread_mutex_init(&this->mutex, NULL);
133 pthread_cond_init(&this->condvar, NULL);
134
135 this->job = callback_job_create((callback_job_cb_t)send_packets,
136 this, NULL, NULL);
137 charon->processor->queue_job(charon->processor, (job_t*)this->job);
138
139 return &this->public;
140 }
141