From 20a26e6fdb703f1ae3703da72d79ce7403ad7279 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Fri, 20 Mar 2020 16:55:20 -0400 Subject: [PATCH] Implement the UDP checksum As long as we are doing UDP and IP in house, might as well do the optional UDP checksum. --- confluent_server/confluent/discovery/protocols/pxe.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/confluent_server/confluent/discovery/protocols/pxe.py b/confluent_server/confluent/discovery/protocols/pxe.py index 31a5a996..dd46dd46 100644 --- a/confluent_server/confluent/discovery/protocols/pxe.py +++ b/confluent_server/confluent/discovery/protocols/pxe.py @@ -38,6 +38,12 @@ udphdr = b'\x00\x43\x00\x44\x00\x00\x00\x00' def _ipsum(data): currsum = 0 + if len(data) % 2: + append = bytearray(2) + append[0] = data[-1] + currsum = struct.unpack('!H', append) + data = memoryview(data) + data = data[:-1] for datum in struct.unpack('!' + 'H' * (len(data) // 2), data): currsum += datum if currsum >> 16: @@ -378,6 +384,11 @@ def check_reply(node, info, packet, sock, cfg, reqview): curripsum = ~(_ipsum(constiphdrsum + pktlen + myipn)) & 0xffff repview[10:12] = struct.pack('!H', curripsum) repview[24:26] = struct.pack('!H', 256 + 8) + sumdata = repview[28:] + datasum = _ipsum(bytes(repview[12:]) + b'\x00\x11' + bytes( + repview[24:26])) + datasum = ~datasum & 0xffff + repview[26:28] = struct.pack('!H', datasum) tsock = socket.socket(socket.AF_PACKET, socket.SOCK_DGRAM, socket.htons(0x800)) targ = sockaddr_ll()