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. |
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 194 of file socket.c.
References nl_cb_alloc().
00195 { 00196 struct nl_cb *cb; 00197 00198 cb = nl_cb_alloc(default_cb); 00199 if (!cb) { 00200 nl_errno(ENOMEM); 00201 return NULL; 00202 } 00203 00204 return __alloc_handle(cb); 00205 }
struct nl_handle* nl_handle_alloc_cb | ( | struct nl_cb * | cb | ) | [read] |
cb | Callback handler |
Definition at line 216 of file socket.c.
00217 { 00218 if (cb == NULL) 00219 BUG(); 00220 00221 return __alloc_handle(nl_cb_get(cb)); 00222 }
void nl_handle_destroy | ( | struct nl_handle * | handle | ) |
handle | Netlink handle. |
Definition at line 228 of file socket.c.
Referenced by nl_cache_mngr_free().
00229 { 00230 if (!handle) 00231 return; 00232 00233 if (handle->h_fd >= 0) 00234 close(handle->h_fd); 00235 00236 if (!(handle->h_flags & NL_OWN_PORT)) 00237 release_local_port(handle->h_local.nl_pid); 00238 00239 nl_cb_put(handle->h_cb); 00240 free(handle); 00241 }
void nl_disable_sequence_check | ( | struct nl_handle * | handle | ) |
handle | Netlink handle. |
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().
00268 { 00269 nl_cb_set(handle->h_cb, NL_CB_SEQ_CHECK, 00270 NL_CB_CUSTOM, noop_seq_check, NULL); 00271 }
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 |
Definition at line 307 of file socket.c.
00308 { 00309 if (port == 0) { 00310 port = generate_local_port(); 00311 handle->h_flags &= ~NL_OWN_PORT; 00312 } else { 00313 if (!(handle->h_flags & NL_OWN_PORT)) 00314 release_local_port(handle->h_local.nl_pid); 00315 handle->h_flags |= NL_OWN_PORT; 00316 } 00317 00318 handle->h_local.nl_pid = port; 00319 }
int nl_socket_add_membership | ( | struct nl_handle * | handle, | |
int | group | |||
) |
handle | Netlink handle | |
group | Group identifier |
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 343 of file socket.c.
Referenced by nl_cache_mngr_add().
00344 { 00345 int err; 00346 00347 if (handle->h_fd == -1) 00348 return nl_error(EBADFD, "Socket not connected"); 00349 00350 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, 00351 &group, sizeof(group)); 00352 if (err < 0) 00353 return nl_error(errno, "setsockopt(NETLINK_ADD_MEMBERSHIP) " 00354 "failed"); 00355 00356 return 0; 00357 }
int nl_socket_drop_membership | ( | struct nl_handle * | handle, | |
int | group | |||
) |
handle | Netlink handle | |
group | Group identifier |
Definition at line 370 of file socket.c.
Referenced by nl_cache_mngr_add().
00371 { 00372 int err; 00373 00374 if (handle->h_fd == -1) 00375 return nl_error(EBADFD, "Socket not connected"); 00376 00377 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, 00378 &group, sizeof(group)); 00379 if (err < 0) 00380 return nl_error(errno, "setsockopt(NETLINK_DROP_MEMBERSHIP) " 00381 "failed"); 00382 00383 return 0; 00384 }
void nl_join_groups | ( | struct nl_handle * | handle, | |
int | groups | |||
) |
handle | Netlink handle. | |
groups | Bitmask of groups to join. |
Definition at line 395 of file socket.c.
int nl_socket_set_nonblocking | ( | struct nl_handle * | handle | ) |
handle | Netlink socket |
Definition at line 436 of file socket.c.
Referenced by nl_cache_mngr_alloc().
00437 { 00438 if (handle->h_fd == -1) 00439 return nl_error(EBADFD, "Socket not connected"); 00440 00441 if (fcntl(handle->h_fd, F_SETFL, O_NONBLOCK) < 0) 00442 return nl_error(errno, "fcntl(F_SETFL, O_NONBLOCK) failed"); 00443 00444 return 0; 00445 }
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 493 of file socket.c.
References nl_cb_set().
Referenced by nl_cache_mngr_alloc().
00496 { 00497 return nl_cb_set(handle->h_cb, type, kind, func, arg); 00498 }
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. |
rxbuf
and txbuf
. Providing a value of 0
assumes a good default value.
Definition at line 520 of file socket.c.
Referenced by nl_connect().
00521 { 00522 int err; 00523 00524 if (rxbuf <= 0) 00525 rxbuf = 32768; 00526 00527 if (txbuf <= 0) 00528 txbuf = 32768; 00529 00530 if (handle->h_fd == -1) 00531 return nl_error(EBADFD, "Socket not connected"); 00532 00533 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_SNDBUF, 00534 &txbuf, sizeof(txbuf)); 00535 if (err < 0) 00536 return nl_error(errno, "setsockopt(SO_SNDBUF) failed"); 00537 00538 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_RCVBUF, 00539 &rxbuf, sizeof(rxbuf)); 00540 if (err < 0) 00541 return nl_error(errno, "setsockopt(SO_RCVBUF) failed"); 00542 00543 handle->h_flags |= NL_SOCK_BUFSIZE_SET; 00544 00545 return 0; 00546 }
int nl_set_passcred | ( | struct nl_handle * | handle, | |
int | state | |||
) |
handle | Netlink handle | |
state | New state (0 - disabled, 1 - enabled) |
Definition at line 555 of file socket.c.
00556 { 00557 int err; 00558 00559 if (handle->h_fd == -1) 00560 return nl_error(EBADFD, "Socket not connected"); 00561 00562 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_PASSCRED, 00563 &state, sizeof(state)); 00564 if (err < 0) 00565 return nl_error(errno, "setsockopt(SO_PASSCRED) failed"); 00566 00567 if (state) 00568 handle->h_flags |= NL_SOCK_PASSCRED; 00569 else 00570 handle->h_flags &= ~NL_SOCK_PASSCRED; 00571 00572 return 0; 00573 }
int nl_socket_recv_pktinfo | ( | struct nl_handle * | handle, | |
int | state | |||
) |
handle | Netlink handle | |
state | New state (0 - disabled, 1 - enabled) |
Definition at line 582 of file socket.c.
00583 { 00584 int err; 00585 00586 if (handle->h_fd == -1) 00587 return nl_error(EBADFD, "Socket not connected"); 00588 00589 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_PKTINFO, 00590 &state, sizeof(state)); 00591 if (err < 0) 00592 return nl_error(errno, "setsockopt(NETLINK_PKTINFO) failed"); 00593 00594 return 0; 00595 }