Networking Forums

Networking Forums > Computer Networking > Linux Networking > rtnetlink, setting gateway for eth1 fails

Reply
Thread Tools Display Modes

rtnetlink, setting gateway for eth1 fails

 
 
Scott Derrick
Guest
Posts: n/a

 
      07-28-2004, 04:40 PM
I'm trying to set the gateway on a multiNIC machine. The code below works
fine for eth0 but does nothing for eth1 or eth2

the parameter gate == IP in dotted notatio, the parameter device == "eth0",
"eth1" or "eth2"

I'm using the decimal equivelant of eth"0" for the RTA_OIF, so eth0 would
be decimal 0, eth1 woudl be decimal 1, etc..

what am I doing wrong that would allow this to work fine for eth0 but not
the other NIC's. I can use the route program and add or delete gateways to
al theNIC's.

thanks, Scott

*****************code follows*******************

const char *envLinux :: ip_gateway ( const char *gate, const char *device )
{
////////////////////////////////////////////////////////////////////////
//
// OVERLOAD IConfigIP
//
// PURPOSE
// - Sets/retreives the current IP gateway address.
//
// PARAMETERS
// - addr specifies the new gateway address (set to NULL to retrieve)
//
// RETURN VALUE
// Ptr. to current gateway address
//
////////////////////////////////////////////////////////////////////////
int inet_sock = -1, ret;
IException *ex = NULL;
char GATEWAY[9];
struct in_addr tempAddr;
int ind = 0;

struct{
struct nlmsghdr netlink_header;
struct rtmsg rt_message;
char buffer[1024];
}request;

try
{
ind = atoi(device + 3);

if(!strcmp(gate, "0.0.0.0") == 0) // add a gateway
{
inet_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (inet_sock == -1)
{
printf( "ip_gateway : Unable to create a socket" );
fflush(stdout);
return 0;
}

memset(&request, 0, sizeof(request));
request.netlink_header.nlmsg_len =NLMSG_LENGTH(sizeof(struct rtmsg));
request.netlink_header.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
request.netlink_header.nlmsg_type = RTM_NEWROUTE;
request.rt_message.rtm_family = AF_INET;
request.rt_message.rtm_table = RT_TABLE_MAIN;

request.rt_message.rtm_protocol = RTPROT_BOOT;
request.rt_message.rtm_scope = RT_SCOPE_UNIVERSE;
request.rt_message.rtm_type = RTN_UNICAST;

if(inet_aton(gate, &tempAddr))
addattr_l(&request.netlink_header, sizeof(request),
RTA_GATEWAY, (void*)&(tempAddr.s_addr), 4);

if(inet_aton("0.0.0.0", &tempAddr))
addattr_l(&request.netlink_header, sizeof(request),
RTA_DST, (void*)&(tempAddr.s_addr), 4);

addattr32(&request.netlink_header, sizeof(request), RTA_OIF, ind);

struct sockaddr_nl nladdr;
struct iovec iov ={(void*)&request,request.netlink_header.nlmsg_len };
struct msghdr msg = {
(void*)&nladdr,
sizeof(nladdr),
&iov,
1,
NULL,
0,
0};
qq
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
nladdr.nl_pid = 0;
nladdr.nl_groups = 0;

ret = sendmsg(inet_sock, &msg, 0);

if (ret < 1)
{
printf("ip_gateway: send returned %d error %d\n", ret, errno);
fflush(stdout);
}
} // if
} // try
catch ( IException *_ex )
{
ex = _ex;
} // catch

// Clean up
if (inet_sock != -1) close ( inet_sock );
if (ex != NULL) throw ex;
else
printf ( "%s: Gateway : %s on %s\n", name, tempstr, device );

return tempstr;
} // ip_gateway



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting master mode fails Marco Linux Networking 1 10-29-2010 12:13 AM
Setting up Net Gateway in Linux? x0054 Linux Networking 3 09-10-2006 08:11 PM
Setting up a simple gateway Captain Beefheart Linux Networking 2 07-22-2004 08:31 PM
Setting up Gateway Dario Kampkaspar Linux Networking 6 02-02-2004 04:29 PM
Gateway Fails Arun Sudhakaran Home Networking 4 10-22-2003 05:43 PM



1 2 3 4 5 6 7 8 9 10 11