Цитата(dronT78 @ Mar 25 2006, 10:34)

перенаправить вывод
route?
и обработать...
или еще
rtnetlinkПриветствую.
Взялся разбираться с libnetlink. Отказывается работать этот простой код (добавление нового маршрута, набрел в гугле для ознакомления):
Код
#include <stdio.h>
#include <string.h>
#include <netinet/in.h> /* inet_addr() */
#include <arpa/inet.h>
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/netlink.h> /* netlink ... */
#include <linux/rtnetlink.h>
#include <libnetlink.h>
#define BUFLEN 1024
/* this function forms the netlink packet to add a route to the kernel routing table */
int route_add(__u32* destination, __u32* gateway)
{
struct rtnl_handle rth;
/* structure of the netlink packet */
struct {
struct nlmsghdr n;
struct rtmsg r;
char buf[BUFLEN];
} req;
char mxbuf[256];
struct rtattr *mxrta = (void *)mxbuf;
memset(&req, 0, sizeof req);
/* initialisation of few parameters */
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
req.n.nlmsg_type = RTM_NEWROUTE;
req.r.rtm_family = AF_INET;
req.r.rtm_table = RT_TABLE_MAIN;
req.r.rtm_protocol = RTPROT_BOOT;
req.r.rtm_scope = RT_SCOPE_UNIVERSE;
req.r.rtm_type = RTN_UNICAST;
mxrta->rta_type = RTA_METRICS;
mxrta->rta_len = RTA_LENGTH(0);
/* RTA_DST and RTA_GW are the two esential parameters for adding a route,
* there are other parameters too which are not discussed here.
* For ipv4, the length of the address is 4 bytes
*/
addattr_l(&req.n, sizeof req, RTA_DST, destination, 4);
addattr_l(&req.n, sizeof req, RTA_GATEWAY, gateway, 4);
/* opening the netlink socket to communicate with the kernel */
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "cannot open rtnetlink\n");
return -1;
}
/* sending the packet to the kernel */
if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
return -1;
}
return 0;
}
int main(void)
{
in_addr_t ip, gw;
ip = inet_addr("192.168.11.100");
gw = inet_addr("192.168.11.1");
route_add(&ip, &gw);
return 0;
}
Все компилируется без ошибок и варнингов, но при запуске от рута получаем ошибку "RTNETLINK answers: Invalid argument". Библиотеку собирал из пакета iproute2-2.4.7-now-ss020116-try.tar.gz, взятого с
ftp://ftp.inr.ac.ru/При этом если добавлять только атрибут RTA_GATEWAY, сообщение отправляется ядру и default-маршрут выставляется. Ошибка возникает только с атрибутом RTA_DST.
Возможно ли что библиотека заточена под более ранее ядро (2.4.7), а у меня 2.4.20 и отсюда проблема?
Заранее благодарю!