libnl 1.1.4
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
20extern "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
43struct nl_msg;
44struct nl_tree;
45struct ucred;
46
47/* size calculations */
48extern int nlmsg_msg_size(int);
49extern int nlmsg_total_size(int);
50extern int nlmsg_padlen(int);
51
52/* payload access */
53extern void * nlmsg_data(const struct nlmsghdr *);
54extern int nlmsg_len(const struct nlmsghdr *);
55extern void * nlmsg_tail(const struct nlmsghdr *);
56
57/* attribute access */
58extern struct nlattr * nlmsg_attrdata(const struct nlmsghdr *, int);
59extern int nlmsg_attrlen(const struct nlmsghdr *, int);
60
61/* message parsing */
62extern int nlmsg_valid_hdr(const struct nlmsghdr *, int);
63extern int nlmsg_ok(const struct nlmsghdr *, int);
64extern struct nlmsghdr * nlmsg_next(struct nlmsghdr *, int *);
65extern int nlmsg_parse(struct nlmsghdr *, int, struct nlattr **,
66 int, struct nla_policy *);
67extern struct nlattr * nlmsg_find_attr(struct nlmsghdr *, int, int);
68extern int nlmsg_validate(struct nlmsghdr *, int, int,
69 struct nla_policy *);
70
71/* Backward compatibility */
72#define nlmsg_new() nlmsg_alloc()
73#define nlmsg_build_simple(a, b) nlmsg_alloc_simple(a, b)
74#define nlmsg_build(ptr) nlmsg_inherit(ptr)
75
76extern struct nl_msg * nlmsg_alloc(void);
77extern struct nl_msg * nlmsg_alloc_size(size_t);
78extern struct nl_msg * nlmsg_alloc_simple(int, int);
79extern void nlmsg_set_default_size(size_t);
80extern struct nl_msg * nlmsg_inherit(struct nlmsghdr *);
81extern struct nl_msg * nlmsg_convert(struct nlmsghdr *);
82extern void * nlmsg_reserve(struct nl_msg *, size_t, int);
83extern int nlmsg_append(struct nl_msg *, void *, size_t, int);
84extern int nlmsg_expand(struct nl_msg *, size_t);
85
86extern struct nlmsghdr * nlmsg_put(struct nl_msg *, uint32_t, uint32_t,
87 int, int, int);
88extern struct nlmsghdr * nlmsg_hdr(struct nl_msg *);
89extern void nlmsg_free(struct nl_msg *);
90
91/* attribute modification */
92extern void nlmsg_set_proto(struct nl_msg *, int);
93extern int nlmsg_get_proto(struct nl_msg *);
94extern size_t nlmsg_get_max_size(struct nl_msg *);
95extern void nlmsg_set_src(struct nl_msg *, struct sockaddr_nl *);
96extern struct sockaddr_nl *nlmsg_get_src(struct nl_msg *);
97extern void nlmsg_set_dst(struct nl_msg *, struct sockaddr_nl *);
98extern struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *);
99extern void nlmsg_set_creds(struct nl_msg *, struct ucred *);
100extern struct ucred * nlmsg_get_creds(struct nl_msg *);
101
102extern char * nl_nlmsgtype2str(int, char *, size_t);
103extern int nl_str2nlmsgtype(const char *);
104
105extern char * nl_nlmsg_flags2str(int, char *, size_t);
106
107extern int nl_msg_parse(struct nl_msg *,
108 void (*cb)(struct nl_object *, void *),
109 void *);
110
111extern void nl_msg_dump(struct nl_msg *, FILE *);
112
113/**
114 * @name Iterators
115 * @{
116 */
117
118/**
119 * @ingroup msg
120 * Iterate over a stream of attributes in a message
121 * @arg pos loop counter, set to current attribute
122 * @arg nlh netlink message header
123 * @arg hdrlen length of family header
124 * @arg rem initialized to len, holds bytes currently remaining in stream
125 */
126#define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
127 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
128 nlmsg_attrlen(nlh, hdrlen), rem)
129
130/**
131 * Iterate over a stream of messages
132 * @arg pos loop counter, set to current message
133 * @arg head head of message stream
134 * @arg len length of message stream
135 * @arg rem initialized to len, holds bytes currently remaining in stream
136 */
137#define nlmsg_for_each_msg(pos, head, len, rem) \
138 for (pos = head, rem = len; \
139 nlmsg_ok(pos, rem); \
140 pos = nlmsg_next(pos, &(rem)))
141
142/** @} */
143
144#ifdef __cplusplus
145}
146#endif
147
148#endif
int nlmsg_ok(const struct nlmsghdr *, int)
check if the netlink message fits into the remaining bytes
Definition: msg.c:285
void nl_msg_dump(struct nl_msg *, FILE *)
Dump message in human readable format to file descriptor.
Definition: msg.c:947
int nlmsg_total_size(int)
length of netlink message including padding
Definition: msg.c:193
struct nlmsghdr * nlmsg_hdr(struct nl_msg *)
Return actual netlink message.
Definition: msg.c:643
struct nl_msg * nlmsg_alloc_simple(int, int)
Allocate a new netlink message.
Definition: msg.c:448
void nlmsg_free(struct nl_msg *)
Free a netlink message.
Definition: msg.c:656
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:319
void * nlmsg_data(const struct nlmsghdr *)
head of message payload
Definition: msg.c:218
int nlmsg_msg_size(int)
length of netlink message not including padding
Definition: msg.c:184
struct nlmsghdr * nlmsg_next(struct nlmsghdr *, int *)
next netlink message in message stream
Definition: msg.c:300
void * nlmsg_reserve(struct nl_msg *, size_t, int)
Reserve room for additional data in a netlink message.
Definition: msg.c:512
struct nl_msg * nlmsg_convert(struct nlmsghdr *)
Convert a netlink message received from a netlink socket to a nl_msg.
Definition: msg.c:484
struct nl_msg * nlmsg_alloc_size(size_t)
Allocate a new netlink message with maximum payload size specified.
Definition: msg.c:409
int nlmsg_expand(struct nl_msg *, size_t)
Expand maximum payload size of a netlink message.
Definition: msg.c:577
struct nlattr * nlmsg_find_attr(struct nlmsghdr *, int, int)
nlmsg_find_attr - find a specific attribute in a netlink message
Definition: msg.c:337
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition: msg.c:401
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:610
int nlmsg_validate(struct nlmsghdr *, int, int, struct nla_policy *)
nlmsg_validate - validate a netlink message including attributes
Definition: msg.c:350
int nlmsg_attrlen(const struct nlmsghdr *, int)
length of attributes data
Definition: msg.c:260
void nlmsg_set_default_size(size_t)
Set the default maximum message payload size for allocated messages.
Definition: msg.c:467
int nlmsg_append(struct nl_msg *, void *, size_t, int)
Append data to tail of a netlink message.
Definition: msg.c:549
int nlmsg_padlen(int)
length of padding at the message's tail
Definition: msg.c:202
struct nlattr * nlmsg_attrdata(const struct nlmsghdr *, int)
head of attributes data
Definition: msg.c:249
int nlmsg_len(const struct nlmsghdr *)
length of message payload
Definition: msg.c:232
struct nl_msg * nlmsg_inherit(struct nlmsghdr *)
Allocate a new netlink message and inherit netlink message header.
Definition: msg.c:424
attribute validation policy
Definition: attr.h:73
Netlink message header.
Netlink socket address.
Definition: netlink-kernel.h:9