def validate(self):
"""
Validate that the contents of this object conform to protocol specs.
"""
# Check if the traffic class fits in 8 bits
if not isinstance(self.traffic_class, int) or not (0 <= self.traffic_class < 2 ** 8):
raise ValueError("Traffic class must be an unsigned 8 bit integer")
# Check if the flow label fits in 20 bits
if not isinstance(self.flow_label, int) or not (0 <= self.flow_label < 2 ** 20):
raise ValueError("Flow label must be an unsigned 20 bit integer")
# Check if the next header fits in 8 bits
if not isinstance(self.next_header, int) or not (0 <= self.next_header < 2 ** 8):
raise ValueError("Next header type must be an unsigned 8 bit integer")
# Check if the hop limit fits in 8 bits
if not isinstance(self.hop_limit, int) or not (0 <= self.hop_limit < 2 ** 8):
raise ValueError("Hop limit must be an unsigned 8 bit integer")
# Check if the source and destination are IPv6 addresses
if not isinstance(self.source, IPv6Address):
raise ValueError("Source must be an IPv6 address")
if self.source.is_multicast:
raise ValueError("Source must be a non-multicast IPv6 address")
if not isinstance(self.destination, IPv6Address):
raise ValueError("Destination must be an IPv6 address")
# Check if the payload is a protocol element
if not isinstance(self.payload, ProtocolElement):
raise ValueError("Payload must be a protocol element")
# Check if all options are allowed
self.validate_contains([self.payload])
self.payload.validate()
评论列表
文章目录