Native fixes, cleanup

This commit is contained in:
M66B 2016-01-15 12:13:12 +01:00
parent aeb9591884
commit bad7ece7ba
1 changed files with 37 additions and 47 deletions

View File

@ -22,10 +22,11 @@
#define TAG "NetGuard.JNI" #define TAG "NetGuard.JNI"
#define MAXPKT 32678 #define MAXPKT 32678
// TODO TCP parameters // TODO TCP parameters
#define SELECTWAIT 10 #define SELECTWAIT 10 // seconds
#define TCPTIMEOUT 30 #define TCPTIMEOUT 30 // seconds
#define TCPTTL 64 #define TCPTTL 64
#define TCPWINDOW 2048 #define TCPWINDOW 2048
#define UIDDELAY 10 // milliseconds
struct arguments { struct arguments {
jobject instance; jobject instance;
@ -73,8 +74,6 @@ jint getUid(const int, const int, const void *, const uint16_t);
unsigned short checksum(unsigned short *, int); unsigned short checksum(unsigned short *, int);
void nsleep(const long);
char *hex(const u_int8_t *, const u_int16_t); char *hex(const u_int8_t *, const u_int16_t);
// Global variables // Global variables
@ -451,7 +450,7 @@ void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
if (datalen > 0) { if (datalen > 0) {
data = malloc(sizeof(struct data)); data = malloc(sizeof(struct data));
data->len = datalen; data->len = datalen;
data->data = malloc(datalen); // TODO free data->data = malloc(datalen);
memcpy(data->data, buffer + dataoff, datalen); memcpy(data->data, buffer + dataoff, datalen);
data->next = NULL; data->next = NULL;
} }
@ -476,7 +475,7 @@ void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
__android_log_print(ANDROID_LOG_DEBUG, TAG, "New SYN"); __android_log_print(ANDROID_LOG_DEBUG, TAG, "New SYN");
// Register connection // Register connection
struct connection *syn = malloc(sizeof(struct connection)); // TODO free struct connection *syn = malloc(sizeof(struct connection));
syn->time = time(NULL); syn->time = time(NULL);
syn->uid = uid; syn->uid = uid;
syn->remote_seq = ntohl(tcphdr->seq); // ISN remote syn->remote_seq = ntohl(tcphdr->seq); // ISN remote
@ -489,11 +488,7 @@ void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
syn->sent = NULL; syn->sent = NULL;
syn->next = NULL; syn->next = NULL;
// Ignore data // TODO handle SYN data?
if (data != NULL) {
free(data->data);
free(data);
}
// Build target address // Build target address
struct sockaddr_in daddr; struct sockaddr_in daddr;
@ -630,6 +625,11 @@ void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
cur->state = TCP_CLOSE; cur->state = TCP_CLOSE;
} }
} }
if (data != NULL) {
free(data->data);
free(data);
}
} }
int openSocket(JNIEnv *env, jobject instance, const struct sockaddr_in *daddr) { int openSocket(JNIEnv *env, jobject instance, const struct sockaddr_in *daddr) {
@ -878,12 +878,9 @@ void handle_ip(JNIEnv *env, jobject instance, const struct arguments *args,
// Get uid // Get uid
jint uid = -1; jint uid = -1;
if (protocol == IPPROTO_TCP || protocol == IPPROTO_UDP) { if ((protocol == IPPROTO_TCP && syn) || protocol == IPPROTO_UDP) {
// Sleep 10 ms // Sleep 10 ms
struct timespec tim, tim2; usleep(1000 * UIDDELAY);
tim.tv_sec = 0;
tim.tv_nsec = 10000000L;
nanosleep(&tim, &tim2);
// Lookup uid // Lookup uid
uid = getUid(protocol, version, saddr, sport); uid = getUid(protocol, version, saddr, sport);
@ -952,44 +949,47 @@ jint getUid(const int protocol, const int version, const void *saddr, const uint
else if (protocol == IPPROTO_UDP) else if (protocol == IPPROTO_UDP)
fn = (version == 4 ? "/proc/net/udp" : "/proc/net/udp6"); fn = (version == 4 ? "/proc/net/udp" : "/proc/net/udp6");
else else
return -1; return uid;
// Open proc file // Open proc file
FILE *fd = fopen(fn, "r"); FILE *fd = fopen(fn, "r");
if (fd == NULL) { if (fd == NULL) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "fopen %s error %d: %s", __android_log_print(ANDROID_LOG_ERROR, TAG, "fopen %s error %d: %s",
fn, errno, strerror(errno)); fn, errno, strerror(errno));
return -1; return uid;
} }
// Scan proc file // Scan proc file
jint u;
int i = 0; int i = 0;
while (fgets(line, sizeof(line), fd) != NULL) { while (fgets(line, sizeof(line), fd) != NULL) {
if (i++) { if (i++) {
if (version == 4) if (version == 4)
fields = sscanf(line, fields = sscanf(line,
"%*d: %X:%X %*X:%*X %*X %*lX:%*lX %*X:%*X %*X %d %*d %*ld ", "%*d: %X:%X %*X:%*X %*X %*lX:%*lX %*X:%*X %*X %d %*d %*ld ",
&addr32, &port, &uid); &addr32, &port, &u);
else else
fields = sscanf(line, fields = sscanf(line,
"%*d: %8X%8X%8X%8X:%X %*X:%*X %*X %*lX:%*lX %*X:%*X %*X %d %*d %*ld ", "%*d: %8X%8X%8X%8X:%X %*X:%*X %*X %*lX:%*lX %*X:%*X %*X %d %*d %*ld ",
addr128, addr128 + 4, addr128 + 8, addr128 + 12, &port, &uid); addr128, addr128 + 4, addr128 + 8, addr128 + 12, &port, &u);
if (fields < 3) { if (fields == (version == 4 ? 3 : 6)) {
if (port == sport) {
if (version == 4) {
if (addr32 == *((int32_t *) saddr)) {
uid = u;
break;
}
}
else {
if (memcmp(addr128, saddr, (size_t) 16) == 0) {
uid = u;
break;
}
}
}
} else
__android_log_print(ANDROID_LOG_ERROR, TAG, "Invalid field #%d: %s", fields, line); __android_log_print(ANDROID_LOG_ERROR, TAG, "Invalid field #%d: %s", fields, line);
break;
}
if (port == sport) {
if (version == 4) {
if (addr32 == *((int32_t *) saddr))
return uid;
}
else {
if (memcmp(addr128, saddr, (size_t) 16) == 0)
return uid;
}
}
} }
} }
@ -997,7 +997,7 @@ jint getUid(const int protocol, const int version, const void *saddr, const uint
__android_log_print(ANDROID_LOG_ERROR, TAG, "fclose %s error %d: %s", __android_log_print(ANDROID_LOG_ERROR, TAG, "fclose %s error %d: %s",
fn, errno, strerror(errno)); fn, errno, strerror(errno));
return -1; return uid;
} }
// TODO data types // TODO data types
@ -1032,21 +1032,11 @@ unsigned short checksum(unsigned short *addr, int len) {
return (answer); return (answer);
} }
void nsleep(const long ns) {
struct timespec tim, tim2;
tim.tv_sec = ns / 1000000000L;
tim.tv_nsec = ns % 1000000000L;
nanosleep(&tim, &tim2);
}
char hexout[250];
char *hex(const u_int8_t *data, const u_int16_t len) { char *hex(const u_int8_t *data, const u_int16_t len) {
char hex_str[] = "0123456789ABCDEF"; char hex_str[] = "0123456789ABCDEF";
//char *out; char *hexout;
//out = (char *) malloc(len * 3 + 1); // TODO free hexout = (char *) malloc(len * 3 + 1); // TODO free
hexout[len * 3] = 0;
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
hexout[i * 3 + 0] = hex_str[(data[i] >> 4) & 0x0F]; hexout[i * 3 + 0] = hex_str[(data[i] >> 4) & 0x0F];