libnl: /data/src/libnl3/include/netlink/msg.h Source File

libnl 3.10.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
14extern "C" {
15#endif
16
17struct 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
42struct nl_msg;
43struct nl_tree;
44struct ucred;
45
46extern int nlmsg_size(int);
47extern int nlmsg_total_size(int);
48extern int nlmsg_padlen(int);
49
50extern void * nlmsg_data(const struct nlmsghdr *);
51extern int nlmsg_datalen(const struct nlmsghdr *);
52extern void * nlmsg_tail(const struct nlmsghdr *);
53
54/* attribute access */
55extern struct nlattr * nlmsg_attrdata(const struct nlmsghdr *, int);
56extern int nlmsg_attrlen(const struct nlmsghdr *, int);
57
58/* message parsing */
59extern int nlmsg_valid_hdr(const struct nlmsghdr *, int);
60extern int nlmsg_ok(const struct nlmsghdr *, int);
61extern struct nlmsghdr * nlmsg_next(struct nlmsghdr *, int *);
62extern int nlmsg_parse(struct nlmsghdr *, int, struct nlattr **,
63 int, const struct nla_policy *);
64extern struct nlattr * nlmsg_find_attr(struct nlmsghdr *, int, int);
65extern int nlmsg_validate(struct nlmsghdr *, int, int,
66 const struct nla_policy *);
67
68extern struct nl_msg * nlmsg_alloc(void);
69extern struct nl_msg * nlmsg_alloc_size(size_t);
70extern struct nl_msg * nlmsg_alloc_simple(int, int);
71extern void nlmsg_set_default_size(size_t);
72extern struct nl_msg * nlmsg_inherit(struct nlmsghdr *);
73extern struct nl_msg * nlmsg_convert(struct nlmsghdr *);
74extern void * nlmsg_reserve(struct nl_msg *, size_t, int);
75extern int nlmsg_append(struct nl_msg *, void *, size_t, int);
76extern int nlmsg_expand(struct nl_msg *, size_t);
77
78extern struct nlmsghdr * nlmsg_put(struct nl_msg *, uint32_t, uint32_t,
79 int, int, int);
80extern struct nlmsghdr * nlmsg_hdr(struct nl_msg *);
81extern void nlmsg_get(struct nl_msg *);
82extern void nlmsg_free(struct nl_msg *);
83
84/* attribute modification */
85extern void nlmsg_set_proto(struct nl_msg *, int);
86extern int nlmsg_get_proto(struct nl_msg *);
87extern size_t nlmsg_get_max_size(struct nl_msg *);
88extern void nlmsg_set_src(struct nl_msg *, struct sockaddr_nl *);
89extern struct sockaddr_nl *nlmsg_get_src(struct nl_msg *);
90extern void nlmsg_set_dst(struct nl_msg *, struct sockaddr_nl *);
91extern struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *);
92extern void nlmsg_set_creds(struct nl_msg *, struct ucred *);
93extern struct ucred * nlmsg_get_creds(struct nl_msg *);
94
95extern char * nl_nlmsgtype2str(int, char *, size_t);
96extern int nl_str2nlmsgtype(const char *);
97
98extern char * nl_nlmsg_flags2str(int, char *, size_t);
99
100extern int nl_msg_parse(struct nl_msg *,
101 void (*cb)(struct nl_object *, void *),
102 void *);
103
104extern 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
nlmsg_ok
int nlmsg_ok(const struct nlmsghdr *, int)
check if the netlink message fits into the remaining bytes
Definition msg.c:185
nl_msg_dump
void nl_msg_dump(struct nl_msg *, FILE *)
Dump message in human readable format to file descriptor.
Definition msg.c:985
nlmsg_total_size
int nlmsg_total_size(int)
Calculates size of netlink message including padding based on payload length.
Definition msg.c:75
nlmsg_hdr
struct nlmsghdr * nlmsg_hdr(struct nl_msg *)
Return actual netlink message.
Definition msg.c:550
nlmsg_alloc_simple
struct nl_msg * nlmsg_alloc_simple(int, int)
Allocate a new netlink message.
Definition msg.c:352
nlmsg_data
void * nlmsg_data(const struct nlmsghdr *)
Return pointer to message payload.
Definition msg.c:108
nlmsg_get
void nlmsg_get(struct nl_msg *)
Acquire a reference on a netlink message.
Definition msg.c:559
nlmsg_next
struct nlmsghdr * nlmsg_next(struct nlmsghdr *, int *)
next netlink message in message stream
Definition msg.c:200
nlmsg_validate
int nlmsg_validate(struct nlmsghdr *, int, int, const struct nla_policy *)
nlmsg_validate - validate a netlink message including attributes
Definition msg.c:250
nlmsg_reserve
void * nlmsg_reserve(struct nl_msg *, size_t, int)
Reserve room for additional data in a netlink message.
Definition msg.c:418
nlmsg_convert
struct nl_msg * nlmsg_convert(struct nlmsghdr *)
Convert a netlink message received from a netlink socket to a nl_msg.
Definition msg.c:393
nlmsg_free
void nlmsg_free(struct nl_msg *)
Release a reference from an netlink message.
Definition msg.c:572
nlmsg_parse
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:219
nlmsg_alloc_size
struct nl_msg * nlmsg_alloc_size(size_t)
Allocate a new netlink message with maximum payload size specified.
Definition msg.c:313
nlmsg_expand
int nlmsg_expand(struct nl_msg *, size_t)
Expand maximum payload size of a netlink message.
Definition msg.c:484
nlmsg_find_attr
struct nlattr * nlmsg_find_attr(struct nlmsghdr *, int, int)
nlmsg_find_attr - find a specific attribute in a netlink message
Definition msg.c:237
nlmsg_alloc
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition msg.c:305
nlmsg_datalen
int nlmsg_datalen(const struct nlmsghdr *)
Return length of message payload.
Definition msg.c:124
nlmsg_put
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:517
nlmsg_attrlen
int nlmsg_attrlen(const struct nlmsghdr *, int)
length of attributes data
Definition msg.c:157
nlmsg_set_default_size
void nlmsg_set_default_size(size_t)
Set the default maximum message payload size for allocated messages.
Definition msg.c:373
nlmsg_append
int nlmsg_append(struct nl_msg *, void *, size_t, int)
Append data to tail of a netlink message.
Definition msg.c:456
nlmsg_padlen
int nlmsg_padlen(int)
Size of padding that needs to be added at end of message.
Definition msg.c:90
nlmsg_attrdata
struct nlattr * nlmsg_attrdata(const struct nlmsghdr *, int)
head of attributes data
Definition msg.c:146
nlmsg_size
int nlmsg_size(int)
Calculates size of netlink message based on payload length.
Definition msg.c:57
nlmsg_inherit
struct nl_msg * nlmsg_inherit(struct nlmsghdr *)
Allocate a new netlink message and inherit netlink message header.
Definition msg.c:328
nla_policy
Attribute validation policy.
Definition attr.h:63