Merge branch 'chunk-mmap'
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / ike_vendor.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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 "ike_vendor.h"
17
18 #include <daemon.h>
19 #include <encoding/payloads/vendor_id_payload.h>
20
21 typedef struct private_ike_vendor_t private_ike_vendor_t;
22
23 /**
24 * Private data of an ike_vendor_t object.
25 */
26 struct private_ike_vendor_t {
27
28 /**
29 * Public ike_vendor_t interface.
30 */
31 ike_vendor_t public;
32
33 /**
34 * Associated IKE_SA
35 */
36 ike_sa_t *ike_sa;
37
38 /**
39 * Are we the inititator of this task
40 */
41 bool initiator;
42 };
43
44 /**
45 * strongSwan specific vendor ID without version, MD5("strongSwan")
46 */
47 static chunk_t strongswan_vid = chunk_from_chars(
48 0x88,0x2f,0xe5,0x6d,0x6f,0xd2,0x0d,0xbc,
49 0x22,0x51,0x61,0x3b,0x2e,0xbe,0x5b,0xeb
50 );
51
52 METHOD(task_t, build, status_t,
53 private_ike_vendor_t *this, message_t *message)
54 {
55 if (lib->settings->get_bool(lib->settings,
56 "%s.send_vendor_id", FALSE, charon->name))
57 {
58 vendor_id_payload_t *vid;
59
60 vid = vendor_id_payload_create_data(VENDOR_ID,
61 chunk_clone(strongswan_vid));
62 message->add_payload(message, &vid->payload_interface);
63 }
64
65 return this->initiator ? NEED_MORE : SUCCESS;
66 }
67
68 METHOD(task_t, process, status_t,
69 private_ike_vendor_t *this, message_t *message)
70 {
71 enumerator_t *enumerator;
72 payload_t *payload;
73
74 enumerator = message->create_payload_enumerator(message);
75 while (enumerator->enumerate(enumerator, &payload))
76 {
77 if (payload->get_type(payload) == VENDOR_ID)
78 {
79 vendor_id_payload_t *vid;
80 chunk_t data;
81
82 vid = (vendor_id_payload_t*)payload;
83 data = vid->get_data(vid);
84
85 if (chunk_equals(data, strongswan_vid))
86 {
87 DBG1(DBG_IKE, "received strongSwan vendor ID");
88 this->ike_sa->enable_extension(this->ike_sa, EXT_STRONGSWAN);
89 }
90 else
91 {
92 DBG1(DBG_ENC, "received unknown vendor ID: %#B", &data);
93 }
94 }
95 }
96 enumerator->destroy(enumerator);
97
98 return this->initiator ? SUCCESS : NEED_MORE;
99 }
100
101 METHOD(task_t, migrate, void,
102 private_ike_vendor_t *this, ike_sa_t *ike_sa)
103 {
104 this->ike_sa = ike_sa;
105 }
106
107 METHOD(task_t, get_type, task_type_t,
108 private_ike_vendor_t *this)
109 {
110 return TASK_IKE_VENDOR;
111 }
112
113 METHOD(task_t, destroy, void,
114 private_ike_vendor_t *this)
115 {
116 free(this);
117 }
118
119 /**
120 * See header
121 */
122 ike_vendor_t *ike_vendor_create(ike_sa_t *ike_sa, bool initiator)
123 {
124 private_ike_vendor_t *this;
125
126 INIT(this,
127 .public = {
128 .task = {
129 .build = _build,
130 .process = _process,
131 .migrate = _migrate,
132 .get_type = _get_type,
133 .destroy = _destroy,
134 },
135 },
136 .initiator = initiator,
137 .ike_sa = ike_sa,
138 );
139
140 return &this->public;
141 }