85410d298c123bbb5b10f71076224e6a1a1edbd0
[strongswan.git] / Source / charon / threads / receiver.c
1 /**
2 * @file receiver.c
3 *
4 * @brief Implementation of receiver_t.
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 <daemon.h>
29 #include <network/socket.h>
30 #include <network/packet.h>
31 #include <queues/job_queue.h>
32 #include <queues/jobs/job.h>
33 #include <queues/jobs/incoming_packet_job.h>
34 #include <utils/allocator.h>
35 #include <utils/logger_manager.h>
36
37 typedef struct private_receiver_t private_receiver_t;
38
39 /**
40 * Private data of a receiver object
41 */
42 struct private_receiver_t {
43 /**
44 * Public part of a receiver object
45 */
46 receiver_t public;
47
48 /**
49 * @brief Thread function started at creation of the receiver object.
50 *
51 * @param this assigned receiver object
52 */
53 void (*receive_packets) (private_receiver_t *this);
54
55 /**
56 * Assigned thread to the receiver_t object
57 */
58 pthread_t assigned_thread;
59 /**
60 * logger for the receiver
61 */
62 logger_t *logger;
63 };
64
65 /**
66 * implements private_receiver_t.receive_packets
67 */
68 static void receive_packets(private_receiver_t * this)
69 {
70 packet_t * current_packet;
71 job_t *current_job;
72
73 /* cancellation disabled by default */
74 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
75
76 this->logger->log(this->logger, CONTROL, "receiver thread running, thread_id %u", (int)pthread_self());
77
78 while (1)
79 {
80 while (charon->socket->receive(charon->socket,&current_packet) == SUCCESS)
81 {
82 this->logger->log(this->logger, CONTROL, "creating job from packet");
83 current_job = (job_t *) incoming_packet_job_create(current_packet);
84
85 charon->job_queue->add(charon->job_queue,current_job);
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 void 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 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
105
106 allocator_free(this);
107 }
108
109 /*
110 * see header
111 */
112 receiver_t * receiver_create()
113 {
114 private_receiver_t *this = allocator_alloc_thing(private_receiver_t);
115
116 this->public.destroy = (void(*)(receiver_t*)) destroy;
117 this->receive_packets = receive_packets;
118
119 this->logger = charon->logger_manager->create_logger(charon->logger_manager, RECEIVER, NULL);
120
121 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->receive_packets, this) != 0)
122 {
123 this->logger->log(this->logger, ERROR, "Receiver thread could not be started");
124 charon->logger_manager->destroy_logger(charon->logger_manager, this->logger);
125 allocator_free(this);
126 return NULL;
127 }
128
129 return &(this->public);
130 }