Define stream number validity

This commit is contained in:
Peter Šurda 2022-07-31 19:29:19 +08:00
parent bb7d8018c6
commit 69e540b504
Signed by: PeterSurda
GPG Key ID: 3E47497CF67ABB95
3 changed files with 20 additions and 1 deletions

View File

@ -36,6 +36,12 @@ class BMObjectUnwantedStreamError(Exception):
errorCodes = ("Object in unwanted stream")
class BMObjectInvalidStreamError(Exception):
"""Exception indicating the object is in a stream
outside of specification."""
errorCodes = ("Object in invalid stream")
class BMObjectInvalidError(Exception):
"""The object's data does not match object specification."""
errorCodes = ("Invalid object")
@ -106,6 +112,9 @@ class BMObject(object): # pylint: disable=too-many-instance-attributes
'The streamNumber %i isn\'t one we are interested in.',
self.streamNumber)
raise BMObjectUnwantedStreamError()
if self.streamNumber < protocol.MIN_VALID_STREAM \
or self.streamNumber > protocol.MAX_VALID_STREAM:
raise BMObjectInvalidStreamError()
def checkAlreadyHave(self):
"""

View File

@ -22,7 +22,8 @@ from network.advanceddispatcher import AdvancedDispatcher
from network.bmobject import (
BMObject, BMObjectAlreadyHaveError, BMObjectExpiredError,
BMObjectInsufficientPOWError, BMObjectInvalidDataError,
BMObjectInvalidError, BMObjectUnwantedStreamError
BMObjectInvalidError, BMObjectUnwantedStreamError,
BMObjectInvalidStreamError
)
from network.constants import (
ADDRESS_ALIVE, MAX_MESSAGE_SIZE, MAX_OBJECT_COUNT,
@ -409,6 +410,10 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
self.object.inventoryHash, acceptmismatch)
if not acceptmismatch:
raise
except BMObjectInvalidStreamError:
BMProto.stopDownloadingObject(
self.object.inventoryHash)
raise
try:
self.object.checkObjectByType()

View File

@ -90,6 +90,11 @@ def isBitSetWithinBitfield(fourByteString, n):
x, = unpack('>L', fourByteString)
return x & 2**n != 0
# Streams
MIN_VALID_STREAM = 1
MAX_VALID_STREAM = 2**63 - 1
# IP addresses