ioBroker / ioBroker.kisshome-research

Adapter to collect the data for KISSHome project https://kisshome.de/news/
MIT License
0 stars 3 forks source link

Use actual IP and TCP header lenght to determine cutoff point #49

Closed gsenkowski closed 4 weeks ago

gsenkowski commented 1 month ago

We noticed some inconsistencies in recorded packets and traced them back to the fixed lenghts for IP and TCP headers. To fix them we propose the following changes in recording.ts.

        // If IPv4
        if (ethType === 0x0800) {
            const ipHeaderStart = offset + 2;
            const ipVersionAndIHL = context.buffer[ipHeaderStart];
            const ipHeaderLength = (ipVersionAndIHL & 0x0F) * 4; // IHL field gives the length of the IP header

            // read protocol type (TCP/UDP/ICMP/etc.)
            const protocolType = context.buffer[ipHeaderStart + 9]; // Protocol field in IP header

            if (protocolType === 6) { // TCP
                const tcpHeaderStart = ipHeaderStart + ipHeaderLength;
                const tcpOffsetAndFlags = context.buffer[tcpHeaderStart + 12];
                const tcpHeaderLength = (tcpOffsetAndFlags >> 4) * 4; // Data offset in TCP header
                maxBytes = ipHeaderLength + tcpHeaderLength + 14; // Total length: IP header + TCP header + Ethernet header
            } else if (protocolType === 17) { // UDP
                maxBytes = ipHeaderLength + 8 + 14; // IP header + 8 bytes UDP header + Ethernet header
            } else if (protocolType === 1) { // ICMP
                maxBytes = 0;
            } else {
                maxBytes = 0;
            }
        } else if (ethTypeModified === 0x0800) {
            const ipHeaderStart = offset + 2 + 18; // What is 18?
            const ipVersionAndIHL = context.buffer[ipHeaderStart];
            const ipHeaderLength = (ipVersionAndIHL & 0x0F) * 4; // IHL field gives the length of the IP header

            // read protocol type (TCP/UDP/ICMP/etc.)
            const protocolType = context.buffer[ipHeaderStart + 9]; // Protocol field in IP header

            if (protocolType === 6) { // TCP
                const tcpHeaderStart = ipHeaderStart + ipHeaderLength;
                const tcpOffsetAndFlags = context.buffer[tcpHeaderStart + 12];
                const tcpHeaderLength = (tcpOffsetAndFlags >> 4) * 4; // Data offset in TCP header
                maxBytes = ipHeaderLength + tcpHeaderLength + 14 + 18; // Total length: IP header + TCP header + Ethernet header
            } else if (protocolType === 17) { // UDP
                maxBytes = ipHeaderLength + 8 + 14 + 18; // IP header + 8 bytes UDP header + Ethernet header
            } else if (protocolType === 1) { // ICMP
                maxBytes = 0;
            } else {
                maxBytes = 0;
            }
        }