4 * Copyright (C) 2009-2015 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
5 * Copyright (C) 2011 Klaus Kurzmann <mok@fluxnetz.de>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 [CCode (lower_case_cprefix = "nl_", cheader_filename = "netlink/netlink.h")]
26 [CCode (cname = "nl_geterror", cheader_filename = "netlink/netlink.h")]
27 public static unowned string strerror( int number );
29 [CCode (instance_pos = -1)]
30 public delegate void CallbackFunc (Object obj);
32 [CCode (cname = "nl_recmsg_msg_cb_t", cheader_filename = "netlink/netlink.h", instance_pos = -1)]
33 public delegate int MessageCallbackFunc (Message msg);
36 [CCode (cprefix = "rtnl_addr_", cname = "struct nl_addr", free_function = "", cheader_filename = "netlink/netlink.h")]
37 public class Address : Object {
38 [CCode (cname = "nl_addr_alloc")]
42 public int build_add_request (int a, out Message m);
43 public int build_delete_request (int a, out Message m);
45 public int set_label (string label);
46 public unowned string get_label ();
48 public void set_family (int family);
49 public int get_family ();
52 public Netlink.Address? get_local();
53 public Netlink.Address? get_peer();
54 public Netlink.Address? get_broadcast();
55 public Netlink.Address? get_anycast();
56 public Netlink.Address? get_multicast();
58 public void set_prefixlen (int len);
59 public int get_prefixlen ();
61 public int get_ifindex();
62 public int get_scope();
64 public void set_flags (int flags);
65 public void unset_flags (int flags);
66 public int get_flags ();
68 [CCode (cname = "nl_addr_get_len")]
71 [CCode (cname = "nl_addr_get_binary_addr")]
72 public void* get_binary_addr();
74 [CCode (cname = "nl_addr2str")]
75 public unowned string to_stringbuf(char[] buf);
77 public string to_string() {
78 char[] buf = new char[256];
79 return to_stringbuf( buf );
84 [CCode (cprefix = "nla_", cname = "struct nlattr", free_function = "", cheader_filename = "netlink/netlink.h")]
85 public class Attribute {
86 public static int attr_size (int payload);
87 public static int total_size (int payload);
88 public static int padlen (int payload);
93 public int ok (int remaining);
94 public Attribute next (out int remaining);
95 public static int parse (Attribute[] attributes, Attribute head, int len, AttributePolicy? policy = null);
96 public int validate (int len, int maxtype, AttributePolicy? policy = null);
97 public Attribute find (int len, int attrtype);
101 [CCode (cname = "struct nla_policy", free_function = "")]
102 public class AttributePolicy {
104 public AttributePolicy( AttributeType type = AttributeType.UNSPEC, uint16 minlen = 0, uint16 maxlen = 65535 )
107 this.minlen = minlen;
108 this.maxlen = maxlen;
111 public uint16 minlen;
112 public uint16 maxlen;
115 [CCode (cprefix = "NLA_", cname = "int", cheader_filename = "netlink/attr.h", has_type_id = false)]
116 public enum AttributeType {
117 UNSPEC, // Unspecified type, binary data chunk
119 U16, // 16 bit integer
120 U32, // 32 bit integer
121 U64, // 64 bit integer
122 STRING, // NUL terminated character string
124 MSECS, // Micro seconds (64bit)
125 NESTED, // Nested attributes
130 [CCode (cprefix = "rtnl_addr_", cname = "struct rtnl_addr", free_function = "", cheader_filename = "netlink/route/addr.h")]
131 public class RouteAddress : Address {
132 [CCode (cname = "rtnl_addr_alloc")]
133 public RouteAddress();
135 public void set_ifindex (int index );
136 public int get_ifindex ();
138 public void set_scope (int scope);
139 public int get_scope ();
141 public unowned Address get_local();
145 [CCode (cprefix = "nl_cache_", cname = "struct nl_cache", free_function = "nl_cache_free", cheader_filename = "netlink/netlink.h")]
147 public static int alloc_name (string name, out Cache c);
149 public void @foreach (CallbackFunc cb);
150 public void foreach_filter (Object obj, CallbackFunc cb);
152 public void mngt_provide();
153 public void mngt_unprovide();
156 [CCode (cname = "int", cprefix = "NL_ACT_", has_type_id = false, cheader_filename = "netlink/cache.h")]
157 public enum CacheAction {
165 [CCode (cname = "change_func_t", cheader_filename = "netlink/cache.h", instance_pos = -1)]
166 public delegate void ChangeCallbackFunc (Cache cache, Object obj, CacheAction act);
169 [CCode (cprefix = "nl_cache_mngr_", cname = "struct nl_cache_mngr", free_function = "nl_cache_mngr_free", cheader_filename = "netlink/cache.h")]
170 public class CacheManager {
171 public static int alloc (Socket? sk, int protocol, int flags, out CacheManager c);
173 public int add_cache(Cache cache, ChangeCallbackFunc cb);
174 public int add(string name, ChangeCallbackFunc cb, out unowned Cache cache);
177 public int poll(int timeout);
179 public int data_ready();
180 public void info(DumpParams params);
185 [CCode (cprefix = "nl_cb_", cname = "struct nl_cb", free_function = "", cheader_filename = "netlink/netlink.h")]
186 public class Callback {
187 [CCode (cname = "nl_cb_alloc")]
188 public Callback (CallbackKind kind = CallbackKind.DEFAULT);
189 [CCode (cname = "nl_cb_set")]
190 public int @set (CallbackType type, CallbackKind kind, MessageCallbackFunc func);
191 [CCode (cname = "nl_cb_set_all")]
192 public int set_all (CallbackKind kind, MessageCallbackFunc func);
195 [CCode (cname = "enum nl_cb_action", cprefix = "NL_", cheader_filename = "netlink/netlink.h", has_type_id = false)]
196 public enum CallbackAction {
197 OK, // Proceed with whatever comes next.
198 SKIP, // Skip this message.
199 STOP, // Stop parsing altogether and discard remaining messages.
202 [CCode (cname = "enum nl_cb_kind", cprefix = "NL_CB_", cheader_filename = "netlink/netlink.h", has_type_id = false)]
203 public enum CallbackKind {
204 DEFAULT, // Default handlers (quiet).
205 VERBOSE, // Verbose default handlers (error messages printed).
206 DEBUG, // Debug handlers for debugging.
207 CUSTOM, // Customized handler specified by the user.
210 [CCode (cname = "enum nl_cb_type", cprefix = "NL_CB_", cheader_filename = "netlink/netlink.h", has_type_id = false)]
211 public enum CallbackType {
212 VALID, // Message is valid.
213 FINISH, // Last message in a series of multi part messages received.
214 OVERRUN, // Report received that data was lost.
215 SKIPPED, // Message wants to be skipped.
216 ACK, // Message is an acknowledge.
217 MSG_IN, // Called for every message received.
218 MSG_OUT, // Called for every message sent out except for nl_sendto().
219 INVALID, // Message is malformed and invalid.
220 SEQ_CHECK, // Called instead of internal sequence number checking.
221 SEND_ACK, // Sending of an acknowledge message has been requested.
225 [CCode (cprefix = "nl_link_cache_", cname = "struct nl_cache", free_function = "nl_cache_free", cheader_filename = "netlink/netlink.h")]
226 public class LinkCache : Cache
228 [CCode (cname = "rtnl_link_name2i")]
229 public int name2i (string name);
230 [CCode (cname = "rtnl_link_i2name")]
231 public unowned string i2name( int idx, char[] buffer );
232 [CCode (cname = "rtnl_link_get")]
233 public CachedLink? get(int idx);
234 [CCode (cname = "rtnl_link_get_by_name")]
235 public CachedLink? get_by_name(string idx);
239 [CCode (cprefix = "nl_addr_cache", cname = "struct nl_cache", free_function = "nl_cache_free", cheader_filename = "netlink/netlink.h")]
240 public class AddrCache : Cache
245 [CCode (cprefix = "nl_msg_", cname = "struct nl_msg", free_function = "nl_msg_free", cheader_filename = "netlink/netlink.h")]
248 public void dump (Posix.FILE file);
249 public int parse (CallbackFunc func);
250 [CCode (cname = "nlmsg_hdr")]
251 public MessageHeader header ();
255 [CCode (cprefix = "nlmsg_", cname = "struct nlmsghdr", free_function = "", cheader_filename = "netlink/netlink.h")]
256 public class MessageHeader
259 public uint32 nlmsg_len;
260 public uint16 nlmsg_type;
261 public uint16 nlmsg_flags;
262 public uint32 nlmsg_seq;
263 public uint32 nlmsg_pid;
266 public static int msg_size (int payload);
267 public static int total_size (int payload);
268 public static int padlen (int payload);
271 public void* data ();
273 public void* tail ();
276 public Attribute attrdata (int hdrlen);
277 public int attrlen (int hdrlen);
280 public bool valid_hdr (int hdrlen);
281 public bool ok (int remaining);
282 public MessageHeader next (out int remaining);
283 public int parse (int hdrlen, [CCode (array_length = "false")] out Attribute[] attributes, AttributeType maxtype, AttributePolicy? policy = null);
284 public Attribute? find_attr (int hdrlen, AttributeType type);
285 public int validate (int hdrlen, AttributeType maxtype, AttributePolicy policy);
289 [CCode (cprefix = "nl_socket_", cname = "struct nl_sock", free_function = "nl_socket_free")]
290 public class Socket {
291 [CCode (cname = "nl_socket_alloc")]
294 [CCode (cname = "rtnl_link_alloc_cache")]
295 public int link_alloc_cache (int family, out LinkCache c);
296 [CCode (cname = "rtnl_addr_alloc_cache")]
297 public int addr_alloc_cache (out AddrCache c);
299 // connection management
300 [CCode (cname = "nl_close")]
302 [CCode (cname = "nl_connect")]
303 public int connect (int family);
306 public int add_memberships (int group, ...);
307 public int add_membership (int group);
308 public int drop_memberships (int group, ...);
309 public int drop_membership (int group);
310 public uint32 get_peer_port ();
311 public void set_peer_port (uint32 port);
313 // callback management
314 public Callback get_cb ();
315 public void set_cb (Callback cb);
316 public int modify_cb (CallbackType type, CallbackKind kind, MessageCallbackFunc callback);
319 public int set_buffer_size (int rxbuf, int txbuf);
320 public int set_passcred (bool on);
321 public int recv_pktinfo (bool on);
323 public void disable_seq_check ();
324 public uint use_seq ();
325 public void disable_auto_ack ();
326 public void enable_auto_ack ();
328 public int get_fd ();
329 public int set_nonblocking ();
330 public void enable_msg_peek ();
331 public void disable_msg_peek ();
333 // receiving messages
334 [CCode (cname = "nl_recv")]
335 public int recv (out Linux.Netlink.SockAddrNl addr, out char[] buf, out Linux.Socket.ucred cred);
337 [CCode (cname = "nl_recvmsgs")]
338 public int recvmsgs (Callback cb);
340 [CCode (cname = "nl_recvmsgs_default")]
341 public int recvmsgs_default ();
343 [CCode (cname = "nl_wait_for_ack")]
344 public int wait_for_ack ();
348 [CCode (cprefix = "nl_object_", cname = "struct nl_object", free_function = "nl_object_free", cheader_filename = "netlink/object.h")]
351 public uint32 ce_mask;
353 public unowned string attrs2str (uint32 attrs, char[] buf);
354 public unowned string attr_list (char[] buf);
355 public void dump (DumpParams params);
359 [CCode (cprefix = "NL_DUMP_", cname = "int", cheader_filename = "netlink/types.h", has_type_id = false)]
360 public enum DumpType {
361 LINE, // Dump object briefly on one line
362 DETAILS, // Dump all attributes but no statistics
363 STATS, // Dump all attributes including statistics
366 [CCode (cname = "struct nl_dump_params", free_function = "", cheader_filename = "netlink/types.h", has_type_id = false)]
367 public struct DumpParams {
368 public DumpType dp_type;
369 public int dp_prefix;
370 public bool dp_print_index;
371 public bool dp_dump_msgtype;
372 public unowned Posix.FILE dp_fd;
373 public unowned string dp_buf;
374 public size_t dp_buflen;
378 [CCode (cprefix = "rtnl_link_", cname = "struct rtnl_link", free_function = "rtnl_link_destroy", cheader_filename = "netlink/route/link.h")]
381 public unowned string get_name();
382 public Netlink.Address? get_addr();
383 public Netlink.Address? get_broadcast();
384 public uint get_flags();
385 public int get_family();
386 public uint get_arptype();
387 public int get_ifindex();
388 public uint get_mtu();
389 public uint get_txqlen();
390 public uint get_weight();
391 public unowned string? get_qdisc();
395 [CCode (cprefix = "rtnl_link_", cname = "struct rtnl_link", free_function = "rtnl_link_put", cheader_filename = "netlink/route/link.h")]
396 public class CachedLink : Link
402 [CCode (cprefix = "rtnl_route_", cname = "struct rtnl_route", cheader_filename = "netlink/route/route.h")]
405 public uint32 get_table();
406 public uint8 get_scope();
407 public uint8 get_tos();
408 public uint8 get_protocol();
409 public uint32 get_priority();
410 public uint8 get_family();
411 public Netlink.Address? get_dst();
412 public Netlink.Address? get_src();
413 public uint8 get_type();
414 public uint32 get_flags();
415 public int get_metric();
416 public Netlink.Address? get_pref_src();
417 public int get_iif();
421 [CCode (cprefix = "rtnl_neigh_", cname = "struct rtnl_neigh", cheader_filename = "netlink/route/neighbour.h")]
422 public class Neighbour
424 public int get_state();
425 public uint get_flags();
426 public int get_ifindex();
427 public Netlink.Address? get_lladdr();
428 public Netlink.Address? get_dst();
429 public int get_type();
430 public int get_family();
434 [CCode (cprefix = "rtnl_rule_", cname = "struct rtnl_rule", cheader_filename = "netlink/route/rule.h")]
437 public int get_family();
438 public uint32 get_prio();
439 public uint32 get_mark();
440 public uint32 get_mask();
441 public uint32 get_table();
442 public uint8 get_dsfield();
443 public Netlink.Address? get_src();
444 public Netlink.Address? get_dst();
445 public uint8 get_action();
446 public unowned string? get_iif();
447 public unowned string? get_oif();
448 public uint32 get_realms();
449 public uint32 get_goto();
453 [CCode (cprefix = "rtnl_qdisc_", cname = "struct rtnl_qdisc", cheader_filename = "netlink/route/qdisc.h")]
456 public int get_ifindex();
457 public uint32 get_handle();
458 public uint32 get_parent();
459 public unowned string? get_kind();
460 public uint64 get_stat();
463 [CCode (cname = "nl_nlmsgtype2str", cheader_filename = "netlink/msg.h")]
464 public unowned string msgType2Str( int type, char[] buf );
465 [CCode (cname = "nl_af2str", cheader_filename = "netlink/addr.h")]
466 public unowned string af2Str( int family, char[] buf );
467 [CCode (cname = "nl_llproto2str", cheader_filename = "netlink/utils.h")]
468 public unowned string llproto2Str( uint proto, char[] buf );
469 [CCode (cname = "rtnl_link_flags2str", cheader_filename = "netlink/route/link.h")]
470 public unowned string linkFlags2Str( uint flags, char[] buf );
471 [CCode (cname = "rtnl_scope2str", cheader_filename = "netlink/route/rtnl.h")]
472 public unowned string routeScope2Str( int scope, char[] buf );
473 [CCode (cname = "nl_rtntype2str", cheader_filename = "netlink/netlink.h")]
474 public unowned string routeType2Str( int type, char[] buf );
475 [CCode (cname = "rtnl_addr_flags2str", cheader_filename = "netlink/netlink.h")]
476 public unowned string addrFlags2Str( int flags, char[] buf );
477 [CCode (cname = "rtnl_neigh_flags2str", cheader_filename = "netlink/netlink.h")]
478 public unowned string neighFlags2Str( uint flags, char[] buf );
479 [CCode (cname = "rtnl_neigh_state2str", cheader_filename = "netlink/netlink.h")]
480 public unowned string neighState2Str( int state, char[] buf );