Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
9 changes: 6 additions & 3 deletions aioping/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def checksum(buffer):
return answer


async def receive_one_ping(my_socket, id_, timeout):
async def receive_one_ping(my_socket, id_, timeout, expected_src_ip):
"""
receive the ping from the socket.
:param my_socket:
Expand All @@ -151,7 +151,7 @@ async def receive_one_ping(my_socket, id_, timeout):
try:
async with async_timeout.timeout(timeout):
while True:
rec_packet = await loop.sock_recv(my_socket, 1024)
rec_packet, addr = await loop.sock_recvfrom(my_socket, 1024)

# No IP Header when unpriviledged on Linux
has_ip_header = (
Expand All @@ -176,6 +176,9 @@ async def receive_one_ping(my_socket, id_, timeout):
if type != ICMP_ECHO_REPLY and type != ICMP6_ECHO_REPLY:
continue

if addr[0] != expected_src_ip:
continue

if not has_ip_header:
# When unprivileged on Linux, ICMP ID is rewrited by kernel
# According to https://stackoverflow.com/a/14023878/4528364
Expand Down Expand Up @@ -293,7 +296,7 @@ async def ping(dest_addr, timeout=10, family=None):
my_id = uuid.uuid4().int & 0xFFFF

await send_one_ping(my_socket, addr, my_id, timeout, family)
delay = await receive_one_ping(my_socket, my_id, timeout)
delay = await receive_one_ping(my_socket, my_id, timeout, addr[0])
my_socket.close()

return delay
Expand Down