Moving charon to libcharon.
[strongswan.git] / src / libcharon / processing / jobs / process_message_job.c
1 /*
2 * Copyright (C) 2005-2007 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "process_message_job.h"
18
19 #include <daemon.h>
20
21 typedef struct private_process_message_job_t private_process_message_job_t;
22
23 /**
24 * Private data of an process_message_job_t Object
25 */
26 struct private_process_message_job_t {
27 /**
28 * public process_message_job_t interface
29 */
30 process_message_job_t public;
31
32 /**
33 * Message associated with this job
34 */
35 message_t *message;
36 };
37
38 /**
39 * Implements job_t.destroy.
40 */
41 static void destroy(private_process_message_job_t *this)
42 {
43 this->message->destroy(this->message);
44 free(this);
45 }
46
47 /**
48 * Implementation of job_t.execute.
49 */
50 static void execute(private_process_message_job_t *this)
51 {
52 ike_sa_t *ike_sa;
53
54 #ifdef ME
55 /* if this is an unencrypted INFORMATIONAL exchange it is likely a
56 * connectivity check. */
57 if (this->message->get_exchange_type(this->message) == INFORMATIONAL &&
58 this->message->get_first_payload_type(this->message) != ENCRYPTED)
59 {
60 /* theoretically this could also be an error message
61 * see RFC 4306, section 1.5. */
62 DBG1(DBG_NET, "received unencrypted informational: from %#H to %#H",
63 this->message->get_source(this->message),
64 this->message->get_destination(this->message));
65 charon->connect_manager->process_check(charon->connect_manager, this->message);
66 destroy(this);
67 return;
68 }
69 #endif /* ME */
70
71 ike_sa = charon->ike_sa_manager->checkout_by_message(charon->ike_sa_manager,
72 this->message);
73 if (ike_sa)
74 {
75 DBG1(DBG_NET, "received packet: from %#H to %#H",
76 this->message->get_source(this->message),
77 this->message->get_destination(this->message));
78 if (ike_sa->process_message(ike_sa, this->message) == DESTROY_ME)
79 {
80 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
81 ike_sa);
82 }
83 else
84 {
85 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
86 }
87 }
88 destroy(this);
89 }
90
91 /*
92 * Described in header
93 */
94 process_message_job_t *process_message_job_create(message_t *message)
95 {
96 private_process_message_job_t *this = malloc_thing(private_process_message_job_t);
97
98 /* interface functions */
99 this->public.job_interface.execute = (void (*) (job_t *)) execute;
100 this->public.job_interface.destroy = (void(*)(job_t*))destroy;
101
102 /* private variables */
103 this->message = message;
104
105 return &(this->public);
106 }