This typical "route add default gw IP" program gives me error :
SIOCADDRT failed: Inappropriate ioctl for device
I did ifconfig to verify the interface name. Is there a way to list all the approprieate ioctl for my device ?
int main(char** args) {
int sockfd;
struct rtentry route;
struct sockaddr_in *addr;
int err = 0;
// create the socket
if(sockfd = socket(AF_INET, SOCK_DGRAM, 0)<0){
perror("socket");
exit(1);
}
memset(&route, 0, sizeof(route));
addr = (struct sockaddr_in*) &route.rt_gateway;
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr("192.168.10.1");
addr = (struct sockaddr_in*) &route.rt_dst;
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = INADDR_ANY;
addr = (struct sockaddr_in*) &route.rt_genmask;
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = INADDR_ANY;
route.rt_dev = "eth0";
route.rt_flags = RTF_UP | RTF_GATEWAY;
route.rt_metric = 0;
if ((err = ioctl(sockfd, SIOCADDRT, &route)) != 0) {
perror("SIOCADDRT failed");
exit(1);
}
}
New error after changing the code to:
addr = (struct sockaddr_in*) &route.rt_gateway;
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr("192.168.10.1");
addr = (struct sockaddr_in*) &route.rt_dst;
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr("192.168.10.3");
addr = (struct sockaddr_in*) &route.rt_genmask;
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr("255.255.255.0");
gdb output :
37 if ((err = ioctl(sockfd, SIOCADDRT, &route)) != 0) {
(gdb) p route
$1 = {rt_pad1 = 0, rt_dst = {sa_family = 2, sa_data = "\000\000\300\250\n\003\000\000\000\000\000\000\000"},
rt_gateway = {sa_family = 2, sa_data = "\000\000\300\250\n\001\000\000\000\000\000\000\000"},
rt_genmask = {sa_family = 2, sa_data = "\000\000\377\377\377\000\000\000\000\000\000\000\000"},
rt_flags = 3, rt_pad2 = 0, rt_pad3 = 0, rt_tos = 0 '\000', rt_class = 0 '\000', rt_pad4 = {0, 0, 0},
rt_metric = 0, rt_dev = 0x4008af "em1", rt_mtu = 0, rt_window = 0, rt_irtt = 0}
(gdb) n
38 perror("SIOCADDRT failed");
(gdb)
SIOCADDRT failed: Invalid argument
I guess the mistake is here:
// create the socket
if(sockfd = socket(AF_INET, SOCK_DGRAM, 0)<0)
The expression socket(AF_INET, SOCK_DGRAM, 0)<0
is evaluated before assignment. That happens because assignment operators have lower precedence than function calls and comparisons. You can read more about it in this wikipedia article:
So what you really have is:
// create the socket
if(sockfd = (socket(AF_INET, SOCK_DGRAM, 0)<0))
That evaluates to:
// create the socket
if(sockfd = 0)
So you are trying to perform an ioctl()
on the invalid file descriptor.
Wrap the assignment into parenthesis or move the expression out of the condition, like this:
// create the socket
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
By the way, GCC or Clang should detect it if you pass -Wall
option.