libnl 3.7.0

Representation of a netlink socket. More...

Allocation

struct nl_sock * nl_socket_alloc (void)
 Allocate new netlink socket. More...
 
struct nl_sock * nl_socket_alloc_cb (struct nl_cb *cb)
 Allocate new socket with custom callbacks. More...
 
void nl_socket_free (struct nl_sock *sk)
 Free a netlink socket. More...
 

Sequence Numbers

void nl_socket_disable_seq_check (struct nl_sock *sk)
 Disable sequence number checking. More...
 
unsigned int nl_socket_use_seq (struct nl_sock *sk)
 Use next sequence number. More...
 
void nl_socket_disable_auto_ack (struct nl_sock *sk)
 Disable automatic request for ACK. More...
 
void nl_socket_enable_auto_ack (struct nl_sock *sk)
 Enable automatic request for ACK (default) More...
 

Source Idenficiation

uint32_t nl_socket_get_local_port (const struct nl_sock *sk)
 
void nl_socket_set_local_port (struct nl_sock *sk, uint32_t port)
 Set local port of socket. More...
 

Group Subscriptions

int nl_socket_add_memberships (struct nl_sock *sk, int group,...)
 Join groups. More...
 
int nl_socket_add_membership (struct nl_sock *sk, int group)
 
int nl_socket_drop_memberships (struct nl_sock *sk, int group,...)
 Leave groups. More...
 
int nl_socket_drop_membership (struct nl_sock *sk, int group)
 
void nl_join_groups (struct nl_sock *sk, int groups)
 Join multicast groups (deprecated) More...
 

Peer Identfication

uint32_t nl_socket_get_peer_port (const struct nl_sock *sk)
 
void nl_socket_set_peer_port (struct nl_sock *sk, uint32_t port)
 
uint32_t nl_socket_get_peer_groups (const struct nl_sock *sk)
 
void nl_socket_set_peer_groups (struct nl_sock *sk, uint32_t groups)
 

File Descriptor

int nl_socket_get_fd (const struct nl_sock *sk)
 Return the file descriptor of the backing socket. More...
 
int nl_socket_set_fd (struct nl_sock *sk, int protocol, int fd)
 Set the socket file descriptor externally which initializes the socket similar to nl_connect(). More...
 
int nl_socket_set_nonblocking (const struct nl_sock *sk)
 Set file descriptor of socket to non-blocking state. More...
 
void nl_socket_enable_msg_peek (struct nl_sock *sk)
 Enable use of MSG_PEEK when reading from socket. More...
 
void nl_socket_disable_msg_peek (struct nl_sock *sk)
 Disable use of MSG_PEEK when reading from socket. More...
 

Callback Handler

struct nl_cb * nl_socket_get_cb (const struct nl_sock *sk)
 
void nl_socket_set_cb (struct nl_sock *sk, struct nl_cb *cb)
 
int nl_socket_modify_cb (struct nl_sock *sk, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
 Modify the callback handler associated with the socket. More...
 
int nl_socket_modify_err_cb (struct nl_sock *sk, enum nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg)
 Modify the error callback handler associated with the socket. More...
 

Utilities

int nl_socket_set_buffer_size (struct nl_sock *sk, int rxbuf, int txbuf)
 Set socket buffer size of netlink socket. More...
 
int nl_socket_set_msg_buf_size (struct nl_sock *sk, size_t bufsize)
 Set default message buffer size of netlink socket. More...
 
size_t nl_socket_get_msg_buf_size (struct nl_sock *sk)
 Get default message buffer size of netlink socket. More...
 
int nl_socket_set_passcred (struct nl_sock *sk, int state)
 Enable/disable credential passing on netlink socket. More...
 
int nl_socket_recv_pktinfo (struct nl_sock *sk, int state)
 Enable/disable receival of additional packet information. More...
 

Detailed Description

Representation of a netlink socket.

Related sections in the development guide:

Header

#include <netlink/socket.h>

Function Documentation

◆ nl_socket_alloc()

struct nl_sock * nl_socket_alloc ( void  )

Allocate new netlink socket.

Returns
Newly allocated netlink socket or NULL.

Definition at line 200 of file socket.c.

References nl_cb_alloc().

Referenced by nl_cache_mngr_alloc().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nl_socket_alloc_cb()

struct nl_sock * nl_socket_alloc_cb ( struct nl_cb *  cb)

Allocate new socket with custom callbacks.

Parameters
cbCallback handler

The reference to the callback handler is taken into account automatically, it is released again upon calling nl_socket_free().

Returns
Newly allocted socket handle or NULL.

Definition at line 226 of file socket.c.

◆ nl_socket_free()

void nl_socket_free ( struct nl_sock *  sk)

Free a netlink socket.

Parameters
skNetlink socket.

Definition at line 238 of file socket.c.

Referenced by nl_cache_mngr_alloc(), and nl_cache_mngr_free().

+ Here is the caller graph for this function:

◆ nl_socket_disable_seq_check()

void nl_socket_disable_seq_check ( struct nl_sock *  sk)

Disable sequence number checking.

Parameters
skNetlink socket.

Disables checking of sequence numbers on the netlink socket This is required to allow messages to be processed which were not requested by a preceding request message, e.g. netlink events.

Note
This function modifies the NL_CB_SEQ_CHECK configuration in the callback handle associated with the socket.

Definition at line 277 of file socket.c.

References NL_CB_CUSTOM, NL_CB_SEQ_CHECK, and nl_cb_set().

Referenced by nl_cache_mngr_alloc().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nl_socket_use_seq()

unsigned int nl_socket_use_seq ( struct nl_sock *  sk)

Use next sequence number.

Parameters
skNetlink socket.

Uses the next available sequence number and increases the counter by one for subsequent calls.

Returns
Unique serial sequence number

Definition at line 292 of file socket.c.

◆ nl_socket_disable_auto_ack()

void nl_socket_disable_auto_ack ( struct nl_sock *  sk)

Disable automatic request for ACK.

Parameters
skNetlink socket.

The default behaviour of a socket is to request an ACK for each message sent to allow for the caller to synchronize to the completion of the netlink operation. This function disables this behaviour and will result in requests being sent which will not have the NLM_F_ACK flag set automatically. However, it is still possible for the caller to set the NLM_F_ACK flag explicitely.

Definition at line 309 of file socket.c.

◆ nl_socket_enable_auto_ack()

void nl_socket_enable_auto_ack ( struct nl_sock *  sk)

Enable automatic request for ACK (default)

Parameters
skNetlink socket.
See also
nl_socket_disable_auto_ack

Definition at line 319 of file socket.c.

◆ nl_socket_get_local_port()

uint32_t nl_socket_get_local_port ( const struct nl_sock *  sk)

Definition at line 359 of file socket.c.

◆ nl_socket_set_local_port()

void nl_socket_set_local_port ( struct nl_sock *  sk,
uint32_t  port 
)

Set local port of socket.

Parameters
skNetlink socket.
portLocal port identifier

Assigns a local port identifier to the socket.

If port is 0, the port is reset to 'unspecified' as it is after newly calling nl_socket_alloc(). Unspecified means, that the port will be generated automatically later on first use (either on nl_socket_get_local_port() or nl_connect()).

Definition at line 398 of file socket.c.

Referenced by nl_connect(), and nl_socket_set_fd().

+ Here is the caller graph for this function:

◆ nl_socket_add_memberships()

int nl_socket_add_memberships ( struct nl_sock *  sk,
int  group,
  ... 
)

Join groups.

Parameters
skNetlink socket
groupGroup identifier

Joins the specified groups using the modern socket option which is available since kernel version 2.6.14. It allows joining an almost arbitary number of groups without limitation. The list of groups has to be terminated by 0 (NFNLGRP_NONE).

Make sure to use the correct group definitions as the older bitmask definitions for nl_join_groups() are likely to still be present for backward compatibility reasons.

Returns
0 on success or a negative error code.

Definition at line 429 of file socket.c.

◆ nl_socket_add_membership()

int nl_socket_add_membership ( struct nl_sock *  sk,
int  group 
)

Definition at line 462 of file socket.c.

◆ nl_socket_drop_memberships()

int nl_socket_drop_memberships ( struct nl_sock *  sk,
int  group,
  ... 
)

Leave groups.

Parameters
skNetlink socket
groupGroup identifier

Leaves the specified groups using the modern socket option which is available since kernel version 2.6.14. The list of groups has to terminated by 0 (NFNLGRP_NONE).

See also
nl_socket_add_membership
Returns
0 on success or a negative error code.

Definition at line 479 of file socket.c.

◆ nl_socket_drop_membership()

int nl_socket_drop_membership ( struct nl_sock *  sk,
int  group 
)

Definition at line 512 of file socket.c.

◆ nl_join_groups()

void nl_join_groups ( struct nl_sock *  sk,
int  groups 
)

Join multicast groups (deprecated)

Parameters
skNetlink socket.
groupsBitmask of groups to join.

This function defines the old way of joining multicast group which has to be done prior to calling nl_connect(). It works on any kernel version but is very limited as only 32 groups can be joined.

Definition at line 527 of file socket.c.

◆ nl_socket_get_peer_port()

uint32_t nl_socket_get_peer_port ( const struct nl_sock *  sk)

Definition at line 540 of file socket.c.

◆ nl_socket_set_peer_port()

void nl_socket_set_peer_port ( struct nl_sock *  sk,
uint32_t  port 
)

Definition at line 545 of file socket.c.

◆ nl_socket_get_peer_groups()

uint32_t nl_socket_get_peer_groups ( const struct nl_sock *  sk)

Definition at line 550 of file socket.c.

◆ nl_socket_set_peer_groups()

void nl_socket_set_peer_groups ( struct nl_sock *  sk,
uint32_t  groups 
)

Definition at line 555 of file socket.c.

◆ nl_socket_get_fd()

int nl_socket_get_fd ( const struct nl_sock *  sk)

Return the file descriptor of the backing socket.

Parameters
skNetlink socket

Only valid after calling nl_connect() to create and bind the respective socket.

Returns
File descriptor or -1 if not available.

Definition at line 578 of file socket.c.

Referenced by nl_cache_mngr_data_ready(), nl_cache_mngr_get_fd(), and nl_cache_mngr_poll().

+ Here is the caller graph for this function:

◆ nl_socket_set_fd()

int nl_socket_set_fd ( struct nl_sock *  sk,
int  protocol,
int  fd 
)

Set the socket file descriptor externally which initializes the socket similar to nl_connect().

Parameters
skNetlink socket (required)
protocolThe socket protocol (optional). Linux 2.6.32 supports the socket option SO_PROTOCOL. In this case, you can set protocol to a negative value and let it autodetect. If you set it to a non-negative value, the detected protocol must match the one provided. To support older kernels, you must specify the protocol.
fdSocket file descriptor to use (required)

Set the socket file descriptor. @fd must be valid and bind'ed.

This is an alternative to nl_connect(). nl_connect() creates, binds and sets the socket. With this function you can set the socket to an externally created file descriptor.

See also
nl_connect()
Returns
0 on success or a negative error code. On error, @fd is not closed but possibly unusable.
Return values
-NLE_BAD_SOCKNetlink socket is already connected
-NLE_INVALSocket is of unexpected type

Definition at line 610 of file socket.c.

References nl_socket_set_local_port().

+ Here is the call graph for this function:

◆ nl_socket_set_nonblocking()

int nl_socket_set_nonblocking ( const struct nl_sock *  sk)

Set file descriptor of socket to non-blocking state.

Parameters
skNetlink socket.
Returns
0 on success or a negative error code.

Definition at line 697 of file socket.c.

Referenced by nl_cache_mngr_alloc().

+ Here is the caller graph for this function:

◆ nl_socket_enable_msg_peek()

void nl_socket_enable_msg_peek ( struct nl_sock *  sk)

Enable use of MSG_PEEK when reading from socket.

Parameters
skNetlink socket.

See also NL_CAPABILITY_NL_RECVMSGS_PEEK_BY_DEFAULT capability

Definition at line 717 of file socket.c.

◆ nl_socket_disable_msg_peek()

void nl_socket_disable_msg_peek ( struct nl_sock *  sk)

Disable use of MSG_PEEK when reading from socket.

Parameters
skNetlink socket.

See also NL_CAPABILITY_NL_RECVMSGS_PEEK_BY_DEFAULT capability

Definition at line 728 of file socket.c.

◆ nl_socket_get_cb()

struct nl_cb * nl_socket_get_cb ( const struct nl_sock *  sk)

Definition at line 741 of file socket.c.

◆ nl_socket_set_cb()

void nl_socket_set_cb ( struct nl_sock *  sk,
struct nl_cb *  cb 
)

Definition at line 746 of file socket.c.

◆ nl_socket_modify_cb()

int nl_socket_modify_cb ( struct nl_sock *  sk,
enum nl_cb_type  type,
enum nl_cb_kind  kind,
nl_recvmsg_msg_cb_t  func,
void *  arg 
)

Modify the callback handler associated with the socket.

Parameters
skNetlink socket.
typewhich type callback to set
kindkind of callback
funccallback function
argargument to be passed to callback function
See also
nl_cb_set

Definition at line 765 of file socket.c.

References nl_cb_set().

+ Here is the call graph for this function:

◆ nl_socket_modify_err_cb()

int nl_socket_modify_err_cb ( struct nl_sock *  sk,
enum nl_cb_kind  kind,
nl_recvmsg_err_cb_t  func,
void *  arg 
)

Modify the error callback handler associated with the socket.

Parameters
skNetlink socket.
kindkind of callback
funccallback function
argargument to be passed to callback function
See also
nl_cb_err

Definition at line 781 of file socket.c.

References nl_cb_err().

+ Here is the call graph for this function:

◆ nl_socket_set_buffer_size()

int nl_socket_set_buffer_size ( struct nl_sock *  sk,
int  rxbuf,
int  txbuf 
)

Set socket buffer size of netlink socket.

Parameters
skNetlink socket.
rxbufNew receive socket buffer size in bytes.
txbufNew transmit socket buffer size in bytes.

Sets the socket buffer size of a netlink socket to the specified values rxbuf and txbuf. Providing a value of 0 assumes a good default value.

Note
It is not required to call this function prior to nl_connect().
Returns
0 on success or a negative error code.

Definition at line 807 of file socket.c.

Referenced by nl_connect().

+ Here is the caller graph for this function:

◆ nl_socket_set_msg_buf_size()

int nl_socket_set_msg_buf_size ( struct nl_sock *  sk,
size_t  bufsize 
)

Set default message buffer size of netlink socket.

Parameters
skNetlink socket.
bufsizeDefault message buffer size in bytes.

Sets the default message buffer size to the specified length in bytes. The default message buffer size limits the maximum message size the socket will be able to receive. It is generally recommneded to specify a buffer size no less than the size of a memory page.

Setting the @bufsize to zero means to use a default of 4 times getpagesize().

When MSG_PEEK is enabled, the buffer size is used for the initial choice of the buffer while peeking. It still makes sense to choose an optimal value to avoid realloc().

When MSG_PEEK is disabled, the buffer size is important because a too small size will lead to failure of receiving the message via nl_recvmsgs().

By default, MSG_PEEK is enabled unless the user calls either nl_socket_disable_msg_peek()/ nl_socket_enable_msg_peek() or sets the message buffer size to a positive value. See capability NL_CAPABILITY_NL_RECVMSGS_PEEK_BY_DEFAULT for that.

Returns
0 on success or a negative error code.

Definition at line 864 of file socket.c.

◆ nl_socket_get_msg_buf_size()

size_t nl_socket_get_msg_buf_size ( struct nl_sock *  sk)

Get default message buffer size of netlink socket.

Parameters
skNetlink socket.
Returns
Size of default message buffer.

Definition at line 877 of file socket.c.

◆ nl_socket_set_passcred()

int nl_socket_set_passcred ( struct nl_sock *  sk,
int  state 
)

Enable/disable credential passing on netlink socket.

Parameters
skNetlink socket.
stateNew state (0 - disabled, 1 - enabled)
Returns
0 on success or a negative error code

Definition at line 889 of file socket.c.

◆ nl_socket_recv_pktinfo()

int nl_socket_recv_pktinfo ( struct nl_sock *  sk,
int  state 
)

Enable/disable receival of additional packet information.

Parameters
skNetlink socket.
stateNew state (0 - disabled, 1 - enabled)
Returns
0 on success or a negative error code

Definition at line 919 of file socket.c.