Commit d84981e8 authored by Boris Pilka's avatar Boris Pilka

Removed unused module core


Former-commit-id: e38741d0
parent 4bd2e095
from datetime import timedelta
from . import Bundle, BundleAgeBlock, BundleProcFlag, BundleStatusReport, CreationTimestamp, HopCountBlock, PayloadBlock,\
PreviousNodeBlock, PrimaryBlock, ReasonCode, StatusCode, dtn_start, print_hex
primary_block = PrimaryBlock(
bundle_proc_flags=BundleProcFlag.MUST_NOT_BE_FRAGMENTED
| BundleProcFlag.CONTAINS_MANIFEST
| BundleProcFlag.REPORT_DELIVERY
| BundleProcFlag.REPORT_STATUS_TIME,
destination="dtn:GS2",
source="dtn:none",
report_to="dtn:none",
creation_time=CreationTimestamp(dtn_start, 0),
)
payload = PayloadBlock(b"Hello world!")
# Extension blocks
previous_node = PreviousNodeBlock("dtn:GS4")
hop_count = HopCountBlock(30, 0)
bundle_age = BundleAgeBlock(0)
bundle = Bundle([
primary_block,
previous_node,
hop_count,
bundle_age,
payload,
])
# Bundle Status Report "Received bundle"
status_report = BundleStatusReport(infos=StatusCode.RECEIVED_BUNDLE,
reason=ReasonCode.NO_INFO,
bundle=bundle,
time=dtn_start + timedelta(seconds=10))
status_report_bundle = Bundle([
PrimaryBlock(
bundle_proc_flags=BundleProcFlag.ADMINISTRATIVE_RECORD,
destination=bundle.primary_block.report_to,
),
status_report,
])
print_hex(bundle.serialize())
# print_hex(status_report_bundle.serialize())
# print_hex(status_report.serialize())
with open("bundle.cbor", "wb") as fd:
fd.write(bundle.serialize())
# TODO: move to tests
#
# from datetime import timedelta
# from . import Bundle, BundleAgeBlock, BundleProcFlag, BundleStatusReport, CreationTimestamp, HopCountBlock, PayloadBlock,\
# PreviousNodeBlock, PrimaryBlock, ReasonCode, StatusCode, dtn_start, print_hex
#
# primary_block = PrimaryBlock(
# bundle_proc_flags=BundleProcFlag.MUST_NOT_BE_FRAGMENTED
# | BundleProcFlag.CONTAINS_MANIFEST
# | BundleProcFlag.REPORT_DELIVERY
# | BundleProcFlag.REPORT_STATUS_TIME,
# destination="dtn:GS2",
# source="dtn:none",
# report_to="dtn:none",
# creation_time=CreationTimestamp(dtn_start, 0),
# )
#
# payload = PayloadBlock(b"Hello world!")
#
# # Extension blocks
# previous_node = PreviousNodeBlock("dtn:GS4")
# hop_count = HopCountBlock(30, 0)
# bundle_age = BundleAgeBlock(0)
#
# bundle = Bundle([
# primary_block,
# previous_node,
# hop_count,
# bundle_age,
# payload,
# ])
#
# # Bundle Status Report "Received bundle"
# status_report = BundleStatusReport(infos=StatusCode.RECEIVED_BUNDLE,
# reason=ReasonCode.NO_INFO,
# bundle=bundle,
# time=dtn_start + timedelta(seconds=10))
#
# status_report_bundle = Bundle([
# PrimaryBlock(
# bundle_proc_flags=BundleProcFlag.ADMINISTRATIVE_RECORD,
# destination=bundle.primary_block.report_to,
# ),
# status_report,
# ])
#
# print_hex(bundle.serialize())
# # print_hex(status_report_bundle.serialize())
# # print_hex(status_report.serialize())
#
# with open("bundle.cbor", "wb") as fd:
# fd.write(bundle.serialize())
from enum import Enum
class Bundle:
"""
This is 1:1 representation of a bundle according to documentation, all the application specific
extensions has to be in the corresponding wrappers.
4.2.1. Bundle - A bundle is a protocol data unit of BP, so named because negotiation
of the parameters of a data exchange may be impractical in a delay-tolerant
network: it is often better practice to "bundle" with a unit of data all
metadata that might be needed in order to make the data immediately usable
when delivered to applications. Each bundle comprises a sequence of two or
more "blocks" of protocol data, which serve various purposes.
@see https://datatracker.ietf.org/doc/draft-ietf-dtn-bpbis/?include_text=1
@author kamil.szabo@ifne.eu
"""
def __init__(self):
self.primaryBlock = None
self.canonicalBlocks = list()
def set_primary_block(self, primary):
"""Sets primary block of the bundle."""
self.primaryBlock = primary
def add_canonical_block(self, canonical):
"""Adds canonical block to the bundle."""
self.canonicalBlocks.append(canonical)
def get_canonical_blocks(self):
"""Returns a collection of all canonical blocks in the bundle."""
return self.canonicalBlocks
class BundlePrimaryBlock:
"""
4.2.2. Primary Bundle Block
The primary bundle block contains the basic information needed to
forward bundles to their destinations.
Each primary block SHALL be represented as a CBOR array; the number
of elements in the array SHALL be 8 (if the bundle is not a fragment
and CRC type is zero) or 9 (if the bundle is not a fragment and CRC
type is non-zero) or 10 (if the bundle is a fragment and CRC type is
zero) or 11 (if the bundle is a fragment and CRC-type is non-zero).
"""
def __init__(self):
self.bundleProcessingFlags = dict()
self.protocolVersion = 7
self.crcType = None
self.destinationEid = None
self.sourceEid = None
self.creationTimestamp = None
self.lifetime = None
"""
Lifetime: The lifetime field is an unsigned integer that indicates the time
at which the bundle’s payload will no longer be useful, encoded as a number
of microseconds past the creation time.
"""
self.fragmentOffset = None
"""
Fragment offset SHALL be represented as a CBOR unsigned integer indicating
the offset from the start of the original application data unit at which the
bytes comprising the payload of this bundle were located.
"""
self.crcValue = None
"""
CRC: If and only if the value of the CRC type field of this Primary block is
non-zero, a CRC SHALL be present in the primary block. The length and nature
of the CRC SHALL be as indicated by the CRC type. The CRC SHALL be computed
over the concatenation of all bytes (including CBOR "break" characters) of
the primary block including the CRC field itself, which for this purpose
SHALL be temporarily populated with the value zero.
"""
def set_flag(self, flag):
"""Sets a certain bundle processing flag to true."""
self.bundleProcessingFlags[flag] = True
def unset_flag(self, flag):
"""Sets a certain processing flag to false."""
self.bundleProcessingFlags[flag] = False
def clear_flags(self):
"""Clears all the processing flags of the bundle."""
self.bundleProcessingFlags.clear()
class BundleCanonicalBlock:
"""
4.2.3. Canonical Bundle Block Format
Every block other than the primary block (all such blocks are termed
"canonical" blocks) SHALL be represented as a CBOR array; the number
of elements in the array SHALL be 6 (if CRC type is zero) or 7
(otherwise).
"""
def __init__(self, block_type=None):
self.blockProcessingFlags = dict()
self.blockType = block_type
"""
Bundle block type code 1
indicates that the block is a bundle payload block. Block type
codes 2 through 9 are explicitly reserved as noted later in
this specification. Block type codes 192 through 255 are not
reserved and are available for private and/or experimental use.
"""
self.blockNumber = None
"""
The block number uniquely
identifies the block within the bundle, enabling blocks
(notably bundle security protocol blocks) to explicitly
reference other blocks in the same bundle. Block numbers need
not be in continuous sequence, and blocks need not appear in
block number sequence in the bundle.
"""
self.crcType = None
self.blockDataLength = None
"""
The block data length
field SHALL contain the aggregate length of all remaining
fields of the block, i.e., the block-type-specific data fields.
Block data length SHALL be represented as a CBOR unsigned
integer item.
"""
self.payload = None
"""
Block-type-specific data fields, whose nature and order are
type-specific and whose aggregate length in octets is the value
of the block data length field. For the Payload Block in
particular (block type 1), there SHALL be exactly one block-
type-specific data field, termed the "payload", which SHALL be
an application data unit, or some contiguous extent thereof,
represented as a CBOR byte string.
"""
self.crcValue = None
def set_flag(self, flag):
"""Sets a certain bundle processing flag to true."""
self.blockProcessingFlags[flag] = True
def unset_flag(self, flag):
"""Sets a certain processing flag to false."""
self.blockProcessingFlags[flag] = False
def clear_flags(self):
"""Clears all the processing flags of the bundle."""
self.blockProcessingFlags = dict()
class BundleProcessingFlagsEnum(Enum):
"""
4.1.3. Bundle Processing Control Flags
Bundle processing control flags assert properties of the bundle as a
whole rather than of any particular block of the bundle. They are
conveyed in the primary block of the bundle.
"""
BIT_0_RESERVED = 0x8000
BIT_1_RESERVED = 0x4000
BIT_2_RESERVED = 0x2000
DELETION_STATUS_REPORT_REQUESTED = 0x1000
DELIVERY_STATUS_REPORT_REQUESTED = 0x0800
FORWARD_STATUS_REPORT_REQUESTED = 0x0400
BIT_6_RESERVED = 0x0200
RECEPTION_STATUS_REPORT_REQUESTED = 0x0100
CONTAINS_MANIFEST_BLOCK = 0x0080
STATUS_TIME_REQUESTED_IN_ALL_REPORTS = 0x0040
USER_APPLICATION_ACK_REQUESTED = 0x0020
BIT_11_RESERVED = 0x0010
BIT_12_RESERVED = 0x0008
BUNDLE_MUST_NOT_BE_FRAGMENTED = 0x0004
PAYLOAD_IS_ADMINISTRATIVE_RECORD = 0x0002
BUNDLE_IS_FRAGMENT = 0x0001
class BlockProcessingFlagsEnum(Enum):
"""
4.1.4. Block Processing Control Flags
The block processing control flags assert properties of canonical
bundle blocks. They are conveyed in the header of the block to
which they pertain.
"""
BIT_0_RESERVED = 0x80
BIT_1_RESERVED = 0x40
BIT_2_RESERVED = 0x20
BIT_3_RESERVED = 0x10
DELETED_IF_NOT_PROCESSED = 0x08
STATUS_REPORT_IF_BLOCK_CANT_BE_PROCESSED = 0x04
BLOCK_REMOVED_IF_CANT_BE_PROCESSED = 0x02
BLOCK_REPLICATED_IN_EVERY_FRAGMENT = 0x01
class CRC(Enum):
"""
4.1.1. CRC Type
CRC type is an unsigned integer type code for which the following
values (and no others) are valid:
. 0 indicates "no CRC is present."
. 1 indicates "a standard X-25 CRC-16 is present." [CRC]
. 2 indicates "a standard PKZIP CRC-32 is present." [CRC]
"""
NONE = 0
X25_CRC_16 = 1
PKZIP_CRC_32 = 2
class CanonicalBlockType(Enum):
BLOCK_TYPE_PAYLOAD = 1
BLOCK_TYPE_MANIFEST = 4
BLOCK_TYPE_PREVIOUS = 7
BLOCK_TYPE_BUNDLE_AGE = 8
BLOCK_TYPE_HOP_COUNT = 9
class EndpointEid(Enum):
"""
The destinations of bundles are bundle endpoints, identified by text
strings termed "endpoint IDs" (see Section 3.1). Each endpoint ID
(EID) is a Uniform Resource Identifier (URI; [URI]). As such, each
endpoint ID can be characterized as having this general structure:
< scheme name > : < scheme-specific part, or "SSP" >
The scheme identified by the < scheme name > in an endpoint ID is a
set of syntactic and semantic rules that fully explain how to parse
and interpret the SSP. The set of allowable schemes is effectively
unlimited. Any scheme conforming to [URIREG] may be used in a bundle
protocol endpoint ID.
"""
def __init__(self, uri_scheme, scheme_specific_part):
self.uriScheme = uri_scheme
self.scheme_specific_part = scheme_specific_part
class UriScheme(Enum):
"""
0 Reserved
1 dtn RFC5050, Section 4.4
2 ipn RFC6260, Section 4
3-254 Unassigned
255 Reserved
"""
DTN = 0
IPN = 1
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment