From 1536d13803a9486f8b8e1c03739362bfa68e2536 Mon Sep 17 00:00:00 2001 From: Sebastian Majewski Date: Sun, 8 Sep 2024 14:48:45 -0500 Subject: [PATCH] Code refactor --- examples/icmp_echo_client.py | 21 +++++++++------------ examples/run_stack.py | 24 ++++++++++++------------ examples/tcp_daytime_service.py | 21 +++++++++------------ examples/tcp_discard_service.py | 21 +++++++++------------ examples/tcp_echo_client.py | 21 +++++++++------------ examples/tcp_echo_service.py | 21 +++++++++------------ examples/udp_daytime_service.py | 21 +++++++++------------ examples/udp_discard_service.py | 21 +++++++++------------ examples/udp_echo_client.py | 21 +++++++++------------ examples/udp_echo_service.py | 21 +++++++++------------ 10 files changed, 93 insertions(+), 120 deletions(-) diff --git a/examples/icmp_echo_client.py b/examples/icmp_echo_client.py index d4978fd8..7e9e9926 100755 --- a/examples/icmp_echo_client.py +++ b/examples/icmp_echo_client.py @@ -180,6 +180,7 @@ def __thread__client(self) -> None: ) @click.option( "--ip6-address", + "ip6_host", type=ClickTypeIp6Host(), default=None, help="IPv6 address/mask to be assigned to the interface.", @@ -192,6 +193,7 @@ def __thread__client(self) -> None: ) @click.option( "--ip4-address", + "ip4_host", type=ClickTypeIp4Host(), default=None, help="IPv4 address/mask to be assigned to the interface.", @@ -212,9 +214,9 @@ def cli( *, interface: str, mac_address: MacAddress | None, - ip6_address: Ip6Host | None, + ip6_host: Ip6Host | None, ip6_gateway: Ip6Address | None, - ip4_address: Ip4Host | None, + ip4_host: Ip4Host | None, ip4_gateway: Ip4Address | None, remote_ip_address: IpAddress, ) -> None: @@ -225,16 +227,11 @@ def cli( fd, mtu = initialize_interface(interface) - ip6_host = ( - None - if ip6_address is None - else Ip6Host(ip6_address, gateway=ip6_gateway) - ) - ip4_host = ( - None - if ip4_address is None - else Ip4Host(ip4_address, gateway=ip4_gateway) - ) + if ip6_host: + ip6_host.gateway = ip6_gateway + + if ip4_host: + ip4_host.gateway = ip4_gateway stack = TcpIpStack( fd=fd, diff --git a/examples/run_stack.py b/examples/run_stack.py index 26d13fee..35f0d0a6 100755 --- a/examples/run_stack.py +++ b/examples/run_stack.py @@ -67,6 +67,7 @@ ) @click.option( "--ip6-address", + "ip6_host", type=ClickTypeIp6Host(), default=None, help="IPv6 address/mask to be assigned to the interface.", @@ -79,6 +80,7 @@ ) @click.option( "--ip4-address", + "ip4_host", type=ClickTypeIp4Host(), default=None, help="IPv4 address/mask to be assigned to the interface.", @@ -93,9 +95,9 @@ def cli( *, interface: str, mac_address: MacAddress | None, - ip6_address: Ip6Host | None, + ip6_host: Ip6Host | None, ip6_gateway: Ip6Address | None, - ip4_address: Ip4Host | None, + ip4_host: Ip4Host | None, ip4_gateway: Ip4Address | None, ) -> None: """ @@ -104,20 +106,18 @@ def cli( fd, mtu = initialize_interface(interface) + if ip6_host: + ip6_host.gateway = ip6_gateway + + if ip4_host: + ip4_host.gateway = ip4_gateway + stack = TcpIpStack( fd=fd, mtu=mtu, mac_address=mac_address, - ip6_host=( - None - if ip6_address is None - else Ip6Host(ip6_address, gateway=ip6_gateway) - ), - ip4_host=( - None - if ip4_address is None - else Ip4Host(ip4_address, gateway=ip4_gateway) - ), + ip6_host=ip6_host, + ip4_host=ip4_host, ) try: diff --git a/examples/tcp_daytime_service.py b/examples/tcp_daytime_service.py index 4ec58ccc..0164a3df 100755 --- a/examples/tcp_daytime_service.py +++ b/examples/tcp_daytime_service.py @@ -139,6 +139,7 @@ def _service(self, *, connected_socket: Socket) -> None: ) @click.option( "--ip6-address", + "ip6_host", type=ClickTypeIp6Host(), default=None, help="IPv6 address/mask to be assigned to the interface.", @@ -151,6 +152,7 @@ def _service(self, *, connected_socket: Socket) -> None: ) @click.option( "--ip4-address", + "ip4_host", type=ClickTypeIp4Host(), default=None, help="IPv4 address/mask to be assigned to the interface.", @@ -171,9 +173,9 @@ def cli( *, interface: str, mac_address: MacAddress | None, - ip6_address: Ip6Host | None, + ip6_host: Ip6Host | None, ip6_gateway: Ip6Address | None, - ip4_address: Ip4Host | None, + ip4_host: Ip4Host | None, ip4_gateway: Ip4Address | None, local_port: int, ) -> None: @@ -184,16 +186,11 @@ def cli( fd, mtu = initialize_interface(interface) - ip6_host = ( - None - if ip6_address is None - else Ip6Host(ip6_address, gateway=ip6_gateway) - ) - ip4_host = ( - None - if ip4_address is None - else Ip4Host(ip4_address, gateway=ip4_gateway) - ) + if ip6_host: + ip6_host.gateway = ip6_gateway + + if ip4_host: + ip4_host.gateway = ip4_gateway stack = TcpIpStack( fd=fd, diff --git a/examples/tcp_discard_service.py b/examples/tcp_discard_service.py index 62c0665b..f6a2e7e4 100755 --- a/examples/tcp_discard_service.py +++ b/examples/tcp_discard_service.py @@ -144,6 +144,7 @@ def _service(self, *, connected_socket: Socket) -> None: ) @click.option( "--ip6-address", + "ip6_host", type=ClickTypeIp6Host(), default=None, help="IPv6 address/mask to be assigned to the interface.", @@ -156,6 +157,7 @@ def _service(self, *, connected_socket: Socket) -> None: ) @click.option( "--ip4-address", + "ip4_host", type=ClickTypeIp4Host(), default=None, help="IPv4 address/mask to be assigned to the interface.", @@ -176,9 +178,9 @@ def cli( *, interface: str, mac_address: MacAddress | None, - ip6_address: Ip6Host | None, + ip6_host: Ip6Host | None, ip6_gateway: Ip6Address | None, - ip4_address: Ip4Host | None, + ip4_host: Ip4Host | None, ip4_gateway: Ip4Address | None, local_port: int, ) -> None: @@ -189,16 +191,11 @@ def cli( fd, mtu = initialize_interface(interface) - ip6_host = ( - None - if ip6_address is None - else Ip6Host(ip6_address, gateway=ip6_gateway) - ) - ip4_host = ( - None - if ip4_address is None - else Ip4Host(ip4_address, gateway=ip4_gateway) - ) + if ip6_host: + ip6_host.gateway = ip6_gateway + + if ip4_host: + ip4_host.gateway = ip4_gateway stack = TcpIpStack( fd=fd, diff --git a/examples/tcp_echo_client.py b/examples/tcp_echo_client.py index 267f7c67..2ddd7b67 100755 --- a/examples/tcp_echo_client.py +++ b/examples/tcp_echo_client.py @@ -194,6 +194,7 @@ def __thread__client(self) -> None: ) @click.option( "--ip6-address", + "ip6_host", type=ClickTypeIp6Host(), default=None, help="IPv6 address/mask to be assigned to the interface.", @@ -206,6 +207,7 @@ def __thread__client(self) -> None: ) @click.option( "--ip4-address", + "ip4_host", type=ClickTypeIp4Host(), default=None, help="IPv4 address/mask to be assigned to the interface.", @@ -226,9 +228,9 @@ def cli( *, interface: str, mac_address: MacAddress | None, - ip6_address: Ip6Host | None, + ip6_host: Ip6Host | None, ip6_gateway: Ip6Address | None, - ip4_address: Ip4Host | None, + ip4_host: Ip4Host | None, ip4_gateway: Ip4Address | None, remote_ip_address: IpAddress, ) -> None: @@ -239,16 +241,11 @@ def cli( fd, mtu = initialize_interface(interface) - ip6_host = ( - None - if ip6_address is None - else Ip6Host(ip6_address, gateway=ip6_gateway) - ) - ip4_host = ( - None - if ip4_address is None - else Ip4Host(ip4_address, gateway=ip4_gateway) - ) + if ip6_host: + ip6_host.gateway = ip6_gateway + + if ip4_host: + ip4_host.gateway = ip4_gateway stack = TcpIpStack( fd=fd, diff --git a/examples/tcp_echo_service.py b/examples/tcp_echo_service.py index bf480ff5..bf210cc5 100755 --- a/examples/tcp_echo_service.py +++ b/examples/tcp_echo_service.py @@ -155,6 +155,7 @@ def _service(self, *, connected_socket: Socket) -> None: ) @click.option( "--ip6-address", + "ip6_host", type=ClickTypeIp6Host(), default=None, help="IPv6 address/mask to be assigned to the interface.", @@ -167,6 +168,7 @@ def _service(self, *, connected_socket: Socket) -> None: ) @click.option( "--ip4-address", + "ip4_host", type=ClickTypeIp4Host(), default=None, help="IPv4 address/mask to be assigned to the interface.", @@ -187,9 +189,9 @@ def cli( *, interface: str, mac_address: MacAddress | None, - ip6_address: Ip6Host | None, + ip6_host: Ip6Host | None, ip6_gateway: Ip6Address | None, - ip4_address: Ip4Host | None, + ip4_host: Ip4Host | None, ip4_gateway: Ip4Address | None, local_port: int, ) -> None: @@ -200,16 +202,11 @@ def cli( fd, mtu = initialize_interface(interface) - ip6_host = ( - None - if ip6_address is None - else Ip6Host(ip6_address, gateway=ip6_gateway) - ) - ip4_host = ( - None - if ip4_address is None - else Ip4Host(ip4_address, gateway=ip4_gateway) - ) + if ip6_host: + ip6_host.gateway = ip6_gateway + + if ip4_host: + ip4_host.gateway = ip4_gateway stack = TcpIpStack( fd=fd, diff --git a/examples/udp_daytime_service.py b/examples/udp_daytime_service.py index 0c86d706..db40619d 100755 --- a/examples/udp_daytime_service.py +++ b/examples/udp_daytime_service.py @@ -107,6 +107,7 @@ def _service(self, *, listening_socket: Socket) -> None: ) @click.option( "--ip6-address", + "ip6_host", type=ClickTypeIp6Host(), default=None, help="IPv6 address/mask to be assigned to the interface.", @@ -119,6 +120,7 @@ def _service(self, *, listening_socket: Socket) -> None: ) @click.option( "--ip4-address", + "ip4_host", type=ClickTypeIp4Host(), default=None, help="IPv4 address/mask to be assigned to the interface.", @@ -139,9 +141,9 @@ def cli( *, interface: str, mac_address: MacAddress | None, - ip6_address: Ip6Host | None, + ip6_host: Ip6Host | None, ip6_gateway: Ip6Address | None, - ip4_address: Ip4Host | None, + ip4_host: Ip4Host | None, ip4_gateway: Ip4Address | None, local_port: int, ) -> None: @@ -152,16 +154,11 @@ def cli( fd, mtu = initialize_interface(interface) - ip6_host = ( - None - if ip6_address is None - else Ip6Host(ip6_address, gateway=ip6_gateway) - ) - ip4_host = ( - None - if ip4_address is None - else Ip4Host(ip4_address, gateway=ip4_gateway) - ) + if ip6_host: + ip6_host.gateway = ip6_gateway + + if ip4_host: + ip4_host.gateway = ip4_gateway stack = TcpIpStack( fd=fd, diff --git a/examples/udp_discard_service.py b/examples/udp_discard_service.py index 99b10eac..a61801b1 100755 --- a/examples/udp_discard_service.py +++ b/examples/udp_discard_service.py @@ -104,6 +104,7 @@ def _service(self, *, listening_socket: Socket) -> None: ) @click.option( "--ip6-address", + "ip6_host", type=ClickTypeIp6Host(), default=None, help="IPv6 address/mask to be assigned to the interface.", @@ -116,6 +117,7 @@ def _service(self, *, listening_socket: Socket) -> None: ) @click.option( "--ip4-address", + "ip4_host", type=ClickTypeIp4Host(), default=None, help="IPv4 address/mask to be assigned to the interface.", @@ -136,9 +138,9 @@ def cli( *, interface: str, mac_address: MacAddress | None, - ip6_address: Ip6Host | None, + ip6_host: Ip6Host | None, ip6_gateway: Ip6Address | None, - ip4_address: Ip4Host | None, + ip4_host: Ip4Host | None, ip4_gateway: Ip4Address | None, local_port: int, ) -> None: @@ -149,16 +151,11 @@ def cli( fd, mtu = initialize_interface(interface) - ip6_host = ( - None - if ip6_address is None - else Ip6Host(ip6_address, gateway=ip6_gateway) - ) - ip4_host = ( - None - if ip4_address is None - else Ip4Host(ip4_address, gateway=ip4_gateway) - ) + if ip6_host: + ip6_host.gateway = ip6_gateway + + if ip4_host: + ip4_host.gateway = ip4_gateway stack = TcpIpStack( fd=fd, diff --git a/examples/udp_echo_client.py b/examples/udp_echo_client.py index 165016e0..41838456 100755 --- a/examples/udp_echo_client.py +++ b/examples/udp_echo_client.py @@ -189,6 +189,7 @@ def __thread__client(self) -> None: ) @click.option( "--ip6-address", + "ip6_host", type=ClickTypeIp6Host(), default=None, help="IPv6 address/mask to be assigned to the interface.", @@ -201,6 +202,7 @@ def __thread__client(self) -> None: ) @click.option( "--ip4-address", + "ip4_host", type=ClickTypeIp4Host(), default=None, help="IPv4 address/mask to be assigned to the interface.", @@ -221,9 +223,9 @@ def cli( *, interface: str, mac_address: MacAddress | None, - ip6_address: Ip6Host | None, + ip6_host: Ip6Host | None, ip6_gateway: Ip6Address | None, - ip4_address: Ip4Host | None, + ip4_host: Ip4Host | None, ip4_gateway: Ip4Address | None, remote_ip_address: IpAddress, ) -> None: @@ -234,16 +236,11 @@ def cli( fd, mtu = initialize_interface(interface) - ip6_host = ( - None - if ip6_address is None - else Ip6Host(ip6_address, gateway=ip6_gateway) - ) - ip4_host = ( - None - if ip4_address is None - else Ip4Host(ip4_address, gateway=ip4_gateway) - ) + if ip6_host: + ip6_host.gateway = ip6_gateway + + if ip4_host: + ip4_host.gateway = ip4_gateway stack = TcpIpStack( fd=fd, diff --git a/examples/udp_echo_service.py b/examples/udp_echo_service.py index 04d9cc67..b3786b32 100755 --- a/examples/udp_echo_service.py +++ b/examples/udp_echo_service.py @@ -120,6 +120,7 @@ def _service(self, *, listening_socket: Socket) -> None: ) @click.option( "--ip6-address", + "ip6_host", type=ClickTypeIp6Host(), default=None, help="IPv6 address/mask to be assigned to the interface.", @@ -132,6 +133,7 @@ def _service(self, *, listening_socket: Socket) -> None: ) @click.option( "--ip4-address", + "ip4_host", type=ClickTypeIp4Host(), default=None, help="IPv4 address/mask to be assigned to the interface.", @@ -152,9 +154,9 @@ def cli( *, interface: str, mac_address: MacAddress | None, - ip6_address: Ip6Host | None, + ip6_host: Ip6Host | None, ip6_gateway: Ip6Address | None, - ip4_address: Ip4Host | None, + ip4_host: Ip4Host | None, ip4_gateway: Ip4Address | None, local_port: int, ) -> None: @@ -165,16 +167,11 @@ def cli( fd, mtu = initialize_interface(interface) - ip6_host = ( - None - if ip6_address is None - else Ip6Host(ip6_address, gateway=ip6_gateway) - ) - ip4_host = ( - None - if ip4_address is None - else Ip4Host(ip4_address, gateway=ip4_gateway) - ) + if ip6_host: + ip6_host.gateway = ip6_gateway + + if ip4_host: + ip4_host.gateway = ip4_gateway stack = TcpIpStack( fd=fd,