Exemple #1
0
    def connect(self):
        try:
            connection = self.makesocket()

            if self.spoof_source_address:
                try:
                    # 19 is `IP_TRANSPARENT`, which is only available on Python 3.3+ on some OSes
                    if not connection.getsockopt(socket.SOL_IP, 19):
                        connection.setsockopt(socket.SOL_IP, 19, 1)
                except socket.error as e:
                    raise exceptions.TcpException(
                        "Failed to spoof the source address: " + e.strerror
                    )
            if self.source_address:
                connection.bind(self.source_address())
            connection.connect(self.address())
            self.source_address = Address(connection.getsockname())
        except (socket.error, IOError) as err:
            raise exceptions.TcpException(
                'Error connecting to "%s": %s' %
                (self.address.host, err)
            )
        self.connection = connection
        self.ip_address = Address(connection.getpeername())
        self._makefile()
        return ConnectionCloser(self)
Exemple #2
0
    def peek(self, length):
        """
        Tries to peek into the underlying file object.

        Returns:
            Up to the next N bytes if peeking is successful.

        Raises:
            exceptions.TcpException if there was an error with the socket
            TlsException if there was an error with pyOpenSSL.
            NotImplementedError if the underlying file object is not a [pyOpenSSL] socket
        """
        if isinstance(self.o, socket_fileobject):
            try:
                return self.o._sock.recv(length, socket.MSG_PEEK)
            except socket.error as e:
                raise exceptions.TcpException(repr(e))
        elif isinstance(self.o, SSL.Connection):
            try:
                return self.o.recv(length, socket.MSG_PEEK)
            except SSL.Error as e:
                six.reraise(exceptions.TlsException,
                            exceptions.TlsException(str(e)),
                            sys.exc_info()[2])
        else:
            raise NotImplementedError("Can only peek into (pyOpenSSL) sockets")
Exemple #3
0
    def peek(self, length):
        """
        Tries to peek into the underlying file object.

        Returns:
            Up to the next N bytes if peeking is successful.

        Raises:
            exceptions.TcpException if there was an error with the socket
            TlsException if there was an error with pyOpenSSL.
            NotImplementedError if the underlying file object is not a [pyOpenSSL] socket
        """
        if isinstance(self.o, socket_fileobject):
            try:
                return self.o._sock.recv(length, socket.MSG_PEEK)
            except socket.error as e:
                raise exceptions.TcpException(repr(e))
        elif isinstance(self.o, SSL.Connection):
            try:
                if tuple(int(x) for x in OpenSSL.__version__.split(".")[:2]) > (0, 15):
                    return self.o.recv(length, socket.MSG_PEEK)
                else:
                    # TODO: remove once a new version is released
                    # Polyfill for pyOpenSSL <= 0.15.1
                    # Taken from https://github.com/pyca/pyopenssl/commit/1d95dea7fea03c7c0df345a5ea30c12d8a0378d2
                    buf = SSL._ffi.new("char[]", length)
                    result = SSL._lib.SSL_peek(self.o._ssl, buf, length)
                    self.o._raise_ssl_error(self.o._ssl, result)
                    return SSL._ffi.buffer(buf, result)[:]
            except SSL.Error as e:
                six.reraise(exceptions.TlsException, exceptions.TlsException(str(e)), sys.exc_info()[2])
        else:
            raise NotImplementedError("Can only peek into (pyOpenSSL) sockets")
Exemple #4
0
 def connect(self):
     try:
         connection = socket.socket(self.address.family, socket.SOCK_STREAM)
         if self.source_address:
             connection.bind(self.source_address())
         connection.connect(self.address())
         self.source_address = Address(connection.getsockname())
     except (socket.error, IOError) as err:
         raise exceptions.TcpException('Error connecting to "%s": %s' %
                                       (self.address.host, err))
     self.connection = connection
     self.ip_address = Address(connection.getpeername())
     self._makefile()
     return ConnectionCloser(self)