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