Пример #1
0
    def open(name):
        """Attempts to start listening for remote stream connections.  'name'
        is a connection name in the form "TYPE:ARGS", where TYPE is an passive
        stream class's name and ARGS are stream class-specific. Currently the
        supported values for TYPE are "punix" and "ptcp".

        Returns (error, pstream): on success 'error' is 0 and 'pstream' is the
        new PassiveStream, on failure 'error' is a positive errno value and
        'pstream' is None."""
        if not PassiveStream.is_valid_name(name):
            return errno.EAFNOSUPPORT, None

        bind_path = name[6:]
        if name.startswith("punix:"):
            bind_path = ovs.util.abs_file_name(ovs.dirs.RUNDIR, bind_path)
            if sys.platform != 'win32':
                error, sock = ovs.socket_util.make_unix_socket(
                    socket.SOCK_STREAM, True, bind_path, None)
                if error:
                    return error, None
            else:
                # Branch used only on Windows
                try:
                    open(bind_path, 'w').close()
                except:
                    return errno.ENOENT, None

                pipename = winutils.get_pipe_name(bind_path)
                if len(pipename) > 255:
                    # Return invalid argument if the name is too long
                    return errno.ENOENT, None

                npipe = winutils.create_named_pipe(pipename)
                if not npipe:
                    return errno.ENOENT, None
                return 0, PassiveStream(None, name, bind_path, pipe=npipe)

        elif name.startswith("ptcp:"):
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            remote = name.split(':')
            sock.bind((remote[1], int(remote[2])))

        else:
            raise Exception('Unknown connection string')

        try:
            sock.listen(10)
        except socket.error as e:
            vlog.err("%s: listen: %s" % (name, os.strerror(e.error)))
            sock.close()
            return e.error, None

        return 0, PassiveStream(sock, name, bind_path)
Пример #2
0
    def open(name):
        """Attempts to start listening for remote stream connections.  'name'
        is a connection name in the form "TYPE:ARGS", where TYPE is an passive
        stream class's name and ARGS are stream class-specific. Currently the
        supported values for TYPE are "punix" and "ptcp".

        Returns (error, pstream): on success 'error' is 0 and 'pstream' is the
        new PassiveStream, on failure 'error' is a positive errno value and
        'pstream' is None."""
        if not PassiveStream.is_valid_name(name):
            return errno.EAFNOSUPPORT, None

        bind_path = name[6:]
        if name.startswith("punix:"):
            bind_path = ovs.util.abs_file_name(ovs.dirs.RUNDIR, bind_path)
            if sys.platform != 'win32':
                error, sock = ovs.socket_util.make_unix_socket(
                    socket.SOCK_STREAM, True, bind_path, None)
                if error:
                    return error, None
            else:
                # Branch used only on Windows
                try:
                    open(bind_path, 'w').close()
                except:
                    return errno.ENOENT, None

                pipename = winutils.get_pipe_name(bind_path)
                if len(pipename) > 255:
                    # Return invalid argument if the name is too long
                    return errno.ENOENT, None

                npipe = winutils.create_named_pipe(pipename)
                if not npipe:
                    return errno.ENOENT, None
                return 0, PassiveStream(None, name, bind_path, pipe=npipe)

        elif name.startswith("ptcp:"):
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            remote = name.split(':')
            sock.bind((remote[1], int(remote[2])))

        else:
            raise Exception('Unknown connection string')

        try:
            sock.listen(10)
        except socket.error as e:
            vlog.err("%s: listen: %s" % (name, os.strerror(e.error)))
            sock.close()
            return e.error, None

        return 0, PassiveStream(sock, name, bind_path)
Пример #3
0
    def __init__(self, pipe_name=None, sec_attributes=-1):
        """Create a named pipe with the given name.

        :param pipe_name(Optional): string representing the name of the pipe
            which should be used to create the named pipe
        :param sec_attributes(Optional): type win32security.SECURITY_ATTRIBUTES
            The default value is -1 which uses the default security attributes.
            This means that the named pipe handle is inherited when a new
            process is created.
        """
        # For reading from the named pipe, we will use an overlapped structure
        # for non-blocking I/O
        self._read = ovs_winutils.pywintypes.OVERLAPPED()
        # Create a new event which will be used by the overlapped structure
        self._read.hEvent = ovs_winutils.get_new_event()
        # This property tells if there is a pending reading operation on
        # the named pipe or not.
        self._read_pending = False

        if pipe_name is None:
            # Generate a random name for the named pipe if the name was not
            # passed explicitly as parameter.
            pipe_name = ("NamedPipe_%d_%s" %
                         (time.time(), str(random.random()).split(".")[1]))

        # Creating the name for a local named pipe. The property "name" will
        # have "\\.\pipe\" appended at the start of pipe_name
        self.name = ovs_winutils.get_pipe_name(pipe_name)
        # This property will contain the handle of the named pipe which can
        # be accessed later on.
        self.namedpipe = ovs_winutils.create_named_pipe(self.name,
                                                        saAttr=sec_attributes)
        # This property should be initialised explicitly later on by calling
        # the method create_file of this class.
        self._npipe_file = None

        if not self.namedpipe:
            # If there was an error when creating the named pipe, the property
            # "namedpipe" should be None. We raise an exception in this case
            raise NamedPipeException("Failed to create named pipe.")
Пример #4
0
    def __accept_windows(self):
        if self.connect_pending:
            try:
                winutils.get_overlapped_result(self.pipe, self.connect, False)
            except pywintypes.error as e:
                if e.winerror == winutils.winerror.ERROR_IO_INCOMPLETE:
                    # The operation is still pending, try again
                    self.connect_pending = True
                    return errno.EAGAIN, None
                else:
                    if self.pipe:
                        win32pipe.DisconnectNamedPipe(self.pipe)
                    return errno.EINVAL, None
            self.connect_pending = False

        error = winutils.connect_named_pipe(self.pipe, self.connect)
        if error:
            if error == winutils.winerror.ERROR_IO_PENDING:
                self.connect_pending = True
                return errno.EAGAIN, None
            elif error != winutils.winerror.ERROR_PIPE_CONNECTED:
                if self.pipe:
                    win32pipe.DisconnectNamedPipe(self.pipe)
                self.connect_pending = False
                return errno.EINVAL, None
            else:
                win32event.SetEvent(self.connect.hEvent)

        npipe = winutils.create_named_pipe(self._pipename)
        if not npipe:
            return errno.ENOENT, None

        old_pipe = self.pipe
        self.pipe = npipe
        winutils.win32event.ResetEvent(self.connect.hEvent)
        return 0, Stream(None, self.name, 0, pipe=old_pipe)
Пример #5
0
    def __accept_windows(self):
        if self.connect_pending:
            try:
                winutils.get_overlapped_result(self.pipe, self.connect, False)
            except pywintypes.error as e:
                if e.winerror == winutils.winerror.ERROR_IO_INCOMPLETE:
                    # The operation is still pending, try again
                    self.connect_pending = True
                    return errno.EAGAIN, None
                else:
                    if self.pipe:
                        win32pipe.DisconnectNamedPipe(self.pipe)
                    return errno.EINVAL, None
            self.connect_pending = False

        error = winutils.connect_named_pipe(self.pipe, self.connect)
        if error:
            if error == winutils.winerror.ERROR_IO_PENDING:
                self.connect_pending = True
                return errno.EAGAIN, None
            elif error != winutils.winerror.ERROR_PIPE_CONNECTED:
                if self.pipe:
                    win32pipe.DisconnectNamedPipe(self.pipe)
                self.connect_pending = False
                return errno.EINVAL, None
            else:
                win32event.SetEvent(self.connect.hEvent)

        npipe = winutils.create_named_pipe(self._pipename)
        if not npipe:
            return errno.ENOENT, None

        old_pipe = self.pipe
        self.pipe = npipe
        winutils.win32event.ResetEvent(self.connect.hEvent)
        return 0, Stream(None, self.name, 0, pipe=old_pipe)