示例#1
0
    def doRead(self):
        """Called when my socket is ready for reading.

        This accepts a connection and calls self.protocol() to handle the
        wire-level protocol.
        """
        try:
            if platformType == "posix":
                numAccepts = self.numberAccepts
            else:
                # win32 event loop breaks if we do more than one accept()
                # in an iteration of the event loop.
                numAccepts = 1
            for i in range(numAccepts):
                # we need this so we can deal with a factory's buildProtocol
                # calling our loseConnection
                if self.disconnecting:
                    return
                try:
                    skt, addr = self.socket.accept()
                except socket.error, e:
                    if e.args[0] in (EWOULDBLOCK, EAGAIN):
                        self.numberAccepts = i
                        break
                    elif e.args[0] == EPERM:
                        # Netfilter on Linux may have rejected the
                        # connection, but we get told to try to accept()
                        # anyway.
                        continue
                    elif e.args[0] in (EMFILE, ENOBUFS, ENFILE, ENOMEM, ECONNABORTED):

                        # Linux gives EMFILE when a process is not allowed
                        # to allocate any more file descriptors.  *BSD and
                        # Win32 give (WSA)ENOBUFS.  Linux can also give
                        # ENFILE if the system is out of inodes, or ENOMEM
                        # if there is insufficient memory to allocate a new
                        # dentry.  ECONNABORTED is documented as possible on
                        # both Linux and Windows, but it is not clear
                        # whether there are actually any circumstances under
                        # which it can happen (one might expect it to be
                        # possible if a client sends a FIN or RST after the
                        # server sends a SYN|ACK but before application code
                        # calls accept(2), however at least on Linux this
                        # _seems_ to be short-circuited by syncookies.

                        log.msg("Could not accept new connection (%s)" % (
                            errorcode[e.args[0]],))
                        break
                    raise

                fdesc._setCloseOnExec(skt.fileno())
                protocol = self.factory.buildProtocol(self._buildAddr(addr))
                if protocol is None:
                    skt.close()
                    continue
                s = self.sessionno
                self.sessionno = s+1
                transport = self.transport(skt, protocol, addr, self, s, self.reactor)
                protocol.makeConnection(transport)
            else:
示例#2
0
def create_stream_socket(addressFamily, shared=False):
    """
    Create a new socket for use with Twisted's IReactor.adoptStreamPort.

    :param addressFamily: The socket address family.
    :type addressFamily: One of socket.AF_INET, socket.AF_INET6, socket.AF_UNIX
    :param shared: If `True`, request to create a shared, load-balanced socket.
                   When this feature is not available, throw an exception.
    :type shared: bool
    :returns obj -- A socket.
    """
    s = socket.socket(addressFamily, socket.SOCK_STREAM)
    s.setblocking(0)
    fdesc._setCloseOnExec(s.fileno())

    if platformType == "posix" and sys.platform != "cygwin":
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    if shared:
        if addressFamily not in [socket.AF_INET, socket.AF_INET6]:
            raise Exception("shared sockets are only supported for TCP")

        if _HAS_SHARED_LOADBALANCED_SOCKET:
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        else:
            raise Exception("shared sockets unsupported on this system")

    return s
示例#3
0
def create_stream_socket(addressFamily, shared=False):
    """
    Create a new socket for use with Twisted's IReactor.adoptStreamPort.

    :param addressFamily: The socket address family.
    :type addressFamily: One of socket.AF_INET, socket.AF_INET6, socket.AF_UNIX
    :param shared: If `True`, request to create a shared, load-balanced socket.
                   When this feature is not available, throw an exception.
    :type shared: bool
    :returns obj -- A socket.
    """
    s = socket.socket(addressFamily, socket.SOCK_STREAM)
    s.setblocking(0)
    fdesc._setCloseOnExec(s.fileno())

    if platformType == "posix" and sys.platform != "cygwin":
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    if shared:
        if addressFamily not in [socket.AF_INET, socket.AF_INET6]:
            raise Exception("shared sockets are only supported for TCP")

        if _HAS_SHARED_LOADBALANCED_SOCKET:
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        else:
            raise Exception("shared sockets unsupported on this system")

    return s
示例#4
0
def create_stream_socket(addressFamily, shared=False):
    """
    Create a new socket for use with Twisted's IReactor.adoptStreamPort.

    :param addressFamily: The socket address family.
    :type addressFamily: One of socket.AF_INET, socket.AF_INET6, socket.AF_UNIX
    :param shared: If `True`, request to create a shared, load-balanced socket.
                   When this feature is not available, throw an exception.
    :type shared: bool
    :returns obj -- A socket.
    """
    s = socket.socket(addressFamily, socket.SOCK_STREAM)
    s.setblocking(0)
    fdesc._setCloseOnExec(s.fileno())

    if platformType == "posix" and sys.platform != "cygwin":
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    if shared:
        if addressFamily not in [socket.AF_INET, socket.AF_INET6]:
            raise Exception("shared sockets are only supported for IPv4 and IPv6")

        if _HAS_SHARED_LOADBALANCED_SOCKET:
            if sys.platform.startswith('linux'):
                s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
            elif sys.platform == 'win32':
                # http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t/14388707#14388707
                s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                # s.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
            else:
                raise Exception("logic error")
        else:
            raise Exception("shared sockets unsupported on this system")

    return s
示例#5
0
 def createInternetSocket(self, socket_family=None):
     if not socket_family:
         socket_family = self.addressFamily
     s = socket.socket(socket_family, self.socketType)
     s.setblocking(0)
     fdesc._setCloseOnExec(s.fileno())
     return s
示例#6
0
def create_stream_socket(addressFamily, shared=False):
    """
    Create a new socket for use with Twisted's IReactor.adoptStreamPort.

    :param addressFamily: The socket address family.
    :type addressFamily: One of socket.AF_INET, socket.AF_INET6, socket.AF_UNIX
    :param shared: If `True`, request to create a shared, load-balanced socket.
                   When this feature is not available, throw an exception.
    :type shared: bool
    :returns obj -- A socket.
    """
    s = socket.socket(addressFamily, socket.SOCK_STREAM)
    s.setblocking(0)
    fdesc._setCloseOnExec(s.fileno())

    if platformType == "posix" and sys.platform != "cygwin":
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    if shared:
        if addressFamily not in [socket.AF_INET, socket.AF_INET6]:
            raise Exception("shared sockets are only supported for IPv4 and IPv6")

        if _HAS_SHARED_LOADBALANCED_SOCKET:
            if sys.platform.startswith('linux'):
                s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
            elif sys.platform == 'win32':
                # http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t/14388707#14388707
                s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                # s.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
            else:
                raise Exception("logic error")
        else:
            raise Exception("shared sockets unsupported on this system")

    return s
示例#7
0
def makesock(sock, protocol, reactor=None):
    if not reactor:
        from twisted.internet import reactor
    fdesc._setCloseOnExec(sock.fileno())
    transport = Paired(sock, protocol, reactor)
    protocol.makeConnection(transport)
    return transport
示例#8
0
文件: tcp.py 项目: antong/twisted
    def doRead(self):
        """Called when my socket is ready for reading.

        This accepts a connection and calls self.protocol() to handle the
        wire-level protocol.
        """
        try:
            if platformType == "posix":
                numAccepts = self.numberAccepts
            else:
                # win32 event loop breaks if we do more than one accept()
                # in an iteration of the event loop.
                numAccepts = 1
            for i in range(numAccepts):
                # we need this so we can deal with a factory's buildProtocol
                # calling our loseConnection
                if self.disconnecting:
                    return
                try:
                    skt, addr = self.socket.accept()
                except socket.error, e:
                    if e.args[0] in (EWOULDBLOCK, EAGAIN):
                        self.numberAccepts = i
                        break
                    elif e.args[0] == EPERM:
                        # Netfilter on Linux may have rejected the
                        # connection, but we get told to try to accept()
                        # anyway.
                        continue
                    elif e.args[0] in (EMFILE, ENOBUFS, ENFILE, ENOMEM, ECONNABORTED):

                        # Linux gives EMFILE when a process is not allowed
                        # to allocate any more file descriptors.  *BSD and
                        # Win32 give (WSA)ENOBUFS.  Linux can also give
                        # ENFILE if the system is out of inodes, or ENOMEM
                        # if there is insufficient memory to allocate a new
                        # dentry.  ECONNABORTED is documented as possible on
                        # both Linux and Windows, but it is not clear
                        # whether there are actually any circumstances under
                        # which it can happen (one might expect it to be
                        # possible if a client sends a FIN or RST after the
                        # server sends a SYN|ACK but before application code
                        # calls accept(2), however at least on Linux this
                        # _seems_ to be short-circuited by syncookies.

                        log.msg("Could not accept new connection (%s)" % (
                            errorcode[e.args[0]],))
                        break
                    raise

                fdesc._setCloseOnExec(skt.fileno())
                protocol = self.factory.buildProtocol(self._buildAddr(addr))
                if protocol is None:
                    skt.close()
                    continue
                s = self.sessionno
                self.sessionno = s+1
                transport = self.transport(skt, protocol, addr, self, s, self.reactor)
                protocol.makeConnection(transport)
            else:
示例#9
0
 def createInternetSocket(self):
     """(internal) Create a non-blocking socket using
     self.addressFamily, self.socketType.
     """
     s = socket.socket(self.addressFamily, self.socketType)
     s.setblocking(0)
     fdesc._setCloseOnExec(s.fileno())
     return s
示例#10
0
 def __init__(self, reactor):
     """Initialize."""
     self.reactor = reactor
     self.i, self.o = os.pipe()
     fdesc.setNonBlocking(self.i)
     fdesc._setCloseOnExec(self.i)
     fdesc.setNonBlocking(self.o)
     fdesc._setCloseOnExec(self.o)
     self.fileno = lambda: self.i
 def createInternetSocket(self):
     s = socket.socket(self.addressFamily, self.socketType, self.port)
     s.setblocking(0)
     fdesc._setCloseOnExec(s.fileno())
     if self.listenMultiple:
         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         if hasattr(socket, "SO_REUSEPORT"):
             s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
     return s
示例#12
0
 def __init__(self, reactor):
     """Initialize.
     """
     self.reactor = reactor
     self.i, self.o = os.pipe()
     fdesc.setNonBlocking(self.i)
     fdesc._setCloseOnExec(self.i)
     fdesc.setNonBlocking(self.o)
     fdesc._setCloseOnExec(self.o)
     self.fileno = lambda: self.i
示例#13
0
 def start(self):
     fdesc._setCloseOnExec(self.skt.fileno())
     protocol = self.factory.buildProtocol(self._buildAddr(self.addr))
     if protocol is None:
         log.error("Can't create protocol")
         return False
     transport = self.transport(
         self.skt, protocol, self.addr, self, 1, self.reactor)
     protocol.makeConnection(transport)
     return True
示例#14
0
 def test_unsetCloseOnExec(self):
     """
     A file descriptor passed to L{fdesc._unsetCloseOnExec} is inherited by
     a new process image created with one of the exec family of functions.
     """
     with open(self.mktemp(), 'wb') as fObj:
         fdesc._setCloseOnExec(fObj.fileno())
         fdesc._unsetCloseOnExec(fObj.fileno())
         status = self._execWithFileDescriptor(fObj)
         self.assertTrue(os.WIFEXITED(status))
         self.assertEqual(os.WEXITSTATUS(status), 20)
示例#15
0
 def test_unsetCloseOnExec(self):
     """
     A file descriptor passed to L{fdesc._unsetCloseOnExec} is inherited by
     a new process image created with one of the exec family of functions.
     """
     with open(self.mktemp(), 'wb') as fObj:
         fdesc._setCloseOnExec(fObj.fileno())
         fdesc._unsetCloseOnExec(fObj.fileno())
         status = self._execWithFileDescriptor(fObj)
         self.assertTrue(os.WIFEXITED(status))
         self.assertEqual(os.WEXITSTATUS(status), 20)
示例#16
0
    def __init__(self, interface, super_socket=None, timeout=5):

        abstract.FileDescriptor.__init__(self, reactor)
        if interface == "auto":
            interface = getDefaultIface()
        if not super_socket:
            super_socket = conf.L3socket(iface=interface, promisc=True, filter="")
            # super_socket = conf.L2socket(iface=interface)

        self.protocols = []
        fdesc._setCloseOnExec(super_socket.ins.fileno())
        self.super_socket = super_socket
示例#17
0
    def __init__(self, interface, super_socket=None, timeout=5):

        abstract.FileDescriptor.__init__(self, reactor)
        if interface == 'auto':
            interface = getDefaultIface()
        if not super_socket and sys.platform == 'darwin':
            super_socket = conf.L3socket(iface=interface, promisc=True, filter='')
        elif not super_socket:
            super_socket = conf.L3socket(iface=interface)

        self.protocols = []
        fdesc._setCloseOnExec(super_socket.ins.fileno())
        self.super_socket = super_socket
示例#18
0
    def createInternetSocket(self):
        """Overridden"""

        err = None
        s = socket.socket(self.addressFamily, self.socketType)
        s.setblocking(0)
        fdesc._setCloseOnExec(s.fileno())

        try:
            if not s.getsockopt(socket.SOL_IP, socket.IP_TRANSPARENT):
                s.setsockopt(socket.SOL_IP, socket.IP_TRANSPARENT, 1)
        except Exception as e:
            LOG.error('Failed to establish transparent proxy: %s', e)

        return s  # Maintain non-transparent behavior.
示例#19
0
文件: inotify.py 项目: 0004c/VTK
    def __init__(self, reactor=None):
        FileDescriptor.__init__(self, reactor=reactor)

        # Smart way to allow parametrization of libc so I can override
        # it and test for the system errors.
        self._fd = self._inotify.init()

        fdesc.setNonBlocking(self._fd)
        fdesc._setCloseOnExec(self._fd)

        # The next 2 lines are needed to have self.loseConnection()
        # to call connectionLost() on us. Since we already created the
        # fd that talks to inotify we want to be notified even if we
        # haven't yet started reading.
        self.connected = 1
        self._writeDisconnected = True

        self._buffer = ''
        self._watchpoints = {}
        self._watchpaths = {}
示例#20
0
    def __init__(self, reactor=None):
        FileDescriptor.__init__(self, reactor=reactor)

        # Smart way to allow parametrization of libc so I can override
        # it and test for the system errors.
        self._fd = self._inotify.init()

        fdesc.setNonBlocking(self._fd)
        fdesc._setCloseOnExec(self._fd)

        # The next 2 lines are needed to have self.loseConnection()
        # to call connectionLost() on us. Since we already created the
        # fd that talks to inotify we want to be notified even if we
        # haven't yet started reading.
        self.connected = 1
        self._writeDisconnected = True

        self._buffer = ''
        self._watchpoints = {}
        self._watchpaths = {}
示例#21
0
文件: httip.py 项目: nott/httip
    def get_socket(self):
        '''
        Return listening socket.
        '''
        if self.SYSTEMD.LISTEN_PID in os.environ:
            # looks like we've been started by systemd socket activation
            if os.environ.get(self.SYSTEMD.LISTEN_FDS, None) == '1':
                fd = self.SYSTEMD.LISTEN_FDS_START + 0
                log_info(
                    'Using socket at FD %s activated by PID %s' % (
                        fd, os.environ[self.SYSTEMD.LISTEN_PID]
                    )
                )

                sckt = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
            else:
                # we expect exactly one socket to be initialized by Systemd
                message = 'Environmental variable %s must point to exactly '\
                          'one file descriptor, got %s intead' % (
                              self.SYSTEMD.LISTEN_FDS,
                              os.environ.get(self.SYSTEMD.LISTEN_FDS, None),
                          )

                log_err(message)
                raise EnvironmentError(message)
        else:
            log_info('Binding to %s:%s' % (self.args.addr, self.args.port))

            sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sckt.bind((self.args.addr, self.args.port))

        # socket initilization for Twisted
        sckt.setblocking(0)
        fdesc._setCloseOnExec(sckt.fileno())
        sckt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sckt.listen(50)

        return sckt
示例#22
0
 def createInternetSocket(self):
     s = socket.socket(self.addressFamily, self.socketType)
     s.setblocking(0)
     fdesc._setCloseOnExec(s.fileno())
     return s
示例#23
0
    def doRead(self):
        """Called when my socket is ready for reading.

        This accepts a connection and calls self.protocol() to handle the
        wire-level protocol.
        """
        try:
            if platformType == "posix":
                numAccepts = self.numberAccepts
            else:
                # win32 event loop breaks if we do more than one accept()
                # in an iteration of the event loop.
                numAccepts = 1
            for i in range(numAccepts):
                # we need this so we can deal with a factory's buildProtocol
                # calling our loseConnection
                if self.disconnecting:
                    return
                try:
                    skt, addr = self.socket.accept()
                except socket.error as e:
                    if e.args[0] in (EWOULDBLOCK, EAGAIN):
                        self.numberAccepts = i
                        break
                    elif e.args[0] == EPERM:
                        # Netfilter on Linux may have rejected the
                        # connection, but we get told to try to accept()
                        # anyway.
                        continue
                    elif e.args[0] in (EMFILE, ENOBUFS, ENFILE, ENOMEM, ECONNABORTED):
                        # Linux gives EMFILE when a process is not allowed to
                        # allocate any more file descriptors.  *BSD and Win32
                        # give (WSA)ENOBUFS.  Linux can also give ENFILE if the
                        # system is out of inodes, or ENOMEM if there is
                        # insufficient memory to allocate a new dentry.
                        # ECONNABORTED is documented as possible on all
                        # relevant platforms (Linux, Windows, macOS, and the
                        # BSDs) but occurs only on the BSDs.  It occurs when a
                        # client sends a FIN or RST after the server sends a
                        # SYN|ACK but before application code calls accept(2).
                        # On Linux, calling accept(2) on such a listener
                        # returns a connection that fails as though the it were
                        # terminated after being fully established.  This
                        # appears to be an implementation choice (see
                        # inet_accept in inet/ipv4/af_inet.c).  On macOS X,
                        # such a listener is not considered readable, so
                        # accept(2) will never be called.  Calling accept(2) on
                        # such a listener, however, does not return at all.
                        log.msg("Could not accept new connection (%s)" % (
                            errorcode[e.args[0]],))
                        break
                    raise

                fdesc._setCloseOnExec(skt.fileno())
                protocol = self.factory.buildProtocol(self._buildAddr(addr))
                if protocol is None:
                    skt.close()
                    continue
                s = self.sessionno
                self.sessionno = s+1
                transport = self.transport(skt, protocol, addr, self, s, self.reactor)
                protocol.makeConnection(transport)
            else:
                self.numberAccepts = self.numberAccepts+20
        except:
            # Note that in TLS mode, this will possibly catch SSL.Errors
            # raised by self.socket.accept()
            #
            # There is no "except SSL.Error:" above because SSL may be
            # None if there is no SSL support.  In any case, all the
            # "except SSL.Error:" suite would probably do is log.deferr()
            # and return, so handling it here works just as well.
            log.deferr()
示例#24
0
 def createInternetSocket(self):
     s = socket.socket(self.addressFamily, self.socketType)
     s.setblocking(0)
     fdesc._setCloseOnExec(s.fileno())
     return s