chunk_to_hex() function declaration deleted
[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
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 * Assigned thread.
50 */
51 pthread_t assigned_thread;
52 };
53
54 /**
55 * Implementation of receiver_t.receive_packets.
56 */
57 static void receive_packets(private_receiver_t * this)
58 {
59 packet_t * current_packet;
60 job_t *current_job;
61
62 /* cancellation disabled by default */
63 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
64
65 DBG1(DBG_NET, "receiver thread running, thread_ID: %06u",
66 (int)pthread_self());
67
68 while (TRUE)
69 {
70 while (charon->socket->receive(charon->socket,&current_packet) == SUCCESS)
71 {
72 DBG2(DBG_NET, "creating job from packet");
73 current_job = (job_t *) incoming_packet_job_create(current_packet);
74
75 charon->job_queue->add(charon->job_queue,current_job);
76 }
77 /* bad bad, TODO: rebuild the socket ? */
78 DBG1(DBG_NET, "receiving from socket failed!");
79 }
80 }
81
82 /**
83 * Implementation of receiver_t.destroy.
84 */
85 static void destroy(private_receiver_t *this)
86 {
87 pthread_cancel(this->assigned_thread);
88 pthread_join(this->assigned_thread, NULL);
89 free(this);
90 }
91
92 /*
93 * Described in header.
94 */
95 receiver_t * receiver_create()
96 {
97 private_receiver_t *this = malloc_thing(private_receiver_t);
98
99 this->public.destroy = (void(*)(receiver_t*)) destroy;
100
101 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))receive_packets, this) != 0)
102 {
103 free(this);
104 charon->kill(charon, "unable to create receiver thread");
105 }
106
107 return &(this->public);
108 }