Merge pull request #825 from aouinizied/fix_python_bindings

Update Python cffi bindings.
This commit is contained in:
Luca Deri
2020-01-05 18:47:43 +01:00
committed by GitHub
+34 -12
View File
@@ -16,6 +16,7 @@ If not, see <http://www.gnu.org/licenses/>.
from os.path import abspath, dirname from os.path import abspath, dirname
import cffi import cffi
import sys
cc = """ cc = """
typedef enum { typedef enum {
@@ -977,12 +978,17 @@ struct ndpi_flow_struct {
*/ */
struct { struct {
ndpi_http_method method; ndpi_http_method method;
char *url, *content_type; char *url, *content_type, *user_agent;
uint8_t num_request_headers, num_response_headers; uint8_t num_request_headers, num_response_headers;
uint8_t request_version; /* 0=1.0 and 1=1.1. Create an enum for this? */ uint8_t request_version; /* 0=1.0 and 1=1.1. Create an enum for this? */
uint16_t response_status_code; /* 200, 404, etc. */ uint16_t response_status_code; /* 200, 404, etc. */
} http; } http;
struct {
char *pktbuf;
uint16_t pktbuf_maxlen, pktbuf_currlen;
} kerberos_buf;
union { union {
/* the only fields useful for nDPI and ntopng */ /* the only fields useful for nDPI and ntopng */
struct { struct {
@@ -996,22 +1002,22 @@ struct ndpi_flow_struct {
uint8_t version; uint8_t version;
} ntp; } ntp;
struct { struct {
char cname[24], realm[24]; char hostname[48], domain[48], username[48];
} kerberos; } kerberos;
struct { struct {
struct { struct {
uint16_t ssl_version; uint16_t ssl_version;
char client_certificate[64], server_certificate[64], server_organization[64]; char client_certificate[64], server_certificate[64], server_organization[64];
uint32_t notBefore, notAfter; uint32_t notBefore, notAfter;
char ja3_client[33], ja3_server[33]; char ja3_client[33], ja3_server[33];
uint16_t server_cipher; uint16_t server_cipher;
ndpi_cipher_weakness server_unsafe_cipher; ndpi_cipher_weakness server_unsafe_cipher;
} ssl; } ssl;
struct { struct {
uint8_t num_udp_pkts, num_processed_pkts, num_binding_requests; uint8_t num_udp_pkts, num_processed_pkts, num_binding_requests;
} stun; } stun;
/* We can have STUN over SSL/TLS thus they need to live together */ /* We can have STUN over SSL/TLS thus they need to live together */
@@ -1026,6 +1032,12 @@ struct ndpi_flow_struct {
uint8_t last_one_byte_pkt, last_byte; uint8_t last_one_byte_pkt, last_byte;
} imo; } imo;
struct {
uint8_t username_detected:1, username_found:1, password_detected:1, password_found:1, _pad:4;
uint8_t character_id;
char username[32], password[32];
} telnet;
struct { struct {
char answer[96]; char answer[96];
} mdns; } mdns;
@@ -1041,6 +1053,11 @@ struct ndpi_flow_struct {
uint8_t nat_ip[24]; uint8_t nat_ip[24];
} http; } http;
struct {
uint8_t auth_found:1, auth_failed:1, _pad:5;
char username[16], password[16];
} ftp_imap_pop_smtp;
struct { struct {
/* Bittorrent hash */ /* Bittorrent hash */
uint8_t hash[20]; uint8_t hash[20];
@@ -1211,10 +1228,13 @@ void ndpi_finalize_initalization(struct ndpi_detection_module_struct *ndpi_str);
class NDPI(): class NDPI():
def __init__(self, libpath=None): def __init__(self, libpath=None, max_tcp_dissections=10, max_udp_dissections=16):
self._ffi = cffi.FFI() self._ffi = cffi.FFI()
if libpath is None: if libpath is None:
self._ndpi = self._ffi.dlopen(dirname(abspath(__file__)) + '/libs/libndpi.so') if "win" in sys.platform[:3]:
self._ndpi = self._ffi.dlopen(dirname(abspath(__file__)) + '/libs/libndpi.dll')
else:
self._ndpi = self._ffi.dlopen(dirname(abspath(__file__)) + '/libs/libndpi.so')
else: else:
self._ndpi = self._ffi.dlopen(libpath) self._ndpi = self._ffi.dlopen(libpath)
self._ffi.cdef(cc) self._ffi.cdef(cc)
@@ -1236,6 +1256,8 @@ class NDPI():
self.SIZEOF_FLOW_STRUCT = self._ffi.sizeof("struct ndpi_flow_struct") self.SIZEOF_FLOW_STRUCT = self._ffi.sizeof("struct ndpi_flow_struct")
self.SIZEOF_ID_STRUCT = self._ffi.sizeof("struct ndpi_id_struct") self.SIZEOF_ID_STRUCT = self._ffi.sizeof("struct ndpi_id_struct")
self.NULL = self._ffi.NULL self.NULL = self._ffi.NULL
self.max_tcp_dissections = max_tcp_dissections
self.max_udp_dissections = max_udp_dissections
def new_ndpi_flow(self): def new_ndpi_flow(self):
f = self._ffi.cast('struct ndpi_flow_struct*', self._ndpi.ndpi_flow_malloc(self.SIZEOF_FLOW_STRUCT)) f = self._ffi.cast('struct ndpi_flow_struct*', self._ndpi.ndpi_flow_malloc(self.SIZEOF_FLOW_STRUCT))