Default Gateway
gregory.menke at gsfc.nasa.gov
gregory.menke at gsfc.nasa.gov
Mon May 9 22:56:33 UTC 2005
Mike Bertosh writes:
> Hi all,
>
> I am trying to create a function that I can call to change the default gateway address. I
> am using the rtrequest() function to remove the default gateway from the routing table
> and then add the new address.Is there a function that will already do this for me?
> Here is the code I have been trying to get to work.
>
The code snippet below sets a gateway for 128.183/16, accessed by
either of two ethernet interfaces. The ethernet addresses themselves
are 192.168.100.102 and 200.102. So its not a default gateway, though
I imagine the calls could be generalized for that. It looks similar
to what you're doing. Why do you need the rtfree()?
Gregm
#define DOWNLINK_ADDRESS "128.183.0.0"
#define DOWNLINK_NETMASK "255.255.0.0"
#define DOWNLINK1_GATEWAY "192.168.100.1"
#define DOWNLINK2_GATEWAY "192.168.200.1"
void setup_downlink_routes()
{
struct sockaddr_in address;
struct sockaddr_in netmask;
struct sockaddr_in gateway;
memset(&address,0,sizeof(address));
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr( DOWNLINK_ADDRESS );
memset(&netmask,0,sizeof(netmask));
netmask.sin_len = sizeof(netmask);
netmask.sin_family = AF_INET;
netmask.sin_addr.s_addr = inet_addr( DOWNLINK_NETMASK );
memset(&gateway,0,sizeof(gateway));
gateway.sin_len = sizeof(gateway);
gateway.sin_family = AF_INET;
gateway.sin_addr.s_addr = inet_addr( DOWNLINK1_GATEWAY );
if (rtems_bsdnet_rtrequest( RTM_ADD,
(struct sockaddr *)&address,
(struct sockaddr *)&gateway,
(struct sockaddr *)&netmask,
(RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL) < 0)
{
printf("Can't create gateway for route %s/%s: %s\n",
DOWNLINK_ADDRESS,
DOWNLINK_NETMASK,
strerror(errno) );
}
}
int select_downlink_route(int selroute)
{
char *offgateway, *ongateway;
struct sockaddr_in address;
struct sockaddr_in netmask;
struct sockaddr_in gateway;
if( selroute == 0 )
{
offgateway = DOWNLINK2_GATEWAY;
ongateway = DOWNLINK1_GATEWAY;
}
else if( selroute == 1 )
{
offgateway = DOWNLINK1_GATEWAY;
ongateway = DOWNLINK2_GATEWAY;
}
else
return -1;
memset(&address,0,sizeof(address));
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr( DOWNLINK_ADDRESS );
memset(&netmask,0,sizeof(netmask));
netmask.sin_len = sizeof(netmask);
netmask.sin_family = AF_INET;
netmask.sin_addr.s_addr = inet_addr( DOWNLINK_NETMASK );
memset(&gateway,0,sizeof(gateway));
gateway.sin_len = sizeof(gateway);
gateway.sin_family = AF_INET;
gateway.sin_addr.s_addr = inet_addr( offgateway );
rtems_bsdnet_rtrequest( RTM_DELETE,
(struct sockaddr *)&address,
(struct sockaddr *)&gateway,
(struct sockaddr *)&netmask,
(RTF_GATEWAY | RTF_STATIC), NULL);
gateway.sin_addr.s_addr = inet_addr( ongateway );
rtems_bsdnet_rtrequest( RTM_ADD,
(struct sockaddr *)&address,
(struct sockaddr *)&gateway,
(struct sockaddr *)&netmask,
(RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
return 0;
}
Mike Bertosh writes:
> Hi all,
>
> I am trying to create a function that I can call to change the default gateway address. I
> am using the rtrequest() function to remove the default gateway from the routing table
> and then add the new address.Is there a function that will already do this for me?
> Here is the code I have been trying to get to work.
>
More information about the users
mailing list