def encode_noncbheeids(bundle): # Put the eid offsets into the dictionary and append their sdnvs # to the data buffer #this seems like an optimization --kurtis eids_buffer = '' dict_offsets = {} dict_buffer = '' for eid in (bundle.dest, bundle.source, bundle.replyto, bundle.custodian): (scheme, ssp) = eid.split(':') if dict_offsets.has_key(scheme): scheme_offset = dict_offsets[scheme] else: dict_offsets[scheme] = scheme_offset = len(dict_buffer) dict_buffer += scheme dict_buffer += '\0' if dict_offsets.has_key(ssp): ssp_offset = dict_offsets[ssp] else: dict_offsets[ssp] = ssp_offset = len(dict_buffer) dict_buffer += ssp dict_buffer += '\0' eids_buffer += sdnv.sdnv_encode(scheme_offset) eids_buffer += sdnv.sdnv_encode(ssp_offset) return (eids_buffer, dict_buffer)
def encode_noncbheeids(bundle): # Put the eid offsets into the dictionary and append their sdnvs # to the data buffer #this seems like an optimization --kurtis eids_buffer = '' dict_offsets = {} dict_buffer = '' for eid in ( bundle.dest, bundle.source, bundle.replyto, bundle.custodian ) : (scheme, ssp) = eid.split(':') if dict_offsets.has_key(scheme): scheme_offset = dict_offsets[scheme] else: dict_offsets[scheme] = scheme_offset = len(dict_buffer) dict_buffer += scheme dict_buffer += '\0' if dict_offsets.has_key(ssp): ssp_offset = dict_offsets[ssp] else: dict_offsets[ssp] = ssp_offset = len(dict_buffer) dict_buffer += ssp dict_buffer += '\0' eids_buffer += sdnv.sdnv_encode(scheme_offset) eids_buffer += sdnv.sdnv_encode(ssp_offset) return (eids_buffer, dict_buffer)
def encode_block_preamble(type, flags, eid_offsets, length): """Encode the standard preamble for a block""" eid_data = '' if len(eid_offsets) != 0: flags = flags | BLOCK_FLAG_EID_REFS eid_data = sdnv.sdnv_encode(len(eid_offsets)) for o in eid_offsets: eid_data += sdnv.sdnv_encode(o) return ''.join((struct.pack('B', type), sdnv.sdnv_encode(flags), eid_data, sdnv.sdnv_encode(length)))
def serialize(self): """Serializes this ACS into a string of bytes that constitute the payload of an aggregate custody signal. This serialization does not include the bundle primary block, or the payload block header; it is only the payload of the payload block. """ # Encode Administrative Record header byte toReturn = "\x40" # Aggregate Custody Signal, not for a fragment. # Encode status byte toReturn += struct.pack('!B', 128 * self.succeeded) # Encode the array of fills. for (start, length) in self.fills: toReturn += sdnv_encode(start) toReturn += sdnv_encode(length) return toReturn
def encode_cbheeids(bundle): eids_buffer = '' dict_buffer = '' for eid in (bundle.dest, bundle.source, bundle.replyto, bundle.custodian): if eid == "dtn:none": eids_buffer += sdnv.sdnv_encode(0) eids_buffer += sdnv.sdnv_encode(0) continue (scheme, ssp) = eid.split(':') (node, service) = map(int, ssp.split('.')) eids_buffer += sdnv.sdnv_encode(node) eids_buffer += sdnv.sdnv_encode(service) return (eids_buffer, dict_buffer)
def serialize(self): """Serializes this ACS into a string of bytes that constitute the payload of an aggregate custody signal. This serialization does not include the bundle primary block, or the payload block header; it is only the payload of the payload block. """ # Encode Administrative Record header byte toReturn = "\x40" # Aggregate Custody Signal, not for a fragment. # Encode status byte toReturn += struct.pack('!B', 128*self.succeeded) # Encode the array of fills. for (start, length) in self.fills: toReturn += sdnv_encode(start) toReturn += sdnv_encode(length) return toReturn
def encode_cbheeids(bundle): eids_buffer = '' dict_buffer = '' for eid in ( bundle.dest, bundle.source, bundle.replyto, bundle.custodian ) : if eid == "dtn:none": eids_buffer += sdnv.sdnv_encode(0) eids_buffer += sdnv.sdnv_encode(0) continue (scheme, ssp) = eid.split(':') (node, service) = map(int,ssp.split('.')) eids_buffer += sdnv.sdnv_encode(node) eids_buffer += sdnv.sdnv_encode(service) return (eids_buffer, dict_buffer)
def encode(bundle): """Encodes all the bundle blocks, not including the preamble for the payload block or the payload data itself, into a binary string.""" data = '' #------------------------------ # Primary block #------------------------------ (eids_buffer, dict_buffer) = encode_eids(bundle) data += eids_buffer # Now append the creation time and expiration sdnvs, the # dictionary length, and the dictionary itself data += sdnv.sdnv_encode(int(bundle.creation_secs)) data += sdnv.sdnv_encode(bundle.creation_seqno) data += sdnv.sdnv_encode(bundle.expiration) data += sdnv.sdnv_encode(len(dict_buffer)) data += dict_buffer # Now fill in the preamble portion, including the version, # processing flags and whole length of the block preamble = struct.pack('B', VERSION) preamble += sdnv.sdnv_encode(bundle.bundle_flags | bundle.priority | bundle.srr_flags) preamble += sdnv.sdnv_encode(len(data)) return preamble + data
def tcpSend(data): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((config.DTN_Address, config.DTN_TCPPort)) #s.setblocking(0) connectData = "dtn!" #magic part connectData += chr(03) #version connectData += chr(0) #flags connectData += chr(0)+chr(10) #keepalive connectData += sdnv.sdnv_encode(len(proxy_eid)) connectData += proxy_eid s.send(connectData) s.recv(1024) s.send(data) response = s.recv(1024) if len(response)>0: print "Response: "+toHex(response) s.close() pplpl()
def serialize(self): self.buildDictionary() allBlocks = "" for block in self.blocks: if block.type != 1: allBlocks += chr(block.type) allBlocks += sdnv.sdnv_encode(block.controlFlags) # TODO: handle eid references.. allBlocks += sdnv.sdnv_encode(len(block.payload)) allBlocks += block.payload payloadBlock = chr(self.payloadBlock.type) payloadBlock += sdnv.sdnv_encode(self.payloadBlock.controlFlags) payloadBlock += sdnv.sdnv_encode(len(self.payloadBlock.payload)) payloadBlock += self.payloadBlock.payload primaryBlockContent = sdnv.sdnv_encode(self.primaryBlock.destSchemeOffset) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.destSSPOffset) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.srcSchemeOffset) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.srcSSPOffset) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.reportToSchemeOffset) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.reportToSSPOffset) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.custodianSchemeOffset) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.custodianSSPOffset) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.creationTimestampTime) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.creationTimestampSeq) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.lifetime) primaryBlockContent += sdnv.sdnv_encode(len(self.primaryBlock.dictionary)) primaryBlockContent += self.primaryBlock.dictionary # print toHex(self.primaryBlock.dictionary) # TODO: Only if bundle is a fragment.. """primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.fragmentOffset) print "fragment offset: "+toHex(sdnv.sdnv_encode(self.primaryBlock.fragmentOffset)) primaryBlockContent += sdnv.sdnv_encode(self.primaryBlock.dataUnitLength) print "data unit length: "+toHex(sdnv.sdnv_encode(self.primaryBlock.dataUnitLength))""" # sdnv.sdnv_encode(len(payloadBlock)) primaryBlock = self.primaryBlock.version primaryBlock += sdnv.sdnv_encode(self.primaryBlock.controlFlags) primaryBlockContentLength = sdnv.sdnv_encode(len(primaryBlockContent)) primaryBlock += primaryBlockContentLength data = chr(self.bundleType) data += sdnv.sdnv_encode(len(primaryBlock) + len(primaryBlockContent) + len(payloadBlock) + len(allBlocks)) # print "Segment size: "+str(len(primaryBlockContent)+len(payloadBlock)+len(primaryBlockContentLEngth)) data += primaryBlock data += primaryBlockContent data += allBlocks data += payloadBlock return data