diff --git a/src/app-layer-htp.c b/src/app-layer-htp.c index 8fbd123e3c..9bc39f1f6f 100644 --- a/src/app-layer-htp.c +++ b/src/app-layer-htp.c @@ -256,9 +256,12 @@ static int HTPHandleRequestData(Flow *f, void *htp_state, goto error; } - if (cfgnode != NULL && cfgnode->prefix != NULL) { - htp = ((HTPCfgRec*)(cfgnode->prefix->user_data_result))->cfg; - SCLogDebug("LIBHTP using config: %p", htp); + if (cfgnode != NULL) { + HTPCfgRec *htp_cfg_rec = SC_RADIX_NODE_USERDATA(cfgnode, HTPCfgRec); + if (htp_cfg_rec != NULL) { + htp = htp_cfg_rec->cfg; + SCLogDebug("LIBHTP using config: %p", htp); + } } else { SCLogDebug("Using default HTP config: %p", htp); } @@ -1680,8 +1683,12 @@ libhtp:\n\ addr = "192.168.10.42"; if (inet_pton(AF_INET, addr, buf) == 1) { cfgnode = SCRadixFindKeyIPV4BestMatch(buf, cfgtree); - if (cfgnode != NULL && cfgnode->prefix != NULL) { - htp = ((HTPCfgRec*)(cfgnode->prefix->user_data_result))->cfg; + if (cfgnode != NULL) { + HTPCfgRec *htp_cfg_rec = SC_RADIX_NODE_USERDATA(cfgnode, HTPCfgRec); + if (htp_cfg_rec != NULL) { + htp = htp_cfg_rec->cfg; + SCLogDebug("LIBHTP using config: %p", htp); + } } if (htp == NULL) { printf("Could not get config for: %s\n", addr); @@ -1696,8 +1703,12 @@ libhtp:\n\ addr = "::1"; if (inet_pton(AF_INET6, addr, buf) == 1) { cfgnode = SCRadixFindKeyIPV6BestMatch(buf, cfgtree); - if (cfgnode != NULL && cfgnode->prefix != NULL) { - htp = ((HTPCfgRec*)(cfgnode->prefix->user_data_result))->cfg; + if (cfgnode != NULL) { + HTPCfgRec *htp_cfg_rec = SC_RADIX_NODE_USERDATA(cfgnode, HTPCfgRec); + if (htp_cfg_rec != NULL) { + htp = htp_cfg_rec->cfg; + SCLogDebug("LIBHTP using config: %p", htp); + } } if (htp == NULL) { printf("Could not get config for: %s\n", addr); @@ -1769,8 +1780,12 @@ libhtp:\n\ SCRadixNode *cfgnode = NULL; htp_cfg_t *htp = cfglist.cfg; cfgnode = SCRadixFindKeyIPV4BestMatch((uint8_t *)GET_IPV4_DST_ADDR_PTR(&f), cfgtree); - if (cfgnode != NULL && cfgnode->prefix != NULL) { - htp = ((HTPCfgRec*)(cfgnode->prefix->user_data_result))->cfg; + if (cfgnode != NULL) { + HTPCfgRec *htp_cfg_rec = SC_RADIX_NODE_USERDATA(cfgnode, HTPCfgRec); + if (htp_cfg_rec != NULL) { + htp = htp_cfg_rec->cfg; + SCLogDebug("LIBHTP using config: %p", htp); + } } if (htp == NULL) { printf("Could not get config for: %s\n", addr); diff --git a/src/util-radix-tree.c b/src/util-radix-tree.c index fc01e82d6b..a08f395724 100644 --- a/src/util-radix-tree.c +++ b/src/util-radix-tree.c @@ -1,4 +1,7 @@ -/** Copyright (c) 2009 Open Information Security Foundation. +/* Copyright (c) 2009, 2010 Open Information Security Foundation. */ + +/** + * \file * \author Anoop Saldanha */ @@ -4042,6 +4045,76 @@ int SCRadixTestIPV4NetblockInsertion25(void) return result; } +/** + * \test SC_RADIX_NODE_USERDATA macro + */ +static int SCRadixTestUserdataMacro01(void) { + SCRadixNode node; + int result = 0; + + memset(&node, 0x00, sizeof(node)); + + void *ptr = SC_RADIX_NODE_USERDATA(&node, void); + if (ptr != NULL) { + printf("ptr %p, expected NULL: ", ptr); + goto end; + } + + result = 1; +end: + return result; +} + +/** + * \test SC_RADIX_NODE_USERDATA macro + */ +static int SCRadixTestUserdataMacro02(void) { + SCRadixNode node; + SCRadixPrefix prefix; + int result = 0; + + memset(&node, 0x00, sizeof(node)); + memset(&prefix, 0x00, sizeof(prefix)); + + node.prefix = &prefix; + + void *ptr = SC_RADIX_NODE_USERDATA(&node, void); + if (ptr != NULL) { + printf("ptr %p, expected NULL: ", ptr); + goto end; + } + + result = 1; +end: + return result; +} + +/** + * \test SC_RADIX_NODE_USERDATA macro + */ +static int SCRadixTestUserdataMacro03(void) { + SCRadixNode node; + SCRadixPrefix prefix; + int result = 0; + void *somep = &result; + + memset(&node, 0x00, sizeof(node)); + memset(&prefix, 0x00, sizeof(prefix)); + + node.prefix = &prefix; + prefix.user_data_result = somep; + + void *ptr = SC_RADIX_NODE_USERDATA(&node, void); + if (ptr != somep) { + printf("ptr %p, expected %p: ", ptr, somep); + goto end; + } + + result = 1; +end: + return result; +} + #endif void SCRadixRegisterTests(void) @@ -4092,6 +4165,12 @@ void SCRadixRegisterTests(void) SCRadixTestIPV6NetBlocksAndBestSearch24, 1); UtRegisterTest("SCRadixTestIPV4NetblockInsertion25", SCRadixTestIPV4NetblockInsertion25, 1); + UtRegisterTest("SCRadixTestUserdataMacro01", + SCRadixTestUserdataMacro01, 1); + UtRegisterTest("SCRadixTestUserdataMacro02", + SCRadixTestUserdataMacro02, 1); + UtRegisterTest("SCRadixTestUserdataMacro03", + SCRadixTestUserdataMacro03, 1); #endif return; diff --git a/src/util-radix-tree.h b/src/util-radix-tree.h index cd37f08071..c64e2b7559 100644 --- a/src/util-radix-tree.h +++ b/src/util-radix-tree.h @@ -1,4 +1,7 @@ -/** Copyright (c) 2009 Open Information Security Foundation. +/* Copyright (c) 2009, 2010 Open Information Security Foundation. */ + +/** + * \file * \author Anoop Saldanha */ @@ -8,7 +11,8 @@ #define SC_RADIX_BITTEST(x, y) ((x) & (y)) /** - * \brief Macro to fetch the user data from a node. + * \brief Macro to fetch the user data from a node. It checks if node is a + * valid pointer and if node->prefix is as well. * * \param node Variable name/expression containing the node * \param type User data type in which the node points to @@ -16,8 +20,8 @@ * \returns User data within the node */ #define SC_RADIX_NODE_USERDATA(node, type) \ - ((type *)( ((node) == NULL || (node)->prefix == NULL) \ - ? NULL : (node)->prefix->user_data_result )) + ((type *)(((node) != NULL) ? (((node)->prefix != NULL) ? \ + (node)->prefix->user_data_result : NULL) : NULL)) /** * \brief Structure that hold the user data and the netmask associated with it.