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 #define NL_DONTPAD	0
24 
25 /**
26  * @ingroup msg
27  * @brief
28  * Will cause the netlink pid to be set to the pid assigned to
29  * the netlink handle (socket) just before sending the message off.
30  * @note Requires the use of nl_send_auto_complete()!
31  */
32 #define NL_AUTO_PID	0
33 
34 /**
35  * @ingroup msg
36  * @brief
37  * May be used to refer to a sequence number which should be
38  * automatically set just before sending the message off.
39  * @note Requires the use of nl_send_auto_complete()!
40  */
41 #define NL_AUTO_SEQ	0
42 
43 struct nl_msg;
44 struct nl_tree;
45 struct ucred;
46 
47 /* size calculations */
48 extern int		  nlmsg_msg_size(int);
49 extern int		  nlmsg_total_size(int);
50 extern int		  nlmsg_padlen(int);
51 
52 /* payload access */
53 extern void *		  nlmsg_data(const struct nlmsghdr *);
54 extern int		  nlmsg_len(const struct nlmsghdr *);
55 extern void *		  nlmsg_tail(const struct nlmsghdr *);
56 
57 /* attribute access */
58 extern struct nlattr *	  nlmsg_attrdata(const struct nlmsghdr *, int);
59 extern int		  nlmsg_attrlen(const struct nlmsghdr *, int);
60 
61 /* message parsing */
62 extern int		  nlmsg_valid_hdr(const struct nlmsghdr *, int);
63 extern int		  nlmsg_ok(const struct nlmsghdr *, int);
64 extern struct nlmsghdr *  nlmsg_next(struct nlmsghdr *, int *);
65 extern int		  nlmsg_parse(struct nlmsghdr *, int, struct nlattr **,
66 				      int, struct nla_policy *);
67 extern struct nlattr *	  nlmsg_find_attr(struct nlmsghdr *, int, int);
68 extern int		  nlmsg_validate(struct nlmsghdr *, int, int,
69 					 struct nla_policy *);
70 
71 extern struct nl_msg *	  nlmsg_alloc(void);
72 extern struct nl_msg *	  nlmsg_alloc_size(size_t);
73 extern struct nl_msg *	  nlmsg_alloc_simple(int, int);
74 extern void		  nlmsg_set_default_size(size_t);
75 extern struct nl_msg *	  nlmsg_inherit(struct nlmsghdr *);
76 extern struct nl_msg *	  nlmsg_convert(struct nlmsghdr *);
77 extern void *		  nlmsg_reserve(struct nl_msg *, size_t, int);
78 extern int		  nlmsg_append(struct nl_msg *, void *, size_t, int);
79 extern int		  nlmsg_expand(struct nl_msg *, size_t);
80 
81 extern struct nlmsghdr *  nlmsg_put(struct nl_msg *, uint32_t, uint32_t,
82 				    int, int, int);
83 extern struct nlmsghdr *  nlmsg_hdr(struct nl_msg *);
84 extern void		  nlmsg_get(struct nl_msg *);
85 extern void		  nlmsg_free(struct nl_msg *);
86 
87 /* attribute modification */
88 extern void		  nlmsg_set_proto(struct nl_msg *, int);
89 extern int		  nlmsg_get_proto(struct nl_msg *);
90 extern size_t		  nlmsg_get_max_size(struct nl_msg *);
91 extern void		  nlmsg_set_src(struct nl_msg *, struct sockaddr_nl *);
92 extern struct sockaddr_nl *nlmsg_get_src(struct nl_msg *);
93 extern void		  nlmsg_set_dst(struct nl_msg *, struct sockaddr_nl *);
94 extern struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *);
95 extern void		  nlmsg_set_creds(struct nl_msg *, struct ucred *);
96 extern struct ucred *	  nlmsg_get_creds(struct nl_msg *);
97 
98 extern char *		  nl_nlmsgtype2str(int, char *, size_t);
99 extern int		  nl_str2nlmsgtype(const char *);
100 
101 extern char *		  nl_nlmsg_flags2str(int, char *, size_t);
102 
103 extern int		  nl_msg_parse(struct nl_msg *,
104 				       void (*cb)(struct nl_object *, void *),
105 				       void *);
106 
107 extern void		nl_msg_dump(struct nl_msg *, FILE *);
108 
109 /**
110  * @name Iterators
111  * @{
112  */
113 
114 /**
115  * @ingroup msg
116  * Iterate over a stream of attributes in a message
117  * @arg pos	loop counter, set to current attribute
118  * @arg nlh	netlink message header
119  * @arg hdrlen	length of family header
120  * @arg rem	initialized to len, holds bytes currently remaining in stream
121  */
122 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
123 	nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
124 			  nlmsg_attrlen(nlh, hdrlen), rem)
125 
126 /**
127  * Iterate over a stream of messages
128  * @arg pos	loop counter, set to current message
129  * @arg head	head of message stream
130  * @arg len	length of message stream
131  * @arg rem	initialized to len, holds bytes currently remaining in stream
132  */
133 #define nlmsg_for_each_msg(pos, head, len, rem) \
134 	for (pos = head, rem = len; \
135 	     nlmsg_ok(pos, rem); \
136 	     pos = nlmsg_next(pos, &(rem)))
137 
138 /** @} */
139 
140 #ifdef __cplusplus
141 }
142 #endif
143 
144 #endif
145