-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathprotocol.py
More file actions
1216 lines (969 loc) · 45.5 KB
/
protocol.py
File metadata and controls
1216 lines (969 loc) · 45.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# mypy: disable-error-code=dict-item
"""Root Protocol
===================
.. module:: pcapkit.protocols.protocol
:mod:`pcapkit.protocols.protocol` contains
:class:`~pcapkit.protocols.protocol.Protocol` only, which is
an abstract base class for all protocol family, with pre-defined
utility arguments and methods of specified protocols.
"""
import abc
import collections
import contextlib
import enum
import functools
import io
import os
import shutil
import string
import struct
import textwrap
import urllib.parse
from typing import TYPE_CHECKING, Any, Generic, Optional, Type, TypeVar, cast, overload
import aenum
import chardet
from pcapkit.corekit.module import ModuleDescriptor
from pcapkit.corekit.protochain import ProtoChain
from pcapkit.protocols import data as data_module
from pcapkit.protocols import schema as schema_module
from pcapkit.protocols.data.data import Data
from pcapkit.protocols.data.misc.raw import Raw as Data_Raw
from pcapkit.protocols.data.protocol import Packet as Data_Packet
from pcapkit.protocols.schema.misc.raw import Raw as Schema_Raw
from pcapkit.protocols.schema.schema import Schema
from pcapkit.utilities.compat import cached_property
from pcapkit.utilities.decorators import beholder, seekset
from pcapkit.utilities.exceptions import (ProtocolNotFound, ProtocolNotImplemented, RegistryError,
StructError, UnsupportedCall)
from pcapkit.utilities.warnings import RegistryWarning, warn
if TYPE_CHECKING:
from enum import IntEnum as StdlibEnum
from typing import IO, Any, DefaultDict, Optional, Type
from aenum import IntEnum as AenumEnum
from typing_extensions import Literal, Self
__all__ = ['ProtocolBase']
_PT = TypeVar('_PT', bound='Data')
_ST = TypeVar('_ST', bound='Schema')
# readable characters' order list
readable = [ord(char) for char in filter(lambda char: not char.isspace(), string.printable)]
class ProtocolMeta(abc.ABCMeta):
"""Meta class to add dynamic support to :class:`Protocol`.
This meta class is used to generate necessary attributes for the
:class:`Protocol` class. It can be useful to reduce unnecessary
registry calls and simplify the customisation process.
"""
class ProtocolBase(Generic[_PT, _ST], metaclass=ProtocolMeta):
"""Abstract base class for all protocol family.
Note:
This class is for internal use only. For customisation, please use
:class:`Protocol` instead.
"""
if TYPE_CHECKING:
#: Parsed packet data.
_info: '_PT'
#: Raw packet data.
_data: 'bytes'
#: Source packet stream.
_file: 'IO[bytes]'
#: Next layer protocol instance.
_next: 'ProtocolBase'
#: Protocol chain instance.
_protos: 'ProtoChain'
# Internal data storage for cached properties.
__cached__: 'dict[str, Any]'
#: Protocol packet data definition.
__data__: 'Type[_PT]'
#: Protocol header schema definition.
__schema__: 'Type[_ST]'
#: Protocol header schema instance.
__header__: '_ST'
##########################################################################
# Defaults.
##########################################################################
#: Layer of protocol, can be one of ``Link``, ``Internet``, ``Transport``
#: and ``Application``. For example, the layer of
#: :class:`~pcapkit.protocols.link.ethernet.Ethernet` is ``Link``. However,
#: certain protocols are not in any layer, such as
#: :class:`~pcapkit.protocols.misc.raw.Raw`, and thus its layer is :obj:`None`.
__layer__: 'Optional[Literal["Link", "Internet", "Transport", "Application"]]' = None
#: Protocol index mapping for decoding next layer, c.f.
#: :meth:`self._decode_next_layer <pcapkit.protocols.protocol.Protocol._decode_next_layer>`
#: & :meth:`self._import_next_layer <pcapkit.protocols.protocol.Protocol._import_next_layer>`.
#: The values should be a tuple representing the module name and class name,
#: or a :class:`Protocol` subclass.
__proto__: 'DefaultDict[int, ModuleDescriptor[ProtocolBase] | Type[ProtocolBase]]' = collections.defaultdict(
lambda: ModuleDescriptor('pcapkit.protocols.misc.raw', 'Raw'),
)
##########################################################################
# Properties.
##########################################################################
# name of current protocol
@property
@abc.abstractmethod
def name(self) -> 'str':
"""Name of current protocol."""
# acronym of current protocol
@property
def alias(self) -> 'str':
"""Acronym of current protocol."""
return self.__class__.__name__
# key name for the info dict
@property
def info_name(self) -> 'str':
"""Key name of the :attr:`info` dict."""
return self.__class__.__name__.lower()
# info dict of current instance
@property
def info(self) -> '_PT':
"""Info dict of current instance."""
return self._info
# binary packet data if current instance
@property
def data(self) -> 'bytes':
"""Binary packet data of current instance."""
return self._data
# header length of current protocol
@property
@abc.abstractmethod
def length(self) -> 'int':
"""Header length of current protocol."""
# payload of current instance
@property
def payload(self) -> 'ProtocolBase':
"""Payload of current instance."""
return self._next
# name of next layer protocol
@property
def protocol(self) -> 'Optional[str]':
"""Name of next layer protocol (if any)."""
with contextlib.suppress(IndexError):
return self._protos[0]
return None
# protocol chain of current instance
@property
def protochain(self) -> 'ProtoChain':
"""Protocol chain of current instance."""
return self._protos
# packet data
@cached_property
def packet(self) -> 'Data_Packet':
"""Data_Packet data of the protocol."""
try:
return self._read_packet(header=self.length)
except UnsupportedCall:
return Data_Packet(
header=b'',
payload=self._read_packet(),
)
# schema data
@cached_property
def schema(self) -> '_ST':
"""Schema data of the protocol."""
return self.__header__
##########################################################################
# Methods.
##########################################################################
@classmethod
def id(cls) -> 'tuple[str, ...]':
"""Index ID of the protocol.
Returns:
By default, it returns the name of the protocol. In certain cases,
the method may return multiple values.
See Also:
:meth:`pcapkit.protocols.protocol.Protocol.__getitem__`
"""
return (cls.__name__,)
@abc.abstractmethod
def read(self, length: 'Optional[int]' = None, **kwargs: 'Any') -> '_PT':
"""Read (parse) packet data.
Args:
length: Length of packet data.
**kwargs: Arbitrary keyword arguments.
Returns:
Parsed packet data.
"""
@abc.abstractmethod
def make(self, **kwargs: 'Any') -> '_ST':
"""Make (construct) packet data.
Args:
**kwargs: Arbitrary keyword arguments.
Returns:
Curated protocol schema data.
"""
def pack(self, **kwargs: 'Any') -> 'bytes':
"""Pack (construct) packet data.
Args:
**kwargs: Arbitrary keyword arguments.
Returns:
Constructed packet data.
Notes:
We used a special keyword argument ``__packet__`` to pass the
global packet data to underlying methods. This is useful when
the packet data is not available in the current instance.
"""
self.__header__ = self.make(**kwargs)
packet = kwargs.get('__packet__', {}) # packet data
return self.__header__.pack(packet)
def unpack(self, length: 'Optional[int]' = None, **kwargs: 'Any') -> '_PT':
"""Unpack (parse) packet data.
Args:
length: Length of packet data.
**kwargs: Arbitrary keyword arguments.
Returns:
Parsed packet data.
Notes:
We used a special keyword argument ``__packet__`` to pass the
global packet data to underlying methods. This is useful when
the packet data is not available in the current instance.
"""
if cast('Optional[_ST]', self.__header__) is None:
packet = kwargs.get('__packet__', {}) # packet data
self.__header__ = cast('_ST', self.__schema__.unpack(self._file, length, packet)) # type: ignore[call-arg,misc]
return self.read(length, **kwargs)
@staticmethod
def decode(byte: bytes, *, encoding: 'Optional[str]' = None,
errors: 'Literal["strict", "ignore", "replace"]' = 'strict') -> 'str':
"""Decode :obj:`bytes` into :obj:`str`.
Should decoding failed using ``encoding``, the method will try again decoding
the :obj:`bytes` as ``'unicode_escape'`` with ``'replace'`` for error handling.
See Also:
The method is a wrapping function for :meth:`bytes.decode`.
Args:
byte: Source bytestring.
encoding: The encoding with which to decode the :obj:`bytes`.
If not provided, :mod:`pcapkit` will first try detecting its encoding
using |chardet|_. The fallback encoding would is **UTF-8**.
errors: The error handling scheme to use for the handling of decoding errors.
The default is ``'strict'`` meaning that decoding errors raise a
:exc:`UnicodeDecodeError`. Other possible values are ``'ignore'`` and ``'replace'``
as well as any other name registered with :func:`codecs.register_error` that
can handle :exc:`UnicodeDecodeError`.
.. |chardet| replace:: ``chardet``
.. _chardet: https://chardet.readthedocs.io
"""
charset = encoding or chardet.detect(byte)['encoding'] or 'utf-8'
try:
return byte.decode(charset, errors=errors)
except UnicodeError:
return byte.decode('unicode_escape', errors='replace')
@staticmethod
def unquote(url: str, *, encoding: 'str' = 'utf-8',
errors: 'Literal["strict", "ignore", "replace"]' = 'replace') -> 'str':
"""Unquote URLs into readable format.
Should decoding failed , the method will try again replacing ``'%'`` with ``'\\x'`` then
decoding the ``url`` as ``'unicode_escape'`` with ``'replace'`` for error handling.
See Also:
This method is a wrapper function for :func:`urllib.parse.unquote`.
Args:
url: URL string.
encoding: The encoding with which to decode the :obj:`bytes`.
errors: The error handling scheme to use for the handling of decoding errors.
The default is ``'strict'`` meaning that decoding errors raise a
:exc:`UnicodeDecodeError`. Other possible values are ``'ignore'`` and ``'replace'``
as well as any other name registered with :func:`codecs.register_error` that
can handle :exc:`UnicodeDecodeError`.
"""
try:
return urllib.parse.unquote(url, encoding=encoding, errors=errors)
except UnicodeError:
return url.replace('%', r'\x').encode().decode('unicode_escape', errors='replace')
@staticmethod
def expand_comp(value: 'str | ProtocolBase | Type[ProtocolBase]') -> 'tuple':
"""Expand protocol class to protocol name.
The method is used to expand protocol class to protocol name, in the
following manner:
1. If ``value`` is a protocol instance, the method will return the
protocol class, and the protocol names in upper case obtained from
:meth:`Protocol.id <pcapkit.protocols.protocol.Protocol.id>`.
2. If ``value`` is a protocol class, the method will return the
protocol class itself, and the protocols names in upper case
obtained from :meth:`Protocol.id <pcapkit.protocols.protocol.Protocol.id>`.
3. If ``value`` is :obj:`str`, the method will attempt to search for
the existing registered protocol class from
:data:`pcapkit.protocols.__proto__` and follow **step 2**; otherwise,
return the value itself.
Args:
value: Protocol class or name.
"""
if isinstance(value, type) and issubclass(value, ProtocolBase):
comp = (value, *(name.upper() for name in value.id()))
elif isinstance(value, ProtocolBase):
comp = (type(value), *(name.upper() for name in value.id()))
else:
from pcapkit.protocols import __proto__ as protocols_registry # pylint: disable=import-outside-toplevel # isort: skip
if (proto := protocols_registry.get(value.upper())) is not None:
comp = (proto, *(name.upper() for name in proto.id()))
else:
comp = (value.upper(),)
return comp
@classmethod
def analyze(cls, proto: 'int', payload: 'bytes', **kwargs: 'Any') -> 'ProtocolBase':
"""Analyse packet payload.
Args:
proto: Protocol registry number.
payload: Packet payload.
**kwargs: Arbitrary keyword arguments.
Returns:
Parsed payload as a :class:`~pcapkit.protocols.protocol.Protocol`
instance.
"""
protocol = cls.__proto__[proto]
if isinstance(protocol, ModuleDescriptor):
protocol = protocol.klass
cls.__proto__[proto] = protocol # update mapping upon import
payload_io = io.BytesIO(payload)
try:
report = protocol(payload_io, len(payload), **kwargs) # type: ignore[abstract]
except Exception as exc:
if isinstance(exc, StructError) and exc.eof: # pylint: disable=no-member
from pcapkit.protocols.misc.null import NoPayload as protocol # pylint: disable=import-outside-toplevel # isort: skip
else:
from pcapkit.protocols.misc.raw import Raw as protocol # pylint: disable=import-outside-toplevel # isort: skip
# error = traceback.format_exc(limit=1).strip().rsplit(os.linesep, maxsplit=1)[-1]
# log error
#logger.error(str(exc), exc_info=exc, stack_info=DEVMODE, stacklevel=stacklevel())
report = protocol(payload_io, len(payload), **kwargs) # type: ignore[abstract]
return report
@classmethod
def register(cls, code: 'int', protocol: 'ModuleDescriptor | Type[ProtocolBase]') -> 'None':
r"""Register a new protocol class.
Notes:
The full qualified class name of the new protocol class
should be as ``{protocol.module}.{protocol.name}``.
Arguments:
code: protocol code
protocol: module descriptor or a
:class:`~pcapkit.protocols.protocol.Protocol` subclass
"""
if isinstance(protocol, ModuleDescriptor):
protocol = protocol.klass
if not issubclass(protocol, ProtocolBase):
raise RegistryError(f'protocol must be a Protocol subclass, not {protocol!r}')
if code in cls.__proto__:
warn(f'protocol {code} already registered, overwriting', RegistryWarning)
cls.__proto__[code] = protocol
@classmethod
def from_schema(cls, schema: '_ST | dict[str, Any]') -> 'Self':
"""Create protocol instance from schema.
Args:
schema: Protocol schema.
Returns:
Protocol instance.
"""
if not isinstance(schema, Schema):
schema = cast('_ST', cls.__schema__.from_dict(schema))
self = cls.__new__(cls)
self.__header__ = schema
# initialize protocol instance
self.__init__(bytes(schema), len(schema)) # type: ignore[misc]
return self
@classmethod
def from_data(cls, data: '_PT | dict[str, Any]') -> 'Self':
"""Create protocol instance from data.
Args:
data: Protocol data.
Returns:
Protocol instance.
"""
if not isinstance(data, Data):
data = cast('_PT', cls.__data__.from_dict(data))
self = cls.__new__(cls)
kwargs = self._make_data(data)
# initialize protocol instance
self.__init__(**kwargs) # type: ignore[misc]
return self
##########################################################################
# Data models.
##########################################################################
def __new__(cls, *args: 'Any', **kwargs: 'Any') -> 'Self': # pylint: disable=unused-argument
self = super().__new__(cls)
# NOTE: Assign this attribute after ``__new__`` to avoid shared memory
# reference between instances.
self.__cached__ = {}
self.__header__ = None # type: ignore[assignment]
return self
@overload
def __init__(self, file: 'IO[bytes] | bytes', length: 'Optional[int]' = ..., **kwargs: 'Any') -> 'None': ...
@overload
def __init__(self, **kwargs: 'Any') -> 'None': ...
def __init__(self, file: 'Optional[IO[bytes] | bytes]' = None, length: 'Optional[int]' = None, **kwargs: 'Any') -> 'None':
"""Initialisation.
Args:
file: Source packet stream.
length: Length of packet data.
_layer (str): Parse packet until ``_layer``
(:attr:`self._exlayer <pcapkit.protocols.protocol.Protocol._exlayer>`).
_protocol (Union[str, Protocol, Type[Protocol]]): Parse packet until ``_protocol``
(:attr:`self._exproto <pcapkit.protocols.protocol.Protocol._exproto>`).
**kwargs: Arbitrary keyword arguments.
"""
#logger.debug('%s(file, %s, **%s)', type(self).__name__, length, kwargs)
#: int: File pointer.
self._seekset = io.SEEK_SET # type: int
#: str: Parse packet until such layer.
self._exlayer = kwargs.pop('_layer', None) # type: Optional[str]
#: str: Parse packet until such protocol.
self._exproto = kwargs.pop('_protocol', None) # type: Optional[str | ProtocolBase | Type[ProtocolBase]]
#: bool: If terminate parsing next layer of protocol.
self._sigterm = self._check_term_threshold()
# post-init customisations
self.__post_init__(file, length, **kwargs) # type: ignore[arg-type]
# inject packet payload to the info dict
self._info.__update__(packet=self.packet.payload)
@overload
def __post_init__(self, file: 'IO[bytes] | bytes', length: 'Optional[int]' = ..., **kwargs: 'Any') -> 'None': ...
@overload
def __post_init__(self, **kwargs: 'Any') -> 'None': ...
def __post_init__(self, file: 'Optional[IO[bytes] | bytes]' = None,
length: 'Optional[int]' = None, **kwargs: 'Any') -> 'None':
"""Post initialisation hook.
Args:
file: Source packet stream.
length: Length of packet data.
**kwargs: Arbitrary keyword arguments.
See Also:
For construction arguments, please refer to
:meth:`self.make <pcapkit.protocols.protocol.Protocol.make>`.
"""
if file is None:
_data = self.pack(**kwargs)
else:
_data = file if isinstance(file, bytes) else file.read(length) # type: ignore[arg-type]
#: bytes: Raw packet data.
self._data = _data
#: io.BytesIO: Source packet stream.
self._file = io.BytesIO(self._data)
#: pcapkit.protocols.data.data.Data: Parsed packet data.
self._info = self.unpack(length, **kwargs)
def __init_subclass__(cls, /, schema: 'Optional[Type[_ST]]' = None,
data: 'Optional[Type[_PT]]' = None, *args: 'Any', **kwargs: 'Any') -> 'None':
"""Initialisation for subclasses.
Args:
schema: Schema class.
data: Data class.
*args: Arbitrary positional arguments.
**kwargs: Arbitrary keyword arguments.
This method is called when a subclass of :class:`Protocol` is defined.
It is used to set the :attr:`self.__schema__ <pcapkit.protocols.protocol.Protocol.__schema__>`
attribute of the subclass.
Notes:
When ``schema`` and/or ``data`` is not specified, the method will first
try to find the corresponding class in the
:mod:`~pcapkit.protocols.schema` and :mod:`~pcapkit.protocols.data`
modules respectively. If the class is not found, the default
:class:`~pcapkit.protocols.schema.schema.Schema_Raw` and
:class:`~pcapkit.protocols.data.data.Data_Raw` classes will be used.
"""
super().__init_subclass__()
if schema is None:
schema = cast('Type[_ST]', getattr(schema_module, cls.__name__, Schema_Raw))
if data is None:
data = cast('Type[_PT]', getattr(data_module, cls.__name__, Data_Raw))
cls.__schema__ = schema
cls.__data__ = data
def __repr__(self) -> 'str':
"""Returns representation of parsed protocol data.
Example:
>>> protocol
<Frame alias='...' frame=(..., packet=b'...', sethernet=..., protocols='Ethernet:IPv6:Raw')>
"""
if (cached := self.__cached__.get('__repr__')) is not None:
return cached
# cache and return
repr_ = f'<{self.alias} {self.info_name}={self._info!r}>'
self.__cached__['__repr__'] = repr_
return repr_
def __str__(self) -> 'str':
"""Returns formatted hex representation of source data stream.
Example:
>>> protocol
Frame(..., packet=b"...", sethernet=..., protocols='Ethernet:IPv6:Raw')
>>> print(protocol)
00 00 00 00 00 00 00 a6 87 f9 27 93 16 ee fe 80 00 00 00 ..........'........
00 00 00 1c cd 7c 77 ba c7 46 b7 87 00 0e aa 00 00 00 00 .....|w..F.........
fe 80 00 00 00 00 00 00 1c cd 7c 77 ba c7 46 b7 01 01 a4 ..........|w..F....
5e 60 d9 6b 97 ^`.k.
"""
if (cached := self.__cached__.get('__str__')) is not None:
return cached
hexbuf = ' '.join(textwrap.wrap(self._data.hex(), 2))
strbuf = ''.join(chr(char) if char in readable else '.' for char in self._data)
number = shutil.get_terminal_size().columns // 4 - 1
length = number * 3
hexlst = textwrap.wrap(hexbuf, length)
strlst = list(iter(functools.partial(io.StringIO(strbuf).read, number), ''))
# cache and return
str_ = os.linesep.join(map(lambda x: f'{x[0].ljust(length)} {x[1]}', zip(hexlst, strlst)))
self.__cached__['__str__'] = str_
return str_
def __bytes__(self) -> 'bytes':
"""Returns source data stream in :obj:`bytes`."""
return self._data
def __len__(self) -> 'int':
"""Total length of corresponding protocol."""
if (cached := self.__cached__.get('__len__')) is not None:
return cached
# cache and return
len_ = len(self._data)
self.__cached__['__len__'] = len_
return len_
def __length_hint__(self) -> 'Optional[int]':
"""Return an estimated length for the object."""
def __iter__(self) -> 'IO[bytes]':
"""Iterate through :attr:`self._data <pcapkit.protocols.protocol.Protocol._data>`."""
return io.BytesIO(self._data)
def __getitem__(self, key: 'str | Protocol | Type[Protocol]') -> 'ProtocolBase':
"""Subscription (``getitem``) support.
* If ``key`` is a :class:`~pcapkit.protocols.protocol.Protocol` object,
the method will fetch its indexes (:meth:`self.id <pcapkit.protocols.protocol.Protocol.id>`).
* Later, search the packet's chain of protocols with the calculated ``key``.
* If no matches, then raises :exc:`~pcapkit.utilities.exceptions.ProtocolNotFound`.
Args:
key: Indexing key.
Returns:
The sub-packet from the current packet of indexed protocol.
Raises:
ProtocolNotFound: If ``key`` is not in the current packet.
See Also:
The method calls
:meth:`self.expand_comp <pcapkit.protocols.protocol.Protocol.expand_comp>`
to handle the ``key`` and expand it for robust searching.
"""
comp = self.expand_comp(key)
# if it's itself
test_comp = (type(self), *(name.upper() for name in self.id()))
for test in comp:
if test in test_comp:
return self
# then check recursively
from pcapkit.protocols.misc.null import NoPayload # pylint: disable=import-outside-toplevel
payload = self._next
while not isinstance(payload, NoPayload):
test_comp = (type(payload), *(name.upper() for name in payload.id()))
for test in comp:
if test in test_comp:
return payload
payload = payload.payload
raise ProtocolNotFound(key)
def __contains__(self, name: 'str | Protocol | Type[Protocol]') -> 'bool':
"""Returns if certain protocol is in the instance.
Args:
name: Name to search
See Also:
The method calls
:meth:`self.expand_comp <pcapkit.protocols.protocol.Protocol.expand_comp>`
to handle the ``name`` and expand it for robust searching.
"""
comp = self.expand_comp(name)
# if it's itself
test_comp = (type(self), *(name.upper() for name in self.id()))
for test in comp:
if test in test_comp:
return True
# then check recursively
from pcapkit.protocols.misc.null import NoPayload # pylint: disable=import-outside-toplevel
payload = self._next
while not isinstance(payload, NoPayload):
test_comp = (type(payload), *(name.upper() for name in payload.id()))
for test in comp:
if test in test_comp:
return True
payload = payload.payload
return False
@classmethod
@abc.abstractmethod
def __index__(cls) -> 'StdlibEnum | AenumEnum':
"""Numeral registry index of the protocol."""
@classmethod
def __eq__(cls, other: 'object') -> 'bool':
"""Returns if ``other`` is of the same protocol as the current object.
Args:
other: Comparision against the object.
"""
if isinstance(other, type) and issubclass(other, ProtocolBase):
return cls is other
if isinstance(other, ProtocolBase):
return cls.id() == other.id()
if isinstance(other, str):
test_comp = cls.expand_comp(cls)
return other.upper() in test_comp
return False
def __hash__(self) -> 'int':
"""Return the hash value for :attr:`self._data <pcapkit.protocols.protocol.Protocol._data>`."""
return hash(self._data)
##########################################################################
# Utilities.
##########################################################################
def _get_payload(self) -> 'bytes':
"""Get payload from :attr:`self.__header__ <Protocol.__header__>`.
Returns:
Payload of :attr:`self.__header__ <Protocol.__header__>` as :obj:`bytes`.
See Also:
This is a wrapper function for :meth:`pcapkit.protocols.schema.schema.Schema.get_payload`.
"""
return self.__header__.get_payload()
def _read_protos(self, size: int) -> 'Optional[StdlibEnum | AenumEnum]': # pylint: disable=unused-argument
"""Read next layer protocol type.
* If *succeed*, returns the enum of next layer protocol.
* If *fail*, returns :obj:`None`.
Arguments:
size: buffer size
"""
def _read_fileng(self, *args: 'Any', **kwargs: 'Any') -> 'bytes':
"""Read file buffer (:attr:`self._file <pcapkit.protocols.protocol.Protocol._file>`).
This method wraps the :meth:`file.read <io.BytesIO.read>` call.
Args:
*args: arbitrary positional arguments
**kwargs: arbitrary keyword arguments
Returns:
bytes: Data read from file buffer.
"""
return self._file.read(*args, **kwargs)
def _read_unpack(self, size: 'int' = 1, *, signed: 'bool' = False,
lilendian: 'bool' = False, quiet: 'bool' = False) -> 'int':
"""Read bytes and unpack for integers.
Arguments:
size: buffer size
signed: signed flag
lilendian: little-endian flag
quiet: quiet (no exception) flag
Returns:
Unpacked data upon success
Raises:
StructError: If unpack (:func:`struct.pack`) failed, and :exc:`struct.error` raised.
"""
endian = '<' if lilendian else '>'
if size == 8: # unpack to 8-byte integer (long long)
kind = 'q' if signed else 'Q'
elif size == 4: # unpack to 4-byte integer (int / long)
kind = 'i' if signed else 'I'
elif size == 2: # unpack to 2-byte integer (short)
kind = 'h' if signed else 'H'
elif size == 1: # unpack to 1-byte integer (char)
kind = 'b' if signed else 'B'
else: # do not unpack
kind = None
mem = self._file.read(size)
if not mem:
raise StructError('unpack: empty buffer', quiet=True, eof=True)
if kind is None:
end = 'little' if lilendian else 'big' # type: Literal['little', 'big']
buf = int.from_bytes(mem, end, signed=signed)
else:
fmt = f'{endian}{kind}'
try:
buf = struct.unpack(fmt, mem)[0] # pylint: disable=no-member
except struct.error as error: # pylint: disable=no-member
if quiet:
end = 'little' if lilendian else 'big'
buf = int.from_bytes(mem, end, signed=signed)
return buf
raise StructError(f'{self.__class__.__name__}: unpack failed') from error
return buf
def _read_binary(self, size: 'int' = 1) -> 'str':
"""Read bytes and convert into binaries.
Arguments:
size: buffer size
Returns:
Binary bits (``0``/``1``).
"""
bin_ = [] # type: list[str]
for _ in range(size):
byte = self._file.read(1)
bin_.append(bin(ord(byte))[2:].zfill(8))
return ''.join(bin_)
@overload
def _read_packet(self, length: 'Optional[int]' = ..., *, header: 'None' = ...) -> 'bytes': ...
@overload
def _read_packet(self, *, header: 'int', payload: 'Optional[int]' = ..., discard: 'Literal[True]') -> 'bytes': ...
@overload
def _read_packet(self, *, header: 'int', payload: 'Optional[int]' = ..., discard: 'Literal[False]' = ...) -> 'Data_Packet': ... # pylint: disable=line-too-long
@seekset # type: ignore[misc]
def _read_packet(self, length: 'Optional[int]' = None, *, header: 'Optional[int]' = None,
payload: 'Optional[int]' = None, discard: bool = False) -> 'bytes | Data_Packet':
"""Read raw packet data.
Arguments:
length: length of the packet
header: length of the packet header
payload: length of the packet payload
discard: flag if discard header data
* If ``header`` omits, returns the whole packet data in :obj:`bytes`.
* If ``discard`` is set as :data:`True`, returns the packet body (in
:obj:`bytes`) only.
* Otherwise, returns the header and payload data as
:class:`~pcapkit.protocols.data.protocol.Packet` object.
"""
if header is not None:
data_header = self._read_fileng(header)
data_payload = self._read_fileng(payload)
if discard:
return data_payload
return Data_Packet(
header=data_header,
payload=data_payload
)
return self._read_fileng(length)
@classmethod
def _make_pack(cls, integer: 'int', *, size: 'int' = 1,
signed: 'bool' = False, lilendian: 'bool' = False) -> 'bytes':
"""Pack integers to bytes.
Arguments:
integer: integer to be packed
size: buffer size
signed: signed flag
lilendian: little-endian flag
Returns:
Packed data upon success.
Raises:
StructError: If failed to pack the integer.
"""
endian = '<' if lilendian else '>'
if size == 8: # unpack to 8-byte integer (long long)
kind = 'q' if signed else 'Q'
elif size == 4: # unpack to 4-byte integer (int / long)
kind = 'i' if signed else 'I'
elif size == 2: # unpack to 2-byte integer (short)
kind = 'h' if signed else 'H'
elif size == 1: # unpack to 1-byte integer (char)
kind = 'b' if signed else 'B'
else: # do not unpack
kind = None
if kind is None:
end = 'little' if lilendian else 'big' # type: Literal['little', 'big']
buf = integer.to_bytes(size, end, signed=signed)
else:
try:
fmt = f'{endian}{kind}'
buf = struct.pack(fmt, integer) # pylint: disable=no-member
except struct.error as error: # pylint: disable=no-member
raise StructError(f'{cls.__name__}: pack failed') from error
return buf
@overload
@classmethod
def _make_index(cls, name: 'int | StdlibEnum | AenumEnum', *, pack: 'Literal[False]' = ...) -> 'int': ...
@overload
@classmethod
def _make_index(cls, name: 'int | StdlibEnum | AenumEnum', *, pack: 'Literal[True]',
size: 'int' = ..., signed: 'bool' = ..., lilendian: 'bool' = ...) -> 'bytes': ...
@overload
@classmethod
def _make_index(cls, name: 'str', default: 'Optional[int]' = ..., *,
namespace: 'Type[StdlibEnum] | Type[AenumEnum]', pack: 'Literal[False]' = ...) -> 'int': ...
@overload
@classmethod
def _make_index(cls, name: 'str', default: 'Optional[int]' = ..., *,
namespace: 'Type[StdlibEnum] | Type[AenumEnum]', pack: 'Literal[True]',
size: 'int' = ..., signed: 'bool' = ..., lilendian: 'bool' = ...) -> 'bytes': ...
@overload
@classmethod
def _make_index(cls, name: 'str', default: 'Optional[int]' = ..., *, namespace: 'dict[int, str]',
reversed: 'Literal[False]' = ..., # pylint: disable=redefined-builtin
pack: 'Literal[False]' = ...) -> 'int': ...
@overload
@classmethod
def _make_index(cls, name: 'str', default: 'Optional[int]' = ..., *, namespace: 'dict[int, str]',
reversed: 'Literal[False]' = ..., # pylint: disable=redefined-builtin
pack: 'Literal[True]', size: 'int' = ..., signed: 'bool' = ...,
lilendian: 'bool' = ...) -> 'bytes': ...
@overload
@classmethod
def _make_index(cls, name: 'str', default: 'Optional[int]' = ..., *, namespace: 'dict[str, int]',
reversed: 'Literal[True]', # pylint: disable=redefined-builtin
pack: 'Literal[False]' = ...) -> 'int': ...
@overload
@classmethod
def _make_index(cls, name: 'str', default: 'Optional[int]' = ..., *, namespace: 'dict[str, int]',
reversed: 'Literal[True]', # pylint: disable=redefined-builtin
pack: 'Literal[True]', size: 'int' = ..., signed: 'bool' = ...,
lilendian: 'bool' = ...) -> 'bytes': ...
@overload
@classmethod
def _make_index(cls, name: 'str | int | StdlibEnum | AenumEnum', default: 'Optional[int]' = ..., *,
namespace: 'Optional[dict[str, int] | dict[int, str] | Type[StdlibEnum] | Type[AenumEnum]]' = ...,
reversed: 'bool' = ..., pack: 'Literal[False]' = ...) -> 'int': ...
@classmethod
def _make_index(cls, name: 'str | int | StdlibEnum | AenumEnum', default: 'Optional[int]' = None, *,
namespace: 'Optional[dict[str, int] | dict[int, str] | Type[StdlibEnum] | Type[AenumEnum]]' = None,
reversed: 'bool' = False, # pylint: disable=redefined-builtin
pack: 'bool' = False, size: 'int' = 4, signed: 'bool' = False,
lilendian: 'bool' = False) -> 'int | bytes':
"""Return first index of ``name`` from a :obj:`dict` or enumeration.
Arguments:
name: item to be indexed
default: default value
namespace: namespace for item