• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1315
  • Last Modified:

Adding route entry fails with different gateways

I am using Windows IP helper functions to add an entry to the routing table which directs all requests to the 82.x.x.x through a VPN connection which has an interface IP 192.168.x.x. The VPN connection is started through a call to RasDial().

When I use my local network's router IP the gateway, which is also the default gateway, the CreateIpForwardEntry() successfully adds the new route to the route table. But I want to make the gateway for that connection same as the 192.168.x.x, but when I try that, the CreateIpForwardEntry() fails with error ERROR_INVALID_PARAMETER.

I tried with different metrics for the connection, but none of them work. Here's the code segment I am using.

MIB_IPFORWARDROW  routeEntry;      //      route entry data
      memset(&routeEntry, 0, sizeof(MIB_IPFORWARDROW));


      CString s;
      DWORD dwIndex=0,dwMask=0, dwVPNIP;;
      in_addr in;
      routeEntry.dwForwardIfIndex = dwIndex;
    routeEntry.dwForwardMetric1 = 20;      //      hard coded ************

    // some default values
    routeEntry.dwForwardProto =MIB_IPPROTO_LOCAL;// MIB_IPPROTO_LOCAL;
    routeEntry.dwForwardMetric2 = 1;
    routeEntry.dwForwardMetric3 = 1;
    routeEntry.dwForwardMetric4 = 1;
      DWORD dwStatus;

      if ((dwStatus=CreateIpForwardEntry(&routeEntry))!= NO_ERROR)
                  MessageBox("Invalid param");
            s.Format("Adding entry failed Status %d",dwStatus);

What might have cause this problem?

  • 4
  • 3
1 Solution
>> routeEntry.dwForwardProto =MIB_IPPROTO_LOCAL;// MIB_IPPROTO_LOCAL;

Is that correct ? it should not be MIB_IPPROTO_NETMGMT  ?? ie. 3 ??

The error usually means that one of the members of the MIB_IPFORWARDROW structure is invalid. You can try to use route.exe and execute "route add" command to add the entry. Thus, you can test whether the parameters are
correct or not.


shaf81Author Commented:
I've used routeEntry.dwForwardProto=MIB_IPPROTO_NETMGMT initially, but it failed ,too. "route add" successfully adds the route. Confused?!
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

changin values of dwForwardMetric2 ,dwForwardMetric3 ,dwForwardMetric4
to ' -1' ?

shaf81Author Commented:
Sorry Mahesh, I tried initially with those values set to -1 but failed and then only tried with 1 which also fails.
shaf81Author Commented:
Any experts out there who can help me?

On the command prompt, when I use:

Route ADD 82.x.x.x MASK 192.168.x.x  IF 0x4003 - it works

Also this too works:

Route ADD 82.x.x.x MASK IF 0x4003

But with CreateIpForwardEntry() only gateway work, and not the VPN gateway. What am I doing wrong?

Please help.
shaf81Author Commented:
Thanks experts,
I've resolved the problem myself !!!!!!  routeEntry,ForwardType must be set to 4 (The next hop is not the final destination (remote route)) to make this happen. When I memset(), this value becomes 0 which falls to none of the allowed values in the documentation.
Closed, 500 points refunded.
Community Support Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now