8 #include <sys/socket.h>
9 #include <linux/netlink.h>
19 #define MNL_SOCKET_AUTOPID 0
20 #define MNL_SOCKET_BUFFER_SIZE (sysconf(_SC_PAGESIZE) < 8192L ? sysconf(_SC_PAGESIZE) : 8192L)
24 extern struct mnl_socket *mnl_socket_open(
int bus);
25 extern struct mnl_socket *mnl_socket_open2(
int bus,
int flags);
26 extern struct mnl_socket *mnl_socket_fdopen(
int fd);
27 extern int mnl_socket_bind(
struct mnl_socket *nl,
unsigned int groups, pid_t pid);
28 extern int mnl_socket_close(
struct mnl_socket *nl);
29 extern int mnl_socket_get_fd(
const struct mnl_socket *nl);
30 extern unsigned int mnl_socket_get_portid(
const struct mnl_socket *nl);
31 extern ssize_t mnl_socket_sendto(
const struct mnl_socket *nl,
const void *req,
size_t siz);
32 extern ssize_t mnl_socket_recvfrom(
const struct mnl_socket *nl,
void *buf,
size_t siz);
33 extern int mnl_socket_setsockopt(
const struct mnl_socket *nl,
int type,
void *buf, socklen_t len);
34 extern int mnl_socket_getsockopt(
const struct mnl_socket *nl,
int type,
void *buf, socklen_t *len);
41 #define MNL_ALIGN(len) (((len)+MNL_ALIGNTO-1) & ~(MNL_ALIGNTO-1))
42 #define MNL_NLMSG_HDRLEN MNL_ALIGN(sizeof(struct nlmsghdr))
44 extern size_t mnl_nlmsg_size(
size_t len);
45 extern size_t mnl_nlmsg_get_payload_len(
const struct nlmsghdr *nlh);
48 extern struct nlmsghdr *mnl_nlmsg_put_header(
void *buf);
49 extern void *mnl_nlmsg_put_extra_header(
struct nlmsghdr *nlh,
size_t size);
52 extern bool mnl_nlmsg_ok(
const struct nlmsghdr *nlh,
int len);
53 extern struct nlmsghdr *mnl_nlmsg_next(
const struct nlmsghdr *nlh,
int *len);
56 extern bool mnl_nlmsg_seq_ok(
const struct nlmsghdr *nlh,
unsigned int seq);
59 extern bool mnl_nlmsg_portid_ok(
const struct nlmsghdr *nlh,
unsigned int portid);
62 extern void *mnl_nlmsg_get_payload(
const struct nlmsghdr *nlh);
63 extern void *mnl_nlmsg_get_payload_offset(
const struct nlmsghdr *nlh,
size_t offset);
64 extern void *mnl_nlmsg_get_payload_tail(
const struct nlmsghdr *nlh);
67 extern void mnl_nlmsg_fprintf(FILE *fd,
const void *data,
size_t datalen,
size_t extra_header_size);
71 extern struct mnl_nlmsg_batch *mnl_nlmsg_batch_start(
void *buf,
size_t bufsiz);
83 #define MNL_ATTR_HDRLEN MNL_ALIGN(sizeof(struct nlattr))
86 extern uint16_t mnl_attr_get_type(
const struct nlattr *attr);
87 extern uint16_t mnl_attr_get_len(
const struct nlattr *attr);
88 extern uint16_t mnl_attr_get_payload_len(
const struct nlattr *attr);
89 extern void *mnl_attr_get_payload(
const struct nlattr *attr);
90 extern uint8_t mnl_attr_get_u8(
const struct nlattr *attr);
91 extern uint16_t mnl_attr_get_u16(
const struct nlattr *attr);
92 extern uint32_t mnl_attr_get_u32(
const struct nlattr *attr);
93 extern uint64_t mnl_attr_get_u64(
const struct nlattr *attr);
94 extern const char *mnl_attr_get_str(
const struct nlattr *attr);
97 extern void mnl_attr_put(
struct nlmsghdr *nlh, uint16_t type,
size_t len,
const void *data);
98 extern void mnl_attr_put_u8(
struct nlmsghdr *nlh, uint16_t type, uint8_t data);
99 extern void mnl_attr_put_u16(
struct nlmsghdr *nlh, uint16_t type, uint16_t data);
100 extern void mnl_attr_put_u32(
struct nlmsghdr *nlh, uint16_t type, uint32_t data);
101 extern void mnl_attr_put_u64(
struct nlmsghdr *nlh, uint16_t type, uint64_t data);
102 extern void mnl_attr_put_str(
struct nlmsghdr *nlh, uint16_t type,
const char *data);
103 extern void mnl_attr_put_strz(
struct nlmsghdr *nlh, uint16_t type,
const char *data);
106 extern bool mnl_attr_put_check(
struct nlmsghdr *nlh,
size_t buflen, uint16_t type,
size_t len,
const void *data);
107 extern bool mnl_attr_put_u8_check(
struct nlmsghdr *nlh,
size_t buflen, uint16_t type, uint8_t data);
108 extern bool mnl_attr_put_u16_check(
struct nlmsghdr *nlh,
size_t buflen, uint16_t type, uint16_t data);
109 extern bool mnl_attr_put_u32_check(
struct nlmsghdr *nlh,
size_t buflen, uint16_t type, uint32_t data);
110 extern bool mnl_attr_put_u64_check(
struct nlmsghdr *nlh,
size_t buflen, uint16_t type, uint64_t data);
111 extern bool mnl_attr_put_str_check(
struct nlmsghdr *nlh,
size_t buflen, uint16_t type,
const char *data);
112 extern bool mnl_attr_put_strz_check(
struct nlmsghdr *nlh,
size_t buflen, uint16_t type,
const char *data);
115 extern struct nlattr *mnl_attr_nest_start(
struct nlmsghdr *nlh, uint16_t type);
116 extern struct nlattr *mnl_attr_nest_start_check(
struct nlmsghdr *nlh,
size_t buflen, uint16_t type);
117 extern void mnl_attr_nest_end(
struct nlmsghdr *nlh,
struct nlattr *start);
118 extern void mnl_attr_nest_cancel(
struct nlmsghdr *nlh,
struct nlattr *start);
121 extern int mnl_attr_type_valid(
const struct nlattr *attr, uint16_t maxtype);
123 enum mnl_attr_data_type {
133 MNL_TYPE_NESTED_COMPAT,
139 extern int mnl_attr_validate(
const struct nlattr *attr,
enum mnl_attr_data_type type);
140 extern int mnl_attr_validate2(
const struct nlattr *attr,
enum mnl_attr_data_type type,
size_t len);
143 extern bool mnl_attr_ok(
const struct nlattr *attr,
int len);
144 extern struct nlattr *mnl_attr_next(
const struct nlattr *attr);
146 #define mnl_attr_for_each(attr, nlh, offset) \
147 for ((attr) = mnl_nlmsg_get_payload_offset((nlh), (offset)); \
148 mnl_attr_ok((attr), (char *)mnl_nlmsg_get_payload_tail(nlh) - (char *)(attr)); \
149 (attr) = mnl_attr_next(attr))
151 #define mnl_attr_for_each_nested(attr, nest) \
152 for ((attr) = mnl_attr_get_payload(nest); \
153 mnl_attr_ok((attr), (char *)mnl_attr_get_payload(nest) + mnl_attr_get_payload_len(nest) - (char *)(attr)); \
154 (attr) = mnl_attr_next(attr))
156 #define mnl_attr_for_each_payload(payload, payload_size) \
157 for ((attr) = (payload); \
158 mnl_attr_ok((attr), (char *)(payload) + payload_size - (char *)(attr)); \
159 (attr) = mnl_attr_next(attr))
162 typedef int (*mnl_attr_cb_t)(
const struct nlattr *attr,
void *data);
164 extern int mnl_attr_parse(
const struct nlmsghdr *nlh,
unsigned int offset, mnl_attr_cb_t cb,
void *data);
165 extern int mnl_attr_parse_nested(
const struct nlattr *attr, mnl_attr_cb_t cb,
void *data);
166 extern int mnl_attr_parse_payload(
const void *payload,
size_t payload_len, mnl_attr_cb_t cb,
void *data);
171 #define MNL_CB_ERROR -1
172 #define MNL_CB_STOP 0
175 typedef int (*mnl_cb_t)(
const struct nlmsghdr *nlh,
void *data);
177 extern int mnl_cb_run(
const void *buf,
size_t numbytes,
unsigned int seq,
178 unsigned int portid, mnl_cb_t cb_data,
void *data);
180 extern int mnl_cb_run2(
const void *buf,
size_t numbytes,
unsigned int seq,
181 unsigned int portid, mnl_cb_t cb_data,
void *data,
182 const mnl_cb_t *cb_ctl_array,
183 unsigned int cb_ctl_array_len);
190 #define SOL_NETLINK 270
193 #ifndef MNL_ARRAY_SIZE
194 #define MNL_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))