Skip to main content

include/ZeroTierSockets.h

More...

Classes#

Name
structzts_in_addr
structzts_in6_addr
unionzts_in6_addr::un
structzts_sockaddr_in
structzts_sockaddr_in6
structzts_sockaddr
structzts_sockaddr_storage
structzts_node_info_t
structzts_addr_info_t
structzts_route_info_t
structzts_multicast_group_t
structzts_net_info_t
structzts_path_t
structzts_peer_info_t
structzts_root_set_t
structzts_netif_info_t
structzts_event_msg_t
structzts_stats_counter_t
structzts_linger
structzts_ip_mreq
structzts_in_pktinfo
structzts_ipv6_mreq
structzts_fd_set
structzts_timeval
structzts_pollfd
structzts_iovec
structzts_msghdr
structzts_hostent
structzts_ip4_addr
structzts_ip6_addr
structzts_ip_addr

Types#

Name
enumzts_error_t { ZTS_ERR_OK = 0, ZTS_ERR_SOCKET = -1, ZTS_ERR_SERVICE = -2, ZTS_ERR_ARG = -3, ZTS_ERR_NO_RESULT = -4, ZTS_ERR_GENERAL = -5}
enumzts_event_t { ZTS_EVENT_NODE_UP = 200, ZTS_EVENT_NODE_ONLINE = 201, ZTS_EVENT_NODE_OFFLINE = 202, ZTS_EVENT_NODE_DOWN = 203, ZTS_EVENT_NODE_FATAL_ERROR = 204, ZTS_EVENT_NETWORK_NOT_FOUND = 210, ZTS_EVENT_NETWORK_CLIENT_TOO_OLD = 211, ZTS_EVENT_NETWORK_REQ_CONFIG = 212, ZTS_EVENT_NETWORK_OK = 213, ZTS_EVENT_NETWORK_ACCESS_DENIED = 214, ZTS_EVENT_NETWORK_READY_IP4 = 215, ZTS_EVENT_NETWORK_READY_IP6 = 216, ZTS_EVENT_NETWORK_READY_IP4_IP6 = 217, ZTS_EVENT_NETWORK_DOWN = 218, ZTS_EVENT_NETWORK_UPDATE = 219, ZTS_EVENT_STACK_UP = 220, ZTS_EVENT_STACK_DOWN = 221, ZTS_EVENT_NETIF_UP = 230, ZTS_EVENT_NETIF_DOWN = 231, ZTS_EVENT_NETIF_REMOVED = 232, ZTS_EVENT_NETIF_LINK_UP = 233, ZTS_EVENT_NETIF_LINK_DOWN = 234, ZTS_EVENT_PEER_DIRECT = 240, ZTS_EVENT_PEER_RELAY = 241, ZTS_EVENT_PEER_UNREACHABLE = 242, ZTS_EVENT_PEER_PATH_DISCOVERED = 243, ZTS_EVENT_PEER_PATH_DEAD = 244, ZTS_EVENT_ROUTE_ADDED = 250, ZTS_EVENT_ROUTE_REMOVED = 251, ZTS_EVENT_ADDR_ADDED_IP4 = 260, ZTS_EVENT_ADDR_REMOVED_IP4 = 261, ZTS_EVENT_ADDR_ADDED_IP6 = 262, ZTS_EVENT_ADDR_REMOVED_IP6 = 263, ZTS_EVENT_STORE_IDENTITY_SECRET = 270, ZTS_EVENT_STORE_IDENTITY_PUBLIC = 271, ZTS_EVENT_STORE_PLANET = 272, ZTS_EVENT_STORE_PEER = 273, ZTS_EVENT_STORE_NETWORK = 274}
enumzts_errno_t { ZTS_EPERM = 1, ZTS_ENOENT = 2, ZTS_ESRCH = 3, ZTS_EINTR = 4, ZTS_EIO = 5, ZTS_ENXIO = 6, ZTS_EBADF = 9, ZTS_EAGAIN = 11, ZTS_EWOULDBLOCK = ZTS_EAGAIN, ZTS_ENOMEM = 12, ZTS_EACCES = 13, ZTS_EFAULT = 14, ZTS_EBUSY = 16, ZTS_EEXIST = 17, ZTS_ENODEV = 19, ZTS_EINVAL = 22, ZTS_ENFILE = 23, ZTS_EMFILE = 24, ZTS_ENOSYS = 38, ZTS_ENOTSOCK = 88, ZTS_EDESTADDRREQ = 89, ZTS_EMSGSIZE = 90, ZTS_EPROTOTYPE = 91, ZTS_ENOPROTOOPT = 92, ZTS_EPROTONOSUPPORT = 93, ZTS_ESOCKTNOSUPPORT = 94, ZTS_EOPNOTSUPP = 95, ZTS_EPFNOSUPPORT = 96, ZTS_EAFNOSUPPORT = 97, ZTS_EADDRINUSE = 98, ZTS_EADDRNOTAVAIL = 99, ZTS_ENETDOWN = 100, ZTS_ENETUNREACH = 101, ZTS_ECONNABORTED = 103, ZTS_ECONNRESET = 104, ZTS_ENOBUFS = 105, ZTS_EISCONN = 106, ZTS_ENOTCONN = 107, ZTS_ETIMEDOUT = 110, ZTS_EHOSTUNREACH = 113, ZTS_EALREADY = 114, ZTS_EINPROGRESS = 115}
enumzts_network_status_t { ZTS_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0, ZTS_NETWORK_STATUS_OK = 1, ZTS_NETWORK_STATUS_ACCESS_DENIED = 2, ZTS_NETWORK_STATUS_NOT_FOUND = 3, ZTS_NETWORK_STATUS_PORT_ERROR = 4, ZTS_NETWORK_STATUS_CLIENT_TOO_OLD = 5}
enumzts_net_info_type_t { ZTS_NETWORK_TYPE_PRIVATE = 0, ZTS_NETWORK_TYPE_PUBLIC = 1}
enumzts_peer_role_t { ZTS_PEER_ROLE_LEAF = 0, ZTS_PEER_ROLE_MOON = 1, ZTS_PEER_ROLE_PLANET = 2}
typedef uint32_tzts_socklen_t
typedef uint32_tzts_in_addr_t
typedef uint16_tzts_in_port_t
typedef uint8_tzts_sa_family_t
typedef struct zts_ip_mreqzts_ip_mreq
typedef struct zts_ipv6_mreqzts_ipv6_mreq
typedef struct zts_fd_setzts_fd_set
typedef struct zts_timevalzts_timeval
typedef unsigned intzts_nfds_t
typedef struct zts_ip_addrzts_ip_addr

Functions#

Name
intzts_id_new(char key, unsigned int key_buf_len)
Generates a node identity (public/secret key-pair) and stores it in a user-provided buffer.
intzts_id_pair_is_valid(const char * key, unsigned int len)
Verifies that a key-pair is valid. Checks formatting and pairing of key to address.
intzts_init_from_storage(const char * path)
Instruct ZeroTier to look for node identity files at the given location. This is an initialization function that can only be called before zts_node_start().
intzts_init_from_memory(const char * key, unsigned int len)
Instruct ZeroTier to use the identity provided in key. This is an initialization function that can only be called before zts_node_start().
intzts_init_set_event_handler(void()(void ) callback)
Set the event handler function. This is an initialization function that can only be called before zts_node_start().
intzts_init_blacklist_if(const char * prefix, unsigned int len)
Blacklist an interface prefix (or name). This prevents ZeroTier from sending traffic over matching interfaces. This is an initialization function that can only be called before zts_node_start().
intzts_init_set_roots(const void * roots_data, unsigned int len)
Present a root set definition for ZeroTier to use instead of the default. This is an initialization function that can only be called before zts_node_start().
intzts_init_set_port(unsigned short port)
Set the port to which the node should bind. This is an initialization function that can only be called before zts_node_start().
intzts_init_set_random_port_range(unsigned short start_port, unsigned short end_port)
Set range that random ports will be selected from. This is an initialization function that can only be called before zts_node_start().
intzts_init_allow_secondary_port(unsigned int allowed)
Allow or disallow ZeroTier from automatically selecting a backup port to help get through buggy NAT. This is enabled by default. This port is randomly chosen and should be disabled if you want to control exactly which ports ZeroTier talks on and (iff) you know with absolute certainty that traffic on your chosen primary port is allowed. This is an initialization function that can only be called before zts_node_start().
intzts_init_allow_port_mapping(unsigned int allowed)
Allow or disallow the use of port-mapping. This is enabled by default. This is an initialization function that can only be called before zts_node_start().
intzts_init_allow_net_cache(unsigned int allowed)
Enable or disable whether the node will cache network details (enabled by default when zts_init_from_storage() is used.) Must be called before zts_node_start().
intzts_init_allow_peer_cache(unsigned int allowed)
Enable or disable whether the node will cache peer details (enabled by default when zts_init_from_storage() is used.) Must be called before zts_node_start().
intzts_init_allow_roots_cache(unsigned int allowed)
Enable or disable whether the node will cache root definitions (enabled by default when zts_init_from_storage() is used.) Must be called before zts_node_start().
intzts_init_allow_id_cache(unsigned int allowed)
Enable or disable whether the node will cache identities (enabled by default when zts_init_from_storage() is used.) Must be called before zts_node_start().
intzts_addr_is_assigned(uint64_t net_id, unsigned int family)
Return whether an address of the given family has been assigned by the network.
intzts_addr_get(uint64_t net_id, unsigned int family, struct zts_sockaddr_storage * addr)
Get the first-assigned IP on the given network.
intzts_addr_get_str(uint64_t net_id, unsigned int family, char * dst, unsigned int len)
Get the first-assigned IP on the given network as a null-terminated human-readable string.
intzts_addr_get_all(uint64_t net_id, struct zts_sockaddr_storage addr, unsigned int count)
Get all IP addresses assigned to this node by the given network.
intzts_addr_compute_6plane(const uint64_t net_id, const uint64_t node_id, struct zts_sockaddr_storage * addr)
Compute a 6PLANE IPv6 address for the given Network ID and Node ID.
intzts_addr_compute_rfc4193(const uint64_t net_id, const uint64_t node_id, struct zts_sockaddr_storage * addr)
Compute RFC4193 IPv6 address for the given Network ID and Node ID.
intzts_addr_compute_rfc4193_str(uint64_t net_id, uint64_t node_id, char * dst, unsigned int len)
Compute RFC4193 IPv6 address for the given Network ID and Node ID and copy its null-terminated human-readable string representation into destination buffer.
intzts_addr_compute_6plane_str(uint64_t net_id, uint64_t node_id, char * dst, unsigned int len)
Compute 6PLANE IPv6 address for the given Network ID and Node ID and copy its null-terminated human-readable string representation into destination buffer.
uint64_tzts_net_compute_adhoc_id(uint16_t start_port, uint16_t end_port)
Compute RFC4193 IPv6 address for the given Network ID and Node ID.
intzts_net_join(uint64_t net_id)
Join a network.
intzts_net_leave(uint64_t net_id)
Leave a network.
intzts_net_transport_is_ready(const uint64_t net_id)
Return whether this network is ready to send and receive traffic.
uint64_tzts_net_get_mac(uint64_t net_id)
Get the MAC Address for this node on the given network.
intzts_net_get_mac_str(uint64_t net_id, char * dst, unsigned int len)
Get the MAC Address for this node on the given network.
intzts_net_get_broadcast(uint64_t net_id)
Return whether broadcast is enabled on this network.
intzts_net_get_mtu(uint64_t net_id)
Get the MTU of the given network.
intzts_net_get_name(uint64_t net_id, char * dst, unsigned int len)
Get the nickname of the network.
intzts_net_get_status(uint64_t net_id)
Get the status of the network.
intzts_net_get_type(uint64_t net_id)
Get the type of network (public or private.)
intzts_route_is_assigned(uint64_t net_id, unsigned int family)
Return whether a managed route of the given address family has been assigned by the network.
intzts_node_start()
Start the ZeroTier node. Should be called after calling the relevant zts_init_* functions for your application. To enable storage call zts_init_from_storage() before this function. To enable event callbacks call zts_init_set_event_handler() before this function.
intzts_node_is_online()
Return whether the node is online (Can reach the Internet)
uint64_tzts_node_get_id()
Get the public node identity (aka node_id). Callable only after the node has been started.
intzts_node_get_id_pair(char key, unsigned int key_dst_len)
Copy the current node's public (and secret!) identity into a buffer.
intzts_node_get_port()
Get the primary port to which the node is bound. Callable only after the node has been started.
intzts_node_stop()
Stop the ZeroTier node and bring down all virtual network transport services. Callable only after the node has been started.
intzts_node_free()
Stop all background threads, bring down all transport services, free all resources. After calling this function an application restart will be required before the library can be used again. Callable only after the node has been started.
intzts_moon_orbit(uint64_t moon_roots_id, uint64_t moon_seed)
Orbit a given moon (user-defined root server)
intzts_moon_deorbit(uint64_t moon_roots_id)
De-orbit a given moon (user-defined root server)
intzts_stats_get_all(zts_stats_counter_t * dst)
Get all statistical counters for all protocols and levels. See also: lwip/stats.h.
intzts_bsd_socket(int family, int type, int protocol)
Create a socket.
intzts_bsd_connect(int fd, const struct zts_sockaddr * addr, zts_socklen_t addrlen)
Connect a socket to a remote host.
intzts_bsd_bind(int fd, const struct zts_sockaddr * addr, zts_socklen_t addrlen)
Bind a socket to a local address.
intzts_bsd_listen(int fd, int backlog)
Listen for incoming connections on socket.
intzts_bsd_accept(int fd, struct zts_sockaddr addr, zts_socklen_t addrlen)
Accept an incoming connection.
intzts_bsd_setsockopt(int fd, int level, int optname, const void * optval, zts_socklen_t optlen)
Set socket options.
intzts_bsd_getsockopt(int fd, int level, int optname, void optval, zts_socklen_t optlen)
Get socket options.
intzts_bsd_getsockname(int fd, struct zts_sockaddr addr, zts_socklen_t addrlen)
Get the name (address) of the local end of the socket.
intzts_bsd_getpeername(int fd, struct zts_sockaddr addr, zts_socklen_t addrlen)
Get the name (address) of the remote end of the socket.
intzts_bsd_close(int fd)
Close socket.
intzts_bsd_select(int nfds, zts_fd_set readfds, zts_fd_set writefds, zts_fd_set exceptfds, struct zts_timeval timeout)
Monitor multiple file descriptors for "readiness".
intzts_bsd_fcntl(int fd, int cmd, int flags)
Issue file control commands on a socket.
intzts_bsd_poll(struct zts_pollfd * fds, zts_nfds_t nfds, int timeout)
Wait for some event on a file descriptor.
intzts_bsd_ioctl(int fd, unsigned long request, void * argp)
Control a device.
ssize_tzts_bsd_send(int fd, const void * buf, size_t len, int flags)
Send data to remote host.
ssize_tzts_bsd_sendto(int fd, const void buf, size_t len, int flags, const struct zts_sockaddr addr, zts_socklen_t addrlen)
Send data to remote host.
ssize_tzts_bsd_sendmsg(int fd, const struct zts_msghdr * msg, int flags)
Send message to remote host.
ssize_tzts_bsd_recv(int fd, void * buf, size_t len, int flags)
Receive data from remote host.
ssize_tzts_bsd_recvfrom(int fd, void buf, size_t len, int flags, struct zts_sockaddr addr, zts_socklen_t * addrlen)
Receive data from remote host.
ssize_tzts_bsd_recvmsg(int fd, struct zts_msghdr * msg, int flags)
Receive a message from remote host.
ssize_tzts_bsd_read(int fd, void * buf, size_t len)
Read data from socket onto buffer.
ssize_tzts_bsd_readv(int fd, const struct zts_iovec * iov, int iovcnt)
Read data from socket into multiple buffers.
ssize_tzts_bsd_write(int fd, const void * buf, size_t len)
Write data from buffer to socket.
ssize_tzts_bsd_writev(int fd, const struct zts_iovec * iov, int iovcnt)
Write data from multiple buffers to socket.
intzts_bsd_shutdown(int fd, int how)
Shut down some aspect of a socket.
intzts_socket(int family, int type, int protocol)
Create a socket.
intzts_connect(int fd, const char * ipstr, unsigned short port, int timeout_ms)
Connect a socket to a remote host.
intzts_bind(int fd, const char * ipstr, unsigned short port)
Bind a socket to a local address.
intzts_listen(int fd, int backlog)
Listen for incoming connections on socket.
intzts_accept(int fd, char remote_addr, int len, unsigned short port)
Accept an incoming connection.
ssize_tzts_send(int fd, const void * buf, size_t len, int flags)
Send data to remote host.
ssize_tzts_recv(int fd, void * buf, size_t len, int flags)
Receive data from remote host.
ssize_tzts_read(int fd, void * buf, size_t len)
Read data from socket onto buffer.
ssize_tzts_write(int fd, const void * buf, size_t len)
Write data from buffer to socket.
intzts_shutdown_rd(int fd)
Shut down read aspect of a socket.
intzts_shutdown_wr(int fd)
Shut down write aspect of a socket.
intzts_shutdown_rdwr(int fd)
Shut down both read and write aspect of a socket.
intzts_close(int fd)
Close socket.
intzts_getpeername(int fd, char remote_addr_str, int len, unsigned short port)
Get the name (address) of the remote end of the socket.
intzts_getsockname(int fd, char local_addr_str, int len, unsigned short port)
Get the name (address) of the local end of the socket.
intzts_tcp_client(const char * remote_ipstr, unsigned short remote_port)
A convenience function that takes a remote address IP string and creates the appropriate type of socket, and uses it to connect to a remote host.
intzts_tcp_server(const char local_ipstr, unsigned short local_port, char remote_ipstr, int len, unsigned short * remote_port)
A convenience function that takes a remote address IP string and creates the appropriate type of socket, binds, listens, and then accepts on it.
intzts_udp_server(const char * local_ipstr, unsigned short local_port)
A convenience function that takes a remote address IP string and creates the appropriate type of socket, and binds to it.
intzts_udp_client(const char * remote_ipstr)
This function doesn't really do anything other than be a namespace counterpart to zts_udp_server. All this function does is create a ZTS_SOCK_DGRAM socket and return its file descriptor.
intzts_set_no_delay(int fd, int enabled)
Enable or disable TCP_NODELAY. Enabling this is equivalent to turning off Nagle's algorithm.
intzts_get_last_socket_error(int fd)
Get the last error for the given socket.
size_tzts_get_data_available(int fd)
Return amount of data available to read from socket.
intzts_get_no_delay(int fd)
Return whether TCP_NODELAY is enabled.
intzts_set_linger(int fd, int enabled, int value)
Enable or disable SO_LINGER while also setting its value.
intzts_get_linger_enabled(int fd)
Return whether SO_LINGER is enabled.
intzts_get_linger_value(int fd)
Return the value of SO_LINGER
intzts_get_pending_data_size(int fd)
Return the number of bytes available to read from the network buffer.
intzts_set_reuse_addr(int fd, int enabled)
Enable or disable SO_REUSEADDR
intzts_get_reuse_addr(int fd)
Return whether SO_REUSEADDR is enabled.
intzts_set_recv_timeout(int fd, int seconds, int microseconds)
Set the value of SO_RCVTIMEO
intzts_get_recv_timeout(int fd)
Return the value of SO_RCVTIMEO
intzts_set_send_timeout(int fd, int seconds, int microseconds)
Set the value of SO_SNDTIMEO
intzts_get_send_timeout(int fd)
Return the value of SO_SNDTIMEO
intzts_set_send_buf_size(int fd, int size)
Set the value of SO_SNDBUF
intzts_get_send_buf_size(int fd)
Return the value of SO_SNDBUF
intzts_set_recv_buf_size(int fd, int size)
Set the value of SO_RCVBUF
intzts_get_recv_buf_size(int fd)
Return the value of SO_RCVBUF
intzts_set_ttl(int fd, int ttl)
Set the value of IP_TTL
intzts_get_ttl(int fd)
Return the value of IP_TTL
intzts_set_blocking(int fd, int enabled)
Change blocking behavior O_NONBLOCK
intzts_get_blocking(int fd)
Return whether blocking mode O_NONBLOCK is enabled.
intzts_set_keepalive(int fd, int enabled)
Enable or disable SO_KEEPALIVE
intzts_get_keepalive(int fd)
Return whether SO_KEEPALIVE is enabled.
struct zts_hostent *zts_bsd_gethostbyname(const char * name)
Resolve a host-name.
intzts_dns_set_server(uint8_t index, const zts_ip_addr * addr)
const zts_ip_addr *zts_dns_get_server(uint8_t index)
intzts_core_lock_obtain()
Lock the core service so that queries about addresses, routes, paths, etc. can be performed.
intzts_core_lock_release()
Lock the core service so that queries about addresses, routes, paths, etc. can be performed.
intzts_core_query_addr_count(uint64_t net_id)
Lock the core service so that queries about addresses, routes, paths, etc. can be performed.
intzts_core_query_addr(uint64_t net_id, unsigned int idx, char * addr, unsigned int len)
Lock the core service so that queries about addresses, routes, paths, etc. can be performed.
intzts_core_query_route_count(uint64_t net_id)
Lock the core service so that queries about addresses, routes, paths, etc. can be performed.
intzts_core_query_route(uint64_t net_id, unsigned int idx, char target, char via, unsigned int len, uint16_t flags, uint16_t metric)
Lock the core service so that queries about addresses, routes, paths, etc. can be performed.
intzts_core_query_path_count(uint64_t peer_id)
Lock the core service so that queries about addresses, routes, paths, etc. can be performed.
intzts_core_query_path(uint64_t peer_id, unsigned int idx, char * dst, unsigned int len)
Lock the core service so that queries about addresses, routes, paths, etc. can be performed.
intzts_core_query_mc_count(uint64_t net_id)
Lock the core service so that queries about addresses, routes, paths, etc. can be performed.
intzts_core_query_mc(uint64_t net_id, unsigned int idx, uint64_t mac, uint32_t adi)
Lock the core service so that queries about addresses, routes, paths, etc. can be performed.
intzts_util_sign_root_set(char roots_out, unsigned int roots_len, char prev_key, unsigned int prev_key_len, char curr_key, unsigned int curr_key_len, uint64_t id, uint64_t ts, zts_root_set_t * roots_spec)
Generates a new root set definition.
voidzts_util_delay(unsigned long milliseconds)
Platform-agnostic delay.
intzts_util_get_ip_family(const char * ipstr)
Return the family type of the IP string.
intzts_util_ipstr_to_saddr(const char src_ipstr, unsigned short port, struct zts_sockaddr dstaddr, zts_socklen_t * addrlen)
intzts_util_ntop(struct zts_sockaddr addr, zts_socklen_t addrlen, char dst_str, int len, unsigned short * port)
Similar to inet_ntop but determines family automatically and returns port as a value result parameter.
char *zts_ipaddr_ntoa(const zts_ip_addr * addr)
intzts_ipaddr_aton(const char cp, zts_ip_addr addr)
const char *zts_inet_ntop(int family, const void src, char dst, zts_socklen_t size)
intzts_inet_pton(int family, const char src, void dst)

Attributes#

Name
intzts_errno

Defines#

Name
ZTS_MAC_ADDRSTRLEN
ZTS_INET_ADDRSTRLEN
ZTS_INET6_ADDRSTRLEN
ZTS_IP_MAX_STR_LEN
ZTS_STORE_DATA_LEN
ZTS_MAX_NETWORK_SHORT_NAME_LENGTH
ZTS_MAX_NETWORK_ROUTES
ZTS_MAX_ASSIGNED_ADDRESSES
ZTS_MAX_PEER_NETWORK_PATHS
ZTS_MAX_MULTICAST_SUBSCRIPTIONS
ZTS_MAX_ENDPOINT_STR_LEN
ZTS_C_API_ONLY
ZTS_IPADDR_NONE
ZTS_IPADDR_LOOPBACK
ZTS_IPADDR_ANY
ZTS_IPADDR_BROADCAST
ZTS_INADDR_NONE
ZTS_INADDR_LOOPBACK
ZTS_INADDR_ANY
ZTS_INADDR_BROADCAST
ZTS_SOCK_STREAM
ZTS_SOCK_DGRAM
ZTS_SOCK_RAW
ZTS_AF_UNSPEC
ZTS_AF_INET
ZTS_AF_INET6
ZTS_PF_INET
ZTS_PF_INET6
ZTS_PF_UNSPEC
ZTS_IPPROTO_IP
ZTS_IPPROTO_ICMP
ZTS_IPPROTO_TCP
ZTS_IPPROTO_UDP
ZTS_IPPROTO_IPV6
ZTS_IPPROTO_ICMPV6
ZTS_IPPROTO_UDPLITE
ZTS_IPPROTO_RAW
ZTS_MSG_PEEK
ZTS_MSG_WAITALL
ZTS_MSG_OOB
ZTS_MSG_DONTWAIT
ZTS_MSG_MORE
ZTS_IOCPARM_MASK
ZTS_IOC_VOID
ZTS_IOC_OUT
ZTS_IOC_IN
ZTS_IOC_INOUT
ZTS_IO(x, y)
ZTS_IOR(x, y, t)
ZTS_IOW(x, y, t)
ZTS_FIONREAD
ZTS_FIONBIO
SIN_ZERO_LEN
ZTS_MAX_NUM_ROOTS
ZTS_MAX_ENDPOINTS_PER_ROOT
ZTS_API
ZTCALL
ZTS_DISABLE_CENTRAL_API
ZTS_ID_STR_BUF_LEN
ZTS_SOL_SOCKET
ZTS_SO_DEBUG
ZTS_SO_ACCEPTCONN
ZTS_SO_REUSEADDR
ZTS_SO_KEEPALIVE
ZTS_SO_DONTROUTE
ZTS_SO_BROADCAST
ZTS_SO_USELOOPBACK
ZTS_SO_LINGER
ZTS_SO_DONTLINGER
ZTS_SO_OOBINLINE
ZTS_SO_REUSEPORT
ZTS_SO_SNDBUF
ZTS_SO_RCVBUF
ZTS_SO_SNDLOWAT
ZTS_SO_RCVLOWAT
ZTS_SO_SNDTIMEO
ZTS_SO_RCVTIMEO
ZTS_SO_ERROR
ZTS_SO_TYPE
ZTS_SO_CONTIMEO
ZTS_SO_NO_CHECK
ZTS_SO_BINDTODEVICE
ZTS_IP_TOS
ZTS_IP_TTL
ZTS_IP_PKTINFO
ZTS_TCP_NODELAY
ZTS_TCP_KEEPALIVE
ZTS_TCP_KEEPIDLE
ZTS_TCP_KEEPINTVL
ZTS_TCP_KEEPCNT
ZTS_IPV6_CHECKSUM
ZTS_IPV6_V6ONLY
ZTS_UDPLITE_SEND_CSCOV
ZTS_UDPLITE_RECV_CSCOV
ZTS_IP_MULTICAST_TTL
ZTS_IP_MULTICAST_IF
ZTS_IP_MULTICAST_LOOP
ZTS_IP_ADD_MEMBERSHIP
ZTS_IP_DROP_MEMBERSHIP
ZTS_IPV6_JOIN_GROUP
ZTS_IPV6_ADD_MEMBERSHIP
ZTS_IPV6_LEAVE_GROUP
ZTS_IPV6_DROP_MEMBERSHIP
ZTS_IPTOS_TOS_MASK
ZTS_IPTOS_TOS(tos)
ZTS_IPTOS_LOWDELAY
ZTS_IPTOS_THROUGHPUT
ZTS_IPTOS_RELIABILITY
ZTS_IPTOS_LOWCOST
ZTS_IPTOS_MINCOST
ZTS_IPTOS_PREC_MASK
ZTS_IPTOS_PREC(tos)
ZTS_IPTOS_PREC_NETCONTROL
ZTS_IPTOS_PREC_INTERNETCONTROL
ZTS_IPTOS_PREC_CRITIC_ECP
ZTS_IPTOS_PREC_FLASHOVERRIDE
ZTS_IPTOS_PREC_FLASH
ZTS_IPTOS_PREC_IMMEDIATE
ZTS_IPTOS_PREC_PRIORITY
ZTS_IPTOS_PREC_ROUTINE
LWIP_SOCKET_OFFSET
MEMP_NUM_NETCONN
ZTS_FD_SETSIZE
ZTS_FDSETSAFESET(n, code)
ZTS_FDSETSAFEGET(n, code)
ZTS_FD_SET(n, p)
ZTS_FD_CLR(n, p)
ZTS_FD_ISSET(n, p)
ZTS_FD_ZERO(p)
ZTS_F_GETFL
ZTS_F_SETFL
ZTS_O_NONBLOCK
ZTS_O_NDELAY
ZTS_O_RDONLY
ZTS_O_WRONLY
ZTS_O_RDWR
ZTS_POLLIN
ZTS_POLLOUT
ZTS_POLLERR
ZTS_POLLNVAL
ZTS_POLLRDNORM
ZTS_POLLRDBAND
ZTS_POLLPRI
ZTS_POLLWRNORM
ZTS_POLLWRBAND
ZTS_POLLHUP
ZTS_MSG_TRUNC
ZTS_MSG_CTRUNC
ZTS_SHUT_RD
ZTS_SHUT_WR
ZTS_SHUT_RDWR
h_addr

Detailed Description#

This defines the external C API for ZeroTier Sockets

Types Documentation#

enum zts_error_t#

EnumeratorValueDescription
ZTS_ERR_OK0No error
ZTS_ERR_SOCKET-1Socket error, see zts_errno
ZTS_ERR_SERVICE-2This operation is not allowed at this time. Or possibly the node hasn't been started
ZTS_ERR_ARG-3Invalid argument
ZTS_ERR_NO_RESULT-4No result (not necessarily an error)
ZTS_ERR_GENERAL-5Consider filing a bug report

Common error return values

enum zts_event_t#

EnumeratorValueDescription
ZTS_EVENT_NODE_UP200Node has been initialized

This is the first event generated, and is always sent. It may occur before node's constructor returns. | | ZTS_EVENT_NODE_ONLINE | 201| Node is online – at least one upstream node appears reachable | | ZTS_EVENT_NODE_OFFLINE | 202| Node is offline – network does not seem to be reachable by any available strategy | | ZTS_EVENT_NODE_DOWN | 203| Node is shutting down

This is generated within Node's destructor when it is being shut down. It's done for convenience, since cleaning up other state in the event handler may appear more idiomatic. | | ZTS_EVENT_NODE_FATAL_ERROR | 204| A fatal error has occurred. One possible reason is:

Your identity has collided with another node's ZeroTier address

This happens if two different public keys both hash (via the algorithm in Identity::generate()) to the same 40-bit ZeroTier address.

This is something you should "never" see, where "never" is defined as once per 2^39 new node initializations / identity creations. If you do see it, you're going to see it very soon after a node is first initialized.

This is reported as an event rather than a return code since it's detected asynchronously via error messages from authoritative nodes.

If this occurs, you must shut down and delete the node, delete the identity.secret record/file from the data store, and restart to generate a new identity. If you don't do this, you will not be able to communicate with other nodes.

We'd automate this process, but we don't think silently deleting private keys or changing our address without telling the calling code is good form. It violates the principle of least surprise.

You can technically get away with not handling this, but we recommend doing so in a mature reliable application. Besides, handling this condition is a good way to make sure it never arises. It's like how umbrellas prevent rain and smoke detectors prevent fires. They do, right?

Meta-data: none | | ZTS_EVENT_NETWORK_NOT_FOUND | 210| Network ID does not correspond to a known network | | ZTS_EVENT_NETWORK_CLIENT_TOO_OLD | 211| The version of ZeroTier inside libzt is too old | | ZTS_EVENT_NETWORK_REQ_CONFIG | 212| The configuration for a network has been requested (no action needed) | | ZTS_EVENT_NETWORK_OK | 213| The node joined the network successfully (no action needed) | | ZTS_EVENT_NETWORK_ACCESS_DENIED | 214| The node is not allowed to join the network (you must authorize node) | | ZTS_EVENT_NETWORK_READY_IP4 | 215| The node has received an IPv4 address from the network controller | | ZTS_EVENT_NETWORK_READY_IP6 | 216| The node has received an IPv6 address from the network controller | | ZTS_EVENT_NETWORK_READY_IP4_IP6 | 217| Deprecated | | ZTS_EVENT_NETWORK_DOWN | 218| Network controller is unreachable | | ZTS_EVENT_NETWORK_UPDATE | 219| Network change received from controller | | ZTS_EVENT_STACK_UP | 220| TCP/IP stack (lwIP) is up (for debug purposes) | | ZTS_EVENT_STACK_DOWN | 221| TCP/IP stack (lwIP) id down (for debug purposes) | | ZTS_EVENT_NETIF_UP | 230| lwIP netif up (for debug purposes) | | ZTS_EVENT_NETIF_DOWN | 231| lwIP netif down (for debug purposes) | | ZTS_EVENT_NETIF_REMOVED | 232| lwIP netif removed (for debug purposes) | | ZTS_EVENT_NETIF_LINK_UP | 233| lwIP netif link up (for debug purposes) | | ZTS_EVENT_NETIF_LINK_DOWN | 234| lwIP netif link down (for debug purposes) | | ZTS_EVENT_PEER_DIRECT | 240| A direct P2P path to peer is known | | ZTS_EVENT_PEER_RELAY | 241| A direct P2P path to peer is NOT known. Traffic is now relayed | | ZTS_EVENT_PEER_UNREACHABLE | 242| A peer is unreachable. Check NAT/Firewall settings | | ZTS_EVENT_PEER_PATH_DISCOVERED | 243| A new path to a peer was discovered | | ZTS_EVENT_PEER_PATH_DEAD | 244| A known path to a peer is now considered dead | | ZTS_EVENT_ROUTE_ADDED | 250| A new managed network route was added | | ZTS_EVENT_ROUTE_REMOVED | 251| A managed network route was removed | | ZTS_EVENT_ADDR_ADDED_IP4 | 260| A new managed IPv4 address was assigned to this peer | | ZTS_EVENT_ADDR_REMOVED_IP4 | 261| A managed IPv4 address assignment was removed from this peer | | ZTS_EVENT_ADDR_ADDED_IP6 | 262| A new managed IPv4 address was assigned to this peer | | ZTS_EVENT_ADDR_REMOVED_IP6 | 263| A managed IPv6 address assignment was removed from this peer | | ZTS_EVENT_STORE_IDENTITY_SECRET | 270| The node's secret key (identity) | | ZTS_EVENT_STORE_IDENTITY_PUBLIC | 271| The node's public key (identity) | | ZTS_EVENT_STORE_PLANET | 272| The node has received an updated planet config | | ZTS_EVENT_STORE_PEER | 273| New reachability hints and peer configuration | | ZTS_EVENT_STORE_NETWORK | 274| New network config |

Event codes used by the (optional) callback API

enum zts_errno_t#

EnumeratorValueDescription
ZTS_EPERM1Operation not permitted (zts_errno value)
ZTS_ENOENT2No such file or directory
ZTS_ESRCH3No such process
ZTS_EINTR4Interrupted system call
ZTS_EIO5I/O error
ZTS_ENXIO6No such device or address
ZTS_EBADF9Bad file number
ZTS_EAGAIN11Try again
ZTS_EWOULDBLOCKZTS_EAGAINOperation would block
ZTS_ENOMEM12Out of memory
ZTS_EACCES13Permission denied
ZTS_EFAULT14Bad address
ZTS_EBUSY16Device or resource busy
ZTS_EEXIST17File exists
ZTS_ENODEV19No such device
ZTS_EINVAL22Invalid argument
ZTS_ENFILE23File table overflow
ZTS_EMFILE24Too many open files
ZTS_ENOSYS38Function not implemented
ZTS_ENOTSOCK88Socket operation on non-socket
ZTS_EDESTADDRREQ89Destination address required
ZTS_EMSGSIZE90Message too long
ZTS_EPROTOTYPE91Protocol wrong type for socket
ZTS_ENOPROTOOPT92Protocol not available
ZTS_EPROTONOSUPPORT93Protocol not supported
ZTS_ESOCKTNOSUPPORT94Socket type not supported
ZTS_EOPNOTSUPP95Operation not supported on transport endpoint
ZTS_EPFNOSUPPORT96Protocol family not supported
ZTS_EAFNOSUPPORT97Address family not supported by protocol
ZTS_EADDRINUSE98Address already in use
ZTS_EADDRNOTAVAIL99Cannot assign requested address
ZTS_ENETDOWN100Network is down
ZTS_ENETUNREACH101Network is unreachable
ZTS_ECONNABORTED103Software caused connection abort
ZTS_ECONNRESET104Connection reset by peer
ZTS_ENOBUFS105No buffer space available
ZTS_EISCONN106Transport endpoint is already connected
ZTS_ENOTCONN107Transport endpoint is not connected
ZTS_ETIMEDOUT110Connection timed out
ZTS_EHOSTUNREACH113No route to host
ZTS_EALREADY114Operation already in progress
ZTS_EINPROGRESS115Operation now in progress

enum zts_network_status_t#

EnumeratorValueDescription
ZTS_NETWORK_STATUS_REQUESTING_CONFIGURATION0Waiting for network configuration (also means revision == 0)
ZTS_NETWORK_STATUS_OK1Configuration received and we are authorized
ZTS_NETWORK_STATUS_ACCESS_DENIED2Netconf master told us 'nope'
ZTS_NETWORK_STATUS_NOT_FOUND3Netconf master exists, but this virtual network does not
ZTS_NETWORK_STATUS_PORT_ERROR4Initialization of network failed or other internal error
ZTS_NETWORK_STATUS_CLIENT_TOO_OLD5ZeroTier core version too old

Virtual network status codes

enum zts_net_info_type_t#

EnumeratorValueDescription
ZTS_NETWORK_TYPE_PRIVATE0Private networks are authorized via certificates of membership
ZTS_NETWORK_TYPE_PUBLIC1Public networks have no access control – they'll always be AUTHORIZED

Virtual network type codes

enum zts_peer_role_t#

EnumeratorValueDescription
ZTS_PEER_ROLE_LEAF0Ordinary node
ZTS_PEER_ROLE_MOON1Moon root
ZTS_PEER_ROLE_PLANET2Planetary root

The peer's trust hierarchy role

typedef zts_socklen_t#

typedef uint32_t zts_socklen_t;

typedef zts_in_addr_t#

typedef uint32_t zts_in_addr_t;

typedef zts_in_port_t#

typedef uint16_t zts_in_port_t;

typedef zts_sa_family_t#

typedef uint8_t zts_sa_family_t;

typedef zts_ip_mreq#

typedef struct zts_ip_mreq zts_ip_mreq;

typedef zts_ipv6_mreq#

typedef struct zts_ipv6_mreq zts_ipv6_mreq;

typedef zts_fd_set#

typedef struct zts_fd_set zts_fd_set;

typedef zts_timeval#

typedef struct zts_timeval zts_timeval;

typedef zts_nfds_t#

typedef unsigned int zts_nfds_t;

typedef zts_ip_addr#

typedef struct zts_ip_addr zts_ip_addr;

A union struct for both IP version's addresses. ATTENTION: watch out for its size when adding IPv6 address scope!

Functions Documentation#

function zts_id_new#

int zts_id_new(
char * key,
unsigned int * key_buf_len
)

Generates a node identity (public/secret key-pair) and stores it in a user-provided buffer.

Parameters:

  • key User-provided destination buffer
  • key_buf_len Length of user-provided destination buffer. Will be set to the number of bytes copied.

Return: ZTS_ERR_OK if successful, ZTS_ERR_ARG if invalid argument.

function zts_id_pair_is_valid#

int zts_id_pair_is_valid(
const char * key,
unsigned int len
)

Verifies that a key-pair is valid. Checks formatting and pairing of key to address.

Parameters:

  • key Buffer containing key-pair
  • len Length of key-pair buffer

Return: 1 if true, 0 if false.

function zts_init_from_storage#

int zts_init_from_storage(
const char * path
)

Instruct ZeroTier to look for node identity files at the given location. This is an initialization function that can only be called before zts_node_start().

Parameters:

  • port Path Null-terminated file-system path string

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

Note that calling this function is not mandatory and if it is not called the node's keys will be kept in memory and retrievable via zts_node_get_id_pair().

See also: zts_init_from_memory()

function zts_init_from_memory#

int zts_init_from_memory(
const char * key,
unsigned int len
)

Instruct ZeroTier to use the identity provided in key. This is an initialization function that can only be called before zts_node_start().

Parameters:

  • key Path Null-terminated file-system path string
  • len Length of key buffer

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

Note that calling this function is not mandatory and if it is not called the node's keys will be kept in memory and retrievable via zts_node_get_id_pair().

See also: zts_init_from_storage()

function zts_init_set_event_handler#

int zts_init_set_event_handler(
void(*)(void *) callback
)

Set the event handler function. This is an initialization function that can only be called before zts_node_start().

Parameters:

  • callback A function pointer to the event handler function
  • family ZTS_AF_INET, or ZTS_AF_INET6

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_init_blacklist_if#

int zts_init_blacklist_if(
const char * prefix,
unsigned int len
)

Blacklist an interface prefix (or name). This prevents ZeroTier from sending traffic over matching interfaces. This is an initialization function that can only be called before zts_node_start().

Parameters:

  • prefix Null-terminated interface prefix string
  • len Length of prefix string

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_init_set_roots#

int zts_init_set_roots(
const void * roots_data,
unsigned int len
)

Present a root set definition for ZeroTier to use instead of the default. This is an initialization function that can only be called before zts_node_start().

Parameters:

  • roots_data Array of roots definition data (binary)
  • len Length of binary data

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_init_set_port#

int zts_init_set_port(
unsigned short port
)

Set the port to which the node should bind. This is an initialization function that can only be called before zts_node_start().

Parameters:

  • port Port number

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_init_set_random_port_range#

int zts_init_set_random_port_range(
unsigned short start_port,
unsigned short end_port
)

Set range that random ports will be selected from. This is an initialization function that can only be called before zts_node_start().

Parameters:

  • start_port Start of port range
  • end_port End of port range

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_init_allow_secondary_port#

int zts_init_allow_secondary_port(
unsigned int allowed
)

Allow or disallow ZeroTier from automatically selecting a backup port to help get through buggy NAT. This is enabled by default. This port is randomly chosen and should be disabled if you want to control exactly which ports ZeroTier talks on and (iff) you know with absolute certainty that traffic on your chosen primary port is allowed. This is an initialization function that can only be called before zts_node_start().

Parameters:

  • port Port number

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_init_allow_port_mapping#

int zts_init_allow_port_mapping(
unsigned int allowed
)

Allow or disallow the use of port-mapping. This is enabled by default. This is an initialization function that can only be called before zts_node_start().

Parameters:

  • port Port number

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_init_allow_net_cache#

int zts_init_allow_net_cache(
unsigned int allowed
)

Enable or disable whether the node will cache network details (enabled by default when zts_init_from_storage() is used.) Must be called before zts_node_start().

Parameters:

  • enabled Whether or not this feature is enabled

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

This can potentially shorten (startup) times between node restarts. This allows the service to nearly instantly inform the network stack of an address to use for this peer so that it can create a transport service. This can be disabled for cases where one may not want network config details to be written to storage. This is especially useful for situations where address assignments do not change often.

See also: zts_init_allow_peer_cache()

function zts_init_allow_peer_cache#

int zts_init_allow_peer_cache(
unsigned int allowed
)

Enable or disable whether the node will cache peer details (enabled by default when zts_init_from_storage() is used.) Must be called before zts_node_start().

Parameters:

  • enabled Whether or not this feature is enabled

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

This can potentially shorten (connection) times between node restarts. This allows the service to re-use previously discovered paths to a peer, this prevents the service from having to go through the entire transport-triggered link provisioning process. This is especially useful for situations where paths to peers do not change often. This is enabled by default and can be disabled for cases where one may not want peer details to be written to storage.

See also: zts_init_allow_net_cache()

function zts_init_allow_roots_cache#

int zts_init_allow_roots_cache(
unsigned int allowed
)

Enable or disable whether the node will cache root definitions (enabled by default when zts_init_from_storage() is used.) Must be called before zts_node_start().

Parameters:

  • enabled Whether or not this feature is enabled

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_init_allow_id_cache#

int zts_init_allow_id_cache(
unsigned int allowed
)

Enable or disable whether the node will cache identities (enabled by default when zts_init_from_storage() is used.) Must be called before zts_node_start().

Parameters:

  • enabled Whether or not this feature is enabled

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_addr_is_assigned#

int zts_addr_is_assigned(
uint64_t net_id,
unsigned int family
)

Return whether an address of the given family has been assigned by the network.

Parameters:

  • net_id Network ID
  • family ZTS_AF_INET, or ZTS_AF_INET6

Return: 1 if true, 0 if false.

function zts_addr_get#

int zts_addr_get(
uint64_t net_id,
unsigned int family,
struct zts_sockaddr_storage * addr
)

Get the first-assigned IP on the given network.

Parameters:

  • net_id Network ID
  • family ZTS_AF_INET, or ZTS_AF_INET6
  • addr Destination buffer to hold address

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

To get all assigned addresses on a given network, use zts_addr_get_all().

function zts_addr_get_str#

int zts_addr_get_str(
uint64_t net_id,
unsigned int family,
char * dst,
unsigned int len
)

Get the first-assigned IP on the given network as a null-terminated human-readable string.

Parameters:

  • net_id Network ID
  • family ZTS_AF_INET, or ZTS_AF_INET6
  • dst Destination buffer
  • len Length of destination buffer (must be exactly ZTS_IP_MAX_STR_LEN)

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

To get all assigned addresses on a given network, use zts_addr_get_all().

function zts_addr_get_all#

int zts_addr_get_all(
uint64_t net_id,
struct zts_sockaddr_storage * addr,
unsigned int * count
)

Get all IP addresses assigned to this node by the given network.

Parameters:

  • net_id Network ID
  • addr Destination buffer to hold address
  • count Number of addresses returned

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_addr_compute_6plane#

int zts_addr_compute_6plane(
const uint64_t net_id,
const uint64_t node_id,
struct zts_sockaddr_storage * addr
)

Compute a 6PLANE IPv6 address for the given Network ID and Node ID.

Parameters:

  • net_id Network ID
  • node_id Node ID
  • addr Destination structure for address

Return: ZTS_ERR_OK if successful, ZTS_ERR_ARG if invalid argument.

function zts_addr_compute_rfc4193#

int zts_addr_compute_rfc4193(
const uint64_t net_id,
const uint64_t node_id,
struct zts_sockaddr_storage * addr
)

Compute RFC4193 IPv6 address for the given Network ID and Node ID.

Parameters:

  • net_id Network ID
  • node_id Node ID
  • addr Destination structure for address

Return: ZTS_ERR_OK if successful, ZTS_ERR_ARG if invalid argument.

function zts_addr_compute_rfc4193_str#

int zts_addr_compute_rfc4193_str(
uint64_t net_id,
uint64_t node_id,
char * dst,
unsigned int len
)

Compute RFC4193 IPv6 address for the given Network ID and Node ID and copy its null-terminated human-readable string representation into destination buffer.

Parameters:

  • net_id Network ID
  • node_id Node ID
  • dst Destination string buffer
  • len Length of destination string buffer (must be exactly ZTS_IP_MAX_STR_LEN)

Return: ZTS_ERR_OK if successful, ZTS_ERR_ARG if invalid argument.

function zts_addr_compute_6plane_str#

int zts_addr_compute_6plane_str(
uint64_t net_id,
uint64_t node_id,
char * dst,
unsigned int len
)

Compute 6PLANE IPv6 address for the given Network ID and Node ID and copy its null-terminated human-readable string representation into destination buffer.

Parameters:

  • net_id Network ID
  • node_id Node ID
  • dst Destination string buffer
  • len Length of destination string buffer (must be exactly ZTS_IP_MAX_STR_LEN)

Return: ZTS_ERR_OK if successful, ZTS_ERR_ARG if invalid argument.

function zts_net_compute_adhoc_id#

uint64_t zts_net_compute_adhoc_id(
uint16_t start_port,
uint16_t end_port
)

Compute RFC4193 IPv6 address for the given Network ID and Node ID.

Parameters:

  • start_port Start of port allowed port range
  • end_port End of allowed port range

Return: An Ad-hoc network ID

Ad-hoc Network:

ffSSSSEEEE000000
| | | |
| | | Reserved for future use, must be 0
| | End of port range (hex)
| Start of port range (hex)
Reserved ZeroTier address prefix indicating a controller-less network.

Ad-hoc networks are public (no access control) networks that have no network controller. Instead their configuration and other credentials are generated locally. Ad-hoc networks permit only IPv6 UDP and TCP unicast traffic (no multicast or broadcast) using 6plane format NDP-emulated IPv6 addresses. In addition an ad-hoc network ID encodes an IP port range. UDP packets and TCP SYN (connection open) packets are only allowed to destination ports within the encoded range.

For example ff00160016000000 is an ad-hoc network allowing only SSH, while ff0000ffff000000 is an ad-hoc network allowing any UDP or TCP port.

Keep in mind that these networks are public and anyone in the entire world can join them. Care must be taken to avoid exposing vulnerable services or sharing unwanted files or other resources.

function zts_net_join#

int zts_net_join(
uint64_t net_id
)

Join a network.

Parameters:

  • net_id Network ID

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_net_leave#

int zts_net_leave(
uint64_t net_id
)

Leave a network.

Parameters:

  • net_id Network ID

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_net_transport_is_ready#

int zts_net_transport_is_ready(
const uint64_t net_id
)

Return whether this network is ready to send and receive traffic.

Return: 1 if true, 0 if false.

function zts_net_get_mac#

uint64_t zts_net_get_mac(
uint64_t net_id
)

Get the MAC Address for this node on the given network.

Parameters:

  • net_id Network ID

Return: MAC address in numerical format

function zts_net_get_mac_str#

int zts_net_get_mac_str(
uint64_t net_id,
char * dst,
unsigned int len
)

Get the MAC Address for this node on the given network.

Parameters:

  • net_id Network ID
  • dst Destination string buffer
  • len Length of destination string buffer. Must be exactly ZTS_MAC_ADDRSTRLEN

Return: MAC address in string format

function zts_net_get_broadcast#

int zts_net_get_broadcast(
uint64_t net_id
)

Return whether broadcast is enabled on this network.

Parameters:

  • net_id Network ID

Return: 1 if true, 0 if false.

function zts_net_get_mtu#

int zts_net_get_mtu(
uint64_t net_id
)

Get the MTU of the given network.

Parameters:

  • net_id Network ID

Return: MTU

function zts_net_get_name#

int zts_net_get_name(
uint64_t net_id,
char * dst,
unsigned int len
)

Get the nickname of the network.

Parameters:

  • net_id Network ID
  • dst Destination string buffer
  • len Length of destination string buffer

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_net_get_status#

int zts_net_get_status(
uint64_t net_id
)

Get the status of the network.

Parameters:

  • net_id Network ID

Return: Status

function zts_net_get_type#

int zts_net_get_type(
uint64_t net_id
)

Get the type of network (public or private.)

Parameters:

  • net_id Network ID

Return: Type

function zts_route_is_assigned#

int zts_route_is_assigned(
uint64_t net_id,
unsigned int family
)

Return whether a managed route of the given address family has been assigned by the network.

Parameters:

  • net_id Network ID
  • family ZTS_AF_INET, or ZTS_AF_INET6

Return: 1 if true, 0 if false.

function zts_node_start#

int zts_node_start()

Start the ZeroTier node. Should be called after calling the relevant zts_init_* functions for your application. To enable storage call zts_init_from_storage() before this function. To enable event callbacks call zts_init_set_event_handler() before this function.

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem.

Note: If neither zts_init_from_storage() or zts_init_from_memory() are called a new identity will be generated and will be retrievable via zts_node_get_id_pair()after the node has started.

function zts_node_is_online#

int zts_node_is_online()

Return whether the node is online (Can reach the Internet)

Return: 1 if true, 0 if false.

function zts_node_get_id#

uint64_t zts_node_get_id()

Get the public node identity (aka node_id). Callable only after the node has been started.

Return: Identity in numerical form

function zts_node_get_id_pair#

int zts_node_get_id_pair(
char * key,
unsigned int * key_dst_len
)

Copy the current node's public (and secret!) identity into a buffer.

Parameters:

  • key User-provided destination buffer
  • key_dst_len Length of user-provided destination buffer. Will be set to number of bytes copied.

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

WARNING: This function exports your secret key and should be used carefully.

function zts_node_get_port#

int zts_node_get_port()

Get the primary port to which the node is bound. Callable only after the node has been started.

Return: Port number

function zts_node_stop#

int zts_node_stop()

Stop the ZeroTier node and bring down all virtual network transport services. Callable only after the node has been started.

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem.

While the ZeroTier will stop, the stack driver (with associated timers) will remain active in case future traffic processing is required. To stop all activity and free all resources use zts_free() instead.

function zts_node_free#

int zts_node_free()

Stop all background threads, bring down all transport services, free all resources. After calling this function an application restart will be required before the library can be used again. Callable only after the node has been started.

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem.

This should be called at the end of your program or when you do not anticipate communicating over ZeroTier again.

function zts_moon_orbit#

int zts_moon_orbit(
uint64_t moon_roots_id,
uint64_t moon_seed
)

Orbit a given moon (user-defined root server)

Parameters:

  • moon_roots_id World ID
  • moon_seed Seed ID

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_moon_deorbit#

int zts_moon_deorbit(
uint64_t moon_roots_id
)

De-orbit a given moon (user-defined root server)

Parameters:

  • moon_roots_id World ID

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument.

function zts_stats_get_all#

int zts_stats_get_all(
zts_stats_counter_t * dst
)

Get all statistical counters for all protocols and levels. See also: lwip/stats.h.

Parameters:

  • dst Pointer to structure that will be populated with statistics

Return: ZTS_ERR_OK on success. ZTS_ERR_ARG or ZTS_ERR_NO_RESULT on failure.

This function can only be used in debug builds.

function zts_bsd_socket#

int zts_bsd_socket(
int family,
int type,
int protocol
)

Create a socket.

Parameters:

  • family ZTS_AF_INET or ZTS_AF_INET6
  • type ZTS_SOCK_STREAM or ZTS_SOCK_DGRAM
  • protocol Protocols supported on this socket

Return: Numbered file descriptor on success, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_connect#

int zts_bsd_connect(
int fd,
const struct zts_sockaddr * addr,
zts_socklen_t addrlen
)

Connect a socket to a remote host.

Parameters:

  • fd Socket file descriptor
  • addr Remote host address to connect to
  • addrlen Length of address

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_bind#

int zts_bsd_bind(
int fd,
const struct zts_sockaddr * addr,
zts_socklen_t addrlen
)

Bind a socket to a local address.

Parameters:

  • fd Socket file descriptor
  • addr Local interface address to bind to
  • addrlen Length of address

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_listen#

int zts_bsd_listen(
int fd,
int backlog
)

Listen for incoming connections on socket.

Parameters:

  • fd Socket file descriptor
  • backlog Number of backlogged connections allowed

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_accept#

int zts_bsd_accept(
int fd,
struct zts_sockaddr * addr,
zts_socklen_t * addrlen
)

Accept an incoming connection.

Parameters:

  • fd Socket file descriptor
  • addr Address of remote host for accepted connection
  • addrlen Length of address

Return: New file descriptor if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_setsockopt#

int zts_bsd_setsockopt(
int fd,
int level,
int optname,
const void * optval,
zts_socklen_t optlen
)

Set socket options.

Parameters:

  • fd Socket file descriptor
  • level Protocol level to which option name should apply
  • optname Option name to set
  • optval Source of option value to set
  • optlen Length of option value

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_getsockopt#

int zts_bsd_getsockopt(
int fd,
int level,
int optname,
void * optval,
zts_socklen_t * optlen
)

Get socket options.

Parameters:

  • fd Socket file descriptor
  • level Protocol level to which option name should apply
  • optname Option name to get
  • optval Where option value will be stored
  • optlen Length of value

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_getsockname#

int zts_bsd_getsockname(
int fd,
struct zts_sockaddr * addr,
zts_socklen_t * addrlen
)

Get the name (address) of the local end of the socket.

Parameters:

  • fd Socket file descriptor
  • addr Name associated with this socket
  • addrlen Length of name

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_getpeername#

int zts_bsd_getpeername(
int fd,
struct zts_sockaddr * addr,
zts_socklen_t * addrlen
)

Get the name (address) of the remote end of the socket.

Parameters:

  • fd Socket file descriptor
  • addr Name associated with remote end of this socket
  • addrlen Length of name

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_close#

int zts_bsd_close(
int fd
)

Close socket.

Parameters:

  • fd Socket file descriptor

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_select#

int zts_bsd_select(
int nfds,
zts_fd_set * readfds,
zts_fd_set * writefds,
zts_fd_set * exceptfds,
struct zts_timeval * timeout
)

Monitor multiple file descriptors for "readiness".

Parameters:

  • nfds Set to the highest numbered file descriptor in any of the given sets
  • readfds Set of file descriptors to monitor for READ readiness
  • writefds Set of file descriptors to monitor for WRITE readiness
  • exceptfds Set of file descriptors to monitor for exceptional conditions
  • timeout How long this call should block

Return: Number of ready file descriptors on success. ZTS_ERR_SOCKET, ZTS_ERR_SERVICE on failure. Sets zts_errno

function zts_bsd_fcntl#

int zts_bsd_fcntl(
int fd,
int cmd,
int flags
)

Issue file control commands on a socket.

Parameters:

  • fd Socket file descriptor
  • cmd Operation to be performed
  • flags Flags

Return:

function zts_bsd_poll#

int zts_bsd_poll(
struct zts_pollfd * fds,
zts_nfds_t nfds,
int timeout
)

Wait for some event on a file descriptor.

Parameters:

  • fds Set of file descriptors to monitor
  • nfds Number of elements in the fds array
  • timeout How long this call should block

Return: Number of ready file descriptors if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_ioctl#

int zts_bsd_ioctl(
int fd,
unsigned long request,
void * argp
)

Control a device.

Parameters:

  • fd Socket file descriptor
  • request Selects the control function to be performed
  • argp Additional information

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_send#

ssize_t zts_bsd_send(
int fd,
const void * buf,
size_t len,
int flags
)

Send data to remote host.

Parameters:

  • fd Socket file descriptor
  • buf Pointer to data buffer
  • len Length of data to write
  • flags (e.g. ZTS_MSG_DONTWAIT, ZTS_MSG_MORE)

Return: Number of bytes sent if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_sendto#

ssize_t zts_bsd_sendto(
int fd,
const void * buf,
size_t len,
int flags,
const struct zts_sockaddr * addr,
zts_socklen_t addrlen
)

Send data to remote host.

Parameters:

  • fd Socket file descriptor
  • buf Pointer to data buffer
  • len Length of data to write
  • flags Specifies type of message transmission
  • addr Destination address
  • addrlen Length of destination address

Return: Number of bytes sent if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_sendmsg#

ssize_t zts_bsd_sendmsg(
int fd,
const struct zts_msghdr * msg,
int flags
)

Send message to remote host.

Parameters:

  • fd Socket file descriptor
  • msg Message to send
  • flags Specifies type of message transmission

Return: Number of bytes sent if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_recv#

ssize_t zts_bsd_recv(
int fd,
void * buf,
size_t len,
int flags
)

Receive data from remote host.

Parameters:

  • fd Socket file descriptor
  • buf Pointer to data buffer
  • len Length of data buffer
  • flags Specifies the type of message receipt

Return: Number of bytes received if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_recvfrom#

ssize_t zts_bsd_recvfrom(
int fd,
void * buf,
size_t len,
int flags,
struct zts_sockaddr * addr,
zts_socklen_t * addrlen
)

Receive data from remote host.

Parameters:

  • fd Socket file descriptor
  • buf Pointer to data buffer
  • len Length of data buffer
  • flags Specifies the type of message receipt
  • addr Destination address buffer
  • addrlen Length of destination address buffer

Return: Number of bytes received if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_recvmsg#

ssize_t zts_bsd_recvmsg(
int fd,
struct zts_msghdr * msg,
int flags
)

Receive a message from remote host.

Parameters:

  • fd Socket file descriptor
  • msg Message that was received
  • flags Specifies the type of message receipt

Return: Number of bytes received if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_read#

ssize_t zts_bsd_read(
int fd,
void * buf,
size_t len
)

Read data from socket onto buffer.

Parameters:

  • fd Socket file descriptor
  • buf Pointer to data buffer
  • len Length of data buffer to receive data

Return: Number of bytes read if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_readv#

ssize_t zts_bsd_readv(
int fd,
const struct zts_iovec * iov,
int iovcnt
)

Read data from socket into multiple buffers.

Parameters:

  • fd Socket file descriptor
  • iov Array of destination buffers
  • iovcnt Number of buffers to read into

Return: Number of bytes read if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_write#

ssize_t zts_bsd_write(
int fd,
const void * buf,
size_t len
)

Write data from buffer to socket.

Parameters:

  • fd Socket file descriptor
  • buf Pointer to data buffer
  • len Length of buffer to write

Return: Number of bytes written if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_writev#

ssize_t zts_bsd_writev(
int fd,
const struct zts_iovec * iov,
int iovcnt
)

Write data from multiple buffers to socket.

Parameters:

  • fd Socket file descriptor
  • iov Array of source buffers
  • iovcnt Number of buffers to read from

Return: Number of bytes written if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_shutdown#

int zts_bsd_shutdown(
int fd,
int how
)

Shut down some aspect of a socket.

Parameters:

  • fd Socket file descriptor
  • how Which aspects of the socket should be shut down. Options are ZTS_SHUT_RD, ZTS_SHUT_WR, or ZTS_SHUT_RDWR.

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_socket#

int zts_socket(
int family,
int type,
int protocol
)

Create a socket.

Parameters:

  • family ZTS_AF_INET or ZTS_AF_INET6
  • type ZTS_SOCK_STREAM or ZTS_SOCK_DGRAM
  • protocol Protocols supported on this socket

Return: Numbered file descriptor on success, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

A subset (and) extension of the traditional BSD-style socket API that simplifies API wrapper generation and usage in other non-C-like languages. Uses simple integer types instead of bit flags, limit the number of operations each function performs, prevent the user from needing to manipulate the contents of structures in a non-native language.

function zts_connect#

int zts_connect(
int fd,
const char * ipstr,
unsigned short port,
int timeout_ms
)

Connect a socket to a remote host.

Parameters:

  • fd Socket file descriptor
  • ipstr Human-readable IP string
  • port Port
  • timeout_ms (Approximate) amount of time in milliseconds before connection attempt is aborted. Will block for 30 seconds if timeout is set to 0.

Return: ZTS_ERR_OK if successful, ZTS_ERR_SOCKET if the function times out with no connection made, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

This convenience function exists because ZeroTier uses transport-triggered links. This means that links between peers do not exist until peers try to talk to each other. This can be a problem during connection procedures since some of the initial packets are lost. To alleviate the need to try zts_bsd_connect many times, this function will keep re-trying for you, even if no known routes exist. However, if the socket is set to non-blocking mode it will behave identically to zts_bsd_connect and return immediately upon failure.

function zts_bind#

int zts_bind(
int fd,
const char * ipstr,
unsigned short port
)

Bind a socket to a local address.

Parameters:

  • fd Socket file descriptor
  • ipstr Human-readable IP string
  • port Port

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_listen#

int zts_listen(
int fd,
int backlog
)

Listen for incoming connections on socket.

Parameters:

  • fd Socket file descriptor
  • backlog Number of backlogged connections allowed

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_accept#

int zts_accept(
int fd,
char * remote_addr,
int len,
unsigned short * port
)

Accept an incoming connection.

Parameters:

  • fd Socket file descriptor
  • remote_addr Buffer that will receive remote host IP string
  • len Size of buffer that will receive remote host IP string (must be exactly ZTS_IP_MAX_STR_LEN)
  • port Port number of the newly connected remote host (value-result)

Return: New file descriptor if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_send#

ssize_t zts_send(
int fd,
const void * buf,
size_t len,
int flags
)

Send data to remote host.

Parameters:

  • fd Socket file descriptor
  • buf Pointer to data buffer
  • len Length of data to write
  • flags (e.g. ZTS_MSG_DONTWAIT, ZTS_MSG_MORE)

Return: Number of bytes sent if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_recv#

ssize_t zts_recv(
int fd,
void * buf,
size_t len,
int flags
)

Receive data from remote host.

Parameters:

  • fd Socket file descriptor
  • buf Pointer to data buffer
  • len Length of data buffer
  • flags Specifies the type of message receipt

Return: Number of bytes received if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_read#

ssize_t zts_read(
int fd,
void * buf,
size_t len
)

Read data from socket onto buffer.

Parameters:

  • fd Socket file descriptor
  • buf Pointer to data buffer
  • len Length of data buffer to receive data

Return: Number of bytes read if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_write#

ssize_t zts_write(
int fd,
const void * buf,
size_t len
)

Write data from buffer to socket.

Parameters:

  • fd Socket file descriptor
  • buf Pointer to data buffer
  • len Length of buffer to write

Return: Number of bytes written if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_shutdown_rd#

int zts_shutdown_rd(
int fd
)

Shut down read aspect of a socket.

Parameters:

  • fd Socket file descriptor

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_shutdown_wr#

int zts_shutdown_wr(
int fd
)

Shut down write aspect of a socket.

Parameters:

  • fd Socket file descriptor

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_shutdown_rdwr#

int zts_shutdown_rdwr(
int fd
)

Shut down both read and write aspect of a socket.

Parameters:

  • fd Socket file descriptor

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_close#

int zts_close(
int fd
)

Close socket.

Parameters:

  • fd Socket file descriptor

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_getpeername#

int zts_getpeername(
int fd,
char * remote_addr_str,
int len,
unsigned short * port
)

Get the name (address) of the remote end of the socket.

Parameters:

  • fd Socket file descriptor
  • remote_addr_str Destination buffer to contain name (address) of the remote end of the socket
  • len Length of destination buffer
  • port Value-result parameter that will contain resultant port number

Return: ZTS_ERR_OK if successful, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_getsockname#

int zts_getsockname(
int fd,
char * local_addr_str,
int len,
unsigned short * port
)

Get the name (address) of the local end of the socket.

Parameters:

  • fd Socket file descriptor
  • local_addr_str Destination buffer to contain name (address) of the local end of the socket
  • len Length of destination buffer
  • port Value-result parameter that will contain resultant port number

Return: ZTS_ERR_OK if successful, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_tcp_client#

int zts_tcp_client(
const char * remote_ipstr,
unsigned short remote_port
)

A convenience function that takes a remote address IP string and creates the appropriate type of socket, and uses it to connect to a remote host.

Parameters:

  • remote_ipstr Remote address string. IPv4 or IPv6
  • remote_port Port to

Return: New file descriptor if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_tcp_server#

int zts_tcp_server(
const char * local_ipstr,
unsigned short local_port,
char * remote_ipstr,
int len,
unsigned short * remote_port
)

A convenience function that takes a remote address IP string and creates the appropriate type of socket, binds, listens, and then accepts on it.

Parameters:

  • local_ipstr Local address to bind
  • local_port Local port to bind
  • remote_ipstr String-format IP address of newly connected remote host
  • len Length of remote_ipstr
  • remote_port Port of remote host

Return: New file descriptor if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_udp_server#

int zts_udp_server(
const char * local_ipstr,
unsigned short local_port
)

A convenience function that takes a remote address IP string and creates the appropriate type of socket, and binds to it.

Parameters:

  • local_ipstr Local address to bind
  • local_port Local port to bind

Return: New file descriptor if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_udp_client#

int zts_udp_client(
const char * remote_ipstr
)

This function doesn't really do anything other than be a namespace counterpart to zts_udp_server. All this function does is create a ZTS_SOCK_DGRAM socket and return its file descriptor.

Parameters:

  • remote_ipstr Remote address string. IPv4 or IPv6

Return: New file descriptor if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_set_no_delay#

int zts_set_no_delay(
int fd,
int enabled
)

Enable or disable TCP_NODELAY. Enabling this is equivalent to turning off Nagle's algorithm.

Parameters:

  • fd Socket file descriptor
  • enabled [0, 1] integer value

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_last_socket_error#

int zts_get_last_socket_error(
int fd
)

Get the last error for the given socket.

Parameters:

  • fd Socket file descriptor

Return: Error number defined in zts_errno_t. ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_data_available#

size_t zts_get_data_available(
int fd
)

Return amount of data available to read from socket.

Parameters:

  • fd Socket file descriptor

Return: Number of bytes available to read. ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_no_delay#

int zts_get_no_delay(
int fd
)

Return whether TCP_NODELAY is enabled.

Parameters:

  • fd Socket file descriptor

Return: 1 if enabled, 0 if disabled, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_set_linger#

int zts_set_linger(
int fd,
int enabled,
int value
)

Enable or disable SO_LINGER while also setting its value.

Parameters:

  • fd Socket file descriptor
  • enabled [0, 1] integer value
  • value How long socket should linger

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_linger_enabled#

int zts_get_linger_enabled(
int fd
)

Return whether SO_LINGER is enabled.

Parameters:

  • fd Socket file descriptor

Return: 1 if enabled, 0 if disabled, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_linger_value#

int zts_get_linger_value(
int fd
)

Return the value of SO_LINGER

Parameters:

  • fd Socket file descriptor

Return: Value of SO_LINGER if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_pending_data_size#

int zts_get_pending_data_size(
int fd
)

Return the number of bytes available to read from the network buffer.

Parameters:

  • fd Socket file descriptor

Return: Number of bytes to read if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_set_reuse_addr#

int zts_set_reuse_addr(
int fd,
int enabled
)

Enable or disable SO_REUSEADDR

Parameters:

  • fd Socket file descriptor
  • enabled [0, 1] integer value

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_reuse_addr#

int zts_get_reuse_addr(
int fd
)

Return whether SO_REUSEADDR is enabled.

Parameters:

  • fd Socket file descriptor

Return: 1 if enabled, 0 if disabled, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_set_recv_timeout#

int zts_set_recv_timeout(
int fd,
int seconds,
int microseconds
)

Set the value of SO_RCVTIMEO

Parameters:

  • fd Socket file descriptor
  • seconds Number of seconds for timeout
  • microseconds Number of microseconds for timeout

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_recv_timeout#

int zts_get_recv_timeout(
int fd
)

Return the value of SO_RCVTIMEO

Parameters:

  • fd Socket file descriptor

Return: Value of SO_RCVTIMEO if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_set_send_timeout#

int zts_set_send_timeout(
int fd,
int seconds,
int microseconds
)

Set the value of SO_SNDTIMEO

Parameters:

  • fd Socket file descriptor
  • seconds Number of seconds for timeout
  • microseconds Number of microseconds for timeout

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_send_timeout#

int zts_get_send_timeout(
int fd
)

Return the value of SO_SNDTIMEO

Parameters:

  • fd Socket file descriptor

Return: Value of SO_SNDTIMEO if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_set_send_buf_size#

int zts_set_send_buf_size(
int fd,
int size
)

Set the value of SO_SNDBUF

Parameters:

  • fd Socket file descriptor
  • size Size of buffer

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_send_buf_size#

int zts_get_send_buf_size(
int fd
)

Return the value of SO_SNDBUF

Parameters:

  • fd Socket file descriptor

Return: Value of SO_SNDBUF if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_set_recv_buf_size#

int zts_set_recv_buf_size(
int fd,
int size
)

Set the value of SO_RCVBUF

Parameters:

  • fd Socket file descriptor
  • size Size of buffer

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_recv_buf_size#

int zts_get_recv_buf_size(
int fd
)

Return the value of SO_RCVBUF

Parameters:

  • fd Socket file descriptor

Return: Value of SO_RCVBUF if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_set_ttl#

int zts_set_ttl(
int fd,
int ttl
)

Set the value of IP_TTL

Parameters:

  • fd Socket file descriptor
  • ttl Value of IP_TTL

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_ttl#

int zts_get_ttl(
int fd
)

Return the value of IP_TTL

Parameters:

  • fd Socket file descriptor

Return: Value of IP_TTL[0,255] if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_set_blocking#

int zts_set_blocking(
int fd,
int enabled
)

Change blocking behavior O_NONBLOCK

Parameters:

  • fd Socket file descriptor
  • enabled [0, 1] integer value, 1 maintains default behavior, 0 sets to non-blocking mode

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_blocking#

int zts_get_blocking(
int fd
)

Return whether blocking mode O_NONBLOCK is enabled.

Parameters:

  • fd Socket file descriptor

Return: 1 if enabled, 0 if disabled, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_set_keepalive#

int zts_set_keepalive(
int fd,
int enabled
)

Enable or disable SO_KEEPALIVE

Parameters:

  • fd Socket file descriptor
  • enabled [0, 1] integer value

Return: ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_get_keepalive#

int zts_get_keepalive(
int fd
)

Return whether SO_KEEPALIVE is enabled.

Parameters:

  • fd Socket file descriptor

Return: 1 if enabled, 0 if disabled, ZTS_ERR_SERVICE if the node experiences a problem, ZTS_ERR_ARG if invalid argument. Sets zts_errno

function zts_bsd_gethostbyname#

struct zts_hostent * zts_bsd_gethostbyname(
const char * name
)

Resolve a host-name.

Parameters:

  • name A null-terminated string representing the name of the host

Return: Pointer to struct zts_hostent if successful, NULL otherwise

function zts_dns_set_server#

int zts_dns_set_server(
uint8_t index,
const zts_ip_addr * addr
)

Parameters:

  • index the index of the DNS server to set must be < DNS_MAX_SERVERS
  • addr IP address of the DNS server to set

Initialize one of the DNS servers.

function zts_dns_get_server#

const zts_ip_addr * zts_dns_get_server(
uint8_t index
)

Parameters:

  • index the index of the DNS server

Return: IP address of the indexed DNS server or ip_addr_any if the DNS server has not been configured.

Obtain one of the currently configured DNS server.

function zts_core_lock_obtain#

int zts_core_lock_obtain()

Lock the core service so that queries about addresses, routes, paths, etc. can be performed.

Return: ZTS_ERR_OK if successful. ZTS_ERR_SERVICE if the core service is unavailable.

Notice: Core locking functions are intended to be used by high-level language wrappers. Only lock the core if you know exactly what you are doing.

function zts_core_lock_release#

int zts_core_lock_release()

Lock the core service so that queries about addresses, routes, paths, etc. can be performed.

Return: ZTS_ERR_OK if successful. ZTS_ERR_SERVICE if the core service is unavailable.

Notice: Core locking functions are intended to be used by high-level language wrappers. Only lock the core if you know exactly what you are doing.

function zts_core_query_addr_count#

int zts_core_query_addr_count(
uint64_t net_id
)

Lock the core service so that queries about addresses, routes, paths, etc. can be performed.

Return: ZTS_ERR_OK if successful. ZTS_ERR_SERVICE if the core service is unavailable.

Notice: Core locking functions are intended to be used by high-level language wrappers. Only lock the core if you know exactly what you are doing. zts_core_lock_obtain() and zts_core_lock_release() must be called before and after this function.

function zts_core_query_addr#

int zts_core_query_addr(
uint64_t net_id,
unsigned int idx,
char * addr,
unsigned int len
)

Lock the core service so that queries about addresses, routes, paths, etc. can be performed.

Return: ZTS_ERR_OK if successful. ZTS_ERR_SERVICE if the core service is unavailable.

Notice: Core locking functions are intended to be used by high-level language wrappers. Only lock the core if you know exactly what you are doing. zts_core_lock_obtain() and zts_core_lock_release() must be called before and after this function.

function zts_core_query_route_count#

int zts_core_query_route_count(
uint64_t net_id
)

Lock the core service so that queries about addresses, routes, paths, etc. can be performed.

Return: ZTS_ERR_OK if successful. ZTS_ERR_SERVICE if the core service is unavailable.

Notice: Core locking functions are intended to be used by high-level language wrappers. Only lock the core if you know exactly what you are doing. zts_core_lock_obtain() and zts_core_lock_release() must be called before and after this function.

function zts_core_query_route#

int zts_core_query_route(
uint64_t net_id,
unsigned int idx,
char * target,
char * via,
unsigned int len,
uint16_t * flags,
uint16_t * metric
)

Lock the core service so that queries about addresses, routes, paths, etc. can be performed.

Return: ZTS_ERR_OK if successful. ZTS_ERR_SERVICE if the core service is unavailable.

Notice: Core locking functions are intended to be used by high-level language wrappers. Only lock the core if you know exactly what you are doing. zts_core_lock_obtain() and zts_core_lock_release() must be called before and after this function.

function zts_core_query_path_count#

int zts_core_query_path_count(
uint64_t peer_id
)

Lock the core service so that queries about addresses, routes, paths, etc. can be performed.

Return: ZTS_ERR_OK if successful. ZTS_ERR_SERVICE if the core service is unavailable.

Notice: Core locking functions are intended to be used by high-level language wrappers. Only lock the core if you know exactly what you are doing. zts_core_lock_obtain() and zts_core_lock_release() must be called before and after this function.

function zts_core_query_path#

int zts_core_query_path(
uint64_t peer_id,
unsigned int idx,
char * dst,
unsigned int len
)

Lock the core service so that queries about addresses, routes, paths, etc. can be performed.

Return: ZTS_ERR_OK if successful. ZTS_ERR_SERVICE if the core service is unavailable.

Notice: Core locking functions are intended to be used by high-level language wrappers. Only lock the core if you know exactly what you are doing. zts_core_lock_obtain() and zts_core_lock_release() must be called before and after this function.

function zts_core_query_mc_count#

int zts_core_query_mc_count(
uint64_t net_id
)

Lock the core service so that queries about addresses, routes, paths, etc. can be performed.

Return: ZTS_ERR_OK if successful. ZTS_ERR_SERVICE if the core service is unavailable.

Notice: Core locking functions are intended to be used by high-level language wrappers. Only lock the core if you know exactly what you are doing. zts_core_lock_obtain() and zts_core_lock_release() must be called before and after this function.

function zts_core_query_mc#

int zts_core_query_mc(
uint64_t net_id,
unsigned int idx,
uint64_t * mac,
uint32_t * adi
)

Lock the core service so that queries about addresses, routes, paths, etc. can be performed.

Return: ZTS_ERR_OK if successful. ZTS_ERR_SERVICE if the core service is unavailable.

Notice: Core locking functions are intended to be used by high-level language wrappers. Only lock the core if you know exactly what you are doing. zts_core_lock_obtain() and zts_core_lock_release() must be called before and after this function.

function zts_util_sign_root_set#

int zts_util_sign_root_set(
char * roots_out,
unsigned int * roots_len,
char * prev_key,
unsigned int * prev_key_len,
char * curr_key,
unsigned int * curr_key_len,
uint64_t id,
uint64_t ts,
zts_root_set_t * roots_spec
)

Generates a new root set definition.

Parameters:

  • roots_id The desired World ID (arbitrary)
  • ts Timestamp indicating when this generation took place

function zts_util_delay#

void zts_util_delay(
unsigned long milliseconds
)

Platform-agnostic delay.

Parameters:

  • milliseconds How long to delay

function zts_util_get_ip_family#

int zts_util_get_ip_family(
const char * ipstr
)

Return the family type of the IP string.

Parameters:

  • ipstr Either IPv4 or IPv6 string

Return: Either ZTS_AF_INET or ZTS_AF_INET6

function zts_util_ipstr_to_saddr#

int zts_util_ipstr_to_saddr(
const char * src_ipstr,
unsigned short port,
struct zts_sockaddr * dstaddr,
zts_socklen_t * addrlen
)

Parameters:

  • src_ipstr Source IP string
  • port Port
  • dstaddr Pointer to destination structure zts_sockaddr_in or zts_sockaddr_in6
  • addrlen Size of destination structure. Value-result: Will be set to actual size of data available

Return: return ZTS_ERR_OK on success, ZTS_ERR_ARG if invalid argument

Convert human-friendly IP string to zts_sockaddr_in or zts_sockaddr_in6.

function zts_util_ntop#

int zts_util_ntop(
struct zts_sockaddr * addr,
zts_socklen_t addrlen,
char * dst_str,
int len,
unsigned short * port
)

Similar to inet_ntop but determines family automatically and returns port as a value result parameter.

Parameters:

  • addr Pointer to address structure
  • addrlen Length of address structure
  • dst_str Destination buffer
  • len Length of destination buffer
  • port Value-result parameter that will contain resultant port number

Return: return ZTS_ERR_OK on success, ZTS_ERR_ARG if invalid argument

function zts_ipaddr_ntoa#

char * zts_ipaddr_ntoa(
const zts_ip_addr * addr
)

Parameters:

  • addr IP address in network order to convert

Return: Pointer to a global static (!) buffer that holds the ASCII representation of addr

Convert numeric IP address (both versions) into ASCII representation. returns ptr to static buffer. Not reentrant.

function zts_ipaddr_aton#

int zts_ipaddr_aton(
const char * cp,
zts_ip_addr * addr
)

Parameters:

  • cp IP address string to convert
  • addr conversion result is stored here

Return: 1 on success, 0 on error

Convert IP address string (both versions) to numeric. The version is auto-detected from the string.

function zts_inet_ntop#

const char * zts_inet_ntop(
int family,
const void * src,
char * dst,
zts_socklen_t size
)

Parameters:

  • family Address family: ZTS_AF_INET or ZTS_AF_INET6
  • src Pointer to source address structure
  • dst Pointer to destination character array
  • size Size of the destination buffer

Return: On success, returns a non-null pointer to the destination character array

Convert IPv4 and IPv6 address structures to human-readable text form.

function zts_inet_pton#

int zts_inet_pton(
int family,
const char * src,
void * dst
)

Parameters:

  • family Address family: ZTS_AF_INET or ZTS_AF_INET6
  • src Pointer to source character array
  • dst Pointer to destination address structure

Return: return 1 on success. 0 or -1 on failure. (Does not follow regular zts_* conventions)

Convert C-string IPv4 and IPv6 addresses to binary form.

Attributes Documentation#

variable zts_errno#

int zts_errno;

Error variable set after each zts_* socket call. Provides additional error context.

Macro Documentation#

define ZTS_MAC_ADDRSTRLEN#

#define ZTS_MAC_ADDRSTRLEN 18

Length of human-readable MAC address string

define ZTS_INET_ADDRSTRLEN#

#define ZTS_INET_ADDRSTRLEN 16

Max length of human-readable IPv4 string

define ZTS_INET6_ADDRSTRLEN#

#define ZTS_INET6_ADDRSTRLEN 46

Max length of human-readable IPv6 string

define ZTS_IP_MAX_STR_LEN#

#define ZTS_IP_MAX_STR_LEN <a href="/autogen/libzt/files/_zero_tier_sockets_8h.md#define-zts_inet6_addrstrlen">ZTS_INET6_ADDRSTRLEN</a>

Maximum (and required) length of string buffers used to receive string-format IP addresses from the API. This is set to ZTS_INET6_ADDRSTRLEN to handle all cases: ZTS_AF_INET and ZTS_AF_INET6

define ZTS_STORE_DATA_LEN#

#define ZTS_STORE_DATA_LEN 4096

Required buffer length to safely receive data store items

define ZTS_MAX_NETWORK_SHORT_NAME_LENGTH#

#define ZTS_MAX_NETWORK_SHORT_NAME_LENGTH 127

Maximum length of network short name

define ZTS_MAX_NETWORK_ROUTES#

#define ZTS_MAX_NETWORK_ROUTES 32

Maximum number of pushed routes on a network

define ZTS_MAX_ASSIGNED_ADDRESSES#

#define ZTS_MAX_ASSIGNED_ADDRESSES 16

Maximum number of statically assigned IP addresses per network endpoint using ZT address management (not DHCP)

define ZTS_MAX_PEER_NETWORK_PATHS#

#define ZTS_MAX_PEER_NETWORK_PATHS 16

Maximum number of direct network paths to a given peer

define ZTS_MAX_MULTICAST_SUBSCRIPTIONS#

#define ZTS_MAX_MULTICAST_SUBSCRIPTIONS 1024

Maximum number of multicast groups a device / network interface can be subscribed to at once

define ZTS_MAX_ENDPOINT_STR_LEN#

#define ZTS_MAX_ENDPOINT_STR_LEN <a href="/autogen/libzt/files/_zero_tier_sockets_8h.md#define-zts_inet6_addrstrlen">ZTS_INET6_ADDRSTRLEN</a> + 6

define ZTS_C_API_ONLY#

#define ZTS_C_API_ONLY 1

define ZTS_IPADDR_NONE#

#define ZTS_IPADDR_NONE ((uint32_t)0xffffffffUL)

255.255.255.255

define ZTS_IPADDR_LOOPBACK#

#define ZTS_IPADDR_LOOPBACK ((uint32_t)0x7f000001UL)

127.0.0.1

define ZTS_IPADDR_ANY#

#define ZTS_IPADDR_ANY ((uint32_t)0x00000000UL)

0.0.0.0

define ZTS_IPADDR_BROADCAST#

#define ZTS_IPADDR_BROADCAST ((uint32_t)0xffffffffUL)

255.255.255.255

define ZTS_INADDR_NONE#

#define ZTS_INADDR_NONE <a href="/autogen/libzt/files/_zero_tier_sockets_8h.md#define-zts_ipaddr_none">ZTS_IPADDR_NONE</a>

255.255.255.255

define ZTS_INADDR_LOOPBACK#

#define ZTS_INADDR_LOOPBACK <a href="/autogen/libzt/files/_zero_tier_sockets_8h.md#define-zts_ipaddr_loopback">ZTS_IPADDR_LOOPBACK</a>

127.0.0.1

define ZTS_INADDR_ANY#

#define ZTS_INADDR_ANY <a href="/autogen/libzt/files/_zero_tier_sockets_8h.md#define-zts_ipaddr_any">ZTS_IPADDR_ANY</a>

0.0.0.0

define ZTS_INADDR_BROADCAST#

#define ZTS_INADDR_BROADCAST <a href="/autogen/libzt/files/_zero_tier_sockets_8h.md#define-zts_ipaddr_broadcast">ZTS_IPADDR_BROADCAST</a>

255.255.255.255

define ZTS_SOCK_STREAM#

#define ZTS_SOCK_STREAM 0x0001

define ZTS_SOCK_DGRAM#

#define ZTS_SOCK_DGRAM 0x0002

define ZTS_SOCK_RAW#

#define ZTS_SOCK_RAW 0x0003

define ZTS_AF_UNSPEC#

#define ZTS_AF_UNSPEC 0x0000

define ZTS_AF_INET#

#define ZTS_AF_INET 0x0002

define ZTS_AF_INET6#

#define ZTS_AF_INET6 0x000a

define ZTS_PF_INET#

#define ZTS_PF_INET ZTS_AF_INET

define ZTS_PF_INET6#

#define ZTS_PF_INET6 ZTS_AF_INET6

define ZTS_PF_UNSPEC#

#define ZTS_PF_UNSPEC ZTS_AF_UNSPEC

define ZTS_IPPROTO_IP#

#define ZTS_IPPROTO_IP 0x0000

define ZTS_IPPROTO_ICMP#

#define ZTS_IPPROTO_ICMP 0x0001

define ZTS_IPPROTO_TCP#

#define ZTS_IPPROTO_TCP 0x0006

define ZTS_IPPROTO_UDP#

#define ZTS_IPPROTO_UDP 0x0011

define ZTS_IPPROTO_IPV6#

#define ZTS_IPPROTO_IPV6 0x0029

define ZTS_IPPROTO_ICMPV6#

#define ZTS_IPPROTO_ICMPV6 0x003a

define ZTS_IPPROTO_UDPLITE#

#define ZTS_IPPROTO_UDPLITE 0x0088

define ZTS_IPPROTO_RAW#

#define ZTS_IPPROTO_RAW 0x00ff

define ZTS_MSG_PEEK#

#define ZTS_MSG_PEEK 0x0001

define ZTS_MSG_WAITALL#

#define ZTS_MSG_WAITALL 0x0002

define ZTS_MSG_OOB#

#define ZTS_MSG_OOB 0x0004

define ZTS_MSG_DONTWAIT#

#define ZTS_MSG_DONTWAIT 0x0008

define ZTS_MSG_MORE#

#define ZTS_MSG_MORE 0x0010

define ZTS_IOCPARM_MASK#

#define ZTS_IOCPARM_MASK 0x7fU

define ZTS_IOC_VOID#

#define ZTS_IOC_VOID 0x20000000UL

define ZTS_IOC_OUT#

#define ZTS_IOC_OUT 0x40000000UL

define ZTS_IOC_IN#

#define ZTS_IOC_IN 0x80000000UL

define ZTS_IOC_INOUT#

#define ZTS_IOC_INOUT (ZTS_IOC_IN | ZTS_IOC_OUT)

define ZTS_IO#

#define ZTS_IO(
x,
y
)
(ZTS_IOC_VOID | ((x) << 8) | (y))

define ZTS_IOR#

#define ZTS_IOR(
x,
y,
t
)
(ZTS_IOC_OUT | (((long)sizeof(t) & ZTS_IOCPARM_MASK) << 16) | ((x) << 8) | (y))

define ZTS_IOW#

#define ZTS_IOW(
x,
y,
t
)
(ZTS_IOC_IN | (((long)sizeof(t) & ZTS_IOCPARM_MASK) << 16) | ((x) << 8) | (y))

define ZTS_FIONREAD#

#define ZTS_FIONREAD ZTS_IOR('f', 127, unsigned long)

define ZTS_FIONBIO#

#define ZTS_FIONBIO ZTS_IOW('f', 126, unsigned long)

define SIN_ZERO_LEN#

#define SIN_ZERO_LEN 8

define ZTS_MAX_NUM_ROOTS#

#define ZTS_MAX_NUM_ROOTS 16

define ZTS_MAX_ENDPOINTS_PER_ROOT#

#define ZTS_MAX_ENDPOINTS_PER_ROOT 32

define ZTS_API#

#define ZTS_API

define ZTCALL#

#define ZTCALL

define ZTS_DISABLE_CENTRAL_API#

#define ZTS_DISABLE_CENTRAL_API 1

define ZTS_ID_STR_BUF_LEN#

#define ZTS_ID_STR_BUF_LEN 384

The length of a human-friendly identity key pair string

define ZTS_SOL_SOCKET#

#define ZTS_SOL_SOCKET 0x0fff

define ZTS_SO_DEBUG#

#define ZTS_SO_DEBUG 0x0001

define ZTS_SO_ACCEPTCONN#

#define ZTS_SO_ACCEPTCONN 0x0002

define ZTS_SO_REUSEADDR#

#define ZTS_SO_REUSEADDR 0x0004

define ZTS_SO_KEEPALIVE#

#define ZTS_SO_KEEPALIVE 0x0008

define ZTS_SO_DONTROUTE#

#define ZTS_SO_DONTROUTE 0x0010

define ZTS_SO_BROADCAST#

#define ZTS_SO_BROADCAST 0x0020

define ZTS_SO_USELOOPBACK#

#define ZTS_SO_USELOOPBACK 0x0040

define ZTS_SO_LINGER#

#define ZTS_SO_LINGER 0x0080

define ZTS_SO_DONTLINGER#

#define ZTS_SO_DONTLINGER ((int)(~ZTS_SO_LINGER))

define ZTS_SO_OOBINLINE#

#define ZTS_SO_OOBINLINE 0x0100

define ZTS_SO_REUSEPORT#

#define ZTS_SO_REUSEPORT 0x0200

define ZTS_SO_SNDBUF#

#define ZTS_SO_SNDBUF 0x1001

define ZTS_SO_RCVBUF#

#define ZTS_SO_RCVBUF 0x1002

define ZTS_SO_SNDLOWAT#

#define ZTS_SO_SNDLOWAT 0x1003

define ZTS_SO_RCVLOWAT#

#define ZTS_SO_RCVLOWAT 0x1004

define ZTS_SO_SNDTIMEO#

#define ZTS_SO_SNDTIMEO 0x1005

define ZTS_SO_RCVTIMEO#

#define ZTS_SO_RCVTIMEO 0x1006

define ZTS_SO_ERROR#

#define ZTS_SO_ERROR 0x1007

define ZTS_SO_TYPE#

#define ZTS_SO_TYPE 0x1008

define ZTS_SO_CONTIMEO#

#define ZTS_SO_CONTIMEO 0x1009

define ZTS_SO_NO_CHECK#

#define ZTS_SO_NO_CHECK 0x100a

define ZTS_SO_BINDTODEVICE#

#define ZTS_SO_BINDTODEVICE 0x100b

define ZTS_IP_TOS#

#define ZTS_IP_TOS 0x0001

define ZTS_IP_TTL#

#define ZTS_IP_TTL 0x0002

define ZTS_IP_PKTINFO#

#define ZTS_IP_PKTINFO 0x0008

define ZTS_TCP_NODELAY#

#define ZTS_TCP_NODELAY 0x0001

define ZTS_TCP_KEEPALIVE#

#define ZTS_TCP_KEEPALIVE 0x0002

define ZTS_TCP_KEEPIDLE#

#define ZTS_TCP_KEEPIDLE 0x0003

define ZTS_TCP_KEEPINTVL#

#define ZTS_TCP_KEEPINTVL 0x0004

define ZTS_TCP_KEEPCNT#

#define ZTS_TCP_KEEPCNT 0x0005

define ZTS_IPV6_CHECKSUM#

#define ZTS_IPV6_CHECKSUM 0x0007 /* RFC3542: calculate and insert the ICMPv6 checksum for raw \
sockets. */

define ZTS_IPV6_V6ONLY#

#define ZTS_IPV6_V6ONLY 0x001b /* RFC3493: boolean control to restrict ZTS_AF_INET6 sockets to \
IPv6 communications only. */

define ZTS_UDPLITE_SEND_CSCOV#

#define ZTS_UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */

define ZTS_UDPLITE_RECV_CSCOV#

#define ZTS_UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */

define ZTS_IP_MULTICAST_TTL#

#define ZTS_IP_MULTICAST_TTL 5

define ZTS_IP_MULTICAST_IF#

#define ZTS_IP_MULTICAST_IF 6

define ZTS_IP_MULTICAST_LOOP#

#define ZTS_IP_MULTICAST_LOOP 7

define ZTS_IP_ADD_MEMBERSHIP#

#define ZTS_IP_ADD_MEMBERSHIP 3

define ZTS_IP_DROP_MEMBERSHIP#

#define ZTS_IP_DROP_MEMBERSHIP 4

define ZTS_IPV6_JOIN_GROUP#

#define ZTS_IPV6_JOIN_GROUP 12

define ZTS_IPV6_ADD_MEMBERSHIP#

#define ZTS_IPV6_ADD_MEMBERSHIP ZTS_IPV6_JOIN_GROUP

define ZTS_IPV6_LEAVE_GROUP#

#define ZTS_IPV6_LEAVE_GROUP 13

define ZTS_IPV6_DROP_MEMBERSHIP#

#define ZTS_IPV6_DROP_MEMBERSHIP ZTS_IPV6_LEAVE_GROUP

define ZTS_IPTOS_TOS_MASK#

#define ZTS_IPTOS_TOS_MASK 0x1E

define ZTS_IPTOS_TOS#

#define ZTS_IPTOS_TOS(
tos
)
((tos)&ZTS_IPTOS_TOS_MASK)

define ZTS_IPTOS_LOWDELAY#

#define ZTS_IPTOS_LOWDELAY 0x10

define ZTS_IPTOS_THROUGHPUT#

#define ZTS_IPTOS_THROUGHPUT 0x08

define ZTS_IPTOS_RELIABILITY#

#define ZTS_IPTOS_RELIABILITY 0x04

define ZTS_IPTOS_LOWCOST#

#define ZTS_IPTOS_LOWCOST 0x02

define ZTS_IPTOS_MINCOST#

#define ZTS_IPTOS_MINCOST ZTS_IPTOS_LOWCOST

define ZTS_IPTOS_PREC_MASK#

#define ZTS_IPTOS_PREC_MASK 0xe0

define ZTS_IPTOS_PREC#

#define ZTS_IPTOS_PREC(
tos
)
((tos)&ZTS_IPTOS_PREC_MASK)

define ZTS_IPTOS_PREC_NETCONTROL#

#define ZTS_IPTOS_PREC_NETCONTROL 0xe0

define ZTS_IPTOS_PREC_INTERNETCONTROL#

#define ZTS_IPTOS_PREC_INTERNETCONTROL 0xc0

define ZTS_IPTOS_PREC_CRITIC_ECP#

#define ZTS_IPTOS_PREC_CRITIC_ECP 0xa0

define ZTS_IPTOS_PREC_FLASHOVERRIDE#

#define ZTS_IPTOS_PREC_FLASHOVERRIDE 0x80

define ZTS_IPTOS_PREC_FLASH#

#define ZTS_IPTOS_PREC_FLASH 0x60

define ZTS_IPTOS_PREC_IMMEDIATE#

#define ZTS_IPTOS_PREC_IMMEDIATE 0x40

define ZTS_IPTOS_PREC_PRIORITY#

#define ZTS_IPTOS_PREC_PRIORITY 0x20

define ZTS_IPTOS_PREC_ROUTINE#

#define ZTS_IPTOS_PREC_ROUTINE 0x00

define LWIP_SOCKET_OFFSET#

#define LWIP_SOCKET_OFFSET 0

define MEMP_NUM_NETCONN#

#define MEMP_NUM_NETCONN 1024

define ZTS_FD_SETSIZE#

#define ZTS_FD_SETSIZE MEMP_NUM_NETCONN

define ZTS_FDSETSAFESET#

#define ZTS_FDSETSAFESET(
n,
code
)
do { \
if (((n)-LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n)-LWIP_SOCKET_OFFSET) >= 0)) { \
code; \
} \
} while (0)

define ZTS_FDSETSAFEGET#

#define ZTS_FDSETSAFEGET(
n,
code
)
(((n)-LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n)-LWIP_SOCKET_OFFSET) >= 0) ? (code) : 0)

define ZTS_FD_SET#

#define ZTS_FD_SET(
n,
p
)
ZTS_FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] |= (1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))

define ZTS_FD_CLR#

#define ZTS_FD_CLR(
n,
p
)
ZTS_FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] &= ~(1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))

define ZTS_FD_ISSET#

#define ZTS_FD_ISSET(
n,
p
)
ZTS_FDSETSAFEGET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] & (1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))

define ZTS_FD_ZERO#

#define ZTS_FD_ZERO(
p
)
memset((void*)(p), 0, sizeof(*(p)))

define ZTS_F_GETFL#

#define ZTS_F_GETFL 0x0003

define ZTS_F_SETFL#

#define ZTS_F_SETFL 0x0004

define ZTS_O_NONBLOCK#

#define ZTS_O_NONBLOCK 1

define ZTS_O_NDELAY#

#define ZTS_O_NDELAY ZTS_O_NONBLOCK

define ZTS_O_RDONLY#

#define ZTS_O_RDONLY 2

define ZTS_O_WRONLY#

#define ZTS_O_WRONLY 4

define ZTS_O_RDWR#

#define ZTS_O_RDWR (ZTS_O_RDONLY | ZTS_O_WRONLY)

define ZTS_POLLIN#

#define ZTS_POLLIN 0x001

define ZTS_POLLOUT#

#define ZTS_POLLOUT 0x002

define ZTS_POLLERR#

#define ZTS_POLLERR 0x004

define ZTS_POLLNVAL#

#define ZTS_POLLNVAL 0x008

define ZTS_POLLRDNORM#

#define ZTS_POLLRDNORM 0x010

define ZTS_POLLRDBAND#

#define ZTS_POLLRDBAND 0x020

define ZTS_POLLPRI#

#define ZTS_POLLPRI 0x040

define ZTS_POLLWRNORM#

#define ZTS_POLLWRNORM 0x080

define ZTS_POLLWRBAND#

#define ZTS_POLLWRBAND 0x100

define ZTS_POLLHUP#

#define ZTS_POLLHUP 0x200

define ZTS_MSG_TRUNC#

#define ZTS_MSG_TRUNC 0x04

define ZTS_MSG_CTRUNC#

#define ZTS_MSG_CTRUNC 0x08

define ZTS_SHUT_RD#

#define ZTS_SHUT_RD 0x0

define ZTS_SHUT_WR#

#define ZTS_SHUT_WR 0x1

define ZTS_SHUT_RDWR#

#define ZTS_SHUT_RDWR 0x2

define h_addr#

#define h_addr h_addr_list[0] /* for backward compatibility */

Source code#

/*
* Copyright (c)2013-2021 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2026-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
#ifndef ZTS_SOCKETS_H
#define ZTS_SOCKETS_H
#ifdef __cplusplus
extern "C" {
#endif
//----------------------------------------------------------------------------//
// Error codes //
//----------------------------------------------------------------------------//
typedef enum {
ZTS_ERR_OK = 0,
ZTS_ERR_SOCKET = -1,
ZTS_ERR_SERVICE = -2,
ZTS_ERR_ARG = -3,
ZTS_ERR_NO_RESULT = -4,
ZTS_ERR_GENERAL = -5
} zts_error_t;
//----------------------------------------------------------------------------//
// Event codes //
//----------------------------------------------------------------------------//
typedef enum {
ZTS_EVENT_NODE_UP = 200,
ZTS_EVENT_NODE_ONLINE = 201,
ZTS_EVENT_NODE_OFFLINE = 202,
ZTS_EVENT_NODE_DOWN = 203,
ZTS_EVENT_NODE_FATAL_ERROR = 204,
ZTS_EVENT_NETWORK_NOT_FOUND = 210,
ZTS_EVENT_NETWORK_CLIENT_TOO_OLD = 211,
ZTS_EVENT_NETWORK_REQ_CONFIG = 212,
ZTS_EVENT_NETWORK_OK = 213,
ZTS_EVENT_NETWORK_ACCESS_DENIED = 214,
ZTS_EVENT_NETWORK_READY_IP4 = 215,
ZTS_EVENT_NETWORK_READY_IP6 = 216,
ZTS_EVENT_NETWORK_READY_IP4_IP6 = 217,
ZTS_EVENT_NETWORK_DOWN = 218,
ZTS_EVENT_NETWORK_UPDATE = 219,
ZTS_EVENT_STACK_UP = 220,
ZTS_EVENT_STACK_DOWN = 221,
ZTS_EVENT_NETIF_UP = 230,
ZTS_EVENT_NETIF_DOWN = 231,
ZTS_EVENT_NETIF_REMOVED = 232,
ZTS_EVENT_NETIF_LINK_UP = 233,
ZTS_EVENT_NETIF_LINK_DOWN = 234,
ZTS_EVENT_PEER_DIRECT = 240,
ZTS_EVENT_PEER_RELAY = 241,
ZTS_EVENT_PEER_UNREACHABLE = 242,
ZTS_EVENT_PEER_PATH_DISCOVERED = 243,
ZTS_EVENT_PEER_PATH_DEAD = 244,
ZTS_EVENT_ROUTE_ADDED = 250,
ZTS_EVENT_ROUTE_REMOVED = 251,
ZTS_EVENT_ADDR_ADDED_IP4 = 260,
ZTS_EVENT_ADDR_REMOVED_IP4 = 261,
ZTS_EVENT_ADDR_ADDED_IP6 = 262,
ZTS_EVENT_ADDR_REMOVED_IP6 = 263,
ZTS_EVENT_STORE_IDENTITY_SECRET = 270,
ZTS_EVENT_STORE_IDENTITY_PUBLIC = 271,
ZTS_EVENT_STORE_PLANET = 272,
ZTS_EVENT_STORE_PEER = 273,
ZTS_EVENT_STORE_NETWORK = 274
} zts_event_t;
//----------------------------------------------------------------------------//
// zts_errno Error codes //
//----------------------------------------------------------------------------//
extern int zts_errno;
typedef enum {
ZTS_EPERM = 1,
ZTS_ENOENT = 2,
ZTS_ESRCH = 3,
ZTS_EINTR = 4,
ZTS_EIO = 5,
ZTS_ENXIO = 6,
ZTS_EBADF = 9,
ZTS_EAGAIN = 11,
ZTS_EWOULDBLOCK = ZTS_EAGAIN,
ZTS_ENOMEM = 12,
ZTS_EACCES = 13,
ZTS_EFAULT = 14,
ZTS_EBUSY = 16,
ZTS_EEXIST = 17,
ZTS_ENODEV = 19,
ZTS_EINVAL = 22,
ZTS_ENFILE = 23,
ZTS_EMFILE = 24,
ZTS_ENOSYS = 38,
ZTS_ENOTSOCK = 88,
ZTS_EDESTADDRREQ = 89,
ZTS_EMSGSIZE = 90,
ZTS_EPROTOTYPE = 91,
ZTS_ENOPROTOOPT = 92,
ZTS_EPROTONOSUPPORT = 93,
ZTS_ESOCKTNOSUPPORT = 94,
ZTS_EOPNOTSUPP = 95,
ZTS_EPFNOSUPPORT = 96,
ZTS_EAFNOSUPPORT = 97,
ZTS_EADDRINUSE = 98,
ZTS_EADDRNOTAVAIL = 99,
ZTS_ENETDOWN = 100,
ZTS_ENETUNREACH = 101,
ZTS_ECONNABORTED = 103,
ZTS_ECONNRESET = 104,
ZTS_ENOBUFS = 105,
ZTS_EISCONN = 106,
ZTS_ENOTCONN = 107,
ZTS_ETIMEDOUT = 110,
ZTS_EHOSTUNREACH = 113,
ZTS_EALREADY = 114,
ZTS_EINPROGRESS = 115
} zts_errno_t;
//----------------------------------------------------------------------------//
// Misc definitions //
//----------------------------------------------------------------------------//
#define ZTS_MAC_ADDRSTRLEN 18
#define ZTS_INET_ADDRSTRLEN 16
#define ZTS_INET6_ADDRSTRLEN 46
#define ZTS_IP_MAX_STR_LEN ZTS_INET6_ADDRSTRLEN
#define ZTS_STORE_DATA_LEN 4096
#define ZTS_MAX_NETWORK_SHORT_NAME_LENGTH 127
#define ZTS_MAX_NETWORK_ROUTES 32
#define ZTS_MAX_ASSIGNED_ADDRESSES 16
#define ZTS_MAX_PEER_NETWORK_PATHS 16
#define ZTS_MAX_MULTICAST_SUBSCRIPTIONS 1024
#define ZTS_MAX_ENDPOINT_STR_LEN ZTS_INET6_ADDRSTRLEN + 6
//----------------------------------------------------------------------------//
// Misc //
//----------------------------------------------------------------------------//
#if ! defined(ZTS_ENABLE_PYTHON) && ! defined(ZTS_ENABLE_PINVOKE) && ! defined(ZTS_ENABLE_JAVA)
#define ZTS_C_API_ONLY 1
#endif
#if ! ZTS_NO_STDINT_H
#include <stdint.h>
#endif
#if defined(_MSC_VER)
#ifndef ssize_t
// TODO: Should be SSIZE_T, would require lwIP patch
// #include <BaseTsd.h>
// typedef SSIZE_T ssize_t;
typedef int ssize_t;
#endif
#else
#include <unistd.h>
#endif
#ifdef ZTS_ENABLE_PINVOKE
// Used by P/INVOKE wrappers
typedef void (*CppCallback)(void* msg);
#endif
//----------------------------------------------------------------------------//
// Common definitions and structures for interoperability between zts_* and //
// lwIP functions. Some of the code in the following section is a borrowed //
// from the lwIP codebase so that the user doesn't need to include headers //
// from that project in addition to the ZeroTier SDK headers. The license //
// applying to this code borrowed from lwIP is produced below and only //
// applies to the portions of code which are merely renamed versions of //
// their lwIP counterparts. The rest of the code in this C API file is //
// governed by the license text provided at the beginning of this file. //
//----------------------------------------------------------------------------//
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
#define ZTS_IPADDR_NONE ((uint32_t)0xffffffffUL)
#define ZTS_IPADDR_LOOPBACK ((uint32_t)0x7f000001UL)
#define ZTS_IPADDR_ANY ((uint32_t)0x00000000UL)
#define ZTS_IPADDR_BROADCAST ((uint32_t)0xffffffffUL)
#define ZTS_INADDR_NONE ZTS_IPADDR_NONE
#define ZTS_INADDR_LOOPBACK ZTS_IPADDR_LOOPBACK
#define ZTS_INADDR_ANY ZTS_IPADDR_ANY
#define ZTS_INADDR_BROADCAST ZTS_IPADDR_BROADCAST
// Socket protocol types
#define ZTS_SOCK_STREAM 0x0001
#define ZTS_SOCK_DGRAM 0x0002
#define ZTS_SOCK_RAW 0x0003
// Socket family types
#define ZTS_AF_UNSPEC 0x0000
#define ZTS_AF_INET 0x0002
#define ZTS_AF_INET6 0x000a
#define ZTS_PF_INET ZTS_AF_INET
#define ZTS_PF_INET6 ZTS_AF_INET6
#define ZTS_PF_UNSPEC ZTS_AF_UNSPEC
// Protocol command types
#define ZTS_IPPROTO_IP 0x0000
#define ZTS_IPPROTO_ICMP 0x0001
#define ZTS_IPPROTO_TCP 0x0006
#define ZTS_IPPROTO_UDP 0x0011
#define ZTS_IPPROTO_IPV6 0x0029
#define ZTS_IPPROTO_ICMPV6 0x003a
#define ZTS_IPPROTO_UDPLITE 0x0088
#define ZTS_IPPROTO_RAW 0x00ff
// send() and recv() flags
#define ZTS_MSG_PEEK 0x0001
#define ZTS_MSG_WAITALL 0x0002 // NOT YET SUPPORTED
#define ZTS_MSG_OOB 0x0004 // NOT YET SUPPORTED
#define ZTS_MSG_DONTWAIT 0x0008
#define ZTS_MSG_MORE 0x0010
// Macro's for defining ioctl() command values
#define ZTS_IOCPARM_MASK 0x7fU
#define ZTS_IOC_VOID 0x20000000UL
#define ZTS_IOC_OUT 0x40000000UL
#define ZTS_IOC_IN 0x80000000UL
#define ZTS_IOC_INOUT (ZTS_IOC_IN | ZTS_IOC_OUT)
#define ZTS_IO(x, y) (ZTS_IOC_VOID | ((x) << 8) | (y))
#define ZTS_IOR(x, y, t) (ZTS_IOC_OUT | (((long)sizeof(t) & ZTS_IOCPARM_MASK) << 16) | ((x) << 8) | (y))
#define ZTS_IOW(x, y, t) (ZTS_IOC_IN | (((long)sizeof(t) & ZTS_IOCPARM_MASK) << 16) | ((x) << 8) | (y))
// ioctl() commands
#define ZTS_FIONREAD ZTS_IOR('f', 127, unsigned long)
#define ZTS_FIONBIO ZTS_IOW('f', 126, unsigned long)
//----------------------------------------------------------------------------//
// Custom but still mostly standard socket interface structures //
//----------------------------------------------------------------------------//
typedef uint32_t zts_socklen_t;
typedef uint32_t zts_in_addr_t;
typedef uint16_t zts_in_port_t;
typedef uint8_t zts_sa_family_t;
struct zts_in_addr {
#if defined(_WIN32)
zts_in_addr_t S_addr;
#else
// A definition in winsock may conflict with s_addr
zts_in_addr_t s_addr;
#endif
};
struct zts_in6_addr {
union un {
uint32_t u32_addr[4];
uint8_t u8_addr[16];
} un;
//#define s6_addr un.u8_addr
};
struct zts_sockaddr_in {
uint8_t sin_len;
zts_sa_family_t sin_family;
zts_in_port_t sin_port;
struct zts_in_addr sin_addr;
#define SIN_ZERO_LEN 8
char sin_zero[SIN_ZERO_LEN];
};
struct zts_sockaddr_in6 {
uint8_t sin6_len; // length of this structure
zts_sa_family_t sin6_family; // ZTS_AF_INET6
zts_in_port_t sin6_port; // Transport layer port #
uint32_t sin6_flowinfo; // IPv6 flow information
struct zts_in6_addr sin6_addr; // IPv6 address
uint32_t sin6_scope_id; // Set of interfaces for scope
};
struct zts_sockaddr {
uint8_t sa_len;
zts_sa_family_t sa_family;
char sa_data[14];
};
struct zts_sockaddr_storage {
uint8_t s2_len;
zts_sa_family_t ss_family;
char s2_data1[2];
uint32_t s2_data2[3];
uint32_t s2_data3[3];
};
//----------------------------------------------------------------------------//
// Callback Structures //
//----------------------------------------------------------------------------//
typedef struct {
uint64_t node_id;
uint16_t port_primary;
uint16_t port_secondary;
uint16_t port_tertiary;
uint8_t ver_major;
uint8_t ver_minor;
uint8_t ver_rev;
} zts_node_info_t;
typedef struct {
uint64_t net_id;
struct zts_sockaddr_storage addr;
} zts_addr_info_t;
typedef enum {
ZTS_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0,
ZTS_NETWORK_STATUS_OK = 1,
ZTS_NETWORK_STATUS_ACCESS_DENIED = 2,
ZTS_NETWORK_STATUS_NOT_FOUND = 3,
ZTS_NETWORK_STATUS_PORT_ERROR = 4,
ZTS_NETWORK_STATUS_CLIENT_TOO_OLD = 5
} zts_network_status_t;
typedef enum {
ZTS_NETWORK_TYPE_PRIVATE = 0,
ZTS_NETWORK_TYPE_PUBLIC = 1
} zts_net_info_type_t;
typedef struct {
struct zts_sockaddr_storage target;
struct zts_sockaddr_storage via;
uint16_t flags;
uint16_t metric;
} zts_route_info_t;
typedef struct {
uint64_t mac;
unsigned long adi;
} zts_multicast_group_t;
typedef enum {
ZTS_PEER_ROLE_LEAF = 0,
ZTS_PEER_ROLE_MOON = 1,
ZTS_PEER_ROLE_PLANET = 2
} zts_peer_role_t;
typedef struct {
uint64_t net_id;
uint64_t mac;
char name[ZTS_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
zts_network_status_t status;
zts_net_info_type_t type;
unsigned int mtu;
int dhcp;
int bridge;
int broadcast_enabled;
int port_error;
unsigned long netconf_rev;
unsigned int assigned_addr_count;
struct zts_sockaddr_storage assigned_addrs[ZTS_MAX_ASSIGNED_ADDRESSES];
unsigned int route_count;
zts_route_info_t routes[ZTS_MAX_NETWORK_ROUTES];
unsigned int multicast_sub_count;
struct {
uint64_t mac; /* MAC in lower 48 bits */
uint32_t adi; /* Additional distinguishing information, usually zero
except for IPv4 ARP groups */
} multicast_subs[ZTS_MAX_MULTICAST_SUBSCRIPTIONS];
} zts_net_info_t;
typedef struct {
struct zts_sockaddr_storage address;
uint64_t last_tx;
uint64_t last_rx;
uint64_t trusted_path_id;
float latency;
float unused_0;
float unused_1;
float unused_2;
float unused_3;
float unused_4;
uint64_t unused_5;
uint64_t unused_6;
float unused_7;
char* ifname;
int expired;
int preferred;
} zts_path_t;
typedef struct {
uint64_t peer_id;
int ver_major;
int ver_minor;
int ver_rev;
int latency;
zts_peer_role_t role;
unsigned int path_count;
int unused_0;
zts_path_t paths[ZTS_MAX_PEER_NETWORK_PATHS];
} zts_peer_info_t;
#define ZTS_MAX_NUM_ROOTS 16
#define ZTS_MAX_ENDPOINTS_PER_ROOT 32
typedef struct {
char* public_id_str[ZTS_MAX_NUM_ROOTS];
char* endpoint_ip_str[ZTS_MAX_NUM_ROOTS][ZTS_MAX_ENDPOINTS_PER_ROOT];
} zts_root_set_t;
typedef struct {
uint64_t net_id;
uint64_t mac;
int mtu;
} zts_netif_info_t;
typedef struct {
int16_t event_code;
zts_node_info_t* node;
zts_net_info_t* network;
zts_netif_info_t* netif;
zts_route_info_t* route;
zts_peer_info_t* peer;
zts_addr_info_t* addr;
void* cache;
int len;
} zts_event_msg_t;
//----------------------------------------------------------------------------//
// Python Bindings (Subset of regular socket API) //
//----------------------------------------------------------------------------//
#ifdef ZTS_ENABLE_PYTHON
#include "Python.h"
class PythonDirectorCallbackClass {
public:
virtual void on_zerotier_event(zts_event_msg_t* msg);
virtual ~PythonDirectorCallbackClass() {};
};
extern PythonDirectorCallbackClass* _userEventCallback;
int zts_py_bind(int fd, int family, int type, PyObject* addro);
int zts_py_connect(int fd, int family, int type, PyObject* addro);
PyObject* zts_py_accept(int fd);
int zts_py_listen(int fd, int backlog);
PyObject* zts_py_recv(int fd, int len, int flags);
int zts_py_send(int fd, PyObject* buf, int flags);
int zts_py_close(int fd);
int zts_py_setblocking(int fd, int flag);
int zts_py_getblocking(int fd);
#endif // ZTS_ENABLE_PYTHON
//----------------------------------------------------------------------------//
// ZeroTier Service and Network Controls //
//----------------------------------------------------------------------------//
#if defined(_WIN32)
#ifdef ADD_EXPORTS
#define ZTS_API __declspec(dllexport)
#else
#define ZTS_API __declspec(dllimport)
#endif
#define ZTCALL __cdecl
#else
#define ZTS_API
#define ZTCALL
#endif
//----------------------------------------------------------------------------//
// Central API //
//----------------------------------------------------------------------------//
#define ZTS_DISABLE_CENTRAL_API 1
#ifndef ZTS_DISABLE_CENTRAL_API
#define ZTS_CENTRAL_DEFAULT_URL "https://my.zerotier.com"
#define ZTS_CENRTAL_MAX_URL_LEN 128
#define ZTS_CENTRAL_TOKEN_LEN 32
#define ZTS_CENTRAL_RESP_BUF_DEFAULT_SZ (128 * 1024)
#define ZTS_HTTP_GET 0
#define ZTS_HTTP_POST 1
#define ZTS_HTTP_DELETE 2
#define ZTS_CENTRAL_NODE_AUTH_FALSE 0
#define ZTS_CENTRAL_NODE_AUTH_TRUE 1
#define ZTS_CENTRAL_READ 1
#define ZTS_CENTRAL_WRITE 2
ZTS_API int ZTCALL zts_central_set_access_mode(int8_t modes);
ZTS_API int ZTCALL zts_central_set_verbose(int8_t is_verbose);
ZTS_API void ZTCALL zts_central_clear_resp_buf();
ZTS_API int ZTCALL zts_central_init(const char* url_str, const char* token_str, char* resp_buf, uint32_t buf_len);
ZTS_API void ZTCALL zts_central_cleanup();
ZTS_API int ZTCALL zts_central_get_last_resp_buf(char* dst, int len);
ZTS_API int ZTCALL zts_central_status_get(int* http_resp_code);
ZTS_API int ZTCALL zts_central_self_get(int* http_resp_code);
ZTS_API int ZTCALL zts_central_net_get(int* http_resp_code, uint64_t net_id);
ZTS_API int ZTCALL zts_central_net_update(int* http_resp_code, uint64_t net_id);
ZTS_API int ZTCALL zts_central_net_delete(int* http_resp_code, uint64_t net_id);
ZTS_API int ZTCALL zts_central_net_get_all(int* http_resp_code);
ZTS_API int ZTCALL zts_central_member_get(int* http_resp_code, uint64_t net_id, uint64_t node_id);
ZTS_API int ZTCALL zts_central_member_update(int* http_resp_code, uint64_t net_id, uint64_t node_id, char* post_data);
ZTS_API int ZTCALL zts_central_node_auth(int* http_resp_code, uint64_t net_id, uint64_t node_id, uint8_t is_authed);
ZTS_API int ZTCALL zts_central_net_get_members(int* http_resp_code, uint64_t net_id);
#endif // ZTS_DISABLE_CENTRAL_API
//----------------------------------------------------------------------------//
// Identity Management //
//----------------------------------------------------------------------------//
#define ZTS_ID_STR_BUF_LEN 384
ZTS_API int ZTCALL zts_id_new(char* key, unsigned int* key_buf_len);
ZTS_API int ZTCALL zts_id_pair_is_valid(const char* key, unsigned int len);
ZTS_API int ZTCALL zts_init_from_storage(const char* path);
ZTS_API int ZTCALL zts_init_from_memory(const char* key, unsigned int len);
#ifdef ZTS_ENABLE_PYTHON
ZTS_API int ZTCALL zts_init_set_event_handler(PythonDirectorCallbackClass* callback);
#endif
#ifdef ZTS_ENABLE_PINVOKE
ZTS_API int ZTCALL zts_init_set_event_handler(CppCallback callback);
#endif
#ifdef ZTS_ENABLE_JAVA
#include <jni.h>
int zts_init_set_event_handler(jobject obj_ref, jmethodID id);
#endif
#ifdef ZTS_C_API_ONLY
ZTS_API int ZTCALL zts_init_set_event_handler(void (*callback)(void*));
#endif
ZTS_API int ZTCALL zts_init_blacklist_if(const char* prefix, unsigned int len);
ZTS_API int ZTCALL zts_init_set_roots(const void* roots_data, unsigned int len);
ZTS_API int ZTCALL zts_init_set_port(unsigned short port);
ZTS_API int ZTCALL zts_init_set_random_port_range(unsigned short start_port, unsigned short end_port);
ZTS_API int ZTCALL zts_init_allow_secondary_port(unsigned int allowed);
ZTS_API int ZTCALL zts_init_allow_port_mapping(unsigned int allowed);
ZTS_API int ZTCALL zts_init_allow_net_cache(unsigned int allowed);
ZTS_API int ZTCALL zts_init_allow_peer_cache(unsigned int allowed);
ZTS_API int ZTCALL zts_init_allow_roots_cache(unsigned int allowed);
ZTS_API int ZTCALL zts_init_allow_id_cache(unsigned int allowed);
ZTS_API int ZTCALL zts_addr_is_assigned(uint64_t net_id, unsigned int family);
ZTS_API int ZTCALL zts_addr_get(uint64_t net_id, unsigned int family, struct zts_sockaddr_storage* addr);
ZTS_API int ZTCALL zts_addr_get_str(uint64_t net_id, unsigned int family, char* dst, unsigned int len);
ZTS_API int ZTCALL zts_addr_get_all(uint64_t net_id, struct zts_sockaddr_storage* addr, unsigned int* count);
ZTS_API int ZTCALL
zts_addr_compute_6plane(const uint64_t net_id, const uint64_t node_id, struct zts_sockaddr_storage* addr);
ZTS_API int ZTCALL
zts_addr_compute_rfc4193(const uint64_t net_id, const uint64_t node_id, struct zts_sockaddr_storage* addr);
ZTS_API int ZTCALL zts_addr_compute_rfc4193_str(uint64_t net_id, uint64_t node_id, char* dst, unsigned int len);
ZTS_API int ZTCALL zts_addr_compute_6plane_str(uint64_t net_id, uint64_t node_id, char* dst, unsigned int len);
ZTS_API uint64_t ZTCALL zts_net_compute_adhoc_id(uint16_t start_port, uint16_t end_port);
ZTS_API int ZTCALL zts_net_join(uint64_t net_id);
ZTS_API int ZTCALL zts_net_leave(uint64_t net_id);
ZTS_API int ZTCALL zts_net_transport_is_ready(const uint64_t net_id);
ZTS_API uint64_t ZTCALL zts_net_get_mac(uint64_t net_id);
ZTS_API int ZTCALL zts_net_get_mac_str(uint64_t net_id, char* dst, unsigned int len);
ZTS_API int ZTCALL zts_net_get_broadcast(uint64_t net_id);
ZTS_API int ZTCALL zts_net_get_mtu(uint64_t net_id);
ZTS_API int ZTCALL zts_net_get_name(uint64_t net_id, char* dst, unsigned int len);
ZTS_API int ZTCALL zts_net_get_status(uint64_t net_id);
ZTS_API int ZTCALL zts_net_get_type(uint64_t net_id);
ZTS_API int ZTCALL zts_route_is_assigned(uint64_t net_id, unsigned int family);
ZTS_API int ZTCALL zts_node_start();
ZTS_API int ZTCALL zts_node_is_online();
ZTS_API uint64_t ZTCALL zts_node_get_id();
ZTS_API int ZTCALL zts_node_get_id_pair(char* key, unsigned int* key_dst_len);
ZTS_API int ZTCALL zts_node_get_port();
ZTS_API int ZTCALL zts_node_stop();
ZTS_API int ZTCALL zts_node_free();
ZTS_API int ZTCALL zts_moon_orbit(uint64_t moon_roots_id, uint64_t moon_seed);
ZTS_API int ZTCALL zts_moon_deorbit(uint64_t moon_roots_id);
//----------------------------------------------------------------------------//
// Statistics //
//----------------------------------------------------------------------------//
typedef struct {
uint32_t link_tx;
uint32_t link_rx;
uint32_t link_drop;
uint32_t link_err;
uint32_t etharp_tx;
uint32_t etharp_rx;
uint32_t etharp_drop;
uint32_t etharp_err;
uint32_t ip4_tx;
uint32_t ip4_rx;
uint32_t ip4_drop;
uint32_t ip4_err;
uint32_t ip6_tx;
uint32_t ip6_rx;
uint32_t ip6_drop;
uint32_t ip6_err;
uint32_t icmp4_tx;
uint32_t icmp4_rx;
uint32_t icmp4_drop;
uint32_t icmp4_err;
uint32_t icmp6_tx;
uint32_t icmp6_rx;
uint32_t icmp6_drop;
uint32_t icmp6_err;
uint32_t udp_tx;
uint32_t udp_rx;
uint32_t udp_drop;
uint32_t udp_err;
uint32_t tcp_tx;
uint32_t tcp_rx;
uint32_t tcp_drop;
uint32_t tcp_err;
uint32_t nd6_tx;
uint32_t nd6_rx;
uint32_t nd6_drop;
uint32_t nd6_err;
} zts_stats_counter_t;
ZTS_API int ZTCALL zts_stats_get_all(zts_stats_counter_t* dst);
//----------------------------------------------------------------------------//
// Socket API //
//----------------------------------------------------------------------------//
ZTS_API int ZTCALL zts_bsd_socket(int family, int type, int protocol);
ZTS_API int ZTCALL zts_bsd_connect(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen);
ZTS_API int ZTCALL zts_bsd_bind(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen);
ZTS_API int ZTCALL zts_bsd_listen(int fd, int backlog);
ZTS_API int ZTCALL zts_bsd_accept(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen);
// Socket level option number
#define ZTS_SOL_SOCKET 0x0fff
// Socket options
#define ZTS_SO_DEBUG 0x0001 // NOT YET SUPPORTED
#define ZTS_SO_ACCEPTCONN 0x0002
#define ZTS_SO_REUSEADDR 0x0004
#define ZTS_SO_KEEPALIVE 0x0008
#define ZTS_SO_DONTROUTE 0x0010 // NOT YET SUPPORTED
#define ZTS_SO_BROADCAST 0x0020
#define ZTS_SO_USELOOPBACK 0x0040 // NOT YET SUPPORTED
#define ZTS_SO_LINGER 0x0080
/*
* Structure used for manipulating linger option.
*/
struct zts_linger {
int l_onoff; // option on/off
int l_linger; // linger time in seconds
};
#define ZTS_SO_DONTLINGER ((int)(~ZTS_SO_LINGER))
#define ZTS_SO_OOBINLINE 0x0100 // NOT YET SUPPORTED
#define ZTS_SO_REUSEPORT 0x0200 // NOT YET SUPPORTED
#define ZTS_SO_SNDBUF 0x1001 // NOT YET SUPPORTED
#define ZTS_SO_RCVBUF 0x1002
#define ZTS_SO_SNDLOWAT 0x1003 // NOT YET SUPPORTED
#define ZTS_SO_RCVLOWAT 0x1004 // NOT YET SUPPORTED
#define ZTS_SO_SNDTIMEO 0x1005
#define ZTS_SO_RCVTIMEO 0x1006
#define ZTS_SO_ERROR 0x1007
#define ZTS_SO_TYPE 0x1008
#define ZTS_SO_CONTIMEO 0x1009
#define ZTS_SO_NO_CHECK 0x100a
#define ZTS_SO_BINDTODEVICE 0x100b
// IPPROTO_IP options
#define ZTS_IP_TOS 0x0001
#define ZTS_IP_TTL 0x0002
#define ZTS_IP_PKTINFO 0x0008
// IPPROTO_TCP options
#define ZTS_TCP_NODELAY 0x0001
#define ZTS_TCP_KEEPALIVE 0x0002
#define ZTS_TCP_KEEPIDLE 0x0003
#define ZTS_TCP_KEEPINTVL 0x0004
#define ZTS_TCP_KEEPCNT 0x0005
// IPPROTO_IPV6 options
#define ZTS_IPV6_CHECKSUM \
0x0007 /* RFC3542: calculate and insert the ICMPv6 checksum for raw \
sockets. */
#define ZTS_IPV6_V6ONLY \
0x001b /* RFC3493: boolean control to restrict ZTS_AF_INET6 sockets to \
IPv6 communications only. */
// UDPLITE options
#define ZTS_UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */
#define ZTS_UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */
// UDPLITE options
#define ZTS_IP_MULTICAST_TTL 5
#define ZTS_IP_MULTICAST_IF 6
#define ZTS_IP_MULTICAST_LOOP 7
// Multicast options
#define ZTS_IP_ADD_MEMBERSHIP 3
#define ZTS_IP_DROP_MEMBERSHIP 4
typedef struct zts_ip_mreq {
struct zts_in_addr imr_multiaddr; /* IP multicast address of group */
struct zts_in_addr imr_interface; /* local IP address of interface */
} zts_ip_mreq;
struct zts_in_pktinfo {
unsigned int ipi_ifindex; /* Interface index */
struct zts_in_addr ipi_addr; /* Destination (from header) address */
};
#define ZTS_IPV6_JOIN_GROUP 12
#define ZTS_IPV6_ADD_MEMBERSHIP ZTS_IPV6_JOIN_GROUP
#define ZTS_IPV6_LEAVE_GROUP 13
#define ZTS_IPV6_DROP_MEMBERSHIP ZTS_IPV6_LEAVE_GROUP
typedef struct zts_ipv6_mreq {
struct zts_in6_addr ipv6mr_multiaddr; /* IPv6 multicast addr */
unsigned int ipv6mr_interface; /* interface index, or 0 */
} zts_ipv6_mreq;
/*
* The Type of Service provides an indication of the abstract
* parameters of the quality of service desired. These parameters are
* to be used to guide the selection of the actual service parameters
* when transmitting a datagram through a particular network. Several
* networks offer service precedence, which somehow treats high
* precedence traffic as more important than other traffic (generally
* by accepting only traffic above a certain precedence at time of high
* load). The major choice is a three way tradeoff between low-delay,
* high-reliability, and high-throughput.
* The use of the Delay, Throughput, and Reliability indications may
* increase the cost (in some sense) of the service. In many networks
* better performance for one of these parameters is coupled with worse
* performance on another. Except for very unusual cases at most two
* of these three indications should be set.
*/
#define ZTS_IPTOS_TOS_MASK 0x1E
#define ZTS_IPTOS_TOS(tos) ((tos)&ZTS_IPTOS_TOS_MASK)
#define ZTS_IPTOS_LOWDELAY 0x10
#define ZTS_IPTOS_THROUGHPUT 0x08
#define ZTS_IPTOS_RELIABILITY 0x04
#define ZTS_IPTOS_LOWCOST 0x02
#define ZTS_IPTOS_MINCOST ZTS_IPTOS_LOWCOST
/*
* The Network Control precedence designation is intended to be used
* within a network only. The actual use and control of that
* designation is up to each network. The Internetwork Control
* designation is intended for use by gateway control originators only.
* If the actual use of these precedence designations is of concern to
* a particular network, it is the responsibility of that network to
* control the access to, and use of, those precedence designations.
*/
#define ZTS_IPTOS_PREC_MASK 0xe0
#define ZTS_IPTOS_PREC(tos) ((tos)&ZTS_IPTOS_PREC_MASK)
#define ZTS_IPTOS_PREC_NETCONTROL 0xe0
#define ZTS_IPTOS_PREC_INTERNETCONTROL 0xc0
#define ZTS_IPTOS_PREC_CRITIC_ECP 0xa0
#define ZTS_IPTOS_PREC_FLASHOVERRIDE 0x80
#define ZTS_IPTOS_PREC_FLASH 0x60
#define ZTS_IPTOS_PREC_IMMEDIATE 0x40
#define ZTS_IPTOS_PREC_PRIORITY 0x20
#define ZTS_IPTOS_PREC_ROUTINE 0x00
ZTS_API int ZTCALL zts_bsd_setsockopt(int fd, int level, int optname, const void* optval, zts_socklen_t optlen);
ZTS_API int ZTCALL zts_bsd_getsockopt(int fd, int level, int optname, void* optval, zts_socklen_t* optlen);
ZTS_API int ZTCALL zts_bsd_getsockname(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen);
ZTS_API int ZTCALL zts_bsd_getpeername(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen);
ZTS_API int ZTCALL zts_bsd_close(int fd);
/* FD_SET used for lwip_select */
#define LWIP_SOCKET_OFFSET 0
#define MEMP_NUM_NETCONN 1024
#ifndef ZTS_FD_SET
#undef ZTS_FD_SETSIZE
// Make FD_SETSIZE match NUM_SOCKETS in socket.c
#define ZTS_FD_SETSIZE MEMP_NUM_NETCONN
#define ZTS_FDSETSAFESET(n, code) \
do { \
if (((n)-LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n)-LWIP_SOCKET_OFFSET) >= 0)) { \
code; \
} \
} while (0)
#define ZTS_FDSETSAFEGET(n, code) \
(((n)-LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n)-LWIP_SOCKET_OFFSET) >= 0) ? (code) : 0)
#define ZTS_FD_SET(n, p) \
ZTS_FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] |= (1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))
#define ZTS_FD_CLR(n, p) \
ZTS_FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] &= ~(1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))
#define ZTS_FD_ISSET(n, p) \
ZTS_FDSETSAFEGET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] & (1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))
#define ZTS_FD_ZERO(p) memset((void*)(p), 0, sizeof(*(p)))
#elif LWIP_SOCKET_OFFSET
#error LWIP_SOCKET_OFFSET does not work with external FD_SET!
#elif ZTS_FD_SETSIZE < MEMP_NUM_NETCONN
#error "external ZTS_FD_SETSIZE too small for number of sockets"
#endif // FD_SET
typedef struct zts_fd_set {
unsigned char fd_bits[(ZTS_FD_SETSIZE + 7) / 8];
} zts_fd_set;
typedef struct zts_timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
} zts_timeval;
ZTS_API int ZTCALL
zts_bsd_select(int nfds, zts_fd_set* readfds, zts_fd_set* writefds, zts_fd_set* exceptfds, struct zts_timeval* timeout);
// fnctl() commands
#define ZTS_F_GETFL 0x0003
#define ZTS_F_SETFL 0x0004
/* File status flags and file access modes for fnctl,
these are bits in an int. */
#define ZTS_O_NONBLOCK 1
#define ZTS_O_NDELAY ZTS_O_NONBLOCK
#define ZTS_O_RDONLY 2
#define ZTS_O_WRONLY 4
#define ZTS_O_RDWR (ZTS_O_RDONLY | ZTS_O_WRONLY)
ZTS_API int ZTCALL zts_bsd_fcntl(int fd, int cmd, int flags);
#define ZTS_POLLIN 0x001
#define ZTS_POLLOUT 0x002
#define ZTS_POLLERR 0x004
#define ZTS_POLLNVAL 0x008
/* Below values are unimplemented */
#define ZTS_POLLRDNORM 0x010
#define ZTS_POLLRDBAND 0x020
#define ZTS_POLLPRI 0x040
#define ZTS_POLLWRNORM 0x080
#define ZTS_POLLWRBAND 0x100
#define ZTS_POLLHUP 0x200
typedef unsigned int zts_nfds_t;
struct zts_pollfd {
int fd;
short events;
short revents;
};
ZTS_API int ZTCALL zts_bsd_poll(struct zts_pollfd* fds, zts_nfds_t nfds, int timeout);
ZTS_API int ZTCALL zts_bsd_ioctl(int fd, unsigned long request, void* argp);
ZTS_API ssize_t ZTCALL zts_bsd_send(int fd, const void* buf, size_t len, int flags);
ZTS_API ssize_t ZTCALL
zts_bsd_sendto(int fd, const void* buf, size_t len, int flags, const struct zts_sockaddr* addr, zts_socklen_t addrlen);
struct zts_iovec {
void* iov_base;
size_t iov_len;
};
/* */
struct zts_msghdr {
void* msg_name;
zts_socklen_t msg_namelen;
struct zts_iovec* msg_iov;
int msg_iovlen;
void* msg_control;
zts_socklen_t msg_controllen;
int msg_flags;
};
/* struct msghdr->msg_flags bit field values */
#define ZTS_MSG_TRUNC 0x04
#define ZTS_MSG_CTRUNC 0x08
ZTS_API ssize_t ZTCALL zts_bsd_sendmsg(int fd, const struct zts_msghdr* msg, int flags);
ZTS_API ssize_t ZTCALL zts_bsd_recv(int fd, void* buf, size_t len,