Skip to content

Commit

Permalink
mini optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
SashaXser committed Apr 14, 2024
1 parent 11d6c62 commit f706b53
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions src/goodbyedpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -382,27 +385,20 @@ 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)
{
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;
}
}
Expand All @@ -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);
Expand All @@ -429,34 +424,31 @@ 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;
}
}
return NULL;
}




/** Fragment and send the packet.
*
* This function cuts off the end of the packet (step=0) or
Expand Down

0 comments on commit f706b53

Please sign in to comment.