10 #include <libmnl/libmnl.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
71 struct sockaddr_nl addr;
85 EXPORT_SYMBOL(mnl_socket_get_fd);
86 int mnl_socket_get_fd(
const struct mnl_socket *nl)
100 EXPORT_SYMBOL(mnl_socket_get_portid);
101 unsigned int mnl_socket_get_portid(
const struct mnl_socket *nl)
103 return nl->addr.nl_pid;
106 static struct mnl_socket *__mnl_socket_open(
int bus,
int flags)
114 nl->fd = socket(AF_NETLINK, SOCK_RAW | flags, bus);
130 EXPORT_SYMBOL(mnl_socket_open);
133 return __mnl_socket_open(bus, 0);
148 EXPORT_SYMBOL(mnl_socket_open2);
149 struct mnl_socket *mnl_socket_open2(
int bus,
int flags)
151 return __mnl_socket_open(bus, flags);
165 EXPORT_SYMBOL(mnl_socket_fdopen);
170 struct sockaddr_nl addr;
171 socklen_t addr_len =
sizeof(
struct sockaddr_nl);
173 ret = getsockname(fd, (
struct sockaddr *) &addr, &addr_len);
182 if (addr.nl_family == AF_NETLINK)
198 EXPORT_SYMBOL(mnl_socket_bind);
199 int mnl_socket_bind(
struct mnl_socket *nl,
unsigned int groups, pid_t pid)
204 nl->addr.nl_family = AF_NETLINK;
205 nl->addr.nl_groups = groups;
206 nl->addr.nl_pid = pid;
208 ret = bind(nl->fd, (
struct sockaddr *) &nl->addr, sizeof (nl->addr));
212 addr_len =
sizeof(nl->addr);
213 ret = getsockname(nl->fd, (
struct sockaddr *) &nl->addr, &addr_len);
217 if (addr_len !=
sizeof(nl->addr)) {
221 if (nl->addr.nl_family != AF_NETLINK) {
237 EXPORT_SYMBOL(mnl_socket_sendto);
238 ssize_t mnl_socket_sendto(
const struct mnl_socket *nl,
const void *buf,
241 static const struct sockaddr_nl snl = {
242 .nl_family = AF_NETLINK
244 return sendto(nl->fd, buf, len, 0,
245 (
struct sockaddr *) &snl,
sizeof(snl));
262 EXPORT_SYMBOL(mnl_socket_recvfrom);
263 ssize_t mnl_socket_recvfrom(
const struct mnl_socket *nl,
void *buf,
267 struct sockaddr_nl addr;
272 struct msghdr msg = {
274 .msg_namelen =
sizeof(
struct sockaddr_nl),
281 ret = recvmsg(nl->fd, &msg, 0);
285 if (msg.msg_flags & MSG_TRUNC) {
289 if (msg.msg_namelen !=
sizeof(
struct sockaddr_nl)) {
303 EXPORT_SYMBOL(mnl_socket_close);
306 int ret = close(nl->fd);
336 EXPORT_SYMBOL(mnl_socket_setsockopt);
337 int mnl_socket_setsockopt(
const struct mnl_socket *nl,
int type,
338 void *buf, socklen_t len)
340 return setsockopt(nl->fd, SOL_NETLINK, type, buf, len);
352 EXPORT_SYMBOL(mnl_socket_getsockopt);
353 int mnl_socket_getsockopt(
const struct mnl_socket *nl,
int type,
354 void *buf, socklen_t *len)
356 return getsockopt(nl->fd, SOL_NETLINK, type, buf, len);