mirror of
https://github.com/M66B/NetGuard.git
synced 2025-02-26 16:13:01 +00:00
Native recv, refactoring
This commit is contained in:
parent
5630d71e27
commit
36ce001643
1 changed files with 48 additions and 15 deletions
|
@ -62,7 +62,7 @@ void handle_tcp(JNIEnv *, jobject, const uint8_t *, const uint16_t);
|
||||||
|
|
||||||
int openSocket(JNIEnv *, jobject, const struct sockaddr_in *);
|
int openSocket(JNIEnv *, jobject, const struct sockaddr_in *);
|
||||||
|
|
||||||
int sendSYN(const struct connection *, const int);
|
int writeSYN(const struct connection *, const int);
|
||||||
|
|
||||||
void decode(JNIEnv *, jobject, const uint8_t *, const uint16_t);
|
void decode(JNIEnv *, jobject, const uint8_t *, const uint16_t);
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ void *handle_events(void *a) {
|
||||||
if (cur->time + TIMEOUTPKT < now) {
|
if (cur->time + TIMEOUTPKT < now) {
|
||||||
// Log
|
// Log
|
||||||
inet_ntop(AF_INET, &(cur->daddr), dest, sizeof(dest));
|
inet_ntop(AF_INET, &(cur->daddr), dest, sizeof(dest));
|
||||||
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Idle %s/%u lport=%u",
|
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Idle %s/%u lport %u",
|
||||||
dest, ntohs(cur->dest), cur->lport);
|
dest, ntohs(cur->dest), cur->lport);
|
||||||
|
|
||||||
// TODO check if open
|
// TODO check if open
|
||||||
|
@ -202,6 +202,11 @@ void *handle_events(void *a) {
|
||||||
if (cur->socket > max)
|
if (cur->socket > max)
|
||||||
max = cur->socket;
|
max = cur->socket;
|
||||||
}
|
}
|
||||||
|
else if (cur->state == TCP_ESTABLISHED) {
|
||||||
|
FD_SET(cur->socket, &rfds);
|
||||||
|
if (cur->socket > max)
|
||||||
|
max = cur->socket;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
last = cur;
|
last = cur;
|
||||||
|
@ -251,14 +256,12 @@ void *handle_events(void *a) {
|
||||||
int serr;
|
int serr;
|
||||||
socklen_t optlen = sizeof(serr);
|
socklen_t optlen = sizeof(serr);
|
||||||
if (getsockopt(cur->socket, SOL_SOCKET, SO_ERROR, &serr, &optlen) < 0) {
|
if (getsockopt(cur->socket, SOL_SOCKET, SO_ERROR, &serr, &optlen) < 0) {
|
||||||
// TODO
|
|
||||||
__android_log_print(ANDROID_LOG_ERROR, TAG, "getsockopt error %d: %s",
|
__android_log_print(ANDROID_LOG_ERROR, TAG, "getsockopt error %d: %s",
|
||||||
errno, strerror(errno));
|
errno, strerror(errno));
|
||||||
cur->state = TCP_CLOSE;
|
cur->state = TCP_CLOSE;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (serr) {
|
if (serr) {
|
||||||
// TODO
|
|
||||||
__android_log_print(ANDROID_LOG_ERROR, TAG, "SO_ERROR %d: %s",
|
__android_log_print(ANDROID_LOG_ERROR, TAG, "SO_ERROR %d: %s",
|
||||||
serr, strerror(serr));
|
serr, strerror(serr));
|
||||||
cur->state = TCP_CLOSE;
|
cur->state = TCP_CLOSE;
|
||||||
|
@ -272,14 +275,44 @@ void *handle_events(void *a) {
|
||||||
// Log
|
// Log
|
||||||
char dest[20];
|
char dest[20];
|
||||||
inet_ntop(AF_INET, &(cur->daddr), dest, sizeof(dest));
|
inet_ntop(AF_INET, &(cur->daddr), dest, sizeof(dest));
|
||||||
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Established %s/%u lport=%u",
|
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Established %s/%u lport %u",
|
||||||
dest, ntohs(cur->dest), cur->lport);
|
dest, ntohs(cur->dest), cur->lport);
|
||||||
if (sendSYN(cur, args->tun) < 0)
|
|
||||||
|
// Set blocking
|
||||||
|
uint8_t flags = fcntl(cur->socket, F_GETFL, 0);
|
||||||
|
if (flags < 0 || fcntl(cur->socket, F_SETFL, flags & ~O_NONBLOCK) < 0) {
|
||||||
|
__android_log_print(ANDROID_LOG_ERROR, TAG, "fcntl error %d: %s",
|
||||||
|
errno, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (writeSYN(cur, args->tun) < 0)
|
||||||
cur->state = TCP_CLOSE;
|
cur->state = TCP_CLOSE;
|
||||||
else
|
else
|
||||||
cur->state = TCP_SYN_SENT;
|
cur->state = TCP_SYN_SENT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check incoming data
|
||||||
|
if (cur->state == TCP_ESTABLISHED) {
|
||||||
|
if (FD_ISSET(cur->socket, &rfds)) {
|
||||||
|
uint8_t buffer[MAXPKT];
|
||||||
|
ssize_t bytes = recv(cur->socket, buffer, MAXPKT, MSG_DONTWAIT);
|
||||||
|
if (bytes < 0) {
|
||||||
|
__android_log_print(ANDROID_LOG_ERROR, TAG, "recv error %d: %s",
|
||||||
|
errno, strerror(errno));
|
||||||
|
cur->state = TCP_CLOSE;
|
||||||
|
}
|
||||||
|
else if (bytes == 0) {
|
||||||
|
__android_log_print(ANDROID_LOG_ERROR, TAG, "recv socket closed");
|
||||||
|
cur->state = TCP_CLOSE;
|
||||||
|
} else {
|
||||||
|
__android_log_print(ANDROID_LOG_DEBUG, TAG, "recv lport %u bytes %d",
|
||||||
|
cur->lport, bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -412,14 +445,6 @@ int openSocket(JNIEnv *env, jobject instance, const struct sockaddr_in *daddr) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set non blocking
|
|
||||||
uint8_t flags = fcntl(sock, F_GETFL, 0);
|
|
||||||
if (flags < 0 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
|
|
||||||
__android_log_print(ANDROID_LOG_ERROR, TAG, "fcntl error %d: %s",
|
|
||||||
errno, strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Protect
|
// Protect
|
||||||
jclass cls = (*env)->GetObjectClass(env, instance);
|
jclass cls = (*env)->GetObjectClass(env, instance);
|
||||||
jmethodID mid = (*env)->GetMethodID(env, cls, "protect", "(I)Z");
|
jmethodID mid = (*env)->GetMethodID(env, cls, "protect", "(I)Z");
|
||||||
|
@ -440,6 +465,14 @@ int openSocket(JNIEnv *env, jobject instance, const struct sockaddr_in *daddr) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set non blocking
|
||||||
|
uint8_t flags = fcntl(sock, F_GETFL, 0);
|
||||||
|
if (flags < 0 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
|
||||||
|
__android_log_print(ANDROID_LOG_ERROR, TAG, "fcntl error %d: %s",
|
||||||
|
errno, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Initiate connect
|
// Initiate connect
|
||||||
int err = connect(sock, daddr, sizeof(struct sockaddr_in));
|
int err = connect(sock, daddr, sizeof(struct sockaddr_in));
|
||||||
if (err < 0 && errno != EINPROGRESS) {
|
if (err < 0 && errno != EINPROGRESS) {
|
||||||
|
@ -462,7 +495,7 @@ int getLocalPort(const int sock) {
|
||||||
return ntohs(sin.sin_port);
|
return ntohs(sin.sin_port);
|
||||||
}
|
}
|
||||||
|
|
||||||
int sendSYN(const struct connection *cur, int tun) {
|
int writeSYN(const struct connection *cur, int tun) {
|
||||||
// Build packet
|
// Build packet
|
||||||
uint16_t len = sizeof(struct iphdr) + sizeof(struct tcphdr); // no data
|
uint16_t len = sizeof(struct iphdr) + sizeof(struct tcphdr); // no data
|
||||||
u_int8_t *buffer = calloc(len, 1);
|
u_int8_t *buffer = calloc(len, 1);
|
||||||
|
|
Loading…
Reference in a new issue