Skip to content

Commit

Permalink
Make checksum codes easier to read.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyan001 committed Jul 9, 2021
1 parent f7653b9 commit 30dcbcb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
39 changes: 38 additions & 1 deletion UPDATES.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# UPDATES
* 2.8.1:
* Improvement: Checksum calculation is faster.
* 2.8.0:
* Feature: Now support endless ping, using `ping3 -c 0 example.com` with a count of 0 or `ping3.verbose_ping('example.com', count=0)` to start, using `ctrl + c` to stop.
* 2.7.0:
* Using `SOCK_DGRAM` instead of `SOCK_RAW` on macOS. According to [this](https://apple.stackexchange.com/questions/312857/how-does-macos-allow-standard-users-to-ping), `SOCK_DGRAM` can be sent by standard user on macOS.
* Feature: Using `SOCK_DGRAM` instead of `SOCK_RAW` on macOS. According to [this](https://apple.stackexchange.com/questions/312857/how-does-macos-allow-standard-users-to-ping), `SOCK_DGRAM` can be sent by standard user on macOS.
* 2.6.6:
* Bug Fix: `setsockopt` error for `SOL_IP.IP_TTL` on windows. ( #28 )
* 2.6.5:
* Bug Fix: When multi-processing or multi-threading, icmp_id will no longer collision. ( #23 )
* 2.6.1:
* Feature: Add network interface binding support for Linux. ( #22 )
* 2.5.1:
* Features:
* Add interval support to `ping3.verbose_ping()`. ( #17 )
* Add `-i/--interval` argument for interval support in command-line.
* 2.4.7:
* Bug Fix: Input parameter `size` in `ping()` should not include ICMP_Header size. ( #21 )
* 2.4.4:
* Bug Fix: When there are a lot of incoming packets, and the destination address has no response, `ping()` never return. ( #14 )
* 2.4.0:
* Feature: Return False if HostUnknown error raised (instead of print info to screen).
* Improvement: Increase usability of errors and provide a more precisely text.
* 2.3.1:
* Features:
* Add `--debug` argument for DEBUG mode in command-line.
* Add `--exceptions` argument for EXCEPTIONS mode in command-line.
* 2.2.3:
* Feature: Add command-line mode.
* 2.0.1:
* Features:
* Add support of ICMP Payload size.
* Add support of EXCEPTIONS mode.
* Add support of DEBUG mode.
* 1.2.1:
* Feature: Add support for multiple interfaces. Use `ping(..., src_addr="INTERFACE IP")`
* 1.1.0:
* Improvement: Update tests and PyPI info.
16 changes: 9 additions & 7 deletions ping3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import errors
from enums import ICMP_DEFAULT_CODE, IcmpType, IcmpTimeExceededCode, IcmpDestinationUnreachableCode

__version__ = "2.8.0"
__version__ = "2.8.1"
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 @@ -93,15 +93,17 @@ def checksum(source: bytes) -> int:
RFC792: https://tools.ietf.org/html/rfc792
Args:
source: The input to be calculated.
source: Bytes. The input to be calculated.
Returns:
Calculated checksum.
int: Calculated checksum.
"""
res = sum(source[::2]) + (sum(source[1::2]) << 8)
while res > 0xffff:
res = sum(divmod(res, 0x10000))
return ~res & 0xffff
BITS = 16 # 16-bit long
carry = 1 << BITS # 0x10000
result = sum(source[::2]) + (sum(source[1::2]) << (BITS // 2)) # Even bytes (odd indexes) shift 1 byte to the left.
while result >= carry: # Ones' complement sum.
result = sum(divmod(result, carry)) # Each carry add to right most bit.
return ~result & ((1 << BITS) - 1) # Ensure 16-bit


def read_icmp_header(raw: bytes) -> dict:
Expand Down

0 comments on commit 30dcbcb

Please sign in to comment.