From 4fd4ce39a1e7e18ae4b4033237750a45336a57a3 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 17 Jan 2016 17:41:54 +0100 Subject: [PATCH] Native header file --- app/src/main/jni/netguard/netguard.c | 90 +--------------------------- app/src/main/jni/netguard/netguard.h | 85 ++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 88 deletions(-) create mode 100644 app/src/main/jni/netguard/netguard.h diff --git a/app/src/main/jni/netguard/netguard.c b/app/src/main/jni/netguard/netguard.c index 49f0771a..87a3db51 100644 --- a/app/src/main/jni/netguard/netguard.c +++ b/app/src/main/jni/netguard/netguard.c @@ -13,6 +13,8 @@ #include #include +#include "netguard.h" + // TODO TCP fragmentation // TODO TCP push // TODO TCPv6 @@ -20,99 +22,11 @@ // TODO UDPv6 // TODO DHCP // TODO log allowed traffic -// TODO header file // TODO fix warnings // Window size < 2^31: x <= y: (uint32_t)(y-x) < 0x80000000 - // It is assumed that no packets will get lost and that packets arrive in order -#define TAG "NetGuard.JNI" -#define MAXPKT 32768 -// TODO TCP parameters (net.inet.tcp.keepinit, etc) -#define SELECTWAIT 10 // seconds -#define TCPTIMEOUT 300 // seconds ~net.inet.tcp.keepidle -#define TCPTTL 64 -#define TCPWINDOW 32768 -#define UIDDELAY 100 // milliseconds -#define UIDTRIES 10 -#define MAXPCAP 80 - -struct arguments { - jobject instance; - int tun; -}; - -struct session { - time_t time; - int uid; - uint32_t remote_seq; // confirmed bytes received, host notation - uint32_t local_seq; // confirmed bytes sent, host notation - uint32_t remote_start; - uint32_t local_start; - int32_t saddr; // network notation - __be16 source; // network notation - int32_t daddr; // network notation - __be16 dest; // network notation - uint8_t state; - jint socket; - uint32_t lport; // host notation - struct session *next; -}; - -// https://wiki.wireshark.org/Development/LibpcapFileFormat - -typedef unsigned short guint16_t; -typedef unsigned int guint32_t; -typedef signed int gint32_t; - -typedef struct pcap_hdr_s { - guint32_t magic_number; - guint16_t version_major; - guint16_t version_minor; - gint32_t thiszone; - guint32_t sigfigs; - guint32_t snaplen; - guint32_t network; -} pcap_hdr_t; - - -typedef struct pcaprec_hdr_s { - guint32_t ts_sec; - guint32_t ts_usec; - guint32_t incl_len; - guint32_t orig_len; -} pcaprec_hdr_t; - -#define LINKTYPE_RAW 101 - -void *handle_events(void *); - -void check_sockets(const struct arguments *, fd_set *, fd_set *, fd_set *); - -void handle_ip(JNIEnv *, jobject, const struct arguments *, const uint8_t *, const uint16_t); - -void handle_tcp(JNIEnv *, jobject, const struct arguments *args, - const uint8_t *, const uint16_t, int uid); - -int open_socket(JNIEnv *, jobject, const struct sockaddr_in *); - -int get_local_port(const int); - -int write_tcp(const struct session *, uint8_t *, uint16_t, uint16_t, int, int, int, int); - -jint get_uid(const int, const int, const void *, const uint16_t); - -uint16_t checksum(uint8_t *, uint16_t); - -void ng_log(int, const char *, ...); - -const char *strstate(const int state); - -char *hex(const u_int8_t *, const u_int16_t); - -void pcap_write(const void *, size_t); - // Global variables static JavaVM *jvm; diff --git a/app/src/main/jni/netguard/netguard.h b/app/src/main/jni/netguard/netguard.h new file mode 100644 index 00000000..a75c313e --- /dev/null +++ b/app/src/main/jni/netguard/netguard.h @@ -0,0 +1,85 @@ +#define TAG "NetGuard.JNI" +#define MAXPKT 32768 +// TODO TCP parameters (net.inet.tcp.keepinit, etc) +#define SELECTWAIT 10 // seconds +#define TCPTIMEOUT 300 // seconds ~net.inet.tcp.keepidle +#define TCPTTL 64 +#define TCPWINDOW 32768 +#define UIDDELAY 100 // milliseconds +#define UIDTRIES 10 +#define MAXPCAP 80 + +struct arguments { + jobject instance; + int tun; +}; + +struct session { + time_t time; + int uid; + uint32_t remote_seq; // confirmed bytes received, host notation + uint32_t local_seq; // confirmed bytes sent, host notation + uint32_t remote_start; + uint32_t local_start; + int32_t saddr; // network notation + __be16 source; // network notation + int32_t daddr; // network notation + __be16 dest; // network notation + uint8_t state; + jint socket; + uint32_t lport; // host notation + struct session *next; +}; + +// https://wiki.wireshark.org/Development/LibpcapFileFormat + +typedef unsigned short guint16_t; +typedef unsigned int guint32_t; +typedef signed int gint32_t; + +typedef struct pcap_hdr_s { + guint32_t magic_number; + guint16_t version_major; + guint16_t version_minor; + gint32_t thiszone; + guint32_t sigfigs; + guint32_t snaplen; + guint32_t network; +} pcap_hdr_t; + + +typedef struct pcaprec_hdr_s { + guint32_t ts_sec; + guint32_t ts_usec; + guint32_t incl_len; + guint32_t orig_len; +} pcaprec_hdr_t; + +#define LINKTYPE_RAW 101 + +void *handle_events(void *); + +void check_sockets(const struct arguments *, fd_set *, fd_set *, fd_set *); + +void handle_ip(JNIEnv *, jobject, const struct arguments *, const uint8_t *, const uint16_t); + +void handle_tcp(JNIEnv *, jobject, const struct arguments *args, + const uint8_t *, const uint16_t, int uid); + +int open_socket(JNIEnv *, jobject, const struct sockaddr_in *); + +int get_local_port(const int); + +int write_tcp(const struct session *, uint8_t *, uint16_t, uint16_t, int, int, int, int); + +jint get_uid(const int, const int, const void *, const uint16_t); + +uint16_t checksum(uint8_t *, uint16_t); + +void ng_log(int, const char *, ...); + +const char *strstate(const int state); + +char *hex(const u_int8_t *, const u_int16_t); + +void pcap_write(const void *, size_t);