Skip to content

Commit 200a7c6

Browse files
authored
Code update py311 (#26)
* Litle refactor aftr last pull request. * Updated code to Python 3.11 --------- Co-authored-by: ccie18643 <[email protected]>
1 parent d21bac7 commit 200a7c6

16 files changed

+51
-44
lines changed

examples/README.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ Before running any of the examples, please make sure to:
2222
- Run the 'sudo make tap' command to create the tap7 interface and assign it to the 'br0' bridge.
2323
- Run the 'make' command to create the proper virtual environment.
2424
- Run '. venv/bin/activate' command to start the stack virtual environment.
25-
- Execute any example, e.g., 'example/run_stack.py'.
26-
- Hit Ctrl-C to stop it.
25+
- Execute any example, e.g., 'examples/run_stack.py'.
26+
- Hit Ctrl-C to stop it.

examples/icmp_echo_client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
import click
4545

46-
from pytcp import TcpIpStack
46+
from pytcp import TcpIpStack, initialize_tap
4747
from pytcp.lib import stack
4848
from pytcp.lib.ip4_address import Ip4Address
4949
from pytcp.lib.ip6_address import Ip6Address
@@ -145,7 +145,7 @@ def cli(*, interface: str, remote_ip_address: str) -> None:
145145
Run the ICMP Echo client.
146146
"""
147147

148-
stack = TcpIpStack(interface=interface)
148+
stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
149149
client = IcmpEchoClient(
150150
remote_ip_address=remote_ip_address,
151151
)

examples/run_stack.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
import click
3939

40-
from pytcp import TcpIpStack
40+
from pytcp import TcpIpStack, initialize_tap
4141

4242

4343
@click.command()
@@ -61,7 +61,7 @@ def cli(
6161
"""
6262

6363
stack = TcpIpStack(
64-
interface=interface,
64+
fd=initialize_tap(tap_name=interface),
6565
mac_address=mac_address,
6666
ip6_address=ip6_address,
6767
ip6_gateway=ip6_gateway,

examples/tcp_daytime_service.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import click
4343

4444
from examples.lib.tcp_service import TcpService
45-
from pytcp import TcpIpStack
45+
from pytcp import TcpIpStack, initialize_tap
4646

4747
if TYPE_CHECKING:
4848
from pytcp.lib.socket import Socket
@@ -119,7 +119,7 @@ def cli(*, interface: str) -> None:
119119
Run the TCP Daytime service.
120120
"""
121121

122-
stack = TcpIpStack(interface=interface)
122+
stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
123123
service = TcpDaytimeService()
124124

125125
try:

examples/tcp_discard_service.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import click
4242

4343
from examples.lib.tcp_service import TcpService
44-
from pytcp import TcpIpStack
44+
from pytcp import TcpIpStack, initialize_tap
4545

4646
if TYPE_CHECKING:
4747
from pytcp.lib.socket import Socket
@@ -126,7 +126,7 @@ def cli(*, interface: str) -> None:
126126
Run the TCP Discard service.
127127
"""
128128

129-
stack = TcpIpStack(interface=interface)
129+
stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
130130
service = TcpDiscardService()
131131

132132
try:

examples/tcp_echo_client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
import click
4545

46-
from pytcp import TcpIpStack
46+
from pytcp import TcpIpStack, initialize_tap
4747
from pytcp.lib import socket
4848
from pytcp.lib.ip_helper import ip_version
4949

@@ -181,7 +181,7 @@ def cli(*, interface: str, remote_ip_address: str) -> None:
181181
Run the TCP Echo client.
182182
"""
183183

184-
stack = TcpIpStack(interface=interface)
184+
stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
185185
client = TcpEchoClient(
186186
remote_ip_address=remote_ip_address,
187187
)

examples/tcp_echo_service.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
from examples.lib.malpi import malpa, malpi, malpka
4444
from examples.lib.tcp_service import TcpService
45-
from pytcp import TcpIpStack
45+
from pytcp import TcpIpStack, initialize_tap
4646

4747
if TYPE_CHECKING:
4848
from pytcp.lib.socket import Socket
@@ -137,7 +137,7 @@ def cli(*, interface: str) -> None:
137137
Run the TCP Echo service.
138138
"""
139139

140-
stack = TcpIpStack(interface=interface)
140+
stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
141141
service = TcpEchoService()
142142

143143
try:

examples/udp_daytime_service.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import click
4343

4444
from examples.lib.udp_service import UdpService
45-
from pytcp import TcpIpStack
45+
from pytcp import TcpIpStack, initialize_tap
4646

4747
if TYPE_CHECKING:
4848
from pytcp.lib.socket import Socket
@@ -89,7 +89,7 @@ def cli(*, interface: str) -> None:
8989
Run the UDP Daytime service.
9090
"""
9191

92-
stack = TcpIpStack(interface=interface)
92+
stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
9393
service = UdpDaytimeService()
9494

9595
try:

examples/udp_discard_service.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import click
4242

4343
from examples.lib.udp_service import UdpService
44-
from pytcp import TcpIpStack
44+
from pytcp import TcpIpStack, initialize_tap
4545

4646
if TYPE_CHECKING:
4747
from pytcp.lib.socket import Socket
@@ -86,7 +86,7 @@ def cli(*, interface: str) -> None:
8686
Run the UDP Discard service.
8787
"""
8888

89-
stack = TcpIpStack(interface=interface)
89+
stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
9090
service = UdpDiscardService()
9191

9292
try:

examples/udp_echo_client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
import click
4545

46-
from pytcp import TcpIpStack
46+
from pytcp import TcpIpStack, initialize_tap
4747
from pytcp.lib import socket
4848
from pytcp.lib.ip_helper import ip_version
4949

@@ -176,7 +176,7 @@ def cli(*, interface: str, remote_ip_address: str) -> None:
176176
Run the TCP Echo client.
177177
"""
178178

179-
stack = TcpIpStack(interface=interface)
179+
stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
180180
client = UdpEchoClient(
181181
remote_ip_address=remote_ip_address,
182182
)

examples/udp_echo_service.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
from examples.lib.malpi import malpa, malpi, malpka
4444
from examples.lib.udp_service import UdpService
45-
from pytcp import TcpIpStack
45+
from pytcp import TcpIpStack, initialize_tap
4646

4747
if TYPE_CHECKING:
4848
from pytcp.lib.socket import Socket
@@ -102,7 +102,7 @@ def cli(*, interface: str) -> None:
102102
Run the UDP Echo service.
103103
"""
104104

105-
stack = TcpIpStack(interface=interface)
105+
stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
106106
service = UdpEchoService()
107107

108108
try:

pytcp/__init__.py

+22-18
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import os
3838
import struct
3939
import sys
40-
from typing import Tuple
4140

4241
from pytcp import config
4342
from pytcp.lib import stack
@@ -51,35 +50,41 @@
5150
IFF_NO_PI = 0x1000
5251

5352

53+
def initialize_tap(*, tap_name: str) -> tuple[int, int]:
54+
"""
55+
Initialize the TAP interface.
56+
"""
57+
58+
try:
59+
fd = os.open("/dev/net/tun", os.O_RDWR)
60+
61+
except FileNotFoundError:
62+
log("stack", "<CRIT>Unable to access '/dev/net/tun' device</>")
63+
sys.exit(-1)
64+
65+
fcntl.ioctl(
66+
fd,
67+
TUNSETIFF,
68+
struct.pack("16sH", tap_name.encode(), IFF_TAP | IFF_NO_PI),
69+
)
70+
71+
return fd, fd
72+
73+
5474
class TcpIpStack:
5575
"""
5676
Main PyTCP library class.
5777
"""
58-
@staticmethod
59-
def create_tun(interface:str):
60-
# Initialize the TAP interface.
61-
try:
62-
fd = os.open("/dev/net/tun", os.O_RDWR)
63-
except FileNotFoundError:
64-
log("stack", "<CRIT>Unable to access '/dev/net/tun' device</>")
65-
sys.exit(-1)
66-
fcntl.ioctl(
67-
fd,
68-
TUNSETIFF,
69-
struct.pack("16sH", interface.encode(), IFF_TAP | IFF_NO_PI),
70-
)
71-
return fd, fd
7278

7379
def __init__(
7480
self,
7581
*,
76-
fd:Tuple[int,int],
82+
fd: tuple[int, int],
7783
mac_address: str | None = None,
7884
ip4_address: str | None = None,
7985
ip4_gateway: str | None = None,
8086
ip6_address: str | None = None,
8187
ip6_gateway: str | None = None,
82-
8388
):
8489
"""
8590
Initialize stack on given interface.
@@ -119,7 +124,6 @@ def __init__(
119124
self.rx_fd = fd[0]
120125
self.tx_fd = fd[1]
121126

122-
123127
def start(self) -> None:
124128
"""
125129
Start stack components.

pytcp/protocols/tcp/session.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646

4747
import random
4848
import threading
49+
from collections.abc import Callable
4950
from enum import Enum, auto
50-
from typing import TYPE_CHECKING, Any, Callable
51+
from typing import TYPE_CHECKING, Any
5152

5253
from pytcp import config
5354
from pytcp.lib import stack

pytcp/subsystems/rx_ring.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import select
4242
import threading
4343
import time
44-
from typing import TYPE_CHECKING, Optional
44+
from typing import TYPE_CHECKING
4545

4646
from pytcp.lib.logger import log
4747
from pytcp.lib.packet import PacketRx
@@ -107,7 +107,7 @@ def __thread_receive(self) -> None:
107107

108108
__debug__ and log("stack", "Stopped RX ring")
109109

110-
def dequeue(self) -> Optional[PacketRx]:
110+
def dequeue(self) -> PacketRx | None:
111111
"""
112112
Dequeue inboutd frame from RX ring.
113113
"""

pytcp/subsystems/timer.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040

4141
import threading
4242
import time
43-
from typing import Any, Callable
43+
from collections.abc import Callable
44+
from typing import Any
4445

4546
from pytcp.lib.logger import log
4647

requirements_dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pyre-check
1010
build
1111
twine
1212
ipython
13+
pyupgrade

0 commit comments

Comments
 (0)