- moved packet and socket in new network-package
[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 <globals.h>
29 #include <network/socket.h>
30 #include <network/packet.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|MORE, "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 this->logger->log(this->logger, CONTROL | MORE, "Going to terminate sender thread");
94 pthread_cancel(this->assigned_thread);
95
96 pthread_join(this->assigned_thread, NULL);
97 this->logger->log(this->logger, CONTROL | MORE, "Sender thread terminated");
98
99 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
100
101 allocator_free(this);
102 return SUCCESS;
103 }
104
105
106 sender_t * sender_create()
107 {
108 private_sender_t *this = allocator_alloc_thing(private_sender_t);
109
110 this->public.destroy = (status_t(*)(sender_t*)) destroy;
111
112 this->logger = global_logger_manager->create_logger(global_logger_manager, SENDER, NULL);
113 if (this->logger == NULL)
114 {
115 allocator_free(this);
116 return NULL;
117 }
118
119 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))sender_thread_function, this) != 0)
120 {
121 /* thread could not be created */
122 allocator_free(this);
123 return NULL;
124 }
125
126 return &(this->public);
127 }