def neigh(self, command, match=None, **kwarg): ''' Neighbours operations, same as `ip neigh` or `bridge fdb` * command -- add, delete, change, replace * match -- match rules * ifindex -- device index * family -- family: AF_INET, AF_INET6, AF_BRIDGE * \*\*kwarg -- msg fields and NLA Example:: pass ''' # FIXME: this is only a draft; all definitions should # be generalized flags_base = NLM_F_REQUEST | NLM_F_ACK flags_make = flags_base | NLM_F_CREATE | NLM_F_EXCL flags_change = flags_base | NLM_F_REPLACE flags_replace = flags_change | NLM_F_CREATE commands = {'add': (RTM_NEWNEIGH, flags_make), 'set': (RTM_NEWNEIGH, flags_replace), 'replace': (RTM_NEWNEIGH, flags_replace), 'change': (RTM_NEWNEIGH, flags_change), 'del': (RTM_DELNEIGH, flags_make), 'remove': (RTM_DELNEIGH, flags_make), 'delete': (RTM_DELNEIGH, flags_make)} (command, flags) = commands.get(command, command) msg = ndmsg() for field in msg.fields: msg[field[0]] = kwarg.pop(field[0], 0) msg['family'] = msg['family'] or AF_INET msg['attrs'] = [] # fix nud kwarg state = kwarg.pop('state', kwarg.pop('nud', 0)) if isinstance(state, basestring): # parse state string states = state.split(',') state = 0 for s in states: s = s.upper() if not s.startswith('NUD_'): s = 'NUD_' + s state |= NUD_NAMES[s] msg['state'] = state for key in kwarg: nla = ndmsg.name2nla(key) if kwarg[key] is not None: msg['attrs'].append([nla, kwarg[key]]) ret = self.nlm_request(msg, msg_type=command, msg_flags=flags) if match is not None: return self._match(match, ret) else: return ret
def neigh(self, command, match=None, **kwarg): ''' Neighbours operations, same as `ip neigh` or `bridge fdb` * command -- add, delete, change, replace * match -- match rules * ifindex -- device index * family -- family: AF_INET, AF_INET6, AF_BRIDGE * \*\*kwarg -- msg fields and NLA Example:: pass ''' # FIXME: this is only a draft; all definitions should # be generalized flags_base = NLM_F_REQUEST | NLM_F_ACK flags_make = flags_base | NLM_F_CREATE | NLM_F_EXCL flags_change = flags_base | NLM_F_REPLACE flags_replace = flags_change | NLM_F_CREATE commands = { 'add': (RTM_NEWNEIGH, flags_make), 'set': (RTM_NEWNEIGH, flags_replace), 'replace': (RTM_NEWNEIGH, flags_replace), 'change': (RTM_NEWNEIGH, flags_change), 'del': (RTM_DELNEIGH, flags_make), 'remove': (RTM_DELNEIGH, flags_make), 'delete': (RTM_DELNEIGH, flags_make) } (command, flags) = commands.get(command, command) msg = ndmsg() for field in msg.fields: msg[field[0]] = kwarg.pop(field[0], 0) msg['family'] = msg['family'] or AF_INET msg['attrs'] = [] for key in kwarg: nla = ndmsg.name2nla(key) if kwarg[key] is not None: msg['attrs'].append([nla, kwarg[key]]) ret = self.nlm_request(msg, msg_type=command, msg_flags=flags) if match is not None: return self._match(match, ret) else: return ret