removed send_queue, handled internally in sender_t know
[strongswan.git] / src / charon / threads / sender.c
1 /**
2 * @file sender.c
3 *
4 * @brief Implementation of sender_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <stdlib.h>
25 #include <pthread.h>
26
27 #include "sender.h"
28
29 #include <daemon.h>
30 #include <network/socket.h>
31
32
33 typedef struct private_sender_t private_sender_t;
34
35 /**
36 * Private data of a sender_t object.
37 */
38 struct private_sender_t {
39 /**
40 * Public part of a sender_t object.
41 */
42 sender_t public;
43
44 /**
45 * Assigned thread.
46 */
47 pthread_t assigned_thread;
48
49 /**
50 * The packets are stored in a linked list
51 */
52 linked_list_t *list;
53
54 /**
55 * mutex to synchronize access to list
56 */
57 pthread_mutex_t mutex;
58
59 /**
60 * condvar to signal for packets in list
61 */
62 pthread_cond_t condvar;
63 };
64
65 /**
66 * implements sender_t.send
67 */
68 static void send_(private_sender_t *this, packet_t *packet)
69 {
70 host_t *src, *dst;
71
72 src = packet->get_source(packet);
73 dst = packet->get_destination(packet);
74 DBG1(DBG_NET, "sending packet: from %#H to %#H", src, dst);
75
76 pthread_mutex_lock(&this->mutex);
77 this->list->insert_last(this->list, packet);
78 pthread_mutex_unlock(&this->mutex);
79 pthread_cond_signal(&this->condvar);
80 }
81
82 /**
83 * Implementation of private_sender_t.send_packets.
84 */
85 static void send_packets(private_sender_t * this)
86 {
87
88 /* cancellation disabled by default */
89 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
90 DBG1(DBG_NET, "sender thread running, thread_ID: %06u", (int)pthread_self());
91
92 while (TRUE)
93 {
94 packet_t *packet;
95 int oldstate;
96
97 pthread_mutex_lock(&this->mutex);
98 /* go to wait while no packets available */
99 while (this->list->get_count(this->list) == 0)
100 {
101 /* add cleanup handler, wait for packet, remove cleanup handler */
102 pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&this->mutex);
103 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
104 pthread_cond_wait(&this->condvar, &this->mutex);
105
106 pthread_setcancelstate(oldstate, NULL);
107 pthread_cleanup_pop(0);
108 }
109 this->list->remove_first(this->list, (void**)&packet);
110 pthread_mutex_unlock(&this->mutex);
111
112 charon->socket->send(charon->socket, packet);
113 packet->destroy(packet);
114 }
115 }
116
117 /**
118 * Implementation of sender_t.destroy.
119 */
120 static void destroy(private_sender_t *this)
121 {
122 pthread_cancel(this->assigned_thread);
123 pthread_join(this->assigned_thread, NULL);
124 this->list->destroy_offset(this->list, offsetof(packet_t, destroy));
125 free(this);
126 }
127
128 /*
129 * Described in header.
130 */
131 sender_t * sender_create()
132 {
133 private_sender_t *this = malloc_thing(private_sender_t);
134
135 this->public.send = (void(*)(sender_t*,packet_t*))send_;
136 this->public.destroy = (void(*)(sender_t*)) destroy;
137
138 this->list = linked_list_create();
139 pthread_mutex_init(&this->mutex, NULL);
140 pthread_cond_init(&this->condvar, NULL);
141
142 if (pthread_create(&this->assigned_thread, NULL,
143 (void*)send_packets, this) != 0)
144 {
145 charon->kill(charon, "unable to create sender thread");
146 }
147
148 return &(this->public);
149 }