NetGuard/app/src/main/jni/netguard/netguard.c

1144 lines
41 KiB
C
Raw Normal View History

2016-01-09 11:10:11 +00:00
#include <jni.h>
2016-01-13 08:23:21 +00:00
#include <android/log.h>
2016-01-09 20:17:42 +00:00
#include <stdio.h>
2016-01-15 09:08:18 +00:00
#include <stdlib.h>
2016-01-10 07:14:47 +00:00
#include <time.h>
2016-01-09 15:56:23 +00:00
#include <unistd.h>
2016-01-13 08:23:21 +00:00
#include <pthread.h>
2016-01-09 15:56:23 +00:00
#include <arpa/inet.h>
2016-01-09 11:10:11 +00:00
#include <netinet/ip.h>
#include <netinet/ip6.h>
2016-01-09 15:56:23 +00:00
#include <netinet/udp.h>
#include <netinet/tcp.h>
2016-01-09 11:10:11 +00:00
2016-01-12 20:15:25 +00:00
// TODO TCP fragmentation
// TODO TCP push
2016-01-15 09:28:31 +00:00
// TODO log allowed traffic
2016-01-12 20:15:25 +00:00
// TODO header file
2016-01-15 07:48:18 +00:00
// TODO debug switches
2016-01-15 11:57:32 +00:00
// TODO fix warnings
// Window size < 2^31: x <= y: (uint32_t)(y-x) < 0x80000000
2016-01-12 20:15:25 +00:00
2016-01-16 08:07:04 +00:00
// It is assumed that no packets will get lost and that packets arrive in order
2016-01-09 11:10:11 +00:00
#define TAG "NetGuard.JNI"
#define MAXPKT 32768
// TODO TCP parameters (net.inet.tcp.keepinit, etc)
2016-01-15 11:13:12 +00:00
#define SELECTWAIT 10 // seconds
#define TCPTIMEOUT 300 // seconds ~net.inet.tcp.keepidle
2016-01-15 07:48:18 +00:00
#define TCPTTL 64
#define TCPWINDOW 32768
2016-01-15 11:13:12 +00:00
#define UIDDELAY 10 // milliseconds
2016-01-11 22:06:35 +00:00
2016-01-13 08:23:21 +00:00
struct arguments {
jobject instance;
int tun;
};
2016-01-16 08:07:04 +00:00
struct session {
2016-01-12 08:45:58 +00:00
time_t time;
2016-01-15 09:28:31 +00:00
int uid;
2016-01-12 20:15:25 +00:00
uint32_t remote_seq; // confirmed bytes received, host notation
uint32_t local_seq; // confirmed bytes sent, host notation
2016-01-17 05:48:33 +00:00
uint32_t remote_start;
uint32_t local_start;
2016-01-12 12:15:21 +00:00
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
2016-01-16 08:07:04 +00:00
struct session *next;
2016-01-11 22:06:35 +00:00
};
2016-01-13 08:23:21 +00:00
void *handle_events(void *);
2016-01-12 12:15:21 +00:00
2016-01-15 18:51:07 +00:00
void handle_ip(JNIEnv *, jobject, const struct arguments *, const uint8_t *, const uint16_t);
2016-01-15 09:28:31 +00:00
void handle_tcp(JNIEnv *, jobject, const struct arguments *args,
const uint8_t *, const uint16_t, int uid);
2016-01-12 20:15:25 +00:00
2016-01-13 09:26:06 +00:00
int openSocket(JNIEnv *, jobject, const struct sockaddr_in *);
2016-01-11 22:06:35 +00:00
2016-01-13 18:25:15 +00:00
int getLocalPort(const int);
int canWrite(const int);
2016-01-16 14:12:42 +00:00
int writeTCP(const struct session *, uint8_t *, uint16_t, uint16_t, int, int, int, int);
2016-01-13 09:26:06 +00:00
2016-01-12 20:15:25 +00:00
jint getUid(const int, const int, const void *, const uint16_t);
2016-01-09 20:17:42 +00:00
2016-01-15 11:57:32 +00:00
uint16_t checksum(uint8_t *, uint16_t);
2016-01-11 22:06:35 +00:00
2016-01-16 11:32:55 +00:00
const char *strstate(const int state);
2016-01-12 20:15:25 +00:00
char *hex(const u_int8_t *, const u_int16_t);
// Global variables
2016-01-12 12:15:21 +00:00
2016-01-13 08:23:21 +00:00
static JavaVM *jvm;
pthread_t thread_id;
2016-01-14 06:46:23 +00:00
int signaled = 0;
2016-01-16 08:07:04 +00:00
struct session *session = NULL;
2016-01-11 22:06:35 +00:00
2016-01-13 08:23:21 +00:00
// JNI
2016-01-11 22:06:35 +00:00
2016-01-14 14:02:32 +00:00
JNIEXPORT void JNICALL
Java_eu_faircode_netguard_SinkholeService_jni_1init(JNIEnv *env, jobject instance) {
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Init");
2016-01-16 08:07:04 +00:00
session = NULL;
2016-01-14 14:02:32 +00:00
}
2016-01-09 11:10:11 +00:00
JNIEXPORT void JNICALL
2016-01-17 05:35:26 +00:00
Java_eu_faircode_netguard_SinkholeService_jni_1start(JNIEnv *env, jobject instance,
jint tun) {
2016-01-13 08:23:21 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Starting tun=%d", tun);
2016-01-12 16:19:27 +00:00
2016-01-14 06:46:23 +00:00
if (pthread_kill(thread_id, 0) == 0)
__android_log_print(ANDROID_LOG_WARN, TAG, "Already running thread %u", thread_id);
else {
jint rs = (*env)->GetJavaVM(env, &jvm);
if (rs != JNI_OK)
__android_log_print(ANDROID_LOG_ERROR, TAG, "GetJavaVM failed");
struct arguments *args = malloc(sizeof(struct arguments));
args->instance = (*env)->NewGlobalRef(env, instance);
args->tun = tun;
int err = pthread_create(&thread_id, NULL, handle_events, args);
if (err != 0)
__android_log_print(ANDROID_LOG_ERROR, TAG, "pthread_create error %d: %s",
err, strerror(err));
}
2016-01-12 12:15:21 +00:00
}
2016-01-09 11:10:11 +00:00
JNIEXPORT void JNICALL
2016-01-17 05:35:26 +00:00
Java_eu_faircode_netguard_SinkholeService_jni_1stop(JNIEnv *env, jobject instance,
jint tun, jboolean clear) {
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Stop tun %d clear %d", tun, clear);
2016-01-14 06:46:23 +00:00
if (pthread_kill(thread_id, 0) == 0) {
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Kill thread %u", thread_id);
int err = pthread_kill(thread_id, SIGUSR1);
if (err != 0)
2016-01-14 06:46:23 +00:00
__android_log_print(ANDROID_LOG_WARN, TAG, "pthread_kill error %d: %s",
err, strerror(err));
else {
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Join thread %u", thread_id);
pthread_join(thread_id, NULL);
if (err != 0)
__android_log_print(ANDROID_LOG_WARN, TAG, "pthread_join error %d: %s",
err, strerror(err));
}
2016-01-17 05:35:26 +00:00
if (clear) {
struct session *s = session;
while (s != NULL) {
struct session *p = s;
s = s->next;
free(p);
}
session = NULL;
}
2016-01-14 06:46:23 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Stopped");
} else
2016-01-14 06:46:23 +00:00
__android_log_print(ANDROID_LOG_WARN, TAG, "Not running");
2016-01-09 11:10:11 +00:00
}
2016-01-11 22:06:35 +00:00
// Private functions
void sig_handler(int sig, siginfo_t *info, void *context) {
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Signal %d", sig);
2016-01-14 06:46:23 +00:00
signaled = 1;
}
2016-01-13 08:23:21 +00:00
void *handle_events(void *a) {
struct arguments *args = (struct arguments *) a;
2016-01-14 06:46:23 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Start events tun=%d thread %u", args->tun,
thread_id);
2016-01-12 16:19:27 +00:00
2016-01-13 08:23:21 +00:00
JNIEnv *env;
jint rs = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
2016-01-13 09:26:06 +00:00
if (rs != JNI_OK)
2016-01-13 08:23:21 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG, "AttachCurrentThread failed");
int max;
fd_set rfds;
fd_set wfds;
fd_set efds;
struct timespec ts;
2016-01-13 08:23:21 +00:00
char dest[20];
sigset_t blockset;
sigset_t emptyset;
struct sigaction sa;
// Block SIGUSR1
sigemptyset(&blockset);
sigaddset(&blockset, SIGUSR1);
sigprocmask(SIG_BLOCK, &blockset, NULL);
/// Handle SIGUSR1
sa.sa_sigaction = sig_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGUSR1, &sa, NULL);
2016-01-14 06:46:23 +00:00
signaled = 0;
// Loop
while (1) {
2016-01-13 08:23:21 +00:00
time_t now = time(NULL);
2016-01-16 18:31:44 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Loop thread %u", thread_id);
2016-01-13 08:23:21 +00:00
// Select
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_ZERO(&efds);
2016-01-15 20:40:37 +00:00
// Always read tun
2016-01-13 08:23:21 +00:00
FD_SET(args->tun, &rfds);
FD_SET(args->tun, &efds);
max = args->tun;
2016-01-16 08:07:04 +00:00
struct session *last = NULL;
struct session *cur = session;
2016-01-13 08:23:21 +00:00
while (cur != NULL) {
// TODO differentiate timeouts
if (cur->time + TCPTIMEOUT < now) {
__android_log_print(ANDROID_LOG_WARN, TAG, "Idle lport %u",
cur->lport);
if (cur->state == TCP_SYN_RECV ||
cur->state == TCP_ESTABLISHED ||
cur->state == TCP_CLOSE_WAIT) {
// TODO can write
2016-01-16 14:12:42 +00:00
if (writeTCP(cur, NULL, 0, 0, 0, 1, 0, args->tun) < 0) { // FIN
__android_log_print(ANDROID_LOG_ERROR, TAG,
"write FIN lport %u error %d: %s",
cur->lport, errno, strerror((errno)));
cur->state = TCP_TIME_WAIT; // Will close socket
}
else {
__android_log_print(ANDROID_LOG_DEBUG, TAG,
"Half close initiated");
cur->local_seq++;
if (cur->state == TCP_SYN_RECV || cur->state == TCP_ESTABLISHED)
cur->state = TCP_FIN_WAIT1;
else // close wait
cur->state = TCP_LAST_ACK;
}
} else
cur->state = TCP_TIME_WAIT; // Will close socket
}
if (cur->state == TCP_TIME_WAIT) {
2016-01-13 08:23:21 +00:00
// Log
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Close lport %u",
cur->lport);
2016-01-14 14:02:32 +00:00
// TODO keep for some time
// TODO non blocking?
2016-01-15 09:28:31 +00:00
if (close(cur->socket))
__android_log_print(ANDROID_LOG_ERROR, TAG, "close error %d: %s",
errno, strerror(errno));
else
cur->state = TCP_CLOSE;
2016-01-11 22:06:35 +00:00
2016-01-14 14:02:32 +00:00
if (last == NULL)
2016-01-16 08:07:04 +00:00
session = cur->next;
2016-01-14 14:02:32 +00:00
else
last->next = cur->next;
2016-01-16 08:07:04 +00:00
struct session *c = cur;
2016-01-14 14:02:32 +00:00
cur = cur->next;
free(c);
continue;
2016-01-12 18:44:56 +00:00
2016-01-15 20:40:37 +00:00
} else if (cur->state != TCP_TIME_WAIT) {
2016-01-16 11:32:55 +00:00
if (cur->state == TCP_LISTEN) {
2016-01-13 08:23:21 +00:00
FD_SET(cur->socket, &wfds);
if (cur->socket > max)
max = cur->socket;
}
2016-01-15 20:40:37 +00:00
else if (cur->state == TCP_ESTABLISHED ||
2016-01-16 11:32:55 +00:00
cur->state == TCP_SYN_RECV ||
2016-01-15 20:40:37 +00:00
cur->state == TCP_CLOSE_WAIT) {
2016-01-13 10:11:11 +00:00
FD_SET(cur->socket, &rfds);
if (cur->socket > max)
max = cur->socket;
}
2016-01-12 18:44:56 +00:00
}
2016-01-13 08:23:21 +00:00
last = cur;
cur = cur->next;
}
2016-01-12 08:45:58 +00:00
2016-01-15 09:28:31 +00:00
ts.tv_sec = SELECTWAIT;
ts.tv_nsec = 0;
// TODO let timeout depend on session timeouts
sigemptyset(&emptyset);
2016-01-16 18:31:44 +00:00
int ready = pselect(max + 1, &rfds, &wfds, &efds, session == NULL ? NULL : &ts, &emptyset);
2016-01-13 08:23:21 +00:00
if (ready < 0) {
if (errno == EINTR) {
2016-01-14 06:46:23 +00:00
if (signaled) { ;
__android_log_print(ANDROID_LOG_DEBUG, TAG, "pselect signaled");
break;
} else {
__android_log_print(ANDROID_LOG_WARN, TAG, "pselect interrupted");
continue;
}
} else {
2016-01-16 18:31:44 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG, "pselect error %d: %s",
errno, strerror(errno));
2016-01-14 06:46:23 +00:00
break;
}
2016-01-13 08:23:21 +00:00
}
int sessions = 0;
struct session *s = session;
while (s != NULL) {
sessions++;
s = s->next;
}
2016-01-13 08:23:21 +00:00
if (ready == 0)
__android_log_print(ANDROID_LOG_DEBUG, TAG, "pselect timeout sessions %d", sessions);
2016-01-13 08:23:21 +00:00
else {
__android_log_print(ANDROID_LOG_DEBUG, TAG, "pselect sessions %d ready %d",
sessions, ready);
2016-01-14 14:02:32 +00:00
// Check tun exception
2016-01-13 08:23:21 +00:00
if (FD_ISSET(args->tun, &efds)) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "tun exception");
break;
}
2016-01-12 08:45:58 +00:00
2016-01-14 14:02:32 +00:00
// Check tun read
2016-01-13 08:23:21 +00:00
if (FD_ISSET(args->tun, &rfds)) {
uint8_t buffer[MAXPKT];
ssize_t length = read(args->tun, buffer, MAXPKT);
if (length < 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "tun read error %d: %s",
2016-01-12 08:45:58 +00:00
errno, strerror(errno));
2016-01-16 11:32:55 +00:00
if (errno == EINTR)
continue;
else
break;
2016-01-12 08:45:58 +00:00
}
2016-01-13 08:23:21 +00:00
if (length > 0)
2016-01-15 09:28:31 +00:00
handle_ip(env, args->instance, args, buffer, length);
else {
__android_log_print(ANDROID_LOG_ERROR, TAG, "tun empty read");
break;
}
2016-01-13 08:23:21 +00:00
}
2016-01-12 08:45:58 +00:00
2016-01-13 08:23:21 +00:00
// Check sockets
2016-01-16 08:07:04 +00:00
struct session *cur = session;
2016-01-13 08:23:21 +00:00
while (cur != NULL) {
2016-01-14 14:02:32 +00:00
// Check socket exception
2016-01-13 08:23:21 +00:00
if (FD_ISSET(cur->socket, &efds)) {
2016-01-12 08:45:58 +00:00
int serr;
socklen_t optlen = sizeof(serr);
2016-01-12 08:45:58 +00:00
if (getsockopt(cur->socket, SOL_SOCKET, SO_ERROR, &serr, &optlen) < 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG,
"getsockopt lport %u error %d: %s",
cur->lport, errno, strerror(errno));
2016-01-15 07:48:18 +00:00
// TODO initiate finish
2016-01-16 11:32:55 +00:00
cur->state = TCP_TIME_WAIT; // will close socket
2016-01-14 18:30:38 +00:00
cur = cur->next;
2016-01-12 08:45:58 +00:00
continue;
}
if (serr) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "lport %u SO_ERROR %d: %s",
cur->lport, serr, strerror(serr));
2016-01-15 07:48:18 +00:00
// TODO initiate FIN
2016-01-16 11:32:55 +00:00
if (serr != EINTR)
cur->state = TCP_TIME_WAIT; // will close socket
2016-01-14 18:30:38 +00:00
cur = cur->next;
2016-01-12 08:45:58 +00:00
continue;
}
2016-01-13 08:23:21 +00:00
}
2016-01-12 08:45:58 +00:00
2016-01-16 11:32:55 +00:00
if (cur->state == TCP_LISTEN) {
2016-01-14 14:02:32 +00:00
// Check socket connect
2016-01-13 18:25:15 +00:00
if (FD_ISSET(cur->socket, &wfds) && canWrite(args->tun)) {
2016-01-13 08:23:21 +00:00
// Log
char dest[20];
inet_ntop(AF_INET, &(cur->daddr), dest, sizeof(dest));
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Connected lport %u %s/%u",
cur->lport, dest, ntohs(cur->dest));
2016-01-13 10:11:11 +00:00
2016-01-15 09:28:31 +00:00
// TODO can write
2016-01-16 14:12:42 +00:00
if (writeTCP(cur, NULL, 0, 1, 1, 0, 0, args->tun) < 0) { // SYN+ACK
2016-01-16 11:32:55 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG,
"write SYN+ACK error %d: %s",
2016-01-15 09:04:44 +00:00
errno, strerror((errno)));
2016-01-16 11:32:55 +00:00
// Remote will retry
cur->state = TCP_TIME_WAIT; // will close socket
2016-01-15 09:28:31 +00:00
cur = cur->next;
continue;
2016-01-16 11:32:55 +00:00
} else {
2016-01-16 18:31:44 +00:00
cur->local_seq++; // local SYN
cur->remote_seq++; // remote SYN
2016-01-16 11:32:55 +00:00
cur->state = TCP_SYN_RECV;
}
2016-01-13 08:23:21 +00:00
}
2016-01-12 08:45:58 +00:00
}
2016-01-13 10:11:11 +00:00
2016-01-16 11:32:55 +00:00
else if (cur->state == TCP_SYN_RECV ||
cur->state == TCP_ESTABLISHED ||
2016-01-15 20:40:37 +00:00
cur->state == TCP_CLOSE_WAIT) {
2016-01-14 14:02:32 +00:00
// Check socket read
2016-01-13 10:11:11 +00:00
if (FD_ISSET(cur->socket, &rfds)) {
2016-01-16 11:32:55 +00:00
// TODO window size
2016-01-13 10:11:11 +00:00
uint8_t buffer[MAXPKT];
2016-01-14 14:02:32 +00:00
ssize_t bytes = recv(cur->socket, buffer, MAXPKT, 0);
2016-01-16 11:32:55 +00:00
if (bytes <= 0) {
// Socket remotely closed / error
if (bytes < 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG,
"recv lport %u error %d: %s",
cur->lport, errno, strerror(errno));
2016-01-16 11:32:55 +00:00
if (errno == EINTR) {
cur = cur->next;
continue;
2016-01-15 20:40:37 +00:00
}
2016-01-15 07:48:18 +00:00
}
2016-01-16 11:32:55 +00:00
else
__android_log_print(ANDROID_LOG_DEBUG, TAG, "recv lport %u empty",
cur->lport);
2016-01-16 11:32:55 +00:00
// TODO can write
2016-01-16 14:12:42 +00:00
if (writeTCP(cur, NULL, 0, 0, 0, 1, 0, args->tun) < 0) // FIN
2016-01-16 11:32:55 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG,
"write FIN lport %u error %d: %s",
cur->lport, errno, strerror((errno)));
2016-01-16 11:32:55 +00:00
else {
__android_log_print(ANDROID_LOG_DEBUG, TAG,
"Half close initiated");
2016-01-16 18:31:44 +00:00
cur->local_seq++; // local FIN
2016-01-16 11:32:55 +00:00
if (cur->state == TCP_SYN_RECV || cur->state == TCP_ESTABLISHED)
cur->state = TCP_FIN_WAIT1;
else // close wait
2016-01-15 07:48:18 +00:00
cur->state = TCP_LAST_ACK;
}
2016-01-13 10:11:11 +00:00
} else {
2016-01-14 14:02:32 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG,
"recv lport %u bytes %d",
2016-01-13 10:11:11 +00:00
cur->lport, bytes);
2016-01-15 09:28:31 +00:00
// TODO can write
2016-01-16 14:12:42 +00:00
if (writeTCP(cur, buffer, bytes, 0, 0, 0, 0, args->tun) < 0) // ACK
2016-01-15 09:28:31 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG,
"write ACK lport %u error %d: %s",
cur->lport, errno, strerror((errno)));
2016-01-15 09:04:44 +00:00
else
2016-01-16 18:31:44 +00:00
cur->local_seq += bytes; // received from socket
2016-01-13 10:11:11 +00:00
}
}
}
2016-01-13 08:23:21 +00:00
cur = cur->next;
2016-01-11 22:06:35 +00:00
}
}
}
2016-01-12 16:19:27 +00:00
2016-01-13 08:23:21 +00:00
(*env)->DeleteGlobalRef(env, args->instance);
rs = (*jvm)->DetachCurrentThread(jvm);
2016-01-13 09:26:06 +00:00
if (rs != JNI_OK)
2016-01-13 08:23:21 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG, "DetachCurrentThread failed");
free(args);
2016-01-14 06:46:23 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Stopped events tun=%d thread %u",
args->tun, thread_id);
2016-01-16 11:32:55 +00:00
// TODO conditionally report to Java
2016-01-11 22:06:35 +00:00
}
2016-01-15 18:51:07 +00:00
void handle_ip(JNIEnv *env, jobject instance, const struct arguments *args,
const uint8_t *buffer, const uint16_t length) {
uint8_t protocol;
void *saddr;
void *daddr;
char source[40];
char dest[40];
char flags[10];
int flen = 0;
uint8_t *payload;
// Get protocol, addresses & payload
uint8_t version = (*buffer) >> 4;
if (version == 4) {
struct iphdr *ip4hdr = buffer;
protocol = ip4hdr->protocol;
saddr = &ip4hdr->saddr;
daddr = &ip4hdr->daddr;
if (ip4hdr->frag_off & IP_MF)
flags[flen++] = '+';
uint8_t optlen = (ip4hdr->ihl - 5) * 4;
payload = buffer + 20 + optlen;
if (ntohs(ip4hdr->tot_len) != length) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "Invalid length %u header length %u",
length, ntohs(ip4hdr->tot_len));
return;
}
uint16_t csum = checksum(ip4hdr, sizeof(struct iphdr));
if (csum != 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "Invalid IP checksum");
return;
}
}
else if (version == 6) {
struct ip6_hdr *ip6hdr = buffer;
protocol = ip6hdr->ip6_nxt;
saddr = &ip6hdr->ip6_src;
daddr = &ip6hdr->ip6_dst;
payload = buffer + 40;
// TODO check length
// TODO checksum
}
else {
__android_log_print(ANDROID_LOG_WARN, TAG, "Unknown version %d", version);
return;
}
inet_ntop(version == 4 ? AF_INET : AF_INET6, saddr, source, sizeof(source));
inet_ntop(version == 4 ? AF_INET : AF_INET6, daddr, dest, sizeof(dest));
// Get ports & flags
int syn = 0;
uint16_t sport = -1;
uint16_t dport = -1;
if (protocol == IPPROTO_TCP) {
struct tcphdr *tcp = payload;
sport = ntohs(tcp->source);
dport = ntohs(tcp->dest);
if (tcp->syn) {
syn = 1;
flags[flen++] = 'S';
}
if (tcp->ack)
flags[flen++] = 'A';
if (tcp->psh)
flags[flen++] = 'P';
if (tcp->fin)
flags[flen++] = 'F';
if (tcp->fin)
flags[flen++] = 'R';
// TODO checksum
} else if (protocol == IPPROTO_UDP) {
struct udphdr *udp = payload;
sport = ntohs(udp->source);
dport = ntohs(udp->dest);
// TODO checksum
}
flags[flen] = 0;
// Get uid
jint uid = -1;
if ((protocol == IPPROTO_TCP && syn) || protocol == IPPROTO_UDP) {
// Sleep 10 ms
2016-01-16 08:07:04 +00:00
// TODO uid retry
2016-01-15 18:51:07 +00:00
usleep(1000 * UIDDELAY);
// Lookup uid
uid = getUid(protocol, version, saddr, sport);
if (uid < 0 && version == 4) {
int8_t saddr128[16];
memset(saddr128, 0, 10);
saddr128[10] = 0xFF;
saddr128[11] = 0xFF;
memcpy(saddr128 + 12, saddr, 4);
uid = getUid(protocol, 6, saddr128, sport);
}
}
__android_log_print(ANDROID_LOG_DEBUG, TAG,
"Packet v%d %s/%u -> %s/%u proto %d flags %s uid %d",
version, source, sport, dest, dport, protocol, flags, uid);
if (protocol == IPPROTO_TCP)
handle_tcp(env, instance, args, buffer, length, uid);
// Call back
if ((protocol == IPPROTO_TCP && syn) || protocol == IPPROTO_UDP) {
jclass cls = (*env)->GetObjectClass(env, instance);
jmethodID mid = (*env)->GetMethodID(env, cls, "logPacket",
"(ILjava/lang/String;ILjava/lang/String;IILjava/lang/String;IZ)V");
if (mid == 0)
__android_log_print(ANDROID_LOG_ERROR, TAG, "logPacket not found");
else {
jboolean allowed = 0;
jstring jsource = (*env)->NewStringUTF(env, source);
jstring jdest = (*env)->NewStringUTF(env, dest);
jstring jflags = (*env)->NewStringUTF(env, flags);
(*env)->CallVoidMethod(env, instance, mid,
version,
jsource, sport,
jdest, dport,
protocol, jflags,
uid, allowed);
(*env)->DeleteLocalRef(env, jsource);
(*env)->DeleteLocalRef(env, jdest);
(*env)->DeleteLocalRef(env, jflags);
jthrowable ex = (*env)->ExceptionOccurred(env);
if (ex) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
(*env)->DeleteLocalRef(env, ex);
}
}
(*env)->DeleteLocalRef(env, cls);
}
}
2016-01-14 14:02:32 +00:00
void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
2016-01-15 09:28:31 +00:00
const uint8_t *buffer, uint16_t length, int uid) {
2016-01-12 08:45:58 +00:00
// Check version
2016-01-14 14:02:32 +00:00
uint8_t version = (*buffer) >> 4;
2016-01-12 08:45:58 +00:00
if (version != 4)
return;
2016-01-11 22:06:35 +00:00
// Get headers
2016-01-12 18:44:56 +00:00
struct iphdr *iphdr = buffer;
uint8_t optlen = (iphdr->ihl - 5) * 4;
2016-01-12 18:44:56 +00:00
struct tcphdr *tcphdr = buffer + sizeof(struct iphdr) + optlen;
2016-01-15 07:48:18 +00:00
if (optlen)
__android_log_print(ANDROID_LOG_DEBUG, TAG, "optlen %d", optlen);
2016-01-12 18:44:56 +00:00
// Get data
uint16_t dataoff = sizeof(struct iphdr) + optlen + sizeof(struct tcphdr);
uint16_t datalen = length - dataoff;
2016-01-11 22:06:35 +00:00
2016-01-16 08:07:04 +00:00
// Search session
struct session *last = NULL;
struct session *cur = session;
while (cur != NULL && !(cur->saddr == iphdr->saddr && cur->source == tcphdr->source &&
cur->daddr == iphdr->daddr && cur->dest == tcphdr->dest)) {
2016-01-11 22:06:35 +00:00
last = cur;
cur = cur->next;
}
// Log
char dest[20];
inet_ntop(AF_INET, &(iphdr->daddr), dest, sizeof(dest));
2016-01-17 05:48:33 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Received %s/%u seq %u ack %u window %u data %d",
2016-01-12 18:44:56 +00:00
dest, ntohs(tcphdr->dest),
2016-01-16 11:32:55 +00:00
ntohl(tcphdr->seq), ntohl(tcphdr->ack_seq),
ntohs(tcphdr->window), datalen);
2016-01-11 22:06:35 +00:00
if (cur == NULL) {
if (tcphdr->syn) {
2016-01-12 18:44:56 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "New SYN");
2016-01-11 22:06:35 +00:00
2016-01-16 08:07:04 +00:00
// Register session
struct session *syn = malloc(sizeof(struct session));
2016-01-12 08:45:58 +00:00
syn->time = time(NULL);
2016-01-15 09:28:31 +00:00
syn->uid = uid;
2016-01-14 14:02:32 +00:00
syn->remote_seq = ntohl(tcphdr->seq); // ISN remote
2016-01-15 09:08:18 +00:00
syn->local_seq = rand(); // ISN local
2016-01-17 05:48:33 +00:00
syn->remote_start = syn->remote_seq;
syn->local_start = syn->local_seq;
2016-01-11 22:06:35 +00:00
syn->saddr = iphdr->saddr;
syn->source = tcphdr->source;
syn->daddr = iphdr->daddr;
syn->dest = tcphdr->dest;
2016-01-16 11:32:55 +00:00
syn->state = TCP_LISTEN;
2016-01-11 22:06:35 +00:00
syn->next = NULL;
2016-01-15 11:13:12 +00:00
// TODO handle SYN data?
2016-01-11 22:06:35 +00:00
2016-01-13 09:26:06 +00:00
// Build target address
struct sockaddr_in daddr;
memset(&daddr, 0, sizeof(struct sockaddr_in));
daddr.sin_family = AF_INET;
daddr.sin_port = tcphdr->dest;
daddr.sin_addr.s_addr = iphdr->daddr;
// Open socket
syn->socket = openSocket(env, instance, &daddr);
2016-01-15 07:48:18 +00:00
if (syn->socket < 0) {
2016-01-15 20:40:37 +00:00
syn->state = TCP_TIME_WAIT;
2016-01-15 07:48:18 +00:00
// Remote will retry
free(syn);
}
2016-01-11 22:06:35 +00:00
else {
2016-01-13 09:26:06 +00:00
syn->lport = getLocalPort(syn->socket);
2016-01-11 22:06:35 +00:00
2016-01-13 09:26:06 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Connecting to %s/%u lport %u",
dest, ntohs(tcphdr->dest), syn->lport);
2016-01-15 07:48:18 +00:00
if (last == NULL)
2016-01-16 08:07:04 +00:00
session = syn;
2016-01-15 07:48:18 +00:00
else
last->next = syn;
2016-01-11 22:06:35 +00:00
}
}
2016-01-15 09:28:31 +00:00
else {
2016-01-16 08:07:04 +00:00
__android_log_print(ANDROID_LOG_WARN, TAG, "Unknown session");
struct session *rst = malloc(sizeof(struct session));
2016-01-15 09:28:31 +00:00
rst->time = time(NULL);
2016-01-17 05:41:32 +00:00
rst->remote_seq = ntohl(tcphdr->seq);
rst->local_seq = 0;
2016-01-15 09:28:31 +00:00
rst->saddr = iphdr->saddr;
rst->source = tcphdr->source;
rst->daddr = iphdr->daddr;
rst->dest = tcphdr->dest;
2016-01-15 20:40:37 +00:00
rst->state = TCP_TIME_WAIT;
2016-01-15 09:28:31 +00:00
rst->next = NULL;
// TODO can write
2016-01-17 05:41:32 +00:00
int confirm = (tcphdr->syn || tcphdr->fin ? 1 : 0) + datalen;
if (writeTCP(rst, NULL, 0, confirm, 0, 0, 1, args->tun) < 0)
2016-01-15 09:28:31 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG,
"write RST error %d: %s",
errno, strerror((errno)));
free(rst);
}
2016-01-11 22:06:35 +00:00
}
else {
2016-01-16 11:32:55 +00:00
int oldstate = cur->state;
uint32_t oldlocal = cur->local_seq;
uint32_t oldremote = cur->remote_seq;
__android_log_print(ANDROID_LOG_DEBUG, TAG,
"Session lport %u state %s local %u remote %u",
2016-01-17 05:48:33 +00:00
cur->lport, strstate(cur->state),
cur->local_seq - cur->local_start,
cur->remote_seq - cur->remote_start);
2016-01-12 08:45:58 +00:00
2016-01-13 08:23:21 +00:00
if (tcphdr->syn)
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Ignoring repeated SYN");
2016-01-16 11:32:55 +00:00
else if (tcphdr->ack && !tcphdr->fin) {
// TODO proper wrap around
if (ntohl(tcphdr->seq) + 1 == cur->remote_seq) {
// TODO respond to keep alive?
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Keep alive");
cur->time = time(NULL);
} else if (ntohl(tcphdr->ack_seq) < cur->local_seq ||
ntohl(tcphdr->seq) < cur->remote_seq)
__android_log_print(ANDROID_LOG_WARN, TAG, "Old ack");
else if (ntohl(tcphdr->ack_seq) == cur->local_seq &&
ntohl(tcphdr->seq) == cur->remote_seq) {
cur->time = time(NULL);
if (cur->state == TCP_SYN_RECV) {
2016-01-15 07:48:18 +00:00
// TODO process data?
2016-01-12 20:15:25 +00:00
cur->state = TCP_ESTABLISHED;
}
else if (cur->state == TCP_ESTABLISHED) {
2016-01-16 18:31:44 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "New ack data %u", datalen);
2016-01-16 14:12:42 +00:00
if (datalen) {
2016-01-14 14:02:32 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "send socket data %u",
2016-01-16 14:12:42 +00:00
datalen);
// TODO non blocking
2016-01-16 14:12:42 +00:00
if (send(cur->socket, buffer + dataoff, datalen, 0) < 0) {
2016-01-14 14:02:32 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG, "send error %d: %s",
errno, strerror(errno));
2016-01-16 11:32:55 +00:00
// Remote will retry
} else {
2016-01-15 09:28:31 +00:00
// TODO can write
2016-01-16 14:12:42 +00:00
if (writeTCP(cur, NULL, 0, datalen, 0, 0, 0, args->tun) < 0) // ACK
2016-01-15 09:28:31 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG,
"write data error %d: %s",
2016-01-15 09:04:44 +00:00
errno, strerror((errno)));
else
2016-01-16 18:31:44 +00:00
cur->remote_seq += datalen; // received from tun
2016-01-14 14:02:32 +00:00
}
}
}
else if (cur->state == TCP_LAST_ACK) {
2016-01-15 07:48:18 +00:00
// socket has been shutdown already
2016-01-16 11:32:55 +00:00
cur->state = TCP_TIME_WAIT; // Will close socket
2016-01-15 07:48:18 +00:00
}
else if (cur->state == TCP_FIN_WAIT1)
cur->state = TCP_FIN_WAIT2;
else if (cur->state == TCP_CLOSING)
cur->state = TCP_TIME_WAIT;
2016-01-15 07:48:18 +00:00
else
__android_log_print(ANDROID_LOG_ERROR, TAG, "Invalid ACK");
}
else {
// TODO check old seq/ack
__android_log_print(ANDROID_LOG_WARN, TAG, "Invalid seq/ack");
2016-01-15 20:40:37 +00:00
}
2016-01-12 12:15:21 +00:00
}
2016-01-14 21:48:05 +00:00
2016-01-16 11:32:55 +00:00
else if (tcphdr->fin /* ack */) {
if (ntohl(tcphdr->ack_seq) == cur->local_seq &&
ntohl(tcphdr->seq) == cur->remote_seq) {
cur->time = time(NULL);
2016-01-16 11:32:55 +00:00
if (shutdown(cur->socket, SHUT_RD)) {
2016-01-15 07:48:18 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG, "shutdown error %d: %s",
errno, strerror(errno));
2016-01-16 11:32:55 +00:00
// Remote will retry
}
else {
2016-01-15 07:48:18 +00:00
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Shutdown socket");
2016-01-16 11:32:55 +00:00
int ok = 1;
if (tcphdr->ack && datalen) {
__android_log_print(ANDROID_LOG_DEBUG, TAG, "send socket data %u",
2016-01-16 14:12:42 +00:00
datalen);
// TODO non blocking
2016-01-16 14:12:42 +00:00
if (send(cur->socket, buffer + dataoff, datalen, 0) < 0) {
2016-01-16 11:32:55 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG, "send error %d: %s",
errno, strerror(errno));
ok = 0;
}
}
if (ok) {
// TODO can write
2016-01-16 14:12:42 +00:00
if (writeTCP(cur, NULL, 0, 1 + datalen, 0, 0, 0, args->tun) < 0) // ACK
2016-01-16 11:32:55 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG,
"write ACK error %d: %s",
errno, strerror((errno)));
else {
2016-01-16 18:31:44 +00:00
cur->remote_seq += 1 + datalen; // FIN + received from tun
// TODO check ACK !TCP_FIN_WAIT1
2016-01-16 11:32:55 +00:00
if (cur->state == TCP_ESTABLISHED)
cur->state = TCP_CLOSE_WAIT;
2016-01-16 18:31:44 +00:00
else if (cur->state == TCP_FIN_WAIT1) {
if (tcphdr->ack)
cur->state = TCP_TIME_WAIT;
else
cur->state = TCP_CLOSING;
} else if (cur->state == TCP_FIN_WAIT2)
2016-01-16 11:32:55 +00:00
cur->state = TCP_TIME_WAIT;
else
__android_log_print(ANDROID_LOG_ERROR, TAG, "Invalid FIN");
}
}
2016-01-15 20:40:37 +00:00
}
}
else {
// TODO check old seq/ack
2016-01-16 11:32:55 +00:00
__android_log_print(ANDROID_LOG_WARN, TAG, "Invalid seq/ack");
}
2016-01-14 21:48:05 +00:00
}
2016-01-16 11:32:55 +00:00
else if (tcphdr->rst)
cur->state = TCP_TIME_WAIT; // will close socket
else
__android_log_print(ANDROID_LOG_ERROR, TAG, "Unknown packet");
if (cur->state != oldstate || cur->local_seq != oldlocal || cur->remote_seq != oldremote)
__android_log_print(ANDROID_LOG_DEBUG, TAG,
"Session lport %u new state %s local %u remote %u",
2016-01-17 05:48:33 +00:00
cur->lport, strstate(cur->state),
cur->local_seq - cur->local_start,
cur->remote_seq - cur->remote_start);
2016-01-13 09:26:06 +00:00
}
}
int openSocket(JNIEnv *env, jobject instance, const struct sockaddr_in *daddr) {
int sock = -1;
// Get TCP socket
// TODO socket options (SO_REUSEADDR, etc)
2016-01-13 09:26:06 +00:00
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "socket error %d: %s",
errno, strerror(errno));
return -1;
}
// Protect
jclass cls = (*env)->GetObjectClass(env, instance);
jmethodID mid = (*env)->GetMethodID(env, cls, "protect", "(I)Z");
if (mid == 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "protect not found");
return -1;
}
else {
jboolean isProtected = (*env)->CallBooleanMethod(env, instance, mid, sock);
if (!isProtected)
__android_log_print(ANDROID_LOG_ERROR, TAG, "protect failed");
jthrowable ex = (*env)->ExceptionOccurred(env);
if (ex) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
(*env)->DeleteLocalRef(env, ex);
}
}
2016-01-12 12:15:21 +00:00
2016-01-13 10:11:11 +00:00
// Set non blocking
uint8_t flags = fcntl(sock, F_GETFL, 0);
if (flags < 0 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
2016-01-13 18:25:15 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG, "fcntl O_NONBLOCK error %d: %s",
2016-01-13 10:11:11 +00:00
errno, strerror(errno));
return -1;
}
2016-01-13 09:26:06 +00:00
// Initiate connect
int err = connect(sock, daddr, sizeof(struct sockaddr_in));
if (err < 0 && errno != EINPROGRESS) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "connect error %d: %s",
errno, strerror(errno));
return -1;
2016-01-11 22:06:35 +00:00
}
2016-01-13 09:26:06 +00:00
2016-01-13 18:25:15 +00:00
// Set blocking
2016-01-14 14:02:32 +00:00
if (fcntl(sock, F_SETFL, flags & ~O_NONBLOCK) < 0) {
2016-01-13 18:25:15 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG, "fcntl error %d: %s",
errno, strerror(errno));
return -1;
}
2016-01-13 09:26:06 +00:00
return sock;
2016-01-11 22:06:35 +00:00
}
2016-01-13 09:26:06 +00:00
int getLocalPort(const int sock) {
struct sockaddr_in sin;
int len = sizeof(sin);
if (getsockname(sock, &sin, &len) < 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "getsockname error %d: %s",
errno, strerror(errno));
return -1;
} else
return ntohs(sin.sin_port);
}
2016-01-13 18:25:15 +00:00
int canWrite(const int fd) {
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
fd_set wfds;
FD_ZERO(&wfds);
FD_SET(fd, &wfds);
return (select(fd + 1, NULL, &wfds, NULL, &tv) > 0);
}
2016-01-16 08:07:04 +00:00
int writeTCP(const struct session *cur,
2016-01-16 14:12:42 +00:00
uint8_t *data, uint16_t datalen, uint16_t confirm,
2016-01-15 09:04:44 +00:00
int syn, int fin, int rst, int tun) {
2016-01-14 14:02:32 +00:00
// Build packet
2016-01-15 09:28:31 +00:00
uint16_t len = sizeof(struct iphdr) + sizeof(struct tcphdr) + datalen;
2016-01-14 14:02:32 +00:00
u_int8_t *buffer = calloc(len, 1);
struct iphdr *ip = buffer;
struct tcphdr *tcp = buffer + sizeof(struct iphdr);
2016-01-14 18:30:38 +00:00
if (datalen)
2016-01-16 14:12:42 +00:00
memcpy(buffer + sizeof(struct iphdr) + sizeof(struct tcphdr), data, datalen);
2016-01-14 14:02:32 +00:00
// Build IP header
ip->version = 4;
ip->ihl = sizeof(struct iphdr) >> 2;
ip->tot_len = htons(len);
2016-01-15 07:48:18 +00:00
ip->ttl = TCPTTL;
2016-01-14 14:02:32 +00:00
ip->protocol = IPPROTO_TCP;
ip->saddr = cur->daddr;
ip->daddr = cur->saddr;
// Calculate IP checksum
ip->check = checksum(ip, sizeof(struct iphdr));
// Build TCP header
tcp->source = cur->dest;
tcp->dest = cur->source;
2016-01-17 05:41:32 +00:00
tcp->seq = htonl(cur->local_seq);
tcp->ack_seq = htonl(cur->remote_seq + confirm); // TODO proper wrap around
2016-01-14 14:02:32 +00:00
tcp->doff = sizeof(struct tcphdr) >> 2;
2016-01-15 09:04:44 +00:00
tcp->syn = syn;
2016-01-16 19:26:15 +00:00
// TODO why does a FIN need an ACK?
tcp->ack = (datalen > 0 || confirm > 0 || syn || fin);
2016-01-14 21:48:05 +00:00
tcp->fin = fin;
2016-01-15 09:04:44 +00:00
tcp->rst = rst;
2016-01-15 07:48:18 +00:00
tcp->window = htons(TCPWINDOW);
2016-01-14 14:02:32 +00:00
// Calculate TCP checksum
2016-01-16 14:12:42 +00:00
// TODO optimize memory usage
2016-01-14 18:30:38 +00:00
uint16_t clen = sizeof(struct ippseudo) + sizeof(struct tcphdr) + datalen;
2016-01-14 14:02:32 +00:00
uint8_t csum[clen];
// Build pseudo header
struct ippseudo *pseudo = csum;
pseudo->ippseudo_src.s_addr = ip->saddr;
pseudo->ippseudo_dst.s_addr = ip->daddr;
pseudo->ippseudo_pad = 0;
pseudo->ippseudo_p = ip->protocol;
2016-01-15 09:28:31 +00:00
pseudo->ippseudo_len = htons(sizeof(struct tcphdr) + datalen);
2016-01-14 14:02:32 +00:00
2016-01-14 18:30:38 +00:00
// Copy TCP header + data
2016-01-14 14:02:32 +00:00
memcpy(csum + sizeof(struct ippseudo), tcp, sizeof(struct tcphdr));
2016-01-14 18:30:38 +00:00
if (datalen)
2016-01-16 14:12:42 +00:00
memcpy(csum + sizeof(struct ippseudo) + sizeof(struct tcphdr), data, datalen);
2016-01-14 14:02:32 +00:00
tcp->check = checksum(csum, clen);
char to[20];
inet_ntop(AF_INET, &(ip->daddr), to, sizeof(to));
// Send packet
__android_log_print(ANDROID_LOG_DEBUG, TAG,
2016-01-15 09:04:44 +00:00
"Sending%s%s%s%s to tun %s/%u seq %u ack %u data %u confirm %u",
(tcp->syn ? " SYN" : ""),
(tcp->ack ? " ACK" : ""),
(tcp->fin ? " FIN" : ""),
(tcp->rst ? " RST" : ""),
to, ntohs(tcp->dest),
2016-01-17 05:48:33 +00:00
ntohl(tcp->seq) - cur->local_start,
ntohl(tcp->ack_seq) - cur->remote_start,
datalen, confirm);
2016-01-16 19:26:15 +00:00
//if (tcp->fin || tcp->rst) {
// char *h = hex(buffer, len);
// __android_log_print(ANDROID_LOG_DEBUG, TAG, "%s", h);
// free(h);
//}
2016-01-14 14:02:32 +00:00
int res = write(tun, buffer, len);
free(buffer);
return res;
}
2016-01-12 20:15:25 +00:00
jint getUid(const int protocol, const int version, const void *saddr, const uint16_t sport) {
2016-01-10 07:14:47 +00:00
char line[250];
int fields;
int32_t addr32;
int8_t addr128[16];
uint16_t port;
jint uid = -1;
2016-01-10 07:14:47 +00:00
// Get proc file name
char *fn = NULL;
if (protocol == IPPROTO_TCP)
fn = (version == 4 ? "/proc/net/tcp" : "/proc/net/tcp6");
else if (protocol == IPPROTO_UDP)
fn = (version == 4 ? "/proc/net/udp" : "/proc/net/udp6");
else
2016-01-15 11:13:12 +00:00
return uid;
2016-01-10 07:14:47 +00:00
// Open proc file
FILE *fd = fopen(fn, "r");
if (fd == NULL) {
2016-01-15 09:28:31 +00:00
__android_log_print(ANDROID_LOG_ERROR, TAG, "fopen %s error %d: %s",
fn, errno, strerror(errno));
2016-01-15 11:13:12 +00:00
return uid;
2016-01-10 07:14:47 +00:00
}
// Scan proc file
2016-01-15 11:13:12 +00:00
jint u;
2016-01-10 07:14:47 +00:00
int i = 0;
while (fgets(line, sizeof(line), fd) != NULL) {
if (i++) {
if (version == 4)
fields = sscanf(line,
"%*d: %X:%X %*X:%*X %*X %*lX:%*lX %*X:%*X %*X %d %*d %*ld ",
2016-01-15 11:13:12 +00:00
&addr32, &port, &u);
2016-01-10 07:14:47 +00:00
else
fields = sscanf(line,
"%*d: %8X%8X%8X%8X:%X %*X:%*X %*X %*lX:%*lX %*X:%*X %*X %d %*d %*ld ",
2016-01-15 11:13:12 +00:00
addr128, addr128 + 4, addr128 + 8, addr128 + 12, &port, &u);
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;
}
}
2016-01-10 11:51:02 +00:00
}
2016-01-15 11:13:12 +00:00
} else
__android_log_print(ANDROID_LOG_ERROR, TAG, "Invalid field #%d: %s", fields, line);
2016-01-09 20:17:42 +00:00
}
}
2016-01-10 07:14:47 +00:00
2016-01-15 09:28:31 +00:00
if (fclose(fd))
__android_log_print(ANDROID_LOG_ERROR, TAG, "fclose %s error %d: %s",
fn, errno, strerror(errno));
2016-01-10 07:14:47 +00:00
2016-01-15 11:13:12 +00:00
return uid;
2016-01-12 12:15:21 +00:00
}
2016-01-15 11:57:32 +00:00
uint16_t checksum(uint8_t *buffer, uint16_t length) {
register uint32_t sum = 0;
register uint16_t *buf = buffer;
register int len = length;
2016-01-12 12:15:21 +00:00
2016-01-15 11:57:32 +00:00
while (len > 1) {
sum += *buf++;
len -= 2;
2016-01-12 12:15:21 +00:00
}
2016-01-15 11:57:32 +00:00
if (len > 0)
sum += *((uint8_t *) buf);
while (sum >> 16)
sum = (sum & 0xFFFF) + (sum >> 16);
return (uint16_t) (~sum);
2016-01-12 12:15:21 +00:00
}
2016-01-16 11:32:55 +00:00
const char *strstate(const int state) {
char buf[20];
switch (state) {
case TCP_ESTABLISHED:
return "TCP_ESTABLISHED";
2016-01-16 11:32:55 +00:00
case TCP_SYN_SENT:
return "TCP_SYN_SENT";
2016-01-16 11:32:55 +00:00
case TCP_SYN_RECV:
return "TCP_SYN_RECV";
2016-01-16 11:32:55 +00:00
case TCP_FIN_WAIT1:
return "TCP_FIN_WAIT1";
2016-01-16 11:32:55 +00:00
case TCP_FIN_WAIT2:
return "TCP_FIN_WAIT2";
2016-01-16 11:32:55 +00:00
case TCP_TIME_WAIT:
return "TCP_TIME_WAIT";
2016-01-16 11:32:55 +00:00
case TCP_CLOSE:
return "TCP_CLOSE";
2016-01-16 11:32:55 +00:00
case TCP_CLOSE_WAIT:
return "TCP_CLOSE_WAIT";
2016-01-16 11:32:55 +00:00
case TCP_LAST_ACK:
return "TCP_LAST_ACK";
2016-01-16 11:32:55 +00:00
case TCP_LISTEN:
return "TCP_LISTEN";
2016-01-16 11:32:55 +00:00
case TCP_CLOSING:
return "TCP_CLOSING";
2016-01-16 11:32:55 +00:00
default:
sprintf(buf, "TCP_%d", state);
2016-01-16 11:32:55 +00:00
return buf;
}
}
2016-01-12 20:15:25 +00:00
char *hex(const u_int8_t *data, const u_int16_t len) {
char hex_str[] = "0123456789ABCDEF";
2016-01-12 12:15:21 +00:00
2016-01-15 11:13:12 +00:00
char *hexout;
hexout = (char *) malloc(len * 3 + 1); // TODO free
2016-01-12 12:15:21 +00:00
for (size_t i = 0; i < len; i++) {
2016-01-13 19:17:21 +00:00
hexout[i * 3 + 0] = hex_str[(data[i] >> 4) & 0x0F];
hexout[i * 3 + 1] = hex_str[(data[i]) & 0x0F];
hexout[i * 3 + 2] = ' ';
2016-01-12 12:15:21 +00:00
}
2016-01-13 19:17:21 +00:00
return hexout;
2016-01-12 16:19:27 +00:00
}
2016-01-17 05:35:26 +00:00