129c4f3604c7b6636626b995fbdc97d9896a3905
[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 #include <network/packet.h>
32 #include <queues/send_queue.h>
33 #include <utils/logger_manager.h>
34
35
36 typedef struct private_sender_t private_sender_t;
37
38 /**
39 * Private data of a sender_t object.
40 */
41 struct private_sender_t {
42 /**
43 * Public part of a sender_t object.
44 */
45 sender_t public;
46
47 /**
48 * Assigned thread.
49 */
50 pthread_t assigned_thread;
51
52 /**
53 * @brief The thread function, sends out packets.
54 *
55 * @param this calling object
56 */
57 void (*send_packets) (private_sender_t * this);
58
59 /**
60 * A logger for this sender_t object.
61 */
62 logger_t *logger;
63
64 };
65
66 /**
67 * Implementation of private_sender_t.send_packets.
68 */
69 static void send_packets(private_sender_t * this)
70 {
71 packet_t * current_packet;
72 status_t status;
73
74 /* cancellation disabled by default */
75 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
76
77 this->logger->log(this->logger, CONTROL, "sender thread running, thread_ID: %06u", (int)pthread_self());
78
79 while (1)
80 {
81 current_packet = charon->send_queue->get(charon->send_queue);
82 this->logger->log(this->logger, CONTROL|LEVEL1, "Got a packet, sending it");
83 status = charon->socket->send(charon->socket,current_packet);
84 if (status != SUCCESS)
85 {
86 this->logger->log(this->logger, ERROR, "Sending failed, socket returned %s",
87 mapping_find(status_m, status));
88 }
89 current_packet->destroy(current_packet);
90 }
91 }
92
93 /**
94 * Implementation of sender_t.destroy.
95 */
96 static void destroy(private_sender_t *this)
97 {
98 this->logger->log(this->logger, CONTROL | LEVEL1, "Going to terminate sender thread");
99 pthread_cancel(this->assigned_thread);
100
101 pthread_join(this->assigned_thread, NULL);
102 this->logger->log(this->logger, CONTROL | LEVEL1, "Sender thread terminated");
103
104 free(this);
105 }
106
107 /*
108 * Described in header.
109 */
110 sender_t * sender_create()
111 {
112 private_sender_t *this = malloc_thing(private_sender_t);
113
114 this->send_packets = send_packets;
115 this->public.destroy = (void(*)(sender_t*)) destroy;
116
117 this->logger = logger_manager->get_logger(logger_manager, SENDER);
118
119 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->send_packets, this) != 0)
120 {
121 this->logger->log(this->logger, ERROR, "Sender thread could not be created");
122 free(this);
123 charon->kill(charon, "Unable to create sender thread");
124 }
125
126 return &(this->public);
127 }