示例#1
0
    def __init__(self, protocol, underlying_address):
        from DTLSSocket import dtls

        self._protocol = protocol
        self._underlying_address = simplesocketserver._Address(
            protocol, underlying_address)

        self._dtls_socket = None

        self._psk_store = SecurityStore(protocol._server_credentials)

        self._dtls_socket = dtls.DTLS(
            # FIXME: Use accessors like tinydtls (but are they needed? maybe shutdown sequence is just already better here...)
            read=self._read,
            write=self._write,
            event=self._event,
            pskId=b"The socket needs something there but we'll never use it",
            pskStore=self._psk_store,
        )
        self._dtls_session = dtls.Session(_SENTINEL_ADDRESS, _SENTINEL_PORT)

        self._retransmission_task = asyncio.create_task(
            self._run_retransmissions(),
            **py38args(name="DTLS server handshake retransmissions"))

        self.log = protocol.log
示例#2
0
    async def _start(self):
        from DTLSSocket import dtls

        self._dtls_socket = None

        self._connection = None

        try:
            self._transport, _ = await self.coaptransport.loop.create_datagram_endpoint(
                self.SingleConnection.factory(self),
                remote_addr=(self._host, self._port),
            )

            self._dtls_socket = dtls.DTLS(
                read=self._build_accessor("_read"),
                write=self._build_accessor("_write"),
                event=self._build_accessor("_event"),
                pskId=self._pskId,
                pskStore={self._pskId: self._psk},
            )
            self._connection = self._dtls_socket.connect(
                _SENTINEL_ADDRESS, _SENTINEL_PORT)

            self._retransmission_task = asyncio.Task(
                self._run_retransmissions())

            self._connecting = asyncio.Future()
            await self._connecting

            queue = self._queue
            self._queue = None

            for message in queue:
                # could be a tad more efficient by stopping the retransmissions
                # in a go, then doing just the punch line and then starting it,
                # but practically this will be a single thing most of the time
                # anyway
                self.send(message)

            return

        except OSError as e:
            self.log.debug("Expressing exception %r as errno %d.", e, e.errno)
            self.coaptransport.ctx.dispatch_error(e.errno, self)
        except asyncio.CancelledError:
            # Can be removed starting with Python 3.8 as it's a workaround for
            # https://bugs.python.org/issue32528
            raise
        except Exception as e:
            self.log.error(
                "Exception %r can not be represented as errno, setting -1.", e)
            self.coaptransport.ctx.dispatch_error(-1, self)
        finally:
            if self._queue is None:
                # all worked, we're done here
                return

            self.shutdown()
示例#3
0
文件: tinydtls.py 项目: ltn22/aiocoap
    async def _run(self, connect_immediately):
        from DTLSSocket import dtls

        self._dtls_socket = None

        if not connect_immediately:
            await self._queue.peek()

        self._connection = None

        try:
            self._transport, singleconnection = await self.coaptransport.loop.create_datagram_endpoint(
                self.SingleConnection.factory(self),
                remote_addr=(self._host, self._port),
            )

            self._dtls_socket = dtls.DTLS(
                read=self._read,
                write=self._write,
                event=self._event,
                pskId=self._pskId,
                pskStore={self._pskId: self._psk},
            )
            self._connection = self._dtls_socket.connect(
                _SENTINEL_ADDRESS, _SENTINEL_PORT)

            self._retransmission_task = asyncio.Task(
                self._run_retransmissions())

            self._connecting = asyncio.Future()

            await self._connecting

            while True:
                message = await self._queue.get()
                self._retransmission_task.cancel()
                self._dtls_socket.write(self._connection, message)
                self._retransmission_task = asyncio.Task(
                    self._run_retransmissions())
        except OSError as e:
            self.log.debug("Expressing exception %r as errno %d.", e, e.errno)
            self.coaptransport.ctx.dispatch_error(e.errno, self)
        except Exception as e:
            self.log.error(
                "Exception %r can not be represented as errno, setting -1.", e)
            self.coaptransport.ctx.dispatch_error(-1, self)
        finally:
            if self._connection is not None:
                try:
                    self._dtls_socket.close(self._connection)
                except:
                    pass  # _dtls_socket actually does raise an empty Exception() here
                self._retransmission_task.cancel()
            # doing this here allows the dtls socket to send a final word, but
            # by closing this, we protect the nascent next connection from any
            # delayed ICMP errors that might still wind up in the old socket
            self._transport.close()
示例#4
0
    dst = 0
    for cmsg_level, cmsg_type, cmsg_data in ancdata:
        if (cmsg_level == socket.IPPROTO_IPV6
                and cmsg_type == socket.IPV6_PKTINFO):
            if cmsg_data[0] == 0xFF:
                dst = socket.inet_ntop(socket.AF_INET6, cmsg_data[:16])
                mc = True
    d.handleMessageAddr(src[0], src[1], data)


#dtls.setLogLevel(dtls.DTLS_LOG_DEBUG)
print("Log Level:", dtls.dtlsGetLogLevel())

d = dtls.DTLS(read=read,
              write=write,
              event=event,
              pskId=b"Client_identity",
              pskStore={b"Client_identity": b"secretPSK"})

pprint("connect:")
s = d.connect("::1", 20220)

#block till connected
while lastEvent != 0x1de:
    querySock(sock, d)

pprint("try to send data")
print("try write:", d.write(s, b"Test!\n"))

pprint("answer:")
querySock(sock, d)