Give processor_t more control over the lifecycle of a job
[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 METHOD(job_t, destroy, void,
39 private_process_message_job_t *this)
40 {
41 this->message->destroy(this->message);
42 free(this);
43 }
44
45 METHOD(job_t, execute, job_requeue_t,
46 private_process_message_job_t *this)
47 {
48 ike_sa_t *ike_sa;
49
50 #ifdef ME
51 /* if this is an unencrypted INFORMATIONAL exchange it is likely a
52 * connectivity check. */
53 if (this->message->get_exchange_type(this->message) == INFORMATIONAL &&
54 this->message->get_first_payload_type(this->message) != ENCRYPTED)
55 {
56 /* theoretically this could also be an error message
57 * see RFC 4306, section 1.5. */
58 DBG1(DBG_NET, "received unencrypted informational: from %#H to %#H",
59 this->message->get_source(this->message),
60 this->message->get_destination(this->message));
61 charon->connect_manager->process_check(charon->connect_manager, this->message);
62 return JOB_REQUEUE_NONE;
63 }
64 #endif /* ME */
65
66 ike_sa = charon->ike_sa_manager->checkout_by_message(charon->ike_sa_manager,
67 this->message);
68 if (ike_sa)
69 {
70 DBG1(DBG_NET, "received packet: from %#H to %#H",
71 this->message->get_source(this->message),
72 this->message->get_destination(this->message));
73 if (ike_sa->process_message(ike_sa, this->message) == DESTROY_ME)
74 {
75 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
76 ike_sa);
77 }
78 else
79 {
80 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
81 }
82 }
83 return JOB_REQUEUE_NONE;
84 }
85
86 METHOD(job_t, get_priority, job_priority_t,
87 private_process_message_job_t *this)
88 {
89 switch (this->message->get_exchange_type(this->message))
90 {
91 case IKE_AUTH:
92 /* IKE auth is rather expensive and often blocking, low priority */
93 return JOB_PRIO_LOW;
94 case INFORMATIONAL:
95 /* INFORMATIONALs are inexpensive, for DPD we should have low
96 * reaction times */
97 return JOB_PRIO_HIGH;
98 case IKE_SA_INIT:
99 case CREATE_CHILD_SA:
100 default:
101 /* IKE_SA_INIT is expensive, but we will drop them in the receiver
102 * if we are overloaded */
103 return JOB_PRIO_MEDIUM;
104 }
105 }
106
107 /*
108 * Described in header
109 */
110 process_message_job_t *process_message_job_create(message_t *message)
111 {
112 private_process_message_job_t *this;
113
114 INIT(this,
115 .public = {
116 .job_interface = {
117 .execute = _execute,
118 .get_priority = _get_priority,
119 .destroy = _destroy,
120 },
121 },
122 .message = message,
123 );
124
125 return &(this->public);
126 }