From b22d1d8e6a31ee878f1c205eb62b9e4bc3d03d9a Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Mon, 18 May 2015 12:48:13 +0200 Subject: File rename to avoid name clashes Cleaned up autogen.sh --- src/lib/protocols/redis.c | 92 -------------------------------------- src/lib/protocols/redis_net.c | 92 ++++++++++++++++++++++++++++++++++++++ src/lib/protocols/zeromq.c | 100 ++++++++++++++++++++++++++++++++++++++++++ src/lib/protocols/zmq.c | 100 ------------------------------------------ 4 files changed, 192 insertions(+), 192 deletions(-) delete mode 100644 src/lib/protocols/redis.c create mode 100644 src/lib/protocols/redis_net.c create mode 100644 src/lib/protocols/zeromq.c delete mode 100644 src/lib/protocols/zmq.c diff --git a/src/lib/protocols/redis.c b/src/lib/protocols/redis.c deleted file mode 100644 index a47778b48..000000000 --- a/src/lib/protocols/redis.c +++ /dev/null @@ -1,92 +0,0 @@ -/* - * redis.c - * - * Copyright (C) 2011-15 - ntop.org - * - * nDPI is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * nDPI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with nDPI. If not, see . - * - */ - - -#include "ndpi_api.h" - -#ifdef NDPI_PROTOCOL_REDIS - -static void ndpi_int_redis_add_connection(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { - ndpi_int_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_REDIS, NDPI_REAL_PROTOCOL); -} - - -static void ndpi_check_redis(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { - struct ndpi_packet_struct *packet = &flow->packet; - u_int32_t payload_len = packet->payload_packet_len; - - if(payload_len == 0) return; /* Shouldn't happen */ - - /* Break after 20 packets. */ - if(flow->packet_counter > 20) { - NDPI_LOG(NDPI_PROTOCOL_REDIS, ndpi_struct, NDPI_LOG_DEBUG, "Exclude Redis.\n"); - NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_REDIS); - return; - } - - if(packet->packet_direction == 0) - flow->redis_s2d_first_char = packet->payload[0]; - else - flow->redis_d2s_first_char = packet->payload[0]; - - if((flow->redis_s2d_first_char != '\0') && (flow->redis_d2s_first_char != '\0')) { - /* - *1 - $4 - PING - +PONG - *3 - $3 - SET - $19 - dns.cache.127.0.0.1 - $9 - localhost - +OK - */ - - if(((flow->redis_s2d_first_char == '*') - && ((flow->redis_d2s_first_char == '+') || (flow->redis_d2s_first_char == ':'))) - || ((flow->redis_d2s_first_char == '*') - && ((flow->redis_s2d_first_char == '+') || (flow->redis_s2d_first_char == ':')))) { - NDPI_LOG(NDPI_PROTOCOL_REDIS, ndpi_struct, NDPI_LOG_DEBUG, "Found Redis.\n"); - ndpi_int_redis_add_connection(ndpi_struct, flow); - } else { - NDPI_LOG(NDPI_PROTOCOL_REDIS, ndpi_struct, NDPI_LOG_DEBUG, "Exclude Redis.\n"); - NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_REDIS); - } - } else - return; /* Too early */ -} - -void ndpi_search_redis(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { - struct ndpi_packet_struct *packet = &flow->packet; - - NDPI_LOG(NDPI_PROTOCOL_REDIS, ndpi_struct, NDPI_LOG_DEBUG, "Redis detection...\n"); - - /* skip marked packets */ - if (packet->detected_protocol_stack[0] != NDPI_PROTOCOL_REDIS) { - if (packet->tcp_retransmission == 0) { - ndpi_check_redis(ndpi_struct, flow); - } - } -} - -#endif diff --git a/src/lib/protocols/redis_net.c b/src/lib/protocols/redis_net.c new file mode 100644 index 000000000..a47778b48 --- /dev/null +++ b/src/lib/protocols/redis_net.c @@ -0,0 +1,92 @@ +/* + * redis.c + * + * Copyright (C) 2011-15 - ntop.org + * + * nDPI is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * nDPI is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with nDPI. If not, see . + * + */ + + +#include "ndpi_api.h" + +#ifdef NDPI_PROTOCOL_REDIS + +static void ndpi_int_redis_add_connection(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { + ndpi_int_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_REDIS, NDPI_REAL_PROTOCOL); +} + + +static void ndpi_check_redis(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { + struct ndpi_packet_struct *packet = &flow->packet; + u_int32_t payload_len = packet->payload_packet_len; + + if(payload_len == 0) return; /* Shouldn't happen */ + + /* Break after 20 packets. */ + if(flow->packet_counter > 20) { + NDPI_LOG(NDPI_PROTOCOL_REDIS, ndpi_struct, NDPI_LOG_DEBUG, "Exclude Redis.\n"); + NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_REDIS); + return; + } + + if(packet->packet_direction == 0) + flow->redis_s2d_first_char = packet->payload[0]; + else + flow->redis_d2s_first_char = packet->payload[0]; + + if((flow->redis_s2d_first_char != '\0') && (flow->redis_d2s_first_char != '\0')) { + /* + *1 + $4 + PING + +PONG + *3 + $3 + SET + $19 + dns.cache.127.0.0.1 + $9 + localhost + +OK + */ + + if(((flow->redis_s2d_first_char == '*') + && ((flow->redis_d2s_first_char == '+') || (flow->redis_d2s_first_char == ':'))) + || ((flow->redis_d2s_first_char == '*') + && ((flow->redis_s2d_first_char == '+') || (flow->redis_s2d_first_char == ':')))) { + NDPI_LOG(NDPI_PROTOCOL_REDIS, ndpi_struct, NDPI_LOG_DEBUG, "Found Redis.\n"); + ndpi_int_redis_add_connection(ndpi_struct, flow); + } else { + NDPI_LOG(NDPI_PROTOCOL_REDIS, ndpi_struct, NDPI_LOG_DEBUG, "Exclude Redis.\n"); + NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_REDIS); + } + } else + return; /* Too early */ +} + +void ndpi_search_redis(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { + struct ndpi_packet_struct *packet = &flow->packet; + + NDPI_LOG(NDPI_PROTOCOL_REDIS, ndpi_struct, NDPI_LOG_DEBUG, "Redis detection...\n"); + + /* skip marked packets */ + if (packet->detected_protocol_stack[0] != NDPI_PROTOCOL_REDIS) { + if (packet->tcp_retransmission == 0) { + ndpi_check_redis(ndpi_struct, flow); + } + } +} + +#endif diff --git a/src/lib/protocols/zeromq.c b/src/lib/protocols/zeromq.c new file mode 100644 index 000000000..12548a2ed --- /dev/null +++ b/src/lib/protocols/zeromq.c @@ -0,0 +1,100 @@ +/* + * zmq.c + * + * Copyright (C) 2011-15 - ntop.org + * + * nDPI is free software: you can zmqtribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * nDPI is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with nDPI. If not, see . + * + */ + + +#include "ndpi_api.h" + +#ifdef NDPI_PROTOCOL_ZMQ + +static void ndpi_int_zmq_add_connection(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { + ndpi_int_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_ZMQ, NDPI_REAL_PROTOCOL); + NDPI_LOG(NDPI_PROTOCOL_ZMQ, ndpi_struct, NDPI_LOG_TRACE, "ZMQ Found.\n"); +} + + +static void ndpi_check_zmq(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { + struct ndpi_packet_struct *packet = &flow->packet; + u_int32_t payload_len = packet->payload_packet_len; + u_char p0[] = { 0x00, 0x00, 0x00, 0x05, 0x01, 0x66, 0x6c, 0x6f, 0x77 }; + u_char p1[] = { 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7f }; + u_char p2[] = { 0x28, 0x66, 0x6c, 0x6f, 0x77, 0x00 }; + + if(payload_len == 0) return; /* Shouldn't happen */ + + /* Break after 17 packets. */ + if(flow->packet_counter > 17) { + NDPI_LOG(NDPI_PROTOCOL_ZMQ, ndpi_struct, NDPI_LOG_TRACE, "Exclude ZMQ.\n"); + NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_ZMQ); + return; + } + + if(flow->l4.tcp.prev_zmq_pkt_len == 0) { + flow->l4.tcp.prev_zmq_pkt_len = ndpi_min(packet->payload_packet_len, 10); + memcpy(flow->l4.tcp.prev_zmq_pkt, packet->payload, flow->l4.tcp.prev_zmq_pkt_len); + return; /* Too early */ + } + + if(payload_len == 2) { + if(flow->l4.tcp.prev_zmq_pkt_len == 2) { + if((memcmp(packet->payload, "\01\01", 2) == 0) + && (memcmp(flow->l4.tcp.prev_zmq_pkt, "\01\02", 2) == 0)) { + ndpi_int_zmq_add_connection(ndpi_struct, flow); + return; + } + } else if(flow->l4.tcp.prev_zmq_pkt_len == 9) { + if((memcmp(packet->payload, "\00\00", 2) == 0) + && (memcmp(flow->l4.tcp.prev_zmq_pkt, p0, 9) == 0)) { + ndpi_int_zmq_add_connection(ndpi_struct, flow); + return; + } + } else if(flow->l4.tcp.prev_zmq_pkt_len == 10) { + if((memcmp(packet->payload, "\01\02", 2) == 0) + && (memcmp(flow->l4.tcp.prev_zmq_pkt, p1, 10) == 0)) { + ndpi_int_zmq_add_connection(ndpi_struct, flow); + return; + } + } + } else if(payload_len >= 10) { + if(flow->l4.tcp.prev_zmq_pkt_len == 10) { + if(((memcmp(packet->payload, p1, 10) == 0) + && (memcmp(flow->l4.tcp.prev_zmq_pkt, p1, 10) == 0)) + || ((memcmp(&packet->payload[1], p2, sizeof(p2)) == 0) + && (memcmp(&flow->l4.tcp.prev_zmq_pkt[1], p2, sizeof(p2)) == 0))) { + ndpi_int_zmq_add_connection(ndpi_struct, flow); + return; + } + } + } +} + +void ndpi_search_zmq(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { + struct ndpi_packet_struct *packet = &flow->packet; + + NDPI_LOG(NDPI_PROTOCOL_ZMQ, ndpi_struct, NDPI_LOG_TRACE, "ZMQ detection...\n"); + + /* skip marked packets */ + if (packet->detected_protocol_stack[0] != NDPI_PROTOCOL_ZMQ) { + if (packet->tcp_retransmission == 0) { + ndpi_check_zmq(ndpi_struct, flow); + } + } +} + +#endif diff --git a/src/lib/protocols/zmq.c b/src/lib/protocols/zmq.c deleted file mode 100644 index 12548a2ed..000000000 --- a/src/lib/protocols/zmq.c +++ /dev/null @@ -1,100 +0,0 @@ -/* - * zmq.c - * - * Copyright (C) 2011-15 - ntop.org - * - * nDPI is free software: you can zmqtribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * nDPI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with nDPI. If not, see . - * - */ - - -#include "ndpi_api.h" - -#ifdef NDPI_PROTOCOL_ZMQ - -static void ndpi_int_zmq_add_connection(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { - ndpi_int_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_ZMQ, NDPI_REAL_PROTOCOL); - NDPI_LOG(NDPI_PROTOCOL_ZMQ, ndpi_struct, NDPI_LOG_TRACE, "ZMQ Found.\n"); -} - - -static void ndpi_check_zmq(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { - struct ndpi_packet_struct *packet = &flow->packet; - u_int32_t payload_len = packet->payload_packet_len; - u_char p0[] = { 0x00, 0x00, 0x00, 0x05, 0x01, 0x66, 0x6c, 0x6f, 0x77 }; - u_char p1[] = { 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7f }; - u_char p2[] = { 0x28, 0x66, 0x6c, 0x6f, 0x77, 0x00 }; - - if(payload_len == 0) return; /* Shouldn't happen */ - - /* Break after 17 packets. */ - if(flow->packet_counter > 17) { - NDPI_LOG(NDPI_PROTOCOL_ZMQ, ndpi_struct, NDPI_LOG_TRACE, "Exclude ZMQ.\n"); - NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_ZMQ); - return; - } - - if(flow->l4.tcp.prev_zmq_pkt_len == 0) { - flow->l4.tcp.prev_zmq_pkt_len = ndpi_min(packet->payload_packet_len, 10); - memcpy(flow->l4.tcp.prev_zmq_pkt, packet->payload, flow->l4.tcp.prev_zmq_pkt_len); - return; /* Too early */ - } - - if(payload_len == 2) { - if(flow->l4.tcp.prev_zmq_pkt_len == 2) { - if((memcmp(packet->payload, "\01\01", 2) == 0) - && (memcmp(flow->l4.tcp.prev_zmq_pkt, "\01\02", 2) == 0)) { - ndpi_int_zmq_add_connection(ndpi_struct, flow); - return; - } - } else if(flow->l4.tcp.prev_zmq_pkt_len == 9) { - if((memcmp(packet->payload, "\00\00", 2) == 0) - && (memcmp(flow->l4.tcp.prev_zmq_pkt, p0, 9) == 0)) { - ndpi_int_zmq_add_connection(ndpi_struct, flow); - return; - } - } else if(flow->l4.tcp.prev_zmq_pkt_len == 10) { - if((memcmp(packet->payload, "\01\02", 2) == 0) - && (memcmp(flow->l4.tcp.prev_zmq_pkt, p1, 10) == 0)) { - ndpi_int_zmq_add_connection(ndpi_struct, flow); - return; - } - } - } else if(payload_len >= 10) { - if(flow->l4.tcp.prev_zmq_pkt_len == 10) { - if(((memcmp(packet->payload, p1, 10) == 0) - && (memcmp(flow->l4.tcp.prev_zmq_pkt, p1, 10) == 0)) - || ((memcmp(&packet->payload[1], p2, sizeof(p2)) == 0) - && (memcmp(&flow->l4.tcp.prev_zmq_pkt[1], p2, sizeof(p2)) == 0))) { - ndpi_int_zmq_add_connection(ndpi_struct, flow); - return; - } - } - } -} - -void ndpi_search_zmq(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { - struct ndpi_packet_struct *packet = &flow->packet; - - NDPI_LOG(NDPI_PROTOCOL_ZMQ, ndpi_struct, NDPI_LOG_TRACE, "ZMQ detection...\n"); - - /* skip marked packets */ - if (packet->detected_protocol_stack[0] != NDPI_PROTOCOL_ZMQ) { - if (packet->tcp_retransmission == 0) { - ndpi_check_zmq(ndpi_struct, flow); - } - } -} - -#endif -- cgit v1.2.3 From 68ac979dc53a130d8beff56b8b359133d2aeebe5 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Mon, 18 May 2015 13:00:02 +0200 Subject: Win32 fixes --- autogen.sh | 5 ++--- src/lib/Makefile.am | 4 ++-- src/lib/third_party/include/patricia.h | 8 ++++++-- src/lib/third_party/src/patricia.c | 33 +++++++++++++++++---------------- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/autogen.sh b/autogen.sh index 9853e65ef..7f109b0a5 100755 --- a/autogen.sh +++ b/autogen.sh @@ -1,7 +1,6 @@ #!/bin/sh -# -# 1.5.1 r8171 06/09/2014 -# + +/bin/rm -f configure config.h config.h.in src/lib/Makefile.in autoreconf -ivf ./configure diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 2ecf1e2ac..4aec40aeb 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -95,7 +95,7 @@ libndpi_la_SOURCES = ndpi_content_match.c.inc \ protocols/quake.c \ protocols/radius.c \ protocols/rdp.c \ - protocols/redis.c \ + protocols/redis_net.c \ protocols/rsync.c \ protocols/rtcp.c \ protocols/rtmp.c \ @@ -148,7 +148,7 @@ libndpi_la_SOURCES = ndpi_content_match.c.inc \ protocols/xdmcp.c \ protocols/yahoo.c \ protocols/zattoo.c \ - protocols/zmq.c \ + protocols/zeromq.c \ third_party/include/actypes.h \ third_party/include/ahocorasick.h \ third_party/include/node.h \ diff --git a/src/lib/third_party/include/patricia.h b/src/lib/third_party/include/patricia.h index be8476e85..c369bd3a7 100644 --- a/src/lib/third_party/include/patricia.h +++ b/src/lib/third_party/include/patricia.h @@ -41,7 +41,11 @@ #ifndef _PATRICIA_H #define _PATRICIA_H -#define HAVE_IPV6 +#ifndef WIN32 +#define PATRICIA_IPV6 HAVE_IPV6 +#else +#define PATRICIA_IPV6 0 +#endif /* typedef unsigned int u_int; */ /* { from defs.h */ @@ -94,7 +98,7 @@ typedef struct the_prefix_t { int ref_count; /* reference count */ union { struct in_addr sin; -#ifdef HAVE_IPV6 +#ifdef PATRICIA_IPV6 struct in6_addr sin6; #endif /* IPV6 */ } add; diff --git a/src/lib/third_party/src/patricia.c b/src/lib/third_party/src/patricia.c index b7b4c2010..7a95a8765 100644 --- a/src/lib/third_party/src/patricia.c +++ b/src/lib/third_party/src/patricia.c @@ -60,6 +60,7 @@ #include "patricia.h" + #ifdef __KERNEL__ long atol(const char *nptr) { @@ -125,12 +126,12 @@ inet_pton (int af, const char *src, void *dst) } } #ifdef NT -#if defined(HAVE_IPV6) && (!defined(__KERNEL__)) +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) else if(af == AF_INET6) { struct in6_addr Address; return (inet6_addr(src, &Address)); } -#endif /* HAVE_IPV6 */ +#endif /* PATRICIA_IPV6 */ #endif /* NT */ #ifndef NT else { @@ -174,10 +175,10 @@ ndpi_my_inet_pton (int af, const char *src, void *dst) } memcpy (dst, xp, sizeof(struct in_addr)); return (1); -#if defined(HAVE_IPV6) && (!defined(__KERNEL__)) +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) } else if(af == AF_INET6) { return (inet_pton (af, src, dst)); -#endif /* HAVE_IPV6 */ +#endif /* PATRICIA_IPV6 */ } else { #ifndef NT #ifndef __KERNEL__ @@ -235,7 +236,7 @@ ndpi_ndpi_prefix_toa2x (prefix_t *prefix, char *buff, int with_len) } return (buff); } -#if defined(HAVE_IPV6) && (!defined(__KERNEL__)) +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) else if(prefix->family == AF_INET6) { char *r; r = (char *) inet_ntop (AF_INET6, &prefix->add.sin6, buff, 48 /* a guess value */ ); @@ -245,7 +246,7 @@ ndpi_ndpi_prefix_toa2x (prefix_t *prefix, char *buff, int with_len) } return (buff); } -#endif /* HAVE_IPV6 */ +#endif /* PATRICIA_IPV6 */ else return (NULL); } @@ -273,7 +274,7 @@ ndpi_New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix) int dynamic_allocated = 0; int default_bitlen = sizeof(struct in_addr) * 8; -#if defined(HAVE_IPV6) && (!defined(__KERNEL__)) +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) if(family == AF_INET6) { default_bitlen = sizeof(struct in6_addr) * 8; if(prefix == NULL) { @@ -283,7 +284,7 @@ ndpi_New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix) memcpy (&prefix->add.sin6, dest, sizeof(struct in6_addr)); } else -#endif /* HAVE_IPV6 */ +#endif /* PATRICIA_IPV6 */ if(family == AF_INET) { if(prefix == NULL) { #ifndef NT @@ -327,9 +328,9 @@ ndpi_ascii2prefix (int family, char *string) long maxbitlen = 0; char *cp; struct in_addr sin; -#if defined(HAVE_IPV6) && (!defined(__KERNEL__)) +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) struct in6_addr sin6; -#endif /* HAVE_IPV6 */ +#endif /* PATRICIA_IPV6 */ char save[MAXLINE]; if(string == NULL) @@ -338,19 +339,19 @@ ndpi_ascii2prefix (int family, char *string) /* easy way to handle both families */ if(family == 0) { family = AF_INET; -#if defined(HAVE_IPV6) && (!defined(__KERNEL__)) +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) if(strchr (string, ':')) family = AF_INET6; -#endif /* HAVE_IPV6 */ +#endif /* PATRICIA_IPV6 */ } if(family == AF_INET) { maxbitlen = sizeof(struct in_addr) * 8; } -#if defined(HAVE_IPV6) && (!defined(__KERNEL__)) +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) else if(family == AF_INET6) { maxbitlen = sizeof(struct in6_addr) * 8; } -#endif /* HAVE_IPV6 */ +#endif /* PATRICIA_IPV6 */ if((cp = strchr (string, '/')) != NULL) { bitlen = atol (cp + 1); @@ -372,7 +373,7 @@ ndpi_ascii2prefix (int family, char *string) return (ndpi_New_Prefix (AF_INET, &sin, bitlen)); } -#if defined(HAVE_IPV6) && (!defined(__KERNEL__)) +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) else if(family == AF_INET6) { // Get rid of this with next IPv6 upgrade #if defined(NT) && !defined(HAVE_INET_NTOP) @@ -384,7 +385,7 @@ ndpi_ascii2prefix (int family, char *string) #endif /* NT */ return (ndpi_New_Prefix (AF_INET6, &sin6, bitlen)); } -#endif /* HAVE_IPV6 */ +#endif /* PATRICIA_IPV6 */ else return (NULL); } -- cgit v1.2.3 From 66394ba05ef09ad5ea6833088a5b52ec98c424bf Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Mon, 18 May 2015 15:26:16 +0200 Subject: File renamed to avoid name clashes --- src/lib/third_party/include/ndpi_patricia.h | 306 ++++++++ src/lib/third_party/include/patricia.h | 306 -------- src/lib/third_party/src/ndpi_patricia.c | 1077 +++++++++++++++++++++++++++ src/lib/third_party/src/patricia.c | 1077 --------------------------- 4 files changed, 1383 insertions(+), 1383 deletions(-) create mode 100644 src/lib/third_party/include/ndpi_patricia.h delete mode 100644 src/lib/third_party/include/patricia.h create mode 100644 src/lib/third_party/src/ndpi_patricia.c delete mode 100644 src/lib/third_party/src/patricia.c diff --git a/src/lib/third_party/include/ndpi_patricia.h b/src/lib/third_party/include/ndpi_patricia.h new file mode 100644 index 000000000..c369bd3a7 --- /dev/null +++ b/src/lib/third_party/include/ndpi_patricia.h @@ -0,0 +1,306 @@ +/* + * $Id: patricia.h,v 1.6 2005/12/07 20:53:01 dplonka Exp $ + * Dave Plonka + * + * This product includes software developed by the University of Michigan, + * Merit Network, Inc., and their contributors. + * + * This file had been called "radix.h" in the MRT sources. + * + * I renamed it to "patricia.h" since it's not an implementation of a general + * radix trie. Also, pulled in various requirements from "mrt.h" and added + * some other things it could be used as a standalone API. + + https://github.com/deepfield/MRT/blob/master/COPYRIGHT + + Copyright (c) 1999-2013 + + The Regents of the University of Michigan ("The Regents") and Merit + Network, Inc. + + Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef _PATRICIA_H +#define _PATRICIA_H + +#ifndef WIN32 +#define PATRICIA_IPV6 HAVE_IPV6 +#else +#define PATRICIA_IPV6 0 +#endif + +/* typedef unsigned int u_int; */ +/* { from defs.h */ +#define prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin) + +#ifdef __KERNEL__ +#define MAXLINE 512 +#else +#define MAXLINE 1024 +#endif + +#define BIT_TEST(f, b) ((f) & (b)) +/* } */ + +#define addroute make_and_lookup + +#ifndef __KERNEL__ +#include /* for u_* definitions (on FreeBSD 5) */ +#include /* for EAFNOSUPPORT */ + +#ifndef EAFNOSUPPORT +# defined EAFNOSUPPORT WSAEAFNOSUPPORT +#else +#ifndef WIN32 +# include /* for struct in_addr */ +#endif +#endif + +#ifndef WIN32 +#include /* for AF_INET */ +#else +#include +#include /* IPv6 */ +#endif + +#endif /* __KERNEL__ */ + +/* { from mrt.h */ + +typedef struct the_prefix4_t { + unsigned short family; /* AF_INET | AF_INET6 */ + unsigned short bitlen; /* same as mask? */ + int ref_count; /* reference count */ + struct in_addr sin; +} prefix4_t; + +typedef struct the_prefix_t { + unsigned short family; /* AF_INET | AF_INET6 */ + unsigned short bitlen; /* same as mask? */ + int ref_count; /* reference count */ + union { + struct in_addr sin; +#ifdef PATRICIA_IPV6 + struct in6_addr sin6; +#endif /* IPV6 */ + } add; +} prefix_t; + +/* } */ + +/* pointer to usr data (ex. route flap info) */ +union patricia_node_value_t { + void *user_data; + u_int32_t user_value; +}; + +typedef struct _patricia_node_t { + u_int bit; /* flag if this node used */ + prefix_t *prefix; /* who we are in patricia tree */ + struct _patricia_node_t *l, *r; /* left and right children */ + struct _patricia_node_t *parent;/* may be used */ + void *data; /* pointer to data */ + union patricia_node_value_t value; +} patricia_node_t; + +typedef struct _patricia_tree_t { + patricia_node_t *head; + u_int maxbits; /* for IP, 32 bit addresses */ + int num_active_node; /* for debug purpose */ +} patricia_tree_t; + +typedef void (*void_fn_t)(void *data); +typedef void (*void_fn2_t)(prefix_t *prefix, void *data); + +/* renamed to ndpi_Patricia to avoid name conflicts */ +patricia_node_t *ndpi_patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix); +patricia_node_t *ndpi_patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix); +patricia_node_t * ndpi_patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, + int inclusive); +patricia_node_t *ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix); +void ndpi_patricia_remove (patricia_tree_t *patricia, patricia_node_t *node); +patricia_tree_t *ndpi_New_Patricia (int maxbits); +void ndpi_Clear_Patricia (patricia_tree_t *patricia, void_fn_t func); +void ndpi_Destroy_Patricia (patricia_tree_t *patricia, void_fn_t func); +void ndpi_patricia_process (patricia_tree_t *patricia, void_fn2_t func); + +#define PATRICIA_MAXBITS (sizeof(struct in6_addr) * 8) +#define PATRICIA_NBIT(x) (0x80 >> ((x) & 0x7f)) +#define PATRICIA_NBYTE(x) ((x) >> 3) + +#define PATRICIA_DATA_GET(node, type) (type *)((node)->data) +#define PATRICIA_DATA_SET(node, value) ((node)->data = (void *)(value)) + +#define PATRICIA_WALK(Xhead, Xnode) \ + do { \ + patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; \ + patricia_node_t **Xsp = Xstack; \ + patricia_node_t *Xrn = (Xhead); \ + while ((Xnode = Xrn)) { \ + if (Xnode->prefix) + +#define PATRICIA_WALK_ALL(Xhead, Xnode) \ + do { \ + patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; \ + patricia_node_t **Xsp = Xstack; \ + patricia_node_t *Xrn = (Xhead); \ + while ((Xnode = Xrn)) { \ + if (1) + +#define PATRICIA_WALK_BREAK { \ + if (Xsp != Xstack) { \ + Xrn = *(--Xsp); \ + } else { \ + Xrn = (patricia_node_t *) 0; \ + } \ + continue; } + +#define PATRICIA_WALK_END \ + if (Xrn->l) { \ + if (Xrn->r) { \ + *Xsp++ = Xrn->r; \ + } \ + Xrn = Xrn->l; \ + } else if (Xrn->r) { \ + Xrn = Xrn->r; \ + } else if (Xsp != Xstack) { \ + Xrn = *(--Xsp); \ + } else { \ + Xrn = (patricia_node_t *) 0; \ + } \ + } \ + } while (0) + +#endif /* _PATRICIA_H */ + +/************************* + + + [newtool.gif] + +MRT Credits + + The Multi-Threaded Routing Toolkit + _________________________________________________________________ + + MRT was developed by [1]Merit Network, Inc., under National Science + Foundation grant NCR-9318902, "Experimentation with Routing Technology + to be Used for Inter-Domain Routing in the Internet." + + Current MRT Staff + + * [2]Craig Labovitz + * [3]Makaki Hirabaru + * [4]Farnam Jahanian + * Susan Hares + * Susan R. Harris + * Nathan Binkert + * Gerald Winters + + Project Alumni + + * [5]Marc Unangst + * John Scudder + + The BGP4+ extension was originally written by Francis Dupont + . + + The public domain Struct C-library of linked list, hash table and + memory allocation routines was developed by Jonathan Dekock + . + + Susan Rebecca Harris provided help with the + documentation. + David Ward provided bug fixes and helpful + suggestions. + Some sections of code and architecture ideas were taken from the GateD + routing daemon. + + The first port to Linux with IPv6 was done by Pedro Roque + . Some interface routines to the Linux kernel were + originally written by him. + + Alexey Kuznetsov made enhancements to 1.4.3a and fixed the Linux + kernel intarface. Linux's netlink interface was written, referring to + his code "iproute2". + + We would also like to thank our other colleagues in Japan, Portugal, + the Netherlands, the UK, and the US for their many contributions to + the MRT development effort. + _________________________________________________________________ + + Cisco is a registered trademark of Cisco Systems Inc. + _________________________________________________________________ + + Merit Network 4251 Plymouth Road Suite C Ann Arbor, MI 48105-2785 + 734-764-9430 + info@merit.edu + _________________________________________________________________ + + © 1999 Merit Network, Inc. + [6]www@merit.edu + +References + + 1. http://www.merit.edu/ + 2. http://www.merit.edu/~labovit + 3. http://www.merit.edu/~masaki + 4. http://www.eecs.umich.edu/~farnam + 5. http://www.contrib.andrew.cmu.edu/~mju/ + 6. mailto:www@merit.edu + +------------ + +Copyright (c) 1997, 1998, 1999 + + +The Regents of the University of Michigan ("The Regents") and Merit Network, +Inc. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above + copyright notice, this list of conditions and the + following disclaimer. +2. Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the + following disclaimer in the documentation and/or other + materials provided with the distribution. +3. All advertising materials mentioning features or use of + this software must display the following acknowledgement: +This product includes software developed by the University of Michigan, Merit +Network, Inc., and their contributors. +4. Neither the name of the University, Merit Network, nor the + names of their contributors may be used to endorse or + promote products derived from this software without + specific prior written permission. +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +************************ */ diff --git a/src/lib/third_party/include/patricia.h b/src/lib/third_party/include/patricia.h deleted file mode 100644 index c369bd3a7..000000000 --- a/src/lib/third_party/include/patricia.h +++ /dev/null @@ -1,306 +0,0 @@ -/* - * $Id: patricia.h,v 1.6 2005/12/07 20:53:01 dplonka Exp $ - * Dave Plonka - * - * This product includes software developed by the University of Michigan, - * Merit Network, Inc., and their contributors. - * - * This file had been called "radix.h" in the MRT sources. - * - * I renamed it to "patricia.h" since it's not an implementation of a general - * radix trie. Also, pulled in various requirements from "mrt.h" and added - * some other things it could be used as a standalone API. - - https://github.com/deepfield/MRT/blob/master/COPYRIGHT - - Copyright (c) 1999-2013 - - The Regents of the University of Michigan ("The Regents") and Merit - Network, Inc. - - Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef _PATRICIA_H -#define _PATRICIA_H - -#ifndef WIN32 -#define PATRICIA_IPV6 HAVE_IPV6 -#else -#define PATRICIA_IPV6 0 -#endif - -/* typedef unsigned int u_int; */ -/* { from defs.h */ -#define prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin) - -#ifdef __KERNEL__ -#define MAXLINE 512 -#else -#define MAXLINE 1024 -#endif - -#define BIT_TEST(f, b) ((f) & (b)) -/* } */ - -#define addroute make_and_lookup - -#ifndef __KERNEL__ -#include /* for u_* definitions (on FreeBSD 5) */ -#include /* for EAFNOSUPPORT */ - -#ifndef EAFNOSUPPORT -# defined EAFNOSUPPORT WSAEAFNOSUPPORT -#else -#ifndef WIN32 -# include /* for struct in_addr */ -#endif -#endif - -#ifndef WIN32 -#include /* for AF_INET */ -#else -#include -#include /* IPv6 */ -#endif - -#endif /* __KERNEL__ */ - -/* { from mrt.h */ - -typedef struct the_prefix4_t { - unsigned short family; /* AF_INET | AF_INET6 */ - unsigned short bitlen; /* same as mask? */ - int ref_count; /* reference count */ - struct in_addr sin; -} prefix4_t; - -typedef struct the_prefix_t { - unsigned short family; /* AF_INET | AF_INET6 */ - unsigned short bitlen; /* same as mask? */ - int ref_count; /* reference count */ - union { - struct in_addr sin; -#ifdef PATRICIA_IPV6 - struct in6_addr sin6; -#endif /* IPV6 */ - } add; -} prefix_t; - -/* } */ - -/* pointer to usr data (ex. route flap info) */ -union patricia_node_value_t { - void *user_data; - u_int32_t user_value; -}; - -typedef struct _patricia_node_t { - u_int bit; /* flag if this node used */ - prefix_t *prefix; /* who we are in patricia tree */ - struct _patricia_node_t *l, *r; /* left and right children */ - struct _patricia_node_t *parent;/* may be used */ - void *data; /* pointer to data */ - union patricia_node_value_t value; -} patricia_node_t; - -typedef struct _patricia_tree_t { - patricia_node_t *head; - u_int maxbits; /* for IP, 32 bit addresses */ - int num_active_node; /* for debug purpose */ -} patricia_tree_t; - -typedef void (*void_fn_t)(void *data); -typedef void (*void_fn2_t)(prefix_t *prefix, void *data); - -/* renamed to ndpi_Patricia to avoid name conflicts */ -patricia_node_t *ndpi_patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix); -patricia_node_t *ndpi_patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix); -patricia_node_t * ndpi_patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, - int inclusive); -patricia_node_t *ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix); -void ndpi_patricia_remove (patricia_tree_t *patricia, patricia_node_t *node); -patricia_tree_t *ndpi_New_Patricia (int maxbits); -void ndpi_Clear_Patricia (patricia_tree_t *patricia, void_fn_t func); -void ndpi_Destroy_Patricia (patricia_tree_t *patricia, void_fn_t func); -void ndpi_patricia_process (patricia_tree_t *patricia, void_fn2_t func); - -#define PATRICIA_MAXBITS (sizeof(struct in6_addr) * 8) -#define PATRICIA_NBIT(x) (0x80 >> ((x) & 0x7f)) -#define PATRICIA_NBYTE(x) ((x) >> 3) - -#define PATRICIA_DATA_GET(node, type) (type *)((node)->data) -#define PATRICIA_DATA_SET(node, value) ((node)->data = (void *)(value)) - -#define PATRICIA_WALK(Xhead, Xnode) \ - do { \ - patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; \ - patricia_node_t **Xsp = Xstack; \ - patricia_node_t *Xrn = (Xhead); \ - while ((Xnode = Xrn)) { \ - if (Xnode->prefix) - -#define PATRICIA_WALK_ALL(Xhead, Xnode) \ - do { \ - patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; \ - patricia_node_t **Xsp = Xstack; \ - patricia_node_t *Xrn = (Xhead); \ - while ((Xnode = Xrn)) { \ - if (1) - -#define PATRICIA_WALK_BREAK { \ - if (Xsp != Xstack) { \ - Xrn = *(--Xsp); \ - } else { \ - Xrn = (patricia_node_t *) 0; \ - } \ - continue; } - -#define PATRICIA_WALK_END \ - if (Xrn->l) { \ - if (Xrn->r) { \ - *Xsp++ = Xrn->r; \ - } \ - Xrn = Xrn->l; \ - } else if (Xrn->r) { \ - Xrn = Xrn->r; \ - } else if (Xsp != Xstack) { \ - Xrn = *(--Xsp); \ - } else { \ - Xrn = (patricia_node_t *) 0; \ - } \ - } \ - } while (0) - -#endif /* _PATRICIA_H */ - -/************************* - - - [newtool.gif] - -MRT Credits - - The Multi-Threaded Routing Toolkit - _________________________________________________________________ - - MRT was developed by [1]Merit Network, Inc., under National Science - Foundation grant NCR-9318902, "Experimentation with Routing Technology - to be Used for Inter-Domain Routing in the Internet." - - Current MRT Staff - - * [2]Craig Labovitz - * [3]Makaki Hirabaru - * [4]Farnam Jahanian - * Susan Hares - * Susan R. Harris - * Nathan Binkert - * Gerald Winters - - Project Alumni - - * [5]Marc Unangst - * John Scudder - - The BGP4+ extension was originally written by Francis Dupont - . - - The public domain Struct C-library of linked list, hash table and - memory allocation routines was developed by Jonathan Dekock - . - - Susan Rebecca Harris provided help with the - documentation. - David Ward provided bug fixes and helpful - suggestions. - Some sections of code and architecture ideas were taken from the GateD - routing daemon. - - The first port to Linux with IPv6 was done by Pedro Roque - . Some interface routines to the Linux kernel were - originally written by him. - - Alexey Kuznetsov made enhancements to 1.4.3a and fixed the Linux - kernel intarface. Linux's netlink interface was written, referring to - his code "iproute2". - - We would also like to thank our other colleagues in Japan, Portugal, - the Netherlands, the UK, and the US for their many contributions to - the MRT development effort. - _________________________________________________________________ - - Cisco is a registered trademark of Cisco Systems Inc. - _________________________________________________________________ - - Merit Network 4251 Plymouth Road Suite C Ann Arbor, MI 48105-2785 - 734-764-9430 - info@merit.edu - _________________________________________________________________ - - © 1999 Merit Network, Inc. - [6]www@merit.edu - -References - - 1. http://www.merit.edu/ - 2. http://www.merit.edu/~labovit - 3. http://www.merit.edu/~masaki - 4. http://www.eecs.umich.edu/~farnam - 5. http://www.contrib.andrew.cmu.edu/~mju/ - 6. mailto:www@merit.edu - ------------- - -Copyright (c) 1997, 1998, 1999 - - -The Regents of the University of Michigan ("The Regents") and Merit Network, -Inc. All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: -1. Redistributions of source code must retain the above - copyright notice, this list of conditions and the - following disclaimer. -2. Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the - following disclaimer in the documentation and/or other - materials provided with the distribution. -3. All advertising materials mentioning features or use of - this software must display the following acknowledgement: -This product includes software developed by the University of Michigan, Merit -Network, Inc., and their contributors. -4. Neither the name of the University, Merit Network, nor the - names of their contributors may be used to endorse or - promote products derived from this software without - specific prior written permission. -THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY -EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -************************ */ diff --git a/src/lib/third_party/src/ndpi_patricia.c b/src/lib/third_party/src/ndpi_patricia.c new file mode 100644 index 000000000..7a95a8765 --- /dev/null +++ b/src/lib/third_party/src/ndpi_patricia.c @@ -0,0 +1,1077 @@ +/* + * $Id: patricia.c,v 1.7 2005/12/07 20:46:41 dplonka Exp $ + * Dave Plonka + * + * This product includes software developed by the University of Michigan, + * Merit Network, Inc., and their contributors. + * + * This file had been called "radix.c" in the MRT sources. + * + * I renamed it to "patricia.c" since it's not an implementation of a general + * radix trie. Also I pulled in various requirements from "prefix.c" and + * "demo.c" so that it could be used as a standalone API. + + + https://github.com/deepfield/MRT/blob/master/COPYRIGHT + + Copyright (c) 1999-2013 + + The Regents of the University of Michigan ("The Regents") and Merit + Network, Inc. + + Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef __KERNEL__ +#include /* assert */ +#include /* isdigit */ +#include /* errno */ +#include /* sin */ +#include /* NULL */ +#include /* sprintf, fprintf, stderr */ +#include /* free, atol, calloc */ +#include /* memcpy, strchr, strlen */ +#include /* BSD: for inet_addr */ +#ifndef WIN32 +#include /* BSD, Linux: for inet_addr */ +#include /* BSD, Linux: for inet_addr */ +#include /* BSD, Linux, Solaris: for inet_addr */ +#endif +#else +#define assert(a) ; +#endif /* __KERNEL__ */ + +#include "patricia.h" + + +#ifdef __KERNEL__ + +long atol(const char *nptr) { + long l; + char *endp; + + l = simple_strtol(nptr, &endp, 10); + return(l); +} +#endif + +// #define PATRICIA_DEBUG + +void ndpi_DeleteEntry(void *a) { + ndpi_free(a); +} + +/* { from prefix.c */ + +/* ndpi_prefix_tochar + * convert prefix information to bytes + */ +u_char * +ndpi_prefix_tochar (prefix_t * prefix) +{ + if(prefix == NULL) + return (NULL); + + return ((u_char *) & prefix->add.sin); +} + +int ndpi_comp_with_mask (void *addr, void *dest, u_int mask) { + if( /* mask/8 == 0 || */ memcmp (addr, dest, mask / 8) == 0) { + int n = mask / 8; + int m = ((-1) << (8 - (mask % 8))); + + if(mask % 8 == 0 || (((u_char *)addr)[n] & m) == (((u_char *)dest)[n] & m)) + return (1); + } + return (0); +} + +#if 0 /* this implementation does not support IPv6, using system inet_pton */ +#ifndef WIN32 +/* inet_pton substitute implementation + * Uses inet_addr to convert an IP address in dotted decimal notation into + * unsigned long and copies the result to dst. + * Only supports AF_INET. Follows standard error return conventions of + * inet_pton. + */ +int +inet_pton (int af, const char *src, void *dst) +{ + u_long result; + + if(af == AF_INET) { + result = inet_addr(src); + if(result == -1) + return 0; + else { + memcpy (dst, &result, sizeof(struct in_addr)); + return 1; + } + } +#ifdef NT +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) + else if(af == AF_INET6) { + struct in6_addr Address; + return (inet6_addr(src, &Address)); + } +#endif /* PATRICIA_IPV6 */ +#endif /* NT */ +#ifndef NT + else { + printf("NOT SUPP\n"); + errno = EAFNOSUPPORT; + return -1; + } +#endif /* NT */ +} +#endif +#endif + +/* this allows imcomplete prefix */ +int +ndpi_my_inet_pton (int af, const char *src, void *dst) +{ + if(af == AF_INET) { + int i; + u_char xp[sizeof(struct in_addr)] = {0, 0, 0, 0}; + + for (i = 0; ; i++) { + int c, val; + + c = *src++; + if(!isdigit (c)) + return (-1); + val = 0; + do { + val = val * 10 + c - '0'; + if(val > 255) + return (0); + c = *src++; + } while (c && isdigit (c)); + xp[i] = val; + if(c == '\0') + break; + if(c != '.') + return (0); + if(i >= 3) + return (0); + } + memcpy (dst, xp, sizeof(struct in_addr)); + return (1); +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) + } else if(af == AF_INET6) { + return (inet_pton (af, src, dst)); +#endif /* PATRICIA_IPV6 */ + } else { +#ifndef NT +#ifndef __KERNEL__ + errno = EAFNOSUPPORT; +#endif +#endif /* NT */ + return -1; + } +} + +#define PATRICIA_MAX_THREADS 16 + +/* + * convert prefix information to ascii string with length + * thread safe and (almost) re-entrant implementation + */ +char * +ndpi_ndpi_prefix_toa2x (prefix_t *prefix, char *buff, int with_len) +{ + if(prefix == NULL) + return ((char*)"(Null)"); + assert (prefix->ref_count >= 0); + if(buff == NULL) { + + struct buffer { + char buffs[PATRICIA_MAX_THREADS][48+5]; + u_int i; + } *buffp; + +# if 0 + THREAD_SPECIFIC_DATA (struct buffer, buffp, 1); +# else + { /* for scope only */ + static struct buffer local_buff; + buffp = &local_buff; + } +# endif + if(buffp == NULL) { + /* XXX should we report an error? */ + return (NULL); + } + + buff = buffp->buffs[buffp->i++%PATRICIA_MAX_THREADS]; + } + if(prefix->family == AF_INET) { + u_char *a; + assert (prefix->bitlen <= sizeof(struct in_addr) * 8); + a = prefix_touchar (prefix); + if(with_len) { + sprintf (buff, "%d.%d.%d.%d/%d", a[0], a[1], a[2], a[3], + prefix->bitlen); + } + else { + sprintf (buff, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); + } + return (buff); + } +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) + else if(prefix->family == AF_INET6) { + char *r; + r = (char *) inet_ntop (AF_INET6, &prefix->add.sin6, buff, 48 /* a guess value */ ); + if(r && with_len) { + assert (prefix->bitlen <= sizeof(struct in6_addr) * 8); + sprintf (buff + strlen (buff), "/%d", prefix->bitlen); + } + return (buff); + } +#endif /* PATRICIA_IPV6 */ + else + return (NULL); +} + +/* ndpi_prefix_toa2 + * convert prefix information to ascii string + */ +char * +ndpi_prefix_toa2 (prefix_t *prefix, char *buff) +{ + return (ndpi_ndpi_prefix_toa2x (prefix, buff, 0)); +} + +/* ndpi_prefix_toa + */ +char * +ndpi_prefix_toa (prefix_t * prefix) +{ + return (ndpi_prefix_toa2 (prefix, (char *) NULL)); +} + +prefix_t * +ndpi_New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix) +{ + int dynamic_allocated = 0; + int default_bitlen = sizeof(struct in_addr) * 8; + +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) + if(family == AF_INET6) { + default_bitlen = sizeof(struct in6_addr) * 8; + if(prefix == NULL) { + prefix = (prefix_t*)ndpi_calloc(1, sizeof (prefix_t)); + dynamic_allocated++; + } + memcpy (&prefix->add.sin6, dest, sizeof(struct in6_addr)); + } + else +#endif /* PATRICIA_IPV6 */ + if(family == AF_INET) { + if(prefix == NULL) { +#ifndef NT + prefix = (prefix_t*)ndpi_calloc(1, sizeof (prefix4_t)); +#else + //for some reason, compiler is getting + //prefix4_t size incorrect on NT + prefix = ndpi_calloc(1, sizeof (prefix_t)); +#endif /* NT */ + + dynamic_allocated++; + } + memcpy (&prefix->add.sin, dest, sizeof(struct in_addr)); + } + else { + return (NULL); + } + + prefix->bitlen = (bitlen >= 0)? bitlen: default_bitlen; + prefix->family = family; + prefix->ref_count = 0; + if(dynamic_allocated) { + prefix->ref_count++; + } + /* fprintf(stderr, "[C %s, %d]\n", ndpi_prefix_toa (prefix), prefix->ref_count); */ + return (prefix); +} + +prefix_t * +ndpi_New_Prefix (int family, void *dest, int bitlen) +{ + return (ndpi_New_Prefix2 (family, dest, bitlen, NULL)); +} + +/* ndpi_ascii2prefix + */ +prefix_t * +ndpi_ascii2prefix (int family, char *string) +{ + long bitlen; + long maxbitlen = 0; + char *cp; + struct in_addr sin; +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) + struct in6_addr sin6; +#endif /* PATRICIA_IPV6 */ + char save[MAXLINE]; + + if(string == NULL) + return (NULL); + + /* easy way to handle both families */ + if(family == 0) { + family = AF_INET; +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) + if(strchr (string, ':')) family = AF_INET6; +#endif /* PATRICIA_IPV6 */ + } + + if(family == AF_INET) { + maxbitlen = sizeof(struct in_addr) * 8; + } +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) + else if(family == AF_INET6) { + maxbitlen = sizeof(struct in6_addr) * 8; + } +#endif /* PATRICIA_IPV6 */ + + if((cp = strchr (string, '/')) != NULL) { + bitlen = atol (cp + 1); + /* *cp = '\0'; */ + /* copy the string to save. Avoid destroying the string */ + assert (cp - string < MAXLINE); + memcpy (save, string, cp - string); + save[cp - string] = '\0'; + string = save; + if((bitlen < 0) || (bitlen > maxbitlen)) + bitlen = maxbitlen; + } else { + bitlen = maxbitlen; + } + + if(family == AF_INET) { + if(ndpi_my_inet_pton (AF_INET, string, &sin) <= 0) + return (NULL); + return (ndpi_New_Prefix (AF_INET, &sin, bitlen)); + } + +#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) + else if(family == AF_INET6) { + // Get rid of this with next IPv6 upgrade +#if defined(NT) && !defined(HAVE_INET_NTOP) + inet6_addr(string, &sin6); + return (ndpi_New_Prefix (AF_INET6, &sin6, bitlen)); +#else + if(inet_pton (AF_INET6, string, &sin6) <= 0) + return (NULL); +#endif /* NT */ + return (ndpi_New_Prefix (AF_INET6, &sin6, bitlen)); + } +#endif /* PATRICIA_IPV6 */ + else + return (NULL); +} + +prefix_t * +ndpi_Ref_Prefix (prefix_t * prefix) +{ + if(prefix == NULL) + return (NULL); + if(prefix->ref_count == 0) { + /* make a copy in case of a static prefix */ + return (ndpi_New_Prefix2 (prefix->family, &prefix->add, prefix->bitlen, NULL)); + } + prefix->ref_count++; + /* fprintf(stderr, "[A %s, %d]\n", ndpi_prefix_toa (prefix), prefix->ref_count); */ + return (prefix); +} + +void +ndpi_Deref_Prefix (prefix_t * prefix) +{ + if(prefix == NULL) + return; + /* for secure programming, raise an assert. no static prefix can call this */ + assert (prefix->ref_count > 0); + + prefix->ref_count--; + assert (prefix->ref_count >= 0); + if(prefix->ref_count <= 0) { + ndpi_DeleteEntry (prefix); + return; + } +} + +/* } */ + +/* #define PATRICIA_DEBUG 1 */ + +static int num_active_patricia = 0; + +/* these routines support continuous mask only */ + +patricia_tree_t * +ndpi_New_Patricia (int maxbits) +{ + patricia_tree_t *patricia = (patricia_tree_t*)ndpi_calloc(1, sizeof *patricia); + + patricia->maxbits = maxbits; + patricia->head = NULL; + patricia->num_active_node = 0; + assert((u_int)maxbits <= PATRICIA_MAXBITS); /* XXX */ + num_active_patricia++; + return (patricia); +} + + +/* + * if func is supplied, it will be called as func(node->data) + * before deleting the node + */ + +void +ndpi_Clear_Patricia (patricia_tree_t *patricia, void_fn_t func) +{ + assert (patricia); + if(patricia->head) { + + patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; + patricia_node_t **Xsp = Xstack; + patricia_node_t *Xrn = patricia->head; + + while (Xrn) { + patricia_node_t *l = Xrn->l; + patricia_node_t *r = Xrn->r; + + if(Xrn->prefix) { + ndpi_Deref_Prefix (Xrn->prefix); + if(Xrn->data && func) + func (Xrn->data); + } + else { + assert (Xrn->data == NULL); + } + ndpi_DeleteEntry (Xrn); + patricia->num_active_node--; + + if(l) { + if(r) { + *Xsp++ = r; + } + Xrn = l; + } else if(r) { + Xrn = r; + } else if(Xsp != Xstack) { + Xrn = *(--Xsp); + } else { + Xrn = NULL; + } + } + } + assert (patricia->num_active_node == 0); + /* ndpi_DeleteEntry (patricia); */ +} + + +void +ndpi_Destroy_Patricia (patricia_tree_t *patricia, void_fn_t func) +{ + ndpi_Clear_Patricia (patricia, func); + ndpi_DeleteEntry (patricia); + num_active_patricia--; +} + + +/* + * if func is supplied, it will be called as func(node->prefix, node->data) + */ + +void +ndpi_patricia_process (patricia_tree_t *patricia, void_fn2_t func) +{ + patricia_node_t *node; + assert (func); + + PATRICIA_WALK (patricia->head, node) { + func (node->prefix, node->data); + } PATRICIA_WALK_END; +} + +size_t +ndpi_patricia_walk_inorder(patricia_node_t *node, void_fn2_t func) +{ + size_t n = 0; + assert(func); + + if(node->l) { + n += ndpi_patricia_walk_inorder(node->l, func); + } + + if(node->prefix) { + func(node->prefix, node->data); + n++; + } + + if(node->r) { + n += ndpi_patricia_walk_inorder(node->r, func); + } + + return n; +} + + +patricia_node_t * +ndpi_patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix) +{ + patricia_node_t *node; + u_char *addr; + u_int bitlen; + + assert (patricia); + assert (prefix); + assert (prefix->bitlen <= patricia->maxbits); + + if(patricia->head == NULL) + return (NULL); + + node = patricia->head; + addr = prefix_touchar (prefix); + bitlen = prefix->bitlen; + + while (node->bit < bitlen) { + + if(BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { +#ifdef PATRICIA_DEBUG + if(node->prefix) + fprintf (stderr, "patricia_search_exact: take right %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); + else + fprintf (stderr, "patricia_search_exact: take right at %u\n", + node->bit); +#endif /* PATRICIA_DEBUG */ + node = node->r; + } + else { +#ifdef PATRICIA_DEBUG + if(node->prefix) + fprintf (stderr, "patricia_search_exact: take left %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); + else + fprintf (stderr, "patricia_search_exact: take left at %u\n", + node->bit); +#endif /* PATRICIA_DEBUG */ + node = node->l; + } + + if(node == NULL) + return (NULL); + } + +#ifdef PATRICIA_DEBUG + if(node->prefix) + fprintf (stderr, "patricia_search_exact: stop at %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); + else + fprintf (stderr, "patricia_search_exact: stop at %u\n", node->bit); +#endif /* PATRICIA_DEBUG */ + if(node->bit > bitlen || node->prefix == NULL) + return (NULL); + assert (node->bit == bitlen); + assert (node->bit == node->prefix->bitlen); + if(ndpi_comp_with_mask (ndpi_prefix_tochar (node->prefix), ndpi_prefix_tochar (prefix), + bitlen)) { +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_search_exact: found %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + return (node); + } + return (NULL); +} + + +/* if inclusive != 0, "best" may be the given prefix itself */ +patricia_node_t * +ndpi_patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inclusive) +{ + patricia_node_t *node; + patricia_node_t *stack[PATRICIA_MAXBITS + 1]; + u_char *addr; + u_int bitlen; + int cnt = 0; + + assert (patricia); + assert (prefix); + assert (prefix->bitlen <= patricia->maxbits); + + if(patricia->head == NULL) + return (NULL); + + node = patricia->head; + addr = prefix_touchar (prefix); + bitlen = prefix->bitlen; + + while (node->bit < bitlen) { + + if(node->prefix) { +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_search_best: push %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + stack[cnt++] = node; + } + + if(BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { +#ifdef PATRICIA_DEBUG + if(node->prefix) + fprintf (stderr, "patricia_search_best: take right %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); + else + fprintf (stderr, "patricia_search_best: take right at %u\n", + node->bit); +#endif /* PATRICIA_DEBUG */ + node = node->r; + } + else { +#ifdef PATRICIA_DEBUG + if(node->prefix) + fprintf (stderr, "patricia_search_best: take left %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); + else + fprintf (stderr, "patricia_search_best: take left at %u\n", + node->bit); +#endif /* PATRICIA_DEBUG */ + node = node->l; + } + + if(node == NULL) + break; + } + + if(inclusive && node && node->prefix) + stack[cnt++] = node; + +#ifdef PATRICIA_DEBUG + if(node == NULL) + fprintf (stderr, "patricia_search_best: stop at null\n"); + else if(node->prefix) + fprintf (stderr, "patricia_search_best: stop at %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); + else + fprintf (stderr, "patricia_search_best: stop at %u\n", node->bit); +#endif /* PATRICIA_DEBUG */ + + if(cnt <= 0) + return (NULL); + + while (--cnt >= 0) { + node = stack[cnt]; +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_search_best: pop %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + if(ndpi_comp_with_mask (ndpi_prefix_tochar (node->prefix), + ndpi_prefix_tochar (prefix), + node->prefix->bitlen) && node->prefix->bitlen <= bitlen) { +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_search_best: found %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + return (node); + } + } + return (NULL); +} + + +patricia_node_t * +ndpi_patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix) +{ + return (ndpi_patricia_search_best2 (patricia, prefix, 1)); +} + + +patricia_node_t * +ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix) +{ + patricia_node_t *node, *new_node, *parent, *glue; + u_char *addr, *test_addr; + u_int bitlen, check_bit, differ_bit; + int i, j; + + assert (patricia); + assert (prefix); + assert (prefix->bitlen <= patricia->maxbits); + + if(patricia->head == NULL) { + node = (patricia_node_t*)ndpi_calloc(1, sizeof *node); + node->bit = prefix->bitlen; + node->prefix = ndpi_Ref_Prefix (prefix); + node->parent = NULL; + node->l = node->r = NULL; + node->data = NULL; + patricia->head = node; +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_lookup: new_node #0 %s/%d (head)\n", + ndpi_prefix_toa (prefix), prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + patricia->num_active_node++; + return (node); + } + + addr = prefix_touchar (prefix); + bitlen = prefix->bitlen; + node = patricia->head; + + while (node->bit < bitlen || node->prefix == NULL) { + + if(node->bit < patricia->maxbits && + BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { + if(node->r == NULL) + break; +#ifdef PATRICIA_DEBUG + if(node->prefix) + fprintf (stderr, "patricia_lookup: take right %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); + else + fprintf (stderr, "patricia_lookup: take right at %u\n", node->bit); +#endif /* PATRICIA_DEBUG */ + node = node->r; + } + else { + if(node->l == NULL) + break; +#ifdef PATRICIA_DEBUG + if(node->prefix) + fprintf (stderr, "patricia_lookup: take left %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); + else + fprintf (stderr, "patricia_lookup: take left at %u\n", node->bit); +#endif /* PATRICIA_DEBUG */ + node = node->l; + } + + assert (node); + } + + assert (node->prefix); +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_lookup: stop at %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + + test_addr = prefix_touchar (node->prefix); + /* find the first bit different */ + check_bit = (node->bit < bitlen)? node->bit: bitlen; + differ_bit = 0; + for (i = 0; (u_int)i*8 < check_bit; i++) { + int r; + + if((r = (addr[i] ^ test_addr[i])) == 0) { + differ_bit = (i + 1) * 8; + continue; + } + /* I know the better way, but for now */ + for (j = 0; j < 8; j++) { + if(BIT_TEST (r, (0x80 >> j))) + break; + } + /* must be found */ + assert (j < 8); + differ_bit = i * 8 + j; + break; + } + if(differ_bit > check_bit) + differ_bit = check_bit; +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_lookup: differ_bit %d\n", differ_bit); +#endif /* PATRICIA_DEBUG */ + + parent = node->parent; + while (parent && parent->bit >= differ_bit) { + node = parent; + parent = node->parent; +#ifdef PATRICIA_DEBUG + if(node->prefix) + fprintf (stderr, "patricia_lookup: up to %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); + else + fprintf (stderr, "patricia_lookup: up to %u\n", node->bit); +#endif /* PATRICIA_DEBUG */ + } + + if(differ_bit == bitlen && node->bit == bitlen) { + if(node->prefix) { +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_lookup: found %s/%d\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + return (node); + } + node->prefix = ndpi_Ref_Prefix (prefix); +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_lookup: new node #1 %s/%d (glue mod)\n", + ndpi_prefix_toa (prefix), prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + assert (node->data == NULL); + return (node); + } + + new_node = (patricia_node_t*)ndpi_calloc(1, sizeof *new_node); + new_node->bit = prefix->bitlen; + new_node->prefix = ndpi_Ref_Prefix (prefix); + new_node->parent = NULL; + new_node->l = new_node->r = NULL; + new_node->data = NULL; + patricia->num_active_node++; + + if(node->bit == differ_bit) { + new_node->parent = node; + if(node->bit < patricia->maxbits && + BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { + assert (node->r == NULL); + node->r = new_node; + } + else { + assert (node->l == NULL); + node->l = new_node; + } +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_lookup: new_node #2 %s/%d (child)\n", + ndpi_prefix_toa (prefix), prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + return (new_node); + } + + if(bitlen == differ_bit) { + if(bitlen < patricia->maxbits && + BIT_TEST (test_addr[bitlen >> 3], 0x80 >> (bitlen & 0x07))) { + new_node->r = node; + } + else { + new_node->l = node; + } + new_node->parent = node->parent; + if(node->parent == NULL) { + assert (patricia->head == node); + patricia->head = new_node; + } + else if(node->parent->r == node) { + node->parent->r = new_node; + } + else { + node->parent->l = new_node; + } + node->parent = new_node; +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_lookup: new_node #3 %s/%d (parent)\n", + ndpi_prefix_toa (prefix), prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + } + else { + glue = (patricia_node_t*)ndpi_calloc(1, sizeof *glue); + glue->bit = differ_bit; + glue->prefix = NULL; + glue->parent = node->parent; + glue->data = NULL; + patricia->num_active_node++; + if(differ_bit < patricia->maxbits && + BIT_TEST (addr[differ_bit >> 3], 0x80 >> (differ_bit & 0x07))) { + glue->r = new_node; + glue->l = node; + } + else { + glue->r = node; + glue->l = new_node; + } + new_node->parent = glue; + + if(node->parent == NULL) { + assert (patricia->head == node); + patricia->head = glue; + } + else if(node->parent->r == node) { + node->parent->r = glue; + } + else { + node->parent->l = glue; + } + node->parent = glue; +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_lookup: new_node #4 %s/%d (glue+node)\n", + ndpi_prefix_toa (prefix), prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + } + return (new_node); +} + + +void +ndpi_patricia_remove (patricia_tree_t *patricia, patricia_node_t *node) +{ + patricia_node_t *parent, *child; + + assert (patricia); + assert (node); + + if(node->r && node->l) { +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_remove: #0 %s/%d (r & l)\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + + /* this might be a placeholder node -- have to check and make sure + * there is a prefix aossciated with it ! */ + if(node->prefix != NULL) + ndpi_Deref_Prefix (node->prefix); + node->prefix = NULL; + /* Also I needed to clear data pointer -- masaki */ + node->data = NULL; + return; + } + + if(node->r == NULL && node->l == NULL) { +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_remove: #1 %s/%d (!r & !l)\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + parent = node->parent; + ndpi_Deref_Prefix (node->prefix); + ndpi_DeleteEntry (node); + patricia->num_active_node--; + + if(parent == NULL) { + assert (patricia->head == node); + patricia->head = NULL; + return; + } + + if(parent->r == node) { + parent->r = NULL; + child = parent->l; + } + else { + assert (parent->l == node); + parent->l = NULL; + child = parent->r; + } + + if(parent->prefix) + return; + + /* we need to remove parent too */ + + if(parent->parent == NULL) { + assert (patricia->head == parent); + patricia->head = child; + } + else if(parent->parent->r == parent) { + parent->parent->r = child; + } + else { + assert (parent->parent->l == parent); + parent->parent->l = child; + } + child->parent = parent->parent; + ndpi_DeleteEntry (parent); + patricia->num_active_node--; + return; + } + +#ifdef PATRICIA_DEBUG + fprintf (stderr, "patricia_remove: #2 %s/%d (r ^ l)\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); +#endif /* PATRICIA_DEBUG */ + if(node->r) { + child = node->r; + } + else { + assert (node->l); + child = node->l; + } + parent = node->parent; + child->parent = parent; + + ndpi_Deref_Prefix (node->prefix); + ndpi_DeleteEntry (node); + patricia->num_active_node--; + + if(parent == NULL) { + assert (patricia->head == node); + patricia->head = child; + return; + } + + if(parent->r == node) { + parent->r = child; + } + else { + assert (parent->l == node); + parent->l = child; + } +} + +/* { from demo.c */ +#if 0 + +patricia_node_t * +ndpi_make_and_lookup (patricia_tree_t *tree, char *string) +{ + prefix_t *prefix; + patricia_node_t *node; + + prefix = ndpi_ascii2prefix (AF_INET, string); + printf ("make_and_lookup: %s/%d\n", ndpi_prefix_toa (prefix), prefix->bitlen); + node = ndpi_patricia_lookup (tree, prefix); + ndpi_Deref_Prefix (prefix); + return (node); +} + +patricia_node_t * +ndpi_try_search_exact (patricia_tree_t *tree, char *string) +{ + prefix_t *prefix; + patricia_node_t *node; + + prefix = ndpi_ascii2prefix (AF_INET, string); + printf ("try_search_exact: %s/%d\n", ndpi_prefix_toa (prefix), prefix->bitlen); + if((node = patricia_search_exact (tree, prefix)) == NULL) { + printf ("try_search_exact: not found\n"); + } + else { + printf ("try_search_exact: %s/%d found\n", + ndpi_prefix_toa (node->prefix), node->prefix->bitlen); + } + ndpi_Deref_Prefix (prefix); + return (node); +} + +void +ndpi_lookup_then_remove (patricia_tree_t *tree, char *string) +{ + patricia_node_t *node; + + if((node = try_search_exact (tree, string))) + patricia_remove (tree, node); +} +#endif + +/* } */ diff --git a/src/lib/third_party/src/patricia.c b/src/lib/third_party/src/patricia.c deleted file mode 100644 index 7a95a8765..000000000 --- a/src/lib/third_party/src/patricia.c +++ /dev/null @@ -1,1077 +0,0 @@ -/* - * $Id: patricia.c,v 1.7 2005/12/07 20:46:41 dplonka Exp $ - * Dave Plonka - * - * This product includes software developed by the University of Michigan, - * Merit Network, Inc., and their contributors. - * - * This file had been called "radix.c" in the MRT sources. - * - * I renamed it to "patricia.c" since it's not an implementation of a general - * radix trie. Also I pulled in various requirements from "prefix.c" and - * "demo.c" so that it could be used as a standalone API. - - - https://github.com/deepfield/MRT/blob/master/COPYRIGHT - - Copyright (c) 1999-2013 - - The Regents of the University of Michigan ("The Regents") and Merit - Network, Inc. - - Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef __KERNEL__ -#include /* assert */ -#include /* isdigit */ -#include /* errno */ -#include /* sin */ -#include /* NULL */ -#include /* sprintf, fprintf, stderr */ -#include /* free, atol, calloc */ -#include /* memcpy, strchr, strlen */ -#include /* BSD: for inet_addr */ -#ifndef WIN32 -#include /* BSD, Linux: for inet_addr */ -#include /* BSD, Linux: for inet_addr */ -#include /* BSD, Linux, Solaris: for inet_addr */ -#endif -#else -#define assert(a) ; -#endif /* __KERNEL__ */ - -#include "patricia.h" - - -#ifdef __KERNEL__ - -long atol(const char *nptr) { - long l; - char *endp; - - l = simple_strtol(nptr, &endp, 10); - return(l); -} -#endif - -// #define PATRICIA_DEBUG - -void ndpi_DeleteEntry(void *a) { - ndpi_free(a); -} - -/* { from prefix.c */ - -/* ndpi_prefix_tochar - * convert prefix information to bytes - */ -u_char * -ndpi_prefix_tochar (prefix_t * prefix) -{ - if(prefix == NULL) - return (NULL); - - return ((u_char *) & prefix->add.sin); -} - -int ndpi_comp_with_mask (void *addr, void *dest, u_int mask) { - if( /* mask/8 == 0 || */ memcmp (addr, dest, mask / 8) == 0) { - int n = mask / 8; - int m = ((-1) << (8 - (mask % 8))); - - if(mask % 8 == 0 || (((u_char *)addr)[n] & m) == (((u_char *)dest)[n] & m)) - return (1); - } - return (0); -} - -#if 0 /* this implementation does not support IPv6, using system inet_pton */ -#ifndef WIN32 -/* inet_pton substitute implementation - * Uses inet_addr to convert an IP address in dotted decimal notation into - * unsigned long and copies the result to dst. - * Only supports AF_INET. Follows standard error return conventions of - * inet_pton. - */ -int -inet_pton (int af, const char *src, void *dst) -{ - u_long result; - - if(af == AF_INET) { - result = inet_addr(src); - if(result == -1) - return 0; - else { - memcpy (dst, &result, sizeof(struct in_addr)); - return 1; - } - } -#ifdef NT -#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) - else if(af == AF_INET6) { - struct in6_addr Address; - return (inet6_addr(src, &Address)); - } -#endif /* PATRICIA_IPV6 */ -#endif /* NT */ -#ifndef NT - else { - printf("NOT SUPP\n"); - errno = EAFNOSUPPORT; - return -1; - } -#endif /* NT */ -} -#endif -#endif - -/* this allows imcomplete prefix */ -int -ndpi_my_inet_pton (int af, const char *src, void *dst) -{ - if(af == AF_INET) { - int i; - u_char xp[sizeof(struct in_addr)] = {0, 0, 0, 0}; - - for (i = 0; ; i++) { - int c, val; - - c = *src++; - if(!isdigit (c)) - return (-1); - val = 0; - do { - val = val * 10 + c - '0'; - if(val > 255) - return (0); - c = *src++; - } while (c && isdigit (c)); - xp[i] = val; - if(c == '\0') - break; - if(c != '.') - return (0); - if(i >= 3) - return (0); - } - memcpy (dst, xp, sizeof(struct in_addr)); - return (1); -#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) - } else if(af == AF_INET6) { - return (inet_pton (af, src, dst)); -#endif /* PATRICIA_IPV6 */ - } else { -#ifndef NT -#ifndef __KERNEL__ - errno = EAFNOSUPPORT; -#endif -#endif /* NT */ - return -1; - } -} - -#define PATRICIA_MAX_THREADS 16 - -/* - * convert prefix information to ascii string with length - * thread safe and (almost) re-entrant implementation - */ -char * -ndpi_ndpi_prefix_toa2x (prefix_t *prefix, char *buff, int with_len) -{ - if(prefix == NULL) - return ((char*)"(Null)"); - assert (prefix->ref_count >= 0); - if(buff == NULL) { - - struct buffer { - char buffs[PATRICIA_MAX_THREADS][48+5]; - u_int i; - } *buffp; - -# if 0 - THREAD_SPECIFIC_DATA (struct buffer, buffp, 1); -# else - { /* for scope only */ - static struct buffer local_buff; - buffp = &local_buff; - } -# endif - if(buffp == NULL) { - /* XXX should we report an error? */ - return (NULL); - } - - buff = buffp->buffs[buffp->i++%PATRICIA_MAX_THREADS]; - } - if(prefix->family == AF_INET) { - u_char *a; - assert (prefix->bitlen <= sizeof(struct in_addr) * 8); - a = prefix_touchar (prefix); - if(with_len) { - sprintf (buff, "%d.%d.%d.%d/%d", a[0], a[1], a[2], a[3], - prefix->bitlen); - } - else { - sprintf (buff, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); - } - return (buff); - } -#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) - else if(prefix->family == AF_INET6) { - char *r; - r = (char *) inet_ntop (AF_INET6, &prefix->add.sin6, buff, 48 /* a guess value */ ); - if(r && with_len) { - assert (prefix->bitlen <= sizeof(struct in6_addr) * 8); - sprintf (buff + strlen (buff), "/%d", prefix->bitlen); - } - return (buff); - } -#endif /* PATRICIA_IPV6 */ - else - return (NULL); -} - -/* ndpi_prefix_toa2 - * convert prefix information to ascii string - */ -char * -ndpi_prefix_toa2 (prefix_t *prefix, char *buff) -{ - return (ndpi_ndpi_prefix_toa2x (prefix, buff, 0)); -} - -/* ndpi_prefix_toa - */ -char * -ndpi_prefix_toa (prefix_t * prefix) -{ - return (ndpi_prefix_toa2 (prefix, (char *) NULL)); -} - -prefix_t * -ndpi_New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix) -{ - int dynamic_allocated = 0; - int default_bitlen = sizeof(struct in_addr) * 8; - -#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) - if(family == AF_INET6) { - default_bitlen = sizeof(struct in6_addr) * 8; - if(prefix == NULL) { - prefix = (prefix_t*)ndpi_calloc(1, sizeof (prefix_t)); - dynamic_allocated++; - } - memcpy (&prefix->add.sin6, dest, sizeof(struct in6_addr)); - } - else -#endif /* PATRICIA_IPV6 */ - if(family == AF_INET) { - if(prefix == NULL) { -#ifndef NT - prefix = (prefix_t*)ndpi_calloc(1, sizeof (prefix4_t)); -#else - //for some reason, compiler is getting - //prefix4_t size incorrect on NT - prefix = ndpi_calloc(1, sizeof (prefix_t)); -#endif /* NT */ - - dynamic_allocated++; - } - memcpy (&prefix->add.sin, dest, sizeof(struct in_addr)); - } - else { - return (NULL); - } - - prefix->bitlen = (bitlen >= 0)? bitlen: default_bitlen; - prefix->family = family; - prefix->ref_count = 0; - if(dynamic_allocated) { - prefix->ref_count++; - } - /* fprintf(stderr, "[C %s, %d]\n", ndpi_prefix_toa (prefix), prefix->ref_count); */ - return (prefix); -} - -prefix_t * -ndpi_New_Prefix (int family, void *dest, int bitlen) -{ - return (ndpi_New_Prefix2 (family, dest, bitlen, NULL)); -} - -/* ndpi_ascii2prefix - */ -prefix_t * -ndpi_ascii2prefix (int family, char *string) -{ - long bitlen; - long maxbitlen = 0; - char *cp; - struct in_addr sin; -#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) - struct in6_addr sin6; -#endif /* PATRICIA_IPV6 */ - char save[MAXLINE]; - - if(string == NULL) - return (NULL); - - /* easy way to handle both families */ - if(family == 0) { - family = AF_INET; -#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) - if(strchr (string, ':')) family = AF_INET6; -#endif /* PATRICIA_IPV6 */ - } - - if(family == AF_INET) { - maxbitlen = sizeof(struct in_addr) * 8; - } -#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) - else if(family == AF_INET6) { - maxbitlen = sizeof(struct in6_addr) * 8; - } -#endif /* PATRICIA_IPV6 */ - - if((cp = strchr (string, '/')) != NULL) { - bitlen = atol (cp + 1); - /* *cp = '\0'; */ - /* copy the string to save. Avoid destroying the string */ - assert (cp - string < MAXLINE); - memcpy (save, string, cp - string); - save[cp - string] = '\0'; - string = save; - if((bitlen < 0) || (bitlen > maxbitlen)) - bitlen = maxbitlen; - } else { - bitlen = maxbitlen; - } - - if(family == AF_INET) { - if(ndpi_my_inet_pton (AF_INET, string, &sin) <= 0) - return (NULL); - return (ndpi_New_Prefix (AF_INET, &sin, bitlen)); - } - -#if defined(PATRICIA_IPV6) && (!defined(__KERNEL__)) - else if(family == AF_INET6) { - // Get rid of this with next IPv6 upgrade -#if defined(NT) && !defined(HAVE_INET_NTOP) - inet6_addr(string, &sin6); - return (ndpi_New_Prefix (AF_INET6, &sin6, bitlen)); -#else - if(inet_pton (AF_INET6, string, &sin6) <= 0) - return (NULL); -#endif /* NT */ - return (ndpi_New_Prefix (AF_INET6, &sin6, bitlen)); - } -#endif /* PATRICIA_IPV6 */ - else - return (NULL); -} - -prefix_t * -ndpi_Ref_Prefix (prefix_t * prefix) -{ - if(prefix == NULL) - return (NULL); - if(prefix->ref_count == 0) { - /* make a copy in case of a static prefix */ - return (ndpi_New_Prefix2 (prefix->family, &prefix->add, prefix->bitlen, NULL)); - } - prefix->ref_count++; - /* fprintf(stderr, "[A %s, %d]\n", ndpi_prefix_toa (prefix), prefix->ref_count); */ - return (prefix); -} - -void -ndpi_Deref_Prefix (prefix_t * prefix) -{ - if(prefix == NULL) - return; - /* for secure programming, raise an assert. no static prefix can call this */ - assert (prefix->ref_count > 0); - - prefix->ref_count--; - assert (prefix->ref_count >= 0); - if(prefix->ref_count <= 0) { - ndpi_DeleteEntry (prefix); - return; - } -} - -/* } */ - -/* #define PATRICIA_DEBUG 1 */ - -static int num_active_patricia = 0; - -/* these routines support continuous mask only */ - -patricia_tree_t * -ndpi_New_Patricia (int maxbits) -{ - patricia_tree_t *patricia = (patricia_tree_t*)ndpi_calloc(1, sizeof *patricia); - - patricia->maxbits = maxbits; - patricia->head = NULL; - patricia->num_active_node = 0; - assert((u_int)maxbits <= PATRICIA_MAXBITS); /* XXX */ - num_active_patricia++; - return (patricia); -} - - -/* - * if func is supplied, it will be called as func(node->data) - * before deleting the node - */ - -void -ndpi_Clear_Patricia (patricia_tree_t *patricia, void_fn_t func) -{ - assert (patricia); - if(patricia->head) { - - patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; - patricia_node_t **Xsp = Xstack; - patricia_node_t *Xrn = patricia->head; - - while (Xrn) { - patricia_node_t *l = Xrn->l; - patricia_node_t *r = Xrn->r; - - if(Xrn->prefix) { - ndpi_Deref_Prefix (Xrn->prefix); - if(Xrn->data && func) - func (Xrn->data); - } - else { - assert (Xrn->data == NULL); - } - ndpi_DeleteEntry (Xrn); - patricia->num_active_node--; - - if(l) { - if(r) { - *Xsp++ = r; - } - Xrn = l; - } else if(r) { - Xrn = r; - } else if(Xsp != Xstack) { - Xrn = *(--Xsp); - } else { - Xrn = NULL; - } - } - } - assert (patricia->num_active_node == 0); - /* ndpi_DeleteEntry (patricia); */ -} - - -void -ndpi_Destroy_Patricia (patricia_tree_t *patricia, void_fn_t func) -{ - ndpi_Clear_Patricia (patricia, func); - ndpi_DeleteEntry (patricia); - num_active_patricia--; -} - - -/* - * if func is supplied, it will be called as func(node->prefix, node->data) - */ - -void -ndpi_patricia_process (patricia_tree_t *patricia, void_fn2_t func) -{ - patricia_node_t *node; - assert (func); - - PATRICIA_WALK (patricia->head, node) { - func (node->prefix, node->data); - } PATRICIA_WALK_END; -} - -size_t -ndpi_patricia_walk_inorder(patricia_node_t *node, void_fn2_t func) -{ - size_t n = 0; - assert(func); - - if(node->l) { - n += ndpi_patricia_walk_inorder(node->l, func); - } - - if(node->prefix) { - func(node->prefix, node->data); - n++; - } - - if(node->r) { - n += ndpi_patricia_walk_inorder(node->r, func); - } - - return n; -} - - -patricia_node_t * -ndpi_patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix) -{ - patricia_node_t *node; - u_char *addr; - u_int bitlen; - - assert (patricia); - assert (prefix); - assert (prefix->bitlen <= patricia->maxbits); - - if(patricia->head == NULL) - return (NULL); - - node = patricia->head; - addr = prefix_touchar (prefix); - bitlen = prefix->bitlen; - - while (node->bit < bitlen) { - - if(BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { -#ifdef PATRICIA_DEBUG - if(node->prefix) - fprintf (stderr, "patricia_search_exact: take right %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); - else - fprintf (stderr, "patricia_search_exact: take right at %u\n", - node->bit); -#endif /* PATRICIA_DEBUG */ - node = node->r; - } - else { -#ifdef PATRICIA_DEBUG - if(node->prefix) - fprintf (stderr, "patricia_search_exact: take left %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); - else - fprintf (stderr, "patricia_search_exact: take left at %u\n", - node->bit); -#endif /* PATRICIA_DEBUG */ - node = node->l; - } - - if(node == NULL) - return (NULL); - } - -#ifdef PATRICIA_DEBUG - if(node->prefix) - fprintf (stderr, "patricia_search_exact: stop at %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); - else - fprintf (stderr, "patricia_search_exact: stop at %u\n", node->bit); -#endif /* PATRICIA_DEBUG */ - if(node->bit > bitlen || node->prefix == NULL) - return (NULL); - assert (node->bit == bitlen); - assert (node->bit == node->prefix->bitlen); - if(ndpi_comp_with_mask (ndpi_prefix_tochar (node->prefix), ndpi_prefix_tochar (prefix), - bitlen)) { -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_search_exact: found %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - return (node); - } - return (NULL); -} - - -/* if inclusive != 0, "best" may be the given prefix itself */ -patricia_node_t * -ndpi_patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inclusive) -{ - patricia_node_t *node; - patricia_node_t *stack[PATRICIA_MAXBITS + 1]; - u_char *addr; - u_int bitlen; - int cnt = 0; - - assert (patricia); - assert (prefix); - assert (prefix->bitlen <= patricia->maxbits); - - if(patricia->head == NULL) - return (NULL); - - node = patricia->head; - addr = prefix_touchar (prefix); - bitlen = prefix->bitlen; - - while (node->bit < bitlen) { - - if(node->prefix) { -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_search_best: push %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - stack[cnt++] = node; - } - - if(BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { -#ifdef PATRICIA_DEBUG - if(node->prefix) - fprintf (stderr, "patricia_search_best: take right %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); - else - fprintf (stderr, "patricia_search_best: take right at %u\n", - node->bit); -#endif /* PATRICIA_DEBUG */ - node = node->r; - } - else { -#ifdef PATRICIA_DEBUG - if(node->prefix) - fprintf (stderr, "patricia_search_best: take left %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); - else - fprintf (stderr, "patricia_search_best: take left at %u\n", - node->bit); -#endif /* PATRICIA_DEBUG */ - node = node->l; - } - - if(node == NULL) - break; - } - - if(inclusive && node && node->prefix) - stack[cnt++] = node; - -#ifdef PATRICIA_DEBUG - if(node == NULL) - fprintf (stderr, "patricia_search_best: stop at null\n"); - else if(node->prefix) - fprintf (stderr, "patricia_search_best: stop at %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); - else - fprintf (stderr, "patricia_search_best: stop at %u\n", node->bit); -#endif /* PATRICIA_DEBUG */ - - if(cnt <= 0) - return (NULL); - - while (--cnt >= 0) { - node = stack[cnt]; -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_search_best: pop %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - if(ndpi_comp_with_mask (ndpi_prefix_tochar (node->prefix), - ndpi_prefix_tochar (prefix), - node->prefix->bitlen) && node->prefix->bitlen <= bitlen) { -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_search_best: found %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - return (node); - } - } - return (NULL); -} - - -patricia_node_t * -ndpi_patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix) -{ - return (ndpi_patricia_search_best2 (patricia, prefix, 1)); -} - - -patricia_node_t * -ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix) -{ - patricia_node_t *node, *new_node, *parent, *glue; - u_char *addr, *test_addr; - u_int bitlen, check_bit, differ_bit; - int i, j; - - assert (patricia); - assert (prefix); - assert (prefix->bitlen <= patricia->maxbits); - - if(patricia->head == NULL) { - node = (patricia_node_t*)ndpi_calloc(1, sizeof *node); - node->bit = prefix->bitlen; - node->prefix = ndpi_Ref_Prefix (prefix); - node->parent = NULL; - node->l = node->r = NULL; - node->data = NULL; - patricia->head = node; -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_lookup: new_node #0 %s/%d (head)\n", - ndpi_prefix_toa (prefix), prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - patricia->num_active_node++; - return (node); - } - - addr = prefix_touchar (prefix); - bitlen = prefix->bitlen; - node = patricia->head; - - while (node->bit < bitlen || node->prefix == NULL) { - - if(node->bit < patricia->maxbits && - BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { - if(node->r == NULL) - break; -#ifdef PATRICIA_DEBUG - if(node->prefix) - fprintf (stderr, "patricia_lookup: take right %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); - else - fprintf (stderr, "patricia_lookup: take right at %u\n", node->bit); -#endif /* PATRICIA_DEBUG */ - node = node->r; - } - else { - if(node->l == NULL) - break; -#ifdef PATRICIA_DEBUG - if(node->prefix) - fprintf (stderr, "patricia_lookup: take left %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); - else - fprintf (stderr, "patricia_lookup: take left at %u\n", node->bit); -#endif /* PATRICIA_DEBUG */ - node = node->l; - } - - assert (node); - } - - assert (node->prefix); -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_lookup: stop at %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - - test_addr = prefix_touchar (node->prefix); - /* find the first bit different */ - check_bit = (node->bit < bitlen)? node->bit: bitlen; - differ_bit = 0; - for (i = 0; (u_int)i*8 < check_bit; i++) { - int r; - - if((r = (addr[i] ^ test_addr[i])) == 0) { - differ_bit = (i + 1) * 8; - continue; - } - /* I know the better way, but for now */ - for (j = 0; j < 8; j++) { - if(BIT_TEST (r, (0x80 >> j))) - break; - } - /* must be found */ - assert (j < 8); - differ_bit = i * 8 + j; - break; - } - if(differ_bit > check_bit) - differ_bit = check_bit; -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_lookup: differ_bit %d\n", differ_bit); -#endif /* PATRICIA_DEBUG */ - - parent = node->parent; - while (parent && parent->bit >= differ_bit) { - node = parent; - parent = node->parent; -#ifdef PATRICIA_DEBUG - if(node->prefix) - fprintf (stderr, "patricia_lookup: up to %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); - else - fprintf (stderr, "patricia_lookup: up to %u\n", node->bit); -#endif /* PATRICIA_DEBUG */ - } - - if(differ_bit == bitlen && node->bit == bitlen) { - if(node->prefix) { -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_lookup: found %s/%d\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - return (node); - } - node->prefix = ndpi_Ref_Prefix (prefix); -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_lookup: new node #1 %s/%d (glue mod)\n", - ndpi_prefix_toa (prefix), prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - assert (node->data == NULL); - return (node); - } - - new_node = (patricia_node_t*)ndpi_calloc(1, sizeof *new_node); - new_node->bit = prefix->bitlen; - new_node->prefix = ndpi_Ref_Prefix (prefix); - new_node->parent = NULL; - new_node->l = new_node->r = NULL; - new_node->data = NULL; - patricia->num_active_node++; - - if(node->bit == differ_bit) { - new_node->parent = node; - if(node->bit < patricia->maxbits && - BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) { - assert (node->r == NULL); - node->r = new_node; - } - else { - assert (node->l == NULL); - node->l = new_node; - } -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_lookup: new_node #2 %s/%d (child)\n", - ndpi_prefix_toa (prefix), prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - return (new_node); - } - - if(bitlen == differ_bit) { - if(bitlen < patricia->maxbits && - BIT_TEST (test_addr[bitlen >> 3], 0x80 >> (bitlen & 0x07))) { - new_node->r = node; - } - else { - new_node->l = node; - } - new_node->parent = node->parent; - if(node->parent == NULL) { - assert (patricia->head == node); - patricia->head = new_node; - } - else if(node->parent->r == node) { - node->parent->r = new_node; - } - else { - node->parent->l = new_node; - } - node->parent = new_node; -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_lookup: new_node #3 %s/%d (parent)\n", - ndpi_prefix_toa (prefix), prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - } - else { - glue = (patricia_node_t*)ndpi_calloc(1, sizeof *glue); - glue->bit = differ_bit; - glue->prefix = NULL; - glue->parent = node->parent; - glue->data = NULL; - patricia->num_active_node++; - if(differ_bit < patricia->maxbits && - BIT_TEST (addr[differ_bit >> 3], 0x80 >> (differ_bit & 0x07))) { - glue->r = new_node; - glue->l = node; - } - else { - glue->r = node; - glue->l = new_node; - } - new_node->parent = glue; - - if(node->parent == NULL) { - assert (patricia->head == node); - patricia->head = glue; - } - else if(node->parent->r == node) { - node->parent->r = glue; - } - else { - node->parent->l = glue; - } - node->parent = glue; -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_lookup: new_node #4 %s/%d (glue+node)\n", - ndpi_prefix_toa (prefix), prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - } - return (new_node); -} - - -void -ndpi_patricia_remove (patricia_tree_t *patricia, patricia_node_t *node) -{ - patricia_node_t *parent, *child; - - assert (patricia); - assert (node); - - if(node->r && node->l) { -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_remove: #0 %s/%d (r & l)\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - - /* this might be a placeholder node -- have to check and make sure - * there is a prefix aossciated with it ! */ - if(node->prefix != NULL) - ndpi_Deref_Prefix (node->prefix); - node->prefix = NULL; - /* Also I needed to clear data pointer -- masaki */ - node->data = NULL; - return; - } - - if(node->r == NULL && node->l == NULL) { -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_remove: #1 %s/%d (!r & !l)\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - parent = node->parent; - ndpi_Deref_Prefix (node->prefix); - ndpi_DeleteEntry (node); - patricia->num_active_node--; - - if(parent == NULL) { - assert (patricia->head == node); - patricia->head = NULL; - return; - } - - if(parent->r == node) { - parent->r = NULL; - child = parent->l; - } - else { - assert (parent->l == node); - parent->l = NULL; - child = parent->r; - } - - if(parent->prefix) - return; - - /* we need to remove parent too */ - - if(parent->parent == NULL) { - assert (patricia->head == parent); - patricia->head = child; - } - else if(parent->parent->r == parent) { - parent->parent->r = child; - } - else { - assert (parent->parent->l == parent); - parent->parent->l = child; - } - child->parent = parent->parent; - ndpi_DeleteEntry (parent); - patricia->num_active_node--; - return; - } - -#ifdef PATRICIA_DEBUG - fprintf (stderr, "patricia_remove: #2 %s/%d (r ^ l)\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); -#endif /* PATRICIA_DEBUG */ - if(node->r) { - child = node->r; - } - else { - assert (node->l); - child = node->l; - } - parent = node->parent; - child->parent = parent; - - ndpi_Deref_Prefix (node->prefix); - ndpi_DeleteEntry (node); - patricia->num_active_node--; - - if(parent == NULL) { - assert (patricia->head == node); - patricia->head = child; - return; - } - - if(parent->r == node) { - parent->r = child; - } - else { - assert (parent->l == node); - parent->l = child; - } -} - -/* { from demo.c */ -#if 0 - -patricia_node_t * -ndpi_make_and_lookup (patricia_tree_t *tree, char *string) -{ - prefix_t *prefix; - patricia_node_t *node; - - prefix = ndpi_ascii2prefix (AF_INET, string); - printf ("make_and_lookup: %s/%d\n", ndpi_prefix_toa (prefix), prefix->bitlen); - node = ndpi_patricia_lookup (tree, prefix); - ndpi_Deref_Prefix (prefix); - return (node); -} - -patricia_node_t * -ndpi_try_search_exact (patricia_tree_t *tree, char *string) -{ - prefix_t *prefix; - patricia_node_t *node; - - prefix = ndpi_ascii2prefix (AF_INET, string); - printf ("try_search_exact: %s/%d\n", ndpi_prefix_toa (prefix), prefix->bitlen); - if((node = patricia_search_exact (tree, prefix)) == NULL) { - printf ("try_search_exact: not found\n"); - } - else { - printf ("try_search_exact: %s/%d found\n", - ndpi_prefix_toa (node->prefix), node->prefix->bitlen); - } - ndpi_Deref_Prefix (prefix); - return (node); -} - -void -ndpi_lookup_then_remove (patricia_tree_t *tree, char *string) -{ - patricia_node_t *node; - - if((node = try_search_exact (tree, string))) - patricia_remove (tree, node); -} -#endif - -/* } */ -- cgit v1.2.3 From 079a42c7525f14bb6dd3c39e9308c3f6600cd4f0 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Mon, 18 May 2015 16:09:57 +0200 Subject: Minor code cleanup --- src/lib/ndpi_main.c | 4 ++-- src/lib/protocols/dns.c | 2 +- src/lib/third_party/include/ndpi_patricia.h | 10 +++++----- src/lib/third_party/src/ndpi_patricia.c | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index b175ffe1b..722984953 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -49,8 +49,8 @@ #endif #include "ndpi_content_match.c.inc" -#include "third_party/include/patricia.h" -#include "third_party/src/patricia.c" +#include "third_party/include/ndpi_patricia.h" +#include "third_party/src/ndpi_patricia.c" #ifdef WIN32 /* http://social.msdn.microsoft.com/Forums/uk/vcgeneral/thread/963aac07-da1a-4612-be4a-faac3f1d65ca */ diff --git a/src/lib/protocols/dns.c b/src/lib/protocols/dns.c index 071039340..631514afd 100644 --- a/src/lib/protocols/dns.c +++ b/src/lib/protocols/dns.c @@ -246,7 +246,7 @@ void ndpi_search_dns(struct ndpi_detection_module_struct *ndpi_struct, struct nd j++, i++; } - if(a_record != 0) { + if(a_record[0] != 0) { char a_buf[32]; int i; diff --git a/src/lib/third_party/include/ndpi_patricia.h b/src/lib/third_party/include/ndpi_patricia.h index c369bd3a7..bf4a86259 100644 --- a/src/lib/third_party/include/ndpi_patricia.h +++ b/src/lib/third_party/include/ndpi_patricia.h @@ -1,5 +1,5 @@ /* - * $Id: patricia.h,v 1.6 2005/12/07 20:53:01 dplonka Exp $ + * $Id: ndpi_patricia.h,v 1.6 2005/12/07 20:53:01 dplonka Exp $ * Dave Plonka * * This product includes software developed by the University of Michigan, @@ -7,7 +7,7 @@ * * This file had been called "radix.h" in the MRT sources. * - * I renamed it to "patricia.h" since it's not an implementation of a general + * I renamed it to "ndpi_patricia.h" since it's not an implementation of a general * radix trie. Also, pulled in various requirements from "mrt.h" and added * some other things it could be used as a standalone API. @@ -38,8 +38,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _PATRICIA_H -#define _PATRICIA_H +#ifndef _NDPI_PATRICIA_H +#define _NDPI_PATRICIA_H #ifndef WIN32 #define PATRICIA_IPV6 HAVE_IPV6 @@ -189,7 +189,7 @@ void ndpi_patricia_process (patricia_tree_t *patricia, void_fn2_t func); } \ } while (0) -#endif /* _PATRICIA_H */ +#endif /* _NDPI_PATRICIA_H */ /************************* diff --git a/src/lib/third_party/src/ndpi_patricia.c b/src/lib/third_party/src/ndpi_patricia.c index 7a95a8765..59d17e556 100644 --- a/src/lib/third_party/src/ndpi_patricia.c +++ b/src/lib/third_party/src/ndpi_patricia.c @@ -58,7 +58,7 @@ #define assert(a) ; #endif /* __KERNEL__ */ -#include "patricia.h" +#include "ndpi_patricia.h" #ifdef __KERNEL__ @@ -196,7 +196,7 @@ ndpi_my_inet_pton (int af, const char *src, void *dst) * thread safe and (almost) re-entrant implementation */ char * -ndpi_ndpi_prefix_toa2x (prefix_t *prefix, char *buff, int with_len) +ndpi_prefix_toa2x (prefix_t *prefix, char *buff, int with_len) { if(prefix == NULL) return ((char*)"(Null)"); @@ -257,7 +257,7 @@ ndpi_ndpi_prefix_toa2x (prefix_t *prefix, char *buff, int with_len) char * ndpi_prefix_toa2 (prefix_t *prefix, char *buff) { - return (ndpi_ndpi_prefix_toa2x (prefix, buff, 0)); + return (ndpi_prefix_toa2x (prefix, buff, 0)); } /* ndpi_prefix_toa -- cgit v1.2.3 From 64121162dd3262eec5d2ebca8b7a3be97f2c552c Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Mon, 18 May 2015 16:17:23 +0200 Subject: Win32 fix --- src/lib/third_party/include/ndpi_patricia.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/third_party/include/ndpi_patricia.h b/src/lib/third_party/include/ndpi_patricia.h index bf4a86259..651e52fc9 100644 --- a/src/lib/third_party/include/ndpi_patricia.h +++ b/src/lib/third_party/include/ndpi_patricia.h @@ -44,7 +44,7 @@ #ifndef WIN32 #define PATRICIA_IPV6 HAVE_IPV6 #else -#define PATRICIA_IPV6 0 +#undef PATRICIA_IPV6 #endif /* typedef unsigned int u_int; */ @@ -142,7 +142,11 @@ void ndpi_Clear_Patricia (patricia_tree_t *patricia, void_fn_t func); void ndpi_Destroy_Patricia (patricia_tree_t *patricia, void_fn_t func); void ndpi_patricia_process (patricia_tree_t *patricia, void_fn2_t func); +#ifdef WIN32 +#define PATRICIA_MAXBITS 128 +#else #define PATRICIA_MAXBITS (sizeof(struct in6_addr) * 8) +#endif #define PATRICIA_NBIT(x) (0x80 >> ((x) & 0x7f)) #define PATRICIA_NBYTE(x) ((x) >> 3) -- cgit v1.2.3 From c6e9ad430963d49b151ccd59c018df8f33a82410 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Mon, 18 May 2015 18:25:32 +0200 Subject: Removed warning --- src/include/ndpi_typedefs.h | 54 ++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h index 5bdd5a6e6..1afca2138 100644 --- a/src/include/ndpi_typedefs.h +++ b/src/include/ndpi_typedefs.h @@ -32,8 +32,8 @@ typedef enum { } ndpi_log_level_t; typedef void (*ndpi_debug_function_ptr) (u_int32_t protocol, - void *module_struct, ndpi_log_level_t log_level, - const char *format, ...); + void *module_struct, ndpi_log_level_t log_level, + const char *format, ...); #define BT_ANNOUNCE typedef enum { @@ -102,43 +102,43 @@ typedef union { #ifdef NDPI_PROTOCOL_BITTORRENT #ifndef __KERNEL__ typedef struct spinlock { - volatile int val; + volatile int val; } spinlock_t; typedef struct atomic { - volatile int counter; + volatile int counter; } atomic_t; #endif struct hash_ip4p_node { - struct hash_ip4p_node *next,*prev; - time_t lchg; - u_int16_t port,count:12,flag:4; - u_int32_t ip; - // + 12 bytes for ipv6 + struct hash_ip4p_node *next,*prev; + time_t lchg; + u_int16_t port,count:12,flag:4; + u_int32_t ip; + // + 12 bytes for ipv6 }; struct hash_ip4p { - struct hash_ip4p_node *top; - spinlock_t lock; - size_t len; + struct hash_ip4p_node *top; + spinlock_t lock; + size_t len; }; struct hash_ip4p_table { - size_t size; - int ipv6; - spinlock_t lock; - atomic_t count; - struct hash_ip4p tbl[0]; + size_t size; + int ipv6; + spinlock_t lock; + atomic_t count; + struct hash_ip4p tbl; }; struct bt_announce { // 192 bytes - u_int32_t hash[5]; - u_int32_t ip[4]; - u_int32_t time; - u_int16_t port; - u_int8_t name_len, - name[192 - 4*10 - 2 - 1]; // 149 bytes + u_int32_t hash[5]; + u_int32_t ip[4]; + u_int32_t time; + u_int16_t port; + u_int8_t name_len, + name[192 - 4*10 - 2 - 1]; // 149 bytes }; #endif @@ -174,7 +174,7 @@ typedef struct ndpi_id_struct { u_int32_t yahoo_video_lan_timer; #endif #endif -/* NDPI_PROTOCOL_IRC_MAXPORT % 2 must be 0 */ + /* NDPI_PROTOCOL_IRC_MAXPORT % 2 must be 0 */ #ifdef NDPI_PROTOCOL_IRC #define NDPI_PROTOCOL_IRC_MAXPORT 8 u_int16_t irc_port[NDPI_PROTOCOL_IRC_MAXPORT]; @@ -518,8 +518,8 @@ typedef struct ndpi_packet_struct { u_int8_t ssl_certificate_detected:4, ssl_certificate_num_checks:4; u_int8_t packet_lines_parsed_complete:1, - packet_direction:1, - empty_line_position_set:1; + packet_direction:1, + empty_line_position_set:1; } ndpi_packet_struct_t; struct ndpi_detection_module_struct; @@ -710,7 +710,7 @@ typedef struct ndpi_flow_struct { Pointer to src or dst that identifies the server of this connection - */ + */ #ifndef __KERNEL__ u_char host_server_name[256]; /* HTTP host or DNS query */ #else -- cgit v1.2.3 From 167c83121262c4621b56f1cb3927bf5f8a5f9a69 Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Mon, 18 May 2015 18:29:22 +0200 Subject: Fixed compilation warnings --- src/lib/ndpi_main.c | 2 +- src/lib/protocols/warcraft3.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 722984953..7719f766e 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -4212,7 +4212,7 @@ unsigned int ndpi_detection_process_packet(struct ndpi_detection_module_struct * flow->packet.tick_timestamp = d; } #else - flow->packet.tick_timestamp = current_tick_l/1000; + flow->packet.tick_timestamp = (u_int32_t)current_tick_l/1000; #endif /* parse packet */ diff --git a/src/lib/protocols/warcraft3.c b/src/lib/protocols/warcraft3.c index 7780dbf6e..39c93378e 100644 --- a/src/lib/protocols/warcraft3.c +++ b/src/lib/protocols/warcraft3.c @@ -43,7 +43,7 @@ void ndpi_search_warcraft3(struct ndpi_detection_module_struct // struct ndpi_id_struct *src=ndpi_struct->src; // struct ndpi_id_struct *dst=ndpi_struct->dst; - u_int32_t l; /* + u_int16_t l; /* Leave it as u_int32_t because otherwise 'u_int16_t temp' might overflood it and thus generate an infinite loop */ -- cgit v1.2.3 From c6d09df8846e6566d7d11b471732d11e15f8b93f Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Tue, 19 May 2015 08:14:33 +0200 Subject: Skype test files --- tests/pcap/skype.pcap | Bin 0 -> 699646 bytes tests/pcap/skype_no_unknown.pcap | Bin 0 -> 526100 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/pcap/skype.pcap create mode 100644 tests/pcap/skype_no_unknown.pcap diff --git a/tests/pcap/skype.pcap b/tests/pcap/skype.pcap new file mode 100644 index 000000000..d8c85f508 Binary files /dev/null and b/tests/pcap/skype.pcap differ diff --git a/tests/pcap/skype_no_unknown.pcap b/tests/pcap/skype_no_unknown.pcap new file mode 100644 index 000000000..5266bca6f Binary files /dev/null and b/tests/pcap/skype_no_unknown.pcap differ -- cgit v1.2.3 From 993665726aa807ff64ca28776af6aee43b19b66d Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Tue, 19 May 2015 12:30:44 +0200 Subject: Fixed bug that prevented (due to endianess) to properly match hosts Added tests for skype detection --- src/lib/ndpi_content_match.c.inc | 7 +++++-- src/lib/ndpi_main.c | 4 +++- tests/result/skype.pcap.out | 13 +++++++++++++ tests/result/skype_no_unknown.pcap.out | 11 +++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tests/result/skype.pcap.out create mode 100644 tests/result/skype_no_unknown.pcap.out diff --git a/src/lib/ndpi_content_match.c.inc b/src/lib/ndpi_content_match.c.inc index 3b4693fc3..1a7f8eef6 100644 --- a/src/lib/ndpi_content_match.c.inc +++ b/src/lib/ndpi_content_match.c.inc @@ -72,12 +72,14 @@ static ndpi_network host_protocol_list[] = { { 0x6CA0A000 /* 108.160.160.0 */, 20, NDPI_PROTOCOL_DROPBOX }, /* - Skype + Skype (Microsoft CDN) 157.56.0.0/14, 157.60.0.0/16, 157.54.0.0/15 + 111.221.64.0 - 111.221.127.255 */ { 0x9D380000 /* 157.56.0.0 */, 14, NDPI_PROTOCOL_SKYPE }, { 0x9D3C0000 /* 157.60.0.0 */, 16, NDPI_PROTOCOL_SKYPE }, - { 0x9D360000 /* 157.54.0.0/ */, 15, NDPI_PROTOCOL_SKYPE }, + { 0x9D360000 /* 157.54.0.0 */, 15, NDPI_PROTOCOL_SKYPE }, + { 0x6FDD4000 /* 111.221.64.0 */, 18, NDPI_PROTOCOL_SKYPE }, /* Google @@ -258,6 +260,7 @@ static ndpi_network host_protocol_list[] = { { 0xD820F200, 24, NDPI_PROTOCOL_SKYPE }, { 0xD821F000, 22, NDPI_PROTOCOL_SKYPE }, { 0xD4A10800, 24, NDPI_PROTOCOL_SKYPE }, + { 0x012A1231, 32, NDPI_PROTOCOL_TOR }, { 0x01E69FA1, 32, NDPI_PROTOCOL_TOR }, { 0x020DE985, 32, NDPI_PROTOCOL_TOR }, diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 7719f766e..8d39d8115 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -1677,6 +1677,7 @@ static int fill_prefix_v4(prefix_t *p, struct in_addr *a, int b, int mb) { if(b < 0 || b > mb) return(-1); + memset(p, 0, sizeof(prefix_t)); memcpy(&p->add.sin, a, (mb+7)/8); p->family = AF_INET; p->bitlen = b; @@ -1692,6 +1693,7 @@ u_int16_t ndpi_network_ptree_match(struct ndpi_detection_module_struct *ndpi_str prefix_t prefix; patricia_node_t *node; + pin->s_addr = ntohl(pin->s_addr); /* Make sure all in network byte order otherwise compares wont work */ fill_prefix_v4(&prefix, pin, 32, ((patricia_tree_t*)ndpi_struct->protocols_ptree)->maxbits); node = ndpi_patricia_search_best(ndpi_struct->protocols_ptree, &prefix); @@ -1755,7 +1757,7 @@ static void ndpi_init_ptree_ipv4(struct ndpi_detection_module_struct *ndpi_str, struct in_addr pin; patricia_node_t *node; - pin.s_addr = host_list[i].network; + pin.s_addr = ntohl(host_list[i].network); if((node = add_to_ptree(ptree, AF_INET, &pin, host_list[i].cidr /* bits */)) != NULL) node->value.user_value = host_list[i].value; } diff --git a/tests/result/skype.pcap.out b/tests/result/skype.pcap.out new file mode 100644 index 000000000..404442549 --- /dev/null +++ b/tests/result/skype.pcap.out @@ -0,0 +1,13 @@ +Unknown 409 49221 18 +DNS 8 807 4 +MDNS 8 1736 2 +NTP 2 180 1 +SSDP 101 38156 6 +ICMP 8 656 1 +IGMP 5 258 4 +SSL 20 1516 1 +DropBox 38 17948 5 +Skype 2362 501011 246 +Apple 15 2045 2 +AppleiCloud 88 20520 2 +Spotify 5 430 1 diff --git a/tests/result/skype_no_unknown.pcap.out b/tests/result/skype_no_unknown.pcap.out new file mode 100644 index 000000000..2b6435897 --- /dev/null +++ b/tests/result/skype_no_unknown.pcap.out @@ -0,0 +1,11 @@ +Unknown 241 60170 16 +DNS 6 627 3 +MDNS 3 400 2 +NetBIOS 22 3106 7 +SSDP 40 14100 3 +ICMP 4 328 1 +IGMP 4 226 4 +SSL 22 1444 3 +DropBox 16 7342 5 +Skype 1637 379382 221 +Apple 84 20699 2 -- cgit v1.2.3 From 8f3b8ff4dfb5b7674b6d87c78c1bdb275f178d4d Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Wed, 20 May 2015 09:27:04 +0200 Subject: Added fix to harden quic detection and limit it to port 80/443 UDP --- src/lib/protocols/quic.c | 193 +++++++++++++++++++++++---------------------- tests/do.sh | 5 +- tests/result/quic.pcap.out | 2 +- 3 files changed, 105 insertions(+), 95 deletions(-) diff --git a/src/lib/protocols/quic.c b/src/lib/protocols/quic.c index 2e1ad6628..cbc95519d 100644 --- a/src/lib/protocols/quic.c +++ b/src/lib/protocols/quic.c @@ -46,111 +46,118 @@ static void ndpi_int_quic_add_connection(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { - ndpi_int_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_QUIC, NDPI_REAL_PROTOCOL); + ndpi_int_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_QUIC, NDPI_REAL_PROTOCOL); } int connect_id(const unsigned char pflags) { - u_int cid_len; + u_int cid_len; - // Check CID length. - switch (pflags & QUIC_CID_MASK) - { - case CID_LEN_8: cid_len = 8; break; - case CID_LEN_4: cid_len = 4; break; - case CID_LEN_1: cid_len = 1; break; - case CID_LEN_0: cid_len = 0; break; - default: - return -1; - - } - // Return offset. - return cid_len + 1; + // Check CID length. + switch (pflags & QUIC_CID_MASK) + { + case CID_LEN_8: cid_len = 8; break; + case CID_LEN_4: cid_len = 4; break; + case CID_LEN_1: cid_len = 1; break; + case CID_LEN_0: cid_len = 0; break; + default: + return -1; + + } + // Return offset. + return cid_len + 1; } int sequence(const unsigned char *payload) { - unsigned char conv[6] = {0}; - u_int seq_value = -1; - u_int seq_lens; - u_int cid_offs; - int i; - - // Search SEQ bytes length. - switch (payload[0] & QUIC_SEQ_MASK) - { - case SEQ_LEN_6: seq_lens = 6; break; - case SEQ_LEN_4: seq_lens = 4; break; - case SEQ_LEN_2: seq_lens = 2; break; - case SEQ_LEN_1: seq_lens = 1; break; - default: - return -1; - } - // Retrieve SEQ offset. - cid_offs = connect_id(payload[0]); - - if (cid_offs >= 0 && seq_lens > 0) - { - for (i = 0; i < seq_lens; i++) - conv[i] = payload[cid_offs + i]; - - seq_value = SEQ_CONV(conv); - } - // Return SEQ dec value; - return seq_value; + unsigned char conv[6] = {0}; + u_int seq_value = -1; + u_int seq_lens; + u_int cid_offs; + int i; + + // Search SEQ bytes length. + switch (payload[0] & QUIC_SEQ_MASK) + { + case SEQ_LEN_6: seq_lens = 6; break; + case SEQ_LEN_4: seq_lens = 4; break; + case SEQ_LEN_2: seq_lens = 2; break; + case SEQ_LEN_1: seq_lens = 1; break; + default: + return -1; + } + // Retrieve SEQ offset. + cid_offs = connect_id(payload[0]); + + if (cid_offs >= 0 && seq_lens > 0) + { + for (i = 0; i < seq_lens; i++) + conv[i] = payload[cid_offs + i]; + + seq_value = SEQ_CONV(conv); + } + // Return SEQ dec value; + return seq_value; } void ndpi_search_quic(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { - struct ndpi_packet_struct *packet = &flow->packet; - u_int ver_offs; - - if(packet->udp != NULL) { - NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "calculating quic over udp.\n"); - - // Settings without version. First check if PUBLIC FLAGS & SEQ bytes are 0x0. SEQ must be 1 at least. - if ((packet->payload[0] == 0x00 && packet->payload[1] != 0x00) || ((packet->payload[0] & QUIC_NO_V_RES_RSV) == 0)) - { - if (sequence(packet->payload) < 1) - { - - NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "exclude quic.\n"); - NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_QUIC); - } - - NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "found quic.\n"); - ndpi_int_quic_add_connection(ndpi_struct, flow); - } - - // Check if version, than the CID length. - else if (packet->payload[0] & QUIC_VER_MASK) - { - // Skip CID length. - ver_offs = connect_id(packet->payload[0]); - - if (ver_offs >= 0){ - unsigned char vers[] = {packet->payload[ver_offs], packet->payload[ver_offs + 1], - packet->payload[ver_offs + 2], packet->payload[ver_offs + 3]}; - - // Version Match. - if (vers[0] == 'Q' && vers[1] == '0' && - (vers[2] == '2' && (vers[3] == '5' || vers[3] == '4' || vers[3] == '3' || vers[3] == '2' || - vers[3] == '1' || vers[3] == '0')) || - (vers[2] == '1' && (vers[3] == '9' || vers[3] == '8' || vers[3] == '7' || vers[3] == '6' || - vers[3] == '5' || vers[3] == '4' || vers[3] == '3' || vers[3] == '2' || - vers[3] == '1' || vers[3] == '0')) || - (vers[2] == '0' && vers[3] == '9')) - - { - NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "found quic.\n"); - ndpi_int_quic_add_connection(ndpi_struct, flow); - } - } - } else - { - NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "exclude quic.\n"); - NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_QUIC); - } - } + struct ndpi_packet_struct *packet = &flow->packet; + u_int ver_offs; + + if(packet->udp != NULL) { + u_int16_t sport = ntohs(packet->udp->source), dport = ntohs(packet->udp->dest); + + NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "calculating quic over udp.\n"); + + if(((packet->payload[0] & 0xF0) != 0) + || (!(sport == 80 || dport == 80 || sport == 443 || dport == 443))) + goto exclude_quic; + + // Settings without version. First check if PUBLIC FLAGS & SEQ bytes are 0x0. SEQ must be 1 at least. + if ((packet->payload[0] == 0x00 && packet->payload[1] != 0x00) || ((packet->payload[0] & QUIC_NO_V_RES_RSV) == 0)) + { + if (sequence(packet->payload) < 1) + { + + NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "exclude quic.\n"); + NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_QUIC); + } + + NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "found quic.\n"); + ndpi_int_quic_add_connection(ndpi_struct, flow); + } + + // Check if version, than the CID length. + else if (packet->payload[0] & QUIC_VER_MASK) + { + // Skip CID length. + ver_offs = connect_id(packet->payload[0]); + + if (ver_offs >= 0){ + unsigned char vers[] = {packet->payload[ver_offs], packet->payload[ver_offs + 1], + packet->payload[ver_offs + 2], packet->payload[ver_offs + 3]}; + + // Version Match. + if (vers[0] == 'Q' && vers[1] == '0' && + (vers[2] == '2' && (vers[3] == '5' || vers[3] == '4' || vers[3] == '3' || vers[3] == '2' || + vers[3] == '1' || vers[3] == '0')) || + (vers[2] == '1' && (vers[3] == '9' || vers[3] == '8' || vers[3] == '7' || vers[3] == '6' || + vers[3] == '5' || vers[3] == '4' || vers[3] == '3' || vers[3] == '2' || + vers[3] == '1' || vers[3] == '0')) || + (vers[2] == '0' && vers[3] == '9')) + + { + NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "found quic.\n"); + ndpi_int_quic_add_connection(ndpi_struct, flow); + } + } + } else + { + exclude_quic: + NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "exclude quic.\n"); + NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_QUIC); + } + } } #endif diff --git a/tests/do.sh b/tests/do.sh index 4f81906f7..e7d28aa9c 100755 --- a/tests/do.sh +++ b/tests/do.sh @@ -15,13 +15,16 @@ build_results() { check_results() { for f in $PCAPS; do if [ -f result/$f.out ]; then - $READER -q -i pcap/$f -w /tmp/reader.out + CMD="$READER -q -i pcap/$f -w /tmp/reader.out" + $CMD NUM_DIFF=`diff result/$f.out /tmp/reader.out | wc -l` if [ $NUM_DIFF -eq 0 ]; then echo "$f\t OK" else echo "$f\t ERROR" + echo "$CMD" + diff result/$f.out /tmp/reader.out fi /bin/rm /tmp/reader.out diff --git a/tests/result/quic.pcap.out b/tests/result/quic.pcap.out index 283fb04d2..900864e4d 100644 --- a/tests/result/quic.pcap.out +++ b/tests/result/quic.pcap.out @@ -1 +1 @@ -Unknown 413 254874 1 +Quic 413 254874 1 -- cgit v1.2.3 From e4923a589c04584a2faa7d9ce35e9875f41b1153 Mon Sep 17 00:00:00 2001 From: Luca Date: Wed, 20 May 2015 16:48:36 +0200 Subject: Fixed warning --- src/lib/protocols/quic.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/protocols/quic.c b/src/lib/protocols/quic.c index cbc95519d..3180ef28b 100644 --- a/src/lib/protocols/quic.c +++ b/src/lib/protocols/quic.c @@ -49,7 +49,7 @@ static void ndpi_int_quic_add_connection(struct ndpi_detection_module_struct ndpi_int_add_connection(ndpi_struct, flow, NDPI_PROTOCOL_QUIC, NDPI_REAL_PROTOCOL); } -int connect_id(const unsigned char pflags) +static int connect_id(const unsigned char pflags) { u_int cid_len; @@ -68,12 +68,12 @@ int connect_id(const unsigned char pflags) return cid_len + 1; } -int sequence(const unsigned char *payload) +static int sequence(const unsigned char *payload) { unsigned char conv[6] = {0}; u_int seq_value = -1; - u_int seq_lens; - u_int cid_offs; + int seq_lens; + int cid_offs; int i; // Search SEQ bytes length. @@ -103,7 +103,7 @@ int sequence(const unsigned char *payload) void ndpi_search_quic(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) { struct ndpi_packet_struct *packet = &flow->packet; - u_int ver_offs; + int ver_offs; if(packet->udp != NULL) { u_int16_t sport = ntohs(packet->udp->source), dport = ntohs(packet->udp->dest); -- cgit v1.2.3 From b519f76d545cfdb5e5b3a23d5d06b79be9bd065a Mon Sep 17 00:00:00 2001 From: Luca Giovenzana Date: Thu, 21 May 2015 00:57:46 +0200 Subject: Update configure.ac: introduced git tag for version, incremental number of commits and release 1.5.2-43-gb3a292d-dirty ^ ^ ^ ^ | | | | | | | if there are uncommitted changes | | SHA of HEAD | number of commits since last tag last tag --- configure.ac | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/configure.ac b/configure.ac index 7c80bdf06..eba84e705 100644 --- a/configure.ac +++ b/configure.ac @@ -10,32 +10,31 @@ AC_PROG_CC AX_PTHREAD if test -d ".git"; then : -GIT_TAG=`git rev-parse HEAD` -GIT_DATE=`date +%Y%m%d` -GIT_RELEASE="${PACKAGE_VERSION} (${GIT_TAG}:${GIT_DATE})" + GIT_TAG=`git log -1 --format=%H` + GIT_DATE=`git log -1 --format=%cd` + GIT_RELEASE=`git describe --tags --long --dirty --always` else -GIT_RELEASE="${PACKAGE_VERSION}" -SVN_DATE=`date` + GIT_RELEASE="${PACKAGE_VERSION}" + GIT_DATE=`date` fi -AC_DEFINE_UNQUOTED(NDPI_GIT_RELEASE, "r${GIT_RELEASE}", [SVN Release]) -AC_DEFINE_UNQUOTED(NDPI_SVN_DATE, "${SVN_DATE}", [Last SVN change]) +AC_DEFINE_UNQUOTED(NDPI_GIT_RELEASE, "${GIT_RELEASE}", [GIT Release]) +AC_DEFINE_UNQUOTED(NDPI_GIT_DATE, "${GIT_DATE}", [Last GIT change]) AC_CHECK_HEADERS([netinet/in.h stdint.h stdlib.h string.h unistd.h]) PCAP_HOME=$HOME/PF_RING/userland if test -d $PCAP_HOME; then : - echo -n "" + echo -n "" else - PCAP_HOME=`pwd`/../../PF_RING/userland + PCAP_HOME=`pwd`/../../PF_RING/userland fi - SHORT_MACHINE=`uname -m | cut -b1-3` if test $SHORT_MACHINE = "arm"; then -LIBNUMA="" + LIBNUMA="" else -LIBNUMA="-lnuma" + LIBNUMA="-lnuma" fi if test -f $PCAP_HOME/libpcap/libpcap.a; then : @@ -57,11 +56,11 @@ else fi if test -d /usr/local/include/json-c/; then : - CFLAGS="$CFLAGS -I/usr/local/include/json-c/" - LDFLAGS="$LDFLAGS -L/usr/local/lib -ljson-c" + CFLAGS="$CFLAGS -I/usr/local/include/json-c/" + LDFLAGS="$LDFLAGS -L/usr/local/lib -ljson-c" else - CFLAGS="$CFLAGS $(pkg-config --cflags json-c)" - LDFLAGS="$LDFLAGS $(pkg-config --libs json-c)" + CFLAGS="$CFLAGS $(pkg-config --cflags json-c)" + LDFLAGS="$LDFLAGS $(pkg-config --libs json-c)" fi OLD_LIBS=$LIBS -- cgit v1.2.3 From e792ed68908a32ae01861e13dc977cdcb8d90f8f Mon Sep 17 00:00:00 2001 From: Luca Giovenzana Date: Wed, 20 May 2015 21:17:43 +0200 Subject: Update ndpiReader.c: autoremoved trailing slash --- example/ndpiReader.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 84bece123..f6f0e55f1 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -160,7 +160,7 @@ static u_int32_t size_id_struct = 0; //< ID tracking structure size #endif // flow tracking -typedef struct ndpi_flow { +typedef struct ndpi_flow { u_int32_t lower_ip; u_int32_t upper_ip; u_int16_t lower_port; @@ -304,7 +304,7 @@ static void parseOptions(int argc, char **argv) { case 'q': quiet_mode = 1; break; - + default: help(0); break; @@ -637,7 +637,7 @@ static void node_idle_scan_walker(const void *node, ndpi_VISIT which, int depth, if (flow->detected_protocol == 0 /* UNKNOWN */ && !undetected_flows_deleted) undetected_flows_deleted = 1; - + free_ndpi_flow(flow); ndpi_thread_info[thread_id].stats.ndpi_flow_count--; @@ -866,7 +866,7 @@ static struct ndpi_flow *get_ndpi_flow6(u_int16_t thread_id, if(iph.protocol == 0x3C /* IPv6 destination option */) { u_int8_t *options = (u_int8_t*)iph6 + sizeof(const struct ndpi_ip6_hdr); - + iph.protocol = options[0]; } @@ -884,7 +884,7 @@ static void setupDetection(u_int16_t thread_id) { memset(&ndpi_thread_info[thread_id], 0, sizeof(ndpi_thread_info[thread_id])); // init global detection structure - ndpi_thread_info[thread_id].ndpi_struct = ndpi_init_detection_module(detection_tick_resolution, + ndpi_thread_info[thread_id].ndpi_struct = ndpi_init_detection_module(detection_tick_resolution, malloc_wrapper, free_wrapper, debug_printf); if(ndpi_thread_info[thread_id].ndpi_struct == NULL) { printf("ERROR: global structure initialization failed\n"); @@ -978,14 +978,14 @@ static unsigned int packet_processing(u_int16_t thread_id, } if(( - (flow->detected_protocol == NDPI_PROTOCOL_HTTP) + (flow->detected_protocol == NDPI_PROTOCOL_HTTP) || (flow->detected_protocol == NDPI_SERVICE_FACEBOOK) ) && full_http_dissection) { char *method; printf("[URL] %s\n", ndpi_get_http_url(ndpi_thread_info[thread_id].ndpi_struct, ndpi_flow)); - printf("[Content-Type] %s\n", ndpi_get_http_content_type(ndpi_thread_info[thread_id].ndpi_struct, ndpi_flow)); + printf("[Content-Type] %s\n", ndpi_get_http_content_type(ndpi_thread_info[thread_id].ndpi_struct, ndpi_flow)); switch(ndpi_get_http_method(ndpi_thread_info[thread_id].ndpi_struct, ndpi_flow)) { case HTTP_METHOD_OPTIONS: method = "HTTP_METHOD_OPTIONS"; break; @@ -1271,7 +1271,7 @@ static void printResults(u_int64_t tot_usec) { ndpi_get_proto_name(ndpi_thread_info[0].ndpi_struct, i), (long long unsigned int)cumulative_stats.protocol_counter[i], (long long unsigned int)cumulative_stats.protocol_counter_bytes[i], - cumulative_stats.protocol_flows[i]); + cumulative_stats.protocol_flows[i]); if((!json_flag) && (!quiet_mode)) { printf("\t%-20s packets: %-13llu bytes: %-13llu " @@ -1594,7 +1594,7 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header, if(proto == 0x3C /* IPv6 destination option */) { u_int8_t *options = (u_int8_t*)&packet[ip_offset+ip_len]; - + proto = options[0]; ip_len += 8 * (options[1] + 1); } -- cgit v1.2.3 From b3b0ce7237fed3d236d2113933a3041264a52828 Mon Sep 17 00:00:00 2001 From: Luca Giovenzana Date: Thu, 21 May 2015 01:18:31 +0200 Subject: Update ndpiReader.c: added -r to print used nDPI version and release --- example/ndpiReader.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/example/ndpiReader.c b/example/ndpiReader.c index f6f0e55f1..50ace72b1 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -208,6 +208,7 @@ static void help(u_int long_help) { " -d | Disable protocol guess and use only DPI\n" " -q | Quiet mode\n" " -t | Dissect GTP tunnels\n" + " -r | Print nDPI version and git revision\n" " -w | Write test output on the specified file. This is useful for\n" " | testing purposes in order to compare results across runs\n" " -h | This help\n" @@ -232,7 +233,7 @@ static void parseOptions(int argc, char **argv) { u_int num_cores = sysconf(_SC_NPROCESSORS_ONLN); #endif - while ((opt = getopt(argc, argv, "df:g:i:hp:l:s:tv:V:n:j:w:q")) != EOF) { + while ((opt = getopt(argc, argv, "df:g:i:hp:l:s:tv:V:n:j:rp:w:q")) != EOF) { switch (opt) { case 'd': enable_protocol_guess = 0; @@ -271,6 +272,10 @@ static void parseOptions(int argc, char **argv) { decode_tunnels = 1; break; + case 'r': + printf("ndpiReader, nDPI (%s)\n\n", ndpi_revision()); + exit(0); + case 'v': verbose = atoi(optarg); break; -- cgit v1.2.3 From de03e1efe4903dbcda84a8a2491cc9e1eeef210b Mon Sep 17 00:00:00 2001 From: Luca Deri Date: Thu, 21 May 2015 19:33:35 +0200 Subject: Modified GIT version format as shown ndpiReader -r --- configure.ac | 6 ++++-- example/ndpiReader.c | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index eba84e705..598c0a22b 100644 --- a/configure.ac +++ b/configure.ac @@ -10,9 +10,11 @@ AC_PROG_CC AX_PTHREAD if test -d ".git"; then : - GIT_TAG=`git log -1 --format=%H` + GIT_TAG=`git log -1 --format=%h` GIT_DATE=`git log -1 --format=%cd` - GIT_RELEASE=`git describe --tags --long --dirty --always` + GIT_NUM=`git rev-list HEAD --count` + GIT_BRANCH=`git rev-parse --abbrev-ref HEAD` + GIT_RELEASE="${PACKAGE_VERSION}-${GIT_BRANCH}-${GIT_NUM}-${GIT_TAG}" else GIT_RELEASE="${PACKAGE_VERSION}" GIT_DATE=`date` diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 50ace72b1..558d204ed 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -273,7 +273,7 @@ static void parseOptions(int argc, char **argv) { break; case 'r': - printf("ndpiReader, nDPI (%s)\n\n", ndpi_revision()); + printf("ndpiReader - nDPI (%s)\n", ndpi_revision()); exit(0); case 'v': -- cgit v1.2.3