Examples

Thomas Haller thaller at redhat.com
Thu Feb 19 03:43:05 PST 2015


On Wed, 2015-02-18 at 15:28 -0500, David Chappelle wrote:
> Does anyone have any examples of how I would use libnl to discover
> network interfaces and then listend for when those interfaces go
> up/own or the ip address changes. Any help would be appreciated as I
> am having trouble finding any concrete examples.


NetworkManager does that.

The code there is not really great, but you can see it all there.

http://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/src/platform/nm-linux-platform.c


the @nlh_event socket is used to receive notifications in
event_handler(). In setup() it subscribes event_handler() to listening
for data to read on the socket. See also the call to
nl_socket_add_memberships()

the @nlh socket is used entirely in a synchronous way, invoking
actions/requests and receiving the responses (synchronously).

I think you cannot make asynchronous requests via netlink -- in the
sense, that the response will be truly processed asynchronously. Of
course, you can make an request and read the response later, but that
would not save you any blocking which would be the reason for making the
requests asynchronously in the first place (I might be wrong on that --
corrections welcome).
But what could be improved in the implementation of NetworkManager is
that you don't use two different sockets for making synchronous requests
and receiving async notifications. If you do it right, you could use
just one socket for both. Doing it right, would mean, to anticipate that
async notifications might interleave with your synchronous responses and
handle them properly. That might be a bit more complicated.


good luck,
Thomas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://lists.infradead.org/pipermail/libnl/attachments/20150219/33ad5ee6/attachment.sig>


More information about the libnl mailing list