1
1
# -*- coding: utf-8 -*-
2
2
"""data models for PCAP-NG file format"""
3
3
4
+ import datetime
4
5
import decimal
5
6
from typing import TYPE_CHECKING
6
7
8
+ from pcapkit .corekit .multidict import MultiDict
7
9
from pcapkit .protocols .data .data import Data
8
10
9
11
__all__ = [
34
36
]
35
37
36
38
if TYPE_CHECKING :
37
- from datetime import datetime
39
+ from datetime import datetime as dt_type
38
40
from datetime import timezone as dt_timezone
39
41
from decimal import Decimal
40
42
from ipaddress import IPv4Address , IPv4Interface , IPv6Address , IPv6Interface
41
- from typing import Optional
42
43
43
44
from typing_extensions import Literal
44
45
50
51
from pcapkit .const .pcapng .secrets_type import SecretsType as Enum_SecretsType
51
52
from pcapkit .const .pcapng .verdict_type import VerdictType as Enum_VerdictType
52
53
from pcapkit .const .reg .linktype import LinkType as Enum_LinkType
53
- from pcapkit .corekit .multidict import MultiDict , OrderedMultiDict
54
+ from pcapkit .corekit .multidict import OrderedMultiDict
54
55
from pcapkit .corekit .version import VersionInfo
55
56
from pcapkit .protocols .misc .pcapng import (PacketDirection , PacketReception , TLSKeyLabel ,
56
57
WireGuardKeyLabel )
@@ -412,7 +413,7 @@ class EnhancedPacketBlock(PCAPNG):
412
413
#: Interface ID.
413
414
interface_id : 'int'
414
415
#: Timestamp (in seconds).
415
- timestamp : 'datetime '
416
+ timestamp : 'dt_type '
416
417
#: Timestamp as in UNIX epoch (in seconds).
417
418
timestamp_epoch : 'Decimal'
418
419
#: Captured packet length.
@@ -426,7 +427,8 @@ class EnhancedPacketBlock(PCAPNG):
426
427
#: Protocol chain.
427
428
protocols : 'str'
428
429
429
- def __init__ (self , section_number : 'int' , number : 'int' , interface_id : 'int' , timestamp : 'datetime' ,
430
+ def __init__ (self , type : 'Enum_BlockType' , length : 'int' , section_number : 'int' ,
431
+ number : 'int' , interface_id : 'int' , timestamp : 'dt_type' ,
430
432
timestamp_epoch : 'Decimal' , captured_len : 'int' , original_len : 'int' ,
431
433
options : 'OrderedMultiDict[Enum_OptionType, Option]' ) -> 'None' : ...
432
434
@@ -441,15 +443,17 @@ class SimplePacketBlock(PCAPNG):
441
443
442
444
#: Original packet length.
443
445
original_len : 'int'
446
+ #: Captured packet length.
447
+ captured_len : 'int'
444
448
445
- def __post_init__ (self ) -> None :
449
+ def __post_init__ (self ) -> ' None' :
446
450
"""Post-initialization handling."""
447
- self . __update__ (
448
- interface_id = 0 ,
449
- timestamp = datetime . utcfromtimestamp ( 0 ) ,
450
- timestamp_epoch = decimal . Decimal ( 0 ),
451
- captured_len = self . length - 16 ,
452
- )
451
+ with decimal . localcontext ( prec = 64 ):
452
+ self . __update__ (
453
+ interface_id = 0 ,
454
+ timestamp = datetime . datetime . fromtimestamp ( 0 , datetime . timezone . utc ),
455
+ timestamp_epoch = decimal . Decimal ( 0 ) ,
456
+ )
453
457
454
458
if TYPE_CHECKING :
455
459
#: Protocol chain.
@@ -458,14 +462,12 @@ def __post_init__(self) -> None:
458
462
#: Interface ID.
459
463
interface_id : 'int'
460
464
#: Timestamp (in seconds).
461
- timestamp : 'datetime '
465
+ timestamp : 'dt_type '
462
466
#: Timestamp as in UNIX epoch (in seconds).
463
467
timestamp_epoch : 'Decimal'
464
- #: Captured packet length.
465
- captured_len : 'int'
466
468
467
469
def __init__ (self , section_number : 'int' , number : 'int' , type : 'Enum_BlockType' ,
468
- length : 'int' , original_len : 'int' ) -> 'None' : ...
470
+ length : 'int' , original_len : 'int' , captured_len : 'int' ) -> 'None' : ...
469
471
470
472
471
473
class NameResolutionRecord (Data ):
@@ -553,42 +555,60 @@ def __init__(self, type: 'Enum_OptionType', length: 'int', ip: 'IPv6Address') ->
553
555
class NameResolutionBlock (PCAPNG ):
554
556
"""Data model for PCAP-NG Name Resolution Block (NRB)."""
555
557
556
- #: Name resolution mapping (IP address -> name).
557
- mapping : 'MultiDict[IPv4Address | IPv6Address, str]'
558
- #: Name resolution mapping (name -> IP address).
559
- reverse_mapping : 'MultiDict[str, IPv4Address | IPv6Address]'
558
+ #: Records.
559
+ records : 'OrderedMultiDict[Enum_RecordType, NameResolutionRecord]'
560
560
#: Options.
561
561
options : 'OrderedMultiDict[Enum_OptionType, Option]'
562
562
563
+ def __post_init__ (self ) -> 'None' :
564
+ """Post-initialization handling."""
565
+ mapping = MultiDict () # type: MultiDict[IPv4Address | IPv6Address, str]
566
+ reverse_mapping = MultiDict () # type: MultiDict[str, IPv4Address | IPv6Address]
567
+
568
+ for record in self .records :
569
+ if isinstance (record , (IPv4Record , IPv6Record )):
570
+ for name in record .names :
571
+ mapping .add (record .ip , name )
572
+ reverse_mapping .add (name , record .ip )
573
+
574
+ self .__update__ (
575
+ mapping = mapping ,
576
+ reverse_mapping = reverse_mapping ,
577
+ )
578
+
563
579
if TYPE_CHECKING :
564
- def __init__ (self , type : 'Enum_BlockType' , length : 'int' , mapping : 'MultiDict[IPv4Address | IPv6Address, str]' ,
565
- reverse_mapping : 'MultiDict[str, IPv4Address | IPv6Address]' ,
580
+ #: Name resolution mapping (IP address -> name).
581
+ mapping : 'MultiDict[IPv4Address | IPv6Address, str]'
582
+ #: Name resolution mapping (name -> IP address).
583
+ reverse_mapping : 'MultiDict[str, IPv4Address | IPv6Address]'
584
+
585
+ def __init__ (self , type : 'Enum_BlockType' , length : 'int' , records : 'OrderedMultiDict[Enum_RecordType, NameResolutionRecord]' ,
566
586
options : 'OrderedMultiDict[Enum_OptionType, Option]' ) -> 'None' : ...
567
587
568
588
569
589
class ISB_StartTimeOption (Option ):
570
590
"""Data model for PCAP-NG ``isb_starttime`` option."""
571
591
572
592
#: Start time.
573
- timestamp : 'datetime '
593
+ timestamp : 'dt_type '
574
594
#: Start time as in UNIX epoch (in seconds).
575
595
timestamp_epoch : 'Decimal'
576
596
577
597
if TYPE_CHECKING :
578
- def __init__ (self , type : 'Enum_OptionType' , length : 'int' , timestamp : 'datetime ' ,
598
+ def __init__ (self , type : 'Enum_OptionType' , length : 'int' , timestamp : 'dt_type ' ,
579
599
timestamp_epoch : 'Decimal' ) -> 'None' : ...
580
600
581
601
582
602
class ISB_EndTimeOption (Option ):
583
603
"""Data model for PCAP-NG ``isb_endtime`` option."""
584
604
585
605
#: End time.
586
- timestamp : 'datetime '
606
+ timestamp : 'dt_type '
587
607
#: End time as in UNIX epoch (in seconds).
588
608
timestamp_epoch : 'Decimal'
589
609
590
610
if TYPE_CHECKING :
591
- def __init__ (self , type : 'Enum_OptionType' , length : 'int' , timestamp : 'datetime ' ,
611
+ def __init__ (self , type : 'Enum_OptionType' , length : 'int' , timestamp : 'dt_type ' ,
592
612
timestamp_epoch : 'Decimal' ) -> 'None' : ...
593
613
594
614
@@ -648,25 +668,25 @@ class InterfaceStatisticsBlock(PCAPNG):
648
668
#: Interface ID.
649
669
interface_id : 'int'
650
670
#: Timestamp.
651
- timestamp : 'datetime '
671
+ timestamp : 'dt_type '
652
672
#: Timestamp as in UNIX epoch (in seconds).
653
673
timestamp_epoch : 'Decimal'
654
674
#: Options.
655
675
options : 'OrderedMultiDict[Enum_OptionType, Option]'
656
676
657
677
if TYPE_CHECKING :
658
- def __init__ (self , type : 'Enum_BlockType' , length : 'int' , interface_id : 'int' , timestamp : 'datetime ' ,
678
+ def __init__ (self , type : 'Enum_BlockType' , length : 'int' , interface_id : 'int' , timestamp : 'dt_type ' ,
659
679
timestamp_epoch : 'Decimal' , options : 'OrderedMultiDict[Enum_OptionType, Option]' ) -> 'None' : ...
660
680
661
681
662
682
class SystemdJournalExportBlock (PCAPNG ):
663
683
"""Data model for PCAP-NG :manpage:`systemd(1)` Journal Export Block."""
664
684
665
685
#: Journal entry.
666
- data : 'tuple[OrderedMultiDict[str, str | bytes]]'
686
+ data : 'tuple[OrderedMultiDict[str, str | bytes], ... ]'
667
687
668
688
if TYPE_CHECKING :
669
- def __init__ (self , type : 'Enum_BlockType' , length : 'int' , data : 'tuple[OrderedMultiDict[str, str | bytes]]' ) -> 'None' : ...
689
+ def __init__ (self , type : 'Enum_BlockType' , length : 'int' , data : 'tuple[OrderedMultiDict[str, str | bytes], ... ]' ) -> 'None' : ...
670
690
671
691
672
692
class DSBSecrets (Data ):
@@ -752,7 +772,7 @@ class CustomBlock(PCAPNG):
752
772
753
773
#: Private enterprise number.
754
774
pen : 'int'
755
- #: Custom block data.
775
+ #: Custom block data (incl. data, options and padding) .
756
776
data : 'bytes'
757
777
758
778
if TYPE_CHECKING :
@@ -817,7 +837,7 @@ class PacketBlock(PCAPNG):
817
837
#: Drops count.
818
838
drop_count : 'int'
819
839
#: Timestamp.
820
- timestamp : 'datetime '
840
+ timestamp : 'dt_type '
821
841
#: Timestamp as in UNIX epoch (in seconds).
822
842
timestamp_epoch : 'Decimal'
823
843
#: Captured packet length.
@@ -831,7 +851,7 @@ class PacketBlock(PCAPNG):
831
851
#: Protocol chain.
832
852
protocols : 'str'
833
853
834
- def __init__ (self , section_number : 'int' , number : 'int' , type : 'Enum_BlockType ' ,
835
- length : 'int' , interface_id : 'int' , drop_count : 'int' , timestamp : 'datetime ' ,
854
+ def __init__ (self , type : 'Enum_BlockType' , length : ' int' , section_number : 'int' , number : 'int ' ,
855
+ interface_id : 'int' , drop_count : 'int' , timestamp : 'dt_type ' ,
836
856
timestamp_epoch : 'Decimal' , captured_length : 'int' , original_length : 'int' ,
837
- protocols : 'str' , options : 'OrderedMultiDict[Enum_OptionType, Option]' ) -> 'None' : ...
857
+ options : 'OrderedMultiDict[Enum_OptionType, Option]' ) -> 'None' : ...
0 commit comments