示例#1
0
def buildMessage(msgtype, token, code, messageId, options=[], payload=[]):
    assert msgtype in d.TYPE_ALL
    assert code in d.METHOD_ALL + d.COAP_RC_ALL

    message = []

    # determine token length
    TKL = None
    for tokenLen in range(1, 8 + 1):
        if token < (1 << (8 * tokenLen)):
            TKL = tokenLen
            break
    if not TKL:
        raise ValueError('token {0} too long'.format(token))

    # header
    message += [d.COAP_VERSION << 6 | msgtype << 4 | TKL]
    message += [code]
    message += u.int2buf(messageId, 2)
    message += u.int2buf(token, TKL)

    # options
    options = sortOptions(options)
    lastOptionNum = 0
    for option in options:
        assert option.optionNumber >= lastOptionNum
        message += option.toBytes(lastOptionNum)
        lastOptionNum = option.optionNumber

    # payload
    if payload:
        message += [d.COAP_PAYLOAD_MARKER]
        message += payload

    return message
示例#2
0
def buildMessage(msgtype,
                 token,
                 code,
                 messageId,
                 options=[],
                 payload=[],
                 securityContext=None,
                 partialIV=None):
    assert msgtype in d.TYPE_ALL
    assert code in d.METHOD_ALL + d.COAP_RC_ALL

    message = []

    TKL = 0
    if token:
        # determine token length
        for tokenLen in range(1, 8 + 1):
            if token < (1 << (8 * tokenLen)):
                TKL = tokenLen
                break
        if not TKL:
            raise ValueError('token {0} too long'.format(token))

    # header
    message += [d.COAP_VERSION << 6 | msgtype << 4 | TKL]
    message += [code]
    message += u.int2buf(messageId, 2)
    message += u.int2buf(token, TKL)

    # options
    options = sortOptions(options)

    if securityContext:
        # invoke oscoap to protect the message
        (outerOptions,
         newPayload) = oscoap.protectMessage(context=securityContext,
                                             version=d.COAP_VERSION,
                                             code=code,
                                             options=options,
                                             payload=payload,
                                             partialIV=partialIV)
    else:
        (outerOptions, newPayload) = (options, payload)

    # add encoded options
    message += encodeOptions(outerOptions)

    # add payload
    message += encodePayload(newPayload)

    return message
示例#3
0
def getRequestSecurityParams(objectSecurityOption):
    if objectSecurityOption:
        context = objectSecurityOption.context
        newSequenceNumber = objectSecurityOption.context.getSequenceNumber()
        # convert sequence number to string that is the length of the IV
        newSequenceNumber = u.buf2str(u.int2buf(newSequenceNumber, context.aeadAlgorithm.ivLength))
        return (context, newSequenceNumber)
    else:
        return (None, None)
示例#4
0
def buildMessage(type, token, code, messageId, options=[], payload=[]):
    assert type in d.TYPE_ALL
    assert code in d.METHOD_ALL + d.COAP_RC_ALL

    message = []

    # determine token length
    TKL = None
    for tokenLen in range(1, 8 + 1):
        if token < (1 << (8 * tokenLen)):
            TKL = tokenLen
            break
    if not TKL:
        raise ValueError("token {0} too long".format(token))

    # header
    message += [d.COAP_VERSION << 6 | type << 4 | TKL]
    message += [code]
    message += u.int2buf(messageId, 2)
    message += u.int2buf(token, TKL)

    # options
    options = sortOptions(options)
    lastOptionNum = 0
    for option in options:
        assert option.optionNumber >= lastOptionNum
        message += option.toBytes(lastOptionNum)
        lastOptionNum = option.optionNumber

    # payload
    if payload:
        message += [d.COAP_PAYLOAD_MARKER]
    message += payload

    print "sent message {0}".format(",".join(str(c) for c in message))

    return message
示例#5
0
    def toBytes(self, lastOptionNum):

        payload = self.getPayloadBytes()
        delta = self.optionNumber - lastOptionNum

        # optionDelta and optionDeltaExt fields
        if delta <= 12:
            optionDelta = delta
            optionDeltaExt = u.int2buf(delta, 0)
        elif delta <= (0xff + 13):
            optionDelta = 13
            optionDeltaExt = u.int2buf(delta - 13, 1)
        elif delta <= (0xffff + 269):
            optionDelta = 14
            optionDeltaExt = u.int2buf(delta - 269, 2)
        else:
            raise ValueError('delta is too large: {0}'.format(delta))

        # optionLength and optionLengthExt fields
        if len(payload) <= 12:
            optionLength = len(payload)
            optionLengthExt = u.int2buf(len(payload), 0)
        elif len(payload) <= (0xff + 13):
            optionLength = 13
            optionLengthExt = u.int2buf(len(payload) - 13, 1)
        elif len(payload) <= (0xffff + 269):
            optionLength = 14
            optionLengthExt = u.int2buf(len(payload) - 269, 2)
        else:
            raise ValueError('payload is too long, {0} bytes'.format(
                len(payload)))

        returnVal = []
        returnVal += [optionDelta << 4 | optionLength]
        returnVal += optionDeltaExt
        returnVal += optionLengthExt
        returnVal += payload

        return returnVal
示例#6
0
 def toBytes(self,lastOptionNum):
     
     payload    = self.getPayloadBytes()
     delta      = self.optionNumber-lastOptionNum
     
     # optionDelta and optionDeltaExt fields
     if   delta<=12:
         optionDelta      = delta
         optionDeltaExt   = u.int2buf(    delta,0)
     elif delta<=(0xff+13):
         optionDelta      = 13
         optionDeltaExt   = u.int2buf( delta-13,1)
     elif delta<=(0xffff+269):
         optionDelta      = 14
         optionDeltaExt   = u.int2buf(delta-269,2)
     else:
         raise ValueError('delta is too large: {0}'.format(delta))
     
     # optionLength and optionLengthExt fields
     if   len(payload)<=12:
         optionLength     = len(payload)
         optionLengthExt  = u.int2buf(    len(payload),0)
     elif len(payload)<=(0xff+13):
         optionLength     = 13
         optionLengthExt  = u.int2buf( len(payload)-13,1)
     elif len(payload)<=(0xffff+269):
         optionLength     = 14
         optionLengthExt  = u.int2buf(len(payload)-269,2)
     else:
         raise ValueError('payload is too long, {0} bytes'.format(len(payload)))
     
     returnVal  = []
     returnVal += [optionDelta<<4 | optionLength]
     returnVal += optionDeltaExt
     returnVal += optionLengthExt
     returnVal += payload
     
     return returnVal