9af13e8e076c247dc953e01c6b8223af517ee4d1
[strongswan.git] / src / conftest / hooks / unsort_message.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 typedef struct private_unsort_message_t private_unsort_message_t;
19
20 /**
21 * Private data of an unsort_message_t object.
22 */
23 struct private_unsort_message_t {
24
25 /**
26 * Implements the hook_t interface.
27 */
28 hook_t hook;
29
30 /**
31 * Alter requests or responses?
32 */
33 bool req;
34
35 /**
36 * ID of message to alter.
37 */
38 int id;
39
40 /**
41 * Order of payloads we want
42 */
43 char *order;
44 };
45
46 METHOD(listener_t, message, bool,
47 private_unsort_message_t *this, ike_sa_t *ike_sa, message_t *message,
48 bool incoming)
49 {
50 if (!incoming &&
51 message->get_request(message) == this->req &&
52 message->get_message_id(message) == this->id)
53 {
54 enumerator_t *enumerator, *order;
55 linked_list_t *list;
56 payload_type_t type;
57 payload_t *payload;
58 char *name;
59
60 list = linked_list_create();
61 enumerator = message->create_payload_enumerator(message);
62 while (enumerator->enumerate(enumerator, &payload))
63 {
64 message->remove_payload_at(message, enumerator);
65 list->insert_last(list, payload);
66 }
67 enumerator->destroy(enumerator);
68
69 order = enumerator_create_token(this->order, ", ", " ");
70 while (order->enumerate(order, &name))
71 {
72 type = enum_from_name(payload_type_short_names, name);
73 if (type != -1)
74 {
75 enumerator = list->create_enumerator(list);
76 while (enumerator->enumerate(enumerator, &payload))
77 {
78 if (payload->get_type(payload) == type)
79 {
80 list->remove_at(list, enumerator);
81 message->add_payload(message, payload);
82 }
83 }
84 enumerator->destroy(enumerator);
85 }
86 else
87 {
88 DBG1(DBG_CFG, "unkown payload to sort: '%s', skipped", name);
89 }
90 }
91 order->destroy(order);
92
93 while (list->remove_first(list, (void**)&payload) == SUCCESS)
94 {
95 message->add_payload(message, payload);
96 }
97 list->destroy(list);
98
99 message->disable_sort(message);
100 }
101 return TRUE;
102 }
103
104 METHOD(hook_t, destroy, void,
105 private_unsort_message_t *this)
106 {
107 free(this);
108 }
109
110 /**
111 * Create the IKE_AUTH fill hook
112 */
113 hook_t *unsort_message_hook_create(char *name)
114 {
115 private_unsort_message_t *this;
116
117 INIT(this,
118 .hook = {
119 .listener = {
120 .message = _message,
121 },
122 .destroy = _destroy,
123 },
124 .req = conftest->test->get_bool(conftest->test,
125 "hooks.%s.request", TRUE, name),
126 .id = conftest->test->get_int(conftest->test,
127 "hooks.%s.id", 0, name),
128 .order = conftest->test->get_str(conftest->test,
129 "hooks.%s.order", "", name),
130 );
131
132 return &this->hook;
133 }