From 60742333ba5b81bed54b843ad7b011b3c61adb09 Mon Sep 17 00:00:00 2001 From: p1-ra Date: Tue, 9 Jun 2020 09:59:12 +0000 Subject: [PATCH 1/3] allow set of default values for ppid, streamid, ttl --- sctp.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/sctp.py b/sctp.py index de14829..28ce86a 100644 --- a/sctp.py +++ b/sctp.py @@ -1030,6 +1030,9 @@ def __init__(self, family, style, sk): self._style = style self._sk = sk self._family = family + self._ppid = 0 + self._ttl = 0 + self._streamid = 0 self.unexpected_event_raises_exception = False self.initparams = initparams(self) @@ -1037,6 +1040,57 @@ def __init__(self, family, style, sk): self.datalogging = False + @property + def ppid(self): + """ + Read default payload protocol identifier + """ + return self._ppid + + @ppid.setter + def ppid(self, newVal): + """ + Write default payload protocol identifier + """ + if not isinstance(newVal, int) or newVal < 0 or newVal > 0xFFFFFFFF: + raise ValueError('PPID shall be a valid unsigned 32bits integer') + + self._ppid = newVal + + @property + def ttl(self): + """ + Read default time to live value, 0 mean infinite + """ + return self._ttl + + @ttl.setter + def ttl(self, newVal): + """ + Write default time to live + """ + if not isinstance(newVal, int) or newVal < 0 or newVal > 255: + raise ValueError('TTL shall be >= 0 and <= 255') + + self._ttl = newVal + + @property + def streamid(self): + """ + Read default stream identifier + """ + return self._streamid + + @streamid.setter + def streamid(self, newVal): + """ + Write default stream identifier + """ + if not isinstance(newVal, int) or newVal < 0 or newVal > 65535: + raise ValueError('streamid shall be a valid unsigned 16bits integer') + + self._streamid = newVal + def bindx(self, sockaddrs, action=BINDX_ADD): """ Binds to a list of addresses. This method() allows to bind to any subset @@ -1107,7 +1161,7 @@ def getladdrs(self, assoc_id = 0): # -> tuple of sockaddrs as strings return _sctp.getladdrs(self._sk.fileno(), assoc_id) - def sctp_send(self, msg, to=("",0), ppid=0, flags=0, stream=0, timetolive=0, context=0, + def sctp_send(self, msg, to=("",0), ppid=None, flags=0, stream=None, timetolive=None, context=0, record_file_prefix="RECORD_sctp_traffic", datalogging = False): """ Sends a SCTP message. While send()/sendto() can also be used, this method also @@ -1121,7 +1175,7 @@ def sctp_send(self, msg, to=("",0), ppid=0, flags=0, stream=0, timetolive=0, con WARNING: identifying destination by Association ID not implemented yet! ppid: adaptation layer value, a 32-bit metadata that is sent along the message. - Defaults to 0. + If not set use default value. flags: a bitmap of MSG_* flags. For example, MSG_UNORDERED indicates that message can be delivered out-of-order, and MSG_EOF + empty message @@ -1131,11 +1185,12 @@ def sctp_send(self, msg, to=("",0), ppid=0, flags=0, stream=0, timetolive=0, con that are supported by sendto(). stream: stream number where the message will sent by. Defaults to 0. + If not set use default value. timetolive: time to live of the message in milisseconds. Zero means infinite TTL. If TTL expires, the message is discarded. Discarding policy changes whether implementation implements the PR-SCTP extension or not. - Defaults to 0 (infinite). + If not set use default value. context: an opaque 32-bit integer that will be returned in some notification events, if the event is directly related to this message transmission. So the @@ -1150,6 +1205,15 @@ def sctp_send(self, msg, to=("",0), ppid=0, flags=0, stream=0, timetolive=0, con both by the implementation and by the transmission buffer (SO_SNDBUF). The application must configure this buffer accordingly. """ + if ppid is None: + ppid = self._ppid + + if timetolive is None: + timetolive = self._ttl + + if stream is None: + stream = self._streamid + if datalogging == True or self.datalogging == True: now = datetime.datetime.now() recordfilename = record_file_prefix + "-" + now.strftime("%Y%m%d%H%M%S") + "-c2s." From f889c6f2edc762b0a7d39032c2a2416429fed5e4 Mon Sep 17 00:00:00 2001 From: p1-ra Date: Wed, 10 Jun 2020 10:33:56 +0000 Subject: [PATCH 2/3] remove ppid attribute, duplicate of {get,set}_adaptation --- sctp.py | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/sctp.py b/sctp.py index 28ce86a..c3105b9 100644 --- a/sctp.py +++ b/sctp.py @@ -230,7 +230,6 @@ def __init__(self, values=None): self.stream = 0 self.ssn = 0 self.flags = 0 - self.ppid = 0 self.context = 0 self.timetolive = 0 self.tsn = 0 @@ -1006,6 +1005,10 @@ class sctpsocket(object): associations, in seconds. A value of 0 means that no automatic close will be done. This property does not work for TCP-style sockets. + timetolive: Default TTL value to use with sctp_send. Default set to 0. + + streamid: Default SCTP stream identifier value to use with sctp_send. Default set to 0. + IMPORTANT NOTE: the maximum message size is limited both by the implementation and by the transmission buffer (SO_SNDBUF). SCTP applications must configure the transmission and receiving bufers accordingly to the biggest messages it @@ -1030,7 +1033,6 @@ def __init__(self, family, style, sk): self._style = style self._sk = sk self._family = family - self._ppid = 0 self._ttl = 0 self._streamid = 0 @@ -1040,23 +1042,6 @@ def __init__(self, family, style, sk): self.datalogging = False - @property - def ppid(self): - """ - Read default payload protocol identifier - """ - return self._ppid - - @ppid.setter - def ppid(self, newVal): - """ - Write default payload protocol identifier - """ - if not isinstance(newVal, int) or newVal < 0 or newVal > 0xFFFFFFFF: - raise ValueError('PPID shall be a valid unsigned 32bits integer') - - self._ppid = newVal - @property def ttl(self): """ @@ -1161,7 +1146,7 @@ def getladdrs(self, assoc_id = 0): # -> tuple of sockaddrs as strings return _sctp.getladdrs(self._sk.fileno(), assoc_id) - def sctp_send(self, msg, to=("",0), ppid=None, flags=0, stream=None, timetolive=None, context=0, + def sctp_send(self, msg, to=("",0), ppid=0, flags=0, stream=None, timetolive=None, context=0, record_file_prefix="RECORD_sctp_traffic", datalogging = False): """ Sends a SCTP message. While send()/sendto() can also be used, this method also @@ -1175,7 +1160,7 @@ def sctp_send(self, msg, to=("",0), ppid=None, flags=0, stream=None, timetolive= WARNING: identifying destination by Association ID not implemented yet! ppid: adaptation layer value, a 32-bit metadata that is sent along the message. - If not set use default value. + Default to 0. flags: a bitmap of MSG_* flags. For example, MSG_UNORDERED indicates that message can be delivered out-of-order, and MSG_EOF + empty message @@ -1184,7 +1169,7 @@ def sctp_send(self, msg, to=("",0), ppid=None, flags=0, stream=None, timetolive= It does NOT include flags like MSG_DONTROUTE or other low-level flags that are supported by sendto(). - stream: stream number where the message will sent by. Defaults to 0. + stream: stream number where the message will sent by. If not set use default value. timetolive: time to live of the message in milisseconds. Zero means infinite @@ -1205,8 +1190,6 @@ def sctp_send(self, msg, to=("",0), ppid=None, flags=0, stream=None, timetolive= both by the implementation and by the transmission buffer (SO_SNDBUF). The application must configure this buffer accordingly. """ - if ppid is None: - ppid = self._ppid if timetolive is None: timetolive = self._ttl From 48fc7cf3d9fefc8a4548b3a5c9704715622153f7 Mon Sep 17 00:00:00 2001 From: p1-ra Date: Wed, 10 Jun 2020 10:39:11 +0000 Subject: [PATCH 3/3] update properties declaration to match code conventions --- sctp.py | 68 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/sctp.py b/sctp.py index c3105b9..6c94eb8 100644 --- a/sctp.py +++ b/sctp.py @@ -1005,7 +1005,7 @@ class sctpsocket(object): associations, in seconds. A value of 0 means that no automatic close will be done. This property does not work for TCP-style sockets. - timetolive: Default TTL value to use with sctp_send. Default set to 0. + ttl: Default timetolive value to use with sctp_send. Default set to 0. streamid: Default SCTP stream identifier value to use with sctp_send. Default set to 0. @@ -1042,40 +1042,6 @@ def __init__(self, family, style, sk): self.datalogging = False - @property - def ttl(self): - """ - Read default time to live value, 0 mean infinite - """ - return self._ttl - - @ttl.setter - def ttl(self, newVal): - """ - Write default time to live - """ - if not isinstance(newVal, int) or newVal < 0 or newVal > 255: - raise ValueError('TTL shall be >= 0 and <= 255') - - self._ttl = newVal - - @property - def streamid(self): - """ - Read default stream identifier - """ - return self._streamid - - @streamid.setter - def streamid(self, newVal): - """ - Write default stream identifier - """ - if not isinstance(newVal, int) or newVal < 0 or newVal > 65535: - raise ValueError('streamid shall be a valid unsigned 16bits integer') - - self._streamid = newVal - def bindx(self, sockaddrs, action=BINDX_ADD): """ Binds to a list of addresses. This method() allows to bind to any subset @@ -1672,6 +1638,36 @@ def set_rtoinfo(self, o): """ _sctp.set_rtoinfo(self._sk.fileno(), o.__dict__) + def get_ttl(self): + """ + Read default time to live value, 0 mean infinite + """ + return self._ttl + + def set_ttl(self, newVal): + """ + Write default time to live + """ + if not isinstance(newVal, int) or newVal < 0 or newVal > 255: + raise ValueError('TTL shall be >= 0 and <= 255') + + self._ttl = newVal + + def get_streamid(self): + """ + Read default stream identifier + """ + return self._streamid + + def set_streamid(self, newVal): + """ + Write default stream identifier + """ + if not isinstance(newVal, int) or newVal < 0 or newVal > 65535: + raise ValueError('streamid shall be a valid unsigned 16bits integer') + + self._streamid = newVal + # delegation def sock(self): @@ -1696,6 +1692,8 @@ def __getattr__(self, name): mappedv4 = property(get_mappedv4, set_mappedv4) maxseg = property(get_maxseg, set_maxseg) autoclose = property(get_autoclose, set_autoclose) + ttl = property(get_ttl, set_ttl) + streamid = property(get_streamid, set_streamid) class sctpsocket_tcp(sctpsocket): """