Using: Windows Mobile 5 PPC
Thanks!
In the meanwhile I came up with the following code that enables me to
retrieve the route table. Adding still fails, though (see below).
========================
[StructLayout(LayoutKind.Sequential)]
public struct MIB_IPFORWARDROW {
public UInt32 dwForwardDest; //destination IP address.
public UInt32 dwForwardMask; //Subnet mask of
destination.
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 to use
for reaching the next hop
public UInt32 dwForwardType; //route type
public UInt32 dwForwardProto; //routing protocol. Set to
PROTO_IP_NETMGMT == 3
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 GetIpForwardTable(byte[]
pIpForwardTable, out int pdwSize, bool bOrder); // Use a byte[] and
instead of a pointer, and 'out' as well.
[DllImport("Iphlpapi.dll")]
[return: MarshalAs(UnmanagedType.U4)]
public static extern int CreateIpForwardEntry(ref
MIB_IPFORWARDROW pRoute);
public void getIpForwardTable() {
int pdwSize = 20000; // Start with 20.000 bytes
byte[] buffer = new byte[pdwSize];
//dummy invoke to determine the buffer size
int ret = IPHlpApiWrapper.GetIpForwardTable(buffer, out
pdwSize, true);
// ERROR codes:
http://www.koders.com/c/fidA3495E2D0...B83A7CA56.aspx
if (ret == 0) {
buffer = new byte[pdwSize];
//have the buffer filled
ret = IPHlpApiWrapper.GetIpForwardTable(buffer, out
pdwSize, true);
if (ret != 0) {
Debug.WriteLine("IPHlpApiHelper: Error on call 2");
return;
}
} else {
Debug.WriteLine("IPHlpApiHelper: Error on call 1");
return;
}
//now we have the buffer filled, create an empty
MIB_IPFORWARDTABLE...
ipForwardTable = new MIB_IPFORWARDTABLE();
// ...and fill the MIB_IPFORWARDTABLE with the bytes from
int nOffset = 0;
// number of rows in the table
ipForwardTable.dwNumEntries =
Convert.ToInt32(buffer[nOffset]);
nOffset += 4;
ipForwardTable.table = new
MIB_IPFORWARDROW[ipForwardTable.dwNumEntries];
for (int i = 0; i < ipForwardTable.dwNumEntries; i++) {
//dwForwardDest
//((MIB_IPFORWARDROW)(ipForwardTable.table[i])).dwForwardDest =
Convert.ToUInt32(buffer[nOffset]);
((MIB_IPFORWARDROW)(ipForwardTable.table[i])).dwForwardDest =
toIPInt(buffer, nOffset);
nOffset += 4;
//dwForwardMask
((MIB_IPFORWARDROW)(ipForwardTable.table[i])).dwForwardMask =
toIPInt(buffer, nOffset);
nOffset += 4;
//dwForwardPolicy
((MIB_IPFORWARDROW)(ipForwardTable.table[i])).dwForwardPolicy =
Convert.ToUInt32(buffer[nOffset]);
nOffset += 4;
//dwForwardNextHop
((MIB_IPFORWARDROW)(ipForwardTable.table[i])).dwForwardNextHop =
toIPInt(buffer, nOffset);
nOffset += 4;
//dwForwardIfIndex
((MIB_IPFORWARDROW)(ipForwardTable.table[i])).dwForwardIfIndex =
Convert.ToUInt32(buffer[nOffset]);
nOffset += 8; //8 since we're skipping the next item
(dwForwardType)
//dwForwardProto
((MIB_IPFORWARDROW)(ipForwardTable.table[i])).dwForwardProto =
Convert.ToUInt32(buffer[nOffset]);
nOffset += 4;
}
}
private UInt32 toIPInt(byte[] bytes, int startOffset) {
uint ipInt = 0;
for (int i = 3; i >= 0; i--) {
ipInt += (uint) (bytes[startOffset + i] << (8 * i));
}
return Convert.ToUInt32(ipInt);
}
==================================
Creating a new route and adding it to the table still fails though. I
think have all the values set correctly
=================================
//Not necessary to set all:
http://forums.microsoft.com/MSDN/Sho...54793&SiteID=1
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); //next hop !=
final destination (remote route)
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 IPHlpApiWrapper.CreateIpForwardEntry(ref mifr);
}
==============================
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). Am I forgetting something with memory or so?
There's one example here that seems much simpler than what I'm doing
right now. I get the feeling it can be done a lot easier.
Thanks.