-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted IPv4 protocol files to the nes naming schema.
- Loading branch information
Showing
26 changed files
with
706 additions
and
606 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
#!/usr/bin/env python3 | ||
|
||
############################################################################ | ||
# # | ||
# PyTCP - Python TCP/IP stack # | ||
# Copyright (C) 2020-present Sebastian Majewski # | ||
# # | ||
# This program is free software: you can redistribute it and/or modify # | ||
# it under the terms of the GNU General Public License as published by # | ||
# the Free Software Foundation, either version 3 of the License, or # | ||
# (at your option) any later version. # | ||
# # | ||
# This program is distributed in the hope that it will be useful, # | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||
# GNU General Public License for more details. # | ||
# # | ||
# You should have received a copy of the GNU General Public License # | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. # | ||
# # | ||
# Author's email: [email protected] # | ||
# Github repository: https://github.com/ccie18643/PyTCP # | ||
# # | ||
############################################################################ | ||
|
||
|
||
""" | ||
Module contains packet structure information for the IPv4 protccol. | ||
pytcp/protocols/ip4/base.py | ||
ver 2.9 | ||
""" | ||
|
||
|
||
from __future__ import annotations | ||
|
||
import struct | ||
from typing import TYPE_CHECKING, TypeAlias, override | ||
|
||
from pytcp.lib.proto import Proto | ||
from pytcp.protocols.ethernet.header import EthernetType | ||
from pytcp.protocols.ip4.header import Ip4HeaderProperties | ||
from pytcp.protocols.ip4.options import Ip4Option | ||
|
||
if TYPE_CHECKING: | ||
from pytcp.protocols.icmp4.fpa import Icmp4Assembler | ||
from pytcp.protocols.ip4.header import Ip4Header | ||
from pytcp.protocols.raw.fpa import RawAssembler | ||
from pytcp.protocols.tcp.fpa import TcpAssembler | ||
from pytcp.protocols.udp.fpa import UdpAssembler | ||
|
||
Ip4Payload: TypeAlias = ( | ||
Icmp4Assembler | TcpAssembler | UdpAssembler | RawAssembler | ||
) | ||
|
||
|
||
IP4_MIN_MTU = 576 # RFC 791 | ||
|
||
|
||
class Ip4(Proto, Ip4HeaderProperties): | ||
""" | ||
Base class for IPv4 packet parser and assembler. | ||
""" | ||
|
||
__ethernet_type = EthernetType.IP4 | ||
|
||
_header: Ip4Header | ||
_options: list[Ip4Option] | ||
|
||
_hlen: int | ||
_plen: int | ||
_olen: int | ||
_dlen: int | ||
|
||
@override | ||
def __str__(self) -> str: | ||
""" | ||
Get packet log string. | ||
""" | ||
|
||
log = ( | ||
f"IPv4 {self._header.src} > {self._header.dst}, " | ||
f"proto {self._header.proto}, id {self._header.id}" | ||
f"{', DF' if self._header.flag_df else ''}" | ||
f"{', MF' if self._header.flag_mf else ''}, " | ||
f"offset {self._header.offset}, plen {self._header.plen}, " | ||
f"ttl {self._header.ttl}" | ||
) | ||
|
||
for option in self._options: | ||
log += ", " + str(option) | ||
|
||
return log | ||
|
||
@override | ||
def __repr__(self) -> str: | ||
""" | ||
Get the packet representation string. | ||
""" | ||
|
||
return f"{self.__class__.__name__}({self._header!r}, {self._options!r})" | ||
|
||
@override | ||
def __bytes__(self) -> bytes: | ||
""" | ||
Get the packet in raw form. | ||
""" | ||
|
||
return bytes(self._header) + b"".join( | ||
bytes(option) for option in self._options | ||
) | ||
|
||
@property | ||
def ethernet_type(self) -> EthernetType: | ||
""" | ||
Get the '_ethernet_type' attribute. | ||
""" | ||
|
||
return self.__ethernet_type | ||
|
||
@property | ||
def options(self) -> list[Ip4Option]: | ||
""" | ||
Getter for '_options' attribute. | ||
""" | ||
|
||
return self._options | ||
|
||
@property | ||
def plen(self) -> int: | ||
""" | ||
Getter for '_plen' attribute. | ||
""" | ||
|
||
return self._plen | ||
|
||
@property | ||
def hlen(self) -> int: | ||
""" | ||
Getter for '_hlen' attribute. | ||
""" | ||
|
||
return self._hlen | ||
|
||
@property | ||
def olen(self) -> int: | ||
""" | ||
Getter for '_olen' attribute. | ||
""" | ||
|
||
return self._olen | ||
|
||
@property | ||
def dlen(self) -> int: | ||
""" | ||
Getter for '_dlen' attribute. | ||
""" | ||
|
||
return self._dlen | ||
|
||
@property | ||
def pshdr_sum(self) -> int: | ||
""" | ||
Create IPv4 pseudo header used by TCP and UDP to compute | ||
their checksums. | ||
""" | ||
|
||
pseudo_header = struct.pack( | ||
"! 4s 4s BBH", | ||
bytes(self._header.src), | ||
bytes(self._header.dst), | ||
0, | ||
int(self._header.proto), | ||
self._plen - self._hlen, | ||
) | ||
|
||
return sum(struct.unpack("! 3L", pseudo_header)) |
Oops, something went wrong.