blob: a26b14c290f3ac908b4ea0bba92b84d517aa5001 [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2015, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* This extension uses the hooks mechanism to display the full content of received and sent messages, for
37 learning & debugging purpose.
38 Do NOT use this extension in production environment because it will slow down all operation. */
39
40/* You can add a configuration parameter on the LoadExtension line, e.g.
41LoadExtension="dbg_msg_dump.fdx":"0x149";
42The value is an hexadecimal value with the following bits meaning: */
43#define HK_ERRORS_QUIET 0x0001 /* errors are not dumped -- removes the default handling as well */
44#define HK_ERRORS_COMPACT 0x0002 /* errors in compact mode */
45#define HK_ERRORS_FULL 0x0004 /* errors in full mode (1 line with all the data) */
46#define HK_ERRORS_TREE 0x0008 /* errors in treeview mode (message split over multiple lines) */
47
48#define HK_SNDRCV_QUIET 0x0010 /* send+rcv are not dumped -- removes the default handling as well */
49#define HK_SNDRCV_COMPACT 0x0020 /* send+rcv in compact mode */
50#define HK_SNDRCV_FULL 0x0040 /* send+rcv in full mode */
51#define HK_SNDRCV_TREE 0x0080 /* send+rcv in tree mode */
52
53#define HK_ROUTING_QUIET 0x0100 /* routing decisions are not dumped -- removes the default handling as well */
54#define HK_ROUTING_COMPACT 0x0200 /* routing decisions in compact mode */
55#define HK_ROUTING_FULL 0x0400 /* routing decisions in full mode */
56#define HK_ROUTING_TREE 0x0800 /* routing decisions in tree mode */
57
58#define HK_PEERS_QUIET 0x1000 /* peers connections events are not dumped -- removes the default handling as well */
59#define HK_PEERS_COMPACT 0x2000 /* peers connections events in compact mode */
60#define HK_PEERS_FULL 0x4000 /* peers connections events in full mode */
61#define HK_PEERS_TREE 0x8000 /* peers connections events in tree mode */
62/*
63Default value is HK_ERRORS_TREE + HK_SNDRCV_TREE + HK_PEERS_TREE
64*/
65
66#include <freeDiameter/extension.h>
67
68static struct fd_hook_hdl *md_hdl[4] = {NULL,NULL,NULL,NULL};
69static uint32_t dump_level = HK_ERRORS_TREE | HK_SNDRCV_TREE | HK_PEERS_TREE; /* default */
70static char * buf = NULL;
71static size_t len;
72static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
73
74/* The callback called when messages are received and sent */
75static void md_hook_cb_tree(enum fd_hook_type type, struct msg * msg, struct peer_hdr * peer, void * other, struct fd_hook_permsgdata *pmd, void * regdata)
76{
77 char * peer_name = peer ? peer->info.pi_diamid : "<unknown peer>";
78
79 CHECK_POSIX_DO( pthread_mutex_lock(&mtx), );
80
81 if (msg) {
82 CHECK_MALLOC_DO( fd_msg_dump_treeview(&buf, &len, NULL, msg, fd_g_config->cnf_dict, (type == HOOK_MESSAGE_PARSING_ERROR) ? 0 : 1, 1),
83 { LOG_E("Error while dumping a message"); pthread_mutex_unlock(&mtx); return; } );
84 }
85
86 switch (type) {
87/* errors */
88 case HOOK_MESSAGE_FAILOVER:
89 LOG_E("FAILOVER from '%s':", peer_name);
90 LOG_SPLIT(FD_LOG_ERROR, " ", buf, NULL);
91 break;
92 case HOOK_MESSAGE_PARSING_ERROR:
93 if (msg) {
94 DiamId_t id = NULL;
95 if (fd_msg_source_get( msg, &id, NULL ))
96 id = (DiamId_t)"<error getting source>";
97 if (!id)
98 id = (DiamId_t)"<local>";
99 LOG_E("PARSING ERROR: '%s' from '%s': ", (char *)other, (char *)id);
100 LOG_SPLIT(FD_LOG_ERROR, " ", buf, NULL);
101 } else {
102 struct fd_cnx_rcvdata *rcv_data = other;
103 CHECK_MALLOC_DO(fd_dump_extend_hexdump(&buf, &len, NULL, rcv_data->buffer, rcv_data->length, 0, 0), break);
104 LOG_E("PARSING ERROR: %zdB msg from '%s': %s", rcv_data->length, peer_name, buf);
105 }
106 break;
107 case HOOK_MESSAGE_PARSING_ERROR2:
108 LOG_E("PARSING ERROR, returning:");
109 LOG_SPLIT(FD_LOG_ERROR, " ", buf, NULL);
110 break;
111 case HOOK_MESSAGE_ROUTING_ERROR:
112 LOG_E("ROUTING ERROR '%s' for: ", (char *)other);
113 LOG_SPLIT(FD_LOG_ERROR, " ", buf, NULL);
114 break;
115 case HOOK_MESSAGE_DROPPED:
116 LOG_E("DROPPED '%s'", (char *)other);
117 LOG_SPLIT(FD_LOG_ERROR, " ", buf, NULL);
118 break;
119
120/* send receive */
121 case HOOK_MESSAGE_RECEIVED:
122 LOG_N("RCV from '%s':", peer_name);
123 LOG_SPLIT(FD_LOG_NOTICE, " ", buf, NULL);
124 break;
125 case HOOK_MESSAGE_SENDING:
126 LOG_N("SNDING to '%s':", peer_name);
127 LOG_SPLIT(FD_LOG_NOTICE, " ", buf, NULL);
128 break;
129 case HOOK_MESSAGE_SENT:
130 LOG_N("SND to '%s':", peer_name);
131 LOG_SPLIT(FD_LOG_NOTICE, " ", buf, NULL);
132 break;
133
134/* routing */
135 case HOOK_MESSAGE_LOCAL:
136 LOG_N("ISSUED:");
137 LOG_SPLIT(FD_LOG_NOTICE, " ", buf, NULL);
138 break;
139 case HOOK_MESSAGE_ROUTING_FORWARD:
140 LOG_N("FORWARDING: %s", buf);
141 LOG_SPLIT(FD_LOG_NOTICE, " ", buf, NULL);
142 break;
143 case HOOK_MESSAGE_ROUTING_LOCAL:
144 LOG_N("DISPATCHING: %s", buf);
145 LOG_SPLIT(FD_LOG_NOTICE, " ", buf, NULL);
146 break;
147
148/* peers */
149 case HOOK_PEER_CONNECT_FAILED:
150 LOG_N("CONNECT FAILED to %s: %s", peer_name, (char *)other);
151 break;
152 case HOOK_PEER_CONNECT_SUCCESS:
153 {
154 char protobuf[40];
155 if (peer) {
156 CHECK_FCT_DO(fd_peer_cnx_proto_info(peer, protobuf, sizeof(protobuf)), break );
157 } else {
158 protobuf[0] = '-';
159 protobuf[1] = '\0';
160 }
161 LOG_N("CONNECTED TO '%s' (%s):", peer_name, protobuf);
162 LOG_SPLIT(FD_LOG_NOTICE, " ", buf, NULL);
163 }
164 break;
165
166/* Not handled */
167 case HOOK_DATA_RECEIVED:
168 break;
169 }
170
171 CHECK_POSIX_DO( pthread_mutex_unlock(&mtx), );
172}
173
174static void md_hook_cb_full(enum fd_hook_type type, struct msg * msg, struct peer_hdr * peer, void * other, struct fd_hook_permsgdata *pmd, void * regdata)
175{
176 char * peer_name = peer ? peer->info.pi_diamid : "<unknown peer>";
177
178 CHECK_POSIX_DO( pthread_mutex_lock(&mtx), );
179
180 if (msg) {
181 CHECK_MALLOC_DO( fd_msg_dump_full(&buf, &len, NULL, msg, fd_g_config->cnf_dict, (type == HOOK_MESSAGE_PARSING_ERROR) ? 0 : 1, 1),
182 { LOG_E("Error while dumping a message"); pthread_mutex_unlock(&mtx); return; } );
183 }
184
185 switch (type) {
186/* errors */
187 case HOOK_MESSAGE_FAILOVER:
188 LOG_E("FAILOVER from '%s': %s", peer_name, buf);
189 break;
190 case HOOK_MESSAGE_PARSING_ERROR:
191 if (msg) {
192 DiamId_t id = NULL;
193 if (fd_msg_source_get( msg, &id, NULL ))
194 id = (DiamId_t)"<error getting source>";
195 if (!id)
196 id = (DiamId_t)"<local>";
197 LOG_E("PARSING ERROR: '%s' from '%s': %s", (char *)other, (char *)id, buf);
198 } else {
199 struct fd_cnx_rcvdata *rcv_data = other;
200 CHECK_MALLOC_DO(fd_dump_extend_hexdump(&buf, &len, NULL, rcv_data->buffer, rcv_data->length, 0, 0), break);
201 LOG_E("PARSING ERROR: %zdB msg from '%s': %s", rcv_data->length, peer_name, buf);
202 }
203 break;
204 case HOOK_MESSAGE_PARSING_ERROR2:
205 LOG_E("PARSING ERROR, returning: %s", buf);
206 break;
207 case HOOK_MESSAGE_ROUTING_ERROR:
208 LOG_E("ROUTING ERROR '%s' for: %s", (char *)other, buf);
209 break;
210 case HOOK_MESSAGE_DROPPED:
211 LOG_E("DROPPED '%s' %s", (char *)other, buf);
212 break;
213
214/* send receive */
215 case HOOK_MESSAGE_RECEIVED:
216 LOG_N("RCV from '%s': %s", peer_name, buf);
217 break;
218 case HOOK_MESSAGE_SENDING:
219 LOG_N("SNDING to '%s': %s", peer_name, buf);
220 break;
221 case HOOK_MESSAGE_SENT:
222 LOG_N("SND to '%s': %s", peer_name, buf);
223 break;
224
225/* routing */
226 case HOOK_MESSAGE_LOCAL:
227 LOG_N("ISSUED: %s", buf);
228 break;
229 case HOOK_MESSAGE_ROUTING_FORWARD:
230 LOG_N("FORWARDING: %s", buf);
231 break;
232 case HOOK_MESSAGE_ROUTING_LOCAL:
233 LOG_N("DISPATCHING: %s", buf);
234 break;
235
236/* peers */
237 case HOOK_PEER_CONNECT_FAILED:
238 LOG_N("CONNECT FAILED to %s: %s", peer_name, (char *)other);
239 break;
240 case HOOK_PEER_CONNECT_SUCCESS: {
241 char protobuf[40];
242 if (peer) {
243 CHECK_FCT_DO(fd_peer_cnx_proto_info(peer, protobuf, sizeof(protobuf)), break );
244 } else {
245 protobuf[0] = '-';
246 protobuf[1] = '\0';
247 }
248 LOG_N("CONNECTED TO '%s' (%s): %s", peer_name, protobuf, buf);
249 }
250 break;
251/* Not handled */
252 case HOOK_DATA_RECEIVED:
253 break;
254 }
255
256 CHECK_POSIX_DO( pthread_mutex_unlock(&mtx), );
257}
258
259static void md_hook_cb_compact(enum fd_hook_type type, struct msg * msg, struct peer_hdr * peer, void * other, struct fd_hook_permsgdata *pmd, void * regdata)
260{
261 char * peer_name = peer ? peer->info.pi_diamid : "<unknown peer>";
262
263 CHECK_POSIX_DO( pthread_mutex_lock(&mtx), );
264
265 if (msg) {
266 CHECK_MALLOC_DO( fd_msg_dump_summary(&buf, &len, NULL, msg, fd_g_config->cnf_dict, 0, 0),
267 { LOG_E("Error while dumping a message"); pthread_mutex_unlock(&mtx); return; } );
268 }
269
270 switch (type) {
271/* errors */
272 case HOOK_MESSAGE_FAILOVER:
273 LOG_E("FAILOVER from '%s': %s", peer_name, buf);
274 break;
275 case HOOK_MESSAGE_PARSING_ERROR:
276 if (msg) {
277 DiamId_t id = NULL;
278 if (fd_msg_source_get( msg, &id, NULL ))
279 id = (DiamId_t)"<error getting source>";
280 if (!id)
281 id = (DiamId_t)"<local>";
282 LOG_E("PARSING ERROR: '%s' from '%s': %s", (char *)other, (char *)id, buf);
283 } else {
284 struct fd_cnx_rcvdata *rcv_data = other;
285 CHECK_MALLOC_DO(fd_dump_extend_hexdump(&buf, &len, NULL, rcv_data->buffer, rcv_data->length, 0, 0), break);
286 LOG_E("PARSING ERROR: %zdB msg from '%s': %s", rcv_data->length, peer_name, buf);
287 }
288 break;
289 case HOOK_MESSAGE_PARSING_ERROR2:
290 LOG_E("PARSING ERROR, returning: %s", buf);
291 break;
292 case HOOK_MESSAGE_ROUTING_ERROR:
293 LOG_E("ROUTING ERROR '%s' for: %s", (char *)other, buf);
294 break;
295 case HOOK_MESSAGE_DROPPED:
296 LOG_E("DROPPED '%s' %s", (char *)other, buf);
297 break;
298
299/* send receive */
300 case HOOK_MESSAGE_RECEIVED:
301 LOG_N("RCV from '%s': %s", peer_name, buf);
302 break;
303 case HOOK_MESSAGE_SENDING:
304 LOG_N("SNDING to '%s': %s", peer_name, buf);
305 break;
306 case HOOK_MESSAGE_SENT:
307 LOG_N("SND to '%s': %s", peer_name, buf);
308 break;
309
310/* routing */
311 case HOOK_MESSAGE_LOCAL:
312 LOG_N("ISSUED: %s", buf);
313 break;
314 case HOOK_MESSAGE_ROUTING_FORWARD:
315 LOG_N("FORWARDING: %s", buf);
316 break;
317 case HOOK_MESSAGE_ROUTING_LOCAL:
318 LOG_N("DISPATCHING: %s", buf);
319 break;
320
321/* peers */
322 case HOOK_PEER_CONNECT_FAILED:
323 LOG_N("CONNECT FAILED to %s: %s", peer_name, (char *)other);
324 break;
325 case HOOK_PEER_CONNECT_SUCCESS: {
326 char protobuf[40];
327 if (peer) {
328 CHECK_FCT_DO(fd_peer_cnx_proto_info(peer, protobuf, sizeof(protobuf)), break );
329 } else {
330 protobuf[0] = '-';
331 protobuf[1] = '\0';
332 }
333 LOG_N("CONNECTED TO '%s' (%s)", peer_name, protobuf);
334 }
335 break;
336/* Not handled */
337 case HOOK_DATA_RECEIVED:
338 break;
339 }
340
341 CHECK_POSIX_DO( pthread_mutex_unlock(&mtx), );
342}
343
344static void md_hook_cb_quiet(enum fd_hook_type type, struct msg * msg, struct peer_hdr * peer, void * other, struct fd_hook_permsgdata *pmd, void * regdata)
345{
346}
347
348/* Entry point */
349static int md_main(char * conffile)
350{
351 uint32_t mask_errors, mask_sndrcv, mask_routing, mask_peers;
352 uint32_t mask_quiet, mask_compact, mask_full, mask_tree;
353 TRACE_ENTRY("%p", conffile);
354
355 if (conffile != NULL) {
356 char * endp;
357 dump_level = (uint32_t)strtoul(conffile, &endp, 16);
358 CHECK_PARAMS_DO( *endp == '\0', {
359 LOG_E("Configuration parameter must be in the form \"0xNNNN\"");
360 return EINVAL; });
361 }
362
363 mask_errors = HOOK_MASK( HOOK_MESSAGE_FAILOVER, HOOK_MESSAGE_PARSING_ERROR, HOOK_MESSAGE_PARSING_ERROR2, HOOK_MESSAGE_ROUTING_ERROR, HOOK_MESSAGE_DROPPED );
364 mask_sndrcv = HOOK_MASK( HOOK_MESSAGE_RECEIVED, HOOK_MESSAGE_SENT ); /* We don t access SENDING hook here */
365 mask_routing= HOOK_MASK( HOOK_MESSAGE_LOCAL, HOOK_MESSAGE_ROUTING_FORWARD, HOOK_MESSAGE_ROUTING_LOCAL );
366 mask_peers = HOOK_MASK( HOOK_PEER_CONNECT_FAILED, HOOK_PEER_CONNECT_SUCCESS );
367
368 mask_quiet = (dump_level & HK_ERRORS_QUIET) ? mask_errors : 0;
369 mask_quiet |= (dump_level & HK_SNDRCV_QUIET) ? mask_sndrcv : 0;
370 mask_quiet |= (dump_level & HK_ROUTING_QUIET) ? mask_routing : 0;
371 mask_quiet |= (dump_level & HK_PEERS_QUIET) ? mask_peers : 0;
372
373 mask_compact = (dump_level & HK_ERRORS_COMPACT) ? mask_errors : 0;
374 mask_compact |= (dump_level & HK_SNDRCV_COMPACT) ? mask_sndrcv : 0;
375 mask_compact |= (dump_level & HK_ROUTING_COMPACT) ? mask_routing : 0;
376 mask_compact |= (dump_level & HK_PEERS_COMPACT) ? mask_peers : 0;
377
378 mask_full = (dump_level & HK_ERRORS_FULL) ? mask_errors : 0;
379 mask_full |= (dump_level & HK_SNDRCV_FULL) ? mask_sndrcv : 0;
380 mask_full |= (dump_level & HK_ROUTING_FULL) ? mask_routing : 0;
381 mask_full |= (dump_level & HK_PEERS_FULL) ? mask_peers : 0;
382
383 mask_tree = (dump_level & HK_ERRORS_TREE) ? mask_errors : 0;
384 mask_tree |= (dump_level & HK_SNDRCV_TREE) ? mask_sndrcv : 0;
385 mask_tree |= (dump_level & HK_ROUTING_TREE) ? mask_routing : 0;
386 mask_tree |= (dump_level & HK_PEERS_TREE) ? mask_peers : 0;
387
388 if (mask_quiet) {
389 CHECK_FCT( fd_hook_register( mask_quiet, md_hook_cb_quiet, NULL, NULL, &md_hdl[0]) );
390 }
391 if (mask_compact) {
392 CHECK_FCT( fd_hook_register( mask_compact, md_hook_cb_compact, NULL, NULL, &md_hdl[1]) );
393 }
394 if (mask_full) {
395 CHECK_FCT( fd_hook_register( mask_full, md_hook_cb_full, NULL, NULL, &md_hdl[2]) );
396 }
397 if (mask_tree) {
398 CHECK_FCT( fd_hook_register( mask_tree, md_hook_cb_tree, NULL, NULL, &md_hdl[3]) );
399 }
400
401 return 0;
402}
403
404/* Cleanup */
405void fd_ext_fini(void)
406{
407 TRACE_ENTRY();
408 if (md_hdl[0]) { CHECK_FCT_DO( fd_hook_unregister( md_hdl[0] ), ); }
409 if (md_hdl[1]) { CHECK_FCT_DO( fd_hook_unregister( md_hdl[1] ), ); }
410 if (md_hdl[2]) { CHECK_FCT_DO( fd_hook_unregister( md_hdl[2] ), ); }
411 if (md_hdl[2]) { CHECK_FCT_DO( fd_hook_unregister( md_hdl[3] ), ); }
412 return ;
413}
414
415EXTENSION_ENTRY("dbg_msg_dumps", md_main);