- added logger
[strongswan.git] / Source / charon / sender.c
1 /**
2 * @file sender.c
3 *
4 * @brief Implements the Sender Thread encapsulated in the sender_t object
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 "socket.h"
29 #include "packet.h"
30 #include "globals.h"
31 #include "queues/send_queue.h"
32 #include "utils/allocator.h"
33 #include "utils/logger_manager.h"
34
35 /**
36 * Private data of a sender object
37 */
38 typedef struct private_sender_s private_sender_t;
39
40 struct private_sender_s {
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 * logger for this sender
53 */
54 logger_t *logger;
55
56 };
57
58 /**
59 * Thread function started at creation of the sender object
60 *
61 * @param this assigned sender object
62 * @return SUCCESS if thread_function ended successfully, FAILED otherwise
63 */
64 static void sender_thread_function(private_sender_t * this)
65 {
66 packet_t * current_packet;
67 status_t status;
68
69 /* cancellation disabled by default */
70 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
71
72 while (1)
73 {
74 while (global_send_queue->get(global_send_queue,&current_packet) == SUCCESS)
75 {
76 this->logger->log(this->logger, CONTROL, "got a packet, sending it");
77 status = global_socket->send(global_socket,current_packet);
78 if (status != SUCCESS)
79 {
80 this->logger->log(this->logger, ERROR, "sending failed, socket returned %s",
81 mapping_find(status_m, status));
82 }
83 current_packet->destroy(current_packet);
84 }
85 }
86 }
87
88 /**
89 * Implementation of sender_t's destroy function
90 */
91 static status_t destroy(private_sender_t *this)
92 {
93 pthread_cancel(this->assigned_thread);
94
95 pthread_join(this->assigned_thread, NULL);
96
97 allocator_free(this);
98 return SUCCESS;
99 }
100
101
102 sender_t * sender_create()
103 {
104 private_sender_t *this = allocator_alloc_thing(private_sender_t);
105
106 this->public.destroy = (status_t(*)(sender_t*)) destroy;
107
108 this->logger = global_logger_manager->create_logger(global_logger_manager, SENDER_THREAD, NULL);
109 if (this->logger == NULL)
110 {
111 allocator_free(this);
112 return NULL;
113 }
114
115 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))sender_thread_function, this) != 0)
116 {
117 /* thread could not be created */
118 allocator_free(this);
119 return NULL;
120 }
121
122 return &(this->public);
123 }