예제 #1
0
파일: test_utils.py 프로젝트: Delsan/coap
def test_ipv6AddrString2Bytes(logFixture, validIpv6AddrStr2Bytes):
    
    (ipv6String,ipv6Bytes) = validIpv6AddrStr2Bytes
    ipv6Bytes = list(ipv6Bytes)
    
    log.debug('ipv6String: {0}'.format(ipv6String))
    log.debug('ipv6Bytes:  {0}'.format(coapUtils.formatBuf(ipv6Bytes)))
    
    result = coapUtils.ipv6AddrString2Bytes(ipv6String)
    
    log.debug('result:     {0}'.format(coapUtils.formatBuf(result)))
    
    assert result==ipv6Bytes
예제 #2
0
def test_ipv6AddrString2Bytes(logFixture, validIpv6AddrStr2Bytes):

    (ipv6String, ipv6Bytes) = validIpv6AddrStr2Bytes
    ipv6Bytes = list(ipv6Bytes)

    log.debug('ipv6String: {0}'.format(ipv6String))
    log.debug('ipv6Bytes:  {0}'.format(coapUtils.formatBuf(ipv6Bytes)))

    result = coapUtils.ipv6AddrString2Bytes(ipv6String)

    log.debug('result:     {0}'.format(coapUtils.formatBuf(result)))

    assert result == ipv6Bytes
예제 #3
0
    def _receiveFromCoAP(self, timestamp, sender, data):
        '''
        Receive CoAP response and forward it to the mesh network.
        Appends UDP and IPv6 headers to the CoAP message and forwards it on the Eventbus towards the mesh.
        '''
        self.coapClient.close()

        # UDP
        udplen = len(data) + 8

        udp = u.int2buf(self.coapClient.udpPort,2)  # src port
        udp += u.int2buf(sender[1],2) # dest port
        udp += [udplen >> 8, udplen & 0xff]  # length
        udp += [0x00, 0x00]  # checksum
        udp += data

        # destination address of the packet is CoAP client's IPv6 address (address of the mote)
        dstIpv6Address = u.ipv6AddrString2Bytes(self.coapClient.ipAddress)
        assert len(dstIpv6Address)==16
        # source address of the packet is DAG root's IPV6 address
        # use the same prefix (link-local or global) as in the destination address
        srcIpv6Address = dstIpv6Address[:8]
        srcIpv6Address += self.dagRootEui64
        assert len(srcIpv6Address)==16

        # CRC See https://tools.ietf.org/html/rfc2460.

        udp[6:8] = openvisualizer.openvisualizer_utils.calculatePseudoHeaderCRC(
            src=srcIpv6Address,
            dst=dstIpv6Address,
            length=[0x00, 0x00] + udp[4:6],
            nh=[0x00, 0x00, 0x00, 17], # UDP as next header
            payload=data,
        )

        # IPv6
        ip = [6 << 4]  # v6 + traffic class (upper nybble)
        ip += [0x00, 0x00, 0x00]  # traffic class (lower nibble) + flow label
        ip += udp[4:6]  # payload length
        ip += [17]  # next header (protocol); UDP=17
        ip += [64]  # hop limit (pick a safe value)
        ip += srcIpv6Address  # source
        ip += dstIpv6Address  # destination
        ip += udp

        # announce network prefix
        self.dispatch(
            signal        = 'v6ToMesh',
            data          = ip
        )
예제 #4
0
    def _messageNotification(self, signal, sender, data):

        timestamp = time.time()

        # log
        log.debug("{0}:{1}->{2}:{3}: {4}".format(
            sender[0],
            sender[1],
            signal[0],
            signal[1],
            u.formatBuf(data),
        ))

        srcIp = u.ipv6AddrString2Bytes(sender[0])
        srcPort = sender[1]
        destIp = u.ipv6AddrString2Bytes(signal[0])
        destPort = signal[1]

        # log in pcap
        self._writePcapMessage(timestamp, srcIp, destIp, srcPort, destPort,
                               data)

        # release the lock
        self.gotMsgSem.release()
예제 #5
0
 def _messageNotification(self,signal,sender,data):
     
     timestamp  = time.time()
     
     # log
     log.debug("{0}:{1}->{2}:{3}: {4}".format(
             sender[0],
             sender[1],
             signal[0],
             signal[1],
             u.formatBuf(data),
         )
     )
     
     srcIp      = u.ipv6AddrString2Bytes(sender[0])
     srcPort    = sender[1]
     destIp     = u.ipv6AddrString2Bytes(signal[0])
     destPort   = signal[1]
     
     # log in pcap
     self._writePcapMessage(timestamp,srcIp,destIp,srcPort,destPort,data)
     
     # release the lock
     self.gotMsgSem.release()
예제 #6
0
    def _receiveFromCoAP(self, timestamp, sender, data):
        '''
        Receive CoAP response and forward it to the mesh network.
        Appends UDP and IPv6 headers to the CoAP message and forwards it on the Eventbus towards the mesh.
        '''
        self.coapClient.close()

        # UDP
        udplen = len(data) + 8

        udp = u.int2buf(sender[1], 2)  # src port
        udp += u.int2buf(self.coapClient.udpPort, 2)  # dest port
        udp += [udplen >> 8, udplen & 0xff]  # length
        udp += [0x00, 0x00]  # checksum
        udp += data

        # destination address of the packet is CoAP client's IPv6 address (address of the mote)
        dstIpv6Address = u.ipv6AddrString2Bytes(self.coapClient.ipAddress)
        assert len(dstIpv6Address) == 16
        # source address of the packet is DAG root's IPV6 address
        # use the same prefix (link-local or global) as in the destination address
        srcIpv6Address = dstIpv6Address[:8]
        srcIpv6Address += self.dagRootEui64
        assert len(srcIpv6Address) == 16

        # CRC See https://tools.ietf.org/html/rfc2460.

        udp[6:
            8] = openvisualizer.openvisualizer_utils.calculatePseudoHeaderCRC(
                src=srcIpv6Address,
                dst=dstIpv6Address,
                length=[0x00, 0x00] + udp[4:6],
                nh=[0x00, 0x00, 0x00, 17],  # UDP as next header
                payload=udp,
            )

        # IPv6
        ip = [6 << 4]  # v6 + traffic class (upper nybble)
        ip += [0x00, 0x00, 0x00]  # traffic class (lower nibble) + flow label
        ip += udp[4:6]  # payload length
        ip += [17]  # next header (protocol); UDP=17
        ip += [64]  # hop limit (pick a safe value)
        ip += srcIpv6Address  # source
        ip += dstIpv6Address  # destination
        ip += udp

        # announce network prefix
        self.dispatch(signal='v6ToMesh', data=ip)
예제 #7
0
def ipv6ToHexArray(ipv6):
    return coapUtils.ipv6AddrString2Bytes(ipv6)