get rid of unused iterator hook functions
[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 added to list
57 */
58 pthread_cond_t gotone;
59
60 /**
61 * condvar to signal for packets sent
62 */
63 pthread_cond_t sentone;
64 };
65
66 /**
67 * implements sender_t.send
68 */
69 static void send_(private_sender_t *this, packet_t *packet)
70 {
71 host_t *src, *dst;
72
73 src = packet->get_source(packet);
74 dst = packet->get_destination(packet);
75 DBG1(DBG_NET, "sending packet: from %#H to %#H", src, dst);
76
77 pthread_mutex_lock(&this->mutex);
78 this->list->insert_last(this->list, packet);
79 pthread_cond_signal(&this->gotone);
80 pthread_mutex_unlock(&this->mutex);
81 }
82
83 /**
84 * Implementation of private_sender_t.send_packets.
85 */
86 static job_requeue_t send_packets(private_sender_t * this)
87 {
88 packet_t *packet;
89 int oldstate;
90
91 pthread_mutex_lock(&this->mutex);
92 while (this->list->get_count(this->list) == 0)
93 {
94 /* add cleanup handler, wait for packet, remove cleanup handler */
95 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&this->mutex);
96 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
97
98 pthread_cond_wait(&this->gotone, &this->mutex);
99
100 pthread_setcancelstate(oldstate, NULL);
101 pthread_cleanup_pop(0);
102 }
103 this->list->remove_first(this->list, (void**)&packet);
104 pthread_cond_signal(&this->sentone);
105 pthread_mutex_unlock(&this->mutex);
106
107 charon->socket->send(charon->socket, packet);
108 packet->destroy(packet);
109 return JOB_REQUEUE_DIRECT;
110 }
111
112 /**
113 * Implementation of sender_t.destroy.
114 */
115 static void destroy(private_sender_t *this)
116 {
117 /* send all packets in the queue */
118 pthread_mutex_lock(&this->mutex);
119 while (this->list->get_count(this->list))
120 {
121 pthread_cond_wait(&this->sentone, &this->mutex);
122 }
123 pthread_mutex_unlock(&this->mutex);
124 pthread_mutex_destroy(&this->mutex);
125 this->job->cancel(this->job);
126 this->list->destroy(this->list);
127 free(this);
128 }
129
130 /*
131 * Described in header.
132 */
133 sender_t * sender_create()
134 {
135 private_sender_t *this = malloc_thing(private_sender_t);
136
137 this->public.send = (void(*)(sender_t*,packet_t*))send_;
138 this->public.destroy = (void(*)(sender_t*)) destroy;
139
140 this->list = linked_list_create();
141 pthread_mutex_init(&this->mutex, NULL);
142 pthread_cond_init(&this->gotone, NULL);
143 pthread_cond_init(&this->sentone, NULL);
144
145 this->job = callback_job_create((callback_job_cb_t)send_packets,
146 this, NULL, NULL);
147 charon->processor->queue_job(charon->processor, (job_t*)this->job);
148
149 return &this->public;
150 }
151