]> git.ipfire.org Git - thirdparty/strongswan.git/blob - programs/charon/charon/threads/receiver.c
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / programs / charon / 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/logger_manager.h>
35
36
37 typedef struct private_receiver_t private_receiver_t;
38
39 /**
40 * Private data of a receiver_t object.
41 */
42 struct private_receiver_t {
43 /**
44 * Public part of a receiver_t object.
45 */
46 receiver_t public;
47
48 /**
49 * @brief Thread function started at creation of the receiver object.
50 *
51 * @param this calling object
52 */
53 void (*receive_packets) (private_receiver_t *this);
54
55 /**
56 * Assigned thread.
57 */
58 pthread_t assigned_thread;
59
60 /**
61 * A logger for the receiver_t object.
62 */
63 logger_t *logger;
64 };
65
66 /**
67 * Implementation of 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, thread_id %u", (int)pthread_self());
78
79 while (1)
80 {
81 while (charon->socket->receive(charon->socket,&current_packet) == SUCCESS)
82 {
83 this->logger->log(this->logger, CONTROL | LEVEL1, "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.destroy.
96 */
97 static void destroy(private_receiver_t *this)
98 {
99 this->logger->log(this->logger, CONTROL | LEVEL1, "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 | LEVEL1, "Receiver thread terminated");
104
105 free(this);
106 }
107
108 /*
109 * Described in header.
110 */
111 receiver_t * receiver_create()
112 {
113 private_receiver_t *this = malloc_thing(private_receiver_t);
114
115 this->public.destroy = (void(*)(receiver_t*)) destroy;
116 this->receive_packets = receive_packets;
117
118 this->logger = logger_manager->get_logger(logger_manager, RECEIVER);
119
120 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->receive_packets, this) != 0)
121 {
122 this->logger->log(this->logger, ERROR, "Receiver thread could not be started");
123 free(this);
124 charon->kill(charon, "Unable to create receiver thread");
125 }
126
127 return &(this->public);
128 }