3 """A caching layer around the kernel's network mapping between IPs and MACs"""
10 from typing import Any, Optional
12 from overrides import overrides
21 from collect.bidict import BiDict
23 logger = logging.getLogger(__name__)
25 cfg = config.add_commandline_args(
26 f'MAC <--> IP Address mapping table cache ({__file__})',
27 'Commandline args related to MAC <--> IP Address mapping',
30 '--arper_cache_location',
31 default=site_config.get_config().arper_cache_file,
33 help='Where to cache the kernel ARP table',
36 '--arper_supplimental_cache_location',
37 default=site_config.get_config(site_config.other_location()).arper_cache_file,
39 help='Where someone else is caching the kernel ARP table',
42 '--arper_cache_max_staleness',
43 type=argparse_utils.valid_duration,
44 default=datetime.timedelta(seconds=60 * 15),
46 help='Max acceptable age of the kernel arp table cache',
49 '--arper_min_entries_to_be_valid',
51 default=site_config.get_config().arper_minimum_device_count,
52 help='Min number of arp entries to bother persisting.',
56 @persistent.persistent_autoloaded_singleton() # type: ignore
57 class Arper(persistent.Persistent):
60 cached_local_state: Optional[BiDict] = None,
61 cached_supplimental_state: Optional[BiDict] = None,
64 if cached_local_state is not None:
65 logger.debug('Loading Arper map from cached local state.')
66 self.state = cached_local_state
68 logger.debug('No usable cached state; calling /usr/sbin/arp')
69 self.update_from_arp_scan()
70 self.update_from_arp()
71 if len(self.state) < config.config['arper_min_entries_to_be_valid']:
72 raise Exception(f'Arper didn\'t find enough entries; only got {len(self.state)}.')
73 if cached_supplimental_state is not None:
74 logger.debug(f'Also added {len(cached_supplimental_state)} supplimental entries.')
75 for mac, ip in cached_supplimental_state.items():
77 for mac, ip in self.state.items():
78 logger.debug(f'{mac} <-> {ip}')
80 def update_from_arp_scan(self):
81 network_spec = site_config.get_config().network
83 output = exec_utils.cmd(
84 f'/usr/local/bin/arp-scan --retry=6 --timeout 350 --backoff=1.4 --random --numeric --plain --ignoredups {network_spec}',
87 except Exception as e:
90 for line in output.split('\n'):
91 ip = string_utils.extract_ip_v4(line)
92 mac = string_utils.extract_mac_address(line)
93 if ip is not None and mac is not None and mac != 'UNKNOWN' and ip != 'UNKNOWN':
95 logger.debug(f'ARPER: {mac} => {ip}')
98 def update_from_arp(self):
100 output = exec_utils.cmd('/usr/sbin/arp -a', timeout_seconds=10.0)
101 except Exception as e:
104 for line in output.split('\n'):
105 ip = string_utils.extract_ip_v4(line)
106 mac = string_utils.extract_mac_address(line)
107 if ip is not None and mac is not None and mac != 'UNKNOWN' and ip != 'UNKNOWN':
109 logger.debug(f'ARPER: {mac} => {ip}')
112 def get_ip_by_mac(self, mac: str) -> Optional[str]:
114 return self.state.get(mac, None)
116 def get_mac_by_ip(self, ip: str) -> Optional[str]:
117 return self.state.inverse.get(ip, None)
120 def load_state(cls, cache_file: str, freshness_threshold_sec: int, state: BiDict):
121 if not file_utils.file_is_readable(cache_file):
122 logger.debug(f'Can\'t read {cache_file}')
124 if persistent.was_file_written_within_n_seconds(
126 freshness_threshold_sec,
128 logger.debug(f'Loading state from {cache_file}')
130 with open(cache_file, 'r') as rf:
131 contents = rf.readlines()
132 for line in contents:
134 logger.debug(f'ARPER:{cache_file}> {line}')
135 (mac, ip) = line.split(',')
142 logger.debug(f'{cache_file} is too stale.')
146 def load(cls) -> Any:
147 local_state = BiDict()
148 cache_file = config.config['arper_cache_location']
149 max_staleness = config.config['arper_cache_max_staleness'].total_seconds()
150 logger.debug(f'Trying to load main arper cache from {cache_file}...')
151 cls.load_state(cache_file, max_staleness, local_state)
152 if len(local_state) <= config.config['arper_min_entries_to_be_valid']:
153 msg = f'{cache_file} is invalid: only {len(local_state)} entries. Deleting it.'
155 warnings.warn(msg, stacklevel=2)
157 os.remove(cache_file)
161 supplimental_state = BiDict()
162 cache_file = config.config['arper_supplimental_cache_location']
163 max_staleness = config.config['arper_cache_max_staleness'].total_seconds()
164 logger.debug(f'Trying to suppliment arper state from {cache_file}...')
165 cls.load_state(cache_file, max_staleness, supplimental_state)
166 if len(supplimental_state) == 0:
167 supplimental_state = None
169 if len(local_state) > 0:
170 return cls(local_state, supplimental_state)
174 def save(self) -> bool:
175 if len(self.state) > config.config['arper_min_entries_to_be_valid']:
176 logger.debug(f'Persisting state to {config.config["arper_cache_location"]}')
177 with file_utils.FileWriter(config.config['arper_cache_location']) as wf:
178 for (mac, ip) in self.state.items():
180 print(f'{mac}, {ip}', file=wf)
184 f'Only saw {len(self.state)} entries; needed at least {config.config["arper_min_entries_to_be_valid"]} to bother persisting.'