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);
Allocation | |
nl_handle * | nl_handle_alloc (void) |
Allocate new netlink socket handle. | |
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 | |
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* nl_handle_alloc | ( | void | ) |
Definition at line 206 of file socket.c.
References nl_cb_alloc().
00207 { 00208 struct nl_cb *cb; 00209 00210 cb = nl_cb_alloc(default_cb); 00211 if (!cb) { 00212 nl_errno(ENOMEM); 00213 return NULL; 00214 } 00215 00216 return __alloc_handle(cb); 00217 }
struct nl_handle* nl_handle_alloc_cb | ( | struct nl_cb * | cb | ) |
cb | Callback handler |
Definition at line 228 of file socket.c.
References nl_cb_get().
00229 { 00230 if (cb == NULL) 00231 BUG(); 00232 00233 return __alloc_handle(nl_cb_get(cb)); 00234 }
void nl_handle_destroy | ( | struct nl_handle * | handle | ) |
handle | Netlink handle. |
Definition at line 240 of file socket.c.
References nl_cb_put().
Referenced by nl_cache_mngr_free().
00241 { 00242 if (!handle) 00243 return; 00244 00245 if (handle->h_fd >= 0) 00246 close(handle->h_fd); 00247 00248 if (!(handle->h_flags & NL_OWN_PORT)) 00249 release_local_port(handle->h_local.nl_pid); 00250 00251 nl_cb_put(handle->h_cb); 00252 free(handle); 00253 }
void nl_disable_sequence_check | ( | struct nl_handle * | handle | ) |
handle | Netlink handle. |
Definition at line 279 of file socket.c.
References NL_CB_CUSTOM, NL_CB_SEQ_CHECK, and nl_cb_set().
Referenced by nl_cache_mngr_alloc().
00280 { 00281 nl_cb_set(handle->h_cb, NL_CB_SEQ_CHECK, 00282 NL_CB_CUSTOM, noop_seq_check, NULL); 00283 }
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 319 of file socket.c.
00320 { 00321 if (port == 0) { 00322 port = generate_local_port(); 00323 handle->h_flags &= ~NL_OWN_PORT; 00324 } else { 00325 if (!(handle->h_flags & NL_OWN_PORT)) 00326 release_local_port(handle->h_local.nl_pid); 00327 handle->h_flags |= NL_OWN_PORT; 00328 } 00329 00330 handle->h_local.nl_pid = port; 00331 }
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 355 of file socket.c.
00356 { 00357 int err; 00358 00359 if (handle->h_fd == -1) 00360 return nl_error(EBADFD, "Socket not connected"); 00361 00362 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, 00363 &group, sizeof(group)); 00364 if (err < 0) 00365 return nl_error(errno, "setsockopt(NETLINK_ADD_MEMBERSHIP) " 00366 "failed"); 00367 00368 return 0; 00369 }
int nl_socket_drop_membership | ( | struct nl_handle * | handle, | |
int | group | |||
) |
handle | Netlink handle | |
group | Group identifier |
Definition at line 382 of file socket.c.
00383 { 00384 int err; 00385 00386 if (handle->h_fd == -1) 00387 return nl_error(EBADFD, "Socket not connected"); 00388 00389 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, 00390 &group, sizeof(group)); 00391 if (err < 0) 00392 return nl_error(errno, "setsockopt(NETLINK_DROP_MEMBERSHIP) " 00393 "failed"); 00394 00395 return 0; 00396 }
void nl_join_groups | ( | struct nl_handle * | handle, | |
int | groups | |||
) |
handle | Netlink handle. | |
groups | Bitmask of groups to join. |
Definition at line 407 of file socket.c.
int nl_socket_set_nonblocking | ( | struct nl_handle * | handle | ) |
handle | Netlink socket |
Definition at line 448 of file socket.c.
Referenced by nl_cache_mngr_alloc().
00449 { 00450 if (handle->h_fd == -1) 00451 return nl_error(EBADFD, "Socket not connected"); 00452 00453 if (fcntl(handle->h_fd, F_SETFL, O_NONBLOCK) < 0) 00454 return nl_error(errno, "fcntl(F_SETFL, O_NONBLOCK) failed"); 00455 00456 return 0; 00457 }
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 505 of file socket.c.
References nl_cb_set().
Referenced by nl_cache_mngr_alloc().
00508 { 00509 return nl_cb_set(handle->h_cb, type, kind, func, arg); 00510 }
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 532 of file socket.c.
Referenced by nl_connect().
00533 { 00534 int err; 00535 00536 if (rxbuf <= 0) 00537 rxbuf = 32768; 00538 00539 if (txbuf <= 0) 00540 txbuf = 32768; 00541 00542 if (handle->h_fd == -1) 00543 return nl_error(EBADFD, "Socket not connected"); 00544 00545 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_SNDBUF, 00546 &txbuf, sizeof(txbuf)); 00547 if (err < 0) 00548 return nl_error(errno, "setsockopt(SO_SNDBUF) failed"); 00549 00550 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_RCVBUF, 00551 &rxbuf, sizeof(rxbuf)); 00552 if (err < 0) 00553 return nl_error(errno, "setsockopt(SO_RCVBUF) failed"); 00554 00555 handle->h_flags |= NL_SOCK_BUFSIZE_SET; 00556 00557 return 0; 00558 }
int nl_set_passcred | ( | struct nl_handle * | handle, | |
int | state | |||
) |
handle | Netlink handle | |
state | New state (0 - disabled, 1 - enabled) |
Definition at line 567 of file socket.c.
00568 { 00569 int err; 00570 00571 if (handle->h_fd == -1) 00572 return nl_error(EBADFD, "Socket not connected"); 00573 00574 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_PASSCRED, 00575 &state, sizeof(state)); 00576 if (err < 0) 00577 return nl_error(errno, "setsockopt(SO_PASSCRED) failed"); 00578 00579 if (state) 00580 handle->h_flags |= NL_SOCK_PASSCRED; 00581 else 00582 handle->h_flags &= ~NL_SOCK_PASSCRED; 00583 00584 return 0; 00585 }
int nl_socket_recv_pktinfo | ( | struct nl_handle * | handle, | |
int | state | |||
) |
handle | Netlink handle | |
state | New state (0 - disabled, 1 - enabled) |
Definition at line 594 of file socket.c.
00595 { 00596 int err; 00597 00598 if (handle->h_fd == -1) 00599 return nl_error(EBADFD, "Socket not connected"); 00600 00601 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_PKTINFO, 00602 &state, sizeof(state)); 00603 if (err < 0) 00604 return nl_error(errno, "setsockopt(NETLINK_PKTINFO) failed"); 00605 00606 return 0; 00607 }