libnl  3.7.0
msg.h
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
4  */
5 
6 #ifndef NETLINK_MSG_H_
7 #define NETLINK_MSG_H_
8 
9 #include <netlink/netlink.h>
10 #include <netlink/object.h>
11 #include <netlink/attr.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 struct nlmsghdr;
18 
19 #define NL_DONTPAD 0
20 
21 /**
22  * @ingroup msg
23  * @brief
24  * Will cause the netlink port to be set to the port assigned to
25  * the netlink icoket ust before sending the message off.
26  *
27  * @note Requires the use of nl_send_auto()!
28  */
29 #define NL_AUTO_PORT 0
30 #define NL_AUTO_PID NL_AUTO_PORT
31 
32 /**
33  * @ingroup msg
34  * @brief
35  * May be used to refer to a sequence number which should be
36  * automatically set just before sending the message off.
37  *
38  * @note Requires the use of nl_send_auto()!
39  */
40 #define NL_AUTO_SEQ 0
41 
42 struct nl_msg;
43 struct nl_tree;
44 struct ucred;
45 
46 extern int nlmsg_size(int);
47 extern int nlmsg_total_size(int);
48 extern int nlmsg_padlen(int);
49 
50 extern void * nlmsg_data(const struct nlmsghdr *);
51 extern int nlmsg_datalen(const struct nlmsghdr *);
52 extern void * nlmsg_tail(const struct nlmsghdr *);
53 
54 /* attribute access */
55 extern struct nlattr * nlmsg_attrdata(const struct nlmsghdr *, int);
56 extern int nlmsg_attrlen(const struct nlmsghdr *, int);
57 
58 /* message parsing */
59 extern int nlmsg_valid_hdr(const struct nlmsghdr *, int);
60 extern int nlmsg_ok(const struct nlmsghdr *, int);
61 extern struct nlmsghdr * nlmsg_next(struct nlmsghdr *, int *);
62 extern int nlmsg_parse(struct nlmsghdr *, int, struct nlattr **,
63  int, const struct nla_policy *);
64 extern struct nlattr * nlmsg_find_attr(struct nlmsghdr *, int, int);
65 extern int nlmsg_validate(struct nlmsghdr *, int, int,
66  const struct nla_policy *);
67 
68 extern struct nl_msg * nlmsg_alloc(void);
69 extern struct nl_msg * nlmsg_alloc_size(size_t);
70 extern struct nl_msg * nlmsg_alloc_simple(int, int);
71 extern void nlmsg_set_default_size(size_t);
72 extern struct nl_msg * nlmsg_inherit(struct nlmsghdr *);
73 extern struct nl_msg * nlmsg_convert(struct nlmsghdr *);
74 extern void * nlmsg_reserve(struct nl_msg *, size_t, int);
75 extern int nlmsg_append(struct nl_msg *, void *, size_t, int);
76 extern int nlmsg_expand(struct nl_msg *, size_t);
77 
78 extern struct nlmsghdr * nlmsg_put(struct nl_msg *, uint32_t, uint32_t,
79  int, int, int);
80 extern struct nlmsghdr * nlmsg_hdr(struct nl_msg *);
81 extern void nlmsg_get(struct nl_msg *);
82 extern void nlmsg_free(struct nl_msg *);
83 
84 /* attribute modification */
85 extern void nlmsg_set_proto(struct nl_msg *, int);
86 extern int nlmsg_get_proto(struct nl_msg *);
87 extern size_t nlmsg_get_max_size(struct nl_msg *);
88 extern void nlmsg_set_src(struct nl_msg *, struct sockaddr_nl *);
89 extern struct sockaddr_nl *nlmsg_get_src(struct nl_msg *);
90 extern void nlmsg_set_dst(struct nl_msg *, struct sockaddr_nl *);
91 extern struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *);
92 extern void nlmsg_set_creds(struct nl_msg *, struct ucred *);
93 extern struct ucred * nlmsg_get_creds(struct nl_msg *);
94 
95 extern char * nl_nlmsgtype2str(int, char *, size_t);
96 extern int nl_str2nlmsgtype(const char *);
97 
98 extern char * nl_nlmsg_flags2str(int, char *, size_t);
99 
100 extern int nl_msg_parse(struct nl_msg *,
101  void (*cb)(struct nl_object *, void *),
102  void *);
103 
104 extern void nl_msg_dump(struct nl_msg *, FILE *);
105 
106 /**
107  * @name Iterators
108  * @{
109  */
110 
111 /**
112  * @ingroup msg
113  * Iterate over a stream of attributes in a message
114  * @arg pos loop counter, set to current attribute
115  * @arg nlh netlink message header
116  * @arg hdrlen length of family header
117  * @arg rem initialized to len, holds bytes currently remaining in stream
118  */
119 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
120  nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
121  nlmsg_attrlen(nlh, hdrlen), rem)
122 
123 /**
124  * Iterate over a stream of messages
125  * @arg pos loop counter, set to current message
126  * @arg head head of message stream
127  * @arg len length of message stream
128  */
129 #define nlmsg_for_each(pos, head, len) \
130  for (int rem = len, pos = head; \
131  nlmsg_ok(pos, rem); \
132  pos = nlmsg_next(pos, &rem))
133 
134 #define nlmsg_for_each_msg(pos, head, len, rem) \
135  nlmsg_for_each(pos, head, len)
136 
137 /** @} */
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif
int nlmsg_ok(const struct nlmsghdr *, int)
check if the netlink message fits into the remaining bytes
Definition: msg.c:174
void nl_msg_dump(struct nl_msg *, FILE *)
Dump message in human readable format to file descriptor.
Definition: msg.c:969
int nlmsg_total_size(int)
Calculates size of netlink message including padding based on payload length.
Definition: msg.c:67
struct nl_msg * nlmsg_alloc_size(size_t)
Allocate a new netlink message with maximum payload size specified.
Definition: msg.c:302
struct nlmsghdr * nlmsg_put(struct nl_msg *, uint32_t, uint32_t, int, int, int)
Add a netlink message header to a netlink message.
Definition: msg.c:503
struct nlattr * nlmsg_attrdata(const struct nlmsghdr *, int)
head of attributes data
Definition: msg.c:138
void * nlmsg_reserve(struct nl_msg *, size_t, int)
Reserve room for additional data in a netlink message.
Definition: msg.c:404
struct nl_msg * nlmsg_convert(struct nlmsghdr *)
Convert a netlink message received from a netlink socket to a nl_msg.
Definition: msg.c:379
void nlmsg_get(struct nl_msg *)
Acquire a reference on a netlink message.
Definition: msg.c:545
struct nl_msg * nlmsg_alloc_simple(int, int)
Allocate a new netlink message.
Definition: msg.c:341
int nlmsg_validate(struct nlmsghdr *, int, int, const struct nla_policy *)
nlmsg_validate - validate a netlink message including attributes
Definition: msg.c:239
void nlmsg_free(struct nl_msg *)
Release a reference from an netlink message.
Definition: msg.c:558
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
Definition: msg.c:208
int nlmsg_expand(struct nl_msg *, size_t)
Expand maximum payload size of a netlink message.
Definition: msg.c:470
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition: msg.c:294
struct nlmsghdr * nlmsg_hdr(struct nl_msg *)
Return actual netlink message.
Definition: msg.c:536
int nlmsg_datalen(const struct nlmsghdr *)
Return length of message payload.
Definition: msg.c:116
struct nl_msg * nlmsg_inherit(struct nlmsghdr *)
Allocate a new netlink message and inherit netlink message header.
Definition: msg.c:317
int nlmsg_attrlen(const struct nlmsghdr *, int)
length of attributes data
Definition: msg.c:149
void nlmsg_set_default_size(size_t)
Set the default maximum message payload size for allocated messages.
Definition: msg.c:362
int nlmsg_append(struct nl_msg *, void *, size_t, int)
Append data to tail of a netlink message.
Definition: msg.c:442
int nlmsg_padlen(int)
Size of padding that needs to be added at end of message.
Definition: msg.c:82
void * nlmsg_data(const struct nlmsghdr *)
Return pointer to message payload.
Definition: msg.c:100
int nlmsg_size(int)
Calculates size of netlink message based on payload length.
Definition: msg.c:49
struct nlmsghdr * nlmsg_next(struct nlmsghdr *, int *)
next netlink message in message stream
Definition: msg.c:189
struct nlattr * nlmsg_find_attr(struct nlmsghdr *, int, int)
nlmsg_find_attr - find a specific attribute in a netlink message
Definition: msg.c:226
Attribute validation policy.
Definition: attr.h:63