def __init__(self, socket, name, status, pipe=None, is_server=False): self.socket = socket self.pipe = pipe if sys.platform == 'win32': self._read = pywintypes.OVERLAPPED() self._read.hEvent = winutils.get_new_event() self._write = pywintypes.OVERLAPPED() self._write.hEvent = winutils.get_new_event() if pipe is not None: # Flag to check if fd is a server HANDLE. In the case of a # server handle we have to issue a disconnect before closing # the actual handle. self._server = is_server suffix = name.split(":", 1)[1] suffix = ovs.util.abs_file_name(ovs.dirs.RUNDIR, suffix) self._pipename = winutils.get_pipe_name(suffix) self.name = name if status == errno.EAGAIN: self.state = Stream.__S_CONNECTING elif status == 0: self.state = Stream.__S_CONNECTED else: self.state = Stream.__S_DISCONNECTED self.error = 0
def __init__(self, socket, name, status, pipe=None, is_server=False): self.socket = socket self.pipe = pipe if sys.platform == 'win32': if pipe is not None: # Flag to check if fd is a server HANDLE. In the case of a # server handle we have to issue a disconnect before closing # the actual handle. self._server = is_server suffix = name.split(":", 1)[1] suffix = ovs.util.abs_file_name(ovs.dirs.RUNDIR, suffix) self._pipename = winutils.get_pipe_name(suffix) self._read = pywintypes.OVERLAPPED() self._read.hEvent = winutils.get_new_event() self._write = pywintypes.OVERLAPPED() self._write.hEvent = winutils.get_new_event() else: self._wevent = winutils.get_new_event(bManualReset=False, bInitialState=False) self.name = name if status == errno.EAGAIN: self.state = Stream.__S_CONNECTING elif status == 0: self.state = Stream.__S_CONNECTED else: self.state = Stream.__S_DISCONNECTED self.error = 0
def open(name, dscp=DSCP_DEFAULT): """Attempts to connect a stream to a remote peer. 'name' is a connection name in the form "TYPE:ARGS", where TYPE is an active stream class's name and ARGS are stream class-specific. The supported TYPEs include "unix", "tcp", and "ssl". Returns (error, stream): on success 'error' is 0 and 'stream' is the new Stream, on failure 'error' is a positive errno value and 'stream' is None. Never returns errno.EAGAIN or errno.EINPROGRESS. Instead, returns 0 and a new Stream. The connect() method can be used to check for successful connection completion.""" cls = Stream._find_method(name) if not cls: return errno.EAFNOSUPPORT, None suffix = name.split(":", 1)[1] if name.startswith("unix:"): suffix = ovs.util.abs_file_name(ovs.dirs.RUNDIR, suffix) if sys.platform == 'win32': pipename = winutils.get_pipe_name(suffix) if len(suffix) > 255: # Return invalid argument if the name is too long return errno.ENOENT, None try: # In case of "unix:" argument, the assumption is that # there is a file created in the path (suffix). open(suffix, 'r').close() except: return errno.ENOENT, None try: npipe = winutils.create_file(pipename) try: winutils.set_pipe_mode(npipe, win32pipe.PIPE_READMODE_BYTE) except pywintypes.error as e: return errno.ENOENT, None except pywintypes.error as e: if e.winerror == winutils.winerror.ERROR_PIPE_BUSY: # Pipe is busy, set the retry flag to true and retry # again during the connect function. Stream.retry_connect = True return 0, cls(None, name, errno.EAGAIN, pipe=win32file.INVALID_HANDLE_VALUE, is_server=False) return errno.ENOENT, None return 0, cls(None, name, 0, pipe=npipe, is_server=False) error, sock = cls._open(suffix, dscp) if error: return error, None else: status = ovs.socket_util.check_connection_completion(sock) return 0, cls(sock, name, status)
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)
def __init__(self, sock, name, bind_path, pipe=None): self.name = name self.pipe = pipe self.socket = sock if pipe is not None: self.connect = pywintypes.OVERLAPPED() self.connect.hEvent = winutils.get_new_event(bManualReset=True) self.connect_pending = False suffix = name.split(":", 1)[1] suffix = ovs.util.abs_file_name(ovs.dirs.RUNDIR, suffix) self._pipename = winutils.get_pipe_name(suffix) self.bind_path = bind_path
def __init__(self, sock, name, bind_path, pipe=None): self.name = name self.pipe = pipe self.socket = sock if pipe is not None: self.connect = pywintypes.OVERLAPPED() self.connect.hEvent = winutils.get_new_event() self.connect_pending = False suffix = name.split(":", 1)[1] suffix = ovs.util.abs_file_name(ovs.dirs.RUNDIR, suffix) self._pipename = winutils.get_pipe_name(suffix) self.bind_path = bind_path
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.")