Version bump to 5.9.0
[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, bool plain)
49 {
50 if (!incoming && plain &&
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 if (enum_from_name(payload_type_short_names, name, &type))
73 {
74 enumerator = list->create_enumerator(list);
75 while (enumerator->enumerate(enumerator, &payload))
76 {
77 if (payload->get_type(payload) == type)
78 {
79 list->remove_at(list, enumerator);
80 message->add_payload(message, payload);
81 }
82 }
83 enumerator->destroy(enumerator);
84 }
85 else
86 {
87 DBG1(DBG_CFG, "unknown payload to sort: '%s', skipped", name);
88 }
89 }
90 order->destroy(order);
91
92 while (list->remove_first(list, (void**)&payload) == SUCCESS)
93 {
94 message->add_payload(message, payload);
95 }
96 list->destroy(list);
97
98 message->disable_sort(message);
99 }
100 return TRUE;
101 }
102
103 METHOD(hook_t, destroy, void,
104 private_unsort_message_t *this)
105 {
106 free(this);
107 }
108
109 /**
110 * Create the IKE_AUTH fill hook
111 */
112 hook_t *unsort_message_hook_create(char *name)
113 {
114 private_unsort_message_t *this;
115
116 INIT(this,
117 .hook = {
118 .listener = {
119 .message = _message,
120 },
121 .destroy = _destroy,
122 },
123 .req = conftest->test->get_bool(conftest->test,
124 "hooks.%s.request", TRUE, name),
125 .id = conftest->test->get_int(conftest->test,
126 "hooks.%s.id", 0, name),
127 .order = conftest->test->get_str(conftest->test,
128 "hooks.%s.order", "", name),
129 );
130
131 return &this->hook;
132 }