Exemplo n.º 1
0
    def receive_datagram(self, args):
        """
        Handle messages coming from the udp socket.

        :param args: (data, client_address)
        """
        data, client_address = args

        logging.debug("receiving datagram")

        try:
            host, port = client_address
        except ValueError:
            host, port, tmp1, tmp2 = client_address

        client_address = (host, port)

        serializer = Serializer()
        message = serializer.deserialize(data, client_address)
        if isinstance(message, int):
            logger.error("receive_datagram - BAD REQUEST")
            rst = Message()
            rst.destination = client_address
            rst.type = Types["RST"]
            rst.code = message
            rst.mid = message.mid
            self.send_datagram(rst)
            return
        logger.debug("receive_datagram - " + str(message))

        if isinstance(message, Request):
            if not message.proxy_uri or message.uri_path != "coap2http":
                logger.error("receive_datagram - BAD REQUEST")
                rst = Message()
                rst.destination = client_address
                rst.type = Types["RST"]
                rst.code = Codes.BAD_REQUEST.number
                rst.mid = message.mid
                self.send_datagram(rst)
                return
            # Execute HTTP/HTTPS request
            http_response = CoAP_HTTP.execute_http_request(
                message.code, message.proxy_uri, message.payload)
            # HTTP response to CoAP response conversion
            coap_response = CoAP_HTTP.to_coap_response(http_response,
                                                       message.code,
                                                       client_address,
                                                       message.mid)
            # Send datagram and return
            self.send_datagram(coap_response)
            return

        elif isinstance(message, Message):
            logger.error("Received message from %s", message.source)

        else:  # is Response
            logger.error("Received response from %s", message.source)
Exemplo n.º 2
0
    def receive_datagram(self, args):
        """
        Handle messages coming from the udp socket.

        :param args: (data, client_address)
        """
        data, client_address = args

        logging.debug("receiving datagram")

        try:
            host, port = client_address
        except ValueError:
            host, port, tmp1, tmp2 = client_address

        client_address = (host, port)

        serializer = Serializer()
        message = serializer.deserialize(data, client_address)
        if isinstance(message, int):
            logger.error("receive_datagram - BAD REQUEST")
            rst = Message()
            rst.destination = client_address
            rst.type = Types["RST"]
            rst.code = message
            rst.mid = message.mid
            self.send_datagram(rst)
            return
        logger.debug("receive_datagram - " + str(message))

        if isinstance(message, Request):
            if not message.proxy_uri or message.uri_path != "coap2http":
                logger.error("receive_datagram - BAD REQUEST")
                rst = Message()
                rst.destination = client_address
                rst.type = Types["RST"]
                rst.code = Codes.BAD_REQUEST.number
                rst.mid = message.mid
                self.send_datagram(rst)
                return
            # Execute HTTP/HTTPS request
            http_response = CoAP_HTTP.execute_http_request(message.code, message.proxy_uri, message.payload)
            # HTTP response to CoAP response conversion
            coap_response = CoAP_HTTP.to_coap_response(http_response, message.code, client_address, message.mid)
            # Send datagram and return
            self.send_datagram(coap_response)
            return

        elif isinstance(message, Message):
            logger.error("Received message from %s", message.source)

        else:  # is Response
            logger.error("Received response from %s", message.source)
Exemplo n.º 3
0
    def listen(self, timeout=10):
        """
        Listen for incoming messages. Timeout is used to check if the server must be switched off.

        :param timeout: Socket Timeout in seconds
        """
        self._socket.settimeout(float(timeout))
        while not self.stopped.isSet():
            try:
                data, client_address = self._socket.recvfrom(4096)
                if len(client_address) > 2:
                    client_address = (client_address[0], client_address[1])
            except socket.timeout:
                continue
            try:
                serializer = Serializer()
                message = serializer.deserialize(data, client_address)
                if isinstance(message, int):
                    logger.error("receive_datagram - BAD REQUEST")

                    rst = Message()
                    rst.destination = client_address
                    rst.type = defines.Types["RST"]
                    rst.code = message
                    rst.mid = self._messageLayer._current_mid
                    self._messageLayer._current_mid += 1 % 65535
                    self.send_datagram(rst)
                    continue

                logger.debug("receive_datagram - " + str(message))
                if isinstance(message, Request):
                    transaction = self._messageLayer.receive_request(message)
                    if transaction.request.duplicated and transaction.completed:
                        logger.debug("message duplicated, transaction completed")
                        if transaction.response is not None:
                            self.send_datagram(transaction.response)
                        return
                    elif transaction.request.duplicated and not transaction.completed:
                        logger.debug("message duplicated, transaction NOT completed")
                        self._send_ack(transaction)
                        return
                    args = (transaction, )
                    t = threading.Thread(target=self.receive_request, args=args)
                    t.start()
                # self.receive_datagram(data, client_address)
                elif isinstance(message, Response):
                    logger.error("Received response from %s", message.source)

                else:  # is Message
                    transaction = self._messageLayer.receive_empty(message)
                    if transaction is not None:
                        with transaction:
                            self._blockLayer.receive_empty(message, transaction)
                            self._observeLayer.receive_empty(message, transaction)

            except RuntimeError:
                print "Exception with Executor"
        self._socket.close()
Exemplo n.º 4
0
def coap_encode(payload):
    message = Message()
    message.type = defines.Types['CON']
    message.token = 4321
    message.mid = 2
    message.options = None
    message.payload = str(payload)
    serializer = Serializer()
    messagestring = serializer.serialize(message)
    return messagestring
Exemplo n.º 5
0
 def cancel_observing(self, response, send_rst):  # pragma: no cover
     if send_rst:
         message = Message()
         message.destination = self.server
         message.code = defines.Codes.EMPTY.number
         message.type = defines.Types["RST"]
         message.token = response.token
         message.mid = response.mid
         self.protocol.send_message(message)
     self.stop()
Exemplo n.º 6
0
def coap_encode(payload):
    message = Message()
    message.type = defines.Types['CON']
    message.token = 4321
    message.mid = 2
    message.options = None
    message.payload = str(payload)
    serializer = Serializer()
    messagestring = serializer.serialize(message)
    return messagestring
Exemplo n.º 7
0
 def cancel_observing(self, response, send_rst):  # pragma: no cover
     if send_rst:
         message = Message()
         message.destination = self.server
         message.code = defines.Codes.EMPTY.number
         message.type = defines.Types["RST"]
         message.token = response.token
         message.mid = response.mid
         self.protocol.send_message(message)
     self.stop()
Exemplo n.º 8
0
 def to_coap_response(http_response, request_method, client_address, mid):
     coap_msg = Message()
     coap_msg.destination = client_address
     coap_msg.type = Types["ACK"]
     coap_msg.code = CoAP_HTTP.to_coap_code(http_response.status_code, request_method)
     coap_msg.mid = mid
     if 'Content-Type' in http_response.headers:
         coap_msg.content_type = CoAP_HTTP.to_coap_content_type(http_response.headers['Content-Type'].split(";")[0])
     else:
         coap_msg.content_type = 0
     coap_msg.payload = http_response.content
     return coap_msg
Exemplo n.º 9
0
 def to_coap_response(http_response, request_method, client_address, mid):
     coap_msg = Message()
     coap_msg.destination = client_address
     coap_msg.type = Types["ACK"]
     coap_msg.code = CoAP_HTTP.to_coap_code(http_response.status_code,
                                            request_method)
     coap_msg.mid = mid
     if 'Content-Type' in http_response.headers:
         coap_msg.content_type = CoAP_HTTP.to_coap_content_type(
             http_response.headers['Content-Type'].split(";")[0])
     else:
         coap_msg.content_type = 0
     coap_msg.payload = http_response.content
     return coap_msg
Exemplo n.º 10
0
    def cancel_observing(self, response, send_rst):  # pragma: no cover
        """
        Delete observing on the remote server.

        :param response: the last received response
        :param send_rst: if explicitly send RST message
        :type send_rst: bool
        """
        if send_rst:
            message = Message()
            message.destination = self.server
            message.code = defines.Codes.EMPTY.number
            message.type = defines.Types["RST"]
            message.token = response.token
            message.mid = response.mid
            self.protocol.send_message(message)
        self.stop()
Exemplo n.º 11
0
def main():  # pragma: no cover
    ip = "192.168.1.21"
    port = defines.COAP_DEFAULT_PORT
    multicast = False
    server = CoAPServer(ip, port, multicast)
    server.notify('/basic')
    try:
        # server.listen()
        while not server.stopped.isSet():
            data, client_address = server._socket.recvfrom(4096)
            try:
                serializer = Serializer()
                message = serializer.deserialize(data, client_address)
                if isinstance(message, int):
                    print("receive_datagram - BAD REQUEST")

                    rst = Message()
                    rst.destination = client_address
                    rst.type = defines.Types["RST"]
                    rst.code = message
                    rst.mid = server._messageLayer.fetch_mid()
                    server.send_datagram(rst)
                    continue

                # print("receive_datagram " + str(message.payload))
                database().insert_value('CoAP', datetime.datetime.now(),
                                        str(message.payload))

                if isinstance(message, Request):
                    transaction = server._messageLayer.receive_request(message)
                    if transaction.request.duplicated and transaction.completed:
                        print("message duplicated, transaction completed")
                        if transaction.response is not None:
                            server.send_datagram(transaction.response)
                        continue
                    elif transaction.request.duplicated and not transaction.completed:
                        print("message duplicated, transaction NOT completed")
                        server._send_ack(transaction)
                        continue
                    args = (transaction, )
                    t = threading.Thread(target=server.receive_request,
                                         args=args)
                    t.start()
                # self.receive_datagram(data, client_address)
                elif isinstance(message, Response):
                    print("Received response from %s", message.source)

                else:  # is Message
                    transaction = server._messageLayer.receive_empty(message)
                    if transaction is not None:
                        with transaction:
                            server._blockLayer.receive_empty(
                                message, transaction)
                            server._observeLayer.receive_empty(
                                message, transaction)

            except RuntimeError:
                print("Exception with Executor")
        server._socket.close()
    except KeyboardInterrupt:
        print("Server Shutdown")
        server.close()
        print("Exiting...")
Exemplo n.º 12
0
    def listen(self, timeout=10):
        """
        Listen for incoming messages. Timeout is used to check if the server must be switched off.

        :param timeout: Socket Timeout in seconds
        """
        self._socket.settimeout(float(timeout))
        while not self.stopped.isSet():
            try:
                data, client_address = self._socket.recvfrom(4096)
                if len(client_address) > 2:
                    client_address = (client_address[0], client_address[1])
            except socket.timeout:
                continue
            try:
                serializer = Serializer()
                message = serializer.deserialize(data, client_address)
                if isinstance(message, int):
                    logger.error("receive_datagram - BAD REQUEST")

                    rst = Message()
                    rst.destination = client_address
                    rst.type = defines.Types["RST"]
                    rst.code = message
                    rst.mid = self._messageLayer._current_mid
                    self._messageLayer._current_mid += 1 % 65535
                    self.send_datagram(rst)
                    continue

                logger.debug("receive_datagram - " + str(message))
                if isinstance(message, Request):
                    transaction = self._messageLayer.receive_request(message)
                    if transaction.request.duplicated and transaction.completed:
                        logger.debug(
                            "message duplicated, transaction completed")
                        if transaction.response is not None:
                            self.send_datagram(transaction.response)
                        continue
                    elif transaction.request.duplicated and not transaction.completed:
                        logger.debug(
                            "message duplicated, transaction NOT completed")
                        self._send_ack(transaction)
                        continue
                    args = (transaction, )
                    t = threading.Thread(target=self.receive_request,
                                         args=args)
                    t.start()
                # self.receive_datagram(data, client_address)
                elif isinstance(message, Response):
                    logger.error("Received response from %s", message.source)

                else:  # is Message
                    transaction = self._messageLayer.receive_empty(message)
                    if transaction is not None:
                        with transaction:
                            self._blockLayer.receive_empty(
                                message, transaction)
                            self._observeLayer.receive_empty(
                                message, transaction)

            except RuntimeError:
                print "Exception with Executor"
        self._socket.close()
Exemplo n.º 13
0
    def listen(self, timeout=10):
        """
        Listen for incoming messages. Timeout is used to check if the server must be switched off.

        :param timeout: Socket Timeout in seconds
        """
        self._socket.settimeout(float(timeout))
        empty = []
        while not self.stopped.isSet():
            try:
                data = None
                client_address = None
                readable, writable, exceptional = select.select(self._socketlist, empty, empty, timeout)
                for s in readable:
                    data, client_address = s.recvfrom(4096)
                    if len(client_address) > 2:
                        client_address = (client_address[0], client_address[1])
                        print ("listen : received ", client_address)
            except socket.timeout:
                print (".")
                continue
            except Exception as e:
                if self._cb_ignore_listen_exception is not None and isinstance(self._cb_ignore_listen_exception, collections.Callable):
                    if self._cb_ignore_listen_exception(e, self):
                        continue
                raise
            try:
                serializer = Serializer()
                if data is not None:
                    message = serializer.deserialize(data, client_address)
                
                    if isinstance(message, int):
                        logger.error("receive_datagram - BAD REQUEST")

                        rst = Message()
                        rst.destination = client_address
                        rst.type = defines.Types["RST"]
                        rst.code = message
                        rst.mid = self._messageLayer.fetch_mid()
                        self.send_datagram(rst)
                        continue

                    logger.debug("receive_datagram - " + str(message))
                    if isinstance(message, Request):
                        transaction = self._messageLayer.receive_request(message)
                        if transaction.request.duplicated and transaction.completed:
                            logger.debug("message duplicated, transaction completed")
                            if transaction.response is not None:
                                self.send_datagram(transaction.response)
                            continue
                        elif transaction.request.duplicated and not transaction.completed:
                            logger.debug("message duplicated, transaction NOT completed")
                            self._send_ack(transaction)
                            continue
                        args = (transaction, )
                        t = threading.Thread(target=self.receive_request, args=args)
                        t.start()
                    # self.receive_datagram(data, client_address)
                    elif isinstance(message, Response):
                        logger.error("Received response from %s", message.source)

                    else:  # is Message
                        transaction = self._messageLayer.receive_empty(message)
                        if transaction is not None:
                            with transaction:
                                self._blockLayer.receive_empty(message, transaction)
                                self._observeLayer.receive_empty(message, transaction)

            except RuntimeError:
                logger.exception("Exception with Executor")
        # close sockets
        for sock in self._socketlist:
            sock.close()
Exemplo n.º 14
0
    def listen(self, timeout=10):
        """
        Listen for incoming messages. Timeout is used to check if the server must be switched off.

        :param timeout: Socket Timeout in seconds
        """
        self._socket.settimeout(float(timeout))
        while not self.stopped.isSet():
            try:
                data, client_address = self._socket.recvfrom(4096)
                if len(client_address) > 2:
                    client_address = (client_address[0], client_address[1])
            except socket.timeout:
                continue
            except Exception as e:
                if self._cb_ignore_listen_exception is not None and callable(self._cb_ignore_listen_exception):
                    if self._cb_ignore_listen_exception(e, self):
                        continue
                raise
            try:
                serializer = Serializer()
                message = serializer.deserialize(data, client_address)
                if isinstance(message, int):
                    logger.error("receive_datagram - BAD REQUEST")

                    rst = Message()
                    rst.destination = client_address
                    rst.type = defines.Types["RST"]
                    rst.code = message
                    rst.mid = self._messageLayer.fetch_mid()
                    self.send_datagram(rst)
                    continue

                logger.debug("receive_datagram - " + str(message))
                if isinstance(message, Request):
                    transaction = self._messageLayer.receive_request(message)
                    if transaction.request.duplicated and transaction.completed:
                        logger.debug("message duplicated, transaction completed")
                        if transaction.response is not None:
                            self.send_datagram(transaction.response)
                        continue
                    elif transaction.request.duplicated and not transaction.completed:
                        logger.debug("message duplicated, transaction NOT completed")
                        self._send_ack(transaction)
                        continue
                    args = (transaction, )
                    t = threading.Thread(target=self.receive_request, args=args)
                    t.start()
                # self.receive_datagram(data, client_address)
                elif isinstance(message, Response):
                    logger.error("Received response from %s", message.source)

                else:  # is Message
                    transaction = self._messageLayer.receive_empty(message)
                    if transaction is not None:
                        with transaction:
                            self._blockLayer.receive_empty(message, transaction)
                            self._observeLayer.receive_empty(message, transaction)
            except Exception as e:
                logger.error("Exception received in listen")
                logger.exception(traceback.format_exc())
                continue
            except RuntimeError:
                logger.exception("Exception with Executor")
        try:
            # Python does not close the OS FD on socket.close()
            # Ensure OS socket is closed with shutdown to prevent FD leak
            self._socket.shutdown(socket.SHUT_RDWR)
        except socket.error:
            pass
        self._socket.close()