moved auth_method to policy
[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 packet failed");
80 }
81 current_packet->destroy(current_packet);
82 }
83 }
84
85 /**
86 * Implementation of sender_t.destroy.
87 */
88 static void destroy(private_sender_t *this)
89 {
90 this->logger->log(this->logger, CONTROL | LEVEL1, "Going to terminate sender thread");
91 pthread_cancel(this->assigned_thread);
92
93 pthread_join(this->assigned_thread, NULL);
94 this->logger->log(this->logger, CONTROL | LEVEL1, "Sender thread terminated");
95
96 free(this);
97 }
98
99 /*
100 * Described in header.
101 */
102 sender_t * sender_create()
103 {
104 private_sender_t *this = malloc_thing(private_sender_t);
105
106 this->public.destroy = (void(*)(sender_t*)) destroy;
107
108 this->logger = logger_manager->get_logger(logger_manager, SENDER);
109
110 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))send_packets, this) != 0)
111 {
112 this->logger->log(this->logger, ERROR, "Sender thread could not be created");
113 free(this);
114 charon->kill(charon, "Unable to create sender thread");
115 }
116
117 return &(this->public);
118 }