Skip to content

Commit

Permalink
NetworkInterface.Linux: take into account physical link status for Op…
Browse files Browse the repository at this point in the history
…erationalStatus and GetIsNetworkAvailable (#44867)
  • Loading branch information
tmds authored Nov 26, 2020
1 parent eb44c3e commit 31ffad2
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public enum NetworkChangeKind
None = -1,
AddressAdded = 0,
AddressRemoved = 1,
LinkAdded = 2,
LinkRemoved = 3,
AvailabilityChanged = 4
AvailabilityChanged = 2
}

public delegate void NetworkChangeEvent(int socket, NetworkChangeKind kind);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,9 @@ int32_t SystemNative_GetNetworkInterfaces(int32_t * interfaceCount, NetworkInter
nii->SupportsMulticast = 1;
}

// Get administrative state as best guess for now.
nii->OperationalState = (ifaddrsEntry->ifa_flags & IFF_UP) ? OperationalStatus_Up : OperationalStatus_Down;
// OperationalState returns whether the interface can transmit data packets.
// The administrator must have enabled the interface (IFF_UP), and the cable must be plugged in (IFF_RUNNING).
nii->OperationalState = ((ifaddrsEntry->ifa_flags & (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING)) ? OperationalStatus_Up : OperationalStatus_Down;
}

if (ifaddrsEntry->ifa_addr == NULL)
Expand Down
10 changes: 2 additions & 8 deletions src/libraries/Native/Unix/System.Native/pal_networkchange.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ static NetworkChangeKind ReadNewLinkMessage(struct nlmsghdr* hdr)
assert(hdr != NULL);
struct ifinfomsg* ifimsg;
ifimsg = (struct ifinfomsg*)NLMSG_DATA(hdr);
if (ifimsg->ifi_family == AF_INET)
if (ifimsg->ifi_family == AF_UNSPEC)
{
if ((ifimsg->ifi_flags & IFF_UP) != 0)
{
return LinkAdded;
}
return AvailabilityChanged;
}

return None;
Expand Down Expand Up @@ -111,9 +108,6 @@ void SystemNative_ReadEvents(int32_t sock, NetworkChangeEvent onNetworkChange)
case RTM_NEWLINK:
onNetworkChange(sock, ReadNewLinkMessage(hdr));
break;
case RTM_DELLINK:
onNetworkChange(sock, LinkRemoved);
break;
case RTM_NEWROUTE:
case RTM_DELROUTE:
{
Expand Down
4 changes: 1 addition & 3 deletions src/libraries/Native/Unix/System.Native/pal_networkchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ typedef enum
None = -1,
AddressAdded = 0,
AddressRemoved = 1,
LinkAdded = 2,
LinkRemoved = 3,
AvailabilityChanged = 4,
AvailabilityChanged = 2,
} NetworkChangeKind;

typedef void (*NetworkChangeEvent)(int32_t sock, NetworkChangeKind notificationKind);
Expand Down

0 comments on commit 31ffad2

Please sign in to comment.