Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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:
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
def __recv_windows(self, n):
if self._read_pending:
try:
nBytesRead = winutils.get_overlapped_result(self.pipe,
self._read,
False)
self._read_pending = False
except pywintypes.error as e:
if e.winerror == winutils.winerror.ERROR_IO_INCOMPLETE:
# The operation is still pending, try again
self._read_pending = True
return (errno.EAGAIN, "")
elif e.winerror in winutils.pipe_disconnected_errors:
# If the pipe was disconnected, return 0.
return (0, "")
else:
return (errno.EINVAL, "")
else:
(errCode, self._read_buffer) = winutils.read_file(self.pipe,
n,
self._read)
if errCode:
if errCode == winutils.winerror.ERROR_IO_PENDING:
self._read_pending = True
return (errno.EAGAIN, "")
elif errCode in winutils.pipe_disconnected_errors:
# If the pipe was disconnected, return 0.
return (0, "")
else:
def check_connection_completion(sock):
if sys.platform == "win32":
p = ovs.poller.SelectPoll()
event = winutils.get_new_event(None, False, True, None)
# Receive notification of readiness for writing, of completed
# connection or multipoint join operation, and of socket closure.
win32file.WSAEventSelect(sock, event,
win32file.FD_WRITE |
win32file.FD_CONNECT |
win32file.FD_CLOSE)
p.register(event, ovs.poller.POLLOUT)
else:
p = ovs.poller.get_system_poll()
p.register(sock, ovs.poller.POLLOUT)
pfds = p.poll(0)
if len(pfds) == 1:
revents = pfds[0][1]
if revents & ovs.poller.POLLERR or revents & ovs.poller.POLLHUP:
try:
# The following should raise an exception.
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)
def __scs_connecting(self):
if self.socket is not None:
retval = ovs.socket_util.check_connection_completion(self.socket)
assert retval != errno.EINPROGRESS
elif sys.platform == 'win32':
if self.retry_connect:
try:
self.pipe = winutils.create_file(self._pipename)
self._retry_connect = False
retval = 0
except pywintypes.error as e:
if e.winerror == winutils.winerror.ERROR_PIPE_BUSY:
retval = errno.EAGAIN
else:
self._retry_connect = False
retval = errno.ENOENT
else:
# If retry_connect is false, it means it's already
# connected so we can set the value of retval to 0
retval = 0
if retval == 0:
self.state = Stream.__S_CONNECTED
elif retval != errno.EAGAIN:
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:
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:
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
nBytesWritten = winutils.get_overlapped_result(self.pipe,
self._write,
False)
self._write_pending = False
except pywintypes.error as e:
if e.winerror == winutils.winerror.ERROR_IO_INCOMPLETE:
# The operation is still pending, try again
self._read_pending = True
return -errno.EAGAIN
elif e.winerror in winutils.pipe_disconnected_errors:
# If the pipe was disconnected, return connection reset.
return -errno.ECONNRESET
else:
return -errno.EINVAL
else:
(errCode, nBytesWritten) = winutils.write_file(self.pipe,
buf,
self._write)
if errCode:
if errCode == winutils.winerror.ERROR_IO_PENDING:
self._write_pending = True
return -errno.EAGAIN
if (not nBytesWritten and
errCode in winutils.pipe_disconnected_errors):
# If the pipe was disconnected, return connection reset.
return -errno.ECONNRESET
return nBytesWritten