- kernel interface hacks, works partially
[strongswan.git] / Source / charon / threads / sender.c
1 /**
2 * @file sender.c
3 *
4 * @brief Implementation of sender_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stdlib.h>
24 #include <pthread.h>
25
26 #include "sender.h"
27
28 #include <daemon.h>
29 #include <network/socket.h>
30 #include <network/packet.h>
31 #include <queues/send_queue.h>
32 #include <utils/allocator.h>
33 #include <utils/logger_manager.h>
34
35 typedef struct private_sender_t private_sender_t;
36
37 /**
38 * Private data of a sender object
39 */
40 struct private_sender_t {
41 /**
42 * Public part of a sender object
43 */
44 sender_t public;
45
46 /**
47 * Assigned thread to the sender_t object
48 */
49 pthread_t assigned_thread;
50
51 /**
52 * @brief The threads function, sends out packets.
53 *
54 * @param this assigned sender object
55 */
56 void (*send_packets) (private_sender_t * this);
57
58 /**
59 * logger for this sender
60 */
61 logger_t *logger;
62
63 };
64
65 /**
66 * implements private_sender_t.send_packets
67 */
68 static void send_packets(private_sender_t * this)
69 {
70 packet_t * current_packet;
71 status_t status;
72
73 /* cancellation disabled by default */
74 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
75
76 this->logger->log(this->logger, CONTROL, "sender thread running, thread_id %u", (int)pthread_self());
77
78 while (1)
79 {
80 current_packet = charon->send_queue->get(charon->send_queue);
81 this->logger->log(this->logger, CONTROL|MORE, "got a packet, sending it");
82 status = charon->socket->send(charon->socket,current_packet);
83 if (status != SUCCESS)
84 {
85 this->logger->log(this->logger, ERROR, "sending failed, socket returned %s",
86 mapping_find(status_m, status));
87 }
88 current_packet->destroy(current_packet);
89 }
90 }
91
92 /**
93 * implements sender_t.destroy
94 */
95 static void destroy(private_sender_t *this)
96 {
97 this->logger->log(this->logger, CONTROL | MORE, "Going to terminate sender thread");
98 pthread_cancel(this->assigned_thread);
99
100 pthread_join(this->assigned_thread, NULL);
101 this->logger->log(this->logger, CONTROL | MORE, "Sender thread terminated");
102
103 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
104
105 allocator_free(this);
106 }
107
108 /*
109 * see header
110 */
111 sender_t * sender_create()
112 {
113 private_sender_t *this = allocator_alloc_thing(private_sender_t);
114
115 this->send_packets = send_packets;
116 this->public.destroy = (void(*)(sender_t*)) destroy;
117
118 this->logger = charon->logger_manager->create_logger(charon->logger_manager, SENDER, NULL);
119
120 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->send_packets, this) != 0)
121 {
122 this->logger->log(this->logger, ERROR, "Sender thread could not be created");
123 allocator_free(this);
124 charon->kill(charon, "Unable to create sender thread");
125 }
126
127 return &(this->public);
128 }