def test_addrs(self, table, *addrs): """Test if one or more addresses match a table. 'table' can be either a PFTable instance or a string containing the table name; 'addrs' can be either PFTableAddr instances or strings. Return the addresses that match. """ if isinstance(table, basestring): table = pfr_table(pfrt_name=table) else: table = pfr_table(pfrt_name=table.name, pfrt_anchor=table.anchor) _addrs = [] for addr in addrs: if isinstance(addr, PFTableAddr): _addrs.append(addr) else: _addrs.append(PFTableAddr(addr)) io = pfioc_table(pfrio_table=table, pfrio_esize=sizeof(pfr_addr), pfrio_size=len(addrs)) buffer = (pfr_addr * len(addrs))(*[a._to_struct() for a in _addrs]) io.pfrio_buffer = addressof(buffer) with open(self.dev, 'w') as d: ioctl(d, DIOCRTSTADDRS, io) return tuple([PFTableAddr(a) for a in buffer[:io.pfrio_size] if a.pfra_fback])
def get_addrs(self, table, buf_size=10): """Get the addresses in the specified table. 'table' can be either a PFTable instance or a string containing the table name. Return a list of PFTableAddr objects. """ if isinstance(table, basestring): table = pfr_table(pfrt_name=table) else: table = pfr_table(pfrt_name=table.name, pfrt_anchor=table.anchor) io = pfioc_table(pfrio_table=table, pfrio_esize=sizeof(pfr_addr)) with open(self.dev, 'w') as d: while True: buffer = (pfr_addr * buf_size)() io.pfrio_buffer = addressof(buffer) io.pfrio_size = buf_size ioctl(d, DIOCRGETADDRS, io) if io.pfrio_size <= buf_size: break buf_size = io.pfrio_size return tuple([PFTableAddr(a) for a in buffer[:io.pfrio_size]])
def set_addrs(self, table, *addrs): """Replace the content of a table. 'table' can be either a PFTable instance or a string containing the table name; 'addrs' can be either PFTableAddr instances or strings. Return a tuple containing the number of addresses deleted, added and changed. """ if isinstance(table, basestring): table = pfr_table(pfrt_name=table) else: table = pfr_table(pfrt_name=table.name, pfrt_anchor=table.anchor) _addrs = [] for addr in addrs: if isinstance(addr, PFTableAddr): _addrs.append(addr) else: _addrs.append(PFTableAddr(addr)) io = pfioc_table(pfrio_table=table, pfrio_esize=sizeof(pfr_addr), pfrio_size=len(addrs)) buffer = (pfr_addr * len(addrs))(*[a._to_struct() for a in _addrs]) io.pfrio_buffer = addressof(buffer) with open(self.dev, 'w') as d: ioctl(d, DIOCRSETADDRS, io) return (io.pfrio_ndel, io.pfrio_nadd, io.pfrio_nchange)
def del_addrs(self, table, *addrs): """Delete one or more addresses from the specified table. 'table' can be either a PFTable instance or a string containing the table name; 'addrs' can be either PFTableAddr instances or strings. Return the number of addresses deleted. """ if isinstance(table, basestring): table = pfr_table(pfrt_name=table) else: table = pfr_table(pfrt_name=table.name, pfrt_anchor=table.anchor) _addrs = [] for addr in addrs: if isinstance(addr, PFTableAddr): _addrs.append(addr) else: _addrs.append(PFTableAddr(addr)) io = pfioc_table(pfrio_table=table, pfrio_esize=sizeof(pfr_addr), pfrio_size=len(addrs)) buffer = (pfr_addr * len(addrs))(*[a._to_struct() for a in _addrs]) io.pfrio_buffer = addressof(buffer) with open(self.dev, 'w') as d: ioctl(d, DIOCRDELADDRS, io) return io.pfrio_ndel