i have code that receives pcap packates as bytes
.
I decode the IP and UDP layer and later the proprietary app layer within (“UMS”).
I would like to check the IP header checksum and the UDP checksum prior continuing.
How to do this?
Here is my current code:
import io
import socket
from typing import Iterator
from dpkt import pcap, ip, udp
class DecodePcap:
"""
Receives a pcap file as bytes to decode it with dpkt.
"""
def __init__(self, pcap_bytes: bytes) -> None:
self.pcap = pcap.Reader(io.BytesIO(pcap_bytes))
"""
the reader is a iterator of tuples (timestamp, buf) where timestamp is a float and buf is a bytes object.
"""
def get_ums_packet_iterator(self) -> Iterator['UmsPacket']:
"""
Returns a iterator of UmsPacket.
"""
return map(UmsPacket, self.pcap)
class UmsPacket:
"""
Data class to hold the fields of a packet from pcap.
"""
def __init__(self, ums_tuple: tuple) -> None:
self.timestamp: float = ums_tuple[0]
ip_packet: ip.IP = ip.IP(ums_tuple[1])
udp_packet: udp.UDP = ip_packet.data
self.src_ip: str = socket.inet_ntoa(ip_packet.src)
self.dst_ip: str = socket.inet_ntoa(ip_packet.dst)
self.src_port: int = udp_packet.sport
self.dst_port: int = udp_packet.dport
self.data: bytes = udp_packet.data
# check ip header checksum
#?? assert ip_packet.sum
# check udp checksum
#?? assert udp_packet.sum
# 6th byte is the ICD version
self.icd: int = self.data[6]