Skip to content

Commit 92f9fb7

Browse files
committed
Working on implementing ping example.
1 parent 7040a3c commit 92f9fb7

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

examples/lib/client.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@
4343
import click
4444
from net_addr.ip_address import IpAddress
4545

46-
from pytcp.socket import AF_INET4, AF_INET6, SOCK_DGRAM, SOCK_STREAM, socket
46+
from pytcp.socket import (
47+
AF_INET4,
48+
AF_INET6,
49+
IPPROTO_ICMP4,
50+
IPPROTO_ICMP6,
51+
SOCK_DGRAM,
52+
SOCK_RAW,
53+
SOCK_STREAM,
54+
socket,
55+
)
4756

4857
if TYPE_CHECKING:
4958
from pytcp.socket.socket import Socket
@@ -103,8 +112,20 @@ def _get_client_socket(self) -> Socket | None:
103112
client_socket = socket(family=AF_INET6, type=SOCK_DGRAM)
104113
case 4, 4, "UDP":
105114
client_socket = socket(family=AF_INET4, type=SOCK_DGRAM)
115+
case 6, 6, "ICMP":
116+
client_socket = socket(
117+
family=AF_INET6, type=SOCK_RAW, protocol=IPPROTO_ICMP6
118+
)
119+
self._local_port = int(IPPROTO_ICMP6)
120+
self._remote_port = 0
121+
case 4, 4, "ICMP":
122+
client_socket = socket(
123+
family=AF_INET4, type=SOCK_RAW, protocol=IPPROTO_ICMP4
124+
)
125+
self._local_port = int(IPPROTO_ICMP4)
126+
self._remote_port = 0
106127
case _:
107-
raise ValueError("Invalid IP version or protocol.")
128+
raise ValueError("Invalid IP versions or protocol combination.")
108129

109130
click.echo(
110131
f"Client {self._protocol_name} {self._client_name}: "

0 commit comments

Comments
 (0)