Skip to content

Commit

Permalink
v0.1.6 error reports less ambiguous errors
Browse files Browse the repository at this point in the history
  • Loading branch information
WraithWireless authored and WraithWireless committed Aug 17, 2016
1 parent 2a0015c commit e811e72
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 88 deletions.
3 changes: 2 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,5 @@ v 0.1.6 Continue with STA functionality
have tested further, phyadd,devadd will work (i.e. not rename the card)
under the following conditions a) the type is not managed or b) there is no
other device currently associated with the new card's phy. Have also determined
that integrated cards are also suspect to this bug?
that integrated cards are also suspect to this bug?
o made pyric error messages less ambiguous
12 changes: 4 additions & 8 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@
26) need to parse dumps (NLM_F_DUMP), for now we're good with link etc, so long
as the card is connected but it may come up eventually especially if we want
to add scan results
29) figure out how to parse the information element in pyw.link - there's some
good shit in it including sometimes the router os and type
29) figure out how to parse the information element in pyw.link - sometimes
the router os and type are included
o in link, NL80211_BSS_BEACON_IES vs NL80211_BSS_INFORMATION_ELEMENT
BEACON_IES may not always be presenet but appears to contain more
BEACON_IES may not always be present but appears to contain more
31) add VHT processing to sta_info bitrate
39) parsing wiphy_bands (should we add the below?)
o _HT_MCS_SET: a 16-bit attr containing the MCS set as defined in 802.11n
o _HT_CAPA: as in the HT information IE
42) is there a simple way to set the format strings as constants keeping in
mind that things like strings require a length in the format string
46) max-tx for bands is showing 15 when iwconfig shows 30
47) add an explain() to errors? or hard-code additional error reporting?
o _HT_CAPA: as in the HT information IE
52 changes: 29 additions & 23 deletions pyric/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
Defines the Pyric error class and constants for some errors. All pyric errors
will follow the 2-tuple form of EnvironmentError
Defines the PyRIC error class and constants for some errors. All pyric errors
will follow the 2-tuple form of EnvironmentError. Also defines constansts for
PyPI packaging.
WARNING: DO NOT import *
Requires:
linux (3.x or 4.x kernel)
Expand All @@ -32,9 +35,6 @@
includes: /nlhelp /lib /net /utils pyw.py
changes:
See CHANGES in top-level directory
WARNING: DO NOT import *
"""

__name__ = 'pyric'
Expand All @@ -46,27 +46,33 @@
__email__ = '[email protected]'
__status__ = 'Production'

# define pyric exceptions
# all exceptions are tuples t=(error code,error message)
# we use error codes defined in errno, adding -1 to define the undefined error
# EUNDEF. I don't like importing all from errno but it provides conformity in
# error handling i.e modules using pyric.error do not need to call pyric.EUNDEF
# and errno.EINVAL but can call pyric.EUNDEF and pyric.EINVAL
"""
define pyric exceptions
all exceptions are tuples t=(error code,error message)
we use error codes defined in errno, using EUNDEF = -1 to define an undefined
error I don't like importing all from errno but it provides conformity in
error handling i.e modules using pyric.error do not need to call pyric.EUNDEF
and errno.EINVAL but can call pyric.EUNDEF and pyric.EINVAL
"""
EUNDEF = -1 # undefined error
from errno import * # make all errno errors pyric errors
errorcode[EUNDEF] = "EUNDEF" # add ours to errorcode dicts
class error(EnvironmentError): pass

# BELOW IS STILL A WORK IN PRGORESS
#def strerror(errno):
# import os
# if errno < 0: return "Undefined error"
# elif errno == EPERM: return "Superuser privileges required"
# elif errno == EINVAL: return "Invalid parameter"
# else:
# return os.strerror(errno)
# device busy if setting channel when card is down
# no open files if attempt to create a virtual card with same name as orginal
class error(EnvironmentError):
def __init__(self,errno,errmsg=None):
if not errmsg: errmsg = strerror(errno)
EnvironmentError.__init__(self,errno,errmsg)

def strerror(errno):
import os
if errno < 0: return "Undefined error"
elif errno == EPERM: return "Superuser privileges required"
elif errno == EINVAL: return "Invalid parameter"
elif errno == EBUSY:
msg = "{0}. Make sure Card is up and no other devices share the same phy"
return msg.format(os.strerror(EBUSY))
elif errno == ENFILE: return "There are no available netlink sockets"
else:
return os.strerror(errno)

# for setup.py use
# redefine version for easier access
Expand Down
2 changes: 1 addition & 1 deletion pyric/lib/libio.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def io_transfer(iosock,flag,ifreq):
return ioctl(iosock.fileno(),flag,ifreq)
except (AttributeError,struct.error) as e:
# either sock is not valid or a bad value passed to ifreq
if e.message.find('fileno'): raise error(errno.ENOTSOCK,"bad socket")
if e.message.find('fileno'): raise error(errno.ENOTSOCK,"Bad socket")
else: raise error(errno.EINVAL,e)
except IOError as e:
# generally device cannot be found sort but can also be
Expand Down
22 changes: 11 additions & 11 deletions pyric/lib/libnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ def nl_recvmsg(sock):
# on success, just return the orginal message
if e.errno == nlh.NLE_SUCCESS: pass
else: raise
if sock.seq != msg.seq: raise error(errno.EBADMSG,"seq. # out of order")
if sock.seq != msg.seq: raise error(errno.EBADMSG,"Seq. # out of order")
return msg
except socket.timeout:
raise error(-1,"socket timed out")
raise error(-1,"Socket timed out")
#except socket.error as e: # this became in issue in python 3
# raise error(errno.ENOTSOCK,e)
except error as e:
Expand Down Expand Up @@ -354,7 +354,7 @@ def nltype(self): return self['type']

@nltype.setter
def nltype(self,v):
if v < 0: raise error(errno.ERANGE,"nltype {0} is invalid".format(v))
if v < 0: raise error(errno.ERANGE,"Netlink type {0} is invalid".format(v))
self['type'] = v

@property
Expand All @@ -368,23 +368,23 @@ def seq(self): return self['seq']

@seq.setter
def seq(self,v):
if v < 1: raise error(errno.ERANGE,"invalid seq. number")
if v < 1: raise error(errno.ERANGE,"Invalid seq. number")
self['seq'] = v

@property
def pid(self): return self['pid']

@pid.setter
def pid(self,v):
if v < 1: raise error(errno.ERANGE,"invalid port id")
if v < 1: raise error(errno.ERANGE,"Invalid port id")
self['pid'] = v

@property
def cmd(self): return self['cmd']

@cmd.setter
def cmd(self,v):
if v < 0: raise error(errno.ERANGE,"invalid cmd")
if v < 0: raise error(errno.ERANGE,"Invalid cmd")
self['cmd'] = v

@property
Expand Down Expand Up @@ -444,7 +444,7 @@ def nlmsg_fromstream(stream,override=False):
raise error(abs(e),strerror(abs(e)))
c,_,_ = struct.unpack_from(genlh.genl_genlmsghdr,stream,nlh.NLMSGHDRLEN)
except struct.error as e:
raise error(-1,"error parsing headers: {0}".format(e))
raise error(-1,"Error parsing headers: {0}".format(e))

# create a new message with hdr values then parse the attributes
msg = nlmsg_new(t,c,s,p,fs)
Expand Down Expand Up @@ -575,7 +575,7 @@ def nla_parse_set(aset,etype):
elif etype == nlh.NLA_U32: fmt = "I"
elif etype == nlh.NLA_U64: fmt = "Q"
else:
raise error(errno.EINVAL,"set elements are not valid datatype")
raise error(errno.EINVAL,"Set elements are not valid datatype")
esize = struct.calcsize(fmt)

ss = []
Expand All @@ -588,7 +588,7 @@ def nla_parse_set(aset,etype):
ss.append(s)
idx += esize
except struct.error:
raise error(errno.EINVAL,"set elements failed to unpack")
raise error(errno.EINVAL,"Set elements failed to unpack")
return ss

def nla_put(msg,v,a,d):
Expand All @@ -599,7 +599,7 @@ def nla_put(msg,v,a,d):
:param a: attribute type
:param d: attribute datatype
"""
if d > nlh.NLA_TYPE_MAX: raise error(errno.ERANGE,"value type is invalid")
if d > nlh.NLA_TYPE_MAX: raise error(errno.ERANGE,"Value type is invalid")
msg['attrs'].append((a,v,d))

# nla_put_* append data of specified datatype
Expand All @@ -626,7 +626,7 @@ def nla_putat(msg,i,v,a,d):
:param a: attribute type
:param d: attribute datatype
"""
if d > nlh.NLA_TYPE_MAX: raise error(errno.ERANGE,"invalid datatype")
if d > nlh.NLA_TYPE_MAX: raise error(errno.ERANGE,"Invalid datatype")
msg['attrs'][i] = (a,v,d)

def nla_pop(msg,i):
Expand Down
Loading

0 comments on commit e811e72

Please sign in to comment.