free web page counters

Windows Mobile Pocket PC Smartphone Programming

==>Click here for the SiteMap<==. Original contents with decent amount of source codes.

Friday, April 28, 2006

/etc/hosts file equivalent in Windows Mobile device

====>SiteMap of this Blog<===

/etc/hosts file equivalent in Windows Mobile device

As you know, the "hosts" file in Windows and other operating systems is used to associate host names with IP addresses. When TCP/IP networking stack tries to resolve a host name to its IP address, it first consults the local hosts file to see whether such mapping is already available. If not, domain name (DNS) server is then consulted. For example, all machines' hosts file might contain the following line:

127.0.0.1 localhost

It maps "localhost" to the loopback address. There is no magic; the TCP/IP stack won't treat "localhost" differently from "www.mit.edu" when it tries to resolve the string name. (For curious mind, the difference comes when IP stack realizes that the IP's first number is 127, a special number.)

Why hosts-like functionality is needed for Windows Mobile device?

Windows Mobile devices does not have the concept of HOSTS file. DNS or WINS is always used. However, a hosts-file-like capability might be useful in certain cases. Below I listed a few possible cases:

  • Corporate intranet: In a corporate intranet environment, quite possibly a large number of Windows Mobile devices are used. Unlike desktop PCs or Linux/Solaris servers, handheld device does not have a good way for naming, thus typically give IT department more management overhead. Distributed a common hosts file to all Windows Mobile devices is one way to manage.
  • Some special APIs: A typical example is MSMQ for Windows Mobile devices. In MSMQ, a message queue can be referenced by its path name in the form of "MachineName\QueueName". So able to resolve machine name is essential for MSMQ application to work.
  • Home or Home Office: At your home you may have a wireless router, several PCs and Windows Mobile devices, all having static-assigned IPs or DHCP-ed IPs. And, you need to sync a device with a home PC over wireless. The trouble is that ActiveSync in the device side needs the name of the desktop PC where ActiveSync is also running, and there is no way for the device to resolve the desktop PC's name (unless you really want to run a WINS service in one desktop PC and configure your device to use the WINS service for name resolution.)

How to make "hosts" in Windows Mobile devices for name resolution?

Although there is no hosts file in Pocket PC or Smartphone's file-system, registry is the place to put such name-IP mappings. To achieve this, add a subkey to HKEY_LOCAL_MACHINE\Comm\Tcpip\Hosts for each machine name that must be resolved, then add binary values for the IP address (value name "ipaddr") and expiration time (value name "expiretime"). The following shows the export of a key entry that resolves the name "hello" to IP address 161.163.165.169. Expiretime is a huge number, indicating no expiration. For the sole purpose of name resolution, expiretime is not strictly needed.

[HKEY_LOCAL_MACHINE\Comm\Tcpip\Hosts\hello]
"ipaddr"=hex:a1,a3,a5,a9
"ExpireTime"=hex:99,99,99,99,99,99,99

Below are the screen shots how to manually add the entry using Remote Registry Editor.

Below is the screen cut shows testing name resolution in the device. The following simple code is used:

C++:
HOSTENT* pHostent = gethostbyname(pchHostName)
or C#:
System.Net.Dns.Resolve(hostName)

Wrap Up

If you only need to take care of one or two name-IP mappings, manually adding them to the registry is do-able. For large number of names, Pocket Hosts is an ideal free utility for such task.

People with good eyesight might notice a subkey called "ppp_peer" in one of screen shots.

[HKEY_LOCAL_MACHINE\Comm\Tcpip\Hosts\ppp_peer]
"aliases"=hex(7): 00
"ipaddr"=hex: c0,a8,37,64

"ppp_peer" is the name the device gives to the desktop PC during ActiveSync. It maps to IP "c0,a8,37,64" ("192.168.55.100"). In the device side itself, the IP is "192.168.55.101".

Update on the Wrap Up

In Windows Mobile 5.0 devices, ActiveSync in the device gives desktop PC a new name: "dtpt_peer", which maps to IP "A9,FE,02,02" (169.254.2.2). In the device side itself, the IP is "169.254.2.1".

Looks like in Windows Mobile 5.0 emulators (and Windows Mobile 2003 devices), "ppp_peer" is still used.

A little bit on why "ppp_peer" or "dtpt_peer" is needed for ActiveSync: When a windows mobile device plus in a desktop PC, a new network interface is created in the desktop PC. This NIC is named like "Windows Mobile-based Device #n", and its IP is hard coded to be "169.254.2.2". New entries are also added to the PC's routing table, so as to route IP packets destined to 169.254.2.X to only the Window Mobile device.

You may notice when your device is cradled, a small tray icon like networking icon, with a plus sign turning left and right, trying to acquire a network address. You can also try typing "ipconfig /all" and "route print" in a command prompt to play with the this NIC.

As far as I know, you cannot explicitly bind to this specific NIC (disclaimer: Never tried myself.)

====>SiteMap of this Blog<===