Codebase list python-pyric / master pyric / pyw.py
master

Tree @master (Download .tar.gz)

pyw.py @masterraw · history · blame

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
#!/usr/bin/env python

""" pyw.py: Linux wireless library for the Python Wireless Developer and Pentester

Copyright (C) 2016  Dale V. Patterson ([email protected])

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

Redistribution and use in source and binary forms, with or without
modifications, are permitted provided that the following conditions are met:
 o Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
 o Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
 o Neither the name of the orginal author Dale V. Patterson nor the names of
    any contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.

Provides a python version of a subset of the iw command & additionally, a
smaller subset of ifconfig, rfkill and macchanger.

Each command/function (excluding interfaces & isinterface which do not rely on
ioctl/netlink sockets) comes in two flavors - one-time & persistent.
 1) one-time: similar to iw. The command, creates the netlink socket
    (or ioctl), composes the message, sends the message & receives the
    response, parses the results, closes the socket & returns the results to
    the caller. At no time does the caller need to be aware of any underlying
    netlink processes or structures.
 2) persistent: communication & parsing only. The onus of socket creation and
    deletion is on the caller which allows them to create one (or more)
    socket(s). The pyw functions will only handle message construction, message
    sending and receiving & message parsing.

Callers that intend to use pyw functionality often & repeatedly may prefer to
use a persistent netlink/ioctl socket. Socket creation & deletion are
relatively fast however, if a program is repeatedly using pyw function(s)
(such as a scanner that is changing channels mulitple times per second) it
makes sense for the caller to create one socket and use it throughout execution.
However, if the caller is only using pyw periodically and/or does not
want to bother with socket maintenance, the one-time flavor would be better.

for one-time execution, for example use

regset('US')

for persistent execution, use

regset('US',nlsocket)

where nlsocket is created with libnl.nl_socket_alloc()

to create/delete sockets use the libraries alloc functions:
 o ioctl: libio.io_socket_alloc() and libio.io_socket_free()
 o netlink: libnl.nl_socket_alloc() and libnl.nl_socket_free()

NOTE:
 1) All functions (excluding wireless core related) will use a Card object which
    collates the physical index, device name and interface index (ifindex) in a
    tuple rather than a device name or physical index or ifindex as this will not
    require the caller to remember if a dev or a phy or a ifindex is needed. The
    Exceptions to this are:
     devinfo which will accept a Card or a dev
     devadd which will accept a Card or a phy
 2) All functions allow pyric errors to pass through. Callers must catch these
    if they desire

"""

__name__ = 'pyw'
__license__ = 'GPLv3'
__version__ = '0.2.1'
__date__ = 'December 2016'
__author__ = 'Dale Patterson'
__maintainer__ = 'Dale Patterson'
__email__ = '[email protected]'
__status__ = 'Production'

import struct                                   # ioctl unpacking
import re                                       # check addr validity
import pyric                                    # pyric exception
from pyric.nlhelp.nlsearch import cmdbynum      # get command name
import pyric.utils.channels as channels         # channel related
import pyric.utils.rfkill as rfkill             # block/unblock
import pyric.utils.hardware as hw               # device related
import pyric.utils.ouifetch as ouifetch         # get oui dict
import pyric.net.netlink_h as nlh               # netlink definition
import pyric.net.genetlink_h as genlh           # genetlink definition
import pyric.net.wireless.nl80211_h as nl80211h # nl80211 definition
import pyric.lib.libnl as nl                    # netlink (library) functions
import pyric.net.wireless.wlan as wlan          # IEEE 802.11 Std definition
import pyric.net.sockios_h as sioch             # sockios constants
import pyric.net.if_h as ifh                    # ifreq structure
import pyric.lib.libio as io                    # ioctl (library) functions
import os

_FAM80211ID_ = None

# redefine some nl80211 enum lists for ease of use
IFTYPES = nl80211h.NL80211_IFTYPES
MNTRFLAGS = nl80211h.NL80211_MNTR_FLAGS
TXPWRSETTINGS = nl80211h.NL80211_TX_POWER_SETTINGS

################################################################################
#### WIRELESS CORE                                                          ####
################################################################################

def interfaces():
    """
    Retrieves all network interfaces (APX ifconfig)

    :returns: a list of device names of current network interfaces cards
    """
    fin = None
    try:
        # read in devices from /proc/net/dev. After splitting on newlines, the
        # first 2 lines are headers and the last line is empty so we remove them
        fin = open(hw.dpath, 'r')
        ds = fin.read().split('\n')[2:-1]
    except IOError:
        return []
    finally:
        if fin: fin.close()

    # the remaining lines are <dev>: p1 p2 ... p3, split on ':' & strip whitespace
    return [d.split(':')[0].strip() for d in ds]

def isinterface(dev):
    """
    Determines if device name belongs to a network card (APX ifconfig <dev>)

    :param dev: device name
    :returns: {True if dev is a device|False otherwise}
    """
    return dev in interfaces()

def winterfaces(iosock=None):
    """
    Retrieve all wireless interfaces (APX iwconfig)

    :param iosock: ioctl socket
    :returns: list of device names of current wireless NICs
    """
    if iosock is None: return _iostub_(winterfaces)

    wifaces = []
    for dev in interfaces():
        if iswireless(dev, iosock): wifaces.append(dev)
    return wifaces

def iswireless(dev, iosock=None):
    """
    Determines if given device is wireless (APX iwconfig <dev>)

    :param dev: device name
    :param iosock: ioctl socket
    :returns: {True:device is wireless|False:device is not wireless/not present}
    """
    if iosock is None: return _iostub_(iswireless, dev)

    try:
        # if the call succeeds, dev is found to be wireless
        _ = io.io_transfer(iosock, sioch.SIOCGIWNAME, ifh.ifreq(dev))
        return True
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except io.error:
        return False

def phylist():
    """
    Use rfkill to return all phys of wireless devices

    :returns: a list of tuples t = (physical index, physical name)
    """
    # these are stroed in /sys/class/ieee80211 but we let rfkill do it (just in
    # case the above path differs across distros or in future upgrades). However,
    # in some cases like OpenWRT which does not support rfkill we have to walk the
    # directory
    phys = []
    try:
        rfdevs = rfkill.rfkill_list()
        for rfk in rfdevs:
            if rfdevs[rfk]['type'] == 'wlan':
                phys.append((int(rfk.split('phy')[1]),rfk))
    except IOError as e:
        #catch 'No such file or directory' errors when rfkill is not supported
        if e.errno == pyric.ENOENT:
            try:
                rfdevs = os.listdir(rfkill.ipath)
            except OSError:
                emsg = "{} is not a directory & rfkill is not supported".format(rfkill.ipath)
                raise pyric.error(pyric.ENOTDIR,emsg)
            else:
                for rfk in rfdevs: phys.append((int(rfk.split('phy')[1]),rfk))
        else:
            raise pyric.error(pyric.EUNDEF,
                              "PHY listing failed: {}-{}".format(e.errno,e.strerror))
    return phys

def regget(nlsock=None):
    """
    Get the current regulatory domain (iw reg get)

    :param nlsock: netlink socket
    :returns: the two charactor regulatory domain
    """
    if nlsock is None: return _nlstub_(regget)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_GET_REG,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nl_sendmsg(nlsock, msg)
        rmsg = nl.nl_recvmsg(nlsock)
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)
    return nl.nla_find(rmsg, nl80211h.NL80211_ATTR_REG_ALPHA2)

def regset(rd, nlsock=None):
    """
    Sets the current regulatory domain (iw reg set <rd>)

    :param rd: regulatory domain code
    :param nlsock: netlink socket
    .. warning:: Requires root privileges
    """
    if len(rd) != 2: raise pyric.error(pyric.EINVAL, "Invalid reg. domain")
    if nlsock is None: return _nlstub_(regset, rd)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_REQ_SET_REG,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_string(msg, rd.upper(), nl80211h.NL80211_ATTR_REG_ALPHA2)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

################################################################################
#### CARD RELATED ####
################################################################################

class Card(tuple):
    """
    A wireless network interface controller - Wrapper around a tuple
    t = (physical index,device name, interface index)
    Exposes the following properties: (callable by '.'):
        phy: physical index
        dev: device name
        idx: interface index (ifindex)
    """
    # noinspection PyInitNewSignature
    def __new__(cls, p, d, i):
        return super(Card, cls).__new__(cls, tuple((p, d, i)))
    def __repr__(self):
        return "Card(phy={0},dev={1},ifindex={2})".format(self.phy,self.dev,self.idx)
    @property
    def phy(self): return self[0]
    @property
    def dev(self): return self[1]
    @property
    def idx(self): return self[2]

def getcard(dev, nlsock=None):
    """
    Get the Card object from device name

    :param dev: device name
    :param nlsock: netlink socket
    :returns: a Card with device name dev
    """
    if nlsock is None: return _nlstub_(getcard, dev)
    return devinfo(dev, nlsock)['card']

def validcard(card, nlsock=None):
    """
    Determines if card is still valid i.e. another program has not changed it

    :param card: Card object
    :param nlsock: netlink socket
    :returns: True if card is still valid, False otherwise
    """
    if nlsock is None: return _nlstub_(validcard, card)

    try:
        return card == devinfo(card.dev, nlsock)['card']
    except pyric.error as e:
        if e.errno == pyric.ENODEV: return False
        else: raise

################################################################################
#### ADDRESS RELATED                                                        ####
################################################################################

def macget(card, iosock=None):
    """
    Gets the interface's hw address (APX ifconfig <card.dev> | grep HWaddr)

    :param card: Card object
    :param iosock: ioctl socket
    :returns: device mac after operation
    """
    if iosock is None: return _iostub_(macget, card)

    try:
        flag = sioch.SIOCGIFHWADDR
        ret = io.io_transfer(iosock, flag, ifh.ifreq(card.dev, flag))
        fam = struct.unpack_from(ifh.sa_addr, ret, ifh.IFNAMELEN)[0]
        if fam in [ifh.ARPHRD_ETHER, ifh.AF_UNSPEC,ifh.ARPHRD_IEEE80211_RADIOTAP]:
            return _hex2mac_(ret[18:24])
        else:
            raise pyric.error(pyric.EAFNOSUPPORT, "Invalid return hwaddr family")
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))
    except io.error as e:
        raise pyric.error(e.errno, e.strerror)

def macset(card, mac, iosock=None):
    """
    Set nic's hwaddr (ifconfig <card.dev> hw ether <mac>)

    :param card: Card object
    :param mac: macaddr to set
    :param iosock: ioctl socket
    :returns True on success, False otherwise
    .. warning:: Requires root privileges
    """
    if not _validmac_(mac): raise pyric.error(pyric.EINVAL, "Invalid mac address")
    if iosock is None: return _iostub_(macset, card, mac)

    try:
        flag = sioch.SIOCSIFHWADDR
        ret = io.io_transfer(iosock, flag, ifh.ifreq(card.dev, flag, [mac]))
        fam = struct.unpack_from(ifh.sa_addr, ret, ifh.IFNAMELEN)[0]
        if fam in [ifh.ARPHRD_ETHER, ifh.AF_UNSPEC, ifh.ARPHRD_IEEE80211_RADIOTAP]:
            return _hex2mac_(ret[18:24]) == mac
        else:
            raise pyric.error(pyric.EAFNOSUPPORT, "Invalid return hwaddr family")
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))
    except io.error as e:
        raise pyric.error(e.errno, e.strerror)

def ifaddrget(card, iosock=None):
    """
    Get nic's ip, netmask and broadcast addresses

    :param card: Card object
    :param iosock: ioctl socket
    :returns: the tuple t = (inet,mask,bcast)
    """
    if iosock is None: return _iostub_(ifaddrget, card)

    try:
        # ip
        flag = sioch.SIOCGIFADDR
        ret = io.io_transfer(iosock, flag, ifh.ifreq(card.dev, flag))
        fam = struct.unpack_from(ifh.sa_addr, ret, ifh.IFNAMELEN)[0]
        if fam == ifh.AF_INET:
            inet = _hex2ip4_(ret[20:24])
        else:
            raise pyric.error(pyric.EAFNOSUPPORT, "Invalid return ip family")

        # netmask
        flag = sioch.SIOCGIFNETMASK
        ret = io.io_transfer(iosock, flag, ifh.ifreq(card.dev, flag))
        fam = struct.unpack_from(ifh.sa_addr, ret, ifh.IFNAMELEN)[0]
        if fam == ifh.AF_INET:
            mask = _hex2ip4_(ret[20:24])
        else:
            raise pyric.error(pyric.EAFNOSUPPORT, "Invalid return netmask family")

        # broadcast
        flag = sioch.SIOCGIFBRDADDR
        ret = io.io_transfer(iosock, flag, ifh.ifreq(card.dev, flag))
        fam = struct.unpack_from(ifh.sa_addr, ret, ifh.IFNAMELEN)[0]
        if fam == ifh.AF_INET:
            bcast = _hex2ip4_(ret[20:24])
        else:
            raise pyric.error(pyric.EAFNOSUPPORT, "Invalid return broadcast family")
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))
    except io.error as e:
        # catch address not available, which means the card currently does not
        # have any addresses set - raise others
        if e.errno == pyric.EADDRNOTAVAIL: return None, None, None
        raise pyric.error(e.errno, e.strerror)

    return inet, mask, bcast

def ifaddrset(card, inet=None, mask=None, bcast=None, iosock=None):
    """
    Set nic's ip4 addr, netmask and/or broadcast
     (ifconfig <card.dev> <inet> netmask <mask> broadcast <bcast>)
     can set ipaddr,netmask and/or broadcast to None but one or more of ipaddr,
     netmask, broadcast must be set

    :param card: Card object
    :param inet: ip address to set
    :param mask: netmask to set
    :param bcast: broadcast to set
    :param iosock: ioctl socket
    :returns: True on success, False otherwise
    .. note:
        1) throws error if setting netmask or broadcast and card does not have
            an ip assigned
        2) if setting only the ip address, netmask and broadcast will be set
            accordingly by the kernel.
        3) If setting multiple or setting the netmask and/or broadcast after the ip
            is assigned, one can set them to erroneous values i.e. ip = 192.168.1.2
            and broadcast = 10.0.0.31.
    .. warning:: Requires root privileges
    """
    # ensure one of params is set & that all set params are valid ip address
    if not inet and not mask and not bcast:
        raise pyric.error(pyric.EINVAL, "No parameters specified")
    if inet and not _validip4_(inet):
        raise pyric.error(pyric.EINVAL, "Invalid IP address")
    if mask and not _validip4_(mask):
        raise pyric.error(pyric.EINVAL, "Invalid netmask")
    if bcast and not _validip4_(bcast):
        raise pyric.error(pyric.EINVAL, "Invalid broadcast")
    if iosock is None: return _iostub_(ifaddrset, card, inet, mask, bcast)

    try:
        success = True
        # we have to do one at a time
        if inet: success &= inetset(card, inet, iosock)
        if mask: success &= maskset(card, mask, iosock)
        if bcast: success &= bcastset(card, bcast, iosock)
        return success
    except pyric.error as e:
        # an ambiguous error is thrown if attempting to set netmask or broadcast
        # without an ip address already set on the card
        if e.errno == pyric.EADDRNOTAVAIL and inet is None:
            raise pyric.error(pyric.EINVAL, "Set ip4 addr first")
        else:
            raise
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))

def inetset(card, inet, iosock=None):
    """
    Set nic's ip4 addr  (ifconfig <card.dev> <inet>

    :param card: Card object
    :param inet: ip address to set
    :param iosock: ioctl socket
    :returns: True on success, False otherwise
    .. note: setting the ip will set netmask and broadcast accordingly
    .. warning:: Requires root privileges
    """
    if not _validip4_(inet): raise pyric.error(pyric.EINVAL, "Invalid IP")
    if iosock is None: return _iostub_(inetset, card, inet)

    try:
        flag = sioch.SIOCSIFADDR
        ret = io.io_transfer(iosock, flag, ifh.ifreq(card.dev, flag, [inet]))
        fam = struct.unpack_from(ifh.sa_addr, ret, ifh.IFNAMELEN)[0]
        if fam == ifh.AF_INET:
            return _hex2ip4_(ret[20:24]) == inet
        else:
            raise pyric.error(pyric.EAFNOSUPPORT, "Invalid return ip family")
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))
    except io.error as e:
        raise pyric.error(e.errno, e.strerror)

def maskset(card, mask, iosock=None):
    """
    Set nic's ip4 netmask (ifconfig <card.dev> netmask <netmask>

    :param card: Card object
    :param mask: netmask to set
    :param iosock: ioctl socket
    :returns: True on success, False otherwise
    .. note:
        throws error if netmask is set and card does not have an ip assigned
    .. warning:: Requires root privileges
    """
    if not _validip4_(mask): raise pyric.error(pyric.EINVAL, "Invalid netmask")
    if iosock is None: return _iostub_(maskset, card, mask)
    try:
        flag = sioch.SIOCSIFNETMASK
        ret = io.io_transfer(iosock, flag, ifh.ifreq(card.dev, flag, [mask]))
        fam = struct.unpack_from(ifh.sa_addr, ret, ifh.IFNAMELEN)[0]
        if fam == ifh.AF_INET:
            return _hex2ip4_(ret[20:24]) == mask
        else:
            raise pyric.error(pyric.EAFNOSUPPORT, "Invalid return netmask family")
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))
    except io.error as e:
        # an ambiguous error is thrown if attempting to set netmask or broadcast
        # without an ip address already set on the card
        if e.errno == pyric.EADDRNOTAVAIL:
            raise pyric.error(pyric.EINVAL, "Cannot set netmask. Set ip first")
        else:
            raise pyric.error(e, e.strerror)

def bcastset(card, bcast, iosock=None):
    """
    Set nic's ip4 netmask (ifconfig <card.dev> broadcast <broadcast>

    :param card: Card object
    :param bcast: netmask to set
    :param iosock: ioctl socket
    :returns: True on success, False otherwise
    .. note:
        1) throws error if netmask is set and card does not have an ip assigned
        2) can set broadcast to erroneous values i.e. ipaddr = 192.168.1.2 and
            broadcast = 10.0.0.31.
    .. warning:: Requires root privileges
    """
    if not _validip4_(bcast):  raise pyric.error(pyric.EINVAL, "Invalid bcast")
    if iosock is None: return _iostub_(bcastset, card, bcast)

    # we have to do one at a time
    try:
        flag = sioch.SIOCSIFBRDADDR
        ret = io.io_transfer(iosock, flag, ifh.ifreq(card.dev, flag, [bcast]))
        fam = struct.unpack_from(ifh.sa_addr, ret, ifh.IFNAMELEN)[0]
        if fam == ifh.AF_INET:
            return _hex2ip4_(ret[20:24]) == bcast
        else:
            raise pyric.error(pyric.EAFNOSUPPORT, "Invalid return broadcast family")
    except pyric.error as e:
        # an ambiguous error is thrown if attempting to set netmask or broadcast
        # without an ip address already set on the card
        if e.errno == pyric.EADDRNOTAVAIL:
            raise pyric.error(pyric.EINVAL, "Cannot set broadcast. Set ip first")
        else:
            raise
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))
    except io.error as e:
        # an ambiguous error is thrown if attempting to set netmask or broadcast
        # without an ip address already set on the card
        if e.errno == pyric.EADDRNOTAVAIL:
            raise pyric.error(pyric.EINVAL, "Cannot set broadcast. Set ip first")
        else:
            raise pyric.error(e, e.strerror)

################################################################################
#### HARDWARE ON/OFF                                                        ####
################################################################################

def isup(card, iosock=None):
    """
    Determine on/off state of card
    :param card: Card object
    :param iosock: ioctl socket
    :returns: True if card is up, False otherwise
    """
    if iosock is None: return _iostub_(isup, card)

    try:
        return _issetf_(_flagsget_(card.dev, iosock), ifh.IFF_UP)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")

def up(card, iosock=None):
    """
    Turns dev on (ifconfig <card.dev> up)

    :param card: Card object
    :param iosock: ioctl socket
    .. warning:: Requires root privileges
    """
    if iosock is None: return _iostub_(up, card)

    try:
        flags = _flagsget_(card.dev, iosock)
        if not _issetf_(flags, ifh.IFF_UP):
            _flagsset_(card.dev, _setf_(flags, ifh.IFF_UP), iosock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")

def down(card, iosock=None):
    """
    Turns def off (ifconfig <card.dev> down)

    :param card: Card object
    :param iosock: ioctl socket
    .. warning:: Requires root privileges
    """
    if iosock is None: return _iostub_(down, card)

    try:
        flags = _flagsget_(card.dev, iosock)
        if _issetf_(flags, ifh.IFF_UP):
            _flagsset_(card.dev, _unsetf_(flags, ifh.IFF_UP), iosock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")

def isblocked(card):
    """
    Determines blocked state of Card

    :param card: Card object
    :returns: tuple (Soft={True if soft blocked|False otherwise},
                     Hard={True if hard blocked|False otherwise})
    """
    try:
        idx = rfkill.getidx(card.phy)
        return rfkill.soft_blocked(idx), rfkill.hard_blocked(idx)
    except AttributeError:
        raise pyric.error(pyric.ENODEV, "Card is no longer registered")

def block(card):
    """
    Soft blocks card

    :param card: Card object
    """
    try:
        idx = rfkill.getidx(card.phy)
        rfkill.rfkill_block(idx)
    except AttributeError:
        raise pyric.error(pyric.ENODEV, "Card is no longer registered")

def unblock(card):
    """
    Turns off soft block

    :param card:
    """
    try:
        idx = rfkill.getidx(card.phy)
        rfkill.rfkill_unblock(idx)
    except AttributeError:
        raise pyric.error(pyric.ENODEV, "Card is no longer registered")

################################################################################
#### RADIO PROPERTIES                                                       ####
################################################################################

def pwrsaveget(card, nlsock=None):
    """
    Returns card's power save state

    :param card: Card object
    :param nlsock: netlink socket
    :returns: True if power save is on, False otherwise
    """
    if nlsock is None: return _nlstub_(pwrsaveget, card)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_GET_POWER_SAVE,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nl_sendmsg(nlsock, msg)
        rmsg = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

    return nl.nla_find(rmsg, nl80211h.NL80211_ATTR_PS_STATE) == 1

def pwrsaveset(card, on, nlsock=None):
    """
    Sets card's power save state

    :param card: Card object
    :param on: {True = on|False = off}
    :param nlsock: netlink socket
    .. warning:: Requires root privileges
    """
    if nlsock is None: return _nlstub_(pwrsaveset, card, on)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_SET_POWER_SAVE,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nla_put_u32(msg, int(on), nl80211h.NL80211_ATTR_PS_STATE)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except ValueError:
        raise pyric.error(pyric.EINVAL, "Invalid parameter {0} for on".format(on))
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

def covclassget(card, nlsock=None):
    """
    Gets the coverage class value

    :param card: Card object
    :param nlsock: netlink socket
    :returns: coverage class value
    """
    if nlsock is None: return _nlstub_(covclassget, card)
    return phyinfo(card, nlsock)['cov_class']

def covclassset(card, cc, nlsock=None):
    """
    Sets the coverage class. The coverage class IAW IEEE Std 802.11-2012 is
    defined as the Air propagation time & together with max tx power control
    the BSS diamter

    :param card: Card object
    :param cc: coverage class 0 to 31 IAW IEEE Std 802.11-2012 Table 8-56
    :param nlsock: netlink socket
    .. warning:: Requires root privileges. Also this might not work on all
        systems.
    """
    if cc < wlan.COV_CLASS_MIN or cc > wlan.COV_CLASS_MAX:
        # this can work 'incorrectly' on non-int values but these will
        # be caught later during conversion
        emsg = "Cov class must be integer {0}-{1}".format(wlan.COV_CLASS_MIN,
                                                          wlan.COV_CLASS_MAX)
        raise pyric.error(pyric.EINVAL, emsg)
    if nlsock is None: return _nlstub_(covclassset, card, cc)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_SET_WIPHY,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.phy, nl80211h.NL80211_ATTR_WIPHY)
        nl.nla_put_u8(msg, int(cc), nl80211h.NL80211_ATTR_WIPHY_COVERAGE_CLASS)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except ValueError:
        raise pyric.error(pyric.EINVAL, "Invalid value {0} for Cov. Class".format(cc))
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

def retryshortget(card, nlsock=None):
    """
    Gets the short retry limit.

    :param card: Card object
    :param nlsock: netlink socket
    """
    if nlsock is None: return _nlstub_(retryshortget, card)
    return phyinfo(card, nlsock)['retry_short']

def retryshortset(card, lim, nlsock=None):
    """
    Sets the short retry limit.
    :param card: Card object
    :param lim: max # of short retries 1 - 255
    :param nlsock: netlink socket
    .. note: with kernel 4, the kernel does not allow setting up to the max
    .. warning:: Requires root privileges
    """
    if lim < wlan.RETRY_MIN or lim > wlan.RETRY_MAX:
        # this can work 'incorrectly' on non-int values but these will
        # be caught later during conversion
        emsg = "Retry short must be integer {0}-{1}".format(wlan.RETRY_MIN,
                                                            wlan.RETRY_MAX)
        raise pyric.error(pyric.EINVAL, emsg)
    if nlsock is None: return _nlstub_(retryshortset, card, lim)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_SET_WIPHY,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.phy, nl80211h.NL80211_ATTR_WIPHY)
        nl.nla_put_u8(msg, int(lim), nl80211h.NL80211_ATTR_WIPHY_RETRY_SHORT)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except ValueError:
        raise pyric.error(pyric.EINVAL, "Invalid value {0} for lim".format(lim))
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

def retrylongget(card, nlsock=None):
    """
    Gets the long retry limit.

    :param card: Card object
    :param nlsock: netlink socket
    :returns: card's long retry
    """
    if nlsock is None: return _nlstub_(retrylongget, card)
    return phyinfo(card, nlsock)['retry_long']

def retrylongset(card, lim, nlsock=None):
    """
    Sets the long retry limit.
    :param card: Card object
    :param lim: max # of short retries 1 - 255
    :param nlsock: netlink socket
    .. note: after moving to kernel 4, the kernel does not allow setting up to
        the max
    .. warning:: Requires root privileges
    """
    if lim < wlan.RETRY_MIN or lim > wlan.RETRY_MAX:
        # this can work 'incorrectly' on non-int values but these will
        # be caught later during conversion
        emsg = "Retry long must be integer {0}-{1}".format(wlan.RETRY_MIN,
                                                           wlan.RETRY_MAX)
        raise pyric.error(pyric.EINVAL, emsg)
    if nlsock is None: return _nlstub_(retrylongset, card, lim)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_SET_WIPHY,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.phy, nl80211h.NL80211_ATTR_WIPHY)
        nl.nla_put_u8(msg, int(lim), nl80211h.NL80211_ATTR_WIPHY_RETRY_LONG)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except ValueError:
        raise pyric.error(pyric.EINVAL, "Invalid value {0} for lim".format(lim))
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

def rtsthreshget(card, nlsock=None):
    """
    Gets RTS Threshold

    :param card: Card Object
    :param nlsock: netlink socket
    :returns: RTS threshold
    """
    if nlsock is None: return _nlstub_(rtsthreshget, card)
    return phyinfo(card, nlsock)['rts_thresh']

def rtsthreshset(card, thresh, nlsock=None):
    """
    Sets the RTS threshold. If off, RTS is disabled. If an integer, sets the
    smallest packet for which card will send an RTS prior to each transmission

    :param card: Card object
    :param thresh: rts threshold limit
    :param nlsock: netlink socket
    .. warning:: Requires root privileges
    """
    if thresh == 'off': thresh = wlan.RTS_THRESH_OFF
    elif thresh == wlan.RTS_THRESH_OFF: pass
    elif thresh < wlan.RTS_THRESH_MIN or thresh > wlan.RTS_THRESH_MAX:
        emsg = "Thresh must be 'off' or integer {0}-{1}".format(wlan.RTS_THRESH_MIN,
                                                                wlan.RTS_THRESH_MAX)
        raise pyric.error(pyric.EINVAL, emsg)
    if nlsock is None: return _nlstub_(rtsthreshset, card, thresh)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_SET_WIPHY,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.phy, nl80211h.NL80211_ATTR_WIPHY)
        nl.nla_put_u32(msg, thresh, nl80211h.NL80211_ATTR_WIPHY_RTS_THRESHOLD)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except ValueError:
        raise pyric.error(pyric.EINVAL, "Invalid value {0} for thresh".format(thresh))
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

def fragthreshget(card, nlsock=None):
    """
    Gets Fragmentation Threshold

    :param card: Card Object
    :param nlsock: netlink socket
    :returns: RTS threshold
    """
    if nlsock is None: return _nlstub_(fragthreshget, card)
    return phyinfo(card, nlsock)['frag_thresh']

def fragthreshset(card, thresh, nlsock=None):
    """
    Sets the Frag threshold. If off, fragmentation is disabled. If an integer,
    sets the largest packet before the card will enable fragmentation

    :param card: Card object
    :param thresh: frag threshold limit in octets
    :param nlsock: netlink socket
    .. warning:: Requires root privileges
    """
    if thresh == 'off': thresh = wlan.FRAG_THRESH_OFF
    elif thresh == wlan.FRAG_THRESH_OFF: pass
    elif thresh < wlan.FRAG_THRESH_MIN or thresh > wlan.FRAG_THRESH_MAX:
        emsg = "Thresh must be 'off' or integer {0}-{1}".format(wlan.FRAG_THRESH_MIN,
                                                                wlan.FRAG_THRESH_MAX)
        raise pyric.error(pyric.EINVAL, emsg)
    if nlsock is None: return _nlstub_(fragthreshset, card, thresh)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_SET_WIPHY,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.phy, nl80211h.NL80211_ATTR_WIPHY)
        nl.nla_put_u32(msg, thresh, nl80211h.NL80211_ATTR_WIPHY_FRAG_THRESHOLD)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

################################################################################
#### INFO RELATED                                                           ####
################################################################################

def devfreqs(card, nlsock=None):
    """
    Returns card's supported frequencies

    :param card: Card object
    :param nlsock: netlink socket
    :returns: list of supported frequencies
    """
    if nlsock is None: return _nlstub_(devfreqs, card)

    rfs = []
    pinfo = phyinfo(card, nlsock)
    for band in pinfo['bands']:
        rfs.extend(pinfo['bands'][band]['rfs'])
    rfs = sorted(rfs)
    return rfs

def devchs(card, nlsock=None):
    """
    Returns card's supported channels

    :param card: Card object
    :param nlsock: netlink socket
    :returns: list of supported channels
    """
    if nlsock is None: return _nlstub_(devchs, card)
    return [channels.rf2ch(rf) for rf in devfreqs(card,nlsock)]

def devstds(card, nlsock=None):
    """
    Gets card's wireless standards (iwconfig <card.dev> | grep IEEE

    :param card: Card object
    :param nlsock: netlink socket
    :returns: list of standards (letter designators)
    """
    if nlsock is None: return _nlstub_(devstds, card)

    stds = []
    bands = phyinfo(card,nlsock)['bands']
    if '5GHz' in bands: stds.append('a')
    if '2GHz' in bands: stds.extend(['b','g']) # assume backward compat with b
    HT = VHT = True
    for band in bands:
        HT &= bands[band]['HT']
        VHT &= bands[band]['VHT']
    if HT: stds.append('n')
    if VHT: stds.append('ac')
    return stds

def devmodes(card, nlsock=None):
    """
    Gets supported modes card can operate in

    :param card: Card object
    :param nlsock: netlink socket
    :returns: list of card's supported modes
    """
    if nlsock is None: return _nlstub_(devmodes, card)
    return phyinfo(card, nlsock)['modes']

def devcmds(card, nlsock=None):
    """
    Get supported commands card can execute

    :param card: Card object
    :param nlsock: netlink socket
    :returns: supported commands
    """
    if nlsock is None: return _nlstub_(devcmds, card)
    return phyinfo(card, nlsock)['commands']

def ifinfo(card, iosock=None):
    """
    Get info for interface (ifconfig <dev>)

    :param card: Card object
    :param iosock: ioctl socket
    :returns: dict with the following key:value pairs
        driver -> card's driver
        chipset -> card's chipset
        manufacturer -> card's manufacturer
        hwaddr -> card's mac address
        inet -> card's inet address
        bcast -> card's broadcast address
        mask -> card's netmask address
    """
    if iosock is None: return _iostub_(ifinfo, card)

    # get oui dict
    ouis = {}
    try:
        ouis = ouifetch.load()
    except pyric.error:
        pass

    try:
        drvr, chips = hw.ifcard(card.dev)
        mac = macget(card, iosock)
        ip4, nmask, bcast = ifaddrget(card, iosock)
        info = {'driver':drvr, 'chipset':chips, 'hwaddr':mac,
                'manufacturer':hw.manufacturer(ouis,mac),
                'inet':ip4, 'bcast':bcast, 'mask':nmask}
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")

    return info

def devinfo(card, nlsock=None):
    """
    Get info for device (iw dev <dev> info)

    :param card: Card object or dev
    :param nlsock: netlink socket
    :returns: dict with the following key:value pairs
        card -> Card(phy,dev,ifindex)
        mode -> i.e. monitor or managed
        wdev -> wireless device id
        mac -> hw address
        RF (if associated) -> frequency
        CF (if assoicate) -> center frequency
        CHW -> channel width i.e. NOHT,HT40- etc
    """
    if nlsock is None: return _nlstub_(devinfo, card)

    dev = None # appease pycharm
    try:
        # if we have a Card, pull out ifindex. otherwise get ifindex from dev
        try:
            dev = card.dev
            idx = card.idx
        except AttributeError:
            dev = card
            idx = _ifindex_(dev)

        # using the ifindex, get the phy and details about the Card
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_GET_INTERFACE,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nl_sendmsg(nlsock, msg)
        rmsg = nl.nl_recvmsg(nlsock)
    except io.error as e:
        # if we get a errno -19, it means ifindex failed & there is no device dev
        raise pyric.error(e.errno, e.strerror)
    except nl.error as e:
        # if we get a errno -19, it is mostly likely because the card does
        # not support nl80211. However check to ensure the card hasn't been
        # unplugged.
        if e.errno == pyric.ENODEV:
            try:
                _ = _ifindex_(dev)
            except io.error as e:
                raise pyric.error(e.errno, "{0}. Check Card".format(e.strerror))
            raise pyric.error(pyric.EPROTONOSUPPORT, "Device does not support nl80211")
        raise pyric.error(e.errno, e.strerror)

    # pull out attributes
    info = {
        'card': Card(nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY), dev, idx),
        'mode': IFTYPES[nl.nla_find(rmsg, nl80211h.NL80211_ATTR_IFTYPE)],
        'wdev': nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WDEV),
        'mac': _hex2mac_(nl.nla_find(rmsg, nl80211h.NL80211_ATTR_MAC)),
        'RF': nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY_FREQ),
        'CF': nl.nla_find(rmsg, nl80211h.NL80211_ATTR_CENTER_FREQ1),
        'CHW': nl.nla_find(rmsg, nl80211h.NL80211_ATTR_CHANNEL_WIDTH)
    }

    # convert CHW to string version
    try:
        info['CHW'] = channels.CHTYPES[info['CHW']]
    except (IndexError,TypeError): # invalid index and NoneType
        info['CHW'] = None
    return info

def phyinfo(card, nlsock=None):
    """
    Get info for phy (iw phy <phy> info)

    :param card: Card
    :param nlsock: netlink socket
    :returns: dict with the following key:value pairs
        generation -> wiphy generation
        modes -> list of supported modes
        bands -> dict of supported bands of the form
        bandid -> {'rates': list of supported rates,
                  'rfs': list of supported freqs,
                  'rd-data': list of data corresponding to rfs,
                  'HT': 802.11n HT supported,
                  'VHT': 802.11ac VHT supported}
        scan_ssids -> max number of scan SSIDS
        retry_short -> retry short limit
        retry_long -> retry long limit
        frag_thresh -> frag threshold
        rts_thresh -> rts threshold
        cov_class -> coverage class
        swmodes -> supported software modes
        commands -> supported commands
        ciphers -> supported ciphers
    """
    if nlsock is None: return _nlstub_(phyinfo, card)

    # iw sends @NL80211_ATTR_SPLIT_WIPHY_DUMP, we don't & get full return at once
    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_GET_WIPHY,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.phy, nl80211h.NL80211_ATTR_WIPHY)
        nl.nl_sendmsg(nlsock, msg)
        rmsg = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

    # pull out attributes
    info = {
        'generation':nl.nla_find(rmsg, nl80211h.NL80211_ATTR_GENERATION),
        'retry_short':nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY_RETRY_SHORT),
        'retry_long':nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY_RETRY_LONG),
        'frag_thresh':nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY_FRAG_THRESHOLD),
        'rts_thresh':nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY_RTS_THRESHOLD),
        'cov_class':nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY_COVERAGE_CLASS),
        'scan_ssids':nl.nla_find(rmsg, nl80211h.NL80211_ATTR_MAX_NUM_SCAN_SSIDS),
        'bands':[],
        'modes':[],
        'swmodes':[],
        'commands':[],
        'ciphers':[]
    }

    # modify frag_thresh and rts_thresh as necessary
    if info['frag_thresh'] >= wlan.FRAG_THRESH_MAX: info['frag_thresh'] = 'off'
    if info['rts_thresh'] >= wlan.RTS_THRESH_MAX: info['rts_thresh'] = 'off'

    # complex attributes
    # NOTE: after correcting my understanding of how to parsed nested attributes
    # they should no longer result in a NLA_ERROR but just in case...
    _, bs, d = nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY_BANDS, False)
    if d != nlh.NLA_ERROR: info['bands'] = _bands_(bs)

    _, cs, d = nl.nla_find(rmsg, nl80211h.NL80211_ATTR_CIPHER_SUITES, False)
    if d != nlh.NLA_ERROR: info['ciphers'] = _ciphers_(cs)

    # supported iftypes, sw iftypes are IAW nl80211.h flags (no attribute data)
    _, ms, d = nl.nla_find(rmsg, nl80211h.NL80211_ATTR_SUPPORTED_IFTYPES, False)
    if d != nlh.NLA_ERROR: info['modes'] = [_iftypes_(iftype) for iftype,_ in ms]

    _, ms, d = nl.nla_find(rmsg, nl80211h.NL80211_ATTR_SOFTWARE_IFTYPES, False)
    if d != nlh.NLA_ERROR: info['swmodes'] = [_iftypes_(iftype) for iftype,_ in ms]

    # get supported commands
    _, cs, d = nl.nla_find(rmsg, nl80211h.NL80211_ATTR_SUPPORTED_COMMANDS, False)
    if d != nlh.NLA_ERROR: info['commands'] = _commands_(cs)

    return info

################################################################################
#### TX/RX RELATED ####
################################################################################

def txset(card, setting, lvl, nlsock=None):
    """
    Sets cards tx power (iw phy card.<phy> <lvl> <pwr> * 100)

    :param card: Card object
    :param setting: power level setting oneof {'auto' = automatically determine
        transmit power|'limit' = limit power by <pwr>|'fixed' = set to <pwr>}
    :param lvl: desired tx power in dBm or None. NOTE: ignored if lvl is 'auto'
    :param nlsock: netlink socket
    :returns: True on success
    .. note: this does not work on my card(s) (nor does the corresponding iw
        command)
    .. warning:: Requires root privileges
    """
    # sanity check on power setting and power level
    if not setting in TXPWRSETTINGS:
        raise pyric.error(pyric.EINVAL, "Invalid power setting {0}".format(setting))
    if setting != 'auto' and lvl is None:
        raise pyric.error(pyric.EINVAL, "Power level must be specified")
    if nlsock is None: return _nlstub_(txset, card, setting, lvl)

    try:
        setting = TXPWRSETTINGS.index(setting)
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_SET_WIPHY,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        # neither sending the phy or ifindex works
        #nl.nla_put_u32(msg, card.phy, nl80211h.NL80211_ATTR_WIPHY)
        nl.nla_put_u32(msg, card.idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nla_put_u32(msg, setting, nl80211h.NL80211_ATTR_WIPHY_TX_POWER_SETTING)
        if setting != nl80211h.NL80211_TX_POWER_AUTOMATIC:
            nl.nla_put_u32(msg, 100*lvl, nl80211h.NL80211_ATTR_WIPHY_TX_POWER_LEVEL)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except ValueError:
        # converting to mBm
        raise pyric.error(pyric.EINVAL, "Invalid value {0} for txpwr".format(lvl))
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

def txget(card, iosock=None):
    """
    Gets card's transmission power (iwconfig <card.dev> | grep Tx-Power)

    :param card: Card object
    :param iosock: ioctl socket
    :returns: transmission power in dBm
    .. note: info can be found by cat /sys/kernel/debug/ieee80211/phy<#>/power but
        how valid is it?
    """
    if iosock is None: return _iostub_(txget, card)

    try:
        flag = sioch.SIOCGIWTXPOW
        ret = io.io_transfer(iosock, flag, ifh.ifreq(card.dev, flag))
        return struct.unpack_from(ifh.ifr_iwtxpwr, ret, ifh.IFNAMELEN)[0]
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except IndexError:
        return None
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))
    except io.error as e:
        raise pyric.error(e.errno, e.strerror)

def chget(card, nlsock=None):
    """
    Gets the current channel for device (iw dev <card.dev> info | grep channel)

    :param card: Card object
    :param nlsock: netlink socket
    .. note: will only work if dev is associated w/ AP or device is in monitor mode
        and has had chset previously
    """
    if nlsock is None: return _nlstub_(chget, card)
    return channels.rf2ch(devinfo(card, nlsock)['RF'])

def chset(card, ch, chw=None, nlsock=None):
    """
    Sets current channel on device (iw phy <card.phy> set channel <ch> <chw>)

    :param card: Card object
    :param ch: channel number
    :param chw: channel width oneof {[None|'HT20'|'HT40-'|'HT40+'}
    :param nlsock: netlink socket
    .. note:
      Can throw a device busy for several reason. 1) Card is down, 2) Another
      device is sharing the phy and wpa_supplicant/Network Manage is using it
    .. warning:: Requires root privileges
    """
    if nlsock is None: return _nlstub_(chset, card, ch, chw)
    return freqset(card, channels.ch2rf(ch), chw, nlsock)

def freqget(card, nlsock=None):
    """
    Gets the current frequency for device (iw dev <card.dev> info | grep channel)

    :param card: Card object
    :param nlsock: netlink socket
    .. note: will only work if dev is associated w/ AP or device is in monitor mode
        and has had [ch|freq]set previously
    """
    if nlsock is None: return _nlstub_(chget, card)
    return devinfo(card, nlsock)['RF']

def freqset(card, rf, chw=None, nlsock = None):
    """
    Set the frequency and width

    :param card: Card object
    :param rf: frequency
    :param chw: channel width oneof {[None|'HT20'|'HT40-'|'HT40+'}
    :param nlsock: netlink socket
    .. note:
        Can throw a device busy for several reason. 1) Card is down, 2) Another
        device is sharing the phy and wpa_supplicant/Network Manage is using it
    .. warning:: Requires root privileges
    """
    if nlsock is None: return _nlstub_(freqset, card, rf, chw)

    try:
        chw = channels.CHTYPES.index(chw)
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_SET_WIPHY,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.phy, nl80211h.NL80211_ATTR_WIPHY)
        nl.nla_put_u32(msg, rf, nl80211h.NL80211_ATTR_WIPHY_FREQ)
        nl.nla_put_u32(msg, chw, nl80211h.NL80211_ATTR_WIPHY_CHANNEL_TYPE)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except ValueError:
        raise pyric.error(pyric.EINVAL, "Invalid channel width")
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        if e.errno == pyric.EBUSY: raise pyric.error(e.errno,pyric.strerror(e.errno))
        raise pyric.error(e.errno, e.strerror)

#### INTERFACE & MODE RELATED ####

def modeget(card, nlsock=None):
    """
    Get current mode of card

    :param card: Card object
    :param nlsock: netlink socket
    :return:
    """
    if nlsock is None: return _nlstub_(modeget, card)
    return devinfo(card, nlsock)['mode']

def modeset(card, mode, flags=None, nlsock=None):
    """
    Sets card to mode (with optional flags if mode is monitor)
    (APX iw dev <card.dev> set type <mode> [flags])

    :param card: Card object
    :param mode: 'name' of mode to operate in (must be one of in {'unspecified'|
        'ibss'|'managed'|'AP'|'AP VLAN'|'wds'|'monitor'|'mesh'|'p2p'}
    :param flags: list of monitor flags (can only be used if card is being set
        to monitor mode) neof {'invalid'|'fcsfail'|'plcpfail'|'control'|'other bss'
                             |'cook'|'active'}
    :param nlsock: netlink socket
    .. note: as far
    """
    if mode not in IFTYPES: raise pyric.error(pyric.EINVAL, 'Invalid mode')
    if flags and mode != 'monitor':
        raise pyric.error(pyric.EINVAL, 'Can only set flags in monitor mode')
    if flags:
        for flag in flags:
            if flag not in MNTRFLAGS:
                raise pyric.error(pyric.EINVAL, 'Invalid flag: {0}'.format(flag))
    else: flags = []
    if nlsock is None: return _nlstub_(modeset, card, mode, flags)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_SET_INTERFACE,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nla_put_u32(msg, IFTYPES.index(mode), nl80211h.NL80211_ATTR_IFTYPE)
        for flag in flags:
            nl.nla_put_u32(msg,
                           MNTRFLAGS.index(flag),
                           nl80211h.NL80211_ATTR_MNTR_FLAGS)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

def ifaces(card, nlsock=None):
    """
    Returns all interfaces sharing the same phy as card (APX iw dev | grep phy#)

    :param card: Card object
    :param nlsock: netlink socket
    :returns: a list of tuples t = (Card,mode) for each device having the same
        phyiscal index as that of card
    """
    if nlsock is None: return _nlstub_(ifaces, card)

    ifs = []
    for dev in winterfaces():
        info = devinfo(dev, nlsock)
        try:
            if info['card'].phy == card.phy:
                ifs.append((info['card'], info['mode']))
        except AttributeError:
            raise pyric.error(pyric.EINVAL, "Invalid Card")
        except nl.error as e:
            raise pyric.error(e.errno, e.strerror)
    return ifs

def devset(card, ndev, nlsock=None):
    """
    Change card's dev to ndev
    :param card: Card object
    :param ndev: new dev name
    :param nlsock: netlink socket
    :returns: the new card object
    .. note:
        - via netlink one can set a new physical name but we want the ability to
            set a new dev.
        - this is not a true set name: it adds a new card with ndev as the dev then
            deletes the current card, returning the new card
        - in effect, it will appear as if the card has a new name but, it will also
            have a new ifindex
    .. warning:: Requires root privileges
    """
    if nlsock is None: return _nlstub_(devset, card, ndev)

    new = None # appease PyCharm
    try:
        mode = modeget(card, nlsock)
        phy = card.phy
        devdel(card, nlsock)
        new = phyadd(phy, ndev, mode, None, nlsock)
    except pyric.error:
        # try and restore the system i.e. delete new if possible
        if new:
            try:
                devdel(new, nlsock)
            except pyric.error:
                pass
        if not validcard(card):
            try:
                pass
            except pyric.error:
                pass
        raise
    return new

def devadd(card, vdev, mode, flags=None, nlsock=None):
    """
    Adds a virtual interface on device having type mode (iw dev <card.dev>
        interface add <vnic> type <mode>
    :param card: Card object or ifindex
    :param vdev: device name of new interface
    :param mode: 'name' of mode to operate in (must be one of in {'unspecified'|
        'ibss'|'managed'|'AP'|'AP VLAN'|'wds'|'monitor'|'mesh'|'p2p'}
    :param flags: list of monitor flags (can only be used if creating monitor
        mode) oneof {'invalid'|'fcsfail'|'plcpfail'|'control'|'other bss'
                    |'cook'|'active'}
    :param nlsock: netlink socket
    :returns: the new Card
    .. note: the new Card will be 'down'
    .. warning:: Requires root privileges
    """
    if iswireless(vdev): raise pyric.error(pyric.ENOTUNIQ,"{0} already exists".format(vdev))
    if mode not in IFTYPES: raise pyric.error(pyric.EINVAL, 'Invalid mode')
    if flags and mode != 'monitor':
        raise pyric.error(pyric.EINVAL, 'Can only set flags in monitor mode')
    if flags:
        for flag in flags:
            if flag not in MNTRFLAGS:
                raise pyric.error(pyric.EINVAL, 'Invalid flag: {0}'.format(flag))
    else: flags = []
    if nlsock is None: return _nlstub_(devadd, card, vdev, mode, flags)

    # if we have a Card, pull out ifindex
    try:
        idx = card.idx
    except AttributeError:
        idx = card

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_NEW_INTERFACE,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nla_put_string(msg, vdev, nl80211h.NL80211_ATTR_IFNAME)
        nl.nla_put_u32(msg, IFTYPES.index(mode), nl80211h.NL80211_ATTR_IFTYPE)
        for flag in flags:
            nl.nla_put_u32(msg,
                           MNTRFLAGS.index(flag),
                           nl80211h.NL80211_ATTR_MNTR_FLAGS)
        nl.nl_sendmsg(nlsock, msg)
        rmsg = nl.nl_recvmsg(nlsock) # success returns new device attributes
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

    # return the new Card with info from the results msg
    return Card(nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY),
                nl.nla_find(rmsg, nl80211h.NL80211_ATTR_IFNAME),
                nl.nla_find(rmsg, nl80211h.NL80211_ATTR_IFINDEX))

def devdel(card, nlsock=None):
    """
     REQUIRES ROOT PRIVILEGES
     deletes the device (dev <card.dev> del
     :param card: Card object
     :param nlsock: netlink socket
     NOTE: the original card is no longer valid (i.e. the phy will still be present
     but the device name and ifindex are no longer 'present' in the system
    """
    if nlsock is None: return _nlstub_(devdel, card)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_DEL_INTERFACE,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

def phyadd(card, vdev, mode, flags=None, nlsock=None):
    """
     REQUIRES ROOT PRIVILEGES
     adds a virtual interface on device having type mode (iw phy <card.phy>
      interface add <vnic> type <mode>
     :param card: Card object or physical index
     :param vdev: device name of new interface
     :param mode: 'name' of mode to operate in (must be one of in {'unspecified'|
     'ibss'|'managed'|'AP'|'AP VLAN'|'wds'|'monitor'|'mesh'|'p2p'}
     :param flags: list of monitor flags (can only be used if creating monitor
     mode) oneof {'invalid'|'fcsfail'|'plcpfail'|'control'|'other bss'
                  |'cook'|'active'}
     :param nlsock: netlink socket
     :returns: the new Card
     NOTE: the new Card will be 'down'
    """
    if mode not in IFTYPES: raise pyric.error(pyric.EINVAL, 'Invalid mode')
    if flags:
        if mode != 'monitor':
            raise pyric.error(pyric.EINVAL, 'Can only set flags in monitor mode')
        for flag in flags:
            if flag not in MNTRFLAGS:
                raise pyric.error(pyric.EINVAL, 'Invalid flag: {0}'.format(flag))
    else: flags = []
    if nlsock is None: return _nlstub_(phyadd, card, vdev, mode, flags)

    # if we have a Card, pull out phy
    try:
        phy = card.phy
    except AttributeError:
        phy = card

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_NEW_INTERFACE,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, phy, nl80211h.NL80211_ATTR_WIPHY)
        nl.nla_put_string(msg, vdev, nl80211h.NL80211_ATTR_IFNAME)
        nl.nla_put_u32(msg, IFTYPES.index(mode), nl80211h.NL80211_ATTR_IFTYPE)
        for flag in flags:
            nl.nla_put_u32(msg,
                           MNTRFLAGS.index(flag),
                           nl80211h.NL80211_ATTR_MNTR_FLAGS)
        nl.nl_sendmsg(nlsock, msg)
        rmsg = nl.nl_recvmsg(nlsock) # success returns new device attributes
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

    # get card & determine if we got a card with the specified name
    return Card(nl.nla_find(rmsg, nl80211h.NL80211_ATTR_WIPHY),
                nl.nla_find(rmsg, nl80211h.NL80211_ATTR_IFNAME),
                nl.nla_find(rmsg, nl80211h.NL80211_ATTR_IFINDEX))

################################################################################
#### STA FUNCTIONS                                                          ####
################################################################################

def isconnected(card, nlsock=None):
    """
     disconnect the card from an AP
     :param card: Card object
     :param nlsock: netlink socket
    """
    if nlsock is None: return _nlstub_(isconnected, card)
    return devinfo(card, nlsock)['RF'] is not None

def connect(card, ssid, bssid=None, rf=None, nlsock=None):
    """
     REQUIRES ROOT PRIVILEGES & WPA_SUPPLICANT MUST BE DISABLED
     connects to (Open) AP
     :param card: Card object
     :param ssid: the SSID, network name
     :param bssid: the AP's BSSID
     :param rf:  the frequency of the AP
     :param nlsock: netlink socket
     :returns: True on successful connect, False otherwise
     NOTE: although connected, traffic will not be routed, card will not have
      an IP assigned
    """
    if nlsock is None: return _nlstub_(connect, card, ssid, bssid, rf)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_CONNECT, # step 1
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nla_put_unspec(msg, ssid, nl80211h.NL80211_ATTR_SSID)
        nl.nla_put_unspec(msg, _mac2hex_(bssid), nl80211h.NL80211_ATTR_MAC)
        nl.nl_sendmsg(nlsock, msg)
        if not nl.nl_recvmsg(nlsock) == nlh.NLE_SUCCESS: return False
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)
    return True

def disconnect(card, nlsock=None):
    """
     REQUIRES ROOT PRIVILEGES
     disconnect the card from an AP
     :param card: Card object
     :param nlsock: netlink socket
     NOTE: does not return error if card is not connected. May not work if
     wpa_supplicant is running
    """
    if nlsock is None: return _nlstub_(disconnect, card)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_DISCONNECT,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

def link(card, nlsock=None):
    """
     returns info about link (iw dev card.<dev> link)
     :param card: Card object
     :param nlsock: netlink socket
     :returns: link info as dict  with the following key:value pairs
       bssid -> AP mac/ net BSSID
       ssid -> the ssid (Experimental)
       freq -> BSSID frequency in MHz
       chw -> width of the BSS control channel
       rss -> Received signal strength in dBm
       int -> beacon interval (ms)
       stat -> status w.r.t of card to BSS one of {'authenticated','associated','ibss'}
       tx -> tx metrics dict of the form
        pkts -> total sent packets to connected STA (AP)
        bytes -> total sent in bytes to connected STA (AP)
        retries -> total # of retries
        failed -> total # of failed
        bitrate -> dict of form
          rate -> tx rate in Mbits
          width -> channel width oneof {None|20|40}
          mcs-index -> mcs index (0..32) or None
          gaurd -> guard interval oneof {None|0=short|1=long}
          Note: width, mcs-index, guard will be None unless 802.11n is being used
       rx -> rx metrics dict (see tx for format exluces retries and fails)
      or None if the card is not connected
     NOTE: if the nested attribute was not parsed correctly will attempt to pull
      out as much as possible
    """
    if nlsock is None: return _nlstub_(link, card)

    # if we're not connected GET_SCAN will dump scan results, we don't want that
    if not isconnected(card, nlsock): return None

    try:
        # we need to set additional flags or the kernel will return ERRNO 95
        flags = nlh.NLM_F_REQUEST | nlh.NLM_F_ACK | nlh.NLM_F_ROOT | nlh.NLM_F_MATCH
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_GET_SCAN,
                           flags=flags)
        nl.nla_put_u32(msg, card.idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nl_sendmsg(nlsock, msg)
        rmsg = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

    # link returns multiple attributes but we are only concerned w/ @NL80211_ATTR_BSS
    # some cards (my integrated intel) do not parse correctly
    info = {'bssid': None, 'ssid': None, 'freq': None, 'rss': None, 'int': None,
            'chw': None, 'stat': None,'tx': {}, 'rx': {}}

    _, bs, d = nl.nla_find(rmsg, nl80211h.NL80211_ATTR_BSS, False)
    if d == nlh.NLA_ERROR: return info
    for idx, attr in bs:
        # any errors attempting to parse -> leave as default None, empty
        try:
            if idx == nl80211h.NL80211_BSS_BSSID:
                info['bssid'] = _hex2mac_(attr)
            if idx == nl80211h.NL80211_BSS_FREQUENCY:
                info['freq'] = struct.unpack_from('I', attr, 0)[0]
            if idx == nl80211h.NL80211_BSS_SIGNAL_MBM:
                info['rss'] = struct.unpack_from('i', attr, 0)[0] / 100
            if idx == nl80211h.NL80211_BSS_INFORMATION_ELEMENTS:
                """
                  hack the proprietary info element attribute: (it should
                  be a nested attribute itself, but I have currently no way of
                  knowing what the individual indexes would mean
                   \x06\x00\x00<l>SSID.....
                  '\x06\x00' is the ie index & the ssid is the first element
                  (from what I've seen). This is not nested. Not sure if the
                  length is the first two bytes or just the second  Get the length
                  of the ssid which is the 3rd,4th byte, then unpack the string
                  starting at the fifth byte up to the specified length
                """
                try:
                    l = struct.unpack_from('>H', attr, 0)[0] # have to change the format
                    info['ssid'] = struct.unpack_from('{0}s'.format(l), attr, 2)[0]
                except struct.error:
                    pass
            if idx == nl80211h.NL80211_BSS_BEACON_INTERVAL:
                info['int'] = struct.unpack_from('H', attr, 0)[0]
            if idx == nl80211h.NL80211_BSS_CHAN_WIDTH:
                j = struct.unpack_from('I', attr, 0)[0]
                info['chw'] = nl80211h.NL80211_BSS_CHAN_WIDTHS[j]
            if idx == nl80211h.NL80211_BSS_STATUS:
                j = struct.unpack_from('I', attr, 0)[0]
                info['stat'] = nl80211h.NL80211_BSS_STATUSES[j]
        except struct.error:
            pass

    # process stainfo of AP
    try:
        sinfo = stainfo(card, info['bssid'], nlsock)
        info['tx'] = {'bytes': sinfo['tx-bytes'],
                      'pkts': sinfo['tx-pkts'],
                      'failed': sinfo['tx-failed'],
                      'retries': sinfo['tx-retries'],
                      'bitrate': {'rate': sinfo['tx-bitrate']['rate']}}
        if sinfo['tx-bitrate'].has_key('mcs-index'):
            info['tx']['bitrate']['mcs-index'] = sinfo['tx-bitrate']['mcs-index']
            info['tx']['bitrate']['gi'] = sinfo['tx-bitrate']['gi']
            info['tx']['bitrate']['width'] = sinfo['tx-bitrate']['width']

        info['rx'] = {'bytes': sinfo['rx-bytes'],
                      'pkts':sinfo['rx-pkts'],
                      'bitrate': {'rate': sinfo['rx-bitrate']['rate']}}
        if sinfo['rx-bitrate'].has_key('mcs-index'):
            info['rx']['bitrate']['mcs-index'] = sinfo['rx-bitrate']['mcs-index']
            info['rx']['bitrate']['gi'] = sinfo['rx-bitrate']['gi']
            info['rx']['bitrate']['width'] = sinfo['rx-bitrate']['width']
    except (KeyError,TypeError,AttributeError):
        # ignore for now, returning what we got
        pass

    return info

def stainfo(card, mac, nlsock=None):
    """
     returns info about sta (AP) the card is associated with (iw dev card.<dev> link)
     :param card: Card object
     :param mac: mac address of STA
     :param nlsock: netlink socket
     :returns: sta info as dict  with the following key:value pairs
      rx-bytes: total received bytes (from STA)
      tx-bytes: total sent bytes (to STA)
      rx-pkts: total received packets (from STA)
      tx-pkts: total sent packets (to STA)
      tx-bitrate: dict of the form
       rate: bitrate in 100kbits/s
       legacy: fallback bitrate in 100kbits/s (only present if rate is not determined)
       mcs-index: mcs index (0..32) (only present if 802.11n)
       gi: guard interval oneof {0=short|1=long} (only present if 802.11n)
       width: channel width oneof {20|40}
      rx-bitrate: see tx-bitrate
     NOTE:
      - if the nested attribute was not parsed correctly will attempt to pull
       out as much as possible
      - given msc index, guard interval and channel width, one can calculate the
       802.11n rate (see wraith->standards->mcs)
    """
    if nlsock is None: return _nlstub_(stainfo, card, mac)

    # if we're not connected GET_SCAN will dump scan results, we don't want that
    if not isconnected(card, nlsock): return None

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_GET_STATION,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nla_put_unspec(msg, _mac2hex_(mac), nl80211h.NL80211_ATTR_MAC)
        nl.nl_sendmsg(nlsock, msg)
        rmsg = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)

    # we are only concerned w/ @NL80211_ATTR_STA_INFO
    info = {'rx-bytes': None, 'tx-bytes': None, 'rx-pkts': None, 'tx-pkts': None,
            'tx-bitrate':{}, 'rx-bitrate':{}}

    _, bs, d = nl.nla_find(rmsg, nl80211h.NL80211_ATTR_STA_INFO, False)
    if d == nlh.NLA_ERROR: return info
    for sidx, sattr in bs: # sidx indexes the enum nl80211_sta_info
        try:
            if sidx == nl80211h.NL80211_STA_INFO_RX_BYTES:
                info['rx-bytes'] = struct.unpack_from('I', sattr, 0)[0]
            elif sidx == nl80211h.NL80211_STA_INFO_TX_BYTES:
                info['tx-bytes'] = struct.unpack_from('I', sattr, 0)[0]
            elif sidx == nl80211h.NL80211_STA_INFO_RX_PACKETS:
                info['rx-pkts'] = struct.unpack_from('I', sattr, 0)[0]
            elif sidx == nl80211h.NL80211_STA_INFO_TX_PACKETS:
                info['tx-pkts'] = struct.unpack_from('I', sattr, 0)[0]
            elif sidx == nl80211h.NL80211_STA_INFO_TX_RETRIES:
                info['tx-retries'] = struct.unpack_from('I', sattr, 0)[0]
            elif sidx == nl80211h.NL80211_STA_INFO_TX_FAILED:
                info['tx-failed'] = struct.unpack_from('I', sattr, 0)[0]
            elif sidx == nl80211h.NL80211_STA_INFO_TX_BITRATE:
                info['tx-bitrate'] = _rateinfo_(sattr)
            elif sidx == nl80211h.NL80211_STA_INFO_RX_BITRATE:
                info['rx-bitrate'] = _rateinfo_(sattr)
        except struct.error:
            # ignore this and hope other elements still work
            pass

    return info

################################################################################
#### FILE PRIVATE                                                           ####
################################################################################

IPADDR = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") # re for ip addr
MACADDR = re.compile("^([0-9a-fA-F]{2}:){5}([0-9a-fA-F]{2})$") # re for mac addr


def _hex2ip4_(v):
    """
     :param v: packed by string
     :returns: a '.' separated ip4 address from byte stream v
    """
    try:
        return '.'.join([str(ord(c)) for c in v])
    except TypeError:
        # python 3 c is already numeric
        return '.'.join([str(c) for c in v])

def _hex2mac_(v):
    """
     :param v: packed bytestream of form \xd8\xc7\xc8\x00\x11\x22
     :returns: a ':' separated mac address from byte stream v
    """
    try:
        return ":".join(['{0:02x}'.format(ord(c)) for c in v])
    except TypeError:
        # it appears that in Python 3.5 c is already numeric
        return ":".join(['{0:02x}'.format(c) for c in v])

def _mac2hex_(v):
    """
     converts mac address to hex string
     :param v: mac address of form xx:yy:zz:00:11:22
     :returns: mac address as hex string
    """
    try:
        return struct.pack('6B',*[int(x,16) for x in v.split(':')])
    except AttributeError:
        raise pyric.error(pyric.EINVAL, 'Mac address is not valid')
    except struct.error:
        raise pyric.error(pyric.EINVAL, 'Mac address is not 6 octets')

def _validip4_(addr):
    """
     determines validity of ip4 address
     :param addr: ip addr to check
     :returns: True if addr is valid ip, False otherwise
    """
    try:
        if re.match(IPADDR, addr): return True
    except TypeError:
        return False
    return False

def _validmac_(addr):
    """
     determines validity of hw addr
     :param addr: address to check
     :returns: True if addr is valid hw address, False otherwise
    """
    try:
        if re.match(MACADDR, addr): return True
    except TypeError:
        return False
    return False

def _issetf_(flags, flag):
    """
      determines if flag is set
      :param flags: current flag value
      :param flag: flag to check
      :return: True if flag is set
     """
    return (flags & flag) == flag

def _setf_(flags, flag):
    """
     sets flag, adding to flags
     :param flags: current flag value
     :param flag: flag to set
     :return: new flag value
    """
    return flags | flag

def _unsetf_(flags, flag):
    """
     unsets flag, adding to flags
     :param flags: current flag value
     :param flag: flag to unset
     :return: new flag value
    """
    return flags & ~flag

def _flagsget_(dev, iosock=None):
    """
     gets the device's flags
     :param dev: device name:
     :param iosock: ioctl socket
     :returns: device flags
    """
    if iosock is None: return _iostub_(_flagsget_, dev)

    try:
        flag = sioch.SIOCGIFFLAGS
        ret = io.io_transfer(iosock, flag, ifh.ifreq(dev, flag))
        return struct.unpack_from(ifh.ifr_flags, ret, ifh.IFNAMELEN)[0]
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))
    except io.error as e:
        raise pyric.error(e.errno, e.strerror)

def _flagsset_(dev, flags, iosock=None):
    """
     gets the device's flags
     :param dev: device name:
     :param flags: flags to set
     :param iosock: ioctl socket
     :returns: device flags after operation
    """
    if iosock is None: return _iostub_(_flagsset_, dev, flags)

    try:
        flag = sioch.SIOCSIFFLAGS
        ret = io.io_transfer(iosock, flag, ifh.ifreq(dev, flag, [flags]))
        return struct.unpack_from(ifh.ifr_flags, ret, ifh.IFNAMELEN)[0]
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))
    except io.error as e:
        raise pyric.error(e.errno, e.strerror)

#### ADDITIONAL PARSING FOR PHYINFO ####

def _iftypes_(i):
    """
     wraps the IFTYPES list to handle index errors
     :param i:
     :returns: the string IFTYPE corresponding to i
    """
    try:
        return IFTYPES[i]
    except IndexError:
        return "Unknown mode ({0})".format(i)

def _bands_(bs):
    """
     extracts supported freqs, rates from bands
     :param bs: a list of one or more unparsed band attributes
     :returns: dict of the form
      band: one of {'2GHz'|'5GHz'|'UNK (n)'} -> band dict
      band dict ->
       HT: HT is supported by the Card on this band
       VHT: VHT is supported by the Card on this band
       rates: list of supported rates
       rfs: list of supported frequencies
       rf-data: list of dicts of rf-data where rf-data[i] contains info
        regarding rf[i]
    """
    # NOTE: in addition to RF and rates there are HT data included in the
    # band info ATT we do not parse these (see "phy info notes 3.txt")
    bands = {}
    for idx, band in bs:
        # the index tell us what band were in (enum nl80211_band)
        try:
            idx = nl80211h.NL80211_BANDS[idx]
        except IndexError:
            idx = "UNK ({0})".format(idx)
        bands[idx] = {'HT': False,
                     'VHT': False,
                     'rates': None,
                     'rfs': None,
                     'rf-data': None}

        # now we delve into multiple levels of nesting
        for bidx,battr in nl.nla_parse_nested(band):
            # There are other data here (see nl80211_h nl80211_band_attr)
            # that we are not currently using
            if bidx == nl80211h.NL80211_BAND_ATTR_RATES:
                try:
                    bands[idx]['rates'] = _band_rates_(battr)
                except nl.error:
                    bands[idx]['rates'] = []
            elif bidx == nl80211h.NL80211_BAND_ATTR_FREQS:
                try:
                    bands[idx]['rfs'], bands[idx]['rf-data'] = _band_rfs_(battr)
                except nl.error:
                    bands[idx]['rfs'], bands[idx]['rf-data'] = [], []
            elif bidx in [nl80211h.NL80211_BAND_ATTR_HT_MCS_SET,
                          nl80211h.NL80211_BAND_ATTR_HT_CAPA,
                          nl80211h.NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
                          nl80211h.NL80211_BAND_ATTR_HT_AMPDU_DENSITY]:
                bands[idx]['HT'] = True
            elif bidx in [nl80211h.NL80211_BAND_ATTR_VHT_MCS_SET,
                          nl80211h.NL80211_BAND_ATTR_VHT_CAPA]:
                bands[idx]['VHT'] = True
    return bands

def _band_rates_(rs):
    """
     unpacks individual rates from packed rates
     :param rs: packed rates
     :returns: a list of rates in Mbits
     NOTE: ATT we ignore any short preamble specifier
    """
    rates = []
    # unlike other nested attributes, the 'index' into rates is actually
    # a counter (which we'll ignore)
    for _, attr in nl.nla_parse_nested(rs):
        # the nested attribute itself is a nested attribute. The idx indexes
        # the enum nl80211_bitrate_attr of which we are only concerned w/ rate
        for idx, bitattr in nl.nla_parse_nested(attr):
            if idx == nl80211h.NL80211_BITRATE_ATTR_RATE:
                rates.append(struct.unpack_from('I', bitattr, 0)[0] * 0.1)
    return rates

def _band_rfs_(rs):
    """
     unpacks individual RFs (and accompanying data) from packed rfs
     :param rs: packed frequencies
     :returns: a tuple t = (freqs: list of supported RFS (MHz), data: list of dicts)
     where for each i in freqs, data[i] is the corresponding data having the
     form {}
    """
    rfs = []
    rfds = []
    # like rates, the index here is a counter and fattr is a nested attribute
    for _, fattr in nl.nla_parse_nested(rs):
        # RF data being compiled ATT we are ignoring DFS related and infrared
        # related. rfd is initially defined with max-tx, radar, 20Mhz and 10Mhz
        # with 'default' values.
        # Additional values may be returned by the kernel. If present they will
        # be appended to not-permitted as the following strings
        #  HT40-, HT40+, 80MHz, 160MHz and outdoor.
        # If present in not-permitted, they represent False Flags
        rfd = {
            'max-tx': 0,        # Card's maximum tx-power on this RF
            'enabled': True,    # w/ current reg. dom. RF is enabled
            '20Mhz': True,      # w/ current reg. dom. 20MHz operation is allowed
            '10Mhz': True,      # w/ current reg. dom. 10MHz operation is allowed
            'radar': False,     # w/ current reg. dom. radar detec. required on RF
            'not-permitted': [] # additional flags
        }
        for rfi, rfattr in nl.nla_parse_nested(fattr):
            # rfi is the index into enum nl80211_frequency_attr
            if rfi == nl80211h.NL80211_FREQUENCY_ATTR_FREQ:
                rfs.append(struct.unpack_from('I', rfattr, 0)[0])
            elif rfi == nl80211h.NL80211_FREQUENCY_ATTR_DISABLED:
                rfd['enabled'] = False
            elif rfi == nl80211h.NL80211_FREQUENCY_ATTR_MAX_TX_POWER: # in mBm
                rfd['max-tx'] = struct.unpack_from('I', rfattr, 0)[0] / 100
            elif rfi == nl80211h.NL80211_FREQUENCY_ATTR_NO_HT40_MINUS:
                rfd['not-permitted'].append('HT40-')
            elif rfi == nl80211h.NL80211_FREQUENCY_ATTR_NO_HT40_PLUS:
                rfd['not-permitted'].append('HT40+')
            elif rfi == nl80211h.NL80211_FREQUENCY_ATTR_NO_80MHZ:
                rfd['not-permitted'].append('80MHz')
            elif rfi == nl80211h.NL80211_FREQUENCY_ATTR_NO_160MHZ:
                rfd['not-permitted'].append('160MHz')
            elif rfi == nl80211h.NL80211_FREQUENCY_ATTR_INDOOR_ONLY:
                rfd['not-permitted'].append('outdoor')
            elif rfi == nl80211h.NL80211_FREQUENCY_ATTR_NO_20MHZ:
                rfd['20MHz'] = False
            elif rfi == nl80211h.NL80211_FREQUENCY_ATTR_NO_10MHZ:
                rfd['10MHz'] = False
        rfds.append(rfd)
    return rfs, rfds

def _unparsed_rf_(band):
    """
     (LEGACY) extract list of supported freqs packed byte stream band
     :param band: packed byte string from NL80211_ATTR_WIPHY_BANDS
     :returns: list of supported frequencies
    """
    rfs = []
    for freq in channels.freqs():
        if band.find(struct.pack("I", freq)) != -1:
            rfs.append(freq)
    return rfs

def _commands_(command):
    """
     converts numeric commands to string version
     :param command: list of command constants
     :returns: list of supported commands as strings
    """
    cs = []
    for _,cmd in command: # rather than an index, commands use a counter, ignore it
        try:
            # use numeric command to lookup string version in form
            #    @NL80211_CMD_<CMD>
            # and strip "@NL80211_CMD_". NOTE: some commands may have multiple
            # string synonyms, in that case, take the first one. Finally, make
            # it lowercase
            cmd = cmdbynum(struct.unpack_from('I', cmd, 0)[0])
            if type(cmd) is type([]): cmd = cmd[0]
            cs.append(cmd[13:].lower()) # skip NL80211_CMD_
        except KeyError:
            # kernel 4 added commands not found in kernel 3 nlh8022.h.
            # keep this just in case new commands pop up again
            cs.append("unknown cmd ({0})".format(cmd))
    return cs

def _ciphers_(ciphers):
    """
     identifies supported ciphers
     :param ciphers: the cipher suite stream
     :returns: a list of supported ciphers
    """
    ss = []
    for cipher in ciphers: # ciphers is a set and not nested
        try:
            ss.append(wlan.WLAN_CIPHER_SUITE_SELECTORS[cipher])
        except KeyError as e:
            # we could do nothing, or append 'rsrv' but we'll add a little
            # for testing/future identificaion purposes
            ss.append('RSRV-{0}'.format(hex(int(e.__str__()))))
    return ss

#### ADDITIONAL PARSING FOR STAINFO

def _rateinfo_(ri):
    """
     parses the rate info stream returning a bitrate dict
     :param ri: rate info stream
     :returns: bitrate dict having the key->value pairs
      rate: bitrate in 100kbits/s
      legacy: fallback bitrate in 100kbits/s (only present if rate is not determined)
      mcs-index: mcs index (0..32) (only present if 802.11n)
      gi: guard interval oneof {0=short|1=long} (only present if 802.11n)
      width: channel width oneof {20|40}
     NOTE: references enum nl80211_rate_info
    """
    bitrate = {'rate': None, 'legacy': None, 'mcs-index': None,
               'gi': 1, 'width': 20}
    for i, attr in nl.nla_parse_nested(ri):
        if i == nl80211h.NL80211_RATE_INFO_BITRATE32:
            bitrate['rate'] = struct.unpack_from('I', attr, 0)[0] * 0.1
        elif i == nl80211h.NL80211_RATE_INFO_BITRATE: # legacy fallback rate
            bitrate['legacy'] = struct.unpack_from('H', attr, 0)[0]
        elif i == nl80211h.NL80211_RATE_INFO_MCS:
            bitrate['mcs-index'] = struct.unpack_from('B', attr, 0)[0]
        elif i == nl80211h.NL80211_RATE_INFO_40_MHZ_WIDTH: # flag
            bitrate['width'] = 40
        elif i == nl80211h.NL80211_RATE_INFO_SHORT_GI: # flag
            bitrate['gi'] = 0

    # clean it up before returning
    # remove legacy if we have rate or make rate = legacy if we dont have rate
    # remove mcs-index and short gi and 40 MHz if there is no mcs-index
    if bitrate['legacy'] and not bitrate['rate']: bitrate['rate'] = bitrate['legacy']
    if bitrate['rate'] and bitrate['legacy']: del bitrate['legacy']
    if bitrate['mcs-index'] is None:
        del bitrate['mcs-index']
        del bitrate['gi']
        del bitrate['width']

    return bitrate

#### NETLINK/IOCTL PARAMETERS ####

def _ifindex_(dev, iosock=None):
    """
     gets the ifindex for device
     :param dev: device name:
     :param iosock: ioctl socket
     :returns: ifindex of device
     NOTE: the ifindex can aslo be found in /sys/class/net/<nic>/ifindex
    """
    if iosock is None: return _iostub_(_ifindex_, dev)

    try:
        flag = sioch.SIOCGIFINDEX
        ret = io.io_transfer(iosock, flag, ifh.ifreq(dev, flag))
        return struct.unpack_from(ifh.ifr_ifindex, ret, ifh.IFNAMELEN)[0]
    except AttributeError as e:
        raise pyric.error(pyric.EINVAL, e)
    except struct.error as e:
        raise pyric.error(pyric.EUNDEF, "Error parsing results: {0}".format(e))

def _familyid_(nlsock):
    """
     extended version: get the family id
     :param nlsock: netlink socket
     :returns: the family id of nl80211
     NOTE:
      In addition to the family id, we get:
       CTRL_ATTR_FAMILY_NAME = nl80211\x00
       CTRL_ATTR_VERSION = \x01\x00\x00\x00 = 1
       CTRL_ATTR_HDRSIZE = \x00\x00\x00\x00 = 0
       CTRL_ATTR_MAXATTR = \xbf\x00\x00\x00 = 191
       CTRL_ATTR_OPS
       CTRL_ATTR_MCAST_GROUPS
      but for now, these are not used
    """
    global _FAM80211ID_
    if _FAM80211ID_ is None:
        # family id is not instantiated, do so now
        msg = nl.nlmsg_new(nltype=genlh.GENL_ID_CTRL,
                           cmd=genlh.CTRL_CMD_GETFAMILY,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_string(msg,
                          nl80211h.NL80211_GENL_NAME,
                          genlh.CTRL_ATTR_FAMILY_NAME)
        nl.nl_sendmsg(nlsock, msg)
        rmsg = nl.nl_recvmsg(nlsock)
        _FAM80211ID_ = nl.nla_find(rmsg, genlh.CTRL_ATTR_FAMILY_ID)
    return _FAM80211ID_

#### TRANSLATION FUNCTIONS ####

def _iostub_(fct, *argv):
    """
     translates from traditional ioctl <cmd> to extended <cmd>ex
     :param fct: function to translate to
     :param argv: parameters to the function
     :returns: the results of fct
    """
    iosock = io.io_socket_alloc()
    try:
        argv = list(argv) + [iosock]
        return fct(*argv)
    except io.error as e:
        raise pyric.error(e.errno, pyric.strerror(e.errno))
    except pyric.error:
        raise # catch and rethrow
    finally:
        io.io_socket_free(iosock)

def _nlstub_(fct, *argv):
    """
     translates from traditional netlink <cmd> to extended <cmd>ex
     :param fct: function to translate to
     :param argv: parameters to the function
     :returns: rresults of fucntion
    """
    nlsock = None
    try:
        nlsock = nl.nl_socket_alloc(timeout=2)
        argv = list(argv) + [nlsock]
        return fct(*argv)
    except nl.error as e:
        raise pyric.error(e.errno, pyric.strerror(e.errno))
    except pyric.error:
        raise
    finally:
        if nlsock: nl.nl_socket_free(nlsock)

#### PENDING ####

def _fut_chset(card, ch, chw, nlsock=None):
    """
     set current channel on device (iw phy <card.phy> set channel <ch> <chw>
     :param card: Card object
     :param ch: channel number
     :param chw: channel width oneof {None|'HT20'|'HT40-'|'HT40+'}
     :param nlsock: netlink socket
     uses the newer NL80211_CMD_SET_CHANNEL vice iw's depecrated version which
     uses *_SET_WIPHY however, ATT does not work raise Errno 22 Invalid Argument
     NOTE: This only works for cards in monitor mode
    """
    if ch not in channels.channels(): raise pyric.error(pyric.EINVAL, "Invalid channel")
    if chw not in channels.CHTYPES: raise pyric.error(pyric.EINVAL, "Invalid channel width")
    if nlsock is None: return _nlstub_(_fut_chset, card, ch, chw)

    try:
        msg = nl.nlmsg_new(nltype=_familyid_(nlsock),
                           cmd=nl80211h.NL80211_CMD_SET_CHANNEL,
                           flags=nlh.NLM_F_REQUEST | nlh.NLM_F_ACK)
        nl.nla_put_u32(msg, card.idx, nl80211h.NL80211_ATTR_IFINDEX)
        nl.nla_put_u32(msg, channels.ch2rf(ch), nl80211h.NL80211_ATTR_WIPHY_FREQ)
        nl.nla_put_u32(msg, channels.CHTYPES.index(chw), nl80211h.NL80211_ATTR_WIPHY_CHANNEL_TYPE)
        nl.nl_sendmsg(nlsock, msg)
        _ = nl.nl_recvmsg(nlsock)
    except AttributeError:
        raise pyric.error(pyric.EINVAL, "Invalid Card")
    except nl.error as e:
        raise pyric.error(e.errno, e.strerror)