Microsoft.SPOT.Net


The primary view of the networking capabilities of your device will be through two .NET Micro Framework namespaces: Microsoft.SPOT.Net and System.Net. You must add an assembly reference to your Microsoft Visual Studio project to the assemblies of the same name (that is, Microsoft.SPOT.Net and System.Net) to make their members available to your code.

Network Information

Within the System.Net namespace is the NetworkInformation subnamespace, which, as its name suggests, provides information about the networking capabilities of the current environment (that is, emulator or device).

Network Interfaces

Each available network device (or adapter) is represented and configured by a NetworkInterface object. Access to these objects is provided by the static method GetAllNetworkInterfaces, which returns an array of them. Most of the configuration information in the NetworkInterface objects pertain to network routing and addressing (analogous to street addresses), a complex subject that is given only cursory treatment here. The following sections describe the available information and configuration options, organized by type of addressing.

MAC Address

The MAC address is a globally unique identifier that represents the network adapter on the network, also referred to as the hardware or physical address. In the OSI model, MAC addresses work at layer 2 (data link). Access to MAC address is through the PhysicalAddress property of the NetworkInterface instance, which consists of 6 bytes (48 bits). The first 3 bytes are the organizational unique identifier (OUI), which is regulated and assigned by the IEEE. The last 3 bytes are the device's unique serial number assigned by the vendor. Taken together, these are required to be unique within a network; otherwise, information cannot be correctly routed between nodes.

MAC addresses have been designed to be read-only. They can be spoofed (you can substitute a value of your choosing) if your application requires it, but only if the underlying hardware supports it (which is vendor-specific) and you take steps to ensure that each device on the network has a unique identifier.

IP Addresses

Although MAC addresses are generally fixed and associated with hardware (Layer 2), IP addresses are generally dynamic and function in software (Layer 3). By having a second tier of addressing, it is possible for more than one device on a network to share the same IP address, allowing packet traffic to be routed to either one, as in the case of load balancing. Specifically, the .NET Micro Framework provides support for IP version 4 (IPv4) addresses, which are 4-byte (32-bit) values typically represented in so-called dot notation (or dotted decimal or dotted quad), for example, 127.0.0.1. (This particular address is the local loopback address, used by the local system to send information to itself.) IP addresses consist of two parts: the first 2 bytes specify the network address, and the second 2 bytes specify the host address (the adapter).

The IP address of a network adapter can be retrieved (as a string) through its NetworkInterface read-only IPAddress property and may be explicitly set using the EnableStaticIP method.

Device IP addresses can be assigned statically, using EnableStaticIP, dynamically, using dynamic host configuration protocol (DHCP). When defining a static IP address, you must ensure that the rest of the network is properly configured to support your choice of address. Also, the only method available for you to explicitly specify a static IP address, EnableStaticIP, requires two additional parameters: the subnet mask and the gateway address.

SubnetMask

IP networks are organized hierarchically. A subnet is a portion of the overall address space. A subnet mask determines which portion (subnet) the device belongs to. Network administrators often use subnetting for reasons of security or performance. This value is exposed as a string through the read-only SubnetMask property. Being a mask, it works by performing a bitwise AND on the IP address. In binary form, the 1s isolate the network portion of the address (the subnet), and the 0s act to filter out the host portion of the address. For example, a common subnet mask is 255.255.255.0, which specifies that only the last byte contains host information (implying a maximum of 255 devices). When represented in binary form, this becomes:

 11111111.11111111.11111111.00000000 

When an IP address, say 192.168.0.102, is then masked by this value, it has the result of removing the host information:

 Subnet mask: 11111111.11111111.11111111.00000000 IP address: 11000000.10101000.00000000.01100110 Subnet: 11000000.10101000.00000000.00000000 

Translated back to dotted decimal notation, the subnet is simply 192.168.0.0. Routers sit on the boundaries between subnets and serve as gateways.

GatewayAddress

When an IP packet is destined for an endpoint outside of the local subnet, it must pass through a gateway device, typically a router. This address is exposed as a string through the read-only property GatewayAddress. This value can be assigned as part of the call to EnableStaticIP or configured automatically by DHCP, described in the next section.

Typically, you will not manually assign these values. Instead, you will rely on the DHCP mechanism to automatically assign your device an IP address and to properly configure the other settings.

DHCP

When using dynamically assigned IP addresses, a DHCP service on the local network must be active and reachable. This service is responsible, as its name would suggest, for dynamically configuring devices (that is, hosts) for use on the local network. As we have discussed, this includes IP address assignment, the subnet mask, and the gateway address. It also can include a list of DNS servers, discussed in the next section. It is often the case that the router itself offers this service.

At its most basic, this service functions by maintaining a list of available IP addresses and mapping them to MAC addresses. A number of different approaches exist for performing this operation, but generally all involve a lease metaphor: IP addresses are assigned to a host for a finite period of time and then revoked.

The EnableDhcp method is the .NET Micro Framework mechanism for explicitly using the DHCP services on your local network for a specific NetworkInterface object. This is also the default, so it is not necessary to do this if you haven't previously statically assigned this information (in a call to EnableStaticIP). You can confirm the disposition of this facility by checking the read-only IsDhcpEnabled property.

It is also possible to manually and explicitly release your DHCP-assigned IP address or to request a renewal. Simply invoke one of the corresponding methods, neither of which takes any parameters: ReleaseDhcpLease and RenewDhcpLease.

Domain Name System (DNS)

Because IP addresses aren't easily remembered by most humans, a naming system has been developed that allows us to assign more friendly names to underlying IP addresses. You are no doubt already familiar with this through your use of the World Wide Web and the ubiquitous Uniform Resource Locator (URL), such as microsoft.com and dotnetmicroframework.com. To translate such names into IP addresses, a DNS service is often used.

The .NET Micro Framework supports the use of DNS through the System.Net.Dns static class (described in the section titled "The Dns Class" later in this chapter). By default, the system will attempt to automatically discover DNS servers. You can verify this by checking the read-only property IsDynamicDnsEnabled on a specific NetworkInterface. It also possible for you to explicitly specify a list of IP addresses of DNS servers on your network using the EnableStaticDns method:

 networkInterface.EnableStaticDns(new string[] { "157.54.14.162", "157.54.14.178", "157.54.14.146" }); 

We can also retrieve the list of DNS server IP addresses through the read-only DnsAddresses property, which returns an array of strings containing the IP addresses in dotted decimal notation.

NetworkInterfaceType

The last remaining piece of configuration information is the read-only NetworkInterfaceType property, which, at this time, will be either the Ethernet or Unknown enumerated value.

Ipconfig

To see the DNS values in action using a standard system command, at a Windows command prompt, issue the ipconfig command:

 ipconfig /all 

You should get a dump of each of the network adapters on your personal computer and their configuration information, similar to the following:

 Windows IP Configuration   Host Name . . . . . . . . . . . . : MyComputerName   Primary Dns Suffix . . . . . . . .: redmond.corp.microsoft.com   Node Type . . . . . . . . . . . . : Hybrid   IP Routing Enabled. . . . . . . . : No   WINS Proxy Enabled. . . . . . . . : No   DNS Suffix Search List. . . . . . : redmond.corp.microsoft.com                                       HOME   System Quarantine State . . . . . : Not Restricted Wireless LAN adapter Wireless Network Connection:   Connection-specific DNS Suffix .. : HOME   Description . . . . . . . . . . . : Intel(R) PRO/Wireless 3945ABG Network Connection   Physical Address. . . . . . . . . : 00-13-02-17-0E-48   DHCP Enabled. . . . . . . . . . . : Yes   Autoconfiguration Enabled . . . . : Yes   Link-local IPv6 Address . . . . . : fe80::98ba:dab3:982a:6943%8(Preferred)   IPv4 Address. . . . . . . . . . . : 192.168.0.102(Preferred)   Subnet Mask . . . . . . . . . . . : 255.255.255.0   Lease Obtained. . . . . . . . . . : Wednesday, January 17, 2007 4:20:12 PM   Lease Expires . . . . . . . . . . : Friday, January 26, 2007 9:37:42 AM   Default Gateway . . . . . . . . . : 192.168.0.1   DHCPv6 IAID . . . . . . . . . . . : 134222594   DNS Servers . . . . . . . . . . . : 192.168.0.1   NetBIOS over Tcpip. . . . . . . . : Enabled Ethernet adapter Local Area Connection:   Media State . . . . . . . . . . . : Media disconnected   Connection-specific DNS Suffix . : redmond.corp.microsoft.com   Description . . . . . . . . . . . : Intel(R) PRO/1000 PL Network Connection   Physical Address. . . . . . . . . : 00-0E-7B-56-DA-DA   DHCP Enabled. . . . . . . . . . . : Yes   Autoconfiguration Enabled . . . . : Yes 

Listing 6-1 accomplishes something similar using the .NET Micro Framework APIs we've just described, which you can try out in the emulator.

Listing 6-1: Enumerating the available network interfaces

image from book
 public static void DumpNetworkInterfaces() {    NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();    int i = 0;    foreach (NetworkInterface networkInterface in networkInterfaces)    {       Debug.Print("*** Network Interface #" + i++);       Debug.Print(" IP Address: " + networkInterface.IPAddress);       Debug.Print(" Gateway Address: " + networkInterface.GatewayAddress);       byte[] physicalAddress = networkInterface.PhysicalAddress;       if (physicalAddress.Length == 6)       {          Debug.Print(" Physical Address: " +             physicalAddress[0] + "-" +             physicalAddress[1] + "-" +             physicalAddress[2] + "-" +             physicalAddress[3] + "-" +             physicalAddress[4] + "-" +             physicalAddress[5]);       }       else          Debug.Print(" Physical Address: (none)");          Debug.Print(" Subnet Mask: " + networkInterface.SubnetMask);       if (networkInterface.DnsAddresses.Length > 0)       {          Debug.Print(" DNS Addresses: ");          foreach (string dnsAddress in networkInterface.DnsAddresses)             Debug.Print("\t" + dnsAddress);       }       else             Debug.Print(" DNS Addresses: (none)");             Debug.Print(" DHCP Enabled: " + networkInterface.IsDhcpEnabled);             Debug.Print(" Dynamic DNS Enabled: " +               networkInterface.IsDynamicDnsEnabled);             Debug.Print("Network Interface Type: " + (networkInterface.               NetworkInterfaceType == NetworkInterfaceType.Ethernet ?             "Ethernet" : "unknown"));       } } 
image from book

The following is sample output produced from this routine running within the emulator on a laptop running Vista connected to a home WiFi network:

 *** Network Interface #0            IP Address: 192.168.0.2       Gateway Address: 192.168.0.1      Physical Address: 0-17-67-28-204-110            Subnet Mask: 0.0.0.0          DNS Addresses: 192.168.0.1           DHCP Enabled: True    Dynamic DNS Enabled: True Network Interface Type: unknown *** Network Interface #1            IP Address: 197.56.144.201       Gateway Address: 197.56.144.1      Physical Address: 2-12-207-12-94-210           Subnet Mask: 0.0.0.0         DNS Addresses: 197.54.14.162                        197.54.14.178          DHCP Enabled: True    Dynamic DNS Enabled: True Network Interface Type: Ethernet 

Now that we've explored the network interfaces, their configuration information, and how to control their high-level operation, we'll next learn how to program network applications using them.




Embedded Programming with the Microsoft .Net Micro Framework
Embedded Programming with the Microsoft .NET Micro Framework
ISBN: 0735623651
EAN: 2147483647
Year: 2007
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net