def send(self, msg, handler): if not isinstance(handler, custom_handler): raise Exception("provided 'handler' is not a custom_handler instance") if self.busy == 1: raise AccessBusyError() self.busy = 1 py_nl_cb_set(self._rx_cb, NL_CB_VALID, NL_CB_CUSTOM, handler.handle, None) self._send(msg)
def __init__(self, level=NL_CB_DEFAULT): self._tx_cb = nl_cb_alloc(level) self._rx_cb = nl_cb_alloc(level) self._sock = nl_socket_alloc_cb(self._tx_cb) py_nl_cb_err(self._rx_cb, NL_CB_CUSTOM, self.error_handler, None) py_nl_cb_set(self._rx_cb, NL_CB_FINISH, NL_CB_CUSTOM, self.finish_handler, None) py_nl_cb_set(self._rx_cb, NL_CB_ACK, NL_CB_CUSTOM, self.ack_handler, None) nl_connect(self._sock, NETLINK_GENERIC) self._family = genl_ctrl_resolve(self._sock, 'nl80211') self._get_protocol_features()
def finish_handler(m, a): return nl.NL_SKIP def ack_handler(m, a): a.done = 0 return nl.NL_STOP try: cbd = test_class() tx_cb = nl.nl_cb_alloc(nl.NL_CB_DEFAULT) rx_cb = nl.nl_cb_clone(tx_cb) s = nl.nl_socket_alloc_cb(tx_cb) nl.py_nl_cb_err(rx_cb, nl.NL_CB_CUSTOM, error_handler, cbd) nl.py_nl_cb_set(rx_cb, nl.NL_CB_FINISH, nl.NL_CB_CUSTOM, finish_handler, cbd) nl.py_nl_cb_set(rx_cb, nl.NL_CB_ACK, nl.NL_CB_CUSTOM, ack_handler, cbd) nl.py_nl_cb_set(rx_cb, nl.NL_CB_VALID, nl.NL_CB_CUSTOM, msg_handler, cbd) genl.genl_connect(s) family = genl.genl_ctrl_resolve(s, 'nl80211') m = nl.nlmsg_alloc() genl.genlmsg_put(m, 0, 0, family, 0, 0, nl80211.NL80211_CMD_GET_WIPHY, 0) nl.nla_put_u32(m, nl80211.NL80211_ATTR_WIPHY, 0) err = nl.nl_send_auto_complete(s, m) if err < 0: nl.nlmsg_free(msg) while cbd.done > 0 and not err < 0: err = nl.nl_recvmsgs(s, rx_cb)
def finish_handler(m, a): return nl.NL_SKIP def ack_handler(m, a): a.done = 0 return nl.NL_STOP try: cbd = test_class() tx_cb = nl.nl_cb_alloc(nl.NL_CB_DEFAULT) rx_cb = nl.nl_cb_clone(tx_cb) s = nl.nl_socket_alloc_cb(tx_cb) nl.py_nl_cb_err(rx_cb, nl.NL_CB_CUSTOM, error_handler, cbd); # nl.py_nl_cb_set(rx_cb, nl.NL_CB_FINISH, nl.NL_CB_CUSTOM, finish_handler, cbd); nl.py_nl_cb_set(rx_cb, nl.NL_CB_ACK, nl.NL_CB_CUSTOM, ack_handler, cbd); nl.py_nl_cb_set(rx_cb, nl.NL_CB_VALID, nl.NL_CB_CUSTOM, msg_handler, cbd); genl.genl_connect(s) family = genl.genl_ctrl_resolve(s, 'nl80211') m = nl.nlmsg_alloc() genl.genlmsg_put(m, 0, 0, family, 0, 0, nl80211.NL80211_CMD_GET_INTERFACE, 0) nl.nla_put_u32(m, nl80211.NL80211_ATTR_IFINDEX, nl.if_nametoindex('wlan0')) err = nl.nl_send_auto_complete(s, m); if err < 0: nl.nlmsg_free(msg) while cbd.done > 0 and not err < 0: err = nl.nl_recvmsgs(s, rx_cb)
def enalbe_seq_check(self): py_nl_cb_set(self._rx_cb, NL_CB_SEQ_CHECK, NL_CB_DEFAULT, None, None)
def disable_seq_check(self): py_nl_cb_set(self._rx_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, self.noseq, None)
def _get_protocol_features(self): msg = self.alloc_genlmsg(nl80211.CMD_GET_PROTOCOL_FEATURES) self.busy = 1 py_nl_cb_set(self._rx_cb, NL_CB_VALID, NL_CB_CUSTOM, self._protocol_feature_handler, None) self._send(msg)
def __init__(self,lig_program, mapresolver=None,simulate=None): self.done = 1; # by default, should be possible to override self.mapresolver = mapresolver or "153.16.49.112"; self.simulate = simulate self.lig_program = lig_program # TODO replace by Socket # allocates callback tx_cb = nl.nl_cb_alloc(nl.NL_CB_DEFAULT) #Clone an existing callback handle self.rx_cb = nl.nl_cb_clone(tx_cb) # allocates sockets self.sk = nl.nl_socket_alloc_cb(tx_cb) # set callback handers # last parameter represents arguments to pass logger.info("Setting callback functions") # nl.py_nl_cb_err(self.rx_cb, nl.NL_CB_CUSTOM, error_handler, self); # nl_cb_set( callback_set, type, kind, function,args ) nl.py_nl_cb_set(self.rx_cb, nl.NL_CB_FINISH, nl.NL_CB_VERBOSE, finish_handler, self); nl.py_nl_cb_set(self.rx_cb, nl.NL_CB_ACK, nl.NL_CB_VERBOSE, ack_handler, self); nl.py_nl_cb_set(self.rx_cb, nl.NL_CB_VALID, nl.NL_CB_CUSTOM, msg_handler, self); # nl.py_nl_cb_set(self.rx_cb, nl.NL_CB_VALID, nl.NL_CB_CUSTOM, self.handle, self); nl.py_nl_cb_set(self.rx_cb, nl.NL_CB_INVALID, nl.NL_CB_DEBUG, None, None); # Notifications do not use sequence numbers, disable sequence number checking. # nl.nl_socket_disable_seq_check(self.sk); # MANDATORY because of a bug in netlink that sends -NLE_SEQ_MISMATCH otherwise nl.nl_socket_disable_auto_ack(self.sk); # establish connection genl.genl_connect(self.sk) self.family_id = genl.genl_ctrl_resolve(self.sk, LIG_FAMILY_NAME) # register to the multicast group # print( dir( sys.modules["netlink.genl.capi"]) ) # print( dir( sys.modules["netlink.capi"]) ) logger.info("family %s registered with number %d"%(LIG_FAMILY_NAME, self.family_id)); self.group_id = genl.genl_ctrl_resolve_grp (self.sk, LIG_FAMILY_NAME, LIG_GROUP_NAME); if self.group_id < 0 : # should log it logger.error("Could not find group group %s. Is the adequate module loaded ?"%LIG_FAMILY_NAME) exit(1) logger.info("Group id found: %d" % self.group_id); logger.info("Using mapresolver %s"%self.mapresolver) if self.simulate: logger.info("Simulation mode enabled %d"%self.simulate) else: logger.info("Real mode enabled") ret = nl.nl_socket_add_membership(self.sk, self.group_id); if ret == 0: logger.info("Registration successful") else: logger.error("Could not register to group") exit(1)
def __init__(self, lig_program, mapresolver=None, simulate=None): self.done = 1 # by default, should be possible to override self.mapresolver = mapresolver or "153.16.49.112" self.simulate = simulate self.lig_program = lig_program # TODO replace by Socket # allocates callback tx_cb = nl.nl_cb_alloc(nl.NL_CB_DEFAULT) #Clone an existing callback handle self.rx_cb = nl.nl_cb_clone(tx_cb) # allocates sockets self.sk = nl.nl_socket_alloc_cb(tx_cb) # set callback handers # last parameter represents arguments to pass logger.info("Setting callback functions") # nl.py_nl_cb_err(self.rx_cb, nl.NL_CB_CUSTOM, error_handler, self); # nl_cb_set( callback_set, type, kind, function,args ) nl.py_nl_cb_set(self.rx_cb, nl.NL_CB_FINISH, nl.NL_CB_VERBOSE, finish_handler, self) nl.py_nl_cb_set(self.rx_cb, nl.NL_CB_ACK, nl.NL_CB_VERBOSE, ack_handler, self) nl.py_nl_cb_set(self.rx_cb, nl.NL_CB_VALID, nl.NL_CB_CUSTOM, msg_handler, self) # nl.py_nl_cb_set(self.rx_cb, nl.NL_CB_VALID, nl.NL_CB_CUSTOM, self.handle, self); nl.py_nl_cb_set(self.rx_cb, nl.NL_CB_INVALID, nl.NL_CB_DEBUG, None, None) # Notifications do not use sequence numbers, disable sequence number checking. # nl.nl_socket_disable_seq_check(self.sk); # MANDATORY because of a bug in netlink that sends -NLE_SEQ_MISMATCH otherwise nl.nl_socket_disable_auto_ack(self.sk) # establish connection genl.genl_connect(self.sk) self.family_id = genl.genl_ctrl_resolve(self.sk, LIG_FAMILY_NAME) # register to the multicast group # print( dir( sys.modules["netlink.genl.capi"]) ) # print( dir( sys.modules["netlink.capi"]) ) logger.info("family %s registered with number %d" % (LIG_FAMILY_NAME, self.family_id)) self.group_id = genl.genl_ctrl_resolve_grp(self.sk, LIG_FAMILY_NAME, LIG_GROUP_NAME) if self.group_id < 0: # should log it logger.error( "Could not find group group %s. Is the adequate module loaded ?" % LIG_FAMILY_NAME) exit(1) logger.info("Group id found: %d" % self.group_id) logger.info("Using mapresolver %s" % self.mapresolver) if self.simulate: logger.info("Simulation mode enabled %d" % self.simulate) else: logger.info("Real mode enabled") ret = nl.nl_socket_add_membership(self.sk, self.group_id) if ret == 0: logger.info("Registration successful") else: logger.error("Could not register to group") exit(1)