Added a force_hookie hook that requests a COOKIE independent of our COOKIE mechanism
[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)
66 {
67 if (!incoming &&
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 type = enum_from_name(payload_type_short_names, this->type);
81 if (type == -1)
82 {
83 DBG1(DBG_CFG, "unknown payload: '%s', skipped", this->type);
84 return TRUE;
85 }
86 }
87 if (this->replace)
88 {
89 enumerator = message->create_payload_enumerator(message);
90 while (enumerator->enumerate(enumerator, &payload))
91 {
92 if (payload->get_type(payload) == type)
93 {
94 message->remove_payload_at(message, enumerator);
95 payload->destroy(payload);
96 break;
97 }
98 }
99 enumerator->destroy(enumerator);
100 }
101 if (strncasecmp(this->data, "0x", 2) == 0)
102 {
103 data = chunk_skip(chunk_create(this->data, strlen(this->data)), 2);
104 data = chunk_from_hex(data, NULL);
105 }
106 else if (this->data && strlen(this->data))
107 {
108 data = chunk_clone(chunk_create(this->data, strlen(this->data)));
109 }
110 unknown = unknown_payload_create_data(type, this->critical, data);
111 message->add_payload(message, &unknown->payload_interface);
112 }
113 return TRUE;
114 }
115
116 METHOD(hook_t, destroy, void,
117 private_add_payload_t *this)
118 {
119 free(this);
120 }
121
122 /**
123 * Create the IKE_AUTH fill hook
124 */
125 hook_t *add_payload_hook_create(char *name)
126 {
127 private_add_payload_t *this;
128
129 INIT(this,
130 .hook = {
131 .listener = {
132 .message = _message,
133 },
134 .destroy = _destroy,
135 },
136 .req = conftest->test->get_bool(conftest->test,
137 "hooks.%s.request", TRUE, name),
138 .id = conftest->test->get_int(conftest->test,
139 "hooks.%s.id", 0, name),
140 .type = conftest->test->get_str(conftest->test,
141 "hooks.%s.type", "", name),
142 .data = conftest->test->get_str(conftest->test,
143 "hooks.%s.data", "", name),
144 .critical = conftest->test->get_bool(conftest->test,
145 "hooks.%s.critical", FALSE, name),
146 .replace = conftest->test->get_bool(conftest->test,
147 "hooks.%s.replace", FALSE, name),
148 );
149
150 return &this->hook;
151 }