4 * Copyright (C) 2009-2010 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 [CCode (lower_case_cprefix = "nl_", cheader_filename = "netlink/netlink.h")]
25 [CCode (cname = "nl_geterror", cheader_filename = "netlink/netlink.h")]
26 public static unowned string strerror( int number );
28 [CCode (instance_pos = -1)]
29 public delegate void CallbackFunc (Object obj);
31 [CCode (cname = "nl_recmsg_msg_cb_t", cheader_filename = "netlink/netlink.h", instance_pos = -1)]
32 public delegate int MessageCallbackFunc (Message msg);
35 [CCode (cprefix = "nl_addr_", cname = "struct nl_addr", free_function = "", cheader_filename = "netlink/netlink.h")]
36 public class Address : Object {
37 [CCode (cname = "nl_addr_alloc")]
41 public int build_add_request (int a, out Message m);
42 public int build_delete_request (int a, out Message m);
44 public int set_label (string label);
45 public string get_label ();
47 public void set_family (int family);
48 public int get_family ();
50 public int get_len ();
52 public void set_prefixlen (int len);
53 public int get_prefixlen ();
55 public void set_flags (uint flags);
56 public void unset_flags (uint flags);
57 public uint get_flags ();
59 public void* get_binary_addr();
61 [CCode (cname = "nl_addr2str")]
62 public unowned string to_stringbuf(char[] buf);
64 public string to_string() {
65 char[] buf = new char[256];
66 return to_stringbuf( buf );
71 [CCode (cprefix = "nla_", cname = "struct nlattr", free_function = "", cheader_filename = "netlink/netlink.h")]
72 public class Attribute {
73 public static int attr_size (int payload);
74 public static int total_size (int payload);
75 public static int padlen (int payload);
80 public int ok (int remaining);
81 public Attribute next (out int remaining);
82 public static int parse (Attribute[] attributes, Attribute head, int len, AttributePolicy? policy = null);
83 public int validate (int len, int maxtype, AttributePolicy? policy = null);
84 public Attribute find (int len, int attrtype);
88 [CCode (cname = "struct nla_policy", free_function = "")]
89 public class AttributePolicy {
91 public AttributePolicy( AttributeType type = AttributeType.UNSPEC, uint16 minlen = 0, uint16 maxlen = 65535 )
102 [CCode (cprefix = "NLA_", cname = "int", cheader_filename = "netlink/attr.h")]
103 public enum AttributeType {
104 UNSPEC, /**< Unspecified type, binary data chunk */
105 U8, /**< 8 bit integer */
106 U16, /**< 16 bit integer */
107 U32, /**< 32 bit integer */
108 U64, /**< 64 bit integer */
109 STRING, /**< NUL terminated character string */
111 MSECS, /**< Micro seconds (64bit) */
112 NESTED, /**< Nested attributes */
117 [CCode (cprefix = "rtnl_addr_", cname = "struct rtnl_addr", free_function = "", cheader_filename = "netlink/route/addr.h")]
118 public class RouteAddress : Address {
119 [CCode (cname = "rtnl_addr_alloc")]
120 public RouteAddress();
122 public void set_ifindex (int index );
123 public int get_ifindex ();
125 public void set_scope (int scope);
126 public int get_scope ();
128 public unowned Address get_local();
132 [CCode (cprefix = "nl_cache_", cname = "struct nl_cache", free_function = "nl_cache_free", cheader_filename = "netlink/netlink.h")]
134 public static int alloc_name (string name, out Cache c);
136 public void @foreach (CallbackFunc cb);
137 public void foreach_filter (Object obj, CallbackFunc cb);
139 public void mngt_provide();
140 public void mngt_unprovide();
144 [CCode (cprefix = "nl_cb_", cname = "struct nl_cb", free_function = "", cheader_filename = "netlink/netlink.h")]
145 public class Callback {
146 [CCode (cname = "nl_cb_alloc")]
147 public Callback (CallbackKind kind = CallbackKind.DEFAULT);
148 [CCode (cname = "nl_cb_set")]
149 public int @set (CallbackType type, CallbackKind kind, MessageCallbackFunc func);
150 [CCode (cname = "nl_cb_set_all")]
151 public int set_all (CallbackKind kind, MessageCallbackFunc func);
154 [CCode (cname = "enum nl_cb_action", cprefix = "NL_", cheader_filename = "netlink/netlink.h")]
155 public enum CallbackAction {
156 OK, // Proceed with whatever comes next.
157 SKIP, // Skip this message.
158 STOP, // Stop parsing altogether and discard remaining messages.
161 [CCode (cname = "enum nl_cb_kind", cprefix = "NL_CB_", cheader_filename = "netlink/netlink.h")]
162 public enum CallbackKind {
163 DEFAULT, // Default handlers (quiet).
164 VERBOSE, // Verbose default handlers (error messages printed).
165 DEBUG, // Debug handlers for debugging.
166 CUSTOM, // Customized handler specified by the user.
169 [CCode (cname = "enum nl_cb_type", cprefix = "NL_CB_", cheader_filename = "netlink/netlink.h")]
170 public enum CallbackType {
171 VALID, // Message is valid.
172 FINISH, // Last message in a series of multi part messages received.
173 OVERRUN, // Report received that data was lost.
174 SKIPPED, // Message wants to be skipped.
175 ACK, // Message is an acknowledge.
176 MSG_IN, // Called for every message received.
177 MSG_OUT, // Called for every message sent out except for nl_sendto().
178 INVALID, // Message is malformed and invalid.
179 SEQ_CHECK, // Called instead of internal sequence number checking.
180 SEND_ACK, // Sending of an acknowledge message has been requested.
184 [CCode (cprefix = "nl_link_cache_", cname = "struct nl_cache", free_function = "nl_cache_free", cheader_filename = "netlink/netlink.h")]
185 public class LinkCache : Cache {
186 [CCode (cname = "rtnl_link_name2i")]
187 public int name2i (string name);
191 [CCode (cprefix = "nl_addr_cache", cname = "struct nl_cache", free_function = "nl_cache_free", cheader_filename = "netlink/netlink.h")]
192 public class AddrCache : Cache {
196 [CCode (cprefix = "nl_msg_", cname = "struct nl_msg", free_function = "nl_msg_free", cheader_filename = "netlink/netlink.h")]
197 public class Message {
198 public void dump (Posix.FILE file);
199 public int parse (CallbackFunc func);
200 [CCode (cname = "nlmsg_hdr")]
201 public MessageHeader header ();
205 [CCode (cprefix = "nlmsg_", cname = "struct nlmsghdr", free_function = "", cheader_filename = "netlink/netlink.h")]
206 public class MessageHeader {
208 public uint32 nlmsg_len;
209 public uint16 nlmsg_type;
210 public uint16 nlmsg_flags;
211 public uint32 nlmsg_seq;
212 public uint32 nlmsg_pid;
215 public static int msg_size (int payload);
216 public static int total_size (int payload);
217 public static int padlen (int payload);
220 public void* data ();
222 public void* tail ();
225 public Attribute attrdata (int hdrlen);
226 public int attrlen (int hdrlen);
229 public bool valid_hdr (int hdrlen);
230 public bool ok (int remaining);
231 public MessageHeader next (out int remaining);
232 public int parse (int hdrlen, [CCode (array_length = "false")] out Attribute[] attributes, AttributeType maxtype, AttributePolicy? policy = null);
233 public Attribute? find_attr (int hdrlen, AttributeType type);
234 public int validate (int hdrlen, AttributeType maxtype, AttributePolicy policy);
238 [CCode (cprefix = "nl_socket_", cname = "struct nl_sock", free_function = "nl_socket_free")]
239 public class Socket {
240 [CCode (cname = "nl_socket_alloc")]
243 [CCode (cname = "rtnl_link_alloc_cache")]
244 public int link_alloc_cache (out LinkCache c);
245 [CCode (cname = "rtnl_addr_alloc_cache")]
246 public int addr_alloc_cache (out AddrCache c);
248 // connection management
249 [CCode (cname = "nl_close")]
251 [CCode (cname = "nl_connect")]
252 public int connect (int family);
255 public int add_memberships (int group, ...);
256 public int add_membership (int group);
257 public int drop_memberships (int group, ...);
258 public int drop_membership (int group);
259 public uint32 get_peer_port ();
260 public void set_peer_port (uint32 port);
262 // callback management
263 public Callback get_cb ();
264 public void set_cb (Callback cb);
265 public int modify_cb (CallbackType type, CallbackKind kind, MessageCallbackFunc callback);
268 public int set_buffer_size (int rxbuf, int txbuf);
269 public int set_passcred (bool on);
270 public int recv_pktinfo (bool on);
272 public void disable_seq_check ();
273 public uint use_seq ();
274 public void disable_auto_ack ();
275 public void enable_auto_ack ();
277 public int get_fd ();
278 public int set_nonblocking ();
279 public void enable_msg_peek ();
280 public void disable_msg_peek ();
282 // receiving messages
283 [CCode (cname = "nl_recv")]
284 public int recv (out Linux.Netlink.SockAddrNl addr, out char[] buf, out Linux.Socket.ucred cred);
286 [CCode (cname = "nl_recvmsgs")]
287 public int recvmsgs (Callback cb);
289 [CCode (cname = "nl_recvmsgs_default")]
290 public int recvmsgs_default ();
292 [CCode (cname = "nl_wait_for_ack")]
293 public int wait_for_ack ();
297 [CCode (cprefix = "nl_object_", cname = "struct nl_object", free_function = "nl_object_free", cheader_filename = "netlink/object.h")]
298 public class Object {
300 public unowned string attrs2str (uint32 attrs, char[] buf);
301 public unowned string attr_list (char[] buf);
302 public void dump (DumpParams params);
306 [CCode (cprefix = "NL_DUMP_", cname = "int", cheader_filename = "netlink/types.h")]
307 public enum DumpType {
308 LINE, /**< Dump object briefly on one line */
309 DETAILS, /**< Dump all attributes but no statistics */
310 STATS, /**< Dump all attributes including statistics */
311 ENV, /**< Dump all attribtues as env variables */
314 [CCode (cname = "struct nl_dump_params", free_function = "", cheader_filename = "netlink/types.h")]
315 public struct DumpParams {
316 public DumpType dp_type;
317 public int dp_prefix;
318 public bool dp_print_index;
319 public bool dp_dump_msgtype;
320 public unowned Posix.FILE dp_fd;
321 public unowned string dp_buf;
322 public size_t dp_buflen;