@@ -430,6 +430,106 @@ psutil_net_if_mtu(PyObject *self, PyObject *args) {
430
430
}
431
431
432
432
433
+ /*
434
+ * Get all of the NIC flags and return them.
435
+ */
436
+ static PyObject *
437
+ psutil_net_if_flags (PyObject * self , PyObject * args ) {
438
+ char * nic_name ;
439
+ int sock = -1 ;
440
+ int ret ;
441
+ struct ifreq ifr ;
442
+ PyObject * py_retlist = PyList_New (0 );
443
+ PyObject * py_flag = NULL ;
444
+ short int flags ;
445
+
446
+ if (py_retlist == NULL )
447
+ return NULL ;
448
+
449
+ if (! PyArg_ParseTuple (args , "s" , & nic_name ))
450
+ return NULL ;
451
+
452
+ sock = socket (AF_INET , SOCK_DGRAM , 0 );
453
+ if (sock == -1 )
454
+ return NULL ;
455
+
456
+ PSUTIL_STRNCPY (ifr .ifr_name , nic_name , sizeof (ifr .ifr_name ));
457
+ ret = ioctl (sock , SIOCGIFFLAGS , & ifr );
458
+ if (ret == -1 )
459
+ goto error ;
460
+
461
+ close (sock );
462
+ sock = -1 ;
463
+
464
+ flags = ifr .ifr_flags & 0xFFFF ;
465
+
466
+ if (flags & IFF_UP ) {
467
+ py_flag = PyUnicode_DecodeFSDefault ("up" );
468
+ if (PyList_Append (py_retlist , py_flag ))
469
+ goto error ;
470
+ }
471
+ if (flags & IFF_BROADCAST ) {
472
+ py_flag = PyUnicode_DecodeFSDefault ("broadcast" );
473
+ if (PyList_Append (py_retlist , py_flag ))
474
+ goto error ;
475
+ }
476
+ if (flags & IFF_DEBUG ) {
477
+ py_flag = PyUnicode_DecodeFSDefault ("debug" );
478
+ if (PyList_Append (py_retlist , py_flag ))
479
+ goto error ;
480
+ }
481
+ if (flags & IFF_LOOPBACK ) {
482
+ py_flag = PyUnicode_DecodeFSDefault ("loopback" );
483
+ if (PyList_Append (py_retlist , py_flag ))
484
+ goto error ;
485
+ }
486
+ if (flags & IFF_POINTOPOINT ) {
487
+ py_flag = PyUnicode_DecodeFSDefault ("pointopoint" );
488
+ if (PyList_Append (py_retlist , py_flag ))
489
+ goto error ;
490
+ }
491
+ if (flags & IFF_NOTRAILERS ) {
492
+ py_flag = PyUnicode_DecodeFSDefault ("notrailers" );
493
+ if (PyList_Append (py_retlist , py_flag ))
494
+ goto error ;
495
+ }
496
+ if (flags & IFF_RUNNING ) {
497
+ py_flag = PyUnicode_DecodeFSDefault ("running" );
498
+ if (PyList_Append (py_retlist , py_flag ))
499
+ goto error ;
500
+ }
501
+ if (flags & IFF_NOARP ) {
502
+ py_flag = PyUnicode_DecodeFSDefault ("noarp" );
503
+ if (PyList_Append (py_retlist , py_flag ))
504
+ goto error ;
505
+ }
506
+ if (flags & IFF_PROMISC ) {
507
+ py_flag = PyUnicode_DecodeFSDefault ("promisc" );
508
+ if (PyList_Append (py_retlist , py_flag ))
509
+ goto error ;
510
+ }
511
+ if (flags & IFF_ALLMULTI ) {
512
+ py_flag = PyUnicode_DecodeFSDefault ("allmulti" );
513
+ if (PyList_Append (py_retlist , py_flag ))
514
+ goto error ;
515
+ }
516
+ if (flags & IFF_MULTICAST ) {
517
+ py_flag = PyUnicode_DecodeFSDefault ("multicast" );
518
+ if (PyList_Append (py_retlist , py_flag ))
519
+ goto error ;
520
+ }
521
+
522
+ return py_retlist ;
523
+
524
+ error :
525
+ Py_XDECREF (py_flag );
526
+ Py_DECREF (py_retlist );
527
+ if (sock != -1 )
528
+ close (sock );
529
+ return NULL ;
530
+ }
531
+
532
+
433
533
/*
434
534
* Inspect NIC flags, returns a bool indicating whether the NIC is
435
535
* running. References:
@@ -667,6 +767,7 @@ static PyMethodDef mod_methods[] = {
667
767
{"getpagesize" , psutil_getpagesize_pywrapper , METH_VARARGS },
668
768
{"getpriority" , psutil_posix_getpriority , METH_VARARGS },
669
769
{"net_if_addrs" , psutil_net_if_addrs , METH_VARARGS },
770
+ {"net_if_flags" , psutil_net_if_flags , METH_VARARGS },
670
771
{"net_if_is_running" , psutil_net_if_is_running , METH_VARARGS },
671
772
{"net_if_mtu" , psutil_net_if_mtu , METH_VARARGS },
672
773
{"setpriority" , psutil_posix_setpriority , METH_VARARGS },
0 commit comments