Home All Groups Group Topic Archive Search About

Changing Route Table from C#

Author
20 Nov 2006 8:57 PM
cyberco
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!

Author
21 Nov 2006 2:02 PM
Mubashir Khan
i would suggest u firstly use the get api and observe your kind of ip entry
for parameter values .... this would be helpful
Show quote
"cyberco" <cybe***@gmail.com> wrote in message
news:1164056227.258391.167630@m73g2000cwd.googlegroups.com...
> 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!
>
Author
27 Nov 2006 10:58 PM
cyberco
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)?

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

AddThis Social Bookmark Button