Skip to content

Commit 2bb2e10

Browse files
frenzymadnesshroncok
authored andcommitted
Add function for getting wireless protocol from wireless devices
1 parent d8deb69 commit 2bb2e10

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

python-ethtool/ethtool.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <sys/types.h>
3030
#include <ifaddrs.h>
3131
#include <netlink/route/addr.h>
32+
#include <linux/wireless.h>
3233
#if !defined IFF_UP
3334
#include <net/if.h>
3435
#endif
@@ -625,6 +626,37 @@ static PyObject *get_sg(PyObject *self __unused, PyObject *args)
625626
return Py_BuildValue("b", value);
626627
}
627628

629+
static PyObject *get_wireless_protocol (PyObject *self __unused, PyObject *args)
630+
{
631+
struct iwreq iwr;
632+
const char *devname;
633+
int fd, err;
634+
635+
if (!PyArg_ParseTuple(args, "s", &devname))
636+
return NULL;
637+
638+
/* Setup our request structure. */
639+
memset(&iwr, 0, sizeof(iwr));
640+
strncpy(iwr.ifr_name, devname, IFNAMSIZ);
641+
642+
/* Open control socket. */
643+
fd = socket(AF_INET, SOCK_DGRAM, 0);
644+
if (fd < 0) {
645+
PyErr_SetFromErrno(PyExc_OSError);
646+
return NULL;
647+
}
648+
err = ioctl(fd, SIOCGIWNAME, &iwr);
649+
if(err < 0) {
650+
PyErr_SetFromErrno(PyExc_IOError);
651+
close(fd);
652+
return NULL;
653+
}
654+
655+
close(fd);
656+
657+
return PyStr_FromString(iwr.u.name);
658+
}
659+
628660
struct struct_desc {
629661
char *name;
630662
unsigned short offset;
@@ -906,6 +938,11 @@ static struct PyMethodDef PyEthModuleMethods[] = {
906938
.ml_meth = (PyCFunction)get_flags,
907939
.ml_flags = METH_VARARGS,
908940
},
941+
{
942+
.ml_name = "get_wireless_protocol",
943+
.ml_meth = (PyCFunction)get_wireless_protocol,
944+
.ml_flags = METH_VARARGS,
945+
},
909946
{ .ml_name = NULL, },
910947
};
911948

0 commit comments

Comments
 (0)