Skip to content

Commit

Permalink
[sctp] fix import warning for crc32c
Browse files Browse the repository at this point in the history
The crc32c package renamed the `crc32` function to `crc32c` as in
release 2.1. It is also no longer necessary to set `CRC32C_SW_MODE`
since release 2.0.
  • Loading branch information
jlaine committed Jan 27, 2021
1 parent 7d8a4c2 commit e50b947
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"aioice>=0.7.0,<0.8.0",
"av>=8.0.0,<9.0.0",
"cffi>=1.0.0",
"crc32c",
"crc32c>=2.1",
"cryptography>=2.2",
'dataclasses; python_version < "3.7"',
"pyee>=6.0.0",
Expand Down
12 changes: 3 additions & 9 deletions src/aiortc/rtcsctptransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,14 @@
no_type_check,
)

from crc32c import crc32c
from pyee import AsyncIOEventEmitter

from .exceptions import InvalidStateError
from .rtcdatachannel import RTCDataChannel, RTCDataChannelParameters
from .rtcdtlstransport import RTCDtlsTransport
from .utils import random32, uint16_add, uint16_gt, uint32_gt, uint32_gte

try:
from crc32c import crc32
except ImportError: # pragma: no cover
os.environ["CRC32C_SW_MODE"] = "1"
from crc32c import crc32


logger = logging.getLogger("sctp")

# local constants
Expand Down Expand Up @@ -406,7 +400,7 @@ def parse_packet(data: bytes) -> Tuple[int, int, int, List[Any]]:

# verify checksum
checksum = unpack_from("<L", data, 8)[0]
if checksum != crc32(data[0:8] + b"\x00\x00\x00\x00" + data[12:]):
if checksum != crc32c(data[0:8] + b"\x00\x00\x00\x00" + data[12:]):
raise ValueError("SCTP packet has invalid checksum")

chunks = []
Expand All @@ -426,7 +420,7 @@ def serialize_packet(
) -> bytes:
header = pack("!HHL", source_port, destination_port, verification_tag)
data = bytes(chunk)
checksum = crc32(header + b"\x00\x00\x00\x00" + data)
checksum = crc32c(header + b"\x00\x00\x00\x00" + data)
return header + pack("<L", checksum) + data


Expand Down
2 changes: 1 addition & 1 deletion stubs/crc32c.pyi
Original file line number Diff line number Diff line change
@@ -1 +1 @@
def crc32(data: bytes) -> int: ...
def crc32c(data: bytes) -> int: ...

0 comments on commit e50b947

Please sign in to comment.