further work done for simultaneous rekeying/delete
[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 * A logger for this sender_t object.
54 */
55 logger_t *logger;
56
57 };
58
59 /**
60 * Implementation of private_sender_t.send_packets.
61 */
62 static void send_packets(private_sender_t * this)
63 {
64 packet_t *current_packet;
65 status_t status;
66
67 /* cancellation disabled by default */
68 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
69
70 this->logger->log(this->logger, CONTROL, "sender thread running, thread_ID: %06u", (int)pthread_self());
71
72 while (TRUE)
73 {
74 current_packet = charon->send_queue->get(charon->send_queue);
75 this->logger->log(this->logger, CONTROL|LEVEL1, "Got a packet, sending it");
76 status = charon->socket->send(charon->socket, current_packet);
77 if (status != SUCCESS)
78 {
79 this->logger->log(this->logger, ERROR, "Sending failed, socket returned %s",
80 mapping_find(status_m, status));
81 }
82 current_packet->destroy(current_packet);
83 }
84 }
85
86 /**
87 * Implementation of sender_t.destroy.
88 */
89 static void destroy(private_sender_t *this)
90 {
91 this->logger->log(this->logger, CONTROL | LEVEL1, "Going to terminate sender thread");
92 pthread_cancel(this->assigned_thread);
93
94 pthread_join(this->assigned_thread, NULL);
95 this->logger->log(this->logger, CONTROL | LEVEL1, "Sender thread terminated");
96
97 free(this);
98 }
99
100 /*
101 * Described in header.
102 */
103 sender_t * sender_create()
104 {
105 private_sender_t *this = malloc_thing(private_sender_t);
106
107 this->public.destroy = (void(*)(sender_t*)) destroy;
108
109 this->logger = logger_manager->get_logger(logger_manager, SENDER);
110
111 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))send_packets, this) != 0)
112 {
113 this->logger->log(this->logger, ERROR, "Sender thread could not be created");
114 free(this);
115 charon->kill(charon, "Unable to create sender thread");
116 }
117
118 return &(this->public);
119 }