def activate_interface(dummy=None): """Turns the dummy interface 'up'""" dummy = dummy or DUMMY try: systemArgs('ifconfig', dummy, 'up') except ErrorcodeException, e: _error(9, "Could not activate interface", dummy, *e)
def get_current_interfaces(dummy=None): """Retrieves current IPs active for given/default dummy interface""" dummy = dummy or DUMMY try: ip_output = systemArgs('ip', 'addr') except ErrorcodeException, e: _error(7, "Could not run 'ip addr', error", *e)
def update_ip6(iface='eth0', real_ip=None, conf=None): """Removes inactive IPv6s and adds new IPv6s to eth0 interface""" (should_have, should_have_ip6) = get_my_virtualservers(real_ip=real_ip, conf=conf) has_already_ip6 = get_current_ip6() # must_delete = filter(lambda x:x not in should_have, has_already) # must_delete = [ip for ip in has_already if ip not in should_have] # Using sets is a lot more sexy.... :) must_delete = has_already_ip6 - should_have_ip6 must_add = should_have_ip6 - has_already_ip6 _debug("Deleting", must_delete) for ip6 in must_delete: try: systemArgs('ip', '-6','addr', 'del', ip6, 'dev', iface) except ErrorcodeException, e: # The error code if 'ip addr del' says # "RTNETLINK answers: Cannot assign requested address" if e[0] == 2: _warning("Ignoring already removed ip", ip6) else: _error(10, "Could not remove", ip6)
def update_interfaces(dummy=None, real_ip=None, conf=None): """Removes inactive IPs and adds new IPs to dummy interface""" dummy = dummy or DUMMY should_have = get_my_virtualservers(real_ip=real_ip, conf=conf) has_already = get_current_interfaces(dummy) # must_delete = filter(lambda x:x not in should_have, has_already) # must_delete = [ip for ip in has_already if ip not in should_have] # Using sets is a lot more sexy.... :) must_delete = has_already - should_have must_add = should_have - has_already _debug("Deleting", must_delete) for ip in must_delete: try: systemArgs('ip', 'addr', 'del', ip, 'dev', dummy) except ErrorcodeException, e: # The error code if 'ip addr del' says # "RTNETLINK answers: Cannot assign requested address" if e[0] == 2: _warning("Ignoring already removed ip", ip) else: _error(10, "Could not remove", ip)
def hide_interface(dummy=None): """Makes the dummy interface 'hidden' to avoid ARP responses and related problems.""" dummy = dummy or DUMMY from os import uname try: if (uname()[2].find('2.6') > -1): # litt annen vri i 2.6 open("/proc/sys/net/ipv4/conf/%s/arp_announce" % dummy, "w").write("2\n") open("/proc/sys/net/ipv4/conf/%s/arp_ignore" % dummy, "w").write("1\n") open("/proc/sys/net/ipv4/conf/all/arp_announce", "w").write("2\n") open("/proc/sys/net/ipv4/conf/all/arp_ignore" , "w").write("1\n") else: open("/proc/sys/net/ipv4/conf/%s/hidden" % dummy, "w").write("1\n") # NEeded according to Magnus.. open("/proc/sys/net/ipv4/conf/all/hidden", "w").write("1\n") except IOError, e: # Turning off interface since we can't hide it try: systemArgs('ifconfig', dummy, 'down') except ErrorcodeException: pass _error(8, "Could not set interface", dummy, "hidden", *e)
def hide_interface(dummy=None): """Makes the dummy interface 'hidden' to avoid ARP responses and related problems.""" dummy = dummy or DUMMY try: if os.uname()[2].startswith('2.4'): open("/proc/sys/net/ipv4/conf/%s/hidden" % dummy, "w").write("1\n") # This global setting is also needed open("/proc/sys/net/ipv4/conf/all/hidden", "w").write("1\n") else: # Assume 2.6 or later # Linux 2.6 does things slightly more verbose open("/proc/sys/net/ipv4/conf/%s/arp_announce" % dummy, "w").write("2\n") open("/proc/sys/net/ipv4/conf/%s/arp_ignore" % dummy, "w").write("1\n") # This global setting is also needed open("/proc/sys/net/ipv4/conf/all/arp_announce", "w").write("2\n") open("/proc/sys/net/ipv4/conf/all/arp_ignore" , "w").write("1\n") except IOError, e: # Turning off interface since we can't hide it try: systemArgs('ifconfig', dummy, 'down') except ErrorcodeException: pass _error(8, "Could not set interface", dummy, "hidden", *e)
def get_current_ip6(iface='eth0'): """Retrieves current IPs active for given/default dummy interface""" try: ip_output = systemArgs('ip', '-6', 'addr', 'show', 'dev', iface) except ErrorcodeException, e: _error(7, "Could not run 'ip addr', error", *e)
must_add = should_have - has_already _debug("Deleting", must_delete) for ip in must_delete: try: systemArgs('ip', 'addr', 'del', "%s/32" % ip, 'dev', dummy) except ErrorcodeException, e: # The error code if 'ip addr del' says # "RTNETLINK answers: Cannot assign requested address" if e[0] == 2: _warning("Ignoring already removed ip", ip) else: _error(10, "Could not remove", ip) _debug("Adding", must_add) for ip in must_add: try: systemArgs('ip', 'addr', 'add', ip, 'dev', dummy) except ErrorcodeException, e: # The error code if 'ip addr add' says # "RTNETLINK answers: File exists" if e[0] == 2: _warning("Ignoring already added ip", ip) else: _error(11, "Could not add", ip) if should_have: # make interface hidden if we have any active IPs # (if no IPv4-addresses are on interface, hide_interface() # won't work) hide_interface(dummy) def activate_interface(dummy=None): """Turns the dummy interface 'up'"""
must_add = should_have - has_already _debug("Deleting", must_delete) for ip in must_delete: try: systemArgs('ip', 'addr', 'del', ip, 'dev', dummy) except ErrorcodeException, e: # The error code if 'ip addr del' says # "RTNETLINK answers: Cannot assign requested address" if e[0] == 2: _warning("Ignoring already removed ip", ip) else: _error(10, "Could not remove", ip) _debug("Adding", must_add) for ip in must_add: try: systemArgs('ip', 'addr', 'add', ip, 'dev', dummy) except ErrorcodeException, e: # The error code if 'ip addr add' says # "RTNETLINK answers: File exists" if e[0] == 2: _warning("Ignoring already added ip", ip) else: _error(11, "Could not add", ip) if should_have: # make interface hidden if we have any active IPs # (if no IPv4-addresses are on interface, hide_interface() # won't work) hide_interface(dummy) def activate_interface(dummy=None): """Turns the dummy interface 'up'"""