LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 12-20-2006, 05:40 AM   #1
sweta
LQ Newbie
 
Registered: Dec 2006
Posts: 1

Rep: Reputation: 0
How to add a gateway address using ioctl in C in linux ?


Hi,
I want to add a default gateway address using ioctl instead of system call.

In c, using system call in main(), it works fine.
but I want to use ioctl SIOCADDRT to add gateway.

So I used the following code.

if ((r = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
return EXIT_FAILURE;
}
route_gateway = (struct sockaddr_in *)&rtentry.rt_gateway;
route_gateway->sin_family = AF_INET;
route_gateway->sin_addr.s_addr = inet_addr("192.168.6.1");

if (ioctl(r, SIOCADDRT, &rtentry) == -1) {
perror("ioctl");
return ;
}

It gives me following error from ioctl :
Address family is not supported by this protocol.
Can anyone help me out to figure out what I m missing ?
Thanks in advance.

Last edited by sweta; 12-20-2006 at 05:47 AM.
 
Old 12-27-2006, 11:26 PM   #2
kotao_78
LQ Newbie
 
Registered: Sep 2006
Posts: 19

Rep: Reputation: 0
Try below codes,

int setDefGateway(const char * deviceName,const char * defGateway)
{
int sockfd;
struct rtentry rm;
struct sockaddr_in ic_gateway ;// Gateway IP address
int err;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1)
{
printf("socket is -1\n");
return -1;
}

memset(&rm, 0, sizeof(rm));


ic_gateway.sin_family = AF_INET;
ic_gateway.sin_addr.s_addr = inet_addr(defGateway);
ic_gateway.sin_port = 0;

(( struct sockaddr_in*)&rm.rt_dst)->sin_family = AF_INET;
(( struct sockaddr_in*)&rm.rt_dst)->sin_addr.s_addr = 0;
(( struct sockaddr_in*)&rm.rt_dst)->sin_port = 0;

(( struct sockaddr_in*)&rm.rt_genmask)->sin_family = AF_INET;
(( struct sockaddr_in*)&rm.rt_genmask)->sin_addr.s_addr = 0;
(( struct sockaddr_in*)&rm.rt_genmask)->sin_port = 0;

memcpy((void *) &rm.rt_gateway, &ic_gateway, sizeof(ic_gateway));
rm.rt_flags = RTF_UP | RTF_GATEWAY;
if ((err = ioctl(sockfd, SIOCADDRT, &rm)) < 0)
{
printf("SIOCADDRT failed , ret->%d\n",err);
return -1;
}
return 0;
}
 
  


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
HOw to get Broad cast address using ioctl( ) and print it... touqeer.ansar Programming 1 07-03-2006 11:35 AM
ioctl[SIOCGIFADDR]: Cannot assign requested address mmcgann Linux - Wireless Networking 1 06-20-2006 12:09 PM
How can I add a second gateway to my linux router? abefroman Linux - Networking 1 11-20-2005 11:45 AM
how to get gateway address in C under linux alqama Programming 2 04-13-2005 05:42 AM
How to specify a gateway IP address for a linux client pjrestrepo Linux - Networking 3 11-30-2001 10:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:32 AM.

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