]> git.ipfire.org Git - thirdparty/strongswan.git/blame - programs/charon/charon/threads/receiver.c
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / programs / charon / charon / threads / receiver.c
CommitLineData
95f8c9aa
JH
1/**
2 * @file receiver.c
79538669 3 *
ca76df97 4 * @brief Implementation of receiver_t.
79538669 5 *
95f8c9aa
JH
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 */
79538669 22
95f8c9aa
JH
23#include <stdlib.h>
24#include <pthread.h>
95f8c9aa
JH
25
26#include "receiver.h"
b85d20d1 27
0e96f7d8 28#include <daemon.h>
716abc9f
MW
29#include <network/socket.h>
30#include <network/packet.h>
021c2322 31#include <queues/job_queue.h>
7cf14676 32#include <queues/jobs/job.h>
8277be60 33#include <queues/jobs/incoming_packet_job.h>
021c2322 34#include <utils/logger_manager.h>
95f8c9aa 35
3febcf15 36
5796aa16
MW
37typedef struct private_receiver_t private_receiver_t;
38
95f8c9aa 39/**
3febcf15 40 * Private data of a receiver_t object.
95f8c9aa 41 */
5796aa16 42struct private_receiver_t {
95f8c9aa 43 /**
3febcf15 44 * Public part of a receiver_t object.
95f8c9aa
JH
45 */
46 receiver_t public;
ca76df97
MW
47
48 /**
49 * @brief Thread function started at creation of the receiver object.
50 *
3febcf15 51 * @param this calling object
ca76df97
MW
52 */
53 void (*receive_packets) (private_receiver_t *this);
79538669 54
95f8c9aa 55 /**
3febcf15 56 * Assigned thread.
95f8c9aa
JH
57 */
58 pthread_t assigned_thread;
3febcf15 59
d17b6ea6 60 /**
3febcf15 61 * A logger for the receiver_t object.
d17b6ea6
MW
62 */
63 logger_t *logger;
95f8c9aa
JH
64};
65
66/**
3febcf15 67 * Implementation of receiver_t.receive_packets.
95f8c9aa 68 */
ca76df97 69static void receive_packets(private_receiver_t * this)
95f8c9aa 70{
95f8c9aa 71 packet_t * current_packet;
d5fc0f73 72 job_t *current_job;
c1eb1537 73
d17b6ea6
MW
74 /* cancellation disabled by default */
75 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
c1eb1537 76
3febcf15 77 this->logger->log(this->logger, CONTROL, "Receiver thread running, thread_id %u", (int)pthread_self());
c1eb1537 78
95f8c9aa
JH
79 while (1)
80 {
0e96f7d8 81 while (charon->socket->receive(charon->socket,&current_packet) == SUCCESS)
95f8c9aa 82 {
aee3eb52 83 this->logger->log(this->logger, CONTROL | LEVEL1, "Creating job from packet");
d5fc0f73 84 current_job = (job_t *) incoming_packet_job_create(current_packet);
95f8c9aa 85
0e96f7d8 86 charon->job_queue->add(charon->job_queue,current_job);
95f8c9aa
JH
87
88 }
d17b6ea6 89 /* bad bad, rebuild the socket ? */
3febcf15 90 this->logger->log(this->logger, ERROR, "Receiving from socket failed!");
95f8c9aa 91 }
95f8c9aa
JH
92}
93
94/**
3febcf15 95 * Implementation of receiver_t.destroy.
95f8c9aa 96 */
d048df5c 97static void destroy(private_receiver_t *this)
95f8c9aa 98{
aee3eb52 99 this->logger->log(this->logger, CONTROL | LEVEL1, "Going to terminate receiver thread");
95f8c9aa 100 pthread_cancel(this->assigned_thread);
79538669 101
95f8c9aa 102 pthread_join(this->assigned_thread, NULL);
aee3eb52 103 this->logger->log(this->logger, CONTROL | LEVEL1, "Receiver thread terminated");
95f8c9aa 104
5113680f 105 free(this);
95f8c9aa
JH
106}
107
ca76df97 108/*
3febcf15 109 * Described in header.
ca76df97 110 */
95f8c9aa
JH
111receiver_t * receiver_create()
112{
5113680f 113 private_receiver_t *this = malloc_thing(private_receiver_t);
79538669 114
d048df5c 115 this->public.destroy = (void(*)(receiver_t*)) destroy;
ca76df97 116 this->receive_packets = receive_packets;
d17b6ea6 117
5113680f 118 this->logger = logger_manager->get_logger(logger_manager, RECEIVER);
d17b6ea6 119
ca76df97 120 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->receive_packets, this) != 0)
95f8c9aa 121 {
ca76df97 122 this->logger->log(this->logger, ERROR, "Receiver thread could not be started");
5113680f 123 free(this);
ba425b87 124 charon->kill(charon, "Unable to create receiver thread");
95f8c9aa 125 }
79538669 126
95f8c9aa
JH
127 return &(this->public);
128}