08e5a7eb2cada88f1e2521430c848874c441eed3
[strongswan.git] / src / libcharon / sa / tasks / informational.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "informational.h"
17
18 #include <daemon.h>
19
20 typedef struct private_informational_t private_informational_t;
21
22 /**
23 * Private members of a informational_t task.
24 */
25 struct private_informational_t {
26
27 /**
28 * Public methods and task_t interface.
29 */
30 informational_t public;
31
32 /**
33 * Assigned IKE_SA.
34 */
35 ike_sa_t *ike_sa;
36
37 /**
38 * Notify payload to send
39 */
40 notify_payload_t *notify;
41 };
42
43 METHOD(task_t, build_i, status_t,
44 private_informational_t *this, message_t *message)
45 {
46 message->add_payload(message, &this->notify->payload_interface);
47 this->notify = NULL;
48 return SUCCESS;
49 }
50
51 METHOD(task_t, process_r, status_t,
52 private_informational_t *this, message_t *message)
53 {
54 enumerator_t *enumerator;
55 notify_payload_t *notify;
56 notify_type_t type;
57 payload_t *payload;
58 status_t status = SUCCESS;
59
60 enumerator = message->create_payload_enumerator(message);
61 while (enumerator->enumerate(enumerator, &payload))
62 {
63 switch (payload->get_type(payload))
64 {
65 case NOTIFY_V1:
66 notify = (notify_payload_t*)payload;
67 type = notify->get_notify_type(notify);
68
69 if (type == INITIAL_CONTACT_IKEV1)
70 {
71 this->ike_sa->set_condition(this->ike_sa,
72 COND_INIT_CONTACT_SEEN, TRUE);
73 }
74 else if (type < 16384)
75 {
76 DBG1(DBG_IKE, "received %N error notify",
77 notify_type_names, notify->get_notify_type(notify));
78 status = FAILED;
79 break;
80 }
81 else
82 {
83 DBG1(DBG_IKE, "received %N notify",
84 notify_type_names, notify->get_notify_type(notify));
85 }
86 continue;
87 case DELETE_V1:
88 /* TODO-IKEv1: handle delete */
89 default:
90 continue;
91 }
92 break;
93 }
94 enumerator->destroy(enumerator);
95
96 return status;
97 }
98
99 METHOD(task_t, build_r, status_t,
100 private_informational_t *this, message_t *message)
101 {
102 return FAILED;
103 }
104
105 METHOD(task_t, process_i, status_t,
106 private_informational_t *this, message_t *message)
107 {
108 return FAILED;
109 }
110
111 METHOD(task_t, get_type, task_type_t,
112 private_informational_t *this)
113 {
114 return TASK_INFORMATIONAL;
115 }
116
117 METHOD(task_t, migrate, void,
118 private_informational_t *this, ike_sa_t *ike_sa)
119 {
120 this->ike_sa = ike_sa;
121 }
122
123 METHOD(task_t, destroy, void,
124 private_informational_t *this)
125 {
126 DESTROY_IF(this->notify);
127 free(this);
128 }
129
130 /*
131 * Described in header.
132 */
133 informational_t *informational_create(ike_sa_t *ike_sa, notify_payload_t *notify)
134 {
135 private_informational_t *this;
136
137 INIT(this,
138 .public = {
139 .task = {
140 .get_type = _get_type,
141 .migrate = _migrate,
142 .destroy = _destroy,
143 },
144 },
145 .ike_sa = ike_sa,
146 .notify = notify,
147 );
148
149 if (notify)
150 {
151 this->public.task.build = _build_i;
152 this->public.task.process = _process_i;
153 }
154 else
155 {
156 this->public.task.build = _build_r;
157 this->public.task.process = _process_r;
158 }
159
160 return &this->public;
161 }