-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathc.lua
More file actions
147 lines (119 loc) · 5.52 KB
/
c.lua
File metadata and controls
147 lines (119 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /usr/bin/env luajit
--
-- c.lua
-- Copyright (C) 2016-2017 Adrian Perez <aperez@igalia.com>
--
-- Distributed under terms of the Apache License 2.0.
--
local ffi = require "ffi"
--
-- Common definitions for all supported nDPI versions.
--
ffi.cdef [[
void* malloc(size_t size);
void free(void *ptr);
typedef enum {
NDPI_PROTOCOL_SAFE,
NDPI_PROTOCOL_ACCEPTABLE,
NDPI_PROTOCOL_FUN,
NDPI_PROTOCOL_UNSAFE,
NDPI_PROTOCOL_POTENTIALLY_DANGEROUS,
NDPI_PROTOCOL_UNRATED,
} ndpi_protocol_breed_t;
typedef struct ndpi_id_struct ndpi_id_t;
typedef struct ndpi_flow_struct ndpi_flow_t;
typedef struct ndpi_detection_module_struct ndpi_detection_module_t;
typedef struct ndpi_protocol_bitmask_struct ndpi_protocol_bitmask_t;
typedef struct { uint16_t master_protocol, protocol; } ndpi_protocol_t;
const char* ndpi_revision (void);
uint32_t ndpi_detection_get_sizeof_ndpi_flow_struct (void);
void ndpi_free_flow (ndpi_flow_t *flow);
uint32_t ndpi_detection_get_sizeof_ndpi_id_struct (void);
void ndpi_set_protocol_detection_bitmask2 (ndpi_detection_module_t *detection_module,
const ndpi_protocol_bitmask_t *bitmask);
ndpi_protocol_t ndpi_detection_process_packet (ndpi_detection_module_t *detection_module,
ndpi_flow_t *flow,
const uint8_t *packet,
unsigned short packetlen,
uint64_t current_tick,
ndpi_id_t *src,
ndpi_id_t *dst);
void ndpi_detection_process_extra_packet (ndpi_detection_module_t *detection_module,
ndpi_flow_t *flow,
const uint8_t *packet,
unsigned short packetlen,
uint64_t current_tick,
ndpi_id_t *src,
ndpi_id_t *dst);
void ndpi_dump_protocols (ndpi_detection_module_t *detection_module);
int ndpi_load_protocols_file (ndpi_detection_module_t *detection_module,
const char *path);
ndpi_protocol_t ndpi_guess_undetected_protocol (ndpi_detection_module_t *detection_module,
uint8_t protocol,
uint32_t src_host, uint16_t src_port,
uint32_t dst_host, uint32_t dst_port);
ndpi_protocol_breed_t ndpi_get_proto_breed (ndpi_detection_module_t *detection_module,
uint16_t protocol);
const char* ndpi_get_proto_breed_name (ndpi_detection_module_t *detection_module,
ndpi_protocol_breed_t breed_id);
const char* ndpi_get_proto_name (ndpi_detection_module_t *detection_module,
uint16_t protocol_id);
int ndpi_get_protocol_id (ndpi_detection_module_t *detection_module, const char *name);
]]
local lib = ffi.load("ndpi")
local lib_version = (function ()
local string = ffi.string(lib.ndpi_revision())
local major, minor, patch = string.match(string, "^(%d+)%.(%d+)%.(%d+)")
return setmetatable({
major = tonumber(major);
minor = tonumber(minor);
patch = tonumber(patch);
}, {
__tostring = function (self) return string end;
})
end)()
-- Only nDPI 1.x versions above 1.7 are supported.
if lib_version.major == 1 and lib_version.minor < 7 then
error("Unsupported nDPI version: " .. tostring(lib_version))
end
if lib_version.major == 1 and lib_version.minor == 7 then
-- nDPI 1.7
ffi.cdef [[
ndpi_detection_module_t* ndpi_init_detection_module (uint32_t ticks_per_second,
void *ndpi_malloc,
void *ndpi_free,
void *ndpi_debug);
void ndpi_exit_detection_module (ndpi_detection_module_t *detection_module,
void *ndpi_free__dummy);
ndpi_protocol_t ndpi_find_port_based_protocol (ndpi_detection_module_t *detection_module,
uint8_t protocol,
uint32_t src_host, uint16_t src_port,
uint32_t dst_host, uint32_t dst_port);
]]
else
-- nDPI 1.8 and later
if lib_version.major >= 3 then
ffi.cdef [[
ndpi_detection_module_t* ndpi_init_detection_module (uint32_t prefs);
void ndpi_finalize_initalization(ndpi_detection_module_t *detection_module);
]]
else
ffi.cdef [[
ndpi_detection_module_t* ndpi_init_detection_module (void);
]]
end
ffi.cdef [[
void ndpi_exit_detection_module (ndpi_detection_module_t *detection_module);
ndpi_protocol_t ndpi_find_port_based_protocol (ndpi_detection_module_t *detection_module,
uint32_t src_host, uint16_t src_port,
uint32_t dst_host, uint32_t dst_port);
]]
end
if lib_version.major >= 3 then
-- nDPI 3.0 and later
ffi.cdef [[
uint8_t ndpi_extra_dissection_possible (ndpi_detection_module_t *detection_module,
ndpi_flow_t *flow);
]]
end
return { version = lib_version, lib = lib }