Handle representing a netlink socket. More...
Allocation | |
| |
struct nl_handle * | nl_handle_alloc (void) |
Allocate new netlink socket handle. | |
struct nl_handle * | nl_handle_alloc_cb (struct nl_cb *cb) |
Allocate new socket handle with custom callbacks. | |
void | nl_handle_destroy (struct nl_handle *handle) |
Destroy netlink handle. | |
Sequence Numbers | |
| |
void | nl_disable_sequence_check (struct nl_handle *handle) |
Disable sequence number checking. | |
unsigned int | nl_socket_use_seq (struct nl_handle *handle) |
Use next sequence number. | |
Source Idenficiation | |
| |
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. | |
Group Subscriptions | |
| |
int | nl_socket_add_membership (struct nl_handle *handle, int group) |
Join a group. | |
int | nl_socket_drop_membership (struct nl_handle *handle, int group) |
Leave a group. | |
void | nl_join_groups (struct nl_handle *handle, int groups) |
Join multicast groups (deprecated). | |
Peer Identfication | |
| |
uint32_t | nl_socket_get_peer_port (struct nl_handle *handle) |
void | nl_socket_set_peer_port (struct nl_handle *handle, uint32_t port) |
File Descriptor | |
| |
int | nl_socket_get_fd (struct nl_handle *handle) |
int | nl_socket_set_nonblocking (struct nl_handle *handle) |
Set file descriptor of socket handle to non-blocking state. | |
void | nl_socket_enable_msg_peek (struct nl_handle *handle) |
Enable use of MSG_PEEK when reading from socket. | |
void | nl_socket_disable_msg_peek (struct nl_handle *handle) |
Disable use of MSG_PEEK when reading from socket. | |
Callback Handler | |
| |
struct nl_cb * | nl_socket_get_cb (struct nl_handle *handle) |
void | nl_socket_set_cb (struct nl_handle *handle, struct nl_cb *cb) |
int | nl_socket_modify_cb (struct nl_handle *handle, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg) |
Modify the callback handler associated to the socket. | |
Utilities | |
| |
int | nl_set_buffer_size (struct nl_handle *handle, int rxbuf, int txbuf) |
Set socket buffer size of netlink handle. | |
int | nl_set_passcred (struct nl_handle *handle, int state) |
Enable/disable credential passing on netlink handle. | |
int | nl_socket_recv_pktinfo (struct nl_handle *handle, int state) |
Enable/disable receival of additional packet information. |
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.
struct nl_handle *handle; // Allocate and initialize a new netlink handle handle = nl_handle_alloc(); // Use nl_socket_get_fd() to fetch the file description, for example to // put a socket into non-blocking i/o mode. fcntl(nl_socket_get_fd(handle), F_SETFL, O_NONBLOCK);
// Event notifications are typically sent to multicast addresses which // represented by groups. Join a group to f.e. receive link notifications. nl_socket_add_membership(handle, RTNLGRP_LINK);
// Finally destroy the netlink handle nl_handle_destroy(handle);
struct nl_handle* nl_handle_alloc | ( | void | ) | [read] |
Definition at line 206 of file socket.c.
References nl_cb_alloc().
00207 { 00208 struct nl_cb *cb; 00209 struct nl_handle *sk; 00210 00211 cb = nl_cb_alloc(default_cb); 00212 if (!cb) { 00213 nl_errno(ENOMEM); 00214 return NULL; 00215 } 00216 00217 /* will increment cb reference count on success */ 00218 sk = __alloc_handle(cb); 00219 00220 nl_cb_put(cb); 00221 00222 return sk; 00223 }
struct nl_handle* nl_handle_alloc_cb | ( | struct nl_cb * | cb | ) | [read] |
cb | Callback handler |
The reference to the callback handler is taken into account automatically, it is released again upon calling nl_handle_destroy().
void nl_handle_destroy | ( | struct nl_handle * | handle | ) |
handle | Netlink handle. |
Definition at line 246 of file socket.c.
Referenced by nl_cache_mngr_free().
void nl_disable_sequence_check | ( | struct nl_handle * | handle | ) |
handle | Netlink handle. |
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.
Definition at line 285 of file socket.c.
References NL_CB_CUSTOM, NL_CB_SEQ_CHECK, and nl_cb_set().
Referenced by nl_cache_mngr_alloc().
00286 { 00287 nl_cb_set(handle->h_cb, NL_CB_SEQ_CHECK, 00288 NL_CB_CUSTOM, noop_seq_check, NULL); 00289 }
unsigned int nl_socket_use_seq | ( | struct nl_handle * | handle | ) |
void nl_socket_set_local_port | ( | struct nl_handle * | handle, | |
uint32_t | port | |||
) |
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 325 of file socket.c.
00326 { 00327 if (port == 0) { 00328 port = generate_local_port(); 00329 handle->h_flags &= ~NL_OWN_PORT; 00330 } else { 00331 if (!(handle->h_flags & NL_OWN_PORT)) 00332 release_local_port(handle->h_local.nl_pid); 00333 handle->h_flags |= NL_OWN_PORT; 00334 } 00335 00336 handle->h_local.nl_pid = port; 00337 }
int nl_socket_add_membership | ( | struct nl_handle * | handle, | |
int | group | |||
) |
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.
Definition at line 361 of file socket.c.
Referenced by nl_cache_mngr_add().
00362 { 00363 int err; 00364 00365 if (handle->h_fd == -1) 00366 return nl_error(EBADFD, "Socket not connected"); 00367 00368 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, 00369 &group, sizeof(group)); 00370 if (err < 0) 00371 return nl_error(errno, "setsockopt(NETLINK_ADD_MEMBERSHIP) " 00372 "failed"); 00373 00374 return 0; 00375 }
int nl_socket_drop_membership | ( | struct nl_handle * | handle, | |
int | group | |||
) |
handle | Netlink handle | |
group | Group identifier |
Leaves the specified group using the modern socket option which is available since kernel version 2.6.14.
Definition at line 388 of file socket.c.
Referenced by nl_cache_mngr_add().
00389 { 00390 int err; 00391 00392 if (handle->h_fd == -1) 00393 return nl_error(EBADFD, "Socket not connected"); 00394 00395 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, 00396 &group, sizeof(group)); 00397 if (err < 0) 00398 return nl_error(errno, "setsockopt(NETLINK_DROP_MEMBERSHIP) " 00399 "failed"); 00400 00401 return 0; 00402 }
void nl_join_groups | ( | struct nl_handle * | handle, | |
int | groups | |||
) |
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.
int nl_socket_set_nonblocking | ( | struct nl_handle * | handle | ) |
handle | Netlink socket |
Definition at line 454 of file socket.c.
Referenced by nl_cache_mngr_alloc().
void nl_socket_enable_msg_peek | ( | struct nl_handle * | handle | ) |
void nl_socket_disable_msg_peek | ( | struct nl_handle * | handle | ) |
int nl_socket_modify_cb | ( | struct nl_handle * | handle, | |
enum nl_cb_type | type, | |||
enum nl_cb_kind | kind, | |||
nl_recvmsg_msg_cb_t | func, | |||
void * | arg | |||
) |
handle | netlink handle | |
type | which type callback to set | |
kind | kind of callback | |
func | callback function | |
arg | argument to be passwd to callback function |
Definition at line 514 of file socket.c.
References nl_cb_set().
Referenced by nl_cache_mngr_alloc().
00517 { 00518 return nl_cb_set(handle->h_cb, type, kind, func, arg); 00519 }
int nl_set_buffer_size | ( | struct nl_handle * | handle, | |
int | rxbuf, | |||
int | txbuf | |||
) |
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.
Definition at line 541 of file socket.c.
Referenced by nl_connect().
00542 { 00543 int err; 00544 00545 if (rxbuf <= 0) 00546 rxbuf = 32768; 00547 00548 if (txbuf <= 0) 00549 txbuf = 32768; 00550 00551 if (handle->h_fd == -1) 00552 return nl_error(EBADFD, "Socket not connected"); 00553 00554 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_SNDBUF, 00555 &txbuf, sizeof(txbuf)); 00556 if (err < 0) 00557 return nl_error(errno, "setsockopt(SO_SNDBUF) failed"); 00558 00559 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_RCVBUF, 00560 &rxbuf, sizeof(rxbuf)); 00561 if (err < 0) 00562 return nl_error(errno, "setsockopt(SO_RCVBUF) failed"); 00563 00564 handle->h_flags |= NL_SOCK_BUFSIZE_SET; 00565 00566 return 0; 00567 }
int nl_set_passcred | ( | struct nl_handle * | handle, | |
int | state | |||
) |
handle | Netlink handle | |
state | New state (0 - disabled, 1 - enabled) |
Definition at line 576 of file socket.c.
00577 { 00578 int err; 00579 00580 if (handle->h_fd == -1) 00581 return nl_error(EBADFD, "Socket not connected"); 00582 00583 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_PASSCRED, 00584 &state, sizeof(state)); 00585 if (err < 0) 00586 return nl_error(errno, "setsockopt(SO_PASSCRED) failed"); 00587 00588 if (state) 00589 handle->h_flags |= NL_SOCK_PASSCRED; 00590 else 00591 handle->h_flags &= ~NL_SOCK_PASSCRED; 00592 00593 return 0; 00594 }
int nl_socket_recv_pktinfo | ( | struct nl_handle * | handle, | |
int | state | |||
) |
handle | Netlink handle | |
state | New state (0 - disabled, 1 - enabled) |
Definition at line 603 of file socket.c.
00604 { 00605 int err; 00606 00607 if (handle->h_fd == -1) 00608 return nl_error(EBADFD, "Socket not connected"); 00609 00610 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_PKTINFO, 00611 &state, sizeof(state)); 00612 if (err < 0) 00613 return nl_error(errno, "setsockopt(NETLINK_PKTINFO) failed"); 00614 00615 return 0; 00616 }