75ce071dd0c8f5f0c86df3108c2a8e3b7aa28458
[strongswan.git] / Source / charon / receiver.c
1 /**
2 * @file receiver.c
3 *
4 * @brief Implements the Receiver Thread encapsulated in the receiver_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 "receiver.h"
27
28 #include <socket.h>
29 #include <packet.h>
30 #include <globals.h>
31 #include <jobs/job.h>
32 #include <queues/job_queue.h>
33 #include <utils/allocator.h>
34 #include <utils/logger_manager.h>
35
36 /**
37 * Private data of a receiver object
38 */
39 typedef struct private_receiver_s private_receiver_t;
40
41 struct private_receiver_s {
42 /**
43 * Public part of a receiver object
44 */
45 receiver_t public;
46
47 /**
48 * Assigned thread to the receiver_t object
49 */
50 pthread_t assigned_thread;
51 /**
52 * logger for the receiver
53 */
54 logger_t *logger;
55
56
57 };
58
59 /**
60 * Thread function started at creation of the receiver object
61 *
62 * @param this assigned receiver object
63 * @return SUCCESS if thread_function ended successfully, FAILED otherwise
64 */
65 static void receiver_thread_function(private_receiver_t * this)
66 {
67 packet_t * current_packet;
68 job_t *current_job;
69 /* cancellation disabled by default */
70 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
71 while (1)
72 {
73 while (global_socket->receive(global_socket,&current_packet) == SUCCESS)
74 {
75 this->logger->log(this->logger, CONTROL, "creating job from packet");
76 current_job = (job_t *) incoming_packet_job_create(current_packet);
77 if (current_job == NULL)
78 {
79 this->logger->log(this->logger, ERROR, "job creation failed");
80 }
81
82 if (global_job_queue->add(global_job_queue,current_job) != SUCCESS)
83 {
84 this->logger->log(this->logger, ERROR, "job queueing failed");
85 }
86
87 }
88 /* bad bad, rebuild the socket ? */
89 this->logger->log(this->logger, ERROR, "receiving from socket failed!");
90 }
91 }
92
93 /**
94 * Implementation of receiver_t's destroy function
95 */
96 static status_t destroy(private_receiver_t *this)
97 {
98 this->logger->log(this->logger, CONTROL | MORE, "Going to terminate receiver thread");
99 pthread_cancel(this->assigned_thread);
100
101 pthread_join(this->assigned_thread, NULL);
102 this->logger->log(this->logger, CONTROL | MORE, "Receiver thread terminated");
103
104 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
105
106 allocator_free(this);
107 return SUCCESS;
108 }
109
110
111 receiver_t * receiver_create()
112 {
113 private_receiver_t *this = allocator_alloc_thing(private_receiver_t);
114
115 this->public.destroy = (status_t(*)(receiver_t*)) destroy;
116
117 this->logger = global_logger_manager->create_logger(global_logger_manager, RECEIVER, NULL);
118 if (this->logger == NULL)
119 {
120 allocator_free(this);
121 }
122
123 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))receiver_thread_function, this) != 0)
124 {
125 /* thread could not be created */
126 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
127 allocator_free(this);
128 return NULL;
129 }
130
131 return &(this->public);
132 }