From f706b5366ed5d7b3bd82a1b1627b3203cc06ba78 Mon Sep 17 00:00:00 2001 From: SashaXser <24498484+SashaXser@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:40:03 +0400 Subject: [PATCH] mini optimization --- src/goodbyedpi.c | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/src/goodbyedpi.c b/src/goodbyedpi.c index cd5eabac..4587ef12 100644 --- a/src/goodbyedpi.c +++ b/src/goodbyedpi.c @@ -346,20 +346,23 @@ static int find_header_and_get_info(const char *pktdata, unsigned int pktlen, char **hdrvalueaddr, unsigned int *hdrvaluelen) { char *data_addr_rn; char *hdr_begin; + size_t hdrname_len; *hdrvaluelen = 0u; *hdrnameaddr = NULL; *hdrvalueaddr = NULL; + hdrname_len = strlen(hdrname); // Calculate once + /* Search for the header */ hdr_begin = dumb_memmem(pktdata, pktlen, - hdrname, strlen(hdrname)); + hdrname, hdrname_len); if (!hdr_begin) return FALSE; if (pktdata > hdr_begin) return FALSE; /* Set header address */ *hdrnameaddr = hdr_begin; - *hdrvalueaddr = hdr_begin + strlen(hdrname); + *hdrvalueaddr = hdr_begin + hdrname_len; /* Search for header end (\r\n) */ data_addr_rn = dumb_memmem(*hdrvalueaddr, @@ -382,7 +385,7 @@ static int extract_sni(const char *pktdata, unsigned int pktlen, const unsigned char *hnaddr = NULL; int hnlen = 0; - while (pktlen >= 8) { + while (pktlen >= 9) { // Adjusted to account for minimum valid packet length if (*d == '\0' && *(d+1) == '\0' && *(d+2) == '\0' && *(d+4) == '\0' && *(d+6) == '\0' && *(d+7) == '\0' && *(d+3) - *(d+5) == 2 && *(d+5) - *(d+8) == 3) @@ -390,19 +393,12 @@ static int extract_sni(const char *pktdata, unsigned int pktlen, hnaddr = d+9; hnlen = *(d+8); - if (hnlen < 3 || hnlen > HOST_MAXLEN) { - return FALSE; - } - - if (pktlen < 8 + hnlen) { + if (hnlen < 3 || hnlen > HOST_MAXLEN || pktlen < 8 + hnlen) { return FALSE; } for (int i = 0; i < hnlen; i++) { - if (!((hnaddr[i] >= '0' && hnaddr[i] <= '9') || - (hnaddr[i] >= 'a' && hnaddr[i] <= 'z') || - hnaddr[i] == '.' || hnaddr[i] == '-')) - { + if (!(isalnum(hnaddr[i]) || hnaddr[i] == '.' || hnaddr[i] == '-')) { return FALSE; } } @@ -418,7 +414,6 @@ static int extract_sni(const char *pktdata, unsigned int pktlen, return FALSE; } - static inline void change_window_size(const PWINDIVERT_TCPHDR ppTcpHdr, unsigned int size) { if (size >= 1 && size <= 0xFFFFu) { ppTcpHdr->Window = htons((u_short)size); @@ -429,24 +424,24 @@ static inline void change_window_size(const PWINDIVERT_TCPHDR ppTcpHdr, unsigned static PVOID find_http_method_end(const char *pkt, unsigned int http_frag, int *is_fragmented) { unsigned int i; unsigned int pkt_length = strlen(pkt); - for (i = 0; i<(sizeof(http_methods) / sizeof(*http_methods)); i++) { - unsigned int method_length = strlen(http_methods[i]); - if (memcmp(pkt, http_methods[i], method_length) == 0) { + unsigned int methods_count = sizeof(http_methods) / sizeof(*http_methods); + unsigned int method_lengths[methods_count]; + for (i = 0; i < methods_count; i++) { + method_lengths[i] = strlen(http_methods[i]); + } + for (i = 0; i < methods_count; i++) { + if (memcmp(pkt, http_methods[i], method_lengths[i]) == 0) { if (is_fragmented) *is_fragmented = 0; - char *end = strchr(pkt + method_length, ' '); + char *end = strchr(pkt + method_lengths[i], ' '); if (end != NULL && end - pkt <= pkt_length) return end; } - /* Try to find HTTP method in a second part of fragmented packet */ if ((http_frag == 1 || http_frag == 2) && - memcmp(pkt, http_methods[i] + http_frag, - method_length - http_frag) == 0 - ) - { + memcmp(pkt, http_methods[i] + http_frag, method_lengths[i] - http_frag) == 0) { if (is_fragmented) *is_fragmented = 1; - char *end = strchr(pkt + method_length - http_frag, ' '); + char *end = strchr(pkt + method_lengths[i] - http_frag, ' '); if (end != NULL && end - pkt <= pkt_length) return end; } @@ -454,9 +449,6 @@ static PVOID find_http_method_end(const char *pkt, unsigned int http_frag, int * return NULL; } - - - /** Fragment and send the packet. * * This function cuts off the end of the packet (step=0) or