Add supybot.utils.net.getAddressFromHostname() and improve Socket driver to try successively all IP addresses.

This commit is contained in:
Valentin Lorentz 2013-05-31 17:21:10 +02:00
parent 9ea1019970
commit 2016983d35
2 changed files with 14 additions and 2 deletions

View File

@ -72,6 +72,7 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
drivers.IrcDriver.__init__(self, irc) drivers.IrcDriver.__init__(self, irc)
drivers.ServersMixin.__init__(self, irc) drivers.ServersMixin.__init__(self, irc)
self.conn = None self.conn = None
self._attempt = -1
self.servers = () self.servers = ()
self.eagains = 0 self.eagains = 0
self.inbuffer = b'' self.inbuffer = b''
@ -239,6 +240,7 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
self.reconnect(reset=False, **kwargs) self.reconnect(reset=False, **kwargs)
def reconnect(self, reset=True): def reconnect(self, reset=True):
self._attempt += 1
self.nextReconnectTime = None self.nextReconnectTime = None
if self.connected: if self.connected:
drivers.log.reconnect(self.irc.network) drivers.log.reconnect(self.irc.network)
@ -256,6 +258,8 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
else: else:
drivers.log.debug('Not resetting %s.', self.irc) drivers.log.debug('Not resetting %s.', self.irc)
server = self._getNextServer() server = self._getNextServer()
address = utils.net.getAddressFromHostname(server[0],
attempt=self._attempt)
drivers.log.connect(self.currentServer) drivers.log.connect(self.currentServer)
try: try:
socks_proxy = getattr(conf.supybot.networks, self.irc.network) \ socks_proxy = getattr(conf.supybot.networks, self.irc.network) \
@ -267,7 +271,7 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
log.error('Cannot use socks proxy (SocksiPy not installed), ' log.error('Cannot use socks proxy (SocksiPy not installed), '
'using direct connection instead.') 'using direct connection instead.')
socks_proxy = '' socks_proxy = ''
self.conn = utils.net.getSocket(server[0], socks_proxy) self.conn = utils.net.getSocket(address, socks_proxy)
vhost = conf.supybot.protocols.irc.vhost() vhost = conf.supybot.protocols.irc.vhost()
self.conn.bind((vhost, 0)) self.conn.bind((vhost, 0))
except socket.error, e: except socket.error, e:
@ -281,7 +285,7 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
if getattr(conf.supybot.networks, self.irc.network).ssl(): if getattr(conf.supybot.networks, self.irc.network).ssl():
assert globals().has_key('ssl') assert globals().has_key('ssl')
self.conn = ssl.wrap_socket(self.conn) self.conn = ssl.wrap_socket(self.conn)
self.conn.connect(server) self.conn.connect((address, server[1]))
def setTimeout(): def setTimeout():
self.conn.settimeout(conf.supybot.drivers.poll()) self.conn.settimeout(conf.supybot.drivers.poll())
conf.supybot.drivers.poll.addCallback(setTimeout) conf.supybot.drivers.poll.addCallback(setTimeout)

View File

@ -80,6 +80,14 @@ class EmailRe:
return count >= 1 return count >= 1
emailRe = EmailRe() emailRe = EmailRe()
def getAddressFromHostname(host, attempt=0):
addrinfo = socket.getaddrinfo(host, None)
addresses = []
for (family, socktype, proto, canonname, sockaddr) in addrinfo:
if sockaddr[0] not in addresses:
addresses.append(sockaddr[0])
return addresses[attempt % len(addresses)]
def getSocket(host, socks_proxy=None): def getSocket(host, socks_proxy=None):
"""Returns a socket of the correct AF_INET type (v4 or v6) in order to """Returns a socket of the correct AF_INET type (v4 or v6) in order to
communicate with host. communicate with host.