Added a hook to print received proposals, including number
[strongswan.git] / src / conftest / hooks / set_critical.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_set_critical_t private_set_critical_t;
21
22 /**
23 * Private data of an set_critical_t object.
24 */
25 struct private_set_critical_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 types, space separated
44 */
45 char *payloads;
46 };
47
48 METHOD(listener_t, message, bool,
49 private_set_critical_t *this, ike_sa_t *ike_sa, message_t *message,
50 bool incoming)
51 {
52 if (!incoming &&
53 message->get_request(message) == this->req &&
54 message->get_message_id(message) == this->id)
55 {
56 enumerator_t *msg, *types;
57 payload_t *payload;
58 payload_type_t type;
59 char *name;
60
61 types = enumerator_create_token(this->payloads, " ", "");
62 while (types->enumerate(types, &name))
63 {
64 type = atoi(name);
65 if (!type)
66 {
67 type = enum_from_name(payload_type_short_names, name);
68 if (type == -1)
69 {
70 DBG1(DBG_CFG, "invalid payload name '%s'", name);
71 break;
72 }
73 }
74 msg = message->create_payload_enumerator(message);
75 while (msg->enumerate(msg, &payload))
76 {
77 if (type == payload->get_type(payload))
78 {
79 encoding_rule_t *rules;
80 size_t count;
81 int i;
82
83 payload->get_encoding_rules(payload, &rules, &count);
84 for (i = 0; i < count; i++)
85 {
86 if (rules[i].type == FLAG)
87 {
88 *(bool*)(((void*)payload) + rules[i].offset) = TRUE;
89 }
90 }
91 }
92 }
93 msg->destroy(msg);
94 }
95 types->destroy(types);
96 }
97 return TRUE;
98 }
99
100 METHOD(hook_t, destroy, void,
101 private_set_critical_t *this)
102 {
103 free(this);
104 }
105
106 /**
107 * Create the IKE_AUTH fill hook
108 */
109 hook_t *set_critical_hook_create(char *name)
110 {
111 private_set_critical_t *this;
112
113 INIT(this,
114 .hook = {
115 .listener = {
116 .message = _message,
117 },
118 .destroy = _destroy,
119 },
120 .req = conftest->test->get_bool(conftest->test,
121 "hooks.%s.request", TRUE, name),
122 .id = conftest->test->get_int(conftest->test,
123 "hooks.%s.id", 0, name),
124 .payloads = conftest->test->get_str(conftest->test,
125 "hooks.%s.payloads", "", name),
126 );
127
128 return &this->hook;
129 }