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<===




[ [permalink] ]

14 Comments:

At October 21, 2006 1:46 PM, Anonymous Anonymous said...

This post addressed my question exactly. Thanks! If you have the time, I wondered if you can answer one more: I'm attempting to reroute an http request from a windows mobile app to my own page and respond. Is it possible to route the request back to the device (in other words, never leaving the device) and respond from another windows mobile app? Preferably without running a web server?
Thanks again...

 
At July 29, 2007 10:08 AM, Anonymous Anonymous said...

I must say you have done good jobs. Your blog is very intresting

 
At July 30, 2007 10:59 PM, Anonymous Anonymous said...

just wat i was looking for!! :) thanks...

 
At October 11, 2007 11:49 AM, Anonymous Anonymous said...

Thanks, this helped me out, too. If you're trying to put this into an .inf file from which you're creating a .cab file, the registry settings section would look something like this:

[RegSettings]
HKLM,Comm\Tcpip\Hosts\hello,"ipaddr",0x00000001,a1,a3,a5,a9
HKLM,Comm\Tcpip\Hosts\hello,"ExpireTime",0x00010000,"99,99,99,99,99,99,99"

 
At November 20, 2007 2:35 AM, Anonymous Anonymous said...

Danke schön für diesen Artikel!

mfg
Dominik

 
At January 21, 2008 3:30 AM, Blogger NozFx said...

Exactly what I needed. I've been able to dynamically add the hosts at runtime. It appears to work straight away on my device. Would this apply to all devices? If not would I need to perform a device reset once the entry was written to the registry?

Many Thanks

Richard

 
At April 16, 2008 2:58 PM, Anonymous Anonymous said...

Can this be done on a Windows Mobile 5.0 device? Or 6.0?

What program can we sue to edit the registry (reg edit) on a WM 5 or 6 device.

Any help is greatly appreciated.

Thanks,
Mark

 
At May 07, 2008 12:38 PM, Blogger MILF Hunter said...

You can use this registry editor:

http://www.breaksoft.com/Blog/Utilities/2005/1/Mobile_Registry_Editor.aspx

 
At June 21, 2008 12:48 PM, Blogger techsta said...

Great post ... thanks a million mate.

techsta

 
At July 16, 2008 1:31 AM, Anonymous Anonymous said...

hey i have been trying to edit the registry by using the software i got from http://www.breaksoft.com/Blog/Utilities/2005/1/Mobile_Registry_Editor.aspx to no avail. When ever i try to create a new registry key there will be a pop up indicating Error creating key telling me that Access is denied. I was wondering if anything could be done to solve this issue? thanks

 
At July 16, 2008 2:02 AM, Anonymous Anonymous said...

hi i have edited the registry key as mentioned but i am still unable to access the web. Is there anyway else to solve this problem?

 
At November 10, 2008 1:26 PM, Blogger Dr.Luiji said...

Thanks a ton, It's just wat i was looking for.

 
At March 14, 2009 1:58 AM, Blogger Computer Science said...

thanks for your effort and actually it is a Nice Topic


I hope you take a look to my blog
http://ebook44free.blogspot.com/
CCNA MCSE CCNP Oracle C# C++ Graphics
Programming Databse Engineering Netwrok Dseign Mbile

It is about FREE eBooks

Thanks..,

 
At May 03, 2009 2:30 AM, Blogger Mohammad Hawash said...

Thanks

please see my blog www.visualcsamples.blogspot.com

 

Post a Comment

<< Home