]> git.ipfire.org Git - people/ms/strongswan.git/blob - src/charon/charon/threads/sender.c
42d11beb959c5695c3f151f1c51192e3acedc53b
[people/ms/strongswan.git] / src / charon / 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/logger_manager.h>
33
34
35 typedef struct private_sender_t private_sender_t;
36
37 /**
38 * Private data of a sender_t object.
39 */
40 struct private_sender_t {
41 /**
42 * Public part of a sender_t object.
43 */
44 sender_t public;
45
46 /**
47 * Assigned thread.
48 */
49 pthread_t assigned_thread;
50
51 /**
52 * @brief The thread function, sends out packets.
53 *
54 * @param this calling object
55 */
56 void (*send_packets) (private_sender_t * this);
57
58 /**
59 * A logger for this sender_t object.
60 */
61 logger_t *logger;
62
63 };
64
65 /**
66 * Implementation of 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|LEVEL1, "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 * Implementation of sender_t.destroy.
94 */
95 static void destroy(private_sender_t *this)
96 {
97 this->logger->log(this->logger, CONTROL | LEVEL1, "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 | LEVEL1, "Sender thread terminated");
102
103 free(this);
104 }
105
106 /*
107 * Described in header.
108 */
109 sender_t * sender_create()
110 {
111 private_sender_t *this = malloc_thing(private_sender_t);
112
113 this->send_packets = send_packets;
114 this->public.destroy = (void(*)(sender_t*)) destroy;
115
116 this->logger = logger_manager->get_logger(logger_manager, SENDER);
117
118 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->send_packets, this) != 0)
119 {
120 this->logger->log(this->logger, ERROR, "Sender thread could not be created");
121 free(this);
122 charon->kill(charon, "Unable to create sender thread");
123 }
124
125 return &(this->public);
126 }