@@ -81,8 +81,9 @@ class RecordTimestamp:
8181 resolution and its original length would be lost between the reader and
8282 :func:`packet2frame`. The engine's readers yield a :class:`Timestamp` or a
8383 :class:`DecimalTimestamp` instead, which *is* the value `DPKT`_ yields --
84- so reassembly and flow labels see exactly what they did before -- and also
85- carries those three.
84+ so a stored frame's timestamp is exactly what it was before -- and also
85+ carries those three. Reassembly and flow tracing are handed a plain
86+ :obj:`float` of it, as by every other engine (:issue:`1501`).
8687
8788 .. _DPKT: https://dpkt.readthedocs.io
8889
@@ -395,6 +396,30 @@ def _layer2bytes(packet: 'Packet', layer: 'Packet') -> 'tuple[bytes, bool]':
395396 return packet2bytes (layer ), False
396397
397398
399+ def _network (packet : 'Packet' ) -> 'Optional[IP | IP6]' :
400+ """Fetch the network layer of a DPKT packet.
401+
402+ Args:
403+ packet: DPKT packet, i.e. the outermost layer.
404+
405+ Returns:
406+ The packet itself if it is an IPv4 or IPv6 packet, as `DPKT`_ reads every
407+ frame of a raw-IP link type (:issue:`1548`); otherwise the IPv4 or IPv6
408+ payload of its link layer, which `DPKT`_ names ``ip`` or ``ip6`` -- a
409+ bare IP packet names neither -- or :data:`None` if it has none.
410+
411+ .. _DPKT: https://dpkt.readthedocs.io
412+
413+ """
414+ import dpkt # isort:skip
415+
416+ if isinstance (packet , (dpkt .ip .IP , dpkt .ip6 .IP6 )):
417+ return packet
418+ if hasattr (packet , 'ip' ):
419+ return cast ('IP' , packet .ip )
420+ return getattr (packet , 'ip6' , None )
421+
422+
398423def ipv6_hdr_len (ipv6 : 'IP6' ) -> 'int' :
399424 """Calculate length of headers before IPv6 Fragment header.
400425
@@ -482,7 +507,9 @@ def ipv4_reassembly(packet: 'Packet', timestamp: 'float', *,
482507 than on the packet, so it is passed in -- as :func:`tcp_traceflow`
483508 does. A caller holding only a frame can read it back with
484509 :func:`packet2timestamp`, which is where
485- :class:`~pcapkit.foundation.engines.dpkt.DPKT` leaves it.
510+ :class:`~pcapkit.foundation.engines.dpkt.DPKT` leaves it. Handed on
511+ as a :obj:`float`, as every engine's adapters do, though `DPKT`_ reads
512+ a nanosecond PCAP's as a :class:`~decimal.Decimal` (:issue:`1501`).
486513 count: Packet index. If not provided, default to ``-1``.
487514
488515 Returns:
@@ -507,7 +534,9 @@ def ipv4_reassembly(packet: 'Packet', timestamp: 'float', *,
507534 :class:`pcapkit.foundation.reassembly.ipv4.IPv4`
508535
509536 """
510- ipv4 = getattr (packet , 'ip' , None ) # type: Optional[IP]
537+ network = _network (packet )
538+ # an IPv6 layer is the one with extension headers, as in ``_wire_hdr_len``
539+ ipv4 = None if hasattr (network , 'extension_hdrs' ) else network
511540 if ipv4 is not None :
512541 if ipv4 .df : # dismiss not fragmented packet
513542 return None
@@ -537,7 +566,7 @@ def ipv4_reassembly(packet: 'Packet', timestamp: 'float', *,
537566 tl = ipv4 .len , # total length, header includes
538567 header = wire [:ihl ], # raw bytes type header
539568 payload = bytearray (wire [ihl :]), # raw bytearray type payload
540- timestamp = timestamp , # capture timestamp
569+ timestamp = float ( timestamp ), # capture timestamp
541570 )
542571 return data
543572 return None
@@ -551,6 +580,7 @@ def ipv6_reassembly(packet: 'Packet', timestamp: 'float', *,
551580 packet: DPKT packet.
552581 timestamp: Capture timestamp of the packet, which drives the reassembly
553582 timeout; :func:`packet2timestamp` reads it back off a stored frame.
583+ Handed on as a :obj:`float`, as for :func:`ipv4_reassembly`.
554584 count: Packet index. If not provided, default to ``-1``.
555585
556586 Returns:
@@ -575,7 +605,8 @@ def ipv6_reassembly(packet: 'Packet', timestamp: 'float', *,
575605 :class:`pcapkit.foundation.reassembly.ipv6.IPv6`
576606
577607 """
578- ipv6 = getattr (packet , 'ip6' , None ) # type: Optional[IP6]
608+ network = _network (packet )
609+ ipv6 = cast ('IP6' , network ) if hasattr (network , 'extension_hdrs' ) else None
579610 if ipv6 is not None :
580611 ipv6_frag = ipv6 .extension_hdrs .get (44 ) # type: Optional[IP6FragmentHeader]
581612 if ipv6_frag is None : # dismiss not fragmented packet
@@ -618,7 +649,7 @@ def ipv6_reassembly(packet: 'Packet', timestamp: 'float', *,
618649 tl = hdr_len + len (payload ), # total length, header includes
619650 header = wire [:hdr_len ], # raw bytes type header before IPv6-Frag
620651 payload = bytearray (payload ), # raw bytearray type payload after IPv6-Frag
621- timestamp = timestamp , # capture timestamp
652+ timestamp = float ( timestamp ), # capture timestamp
622653 )
623654 return data
624655 return None
@@ -632,6 +663,7 @@ def tcp_reassembly(packet: 'Packet', timestamp: 'float', *,
632663 packet: DPKT packet.
633664 timestamp: Capture timestamp of the packet, which drives the reassembly
634665 timeout; :func:`packet2timestamp` reads it back off a stored frame.
666+ Handed on as a :obj:`float`, as for :func:`ipv4_reassembly`.
635667 count: Packet index. If not provided, default to ``-1``.
636668
637669 Returns:
@@ -655,11 +687,8 @@ def tcp_reassembly(packet: 'Packet', timestamp: 'float', *,
655687 :class:`pcapkit.foundation.reassembly.tcp.TCP`
656688
657689 """
658- if hasattr (packet , 'ip' ):
659- ip = cast ('IP' , packet .ip )
660- elif hasattr (packet , 'ip6' ):
661- ip = cast ('IP6' , packet .ip6 )
662- else :
690+ ip = _network (packet )
691+ if ip is None :
663692 return None
664693
665694 tcp = getattr (ip , 'tcp' , None ) # type: Optional[TCP]
@@ -688,7 +717,7 @@ def tcp_reassembly(packet: 'Packet', timestamp: 'float', *,
688717 first = tcp .seq , # first sequence number of payload
689718 last = tcp .seq + raw_len - 1 , # last sequence number of payload
690719 len = raw_len , # payload length, header excludes
691- timestamp = timestamp , # capture timestamp
720+ timestamp = float ( timestamp ), # capture timestamp
692721 )
693722 return data
694723 return None
@@ -700,7 +729,10 @@ def tcp_traceflow(packet: 'Packet', timestamp: 'float', *,
700729
701730 Args:
702731 packet: DPKT packet.
703- timestamp: Timestamp of the packet.
732+ timestamp: Timestamp of the packet, handed on as a :obj:`float`, as for
733+ :func:`ipv4_reassembly`. The PCAP frame record is built from the
734+ exact timestamp attached by :func:`attach_timestamp`, if any; see
735+ :func:`packet2frame`.
704736 data_link: Data link layer protocol (from global header).
705737 count: Packet index. If not provided, default to ``-1``.
706738
@@ -725,11 +757,8 @@ def tcp_traceflow(packet: 'Packet', timestamp: 'float', *,
725757 :class:`pcapkit.foundation.traceflow.tcp.TCP`
726758
727759 """
728- if hasattr (packet , 'ip' ):
729- ip = cast ('IP' , packet .ip )
730- elif hasattr (packet , 'ip6' ):
731- ip = cast ('IP6' , packet .ip6 )
732- else :
760+ ip = _network (packet )
761+ if ip is None :
733762 return None
734763
735764 tcp = getattr (ip , 'tcp' , None ) # type: Optional[TCP]
@@ -750,7 +779,7 @@ def tcp_traceflow(packet: 'Packet', timestamp: 'float', *,
750779 dst = ipaddress .ip_address (ip .dst ), # destination IP
751780 srcport = tcp .sport , # TCP source port
752781 dstport = tcp .dport , # TCP destination port
753- timestamp = timestamp , # timestamp
782+ timestamp = float ( timestamp ), # timestamp
754783 seq = tcp .seq , # TCP sequence number
755784 ack = tcp .ack , # TCP acknowledgement number
756785 header = wire [:tcp .off * 4 ], # raw bytes type header
0 commit comments