def send_batch(nl, b, portid): fd = nl.get_fd() size = b.size() try: nl.sendto(b.head()) except Exception as e: print("mnl_socket_sendto: %s" % e, file=sys.stderr) raise while True: rlist, _wlist, _xlist = select.select([fd], [], [], 0.0) if not fd in rlist: break try: rcv_buf = nl.recv(mnl.MNL_SOCKET_BUFFER_SIZE) if len(rcv_buf) == 0: break except Exception as e: print("mnl_socket_recvfrom: %s" % e, file=sys.stderr) raise try: mnl.cb_run2(rcv_buf, 0, portid, None, None, CB_CTL_ARRAY) except Exception as e: print("mnl_cb_run2: %s" % e, file=sys.stderr) raise
def test_cb_run(self): self.assertEqual(mnl.cb_run(self.nlmsghdr_noop, 1, 1, None, None), mnl.MNL_CB_OK) self.assertEqual(mnl.cb_run(self.nlmsghdr_mintype, 1, 1, None, None), mnl.MNL_CB_OK) try: mnl.cb_run(self.nlmsghdr_error, 1, 1, None, None) except OSError as e: self.assertEquals(e.errno, errno.EPERM) else: self.fail("not raise OSError") @mnl.mnl_cb_t def cb_data(h, d): d is not None and d.append(h.nlmsg_type) if h.nlmsg_type == 0xff: ctypes.set_errno(errno.ENOBUFS) return mnl.MNL_CB_ERROR elif h.nlmsg_type == 0x7f: return mnl.MNL_CB_STOP else: return mnl.MNL_CB_OK l = [] try: mnl.cb_run(self.nlmsghdr_typeFF, 1, 1, cb_data, l) except OSError as e: self.assertEquals(e.errno, errno.ENOBUFS) else: self.fail("not raise OSError") l = [] ret = mnl.cb_run(self.nlmsghdr_type7F, 1, 1, cb_data, l) self.assertEqual(ret, mnl.MNL_CB_STOP) self.assertEqual(l[0], netlink.NLMSG_MIN_TYPE) self.assertEqual(l[1], 0x7f) try: mnl.cb_run(self.nlmsghdr_pid2, 1, 1, cb_data, None) except OSError as e: self.assertEqual(e.errno, errno.ESRCH) else: self.fail("not raise OSError") try: mnl.cb_run(self.nlmsghdr_seq2, 1, 1, cb_data, None) except OSError as e: self.assertEqual(e.errno, errno.EPROTO) else: self.fail("not raise OSError") # Python2.5 returns -1 but could not get EINTR? if sys.version_info < (2, 6): ret = mnl.cb_run2(self.nlmsghdr_intr, 1, 1, cb_data, None) self.assertEquals(ret, mnl.MNL_CB_ERROR) else: try: mnl.cb_run2(self.nlmsghdr_intr, 1, 1, cb_data, None) except OSError as e: self.assertEqual(e.errno, errno.EINTR) else: self.fail("not raise OSError")
def test_cb_run2(self): self.assertEqual(mnl.cb_run2(self.nlmsghdr_noop, 1, 1, None, None), mnl.MNL_CB_OK) self.assertEqual(mnl.cb_run2(self.nlmsghdr_mintype, 1, 1, None, None), mnl.MNL_CB_OK) try: mnl.cb_run2(self.nlmsghdr_error, 1, 1, None, None) except OSError as e: self.assertEquals(e.errno, errno.EPERM) else: self.fail("not raise OSError") @mnl.mnl_cb_t def cb_data(h, d): d is not None and d.append(h.nlmsg_type) if h.nlmsg_type == 0xff: ctypes.set_errno(errno.ENOBUFS) return mnl.MNL_CB_ERROR elif h.nlmsg_type == 0x7f: return mnl.MNL_CB_STOP else: return mnl.MNL_CB_OK l = [] try: mnl.cb_run2(self.nlmsghdr_typeFF, 1, 1, cb_data, l) except OSError as e: self.assertEquals(e.errno, errno.ENOBUFS) else: self.fail("not raise OSError") l = [] ret = mnl.cb_run2(self.nlmsghdr_type7F, 1, 1, cb_data, l) self.assertEqual(ret, mnl.MNL_CB_STOP) self.assertEqual(l[0], netlink.NLMSG_MIN_TYPE) self.assertEqual(l[1], 0x7f) try: mnl.cb_run2(self.nlmsghdr_pid2, 1, 1, cb_data, None) except OSError as e: self.assertEqual(e.errno, errno.ESRCH) else: self.fail("not raise OSError") try: mnl.cb_run2(self.nlmsghdr_seq2, 1, 1, cb_data, None) except OSError as e: self.assertEqual(e.errno, errno.EPROTO) else: self.fail("not raise OSError") # Python2.5 returns -1 but could not get EINTR? if sys.version_info < (2, 6): ret = mnl.cb_run2(self.nlmsghdr_intr, 1, 1, cb_data, None) self.assertEquals(ret, mnl.MNL_CB_ERROR) else: try: mnl.cb_run2(self.nlmsghdr_intr, 1, 1, cb_data, None) except OSError as e: self.assertEqual(e.errno, errno.EINTR) else: self.fail("not raise OSError") # with crl cb @mnl.nlmsg_cb def cb_done(nlh, d): return mnl.MNL_CB_STOP @mnl.nlmsg_cb def cb_overrun(nlh, d): set_errno(errno.ENOSPC) return mnl.MNL_CB_ERROR @mnl.nlmsg_cb def cb_err(nlh, d): err = nlh.get_payload_as(netlink.Nlmsgerr) if nlh.nlmsg_len < nlh.size(netlink.Nlmsgerr.csize()): set_errno(errno.EBADMSG) return mnl.MNL_CB_ERROR if err.error < 0: en = - err.error else: en = err.error if errno == 0: return mnl.MNL_CB_STOP else: set_errno(en) return mnl.MNL_CB_ERROR cb_ctls = {netlink.NLMSG_OVERRUN: cb_overrun, netlink.NLMSG_DONE: cb_done, netlink.NLMSG_ERROR: cb_err} self.assertEqual(mnl.cb_run2(self.nlmsghdr_noop, 1, 1, None, None, cb_ctls), mnl.MNL_CB_OK) self.assertEqual(mnl.cb_run2(self.nlmsghdr_done, 1, 1, None, None, cb_ctls), mnl.MNL_CB_STOP) try: mnl.cb_run2(self.nlmsghdr_overrun, 1, 1, None, None, cb_ctls) except OSError as e: self.assertEqual(e.errno, errno.ENOSPC) else: self.fail("not raise OSError") try: mnl.cb_run2(self.nlmsghdr_error, 1, 1, None, None, cb_ctls) except OSError as e: self.assertEqual(e.errno, errno.EPERM) else: self.fail("not raise OSError")