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