Use payload_get_field() to look up payload fields
[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 bool *critical;
60 char *name;
61
62 types = enumerator_create_token(this->payloads, " ", "");
63 while (types->enumerate(types, &name))
64 {
65 type = atoi(name);
66 if (!type)
67 {
68 type = enum_from_name(payload_type_short_names, name);
69 if (type == -1)
70 {
71 DBG1(DBG_CFG, "invalid payload name '%s'", name);
72 break;
73 }
74 }
75 msg = message->create_payload_enumerator(message);
76 while (msg->enumerate(msg, &payload))
77 {
78 if (type == payload->get_type(payload))
79 {
80 critical = payload_get_field(payload, FLAG, 0);
81 if (critical)
82 {
83 *critical = TRUE;
84 }
85 }
86 }
87 msg->destroy(msg);
88 }
89 types->destroy(types);
90 }
91 return TRUE;
92 }
93
94 METHOD(hook_t, destroy, void,
95 private_set_critical_t *this)
96 {
97 free(this);
98 }
99
100 /**
101 * Create the IKE_AUTH fill hook
102 */
103 hook_t *set_critical_hook_create(char *name)
104 {
105 private_set_critical_t *this;
106
107 INIT(this,
108 .hook = {
109 .listener = {
110 .message = _message,
111 },
112 .destroy = _destroy,
113 },
114 .req = conftest->test->get_bool(conftest->test,
115 "hooks.%s.request", TRUE, name),
116 .id = conftest->test->get_int(conftest->test,
117 "hooks.%s.id", 0, name),
118 .payloads = conftest->test->get_str(conftest->test,
119 "hooks.%s.payloads", "", name),
120 );
121
122 return &this->hook;
123 }