Skip to content

Commit e811e72

Browse files
WraithWirelessWraithWireless
authored andcommitted
v0.1.6 error reports less ambiguous errors
1 parent 2a0015c commit e811e72

File tree

7 files changed

+99
-88
lines changed

7 files changed

+99
-88
lines changed

CHANGES

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,5 @@ v 0.1.6 Continue with STA functionality
180180
have tested further, phyadd,devadd will work (i.e. not rename the card)
181181
under the following conditions a) the type is not managed or b) there is no
182182
other device currently associated with the new card's phy. Have also determined
183-
that integrated cards are also suspect to this bug?
183+
that integrated cards are also suspect to this bug?
184+
o made pyric error messages less ambiguous

TODO

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@
1919
26) need to parse dumps (NLM_F_DUMP), for now we're good with link etc, so long
2020
as the card is connected but it may come up eventually especially if we want
2121
to add scan results
22-
29) figure out how to parse the information element in pyw.link - there's some
23-
good shit in it including sometimes the router os and type
22+
29) figure out how to parse the information element in pyw.link - sometimes
23+
the router os and type are included
2424
o in link, NL80211_BSS_BEACON_IES vs NL80211_BSS_INFORMATION_ELEMENT
25-
BEACON_IES may not always be presenet but appears to contain more
25+
BEACON_IES may not always be present but appears to contain more
2626
31) add VHT processing to sta_info bitrate
2727
39) parsing wiphy_bands (should we add the below?)
2828
o _HT_MCS_SET: a 16-bit attr containing the MCS set as defined in 802.11n
29-
o _HT_CAPA: as in the HT information IE
30-
42) is there a simple way to set the format strings as constants keeping in
31-
mind that things like strings require a length in the format string
32-
46) max-tx for bands is showing 15 when iwconfig shows 30
33-
47) add an explain() to errors? or hard-code additional error reporting?
29+
o _HT_CAPA: as in the HT information IE

pyric/__init__.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
contributors may be used to endorse or promote products derived from this
2020
software without specific prior written permission.
2121
22-
Defines the Pyric error class and constants for some errors. All pyric errors
23-
will follow the 2-tuple form of EnvironmentError
22+
Defines the PyRIC error class and constants for some errors. All pyric errors
23+
will follow the 2-tuple form of EnvironmentError. Also defines constansts for
24+
PyPI packaging.
25+
26+
WARNING: DO NOT import *
2427
2528
Requires:
2629
linux (3.x or 4.x kernel)
@@ -32,9 +35,6 @@
3235
includes: /nlhelp /lib /net /utils pyw.py
3336
changes:
3437
See CHANGES in top-level directory
35-
36-
WARNING: DO NOT import *
37-
3838
"""
3939

4040
__name__ = 'pyric'
@@ -46,27 +46,33 @@
4646
__email__ = '[email protected]'
4747
__status__ = 'Production'
4848

49-
# define pyric exceptions
50-
# all exceptions are tuples t=(error code,error message)
51-
# we use error codes defined in errno, adding -1 to define the undefined error
52-
# EUNDEF. I don't like importing all from errno but it provides conformity in
53-
# error handling i.e modules using pyric.error do not need to call pyric.EUNDEF
54-
# and errno.EINVAL but can call pyric.EUNDEF and pyric.EINVAL
49+
"""
50+
define pyric exceptions
51+
all exceptions are tuples t=(error code,error message)
52+
we use error codes defined in errno, using EUNDEF = -1 to define an undefined
53+
error I don't like importing all from errno but it provides conformity in
54+
error handling i.e modules using pyric.error do not need to call pyric.EUNDEF
55+
and errno.EINVAL but can call pyric.EUNDEF and pyric.EINVAL
56+
"""
5557
EUNDEF = -1 # undefined error
5658
from errno import * # make all errno errors pyric errors
5759
errorcode[EUNDEF] = "EUNDEF" # add ours to errorcode dicts
58-
class error(EnvironmentError): pass
59-
60-
# BELOW IS STILL A WORK IN PRGORESS
61-
#def strerror(errno):
62-
# import os
63-
# if errno < 0: return "Undefined error"
64-
# elif errno == EPERM: return "Superuser privileges required"
65-
# elif errno == EINVAL: return "Invalid parameter"
66-
# else:
67-
# return os.strerror(errno)
68-
# device busy if setting channel when card is down
69-
# no open files if attempt to create a virtual card with same name as orginal
60+
class error(EnvironmentError):
61+
def __init__(self,errno,errmsg=None):
62+
if not errmsg: errmsg = strerror(errno)
63+
EnvironmentError.__init__(self,errno,errmsg)
64+
65+
def strerror(errno):
66+
import os
67+
if errno < 0: return "Undefined error"
68+
elif errno == EPERM: return "Superuser privileges required"
69+
elif errno == EINVAL: return "Invalid parameter"
70+
elif errno == EBUSY:
71+
msg = "{0}. Make sure Card is up and no other devices share the same phy"
72+
return msg.format(os.strerror(EBUSY))
73+
elif errno == ENFILE: return "There are no available netlink sockets"
74+
else:
75+
return os.strerror(errno)
7076

7177
# for setup.py use
7278
# redefine version for easier access

pyric/lib/libio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def io_transfer(iosock,flag,ifreq):
6464
return ioctl(iosock.fileno(),flag,ifreq)
6565
except (AttributeError,struct.error) as e:
6666
# either sock is not valid or a bad value passed to ifreq
67-
if e.message.find('fileno'): raise error(errno.ENOTSOCK,"bad socket")
67+
if e.message.find('fileno'): raise error(errno.ENOTSOCK,"Bad socket")
6868
else: raise error(errno.EINVAL,e)
6969
except IOError as e:
7070
# generally device cannot be found sort but can also be

pyric/lib/libnl.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ def nl_recvmsg(sock):
249249
# on success, just return the orginal message
250250
if e.errno == nlh.NLE_SUCCESS: pass
251251
else: raise
252-
if sock.seq != msg.seq: raise error(errno.EBADMSG,"seq. # out of order")
252+
if sock.seq != msg.seq: raise error(errno.EBADMSG,"Seq. # out of order")
253253
return msg
254254
except socket.timeout:
255-
raise error(-1,"socket timed out")
255+
raise error(-1,"Socket timed out")
256256
#except socket.error as e: # this became in issue in python 3
257257
# raise error(errno.ENOTSOCK,e)
258258
except error as e:
@@ -354,7 +354,7 @@ def nltype(self): return self['type']
354354

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

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

369369
@seq.setter
370370
def seq(self,v):
371-
if v < 1: raise error(errno.ERANGE,"invalid seq. number")
371+
if v < 1: raise error(errno.ERANGE,"Invalid seq. number")
372372
self['seq'] = v
373373

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

377377
@pid.setter
378378
def pid(self,v):
379-
if v < 1: raise error(errno.ERANGE,"invalid port id")
379+
if v < 1: raise error(errno.ERANGE,"Invalid port id")
380380
self['pid'] = v
381381

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

385385
@cmd.setter
386386
def cmd(self,v):
387-
if v < 0: raise error(errno.ERANGE,"invalid cmd")
387+
if v < 0: raise error(errno.ERANGE,"Invalid cmd")
388388
self['cmd'] = v
389389

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

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

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

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

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

632632
def nla_pop(msg,i):

0 commit comments

Comments
 (0)