Hi,

I have been trying to write a program that uses the new netlink 
code but I haven't been able make it work. Even the example code 
from rtnetlink(3) doesn't work. 'ip' utility from iproute2 package 
works so I assume the kernel is configured properly.

Here is the slightly modified example code:

--- cut ---

#include <stdio.h> 
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <errno.h>
#include <stdlib.h>
#include <net/if.h>
#include <sys/ioctl.h>

/* This should change the MTU of eth1 to 1000.
 */

int main() 
{
     struct {
          struct nlmsghdr nh;
          struct ifinfomsg info;
          char attrbuf[512];
     } req;
     struct rtattr *rta;
     struct ifreq if_req;
     unsigned int mtu = 1000;
     int rtnetlink_socket = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
     int ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);

     memset(&if_req, 0, sizeof(if_req));
     strncpy(if_req.ifr_name, "eth1", IFNAMSIZ);
     ioctl(ioctl_socket, SIOCGIFINDEX, &if_req);
     printf("ifr_ifindex = %d.\n", if_req.ifr_ifindex);

     memset(&req, 0, sizeof(req));
     req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
     req.nh.nlmsg_flags = NLM_F_REQUEST;
     req.nh.nlmsg_type = RTM_NEWLINK;
     req.info.ifi_family = AF_UNSPEC;
     req.info.ifi_index = if_req.ifr_ifindex;
     req.info.ifi_change = 0xffffffff;
     rta = (struct rtattr*)(((char*) &req) + NLMSG_ALIGN(req.nh.nlmsg_len));
     rta->rta_type = IFLA_MTU;
     rta->rta_len = sizeof(unsigned int);
     req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len)+RTA_LENGTH(sizeof(mtu));
     memcpy(RTA_DATA(rta), &mtu, sizeof (mtu));
     send(rtnetlink_socket, &req, req.nh.nlmsg_len, 0);

     return 0;
}

--- cut ---

What puzzles me is that if I run it as non-root it doesn't give
any errors like 'ip' does.

/* Tuomas M��tt�nen              [EMAIL PROTECTED]
 * Insin��rinkatu 60 C190
 * 33720 Tampere                 Linux - the choice of a GNU generation!
 */  
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to [EMAIL PROTECTED]

Reply via email to