LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2010, 07:27 AM   #1
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Rep: Reputation: 18
Problem to set gateway using c++ program


Hi,

I'm trying to set the gateway with my c++ program. My function receive the gateway on the first parameter.

The return of execution of ioctl() is 0, so it's working good, but when I run the "route" command on console, the configured gateway isn't there.

Check the output and the function below:


$ route
Quote:
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 * 255.0.0.0 U 0 0 0 eth0
Code:
int gateWayConfig(string gateWay) {
    int result = ERROR; //retorno da função
    try {
        int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        gateWay = formatIp(gateWay); //converts 192.168.000.001 to 192.168.0.1

        if (sockfd > 0) {
            struct sockaddr_in *dst, *gw, *mask;
            struct rtentry route;

            printf("gateway: %s\n", gateWay.c_str());
            printf("gateway: %d\n", inet_addr(gateWay.c_str()));

            memset(&route, 0, sizeof (struct rtentry));

            dst = (struct sockaddr_in *) (&(route.rt_dst));
            gw = (struct sockaddr_in *) (&(route.rt_gateway));
            mask = (struct sockaddr_in *) (&(route.rt_genmask));

            // Make sure we're talking about IP here
            dst->sin_family = AF_INET;
            gw->sin_family = AF_INET;
            mask->sin_family = AF_INET;

            // Set up the data for removing the default route
            dst->sin_addr.s_addr = 0;
            gw->sin_addr.s_addr = 0;
            mask->sin_addr.s_addr = 0;
            route.rt_flags = RTF_UP | RTF_GATEWAY;

            // Remove the default route
            ioctl(sockfd, SIOCDELRT, &route);

            // Set up the data for adding the default route
            dst->sin_addr.s_addr = 0;
            gw->sin_addr.s_addr = inet_addr(gateWay.c_str());
            mask->sin_addr.s_addr = 0;
            route.rt_metric = 1;
            route.rt_flags = RTF_UP | RTF_GATEWAY;

            // Remove this route if it already exists
            ioctl(sockfd, SIOCDELRT, &route);

            // Add the default route
            if (ioctl(sockfd, SIOCADDRT, &route) == -1) {
                // *** The error never occurs ***
                fprintf(stderr, "Adding default route: %d", errno);
            }
            shutdown(sockfd, SHUT_RDWR);
            close(sockfd);
        }
        printf("result: %d\n", result); 
        
    } catch (...) {

    }
    return result;
}

Could someone help me to solve this issue?

I tried to use system() function to set the gateway using:
system("route del default");
system("route add default gw 10.100.40.1 dev eth0");

But didn't work too. :/


Thanks in advance

Last edited by Lobinho; 11-26-2010 at 07:34 AM.
 
Old 11-26-2010, 08:55 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Did you run as root ?
 
Old 11-26-2010, 09:14 AM   #3
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
Hi Sergei,

Yes, I was running the program as root. I discovered right now the problem: I was setting interface down to set mac address and the gateway config was going to hell with the "ifconfig eth0 down" command. :P

sorry, my fault. Thanks anyway.
 
Old 11-26-2010, 09:14 AM   #4
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
Hi Sergei,

Yes, I was running the program as root. I discovered right now the problem: I was setting interface down to set mac address and the gateway config was going to hell with the "ifconfig eth0 down" command. :P

sorry, my fault. Thanks anyway.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
lm10.0 gateway is set but when I reboot I have to set the gateway rharvey32 Mandriva 8 02-13-2006 01:35 PM
Gateway set up xowl Slackware 1 02-04-2005 10:20 PM
Odd problem: Gateway unreachable after certain amount of time (Win XP Gateway) SocialEngineer Linux - Networking 2 08-13-2004 12:54 AM
How can I set Gateway? gubak Linux - Networking 29 07-20-2004 04:43 AM
trying to set up a gateway annehoog Linux - Newbie 6 05-12-2003 09:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration