moved interface enumeration code to socket, where it belongs
[strongswan.git] / src / charon / threads / receiver.c
1 /**
2 * @file receiver.c
3 *
4 * @brief Implementation of receiver_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <stdlib.h>
25 #include <pthread.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/logger_manager.h>
36
37
38 typedef struct private_receiver_t private_receiver_t;
39
40 /**
41 * Private data of a receiver_t object.
42 */
43 struct private_receiver_t {
44 /**
45 * Public part of a receiver_t object.
46 */
47 receiver_t public;
48
49 /**
50 * Assigned thread.
51 */
52 pthread_t assigned_thread;
53
54 /**
55 * A logger for the receiver_t object.
56 */
57 logger_t *logger;
58 };
59
60 /**
61 * Implementation of receiver_t.receive_packets.
62 */
63 static void receive_packets(private_receiver_t * this)
64 {
65 packet_t * current_packet;
66 job_t *current_job;
67
68 /* cancellation disabled by default */
69 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
70
71 this->logger->log(this->logger, CONTROL, "receiver thread running, thread_ID: %06u", (int)pthread_self());
72
73 while (1)
74 {
75 while (charon->socket->receive(charon->socket,&current_packet) == SUCCESS)
76 {
77 this->logger->log(this->logger, CONTROL | LEVEL1, "Creating job from packet");
78 current_job = (job_t *) incoming_packet_job_create(current_packet);
79
80 charon->job_queue->add(charon->job_queue,current_job);
81
82 }
83 /* bad bad, rebuild the socket ? */
84 this->logger->log(this->logger, ERROR, "Receiving from socket failed!");
85 }
86 }
87
88 /**
89 * Implementation of receiver_t.destroy.
90 */
91 static void destroy(private_receiver_t *this)
92 {
93 this->logger->log(this->logger, CONTROL | LEVEL1, "Going to terminate receiver thread");
94 pthread_cancel(this->assigned_thread);
95
96 pthread_join(this->assigned_thread, NULL);
97 this->logger->log(this->logger, CONTROL | LEVEL1, "Receiver thread terminated");
98
99 free(this);
100 }
101
102 /*
103 * Described in header.
104 */
105 receiver_t * receiver_create()
106 {
107 private_receiver_t *this = malloc_thing(private_receiver_t);
108
109 this->public.destroy = (void(*)(receiver_t*)) destroy;
110
111 this->logger = logger_manager->get_logger(logger_manager, RECEIVER);
112
113 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))receive_packets, this) != 0)
114 {
115 this->logger->log(this->logger, ERROR, "Receiver thread could not be started");
116 free(this);
117 charon->kill(charon, "Unable to create receiver thread");
118 }
119
120 return &(this->public);
121 }