Skip to content

Commit 4ee6b5a

Browse files
committed
Code cleanup.
1 parent 7b5de7d commit 4ee6b5a

File tree

2 files changed

+36
-313
lines changed

2 files changed

+36
-313
lines changed

examples/icmp_echo_client.py

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,6 @@ def __init__(
9595
self._message_size = message_size
9696
self._run_thread = False
9797

98-
@staticmethod
99-
def _checksum(data: bytes) -> int:
100-
"""
101-
Compute the Internet Checksum of the supplied data.
102-
"""
103-
104-
if len(data) % 2:
105-
data += b"\x00"
106-
res = sum(struct.unpack(f"!{len(data) // 2}H", data))
107-
res = (res >> 16) + (res & 0xFFFF)
108-
res += res >> 16
109-
return int(~res & 0xFFFF)
110-
11198
@classmethod
11299
def _create_icmp4_message(cls, identifier: int, sequence: int) -> bytes:
113100
"""
@@ -133,7 +120,6 @@ def _create_icmp6_message(cls, identifier: int, sequence: int) -> bytes:
133120
Create ICMPv6 Echo Request packet.
134121
"""
135122

136-
# Create the ICMPv6 header directly (without checksum)
137123
header = struct.pack(
138124
"!BBHHH",
139125
ICMP6_ECHO_REQUEST_TYPE,
@@ -152,6 +138,8 @@ def _thread__client__sender(self) -> None:
152138
Client thread used to send data.
153139
"""
154140

141+
identifier = os.getpid() & 0xFFFF
142+
155143
if self._client_socket:
156144
message_count = self._message_count
157145

@@ -162,12 +150,12 @@ def _thread__client__sender(self) -> None:
162150
):
163151
case 6, 6:
164152
icmp_message = self._create_icmp6_message(
165-
identifier=os.getpid() & 0xFFFF,
153+
identifier=identifier,
166154
sequence=self._message_count - message_count + 1,
167155
)
168156
case 4, 4:
169157
icmp_message = self._create_icmp4_message(
170-
identifier=os.getpid() & 0xFFFF,
158+
identifier=identifier,
171159
sequence=self._message_count - message_count + 1,
172160
)
173161
case _:
@@ -205,43 +193,46 @@ def _thread__client__receiver(self) -> None:
205193
f"Client ICMP Echo: Received {len(data) - 8} bytes from "
206194
f"'{self._remote_ip_address}'."
207195
)
208-
time.sleep(1)
209196

210197

211198
@click.command()
212199
@click.option(
213-
"--interface",
200+
"--stack-interface",
201+
"stack__interface",
214202
default="tap7",
215203
help="Name of the interface to be used by the stack.",
216204
)
217205
@click.option(
218-
"--mac-address",
206+
"--stack-mac-address",
207+
"stack__mac_address",
219208
type=ClickTypeMacAddress(),
220209
default=None,
221210
help="MAC address to be assigned to the interface.",
222211
)
223212
@click.option(
224-
"--ip6-address",
225-
"ip6_host",
213+
"--stack-ip6-address",
214+
"stack__ip6_host",
226215
type=ClickTypeIp6Host(),
227216
default=None,
228217
help="IPv6 address/mask to be assigned to the interface.",
229218
)
230219
@click.option(
231-
"--ip6-gateway",
220+
"--stack-ip6-gateway",
221+
"stack__ip6_gateway",
232222
type=ClickTypeIp6Address(),
233223
default=None,
234224
help="IPv6 gateway address to be assigned to the interface.",
235225
)
236226
@click.option(
237-
"--ip4-address",
238-
"ip4_host",
227+
"--stack-ip4-address",
228+
"stack__ip4_host",
239229
type=ClickTypeIp4Host(),
240230
default=None,
241231
help="IPv4 address/mask to be assigned to the interface.",
242232
)
243233
@click.option(
244-
"--ip4-gateway",
234+
"--stack-ip4-gateway",
235+
"stack__ip4_gateway",
245236
type=ClickTypeIp4Address(),
246237
default=None,
247238
help="IPv4 gateway address to be assigned to the interface.",
@@ -253,41 +244,45 @@ def _thread__client__receiver(self) -> None:
253244
)
254245
def cli(
255246
*,
256-
interface: str,
257-
mac_address: MacAddress | None,
258-
ip6_host: Ip6Host | None,
259-
ip6_gateway: Ip6Address | None,
260-
ip4_host: Ip4Host | None,
261-
ip4_gateway: Ip4Address | None,
247+
stack__interface: str,
248+
stack__mac_address: MacAddress | None,
249+
stack__ip6_host: Ip6Host | None,
250+
stack__ip6_gateway: Ip6Address | None,
251+
stack__ip4_host: Ip4Host | None,
252+
stack__ip4_gateway: Ip4Address | None,
262253
remote_ip_address: IpAddress,
263254
) -> None:
264255
"""
265256
Start PyTCP stack and stop it when user presses Ctrl-C.
266257
Start ICMP Echo client.
267258
"""
268259

269-
if ip6_host:
270-
ip6_host.gateway = ip6_gateway
260+
if stack__ip6_host:
261+
stack__ip6_host.gateway = stack__ip6_gateway
271262

272-
if ip4_host:
273-
ip4_host.gateway = ip4_gateway
263+
if stack__ip4_host:
264+
stack__ip4_host.gateway = stack__ip4_gateway
274265

275266
stack.init(
276-
*stack.initialize_interface(interface),
277-
mac_address=mac_address,
278-
ip6_host=ip6_host,
279-
ip4_host=ip4_host,
267+
*stack.initialize_interface(stack__interface),
268+
mac_address=stack__mac_address,
269+
ip6_host=stack__ip6_host,
270+
ip4_host=stack__ip4_host,
280271
)
281272

282273
match remote_ip_address.version:
283274
case 6:
284275
client = IcmpEchoClient(
285-
local_ip_address=ip6_host.address if ip6_host else Ip6Address(),
276+
local_ip_address=(
277+
stack__ip6_host.address if stack__ip6_host else Ip6Address()
278+
),
286279
remote_ip_address=remote_ip_address,
287280
)
288281
case 4:
289282
client = IcmpEchoClient(
290-
local_ip_address=ip4_host.address if ip4_host else Ip4Address(),
283+
local_ip_address=(
284+
stack__ip4_host.address if stack__ip4_host else Ip4Address()
285+
),
291286
remote_ip_address=remote_ip_address,
292287
)
293288
case _:

0 commit comments

Comments
 (0)