Added a hook to inject custom payloads with critical bit
[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 METHOD(listener_t, message, bool,
59 private_add_payload_t *this, ike_sa_t *ike_sa, message_t *message,
60 bool incoming)
61 {
62 if (!incoming &&
63 message->get_request(message) == this->req &&
64 message->get_message_id(message) == this->id)
65 {
66 unknown_payload_t *payload;
67 chunk_t data = chunk_empty;
68 payload_type_t type;
69
70 type = atoi(this->type);
71 if (!type)
72 {
73 type = enum_from_name(payload_type_short_names, this->type);
74 if (type == -1)
75 {
76 DBG1(DBG_CFG, "unknown payload: '%s', skipped", this->type);
77 return TRUE;
78 }
79 }
80 if (strncasecmp(this->data, "0x", 2) == 0)
81 {
82 data = chunk_skip(chunk_create(this->data, strlen(this->data)), 2);
83 data = chunk_from_hex(data, NULL);
84 }
85 else if (data.len)
86 {
87 data = chunk_clone(chunk_create(this->data, strlen(this->data)));
88 }
89 payload = unknown_payload_create_data(type, this->critical, data);
90 message->add_payload(message, &payload->payload_interface);
91 }
92 return TRUE;
93 }
94
95 METHOD(hook_t, destroy, void,
96 private_add_payload_t *this)
97 {
98 free(this);
99 }
100
101 /**
102 * Create the IKE_AUTH fill hook
103 */
104 hook_t *add_payload_hook_create(char *name)
105 {
106 private_add_payload_t *this;
107
108 INIT(this,
109 .hook = {
110 .listener = {
111 .message = _message,
112 },
113 .destroy = _destroy,
114 },
115 .req = conftest->test->get_bool(conftest->test,
116 "hooks.%s.request", TRUE, name),
117 .id = conftest->test->get_int(conftest->test,
118 "hooks.%s.id", 0, name),
119 .type = conftest->test->get_str(conftest->test,
120 "hooks.%s.type", "", name),
121 .data = conftest->test->get_str(conftest->test,
122 "hooks.%s.data", "", name),
123 .critical = conftest->test->get_bool(conftest->test,
124 "hooks.%s.critical", FALSE, name),
125 );
126
127 return &this->hook;
128 }