Skip to content

Commit

Permalink
using zlib.crc32 to avoid icmp id collision
Browse files Browse the repository at this point in the history
  • Loading branch information
kyan001 committed May 18, 2020
1 parent 504a826 commit a7bb315
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions ping3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import struct
import select
import time
import zlib
import threading
import logging
import functools

import errors
from enums import ICMP_DEFAULT_CODE, IcmpType, IcmpTimeExceededCode, IcmpDestinationUnreachableCode

__version__ = "2.6.2"
__version__ = "2.6.5"
DEBUG = False # DEBUG: Show debug info for developers. (default False)
EXCEPTIONS = False # EXCEPTIONS: Raise exception when delay is not available.
LOGGER = None # LOGGER: Record logs into console or file.
Expand Down Expand Up @@ -164,7 +165,7 @@ def send_one_ping(sock: socket, dest_addr: str, icmp_id: int, seq: int, size: in
Args:
sock: Socket.
dest_addr: The destination address, can be an IP address or a domain name. Ex. "192.168.1.1"/"example.com"
icmp_id: ICMP packet id. Calculated from Process ID plus Thread ID.
icmp_id: ICMP packet id. Calculated from Process ID and Thread ID.
seq: ICMP packet sequence, usually increases from 0 in the same process.
size: The ICMP packet payload size in bytes. Note this is only for the payload part.
Expand Down Expand Up @@ -285,7 +286,7 @@ def ping(dest_addr: str, timeout: int = 4, unit: str = "s", src_addr: str = None
_debug("Socket Source Address Binded:", src_addr)
thread_id = threading.get_native_id() if hasattr(threading, 'get_native_id') else threading.currentThread().ident # threading.get_native_id() is supported >= python3.8.
process_id = os.getpid() # If ping() run under different process, thread_id may be identical.
icmp_id = checksum(str(process_id + thread_id).encode()) # using checksum to avoid icmp_id collision.
icmp_id = zlib.crc32("{}{}".format(process_id, thread_id).encode()) & 0xffff # to avoid icmp_id collision.
try:
send_one_ping(sock=sock, dest_addr=dest_addr, icmp_id=icmp_id, seq=seq, size=size)
delay = receive_one_ping(sock=sock, icmp_id=icmp_id, seq=seq, timeout=timeout) # in seconds
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='ping3',
version='2.6.2',
version='2.6.5',
description='A pure python3 version of ICMP ping implementation using raw socket.',
long_description='Ping3 is a pure python3 version of ICMP ping implementation using raw socket. Note that ICMP messages can only be sent from processes running as root.',
url='https://github.com/kyan001/ping3',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ping3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class test_ping3(unittest.TestCase):
"""ping3 unittest"""
__version__ = "2.6.2"
__version__ = "2.6.5"

def setUp(self):
pass
Expand Down

0 comments on commit a7bb315

Please sign in to comment.