Version bump to 5.9.0
[strongswan.git] / src / conftest / hooks / add_payload.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 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 "hook.h"
17
18 #include <encoding/payloads/unknown_payload.h>
19
20 typedef struct private_add_payload_t private_add_payload_t;
21
22 /**
23 * Private data of an add_payload_t object.
24 */
25 struct private_add_payload_t {
26
27 /**
28 * Implements the hook_t interface.
29 */
30 hook_t hook;
31
32 /**
33 * Alter requests or responses?
34 */
35 bool req;
36
37 /**
38 * ID of message to alter.
39 */
40 int id;
41
42 /**
43 * Payload type
44 */
45 char *type;
46
47 /**
48 * Payload data
49 */
50 char *data;
51
52 /**
53 * Set critical bit of the payload
54 */
55 bool critical;
56
57 /**
58 * True to replace existing payload of this type
59 */
60 bool replace;
61 };
62
63 METHOD(listener_t, message, bool,
64 private_add_payload_t *this, ike_sa_t *ike_sa, message_t *message,
65 bool incoming, bool plain)
66 {
67 if (!incoming && plain &&
68 message->get_request(message) == this->req &&
69 message->get_message_id(message) == this->id)
70 {
71 unknown_payload_t *unknown;
72 payload_t *payload;
73 enumerator_t *enumerator;
74 chunk_t data = chunk_empty;
75 payload_type_t type;
76
77 type = atoi(this->type);
78 if (!type)
79 {
80 if (!enum_from_name(payload_type_short_names, this->type, &type))
81 {
82 DBG1(DBG_CFG, "unknown payload: '%s', skipped", this->type);
83 return TRUE;
84 }
85 }
86 if (this->replace)
87 {
88 enumerator = message->create_payload_enumerator(message);
89 while (enumerator->enumerate(enumerator, &payload))
90 {
91 if (payload->get_type(payload) == type)
92 {
93 message->remove_payload_at(message, enumerator);
94 payload->destroy(payload);
95 break;
96 }
97 }
98 enumerator->destroy(enumerator);
99 }
100 if (strncaseeq(this->data, "0x", 2))
101 {
102 data = chunk_skip(chunk_create(this->data, strlen(this->data)), 2);
103 data = chunk_from_hex(data, NULL);
104 }
105 else if (strlen(this->data))
106 {
107 data = chunk_clone(chunk_create(this->data, strlen(this->data)));
108 }
109 unknown = unknown_payload_create_data(type, this->critical, data);
110 message->add_payload(message, &unknown->payload_interface);
111 }
112 return TRUE;
113 }
114
115 METHOD(hook_t, destroy, void,
116 private_add_payload_t *this)
117 {
118 free(this);
119 }
120
121 /**
122 * Create the IKE_AUTH fill hook
123 */
124 hook_t *add_payload_hook_create(char *name)
125 {
126 private_add_payload_t *this;
127
128 INIT(this,
129 .hook = {
130 .listener = {
131 .message = _message,
132 },
133 .destroy = _destroy,
134 },
135 .req = conftest->test->get_bool(conftest->test,
136 "hooks.%s.request", TRUE, name),
137 .id = conftest->test->get_int(conftest->test,
138 "hooks.%s.id", 0, name),
139 .type = conftest->test->get_str(conftest->test,
140 "hooks.%s.type", "", name),
141 .data = conftest->test->get_str(conftest->test,
142 "hooks.%s.data", "", name),
143 .critical = conftest->test->get_bool(conftest->test,
144 "hooks.%s.critical", FALSE, name),
145 .replace = conftest->test->get_bool(conftest->test,
146 "hooks.%s.replace", FALSE, name),
147 );
148
149 return &this->hook;
150 }