PC Review


Reply
Thread Tools Rate Thread

Changing Route Table

 
 
cyberco
Guest
Posts: n/a
 
      20th Nov 2006
I've posted this question in
'microsoft.public.dotnet.framework.compactframework' as well, but
despite the great help I still haven't solved the problem. So before
going the C++ route I would like to see if anybody in this group may
have the answer.

What I'm trying to do is change the route table of my Windows Mobile 5
Pocket PC (.Net CF) device (TyTN) using C#. This is probably similar as
doing it on standard CF. Using Iphlpapi.dll I can retrieve the route
table, but I am unable to change/add a route. I have specified the
following structs/pinvokes:

=========START===========
[StructLayout(LayoutKind.Sequential)]
public struct MIB_IPFORWARDROW {
public UInt32 dwForwardDest; //destination IP address.
public UInt32 dwForwardMask; //Subnet mask
public UInt32 dwForwardPolicy; //conditions for multi-path
route. Unused, specify 0.
public UInt32 dwForwardNextHop; //IP address of the next hop.
Own address?
public UInt32 dwForwardIfIndex; //index of interface
public UInt32 dwForwardType; //route type
public UInt32 dwForwardProto; //routing protocol.
public UInt32 dwForwardAge; //age of route.
public UInt32 dwForwardNextHopAS; //autonomous system number. 0
if not relevant
public int dwForwardMetric1; //-1 if not used (goes for all
metrics)
public int dwForwardMetric2;
public int dwForwardMetric3;
public int dwForwardMetric4;
public int dwForwardMetric5;
}

[StructLayout(LayoutKind.Sequential)]
public struct MIB_IPFORWARDTABLE {
public int dwNumEntries; //number of route entries in
the table.
public MIB_IPFORWARDROW[] table;
}


[DllImport("Iphlpapi.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern int CreateIpForwardEntry(ref MIB_IPFORWARDROW
pRoute);

[DllImport("Iphlpapi.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern int DeleteIpForwardEntry(ref MIB_IPFORWARDROW
pRoute);

[DllImport("Iphlpapi.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern int SetIpForwardEntry(ref MIB_IPFORWARDROW
pRoute);

[DllImport("Iphlpapi.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern int GetIpForwardTable(byte[] pIpForwardTable, out
int pdwSize, bool bOrder);

=========END===========


I invoke these methods as follows:


=========START===========
public int createIpForwardEntry(UInt32 destIPAddress, UInt32 destMask,
UInt32 nextHopIPAddress,
UInt32 ifIndex) {
MIB_IPFORWARDROW mifr = new MIB_IPFORWARDROW();
mifr.dwForwardDest = destIPAddress;
mifr.dwForwardMask = destMask;
mifr.dwForwardPolicy = Convert.ToUInt32(0);
mifr.dwForwardNextHop = nextHopIPAddress;
mifr.dwForwardIfIndex = ifIndex; //?
mifr.dwForwardType = Convert.ToUInt32(4);
mifr.dwForwardProto = Convert.ToUInt32(3);
mifr.dwForwardAge = Convert.ToUInt32(0);
mifr.dwForwardNextHopAS = Convert.ToUInt32(0);
mifr.dwForwardMetric1 = -1;
mifr.dwForwardMetric2 = -1;
mifr.dwForwardMetric3 = -1;
mifr.dwForwardMetric4 = -1;
mifr.dwForwardMetric5 = -1;
return CreateIpForwardEntry(ref mifr);
}
=========END===========

All IP addresses are added as unsigned ints (double checked whether
they are correct. I even tried adding a row that I just successfully
retrieved (thereby skipping my own structure). If that gives me an
error code 87 (invalid param).

Is there anybody here that has successfully done this or has a few
relevant pointers? Preferably C# examples, but C++ examples are a step
in the right direction. Thanks!

 
Reply With Quote
 
 
 
 
chanmm
Guest
Posts: n/a
 
      21st Nov 2006
Probably can find something here:
http://msdn2.microsoft.com/en-us/library/aa915714.aspx

chanmm

"cyberco" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I've posted this question in
> 'microsoft.public.dotnet.framework.compactframework' as well, but
> despite the great help I still haven't solved the problem. So before
> going the C++ route I would like to see if anybody in this group may
> have the answer.
>
> What I'm trying to do is change the route table of my Windows Mobile 5
> Pocket PC (.Net CF) device (TyTN) using C#. This is probably similar as
> doing it on standard CF. Using Iphlpapi.dll I can retrieve the route
> table, but I am unable to change/add a route. I have specified the
> following structs/pinvokes:
>
> =========START===========
> [StructLayout(LayoutKind.Sequential)]
> public struct MIB_IPFORWARDROW {
> public UInt32 dwForwardDest; //destination IP address.
> public UInt32 dwForwardMask; //Subnet mask
> public UInt32 dwForwardPolicy; //conditions for multi-path
> route. Unused, specify 0.
> public UInt32 dwForwardNextHop; //IP address of the next hop.
> Own address?
> public UInt32 dwForwardIfIndex; //index of interface
> public UInt32 dwForwardType; //route type
> public UInt32 dwForwardProto; //routing protocol.
> public UInt32 dwForwardAge; //age of route.
> public UInt32 dwForwardNextHopAS; //autonomous system number. 0
> if not relevant
> public int dwForwardMetric1; //-1 if not used (goes for all
> metrics)
> public int dwForwardMetric2;
> public int dwForwardMetric3;
> public int dwForwardMetric4;
> public int dwForwardMetric5;
> }
>
> [StructLayout(LayoutKind.Sequential)]
> public struct MIB_IPFORWARDTABLE {
> public int dwNumEntries; //number of route entries in
> the table.
> public MIB_IPFORWARDROW[] table;
> }
>
>
> [DllImport("Iphlpapi.dll")]
> [return: MarshalAs(UnmanagedType.U4)]
> public static extern int CreateIpForwardEntry(ref MIB_IPFORWARDROW
> pRoute);
>
> [DllImport("Iphlpapi.dll")]
> [return: MarshalAs(UnmanagedType.U4)]
> public static extern int DeleteIpForwardEntry(ref MIB_IPFORWARDROW
> pRoute);
>
> [DllImport("Iphlpapi.dll")]
> [return: MarshalAs(UnmanagedType.U4)]
> public static extern int SetIpForwardEntry(ref MIB_IPFORWARDROW
> pRoute);
>
> [DllImport("Iphlpapi.dll")]
> [return: MarshalAs(UnmanagedType.U4)]
> public static extern int GetIpForwardTable(byte[] pIpForwardTable, out
> int pdwSize, bool bOrder);
>
> =========END===========
>
>
> I invoke these methods as follows:
>
>
> =========START===========
> public int createIpForwardEntry(UInt32 destIPAddress, UInt32 destMask,
> UInt32 nextHopIPAddress,
> UInt32 ifIndex) {
> MIB_IPFORWARDROW mifr = new MIB_IPFORWARDROW();
> mifr.dwForwardDest = destIPAddress;
> mifr.dwForwardMask = destMask;
> mifr.dwForwardPolicy = Convert.ToUInt32(0);
> mifr.dwForwardNextHop = nextHopIPAddress;
> mifr.dwForwardIfIndex = ifIndex; //?
> mifr.dwForwardType = Convert.ToUInt32(4);
> mifr.dwForwardProto = Convert.ToUInt32(3);
> mifr.dwForwardAge = Convert.ToUInt32(0);
> mifr.dwForwardNextHopAS = Convert.ToUInt32(0);
> mifr.dwForwardMetric1 = -1;
> mifr.dwForwardMetric2 = -1;
> mifr.dwForwardMetric3 = -1;
> mifr.dwForwardMetric4 = -1;
> mifr.dwForwardMetric5 = -1;
> return CreateIpForwardEntry(ref mifr);
> }
> =========END===========
>
> All IP addresses are added as unsigned ints (double checked whether
> they are correct. I even tried adding a row that I just successfully
> retrieved (thereby skipping my own structure). If that gives me an
> error code 87 (invalid param).
>
> Is there anybody here that has successfully done this or has a few
> relevant pointers? Preferably C# examples, but C++ examples are a step
> in the right direction. Thanks!
>



 
Reply With Quote
 
cyberco
Guest
Posts: n/a
 
      21st Nov 2006
Those look like really useful utilities, but, forgive me for asking an
obvious question, where can I get those tools? The location at the top
of the page doesn't mean much to me...


chanmm wrote:
> Probably can find something here:
> http://msdn2.microsoft.com/en-us/library/aa915714.aspx
>
> chanmm


 
Reply With Quote
 
cyberco
Guest
Posts: n/a
 
      27th Nov 2006
I figured maybe the problem is that I should reserve some memory for
the new MIB_IPFORWARDROW I'm creating (as I do in the following code)?

> =========START===========
> public int createIpForwardEntry(UInt32 destIPAddress, UInt32 destMask,
> UInt32 nextHopIPAddress,
> UInt32 ifIndex) {
> MIB_IPFORWARDROW mifr = new MIB_IPFORWARDROW();
> mifr.dwForwardDest = destIPAddress;
> mifr.dwForwardMask = destMask;
> mifr.dwForwardPolicy = Convert.ToUInt32(0);
> mifr.dwForwardNextHop = nextHopIPAddress;
> mifr.dwForwardIfIndex = ifIndex; //?
> mifr.dwForwardType = Convert.ToUInt32(4);
> mifr.dwForwardProto = Convert.ToUInt32(3);
> mifr.dwForwardAge = Convert.ToUInt32(0);
> mifr.dwForwardNextHopAS = Convert.ToUInt32(0);
> mifr.dwForwardMetric1 = -1;
> mifr.dwForwardMetric2 = -1;
> mifr.dwForwardMetric3 = -1;
> mifr.dwForwardMetric4 = -1;
> mifr.dwForwardMetric5 = -1;
> return CreateIpForwardEntry(ref mifr);}
> =========END===========


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Route Table from C# cyberco Microsoft Dot NET Framework 2 27th Nov 2006 10:58 PM
Changing Route Table, Iphlpapi.dll and P/Invoke: How? cyberco Microsoft Dot NET Compact Framework 8 20th Nov 2006 03:51 PM
Changing the route/routing table cyberco Microsoft Dot NET Compact Framework 6 10th Nov 2006 03:16 PM
Route table changing application Jothi Microsoft VC .NET 0 13th May 2005 03:18 PM
Route table changing application Jothi Windows Networking 0 13th May 2005 03:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:16 AM.