Handle representing a netlink socket.
More...
|
uint32_t | nl_socket_get_local_port (struct nl_handle *handle) |
|
void | nl_socket_set_local_port (struct nl_handle *handle, uint32_t port) |
| Set local port of socket. More...
|
|
|
uint32_t | nl_socket_get_peer_port (struct nl_handle *handle) |
|
void | nl_socket_set_peer_port (struct nl_handle *handle, uint32_t port) |
|
The socket is represented in a structure called the netlink handle, besides the socket, it stores various settings and values related to the socket. Every socket handle has a mandatory association with a set of callbacks which can be used to modify the behaviour when sending/receiving data from the socket.
- Socket Attributes
- Local Port: The local port is a netlink port identifying the local endpoint. It is used as source address for outgoing messages and will be addressed in replies. It must therefore be unique among all userspace applications. When the socket handle is allocated, a unique port number is generated automatically in the form of 22 bits Process Identifier + 10 bits Arbitary Number. Therefore the library is capable of generating 1024 unique local port numbers for every process. If more sockets are required, the application has to manage port numbers itself using nl_socket_set_local_port().
- Group Subscriptions: A socket can subscribe to any number of multicast groups. It will then receive a copy of all messages sent to one of the groups. This method is mainly used for event notification. Prior to kernel 2.6.14, the group subscription was done via bitmask which limited to a total number of groups of 32. With 2.6.14 a new method was added based on continous identifiers which supports an arbitary number of groups. Both methods are supported, see nl_join_groups() respectively nl_socket_add_membership() and nl_socket_drop_membership().
- Peer Port: The peer port is a netlink port identifying the peer's endpoint. If no peer port is specified, the kernel will try to autobind to a socket of the specified netlink family automatically. This is very common as typically only one listening socket exists on the kernel side. The peer port can be modified using nl_socket_set_peer_port().
- Peer Groups:
- File Descriptor: The file descriptor of the socket, it can be accessed via nl_socket_get_fd() to change socket options or monitor activity using poll()/select().
- Protocol: Once connected, the socket is bound to stick to one netlink family. This field is invisible, it is maintained automatically. (See nl_connect())
- Next Sequence Number: Next available sequence number to be used for the next message being sent out. (Initial value: UNIX time when the socket was allocated.) Sequence numbers can be used via nl_socket_use_seq().
- Expected Sequence Number: Expected sequence number in the next message received from the socket. (Initial value: Equal to next sequence number.)
- Callbacks Configuration:
- 1) Creating the netlink handle
struct nl_handle *handle;
fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
- 2) Group Subscriptions
- 6) Cleaning up
-
struct nl_handle* nl_handle_alloc |
( |
void |
| ) |
|
- Returns
- Newly allocated netlink socket handle or NULL.
Definition at line 194 of file socket.c.
References nl_cb_alloc().
struct nl_handle* nl_handle_alloc_cb |
( |
struct nl_cb * |
cb | ) |
|
- Parameters
-
The reference to the callback handler is taken into account automatically, it is released again upon calling nl_handle_destroy().
- Returns
- Newly allocted socket handle or NULL.
Definition at line 216 of file socket.c.
void nl_handle_destroy |
( |
struct nl_handle * |
handle | ) |
|
void nl_disable_sequence_check |
( |
struct nl_handle * |
handle | ) |
|
- Parameters
-
Disables checking of sequence numbers on the netlink handle. 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 267 of file socket.c.
References NL_CB_CUSTOM, NL_CB_SEQ_CHECK, and nl_cb_set().
Referenced by nl_cache_mngr_alloc().
unsigned int nl_socket_use_seq |
( |
struct nl_handle * |
handle | ) |
|
- Parameters
-
Uses the next available sequence number and increases the counter by one for subsequent calls.
- Returns
- Unique serial sequence number
Definition at line 282 of file socket.c.
void nl_socket_set_local_port |
( |
struct nl_handle * |
handle, |
|
|
uint32_t |
port |
|
) |
| |
- Parameters
-
handle | Netlink handle |
port | Local port identifier |
Assigns a local port identifier to the socket. If port is 0 a unique port identifier will be generated automatically.
Definition at line 307 of file socket.c.
int nl_socket_add_membership |
( |
struct nl_handle * |
handle, |
|
|
int |
group |
|
) |
| |
- Parameters
-
handle | Netlink handle |
group | Group identifier |
Joins the specified group 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.
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 sucess or a negative error code.
Definition at line 343 of file socket.c.
Referenced by nl_cache_mngr_add().
int nl_socket_drop_membership |
( |
struct nl_handle * |
handle, |
|
|
int |
group |
|
) |
| |
- Parameters
-
handle | Netlink handle |
group | Group identifier |
Leaves the specified group using the modern socket option which is available since kernel version 2.6.14.
- See also
- nl_socket_add_membership
- Returns
- 0 on success or a negative error code.
Definition at line 370 of file socket.c.
Referenced by nl_cache_mngr_add().
void nl_join_groups |
( |
struct nl_handle * |
handle, |
|
|
int |
groups |
|
) |
| |
- Parameters
-
handle | Netlink handle. |
groups | Bitmask 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 395 of file socket.c.
int nl_socket_set_nonblocking |
( |
struct nl_handle * |
handle | ) |
|
void nl_socket_enable_msg_peek |
( |
struct nl_handle * |
handle | ) |
|
void nl_socket_disable_msg_peek |
( |
struct nl_handle * |
handle | ) |
|
- Parameters
-
handle | netlink handle |
type | which type callback to set |
kind | kind of callback |
func | callback function |
arg | argument to be passwd to callback function |
- See also
- nl_cb_set
Definition at line 493 of file socket.c.
References nl_cb_set().
Referenced by nl_cache_mngr_alloc().
int nl_set_buffer_size |
( |
struct nl_handle * |
handle, |
|
|
int |
rxbuf, |
|
|
int |
txbuf |
|
) |
| |
- Parameters
-
handle | Netlink handle. |
rxbuf | New receive socket buffer size in bytes. |
txbuf | New transmit socket buffer size in bytes. |
Sets the socket buffer size of a netlink handle 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 sucess or a negative error code.
Definition at line 520 of file socket.c.
Referenced by nl_connect().
int nl_set_passcred |
( |
struct nl_handle * |
handle, |
|
|
int |
state |
|
) |
| |
- Parameters
-
handle | Netlink handle |
state | New state (0 - disabled, 1 - enabled) |
- Returns
- 0 on success or a negative error code
Definition at line 555 of file socket.c.
int nl_socket_recv_pktinfo |
( |
struct nl_handle * |
handle, |
|
|
int |
state |
|
) |
| |
- Parameters
-
handle | Netlink handle |
state | New state (0 - disabled, 1 - enabled) |
- Returns
- 0 on success or a negative error code
Definition at line 582 of file socket.c.