Native checksum

This commit is contained in:
M66B 2016-01-15 12:57:32 +01:00
parent bad7ece7ba
commit 00b476f599
1 changed files with 19 additions and 34 deletions

View File

@ -18,6 +18,9 @@
// TODO log allowed traffic // TODO log allowed traffic
// TODO header file // TODO header file
// TODO debug switches // TODO debug switches
// TODO fix warnings
// Window size < 2^31: x <= y: (uint32_t)(y-x) < 0x80000000
#define TAG "NetGuard.JNI" #define TAG "NetGuard.JNI"
#define MAXPKT 32678 #define MAXPKT 32678
@ -72,7 +75,7 @@ void handle_ip(JNIEnv *, jobject, const struct arguments *, const uint8_t *, con
jint getUid(const int, const int, const void *, const uint16_t); jint getUid(const int, const int, const void *, const uint16_t);
unsigned short checksum(unsigned short *, int); uint16_t checksum(uint8_t *, uint16_t);
char *hex(const u_int8_t *, const u_int16_t); char *hex(const u_int8_t *, const u_int16_t);
@ -88,11 +91,6 @@ struct connection *connection = NULL;
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_eu_faircode_netguard_SinkholeService_jni_1init(JNIEnv *env, jobject instance) { Java_eu_faircode_netguard_SinkholeService_jni_1init(JNIEnv *env, jobject instance) {
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Init"); __android_log_print(ANDROID_LOG_DEBUG, TAG, "Init");
__android_log_print(ANDROID_LOG_DEBUG, TAG, "TCP_SYN_RECV %d", TCP_SYN_RECV);
__android_log_print(ANDROID_LOG_DEBUG, TAG, "TCP_ESTABLISHED %d", TCP_ESTABLISHED);
__android_log_print(ANDROID_LOG_DEBUG, TAG, "TCP_CLOSE_WAIT %d", TCP_CLOSE_WAIT);
__android_log_print(ANDROID_LOG_DEBUG, TAG, "TCP_LAST_ACK %d", TCP_LAST_ACK);
__android_log_print(ANDROID_LOG_DEBUG, TAG, "TCP_CLOSE %d", TCP_CLOSE);
connection = NULL; connection = NULL;
} }
@ -593,8 +591,8 @@ void handle_tcp(JNIEnv *env, jobject instance, const struct arguments *args,
else if (cur->state == TCP_LAST_ACK) { else if (cur->state == TCP_LAST_ACK) {
if (ntohl(tcphdr->ack_seq) == cur->local_seq + 1 && if (ntohl(tcphdr->ack_seq) == cur->local_seq + 1 &&
ntohl(tcphdr->seq) >= cur->remote_seq + 1) { ntohl(tcphdr->seq) >= cur->remote_seq + 1) {
cur->local_seq += 1; // local SYN cur->local_seq += 1; // local ACK
cur->remote_seq += 1; // remote SYN cur->remote_seq += 1; // remote FIN
__android_log_print(ANDROID_LOG_DEBUG, TAG, "Full close"); __android_log_print(ANDROID_LOG_DEBUG, TAG, "Full close");
// socket has been shutdown already // socket has been shutdown already
@ -1000,36 +998,23 @@ jint getUid(const int protocol, const int version, const void *saddr, const uint
return uid; return uid;
} }
// TODO data types uint16_t checksum(uint8_t *buffer, uint16_t length) {
// TODO endianess? register uint32_t sum = 0;
unsigned short checksum(unsigned short *addr, int len) { register uint16_t *buf = buffer;
register int sum = 0; register int len = length;
u_short answer = 0;
register u_short *w = addr;
register int nleft = len;
/* while (len > 1) {
* Our algorithm is simple, using a 32-bit accumulator (sum), sum += *buf++;
* we add sequential 16-bit words to it, and at the end, fold back len -= 2;
* all the carry bits from the top 16 bits into the lower 16 bits.
*/
while (nleft > 1) {
sum += *w++;
nleft -= 2;
} }
/* mop up an odd byte, if necessary */ if (len > 0)
if (nleft == 1) { sum += *((uint8_t *) buf);
*(u_char *) (&answer) = *(u_char *) w;
sum += answer;
}
/* add back carry outs from top 16 bits to low 16 bits */ while (sum >> 16)
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ sum = (sum & 0xFFFF) + (sum >> 16);
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */ return (uint16_t) (~sum);
return (answer);
} }
char *hex(const u_int8_t *data, const u_int16_t len) { char *hex(const u_int8_t *data, const u_int16_t len) {