Esempio n. 1
0
    def connect_socket(self, host, port):
        try:
            addr_info = io.AddrInfo(host, family=self.family)
        except Exception:
            raise NSPRError(error_code=error.PR_ADDRESS_NOT_SUPPORTED_ERROR,
                            error_message="Cannot resolve %s using family %s" %
                            (host, io.addr_family_name(self.family)))

        for net_addr in addr_info:
            root_logger.debug("Connecting: %s", net_addr)
            net_addr.port = port
            self.family = net_addr.family
            try:
                self._create_socket()
                self.sock.connect(net_addr)
                return
            except Exception as e:
                root_logger.debug("Could not connect socket to %s, error: %s",
                                  net_addr, str(e))
                root_logger.debug("Try to continue with next family...")
                continue

        raise NSPRError(
            error_code=error.PR_ADDRESS_NOT_SUPPORTED_ERROR,
            error_message="Could not connect to %s using any address" % host)
Esempio n. 2
0
    def connect_socket(self, host, port):
        try:
            addr_info = io.AddrInfo(host, family=self.family)
        except Exception:
            raise NSPRError(
                error_code=error.PR_ADDRESS_NOT_SUPPORTED_ERROR,
                error_message="Cannot resolve %s using family %s" % (host, io.addr_family_name(self.family)),
            )

        for net_addr in addr_info:
            root_logger.debug("Connecting: %s", net_addr)
            net_addr.port = port
            self.family = net_addr.family
            try:
                self._create_socket()
                self.sock.connect(net_addr)
                return
            except Exception as e:
                root_logger.debug("Could not connect socket to %s, error: %s", net_addr, str(e))
                root_logger.debug("Try to continue with next family...")
                continue

        raise NSPRError(
            error_code=error.PR_ADDRESS_NOT_SUPPORTED_ERROR,
            error_message="Could not connect to %s using any address" % host,
        )
Esempio n. 3
0
        else:
            sock = io.Socket(net_addr.family)

        try:
            if verbose: print "client trying connection to: %s" % (net_addr)
            sock.connect(net_addr, timeout=io.seconds_to_interval(timeout_secs))
            if verbose: print "client connected to: %s" % (net_addr)
            valid_addr = True
            break
        except Exception, e:
            sock.close()
            print >>sys.stderr, "client: connection to: %s failed (%s)" % (net_addr, e)

    if not valid_addr:
        print >>sys.stderr, "Could not establish valid address for \"%s\" in family %s" % \
        (hostname, io.addr_family_name(family))
        return

    # Talk to the server
    try:
        if info: print "client: sending \"%s\"" % (request)
        sock.send(request)
        buf = sock.recv(1024)
        if not buf:
            print >>sys.stderr, "client: lost connection"
            sock.close()
            return
        if info: print "client: received \"%s\"" % (buf)
    except Exception, e:
        print >>sys.stderr, "client: %s" % e
        try:
Esempio n. 4
0
        try:
            if verbose: print "client trying connection to: %s" % (net_addr)
            sock.connect(net_addr,
                         timeout=io.seconds_to_interval(timeout_secs))
            if verbose: print "client connected to: %s" % (net_addr)
            valid_addr = True
            break
        except Exception, e:
            sock.close()
            print >> sys.stderr, "client: connection to: %s failed (%s)" % (
                net_addr, e)

    if not valid_addr:
        print >>sys.stderr, "Could not establish valid address for \"%s\" in family %s" % \
        (hostname, io.addr_family_name(family))
        return

    # Talk to the server
    try:
        if info: print "client: sending \"%s\"" % (request)
        sock.send(request)
        buf = sock.recv(1024)
        if not buf:
            print >> sys.stderr, "client: lost connection"
            sock.close()
            return
        if info: print "client: received \"%s\"" % (buf)
    except Exception, e:
        print >> sys.stderr, "client: %s" % e
        try:
            sock = io.Socket(net_addr.family)

        try:
            print "client trying connection to: %s" % (net_addr)
            sock.connect(net_addr,
                         timeout=io.seconds_to_interval(timeout_secs))
            print "client connected to: %s" % (net_addr)
            valid_addr = True
            break
        except Exception, e:
            sock.close()
            print "client connection to: %s failed (%s)" % (net_addr, e)

    if not valid_addr:
        print "Could not establish valid address for \"%s\" in family %s" % \
        (hostname, io.addr_family_name(family))
        return

    # Talk to the server
    try:
        sock.send("Hello")
        buf = sock.recv(1024)
        if not buf:
            print "client lost connection"
            sock.close()
            return
        print "client received: %s" % (buf)
    except Exception, e:
        print e.strerror
        try:
            sock.close()
Esempio n. 6
0
def Client():
    valid_addr = False
    # Get the IP Address of our server
    try:
        addr_info = io.AddrInfo(options.hostname)
    except Exception as e:
        print("could not resolve host address \"%s\"" % options.hostname)
        return

    for net_addr in addr_info:
        if options.family != io.PR_AF_UNSPEC:
            if net_addr.family != options.family:
                continue
        net_addr.port = options.port

        if options.use_ssl:
            sock = ssl.SSLSocket(net_addr.family)

            # Set client SSL socket options
            sock.set_ssl_option(ssl.SSL_SECURITY, True)
            sock.set_ssl_option(ssl.SSL_HANDSHAKE_AS_CLIENT, True)
            sock.set_hostname(options.hostname)

            # Provide a callback which notifies us when the SSL handshake is complete
            sock.set_handshake_callback(handshake_callback)

            # Provide a callback to supply our client certificate info
            sock.set_client_auth_data_callback(
                client_auth_data_callback,
                options.client_nickname,
                options.password,
                nss.get_default_certdb(),
            )

            # Provide a callback to verify the servers certificate
            sock.set_auth_certificate_callback(auth_certificate_callback,
                                               nss.get_default_certdb())
        else:
            sock = io.Socket(net_addr.family)

        try:
            print("client trying connection to: %s" % (net_addr))
            sock.connect(net_addr,
                         timeout=io.seconds_to_interval(timeout_secs))
            print("client connected to: %s" % (net_addr))
            valid_addr = True
            break
        except Exception as e:
            sock.close()
            print("client connection to: %s failed (%s)" % (net_addr, e))

    if not valid_addr:
        print("Could not establish valid address for \"%s\" in family %s" %
              (options.hostname, io.addr_family_name(options.family)))
        return

    # Talk to the server
    try:
        data = 'Hello' + '\n'  # newline is protocol record separator
        sock.send(data.encode('utf-8'))
        buf = sock.readline()
        if not buf:
            print("client lost connection")
            sock.close()
            return
        buf = buf.decode('utf-8')
        buf = buf.rstrip()  # remove newline record separator
        print("client received: %s" % (buf))
    except Exception as e:
        print(e.strerror)
        try:
            sock.close()
        except:
            pass
        return

    # End of (simple) protocol session?
    if buf == 'Goodbye':
        try:
            sock.shutdown()
        except:
            pass

    try:
        sock.close()
        if options.use_ssl:
            ssl.clear_session_cache()
    except Exception as e:
        print(e)
Esempio n. 7
0
def Client():
    valid_addr = False
    # Get the IP Address of our server
    try:
        addr_info = io.AddrInfo(options.hostname)
    except Exception as e:
        print("could not resolve host address \"%s\"" % options.hostname)
        return

    for net_addr in addr_info:
        if options.family != io.PR_AF_UNSPEC:
            if net_addr.family != options.family:
                continue
        net_addr.port = options.port

        if options.use_ssl:
            sock = ssl.SSLSocket(net_addr.family)

            # Set client SSL socket options
            sock.set_ssl_option(ssl.SSL_SECURITY, True)
            sock.set_ssl_option(ssl.SSL_HANDSHAKE_AS_CLIENT, True)
            sock.set_hostname(options.hostname)

            # Provide a callback which notifies us when the SSL handshake is complete
            sock.set_handshake_callback(handshake_callback)

            # Provide a callback to supply our client certificate info
            sock.set_client_auth_data_callback(client_auth_data_callback, options.client_nickname,
                                               options.password, nss.get_default_certdb())

            # Provide a callback to verify the servers certificate
            sock.set_auth_certificate_callback(auth_certificate_callback,
                                               nss.get_default_certdb())
        else:
            sock = io.Socket(net_addr.family)

        try:
            print("client trying connection to: %s" % (net_addr))
            sock.connect(net_addr, timeout=io.seconds_to_interval(timeout_secs))
            print("client connected to: %s" % (net_addr))
            valid_addr = True
            break
        except Exception as e:
            sock.close()
            print("client connection to: %s failed (%s)" % (net_addr, e))

    if not valid_addr:
        print("Could not establish valid address for \"%s\" in family %s" % \
        (options.hostname, io.addr_family_name(options.family)))
        return

    # Talk to the server
    try:
        data = 'Hello' + '\n' # newline is protocol record separator
        sock.send(data.encode('utf-8'))
        buf = sock.readline()
        if not buf:
            print("client lost connection")
            sock.close()
            return
        buf = buf.decode('utf-8')
        buf = buf.rstrip()        # remove newline record separator
        print("client received: %s" % (buf))
    except Exception as e:
        print(e.strerror)
        try:
            sock.close()
        except:
            pass
        return

    # End of (simple) protocol session?
    if buf == 'Goodbye':
        try:
            sock.shutdown()
        except:
            pass

    try:
        sock.close()
        if options.use_ssl:
            ssl.clear_session_cache()
    except Exception as e:
        print(e)
        else:
            sock = io.Socket(net_addr.family)

        try:
            print "client trying connection to: %s" % (net_addr)
            sock.connect(net_addr, timeout=io.seconds_to_interval(timeout_secs))
            print "client connected to: %s" % (net_addr)
            valid_addr = True
            break
        except Exception, e:
            sock.close()
            print "client connection to: %s failed (%s)" % (net_addr, e)

    if not valid_addr:
        print "Could not establish valid address for \"%s\" in family %s" % \
        (hostname, io.addr_family_name(family))
        return

    # Talk to the server
    try:
        sock.send("Hello")
        buf = sock.recv(1024)
        if not buf:
            print "client lost connection"
            sock.close()
            return
        print "client received: %s" % (buf)
    except Exception, e:
        print e.strerror
        try:
            sock.close()