Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding valid interface combinations #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions examples/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@
"""

from __future__ import print_function # python 2to3 compability
import argparse as ap # cli arg parsing
import sys # cli exiting
import pyric # pyric error (and ecode EUNDEF)
import pyric.pyw as pyw # for iw functionality
from pyric.utils.channels import rf2ch # rf to channel conversion
import argparse as ap # cli arg parsing
import sys # cli exiting
import pyric # pyric error (and ecode EUNDEF)
import pyric.pyw as pyw # for iw functionality
from pyric.utils.channels import rf2ch # rf to channel conversion

def execute(dev,itype):

def execute(dev, itype):
# ensure dev is a wireless interfaces
wifaces = pyw.winterfaces()
if dev not in wifaces:
print("Device {0} is not wireless, use one of {1}".format(dev,wifaces))
print("Device {0} is not wireless, use one of {1}".format(dev, wifaces))

# get info dicts
dinfo = pyw.devinfo(dev)
Expand All @@ -43,7 +44,7 @@ def execute(dev,itype):

if itype == 'all' or itype == 'if':
msg = "Interface {0}\n".format(card.idx)
msg += "\tDriver: {0} Chipset: {1}\n".format(iinfo['driver'],iinfo['chipset'])
msg += "\tDriver: {0} Chipset: {1}\n".format(iinfo['driver'], iinfo['chipset'])
msg += "\tHW Addr: {0} Manufacturer: {1}\n".format(iinfo['hwaddr'],
iinfo['manufacturer'])
msg += "\tInet: {0} Bcast: {1} Mask: {2}\n".format(iinfo['inet'],
Expand All @@ -58,7 +59,8 @@ def execute(dev,itype):
msg += "\taddr: {0}\n".format(dinfo['mac'])
msg += "\tmode: {0}\n".format(dinfo['mode'])
msg += "\twiphy: {0}\n".format(card.phy)
if dinfo['mode'] != 'managed': msg += "\tDevice not associated\n"
if dinfo['mode'] != 'managed':
msg += "\tDevice not associated\n"
else:
msg += "\tchannel: {0} ({1} MHz), width: {2}, CF: {3} MHz\n".format(rf2ch(dinfo['RF']),
dinfo['RF'],
Expand Down Expand Up @@ -89,21 +91,30 @@ def execute(dev,itype):
for rate in pinfo['bands'][band]['rates']:
msg += "\t * {0} Mbps\n".format(rate)
msg += "\t Frequencies:\n"
for i,rf in enumerate(pinfo['bands'][band]['rfs']):
for i, rf in enumerate(pinfo['bands'][band]['rfs']):
dbm = pinfo['bands'][band]['rf-data'][i]['max-tx']
msg += "\t * {0} MHz ({1} dBm)".format(rf,dbm)
msg += "\t * {0} MHz ({1} dBm)".format(rf, dbm)
if not pinfo['bands'][band]['rf-data'][i]['enabled']:
msg += " (disabled)\n"
else:
msg += "\n"

msg += "\tvalid interface combinations::\n"
msg += "\t\t* #{"
for c in pinfo['combinations']['interfaces']:
msg += " {},".format(c)
msg += " }"
msg += " total <= {}, #channels <= {}".format(pinfo['combinations']['total'], pinfo['combinations']['channels'])

print(msg)


if __name__ == '__main__':
# create arg parser and parse command line args
print("Wireless Device Info Display using PyRIC v{0}".format(pyric.version))
argp = ap.ArgumentParser(description="Wireless Device Data")
argp.add_argument('-d','--dev',help="Wireless Device")
argp.add_argument('-t','--type',help="Info type one of {all|if|dev|phy}")
argp.add_argument('-d', '--dev', help="Wireless Device")
argp.add_argument('-t', '--type', help="Info type one of {all|if|dev|phy}")
args = argp.parse_args()
usage = "usage: python info.py -d <dev> [-t one of {all|if|dev|phy}]"
try:
Expand All @@ -113,9 +124,9 @@ def execute(dev,itype):
print(usage)
sys.exit(0)
if infotype is None: infotype = 'all'
if infotype not in ['all','if','dev','phy']:
if infotype not in ['all', 'if', 'dev', 'phy']:
print(usage)
sys.exit(0)
execute(dname,infotype)
execute(dname, infotype)
except pyric.error as e:
print(e)
print(e)
2 changes: 1 addition & 1 deletion pyric/lib/libnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ def nla_parse_nested(nested):
Header
"""
ns = []
idx = 0
idx = 0
l = len(nested)
while idx < l:
# first two bytes define length, including these bytes, length does not
Expand Down
24 changes: 23 additions & 1 deletion pyric/pyw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,8 @@ def phyinfo(card, nlsock=None):
'modes':[],
'swmodes':[],
'commands':[],
'ciphers':[]
'ciphers':[],
'combinations':[]
}

# modify frag_thresh and rts_thresh as necessary
Expand All @@ -1181,6 +1182,10 @@ def phyinfo(card, nlsock=None):
_, cs, d = nl.nla_find(rmsg, nl80211h.NL80211_ATTR_SUPPORTED_COMMANDS, False)
if d != nlh.NLA_ERROR: info['commands'] = _commands_(cs)

# get valid interface combinations
_, cs, d = nl.nla_find(rmsg, nl80211h.NL80211_ATTR_INTERFACE_COMBINATIONS, False)
if d != nlh.NLA_ERROR: info['combinations'] = _combinations_(cs)

return info

################################################################################
Expand Down Expand Up @@ -2195,6 +2200,23 @@ def _rateinfo_(ri):

return bitrate

def _combinations_(cs):
ic = cs.encode()
data = {
'interfaces': [],
'total': 0,
'channels': 0
}
for i, attr in nl.nla_parse_nested(ic):
for x, xttr in nl.nla_parse_nested(attr):
if x == 1:
data['interfaces'] = [_iftypes_(i[0]) for i in
nl.nla_parse_nested(nl.nla_parse_nested(nl.nla_parse_nested(xttr)[0][1])[1][1])]
if x == 2:
data['total'] = struct.unpack_from('I', xttr, 0)[0]
if x == 4:
data['channels'] = struct.unpack_from('I', xttr, 0)[0]
return data
#### NETLINK/IOCTL PARAMETERS ####

def _ifindex_(dev, iosock=None):
Expand Down