- globals packed in a daemon
[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 #include <unistd.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/allocator.h>
34 #include <utils/logger_manager.h>
35
36 typedef struct private_sender_t private_sender_t;
37
38 /**
39 * Private data of a sender object
40 */
41 struct private_sender_t {
42 /**
43 * Public part of a sender object
44 */
45 sender_t public;
46
47 /**
48 * Assigned thread to the sender_t object
49 */
50 pthread_t assigned_thread;
51
52 /**
53 * @brief The threads function, sends out packets.
54 *
55 * @param this assigned sender object
56 */
57 void (*send_packets) (private_sender_t * this);
58
59 /**
60 * logger for this sender
61 */
62 logger_t *logger;
63
64 };
65
66 /**
67 * implements 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, pid %d", getpid());
78
79 while (1)
80 {
81 current_packet = charon->send_queue->get(charon->send_queue);
82 this->logger->log(this->logger, CONTROL|MORE, "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 * implements sender_t.destroy
95 */
96 static void destroy(private_sender_t *this)
97 {
98 this->logger->log(this->logger, CONTROL | MORE, "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 | MORE, "Sender thread terminated");
103
104 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
105
106 allocator_free(this);
107 }
108
109 /*
110 * see header
111 */
112 sender_t * sender_create()
113 {
114 private_sender_t *this = allocator_alloc_thing(private_sender_t);
115
116 this->send_packets = send_packets;
117 this->public.destroy = (void(*)(sender_t*)) destroy;
118
119 this->logger = charon->logger_manager->create_logger(charon->logger_manager, SENDER, NULL);
120
121 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->send_packets, this) != 0)
122 {
123 this->logger->log(this->logger, ERROR, "Sender thread could not be created");
124 allocator_free(this);
125 return NULL;
126 }
127
128 return &(this->public);
129 }