Compare commits

...
12 Commits
Author SHA1 Message Date
Michele Campus 625ed3d4e4 Merge pull request #478 from hvanderheide/1.7-stable
Add ndpi_includes.h to include headers
2017-10-23 11:57:13 +02:00
Hidde van der Heide 16fc15fba2 Add ndpi_includes.h to include headers
Add this header file to the proper variable so it gets installed to the
system
2017-10-19 17:12:16 +02:00
Michele Campus 5b140565b1 Merge pull request #285 from vpiserchia/1.7-stable
Fix KAKAOTALK VOICE MACRO name
2016-11-03 15:11:10 +01:00
Vito Piserchia cc83d808de Fix KAKAOTALK VOICE MACRO name 2016-11-03 15:06:46 +01:00
Luca Deri a121161480 Added missing HEP initialization 2015-12-04 21:34:26 +01:00
Luca Deri e4fd4bc9f9 Typedef refresh 2015-11-30 18:46:52 +01:00
Luca Deri ff04ff42fe Merge pull request #105 from vel21ripn/1.7-stable
ndpi_search_dns: fix potential buffer overflow
2015-10-09 14:59:25 +02:00
Vitaly Lavrov 8c4fada42f ndpi_search_mail_smtp_tcp: minor fix for starttls command 2015-10-09 15:15:34 +03:00
Vitaly Lavrov 4e5b7f625d ndpi_search_dns: fix potential buffer overflow 2015-10-09 14:45:16 +03:00
Luca Deri a3d66b0777 Fixes #83. Critical fix: due to an invalid endianess conversion some protocol were not properly indetified 2015-08-26 16:15:54 +02:00
Luca Deri 49983c07b8 Updated formula for 1.7 release 2015-08-18 11:57:36 +02:00
Luca Deri 431b139e44 Sync formula with homebrew 2015-08-18 11:32:34 +02:00
24 changed files with 1187 additions and 1107 deletions
+363 -161
View File
@@ -42,18 +42,50 @@
#include <pcap.h> #include <pcap.h>
#include <signal.h> #include <signal.h>
#include <pthread.h> #include <pthread.h>
#include <sys/socket.h>
#include "../config.h" #include "../config.h"
#include "ndpi_api.h"
#ifdef HAVE_JSON_C #ifdef HAVE_JSON_C
#include <json.h> #include <json.h>
#endif #endif
#include "ndpi_api.h"
#include <sys/socket.h>
#define MAX_NUM_READER_THREADS 16 #define MAX_NUM_READER_THREADS 16
#define IDLE_SCAN_PERIOD 10 /* msec (use detection_tick_resolution = 1000) */
#define MAX_IDLE_TIME 30000
#define IDLE_SCAN_BUDGET 1024
#define NUM_ROOTS 512
#define GTP_U_V1_PORT 2152
#define TZSP_PORT 37008
#define MAX_NDPI_FLOWS 200000000
#ifndef ETH_P_IP
#define ETH_P_IP 0x0800 /* IPv4 */
#endif
#ifndef ETH_P_IPv6
#define ETH_P_IPV6 0x86dd /* IPv6 */
#endif
#define SLARP 0x8035 /* Cisco Slarp */
#define CISCO_D_PROTO 0x2000 /* Cisco Discovery Protocol */
#define VLAN 0x8100
#define MPLS_UNI 0x8847
#define MPLS_MULTI 0x8848
#define PPPoE 0x8864
#define SNAP 0xaa
/* mask for FCF */
#define WIFI_DATA 0x2 /* 0000 0010 */
#define FCF_TYPE(fc) (((fc) >> 2) & 0x3) /* 0000 0011 = 0x3 */
#define FCF_SUBTYPE(fc) (((fc) >> 4) & 0xF) /* 0000 1111 = 0xF */
#define FCF_TO_DS(fc) ((fc) & 0x0100)
#define FCF_FROM_DS(fc) ((fc) & 0x0200)
/* mask for Bad FCF presence */
#define BAD_FCS 0x50 /* 0101 0000 */
/** /**
* @brief Set main components necessary to the detection * @brief Set main components necessary to the detection
@@ -100,12 +132,6 @@ static u_int32_t detection_tick_resolution = 1000;
static time_t capture_for = 0; static time_t capture_for = 0;
static time_t capture_until = 0; static time_t capture_until = 0;
#define IDLE_SCAN_PERIOD 10 /* msec (use detection_tick_resolution = 1000) */
#define MAX_IDLE_TIME 30000
#define IDLE_SCAN_BUDGET 1024
#define NUM_ROOTS 512
static u_int32_t num_flows; static u_int32_t num_flows;
struct thread_stats { struct thread_stats {
@@ -143,21 +169,15 @@ struct reader_thread {
static struct reader_thread ndpi_thread_info[MAX_NUM_READER_THREADS]; static struct reader_thread ndpi_thread_info[MAX_NUM_READER_THREADS];
#define GTP_U_V1_PORT 2152
#define MAX_NDPI_FLOWS 200000000
/** /**
* @brief ID tracking * @brief ID tracking
*/ */
typedef struct ndpi_id { typedef struct ndpi_id {
u_int8_t ip[4]; //< Ip address u_int8_t ip[4]; // Ip address
struct ndpi_id_struct *ndpi_id; //< nDpi worker structure struct ndpi_id_struct *ndpi_id; // nDpi worker structure
} ndpi_id_t; } ndpi_id_t;
static u_int32_t size_id_struct = 0; //< ID tracking structure size static u_int32_t size_id_struct = 0; // ID tracking structure size
#ifndef ETH_P_IP
#define ETH_P_IP 0x0800
#endif
// flow tracking // flow tracking
typedef struct ndpi_flow { typedef struct ndpi_flow {
@@ -168,10 +188,9 @@ typedef struct ndpi_flow {
u_int8_t detection_completed, protocol; u_int8_t detection_completed, protocol;
u_int16_t vlan_id; u_int16_t vlan_id;
struct ndpi_flow_struct *ndpi_flow; struct ndpi_flow_struct *ndpi_flow;
char lower_name[32], upper_name[32]; char lower_name[48], upper_name[48];
u_int8_t ip_version;
u_int64_t last_seen; u_int64_t last_seen;
u_int64_t bytes; u_int64_t bytes;
u_int32_t packets; u_int32_t packets;
@@ -207,7 +226,7 @@ static void help(u_int long_help) {
#endif #endif
" -d | Disable protocol guess and use only DPI\n" " -d | Disable protocol guess and use only DPI\n"
" -q | Quiet mode\n" " -q | Quiet mode\n"
" -t | Dissect GTP tunnels\n" " -t | Dissect GTP/TZSP tunnels\n"
" -r | Print nDPI version and git revision\n" " -r | Print nDPI version and git revision\n"
" -w <path> | Write test output on the specified file. This is useful for\n" " -w <path> | Write test output on the specified file. This is useful for\n"
" | testing purposes in order to compare results across runs\n" " | testing purposes in order to compare results across runs\n"
@@ -476,19 +495,18 @@ static void printFlow(u_int16_t thread_id, struct ndpi_flow *flow) {
FILE *out = results_file ? results_file : stdout; FILE *out = results_file ? results_file : stdout;
if(!json_flag) { if(!json_flag) {
#if 0
fprintf(out, "\t%s [VLAN: %u] %s:%u <-> %s:%u\n",
ipProto2Name(flow->protocol), flow->vlan_id,
flow->lower_name, ntohs(flow->lower_port),
flow->upper_name, ntohs(flow->upper_port));
#else
fprintf(out, "\t%u", ++num_flows); fprintf(out, "\t%u", ++num_flows);
fprintf(out, "\t%s %s:%u <-> %s:%u ", fprintf(out, "\t%s %s%s%s:%u <-> %s%s%s:%u ",
ipProto2Name(flow->protocol), ipProto2Name(flow->protocol),
flow->lower_name, ntohs(flow->lower_port), (flow->ip_version == 6) ? "[" : "",
flow->upper_name, ntohs(flow->upper_port)); flow->lower_name,
(flow->ip_version == 6) ? "]" : "",
ntohs(flow->lower_port),
(flow->ip_version == 6) ? "[" : "",
flow->upper_name,
(flow->ip_version == 6) ? "]" : "",
ntohs(flow->upper_port));
if(flow->vlan_id > 0) fprintf(out, "[VLAN: %u]", flow->vlan_id); if(flow->vlan_id > 0) fprintf(out, "[VLAN: %u]", flow->vlan_id);
@@ -512,7 +530,6 @@ static void printFlow(u_int16_t thread_id, struct ndpi_flow *flow) {
if(flow->ssl.server_certificate[0] != '\0') fprintf(out, "[SSL server: %s]", flow->ssl.server_certificate); if(flow->ssl.server_certificate[0] != '\0') fprintf(out, "[SSL server: %s]", flow->ssl.server_certificate);
fprintf(out, "\n"); fprintf(out, "\n");
#endif
} else { } else {
#ifdef HAVE_JSON_C #ifdef HAVE_JSON_C
jObj = json_object_new_object(); jObj = json_object_new_object();
@@ -633,16 +650,6 @@ static void node_proto_guess_walker(const void *node, ndpi_VISIT which, int dept
struct ndpi_flow *flow = *(struct ndpi_flow **) node; struct ndpi_flow *flow = *(struct ndpi_flow **) node;
u_int16_t thread_id = *((u_int16_t *) user_data); u_int16_t thread_id = *((u_int16_t *) user_data);
#if 0
printf("<%d>Walk on node %s (%p)\n",
depth,
which == preorder?"preorder":
which == postorder?"postorder":
which == endorder?"endorder":
which == leaf?"leaf": "unknown",
flow);
#endif
if((which == ndpi_preorder) || (which == ndpi_leaf)) { /* Avoid walking the same node multiple times */ if((which == ndpi_preorder) || (which == ndpi_leaf)) { /* Avoid walking the same node multiple times */
if(enable_protocol_guess) { if(enable_protocol_guess) {
if(flow->detected_protocol.protocol == NDPI_PROTOCOL_UNKNOWN) { if(flow->detected_protocol.protocol == NDPI_PROTOCOL_UNKNOWN) {
@@ -706,23 +713,27 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id,
const u_int8_t version, const u_int8_t version,
u_int16_t vlan_id, u_int16_t vlan_id,
const struct ndpi_iphdr *iph, const struct ndpi_iphdr *iph,
const struct ndpi_ipv6hdr *iph6,
u_int16_t ip_offset, u_int16_t ip_offset,
u_int16_t ipsize, u_int16_t ipsize,
u_int16_t l4_packet_len, u_int16_t l4_packet_len,
struct ndpi_tcphdr **tcph,
struct ndpi_udphdr **udph,
u_int16_t *sport, u_int16_t *dport,
struct ndpi_id_struct **src, struct ndpi_id_struct **src,
struct ndpi_id_struct **dst, struct ndpi_id_struct **dst,
u_int8_t *proto, u_int8_t *proto,
const struct ndpi_ip6_hdr *iph6) { u_int8_t **payload,
u_int16_t *payload_len,
u_int8_t *src_to_dst_direction) {
u_int32_t idx, l4_offset; u_int32_t idx, l4_offset;
struct ndpi_tcphdr *tcph = NULL;
struct ndpi_udphdr *udph = NULL;
u_int32_t lower_ip; u_int32_t lower_ip;
u_int32_t upper_ip; u_int32_t upper_ip;
u_int16_t lower_port; u_int16_t lower_port;
u_int16_t upper_port; u_int16_t upper_port;
struct ndpi_flow flow; struct ndpi_flow flow;
void *ret; void *ret;
u_int8_t *l3; u_int8_t *l3, *l4;
/* /*
Note: to keep things simple (ndpiReader is just a demo app) Note: to keep things simple (ndpiReader is just a demo app)
@@ -739,7 +750,7 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id,
l4_offset = iph->ihl * 4; l4_offset = iph->ihl * 4;
l3 = (u_int8_t*)iph; l3 = (u_int8_t*)iph;
} else { } else {
l4_offset = sizeof(struct ndpi_ip6_hdr); l4_offset = sizeof(struct ndpi_ipv6hdr);
l3 = (u_int8_t*)iph6; l3 = (u_int8_t*)iph6;
} }
@@ -768,18 +779,54 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id,
} }
*proto = iph->protocol; *proto = iph->protocol;
l4 = ((u_int8_t *) l3 + l4_offset);
if(iph->protocol == 6 && l4_packet_len >= 20) { if(iph->protocol == 6 && l4_packet_len >= 20) {
u_int tcp_len;
ndpi_thread_info[thread_id].stats.tcp_count++; ndpi_thread_info[thread_id].stats.tcp_count++;
// tcp // tcp
tcph = (struct ndpi_tcphdr *) ((u_int8_t *) l3 + l4_offset); *tcph = (struct ndpi_tcphdr *)l4;
*sport = ntohs((*tcph)->source), *dport = ntohs((*tcph)->dest);
if(iph->saddr < iph->daddr) { if(iph->saddr < iph->daddr) {
lower_port = tcph->source; lower_port = (*tcph)->source, upper_port = (*tcph)->dest;
upper_port = tcph->dest; *src_to_dst_direction = 1;
} else { } else {
lower_port = tcph->dest; lower_port = (*tcph)->dest;
upper_port = tcph->source; upper_port = (*tcph)->source;
*src_to_dst_direction = 0;
if(iph->saddr == iph->daddr) {
if(lower_port > upper_port) {
u_int16_t p = lower_port;
lower_port = upper_port;
upper_port = p;
}
}
}
tcp_len = ndpi_min(4*(*tcph)->doff, l4_packet_len);
*payload = &l4[tcp_len];
*payload_len = ndpi_max(0, l4_packet_len-4*(*tcph)->doff);
} else if(iph->protocol == 17 && l4_packet_len >= 8) {
// udp
ndpi_thread_info[thread_id].stats.udp_count++;
*udph = (struct ndpi_udphdr *)l4;
*sport = ntohs((*udph)->source), *dport = ntohs((*udph)->dest);
*payload = &l4[sizeof(struct ndpi_udphdr)];
*payload_len = ndpi_max(0, l4_packet_len-sizeof(struct ndpi_udphdr));
if(iph->saddr < iph->daddr) {
lower_port = (*udph)->source, upper_port = (*udph)->dest;
*src_to_dst_direction = 1;
} else {
lower_port = (*udph)->dest, upper_port = (*udph)->source;
*src_to_dst_direction = 0;
if(iph->saddr == iph->daddr) { if(iph->saddr == iph->daddr) {
if(lower_port > upper_port) { if(lower_port > upper_port) {
@@ -790,18 +837,8 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id,
} }
} }
} }
} else if(iph->protocol == 17 && l4_packet_len >= 8) {
// udp
ndpi_thread_info[thread_id].stats.udp_count++;
udph = (struct ndpi_udphdr *) ((u_int8_t *) l3 + l4_offset); *sport = ntohs(lower_port), *dport = ntohs(upper_port);
if(iph->saddr < iph->daddr) {
lower_port = udph->source;
upper_port = udph->dest;
} else {
lower_port = udph->dest;
upper_port = udph->source;
}
} else { } else {
// non tcp/udp protocols // non tcp/udp protocols
lower_port = 0; lower_port = 0;
@@ -835,6 +872,7 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id,
newflow->protocol = iph->protocol, newflow->vlan_id = vlan_id; newflow->protocol = iph->protocol, newflow->vlan_id = vlan_id;
newflow->lower_ip = lower_ip, newflow->upper_ip = upper_ip; newflow->lower_ip = lower_ip, newflow->upper_ip = upper_ip;
newflow->lower_port = lower_port, newflow->upper_port = upper_port; newflow->lower_port = lower_port, newflow->upper_port = upper_port;
newflow->ip_version = version;
if(version == 4) { if(version == 4) {
inet_ntop(AF_INET, &lower_ip, newflow->lower_name, sizeof(newflow->lower_name)); inet_ntop(AF_INET, &lower_ip, newflow->lower_name, sizeof(newflow->lower_name));
@@ -871,7 +909,6 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id,
*src = newflow->src_id, *dst = newflow->dst_id; *src = newflow->src_id, *dst = newflow->dst_id;
// printFlow(thread_id, newflow); // printFlow(thread_id, newflow);
return newflow; return newflow;
} }
} else { } else {
@@ -891,29 +928,36 @@ static struct ndpi_flow *get_ndpi_flow(u_int16_t thread_id,
static struct ndpi_flow *get_ndpi_flow6(u_int16_t thread_id, static struct ndpi_flow *get_ndpi_flow6(u_int16_t thread_id,
u_int16_t vlan_id, u_int16_t vlan_id,
const struct ndpi_ip6_hdr *iph6, const struct ndpi_ipv6hdr *iph6,
u_int16_t ip_offset, u_int16_t ip_offset,
struct ndpi_tcphdr **tcph,
struct ndpi_udphdr **udph,
u_int16_t *sport, u_int16_t *dport,
struct ndpi_id_struct **src, struct ndpi_id_struct **src,
struct ndpi_id_struct **dst, struct ndpi_id_struct **dst,
u_int8_t *proto) { u_int8_t *proto,
u_int8_t **payload,
u_int16_t *payload_len,
u_int8_t *src_to_dst_direction) {
struct ndpi_iphdr iph; struct ndpi_iphdr iph;
memset(&iph, 0, sizeof(iph)); memset(&iph, 0, sizeof(iph));
iph.version = 4; iph.version = 4;
iph.saddr = iph6->ip6_src.__u6_addr.__u6_addr32[2] + iph6->ip6_src.__u6_addr.__u6_addr32[3]; iph.saddr = iph6->ip6_src.u6_addr.u6_addr32[2] + iph6->ip6_src.u6_addr.u6_addr32[3];
iph.daddr = iph6->ip6_dst.__u6_addr.__u6_addr32[2] + iph6->ip6_dst.__u6_addr.__u6_addr32[3]; iph.daddr = iph6->ip6_dst.u6_addr.u6_addr32[2] + iph6->ip6_dst.u6_addr.u6_addr32[3];
iph.protocol = iph6->ip6_ctlun.ip6_un1.ip6_un1_nxt; iph.protocol = iph6->ip6_ctlun.ip6_un1.ip6_un1_nxt;
if(iph.protocol == 0x3C /* IPv6 destination option */) { if(iph.protocol == 0x3C /* IPv6 destination option */) {
u_int8_t *options = (u_int8_t*)iph6 + sizeof(const struct ndpi_ip6_hdr); u_int8_t *options = (u_int8_t*)iph6 + sizeof(const struct ndpi_ipv6hdr);
iph.protocol = options[0]; iph.protocol = options[0];
} }
return(get_ndpi_flow(thread_id, 6, vlan_id, &iph, ip_offset, return(get_ndpi_flow(thread_id, 6, vlan_id, &iph, iph6, ip_offset,
sizeof(struct ndpi_ip6_hdr), sizeof(struct ndpi_ipv6hdr),
ntohs(iph6->ip6_ctlun.ip6_un1.ip6_un1_plen), ntohs(iph6->ip6_ctlun.ip6_un1.ip6_un1_plen),
src, dst, proto, iph6)); tcph, udph, sport, dport,
src, dst, proto, payload, payload_len, src_to_dst_direction));
} }
/* ***************************************************** */ /* ***************************************************** */
@@ -938,8 +982,8 @@ static void setupDetection(u_int16_t thread_id) {
ndpi_set_protocol_detection_bitmask2(ndpi_thread_info[thread_id].ndpi_struct, &all); ndpi_set_protocol_detection_bitmask2(ndpi_thread_info[thread_id].ndpi_struct, &all);
// allocate memory for id and flow tracking // allocate memory for id and flow tracking
size_id_struct = ndpi_detection_get_sizeof_ndpi_id_struct(); size_id_struct = sizeof(struct ndpi_id_struct);
size_flow_struct = ndpi_detection_get_sizeof_ndpi_flow_struct(); size_flow_struct = sizeof(struct ndpi_flow_struct);
// clear memory for results // clear memory for results
memset(ndpi_thread_info[thread_id].stats.protocol_counter, 0, sizeof(ndpi_thread_info[thread_id].stats.protocol_counter)); memset(ndpi_thread_info[thread_id].stats.protocol_counter, 0, sizeof(ndpi_thread_info[thread_id].stats.protocol_counter));
@@ -970,20 +1014,31 @@ static unsigned int packet_processing(u_int16_t thread_id,
const u_int64_t time, const u_int64_t time,
u_int16_t vlan_id, u_int16_t vlan_id,
const struct ndpi_iphdr *iph, const struct ndpi_iphdr *iph,
struct ndpi_ip6_hdr *iph6, struct ndpi_ipv6hdr *iph6,
u_int16_t ip_offset, u_int16_t ip_offset,
u_int16_t ipsize, u_int16_t rawsize) { u_int16_t ipsize, u_int16_t rawsize) {
struct ndpi_id_struct *src, *dst; struct ndpi_id_struct *src, *dst;
struct ndpi_flow *flow; struct ndpi_flow *flow;
struct ndpi_flow_struct *ndpi_flow = NULL; struct ndpi_flow_struct *ndpi_flow = NULL;
u_int8_t proto; u_int8_t proto;
struct ndpi_tcphdr *tcph = NULL;
struct ndpi_udphdr *udph = NULL;
u_int16_t sport, dport, payload_len;
u_int8_t *payload;
u_int8_t src_to_dst_direction= 1;
if(iph) if(iph)
flow = get_ndpi_flow(thread_id, 4, vlan_id, iph, ip_offset, ipsize, flow = get_ndpi_flow(thread_id, 4, vlan_id, iph, NULL,
ip_offset, ipsize,
ntohs(iph->tot_len) - (iph->ihl * 4), ntohs(iph->tot_len) - (iph->ihl * 4),
&src, &dst, &proto, NULL); &tcph, &udph, &sport, &dport,
&src, &dst, &proto,
&payload, &payload_len, &src_to_dst_direction);
else else
flow = get_ndpi_flow6(thread_id, vlan_id, iph6, ip_offset, &src, &dst, &proto); flow = get_ndpi_flow6(thread_id, vlan_id, iph6, ip_offset,
&tcph, &udph, &sport, &dport,
&src, &dst, &proto,
&payload, &payload_len, &src_to_dst_direction);
if(flow != NULL) { if(flow != NULL) {
ndpi_thread_info[thread_id].stats.ip_packet_count++; ndpi_thread_info[thread_id].stats.ip_packet_count++;
@@ -1016,31 +1071,6 @@ static unsigned int packet_processing(u_int16_t thread_id,
snprintf(flow->ssl.server_certificate, sizeof(flow->ssl.server_certificate), "%s", flow->ndpi_flow->protos.ssl.server_certificate); snprintf(flow->ssl.server_certificate, sizeof(flow->ssl.server_certificate), "%s", flow->ndpi_flow->protos.ssl.server_certificate);
} }
#if 0
if(verbose > 1) {
if(ndpi_is_proto(flow->detected_protocol, NDPI_PROTOCOL_HTTP)) {
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));
switch(ndpi_get_http_method(ndpi_thread_info[thread_id].ndpi_struct, ndpi_flow)) {
case HTTP_METHOD_OPTIONS: method = "HTTP_METHOD_OPTIONS"; break;
case HTTP_METHOD_GET: method = "HTTP_METHOD_GET"; break;
case HTTP_METHOD_HEAD: method = "HTTP_METHOD_HEAD"; break;
case HTTP_METHOD_POST: method = "HTTP_METHOD_POST"; break;
case HTTP_METHOD_PUT: method = "HTTP_METHOD_PUT"; break;
case HTTP_METHOD_DELETE: method = "HTTP_METHOD_DELETE"; break;
case HTTP_METHOD_TRACE: method = "HTTP_METHOD_TRACE"; break;
case HTTP_METHOD_CONNECT: method = "HTTP_METHOD_CONNECT"; break;
default: method = "HTTP_METHOD_UNKNOWN"; break;
}
printf("[Method] %s\n", method);
}
}
#endif
free_ndpi_flow(flow); free_ndpi_flow(flow);
if(verbose > 1) { if(verbose > 1) {
@@ -1055,11 +1085,6 @@ static unsigned int packet_processing(u_int16_t thread_id,
} }
} }
#if 0
if(ndpi_flow->l4.tcp.host_server_name[0] != '\0')
printf("%s\n", ndpi_flow->l4.tcp.host_server_name);
#endif
if(live_capture) { if(live_capture) {
if(ndpi_thread_info[thread_id].last_idle_scan_time + IDLE_SCAN_PERIOD < ndpi_thread_info[thread_id].last_time) { if(ndpi_thread_info[thread_id].last_idle_scan_time + IDLE_SCAN_PERIOD < ndpi_thread_info[thread_id].last_time) {
/* scan for idle flows */ /* scan for idle flows */
@@ -1525,86 +1550,229 @@ static void openPcapFileOrDevice(u_int16_t thread_id) {
/* ***************************************************** */ /* ***************************************************** */
static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { static void pcap_packet_callback(u_char *args,
const struct pcap_pkthdr *header,
const u_char *packet) {
/*
* Declare pointers to packet headers
*/
/* --- Ethernet header --- */
const struct ndpi_ethhdr *ethernet; const struct ndpi_ethhdr *ethernet;
/* --- Ethernet II header --- */
const struct ndpi_ethhdr *ethernet_2;
/* --- LLC header --- */
const struct ndpi_llc_header *llc;
/* --- Cisco HDLC header --- */
const struct ndpi_chdlc *chdlc;
/* --- SLARP frame --- */
struct ndpi_slarp *slarp;
/* --- CDP --- */
struct ndpi_cdp *cdp;
/* --- Radio Tap header --- */
const struct ndpi_radiotap_header *radiotap;
/* --- Wifi header --- */
const struct ndpi_wifi_header *wifi;
/* --- MPLS header --- */
struct ndpi_mpls_header *mpls;
/** --- IP header --- **/
struct ndpi_iphdr *iph; struct ndpi_iphdr *iph;
struct ndpi_ip6_hdr *iph6; /** --- IPv6 header --- **/
struct ndpi_ipv6hdr *iph6;
/* lengths and offsets */
u_int16_t eth_offset = 0;
u_int16_t radio_len;
u_int16_t fc;
u_int16_t type;
int wifi_len;
int llc_off;
int pyld_eth_len = 0;
int check;
u_int32_t fcs;
u_int64_t time; u_int64_t time;
u_int16_t type, ip_offset, ip_len; u_int16_t ip_offset, ip_len, ip6_offset;
u_int16_t frag_off = 0, vlan_id = 0; u_int16_t frag_off = 0, vlan_id = 0;
u_int8_t proto = 0, vlan_packet = 0; u_int8_t proto = 0;
u_int32_t label;
u_int16_t thread_id = *((u_int16_t*)args); u_int16_t thread_id = *((u_int16_t*)args);
// printf("[ndpiReader] pcap_packet_callback : [%u.%u.%u.%u.%u -> %u.%u.%u.%u.%u]\n", ethernet->h_dest[1],ethernet->h_dest[2],ethernet->h_dest[3],ethernet->h_dest[4],ethernet->h_dest[5],ethernet->h_source[1],ethernet->h_source[2],ethernet->h_source[3],ethernet->h_source[4],ethernet->h_source[5]); /* counters */
u_int8_t malformed_pkts = 0, vlan_packet = 0;
u_int8_t slarp_pkts = 0, cdp_pkts = 0;
/* Increment raw packet counter */
ndpi_thread_info[thread_id].stats.raw_packet_count++; ndpi_thread_info[thread_id].stats.raw_packet_count++;
if((capture_until != 0) && (header->ts.tv_sec >= capture_until)) { if((capture_until != 0) && (header->ts.tv_sec >= capture_until)) {
if(ndpi_thread_info[thread_id]._pcap_handle != NULL) if(ndpi_thread_info[thread_id]._pcap_handle != NULL)
pcap_breakloop(ndpi_thread_info[thread_id]._pcap_handle); pcap_breakloop(ndpi_thread_info[thread_id]._pcap_handle);
return; return;
} }
/* Check if capture is live or not */
if (!live_capture) { if (!live_capture) {
if (!pcap_start.tv_sec) pcap_start.tv_sec = header->ts.tv_sec, pcap_start.tv_usec = header->ts.tv_usec; if (!pcap_start.tv_sec) pcap_start.tv_sec = header->ts.tv_sec, pcap_start.tv_usec = header->ts.tv_usec;
pcap_end.tv_sec = header->ts.tv_sec, pcap_end.tv_usec = header->ts.tv_usec; pcap_end.tv_sec = header->ts.tv_sec, pcap_end.tv_usec = header->ts.tv_usec;
} }
/* setting time */
time = ((uint64_t) header->ts.tv_sec) * detection_tick_resolution + time = ((uint64_t) header->ts.tv_sec) * detection_tick_resolution +
header->ts.tv_usec / (1000000 / detection_tick_resolution); header->ts.tv_usec / (1000000 / detection_tick_resolution);
if(ndpi_thread_info[thread_id].last_time > time) { /* safety check */ /* safety check */
// printf("\nWARNING: timestamp bug in the pcap file (ts delta: %llu, repairing)\n", ndpi_thread_info[thread_id].last_time - time); if(ndpi_thread_info[thread_id].last_time > time) {
/* printf("\nWARNING: timestamp bug in the pcap file (ts delta: %llu, repairing)\n", ndpi_thread_info[thread_id].last_time - time); */
time = ndpi_thread_info[thread_id].last_time; time = ndpi_thread_info[thread_id].last_time;
} }
/* update last time value */
ndpi_thread_info[thread_id].last_time = time; ndpi_thread_info[thread_id].last_time = time;
if(ndpi_thread_info[thread_id]._pcap_datalink_type == DLT_NULL) { /*** check Data Link type ***/
if(ntohl(*((u_int32_t*)packet)) == 2) int datalink_type = ndpi_thread_info[thread_id]._pcap_datalink_type;
datalink_check:
switch(datalink_type) {
case DLT_NULL :
if(ntohl(*((u_int32_t*)&packet[eth_offset])) == 2)
type = ETH_P_IP; type = ETH_P_IP;
else else
type = 0x86DD; /* IPv6 */ type = ETH_P_IPV6;
ip_offset = 4; ip_offset = 4 + eth_offset;
} else if(ndpi_thread_info[thread_id]._pcap_datalink_type == DLT_EN10MB) {
ethernet = (struct ndpi_ethhdr *) packet; /* Cisco PPP in HDLC-like framing - 50 */
ip_offset = sizeof(struct ndpi_ethhdr); case DLT_PPP_SERIAL:
type = ntohs(ethernet->h_proto); chdlc = (struct ndpi_chdlc *) &packet[eth_offset];
} else if(ndpi_thread_info[thread_id]._pcap_datalink_type == 113 /* Linux Cooked Capture */) { ip_offset = sizeof(struct ndpi_chdlc); /* CHDLC_OFF = 4 */
type = (packet[14] << 8) + packet[15]; type = ntohs(chdlc->proto_code);
ip_offset = 16; break;
} else
/* Cisco PPP with HDLC framing - 104 */
case DLT_C_HDLC:
chdlc = (struct ndpi_chdlc *) &packet[eth_offset];
ip_offset = sizeof(struct ndpi_chdlc); /* CHDLC_OFF = 4 */
type = ntohs(chdlc->proto_code);
break;
/* IEEE 802.3 Ethernet - 1 */
case DLT_EN10MB :
ethernet = (struct ndpi_ethhdr *) &packet[eth_offset];
ip_offset = sizeof(struct ndpi_ethhdr) + eth_offset;
check = ntohs(ethernet->h_proto);
if(check <= 1500)
pyld_eth_len = check;
else if (check >= 1536)
type = check;
if(pyld_eth_len != 0) {
/* check for LLC layer with SNAP extension */
if(packet[ip_offset] == SNAP) {
llc = (struct ndpi_llc_header *)(&packet[ip_offset]);
type = llc->snap.proto_ID;
ip_offset += + 8;
}
}
break;
/* Linux Cooked Capture - 113 */
case DLT_LINUX_SLL :
type = (packet[eth_offset+14] << 8) + packet[eth_offset+15];
ip_offset = 16 + eth_offset;
break;
/* Radiotap link-layer - 127 */
case DLT_IEEE802_11_RADIO :
radiotap = (struct ndpi_radiotap_header *) &packet[eth_offset];
radio_len = radiotap->len;
/* Check Bad FCS presence */
if((radiotap->flags & BAD_FCS) == BAD_FCS) {
malformed_pkts += 1;
ndpi_thread_info[thread_id].stats.total_discarded_bytes += header->len;
return; return;
}
while(1) { fcs = header->len - 4;
if(type == 0x8100 /* VLAN */) {
/* Calculate 802.11 header length (variable) */
wifi = (struct ndpi_wifi_header*)( packet + eth_offset + radio_len);
fc = wifi->fc;
/* check wifi data presence */
if(FCF_TYPE(fc) == WIFI_DATA) {
if((FCF_TO_DS(fc) && FCF_FROM_DS(fc) == 0x0) ||
(FCF_TO_DS(fc) == 0x0 && FCF_FROM_DS(fc)))
wifi_len = 26; /* + 4 byte fcs */
} else /* no data frames */
break;
/* Check ether_type from LLC */
llc = (struct ndpi_llc_header*)(packet + eth_offset + wifi_len + radio_len);
if(llc->dsap == SNAP)
type = ntohs(llc->snap.proto_ID);
/* Set IP header offset */
ip_offset = wifi_len + radio_len + sizeof(struct ndpi_llc_header) + eth_offset;
break;
default:
return;
}
/* check ether type */
if(type == VLAN) {
vlan_id = ((packet[ip_offset] << 8) + packet[ip_offset+1]) & 0xFFF; vlan_id = ((packet[ip_offset] << 8) + packet[ip_offset+1]) & 0xFFF;
type = (packet[ip_offset+2] << 8) + packet[ip_offset+3]; type = (packet[ip_offset+2] << 8) + packet[ip_offset+3];
ip_offset += 4; ip_offset += 4;
vlan_packet = 1; vlan_packet = 1;
} else if(type == 0x8847 /* MPLS */) { }
u_int32_t label = ntohl(*((u_int32_t*)&packet[ip_offset])); else if(type == MPLS_UNI || type == MPLS_MULTI) {
mpls = (struct ndpi_mpls_header *) &packet[ip_offset];
label = ntohl(mpls->label);
/* label = ntohl(*((u_int32_t*)&packet[ip_offset])); */
ndpi_thread_info[thread_id].stats.mpls_count++; ndpi_thread_info[thread_id].stats.mpls_count++;
type = 0x800, ip_offset += 4; type = ETH_P_IP, ip_offset += 4;
while((label & 0x100) != 0x100) { while((label & 0x100) != 0x100) {
ip_offset += 4; ip_offset += 4;
label = ntohl(*((u_int32_t*)&packet[ip_offset])); label = ntohl(mpls->label);
} }
} else if(type == 0x8864 /* PPPoE */) { }
else if(type == SLARP) {
slarp = (struct ndpi_slarp *) &packet[ip_offset];
if(slarp->slarp_type == 0x02 || slarp->slarp_type == 0x00 || slarp->slarp_type == 0x01) {
/* TODO if info are needed */
}
slarp_pkts++;
}
else if(type == CISCO_D_PROTO) {
cdp = (struct ndpi_cdp *) &packet[ip_offset];
cdp_pkts++;
}
else if(type == PPPoE) {
ndpi_thread_info[thread_id].stats.pppoe_count++; ndpi_thread_info[thread_id].stats.pppoe_count++;
type = 0x0800; type = ETH_P_IP;
ip_offset += 8; ip_offset += 8;
} else
break;
} }
ndpi_thread_info[thread_id].stats.vlan_count += vlan_packet; ndpi_thread_info[thread_id].stats.vlan_count += vlan_packet;
iph_check:
/* Check and set IP header size and total packet length */
iph = (struct ndpi_iphdr *) &packet[ip_offset]; iph = (struct ndpi_iphdr *) &packet[ip_offset];
// just work on Ethernet packets that contain IP /* just work on Ethernet packets that contain IP */
if(type == ETH_P_IP && header->caplen >= ip_offset) { if(type == ETH_P_IP && header->caplen >= ip_offset) {
frag_off = ntohs(iph->frag_off); frag_off = ntohs(iph->frag_off);
@@ -1623,10 +1791,15 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
ip_len = ((u_short)iph->ihl * 4); ip_len = ((u_short)iph->ihl * 4);
iph6 = NULL; iph6 = NULL;
if(iph->protocol == 41) {
ip_offset += ip_len;
goto iph_check;
}
if((frag_off & 0x3FFF) != 0) { if((frag_off & 0x3FFF) != 0) {
static u_int8_t ipv4_frags_warning_used = 0; static u_int8_t ipv4_frags_warning_used = 0;
ndpi_thread_info[thread_id].stats.fragmented_count++; ndpi_thread_info[thread_id].stats.fragmented_count++;
if(ipv4_frags_warning_used == 0) { if(ipv4_frags_warning_used == 0) {
if((!json_flag) && (!quiet_mode)) printf("\n\nWARNING: IPv4 fragments are not handled by this demo (nDPI supports them)\n"); if((!json_flag) && (!quiet_mode)) printf("\n\nWARNING: IPv4 fragments are not handled by this demo (nDPI supports them)\n");
ipv4_frags_warning_used = 1; ipv4_frags_warning_used = 1;
@@ -1636,27 +1809,27 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
return; return;
} }
} else if(iph->version == 6) { } else if(iph->version == 6) {
iph6 = (struct ndpi_ip6_hdr *)&packet[ip_offset]; iph6 = (struct ndpi_ipv6hdr *)&packet[ip_offset];
proto = iph6->ip6_ctlun.ip6_un1.ip6_un1_nxt; proto = iph6->ip6_ctlun.ip6_un1.ip6_un1_nxt;
ip_len = sizeof(struct ndpi_ip6_hdr); ip_len = sizeof(struct ndpi_ipv6hdr);
if(proto == 0x3C /* IPv6 destination option */) { if(proto == 0x3C /* IPv6 destination option */) {
u_int8_t *options = (u_int8_t*)&packet[ip_offset+ip_len];
u_int8_t *options = (u_int8_t*)&packet[ip_offset+ip_len];
proto = options[0]; proto = options[0];
ip_len += 8 * (options[1] + 1); ip_len += 8 * (options[1] + 1);
} }
iph = NULL; iph = NULL;
} else { } else {
static u_int8_t ipv4_warning_used = 0; static u_int8_t ipv4_warning_used = 0;
v4_warning: v4_warning:
if(ipv4_warning_used == 0) { if(ipv4_warning_used == 0) {
if((!json_flag) && (!quiet_mode)) printf("\n\nWARNING: only IPv4/IPv6 packets are supported in this demo (nDPI supports both IPv4 and IPv6), all other packets will be discarded\n\n"); if((!json_flag) && (!quiet_mode))
printf("\n\nWARNING: only IPv4/IPv6 packets are supported in this demo (nDPI supports both IPv4 and IPv6), all other packets will be discarded\n\n");
ipv4_warning_used = 1; ipv4_warning_used = 1;
} }
ndpi_thread_info[thread_id].stats.total_discarded_bytes += header->len; ndpi_thread_info[thread_id].stats.total_discarded_bytes += header->len;
return; return;
} }
@@ -1671,9 +1844,10 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
u_int8_t flags = packet[offset]; u_int8_t flags = packet[offset];
u_int8_t message_type = packet[offset+1]; u_int8_t message_type = packet[offset+1];
if((((flags & 0xE0) >> 5) == 1 /* GTPv1 */) && (message_type == 0xFF /* T-PDU */)) { if((((flags & 0xE0) >> 5) == 1 /* GTPv1 */) &&
ip_offset = ip_offset+ip_len+sizeof(struct ndpi_udphdr)+8 /* GTPv1 header len */; (message_type == 0xFF /* T-PDU */)) {
ip_offset = ip_offset+ip_len+sizeof(struct ndpi_udphdr)+8; /* GTPv1 header len */
if(flags & 0x04) ip_offset += 1; /* next_ext_header is present */ if(flags & 0x04) ip_offset += 1; /* next_ext_header is present */
if(flags & 0x02) ip_offset += 4; /* sequence_number is present (it also includes next_ext_header and pdu_number) */ if(flags & 0x02) ip_offset += 4; /* sequence_number is present (it also includes next_ext_header and pdu_number) */
if(flags & 0x01) ip_offset += 1; /* pdu_number is present */ if(flags & 0x01) ip_offset += 1; /* pdu_number is present */
@@ -1685,10 +1859,48 @@ static void pcap_packet_callback(u_char *args, const struct pcap_pkthdr *header,
goto v4_warning; goto v4_warning;
} }
} }
} else if((sport == TZSP_PORT) || (dport == TZSP_PORT)) {
/* https://en.wikipedia.org/wiki/TZSP */
u_int offset = ip_offset+ip_len+sizeof(struct ndpi_udphdr);
u_int8_t version = packet[offset];
u_int8_t type = packet[offset+1];
u_int16_t encapsulates = ntohs(*((u_int16_t*)&packet[offset+2]));
if((version == 1) && (type == 0) && (encapsulates == 1)) {
u_int8_t stop = 0;
offset += 4;
while((!stop) && (offset < header->caplen)) {
u_int8_t tag_type = packet[offset];
u_int8_t tag_len;
switch(tag_type) {
case 0: /* PADDING Tag */
tag_len = 1;
break;
case 1: /* END Tag */
tag_len = 1, stop = 1;
break;
default:
tag_len = packet[offset+1];
break;
}
offset += tag_len;
if(offset >= header->caplen)
return; /* Invalid packet */
else {
eth_offset = offset;
goto datalink_check;
}
}
}
} }
} }
// process the packet /* process the packet */
packet_processing(thread_id, time, vlan_id, iph, iph6, packet_processing(thread_id, time, vlan_id, iph, iph6,
ip_offset, header->len - ip_offset, header->len); ip_offset, header->len - ip_offset, header->len);
} }
@@ -1823,16 +2035,6 @@ struct timezone {
/* ***************************************************** */ /* ***************************************************** */
#if 0
int gettimeofday(struct timeval *tv, void *notUsed) {
tv->tv_sec = time(NULL);
tv->tv_usec = 0;
return(0);
}
#endif
/* ***************************************************** */
int gettimeofday(struct timeval *tv, struct timezone *tz) { int gettimeofday(struct timeval *tv, struct timezone *tz) {
FILETIME ft; FILETIME ft;
LARGE_INTEGER li; LARGE_INTEGER li;
+12 -5
View File
@@ -1,9 +1,15 @@
require "formula"
class Ndpi < Formula class Ndpi < Formula
desc "Deep Packet Inspection (DPI) library"
homepage "http://www.ntop.org/products/ndpi/" homepage "http://www.ntop.org/products/ndpi/"
url "https://downloads.sourceforge.net/project/ntop/nDPI/libndpi-1.5.1.tar.gz" url "https://downloads.sourceforge.net/project/ntop/nDPI/nDPI-1.7.tar.gz"
sha1 "0a6ed585545ab6611f3f0ac9efd9eb36bb5481dd" sha256 "714b745103a072462130b0e14cf31b2eb5270f580b7c839da5cf5ea75150262d"
bottle do
cellar :any
sha256 "e9464d314479ba3e7a91422e0bc606cfd5f6e72e94d6441cc4fa30e9c925da5c" => :yosemite
sha256 "1d6b1d860669b42766baa276ed948c342e2fa4fd28663ba64a90fd0e200ba9c4" => :mavericks
sha256 "b814918b4fb9588de7126061ce4ac3eb41a5c3eee27c7432b669f6dc6921bfde" => :mountain_lion
end
depends_on "autoconf" => :build depends_on "autoconf" => :build
depends_on "automake" => :build depends_on "automake" => :build
@@ -12,12 +18,13 @@ class Ndpi < Formula
depends_on "json-c" depends_on "json-c"
def install def install
system "./autogen.sh"
system "./configure", "--prefix=#{prefix}" system "./configure", "--prefix=#{prefix}"
system "make" system "make"
system "make", "install" system "make", "install"
end end
test do test do
system "#{bin}/ndpiReader","-h" system "#{bin}/ndpiReader", "-i", test_fixtures("test.pcap")
end end
end end
+6 -5
View File
@@ -1,9 +1,10 @@
library_includedir=$(includedir)/libndpi-1.4/libndpi library_includedir=$(includedir)/libndpi-1.4/libndpi
library_include_HEADERS = ndpi_api.h \ library_include_HEADERS = ndpi_api.h \
ndpi_debug_functions.h \
ndpi_define.h \ ndpi_define.h \
ndpi_macros.h \ ndpi_typedefs.h \
ndpi_protocols_osdpi.h \ ndpi_main.h \
ndpi_public_functions.h ndpi_protocol_ids.h \
ndpi_protocols.h \
ndpi_win32.h \
ndpi_includes.h
-188
View File
@@ -1,188 +0,0 @@
/*
* linux_compat.h
*
* Copyright (C) 2009-2011 by ipoque GmbH
* Copyright (C) 2011-15 - ntop.org
*
* This file is part of nDPI, an open source deep packet inspection
* library based on the OpenDPI and PACE technology by ipoque GmbH
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#ifndef __NDPI_LINUX_COMPAT_H__
#define __NDPI_LINUX_COMPAT_H__
#include "ndpi_define.h"
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#include <machine/endian.h>
#if _BYTE_ORDER == _LITTLE_ENDIAN
#ifndef __LITTLE_ENDIAN__
#define __LITTLE_ENDIAN__ 1
#endif
#else
#ifndef __BIG_ENDIAN__
#define __BIG_ENDIAN__ 1
#endif
#endif
#endif
#pragma pack(push, 1) /* push current alignment to stack */
#pragma pack(1) /* set alignment to 1 byte boundary */
#pragma pack(pop) /* restore original alignment from stack */
struct ndpi_ethhdr {
u_char h_dest[6]; /* destination eth addr */
u_char h_source[6]; /* source ether addr */
u_int16_t h_proto; /* packet type ID field */
};
struct ndpi_80211q {
u_int16_t vlanId;
u_int16_t protoType;
};
struct ndpi_iphdr {
#if defined(__LITTLE_ENDIAN__)
u_int8_t ihl:4, version:4;
#elif defined(__BIG_ENDIAN__)
u_int8_t version:4, ihl:4;
#else
# error "Byte order must be defined"
#endif
u_int8_t tos;
u_int16_t tot_len;
u_int16_t id;
u_int16_t frag_off;
u_int8_t ttl;
u_int8_t protocol;
u_int16_t check;
u_int32_t saddr;
u_int32_t daddr;
};
#ifdef WIN32
typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned int uint;
typedef unsigned long u_long;
typedef u_char u_int8_t;
typedef u_short u_int16_t;
typedef uint u_int32_t;
#define _WS2TCPIP_H_ /* Avoid compilation problems */
#define HAVE_SIN6_LEN
/* IPv6 address */
/* Already defined in WS2tcpip.h */
struct ndpi_win_in6_addr
{
union
{
u_int8_t u6_addr8[16];
u_int16_t u6_addr16[8];
u_int32_t u6_addr32[4];
} in6_u;
#ifdef s6_addr
#undef s6_addr
#endif
#ifdef s6_addr16
#undef s6_addr16
#endif
#ifdef s6_addr32
#undef s6_addr32
#endif
#define s6_addr in6_u.u6_addr8
// #define s6_addr16 in6_u.u6_addr16
// #define s6_addr32 in6_u.u6_addr32
};
#define in6_addr win_in6_addr
/* Generic extension header. */
struct ndpi_ip6_ext
{
u_int8_t ip6e_nxt; /* next header. */
u_int8_t ip6e_len; /* length in units of 8 octets. */
};
#define s6_addr16 __u6_addr.__u6_addr16
#define s6_addr32 __u6_addr.__u6_addr32
#else
#ifndef __KERNEL__
#include <arpa/inet.h>
#endif
#endif
struct ndpi_in6_addr {
union {
u_int8_t __u6_addr8[16];
u_int16_t __u6_addr16[8];
u_int32_t __u6_addr32[4];
} __u6_addr; /* 128-bit IP6 address */
};
struct ndpi_ip6_hdr {
union {
struct ndpi_ip6_hdrctl {
u_int32_t ip6_un1_flow;
u_int16_t ip6_un1_plen;
u_int8_t ip6_un1_nxt;
u_int8_t ip6_un1_hlim;
} ip6_un1;
u_int8_t ip6_un2_vfc;
} ip6_ctlun;
struct ndpi_in6_addr ip6_src;
struct ndpi_in6_addr ip6_dst;
};
struct ndpi_tcphdr {
u_int16_t source;
u_int16_t dest;
u_int32_t seq;
u_int32_t ack_seq;
#if defined(__LITTLE_ENDIAN__)
u_int16_t res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, ece:1, cwr:1;
#elif defined(__BIG_ENDIAN__)
u_int16_t doff:4, res1:4, cwr:1, ece:1, urg:1, ack:1, psh:1, rst:1, syn:1, fin:1;
#else
# error "Byte order must be defined"
#endif
u_int16_t window;
u_int16_t check;
u_int16_t urg_ptr;
};
struct ndpi_udphdr {
u_int16_t source;
u_int16_t dest;
u_int16_t len;
u_int16_t check;
};
#endif
+9 -16
View File
@@ -23,8 +23,8 @@
*/ */
#ifndef __NDPI_PUBLIC_FUNCTIONS_H__ #ifndef __NDPI_API_H__
#define __NDPI_PUBLIC_FUNCTIONS_H__ #define __NDPI_API_H__
#include "ndpi_main.h" #include "ndpi_main.h"
@@ -44,14 +44,14 @@ extern "C" {
*/ */
u_int32_t ndpi_detection_get_sizeof_ndpi_id_struct(void); u_int32_t ndpi_detection_get_sizeof_ndpi_id_struct(void);
/* Public malloc/free */ /* Public malloc/free */
void* ndpi_malloc(unsigned long size); void* ndpi_malloc(unsigned long size);
void* ndpi_calloc(unsigned long count, unsigned long size); void* ndpi_calloc(unsigned long count, unsigned long size);
void ndpi_free(void *ptr); void ndpi_free(void *ptr);
void *ndpi_realloc(void *ptr, size_t old_size, size_t new_size); void *ndpi_realloc(void *ptr, size_t old_size, size_t new_size);
char *ndpi_strdup(const char *s); char *ndpi_strdup(const char *s);
/*
/**
* Find the first occurrence of find in s, where the search is limited to the * Find the first occurrence of find in s, where the search is limited to the
* first slen characters of s. * first slen characters of s.
*/ */
@@ -70,7 +70,7 @@ extern "C" {
/** /**
* This function returns a new initialized detection module. * This function returns a new initialized detection module.
* @param ticks_per_second the timestamp resolution per second (like 1000 for millisecond resolution) * @param ticks_per_second the timestamp resolution per second (like 1000 for millisecond resolution)
* @param ndpi_malloc function pointer to a memory allocator * @param __ndpi_malloc function pointer to a memory allocator
* @param ndpi_debug_printf a function pointer to a debug output function, use NULL in productive envionments * @param ndpi_debug_printf a function pointer to a debug output function, use NULL in productive envionments
* @return the initialized detection module * @return the initialized detection module
*/ */
@@ -88,8 +88,8 @@ extern "C" {
/** /**
* This function enables cache support in nDPI used for some protocol such as Skype * This function enables cache support in nDPI used for some protocol such as Skype
* @param cache host name * @param host host name
* @param cache port * @param port port number
*/ */
void ndpi_enable_cache(struct ndpi_detection_module_struct *ndpi_mod, char* host, u_int port); void ndpi_enable_cache(struct ndpi_detection_module_struct *ndpi_mod, char* host, u_int port);
@@ -150,14 +150,6 @@ extern "C" {
*/ */
u_int8_t ndpi_detection_get_l4(const u_int8_t * l3, u_int16_t l3_len, const u_int8_t ** l4_return, u_int16_t * l4_len_return, u_int8_t ndpi_detection_get_l4(const u_int8_t * l3, u_int16_t l3_len, const u_int8_t ** l4_return, u_int16_t * l4_len_return,
u_int8_t * l4_protocol_return, u_int32_t flags); u_int8_t * l4_protocol_return, u_int32_t flags);
/**
* returns the real protocol for the flow of the last packet given to the detection.
* if no real protocol could be found, the unknown protocol will be returned.
*
* @param ndpi_struct the detection module
* @return the protocol id of the last real protocol found in the protocol history of the flow
*/
/* u_int16_t ndpi_detection_get_real_protocol_of_flow(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); */
/** /**
* returns true if the protocol history of the flow of the last packet given to the detection * returns true if the protocol history of the flow of the last packet given to the detection
@@ -244,4 +236,5 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif
#endif /* __NDPI_API_H__ */
+15 -29
View File
@@ -42,15 +42,22 @@
#endif/* BYTE_ORDER */ #endif/* BYTE_ORDER */
#endif/* __OPENBSD__ */ #endif/* __OPENBSD__ */
#if 0
#ifndef NDPI_ENABLE_DEBUG_MESSAGES #if __BYTE_ORDER == __LITTLE_ENDIAN
#define NDPI_ENABLE_DEBUG_MESSAGES #ifndef __LITTLE_ENDIAN__
#define __LITTLE_ENDIAN__
#endif
#else
#ifndef __BIG_ENDIAN__
#define __BIG_ENDIAN__
#endif #endif
#endif #endif
#ifdef WIN32 #ifdef WIN32
#ifndef __LITTLE_ENDIAN__
#define __LITTLE_ENDIAN__ 1 #define __LITTLE_ENDIAN__ 1
#endif #endif
#endif
#if !(defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__)) #if !(defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__))
#if defined(__mips__) #if defined(__mips__)
@@ -59,13 +66,6 @@
#define __BIG_ENDIAN__ #define __BIG_ENDIAN__
#endif #endif
/* Kernel modules */
#if defined(__LITTLE_ENDIAN)
#define __LITTLE_ENDIAN__
#endif
#if defined(__BIG_ENDIAN)
#define __BIG_ENDIAN__
#endif
/* Everything else */ /* Everything else */
#if (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)) #if (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__))
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
@@ -165,11 +165,6 @@
#define MAX_PACKET_COUNTER 65000 #define MAX_PACKET_COUNTER 65000
#define MAX_DEFAULT_PORTS 5 #define MAX_DEFAULT_PORTS 5
/**********************
* detection features *
**********************/
/* #define NDPI_SELECT_DETECTION_WITH_REAL_PROTOCOL ( 1 << 0 ) */
#define NDPI_DIRECTCONNECT_CONNECTION_IP_TICK_TIMEOUT 600 #define NDPI_DIRECTCONNECT_CONNECTION_IP_TICK_TIMEOUT 600
#define NDPI_IRC_CONNECTION_TIMEOUT 120 #define NDPI_IRC_CONNECTION_TIMEOUT 120
#define NDPI_GNUTELLA_CONNECTION_TIMEOUT 60 #define NDPI_GNUTELLA_CONNECTION_TIMEOUT 60
@@ -199,7 +194,7 @@
#else /* NDPI_ENABLE_DEBUG_MESSAGES */ #else /* NDPI_ENABLE_DEBUG_MESSAGES */
#if defined(WIN32) #ifdef WIN32
#define NDPI_LOG(...) {} #define NDPI_LOG(...) {}
#else #else
#define NDPI_LOG(proto, mod, log_level, args...) {} #define NDPI_LOG(proto, mod, log_level, args...) {}
@@ -218,12 +213,7 @@
#define NDPI_COMPARE_IPV6_ADDRESS_STRUCTS(x,y) \ #define NDPI_COMPARE_IPV6_ADDRESS_STRUCTS(x,y) \
((((u_int64_t *)(x))[0]) < (((u_int64_t *)(y))[0]) || ( (((u_int64_t *)(x))[0]) == (((u_int64_t *)(y))[0]) && (((u_int64_t *)(x))[1]) < (((u_int64_t *)(y))[1])) ) ((((u_int64_t *)(x))[0]) < (((u_int64_t *)(y))[0]) || ( (((u_int64_t *)(x))[0]) == (((u_int64_t *)(y))[0]) && (((u_int64_t *)(x))[1]) < (((u_int64_t *)(y))[1])) )
#if !defined(__KERNEL__) && !defined(NDPI_IPTABLES_EXT)
#define NDPI_NUM_BITS 256 #define NDPI_NUM_BITS 256
#else
/* custom protocols not supported */
#define NDPI_NUM_BITS 192
#endif
#define NDPI_BITS /* 32 */ (sizeof(ndpi_ndpi_mask) * 8 /* number of bits in a byte */) /* bits per mask */ #define NDPI_BITS /* 32 */ (sizeof(ndpi_ndpi_mask) * 8 /* number of bits in a byte */) /* bits per mask */
#define howmanybits(x, y) (((x)+((y)-1))/(y)) #define howmanybits(x, y) (((x)+((y)-1))/(y))
@@ -289,21 +279,17 @@
#define get_l32(X,O) get_u_int32_t(X,O) #define get_l32(X,O) get_u_int32_t(X,O)
#elif defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) #elif defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN)
/* convert the bytes from big to little endian */ /* convert the bytes from big to little endian */
#ifndef __KERNEL__
# define get_l16(X,O) bswap_16(get_u_int16_t(X,O)) # define get_l16(X,O) bswap_16(get_u_int16_t(X,O))
# define get_l32(X,O) bswap_32(get_u_int32_t(X,O)) # define get_l32(X,O) bswap_32(get_u_int32_t(X,O))
#else #else
# define get_l16(X,O) __cpu_to_le16(get_u_int16_t(X,O))
# define get_l32(X,O) __cpu_to_le32(get_u_int32_t(X,O))
#endif
#else
#error "__BYTE_ORDER MUST BE DEFINED !" #error "__BYTE_ORDER MUST BE DEFINED !"
#endif /* __BYTE_ORDER */ #endif /* __BYTE_ORDER */
/* define memory callback function */ /* define memory callback function */
#define match_first_bytes(payload,st) (memcmp((payload),(st),(sizeof(st)-1))==0) #define match_first_bytes(payload,st) (memcmp((payload),(st),(sizeof(st)-1))==0)
#if defined(WIN32) && !defined(snprintf)
#define snprintf _snprintf
#endif
#endif /* __NDPI_DEFINE_INCLUDE_FILE__ */ #endif /* __NDPI_DEFINE_INCLUDE_FILE__ */
+68
View File
@@ -0,0 +1,68 @@
/*
* ndpi_includes.h
*
* Copyright (C) 2011-15 - ntop.org
*
* This file is part of nDPI, an open source deep packet inspection
* library based on the OpenDPI and PACE technology by ipoque GmbH
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#ifndef __NDPI_INCLUDES_H__
#define __NDPI_INCLUDES_H__
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <limits.h>
#ifdef WIN32
#include "ndpi_win32.h"
#else
#include <sys/param.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#if !defined __APPLE__ && !defined __FreeBSD__ && !defined __NetBSD__ && !defined __OpenBSD__
#include <endian.h>
#include <byteswap.h>
#if defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
#include <netinet/in.h>
#if defined __NetBSD__ || defined __OpenBSD__
#include <netinet/in_systm.h>
#ifdef __OpenBSD__
#include <pthread.h>
#endif
#endif
#endif
#endif
#endif /* Win32 */
#endif /* __NDPI_INCLUDES_H__ */
+19 -69
View File
@@ -22,53 +22,15 @@
* *
*/ */
#ifndef __NDPI_MAIN_INCLUDE_FILE__ #ifndef __NDPI_MAIN_H__
#define __NDPI_MAIN_INCLUDE_FILE__ #define __NDPI_MAIN_H__
#ifndef __KERNEL__ #include "ndpi_includes.h"
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#ifndef WIN32
#include <pthread.h>
#endif
#include <ctype.h>
#include <time.h>
#endif
#ifndef WIN32
#ifndef __KERNEL__
#include <sys/time.h>
#endif
#if !defined __APPLE__ && !defined __FreeBSD__ && !defined __NetBSD__ && !defined __OpenBSD__
#ifndef __KERNEL__
#include <endian.h>
#include <byteswap.h>
#else
#include <asm/byteorder.h>
#include <linux/ctype.h>
#endif
#endif
/* default includes */
#ifndef __KERNEL__
#include <sys/param.h>
#include <limits.h>
#endif
#endif
#include "ndpi_win32.h"
#include "ndpi_unix.h"
#include "ndpi_define.h" #include "ndpi_define.h"
#include "ndpi_protocol_ids.h" #include "ndpi_protocol_ids.h"
#include "ndpi_typedefs.h" #include "ndpi_typedefs.h"
#include "ndpi_protocols.h" #include "ndpi_protocols.h"
#include "ndpi_api.h"
void *ndpi_tdelete(const void * __restrict, void ** __restrict, void *ndpi_tdelete(const void * __restrict, void ** __restrict,
@@ -103,58 +65,46 @@ void ndpi_set_detected_protocol(struct ndpi_detection_module_struct *ndpi_struct
u_int16_t upper_detected_protocol, u_int16_t upper_detected_protocol,
u_int16_t lower_detected_protocol); u_int16_t lower_detected_protocol);
/* function to parse a packet which has line based information into a line based structure
* this function will also set some well known line pointers like:
* - host, user agent, empty line,....
*/
extern void ndpi_parse_packet_line_info(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); extern void ndpi_parse_packet_line_info(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
extern void ndpi_parse_packet_line_info_any(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); extern void ndpi_parse_packet_line_info_any(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
extern u_int16_t ndpi_check_for_email_address(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow, u_int16_t counter); extern u_int16_t ndpi_check_for_email_address(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow, u_int16_t counter);
extern void ndpi_int_change_packet_protocol(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow, extern void ndpi_int_change_packet_protocol(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow,
u_int16_t upper_detected_protocol, u_int16_t upper_detected_protocol,
u_int16_t lower_detected_protocol); u_int16_t lower_detected_protocol);
extern void ndpi_int_change_protocol(struct ndpi_detection_module_struct *ndpi_struct, extern void ndpi_int_change_protocol(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow,
struct ndpi_flow_struct *flow,
u_int16_t upper_detected_protocol, u_int16_t upper_detected_protocol,
u_int16_t lower_detected_protocol); u_int16_t lower_detected_protocol);
extern void ndpi_set_proto_defaults(struct ndpi_detection_module_struct *ndpi_mod, extern void ndpi_set_proto_defaults(struct ndpi_detection_module_struct *ndpi_mod, ndpi_protocol_breed_t protoBreed, u_int16_t protoId,
ndpi_protocol_breed_t protoBreed, u_int16_t protoId, u_int16_t tcp_alias_protoId[2], u_int16_t udp_alias_protoId[2], char *protoName,
u_int16_t tcp_alias_protoId[2], u_int16_t udp_alias_protoId[2],
char *protoName,
ndpi_port_range *tcpDefPorts, ndpi_port_range *udpDefPorts); ndpi_port_range *tcpDefPorts, ndpi_port_range *udpDefPorts);
extern void ndpi_int_reset_packet_protocol(struct ndpi_packet_struct *packet); extern void ndpi_int_reset_packet_protocol(struct ndpi_packet_struct *packet);
extern void ndpi_int_reset_protocol(struct ndpi_flow_struct *flow); extern void ndpi_int_reset_protocol(struct ndpi_flow_struct *flow);
extern int ndpi_packet_src_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip); extern int ndpi_packet_src_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip);
extern int ndpi_packet_dst_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip); extern int ndpi_packet_dst_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip);
extern void ndpi_packet_src_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip); extern void ndpi_packet_src_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip);
extern void ndpi_packet_dst_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip); extern void ndpi_packet_dst_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip);
extern char *ndpi_get_ip_string(struct ndpi_detection_module_struct *ndpi_struct, const ndpi_ip_addr_t * ip); extern char *ndpi_get_ip_string(struct ndpi_detection_module_struct *ndpi_struct, const ndpi_ip_addr_t * ip);
extern char *ndpi_get_packet_src_ip_string(struct ndpi_detection_module_struct *ndpi_struct, extern char *ndpi_get_packet_src_ip_string(struct ndpi_detection_module_struct *ndpi_struct, const struct ndpi_packet_struct *packet);
const struct ndpi_packet_struct *packet);
extern char* ndpi_get_proto_by_id(struct ndpi_detection_module_struct *ndpi_mod, u_int id); extern char* ndpi_get_proto_by_id(struct ndpi_detection_module_struct *ndpi_mod, u_int id);
extern u_int16_t ndpi_guess_protocol_id(struct ndpi_detection_module_struct *ndpi_struct, extern u_int16_t ndpi_guess_protocol_id(struct ndpi_detection_module_struct *ndpi_struct,
u_int8_t proto, u_int16_t sport, u_int16_t dport); u_int8_t proto, u_int16_t sport, u_int16_t dport);
extern u_int8_t ndpi_is_proto(ndpi_protocol p, u_int16_t proto); extern u_int8_t ndpi_is_proto(ndpi_protocol p, u_int16_t proto);
extern u_int16_t ndpi_get_lower_proto(ndpi_protocol p); extern u_int16_t ndpi_get_lower_proto(ndpi_protocol p);
extern int ndpi_get_protocol_id_master_proto(struct ndpi_detection_module_struct *ndpi_struct, extern int ndpi_get_protocol_id_master_proto(struct ndpi_detection_module_struct *ndpi_struct, u_int16_t protocol_id,
u_int16_t protocol_id,
u_int16_t** tcp_master_proto, u_int16_t** tcp_master_proto,
u_int16_t** udp_master_proto); u_int16_t** udp_master_proto);
extern u_int8_t ndpi_net_match(u_int32_t ip_to_check,
u_int32_t net,
u_int32_t num_bits);
extern u_int8_t ndpi_ips_match(u_int32_t src, u_int32_t dst,
u_int32_t net, u_int32_t num_bits);
#ifdef NDPI_ENABLE_DEBUG_MESSAGES #ifdef NDPI_ENABLE_DEBUG_MESSAGES
void ndpi_debug_get_last_log_function_line(struct ndpi_detection_module_struct *ndpi_struct, void ndpi_debug_get_last_log_function_line(struct ndpi_detection_module_struct *ndpi_struct,
const char **file, const char **func, u_int32_t * line); const char **file, const char **func, u_int32_t * line);
#endif #endif
#include "ndpi_api.h" #endif /* __NDPI_MAIN_H__ */
#endif /* __NDPI_MAIN_INCLUDE_FILE__ */
+7 -2
View File
@@ -23,7 +23,7 @@
*/ */
#ifndef __NDPI_API_INCLUDE_FILE__ #ifndef __NDPI_API_H__
#endif #endif
@@ -201,6 +201,11 @@
#define NDPI_PROTOCOL_WHATSAPP_VOICE 189 #define NDPI_PROTOCOL_WHATSAPP_VOICE 189
#define NDPI_PROTOCOL_STARCRAFT 213 /* Matteo Bracci <matteobracci1@gmail.com> */ #define NDPI_PROTOCOL_STARCRAFT 213 /* Matteo Bracci <matteobracci1@gmail.com> */
#define NDPI_PROTOCOL_TEREDO 214 #define NDPI_PROTOCOL_TEREDO 214
#define NDPI_PROTOCOL_HEP 216 /* Sipcapture.org QXIP BV */
#define NDPI_PROTOCOL_UBNTAC2 217 /* Ubiquity UBNT AirControl 2 - Thomas Fjellstrom <thomas+ndpi@fjellstrom.ca> */
#define NDPI_PROTOCOL_MS_LYNC 218
#define NDPI_CONTENT_AVI 39 #define NDPI_CONTENT_AVI 39
#define NDPI_CONTENT_FLASH 40 #define NDPI_CONTENT_FLASH 40
@@ -263,7 +268,7 @@
#define NDPI_SERVICE_HOTSPOT_SHIELD 215 #define NDPI_SERVICE_HOTSPOT_SHIELD 215
/* UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE */ /* UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE */
#define NDPI_LAST_IMPLEMENTED_PROTOCOL NDPI_SERVICE_HOTSPOT_SHIELD #define NDPI_LAST_IMPLEMENTED_PROTOCOL NDPI_PROTOCOL_MS_LYNC
#define NDPI_MAX_SUPPORTED_PROTOCOLS (NDPI_LAST_IMPLEMENTED_PROTOCOL + 1) #define NDPI_MAX_SUPPORTED_PROTOCOLS (NDPI_LAST_IMPLEMENTED_PROTOCOL + 1)
#define NDPI_MAX_NUM_CUSTOM_PROTOCOLS (NDPI_NUM_BITS-NDPI_LAST_IMPLEMENTED_PROTOCOL) #define NDPI_MAX_NUM_CUSTOM_PROTOCOLS (NDPI_NUM_BITS-NDPI_LAST_IMPLEMENTED_PROTOCOL)
+7 -3
View File
@@ -23,8 +23,8 @@
*/ */
#ifndef __NDPI_PROTOCOLS_INCLUDE_FILE__ #ifndef __NDPI_PROTOCOLS_H__
#define __NDPI_PROTOCOLS_INCLUDE_FILE__ #define __NDPI_PROTOCOLS_H__
#include "ndpi_main.h" #include "ndpi_main.h"
@@ -72,6 +72,7 @@ void ndpi_search_oscar(struct ndpi_detection_module_struct *ndpi_struct, struct
void ndpi_search_jabber_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_jabber_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
void ndpi_search_irc_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_irc_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
void ndpi_search_sip(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_sip(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
void ndpi_search_hep(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
void ndpi_search_direct_download_link_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_direct_download_link_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
void ndpi_search_mail_pop_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_mail_pop_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
void ndpi_search_mail_imap_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_mail_imap_tcp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
@@ -195,6 +196,7 @@ void ndpi_search_eaq(struct ndpi_detection_module_struct *ndpi_struct, struct nd
void ndpi_search_kakaotalk_voice(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_kakaotalk_voice(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
void ndpi_search_mpegts(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_mpegts(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
void ndpi_search_starcraft(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow); void ndpi_search_starcraft(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
void ndpi_search_ubntac2(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow);
/* --- INIT FUNCTIONS --- */ /* --- INIT FUNCTIONS --- */
@@ -288,6 +290,7 @@ void init_rtsp_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int
void init_sflow_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_sflow_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
void init_shoutcast_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_shoutcast_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
void init_sip_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_sip_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
void init_hep_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
void init_skinny_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_skinny_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
void init_skype_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_skype_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
void init_smb_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_smb_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
@@ -332,5 +335,6 @@ void init_yahoo_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_in
void init_zattoo_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_zattoo_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
void init_zmq_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_zmq_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
void init_stracraft_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask); void init_stracraft_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
void init_ubntac2_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask);
#endif /* __NDPI_PROTOCOLS_INCLUDE_FILE__ */ #endif /* __NDPI_PROTOCOLS_H__ */
+259 -74
View File
@@ -22,94 +22,280 @@
* *
*/ */
#ifndef __NDPI_TYPEDEFS_FILE__ #ifndef __NDPI_TYPEDEFS_H__
#define __NDPI_TYPEDEFS_FILE__ #define __NDPI_TYPEDEFS_H__
typedef enum { #include "ndpi_define.h"
#define BT_ANNOUNCE
#define SNAP_EXT
/* NDPI_LOG_LEVEL */
typedef enum
{
NDPI_LOG_ERROR, NDPI_LOG_ERROR,
NDPI_LOG_TRACE, NDPI_LOG_TRACE,
NDPI_LOG_DEBUG NDPI_LOG_DEBUG
} ndpi_log_level_t; } ndpi_log_level_t;
typedef void (*ndpi_debug_function_ptr) (u_int32_t protocol, /* NDPI_VISIT */
void *module_struct, ndpi_log_level_t log_level, typedef enum
const char *format, ...); {
#define BT_ANNOUNCE
typedef enum {
ndpi_preorder, ndpi_preorder,
ndpi_postorder, ndpi_postorder,
ndpi_endorder, ndpi_endorder,
ndpi_leaf ndpi_leaf
} ndpi_VISIT; } ndpi_VISIT;
typedef struct node_t { /* NDPI_NODE */
typedef struct node_t
{
char *key; char *key;
struct node_t *left, *right; struct node_t *left, *right;
} ndpi_node; } ndpi_node;
/* NDPI_MASK_SIZE */
typedef u_int32_t ndpi_ndpi_mask; typedef u_int32_t ndpi_ndpi_mask;
typedef struct ndpi_protocol_bitmask_struct { /* NDPI_PROTO_BITMASK_STRUCT */
typedef struct ndpi_protocol_bitmask_struct
{
ndpi_ndpi_mask fds_bits[NDPI_NUM_FDS_BITS]; ndpi_ndpi_mask fds_bits[NDPI_NUM_FDS_BITS];
} ndpi_protocol_bitmask_struct_t; } ndpi_protocol_bitmask_struct_t;
#ifdef NDPI_DETECTION_SUPPORT_IPV6 /* NDPI_DEBUG_FUNCTION_PTR (cast) */
struct ndpi_ip6_addr { typedef void (*ndpi_debug_function_ptr) (u_int32_t protocol, void *module_struct,
union { ndpi_log_level_t log_level, const char *format, ...);
u_int8_t u6_addr8[16];
u_int16_t u6_addr16[8];
u_int32_t u6_addr32[4];
u_int64_t u6_addr64[2];
} ndpi_v6_u;
#define ndpi_v6_addr ndpi_v6_u.u6_addr8
#define ndpi_v6_addr16 ndpi_v6_u.u6_addr16
#define ndpi_v6_addr32 ndpi_v6_u.u6_addr32
#define ndpi_v6_addr64 ndpi_v6_u.u6_addr64
};
struct ndpi_ipv6hdr { /* ************************************************************ */
/* use userspace and kernelspace compatible compile parameters */ /* ******************* NDPI NETWORKS HEADERS ****************** */
/* ************************************************************ */
/* ++++++++++++++++++++++++ Cisco headers +++++++++++++++++++++ */
/* Cisco HDLC */
#ifdef _MSC_VER
/* Windows */
#define PACK_ON __pragma(pack(push, 1))
#define PACK_OFF __pragma(pack(pop))
#elif defined(__GNUC__)
/* GNU C */
#define PACK_ON
#define PACK_OFF __attribute__((packed))
#endif
PACK_ON
struct ndpi_chdlc
{
u_int8_t addr; /* 0x0F (Unicast) - 0x8F (Broadcast) */
u_int8_t ctrl; /* always 0x00 */
u_int16_t proto_code; /* protocol type (e.g. 0x0800 IP) */
} PACK_OFF;
/* SLARP - Serial Line ARP http://tinyurl.com/qa54e95 */
PACK_ON
struct ndpi_slarp
{
/* address requests (0x00)
address replies (0x01)
keep-alive (0x02)
*/
u_int32_t slarp_type;
u_int32_t addr_1;
u_int32_t addr_2;
} PACK_OFF;
/* Cisco Discovery Protocol http://tinyurl.com/qa6yw9l */
PACK_ON
struct ndpi_cdp
{
u_int8_t version;
u_int8_t ttl;
u_int16_t checksum;
u_int16_t type;
u_int16_t length;
} PACK_OFF;
/* +++++++++++++++ Ethernet header (IEEE 802.3) +++++++++++++++ */
PACK_ON
struct ndpi_ethhdr
{
u_char h_dest[6]; /* destination eth addr */
u_char h_source[6]; /* source ether addr */
u_int16_t h_proto; /* data length (<= 1500) or type ID proto (>=1536) */
} PACK_OFF;
/* +++++++++++++++++++ LLC header (IEEE 802.2) ++++++++++++++++ */
PACK_ON
struct ndpi_snap_extension
{
u_int16_t oui;
u_int8_t oui2;
u_int16_t proto_ID;
} PACK_OFF;
PACK_ON
struct ndpi_llc_header
{
u_int8_t dsap;
u_int8_t ssap;
u_int8_t ctrl;
#ifdef SNAP_EXT
struct ndpi_snap_extension snap;
#endif
} PACK_OFF;
/* ++++++++++ RADIO TAP header (for IEEE 802.11) +++++++++++++ */
PACK_ON
struct ndpi_radiotap_header
{
u_int8_t version; /* set to 0 */
u_int8_t pad;
u_int16_t len;
u_int32_t present;
u_int64_t MAC_timestamp;
u_int8_t flags;
} PACK_OFF;
/* ++++++++++++ Wireless header (IEEE 802.11) ++++++++++++++++ */
PACK_ON
struct ndpi_wifi_header
{
u_int16_t fc;
u_int16_t duration;
u_char rcvr[6];
u_char trsm[6];
u_char dest[6];
u_int16_t seq_ctrl;
/* u_int64_t ccmp - for data encription only - check fc.flag */
} PACK_OFF;
/* +++++++++++++++++++++++ MPLS header +++++++++++++++++++++++ */
PACK_ON
struct ndpi_mpls_header
{
u_int32_t label:20, exp:3, s:1, ttl:8;
} PACK_OFF;
/* ++++++++++++++++++++++++ IP header ++++++++++++++++++++++++ */
PACK_ON
struct ndpi_iphdr {
#if defined(__LITTLE_ENDIAN__) #if defined(__LITTLE_ENDIAN__)
u_int8_t priority:4, version:4; u_int8_t ihl:4, version:4;
#elif defined(__BIG_ENDIAN__) #elif defined(__BIG_ENDIAN__)
u_int8_t version:4, priority:4; u_int8_t version:4, ihl:4;
#else #else
# error "Byte order must be defined" # error "Byte order must be defined"
#endif #endif
u_int8_t tos;
u_int16_t tot_len;
u_int16_t id;
u_int16_t frag_off;
u_int8_t ttl;
u_int8_t protocol;
u_int16_t check;
u_int32_t saddr;
u_int32_t daddr;
} PACK_OFF;
u_int8_t flow_lbl[3]; /* +++++++++++++++++++++++ IPv6 header +++++++++++++++++++++++ */
/* rfc3542 */
u_int16_t payload_len; struct ndpi_in6_addr
u_int8_t nexthdr; {
u_int8_t hop_limit; union
{
struct ndpi_ip6_addr saddr; u_int8_t u6_addr8[16];
struct ndpi_ip6_addr daddr; u_int16_t u6_addr16[8];
u_int32_t u6_addr32[4];
} u6_addr; /* 128-bit IP6 address */
}; };
#endif
typedef union { PACK_ON
struct ndpi_ipv6hdr
{
union
{
struct ndpi_ip6_hdrctl
{
u_int32_t ip6_un1_flow;
u_int16_t ip6_un1_plen;
u_int8_t ip6_un1_nxt;
u_int8_t ip6_un1_hlim;
} ip6_un1;
u_int8_t ip6_un2_vfc;
} ip6_ctlun;
struct ndpi_in6_addr ip6_src;
struct ndpi_in6_addr ip6_dst;
} PACK_OFF;
/* +++++++++++++++++++++++ TCP header +++++++++++++++++++++++ */
PACK_ON
struct ndpi_tcphdr
{
u_int16_t source;
u_int16_t dest;
u_int32_t seq;
u_int32_t ack_seq;
#if defined(__LITTLE_ENDIAN__)
u_int16_t res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, ece:1, cwr:1;
#elif defined(__BIG_ENDIAN__)
u_int16_t doff:4, res1:4, cwr:1, ece:1, urg:1, ack:1, psh:1, rst:1, syn:1, fin:1;
#else
# error "Byte order must be defined"
#endif
u_int16_t window;
u_int16_t check;
u_int16_t urg_ptr;
} PACK_OFF;
/* +++++++++++++++++++++++ UDP header +++++++++++++++++++++++ */
PACK_ON
struct ndpi_udphdr
{
u_int16_t source;
u_int16_t dest;
u_int16_t len;
u_int16_t check;
} PACK_OFF;
PACK_ON
struct ndpi_dns_packet_header {
u_int16_t transaction_id, flags, num_queries, answer_rrs, authority_rrs, additional_rrs;
} PACK_OFF;
typedef union
{
u_int32_t ipv4; u_int32_t ipv4;
u_int8_t ipv4_u_int8_t[4]; u_int8_t ipv4_u_int8_t[4];
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
struct ndpi_ip6_addr ipv6; struct ndpi_in6_addr ipv6;
#endif #endif
} ndpi_ip_addr_t; } ndpi_ip_addr_t;
/* ************************************************************ */
/* ******************* ********************* ****************** */
/* ************************************************************ */
#ifdef NDPI_PROTOCOL_BITTORRENT #ifdef NDPI_PROTOCOL_BITTORRENT
#ifndef __KERNEL__
typedef struct spinlock { typedef struct spinlock {
volatile int val; volatile int val;
} spinlock_t; } spinlock_t;
typedef struct atomic { typedef struct atomic {
volatile int counter; volatile int counter;
} atomic_t; } atomic_t;
#endif
struct hash_ip4p_node { struct hash_ip4p_node {
struct hash_ip4p_node *next,*prev; struct hash_ip4p_node *next,*prev;
time_t lchg; time_t lchg;
@@ -155,16 +341,12 @@ typedef enum {
} ndpi_http_method; } ndpi_http_method;
typedef struct ndpi_id_struct { typedef struct ndpi_id_struct {
/* detected_protocol_bitmask: /**
* access this bitmask to find out whether an id has used skype or not detected_protocol_bitmask:
* if a flag is set here, it will not be resetted access this bitmask to find out whether an id has used skype or not
* to compare this, use: if a flag is set here, it will not be resetted
* if (NDPI_BITMASK_COMPARE(id->detected_protocol_bitmask, to compare this, use:
* NDPI_PROTOCOL_BITMASK_XXX) != 0) **/
* {
* // protocol XXX detected on this id
* }
*/
NDPI_PROTOCOL_BITMASK detected_protocol_bitmask; NDPI_PROTOCOL_BITMASK detected_protocol_bitmask;
#ifdef NDPI_PROTOCOL_RTSP #ifdef NDPI_PROTOCOL_RTSP
ndpi_ip_addr_t rtsp_ip_address; ndpi_ip_addr_t rtsp_ip_address;
@@ -390,7 +572,7 @@ struct ndpi_flow_tcp_struct {
u_char prev_zmq_pkt[10]; u_char prev_zmq_pkt[10];
#endif #endif
} }
#if !defined(WIN32) #ifndef WIN32
__attribute__ ((__packed__)) __attribute__ ((__packed__))
#endif #endif
; ;
@@ -439,7 +621,7 @@ struct ndpi_flow_udp_struct {
u_int32_t eaq_sequence; u_int32_t eaq_sequence;
#endif #endif
} }
#if !defined(WIN32) #ifndef WIN32
__attribute__ ((__packed__)) __attribute__ ((__packed__))
#endif #endif
; ;
@@ -468,7 +650,7 @@ typedef struct ndpi_packet_struct {
u_int8_t detected_subprotocol_stack[NDPI_PROTOCOL_HISTORY_SIZE]; u_int8_t detected_subprotocol_stack[NDPI_PROTOCOL_HISTORY_SIZE];
#if !defined(WIN32) #ifndef WIN32
__attribute__ ((__packed__)) __attribute__ ((__packed__))
#endif #endif
u_int16_t protocol_stack_info; u_int16_t protocol_stack_info;
@@ -666,41 +848,43 @@ typedef struct ndpi_detection_module_struct {
typedef struct ndpi_flow_struct { typedef struct ndpi_flow_struct {
u_int16_t detected_protocol_stack[NDPI_PROTOCOL_HISTORY_SIZE]; u_int16_t detected_protocol_stack[NDPI_PROTOCOL_HISTORY_SIZE];
#if !defined(WIN32) #ifndef WIN32
__attribute__ ((__packed__)) __attribute__ ((__packed__))
#endif #endif
u_int16_t protocol_stack_info; u_int16_t protocol_stack_info;
/* init parameter, internal used to set up timestamp,... */ /* init parameter, internal used to set up timestamp,... */
u_int16_t guessed_protocol_id; u_int16_t guessed_protocol_id, guessed_host_proto_id;
u_int8_t protocol_id_already_guessed:1; u_int8_t protocol_id_already_guessed:1, host_already_guessed:1, init_finished:1, setup_packet_direction:1, packet_direction:1;
u_int8_t init_finished:1;
u_int8_t setup_packet_direction:1; /*
u_int8_t packet_direction:1; /* if ndpi_struct->direction_detect_disable == 1 */ if ndpi_struct->direction_detect_disable == 1
/* tcp sequence number connection tracking */ tcp sequence number connection tracking
*/
u_int32_t next_tcp_seq_nr[2]; u_int32_t next_tcp_seq_nr[2];
/* the tcp / udp / other l4 value union /*
* this is used to reduce the number of bytes for tcp or udp protocol states the tcp / udp / other l4 value union
* */ used to reduce the number of bytes for tcp or udp protocol states
*/
union { union {
struct ndpi_flow_tcp_struct tcp; struct ndpi_flow_tcp_struct tcp;
struct ndpi_flow_udp_struct udp; struct ndpi_flow_udp_struct udp;
} l4; } l4;
struct ndpi_id_struct *server_id; /* /*
Pointer to src or dst Pointer to src or dst
that identifies the that identifies the
server of this connection server of this connection
*/ */
#ifndef __KERNEL__ struct ndpi_id_struct *server_id;
u_char host_server_name[256]; /* HTTP host or DNS query */ /* HTTP host or DNS query */
#else u_char host_server_name[256];
u_char host_server_name[160]; /* Via HTTP User-Agent */
#endif u_char detected_os[32];
u_char detected_os[32]; /* Via HTTP User-Agent */ /* Via HTTP X-Forwarded-For */
u_char nat_ip[24]; /* Via HTTP X-Forwarded-For */ u_char nat_ip[24];
/* /*
This structure below will not not stay inside the protos This structure below will not not stay inside the protos
@@ -730,7 +914,8 @@ typedef struct ndpi_flow_struct {
char client_certificate[48], server_certificate[48]; char client_certificate[48], server_certificate[48];
} ssl; } ssl;
} protos; } protos;
/* ALL protocol specific 64 bit variables here */
/*** ALL protocol specific 64 bit variables here ***/
/* protocols which have marked a connection as this connection cannot be protocol XXX, multiple u_int64_t */ /* protocols which have marked a connection as this connection cannot be protocol XXX, multiple u_int64_t */
NDPI_PROTOCOL_BITMASK excluded_protocol_bitmask; NDPI_PROTOCOL_BITMASK excluded_protocol_bitmask;
@@ -820,4 +1005,4 @@ typedef struct ndpi_flow_struct {
struct ndpi_id_struct *dst; struct ndpi_id_struct *dst;
} ndpi_flow_struct_t; } ndpi_flow_struct_t;
#endif/* __NDPI_TYPEDEFS_FILE__ */ #endif/* __NDPI_TYPEDEFS_H__ */
-9
View File
@@ -25,8 +25,6 @@
#ifndef __NDPI_UNIX_INCLUDE_FILE__ #ifndef __NDPI_UNIX_INCLUDE_FILE__
#define __NDPI_UNIX_INCLUDE_FILE__ #define __NDPI_UNIX_INCLUDE_FILE__
#include "linux_compat.h"
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#include <netinet/in.h> #include <netinet/in.h>
#if defined(__NetBSD__) || defined(__OpenBSD__) #if defined(__NetBSD__) || defined(__OpenBSD__)
@@ -38,16 +36,9 @@
#endif #endif
#ifndef WIN32 #ifndef WIN32
#ifndef __KERNEL__
#include <netinet/ip.h> #include <netinet/ip.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <netinet/udp.h> #include <netinet/udp.h>
#else
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#endif
#endif #endif
#endif /* __NDPI_UNIX_INCLUDE_FILE__ */ #endif /* __NDPI_UNIX_INCLUDE_FILE__ */
+12 -16
View File
@@ -22,37 +22,35 @@
* *
*/ */
#ifndef __NDPI_WIN32_INCLUDE_FILE__ #ifndef __NDPI_WIN32_H__
#define __NDPI_WIN32_INCLUDE_FILE__ #define __NDPI_WIN32_H__
#ifdef WIN32 #include <winsock2.h>
#include <Winsock2.h> /* winsock.h is included automatically */ #include <ws2tcpip.h>
#include <process.h> #include <process.h>
#include <io.h> #include <io.h>
#include <getopt.h> /* getopt from: http://www.pwilson.net/sample.html. */ #include <getopt.h> /* getopt from: http://www.pwilson.net/sample.html. */
#include <process.h> /* for getpid() and the exec..() family */ #include <process.h> /* for getpid() and the exec..() family */
#include <stdint.h>
#ifndef _CRT_SECURE_NO_WARNINGS #ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#endif #endif
#define snprintf _snprintf
extern char* strsep(char **stringp, const char *delim); #define _WS2TCPIP_H_ /* Avoid compilation problems */
extern char* strsep(char **sp, const char *sep);
#define __attribute__(x)
#include <stdint.h>
#ifndef __GNUC__
typedef unsigned char u_char; typedef unsigned char u_char;
typedef unsigned short u_short; typedef unsigned short u_short;
typedef unsigned int uint; typedef unsigned int uint;
typedef unsigned long u_long; typedef unsigned long u_long;
#endif
typedef u_char u_int8_t; typedef u_char u_int8_t;
typedef u_short u_int16_t; typedef u_short u_int16_t;
typedef unsigned int u_int32_t; typedef uint u_int32_t;
typedef uint u_int;
typedef unsigned __int64 u_int64_t; typedef unsigned __int64 u_int64_t;
#define pthread_t HANDLE #define pthread_t HANDLE
#define pthread_mutex_t HANDLE #define pthread_mutex_t HANDLE
#define pthread_rwlock_t pthread_mutex_t #define pthread_rwlock_t pthread_mutex_t
@@ -62,12 +60,10 @@ typedef unsigned __int64 u_int64_t;
#define pthread_rwlock_unlock pthread_mutex_unlock #define pthread_rwlock_unlock pthread_mutex_unlock
#define pthread_rwlock_destroy pthread_mutex_destroy #define pthread_rwlock_destroy pthread_mutex_destroy
#define gmtime_r(a, b) gmtime(a) /* Already thread safe on windows */ #define gmtime_r(a, b) memcpy(b, gmtime(a), sizeof(struct tm))
extern unsigned long waitForNextEvent(unsigned long ulDelay /* ms */); extern unsigned long waitForNextEvent(unsigned long ulDelay /* ms */);
#define sleep(a /* sec */) waitForNextEvent(1000*a /* ms */) #define sleep(a /* sec */) waitForNextEvent(1000*a /* ms */)
#endif /* Win32 */ #endif /* __NDPI_WIN32_H__ */
#endif /* __NDPI_WIN32_INCLUDE_FILE__ */
+1 -3
View File
@@ -7,14 +7,12 @@ libndpi_la_LDFLAGS = -version-info 1:0:0 -export-symbols $(top_srcdir)/libndpi.s
libndpi_la_includedir = $(includedir)/libndpi-@VERSION@/libndpi libndpi_la_includedir = $(includedir)/libndpi-@VERSION@/libndpi
libndpi_la_include_HEADERS = ../include/ndpi_api.h \ libndpi_la_include_HEADERS = ../include/ndpi_api.h \
../include/linux_compat.h \
../include/ndpi_define.h \ ../include/ndpi_define.h \
../include/ndpi_main.h \ ../include/ndpi_main.h \
../include/ndpi_protocol_ids.h \ ../include/ndpi_protocol_ids.h \
../include/ndpi_protocols.h \ ../include/ndpi_protocols.h \
../include/ndpi_typedefs.h \ ../include/ndpi_typedefs.h \
../include/ndpi_unix.h \ ../include/ndpi_includes.h
../include/ndpi_win32.h
libndpi_la_SOURCES = ndpi_content_match.c.inc \ libndpi_la_SOURCES = ndpi_content_match.c.inc \
ndpi_main.c \ ndpi_main.c \
+230 -347
View File
@@ -23,151 +23,21 @@
*/ */
#ifndef __KERNEL__
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#endif
#include "ahocorasick.h" #include "ahocorasick.h"
#include "ndpi_api.h" #include "ndpi_api.h"
#ifndef __KERNEL__
#include "../../config.h" #include "../../config.h"
#endif
// #define DEBUG
#ifdef __KERNEL__
#include <linux/version.h>
#define printf printk
#else
#include <time.h> #include <time.h>
#ifndef WIN32 #ifndef WIN32
#include <unistd.h> #include <unistd.h>
#endif #endif
#endif
#include "ndpi_content_match.c.inc" #include "ndpi_content_match.c.inc"
#include "third_party/include/ndpi_patricia.h" #include "third_party/include/ndpi_patricia.h"
#include "third_party/src/ndpi_patricia.c" #include "third_party/src/ndpi_patricia.c"
#ifdef WIN32
/* http://social.msdn.microsoft.com/Forums/uk/vcgeneral/thread/963aac07-da1a-4612-be4a-faac3f1d65ca */
#ifndef strtok_r
#define strtok_r(a,b,c) strtok(a,b)
#endif
#endif
#ifdef __KERNEL__
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
static inline char _tolower(const char c)
{
return c | 0x20;
}
static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
{
unsigned long long acc;
int ok;
if(base == 0) {
if(s[0] == '0') {
if(_tolower(s[1]) == 'x' && isxdigit(s[2]))
base = 16;
else
base = 8;
} else
base = 10;
}
if(base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
s += 2;
acc = 0;
ok = 0;
while (*s) {
unsigned int val;
if('0' <= *s && *s <= '9')
val = *s - '0';
else if('a' <= _tolower(*s) && _tolower(*s) <= 'f')
val = _tolower(*s) - 'a' + 10;
else if(*s == '\n') {
if(*(s + 1) == '\0')
break;
else
return -EINVAL;
} else
return -EINVAL;
if(val >= base)
return -EINVAL;
if(acc > div_u64(ULLONG_MAX - val, base))
return -ERANGE;
acc = acc * base + val;
ok = 1;
s++;
}
if(!ok)
return -EINVAL;
*res = acc;
return 0;
}
int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
{
if(s[0] == '+')
s++;
return _kstrtoull(s, base, res);
}
int kstrtoll(const char *s, unsigned int base, long long *res)
{
unsigned long long tmp;
int rv;
if(s[0] == '-') {
rv = _kstrtoull(s + 1, base, &tmp);
if(rv < 0)
return rv;
if((long long)(-tmp) >= 0)
return -ERANGE;
*res = -tmp;
} else {
rv = kstrtoull(s, base, &tmp);
if(rv < 0)
return rv;
if((long long)tmp < 0)
return -ERANGE;
*res = tmp;
}
return 0;
}
int kstrtoint(const char *s, unsigned int base, int *res)
{
long long tmp;
int rv;
rv = kstrtoll(s, base, &tmp);
if(rv < 0)
return rv;
if(tmp != (long long)(int)tmp)
return -ERANGE;
*res = tmp;
return 0;
}
#endif
int atoi(const char *str) {
int rc;
if(kstrtoint(str, 0, &rc) == 0 /* Success */)
return(rc);
else
return(0);
}
#endif
/* ftp://ftp.cc.uoc.gr/mirrors/OpenBSD/src/lib/libc/stdlib/tsearch.c */ /* ftp://ftp.cc.uoc.gr/mirrors/OpenBSD/src/lib/libc/stdlib/tsearch.c */
/* find or insert datum into search tree */ /* find or insert datum into search tree */
void * void *
@@ -560,15 +430,6 @@ void ndpi_set_proto_defaults(struct ndpi_detection_module_struct *ndpi_mod,
if(udpDefPorts[j].port_low != 0) addDefaultPort(&udpDefPorts[j], &ndpi_mod->proto_defaults[protoId], &ndpi_mod->udpRoot); if(udpDefPorts[j].port_low != 0) addDefaultPort(&udpDefPorts[j], &ndpi_mod->proto_defaults[protoId], &ndpi_mod->udpRoot);
if(tcpDefPorts[j].port_low != 0) addDefaultPort(&tcpDefPorts[j], &ndpi_mod->proto_defaults[protoId], &ndpi_mod->tcpRoot); if(tcpDefPorts[j].port_low != 0) addDefaultPort(&tcpDefPorts[j], &ndpi_mod->proto_defaults[protoId], &ndpi_mod->tcpRoot);
} }
#if 0
printf("%s(%d, %s, %p) [%s]\n",
__FUNCTION__,
protoId,
ndpi_mod->proto_defaults[protoId].protoName,
ndpi_mod,
ndpi_mod->proto_defaults[1].protoName);
#endif
} }
/* ******************************************************************** */ /* ******************************************************************** */
@@ -833,6 +694,11 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp
no_master, "IPP", no_master, "IPP",
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */, ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0) /* TCP */,
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */); ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0) /* UDP */);
ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_HEP,
no_master,
no_master, "HEP",
ndpi_build_default_ports(ports_a, 9064, 0, 0, 0, 0) /* TCP */,
ndpi_build_default_ports(ports_b, 9063, 0, 0, 0, 0) /* UDP */);
ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_HTTP, ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_ACCEPTABLE, NDPI_PROTOCOL_HTTP,
no_master, no_master,
no_master, "HTTP", no_master, "HTTP",
@@ -1622,6 +1488,16 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp
no_master, "Starcraft", no_master, "Starcraft",
ndpi_build_default_ports(ports_a, 1119, 0, 0, 0, 0), /* TCP */ ndpi_build_default_ports(ports_a, 1119, 0, 0, 0, 0), /* TCP */
ndpi_build_default_ports(ports_b, 1119, 0, 0, 0, 0)); /* UDP */ ndpi_build_default_ports(ports_b, 1119, 0, 0, 0, 0)); /* UDP */
ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_SAFE, NDPI_PROTOCOL_UBNTAC2,
no_master,
no_master, "UBNTAC2",
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0), /* TCP */
ndpi_build_default_ports(ports_b, 10001, 0, 0, 0, 0)); /* UDP */
ndpi_set_proto_defaults(ndpi_mod, NDPI_PROTOCOL_SAFE, NDPI_PROTOCOL_MS_LYNC,
no_master,
no_master, "Lync",
ndpi_build_default_ports(ports_a, 0, 0, 0, 0, 0), /* TCP */
ndpi_build_default_ports(ports_b, 0, 0, 0, 0, 0)); /* UDP */
/* calling function for host and content matched protocols */ /* calling function for host and content matched protocols */
init_string_based_protocols(ndpi_mod); init_string_based_protocols(ndpi_mod);
@@ -1666,11 +1542,11 @@ static int fill_prefix_v4(prefix_t *p, struct in_addr *a, int b, int mb) {
/* ******************************************* */ /* ******************************************* */
u_int16_t ndpi_network_ptree_match(struct ndpi_detection_module_struct *ndpi_struct, struct in_addr *pin) { u_int16_t ndpi_network_ptree_match(struct ndpi_detection_module_struct *ndpi_struct, struct in_addr *pin /* network byte order */) {
prefix_t prefix; prefix_t prefix;
patricia_node_t *node; patricia_node_t *node;
pin->s_addr = ntohl(pin->s_addr); /* Make sure all in network byte order otherwise compares wont work */ /* 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); fill_prefix_v4(&prefix, pin, 32, ((patricia_tree_t*)ndpi_struct->protocols_ptree)->maxbits);
node = ndpi_patricia_search_best(ndpi_struct->protocols_ptree, &prefix); node = ndpi_patricia_search_best(ndpi_struct->protocols_ptree, &prefix);
@@ -1679,7 +1555,7 @@ u_int16_t ndpi_network_ptree_match(struct ndpi_detection_module_struct *ndpi_str
/* ******************************************* */ /* ******************************************* */
u_int16_t ndpi_host_ptree_match(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t host) { u_int16_t ndpi_host_ptree_match(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t host /* network byte order */) {
struct in_addr pin; struct in_addr pin;
pin.s_addr = host; pin.s_addr = host;
@@ -1700,7 +1576,7 @@ u_int8_t ndpi_is_tor_flow(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_packet_struct *packet = &flow->packet; struct ndpi_packet_struct *packet = &flow->packet;
if(packet->tcp != NULL) { if(packet->tcp != NULL) {
if(flow->packet.iph) { if(packet->iph) {
if(tor_ptree_match(ndpi_struct, (struct in_addr *)&packet->iph->saddr) if(tor_ptree_match(ndpi_struct, (struct in_addr *)&packet->iph->saddr)
|| tor_ptree_match(ndpi_struct, (struct in_addr *)&packet->iph->daddr)) { || tor_ptree_match(ndpi_struct, (struct in_addr *)&packet->iph->daddr)) {
return(1); return(1);
@@ -1734,7 +1610,7 @@ static void ndpi_init_ptree_ipv4(struct ndpi_detection_module_struct *ndpi_str,
struct in_addr pin; struct in_addr pin;
patricia_node_t *node; patricia_node_t *node;
pin.s_addr = ntohl(host_list[i].network); pin.s_addr = htonl(host_list[i].network);
if((node = add_to_ptree(ptree, AF_INET, &pin, host_list[i].cidr /* bits */)) != NULL) if((node = add_to_ptree(ptree, AF_INET, &pin, host_list[i].cidr /* bits */)) != NULL)
node->value.user_value = host_list[i].value; node->value.user_value = host_list[i].value;
} }
@@ -1749,7 +1625,6 @@ static int ndpi_add_host_ip_subprotocol(struct ndpi_detection_module_struct *ndp
struct in_addr pin; struct in_addr pin;
inet_pton(AF_INET, value, &pin); inet_pton(AF_INET, value, &pin);
pin.s_addr = ntohl(pin.s_addr);
if((node = add_to_ptree(ndpi_struct->protocols_ptree, AF_INET, &pin, 32)) != NULL) { if((node = add_to_ptree(ndpi_struct->protocols_ptree, AF_INET, &pin, 32)) != NULL) {
node->value.user_value = protocol_id; node->value.user_value = protocol_id;
@@ -1889,13 +1764,16 @@ u_int16_t ndpi_guess_protocol_id(struct ndpi_detection_module_struct *ndpi_struc
ndpi_default_ports_tree_node_t node; ndpi_default_ports_tree_node_t node;
if(sport && dport) { if(sport && dport) {
node.default_port = sport; int low = ndpi_min(sport, dport);
int high = ndpi_max(sport, dport);
node.default_port = low; /* Check server port first */
ret = ndpi_tfind(&node, ret = ndpi_tfind(&node,
(proto == IPPROTO_TCP) ? (void*)&ndpi_struct->tcpRoot : (void*)&ndpi_struct->udpRoot, (proto == IPPROTO_TCP) ? (void*)&ndpi_struct->tcpRoot : (void*)&ndpi_struct->udpRoot,
ndpi_default_ports_tree_node_t_cmp); ndpi_default_ports_tree_node_t_cmp);
if(ret == NULL) { if(ret == NULL) {
node.default_port = dport; node.default_port = high;
ret = ndpi_tfind(&node, ret = ndpi_tfind(&node,
(proto == IPPROTO_TCP) ? (void*)&ndpi_struct->tcpRoot : (void*)&ndpi_struct->udpRoot, (proto == IPPROTO_TCP) ? (void*)&ndpi_struct->tcpRoot : (void*)&ndpi_struct->udpRoot,
ndpi_default_ports_tree_node_t_cmp); ndpi_default_ports_tree_node_t_cmp);
@@ -1949,60 +1827,28 @@ u_int16_t ndpi_guess_protocol_id(struct ndpi_detection_module_struct *ndpi_struc
/* ******************************************************************** */ /* ******************************************************************** */
#if 0
#ifndef __KERNEL__
static int add_proto_default_port(u_int16_t **ports, u_int16_t new_port,
ndpi_proto_defaults_t *def,
ndpi_default_ports_tree_node_t *root) {
u_int num_ports, i;
if(*ports == NULL) {
ndpi_port_range range = { new_port, new_port };
addDefaultPort(&range, def, &root);
return(0);
}
for(num_ports=0; (*ports)[num_ports] != 0; num_ports++)
;
if(num_ports >= MAX_DEFAULT_PORTS) {
printf("Too many ports defined: ignored port %d\n", new_port);
return(-1);
} else {
u_int16_t *new_ports = (u_int16_t*)ndpi_malloc(num_ports+1);
ndpi_port_range range;
if(new_ports == NULL) {
printf("Not enough memory\n");
return(-2);
}
for(i=0; i<num_ports; i++)
new_ports[i] = (*ports)[i];
new_ports[i++] = new_port;
new_ports[i++] = 0;
ndpi_free(*ports);
*ports = new_ports;
range.port_low = range.port_high = new_port;
addDefaultPort(&range, def, &root);
return(0);
}
}
#endif
#endif
/* ******************************************************************** */
u_int ndpi_get_num_supported_protocols(struct ndpi_detection_module_struct *ndpi_mod) { u_int ndpi_get_num_supported_protocols(struct ndpi_detection_module_struct *ndpi_mod) {
return(ndpi_mod->ndpi_num_supported_protocols); return(ndpi_mod->ndpi_num_supported_protocols);
} }
/* ******************************************************************** */ /* ******************************************************************** */
#ifdef WIN32
char * strsep(char **sp, char *sep)
{
char *p, *s;
if (sp == NULL || *sp == NULL || **sp == '\0') return(NULL);
s = *sp;
p = s + strcspn(s, sep);
if (*p != '\0') *p++ = '\0';
*sp = p;
return(s);
}
#endif
/* ******************************************************************** */
int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_mod, char* rule, u_int8_t do_add) { int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_mod, char* rule, u_int8_t do_add) {
char *at, *proto, *elem; char *at, *proto, *elem;
ndpi_proto_defaults_t *def; ndpi_proto_defaults_t *def;
@@ -2118,9 +1964,6 @@ int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_mod, char* rule,
*/ */
int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod, char* path) { int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod, char* path) {
#ifdef __KERNEL__
return(0);
#else
FILE *fd = fopen(path, "r"); FILE *fd = fopen(path, "r");
int i; int i;
@@ -2145,14 +1988,6 @@ int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod, char
fclose(fd); fclose(fd);
#if 0
printf("\nTCP:\n");
ndpi_twalk(ndpi_mod->tcpRoot, ndpi_default_ports_tree_node_t_walker, NULL);
printf("\nUDP:\n");
ndpi_twalk(ndpi_mod->udpRoot, ndpi_default_ports_tree_node_t_walker, NULL);
#endif
#endif
return(0); return(0);
} }
@@ -2184,7 +2019,7 @@ void ndpi_set_bitmask_protocol_detection( char * label,
} }
/* /*
Set funcition and index protocol within proto_default strcuture for port protocol detection Set function and index protocol within proto_default strcuture for port protocol detection
and callback_buffer function for DPI protocol detection and callback_buffer function for DPI protocol detection
*/ */
ndpi_struct->proto_defaults[ndpi_protocol_id].protoIdx = idx; ndpi_struct->proto_defaults[ndpi_protocol_id].protoIdx = idx;
@@ -2224,9 +2059,6 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n
/* HTTP */ /* HTTP */
init_http_dissector(ndpi_struct, &a, detection_bitmask); init_http_dissector(ndpi_struct, &a, detection_bitmask);
/* SKYPE */
init_skype_dissector(ndpi_struct, &a, detection_bitmask);
/* Stracraft */ /* Stracraft */
init_starcraft_dissector(ndpi_struct, &a, detection_bitmask); init_starcraft_dissector(ndpi_struct, &a, detection_bitmask);
@@ -2248,9 +2080,6 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n
/* SIP */ /* SIP */
init_sip_dissector(ndpi_struct, &a, detection_bitmask); init_sip_dissector(ndpi_struct, &a, detection_bitmask);
/* BITTORRENT */
init_bittorrent_dissector(ndpi_struct, &a, detection_bitmask);
/* Teredo */ /* Teredo */
init_teredo_dissector(ndpi_struct, &a, detection_bitmask); init_teredo_dissector(ndpi_struct, &a, detection_bitmask);
@@ -2620,6 +2449,13 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n
/* MPEGTS */ /* MPEGTS */
init_mpegts_dissector(ndpi_struct, &a, detection_bitmask); init_mpegts_dissector(ndpi_struct, &a, detection_bitmask);
/* Put false-positive sensitive protocols at the end */
/* SKYPE */
init_skype_dissector(ndpi_struct, &a, detection_bitmask);
/* BITTORRENT */
init_bittorrent_dissector(ndpi_struct, &a, detection_bitmask);
/* ----------------------------------------------------------------- */ /* ----------------------------------------------------------------- */
@@ -2703,8 +2539,7 @@ void ndpi_set_protocol_detection_bitmask2(struct ndpi_detection_module_struct *n
* nxt_hdr: protocol of the actual payload * nxt_hdr: protocol of the actual payload
* returns 0 upon success and 1 upon failure * returns 0 upon success and 1 upon failure
*/ */
static int ndpi_handle_ipv6_extension_headers(struct ndpi_detection_module_struct *ndpi_struct, static int ndpi_handle_ipv6_extension_headers(struct ndpi_detection_module_struct *ndpi_struct, const u_int8_t ** l4ptr, u_int16_t * l4len, u_int8_t * nxt_hdr)
const u_int8_t ** l4ptr, u_int16_t * l4len, u_int8_t * nxt_hdr)
{ {
while ((*nxt_hdr == 0 || *nxt_hdr == 43 || *nxt_hdr == 44 || *nxt_hdr == 60 || *nxt_hdr == 135 || *nxt_hdr == 59)) { while ((*nxt_hdr == 0 || *nxt_hdr == 43 || *nxt_hdr == 44 || *nxt_hdr == 60 || *nxt_hdr == 135 || *nxt_hdr == 59)) {
u_int16_t ehdr_len; u_int16_t ehdr_len;
@@ -2754,7 +2589,8 @@ static u_int8_t ndpi_iph_is_valid_and_not_fragmented(const struct ndpi_iphdr *ip
} }
static u_int8_t ndpi_detection_get_l4_internal(struct ndpi_detection_module_struct *ndpi_struct, static u_int8_t ndpi_detection_get_l4_internal(struct ndpi_detection_module_struct *ndpi_struct,
const u_int8_t * l3, u_int16_t l3_len, const u_int8_t ** l4_return, u_int16_t * l4_len_return, const u_int8_t * l3, u_int16_t l3_len,
const u_int8_t ** l4_return, u_int16_t * l4_len_return,
u_int8_t * l4_protocol_return, u_int32_t flags) u_int8_t * l4_protocol_return, u_int32_t flags)
{ {
const struct ndpi_iphdr *iph = NULL; const struct ndpi_iphdr *iph = NULL;
@@ -2807,10 +2643,10 @@ static u_int8_t ndpi_detection_get_l4_internal(struct ndpi_detection_module_stru
l4protocol = iph->protocol; l4protocol = iph->protocol;
} }
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
else if(iph_v6 != NULL && (l3_len - sizeof(struct ndpi_ipv6hdr)) >= ntohs(iph_v6->payload_len)) { else if(iph_v6 != NULL && (l3_len - sizeof(struct ndpi_ipv6hdr)) >= ntohs(iph_v6->ip6_ctlun.ip6_un1.ip6_un1_plen)) {
l4ptr = (((const u_int8_t *) iph_v6) + sizeof(struct ndpi_ipv6hdr)); l4ptr = (((const u_int8_t *) iph_v6) + sizeof(struct ndpi_ipv6hdr));
l4len = ntohs(iph_v6->payload_len); l4len = ntohs(iph_v6->ip6_ctlun.ip6_un1.ip6_un1_plen);
l4protocol = iph_v6->nexthdr; l4protocol = iph_v6->ip6_ctlun.ip6_un1.ip6_un1_nxt;
// we need to handle IPv6 extension headers if present // we need to handle IPv6 extension headers if present
if(ndpi_handle_ipv6_extension_headers(ndpi_struct, &l4ptr, &l4len, &l4protocol) != 0) { if(ndpi_handle_ipv6_extension_headers(ndpi_struct, &l4ptr, &l4len, &l4protocol) != 0) {
@@ -2838,11 +2674,6 @@ static u_int8_t ndpi_detection_get_l4_internal(struct ndpi_detection_module_stru
return 0; return 0;
} }
#if !defined(WIN32)
#define ATTRIBUTE_ALWAYS_INLINE static inline
#else
__forceinline static
#endif
void ndpi_apply_flow_protocol_to_packet(struct ndpi_flow_struct *flow, void ndpi_apply_flow_protocol_to_packet(struct ndpi_flow_struct *flow,
struct ndpi_packet_struct *packet) struct ndpi_packet_struct *packet)
{ {
@@ -2968,12 +2799,6 @@ static int ndpi_init_packet_header(struct ndpi_detection_module_struct *ndpi_str
return 0; return 0;
} }
#if !defined(WIN32)
static inline
#else
__forceinline static
#endif
void ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_struct, void ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow) struct ndpi_flow_struct *flow)
{ {
@@ -2986,9 +2811,6 @@ void ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_struct,
const struct ndpi_tcphdr *tcph = packet->tcp; const struct ndpi_tcphdr *tcph = packet->tcp;
const struct ndpi_udphdr *udph = flow->packet.udp; const struct ndpi_udphdr *udph = flow->packet.udp;
//struct ndpi_unique_flow_struct unique_flow;
//uint8_t new_connection;
u_int8_t proxy_enabled = 0; u_int8_t proxy_enabled = 0;
packet->tcp_retransmission = 0, packet->packet_direction = 0; packet->tcp_retransmission = 0, packet->packet_direction = 0;
@@ -3000,7 +2822,7 @@ void ndpi_connection_tracking(struct ndpi_detection_module_struct *ndpi_struct,
packet->packet_direction = 1; packet->packet_direction = 1;
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
if(iphv6 != NULL && NDPI_COMPARE_IPV6_ADDRESS_STRUCTS(&iphv6->saddr, &iphv6->daddr) != 0) if(iphv6 != NULL && NDPI_COMPARE_IPV6_ADDRESS_STRUCTS(&iphv6->ip6_src, &iphv6->ip6_dst) != 0)
packet->packet_direction = 1; packet->packet_direction = 1;
#endif #endif
} }
@@ -3206,8 +3028,7 @@ void check_ndpi_tcp_flow_func(struct ndpi_detection_module_struct *ndpi_struct,
ndpi_struct->callback_buffer[proto_index].excluded_protocol_bitmask) == 0 ndpi_struct->callback_buffer[proto_index].excluded_protocol_bitmask) == 0
&& NDPI_BITMASK_COMPARE(ndpi_struct->callback_buffer[proto_index].detection_bitmask, && NDPI_BITMASK_COMPARE(ndpi_struct->callback_buffer[proto_index].detection_bitmask,
detection_bitmask) != 0 detection_bitmask) != 0
&& (ndpi_struct->callback_buffer[proto_index].ndpi_selection_bitmask && (ndpi_struct->callback_buffer[proto_index].ndpi_selection_bitmask & *ndpi_selection_packet) == ndpi_struct->callback_buffer[proto_index].ndpi_selection_bitmask) {
& *ndpi_selection_packet) == ndpi_struct->callback_buffer[proto_index].ndpi_selection_bitmask) {
if((flow->guessed_protocol_id != NDPI_PROTOCOL_UNKNOWN) if((flow->guessed_protocol_id != NDPI_PROTOCOL_UNKNOWN)
&& (ndpi_struct->proto_defaults[flow->guessed_protocol_id].func != NULL)) && (ndpi_struct->proto_defaults[flow->guessed_protocol_id].func != NULL))
ndpi_struct->proto_defaults[flow->guessed_protocol_id].func(ndpi_struct, flow), ndpi_struct->proto_defaults[flow->guessed_protocol_id].func(ndpi_struct, flow),
@@ -3217,8 +3038,7 @@ void check_ndpi_tcp_flow_func(struct ndpi_detection_module_struct *ndpi_struct,
if(flow->detected_protocol_stack[0] == NDPI_PROTOCOL_UNKNOWN) { if(flow->detected_protocol_stack[0] == NDPI_PROTOCOL_UNKNOWN) {
for(a = 0; a < ndpi_struct->callback_buffer_size_tcp_payload; a++) { for(a = 0; a < ndpi_struct->callback_buffer_size_tcp_payload; a++) {
if((func != ndpi_struct->callback_buffer_tcp_payload[a].func) if((func != ndpi_struct->callback_buffer_tcp_payload[a].func)
&& (ndpi_struct->callback_buffer_tcp_payload[a].ndpi_selection_bitmask && (ndpi_struct->callback_buffer_tcp_payload[a].ndpi_selection_bitmask & *ndpi_selection_packet) == ndpi_struct->callback_buffer_tcp_payload[a].ndpi_selection_bitmask
& *ndpi_selection_packet) == ndpi_struct->callback_buffer_tcp_payload[a].ndpi_selection_bitmask
&& NDPI_BITMASK_COMPARE(flow->excluded_protocol_bitmask, && NDPI_BITMASK_COMPARE(flow->excluded_protocol_bitmask,
ndpi_struct->callback_buffer_tcp_payload[a].excluded_protocol_bitmask) == 0 ndpi_struct->callback_buffer_tcp_payload[a].excluded_protocol_bitmask) == 0
&& NDPI_BITMASK_COMPARE(ndpi_struct->callback_buffer_tcp_payload[a].detection_bitmask, && NDPI_BITMASK_COMPARE(ndpi_struct->callback_buffer_tcp_payload[a].detection_bitmask,
@@ -3275,6 +3095,109 @@ void check_ndpi_flow_func(struct ndpi_detection_module_struct *ndpi_struct,
check_ndpi_other_flow_func(ndpi_struct, flow, ndpi_selection_packet); check_ndpi_other_flow_func(ndpi_struct, flow, ndpi_selection_packet);
} }
/* ********************************************************************************* */
ndpi_protocol ndpi_l4_detection_process_packet(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow,
const struct ndpi_iphdr *iph,
struct ndpi_ipv6hdr *iph6,
struct ndpi_tcphdr *tcp,
struct ndpi_udphdr *udp,
u_int8_t src_to_dst_direction,
u_int8_t l4_proto,
struct ndpi_id_struct *src,
u_int16_t sport,
struct ndpi_id_struct *dst,
u_int16_t dport,
u_int8_t *payload, u_int16_t payload_len) {
NDPI_SELECTION_BITMASK_PROTOCOL_SIZE ndpi_selection_packet;
u_int32_t a;
ndpi_protocol ret = { NDPI_PROTOCOL_UNKNOWN, NDPI_PROTOCOL_UNKNOWN };
if(payload_len == 0) return(ret);
flow->packet.tcp = tcp, flow->packet.udp = udp;
flow->packet.payload = payload, flow->packet.payload_packet_len = payload_len;
if(src_to_dst_direction)
flow->src = src, flow->dst = dst;
else
flow->src = dst, flow->dst = src;
ndpi_selection_packet = NDPI_SELECTION_BITMASK_PROTOCOL_COMPLETE_TRAFFIC;
if((flow->packet.iph = iph) != NULL)
ndpi_selection_packet |= NDPI_SELECTION_BITMASK_PROTOCOL_IP | NDPI_SELECTION_BITMASK_PROTOCOL_IPV4_OR_IPV6;
#ifdef NDPI_DETECTION_SUPPORT_IPV6
else if((flow->packet.iphv6 = iph6) != NULL)
ndpi_selection_packet |= NDPI_SELECTION_BITMASK_PROTOCOL_IPV6 | NDPI_SELECTION_BITMASK_PROTOCOL_IPV4_OR_IPV6;
#endif /* NDPI_DETECTION_SUPPORT_IPV6 */
if(flow->packet.tcp != NULL)
ndpi_selection_packet |=
(NDPI_SELECTION_BITMASK_PROTOCOL_INT_TCP | NDPI_SELECTION_BITMASK_PROTOCOL_INT_TCP_OR_UDP);
if(flow->packet.udp != NULL)
ndpi_selection_packet |=
(NDPI_SELECTION_BITMASK_PROTOCOL_INT_UDP | NDPI_SELECTION_BITMASK_PROTOCOL_INT_TCP_OR_UDP);
if(flow->packet.payload_packet_len != 0) {
ndpi_selection_packet |= NDPI_SELECTION_BITMASK_PROTOCOL_HAS_PAYLOAD;
if(!flow->protocol_id_already_guessed) {
flow->guessed_protocol_id = (int16_t)ndpi_guess_protocol_id(ndpi_struct, l4_proto, sport, dport);
flow->protocol_id_already_guessed = 1;
}
}
if(flow->packet.tcp_retransmission == 0)
ndpi_selection_packet |= NDPI_SELECTION_BITMASK_PROTOCOL_NO_TCP_RETRANSMISSION;
flow->packet.l4_protocol = l4_proto, flow->packet.packet_direction = src_to_dst_direction;
check_ndpi_flow_func(ndpi_struct, flow, &ndpi_selection_packet);
a = flow->packet.detected_protocol_stack[0];
if(NDPI_COMPARE_PROTOCOL_TO_BITMASK(ndpi_struct->detection_bitmask, a) == 0)
a = NDPI_PROTOCOL_UNKNOWN;
if(a != NDPI_PROTOCOL_UNKNOWN) {
int i;
for(i=0; (i<sizeof(flow->host_server_name)) && (flow->host_server_name[i] != '\0'); i++)
flow->host_server_name[i] = tolower(flow->host_server_name[i]);
flow->host_server_name[i] ='\0';
}
ret_protocols:
if(flow->detected_protocol_stack[1] != NDPI_PROTOCOL_UNKNOWN) {
ret.master_protocol = flow->detected_protocol_stack[1], ret.protocol = flow->detected_protocol_stack[0];
if(ret.protocol == ret.master_protocol)
ret.master_protocol = NDPI_PROTOCOL_UNKNOWN;
} else
ret.protocol = flow->detected_protocol_stack[0];
if((ret.protocol == NDPI_PROTOCOL_UNKNOWN)
&& flow->packet.iph
&& (!flow->host_already_guessed)) {
if((flow->guessed_host_proto_id = ndpi_network_ptree_match(ndpi_struct,
(struct in_addr *)&flow->packet.iph->saddr)) == NDPI_PROTOCOL_UNKNOWN) {
flow->guessed_host_proto_id = ndpi_network_ptree_match(ndpi_struct, (struct in_addr *)&flow->packet.iph->daddr);
}
flow->host_already_guessed = 1;
}
if((ret.protocol == NDPI_PROTOCOL_UNKNOWN) && (ret.master_protocol != NDPI_PROTOCOL_UNKNOWN))
ret.protocol = flow->guessed_host_proto_id;
return(ret);
}
/* ********************************************************************************* */
ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct *ndpi_struct, ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct *ndpi_struct,
struct ndpi_flow_struct *flow, struct ndpi_flow_struct *flow,
const unsigned char *packet, const unsigned char *packet,
@@ -3302,15 +3225,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
} }
flow->packet.tick_timestamp_l = current_tick_l; flow->packet.tick_timestamp_l = current_tick_l;
#ifdef __KERNEL__
{
u_int64_t d = current_tick_l;
do_div(d,1000);
flow->packet.tick_timestamp = d;
}
#else
flow->packet.tick_timestamp = (u_int32_t)current_tick_l/1000; flow->packet.tick_timestamp = (u_int32_t)current_tick_l/1000;
#endif
/* parse packet */ /* parse packet */
flow->packet.iph = (struct ndpi_iphdr *)packet; flow->packet.iph = (struct ndpi_iphdr *)packet;
@@ -3361,7 +3276,7 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
if(flow->packet.iphv6 != NULL) { if(flow->packet.iphv6 != NULL) {
protocol = flow->packet.iphv6->nexthdr, saddr = 0, daddr = 0; protocol = flow->packet.iphv6->ip6_ctlun.ip6_un1.ip6_un1_nxt, saddr = 0, daddr = 0;
} else } else
#endif #endif
{ {
@@ -3383,15 +3298,6 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
} }
} }
#if 0
a = flow->detected_protocol_stack[0];
if(a != NDPI_PROTOCOL_UNKNOWN) {
NDPI_LOG(NDPI_PROTOCOL_UNKNOWN, ndpi_struct, NDPI_LOG_TRACE, "PROCESS KNOWN PROTOCOL\n");
ndpi_struct->proto_defaults[a].func(ndpi_struct, flow);
return a;
}
#endif
check_ndpi_flow_func(ndpi_struct, flow, &ndpi_selection_packet); check_ndpi_flow_func(ndpi_struct, flow, &ndpi_selection_packet);
a = flow->packet.detected_protocol_stack[0]; a = flow->packet.detected_protocol_stack[0];
@@ -3416,22 +3322,20 @@ ndpi_protocol ndpi_detection_process_packet(struct ndpi_detection_module_struct
} else } else
ret.protocol = flow->detected_protocol_stack[0]; ret.protocol = flow->detected_protocol_stack[0];
if((ret.protocol == NDPI_PROTOCOL_UNKNOWN)
&& flow->packet.iph
&& (!flow->host_already_guessed)) {
if((ret.master_protocol == NDPI_PROTOCOL_UNKNOWN) && flow->packet.iph) { if((flow->guessed_host_proto_id = ndpi_network_ptree_match(ndpi_struct, (struct in_addr *)&flow->packet.iph->saddr)) == NDPI_PROTOCOL_UNKNOWN) {
struct ndpi_packet_struct *packet = &flow->packet; flow->guessed_host_proto_id = ndpi_network_ptree_match(ndpi_struct, (struct in_addr *)&flow->packet.iph->daddr);
if((ret.master_protocol = ndpi_network_ptree_match(ndpi_struct, (struct in_addr *)&packet->iph->saddr)) == NDPI_PROTOCOL_UNKNOWN)
ret.master_protocol = ndpi_network_ptree_match(ndpi_struct, (struct in_addr *)&packet->iph->daddr);
/* Swap proocols in case of success */
if(ret.master_protocol != NDPI_PROTOCOL_UNKNOWN) {
u_int16_t t = ret.master_protocol;
ret.master_protocol = ret.protocol;
ret.protocol = t;
} }
flow->host_already_guessed = 1;
} }
if((ret.protocol == NDPI_PROTOCOL_UNKNOWN) && (ret.master_protocol != NDPI_PROTOCOL_UNKNOWN))
ret.protocol = flow->guessed_host_proto_id;
return(ret); return(ret);
} }
@@ -4042,39 +3946,50 @@ int NDPI_PROTOCOL_IP_is_set(const ndpi_ip_addr_t * ip)
/* NTOP */ /* NTOP */
int ndpi_packet_src_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip) int ndpi_packet_src_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip)
{ {
#ifdef NDPI_DETECTION_SUPPORT_IPV6
if(packet->iphv6 != NULL) {
if(packet->iphv6->saddr.ndpi_v6_u.u6_addr64[0] == ip->ipv6.ndpi_v6_u.u6_addr64[0] &&
packet->iphv6->saddr.ndpi_v6_u.u6_addr64[1] == ip->ipv6.ndpi_v6_u.u6_addr64[1]) {
#ifdef NDPI_DETECTION_SUPPORT_IPV6
/* IPv6 */
if(packet->iphv6 != NULL) {
if(packet->iphv6->ip6_src.u6_addr.u6_addr32[0] == ip->ipv6.u6_addr.u6_addr32[0] &&
packet->iphv6->ip6_src.u6_addr.u6_addr32[1] == ip->ipv6.u6_addr.u6_addr32[1] &&
packet->iphv6->ip6_src.u6_addr.u6_addr32[2] == ip->ipv6.u6_addr.u6_addr32[2] &&
packet->iphv6->ip6_src.u6_addr.u6_addr32[3] == ip->ipv6.u6_addr.u6_addr32[3])
return 1; return 1;
} else { //else
return 0; return 0;
} }
}
#endif #endif
if(packet->iph->saddr == ip->ipv4) {
/* IPv4 */
if(packet->iph->saddr == ip->ipv4)
return 1; return 1;
}
return 0; return 0;
} }
/* check if the destination ip address in packet and ip are equal */ /* check if the destination ip address in packet and ip are equal */
int ndpi_packet_dst_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip) int ndpi_packet_dst_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_ip_addr_t * ip)
{ {
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
/* IPv6 */
if(packet->iphv6 != NULL) { if(packet->iphv6 != NULL) {
if(packet->iphv6->daddr.ndpi_v6_u.u6_addr64[0] == ip->ipv6.ndpi_v6_u.u6_addr64[0] &&
packet->iphv6->daddr.ndpi_v6_u.u6_addr64[1] == ip->ipv6.ndpi_v6_u.u6_addr64[1]) { if(packet->iphv6->ip6_dst.u6_addr.u6_addr32[0] == ip->ipv6.u6_addr.u6_addr32[0] &&
packet->iphv6->ip6_dst.u6_addr.u6_addr32[1] == ip->ipv6.u6_addr.u6_addr32[1] &&
packet->iphv6->ip6_dst.u6_addr.u6_addr32[2] == ip->ipv6.u6_addr.u6_addr32[2] &&
packet->iphv6->ip6_dst.u6_addr.u6_addr32[3] == ip->ipv6.u6_addr.u6_addr32[3])
return 1; return 1;
} else { //else
return 0; return 0;
} }
}
#endif #endif
if(packet->iph->daddr == ip->ipv4) {
/* IPv4 */
if(packet->iph->saddr == ip->ipv4)
return 1; return 1;
}
return 0; return 0;
} }
@@ -4083,12 +3998,21 @@ int ndpi_packet_dst_ip_eql(const struct ndpi_packet_struct *packet, const ndpi_i
void ndpi_packet_src_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip) void ndpi_packet_src_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip)
{ {
NDPI_PROTOCOL_IP_clear(ip); NDPI_PROTOCOL_IP_clear(ip);
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
/* IPv6 */
if(packet->iphv6 != NULL) { if(packet->iphv6 != NULL) {
ip->ipv6.ndpi_v6_u.u6_addr64[0] = packet->iphv6->saddr.ndpi_v6_u.u6_addr64[0];
ip->ipv6.ndpi_v6_u.u6_addr64[1] = packet->iphv6->saddr.ndpi_v6_u.u6_addr64[1]; ip->ipv6.u6_addr.u6_addr32[0] = packet->iphv6->ip6_src.u6_addr.u6_addr32[0];
ip->ipv6.u6_addr.u6_addr32[1] = packet->iphv6->ip6_src.u6_addr.u6_addr32[1];
ip->ipv6.u6_addr.u6_addr32[2] = packet->iphv6->ip6_src.u6_addr.u6_addr32[2];
ip->ipv6.u6_addr.u6_addr32[3] = packet->iphv6->ip6_src.u6_addr.u6_addr32[3];
} else } else
#endif #endif
/* IPv4 */
ip->ipv4 = packet->iph->saddr; ip->ipv4 = packet->iph->saddr;
} }
@@ -4097,12 +4021,20 @@ void ndpi_packet_src_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_add
void ndpi_packet_dst_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip) void ndpi_packet_dst_ip_get(const struct ndpi_packet_struct *packet, ndpi_ip_addr_t * ip)
{ {
NDPI_PROTOCOL_IP_clear(ip); NDPI_PROTOCOL_IP_clear(ip);
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
if(packet->iphv6 != NULL) { if(packet->iphv6 != NULL) {
ip->ipv6.ndpi_v6_u.u6_addr64[0] = packet->iphv6->daddr.ndpi_v6_u.u6_addr64[0];
ip->ipv6.ndpi_v6_u.u6_addr64[1] = packet->iphv6->daddr.ndpi_v6_u.u6_addr64[1]; ip->ipv6.u6_addr.u6_addr32[0] = packet->iphv6->ip6_dst.u6_addr.u6_addr32[0];
ip->ipv6.u6_addr.u6_addr32[1] = packet->iphv6->ip6_dst.u6_addr.u6_addr32[1];
ip->ipv6.u6_addr.u6_addr32[2] = packet->iphv6->ip6_dst.u6_addr.u6_addr32[2];
ip->ipv6.u6_addr.u6_addr32[3] = packet->iphv6->ip6_dst.u6_addr.u6_addr32[3];
} else } else
#endif #endif
ip->ipv4 = packet->iph->daddr; ip->ipv4 = packet->iph->daddr;
} }
@@ -4116,15 +4048,22 @@ char *ndpi_get_ip_string(struct ndpi_detection_module_struct *ndpi_struct,
const u_int8_t *a = (const u_int8_t *) &ip->ipv4; const u_int8_t *a = (const u_int8_t *) &ip->ipv4;
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
if(ip->ipv6.ndpi_v6_u.u6_addr32[1] != 0 || ip->ipv6.ndpi_v6_u.u6_addr64[1] != 0) { if(ip->ipv6.u6_addr.u6_addr32[0] != 0 ||
const u_int16_t *b = ip->ipv6.ndpi_v6_u.u6_addr16; ip->ipv6.u6_addr.u6_addr32[1] != 0 ||
ip->ipv6.u6_addr.u6_addr32[1] != 0 ||
ip->ipv6.u6_addr.u6_addr32[1] != 0) {
const u_int16_t *b = ip->ipv6.u6_addr.u6_addr16;
snprintf(ndpi_struct->ip_string, 32, "%x:%x:%x:%x:%x:%x:%x:%x", snprintf(ndpi_struct->ip_string, 32, "%x:%x:%x:%x:%x:%x:%x:%x",
ntohs(b[0]), ntohs(b[1]), ntohs(b[2]), ntohs(b[3]), ntohs(b[0]), ntohs(b[1]), ntohs(b[2]), ntohs(b[3]),
ntohs(b[4]), ntohs(b[5]), ntohs(b[6]), ntohs(b[7])); ntohs(b[4]), ntohs(b[5]), ntohs(b[6]), ntohs(b[7]));
return ndpi_struct->ip_string; return ndpi_struct->ip_string;
} }
#endif #endif
snprintf(ndpi_struct->ip_string, 32, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]); snprintf(ndpi_struct->ip_string, 32, "%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
return ndpi_struct->ip_string; return ndpi_struct->ip_string;
} }
@@ -4159,16 +4098,6 @@ u_int16_t ntohs_ndpi_bytestream_to_number(const u_int8_t * str, u_int16_t max_ch
/* ****************************************************** */ /* ****************************************************** */
#if 0
#ifndef __KERNEL__
static u_int is_port(u_int16_t sport, u_int16_t dport, u_int16_t match_port) {
return(((match_port == sport) || (match_port == dport)) ? 1 : 0);
}
#endif
#endif
/* ****************************************************** */
ndpi_protocol ndpi_find_port_based_protocol(struct ndpi_detection_module_struct *ndpi_struct /* NOTUSED */, ndpi_protocol ndpi_find_port_based_protocol(struct ndpi_detection_module_struct *ndpi_struct /* NOTUSED */,
u_int8_t proto, u_int8_t proto,
u_int32_t shost, u_int16_t sport, u_int32_t shost, u_int16_t sport,
@@ -4235,11 +4164,11 @@ ndpi_protocol ndpi_guess_undetected_protocol(struct ndpi_detection_module_struct
return(ret); return(ret);
check_guessed_skype: check_guessed_skype:
addr.s_addr = shost; addr.s_addr = htonl(shost);
if(ndpi_network_ptree_match(ndpi_struct, &addr) == NDPI_PROTOCOL_SKYPE) { if(ndpi_network_ptree_match(ndpi_struct, &addr) == NDPI_PROTOCOL_SKYPE) {
ret.protocol = NDPI_PROTOCOL_SKYPE; ret.protocol = NDPI_PROTOCOL_SKYPE;
} else { } else {
addr.s_addr = dhost; addr.s_addr = htonl(dhost);
if(ndpi_network_ptree_match(ndpi_struct, &addr) == NDPI_PROTOCOL_SKYPE) if(ndpi_network_ptree_match(ndpi_struct, &addr) == NDPI_PROTOCOL_SKYPE)
ret.protocol = NDPI_PROTOCOL_SKYPE; ret.protocol = NDPI_PROTOCOL_SKYPE;
} }
@@ -4473,11 +4402,7 @@ void ndpi_free_flow(struct ndpi_flow_struct *flow) {
/* ****************************************************** */ /* ****************************************************** */
#ifndef __KERNEL__ char* ndpi_revision() { return(NDPI_GIT_RELEASE); }
char* ndpi_revision() {
return(NDPI_GIT_RELEASE);
}
#endif
/* ****************************************************** */ /* ****************************************************** */
@@ -4553,45 +4478,3 @@ void NDPI_DUMP_BITMASK(NDPI_PROTOCOL_BITMASK a) {
printf("\n"); printf("\n");
} }
#ifdef WIN32
/* http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/libkern/strsep.c */
/*
* Get next token from string *stringp, where tokens are possibly-empty
* strings separated by characters from delim.
*
* Writes NULs into the string at *stringp to end tokens.
* delim need not remain constant from call to call.
* On return, *stringp points past the last NUL written (if there might
* be further tokens), or is NULL (if there are definitely no more tokens).
*
* If *stringp is NULL, strsep returns NULL.
*/
char* strsep(char **stringp, const char *delim) {
char *s;
const char *spanp;
int c, sc;
char *tok;
if((s = *stringp) == NULL)
return (NULL);
for(tok = s;;) {
c = *s++;
spanp = delim;
do {
if((sc = *spanp++) == c) {
if(c == 0)
s = NULL;
else
s[-1] = 0;
*stringp = s;
return (tok);
}
} while (sc != 0);
}
/* NOTREACHED */
}
#endif
+1 -1
View File
@@ -250,7 +250,7 @@ void ndpi_search_dns(struct ndpi_detection_module_struct *ndpi_struct, struct nd
char a_buf[32]; char a_buf[32];
int i; int i;
for(i=0; i<num_a_records; i++) { for(i=0; j < sizeof(flow->host_server_name)-1 && i < num_a_records; i++) {
j += snprintf((char*)&flow->host_server_name[j], sizeof(flow->host_server_name)-1-j, "%s%s", j += snprintf((char*)&flow->host_server_name[j], sizeof(flow->host_server_name)-1-j, "%s%s",
(i == 0) ? "@" : ";", (i == 0) ? "@" : ";",
ndpi_intoa_v4(a_record[i], a_buf, sizeof(a_buf))); ndpi_intoa_v4(a_record[i], a_buf, sizeof(a_buf)));
+1 -1
View File
@@ -56,7 +56,7 @@ void ndpi_search_kakaotalk_voice(struct ndpi_detection_module_struct *ndpi_struc
} }
} }
NDPI_LOG(NDPI_PROTOCOL_KAKAOTALK_VOICE, ndpi_struct, NDPI_LOG_DEBUG, "Exclude kakaotalk_voice.\n"); NDPI_LOG(NDPI_SERVICE_KAKAOTALK_VOICE, ndpi_struct, NDPI_LOG_DEBUG, "Exclude kakaotalk_voice.\n");
NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_SERVICE_KAKAOTALK_VOICE); NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_SERVICE_KAKAOTALK_VOICE);
} }
+4 -4
View File
@@ -117,10 +117,10 @@ void ndpi_search_mail_smtp_tcp(struct ndpi_detection_module_struct
&& (packet->line[a].ptr[1] == 'T' || packet->line[a].ptr[1] == 't') && (packet->line[a].ptr[1] == 'T' || packet->line[a].ptr[1] == 't')
&& (packet->line[a].ptr[2] == 'A' || packet->line[a].ptr[2] == 'a') && (packet->line[a].ptr[2] == 'A' || packet->line[a].ptr[2] == 'a')
&& (packet->line[a].ptr[3] == 'R' || packet->line[a].ptr[3] == 'r') && (packet->line[a].ptr[3] == 'R' || packet->line[a].ptr[3] == 'r')
&& (packet->line[a].ptr[4] == 'T' || packet->line[a].ptr[0] == 't') && (packet->line[a].ptr[4] == 'T' || packet->line[a].ptr[4] == 't')
&& (packet->line[a].ptr[5] == 'T' || packet->line[a].ptr[1] == 't') && (packet->line[a].ptr[5] == 'T' || packet->line[a].ptr[5] == 't')
&& (packet->line[a].ptr[6] == 'L' || packet->line[a].ptr[2] == 'l') && (packet->line[a].ptr[6] == 'L' || packet->line[a].ptr[6] == 'l')
&& (packet->line[a].ptr[7] == 'S' || packet->line[a].ptr[3] == 's')) { && (packet->line[a].ptr[7] == 'S' || packet->line[a].ptr[7] == 's')) {
flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_STARTTLS; flow->l4.tcp.smtp_command_bitmask |= SMTP_BIT_STARTTLS;
} }
} }
+1 -1
View File
@@ -122,7 +122,7 @@ void ndpi_search_mdns(struct ndpi_detection_module_struct *ndpi_struct, struct n
} }
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
if (packet->iphv6 != NULL) { if (packet->iphv6 != NULL) {
const u_int32_t *daddr = packet->iphv6->daddr.ndpi_v6_u.u6_addr32; const u_int32_t *daddr = packet->iphv6->ip6_dst.u6_addr.u6_addr32;
if (daddr[0] == htonl(0xff020000) && daddr[1] == 0 && daddr[2] == 0 && daddr[3] == htonl(0xfb)) { if (daddr[0] == htonl(0xff020000) && daddr[1] == 0 && daddr[2] == 0 && daddr[3] == htonl(0xfb)) {
NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct, NDPI_LOG(NDPI_PROTOCOL_MDNS, ndpi_struct,
+2 -2
View File
@@ -40,8 +40,8 @@ u_int8_t is_skype_flow(struct ndpi_detection_module_struct *ndpi_struct,
Skype connections are identified by some SSL-like communications Skype connections are identified by some SSL-like communications
without SSL certificate being exchanged without SSL certificate being exchanged
*/ */
if(is_skype_host(ndpi_struct, ntohl(packet->iph->saddr)) if(is_skype_host(ndpi_struct, packet->iph->saddr)
|| is_skype_host(ndpi_struct, ntohl(packet->iph->daddr))) { || is_skype_host(ndpi_struct, packet->iph->daddr)) {
return(1); return(1);
} }
} }
+6 -3
View File
@@ -37,9 +37,9 @@ u_int ndpi_search_tcp_or_udp_raw(struct ndpi_detection_module_struct *ndpi_struc
} }
} }
if((rc = ndpi_host_ptree_match(ndpi_struct, saddr)) != NDPI_PROTOCOL_UNKNOWN) return(rc); if((rc = ndpi_host_ptree_match(ndpi_struct, htonl(saddr))) != NDPI_PROTOCOL_UNKNOWN) return(rc);
return(ndpi_host_ptree_match(ndpi_struct, daddr)); return(ndpi_host_ptree_match(ndpi_struct, htonl(daddr)));
} }
void ndpi_search_tcp_or_udp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) void ndpi_search_tcp_or_udp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow)
@@ -48,6 +48,9 @@ void ndpi_search_tcp_or_udp(struct ndpi_detection_module_struct *ndpi_struct, st
u_int proto; u_int proto;
struct ndpi_packet_struct *packet = &flow->packet; struct ndpi_packet_struct *packet = &flow->packet;
if(flow->host_server_name[0] != '\0')
return;
if(ndpi_is_tor_flow(ndpi_struct, flow)) { if(ndpi_is_tor_flow(ndpi_struct, flow)) {
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TOR, NDPI_PROTOCOL_UNKNOWN); ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TOR, NDPI_PROTOCOL_UNKNOWN);
return; return;
@@ -61,7 +64,7 @@ void ndpi_search_tcp_or_udp(struct ndpi_detection_module_struct *ndpi_struct, st
proto = ndpi_search_tcp_or_udp_raw(ndpi_struct, proto = ndpi_search_tcp_or_udp_raw(ndpi_struct,
flow->packet.iph ? flow->packet.iph->protocol : flow->packet.iph ? flow->packet.iph->protocol :
#ifdef NDPI_DETECTION_SUPPORT_IPV6 #ifdef NDPI_DETECTION_SUPPORT_IPV6
flow->packet.iphv6->nexthdr, flow->packet.iphv6->ip6_ctlun.ip6_un1.ip6_un1_nxt,
#else #else
0, 0,
#endif #endif
+2 -2
View File
@@ -1,3 +1,3 @@
NTP 1 90 1 Quic 1 90 1
1 UDP 78.46.76.2:80 <-> 175.144.140.29:123 [proto: 9/NTP][1 pkts/90 bytes] 1 UDP 78.46.76.2:80 <-> 175.144.140.29:123 [proto: 188/Quic][1 pkts/90 bytes]
-5
View File
@@ -1,5 +0,0 @@
Place here test results for pcaps used for regressions testing
Example
for pcap/myprotocol.pcap add result/myprotocol.result
+6 -5
View File
@@ -4,7 +4,8 @@ HTTP 450 294880 19
SSDP 11 4984 1 SSDP 11 4984 1
WorldOfWarcraft 9 880 1 WorldOfWarcraft 9 880 1
IGMP 2 120 1 IGMP 2 120 1
SSL 43 2903 13 SSL 27 1803 9
Skype 16 1100 4
Google 12 1467 2 Google 12 1467 2
Quic 6 475 1 Quic 6 475 1
Starcraft 236 51494 6 Starcraft 236 51494 6
@@ -27,10 +28,10 @@ Starcraft 236 51494 6
16 TCP 192.168.1.100:3530 <-> 2.228.46.112:80 [proto: 7/HTTP][29 pkts/25102 bytes][Host: bnetcmsus-a.akamaihd.net] 16 TCP 192.168.1.100:3530 <-> 2.228.46.112:80 [proto: 7/HTTP][29 pkts/25102 bytes][Host: bnetcmsus-a.akamaihd.net]
17 TCP 192.168.1.100:3532 <-> 2.228.46.112:80 [proto: 7/HTTP][4 pkts/386 bytes] 17 TCP 192.168.1.100:3532 <-> 2.228.46.112:80 [proto: 7/HTTP][4 pkts/386 bytes]
18 TCP 192.168.1.100:3534 <-> 2.228.46.112:80 [proto: 7/HTTP][1 pkts/66 bytes] 18 TCP 192.168.1.100:3534 <-> 2.228.46.112:80 [proto: 7/HTTP][1 pkts/66 bytes]
19 TCP 192.168.1.100:3489 <-> 2.228.46.104:443 [proto: 91/SSL][4 pkts/275 bytes] 19 TCP 192.168.1.100:3489 <-> 2.228.46.104:443 [proto: 125/Skype][4 pkts/275 bytes]
20 TCP 192.168.1.100:3481 <-> 2.228.46.114:443 [proto: 91/SSL][4 pkts/275 bytes] 20 TCP 192.168.1.100:3481 <-> 2.228.46.114:443 [proto: 91/SSL][4 pkts/275 bytes]
21 TCP 192.168.1.100:3479 <-> 2.228.46.114:443 [proto: 91/SSL][4 pkts/275 bytes] 21 TCP 192.168.1.100:3479 <-> 2.228.46.114:443 [proto: 91/SSL][4 pkts/275 bytes]
22 TCP 192.168.1.100:3491 <-> 2.228.46.104:443 [proto: 91/SSL][4 pkts/275 bytes] 22 TCP 192.168.1.100:3491 <-> 2.228.46.104:443 [proto: 125/Skype][4 pkts/275 bytes]
23 TCP 80.239.186.26:80 <-> 192.168.1.100:3515 [proto: 7/HTTP][10 pkts/1224 bytes][Host: nydus.battle.net] 23 TCP 80.239.186.26:80 <-> 192.168.1.100:3515 [proto: 7/HTTP][10 pkts/1224 bytes][Host: nydus.battle.net]
24 TCP 80.239.186.21:80 <-> 192.168.1.100:3519 [proto: 7/HTTP][9 pkts/979 bytes][Host: eu.launcher.battle.net] 24 TCP 80.239.186.21:80 <-> 192.168.1.100:3519 [proto: 7/HTTP][9 pkts/979 bytes][Host: eu.launcher.battle.net]
25 TCP 80.239.186.26:80 <-> 192.168.1.100:3521 [proto: 7/HTTP][10 pkts/1224 bytes][Host: nydus.battle.net] 25 TCP 80.239.186.26:80 <-> 192.168.1.100:3521 [proto: 7/HTTP][10 pkts/1224 bytes][Host: nydus.battle.net]
@@ -54,8 +55,8 @@ Starcraft 236 51494 6
43 TCP 192.168.1.100:3529 <-> 2.228.46.112:80 [proto: 7/HTTP][29 pkts/25102 bytes][Host: bnetcmsus-a.akamaihd.net] 43 TCP 192.168.1.100:3529 <-> 2.228.46.112:80 [proto: 7/HTTP][29 pkts/25102 bytes][Host: bnetcmsus-a.akamaihd.net]
44 TCP 192.168.1.100:3531 <-> 2.228.46.112:80 [proto: 7/HTTP][29 pkts/25102 bytes][Host: bnetcmsus-a.akamaihd.net] 44 TCP 192.168.1.100:3531 <-> 2.228.46.112:80 [proto: 7/HTTP][29 pkts/25102 bytes][Host: bnetcmsus-a.akamaihd.net]
45 TCP 192.168.1.100:3533 <-> 2.228.46.112:80 [proto: 7/HTTP][4 pkts/386 bytes] 45 TCP 192.168.1.100:3533 <-> 2.228.46.112:80 [proto: 7/HTTP][4 pkts/386 bytes]
46 TCP 192.168.1.100:3492 <-> 2.228.46.104:443 [proto: 91/SSL][4 pkts/275 bytes] 46 TCP 192.168.1.100:3492 <-> 2.228.46.104:443 [proto: 125/Skype][4 pkts/275 bytes]
47 TCP 192.168.1.100:3490 <-> 2.228.46.104:443 [proto: 91/SSL][4 pkts/275 bytes] 47 TCP 192.168.1.100:3490 <-> 2.228.46.104:443 [proto: 125/Skype][4 pkts/275 bytes]
48 TCP 192.168.1.100:3482 <-> 2.228.46.114:443 [proto: 91/SSL][4 pkts/275 bytes] 48 TCP 192.168.1.100:3482 <-> 2.228.46.114:443 [proto: 91/SSL][4 pkts/275 bytes]
49 TCP 192.168.1.100:3480 <-> 2.228.46.114:443 [proto: 91/SSL][4 pkts/275 bytes] 49 TCP 192.168.1.100:3480 <-> 2.228.46.114:443 [proto: 91/SSL][4 pkts/275 bytes]
50 TCP 12.129.222.54:80 <-> 192.168.1.100:3512 [proto: 7.76/HTTP.WorldOfWarcraft][9 pkts/880 bytes][Host: us.scan.worldofwarcraft.com] 50 TCP 12.129.222.54:80 <-> 192.168.1.100:3512 [proto: 7.76/HTTP.WorldOfWarcraft][9 pkts/880 bytes][Host: us.scan.worldofwarcraft.com]