blob: afeafff3edd1367d222a3b4fb049036fc39d3575 [file] [log] [blame]
Christian Clauss00e46f12022-07-06 14:49:33 +02001from __future__ import print_function
Arend van Spriel98283262013-09-05 14:11:30 +02002import netlink.capi as nl
3import netlink.genl.capi as genl
4import nl80211
5import sys
6import traceback
7
Thomas Haller3c753e32023-07-13 09:31:52 +02008
Arend van Spriel98283262013-09-05 14:11:30 +02009class test_class:
Thomas Haller3c753e32023-07-13 09:31:52 +020010 def __init__(self):
11 self.done = 1
12
Arend van Spriel98283262013-09-05 14:11:30 +020013
14def msg_handler(m, a):
Thomas Haller3c753e32023-07-13 09:31:52 +020015 try:
16 e, attr = genl.py_genlmsg_parse(
17 nl.nlmsg_hdr(m), 0, nl80211.NL80211_ATTR_MAX, None
18 )
19 if nl80211.NL80211_ATTR_WIPHY in attr:
20 thiswiphy = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY])
21 print("phy#%d" % thiswiphy)
22 if nl80211.NL80211_ATTR_IFNAME in attr:
23 print(
24 "\tinterface %s" % nl.nla_get_string(attr[nl80211.NL80211_ATTR_IFNAME])
25 )
26 if nl80211.NL80211_ATTR_IFINDEX in attr:
27 print("\tifindex %d" % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFINDEX]))
28 if nl80211.NL80211_ATTR_WDEV in attr:
29 print("\twdev 0x%lx" % nl.nla_get_u64(attr[nl80211.NL80211_ATTR_WDEV]))
30 if nl80211.NL80211_ATTR_MAC in attr:
31 print(
32 "\tmac %02x:%02x:%02x:%02x:%02x:%02x"
33 % tuple(nl.nla_data(attr[nl80211.NL80211_ATTR_MAC]))
34 )
35 if nl80211.NL80211_ATTR_SSID in attr:
36 print("\tssid ", nl.nla_data(attr[nl80211.NL80211_ATTR_SSID]))
37 if nl80211.NL80211_ATTR_IFTYPE in attr:
38 iftype = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_IFTYPE])
39 print("\ttype %s" % nl80211.nl80211_iftype2str[iftype])
40 if nl80211.NL80211_ATTR_WIPHY_FREQ in attr:
41 freq = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_WIPHY_FREQ])
Arend van Spriel98283262013-09-05 14:11:30 +020042
Thomas Haller3c753e32023-07-13 09:31:52 +020043 sys.stdout.write("\tfreq %d MHz" % freq)
Arend van Spriel98283262013-09-05 14:11:30 +020044
Thomas Haller3c753e32023-07-13 09:31:52 +020045 if nl80211.NL80211_ATTR_CHANNEL_WIDTH in attr:
46 chanw = nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CHANNEL_WIDTH])
47 sys.stdout.write(", width: %s" % nl80211.nl80211_chan_width2str[chanw])
48 if nl80211.NL80211_ATTR_CENTER_FREQ1 in attr:
49 sys.stdout.write(
50 ", center1: %d MHz"
51 % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CENTER_FREQ1])
52 )
53 if nl80211.NL80211_ATTR_CENTER_FREQ2 in attr:
54 sys.stdout.write(
55 ", center2: %d MHz"
56 % nl.nla_get_u32(attr[nl80211.NL80211_ATTR_CENTER_FREQ2])
57 )
58 elif nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE in attr:
59 channel_type = nl.nla_get_u32(
60 attr[nl80211.NL80211_ATTR_WIPHY_CHANNEL_TYPE]
61 )
62 sys.stdout.write(" %s" % nl80211.nl80211_channel_type2str(channel_type))
Arend van Spriel98283262013-09-05 14:11:30 +020063
Thomas Haller3c753e32023-07-13 09:31:52 +020064 sys.stdout.write("\n")
65 return nl.NL_SKIP
Thomas Haller4dc1f492023-07-25 15:50:16 +020066 except Exception:
Thomas Haller3c753e32023-07-13 09:31:52 +020067 (t, v, tb) = sys.exc_info()
68 print(v.message)
69 traceback.print_tb(tb)
70
Arend van Spriel98283262013-09-05 14:11:30 +020071
72def error_handler(err, a):
Thomas Haller3c753e32023-07-13 09:31:52 +020073 a.done = err.error
74 return nl.NL_STOP
75
Arend van Spriel98283262013-09-05 14:11:30 +020076
77def finish_handler(m, a):
Thomas Haller3c753e32023-07-13 09:31:52 +020078 return nl.NL_SKIP
79
Arend van Spriel98283262013-09-05 14:11:30 +020080
81def ack_handler(m, a):
Thomas Haller3c753e32023-07-13 09:31:52 +020082 a.done = 0
83 return nl.NL_STOP
84
Arend van Spriel98283262013-09-05 14:11:30 +020085
86try:
Thomas Haller3c753e32023-07-13 09:31:52 +020087 cbd = test_class()
88 tx_cb = nl.nl_cb_alloc(nl.NL_CB_DEFAULT)
89 rx_cb = nl.nl_cb_clone(tx_cb)
90 s = nl.nl_socket_alloc_cb(tx_cb)
91 nl.py_nl_cb_err(rx_cb, nl.NL_CB_CUSTOM, error_handler, cbd)
92 nl.py_nl_cb_set(rx_cb, nl.NL_CB_FINISH, nl.NL_CB_CUSTOM, finish_handler, cbd)
93 nl.py_nl_cb_set(rx_cb, nl.NL_CB_ACK, nl.NL_CB_CUSTOM, ack_handler, cbd)
94 nl.py_nl_cb_set(rx_cb, nl.NL_CB_VALID, nl.NL_CB_CUSTOM, msg_handler, cbd)
Arend van Spriel98283262013-09-05 14:11:30 +020095
Thomas Haller3c753e32023-07-13 09:31:52 +020096 genl.genl_connect(s)
97 family = genl.genl_ctrl_resolve(s, "nl80211")
98 m = nl.nlmsg_alloc()
99 genl.genlmsg_put(m, 0, 0, family, 0, 0, nl80211.NL80211_CMD_GET_INTERFACE, 0)
100 nl.nla_put_u32(m, nl80211.NL80211_ATTR_IFINDEX, nl.if_nametoindex("wlan0"))
Arend van Spriel98283262013-09-05 14:11:30 +0200101
Thomas Haller3c753e32023-07-13 09:31:52 +0200102 err = nl.nl_send_auto_complete(s, m)
103 if err < 0:
104 nl.nlmsg_free(m)
Arend van Spriel98283262013-09-05 14:11:30 +0200105
Thomas Haller3c753e32023-07-13 09:31:52 +0200106 while cbd.done > 0 and not err < 0:
107 err = nl.nl_recvmsgs(s, rx_cb)
Arend van Spriel98283262013-09-05 14:11:30 +0200108
Thomas Haller4dc1f492023-07-25 15:50:16 +0200109except Exception:
Thomas Haller3c753e32023-07-13 09:31:52 +0200110 (t, v, tb) = sys.exc_info()
111 print(v.message)
112 traceback.print_tb(tb)