|
37 | 37 | import os
|
38 | 38 | import struct
|
39 | 39 | import sys
|
40 |
| -from typing import Tuple |
41 | 40 |
|
42 | 41 | from pytcp import config
|
43 | 42 | from pytcp.lib import stack
|
|
51 | 50 | IFF_NO_PI = 0x1000
|
52 | 51 |
|
53 | 52 |
|
| 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 | + |
54 | 74 | class TcpIpStack:
|
55 | 75 | """
|
56 | 76 | Main PyTCP library class.
|
57 | 77 | """
|
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 |
72 | 78 |
|
73 | 79 | def __init__(
|
74 | 80 | self,
|
75 | 81 | *,
|
76 |
| - fd:Tuple[int,int], |
| 82 | + fd: tuple[int, int], |
77 | 83 | mac_address: str | None = None,
|
78 | 84 | ip4_address: str | None = None,
|
79 | 85 | ip4_gateway: str | None = None,
|
80 | 86 | ip6_address: str | None = None,
|
81 | 87 | ip6_gateway: str | None = None,
|
82 |
| - |
83 | 88 | ):
|
84 | 89 | """
|
85 | 90 | Initialize stack on given interface.
|
@@ -119,7 +124,6 @@ def __init__(
|
119 | 124 | self.rx_fd = fd[0]
|
120 | 125 | self.tx_fd = fd[1]
|
121 | 126 |
|
122 |
| - |
123 | 127 | def start(self) -> None:
|
124 | 128 | """
|
125 | 129 | Start stack components.
|
|
0 commit comments