1# 2# Copyright (c) 2011 Thomas Graf <tgraf@suug.ch> 3# 4 5"""IPv4 6 7""" 8 9from __future__ import absolute_import 10 11__all__ = [ 12 '', 13] 14 15from ... import core as netlink 16from .. import capi as capi 17from ... import util as util 18DEVCONF_FORWARDING = 1 19DEVCONF_MC_FORWARDING = 2 20DEVCONF_PROXY_ARP = 3 21DEVCONF_ACCEPT_REDIRECTS = 4 22DEVCONF_SECURE_REDIRECTS = 5 23DEVCONF_SEND_REDIRECTS = 6 24DEVCONF_SHARED_MEDIA = 7 25DEVCONF_RP_FILTER = 8 26DEVCONF_ACCEPT_SOURCE_ROUTE = 9 27DEVCONF_BOOTP_RELAY = 10 28DEVCONF_LOG_MARTIANS = 11 29DEVCONF_TAG = 12 30DEVCONF_ARPFILTER = 13 31DEVCONF_MEDIUM_ID = 14 32DEVCONF_NOXFRM = 15 33DEVCONF_NOPOLICY = 16 34DEVCONF_FORCE_IGMP_VERSION = 17 35DEVCONF_ARP_ANNOUNCE = 18 36DEVCONF_ARP_IGNORE = 19 37DEVCONF_PROMOTE_SECONDARIES = 20 38DEVCONF_ARP_ACCEPT = 21 39DEVCONF_ARP_NOTIFY = 22 40DEVCONF_ACCEPT_LOCAL = 23 41DEVCONF_SRC_VMARK = 24 42DEVCONF_PROXY_ARP_PVLAN = 25 43DEVCONF_MAX = DEVCONF_PROXY_ARP_PVLAN 44 45def _resolve(id): 46 if type(id) is str: 47 id = capi.rtnl_link_inet_str2devconf(id)[0] 48 if id < 0: 49 raise NameError('unknown configuration id') 50 return id 51 52class InetLink(object): 53 def __init__(self, link): 54 self._link = link 55 56 def details(self, fmt): 57 buf = fmt.nl('\n\t{0}\n\t'.format(util.title('Configuration:'))) 58 59 for i in range(DEVCONF_FORWARDING, DEVCONF_MAX+1): 60 if i & 1 and i > 1: 61 buf += fmt.nl('\t') 62 txt = util.kw(capi.rtnl_link_inet_devconf2str(i, 32)[0]) 63 buf += fmt.format('{0:28s} {1:12} ', txt, 64 self.get_conf(i)) 65 66 67 return buf 68 69 def get_conf(self, id): 70 return capi.inet_get_conf(self._link._rtnl_link, _resolve(id)) 71 72 def set_conf(self, id, value): 73 return capi.rtnl_link_inet_set_conf(self._link._rtnl_link, 74 _resolve(id), int(value)) 75 76 @property 77 @netlink.nlattr(type=bool, fmt=util.boolean) 78 def forwarding(self): 79 return bool(self.get_conf(DEVCONF_FORWARDING)) 80 81 @forwarding.setter 82 def forwarding(self, value): 83 self.set_conf(DEVCONF_FORWARDING, int(value)) 84 85 86 @property 87 @netlink.nlattr(type=bool, fmt=util.boolean) 88 def mc_forwarding(self): 89 return bool(self.get_conf(DEVCONF_MC_FORWARDING)) 90 91 @mc_forwarding.setter 92 def mc_forwarding(self, value): 93 self.set_conf(DEVCONF_MC_FORWARDING, int(value)) 94 95 96 @property 97 @netlink.nlattr(type=bool, fmt=util.boolean) 98 def proxy_arp(self): 99 return bool(self.get_conf(DEVCONF_PROXY_ARP)) 100 101 @proxy_arp.setter 102 def proxy_arp(self, value): 103 self.set_conf(DEVCONF_PROXY_ARP, int(value)) 104 105 @property 106 @netlink.nlattr(type=bool, fmt=util.boolean) 107 def accept_redirects(self): 108 return bool(self.get_conf(DEVCONF_ACCEPT_REDIRECTS)) 109 110 @accept_redirects.setter 111 def accept_redirects(self, value): 112 self.set_conf(DEVCONF_ACCEPT_REDIRECTS, int(value)) 113 114 @property 115 @netlink.nlattr(type=bool, fmt=util.boolean) 116 def secure_redirects(self): 117 return bool(self.get_conf(DEVCONF_SECURE_REDIRECTS)) 118 119 @secure_redirects.setter 120 def secure_redirects(self, value): 121 self.set_conf(DEVCONF_SECURE_REDIRECTS, int(value)) 122 123 @property 124 @netlink.nlattr(type=bool, fmt=util.boolean) 125 def send_redirects(self): 126 return bool(self.get_conf(DEVCONF_SEND_REDIRECTS)) 127 128 @send_redirects.setter 129 def send_redirects(self, value): 130 self.set_conf(DEVCONF_SEND_REDIRECTS, int(value)) 131 132 @property 133 @netlink.nlattr(type=bool, fmt=util.boolean) 134 def shared_media(self): 135 return bool(self.get_conf(DEVCONF_SHARED_MEDIA)) 136 137 @shared_media.setter 138 def shared_media(self, value): 139 self.set_conf(DEVCONF_SHARED_MEDIA, int(value)) 140 141# IPV4_DEVCONF_RP_FILTER, 142# IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE, 143# IPV4_DEVCONF_BOOTP_RELAY, 144# IPV4_DEVCONF_LOG_MARTIANS, 145# IPV4_DEVCONF_TAG, 146# IPV4_DEVCONF_ARPFILTER, 147# IPV4_DEVCONF_MEDIUM_ID, 148# IPV4_DEVCONF_NOXFRM, 149# IPV4_DEVCONF_NOPOLICY, 150# IPV4_DEVCONF_FORCE_IGMP_VERSION, 151# IPV4_DEVCONF_ARP_ANNOUNCE, 152# IPV4_DEVCONF_ARP_IGNORE, 153# IPV4_DEVCONF_PROMOTE_SECONDARIES, 154# IPV4_DEVCONF_ARP_ACCEPT, 155# IPV4_DEVCONF_ARP_NOTIFY, 156# IPV4_DEVCONF_ACCEPT_LOCAL, 157# IPV4_DEVCONF_SRC_VMARK, 158# IPV4_DEVCONF_PROXY_ARP_PVLAN, 159