Пример #1
0
 def send_command(self, header, body):
     log("Trying to send a command from the processor [header:%s, body:%s]" % (header, body), DEBUG)
     code = self.__get_code(header)
     log("Code is [%s]" % code, DEBUG)
     if code == self.READ_COMMAND_CODE:
         log("Return a read command with value FF", DEBUG)
         return "%s=FF" % body
     log("Unsupported code [%s] for now" % code, WARNING)
     return None
Пример #2
0
 def is_valid_helo_command(self, command):
     log("Validating format for helo with command [%s]" % command, DEBUG)
     match = self.helo_cmd.match(command)
     if match:
         log("Command [%s] is a valid helo command" % command, DEBUG)
         return True
     log("Command [%s] is not a valid helo command" % command, DEBUG)
     return False
Пример #3
0
 def is_valid_test_command(self, command):
     log("Validating format for test command [%s]" % command, DEBUG)
     match = self.test_cmd.match(command)
     if match:
         log("Match detected for test command [%s]" % command, DEBUG)
         return True
     log("Validating error for test command [%s]" % command, DEBUG)
     return False
Пример #4
0
 def is_valid_send_command(self, command):
     log("Validating format for send with command [%s]" % command, DEBUG)
     match = self.send_cmd.match(command)
     if match:
         log("Command [%s] is a valid send command" % command, DEBUG)
         return True
     log("Command [%s] is not a valid send command" % command, DEBUG)
     return False
Пример #5
0
 def is_valid_helo_command(self, command):
     log("Validating format for helo with command [%s]" % command, DEBUG)
     match = self.helo_cmd.match(command)
     if match:
         log("Command [%s] is a valid helo command" % command, DEBUG)
         return True
     log("Command [%s] is not a valid helo command" % command, DEBUG)
     return False
Пример #6
0
 def is_valid_command(self, command):
     log("Validating generic format for command [%s]" % command, DEBUG)
     match = self.general_cmd.match(command)
     if match:
         log("Match detected for command [%s]" % command, DEBUG)
         return True
     log("Validating error for command [%s]" % command, DEBUG)
     return False
Пример #7
0
 def is_valid_unwatch_command(self, command):
     log("Validating format for unwatch with command [%s]" % command, DEBUG)
     match = self.unwatch_cmd.match(command)
     if match:
         log("Command [%s] is a valid unwatch command" % command, DEBUG)
         return True
     log("Command [%s] is not a valid unwatch command" % command, DEBUG)
     return False
Пример #8
0
 def is_valid_send_command(self, command):
     log("Validating format for send with command [%s]" % command, DEBUG)
     match = self.send_cmd.match(command)
     if match:
         log("Command [%s] is a valid send command" % command, DEBUG)
         return True
     log("Command [%s] is not a valid send command" % command, DEBUG)
     return False
Пример #9
0
 def is_valid_test_command(self, command):
     log("Validating format for test command [%s]" % command, DEBUG)
     match = self.test_cmd.match(command)
     if match:
         log("Match detected for test command [%s]" % command, DEBUG)
         return True
     log("Validating error for test command [%s]" % command, DEBUG)
     return False
Пример #10
0
 def is_valid_command(self, command):
     log("Validating generic format for command [%s]" % command, DEBUG)
     match = self.general_cmd.match(command)
     if match:
         log("Match detected for command [%s]" % command, DEBUG)
         return True
     log("Validating error for command [%s]" % command, DEBUG)
     return False
Пример #11
0
 def is_valid_unwatch_command(self, command):
     log("Validating format for unwatch with command [%s]" % command, DEBUG)
     match = self.unwatch_cmd.match(command)
     if match:
         log("Command [%s] is a valid unwatch command" % command, DEBUG)
         return True
     log("Command [%s] is not a valid unwatch command" % command, DEBUG)
     return False
Пример #12
0
def configure_and_start_server(config, options=None):
    """
    Configure and start the server.
    Command line options always take advantage over configuration read from file.
    The file is a way to define default command line options

    :param config: Configuration read from file
    :param options: option read from command line
    """

    ################################################################################################
    #                                           socket init                                        #
    ################################################################################################
    s = socket()                                    # Create a socket object
    #host = gethostname()            th                # Get local machine name
    host = gethostbyname(config['server_address'])
    #s.bind((host, int(config['port'])))             # Bind to the port
    s.bind((host, int(config['port'])))
    buffer_size = int(config['buffer_size'])        # sizing the buffer for reading from socket
    max_connection = int(config['max_connection'])  # define the max concurrent connection supported by the server

    s.listen(max_connection)      # Now wait for client connection.
    liblogging.log("Server is now waiting for connection", liblogging.INFO)

    ################################################################################################
    #                                           socket start                                       #
    ################################################################################################
    while True:
        c, addr = s.accept()     # Establish connection with client.
        liblogging.log("Got connection from (%s, %s)" % addr)
        c.send("cE %s\n" % config['msg_banner'])
        client_thread = dispatcher.Dispatcher(c, addr, buffer_size, options)
        client_thread.setDaemon(True)
        client_thread.start()

        #thread.start_new_thread(handler, (c, addr, buffer_size))
        # c.close()                # Close the connection
Пример #13
0
def check_server(address, port):
    """

    :param address: address to check
    :param port: port to connect to
    :return: true if address is ready to use false otherwise
    """
    s = socket()
    liblogging.log("Checking local on port [%s:%d]" % (address, port), liblogging.INFO)
    try:
        s.connect((address, port))
        # we are able to connect on this port, it means that a server already listening on it ... cannot start the server
        liblogging.log(
            "Failure port [%s:%d] is not ready [error : %s]" % (address, port, 'Already listen on the requested port'), liblogging.WARNING)
        return False
    except error, e:
        liblogging.log("OK local port [%s:%d] is ready to use" % (address, port), liblogging.INFO)
        return True
Пример #14
0
    def manage_watch_stack(self, tasks):
        """
        Add or remove a watched address / list of addresses from the stack of watched address

        :param tasks: list of task which define the command => addresses concerned by the current job
        """
        self.watch_stack_lock.acquire()

        for task in tasks:
            group_address = task.group_address
            if group_address in self.watch_stack and task.command == libkonext.UNWATCH:
                liblogging.log("Removing address [%s] into the watch stack" % group_address, liblogging.DEBUG)
                self.watch_stack.remove(group_address)
            elif group_address not in self.watch_stack and task.command == libkonext.WATCH:
                liblogging.log("Adding address [%s] into the watch stack" % group_address, liblogging.DEBUG)
                self.watch_stack.append(group_address)
            else:
                liblogging.log("Nothing to do with the val [%s] into the watch stack" % group_address, liblogging.DEBUG)

        self.watch_stack_lock.release()
Пример #15
0
    def watch(self):
        resp_len = self.eibd_connection.EIBGetGroup_Src(self.buff, self.src, self.dest)
        resp = self.buff.buffer

        if resp_len == -1:
            log("Read failed", CRITICAL)
        elif resp_len < 2:
            log("Ivalid packet", ERROR)
        elif resp[0] & 0x3 or (resp[1] & 0xC0) == 0xC0:
            log("Unknown APDU received", ERROR)
        else:
            # manage response type
            resp_kind = resp[1] & 0xC0
            if resp_kind == self.KNX_READ_FLAG:
                # read ...
                log("Read datagram handled", INFO)
            elif resp_kind == self.KNX_RESPONSE_FLAG or resp_kind == self.KNX_WRITE_FLAG:
                if resp_kind == self.KNX_WRITE_FLAG:
                    log("Write datagram handled", INFO)
                else:
                    log("Response datagram handled", INFO)

                # do response processing ...
                physical_address = self.parser._decode_physical_addr(self.src.data)
                print "addr phy %s" % physical_address
                group_address = self.parser._decode_group_addr(self.dest.data)
                print "addr group %s" % group_address
                #response_val = None
                resp_str = self.parser.format_result(resp)
                print "Response is : %s" % resp_str
            else:
                log("Unknown datagram handled", WARNING)

        return resp
Пример #16
0
    def init_watcher(self):
        log("Initialize EIBD listening process EIBOpen_GroupSocket_async", DEBUG)
        self.eibd_connection.EIBOpen_GroupSocket_async(0)

        #
        # Forget the cache processing for now, it was a test
        #
        #if not self.cache_ok:
            # self.eibd_connection.EIB_Cache_Enable()
        #    self.cache_ok = True

        log("Initialize EIBD buffer", DEBUG)
        self.buff = EIBConnection.EIBBuffer()
        log("EIBD buffer initialized", DEBUG)
        log("Initialize EIBD src addr", DEBUG)
        self.src = EIBConnection.EIBAddr()
        log("EIBD src addr initialized", DEBUG)
        log("Initialize EIBD dest addr", DEBUG)
        self.dest = EIBConnection.EIBAddr()
        log("EIBD dest addr initialized", DEBUG)
        self.watch_initialized = True
Пример #17
0
 def log(self, message, level=liblogging.INFO):
     liblogging.log(message, level)
Пример #18
0
def handler(client_socket, client_address, buffer_size=1024):
    """
    This method will run for each client. It is a thread callback,
    It means that it is execute inside a thread.

    This thread should have many references to be able to dial with the client.

    :param clientsock: Reference to the client socket
    :param addr: address of the socket.
    """

    parser = Parser()               # define the parser for the thread
    processor = Processor()         # define the process launcher for the thread ...
    processor.set_parser(parser)    # ... and configure it
    name = "anonymous"              # define the name of the client
    logged_in = False               # tag user as not logged in (according to the protocol

    while 1:
        data = client_socket.recv(buffer_size)
        if not data or data.rstrip() == 'quit':     # hang off in this case
            break

        #### command validation ... ####
        command = data.rstrip()
        liblogging.log("Receive command [%s] from %s" % (command, client_address), liblogging.DEBUG)
        if not parser.is_valid_command(command):
            msg = "nE E22, \"%s: Invalid argument\"\n" % command
            liblogging.log("Invalid command [%s] received from[ %s]" % (command, client_address), liblogging.WARNING)
            client_socket.send(msg)
            continue

        # command parsing process
        header = libkonext.get_header(command)
        liblogging.log("Receive header [%s] from %s" % (header, client_address), liblogging.DEBUG)

        # first, check if the login command has been sent
        if not logged_in and header != libkonext.HELO_PREFIX:
            msg = "nE E13, \"%s: Permission denied\"\n" % command
            liblogging.log("Permission denied for %s, client not connected" % client_address, liblogging.WARNING)
            client_socket.send(msg)
            continue

        # check if the command is a valid helo command
        if not logged_in and header == libkonext.HELO_PREFIX and not parser.is_valid_helo_command(command):
            msg = "nE E22, \"%s: Invalid argument\"\n" % command
            liblogging.log("Invalid helo command [%s] received from[ %s]" % (command, client_address), liblogging.WARNING)
            client_socket.send(msg)
            continue

        #### end of command validation ... ####

        #### command processing ####

        # log in client
        if header == libkonext.HELO_PREFIX:
            name = libkonext.get_client_name(command)
            liblogging.log("The client %s is now connected with %s" % (client_address, name), liblogging.INFO)
            msg = "%s\n" % (libkonext.get_ack(header) % name)
            client_socket.send(msg)
            logged_in = True
        else:
            try:
                # manage command case ...
                response = libkonext.send_command(command, processor, parser)
                client_socket.send(response)
                continue
            except Exception:
                msg = "nE E22, \"%s: Invalid argument\"\n" % command
                liblogging.log("Invalid command [%s] received from[ %s]" % (command, client_address), liblogging.WARNING)
                client_socket.send(msg)

        # try:
        #     msg = "%s\n" % (libkonext.get_ack(header) % name)
        # except TypeError:
        #     msg = "%s\n" % libkonext.get_ack(header)
        #
        # client_socket.send(msg)


    #### Connection closing ####
    header = libkonext.get_header(libkonext.BYE_ACK)
    msg = "%s %s\n" %(header, name)
    client_socket.send(msg)
    client_socket.close()
    liblogging.log("Connection closed from (%s,%s)." % client_address)
Пример #19
0
import liblogging
from flashpolicyd import policy_server


if __name__ == '__main__':

    print "Trying to start the flash policy daemon"
    server = policy_server(843, './flashpolicy.xml')
    server.start()

    config = setup_config("konext")
    port = int(config['port'])                  # Port to listening on
    address = config['server_address']          # Address to listening on
    app_name = config['app_name']               # Name of the application

    # initialize the logger
    liblogging.logger = init_logger(config['format'],
                                    app_name,
                                    config['log_file'],
                                    config['max_logfile_size'],
                                    config['nb_logfile'])

    # checking availability ...
    if not socket_engine.check_server(address, port):
        liblogging.log("Port or address is busy. Please refer to logs to know more about the failure", liblogging.ERROR)
        sys.exit(1)     # exiting due to error ...

    mark_pid_on_fs()
    liblogging.log("Server starting with pid [%d]" % os.getpid())
    socket_engine.configure_and_start_server(config)
Пример #20
0
import liblogging
from flashpolicyd import policy_server

if __name__ == '__main__':

    print "Trying to start the flash policy daemon"
    server = policy_server(843, './flashpolicy.xml')
    server.start()

    config = setup_config("konext")
    port = int(config['port'])  # Port to listening on
    address = config['server_address']  # Address to listening on
    app_name = config['app_name']  # Name of the application

    # initialize the logger
    liblogging.logger = init_logger(config['format'], app_name,
                                    config['log_file'],
                                    config['max_logfile_size'],
                                    config['nb_logfile'])

    # checking availability ...
    if not socket_engine.check_server(address, port):
        liblogging.log(
            "Port or address is busy. Please refer to logs to know more about the failure",
            liblogging.ERROR)
        sys.exit(1)  # exiting due to error ...

    mark_pid_on_fs()
    liblogging.log("Server starting with pid [%d]" % os.getpid())
    socket_engine.configure_and_start_server(config)