Skip to content

Commit

Permalink
Define stream number validity
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSurda committed Jul 31, 2022
1 parent bb7d801 commit 69e540b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/network/bmobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -106,6 +112,9 @@ def checkStream(self):
'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):
"""
Expand Down
7 changes: 6 additions & 1 deletion src/network/bmproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
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,
Expand Down Expand Up @@ -409,6 +410,10 @@ def bm_command_object(self):
self.object.inventoryHash, acceptmismatch)
if not acceptmismatch:
raise
except BMObjectInvalidStreamError:
BMProto.stopDownloadingObject(
self.object.inventoryHash)
raise

try:
self.object.checkObjectByType()
Expand Down
5 changes: 5 additions & 0 deletions src/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 69e540b

Please sign in to comment.