IPv4 - Internet Protocol version 4

pcapkit.protocols.internet.ipv4 contains IPv4 only, which implements extractor for Internet Protocol version 4 (IPv4) *, whose structure is described as below:

Octets

Bits

Name

Description

0

0

ip.version

Version (4)

0

4

ip.hdr_len

Internal Header Length (IHL)

1

8

ip.dsfield.dscp

Differentiated Services Code Point (DSCP)

1

14

ip.dsfield.ecn

Explicit Congestion Notification (ECN)

2

16

ip.len

Total Length

4

32

ip.id

Identification

6

48

Reserved Bit (must be \x00)

6

49

ip.flags.df

Don’t Fragment (DF)

6

50

ip.flags.mf

More Fragments (MF)

6

51

ip.frag_offset

Fragment Offset

8

64

ip.ttl

Time To Live (TTL)

9

72

ip.proto

Protocol (Transport Layer)

10

80

ip.checksum

Header Checksum

12

96

ip.src

Source IP Address

16

128

ip.dst

Destination IP Address

20

160

ip.options

IP Options (if IHL > 5)


class pcapkit.protocols.internet.ipv4.IPv4(file=None, length=None, **kwargs)[source]

Bases: IP[IPv4]

This class implements Internet Protocol version 4.

This class currently supports parsing of the following IPv4 options, which are directly mapped to the pcapkit.const.ipv4.option_number.OptionNumber enumeration:

Option Code

Option Parser

EOOL

_read_opt_eool()

NOP

_read_opt_nop()

SEC

_read_opt_sec()

LSR

_read_opt_lsr()

TS

_read_opt_ts()

ESEC

_read_opt_esec()

RR

_read_opt_rr()

SID

_read_opt_sid()

SSR

_read_opt_ssr()

MTUP

_read_opt_mtup()

MTUR

_read_opt_mtur()

TR

_read_opt_tr()

RTRALT

_read_opt_rtralt()

QS

_read_opt_qs()

Parameters
  • *args (Any) – Arbitrary positional arguments.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type

Protocol[PT]

classmethod __index__()[source]

Numeral registry index of the protocol.

Return type

TransType

Returns

Numeral registry index of the protocol in IANA.

property name: Literal['Internet Protocol version 4']

Name of corresponding protocol.

Return type

Literal[“Internet Protocol version 4”]

property length: int

Header length of corresponding protocol.

Return type

int

property protocol: TransType

Name of next layer protocol.

Return type

TransType

property src: IPv4Address

Source IP address.

Return type

IPv4Address

property dst: IPv4Address

Destination IP address.

Return type

IPv4Address

read(length=None, **kwargs)[source]

Read Internet Protocol version 4 (IPv4).

Structure of IPv4 header [RFC 791]:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version|  IHL  |Type of Service|          Total Length         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|         Identification        |Flags|      Fragment Offset    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Time to Live |    Protocol   |         Header Checksum       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Source Address                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Destination Address                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Parameters
  • length (Optional[int]) – Length of packet data.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type

IPv4

Returns

Parsed packet data.

make(**kwargs)[source]

Make (construct) packet data.

Parameters

**kwargs (Any) – Arbitrary keyword arguments.

Return type

NoReturn

Returns

Constructed packet data.

classmethod id()[source]

Index ID of the protocol.

Return type

tuple[Literal[“IPv4”]]

Returns

Index ID of the protocol.

_read_ipv4_addr()[source]

Read IP address.

Return type

IPv4Address

Returns

Parsed IP address.

_read_ipv4_opt_type(code)[source]

Read option type field.

Parameters

code (int) – option kind value

Return type

OptionType

Returns

Extracted IPv4 option type, as an object of the option flag (copied flag), option class, and option number.

_read_ipv4_options(length)[source]

Read IPv4 option list.

Parameters

length (int) – length of options

Return type

Option

Returns

Extracted IPv4 options.

Raises

ProtocolError – If the threshold is NOT matching.

_read_opt_unassigned(kind, *, options)[source]

Read IPv4 unassigned options.

Structure of IPv4 unassigned options [RFC 791]:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|     type      |    length     |         option data ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_UnassignedOption

Returns

Parsed option data.

Raises

ProtocolError – If size is LESS THAN 3.

_read_opt_eool(kind, *, options)[source]

Read IPv4 End of Option List (EOOL) option.

Structure of IPv4 End of Option List (EOOL) option [RFC 719]:

+--------+
|00000000|
+--------+
  Type=0
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_EOOLOption

Returns

Parsed option data.

_read_opt_nop(kind, *, options)[source]

Read IPv4 No Operation (NOP) option.

Structure of IPv4 No Operation (NOP) option [RFC 719]:

+--------+
|00000001|
+--------+
  Type=1
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_NOPOption

Returns

Parsed option data.

_read_opt_sec(kind, *, options)[source]

Read IPv4 Security (SEC) option.

Structure of IPv4 Security (SEC) option [RFC 1108]:

+------------+------------+------------+-------------//----------+
|  10000010  |  XXXXXXXX  |  SSSSSSSS  |  AAAAAAA[1]    AAAAAAA0 |
|            |            |            |         [0]             |
+------------+------------+------------+-------------//----------+
  TYPE = 130     LENGTH   CLASSIFICATION         PROTECTION
                               LEVEL              AUTHORITY
                                                    FLAGS
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_SECOption

Returns

Parsed option data.

Raises

ProtocolError – If size is LESS THAN 3.

_read_opt_lsr(kind, *, options)[source]

Read IPv4 Loose Source Route (LSR) option.

Structure of IPv4 Loose Source Route (LSR) option [RFC 791]:

+--------+--------+--------+---------//--------+
|10000011| length | pointer|     route data    |
+--------+--------+--------+---------//--------+
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_LSROption

Returns

Parsed option data.

Raises

ProtocolError – If option is malformed.

_read_opt_ts(kind, *, options)[source]

Read IPv4 Time Stamp (TS) option.

Structure of IPv4 Time Stamp (TS) option [RFC 791]:

+--------+--------+--------+--------+
|01000100| length | pointer|oflw|flg|
+--------+--------+--------+--------+
|         internet address          |
+--------+--------+--------+--------+
|             timestamp             |
+--------+--------+--------+--------+
|                 .                 |
                  .
                  .
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_TSOption

Returns

Parsed option data.

Raises

ProtocolError – If the option is malformed.

_read_opt_esec(kind, *, options)[source]

Read IPv4 Extended Security (ESEC) option.

Structure of IPv4 Extended Security (ESEC) option [RFC 1108]:

+------------+------------+------------+-------//-------+
|  10000101  |  000LLLLL  |  AAAAAAAA  |  add sec info  |
+------------+------------+------------+-------//-------+
 TYPE = 133      LENGTH     ADDITIONAL      ADDITIONAL
                           SECURITY INFO     SECURITY
                            FORMAT CODE        INFO
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_ESECOption

Returns

Parsed option data.

Raises

ProtocolError – If size is LESS THAN 3.

_read_opt_rr(kind, *, options)[source]

Read IPv4 Record Route (RR) option.

Structure of IPv4 Record Route (RR) option [RFC 791]:

+--------+--------+--------+---------//--------+
|00000111| length | pointer|     route data    |
+--------+--------+--------+---------//--------+
  Type=7
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_RROption

Returns

Parsed option data.

Raises

ProtocolError – If option is malformed.

_read_opt_sid(kind, *, options)[source]

Read IPv4 Stream ID (SID) option.

Structure of IPv4 Stream ID (SID) option [RFC 791][RFC 6814]:

+--------+--------+--------+--------+
|10001000|00000010|    Stream ID    |
+--------+--------+--------+--------+
 Type=136 Length=4
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_SIDOption

Returns

Parsed option data.

Raises

ProtocolError – If size is NOT 4.

_read_opt_ssr(kind, *, options)[source]

Read IPv4 Strict Source Route (SSR) option.

Structure of IPv4 Strict Source Route (SSR) option [RFC 791]:

+--------+--------+--------+---------//--------+
|10001001| length | pointer|     route data    |
+--------+--------+--------+---------//--------+
 Type=137
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_SSROption

Returns

Parsed option data.

Raises

ProtocolError – If option is malformed.

_read_opt_mtup(kind, *, options)[source]

Read IPv4 MTU Probe (MTUP) option.

Structure of IPv4 MTU Probe (MTUP) option [RFC 1063][RFC 1191]:

+--------+--------+--------+--------+
|00001011|00000100|   2 octet value |
+--------+--------+--------+--------+
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_MTUPOption

Returns

Parsed option data.

Raises

ProtocolError – If size is NOT 4.

_read_opt_mtur(kind, *, options)[source]

Read IPv4 MTU Reply (MTUR) option.

Structure of IPv4 MTU Reply (MTUR) option [RFC 1063][RFC 1191]:

+--------+--------+--------+--------+
|00001100|00000100|   2 octet value |
+--------+--------+--------+--------+
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_MTUROption

Returns

Parsed option data.

Raises

ProtocolError – If size is NOT 4.

_read_opt_tr(kind, *, options)[source]

Read IPv4 Traceroute (TR) option.

Structure of IPv4 Traceroute (TR) option [RFC 1393][RFC 6814]:

 0               8              16              24
+-+-+-+-+-+-+-+-+---------------+---------------+---------------+
|F| C |  Number |    Length     |          ID Number            |
+-+-+-+-+-+-+-+-+---------------+---------------+---------------+
|      Outbound Hop Count       |       Return Hop Count        |
+---------------+---------------+---------------+---------------+
|                     Originator IP Address                     |
+---------------+---------------+---------------+---------------+
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_TROption

Returns

Parsed option data.

Raises

ProtocolError – If size is NOT 12.

_read_opt_rtralt(kind, *, options)[source]

Read IPv4 Router Alert (RTRALT) option.

Structure of IPv4 Router Alert (RTRALT) option [RFC 2113]:

+--------+--------+--------+--------+
|10010100|00000100|  2 octet value  |
+--------+--------+--------+--------+
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_RTRALTOption

Returns

Parsed option data.

Raises

ProtocolError – If size is NOT 4.

_read_opt_qs(kind, *, options)[source]

Read IPv4 Quick Start (QS) option.

Structure of IPv4 Quick Start (QS) option [RFC 4782]:

  • A Quick-Start Request

     0                   1                   2                   3
     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |   Option      |  Length=8     | Func. | Rate  |   QS TTL      |
    |               |               | 0000  |Request|               |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                        QS Nonce                           | R |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    
  • Report of Approved Rate

     0                   1                   2                   3
     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |   Option      |  Length=8     | Func. | Rate  |   Not Used    |
    |               |               | 1000  | Report|               |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                        QS Nonce                           | R |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    
Parameters
  • kind (RegType_OptionNumber) – option type code

  • options (Option) – extracted IPv4 options

Return type

DataType_QSOption

Returns

Parsed option data.

Raises

ProtocolError – If the option is malformed.

Data Structures

class pcapkit.protocols.data.internet.ipv4.IPv4(version, hdr_len, tos, len, id, flags, offset, ttl, protocol, checksum, src, dst)[source]

Bases: Info

Data model for IPv4 packet.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

version: Literal[4]

Version.

hdr_len: int

Internet header length.

tos: ToSField

Type of services.

len: int

Total length.

id: int

Identification.

flags: Flags

Flags.

offset: int

Fragment offset.

ttl: timedelta

Time to live.

protocol: TransType

Protocol.

checksum: bytes

Header checksum.

src: IPv4Address

Source address.

dst: IPv4Address

Destination address.

options: OrderedMultiDict[OptionNumber, Option]
class pcapkit.protocols.data.internet.ipv4.ToSField(pre, del, thr, rel, ecn)[source]

Bases: Info

Data model for IPv4 ToS fields.

Important

Due to the preserved keyword conflict, please use from_dict() to create an instance of this data model.

Parameters
  • *args (Any) – Arbitrary positional arguments.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type

ToSField

pre: ToSPrecedence

Precedence.

del: ToSDelay

Delay.

Note

This field is conflict with del keyword. To access this field, directly use getattr() instead.

thr: ToSThroughput

Throughput.

rel: ToSReliability

Reliability.

ecn: ToSECN

Explicit congestion notification (ECN).

class pcapkit.protocols.data.internet.ipv4.Flags(df, mf)[source]

Bases: Info

Data model for IPv4 Flags.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

df: bool

Don’t fragment.

mf: bool

More fragments.

class pcapkit.protocols.data.internet.ipv4.Option(code, length, type)[source]

Bases: Info

Data model for IPv4 options.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

code: OptionNumber

Option code.

length: int

Option length.

type: OptionType

Option type.

class pcapkit.protocols.data.internet.ipv4.OptionType(change, class, number)[source]

Bases: Info

Data model for IPv4 option type data.

Parameters
  • *args (Any) – Arbitrary positional arguments.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type

OptionType

change: bool

Change flag.

class: OptionClass

Option class.

Note

This field is conflict with class keyword. To access this field, directly use getattr() instead.

number: int

Number.

class pcapkit.protocols.data.internet.ipv4.UnassignedOption(code, length, type, data)[source]

Bases: Option

Data model for IPv4 unassigned option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

data: bytes

Option data.

class pcapkit.protocols.data.internet.ipv4.EOOLOption(code, length, type)[source]

Bases: Option

Data model for IPv4 End of Option List (EOOL) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

class pcapkit.protocols.data.internet.ipv4.NOPOption(code, length, type)[source]

Bases: Option

Data model for IPv4 No Operation (NOP) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

class pcapkit.protocols.data.internet.ipv4.SECOption(code, length, type, level, flags)[source]

Bases: Option

Data model for IPv4 Security (SEC) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

level: ClassificationLevel

Classification level.

flags: Optional[OrderedMultiDict[ProtectionAuthority, bool]]

Protection authority flags.

class pcapkit.protocols.data.internet.ipv4.LSROption(code, length, type, pointer, route)[source]

Bases: Option

Data model for IPv4 Loose Source Route (LSR) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

pointer: int

Pointer.

route: Optional[tuple[IPv4Address, ...]]

Route.

class pcapkit.protocols.data.internet.ipv4.TSOption(code, length, type, pointer, overflow, flag, timestamp)[source]

Bases: Option

Data model for IPv4 Time Stamp (TS) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

pointer: int

Pointer.

overflow: int

Overflow.

flag: TSFlag

Flag.

timestamp: Optional[bytes | tuple[dt_type, ...] | OrderedMultiDict[IPv4Address, dt_type]]

Timestamp data.

class pcapkit.protocols.data.internet.ipv4.ESECOption(code, length, type, level, flags)[source]

Bases: Option

Data model for IPv4 Extended Security (ESEC) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

level: ClassificationLevel

Classification level.

flags: Optional[OrderedMultiDict[ProtectionAuthority, bool]]

Protection authority flags.

class pcapkit.protocols.data.internet.ipv4.RROption(code, length, type, pointer, route)[source]

Bases: Option

Data model for IPv4 Record Route (RR) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

pointer: int

Pointer.

route: Optional[tuple[IPv4Address, ...]]

Route.

class pcapkit.protocols.data.internet.ipv4.SIDOption(code, length, type, sid)[source]

Bases: Option

Data model for IPv4 Stream ID (SID) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

sid: int

Stream ID.

class pcapkit.protocols.data.internet.ipv4.SSROption(code, length, type, pointer, route)[source]

Bases: Option

Data model for IPv4 Strict Source Route (SSR) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

pointer: int

Pointer.

route: Optional[tuple[IPv4Address, ...]]

Route.

class pcapkit.protocols.data.internet.ipv4.MTUPOption(code, length, type, mtu)[source]

Bases: Option

Data model for IPv4 MTU Probe (MTUP) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

mtu: int

MTU.

class pcapkit.protocols.data.internet.ipv4.MTUROption(code, length, type, mtu)[source]

Bases: Option

Data model for IPv4 MTU Reply (MTUR) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

mtu: int

MTU.

class pcapkit.protocols.data.internet.ipv4.TROption(code, length, type, id, outbound, return, originator)[source]

Bases: Option

Data model for IPv4 Traceroute (TR) option.

Important

Due to the preserved keyword conflict, please use from_dict() to create an instance of this data model.

Parameters
  • *args (Any) – Arbitrary positional arguments.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type

TROption

id: int

ID number.

outbound: int

Outbound hop count.

return: int

Return hop count.

Note

This field is conflict with return keyword. To access this field, directly use getattr() instead.

originator: IPv4Address
class pcapkit.protocols.data.internet.ipv4.RTRALTOption(code, length, type, alert)[source]

Bases: Option

Data model for IPv4 Router Alert (RTRALT) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

alert: RouterAlert

Router alert.

class pcapkit.protocols.data.internet.ipv4.QSOption(code, length, type, func, rate, ttl, nounce)[source]

Bases: Option

Data model for IPv4 Quick Start (QS) option.

Parameters
  • *args (VT) – Arbitrary positional arguments.

  • **kwargs (VT) – Arbitrary keyword arguments.

Return type

Info

func: QSFunction

QS function.

rate: int

Rate request/report.

ttl: Optional[timedelta]

TTL.

nounce: int

Nounce.


*

https://en.wikipedia.org/wiki/IPv4