Wednesday, 2 May 2007

Routing IP traffic via specified adapter

On a recent project it became necessary to be able to route certain IP traffic via a specified network adapter. Although rather involved this is not as hard as it may first seem. On a desktop this would be achieved by changing the IP routing tables using the ROUTE command, but how do you do this programmatically on a handheld?

First it is necessary to find the IP address, gateway address and interface index of the adapter that you wish to route over. This information can easily be obtained by calling GetAdapterAddresses() a number of times to enumerate all network device data.

Once the IP information has been obtained for the selected adapter then CreateIpForwardEntry() can be called to setup the route. The CreateIpForwardEntry() API takes a PMIB_IPFORWARDROW parameter essentially a route definition. This needs to be setup as follows:
  • dwForwardDest - Set to the IP address of server you will be connecting too.
  • dwForwardMask - Mapping IP address mask. This will need to be for specified traffic.
  • dwForwardPolicy - Set to zero.
  • dwForwardNextHop - This needs to be set to the discovered IP adapters gateway address.
  • dwForwardIfIndex - This needs to be set to the discovered IP adapters index.
  • dwForwardType - This is set to MIB_IPROUTE_TYPE_INDIRECT.
  • dwForwardProto - This is set to MIB_IPPROTO_NETMGMT.
  • dwForwardAge - Set to zero.
  • dwForwardNextHopAS - Set to zero.
  • dwForwardMetric1 - Set to 1.
  • dwForwardMetric2-5 - Set to 0xFFFFFFFF.
It is now possible to simply use sockets to connect to the server or service that you specified in the dwForwardDest. All IP traffic over this socket will be directed via the adapter that was specified.

At the end of the session the DeleteIpForwardEntry() API should be called to tear down the route.


Unknown said...

Sorry for cross posting.. as i didnt have your mail id. saw your message in the below link.

Actually i wanted to execute the GSM algorithms using RIL or any other API's like RIL_SendSimCmd ( AT + CSIM). Is it possible ? does i have to be part of OEM. Hope you could help me out.

Anonymous said...

It is possible to use the RIL commands. Unfortunately the RIL.H and RIL.LIB files are not included in the SDK. This means that unless you own the BSP (you are the OEM) that you cannot do the link. There are several rough copies of the RIL files on the internet but I would not endorse using them.

Anonymous said...

Tried this on some other units and could not get the CreateIpForwardEntry() to work. I tracked down the problem and needed to use MIB_IPROUTE_TYPE_DIRECT instead.