SDF Sample: Checking Device Network Interfaces

Smart Device Framework 2.2 refactored a lot of our network interface classes.  Yes, it’s likely to casue some angst with people that used the older stuff, but it really is for the best.  I never liked the older mechanism that exposed things like SSID for network interfaces that weren’t even wireless.  The new model is much friendlier and object-oriented.  Here’s a quick (and very thorough) example of how to use the new classes.  As you can see, there’s *nothing* to be known about the interface that isn’t exposed.


NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
WirelessZeroConfigNetworkInterface wzcInterface = null;

foreach (NetworkInterface ni in interfaces)
{
    Debug.WriteLine(string.Format(“===========rnInterface {0}rn===========”, ni.Id));
    Debug.WriteLine(string.Format(” Type: {0}”, ni.GetType().Name));
    Debug.WriteLine(string.Format(” Name: {0}”, ni.Name));
    Debug.WriteLine(string.Format(” Description: {0}”, ni.Description));
    Debug.WriteLine(string.Format(” Network Interface Type: {0}”, ni.NetworkInterfaceType.ToString()));
    PhysicalAddress address = ni.GetPhysicalAddress();
    Debug.WriteLine(string.Format(” MAC: {0}”, address == null ? “{none}” : address.ToString()));
    Debug.WriteLine(string.Format(” Operational Status: {0}”, ni.OperationalStatus.ToString()));
    Debug.WriteLine(string.Format(” Speed: {0}bps”, ni.Speed));
    Debug.WriteLine(string.Format(” *Interface Operational Status: {0}”, ni.InterfaceOperationalStatus.ToString()));
    Debug.WriteLine(string.Format(” *Current IP: {0}”, ni.CurrentIpAddress.ToString()));
    Debug.WriteLine(string.Format(” *Current Subnet: {0}”, ni.CurrentSubnetMask.ToString()));

    IPInterfaceProperties ipProps = ni.GetIPProperties();
    Debug.WriteLine(“rn IP Propertiesrn ————-“);

#region
— Server Lists —
    if (ipProps.DhcpServerAddresses.Count > 0)
    {
        for (int i = 0; i < ipProps.DhcpServerAddresses.Count; i++)
        {
            Debug.WriteLine(string.Format(” DHCP Server {0}: {1}”, i, ipProps.DhcpServerAddresses[i].ToString()));
        }
    }
    else
    {
        Debug.WriteLine(” No DHCP Servers”);
    }

    if (ipProps.DnsAddresses.Count > 0)
    {
        for (int i = 0; i < ipProps.DnsAddresses.Count; i++)
        {
            Debug.WriteLine(string.Format(” DNS Server {0}: {1}”, i, ipProps.DnsAddresses[i].ToString()));
        }
    }
    else
    {
        Debug.WriteLine(” No DNS Servers”);
    }

    if (ipProps.GatewayAddresses.Count > 0)
    {
        for (int i = 0; i < ipProps.GatewayAddresses.Count; i++)
        {
            Debug.WriteLine(string.Format(” Gateway {0}: {1}”, i, ipProps.GatewayAddresses[i].Address.ToString()));
        }
    }
    else
    {
        Debug.WriteLine(” No Gateways”);
    }
    
    if (ipProps.WinsServersAddresses.Count > 0)
    {
        for (int i = 0; i < ipProps.WinsServersAddresses.Count; i++)
        {
            Debug.WriteLine(string.Format(” WINS Server {0}: {1}”, i, ipProps.WinsServersAddresses[i].ToString()));
        }
    }
    else
    {
        Debug.WriteLine(” No WINS Servers”);
    }
#endregion

#region — Unicast Addresses —
    if (ipProps.UnicastAddresses.Count > 0)
    {
        for (int i = 0; i < ipProps.UnicastAddresses.Count; i++)
        {
            Debug.WriteLine(string.Format(” Unicast address {0}: {1}”, i, ipProps.UnicastAddresses[i].Address.ToString()));
            Debug.WriteLine(string.Format(” Address Family: {0}”, ipProps.UnicastAddresses[i].Address.AddressFamily));
            if (ipProps.UnicastAddresses[i].Address.AddressFamily == AddressFamily.InterNetworkV6)
            {
                Debug.WriteLine(string.Format(” Scope ID: {0}”, ipProps.UnicastAddresses[i].Address.ScopeId));
            }
            else // ipv4
            {
                Debug.WriteLine(string.Format(” IPv4 Mask: {0}”, ipProps.UnicastAddresses[i].IPv4Mask == null 
                        ? “{none}” : ipProps.UnicastAddresses[i].IPv4Mask.ToString()));
            }
            Debug.WriteLine(string.Format(” Preferred Limetime: {0}”, ipProps.UnicastAddresses[i].AddressPreferredLifetime));
            Debug.WriteLine(string.Format(” Valid Limetime: {0}”, ipProps.UnicastAddresses[i].AddressValidLifetime));
            Debug.WriteLine(string.Format(” DHCP Lease Limetime: {0}”, ipProps.UnicastAddresses[i].DhcpLeaseLifetime));
            Debug.WriteLine(string.Format(” DAD State: {0}”, ipProps.UnicastAddresses[i].DuplicateAddressDetectionState));
            Debug.WriteLine(string.Format(” {0} DNS Eligible”, ipProps.UnicastAddresses[i].IsDnsEligible ? “IS” : “IS NOT”));
            Debug.WriteLine(string.Format(” {0} Transient”, ipProps.UnicastAddresses[i].IsTransient ? “IS” : “IS NOT”));
            Debug.WriteLine(string.Format(” Prefix Origin: {0}”, ipProps.UnicastAddresses[i].PrefixOrigin.ToString()));
            Debug.WriteLine(string.Format(” Suffix Origin: {0}”, ipProps.UnicastAddresses[i].SuffixOrigin.ToString()));
        }
    }
    else
    {
        Debug.WriteLine(” No Unicast addresses”);
    }
#endregion

#region
— Anycast Addresses —
    if (ipProps.AnycastAddresses.Count > 0)
    {
        for (int i = 0; i < ipProps.AnycastAddresses.Count; i++)
        {
            Debug.WriteLine(string.Format(” Anycast address {0}: {1}”, i, ipProps.AnycastAddresses[i].Address.ToString()));
            Debug.WriteLine(string.Format(” Address Family: {0}”, ipProps.AnycastAddresses[i].Address.AddressFamily));
            if (ipProps.AnycastAddresses[i].Address.AddressFamily == AddressFamily.InterNetworkV6)
            {
                Debug.WriteLine(string.Format(” Scope ID: {0}”, ipProps.AnycastAddresses[i].Address.ScopeId));
            }
            Debug.WriteLine(string.Format(” {0} DNS Eligible”, ipProps.UnicastAddresses[i].IsDnsEligible ? “IS” : “IS NOT”));
            Debug.WriteLine(string.Format(” {0} Transient”, ipProps.UnicastAddresses[i].IsTransient ? “IS” : “IS NOT”));
        }
    }
    else
    {
        Debug.WriteLine(” No Anycast addresses”);
    }
#endregion

#region — Multicast Addresses —
    if (ipProps.MulticastAddresses.Count > 0)
    {
        for (int i = 0; i < ipProps.MulticastAddresses.Count; i++)
        {
            Debug.WriteLine(string.Format(” Anycast address {0}: {1}”, i, ipProps.MulticastAddresses[i].Address.ToString()));
            Debug.WriteLine(string.Format(” Address Family: {0}”, ipProps.MulticastAddresses[i].Address.AddressFamily));
            if (ipProps.MulticastAddresses[i].Address.AddressFamily == AddressFamily.InterNetworkV6)
            {
                Debug.WriteLine(string.Format(” Scope ID: {0}”, ipProps.MulticastAddresses[i].Address.ScopeId));
            }
            Debug.WriteLine(string.Format(” {0} DNS Eligible”, ipProps.MulticastAddresses[i].IsDnsEligible ? “IS” : “IS NOT”));
            Debug.WriteLine(string.Format(” {0} Transient”, ipProps.MulticastAddresses[i].IsTransient ? “IS” : “IS NOT”));
        }
    }
    else
    {
        Debug.WriteLine(” No Multicast addresses”);
    }
#endregion

#region — IPv4 Stats —
    IPv4InterfaceStatistics ipstats = ni.GetIPv4Statistics();
    Debug.WriteLine(“rn IPv4 Statisticsrn ————-“);
    Debug.WriteLine(string.Format(” Bytes Received: {0}”, ipstats.BytesReceived));
    Debug.WriteLine(string.Format(” Bytes Sent: {0}”, ipstats.BytesSent));
    Debug.WriteLine(string.Format(” Incoming Packets Discarded: {0}”, ipstats.IncomingPacketsDiscarded));
    Debug.WriteLine(string.Format(” Incoming Packets with errors: {0}”, ipstats.IncomingPacketsWithErrors));
    Debug.WriteLine(string.Format(” Incoming Packets Unk. Protocol: {0}”, ipstats.IncomingUnknownProtocolPackets));
    Debug.WriteLine(string.Format(” Non Unicast Packets Received: {0}”, ipstats.NonUnicastPacketsReceived));
    Debug.WriteLine(string.Format(” Non Unicast Packets Sent: {0}”, ipstats.NonUnicastPacketsSent));
    Debug.WriteLine(string.Format(” Outgoing Packets Discarded: {0}”, ipstats.OutgoingPacketsDiscarded));
    Debug.WriteLine(string.Format(” Outgoing Packets With Errors: {0}”, ipstats.OutgoingPacketsWithErrors));
    Debug.WriteLine(string.Format(” Output Queue Length: {0}”, ipstats.OutputQueueLength));
    Debug.WriteLine(string.Format(” Unicast Packets Received: {0}”, ipstats.UnicastPacketsReceived));
    Debug.WriteLine(string.Format(” Unicast Packets Sent: {0}”, ipstats.UnicastPacketsSent));
#endregion

#region — IPv4 Properties —
    IPv4InterfaceProperties ipv4props = ipProps.GetIPv4Properties();
    Debug.WriteLine(“rn IPv4 Propertiesrn ————-“);
    Debug.WriteLine(string.Format(” Index: {0}”, ipv4props.Index));
    Debug.WriteLine(string.Format(” Auto Addressing {0}”, ipv4props.IsAutomaticPrivateAddressingActive 
        ? “ACTIVE” : “INACTIVE”));
    Debug.WriteLine(string.Format(” Auto Addressing {0}”, ipv4props.IsAutomaticPrivateAddressingEnabled 
        ? “ENABLED” : “DISABLED”));
    Debug.WriteLine(string.Format(” DHCP {0}”, ipv4props.IsDhcpEnabled ? “ENABLED” : “DISABLED”));
    Debug.WriteLine(string.Format(” MTU: {0}”, ipv4props.Mtu));
    Debug.WriteLine(string.Format(” {0} use WINS”, ipv4props.UsesWins ? “DOES” : “DOES NOT”));
#endregion

    if (ni is WirelessZeroConfigNetworkInterface)
    {
        WirelessZeroConfigNetworkInterface wzc = (WirelessZeroConfigNetworkInterface)ni;

        // save for later operations
        wzcInterface = wzc;

        Debug.WriteLine(“rn *WZC Propertiesrn ————-“);
        Debug.WriteLine(string.Format(” *Associated AP: {0}”, wzc.AssociatedAccessPoint));
        Debug.WriteLine(string.Format(” *Associated AP MAC: {0}”, wzc.AssociatedAccessPointMAC));
        Debug.WriteLine(string.Format(” *Authentication Mode: {0}”, wzc.AuthenticationMode.ToString()));
        Debug.WriteLine(string.Format(” *WZC Fallback Enabled: {0}”, wzc.FallbackEnabled));
        Debug.WriteLine(string.Format(” *WEP Status: {0}”, wzc.WEPStatus));
        Debug.WriteLine(string.Format(” *Signal Strength: {0}db ({1})”, wzc.SignalStrength.Decibels, 
                wzc.SignalStrength.Strength.ToString()));
        Debug.WriteLine(string.Format(” *Authentication Mode: {0}”, wzc.AuthenticationMode.ToString()));
        Debug.WriteLine(string.Format(” *Infrastructure Mode: {0}”, wzc.InfrastructureMode.ToString()));
        Debug.WriteLine(string.Format(” *WEP Status: {0}”, wzc.WEPStatus.ToString()));
        foreach (int rate in wzc.SupportedRates)
        {
            Debug.WriteLine(string.Format(” *Supports {0}kbps”, rate));
        }
        Debug.WriteLine(string.Format(” *Radio ATIM Window: {0}Kµsec”, wzc.RadioConfiguration.ATIMWindow));
        Debug.WriteLine(string.Format(” *Radio beacon Period: {0}Kµsec”, wzc.RadioConfiguration.BeaconPeriod));
        Debug.WriteLine(string.Format(” *Radio Frequency: {0}kHz”, wzc.RadioConfiguration.Frequency));
        Debug.WriteLine(string.Format(” *Radio Frequency Hop Dwell Time: {0}Kµsec”,         
                wzc.RadioConfiguration.FrequencyHopDwellTime));
        Debug.WriteLine(string.Format(” *Radio Frequency Hop Pattern: {0}”, wzc.RadioConfiguration.FrequencyHopPattern));
        Debug.WriteLine(string.Format(” *Radio Frequency Hop Set: {0}”, wzc.RadioConfiguration.FrequencyHopSet));

#region — Preferred APs —
        if (wzc.PreferredAccessPoints.Count > 0)
        {
            Debug.WriteLine(“rn *Preferred APsrn ————-“);
            foreach (AccessPoint ap in wzc.PreferredAccessPoints)
            {
                Debug.WriteLine(string.Format(” *SSID: {0}”, ap.Name));
                Debug.WriteLine(string.Format(” *MAC {0}”, ap.PhysicalAddress.ToString()));
                Debug.WriteLine(string.Format(” *Mode {0}”, ap.InfrastructureMode.ToString()));
                Debug.WriteLine(string.Format(” *Network Type {0}”, ap.NetworkTypeInUse.ToString()));
                Debug.WriteLine(string.Format(” *Privacy {0}”, ap.Privacy));
                Debug.WriteLine(string.Format(” *Signal Strength: {0}db ({1})”, ap.SignalStrength.Decibels,                 
                        ap.SignalStrength.Strength.ToString()));
                // supported rates
                foreach (int rate in ap.SupportedRates)
                {
                    Debug.WriteLine(string.Format(” *Supports {0}kbps”, rate));
                }
            }
        }
        else
        {
            Debug.WriteLine(“rn *No Preferred APs”);
        }
#endregion
#region — Nearby APs —
        if (wzc.NearbyAccessPoints.Count > 0)
        {
            Debug.WriteLine(“rn *Nearby APsrn ————-“);
            foreach (AccessPoint ap in wzc.NearbyAccessPoints)
            {
                Debug.WriteLine(string.Format(” *SSID: {0}”, ap.Name));
                Debug.WriteLine(string.Format(” *MAC {0}”, ap.PhysicalAddress.ToString()));
                Debug.WriteLine(string.Format(” *Mode {0}”, ap.InfrastructureMode.ToString()));
                Debug.WriteLine(string.Format(” *Network Type {0}”, ap.NetworkTypeInUse.ToString()));
                Debug.WriteLine(string.Format(” *Privacy {0}”, ap.Privacy));
                Debug.WriteLine(string.Format(” *Signal Strength: {0}db ({1})”, ap.SignalStrength.Decibels, 
                        ap.SignalStrength.Strength.ToString()));
                // supported rates
                foreach (int rate in ap.SupportedRates)
                {
                    Debug.WriteLine(string.Format(” *Supports {0}kbps”, rate));
                }
            }
        }
        else
        {
            Debug.WriteLine(“rn *No Nearby APs”);
        }
#endregion
#region — Preferred APs —
        if (wzc.NearbyPreferredAccessPoints.Count > 0)
        {
            Debug.WriteLine(“rn *Preferred Nearby APsrn ————-“);
            foreach (AccessPoint ap in wzc.NearbyPreferredAccessPoints)
            {
                Debug.WriteLine(string.Format(” *SSID: {0}”, ap.Name));
                Debug.WriteLine(string.Format(” *MAC {0}”, ap.PhysicalAddress.ToString()));
                Debug.WriteLine(string.Format(” *Mode {0}”, ap.InfrastructureMode.ToString()));
                Debug.WriteLine(string.Format(” *Network Type {0}”, ap.NetworkTypeInUse.ToString()));
                Debug.WriteLine(string.Format(” *Privacy {0}”, ap.Privacy));
                Debug.WriteLine(string.Format(” *Signal Strength: {0}db ({1})”, ap.SignalStrength.Decibels,
                        ap.SignalStrength.Strength.ToString()));
                // supported rates
                foreach (int rate in ap.SupportedRates)
                {
                    Debug.WriteLine(string.Format(” *Supports {0}kbps”, rate));
                }
            }
        }
        else
        {
            Debug.WriteLine(“rn *No Nearby Preferred APs”);
        }
#endregion
    }
    else if (ni is WirelessNetworkInterface)
    {
        WirelessNetworkInterface wni = (WirelessNetworkInterface)ni;

        Debug.WriteLine(“rn *Wireless Propertiesrn ————-“);
        Debug.WriteLine(string.Format(” *Associated AP: {0}”, wni.AssociatedAccessPoint));
        Debug.WriteLine(string.Format(” *Associated AP MAC: {0}”, wni.AssociatedAccessPointMAC));
        Debug.WriteLine(string.Format(” *Signal Strength: {0}db ({1})”, wni.SignalStrength.Decibels,         
            wni.SignalStrength.Strength.ToString()));
        Debug.WriteLine(string.Format(” *Authentication Mode: {0}”, wni.AuthenticationMode.ToString()));
        Debug.WriteLine(string.Format(” *Infrastructure Mode: {0}”, wni.InfrastructureMode.ToString()));
        Debug.WriteLine(string.Format(” *WEP Status: {0}”, wni.WEPStatus.ToString()));
        foreach (int rate in wni.SupportedRates)
        {
            Debug.WriteLine(string.Format(” *Supports {0}kbps”, rate));
        }
        Debug.WriteLine(string.Format(” *Radio ATIM Window: {0}Kµsec”, wni.RadioConfiguration.ATIMWindow));
        Debug.WriteLine(string.Format(” *Radio beacon Period: {0}Kµsec”, wni.RadioConfiguration.BeaconPeriod));
        Debug.WriteLine(string.Format(” *Radio Frequency: {0}kHz”, wni.RadioConfiguration.Frequency));
        Debug.WriteLine(string.Format(” *Radio Frequency Hop Dwell Time: {0}Kµsec”
                wni.RadioConfiguration.FrequencyHopDwellTime));
        Debug.WriteLine(string.Format(” *Radio Frequency Hop Pattern: {0}”, wni.RadioConfiguration.FrequencyHopPattern));
        Debug.WriteLine(string.Format(” *Radio Frequency Hop Set: {0}”, wni.RadioConfiguration.FrequencyHopSet));
    }
} // foreach (NetworkInterface ni in interfaces)

if (wzcInterface != null)
{
        byte[] key = null;
        bool b = wzcInterface.AddPreferredNetwork(“MyOpenNetwork1”, true, key, 1, AuthenticationMode.Open, 
                WEPStatus.WEPDisabled, null);
        b = wzcInterface.AddPreferredNetwork(“MyOpenNetwork2”, true, key, 1, AuthenticationMode.Open, 
                WEPStatus.WEPDisabled, null);
        b = wzcInterface.AddPreferredNetwork(“MyOpenNetwork3”, true, key, 1, AuthenticationMode.Open, 
                WEPStatus.WEPDisabled, null);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s