Logo Search packages:      
Sourcecode: libnl version File versions  Download package

Core Netlink API


Detailed Description

1) Creating the netlink handle
 struct nl_handle *handle;

 // Allocate and initialize a new netlink handle
 handle = nl_handle_new();

 // Are multiple handles being allocated? You have to provide a unique
 // netlink process id and overwrite the default local process id.
 nl_handle_set_pid(handle, MY_UNIQUE_PID);

 // Is this socket used for event processing? You need to disable sequence
 // number checking in order to be able to receive messages not explicitely
 // requested.
 nl_disable_sequence_check(handle);

 // Use nl_handle_get_fd() to fetch the file description, for example to
 // put a socket into non-blocking i/o mode.
 fcntl(nl_handle_get_fd(handle), F_SETFL, O_NONBLOCK);
2) Joining Groups
 // You may join/subscribe to as many groups as you want, don't forget
 // to eventually disable sequence number checking. Note: Joining must
 // be done before connecting/binding the socket.
 nl_join_groups(handle, GROUP_ID1 | GROUP_ID2);
3) Connecting the socket
 // Bind and connect the socket to a protocol, NETLINK_ROUTE in this example.
 nl_connect(handle, NETLINK_ROUTE);
4) Sending data
 // The most rudimentary method is to use nl_sendto() simply pushing
 // a piece of data to the other netlink peer. This method is not
 // recommended.
 const char buf[] = { 0x01, 0x02, 0x03, 0x04 };
 nl_sendto(handle, buf, sizeof(buf));

 // A more comfortable interface is nl_send() taking a pointer to
 // a netlink message.
 struct nl_msg *msg = my_msg_builder();
 nl_send(handle, nlmsg_hdr(msg));

 // nl_sendmsg() provides additional control over the sendmsg() message
 // header in order to allow more specific addressing of multiple peers etc.
 struct msghdr hdr = { ... };
 nl_sendmsg(handle, nlmsg_hdr(msg), &hdr);

 // You're probably too lazy to fill out the netlink pid, sequence number
 // and message flags all the time. nl_send_auto_complete() automatically
 // extends your message header as needed with an appropriate sequence
 // number, the netlink pid stored in the netlink handle and the message
 // flags NLM_F_REQUEST and NLM_F_ACK
 nl_send_auto_complete(handle, nlmsg_hdr(msg));

 // Simple protocols don't require the complex message construction interface
 // and may favour nl_send_simple() to easly send a bunch of payload
 // encapsulated in a netlink message header.
 nl_send_simple(handle, MY_MSG_TYPE, 0, buf, sizeof(buf));
5) Receiving data
 // nl_recv() receives a single message allocating a buffer for the message
 // content and gives back the pointer to you.
 struct sockaddr_nl peer;
 unsigned char *msg;
 nl_recv(handle, &peer, &msg);

 // nl_recvmsgs() receives a bunch of messages until the callback system
 // orders it to state, usually after receving a compolete multi part
 // message series.
 nl_recvmsgs(handle, my_callback_configuration);

 // nl_recvmsgs_def() acts just like nl_recvmsg() but uses the callback
 // configuration stored in the handle.
 nl_recvmsgs_def(handle);

 // In case you want to wait for the ACK to be recieved that you requested
 // with your latest message, you can call nl_wait_for_ack()
 nl_wait_for_ack(handle);
6) Cleaning up
 // Close the socket first to release kernel memory
 nl_close(handle);

 // Finally destroy the netlink handle
 nl_handle_destroy(handle);


Modules

 Attributes
 Callbacks/Customization
 Messages

Classes

struct  sockaddr_nl

Netlink Family Translations

static struct trans_tbl nlfamilies []
char * nl_nlfamily2str (int family, char *buf, size_t size)
int nl_str2nlfamily (const char *name)

Receive

static int ack_wait_handler (struct nl_msg *msg, void *arg)
int nl_recv (struct nl_handle *handle, struct sockaddr_nl *nla, unsigned char **buf)
int nl_recvmsgs (struct nl_handle *handle, struct nl_cb *cb)
int nl_recvmsgs_def (struct nl_handle *handle)
int nl_wait_for_ack (struct nl_handle *handle)

Connection Management

void nl_close (struct nl_handle *handle)
int nl_connect (struct nl_handle *handle, int protocol)

Utilities

void nl_disable_sequence_check (struct nl_handle *handle)
int nl_join_group (struct nl_handle *handle, int group)
void nl_join_groups (struct nl_handle *handle, int groups)
int nl_set_buffer_size (struct nl_handle *handle, int rxbuf, int txbuf)
static int noop_seq_check (struct nl_msg *msg, void *arg)
#define SOL_NETLINK   270

Handle Management

struct nl_handle * nl_handle_alloc (void)
struct nl_handle * nl_handle_alloc_nondefault (enum nl_cb_kind kind)
void nl_handle_destroy (struct nl_handle *handle)

Acccess Functions

struct nl_cb * nl_handle_get_cb (struct nl_handle *handle)
int nl_handle_get_fd (struct nl_handle *handle)
struct sockaddr_nlnl_handle_get_local_addr (struct nl_handle *handle)
struct sockaddr_nlnl_handle_get_peer_addr (struct nl_handle *handle)
pid_t nl_handle_get_pid (struct nl_handle *handle)
void nl_handle_set_pid (struct nl_handle *handle, pid_t pid)

Send

int nl_send (struct nl_handle *handle, struct nl_msg *msg)
int nl_send_auto_complete (struct nl_handle *handle, struct nl_msg *msg)
int nl_send_simple (struct nl_handle *handle, int type, int flags, void *buf, size_t size)
int nl_sendmsg (struct nl_handle *handle, struct nl_msg *msg, struct msghdr *hdr)
int nl_sendto (struct nl_handle *handle, void *buf, size_t size)


Generated by  Doxygen 1.6.0   Back to index