def is_legal_connection(address, allowed_hosts, logger): """ Return True if `address` is from an allowed host. address: string Connection address to be checked. allowed_hosts: list(string) IPv4 address patterns to check against. If a pattern ends with '.' (a domain) then the host address must begin with the pattern. Otherwise the host address must completely match the pattern. logger: :class:`logging.Logger` Used to output warnings about rejected connections. """ if address and connection.address_type(address) == 'AF_INET': host_addr = address[0] for pattern in allowed_hosts: if pattern[-1] == '.': # Any host in domain. if host_addr.startswith(pattern): return True elif host_addr == pattern: return True logger.warning('Rejecting connection from %r', address) return False else: # Presumably a pipe (AF_UNIX, AF_PIPE). return True
def __init__(self, address=None, family=None, backlog=500, authkey=None): family = family or (address and address_type(address)) \ or default_family if family != 'AF_INET': raise NotImplementedError backend = get_backend() # TODO(jiale) Add support for other address family for # backend.get_listen_addr address = address or backend.get_listen_addr() self._address = address _validate_family(family) if family == 'AF_PIPE': # PIPE cannot be used across machines raise NotImplementedError else: # Listens on '0.0.0.0' so that it accepts connection regardless of # net interfaces. When connect connects, it uses a specific IP # address. self._listener = SocketListener(('0.0.0.0', 0), family, backlog) if authkey is not None and not isinstance(authkey, bytes): raise TypeError('authkey should be a byte string') self._authkey = authkey
def SocketClient(address): ''' Return a connection object connected to the socket given by `address` ''' family = MP.address_type(address) s = socket.socket(getattr(socket, family)) defTimeOut = socket.getdefaulttimeout() s.settimeout(30) t = MP._init_timeout() while 1: try: s.connect(address) except socket.error as e: if e.args[0] != errno.ECONNREFUSED or MP._check_timeout(t): MP.debug('failed to connect to address %s', address) raise time.sleep(0.01) else: break else: raise s.settimeout(defTimeOut) fd = MP.duplicate(s.fileno()) conn = MP._multiprocessing.Connection(fd) s.close() return conn
def write_server_config(server, filename, real_ip=None): #pragma no cover """ Write server configuration information. server: OpenMDAO_Server Server to be recorded. filename: string Path to file to be written. real_ip: string If specified, the IP address to report (rather than possible tunnel) Connection information including IP address, port, and public key is written using :class:`ConfigParser`. """ parser = ConfigParser.ConfigParser() section = 'ServerInfo' parser.add_section(section) if connection.address_type(server.address) == 'AF_INET': parser.set(section, 'address', str(real_ip or server.address[0])) parser.set(section, 'port', str(server.address[1])) tunnel = real_ip is not None and \ socket.gethostbyname(real_ip) != server.address[0] parser.set(section, 'tunnel', str(tunnel)) else: parser.set(section, 'address', server.address) parser.set(section, 'port', '-1') parser.set(section, 'tunnel', str(False)) parser.set(section, 'key', server.public_key_text) logfile = os.path.join(os.getcwd(), 'openmdao_log.txt') parser.set(section, 'logfile', '%s:%s' % (socket.gethostname(), logfile)) with open(filename, 'w') as cfg: parser.write(cfg)
def write_server_config(server, filename, real_ip=None): # pragma no cover """ Write server configuration information. server: OpenMDAO_Server Server to be recorded. filename: string Path to file to be written. real_ip: string If specified, the IP address to report (rather than possible tunnel). Connection information including IP address, port, and public key is written using :class:`ConfigParser`. """ parser = ConfigParser.ConfigParser() section = "ServerInfo" parser.add_section(section) if connection.address_type(server.address) == "AF_INET": parser.set(section, "address", str(real_ip or server.address[0])) parser.set(section, "port", str(server.address[1])) tunnel = real_ip is not None and socket.gethostbyname(real_ip) != server.address[0] parser.set(section, "tunnel", str(tunnel)) else: parser.set(section, "address", server.address) parser.set(section, "port", "-1") parser.set(section, "tunnel", str(False)) parser.set(section, "key", server.public_key_text) logfile = os.path.join(os.getcwd(), "openmdao_log.txt") parser.set(section, "logfile", "%s:%s" % (socket.gethostname(), logfile)) parser.set(section, "version", __version__) with open(filename, "w") as cfg: parser.write(cfg)
def SocketClient(address, timeout=5): ''' Return a connection object connected to the socket given by `address` ''' family = address_type(address) s = socket.socket(getattr(socket, family)) if timeout: end_time = time.time() + timeout while 1: try: if timeout: cur_timeout = end_time - time.time() if cur_timeout <= 0: raise socket.timeout, "timed out." #s.settimeout(cur_timeout) s.connect(address) except socket.error, e: if e.args[0] != errno.ECONNREFUSED: # connection refused debug('failed to connect to address %s', address) raise time.sleep(0.01) else: break
def write_server_config(server, filename): # pragma no cover """ Write server connection information. server: OpenMDAO_Server Server to be recorded. filename: string Path to file to be written. Connection information including IP address, port, and public key is written using :class:`ConfigParser`. """ parser = ConfigParser.ConfigParser() section = "ServerInfo" parser.add_section(section) if connection.address_type(server.address) == "AF_INET": parser.set(section, "address", str(server.address[0])) parser.set(section, "port", str(server.address[1])) else: parser.set(section, "address", server.address) parser.set(section, "port", "-1") parser.set(section, "key", server.public_key_text) with open(filename, "w") as cfg: parser.write(cfg)
def Client(address, family=None, authkey=None, timeout=None): family = family or address_type(address) if family == 'AF_PIPE': c = PipeClient(address, timeout=timeout) else: c = SocketClient(address, timeout=timeout) if authkey is not None and not isinstance(authkey, bytes): raise TypeError, 'authkey should be a byte string' if authkey is not None: answer_challenge(c, authkey) deliver_challenge(c, authkey) return c
def SocketClient(address): ''' Return a connection object connected to the socket given by `address` ''' family = MP.address_type(address) s = socket.socket(getattr(socket, family)) defTimeOut = socket.getdefaulttimeout() s.settimeout(30) t = MP._init_timeout() while 1: try: s.connect(address) except socket.error, e: if e.args[0] != errno.ECONNREFUSED or MP._check_timeout(t): MP.debug('failed to connect to address %s', address) raise time.sleep(0.01) else: break
def SocketClient(address): ''' Return a connection object connected to the socket given by `address` ''' family = MP.address_type(address) s = socket.socket( getattr(socket, family) ) defTimeOut = socket.getdefaulttimeout() s.settimeout(30) t = MP._init_timeout() while 1: try: s.connect(address) except socket.error, e: if e.args[0] != errno.ECONNREFUSED or MP._check_timeout(t): MP.debug('failed to connect to address %s', address) raise time.sleep(0.01) else: break