From ab215c72f6e5f2702e4d2eb05e7d5a71b5699863 Mon Sep 17 00:00:00 2001 From: Duarte Silva Date: Tue, 30 Jul 2013 16:17:34 +0100 Subject: [PATCH] Now using the common functions --- src/util-host-os-info.c | 99 +++-------------------------------------- src/util-radix-tree.c | 94 +++----------------------------------- 2 files changed, 12 insertions(+), 181 deletions(-) diff --git a/src/util-host-os-info.c b/src/util-host-os-info.c index e2d1698de2..1eeaa531ac 100644 --- a/src/util-host-os-info.c +++ b/src/util-host-os-info.c @@ -27,6 +27,7 @@ #include "util-host-os-info.h" #include "util-error.h" #include "util-debug.h" +#include "util-ip.h" #include "util-radix-tree.h" #include "stream-tcp-private.h" #include "stream-tcp-reassemble.h" @@ -59,61 +60,6 @@ SCEnumCharMap sc_hinfo_os_policy_map[ ] = { /** Radix tree that holds the host OS information */ static SCRadixTree *sc_hinfo_tree = NULL; -/** - * \brief Validates an IPV4 address and returns the network endian arranged - * version of the IPV4 address - * - * \param addr Pointer to a character string containing an IPV4 address. A - * valid IPV4 address is a character string containing a dotted - * format of "ddd.ddd.ddd.ddd" - * - * \retval Pointer to an in_addr instance containing the network endian format - * of the IPV4 address - * \retval NULL if the IPV4 address is invalid - */ -static struct in_addr *SCHInfoValidateIPV4Address(const char *addr_str) -{ - struct in_addr *addr = NULL; - - if ( (addr = SCMalloc(sizeof(struct in_addr))) == NULL) { - SCLogError(SC_ERR_MEM_ALLOC, "Fatal error encountered in SCHInfoValidateIPV4Address. Mem not allocated"); - return NULL; - } - - if (inet_pton(AF_INET, addr_str, addr) <= 0) { - SCFree(addr); - return NULL; - } - - return addr; -} - -/** - * \brief Validates an IPV6 address and returns the network endian arranged - * version of the IPV6 addresss - * - * \param addr Pointer to a character string containing an IPV6 address - * - * \retval Pointer to a in6_addr instance containing the network endian format - * of the IPV6 address - * \retval NULL if the IPV6 address is invalid - */ -static struct in6_addr *SCHInfoValidateIPV6Address(const char *addr_str) -{ - struct in6_addr *addr = NULL; - - if ( (addr = SCMalloc(sizeof(struct in6_addr))) == NULL) { - SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCHInfoValidateIPV6Address. Exiting..."); - exit(EXIT_FAILURE); - } - - if (inet_pton(AF_INET6, addr_str, addr) <= 0) { - SCFree(addr); - return NULL; - } - - return addr; -} /** * \brief Allocates the host_os flavour wrapped in user_data variable to be sent @@ -159,37 +105,6 @@ static void SCHInfoFreeUserDataOSPolicy(void *data) return; } -/** - * \brief Culls the non-netmask portion of the ip address. - * - * This function can also be used for any general purpose use, to mask - * the first netmask bits of the stream data sent as argument - * - * \param stream Pointer to the data to be masked - * \param netmask The mask length(netmask) - * \param bitlen The bitlen of the stream - */ -static void SCHInfoMaskIPNetblock(uint8_t *stream, int netmask, int bitlen) -{ - int bytes = 0; - int mask = 0; - int i = 0; - - bytes = bitlen / 8; - for (i = 0; i < bytes; i++) { - mask = -1; - if ( ((i + 1) * 8) > netmask) { - if ( ((i + 1) * 8 - netmask) < 8) - mask = -1 << ((i + 1) * 8 - netmask); - else - mask = 0; - } - stream[i] &= mask; - } - - return; -} - /** * \brief Used to add the host-os-info data obtained from the conf * @@ -259,7 +174,7 @@ int SCHInfoAddHostOSInfo(char *host_os, char *host_os_ip_range, int is_ipv4) if (index(ip_str, ':') == NULL) { /* if we are here, we have an IPV4 address */ - if ( (ipv4_addr = SCHInfoValidateIPV4Address(ip_str)) == NULL) { + if ( (ipv4_addr = ValidateIPV4Address(ip_str)) == NULL) { SCLogError(SC_ERR_INVALID_IPV4_ADDR, "Invalid IPV4 address"); return -1; } @@ -275,13 +190,13 @@ int SCHInfoAddHostOSInfo(char *host_os, char *host_os_ip_range, int is_ipv4) return -1; } - SCHInfoMaskIPNetblock((uint8_t *)ipv4_addr, netmask_value, 32); + MaskIPNetblock((uint8_t *)ipv4_addr, netmask_value, 32); SCRadixAddKeyIPV4Netblock((uint8_t *)ipv4_addr, sc_hinfo_tree, (void *)user_data, netmask_value); } } else { /* if we are here, we have an IPV6 address */ - if ( (ipv6_addr = SCHInfoValidateIPV6Address(ip_str)) == NULL) { + if ( (ipv6_addr = ValidateIPV6Address(ip_str)) == NULL) { SCLogError(SC_ERR_INVALID_IPV6_ADDR, "Invalid IPV6 address inside"); return -1; } @@ -297,7 +212,7 @@ int SCHInfoAddHostOSInfo(char *host_os, char *host_os_ip_range, int is_ipv4) return -1; } - SCHInfoMaskIPNetblock((uint8_t *)ipv6_addr, netmask_value, 128); + MaskIPNetblock((uint8_t *)ipv6_addr, netmask_value, 128); SCRadixAddKeyIPV6Netblock((uint8_t *)ipv6_addr, sc_hinfo_tree, (void *)user_data, netmask_value); } @@ -333,7 +248,7 @@ int SCHInfoGetHostOSFlavour(char *ip_addr_str) return -1; if (index(ip_addr_str, ':') != NULL) { - if ( (ipv6_addr = SCHInfoValidateIPV6Address(ip_addr_str)) == NULL) { + if ( (ipv6_addr = ValidateIPV6Address(ip_addr_str)) == NULL) { SCLogError(SC_ERR_INVALID_IPV4_ADDR, "Invalid IPV4 address"); return -1; } @@ -343,7 +258,7 @@ int SCHInfoGetHostOSFlavour(char *ip_addr_str) else return *((int *)node->prefix->user_data_result); } else { - if ( (ipv4_addr = SCHInfoValidateIPV4Address(ip_addr_str)) == NULL) { + if ( (ipv4_addr = ValidateIPV4Address(ip_addr_str)) == NULL) { SCLogError(SC_ERR_INVALID_IPV4_ADDR, "Invalid IPV4 address"); return -1; } diff --git a/src/util-radix-tree.c b/src/util-radix-tree.c index e57f8aaf1b..8fb961dd14 100644 --- a/src/util-radix-tree.c +++ b/src/util-radix-tree.c @@ -27,94 +27,10 @@ #include "util-radix-tree.h" #include "util-debug.h" #include "util-error.h" +#include "util-ip.h" #include "util-unittest.h" #include "util-memcmp.h" -/** - * \brief Validates an IPV4 address and returns the network endian arranged - * version of the IPV4 address - * - * \param addr Pointer to a character string containing an IPV4 address. A - * valid IPV4 address is a character string containing a dotted - * format of "ddd.ddd.ddd.ddd" - * - * \retval Pointer to an in_addr instance containing the network endian format - * of the IPV4 address - * \retval NULL if the IPV4 address is invalid - */ -struct in_addr *SCRadixValidateIPV4Address(const char *addr_str) -{ - struct in_addr *addr = NULL; - - if ( (addr = SCMalloc(sizeof(struct in_addr))) == NULL) { - SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCRadixValidateIPV4Address. Exiting..."); - exit(EXIT_FAILURE); - } - - if (inet_pton(AF_INET, addr_str, addr) <= 0) { - SCFree(addr); - return NULL; - } - - return addr; -} - -/** - * \brief Validates an IPV6 address and returns the network endian arranged - * version of the IPV6 addresss - * - * \param addr Pointer to a character string containing an IPV6 address - * - * \retval Pointer to a in6_addr instance containing the network endian format - * of the IPV6 address - * \retval NULL if the IPV6 address is invalid - */ -struct in6_addr *SCRadixValidateIPV6Address(const char *addr_str) -{ - struct in6_addr *addr = NULL; - - if ( (addr = SCMalloc(sizeof(struct in6_addr))) == NULL) { - SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCRadixValidateIPV6Address. Exiting..."); - exit(EXIT_FAILURE); - } - - if (inet_pton(AF_INET6, addr_str, addr) <= 0) { - SCFree(addr); - return NULL; - } - - return addr; -} - -/** - * \brief Chops an ip address against a netmask. For example an ip address - * 192.168.240.1 would be chopped to 192.168.224.0 against a netmask - * value of 19. - * - * \param stream Pointer the ip address that has to be chopped. - * \param netmask The netmask value (cidr) to which the ip address has to be chopped. - */ -void SCRadixChopIPAddressAgainstNetmask(uint8_t *stream, uint8_t netmask, - uint16_t key_bitlen) -{ - int mask = 0; - int i = 0; - int bytes = key_bitlen / 8; - - for (i = 0; i < bytes; i++) { - mask = -1; - if ( ((i + 1) * 8) > netmask) { - if ( ((i + 1) * 8 - netmask) < 8) - mask = -1 << ((i + 1) * 8 - netmask); - else - mask = 0; - } - stream[i] &= mask; - } - - return; -} - /** * \brief Allocates and returns a new instance of SCRadixUserData. * @@ -594,7 +510,7 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, } /* chop the ip address against a netmask */ - SCRadixChopIPAddressAgainstNetmask(key_stream, netmask, key_bitlen); + MaskIPNetblock(key_stream, netmask, key_bitlen); if ( (prefix = SCRadixCreatePrefix(key_stream, key_bitlen, user, netmask)) == NULL) { @@ -4158,7 +4074,7 @@ int SCRadixTestIPV4NetblockInsertion26(void) return 0; tmp = SCRadixAddKeyIPV4Netblock((uint8_t *)&servaddr.sin_addr, tree, str, 0); if (!tmp) { - printf("Not inserted correctly 1 :"); + printf("Not inserted correctly 1:"); result = 0; goto this_end; } @@ -4169,7 +4085,7 @@ int SCRadixTestIPV4NetblockInsertion26(void) return 0; tmp = SCRadixAddKeyIPV4Netblock((uint8_t *)&servaddr.sin_addr, tree, str, 5); if (!tmp) { - printf("Not inserted correctly 2 :"); + printf("Not inserted correctly 2:"); result = 0; goto this_end; } @@ -4182,7 +4098,7 @@ int SCRadixTestIPV4NetblockInsertion26(void) } tmp = SCRadixAddKeyIPV4Netblock((uint8_t *)&servaddr.sin_addr, tree, str, 7); if (!tmp) { - printf("Not inserted correctly 3 :"); + printf("Not inserted correctly 3:"); result = 0; goto this_end; }