lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | import netlink.capi as nl |
| 2 | import netlink.genl.capi as genl |
| 3 | import nl80211 |
| 4 | import sys |
| 5 | import traceback |
| 6 | |
| 7 | class test_class: |
| 8 | def __init__(self): |
| 9 | self.done = 1; |
| 10 | |
| 11 | def msg_handler(m, a): |
| 12 | try: |
| 13 | e, attr = genl.py_genlmsg_parse(nl.nlmsg_hdr(m), 0, |
| 14 | nl80211.NL80211_ATTR_MAX, None) |
| 15 | if nl80211.NL80211_ATTR_WIPHY in attr: |
| 16 | thiswiphy = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY]) |
| 17 | print("phy#%d" % thiswiphy) |
| 18 | if nl80211.NL80211_ATTR_IFNAME in attr: |
| 19 | print("\tinterface %s" % nl.nla_get_string(attr[nl80211.NL80211_ATTR_IFNAME])); |
| 20 | if nl80211.NL80211_ATTR_IFINDEX in attr: |
| 21 | print("\tifindex %d" % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFINDEX])) |
| 22 | if nl80211.NL80211_ATTR_WDEV in attr: |
| 23 | print("\twdev 0x%lx" % nl.nla_get_u64(attr[nl80211.NL80211_ATTR_WDEV])) |
| 24 | if nl80211.NL80211_ATTR_MAC in attr: |
| 25 | print("\tmac %02x:%02x:%02x:%02x:%02x:%02x" % tuple(nl.nla_data(attr[nl80211.NL80211_ATTR_MAC]))) |
| 26 | if nl80211.NL80211_ATTR_SSID in attr: |
| 27 | print("\tssid ", nl.nla_data(attr[nl80211.NL80211_ATTR_SSID])) |
| 28 | if nl80211.NL80211_ATTR_IFTYPE in attr: |
| 29 | iftype = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFTYPE]) |
| 30 | print("\ttype %s" % nl80211.nl80211_iftype2str[iftype]) |
| 31 | if nl80211.NL80211_ATTR_WIPHY_FREQ in attr: |
| 32 | freq = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY_FREQ]) |
| 33 | |
| 34 | sys.stdout.write("\tfreq %d MHz" % freq); |
| 35 | |
| 36 | if nl80211.NL80211_ATTR_CHANNEL_WIDTH in attr: |
| 37 | chanw = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CHANNEL_WIDTH]) |
| 38 | sys.stdout.write(", width: %s" % nl80211.nl80211_chan_width2str[chanw]) |
| 39 | if nl80211.NL80211_ATTR_CENTER_FREQ1 in attr: |
| 40 | sys.stdout.write(", center1: %d MHz" % |
| 41 | nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CENTER_FREQ1])) |
| 42 | if nl80211.NL80211_ATTR_CENTER_FREQ2 in attr: |
| 43 | sys.stdout.write(", center2: %d MHz" % |
| 44 | nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CENTER_FREQ2])) |
| 45 | elif nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE in attr: |
| 46 | channel_type = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE]) |
| 47 | sys.stdout.write(" %s" % nl80211.nl80211_channel_type2str(channel_type)); |
| 48 | |
| 49 | sys.stdout.write("\n"); |
| 50 | return nl.NL_SKIP; |
| 51 | except Exception as e: |
| 52 | (t,v,tb) = sys.exc_info() |
| 53 | print v.message |
| 54 | traceback.print_tb(tb) |
| 55 | |
| 56 | def error_handler(err, a): |
| 57 | a.done = err.error |
| 58 | return nl.NL_STOP |
| 59 | |
| 60 | def finish_handler(m, a): |
| 61 | return nl.NL_SKIP |
| 62 | |
| 63 | def ack_handler(m, a): |
| 64 | a.done = 0 |
| 65 | return nl.NL_STOP |
| 66 | |
| 67 | try: |
| 68 | cbd = test_class() |
| 69 | tx_cb = nl.nl_cb_alloc(nl.NL_CB_DEFAULT) |
| 70 | rx_cb = nl.nl_cb_clone(tx_cb) |
| 71 | s = nl.nl_socket_alloc_cb(tx_cb) |
| 72 | nl.py_nl_cb_err(rx_cb, nl.NL_CB_CUSTOM, error_handler, cbd); |
| 73 | nl.py_nl_cb_set(rx_cb, nl.NL_CB_FINISH, nl.NL_CB_CUSTOM, finish_handler, cbd); |
| 74 | nl.py_nl_cb_set(rx_cb, nl.NL_CB_ACK, nl.NL_CB_CUSTOM, ack_handler, cbd); |
| 75 | nl.py_nl_cb_set(rx_cb, nl.NL_CB_VALID, nl.NL_CB_CUSTOM, msg_handler, cbd); |
| 76 | |
| 77 | genl.genl_connect(s) |
| 78 | family = genl.genl_ctrl_resolve(s, 'nl80211') |
| 79 | m = nl.nlmsg_alloc() |
| 80 | genl.genlmsg_put(m, 0, 0, family, 0, 0, nl80211.NL80211_CMD_GET_INTERFACE, 0) |
| 81 | nl.nla_put_u32(m, nl80211.NL80211_ATTR_IFINDEX, nl.if_nametoindex('wlan0')) |
| 82 | |
| 83 | err = nl.nl_send_auto_complete(s, m); |
| 84 | if err < 0: |
| 85 | nl.nlmsg_free(msg) |
| 86 | |
| 87 | while cbd.done > 0 and not err < 0: |
| 88 | err = nl.nl_recvmsgs(s, rx_cb) |
| 89 | |
| 90 | except Exception as e: |
| 91 | (t, v, tb) = sys.exc_info() |
| 92 | print v.message |
| 93 | traceback.print_tb(tb) |