2 * $Id: netlink.c,v 1.1 2011/02/25 04:17:23 reubenhwk Exp $
5 * Lars Fenneberg <lf@elemental.net>
6 * Reuben Hawkins <reubenhwk@gmail.com>
8 * This software is Copyright 1996,1997 by the above mentioned author(s),
11 * The license which is distributed with this software in the file COPYRIGHT
12 * applies to this software. If your distribution is missing this file, you
13 * may request it from <pekkas@netcore.fi>.
22 #include <asm/types.h>
23 #include <sys/socket.h>
24 #include <linux/netlink.h>
25 #include <linux/rtnetlink.h>
32 void process_netlink_msg(int sock
)
36 struct iovec iov
= { buf
, sizeof(buf
) };
37 struct sockaddr_nl sa
;
38 struct msghdr msg
= { (void *)&sa
, sizeof(sa
), &iov
, 1, NULL
, 0, 0 };
40 struct ifinfomsg
* ifinfo
;
41 char ifname
[IF_NAMESIZE
] = {""};
44 len
= recvmsg (sock
, &msg
, 0);
46 flog(LOG_ERR
, "recvmsg failed: %s", strerror(errno
));
49 for (nh
= (struct nlmsghdr
*) buf
; NLMSG_OK (nh
, len
); nh
= NLMSG_NEXT (nh
, len
)) {
50 /* The end of multipart message. */
51 if (nh
->nlmsg_type
== NLMSG_DONE
)
54 if (nh
->nlmsg_type
== NLMSG_ERROR
) {
55 flog(LOG_ERR
, "%s:%d Some type of netlink error.\n", __FILE__
, __LINE__
);
59 /* Continue with parsing payload. */
60 ifinfo
= NLMSG_DATA(nh
);
61 rc
= if_indextoname(ifinfo
->ifi_index
, ifname
);
62 if (ifinfo
->ifi_flags
& IFF_RUNNING
) {
63 dlog(LOG_DEBUG
, 3, "%s, ifindex %d, flags is running", ifname
, ifinfo
->ifi_index
);
66 dlog(LOG_DEBUG
, 3, "%s, ifindex %d, flags is *NOT* running", ifname
, ifinfo
->ifi_index
);
73 int netlink_socket(void)
76 struct sockaddr_nl snl
;
78 sock
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
80 flog(LOG_ERR
, "Unable to open netlink socket: %s", strerror(errno
));
83 memset(&snl
, 0, sizeof(snl
));
84 snl
.nl_family
= AF_NETLINK
;
85 snl
.nl_groups
= RTMGRP_LINK
;
87 rc
= bind(sock
, (struct sockaddr
*)&snl
, sizeof(snl
));
89 flog(LOG_ERR
, "Unable to bind netlink socket: %s", strerror(errno
));