Ask for link and address data on the same socket.

Thomas Graf tgraf at suug.ch
Thu Jul 4 15:55:31 EDT 2013


On 07/04/13 at 02:56pm, Tilman Baumann wrote:
> 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;

NLMSG_DONE is not a flag but a message type. You would need to
send it as a separate message to end the multi message stream.
You can find additional information on this in the libnl guide.

However, I suggest you send each request separately to ease
parsing of the response.



More information about the libnl mailing list