KNET_HAND...DD_DATAFD(3)
π KNET_HAND...DD_DATAFD π
okay bestie, welcome to the Kronosnet Programmer's Manual β¨ this one's called knet_handle_add_datafd and it's literally the "hey knet, here's a file descriptor, go talk to people" function. she's iconic, she's essential, let's get into it π«Ά
πΈ NAME πΈ
knet_handle_add_datafd β Install a file descriptor for communication.
β¨ SYNOPSIS β¨
#include <libknet.h>
int knet_handle_add_datafd(
knet_handle_t knet_h,
int *datafd,
int8_t *channel
);
π¦ DESCRIPTION π¦
knet_handle_add_datafd
IMPORTANT, no cap: to add a datafd to knet,knet_handle_enable_sock_notifyMUST be set and it has to be able to handle both errors (-1) and 0 bytes read / write from the provideddatafd. On read error (< 0) fromdatafd, the socket is automatically removed from polling so we don't spin on dead sockets (we're not doing that today π). It is safe to callknet_handle_remove_datafdeven on sockets that have been removed. periodt.
Now the parameters, one by one, iykyk:
knet_hβ pointer toknet_handle_t. she's the main character, your handle.*datafdβ read/write file descriptor. knet will read data here to send to the other hosts and will write data received from the network. Each data packet can be of max sizeKNET_MAX_PACKET_SIZE! Applications usingknet_send/knet_recvwill receive a proper error if the packet size is not within boundaries. Applications using their own functions to write to thedatafdshould NOT write more thanKNET_MAX_PACKET_SIZE. (that's the vibe check, respect the limit fr fr)
datafd can be 0, and knet_handle_add_datafd will create an internal SOCK_DGRAM AF_UNIX socketpair for communication (valid for both testing and production, bestie β yes, production too π
). SOCK_DGRAM provides atomic message boundaries, ensuring each write becomes one distinct read on the other end. A value higher than 0 provides a user-managed file descriptor. A negative number will return an error. On exit knet_handle_free will cleanup socketpairs created by knet_handle_add_datafd, but will not close user-provided fds. (she's not your mom, she's not cleaning up your fds)
SUPPORTED file descriptor types (user-provided):
- TAP devices (e.g., from libnozzle) β PRIMARY use case π
- Regular sockets (must be bound or connected)
- Character devices (opened with
O_RDWR) - Any bidirectional file descriptor
UNSUPPORTED file descriptor types:
- User-created socketpairs β will NOT work (knet needs both ends but API only accepts one fd). Use
datafd=0for knet-managed pairs. - Pipes (all types) β each pipe fd only supports unidirectional data flow, knet requires bidirectional I/O on the same fd
- Unconnected sockets
REQUIREMENT: The file descriptor MUST be bidirectional β knet needs to both read and write using the SAME fd. User provided datafd will be marked as non-blocking and close-on-exec.
*channelβ This value is analogous to the tag in VLAN tagging. A negative value will auto-allocate a channel. Setting a value between0and31will try to allocate that specific channel (unless already in use).
It is possible to add up to 32 datafds but be aware that each one of them must have a receiving end on the other host. π
Example: hostA channel 0 will be delivered to datafd on hostB channel 0, hostA channel 1 to hostB channel 1.
Each channel must have a unique file descriptor.
If your application could have 2 channels on one host and one channel on another host, then you can use dst_host_filter to manipulate channel values on TX and RX. slay.
π RETURN VALUE π
knet_handle_add_datafd returns:
0β on success,*datafdwill be populated with a socket if the original value was0or if a specific fd was set, the value is untouched.*channelwill be populated with a channel number if the original value was negative or the value is untouched if a specific channel was requested. (win π«Ά)-1β on error anderrnois set.*datafdand*channelare untouched or empty. π
π₯ SEE ALSO π₯
the whole friend group, all invited to the group chat:
knet_handle_remove_datafd(3)knet_handle_get_stats(3)knet_host_add(3)knet_handle_pmtud_setfreq(3)knet_handle_pmtud_get(3)knet_handle_crypto_use_config(3)knet_host_get_id_by_host_name(3)knet_host_get_status(3)knet_link_add_acl(3)knet_link_get_pong_count(3)knet_link_get_priority(3)knet_handle_free(3)knet_handle_enable_sock_notify(3)knet_handle_get_datafd(3)knet_recv(3)knet_link_get_ping_timers(3)knet_log_get_subsystem_id(3)knet_host_remove(3)knet_host_enable_status_change_notify(3)knet_strtoaddr(3)knet_link_rm_acl(3)knet_send(3)knet_handle_enable_pmtud_notify(3)knet_get_crypto_cipher_list(3)knet_handle_get_transport_reconnect_interval(3)knet_link_get_enable(3)knet_link_set_priority(3)knet_log_set_loglevel(3)knet_handle_get_channel(3)knet_link_get_config(3)knet_link_get_link_list(3)knet_get_crypto_hash_list(3)knet_get_transport_list(3)knet_get_transport_id_by_name(3)knet_log_get_loglevel_id(3)knet_handle_new_ex(3)knet_host_set_name(3)knet_addrtostr(3)knet_handle_setfwd(3)knet_get_compress_list(3)knet_host_set_policy(3)knet_get_transport_name_by_id(3)knet_handle_enable_filter(3)knet_handle_crypto_rx_clear_traffic(3)knet_handle_compress(3)knet_link_get_status(3)knet_send_sync(3)knet_log_get_loglevel_name(3)knet_handle_enable_access_lists(3)knet_host_get_host_list(3)knet_host_get_policy(3)knet_link_set_enable(3)knet_link_set_pong_count(3)knet_log_get_subsystem_name(3)knet_host_get_name_by_host_id(3)knet_link_clear_config(3)knet_log_get_loglevel(3)knet_handle_new(3)knet_handle_pmtud_getfreq(3)knet_handle_pmtud_set(3)knet_handle_clear_stats(3)knet_link_set_config(3)knet_handle_crypto_set_config(3)knet_handle_crypto(3)knet_get_crypto_list(3)knet_handle_set_transport_reconnect_interval(3)knet_link_clear_acl(3)knet_link_set_ping_timers(3)knet_handle_setprio_dscp(3)knet_link_insert_acl(3)
π COPYRIGHT π
Copyright (C) 2010-2026 Red Hat, Inc. All rights reserved.
and that's the whole page, bestie π§ stay hydrated, keep your fds bidirectional, and remember: you're doing amazing sweetie π
kronosnet Β· 2026-07-20 Β· mwah, love u, bye π
π¦ where this page comes from
- fedora 43: libknet1-devel 1.35-1.fc43 Β· LGPL-2.1-or-later
- fedora 43: libknet1-devel 1.35-1.fc43 Β· LGPL-2.1-or-later
- fedora 44: libknet1-devel 1.35-1.fc44 Β· LGPL-2.1-or-later
- fedora 44: libknet1-devel 1.35-1.fc44 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms