Ask for link and address data on the same socket.

Tilman Baumann tilman at baumann.name
Thu Jul 4 09:56:28 EDT 2013


Hi,

I have a receive function that reads RTM_NEWADDR and RTM_NEWLINK messages.

But my code for sending the request is buggered.

I must be doing the chaining of netlink messages wrong.
The RTM_GETLINK part seems to be ignored. I never receive the response
to that.

My kernel is really old, 2.6.18 plus some montavista patches. (Running
on arm)
But it probably has to do with the way I send the request.

How should it be done?

Thank you very much for your help.
I started that code a long time ago, but left the macaddress stuff
broken so far, but now I need it and I don't even remember if I was sure
the way I coded it way back made any sense. :-/



int ifaddr_nlmsg_len, ifinfo_nlmsg_len, packet_length;


struct nlmsghdr  *ifaddr_hdr = NULL;
struct nlmsghdr  *ifinfo_hdr = NULL;
struct ifaddrmsg *ifaddr_msg = NULL;
struct ifinfomsg *ifinfo_msg = NULL;

ifaddr_nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
ifinfo_nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
packet_length = ifaddr_nlmsg_len + ifinfo_nlmsg_len;


ifaddr_hdr = (struct nlmsghdr *) malloc(packet_length);
memset(ifaddr_hdr, 0, packet_length);

// Set NL message header
ifaddr_hdr->nlmsg_len   = ifaddr_nlmsg_len;
ifaddr_hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT | NLM_F_MULTI;
ifaddr_hdr->nlmsg_type  = RTM_GETADDR;

// set payload sruct
ifaddr_msg = (struct ifaddrmsg *) NLMSG_DATA(ifaddr_hdr);
//ifaddr_msg->ifa_family = AF_INET;

// Set NL message header
ifinfo_hdr = (struct nlmsghdr *) NLMSG_NEXT(ifaddr_hdr, packet_length);
ifinfo_hdr->nlmsg_len   = ifaddr_nlmsg_len;
ifinfo_hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT | NLMSG_DONE;
ifinfo_hdr->nlmsg_type  = RTM_GETLINK;

// set payload sruct
ifinfo_msg = (struct ifinfomsg *) NLMSG_DATA(ifinfo_hdr);
ifinfo_msg->ifi_family = AF_UNSPEC;

send(netlinkfd, ifaddr_hdr, packet_length, 0);


Cheers
 Tilman Baumann



More information about the libnl mailing list