10#include <libmnl/libmnl.h>
12#include <sys/socket.h>
71 struct sockaddr_nl addr;
101 return nl->addr.nl_pid;
104static struct mnl_socket *__mnl_socket_open(
int bus,
int flags)
106 struct mnl_socket *nl;
108 nl = calloc(1,
sizeof(
struct mnl_socket));
112 nl->fd = socket(AF_NETLINK, SOCK_RAW | flags, bus);
130 return __mnl_socket_open(bus, 0);
147 return __mnl_socket_open(bus, flags);
164 struct mnl_socket *nl;
165 struct sockaddr_nl addr;
166 socklen_t addr_len =
sizeof(
struct sockaddr_nl);
168 ret = getsockname(fd, (
struct sockaddr *) &addr, &addr_len);
172 nl = calloc(1,
sizeof(
struct mnl_socket));
177 if (addr.nl_family == AF_NETLINK)
199 nl->addr.nl_family = AF_NETLINK;
200 nl->addr.nl_groups = groups;
201 nl->addr.nl_pid = pid;
203 ret = bind(nl->fd, (
struct sockaddr *) &nl->addr, sizeof (nl->addr));
207 addr_len =
sizeof(nl->addr);
208 ret = getsockname(nl->fd, (
struct sockaddr *) &nl->addr, &addr_len);
212 if (addr_len !=
sizeof(nl->addr)) {
216 if (nl->addr.nl_family != AF_NETLINK) {
233 const void *buf,
size_t len)
235 static const struct sockaddr_nl snl = {
236 .nl_family = AF_NETLINK
238 return sendto(nl->fd, buf, len, 0,
239 (
struct sockaddr *) &snl,
sizeof(snl));
257 void *buf,
size_t bufsiz)
260 struct sockaddr_nl addr;
265 struct msghdr msg = {
267 .msg_namelen =
sizeof(
struct sockaddr_nl),
274 ret = recvmsg(nl->fd, &msg, 0);
278 if (msg.msg_flags & MSG_TRUNC) {
282 if (msg.msg_namelen !=
sizeof(
struct sockaddr_nl)) {
298 int ret = close(nl->fd);
329 void *buf, socklen_t len)
331 return setsockopt(nl->fd, SOL_NETLINK, type, buf, len);
344 void *buf, socklen_t *len)
346 return getsockopt(nl->fd, SOL_NETLINK, type, buf, len);
int mnl_socket_getsockopt(const struct mnl_socket *nl, int type, void *buf, socklen_t *len)
int mnl_socket_get_fd(const struct mnl_socket *nl)
int mnl_socket_close(struct mnl_socket *nl)
int mnl_socket_setsockopt(const struct mnl_socket *nl, int type, void *buf, socklen_t len)
unsigned int mnl_socket_get_portid(const struct mnl_socket *nl)
struct mnl_socket * mnl_socket_open(int bus)
struct mnl_socket * mnl_socket_open2(int bus, int flags)
ssize_t mnl_socket_recvfrom(const struct mnl_socket *nl, void *buf, size_t bufsiz)
int mnl_socket_bind(struct mnl_socket *nl, unsigned int groups, pid_t pid)
ssize_t mnl_socket_sendto(const struct mnl_socket *nl, const void *buf, size_t len)
struct mnl_socket * mnl_socket_fdopen(int fd)