Skip to content

Commit 9b491c2

Browse files
committed
Wifi (Linux): fix interface status when the interface is up but not connected
1 parent be3e2ba commit 9b491c2

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

src/detection/wifi/wifi_linux.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,21 +554,48 @@ const char* ffDetectWifi(FF_MAYBE_UNUSED FFlist* result)
554554
item->conn.channel = 0;
555555
item->conn.frequency = 0;
556556

557+
char operstate;
557558
ffStrbufSetF(&buffer, "/sys/class/net/%s/operstate", i->if_name);
558-
if (!ffAppendFileBuffer(buffer.chars, &item->inf.status))
559+
if (!ffReadFileData(buffer.chars, 1, &operstate))
559560
{
560561
FF_DEBUG("Failed to read operstate file");
561562
continue;
562563
}
563564

564-
ffStrbufTrimRightSpace(&item->inf.status);
565-
FF_DEBUG("Interface status: %s", item->inf.status.chars);
566-
if (!ffStrbufEqualS(&item->inf.status, "up"))
565+
FF_DEBUG("Connection status: %c", operstate);
566+
if (operstate != 'u')
567567
{
568568
FF_DEBUG("Skipping interface as it's not up");
569+
ffStrbufSetStatic(&item->conn.status, "disconnected");
570+
571+
ffStrbufSetF(&buffer, "/sys/class/net/%s/flags", i->if_name);
572+
char flags[16];
573+
ssize_t len = ffReadFileData(buffer.chars, sizeof(flags), flags);
574+
if (len <= 0)
575+
{
576+
FF_DEBUG("Failed to read flags file");
577+
ffStrbufSetStatic(&item->inf.status, "unknown");
578+
continue;
579+
}
580+
flags[len] = '\0';
581+
FF_DEBUG("Interface flags: %s", flags);
582+
unsigned flagsVal = (unsigned) strtoul(flags, NULL, 16);
583+
if (flagsVal & IFF_UP)
584+
{
585+
ffStrbufSetStatic(&item->inf.status, "up");
586+
FF_DEBUG("Interface is up but not connected");
587+
}
588+
else
589+
{
590+
ffStrbufSetStatic(&item->inf.status, "down");
591+
FF_DEBUG("Interface is down");
592+
}
593+
569594
continue;
570595
}
571596

597+
ffStrbufSetStatic(&item->inf.status, "up");
598+
572599
FF_DEBUG("Trying to detect wifi with iw");
573600
if (detectWifiWithIw(item, &buffer) != NULL)
574601
{

0 commit comments

Comments
 (0)