payload: Use common prefixes for all payload type identifiers
[strongswan.git] / src / conftest / hooks / log_proposals.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/sa_payload.h>
19
20 typedef struct private_log_proposals_t private_log_proposals_t;
21
22 /**
23 * Private data of an log_proposals_t object.
24 */
25 struct private_log_proposals_t {
26
27 /**
28 * Implements the hook_t interface.
29 */
30 hook_t hook;
31 };
32
33 METHOD(listener_t, message, bool,
34 private_log_proposals_t *this, ike_sa_t *ike_sa, message_t *message,
35 bool incoming, bool plain)
36 {
37 if (incoming && plain)
38 {
39 enumerator_t *enumerator, *proposals;
40 payload_t *payload;
41 linked_list_t *list;
42 sa_payload_t *sa;
43 proposal_t *proposal;
44
45 enumerator = message->create_payload_enumerator(message);
46 while (enumerator->enumerate(enumerator, &payload))
47 {
48 if (payload->get_type(payload) == PLV2_SECURITY_ASSOCIATION)
49 {
50 sa = (sa_payload_t*)payload;
51 list = sa->get_proposals(sa);
52 DBG1(DBG_CFG, "received %d proposal%s:", list->get_count(list),
53 list->get_count(list) == 1 ? "" : "s");
54 proposals = list->create_enumerator(list);
55 while (proposals->enumerate(proposals, &proposal))
56 {
57 u_int64_t spi = proposal->get_spi(proposal);
58
59 if (proposal->get_protocol(proposal) != PROTO_IKE)
60 {
61 spi = htonl(spi);
62 }
63 DBG1(DBG_CFG, " %d (SPI 0x%llx): %P",
64 proposal->get_number(proposal), spi, proposal);
65 }
66 proposals->destroy(proposals);
67 list->destroy_offset(list, offsetof(proposal_t, destroy));
68 }
69 }
70 enumerator->destroy(enumerator);
71 }
72 return TRUE;
73 }
74
75 METHOD(hook_t, destroy, void,
76 private_log_proposals_t *this)
77 {
78 free(this);
79 }
80
81 /**
82 * Create the IKE_AUTH fill hook
83 */
84 hook_t *log_proposals_hook_create(char *name)
85 {
86 private_log_proposals_t *this;
87
88 INIT(this,
89 .hook = {
90 .listener = {
91 .message = _message,
92 },
93 .destroy = _destroy,
94 },
95 );
96
97 return &this->hook;
98 }