diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d81df1..eda9938 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -161,7 +161,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Android") set(BUILD_ANDROID TRUE) set(BUILD_STATIC_LIB FALSE) set(BUILD_SHARED_LIB TRUE) - set(BUILD_SELFTEST FALSE) + set(BUILD_HOST_SELFTEST FALSE) set(BUILD_EXAMPLES FALSE) set(ALLOW_INSTALL_TARGET FALSE) endif() @@ -169,9 +169,10 @@ endif() if(BUILD_MACOS_FRAMEWORK) set(BUILD_STATIC_LIB TRUE) set(BUILD_SHARED_LIB TRUE) - set(BUILD_SELFTEST FALSE) + set(BUILD_HOST_SELFTEST FALSE) set(BUILD_EXAMPLES FALSE) set(ALLOW_INSTALL_TARGET FALSE) + set(ZTS_ENABLE_CENTRAL_API FALSE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOMIT_JSON_SUPPORT=1") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DOMIT_JSON_SUPPORT=1") set(CMAKE_XCODE_ATTRIBUTE_ARCHS "$(ARCHS_STANDARD)") @@ -183,9 +184,10 @@ endif() if(BUILD_IOS_FRAMEWORK) set(BUILD_STATIC_LIB TRUE) set(BUILD_SHARED_LIB TRUE) - set(BUILD_SELFTEST FALSE) + set(BUILD_HOST_SELFTEST FALSE) set(BUILD_EXAMPLES FALSE) set(ALLOW_INSTALL_TARGET FALSE) + set(ZTS_ENABLE_CENTRAL_API FALSE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DOMIT_JSON_SUPPORT=1") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DOMIT_JSON_SUPPORT=1") set(DEVROOT diff --git a/include/ZeroTierSockets.h b/include/ZeroTierSockets.h index 0983917..2503723 100644 --- a/include/ZeroTierSockets.h +++ b/include/ZeroTierSockets.h @@ -24,15 +24,6 @@ // Configuration Options // ////////////////////////////////////////////////////////////////////////////// -#ifdef ZTS_ENABLE_PYTHON - /** - * In some situations (Python comes to mind) a signal may not make its - * way to libzt, for this reason we make sure to define a custom signal - * handler that can at least process SIGTERMs - */ - #define ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS 1 -#endif - #if !defined(ZTS_ENABLE_PYTHON) && !defined(ZTS_ENABLE_PINVOKE) #define ZTS_C_API_ONLY 1 #endif @@ -1914,21 +1905,100 @@ ZTS_API ssize_t ZTCALL zts_writev(int fd, const struct zts_iovec *iov, int iovcn */ ZTS_API int ZTCALL zts_shutdown(int fd, int how); -/** - * @brief Add a DNS nameserver for the network stack to use - * - * @param addr Address for DNS nameserver - * @return ZTS_ERR_SERVICE - */ -ZTS_API int ZTCALL zts_add_dns_nameserver(struct zts_sockaddr *addr); +////////////////////////////////////////////////////////////////////////////// +// DNS // +////////////////////////////////////////////////////////////////////////////// + +struct zts_hostent { + char *h_name; /* Official name of the host. */ + char **h_aliases; /* A pointer to an array of pointers to alternative host names, + terminated by a null pointer. */ + int h_addrtype; /* Address type. */ + int h_length; /* The length, in bytes, of the address. */ + char **h_addr_list; /* A pointer to an array of pointers to network addresses (in + network byte order) for the host, terminated by a null pointer. */ +#define h_addr h_addr_list[0] /* for backward compatibility */ +}; /** - * @brief Remove a DNS nameserver + * @brief Resolve a hostname * - * @param addr Address for DNS nameserver - * @return ZTS_ERR_SERVICE + * @param name A null-terminated string representating the name of the host + * @return Pointer to struct zts_hostent if successful, NULL otherwise */ -ZTS_API int ZTCALL zts_del_dns_nameserver(struct zts_sockaddr *addr); +struct zts_hostent *zts_gethostbyname(const char *name); + +enum zts_ip_addr_type { + ZTS_IPADDR_TYPE_V4 = 0U, + ZTS_IPADDR_TYPE_V6 = 6U, + ZTS_IPADDR_TYPE_ANY = 46U // Dual stack +}; + +struct zts_ip4_addr { + uint32_t addr; +}; + +/** This is the aligned version of ip6_addr_t, + used as local variable, on the stack, etc. */ +struct zts_ip6_addr { + uint32_t addr[4]; +#if LWIP_IPV6_SCOPES + uint8_t zone; +#endif /* LWIP_IPV6_SCOPES */ +}; + +/** + * A union struct for both IP version's addresses. + * ATTENTION: watch out for its size when adding IPv6 address scope! + */ +typedef struct zts_ip_addr { + union { + zts_ip6_addr ip6; + zts_ip4_addr ip4; + } u_addr; + uint8_t type; // ZTS_IPADDR_TYPE_V4, ZTS_IPADDR_TYPE_V6 +} zts_ip_addr; + +/** + * Initialize one of the DNS servers. + * + * @param index the index of the DNS server to set must be < DNS_MAX_SERVERS + * @param addr IP address of the DNS server to set + */ +ZTS_API int ZTCALL zts_dns_set_server(uint8_t index, const zts_ip_addr *addr); + +/** + * Obtain one of the currently configured DNS server. + * + * @param 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. + */ +ZTS_API const zts_ip_addr * ZTCALL zts_dns_get_server(uint8_t index); + +////////////////////////////////////////////////////////////////////////////// +// Convenience functions pulled from lwIP // +////////////////////////////////////////////////////////////////////////////// + +/** + * Convert numeric IP address (both versions) into ASCII representation. + * returns ptr to static buffer; not reentrant! + * + * @param addr ip address in network order to convert + * @return pointer to a global static (!) buffer that holds the ASCII + * representation of addr + */ +char *zts_ipaddr_ntoa(const zts_ip_addr *addr); + +/** + * Convert IP address string (both versions) to numeric. + * The version is auto-detected from the string. + * + * @param cp IP address string to convert + * @param addr conversion result is stored here + * @return 1 on success, 0 on error + */ +int zts_ipaddr_aton(const char *cp, zts_ip_addr *addr); /** * Convert IPv4 and IPv6 address structures to human-readable text form. @@ -1939,7 +2009,8 @@ ZTS_API int ZTCALL zts_del_dns_nameserver(struct zts_sockaddr *addr); * @param size Size of the destination buffer * @return On success, returns a non-null pointer to the destination character array */ -ZTS_API const char * ZTCALL zts_inet_ntop(int af, const void *src, char *dst, zts_socklen_t size); +ZTS_API const char * ZTCALL zts_inet_ntop( + int af, const void *src, char *dst, zts_socklen_t size); /** * Convert C-string IPv4 and IPv6 addresses to binary form. diff --git a/src/Signals.cpp b/src/Signals.cpp index 8818898..e604230 100644 --- a/src/Signals.cpp +++ b/src/Signals.cpp @@ -17,11 +17,20 @@ * Custom signal handler */ -#include "ZeroTierSockets.h" -#include "Signals.hpp" +#ifdef ZTS_ENABLE_PYTHON + /** + * In some situations (Python comes to mind) a signal may not make its + * way to libzt, for this reason we make sure to define a custom signal + * handler that can at least process SIGTERMs + */ + #define ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS 1 +#endif #ifdef ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS +#include "ZeroTierSockets.h" +#include "Signals.hpp" + #include #include #include diff --git a/src/Sockets.cpp b/src/Sockets.cpp index 9f8d0a8..9b9cf8e 100644 --- a/src/Sockets.cpp +++ b/src/Sockets.cpp @@ -21,6 +21,9 @@ #include "lwip/def.h" #include "lwip/inet.h" #include "lwip/stats.h" +#include "lwip/netdb.h" +#include "lwip/dns.h" +#include "lwip/ip_addr.h" #include "ZeroTierSockets.h" @@ -294,23 +297,54 @@ int zts_shutdown(int fd, int how) return lwip_shutdown(fd, how); } -int zts_add_dns_nameserver(struct zts_sockaddr *addr) +struct zts_hostent *zts_gethostbyname(const char *name) +{ + if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) { + return NULL; + } + if (!name) { + return NULL; + } + return (struct zts_hostent *)lwip_gethostbyname(name); +} + +int zts_dns_set_server(uint8_t index, const zts_ip_addr *addr) { if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) { return ZTS_ERR_SERVICE; } - return ZTS_ERR_SERVICE; // TODO + if (index >= DNS_MAX_SERVERS) { + return ZTS_ERR_ARG; + } + if (!addr) { + return ZTS_ERR_ARG; + } + dns_setserver(index, (const ip_addr_t *)addr); + return ZTS_ERR_OK; } -int zts_del_dns_nameserver(struct zts_sockaddr *addr) +const zts_ip_addr *zts_dns_get_server(uint8_t index) { if (!(_serviceStateFlags & ZTS_STATE_NET_SERVICE_RUNNING)) { - return ZTS_ERR_SERVICE; + return NULL; } - return ZTS_ERR_SERVICE; // TODO + if (index >= DNS_MAX_SERVERS) { + return NULL; + } + return (const zts_ip_addr *)dns_getserver(index); } -const char *zts_inet_ntop(int af, const void *src, char *dst,zts_socklen_t size) +char *zts_ipaddr_ntoa(const zts_ip_addr *addr) +{ + return ipaddr_ntoa((ip_addr_t *)addr); +} + +int zts_ipaddr_aton(const char *cp, zts_ip_addr *addr) +{ + return ipaddr_aton(cp, (ip_addr_t *)addr); +} + +const char *zts_inet_ntop(int af, const void *src, char *dst, zts_socklen_t size) { return lwip_inet_ntop(af,src,dst,size); } diff --git a/src/VirtualTap.hpp b/src/VirtualTap.hpp index a3049f5..e0bef64 100644 --- a/src/VirtualTap.hpp +++ b/src/VirtualTap.hpp @@ -224,11 +224,6 @@ void _lwip_driver_shutdown(); */ void _lwip_remove_netif(void *netif); -/** - * @brief Initialize and start the DNS client - */ -void _lwip_dns_init(); - /** * @brief Starts DHCP timers */ diff --git a/src/bindings/csharp/zt_wrap.cpp b/src/bindings/csharp/zt_wrap.cpp index eac1aba..0f08b39 100644 --- a/src/bindings/csharp/zt_wrap.cpp +++ b/src/bindings/csharp/zt_wrap.cpp @@ -887,14 +887,14 @@ SWIGEXPORT void * SWIGSTDCALL CSharp_zts_sendto(int jarg1, void * jarg2, unsigne SWIGEXPORT void * SWIGSTDCALL CSharp_zts_sendmsg(int jarg1, void * jarg2, int jarg3) { void * jresult ; int arg1 ; - msghdr *arg2 = (msghdr *) 0 ; + zts_msghdr *arg2 = (zts_msghdr *) 0 ; int arg3 ; ssize_t result; arg1 = (int)jarg1; - arg2 = (msghdr *)jarg2; + arg2 = (zts_msghdr *)jarg2; arg3 = (int)jarg3; - result = zts_sendmsg(arg1,(msghdr const *)arg2,arg3); + result = zts_sendmsg(arg1,(zts_msghdr const *)arg2,arg3); jresult = new ssize_t((const ssize_t &)result); return jresult; } @@ -942,12 +942,12 @@ SWIGEXPORT void * SWIGSTDCALL CSharp_zts_recvfrom(int jarg1, void * jarg2, unsig SWIGEXPORT void * SWIGSTDCALL CSharp_zts_recvmsg(int jarg1, void * jarg2, int jarg3) { void * jresult ; int arg1 ; - msghdr *arg2 = (msghdr *) 0 ; + zts_msghdr *arg2 = (zts_msghdr *) 0 ; int arg3 ; ssize_t result; arg1 = (int)jarg1; - arg2 = (msghdr *)jarg2; + arg2 = (zts_msghdr *)jarg2; arg3 = (int)jarg3; result = zts_recvmsg(arg1,arg2,arg3); jresult = new ssize_t((const ssize_t &)result); @@ -1032,8 +1032,8 @@ SWIGEXPORT int SWIGSTDCALL CSharp_zts_shutdown(int jarg1, int jarg2) { return jresult; } - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_add_dns_nameserver(zts_sockaddr* jarg1) { +/* +SWIGEXPORT int SWIGSTDCALL CSharp_zts_dns_set_server(zts_sockaddr* jarg1) { int jresult ; zts_sockaddr *arg1 = (zts_sockaddr *) 0 ; int result; @@ -1045,7 +1045,7 @@ SWIGEXPORT int SWIGSTDCALL CSharp_zts_add_dns_nameserver(zts_sockaddr* jarg1) { } -SWIGEXPORT int SWIGSTDCALL CSharp_zts_del_dns_nameserver(zts_sockaddr* jarg1) { +SWIGEXPORT int SWIGSTDCALL CSharp_zts_dns_get_server(zts_sockaddr* jarg1) { int jresult ; zts_sockaddr *arg1 = (zts_sockaddr *) 0 ; int result; @@ -1055,6 +1055,7 @@ SWIGEXPORT int SWIGSTDCALL CSharp_zts_del_dns_nameserver(zts_sockaddr* jarg1) { jresult = result; return jresult; } +*/ SWIGEXPORT char * SWIGSTDCALL CSharp_zts_inet_ntop(int jarg1, void * jarg2, char * jarg3, unsigned short jarg4) { diff --git a/src/bindings/python/libzt.py b/src/bindings/python/libzt.py index 80efdec..e372973 100644 --- a/src/bindings/python/libzt.py +++ b/src/bindings/python/libzt.py @@ -64,7 +64,6 @@ class _SwigNonDynamicMeta(type): import weakref ZTS_ENABLE_PYTHON = _libzt.ZTS_ENABLE_PYTHON -ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS = _libzt.ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS ZTS_EVENT_NODE_UP = _libzt.ZTS_EVENT_NODE_UP ZTS_EVENT_NODE_ONLINE = _libzt.ZTS_EVENT_NODE_ONLINE ZTS_EVENT_NODE_OFFLINE = _libzt.ZTS_EVENT_NODE_OFFLINE @@ -740,12 +739,76 @@ ZTS_SHUT_RDWR = _libzt.ZTS_SHUT_RDWR def zts_shutdown(fd, how): return _libzt.zts_shutdown(fd, how) +class zts_hostent(object): + thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag") + __repr__ = _swig_repr + h_name = property(_libzt.zts_hostent_h_name_get, _libzt.zts_hostent_h_name_set) + h_aliases = property(_libzt.zts_hostent_h_aliases_get, _libzt.zts_hostent_h_aliases_set) + h_addrtype = property(_libzt.zts_hostent_h_addrtype_get, _libzt.zts_hostent_h_addrtype_set) + h_length = property(_libzt.zts_hostent_h_length_get, _libzt.zts_hostent_h_length_set) + h_addr_list = property(_libzt.zts_hostent_h_addr_list_get, _libzt.zts_hostent_h_addr_list_set) -def zts_add_dns_nameserver(addr): - return _libzt.zts_add_dns_nameserver(addr) + def __init__(self): + _libzt.zts_hostent_swiginit(self, _libzt.new_zts_hostent()) + __swig_destroy__ = _libzt.delete_zts_hostent -def zts_del_dns_nameserver(addr): - return _libzt.zts_del_dns_nameserver(addr) +# Register zts_hostent in _libzt: +_libzt.zts_hostent_swigregister(zts_hostent) + + +def zts_gethostbyname(name): + return _libzt.zts_gethostbyname(name) +ZTS_IPADDR_TYPE_V4 = _libzt.ZTS_IPADDR_TYPE_V4 +ZTS_IPADDR_TYPE_V6 = _libzt.ZTS_IPADDR_TYPE_V6 +ZTS_IPADDR_TYPE_ANY = _libzt.ZTS_IPADDR_TYPE_ANY +class zts_ip4_addr(object): + thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag") + __repr__ = _swig_repr + addr = property(_libzt.zts_ip4_addr_addr_get, _libzt.zts_ip4_addr_addr_set) + + def __init__(self): + _libzt.zts_ip4_addr_swiginit(self, _libzt.new_zts_ip4_addr()) + __swig_destroy__ = _libzt.delete_zts_ip4_addr + +# Register zts_ip4_addr in _libzt: +_libzt.zts_ip4_addr_swigregister(zts_ip4_addr) + +class zts_ip6_addr(object): + thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag") + __repr__ = _swig_repr + addr = property(_libzt.zts_ip6_addr_addr_get, _libzt.zts_ip6_addr_addr_set) + + def __init__(self): + _libzt.zts_ip6_addr_swiginit(self, _libzt.new_zts_ip6_addr()) + __swig_destroy__ = _libzt.delete_zts_ip6_addr + +# Register zts_ip6_addr in _libzt: +_libzt.zts_ip6_addr_swigregister(zts_ip6_addr) + +class zts_ip_addr(object): + thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag") + __repr__ = _swig_repr + type = property(_libzt.zts_ip_addr_type_get, _libzt.zts_ip_addr_type_set) + + def __init__(self): + _libzt.zts_ip_addr_swiginit(self, _libzt.new_zts_ip_addr()) + __swig_destroy__ = _libzt.delete_zts_ip_addr + +# Register zts_ip_addr in _libzt: +_libzt.zts_ip_addr_swigregister(zts_ip_addr) + + +def zts_dns_set_server(index, addr): + return _libzt.zts_dns_set_server(index, addr) + +def zts_dns_get_server(index): + return _libzt.zts_dns_get_server(index) + +def zts_ipaddr_ntoa(addr): + return _libzt.zts_ipaddr_ntoa(addr) + +def zts_ipaddr_aton(cp, addr): + return _libzt.zts_ipaddr_aton(cp, addr) def zts_inet_ntop(af, src, dst, size): return _libzt.zts_inet_ntop(af, src, dst, size) diff --git a/src/lwipopts.h b/src/lwipopts.h index bfbe47c..20e7437 100644 --- a/src/lwipopts.h +++ b/src/lwipopts.h @@ -1199,7 +1199,7 @@ happening sooner than they should. * transport. */ #if !defined LWIP_DNS || defined __DOXYGEN__ -#define LWIP_DNS 0 +#define LWIP_DNS 1 #endif /** DNS maximum number of entries to maintain locally. */ @@ -1710,7 +1710,7 @@ happening sooner than they should. #endif /** - * LWIP_NETIF_EXT_STATUS_CALLBACK==1: Support an extended callback function + * LWIP_NETIF_EXT_STATUS_CALLBACK==1: Support an extended callback function * for several netif related event that supports multiple subscribers. * @see netif_ext_status_callback */ @@ -2324,7 +2324,7 @@ happening sooner than they should. * All addresses that have a scope according to the default policy (link-local * unicast addresses, interface-local and link-local multicast addresses) should * now have a zone set on them before being passed to the core API, although - * lwIP will currently attempt to select a zone on the caller's behalf when + * lwIP will currently attempt to select a zone on the caller's behalf when * necessary. Applications that directly assign IPv6 addresses to interfaces * (which is NOT recommended) must now ensure that link-local addresses carry * the netif's zone. See the new ip6_zone.h header file for more information and @@ -2962,8 +2962,8 @@ happening sooner than they should. * - src: source eth address * - dst: destination eth address * - eth_type: ethernet type to packet to be sent\n - * - * + * + * * Return values: * - <0: Packet shall not contain VLAN header. * - 0 <= return value <= 0xFFFF: Packet shall contain VLAN header. Return value is prio_vid in host byte order. @@ -3451,4 +3451,4 @@ happening sooner than they should. * @} */ -#endif /* LWIP_HDR_OPT_H */ \ No newline at end of file +#endif /* LWIP_HDR_OPT_H */