- returned wrong payload type
[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
34 /**
35 * Private data of a sender object
36 */
37 typedef struct private_sender_s private_sender_t;
38
39 struct private_sender_s {
40 /**
41 * Public part of a sender object
42 */
43 sender_t public;
44
45 /**
46 * Assigned thread to the sender_t object
47 */
48 pthread_t assigned_thread;
49
50 };
51
52 /**
53 * Thread function started at creation of the sender object
54 *
55 * @param this assigned sender object
56 * @return SUCCESS if thread_function ended successfully, FAILED otherwise
57 */
58 static void sender_thread_function(private_sender_t * this)
59 {
60 /* cancellation disabled by default */
61 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
62 packet_t * current_packet;
63
64 while (1)
65 {
66 while (global_send_queue->get(global_send_queue,&current_packet) == SUCCESS)
67 {
68 if ( global_socket->send(global_socket,current_packet) == SUCCESS)
69 {
70 current_packet->destroy(current_packet);
71 }
72 else
73 {
74 /* Packet could not be sent */
75 /* TODO LOG it */
76 }
77
78 }
79
80 /* NOT GOOD !!!!!! */
81 /* TODO LOG it */
82 }
83
84
85 }
86
87 /**
88 * Implementation of sender_t's destroy function
89 */
90 static status_t destroy(private_sender_t *this)
91 {
92 pthread_cancel(this->assigned_thread);
93
94 pthread_join(this->assigned_thread, NULL);
95
96 allocator_free(this);
97 return SUCCESS;
98 }
99
100
101 sender_t * sender_create()
102 {
103 private_sender_t *this = allocator_alloc_thing(private_sender_t);
104
105 this->public.destroy = (status_t(*)(sender_t*)) destroy;
106 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))sender_thread_function, this) != 0)
107 {
108 /* thread could not be created */
109 allocator_free(this);
110 return NULL;
111 }
112
113 return &(this->public);
114 }