Skip to content

Commit f33ca03

Browse files
rubenkacmel
authored andcommitted
ethtool: Add a function to get the interface flags
Signed-off-by: Ruben Kerkhof <ruben@rubenkerkhof.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 24c8d88 commit f33ca03

File tree

1 file changed

+65
-15
lines changed

1 file changed

+65
-15
lines changed

python-ethtool/ethtool.c

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ static PyObject *get_devices(PyObject *self __unused, PyObject *args __unused)
124124

125125
static PyObject *get_hwaddress(PyObject *self __unused, PyObject *args)
126126
{
127-
struct ethtool_cmd ecmd;
128127
struct ifreq ifr;
129128
int fd, err;
130129
char *devname;
@@ -133,8 +132,7 @@ static PyObject *get_hwaddress(PyObject *self __unused, PyObject *args)
133132
if (!PyArg_ParseTuple(args, "s", &devname))
134133
return NULL;
135134

136-
/* Setup our control structures. */
137-
memset(&ecmd, 0, sizeof(ecmd));
135+
/* Setup our request structure. */
138136
memset(&ifr, 0, sizeof(ifr));
139137
strncpy(&ifr.ifr_name[0], devname, IFNAMSIZ);
140138
ifr.ifr_name[IFNAMSIZ - 1] = 0;
@@ -173,7 +171,6 @@ static PyObject *get_hwaddress(PyObject *self __unused, PyObject *args)
173171

174172
static PyObject *get_ipaddress(PyObject *self __unused, PyObject *args)
175173
{
176-
struct ethtool_cmd ecmd;
177174
struct ifreq ifr;
178175
int fd, err;
179176
char *devname;
@@ -182,8 +179,7 @@ static PyObject *get_ipaddress(PyObject *self __unused, PyObject *args)
182179
if (!PyArg_ParseTuple(args, "s", &devname))
183180
return NULL;
184181

185-
/* Setup our control structures. */
186-
memset(&ecmd, 0, sizeof(ecmd));
182+
/* Setup our request structure. */
187183
memset(&ifr, 0, sizeof(ifr));
188184
strncpy(&ifr.ifr_name[0], devname, IFNAMSIZ);
189185
ifr.ifr_name[IFNAMSIZ - 1] = 0;
@@ -217,9 +213,44 @@ static PyObject *get_ipaddress(PyObject *self __unused, PyObject *args)
217213
return PyString_FromString(ipaddr);
218214
}
219215

216+
static PyObject *get_flags (PyObject *self __unused, PyObject *args)
217+
{
218+
struct ifreq ifr;
219+
char *devname;
220+
int fd, err;
221+
222+
if (!PyArg_ParseTuple(args, "s", &devname))
223+
return NULL;
224+
225+
/* Setup our request structure. */
226+
memset(&ifr, 0, sizeof(ifr));
227+
strncpy(&ifr.ifr_name[0], devname, IFNAMSIZ);
228+
ifr.ifr_name[IFNAMSIZ - 1] = 0;
229+
230+
/* Open control socket. */
231+
fd = socket(AF_INET, SOCK_DGRAM, 0);
232+
if (fd < 0) {
233+
PyErr_SetString(PyExc_OSError, strerror(errno));
234+
return NULL;
235+
}
236+
err = ioctl(fd, SIOCGIFFLAGS, &ifr);
237+
if(err < 0) {
238+
char buf[2048];
239+
int eno = errno;
240+
snprintf(buf, sizeof(buf), "[Errno %d] %s", eno, strerror(eno));
241+
PyErr_SetString(PyExc_IOError, buf);
242+
close(fd);
243+
return NULL;
244+
}
245+
246+
close(fd);
247+
248+
return Py_BuildValue("h", ifr.ifr_flags);
249+
250+
251+
}
220252
static PyObject *get_netmask (PyObject *self __unused, PyObject *args)
221253
{
222-
struct ethtool_cmd ecmd;
223254
struct ifreq ifr;
224255
int fd, err;
225256
char *devname;
@@ -228,8 +259,7 @@ static PyObject *get_netmask (PyObject *self __unused, PyObject *args)
228259
if (!PyArg_ParseTuple(args, "s", &devname))
229260
return NULL;
230261

231-
/* Setup our control structures. */
232-
memset(&ecmd, 0, sizeof(ecmd));
262+
/* Setup our request structure. */
233263
memset(&ifr, 0, sizeof(ifr));
234264
strncpy(&ifr.ifr_name[0], devname, IFNAMSIZ);
235265
ifr.ifr_name[IFNAMSIZ - 1] = 0;
@@ -265,7 +295,6 @@ static PyObject *get_netmask (PyObject *self __unused, PyObject *args)
265295

266296
static PyObject *get_broadcast(PyObject *self __unused, PyObject *args)
267297
{
268-
struct ethtool_cmd ecmd;
269298
struct ifreq ifr;
270299
int fd, err;
271300
char *devname;
@@ -274,8 +303,7 @@ static PyObject *get_broadcast(PyObject *self __unused, PyObject *args)
274303
if (!PyArg_ParseTuple(args, "s", &devname))
275304
return NULL;
276305

277-
/* Setup our control structures. */
278-
memset(&ecmd, 0, sizeof(ecmd));
306+
/* Setup our request structure. */
279307
memset(&ifr, 0, sizeof(ifr));
280308
strncpy(&ifr.ifr_name[0], devname, IFNAMSIZ);
281309
ifr.ifr_name[IFNAMSIZ - 1] = 0;
@@ -427,7 +455,7 @@ static PyObject *get_businfo(PyObject *self __unused, PyObject *args)
427455

428456
static int send_command(int cmd, char *devname, void *value)
429457
{
430-
/* Setup our control structures. */
458+
/* Setup our request structure. */
431459
int fd, err;
432460
struct ifreq ifr;
433461
struct ethtool_value *eval = value;
@@ -463,7 +491,6 @@ static int get_dev_value(int cmd, PyObject *args, void *value)
463491
int err = -1;
464492

465493
if (PyArg_ParseTuple(args, "s", &devname))
466-
/* Setup our control structures. */
467494
err = send_command(cmd, devname, value);
468495

469496
return err;
@@ -809,11 +836,34 @@ static struct PyMethodDef PyEthModuleMethods[] = {
809836
.ml_meth = (PyCFunction)get_sg,
810837
.ml_flags = METH_VARARGS,
811838
},
839+
{
840+
.ml_name = "get_flags",
841+
.ml_meth = (PyCFunction)get_flags,
842+
.ml_flags = METH_VARARGS,
843+
},
812844
{ .ml_name = NULL, },
813845
};
814846

815847
PyMODINIT_FUNC initethtool(void)
816848
{
817-
Py_InitModule("ethtool", PyEthModuleMethods);
849+
PyObject *m;
850+
m = Py_InitModule("ethtool", PyEthModuleMethods);
851+
PyModule_AddIntConstant(m, "IFF_UP", IFF_UP); /* Interface is up. */
852+
PyModule_AddIntConstant(m, "IFF_BROADCAST", IFF_BROADCAST); /* Broadcast address valid. */
853+
PyModule_AddIntConstant(m, "IFF_DEBUG", IFF_DEBUG); /* Turn on debugging. */
854+
PyModule_AddIntConstant(m, "IFF_LOOPBACK", IFF_LOOPBACK); /* Is a loopback net */
855+
PyModule_AddIntConstant(m, "IFF_POINTOPOINT", IFF_POINTOPOINT); /* Is a point-to-point link */
856+
PyModule_AddIntConstant(m, "IFF_NOTRAILERS", IFF_NOTRAILERS); /* Avoid use of trailers */
857+
PyModule_AddIntConstant(m, "IFF_RUNNING", IFF_RUNNING); /* Resources allocated */
858+
PyModule_AddIntConstant(m, "IFF_NOARP", IFF_NOARP); /* No address resolution protocol. */
859+
PyModule_AddIntConstant(m, "IFF_PROMISC", IFF_PROMISC); /* Receive all packets. */
860+
PyModule_AddIntConstant(m, "IFF_ALLMULTI", IFF_ALLMULTI); /* Receive all multicast packets. */
861+
PyModule_AddIntConstant(m, "IFF_MASTER", IFF_MASTER); /* Master of a load balancer. */
862+
PyModule_AddIntConstant(m, "IFF_SLAVE", IFF_SLAVE); /* Slave of a load balancer. */
863+
PyModule_AddIntConstant(m, "IFF_MULTICAST", IFF_MULTICAST); /* Supports multicast. */
864+
PyModule_AddIntConstant(m, "IFF_PORTSEL", IFF_PORTSEL); /* Can set media type. */
865+
PyModule_AddIntConstant(m, "IFF_AUTOMEDIA", IFF_AUTOMEDIA); /* Auto media select active. */
866+
PyModule_AddIntConstant(m, "IFF_DYNAMIC", IFF_DYNAMIC); /* Dialup device with changing addresses. */
818867
}
819868

869+

0 commit comments

Comments
 (0)