From 704a619abab0e8b7d18477fb6804d93434d4d3c0 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 29 Aug 2023 19:35:06 +0300 Subject: [PATCH 01/19] mdns: add mdns for local network routing --- WORKSPACE.bazel | 28 + bazel/third_party/avahi/BUILD.bazel | 0 bazel/third_party/avahi/avahi.BUILD | 30 + bazel/third_party/dbus/BUILD.bazel | 0 bazel/third_party/dbus/dbus.BUILD | 21 + bazel/third_party/expat/BUILD.bazel | 0 bazel/third_party/expat/expat.BUILD | 20 + pkg/c3/motes.h | 1 + pkg/vere/BUILD.bazel | 14 +- pkg/vere/dns_sd.h | 1722 +++++++++++++++++++++++++++ pkg/vere/io/ames.c | 50 + pkg/vere/mdns.c | 226 ++++ pkg/vere/vere.h | 12 + 13 files changed, 2122 insertions(+), 2 deletions(-) create mode 100644 bazel/third_party/avahi/BUILD.bazel create mode 100644 bazel/third_party/avahi/avahi.BUILD create mode 100644 bazel/third_party/dbus/BUILD.bazel create mode 100644 bazel/third_party/dbus/dbus.BUILD create mode 100644 bazel/third_party/expat/BUILD.bazel create mode 100644 bazel/third_party/expat/expat.BUILD create mode 100644 pkg/vere/dns_sd.h create mode 100644 pkg/vere/mdns.c diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 56a347ed6d..d1dad3d96a 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -130,6 +130,15 @@ versioned_http_archive( version = "a4c1e3f7138c2e577376beb99f964cf71e1c8b1b", ) +versioned_http_archive( + name = "avahi", + build_file = "//bazel/third_party/avahi:avahi.BUILD", + sha256 = "060309d7a333d38d951bc27598c677af1796934dbd98e1024e7ad8de798fedda", + strip_prefix = "avahi-{version}", + url = "https://github.com/lathiat/avahi/releases/download/v{version}/avahi-{version}.tar.gz", + version = "0.8", +) + versioned_http_archive( name = "bazel_gazelle", sha256 = "efbbba6ac1a4fd342d5122cbdfdb82aeb2cf2862e35022c752eaddffada7c3f3", @@ -154,6 +163,15 @@ versioned_http_archive( version = "7.85.0", ) +versioned_http_archive( + name = "dbus", + build_file = "//bazel/third_party/dbus:dbus.BUILD", + sha256 = "a6bd5bac5cf19f0c3c594bdae2565a095696980a683a0ef37cb6212e093bde35", + strip_prefix = "dbus-{version}", + url = "https://dbus.freedesktop.org/releases/dbus/dbus-{version}.tar.xz", + version = "1.14.8", +) + versioned_http_archive( name = "ed25519", build_file = "//bazel/third_party/ed25519:ed25519.BUILD", @@ -163,6 +181,16 @@ versioned_http_archive( version = "7fa6712ef5d581a6981ec2b08ee623314cd1d1c4", ) +versioned_http_archive( + name = "expat", + build_file = "//bazel/third_party/expat:expat.BUILD", + strip_prefix = "expat-{version}", + sha256 = "ef2420f0232c087801abf705e89ae65f6257df6b7931d37846a193ef2e8cdcbe", + # TODO: fix the R_2_5_0 nonsense + url = "https://github.com/libexpat/libexpat/releases/download/R_2_5_0/expat-{version}.tar.xz", + version = "2.5.0", +) + versioned_http_archive( name = "gmp", build_file = "//bazel/third_party/gmp:gmp.BUILD", diff --git a/bazel/third_party/avahi/BUILD.bazel b/bazel/third_party/avahi/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/avahi/avahi.BUILD b/bazel/third_party/avahi/avahi.BUILD new file mode 100644 index 0000000000..84d27171c2 --- /dev/null +++ b/bazel/third_party/avahi/avahi.BUILD @@ -0,0 +1,30 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +cc_library( + name = "dns-sd", + hdrs = ["dns_sd.h"], + visibility = ["//visibility:public"], +) + +configure_make( + name = "avahi", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + configure_options = ["--with-dbus-system-address='unix:path=/var/run/dbus/system_bus_socket' --with-xml=none --disable-libevent --disable-glib --disable-gobject --disable-gdbm --disable-qt3 --disable-qt4 --disable-qt5 --disable-gtk --disable-gtk3 --disable-mono --disable-monodoc --disable-python --disable-libdaemon --enable-compat-libdns_sd --disable-rpath"], + lib_source = ":all", + # out_include_dir = "avahi-compat-libdns_sd", + deps = ["@dbus"], + configure_in_place = True, + autogen = True, + autoconf = True, + autogen_command = "bootstrap.sh", + out_static_libs = ["libdns_sd.a", "libavahi-client.a", "libavahi-common.a"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/dbus/BUILD.bazel b/bazel/third_party/dbus/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/dbus/dbus.BUILD b/bazel/third_party/dbus/dbus.BUILD new file mode 100644 index 0000000000..ac55b84103 --- /dev/null +++ b/bazel/third_party/dbus/dbus.BUILD @@ -0,0 +1,21 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "dbus", + lib_name = "libdbus-1", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + copts = ["-O3"], + configure_options = ["--disable-selinux"], + lib_source = ":all", + configure_in_place = True, + deps = ["@expat"], + visibility = ["//visibility:public"], +) diff --git a/bazel/third_party/expat/BUILD.bazel b/bazel/third_party/expat/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/expat/expat.BUILD b/bazel/third_party/expat/expat.BUILD new file mode 100644 index 0000000000..10e536a620 --- /dev/null +++ b/bazel/third_party/expat/expat.BUILD @@ -0,0 +1,20 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +configure_make( + name = "expat", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + # configure_options = ["--with-xml=none --disable-libevent --disable-glib --disable-gobject --disable-gdbm --disable-qt3 --disable-qt4 --disable-qt5 --disable-gtk --disable-gtk3 --disable-mono --disable-monodoc --disable-python --enable-compat-libdns_sd"], + copts = ["-O3"], + lib_source = ":all", + # deps = ["@dbus"], + out_static_libs = ["libexpat.a"], + visibility = ["//visibility:public"], +) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index 0000d5c593..db17834057 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -308,6 +308,7 @@ # define c3__deep c3_s4('d','e','e','p') # define c3__defn c3_s4('d','e','f','n') # define c3__del c3_s3('d','e','l') +# define c3__dear c3_s4('d','e','a','r') # define c3__delc c3_s4('d','e','l','c') # define c3__delt c3_s4('d','e','l','t') # define c3__dept c3_s4('d','e','p','t') diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index 76f8b47eca..249fc42e42 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -119,7 +119,12 @@ vere_library( hdrs = [ "db/lmdb.h", "vere.h", - ], + ] + select({ + "@platforms//os:macos": [], + "@platforms//os:linux": [ + "dns_sd.h", + ], + }), includes = ["."], linkstatic = True, visibility = ["//pkg:__subpackages__"], @@ -133,7 +138,12 @@ vere_library( "@lmdb", "@openssl", "@uv", - ], + ] + select({ + "@platforms//os:macos": [], + "@platforms//os:linux": [ + "@avahi", + ], + }) ) # diff --git a/pkg/vere/dns_sd.h b/pkg/vere/dns_sd.h new file mode 100644 index 0000000000..66e494e402 --- /dev/null +++ b/pkg/vere/dns_sd.h @@ -0,0 +1,1722 @@ +/* -*- Mode: C; tab-width: 4 -*- + * + * Copyright (c) 2003-2004, Apple Computer, Inc. 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. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "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 APPLE OR ITS CONTRIBUTORS 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. + */ + +#ifndef _DNS_SD_H +#define _DNS_SD_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* standard calling convention under Win32 is __stdcall */ +/* Note: When compiling Intel EFI (Extensible Firmware Interface) under MS Visual Studio, the */ +/* _WIN32 symbol is defined by the compiler even though it's NOT compiling code for Windows32 */ +#if defined(_WIN32) && !defined(EFI32) && !defined(EFI64) +#define DNSSD_API __stdcall +#else +#define DNSSD_API +#endif + +/* stdint.h does not exist on FreeBSD 4.x; its types are defined in sys/types.h instead */ +#if defined(__FreeBSD__) && (__FreeBSD__ < 5) +#include + +/* Likewise, on Sun, standard integer types are in sys/types.h */ +#elif defined(__sun__) +#include + +/* EFI does not have stdint.h, or anything else equivalent */ +#elif defined(EFI32) || defined(EFI64) +typedef UINT8 uint8_t; +typedef INT8 int8_t; +typedef UINT16 uint16_t; +typedef INT16 int16_t; +typedef UINT32 uint32_t; +typedef INT32 int32_t; + +/* Windows has its own differences */ +#elif defined(_WIN32) +#include +#define _UNUSED +#define bzero(a, b) memset(a, 0, b) +#ifndef _MSL_STDINT_H +typedef UINT8 uint8_t; +typedef INT8 int8_t; +typedef UINT16 uint16_t; +typedef INT16 int16_t; +typedef UINT32 uint32_t; +typedef INT32 int32_t; +#endif + +/* All other Posix platforms use stdint.h */ +#else +#include +#endif + +/* DNSServiceRef, DNSRecordRef + * + * Opaque internal data types. + * Note: client is responsible for serializing access to these structures if + * they are shared between concurrent threads. + */ + +typedef struct _DNSServiceRef_t *DNSServiceRef; +typedef struct _DNSRecordRef_t *DNSRecordRef; + +/* General flags used in functions defined below */ +enum + { + kDNSServiceFlagsMoreComing = 0x1, + /* MoreComing indicates to a callback that at least one more result is + * queued and will be delivered following immediately after this one. + * Applications should not update their UI to display browse + * results when the MoreComing flag is set, because this would + * result in a great deal of ugly flickering on the screen. + * Applications should instead wait until until MoreComing is not set, + * and then update their UI. + * When MoreComing is not set, that doesn't mean there will be no more + * answers EVER, just that there are no more answers immediately + * available right now at this instant. If more answers become available + * in the future they will be delivered as usual. + */ + + kDNSServiceFlagsAdd = 0x2, + kDNSServiceFlagsDefault = 0x4, + /* Flags for domain enumeration and browse/query reply callbacks. + * "Default" applies only to enumeration and is only valid in + * conjuction with "Add". An enumeration callback with the "Add" + * flag NOT set indicates a "Remove", i.e. the domain is no longer + * valid. + */ + + kDNSServiceFlagsNoAutoRename = 0x8, + /* Flag for specifying renaming behavior on name conflict when registering + * non-shared records. By default, name conflicts are automatically handled + * by renaming the service. NoAutoRename overrides this behavior - with this + * flag set, name conflicts will result in a callback. The NoAutorename flag + * is only valid if a name is explicitly specified when registering a service + * (i.e. the default name is not used.) + */ + + kDNSServiceFlagsShared = 0x10, + kDNSServiceFlagsUnique = 0x20, + /* Flag for registering individual records on a connected + * DNSServiceRef. Shared indicates that there may be multiple records + * with this name on the network (e.g. PTR records). Unique indicates that the + * record's name is to be unique on the network (e.g. SRV records). + */ + + kDNSServiceFlagsBrowseDomains = 0x40, + kDNSServiceFlagsRegistrationDomains = 0x80, + /* Flags for specifying domain enumeration type in DNSServiceEnumerateDomains. + * BrowseDomains enumerates domains recommended for browsing, RegistrationDomains + * enumerates domains recommended for registration. + */ + + kDNSServiceFlagsLongLivedQuery = 0x100, + /* Flag for creating a long-lived unicast query for the DNSServiceQueryRecord call. */ + + kDNSServiceFlagsAllowRemoteQuery = 0x200, + /* Flag for creating a record for which we will answer remote queries + * (queries from hosts more than one hop away; hosts not directly connected to the local link). + */ + + kDNSServiceFlagsForceMulticast = 0x400, + /* Flag for signifying that a query or registration should be performed exclusively via multicast DNS, + * even for a name in a domain (e.g. foo.apple.com.) that would normally imply unicast DNS. + */ + + kDNSServiceFlagsReturnCNAME = 0x800 + /* Flag for returning CNAME records in the DNSServiceQueryRecord call. CNAME records are + * normally followed without indicating to the client that there was a CNAME record. + */ + }; + +/* + * The values for DNS Classes and Types are listed in RFC 1035, and are available + * on every OS in its DNS header file. Unfortunately every OS does not have the + * same header file containing DNS Class and Type constants, and the names of + * the constants are not consistent. For example, BIND 8 uses "T_A", + * BIND 9 uses "ns_t_a", Windows uses "DNS_TYPE_A", etc. + * For this reason, these constants are also listed here, so that code using + * the DNS-SD programming APIs can use these constants, so that the same code + * can compile on all our supported platforms. + */ + +enum + { + kDNSServiceClass_IN = 1 /* Internet */ + }; + +enum + { + kDNSServiceType_A = 1, /* Host address. */ + kDNSServiceType_NS = 2, /* Authoritative server. */ + kDNSServiceType_MD = 3, /* Mail destination. */ + kDNSServiceType_MF = 4, /* Mail forwarder. */ + kDNSServiceType_CNAME = 5, /* Canonical name. */ + kDNSServiceType_SOA = 6, /* Start of authority zone. */ + kDNSServiceType_MB = 7, /* Mailbox domain name. */ + kDNSServiceType_MG = 8, /* Mail group member. */ + kDNSServiceType_MR = 9, /* Mail rename name. */ + kDNSServiceType_NULL = 10, /* Null resource record. */ + kDNSServiceType_WKS = 11, /* Well known service. */ + kDNSServiceType_PTR = 12, /* Domain name pointer. */ + kDNSServiceType_HINFO = 13, /* Host information. */ + kDNSServiceType_MINFO = 14, /* Mailbox information. */ + kDNSServiceType_MX = 15, /* Mail routing information. */ + kDNSServiceType_TXT = 16, /* One or more text strings. */ + kDNSServiceType_RP = 17, /* Responsible person. */ + kDNSServiceType_AFSDB = 18, /* AFS cell database. */ + kDNSServiceType_X25 = 19, /* X_25 calling address. */ + kDNSServiceType_ISDN = 20, /* ISDN calling address. */ + kDNSServiceType_RT = 21, /* Router. */ + kDNSServiceType_NSAP = 22, /* NSAP address. */ + kDNSServiceType_NSAP_PTR = 23, /* Reverse NSAP lookup (deprecated). */ + kDNSServiceType_SIG = 24, /* Security signature. */ + kDNSServiceType_KEY = 25, /* Security key. */ + kDNSServiceType_PX = 26, /* X.400 mail mapping. */ + kDNSServiceType_GPOS = 27, /* Geographical position (withdrawn). */ + kDNSServiceType_AAAA = 28, /* IPv6 Address. */ + kDNSServiceType_LOC = 29, /* Location Information. */ + kDNSServiceType_NXT = 30, /* Next domain (security). */ + kDNSServiceType_EID = 31, /* Endpoint identifier. */ + kDNSServiceType_NIMLOC = 32, /* Nimrod Locator. */ + kDNSServiceType_SRV = 33, /* Server Selection. */ + kDNSServiceType_ATMA = 34, /* ATM Address */ + kDNSServiceType_NAPTR = 35, /* Naming Authority PoinTeR */ + kDNSServiceType_KX = 36, /* Key Exchange */ + kDNSServiceType_CERT = 37, /* Certification record */ + kDNSServiceType_A6 = 38, /* IPv6 Address (deprecated) */ + kDNSServiceType_DNAME = 39, /* Non-terminal DNAME (for IPv6) */ + kDNSServiceType_SINK = 40, /* Kitchen sink (experimentatl) */ + kDNSServiceType_OPT = 41, /* EDNS0 option (meta-RR) */ + kDNSServiceType_TKEY = 249, /* Transaction key */ + kDNSServiceType_TSIG = 250, /* Transaction signature. */ + kDNSServiceType_IXFR = 251, /* Incremental zone transfer. */ + kDNSServiceType_AXFR = 252, /* Transfer zone of authority. */ + kDNSServiceType_MAILB = 253, /* Transfer mailbox records. */ + kDNSServiceType_MAILA = 254, /* Transfer mail agent records. */ + kDNSServiceType_ANY = 255 /* Wildcard match. */ + }; + + +/* possible error code values */ +enum + { + kDNSServiceErr_NoError = 0, + kDNSServiceErr_Unknown = -65537, /* 0xFFFE FFFF */ + kDNSServiceErr_NoSuchName = -65538, + kDNSServiceErr_NoMemory = -65539, + kDNSServiceErr_BadParam = -65540, + kDNSServiceErr_BadReference = -65541, + kDNSServiceErr_BadState = -65542, + kDNSServiceErr_BadFlags = -65543, + kDNSServiceErr_Unsupported = -65544, + kDNSServiceErr_NotInitialized = -65545, + kDNSServiceErr_AlreadyRegistered = -65547, + kDNSServiceErr_NameConflict = -65548, + kDNSServiceErr_Invalid = -65549, + kDNSServiceErr_Firewall = -65550, + kDNSServiceErr_Incompatible = -65551, /* client library incompatible with daemon */ + kDNSServiceErr_BadInterfaceIndex = -65552, + kDNSServiceErr_Refused = -65553, + kDNSServiceErr_NoSuchRecord = -65554, + kDNSServiceErr_NoAuth = -65555, + kDNSServiceErr_NoSuchKey = -65556, + kDNSServiceErr_NATTraversal = -65557, + kDNSServiceErr_DoubleNAT = -65558, + kDNSServiceErr_BadTime = -65559 + /* mDNS Error codes are in the range + * FFFE FF00 (-65792) to FFFE FFFF (-65537) */ + }; + + +/* Maximum length, in bytes, of a service name represented as a */ +/* literal C-String, including the terminating NULL at the end. */ + +#define kDNSServiceMaxServiceName 64 + +/* Maximum length, in bytes, of a domain name represented as an *escaped* C-String */ +/* including the final trailing dot, and the C-String terminating NULL at the end. */ + +#define kDNSServiceMaxDomainName 1005 + +/* + * Notes on DNS Name Escaping + * -- or -- + * "Why is kDNSServiceMaxDomainName 1005, when the maximum legal domain name is 255 bytes?" + * + * All strings used in DNS-SD are UTF-8 strings. + * With few exceptions, most are also escaped using standard DNS escaping rules: + * + * '\\' represents a single literal '\' in the name + * '\.' represents a single literal '.' in the name + * '\ddd', where ddd is a three-digit decimal value from 000 to 255, + * represents a single literal byte with that value. + * A bare unescaped '.' is a label separator, marking a boundary between domain and subdomain. + * + * The exceptions, that do not use escaping, are the routines where the full + * DNS name of a resource is broken, for convenience, into servicename/regtype/domain. + * In these routines, the "servicename" is NOT escaped. It does not need to be, since + * it is, by definition, just a single literal string. Any characters in that string + * represent exactly what they are. The "regtype" portion is, technically speaking, + * escaped, but since legal regtypes are only allowed to contain letters, digits, + * and hyphens, there is nothing to escape, so the issue is moot. The "domain" + * portion is also escaped, though most domains in use on the public Internet + * today, like regtypes, don't contain any characters that need to be escaped. + * As DNS-SD becomes more popular, rich-text domains for service discovery will + * become common, so software should be written to cope with domains with escaping. + * + * The servicename may be up to 63 bytes of UTF-8 text (not counting the C-String + * terminating NULL at the end). The regtype is of the form _service._tcp or + * _service._udp, where the "service" part is 1-14 characters, which may be + * letters, digits, or hyphens. The domain part of the three-part name may be + * any legal domain, providing that the resulting servicename+regtype+domain + * name does not exceed 255 bytes. + * + * For most software, these issues are transparent. When browsing, the discovered + * servicenames should simply be displayed as-is. When resolving, the discovered + * servicename/regtype/domain are simply passed unchanged to DNSServiceResolve(). + * When a DNSServiceResolve() succeeds, the returned fullname is already in + * the correct format to pass to standard system DNS APIs such as res_query(). + * For converting from servicename/regtype/domain to a single properly-escaped + * full DNS name, the helper function DNSServiceConstructFullName() is provided. + * + * The following (highly contrived) example illustrates the escaping process. + * Suppose you have an service called "Dr. Smith\Dr. Johnson", of type "_ftp._tcp" + * in subdomain "4th. Floor" of subdomain "Building 2" of domain "apple.com." + * The full (escaped) DNS name of this service's SRV record would be: + * Dr\.\032Smith\\Dr\.\032Johnson._ftp._tcp.4th\.\032Floor.Building\0322.apple.com. + */ + + +/* + * Constants for specifying an interface index + * + * Specific interface indexes are identified via a 32-bit unsigned integer returned + * by the if_nametoindex() family of calls. + * + * If the client passes 0 for interface index, that means "do the right thing", + * which (at present) means, "if the name is in an mDNS local multicast domain + * (e.g. 'local.', '254.169.in-addr.arpa.', '{8,9,A,B}.E.F.ip6.arpa.') then multicast + * on all applicable interfaces, otherwise send via unicast to the appropriate + * DNS server." Normally, most clients will use 0 for interface index to + * automatically get the default sensible behaviour. + * + * If the client passes a positive interface index, then for multicast names that + * indicates to do the operation only on that one interface. For unicast names the + * interface index is ignored unless kDNSServiceFlagsForceMulticast is also set. + * + * If the client passes kDNSServiceInterfaceIndexLocalOnly when registering + * a service, then that service will be found *only* by other local clients + * on the same machine that are browsing using kDNSServiceInterfaceIndexLocalOnly + * or kDNSServiceInterfaceIndexAny. + * If a client has a 'private' service, accessible only to other processes + * running on the same machine, this allows the client to advertise that service + * in a way such that it does not inadvertently appear in service lists on + * all the other machines on the network. + * + * If the client passes kDNSServiceInterfaceIndexLocalOnly when browsing + * then it will find *all* records registered on that same local machine. + * Clients explicitly wishing to discover *only* LocalOnly services can + * accomplish this by inspecting the interfaceIndex of each service reported + * to their DNSServiceBrowseReply() callback function, and discarding those + * where the interface index is not kDNSServiceInterfaceIndexLocalOnly. + */ + +#define kDNSServiceInterfaceIndexAny 0 +#define kDNSServiceInterfaceIndexLocalOnly ( (uint32_t) -1 ) + + +typedef uint32_t DNSServiceFlags; +typedef int32_t DNSServiceErrorType; + + +/********************************************************************************************* + * + * Unix Domain Socket access, DNSServiceRef deallocation, and data processing functions + * + *********************************************************************************************/ + + +/* DNSServiceRefSockFD() + * + * Access underlying Unix domain socket for an initialized DNSServiceRef. + * The DNS Service Discovery implmementation uses this socket to communicate between + * the client and the mDNSResponder daemon. The application MUST NOT directly read from + * or write to this socket. Access to the socket is provided so that it can be used as a + * run loop source, or in a select() loop: when data is available for reading on the socket, + * DNSServiceProcessResult() should be called, which will extract the daemon's reply from + * the socket, and pass it to the appropriate application callback. By using a run loop or + * select(), results from the daemon can be processed asynchronously. Without using these + * constructs, DNSServiceProcessResult() will block until the response from the daemon arrives. + * The client is responsible for ensuring that the data on the socket is processed in a timely + * fashion - the daemon may terminate its connection with a client that does not clear its + * socket buffer. + * + * sdRef: A DNSServiceRef initialized by any of the DNSService calls. + * + * return value: The DNSServiceRef's underlying socket descriptor, or -1 on + * error. + */ + +int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdRef); + + +/* DNSServiceProcessResult() + * + * Read a reply from the daemon, calling the appropriate application callback. This call will + * block until the daemon's response is received. Use DNSServiceRefSockFD() in + * conjunction with a run loop or select() to determine the presence of a response from the + * server before calling this function to process the reply without blocking. Call this function + * at any point if it is acceptable to block until the daemon's response arrives. Note that the + * client is responsible for ensuring that DNSServiceProcessResult() is called whenever there is + * a reply from the daemon - the daemon may terminate its connection with a client that does not + * process the daemon's responses. + * + * sdRef: A DNSServiceRef initialized by any of the DNSService calls + * that take a callback parameter. + * + * return value: Returns kDNSServiceErr_NoError on success, otherwise returns + * an error code indicating the specific failure that occurred. + */ + +DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdRef); + + +/* DNSServiceRefDeallocate() + * + * Terminate a connection with the daemon and free memory associated with the DNSServiceRef. + * Any services or records registered with this DNSServiceRef will be deregistered. Any + * Browse, Resolve, or Query operations called with this reference will be terminated. + * + * Note: If the reference's underlying socket is used in a run loop or select() call, it should + * be removed BEFORE DNSServiceRefDeallocate() is called, as this function closes the reference's + * socket. + * + * Note: If the reference was initialized with DNSServiceCreateConnection(), any DNSRecordRefs + * created via this reference will be invalidated by this call - the resource records are + * deregistered, and their DNSRecordRefs may not be used in subsequent functions. Similarly, + * if the reference was initialized with DNSServiceRegister, and an extra resource record was + * added to the service via DNSServiceAddRecord(), the DNSRecordRef created by the Add() call + * is invalidated when this function is called - the DNSRecordRef may not be used in subsequent + * functions. + * + * Note: This call is to be used only with the DNSServiceRef defined by this API. It is + * not compatible with dns_service_discovery_ref objects defined in the legacy Mach-based + * DNSServiceDiscovery.h API. + * + * sdRef: A DNSServiceRef initialized by any of the DNSService calls. + * + */ + +void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdRef); + + +/********************************************************************************************* + * + * Domain Enumeration + * + *********************************************************************************************/ + +/* DNSServiceEnumerateDomains() + * + * Asynchronously enumerate domains available for browsing and registration. + * + * The enumeration MUST be cancelled via DNSServiceRefDeallocate() when no more domains + * are to be found. + * + * Note that the names returned are (like all of DNS-SD) UTF-8 strings, + * and are escaped using standard DNS escaping rules. + * (See "Notes on DNS Name Escaping" earlier in this file for more details.) + * A graphical browser displaying a hierarchical tree-structured view should cut + * the names at the bare dots to yield individual labels, then de-escape each + * label according to the escaping rules, and then display the resulting UTF-8 text. + * + * DNSServiceDomainEnumReply Callback Parameters: + * + * sdRef: The DNSServiceRef initialized by DNSServiceEnumerateDomains(). + * + * flags: Possible values are: + * kDNSServiceFlagsMoreComing + * kDNSServiceFlagsAdd + * kDNSServiceFlagsDefault + * + * interfaceIndex: Specifies the interface on which the domain exists. (The index for a given + * interface is determined via the if_nametoindex() family of calls.) + * + * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise indicates + * the failure that occurred (other parameters are undefined if errorCode is nonzero). + * + * replyDomain: The name of the domain. + * + * context: The context pointer passed to DNSServiceEnumerateDomains. + * + */ + +typedef void (DNSSD_API *DNSServiceDomainEnumReply) + ( + DNSServiceRef sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *replyDomain, + void *context + ); + + +/* DNSServiceEnumerateDomains() Parameters: + * + * + * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds + * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, + * and the enumeration operation will run indefinitely until the client + * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). + * + * flags: Possible values are: + * kDNSServiceFlagsBrowseDomains to enumerate domains recommended for browsing. + * kDNSServiceFlagsRegistrationDomains to enumerate domains recommended + * for registration. + * + * interfaceIndex: If non-zero, specifies the interface on which to look for domains. + * (the index for a given interface is determined via the if_nametoindex() + * family of calls.) Most applications will pass 0 to enumerate domains on + * all interfaces. See "Constants for specifying an interface index" for more details. + * + * callBack: The function to be called when a domain is found or the call asynchronously + * fails. + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is not invoked and the DNSServiceRef + * is not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains + ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceDomainEnumReply callBack, + void *context /* may be NULL */ + ); + + +/********************************************************************************************* + * + * Service Registration + * + *********************************************************************************************/ + +/* Register a service that is discovered via Browse() and Resolve() calls. + * + * + * DNSServiceRegisterReply() Callback Parameters: + * + * sdRef: The DNSServiceRef initialized by DNSServiceRegister(). + * + * flags: Currently unused, reserved for future use. + * + * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will + * indicate the failure that occurred (including name conflicts, + * if the kDNSServiceFlagsNoAutoRename flag was used when registering.) + * Other parameters are undefined if errorCode is nonzero. + * + * name: The service name registered (if the application did not specify a name in + * DNSServiceRegister(), this indicates what name was automatically chosen). + * + * regtype: The type of service registered, as it was passed to the callout. + * + * domain: The domain on which the service was registered (if the application did not + * specify a domain in DNSServiceRegister(), this indicates the default domain + * on which the service was registered). + * + * context: The context pointer that was passed to the callout. + * + */ + +typedef void (DNSSD_API *DNSServiceRegisterReply) + ( + DNSServiceRef sdRef, + DNSServiceFlags flags, + DNSServiceErrorType errorCode, + const char *name, + const char *regtype, + const char *domain, + void *context + ); + + +/* DNSServiceRegister() Parameters: + * + * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds + * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, + * and the registration will remain active indefinitely until the client + * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). + * + * interfaceIndex: If non-zero, specifies the interface on which to register the service + * (the index for a given interface is determined via the if_nametoindex() + * family of calls.) Most applications will pass 0 to register on all + * available interfaces. See "Constants for specifying an interface index" for more details. + * + * flags: Indicates the renaming behavior on name conflict (most applications + * will pass 0). See flag definitions above for details. + * + * name: If non-NULL, specifies the service name to be registered. + * Most applications will not specify a name, in which case the computer + * name is used (this name is communicated to the client via the callback). + * If a name is specified, it must be 1-63 bytes of UTF-8 text. + * If the name is longer than 63 bytes it will be automatically truncated + * to a legal length, unless the NoAutoRename flag is set, + * in which case kDNSServiceErr_BadParam will be returned. + * + * regtype: The service type followed by the protocol, separated by a dot + * (e.g. "_ftp._tcp"). The service type must be an underscore, followed + * by 1-14 characters, which may be letters, digits, or hyphens. + * The transport protocol must be "_tcp" or "_udp". New service types + * should be registered at . + * + * domain: If non-NULL, specifies the domain on which to advertise the service. + * Most applications will not specify a domain, instead automatically + * registering in the default domain(s). + * + * host: If non-NULL, specifies the SRV target host name. Most applications + * will not specify a host, instead automatically using the machine's + * default host name(s). Note that specifying a non-NULL host does NOT + * create an address record for that host - the application is responsible + * for ensuring that the appropriate address record exists, or creating it + * via DNSServiceRegisterRecord(). + * + * port: The port, in network byte order, on which the service accepts connections. + * Pass 0 for a "placeholder" service (i.e. a service that will not be discovered + * by browsing, but will cause a name conflict if another client tries to + * register that same name). Most clients will not use placeholder services. + * + * txtLen: The length of the txtRecord, in bytes. Must be zero if the txtRecord is NULL. + * + * txtRecord: The TXT record rdata. A non-NULL txtRecord MUST be a properly formatted DNS + * TXT record, i.e. ... + * Passing NULL for the txtRecord is allowed as a synonym for txtLen=1, txtRecord="", + * i.e. it creates a TXT record of length one containing a single empty string. + * RFC 1035 doesn't allow a TXT record to contain *zero* strings, so a single empty + * string is the smallest legal DNS TXT record. + * As with the other parameters, the DNSServiceRegister call copies the txtRecord + * data; e.g. if you allocated the storage for the txtRecord parameter with malloc() + * then you can safely free that memory right after the DNSServiceRegister call returns. + * + * callBack: The function to be called when the registration completes or asynchronously + * fails. The client MAY pass NULL for the callback - The client will NOT be notified + * of the default values picked on its behalf, and the client will NOT be notified of any + * asynchronous errors (e.g. out of memory errors, etc.) that may prevent the registration + * of the service. The client may NOT pass the NoAutoRename flag if the callback is NULL. + * The client may still deregister the service at any time via DNSServiceRefDeallocate(). + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is never invoked and the DNSServiceRef + * is not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceRegister + ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *name, /* may be NULL */ + const char *regtype, + const char *domain, /* may be NULL */ + const char *host, /* may be NULL */ + uint16_t port, + uint16_t txtLen, + const void *txtRecord, /* may be NULL */ + DNSServiceRegisterReply callBack, /* may be NULL */ + void *context /* may be NULL */ + ); + + +/* DNSServiceAddRecord() + * + * Add a record to a registered service. The name of the record will be the same as the + * registered service's name. + * The record can later be updated or deregistered by passing the RecordRef initialized + * by this function to DNSServiceUpdateRecord() or DNSServiceRemoveRecord(). + * + * Note that the DNSServiceAddRecord/UpdateRecord/RemoveRecord are *NOT* thread-safe + * with respect to a single DNSServiceRef. If you plan to have multiple threads + * in your program simultaneously add, update, or remove records from the same + * DNSServiceRef, then it's the caller's responsibility to use a mutext lock + * or take similar appropriate precautions to serialize those calls. + * + * + * Parameters; + * + * sdRef: A DNSServiceRef initialized by DNSServiceRegister(). + * + * RecordRef: A pointer to an uninitialized DNSRecordRef. Upon succesfull completion of this + * call, this ref may be passed to DNSServiceUpdateRecord() or DNSServiceRemoveRecord(). + * If the above DNSServiceRef is passed to DNSServiceRefDeallocate(), RecordRef is also + * invalidated and may not be used further. + * + * flags: Currently ignored, reserved for future use. + * + * rrtype: The type of the record (e.g. kDNSServiceType_TXT, kDNSServiceType_SRV, etc) + * + * rdlen: The length, in bytes, of the rdata. + * + * rdata: The raw rdata to be contained in the added resource record. + * + * ttl: The time to live of the resource record, in seconds. Pass 0 to use a default value. + * + * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an + * error code indicating the error that occurred (the RecordRef is not initialized). + */ + +DNSServiceErrorType DNSSD_API DNSServiceAddRecord + ( + DNSServiceRef sdRef, + DNSRecordRef *RecordRef, + DNSServiceFlags flags, + uint16_t rrtype, + uint16_t rdlen, + const void *rdata, + uint32_t ttl + ); + + +/* DNSServiceUpdateRecord + * + * Update a registered resource record. The record must either be: + * - The primary txt record of a service registered via DNSServiceRegister() + * - A record added to a registered service via DNSServiceAddRecord() + * - An individual record registered by DNSServiceRegisterRecord() + * + * + * Parameters: + * + * sdRef: A DNSServiceRef that was initialized by DNSServiceRegister() + * or DNSServiceCreateConnection(). + * + * RecordRef: A DNSRecordRef initialized by DNSServiceAddRecord, or NULL to update the + * service's primary txt record. + * + * flags: Currently ignored, reserved for future use. + * + * rdlen: The length, in bytes, of the new rdata. + * + * rdata: The new rdata to be contained in the updated resource record. + * + * ttl: The time to live of the updated resource record, in seconds. + * + * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an + * error code indicating the error that occurred. + */ + +DNSServiceErrorType DNSSD_API DNSServiceUpdateRecord + ( + DNSServiceRef sdRef, + DNSRecordRef RecordRef, /* may be NULL */ + DNSServiceFlags flags, + uint16_t rdlen, + const void *rdata, + uint32_t ttl + ); + + +/* DNSServiceRemoveRecord + * + * Remove a record previously added to a service record set via DNSServiceAddRecord(), or deregister + * an record registered individually via DNSServiceRegisterRecord(). + * + * Parameters: + * + * sdRef: A DNSServiceRef initialized by DNSServiceRegister() (if the + * record being removed was registered via DNSServiceAddRecord()) or by + * DNSServiceCreateConnection() (if the record being removed was registered via + * DNSServiceRegisterRecord()). + * + * recordRef: A DNSRecordRef initialized by a successful call to DNSServiceAddRecord() + * or DNSServiceRegisterRecord(). + * + * flags: Currently ignored, reserved for future use. + * + * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an + * error code indicating the error that occurred. + */ + +DNSServiceErrorType DNSSD_API DNSServiceRemoveRecord + ( + DNSServiceRef sdRef, + DNSRecordRef RecordRef, + DNSServiceFlags flags + ); + + +/********************************************************************************************* + * + * Service Discovery + * + *********************************************************************************************/ + +/* Browse for instances of a service. + * + * + * DNSServiceBrowseReply() Parameters: + * + * sdRef: The DNSServiceRef initialized by DNSServiceBrowse(). + * + * flags: Possible values are kDNSServiceFlagsMoreComing and kDNSServiceFlagsAdd. + * See flag definitions for details. + * + * interfaceIndex: The interface on which the service is advertised. This index should + * be passed to DNSServiceResolve() when resolving the service. + * + * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise will + * indicate the failure that occurred. Other parameters are undefined if + * the errorCode is nonzero. + * + * serviceName: The discovered service name. This name should be displayed to the user, + * and stored for subsequent use in the DNSServiceResolve() call. + * + * regtype: The service type, which is usually (but not always) the same as was passed + * to DNSServiceBrowse(). One case where the discovered service type may + * not be the same as the requested service type is when using subtypes: + * The client may want to browse for only those ftp servers that allow + * anonymous connections. The client will pass the string "_ftp._tcp,_anon" + * to DNSServiceBrowse(), but the type of the service that's discovered + * is simply "_ftp._tcp". The regtype for each discovered service instance + * should be stored along with the name, so that it can be passed to + * DNSServiceResolve() when the service is later resolved. + * + * domain: The domain of the discovered service instance. This may or may not be the + * same as the domain that was passed to DNSServiceBrowse(). The domain for each + * discovered service instance should be stored along with the name, so that + * it can be passed to DNSServiceResolve() when the service is later resolved. + * + * context: The context pointer that was passed to the callout. + * + */ + +typedef void (DNSSD_API *DNSServiceBrowseReply) + ( + DNSServiceRef sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *serviceName, + const char *regtype, + const char *replyDomain, + void *context + ); + + +/* DNSServiceBrowse() Parameters: + * + * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds + * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, + * and the browse operation will run indefinitely until the client + * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). + * + * flags: Currently ignored, reserved for future use. + * + * interfaceIndex: If non-zero, specifies the interface on which to browse for services + * (the index for a given interface is determined via the if_nametoindex() + * family of calls.) Most applications will pass 0 to browse on all available + * interfaces. See "Constants for specifying an interface index" for more details. + * + * regtype: The service type being browsed for followed by the protocol, separated by a + * dot (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp". + * + * domain: If non-NULL, specifies the domain on which to browse for services. + * Most applications will not specify a domain, instead browsing on the + * default domain(s). + * + * callBack: The function to be called when an instance of the service being browsed for + * is found, or if the call asynchronously fails. + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is not invoked and the DNSServiceRef + * is not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceBrowse + ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *regtype, + const char *domain, /* may be NULL */ + DNSServiceBrowseReply callBack, + void *context /* may be NULL */ + ); + + +/* DNSServiceResolve() + * + * Resolve a service name discovered via DNSServiceBrowse() to a target host name, port number, and + * txt record. + * + * Note: Applications should NOT use DNSServiceResolve() solely for txt record monitoring - use + * DNSServiceQueryRecord() instead, as it is more efficient for this task. + * + * Note: When the desired results have been returned, the client MUST terminate the resolve by calling + * DNSServiceRefDeallocate(). + * + * Note: DNSServiceResolve() behaves correctly for typical services that have a single SRV record + * and a single TXT record. To resolve non-standard services with multiple SRV or TXT records, + * DNSServiceQueryRecord() should be used. + * + * DNSServiceResolveReply Callback Parameters: + * + * sdRef: The DNSServiceRef initialized by DNSServiceResolve(). + * + * flags: Currently unused, reserved for future use. + * + * interfaceIndex: The interface on which the service was resolved. + * + * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise will + * indicate the failure that occurred. Other parameters are undefined if + * the errorCode is nonzero. + * + * fullname: The full service domain name, in the form ... + * (This name is escaped following standard DNS rules, making it suitable for + * passing to standard system DNS APIs such as res_query(), or to the + * special-purpose functions included in this API that take fullname parameters. + * See "Notes on DNS Name Escaping" earlier in this file for more details.) + * + * hosttarget: The target hostname of the machine providing the service. This name can + * be passed to functions like gethostbyname() to identify the host's IP address. + * + * port: The port, in network byte order, on which connections are accepted for this service. + * + * txtLen: The length of the txt record, in bytes. + * + * txtRecord: The service's primary txt record, in standard txt record format. + * + * context: The context pointer that was passed to the callout. + * + * NOTE: In earlier versions of this header file, the txtRecord parameter was declared "const char *" + * This is incorrect, since it contains length bytes which are values in the range 0 to 255, not -128 to +127. + * Depending on your compiler settings, this change may cause signed/unsigned mismatch warnings. + * These should be fixed by updating your own callback function definition to match the corrected + * function signature using "const unsigned char *txtRecord". Making this change may also fix inadvertent + * bugs in your callback function, where it could have incorrectly interpreted a length byte with value 250 + * as being -6 instead, with various bad consequences ranging from incorrect operation to software crashes. + * If you need to maintain portable code that will compile cleanly with both the old and new versions of + * this header file, you should update your callback function definition to use the correct unsigned value, + * and then in the place where you pass your callback function to DNSServiceResolve(), use a cast to eliminate + * the compiler warning, e.g.: + * DNSServiceResolve(sd, flags, index, name, regtype, domain, (DNSServiceResolveReply)MyCallback, context); + * This will ensure that your code compiles cleanly without warnings (and more importantly, works correctly) + * with both the old header and with the new corrected version. + * + */ + +typedef void (DNSSD_API *DNSServiceResolveReply) + ( + DNSServiceRef sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *fullname, + const char *hosttarget, + uint16_t port, + uint16_t txtLen, + const unsigned char *txtRecord, + void *context + ); + + +/* DNSServiceResolve() Parameters + * + * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds + * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, + * and the resolve operation will run indefinitely until the client + * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). + * + * flags: Currently ignored, reserved for future use. + * + * interfaceIndex: The interface on which to resolve the service. If this resolve call is + * as a result of a currently active DNSServiceBrowse() operation, then the + * interfaceIndex should be the index reported in the DNSServiceBrowseReply + * callback. If this resolve call is using information previously saved + * (e.g. in a preference file) for later use, then use interfaceIndex 0, because + * the desired service may now be reachable via a different physical interface. + * See "Constants for specifying an interface index" for more details. + * + * name: The name of the service instance to be resolved, as reported to the + * DNSServiceBrowseReply() callback. + * + * regtype: The type of the service instance to be resolved, as reported to the + * DNSServiceBrowseReply() callback. + * + * domain: The domain of the service instance to be resolved, as reported to the + * DNSServiceBrowseReply() callback. + * + * callBack: The function to be called when a result is found, or if the call + * asynchronously fails. + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is never invoked and the DNSServiceRef + * is not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceResolve + ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *name, + const char *regtype, + const char *domain, + DNSServiceResolveReply callBack, + void *context /* may be NULL */ + ); + + +/********************************************************************************************* + * + * Special Purpose Calls (most applications will not use these) + * + *********************************************************************************************/ + +/* DNSServiceCreateConnection() + * + * Create a connection to the daemon allowing efficient registration of + * multiple individual records. + * + * + * Parameters: + * + * sdRef: A pointer to an uninitialized DNSServiceRef. Deallocating + * the reference (via DNSServiceRefDeallocate()) severs the + * connection and deregisters all records registered on this connection. + * + * return value: Returns kDNSServiceErr_NoError on success, otherwise returns + * an error code indicating the specific failure that occurred (in which + * case the DNSServiceRef is not initialized). + */ + +DNSServiceErrorType DNSSD_API DNSServiceCreateConnection(DNSServiceRef *sdRef); + + +/* DNSServiceRegisterRecord + * + * Register an individual resource record on a connected DNSServiceRef. + * + * Note that name conflicts occurring for records registered via this call must be handled + * by the client in the callback. + * + * + * DNSServiceRegisterRecordReply() parameters: + * + * sdRef: The connected DNSServiceRef initialized by + * DNSServiceCreateConnection(). + * + * RecordRef: The DNSRecordRef initialized by DNSServiceRegisterRecord(). If the above + * DNSServiceRef is passed to DNSServiceRefDeallocate(), this DNSRecordRef is + * invalidated, and may not be used further. + * + * flags: Currently unused, reserved for future use. + * + * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will + * indicate the failure that occurred (including name conflicts.) + * Other parameters are undefined if errorCode is nonzero. + * + * context: The context pointer that was passed to the callout. + * + */ + + typedef void (DNSSD_API *DNSServiceRegisterRecordReply) + ( + DNSServiceRef sdRef, + DNSRecordRef RecordRef, + DNSServiceFlags flags, + DNSServiceErrorType errorCode, + void *context + ); + + +/* DNSServiceRegisterRecord() Parameters: + * + * sdRef: A DNSServiceRef initialized by DNSServiceCreateConnection(). + * + * RecordRef: A pointer to an uninitialized DNSRecordRef. Upon succesfull completion of this + * call, this ref may be passed to DNSServiceUpdateRecord() or DNSServiceRemoveRecord(). + * (To deregister ALL records registered on a single connected DNSServiceRef + * and deallocate each of their corresponding DNSServiceRecordRefs, call + * DNSServiceRefDealloocate()). + * + * flags: Possible values are kDNSServiceFlagsShared or kDNSServiceFlagsUnique + * (see flag type definitions for details). + * + * interfaceIndex: If non-zero, specifies the interface on which to register the record + * (the index for a given interface is determined via the if_nametoindex() + * family of calls.) Passing 0 causes the record to be registered on all interfaces. + * See "Constants for specifying an interface index" for more details. + * + * fullname: The full domain name of the resource record. + * + * rrtype: The numerical type of the resource record (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) + * + * rrclass: The class of the resource record (usually kDNSServiceClass_IN) + * + * rdlen: Length, in bytes, of the rdata. + * + * rdata: A pointer to the raw rdata, as it is to appear in the DNS record. + * + * ttl: The time to live of the resource record, in seconds. Pass 0 to use a default value. + * + * callBack: The function to be called when a result is found, or if the call + * asynchronously fails (e.g. because of a name conflict.) + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is never invoked and the DNSRecordRef is + * not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceRegisterRecord + ( + DNSServiceRef sdRef, + DNSRecordRef *RecordRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + uint16_t rdlen, + const void *rdata, + uint32_t ttl, + DNSServiceRegisterRecordReply callBack, + void *context /* may be NULL */ + ); + + +/* DNSServiceQueryRecord + * + * Query for an arbitrary DNS record. + * + * + * DNSServiceQueryRecordReply() Callback Parameters: + * + * sdRef: The DNSServiceRef initialized by DNSServiceQueryRecord(). + * + * flags: Possible values are kDNSServiceFlagsMoreComing and + * kDNSServiceFlagsAdd. The Add flag is NOT set for PTR records + * with a ttl of 0, i.e. "Remove" events. + * + * interfaceIndex: The interface on which the query was resolved (the index for a given + * interface is determined via the if_nametoindex() family of calls). + * See "Constants for specifying an interface index" for more details. + * + * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will + * indicate the failure that occurred. Other parameters are undefined if + * errorCode is nonzero. + * + * fullname: The resource record's full domain name. + * + * rrtype: The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) + * + * rrclass: The class of the resource record (usually kDNSServiceClass_IN). + * + * rdlen: The length, in bytes, of the resource record rdata. + * + * rdata: The raw rdata of the resource record. + * + * ttl: The resource record's time to live, in seconds. + * + * context: The context pointer that was passed to the callout. + * + */ + +typedef void (DNSSD_API *DNSServiceQueryRecordReply) + ( + DNSServiceRef DNSServiceRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + uint16_t rdlen, + const void *rdata, + uint32_t ttl, + void *context + ); + + +/* DNSServiceQueryRecord() Parameters: + * + * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds + * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, + * and the query operation will run indefinitely until the client + * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). + * + * flags: Pass kDNSServiceFlagsLongLivedQuery to create a "long-lived" unicast + * query in a non-local domain. Without setting this flag, unicast queries + * will be one-shot - that is, only answers available at the time of the call + * will be returned. By setting this flag, answers (including Add and Remove + * events) that become available after the initial call is made will generate + * callbacks. This flag has no effect on link-local multicast queries. + * + * interfaceIndex: If non-zero, specifies the interface on which to issue the query + * (the index for a given interface is determined via the if_nametoindex() + * family of calls.) Passing 0 causes the name to be queried for on all + * interfaces. See "Constants for specifying an interface index" for more details. + * + * fullname: The full domain name of the resource record to be queried for. + * + * rrtype: The numerical type of the resource record to be queried for + * (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) + * + * rrclass: The class of the resource record (usually kDNSServiceClass_IN). + * + * callBack: The function to be called when a result is found, or if the call + * asynchronously fails. + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is never invoked and the DNSServiceRef + * is not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceQueryRecord + ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + DNSServiceQueryRecordReply callBack, + void *context /* may be NULL */ + ); + + +/* DNSServiceReconfirmRecord + * + * Instruct the daemon to verify the validity of a resource record that appears to + * be out of date (e.g. because tcp connection to a service's target failed.) + * Causes the record to be flushed from the daemon's cache (as well as all other + * daemons' caches on the network) if the record is determined to be invalid. + * + * Parameters: + * + * flags: Currently unused, reserved for future use. + * + * interfaceIndex: If non-zero, specifies the interface of the record in question. + * Passing 0 causes all instances of this record to be reconfirmed. + * + * fullname: The resource record's full domain name. + * + * rrtype: The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) + * + * rrclass: The class of the resource record (usually kDNSServiceClass_IN). + * + * rdlen: The length, in bytes, of the resource record rdata. + * + * rdata: The raw rdata of the resource record. + * + */ + +DNSServiceErrorType DNSSD_API DNSServiceReconfirmRecord + ( + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + uint16_t rdlen, + const void *rdata + ); + + +/********************************************************************************************* + * + * General Utility Functions + * + *********************************************************************************************/ + +/* DNSServiceConstructFullName() + * + * Concatenate a three-part domain name (as returned by the above callbacks) into a + * properly-escaped full domain name. Note that callbacks in the above functions ALREADY ESCAPE + * strings where necessary. + * + * Parameters: + * + * fullName: A pointer to a buffer that where the resulting full domain name is to be written. + * The buffer must be kDNSServiceMaxDomainName (1005) bytes in length to + * accommodate the longest legal domain name without buffer overrun. + * + * service: The service name - any dots or backslashes must NOT be escaped. + * May be NULL (to construct a PTR record name, e.g. + * "_ftp._tcp.apple.com."). + * + * regtype: The service type followed by the protocol, separated by a dot + * (e.g. "_ftp._tcp"). + * + * domain: The domain name, e.g. "apple.com.". Literal dots or backslashes, + * if any, must be escaped, e.g. "1st\. Floor.apple.com." + * + * return value: Returns 0 on success, -1 on error. + * + */ + +int DNSSD_API DNSServiceConstructFullName + ( + char *fullName, + const char *service, /* may be NULL */ + const char *regtype, + const char *domain + ); + + +/********************************************************************************************* + * + * TXT Record Construction Functions + * + *********************************************************************************************/ + +/* + * A typical calling sequence for TXT record construction is something like: + * + * Client allocates storage for TXTRecord data (e.g. declare buffer on the stack) + * TXTRecordCreate(); + * TXTRecordSetValue(); + * TXTRecordSetValue(); + * TXTRecordSetValue(); + * ... + * DNSServiceRegister( ... TXTRecordGetLength(), TXTRecordGetBytesPtr() ... ); + * TXTRecordDeallocate(); + * Explicitly deallocate storage for TXTRecord data (if not allocated on the stack) + */ + + +/* TXTRecordRef + * + * Opaque internal data type. + * Note: Represents a DNS-SD TXT record. + */ + +typedef union _TXTRecordRef_t { char PrivateData[16]; char *ForceNaturalAlignment; } TXTRecordRef; + + +/* TXTRecordCreate() + * + * Creates a new empty TXTRecordRef referencing the specified storage. + * + * If the buffer parameter is NULL, or the specified storage size is not + * large enough to hold a key subsequently added using TXTRecordSetValue(), + * then additional memory will be added as needed using malloc(). + * + * On some platforms, when memory is low, malloc() may fail. In this + * case, TXTRecordSetValue() will return kDNSServiceErr_NoMemory, and this + * error condition will need to be handled as appropriate by the caller. + * + * You can avoid the need to handle this error condition if you ensure + * that the storage you initially provide is large enough to hold all + * the key/value pairs that are to be added to the record. + * The caller can precompute the exact length required for all of the + * key/value pairs to be added, or simply provide a fixed-sized buffer + * known in advance to be large enough. + * A no-value (key-only) key requires (1 + key length) bytes. + * A key with empty value requires (1 + key length + 1) bytes. + * A key with non-empty value requires (1 + key length + 1 + value length). + * For most applications, DNS-SD TXT records are generally + * less than 100 bytes, so in most cases a simple fixed-sized + * 256-byte buffer will be more than sufficient. + * Recommended size limits for DNS-SD TXT Records are discussed in + * + * + * Note: When passing parameters to and from these TXT record APIs, + * the key name does not include the '=' character. The '=' character + * is the separator between the key and value in the on-the-wire + * packet format; it is not part of either the key or the value. + * + * txtRecord: A pointer to an uninitialized TXTRecordRef. + * + * bufferLen: The size of the storage provided in the "buffer" parameter. + * + * buffer: Optional caller-supplied storage used to hold the TXTRecord data. + * This storage must remain valid for as long as + * the TXTRecordRef. + */ + +void DNSSD_API TXTRecordCreate + ( + TXTRecordRef *txtRecord, + uint16_t bufferLen, + void *buffer + ); + + +/* TXTRecordDeallocate() + * + * Releases any resources allocated in the course of preparing a TXT Record + * using TXTRecordCreate()/TXTRecordSetValue()/TXTRecordRemoveValue(). + * Ownership of the buffer provided in TXTRecordCreate() returns to the client. + * + * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). + * + */ + +void DNSSD_API TXTRecordDeallocate + ( + TXTRecordRef *txtRecord + ); + + +/* TXTRecordSetValue() + * + * Adds a key (optionally with value) to a TXTRecordRef. If the "key" already + * exists in the TXTRecordRef, then the current value will be replaced with + * the new value. + * Keys may exist in four states with respect to a given TXT record: + * - Absent (key does not appear at all) + * - Present with no value ("key" appears alone) + * - Present with empty value ("key=" appears in TXT record) + * - Present with non-empty value ("key=value" appears in TXT record) + * For more details refer to "Data Syntax for DNS-SD TXT Records" in + * + * + * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). + * + * key: A null-terminated string which only contains printable ASCII + * values (0x20-0x7E), excluding '=' (0x3D). Keys should be + * 8 characters or less (not counting the terminating null). + * + * valueSize: The size of the value. + * + * value: Any binary value. For values that represent + * textual data, UTF-8 is STRONGLY recommended. + * For values that represent textual data, valueSize + * should NOT include the terminating null (if any) + * at the end of the string. + * If NULL, then "key" will be added with no value. + * If non-NULL but valueSize is zero, then "key=" will be + * added with empty value. + * + * return value: Returns kDNSServiceErr_NoError on success. + * Returns kDNSServiceErr_Invalid if the "key" string contains + * illegal characters. + * Returns kDNSServiceErr_NoMemory if adding this key would + * exceed the available storage. + */ + +DNSServiceErrorType DNSSD_API TXTRecordSetValue + ( + TXTRecordRef *txtRecord, + const char *key, + uint8_t valueSize, /* may be zero */ + const void *value /* may be NULL */ + ); + + +/* TXTRecordRemoveValue() + * + * Removes a key from a TXTRecordRef. The "key" must be an + * ASCII string which exists in the TXTRecordRef. + * + * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). + * + * key: A key name which exists in the TXTRecordRef. + * + * return value: Returns kDNSServiceErr_NoError on success. + * Returns kDNSServiceErr_NoSuchKey if the "key" does not + * exist in the TXTRecordRef. + */ + +DNSServiceErrorType DNSSD_API TXTRecordRemoveValue + ( + TXTRecordRef *txtRecord, + const char *key + ); + + +/* TXTRecordGetLength() + * + * Allows you to determine the length of the raw bytes within a TXTRecordRef. + * + * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). + * + * return value: Returns the size of the raw bytes inside a TXTRecordRef + * which you can pass directly to DNSServiceRegister() or + * to DNSServiceUpdateRecord(). + * Returns 0 if the TXTRecordRef is empty. + */ + +uint16_t DNSSD_API TXTRecordGetLength + ( + const TXTRecordRef *txtRecord + ); + + +/* TXTRecordGetBytesPtr() + * + * Allows you to retrieve a pointer to the raw bytes within a TXTRecordRef. + * + * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). + * + * return value: Returns a pointer to the raw bytes inside the TXTRecordRef + * which you can pass directly to DNSServiceRegister() or + * to DNSServiceUpdateRecord(). + */ + +const void * DNSSD_API TXTRecordGetBytesPtr + ( + const TXTRecordRef *txtRecord + ); + + +/********************************************************************************************* + * + * TXT Record Parsing Functions + * + *********************************************************************************************/ + +/* + * A typical calling sequence for TXT record parsing is something like: + * + * Receive TXT record data in DNSServiceResolve() callback + * if (TXTRecordContainsKey(txtLen, txtRecord, "key")) then do something + * val1ptr = TXTRecordGetValuePtr(txtLen, txtRecord, "key1", &len1); + * val2ptr = TXTRecordGetValuePtr(txtLen, txtRecord, "key2", &len2); + * ... + * bcopy(val1ptr, myval1, len1); + * bcopy(val2ptr, myval2, len2); + * ... + * return; + * + * If you wish to retain the values after return from the DNSServiceResolve() + * callback, then you need to copy the data to your own storage using bcopy() + * or similar, as shown in the example above. + * + * If for some reason you need to parse a TXT record you built yourself + * using the TXT record construction functions above, then you can do + * that using TXTRecordGetLength and TXTRecordGetBytesPtr calls: + * TXTRecordGetValue(TXTRecordGetLength(x), TXTRecordGetBytesPtr(x), key, &len); + * + * Most applications only fetch keys they know about from a TXT record and + * ignore the rest. + * However, some debugging tools wish to fetch and display all keys. + * To do that, use the TXTRecordGetCount() and TXTRecordGetItemAtIndex() calls. + */ + +/* TXTRecordContainsKey() + * + * Allows you to determine if a given TXT Record contains a specified key. + * + * txtLen: The size of the received TXT Record. + * + * txtRecord: Pointer to the received TXT Record bytes. + * + * key: A null-terminated ASCII string containing the key name. + * + * return value: Returns 1 if the TXT Record contains the specified key. + * Otherwise, it returns 0. + */ + +int DNSSD_API TXTRecordContainsKey + ( + uint16_t txtLen, + const void *txtRecord, + const char *key + ); + + +/* TXTRecordGetValuePtr() + * + * Allows you to retrieve the value for a given key from a TXT Record. + * + * txtLen: The size of the received TXT Record + * + * txtRecord: Pointer to the received TXT Record bytes. + * + * key: A null-terminated ASCII string containing the key name. + * + * valueLen: On output, will be set to the size of the "value" data. + * + * return value: Returns NULL if the key does not exist in this TXT record, + * or exists with no value (to differentiate between + * these two cases use TXTRecordContainsKey()). + * Returns pointer to location within TXT Record bytes + * if the key exists with empty or non-empty value. + * For empty value, valueLen will be zero. + * For non-empty value, valueLen will be length of value data. + */ + +const void * DNSSD_API TXTRecordGetValuePtr + ( + uint16_t txtLen, + const void *txtRecord, + const char *key, + uint8_t *valueLen + ); + + +/* TXTRecordGetCount() + * + * Returns the number of keys stored in the TXT Record. The count + * can be used with TXTRecordGetItemAtIndex() to iterate through the keys. + * + * txtLen: The size of the received TXT Record. + * + * txtRecord: Pointer to the received TXT Record bytes. + * + * return value: Returns the total number of keys in the TXT Record. + * + */ + +uint16_t DNSSD_API TXTRecordGetCount + ( + uint16_t txtLen, + const void *txtRecord + ); + + +/* TXTRecordGetItemAtIndex() + * + * Allows you to retrieve a key name and value pointer, given an index into + * a TXT Record. Legal index values range from zero to TXTRecordGetCount()-1. + * It's also possible to iterate through keys in a TXT record by simply + * calling TXTRecordGetItemAtIndex() repeatedly, beginning with index zero + * and increasing until TXTRecordGetItemAtIndex() returns kDNSServiceErr_Invalid. + * + * On return: + * For keys with no value, *value is set to NULL and *valueLen is zero. + * For keys with empty value, *value is non-NULL and *valueLen is zero. + * For keys with non-empty value, *value is non-NULL and *valueLen is non-zero. + * + * txtLen: The size of the received TXT Record. + * + * txtRecord: Pointer to the received TXT Record bytes. + * + * index: An index into the TXT Record. + * + * keyBufLen: The size of the string buffer being supplied. + * + * key: A string buffer used to store the key name. + * On return, the buffer contains a null-terminated C string + * giving the key name. DNS-SD TXT keys are usually + * 8 characters or less. To hold the maximum possible + * key name, the buffer should be 256 bytes long. + * + * valueLen: On output, will be set to the size of the "value" data. + * + * value: On output, *value is set to point to location within TXT + * Record bytes that holds the value data. + * + * return value: Returns kDNSServiceErr_NoError on success. + * Returns kDNSServiceErr_NoMemory if keyBufLen is too short. + * Returns kDNSServiceErr_Invalid if index is greater than + * TXTRecordGetCount()-1. + */ + +DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex + ( + uint16_t txtLen, + const void *txtRecord, + uint16_t index, + uint16_t keyBufLen, + char *key, + uint8_t *valueLen, + const void **value + ); + +#ifdef __APPLE_API_PRIVATE + +/* + * Mac OS X specific functionality + * 3rd party clients of this API should not depend on future support or availability of this routine + */ + +/* DNSServiceSetDefaultDomainForUser() + * + * Set the default domain for the caller's UID. Future browse and registration + * calls by this user that do not specify an explicit domain will browse and + * register in this wide-area domain in addition to .local. In addition, this + * domain will be returned as a Browse domain via domain enumeration calls. + * + * + * Parameters: + * + * flags: Pass kDNSServiceFlagsAdd to add a domain for a user. Call without + * this flag set to clear a previously added domain. + * + * domain: The domain to be used for the caller's UID. + * + * return value: Returns kDNSServiceErr_NoError on succeses, otherwise returns + * an error code indicating the error that occurred + */ + +DNSServiceErrorType DNSSD_API DNSServiceSetDefaultDomainForUser + ( + DNSServiceFlags flags, + const char *domain + ); + +#endif //__APPLE_API_PRIVATE + +// Some C compiler cleverness. We can make the compiler check certain things for us, +// and report errors at compile-time if anything is wrong. The usual way to do this would +// be to use a run-time "if" statement or the conventional run-time "assert" mechanism, but +// then you don't find out what's wrong until you run the software. This way, if the assertion +// condition is false, the array size is negative, and the complier complains immediately. + +struct DNS_SD_CompileTimeAssertionChecks + { + char assert0[(sizeof(union _TXTRecordRef_t) == 16) ? 1 : -1]; + }; + +#ifdef __cplusplus + } +#endif + +#endif /* _DNS_SD_H */ diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 24baa6e042..6dd6cff67d 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2060,6 +2060,50 @@ _ames_recv_cb(uv_udp_t* wax_u, } } +static void +_mdns_dear_bail(u3_ovum* egg_u, u3_noun lud) +{ + u3l_log("mdns: %%dear failure;"); + u3z(lud); + u3_ovum_free(egg_u); +} + +/* _ames_put_dear(): send lane to arvo after hearing mdns response +*/ +static void +_ames_put_dear(char* ship, unsigned long s_addr, uint16_t port, void* context) +{ + u3_ames* sam_u = (u3_ames*)context; + + u3_lane lan; + lan.pip_w = ntohl(s_addr); + lan.por_s = ntohs(port); + + u3_noun shp_u = u3dc("slaw", c3__p, u3i_string(ship)); + + if (u3_nul == shp_u) { + u3l_log("ames: strange ship from mdns: %s", ship); + return; + } + + u3_noun our = u3i_chubs(2, sam_u->pir_u->who_d); + if (our == u3t(shp_u)) { + u3z(our); + return; + } + + u3z(our); + + u3_noun wir = u3nc(c3__ames, u3_nul); + u3_noun cad = u3nt(c3__dear, u3k(u3t(shp_u)), u3nc(c3n, u3_ames_encode_lane(lan))); + + u3_auto_peer( + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__a, wir, cad)), + 0, 0, _mdns_dear_bail); + u3z(shp_u); +} + /* _ames_io_start(): initialize ames I/O. */ static void @@ -2124,6 +2168,12 @@ _ames_io_start(u3_ames* sam_u) u3l_log("ames: live on %d (localhost only)", sam_u->pir_u->por_s); } + u3_noun our = u3dc("scot", 'p', who); + char* our_s = u3r_string(our); + u3z(our); + + mdns_init(por_s, our_s, _ames_put_dear, (void *)sam_u); + uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _ames_recv_cb); sam_u->car_u.liv_o = c3y; diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c new file mode 100644 index 0000000000..54fd797cf4 --- /dev/null +++ b/pkg/vere/mdns.c @@ -0,0 +1,226 @@ +/// @file + +#include "vere.h" + +#include "dns_sd.h" + +typedef struct _mdns_payload { + mdns_cb* cb; + DNSServiceRef sref; + char* who; + char* our; + uint16_t port; + uv_poll_t* poll; + void* context; +} mdns_payload; + + +static void getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { + mdns_payload* payload = (mdns_payload*)req->data; + + if (status < 0) { + u3l_log("mdns: getaddrinfo error: %s", uv_strerror(status)); + uv_poll_stop(payload->poll); + c3_free(payload->poll); + DNSServiceRefDeallocate(payload->sref); + c3_free(payload); + c3_free(req); + uv_freeaddrinfo(res); + return; + } + + struct sockaddr_in* addr = (struct sockaddr_in*)res->ai_addr; + payload->cb(payload->who, addr->sin_addr.s_addr, payload->port, payload->context); + + c3_free(payload->who); + uv_poll_stop(payload->poll); + c3_free(payload->poll); + uv_freeaddrinfo(res); + c3_free(req); + DNSServiceRefDeallocate(payload->sref); + c3_free(payload); + +} + +static void resolve_cb(DNSServiceRef sref, + DNSServiceFlags f, + uint32_t interface, + DNSServiceErrorType err, + const char *name, + const char *host, + uint16_t port, + uint16_t tl, + const unsigned char *t, + void *context) +{ + mdns_payload* payload = (mdns_payload*)context; + + if (err != kDNSServiceErr_NoError) { + u3l_log("mdns: dns resolve error %d", err); + uv_poll_stop(payload->poll); + c3_free(payload->poll); + c3_free(payload); + DNSServiceRefDeallocate(sref); + return; + } + + payload->sref = sref; + payload->port = port; + + char* who = strtok(name, "."); + + if (who == NULL) { + u3l_log("mdns: invalid domain name"); + uv_poll_stop(payload->poll); + c3_free(payload->poll); + c3_free(payload); + DNSServiceRefDeallocate(sref); + return; + } + + char* who2 = c3_malloc(strlen(who) + 1); + snprintf(who2, sizeof who2, "~%s", who); + payload->who = who2; + + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; // Request only IPv4 addresses + hints.ai_socktype = SOCK_STREAM; // TCP socket + + uv_getaddrinfo_t* req = (uv_getaddrinfo_t*)c3_malloc(sizeof(uv_getaddrinfo_t)); + req->data = (void*)payload; + + uv_loop_t* loop = uv_default_loop(); + int error = uv_getaddrinfo(loop, req, getaddrinfo_cb, host, NULL, &hints); + + if (error < 0) { + u3l_log("mdns: getaddrinfo error: %s\n", uv_strerror(error)); + uv_poll_stop(payload->poll); + c3_free(payload->poll); + c3_free(payload); + c3_free(req); + DNSServiceRefDeallocate(sref); + } +} + +static void poll_cb(uv_poll_t *handle, int status, int events) { + DNSServiceRef sref = (DNSServiceRef) handle->data; + int err = DNSServiceProcessResult(sref); +} + +static void dns_sd_cb(DNSServiceRef sref, mdns_payload* payload) { + int fd = DNSServiceRefSockFD(sref); + uv_loop_t* loop = uv_default_loop(); + uv_poll_t* poll = (uv_poll_t *)c3_malloc(sizeof(uv_poll_t)); + poll->data = (void *)sref; + payload->poll = poll; + uv_poll_init(loop, poll, fd); + uv_poll_start(poll, UV_READABLE, poll_cb); +} + +static void browse_cb(DNSServiceRef s, + DNSServiceFlags f, + uint32_t interface, + DNSServiceErrorType err, + const char* name, + const char* type, + const char* domain, + void* context) +{ + if (err != kDNSServiceErr_NoError) { + u3l_log("mdns: service browse error %i", err); + return; + } + + if (f & kDNSServiceFlagsAdd) { // Add + DNSServiceRef sr; + + mdns_payload* payload = (mdns_payload*)context; + mdns_payload* payload_copy = c3_malloc(sizeof *payload_copy); + + // copy to prevent asynchronous thrashing of payload + memcpy(payload_copy, payload, sizeof(mdns_payload)); + + DNSServiceErrorType err = + DNSServiceResolve(&sr, 0, interface, + name, type, domain, resolve_cb, + (void*)payload_copy); + + if (err != kDNSServiceErr_NoError) { + u3l_log("mdns: dns service resolve error %i", err); + c3_free(payload_copy); + DNSServiceRefDeallocate(sr); + return; + } + dns_sd_cb(sr, payload_copy); + } +} + +static void register_cb(DNSServiceRef sref, + DNSServiceFlags f, + DNSServiceErrorType err, + const char* name, + const char* type, + const char* domain, + void* context) +{ + mdns_payload* payload = (mdns_payload*)context; + + if (err != kDNSServiceErr_NoError) { + u3l_log("mdns: service register error %i", err); + } else { + u3l_log("mdns: %s registered on all interfaces", name); + } + uv_poll_stop(payload->poll); + c3_free(payload->poll); + c3_free(payload->our); + c3_free(payload); +} + +void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) +{ + #if defined(U3_OS_linux) + setenv("AVAHI_COMPAT_NOWARN", "1", 0); + setenv("DBUS_SYSTEM_BUS_ADDRESS", "unix:path=/var/run/dbus/system_bus_socket", 0); + # endif + + memmove(our, our+1, strlen(our)); // certain url parsers don't like the sig + + mdns_payload* register_payload = (mdns_payload*)c3_malloc(sizeof(mdns_payload)); + + register_payload->cb = cb; + register_payload->context = context; + register_payload->our = our; + + DNSServiceRef sref; + DNSServiceErrorType err; + err = DNSServiceRegister(&sref, 0, 0, our, "_ames._udp", + NULL, NULL, htons(port), 0, NULL, register_cb, (void*)register_payload); + + if (err != kDNSServiceErr_NoError) { + u3l_log("mdns: service register error %i", err); + DNSServiceRefDeallocate(sref); + return; + } + + dns_sd_cb(sref, register_payload); + + mdns_payload* browse_payload = (mdns_payload*)c3_malloc(sizeof(mdns_payload)); + + browse_payload->cb = cb; + browse_payload->context = context; + + DNSServiceErrorType dnserr; + DNSServiceRef sref2; + + dnserr = DNSServiceBrowse(&sref2, 0, 0, "_ames._udp", NULL, browse_cb, (void *)browse_payload); + + if (dnserr != kDNSServiceErr_NoError) { + u3l_log("mdns: service browse error %i", dnserr); + DNSServiceRefDeallocate(sref2); + return; + } + + dns_sd_cb(sref2, browse_payload); + +} diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index cef16810b6..3154432fc5 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -674,6 +674,12 @@ uv_timer_t tim_u; // gc timer } u3_king; + /* mdns_cb: callback when mdns finds a new ship + */ + typedef void mdns_cb(char* ship, + unsigned long s_addr, + uint16_t port, + void* context); /* u3_pier_spin(): (re-)activate idle handler */ void @@ -1238,6 +1244,12 @@ u3_auto* u3_lick_io_init(u3_pier* pir_u); + /** mdns, routing for the local network + **/ + /* mdns_init(): initialize mdns. + */ + void + mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context); /** HTTP server. **/ /* u3_http_io_init(): initialize http I/O. From 3779c75beb52c72a09c1673faeb45d40b80b6cd4 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 5 Sep 2023 13:20:56 +0300 Subject: [PATCH 02/19] bazel: host h2o from s3 since github is unable to serve it reliably --- WORKSPACE.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index d1dad3d96a..8fa018b97a 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -207,7 +207,7 @@ versioned_http_archive( patches = ["//bazel/third_party/h2o:{version}.patch"], sha256 = "f8cbc1b530d85ff098f6efc2c3fdbc5e29baffb30614caac59d5c710f7bda201", strip_prefix = "h2o-{version}", - url = "https://github.com/h2o/h2o/archive/refs/tags/v{version}.tar.gz", + url = "https://urbit-foundation.s3.us-east-2.amazonaws.com/h2o-{version}.tar.gz", # When bumping the version, compare `CMakeLists.txt` in the `h2o` repo to # {build_file} and confirm that {build_file} remains an accurate description # of the h2o build process. From b3c381a581623bcedadf759988c64224b820b2bd Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 5 Sep 2023 13:47:32 +0300 Subject: [PATCH 03/19] bazel: mess with config options to make dbus build in CI --- bazel/third_party/dbus/dbus.BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bazel/third_party/dbus/dbus.BUILD b/bazel/third_party/dbus/dbus.BUILD index ac55b84103..0642883163 100644 --- a/bazel/third_party/dbus/dbus.BUILD +++ b/bazel/third_party/dbus/dbus.BUILD @@ -13,7 +13,7 @@ configure_make( "//conditions:default": ["--jobs=`nproc`"], }), copts = ["-O3"], - configure_options = ["--disable-selinux"], + configure_options = ["--disable-selinux --without-x --disable-tests"], lib_source = ":all", configure_in_place = True, deps = ["@expat"], From fd96138634a9408cd652c945fe19948270ba2571 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 5 Sep 2023 14:58:28 +0300 Subject: [PATCH 04/19] mdns: free our on register_cb fail --- pkg/vere/mdns.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index 54fd797cf4..77ac270824 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -199,6 +199,7 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) if (err != kDNSServiceErr_NoError) { u3l_log("mdns: service register error %i", err); + c3_free(our); DNSServiceRefDeallocate(sref); return; } From 66b17ccaaf89ab918e5aabd1875d4380362c54de Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 5 Sep 2023 16:25:21 +0300 Subject: [PATCH 05/19] bazel: revert serving h2o from our s3 since github incident is over This reverts commit 3779c75beb52c72a09c1673faeb45d40b80b6cd4. --- WORKSPACE.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 8fa018b97a..d1dad3d96a 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -207,7 +207,7 @@ versioned_http_archive( patches = ["//bazel/third_party/h2o:{version}.patch"], sha256 = "f8cbc1b530d85ff098f6efc2c3fdbc5e29baffb30614caac59d5c710f7bda201", strip_prefix = "h2o-{version}", - url = "https://urbit-foundation.s3.us-east-2.amazonaws.com/h2o-{version}.tar.gz", + url = "https://github.com/h2o/h2o/archive/refs/tags/v{version}.tar.gz", # When bumping the version, compare `CMakeLists.txt` in the `h2o` repo to # {build_file} and confirm that {build_file} remains an accurate description # of the h2o build process. From 43e92073468972b5d9de57f5a1526b4eb1082cda Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 5 Sep 2023 16:45:17 +0300 Subject: [PATCH 06/19] mdns: fix wonky indentation --- pkg/vere/mdns.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index 77ac270824..38bcf3fa77 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -55,7 +55,7 @@ static void resolve_cb(DNSServiceRef sref, { mdns_payload* payload = (mdns_payload*)context; - if (err != kDNSServiceErr_NoError) { + if (err != kDNSServiceErr_NoError) { u3l_log("mdns: dns resolve error %d", err); uv_poll_stop(payload->poll); c3_free(payload->poll); @@ -105,11 +105,11 @@ static void resolve_cb(DNSServiceRef sref, static void poll_cb(uv_poll_t *handle, int status, int events) { DNSServiceRef sref = (DNSServiceRef) handle->data; - int err = DNSServiceProcessResult(sref); + int err = DNSServiceProcessResult(sref); } static void dns_sd_cb(DNSServiceRef sref, mdns_payload* payload) { - int fd = DNSServiceRefSockFD(sref); + int fd = DNSServiceRefSockFD(sref); uv_loop_t* loop = uv_default_loop(); uv_poll_t* poll = (uv_poll_t *)c3_malloc(sizeof(uv_poll_t)); poll->data = (void *)sref; @@ -214,7 +214,7 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) DNSServiceErrorType dnserr; DNSServiceRef sref2; - dnserr = DNSServiceBrowse(&sref2, 0, 0, "_ames._udp", NULL, browse_cb, (void *)browse_payload); + dnserr = DNSServiceBrowse(&sref2, 0, 0, "_ames._udp", NULL, browse_cb, (void *)browse_payload); if (dnserr != kDNSServiceErr_NoError) { u3l_log("mdns: service browse error %i", dnserr); From 390735c9d1207885da7f0d1fdd086c9bb9d83e4c Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 5 Sep 2023 18:42:21 +0300 Subject: [PATCH 07/19] mdns: stop polling as soon as we reach resolve_cb --- pkg/vere/mdns.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index 38bcf3fa77..b38e5ea737 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -20,8 +20,6 @@ static void getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* r if (status < 0) { u3l_log("mdns: getaddrinfo error: %s", uv_strerror(status)); - uv_poll_stop(payload->poll); - c3_free(payload->poll); DNSServiceRefDeallocate(payload->sref); c3_free(payload); c3_free(req); @@ -33,8 +31,6 @@ static void getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* r payload->cb(payload->who, addr->sin_addr.s_addr, payload->port, payload->context); c3_free(payload->who); - uv_poll_stop(payload->poll); - c3_free(payload->poll); uv_freeaddrinfo(res); c3_free(req); DNSServiceRefDeallocate(payload->sref); @@ -55,10 +51,11 @@ static void resolve_cb(DNSServiceRef sref, { mdns_payload* payload = (mdns_payload*)context; + uv_poll_stop(payload->poll); + c3_free(payload->poll); + if (err != kDNSServiceErr_NoError) { u3l_log("mdns: dns resolve error %d", err); - uv_poll_stop(payload->poll); - c3_free(payload->poll); c3_free(payload); DNSServiceRefDeallocate(sref); return; @@ -71,8 +68,6 @@ static void resolve_cb(DNSServiceRef sref, if (who == NULL) { u3l_log("mdns: invalid domain name"); - uv_poll_stop(payload->poll); - c3_free(payload->poll); c3_free(payload); DNSServiceRefDeallocate(sref); return; @@ -95,8 +90,6 @@ static void resolve_cb(DNSServiceRef sref, if (error < 0) { u3l_log("mdns: getaddrinfo error: %s\n", uv_strerror(error)); - uv_poll_stop(payload->poll); - c3_free(payload->poll); c3_free(payload); c3_free(req); DNSServiceRefDeallocate(sref); From e99d9b50f66d4733fc196804b3d75b41874153a0 Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 8 Sep 2023 15:26:36 +0300 Subject: [PATCH 08/19] mdns: address joes feedback --- pkg/vere/BUILD.bazel | 1 + pkg/vere/io/ames.c | 16 ++++++++----- pkg/vere/mdns.c | 56 ++++++++++++++++---------------------------- pkg/vere/mdns.h | 5 ++++ pkg/vere/vere.h | 12 ---------- 5 files changed, 36 insertions(+), 54 deletions(-) create mode 100644 pkg/vere/mdns.h diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index 249fc42e42..7e772ae61c 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -119,6 +119,7 @@ vere_library( hdrs = [ "db/lmdb.h", "vere.h", + "mdns.h", ] + select({ "@platforms//os:macos": [], "@platforms//os:linux": [ diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 6dd6cff67d..cba1dbf48d 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1,6 +1,7 @@ /// @file #include "vere.h" +#include "mdns.h" #include "noun.h" #include "ur.h" @@ -2063,7 +2064,6 @@ _ames_recv_cb(uv_udp_t* wax_u, static void _mdns_dear_bail(u3_ovum* egg_u, u3_noun lud) { - u3l_log("mdns: %%dear failure;"); u3z(lud); u3_ovum_free(egg_u); } @@ -2071,7 +2071,7 @@ _mdns_dear_bail(u3_ovum* egg_u, u3_noun lud) /* _ames_put_dear(): send lane to arvo after hearing mdns response */ static void -_ames_put_dear(char* ship, unsigned long s_addr, uint16_t port, void* context) +_ames_put_dear(c3_c* ship, c3_w s_addr, c3_s port, void* context) { u3_ames* sam_u = (u3_ames*)context; @@ -2088,6 +2088,7 @@ _ames_put_dear(char* ship, unsigned long s_addr, uint16_t port, void* context) u3_noun our = u3i_chubs(2, sam_u->pir_u->who_d); if (our == u3t(shp_u)) { + u3z(shp_u); u3z(our); return; } @@ -2168,11 +2169,14 @@ _ames_io_start(u3_ames* sam_u) u3l_log("ames: live on %d (localhost only)", sam_u->pir_u->por_s); } - u3_noun our = u3dc("scot", 'p', who); - char* our_s = u3r_string(our); - u3z(our); + { + u3_noun our = u3dc("scot", 'p', u3k(who)); + char* our_s = u3r_string(our); + u3z(our); - mdns_init(por_s, our_s, _ames_put_dear, (void *)sam_u); + mdns_init(por_s, our_s, _ames_put_dear, (void *)sam_u); + c3_free(our_s); + } uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _ames_recv_cb); diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index b38e5ea737..9489ad4841 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -1,16 +1,15 @@ /// @file #include "vere.h" - +#include "mdns.h" #include "dns_sd.h" typedef struct _mdns_payload { mdns_cb* cb; DNSServiceRef sref; - char* who; - char* our; + char who[58]; uint16_t port; - uv_poll_t* poll; + uv_poll_t poll; void* context; } mdns_payload; @@ -30,7 +29,6 @@ static void getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* r struct sockaddr_in* addr = (struct sockaddr_in*)res->ai_addr; payload->cb(payload->who, addr->sin_addr.s_addr, payload->port, payload->context); - c3_free(payload->who); uv_freeaddrinfo(res); c3_free(req); DNSServiceRefDeallocate(payload->sref); @@ -51,8 +49,7 @@ static void resolve_cb(DNSServiceRef sref, { mdns_payload* payload = (mdns_payload*)context; - uv_poll_stop(payload->poll); - c3_free(payload->poll); + uv_poll_stop(&payload->poll); if (err != kDNSServiceErr_NoError) { u3l_log("mdns: dns resolve error %d", err); @@ -64,19 +61,13 @@ static void resolve_cb(DNSServiceRef sref, payload->sref = sref; payload->port = port; - char* who = strtok(name, "."); - - if (who == NULL) { - u3l_log("mdns: invalid domain name"); - c3_free(payload); - DNSServiceRefDeallocate(sref); - return; + int i; + payload->who[0] = '~'; + for (i = 0; name[i] != '\0' && name[i] != '.' && i < 58; ++i) + { + payload->who[i+1] = name[i]; } - char* who2 = c3_malloc(strlen(who) + 1); - snprintf(who2, sizeof who2, "~%s", who); - payload->who = who2; - struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; // Request only IPv4 addresses @@ -96,19 +87,17 @@ static void resolve_cb(DNSServiceRef sref, } } -static void poll_cb(uv_poll_t *handle, int status, int events) { +static void poll_cb(uv_poll_t* handle, int status, int events) { DNSServiceRef sref = (DNSServiceRef) handle->data; int err = DNSServiceProcessResult(sref); } -static void dns_sd_cb(DNSServiceRef sref, mdns_payload* payload) { +static void init_sref_poll(DNSServiceRef sref, mdns_payload* payload) { int fd = DNSServiceRefSockFD(sref); uv_loop_t* loop = uv_default_loop(); - uv_poll_t* poll = (uv_poll_t *)c3_malloc(sizeof(uv_poll_t)); - poll->data = (void *)sref; - payload->poll = poll; - uv_poll_init(loop, poll, fd); - uv_poll_start(poll, UV_READABLE, poll_cb); + payload->poll.data = (void*)sref; + uv_poll_init(loop, &payload->poll, fd); + uv_poll_start(&payload->poll, UV_READABLE, poll_cb); } static void browse_cb(DNSServiceRef s, @@ -145,7 +134,7 @@ static void browse_cb(DNSServiceRef s, DNSServiceRefDeallocate(sr); return; } - dns_sd_cb(sr, payload_copy); + init_sref_poll(sr, payload_copy); } } @@ -164,9 +153,9 @@ static void register_cb(DNSServiceRef sref, } else { u3l_log("mdns: %s registered on all interfaces", name); } - uv_poll_stop(payload->poll); - c3_free(payload->poll); - c3_free(payload->our); + // not freeing sref with DNSServiceRefDeallocate since that + // deregisters us from mdns + uv_poll_stop(&payload->poll); c3_free(payload); } @@ -181,10 +170,6 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) mdns_payload* register_payload = (mdns_payload*)c3_malloc(sizeof(mdns_payload)); - register_payload->cb = cb; - register_payload->context = context; - register_payload->our = our; - DNSServiceRef sref; DNSServiceErrorType err; err = DNSServiceRegister(&sref, 0, 0, our, "_ames._udp", @@ -192,12 +177,11 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) if (err != kDNSServiceErr_NoError) { u3l_log("mdns: service register error %i", err); - c3_free(our); DNSServiceRefDeallocate(sref); return; } - dns_sd_cb(sref, register_payload); + init_sref_poll(sref, register_payload); mdns_payload* browse_payload = (mdns_payload*)c3_malloc(sizeof(mdns_payload)); @@ -215,6 +199,6 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) return; } - dns_sd_cb(sref2, browse_payload); + init_sref_poll(sref2, browse_payload); } diff --git a/pkg/vere/mdns.h b/pkg/vere/mdns.h new file mode 100644 index 0000000000..66c3477179 --- /dev/null +++ b/pkg/vere/mdns.h @@ -0,0 +1,5 @@ +#include "noun.h" + +typedef void mdns_cb(c3_c* ship, c3_w s_addr, c3_s port, void* context); + +void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context); diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 3154432fc5..cef16810b6 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -674,12 +674,6 @@ uv_timer_t tim_u; // gc timer } u3_king; - /* mdns_cb: callback when mdns finds a new ship - */ - typedef void mdns_cb(char* ship, - unsigned long s_addr, - uint16_t port, - void* context); /* u3_pier_spin(): (re-)activate idle handler */ void @@ -1244,12 +1238,6 @@ u3_auto* u3_lick_io_init(u3_pier* pir_u); - /** mdns, routing for the local network - **/ - /* mdns_init(): initialize mdns. - */ - void - mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context); /** HTTP server. **/ /* u3_http_io_init(): initialize http I/O. From 7aaa3a9c8464e5dd5db6f0e449ca18f388678b09 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 11 Sep 2023 13:41:11 +0300 Subject: [PATCH 09/19] bazel: remove unused options from expat.BUILD --- bazel/third_party/expat/expat.BUILD | 2 -- 1 file changed, 2 deletions(-) diff --git a/bazel/third_party/expat/expat.BUILD b/bazel/third_party/expat/expat.BUILD index 10e536a620..030f81ed78 100644 --- a/bazel/third_party/expat/expat.BUILD +++ b/bazel/third_party/expat/expat.BUILD @@ -11,10 +11,8 @@ configure_make( "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], "//conditions:default": ["--jobs=`nproc`"], }), - # configure_options = ["--with-xml=none --disable-libevent --disable-glib --disable-gobject --disable-gdbm --disable-qt3 --disable-qt4 --disable-qt5 --disable-gtk --disable-gtk3 --disable-mono --disable-monodoc --disable-python --enable-compat-libdns_sd"], copts = ["-O3"], lib_source = ":all", - # deps = ["@dbus"], out_static_libs = ["libexpat.a"], visibility = ["//visibility:public"], ) From 4ac9875fd8472e188da92bae8eea95973a243f88 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 11 Sep 2023 13:41:58 +0300 Subject: [PATCH 10/19] ames: shp_u -> whu in _ames_put_dear --- pkg/vere/io/ames.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index cba1dbf48d..03f316a6a7 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2079,16 +2079,16 @@ _ames_put_dear(c3_c* ship, c3_w s_addr, c3_s port, void* context) lan.pip_w = ntohl(s_addr); lan.por_s = ntohs(port); - u3_noun shp_u = u3dc("slaw", c3__p, u3i_string(ship)); + u3_noun whu = u3dc("slaw", c3__p, u3i_string(ship)); - if (u3_nul == shp_u) { + if (u3_nul == whu) { u3l_log("ames: strange ship from mdns: %s", ship); return; } u3_noun our = u3i_chubs(2, sam_u->pir_u->who_d); - if (our == u3t(shp_u)) { - u3z(shp_u); + if (our == u3t(whu)) { + u3z(whu); u3z(our); return; } @@ -2096,13 +2096,13 @@ _ames_put_dear(c3_c* ship, c3_w s_addr, c3_s port, void* context) u3z(our); u3_noun wir = u3nc(c3__ames, u3_nul); - u3_noun cad = u3nt(c3__dear, u3k(u3t(shp_u)), u3nc(c3n, u3_ames_encode_lane(lan))); + u3_noun cad = u3nt(c3__dear, u3k(u3t(whu)), u3nc(c3n, u3_ames_encode_lane(lan))); u3_auto_peer( u3_auto_plan(&sam_u->car_u, u3_ovum_init(0, c3__a, wir, cad)), 0, 0, _mdns_dear_bail); - u3z(shp_u); + u3z(whu); } /* _ames_io_start(): initialize ames I/O. From b173a028b6d7293516e15bdd0d310dca1fd5c68c Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 11 Sep 2023 14:12:11 +0300 Subject: [PATCH 11/19] mdns: use pointer arithmetic instead of memmove to desig --- pkg/vere/mdns.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index 9489ad4841..62b07a8219 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -77,6 +77,7 @@ static void resolve_cb(DNSServiceRef sref, req->data = (void*)payload; uv_loop_t* loop = uv_default_loop(); + int error = uv_getaddrinfo(loop, req, getaddrinfo_cb, host, NULL, &hints); if (error < 0) { @@ -166,13 +167,12 @@ void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) setenv("DBUS_SYSTEM_BUS_ADDRESS", "unix:path=/var/run/dbus/system_bus_socket", 0); # endif - memmove(our, our+1, strlen(our)); // certain url parsers don't like the sig - mdns_payload* register_payload = (mdns_payload*)c3_malloc(sizeof(mdns_payload)); DNSServiceRef sref; DNSServiceErrorType err; - err = DNSServiceRegister(&sref, 0, 0, our, "_ames._udp", + char* our_no_sig = our + 1; // certain url parsers don't like the ~ + err = DNSServiceRegister(&sref, 0, 0, our_no_sig, "_ames._udp", NULL, NULL, htons(port), 0, NULL, register_cb, (void*)register_payload); if (err != kDNSServiceErr_NoError) { From 14a2d7a14d035075a42a3d9048c5ca4c6ae51705 Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 8 Nov 2023 19:17:25 +0200 Subject: [PATCH 12/19] mdns: handle poll lifetimes properly --- pkg/vere/mdns.c | 58 +++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index 62b07a8219..e62b7ea767 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -13,27 +13,26 @@ typedef struct _mdns_payload { void* context; } mdns_payload; +static void close_cb(uv_handle_t* poll) { + mdns_payload* payload = (mdns_payload*)poll->data; + DNSServiceRefDeallocate(payload->sref); + c3_free(payload); +} static void getaddrinfo_cb(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { mdns_payload* payload = (mdns_payload*)req->data; if (status < 0) { u3l_log("mdns: getaddrinfo error: %s", uv_strerror(status)); - DNSServiceRefDeallocate(payload->sref); - c3_free(payload); - c3_free(req); - uv_freeaddrinfo(res); - return; + } else { + struct sockaddr_in* addr = (struct sockaddr_in*)res->ai_addr; + payload->cb(payload->who, addr->sin_addr.s_addr, payload->port, payload->context); } - struct sockaddr_in* addr = (struct sockaddr_in*)res->ai_addr; - payload->cb(payload->who, addr->sin_addr.s_addr, payload->port, payload->context); - - uv_freeaddrinfo(res); + payload->poll.data = payload; + uv_close((uv_handle_t*)&payload->poll, close_cb); c3_free(req); - DNSServiceRefDeallocate(payload->sref); - c3_free(payload); - + uv_freeaddrinfo(res); } static void resolve_cb(DNSServiceRef sref, @@ -53,8 +52,8 @@ static void resolve_cb(DNSServiceRef sref, if (err != kDNSServiceErr_NoError) { u3l_log("mdns: dns resolve error %d", err); - c3_free(payload); - DNSServiceRefDeallocate(sref); + payload->poll.data = payload; + uv_close((uv_handle_t*)&payload->poll, close_cb); return; } @@ -82,9 +81,9 @@ static void resolve_cb(DNSServiceRef sref, if (error < 0) { u3l_log("mdns: getaddrinfo error: %s\n", uv_strerror(error)); - c3_free(payload); + payload->poll.data = payload; + uv_close((uv_handle_t*)&payload->poll, close_cb); c3_free(req); - DNSServiceRefDeallocate(sref); } } @@ -116,29 +115,38 @@ static void browse_cb(DNSServiceRef s, } if (f & kDNSServiceFlagsAdd) { // Add - DNSServiceRef sr; - + // we are leaking payload because we don't know when we are done + // browsing, luckily we only browse once mdns_payload* payload = (mdns_payload*)context; mdns_payload* payload_copy = c3_malloc(sizeof *payload_copy); // copy to prevent asynchronous thrashing of payload memcpy(payload_copy, payload, sizeof(mdns_payload)); + DNSServiceErrorType err = - DNSServiceResolve(&sr, 0, interface, + DNSServiceResolve(&payload_copy->sref, 0, interface, name, type, domain, resolve_cb, (void*)payload_copy); + init_sref_poll(payload_copy->sref, payload_copy); + if (err != kDNSServiceErr_NoError) { u3l_log("mdns: dns service resolve error %i", err); - c3_free(payload_copy); - DNSServiceRefDeallocate(sr); - return; + payload_copy->poll.data = payload_copy; + uv_close((uv_handle_t*)&payload_copy->poll, close_cb); } - init_sref_poll(sr, payload_copy); } } +static void register_close_cb(uv_handle_t* poll) { + // not freeing sref with DNSServiceRefDeallocate since that + // deregisters us from mdns + mdns_payload* payload = (mdns_payload*)poll->data; + c3_free(payload); +} + + static void register_cb(DNSServiceRef sref, DNSServiceFlags f, DNSServiceErrorType err, @@ -154,10 +162,8 @@ static void register_cb(DNSServiceRef sref, } else { u3l_log("mdns: %s registered on all interfaces", name); } - // not freeing sref with DNSServiceRefDeallocate since that - // deregisters us from mdns uv_poll_stop(&payload->poll); - c3_free(payload); + uv_close((uv_handle_t*)&payload->poll, register_close_cb); } void mdns_init(uint16_t port, char* our, mdns_cb* cb, void* context) From abb1599669cfe46ab89c97ca3759e0bcf36b4c67 Mon Sep 17 00:00:00 2001 From: yosoyubik Date: Fri, 1 Dec 2023 10:21:32 +0100 Subject: [PATCH 13/19] ames: account for variable-size num fragments --- pkg/vere/io/ames.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 24baa6e042..0b4f3ba3bf 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -112,7 +112,7 @@ typedef struct _u3_meow { c3_y sig_y[64]; // host signature c3_w num_w; // number of fragments - c3_w siz_s; // datum size (actual) + c3_s siz_s; // datum size (actual) c3_y* dat_y; // datum (0 if null response) } u3_meow; @@ -358,12 +358,25 @@ _fine_peep_size(u3_peep* pep_u) pep_u->len_s); } +static inline c3_y +_fine_bytes_word(c3_w num_w) +{ + return (c3_bits_word(num_w) + 7) >> 3; +} + static inline c3_s _fine_meow_size(u3_meow* mew_u) { + c3_y cur_y = 0; + if (mew_u->siz_s != 0) { + cur_y = sizeof(mew_u->num_w); + } + else { + cur_y = _fine_bytes_word(mew_u->num_w); + } return ( sizeof(mew_u->sig_y) + - sizeof(mew_u->num_w) + + cur_y + mew_u->siz_s); } @@ -603,7 +616,9 @@ _fine_sift_meow(u3_meow* mew_u, u3_noun mew) // parse number of fragments // u3r_bytes(cur_w, num_w, (c3_y*)&mew_u->num_w, mew); + num_w = c3_min(num_w, (len_w - cur_w)); cur_w += num_w; + u3_assert(len_w >= cur_w); // parse data payload // @@ -735,10 +750,22 @@ _fine_etch_meow(u3_meow* mew_u, c3_y* buf_y) memcpy(buf_y + cur_w, mew_u->sig_y, sig_w); cur_w += sig_w; - // write number of fragments - // - _ames_etch_word(buf_y + cur_w, mew_u->num_w); - cur_w += sizeof(mew_u->num_w); + { + c3_y num_y[4]; + c3_y len_y = _fine_bytes_word(mew_u->num_w); + + // write number of fragments + // + _ames_etch_word(num_y, mew_u->num_w); + memcpy(buf_y + cur_w, num_y, len_y); + + if (mew_u->siz_s != 0) { + cur_w += sizeof(mew_u->num_w); + } + else { + cur_w += len_y; + } + } // write response fragment data // From b95f2280bf7e67e365a8169f50a14ade195fcc95 Mon Sep 17 00:00:00 2001 From: Amadeo Bellotti Date: Mon, 4 Dec 2023 10:42:49 -0500 Subject: [PATCH 14/19] fixed lick bug where a shut port will still send soaks and errors if somethign is connected --- pkg/vere/io/lick.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pkg/vere/io/lick.c b/pkg/vere/io/lick.c index e420057246..b4681019e0 100644 --- a/pkg/vere/io/lick.c +++ b/pkg/vere/io/lick.c @@ -29,6 +29,7 @@ typedef struct _u3_shan { typedef struct _u3_port { c3_c* nam_c; // name of port c3_o con_o; + c3_o liv_o; struct _u3_shan* san_u; // server reference struct _u3_lick* lic_u; // device backpointer struct _u3_port* nex_u; // next pointer @@ -181,12 +182,16 @@ _lick_moor_poke(void* ptr_v, c3_d len_d, c3_y* byt_y) return; } - wir = u3nc(c3__lick, u3_nul); - dev = _lick_string_to_path(gen_u->nam_c+1); - cad = u3nt(c3__soak, dev, put); - u3_auto_peer( - u3_auto_plan(&lic_u->car_u, u3_ovum_init(0, c3__l, wir, cad)), - 0, 0, 0); + if ( c3y == gen_u->liv_o ) { + wir = u3nc(c3__lick, u3_nul); + dev = _lick_string_to_path(gen_u->nam_c+1); + cad = u3nt(c3__soak, dev, put); + u3_auto_peer( + u3_auto_plan(&lic_u->car_u, u3_ovum_init(0, c3__l, wir, cad)), + 0, 0, 0); + } else { + u3z(put); + } } /* _lick_close_chan(): close given channel, freeing. @@ -214,7 +219,7 @@ _lick_close_chan(u3_chan* can_u) } can_u->mor_u.nex_u = NULL; - if ( NULL == san_u->can_u ) { + if ( NULL == san_u->can_u && c3y == gen_u->liv_o ) { // send a close event to arvo and stop reading. // u3_noun wir, cad, dev, dat, mar; @@ -423,6 +428,7 @@ _lick_ef_shut(u3_lick* lic_u, u3_noun nam) while ( NULL != cur_u ) { if ( 0 == strcmp(cur_u->nam_c, nam_c) ) { + cur_u->liv_o = c3n; _lick_close_sock(cur_u->san_u); if( las_u == NULL ) { lic_u->gen_u = cur_u->nex_u; @@ -462,6 +468,7 @@ _lick_ef_spin(u3_lick* lic_u, u3_noun nam) gen_u->san_u->gen_u = gen_u; gen_u->nam_c = nam_c; gen_u->con_o = c3n; + gen_u->liv_o = c3y; _lick_init_sock(gen_u->san_u); if ( NULL == las_u) { From 9826d8ae291ef0709d91afed4f6952449a5f48a6 Mon Sep 17 00:00:00 2001 From: Amadeo Bellotti Date: Mon, 4 Dec 2023 10:54:39 -0500 Subject: [PATCH 15/19] close channel on a %shut --- pkg/vere/io/lick.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/lick.c b/pkg/vere/io/lick.c index b4681019e0..c47e1b63b4 100644 --- a/pkg/vere/io/lick.c +++ b/pkg/vere/io/lick.c @@ -182,16 +182,12 @@ _lick_moor_poke(void* ptr_v, c3_d len_d, c3_y* byt_y) return; } - if ( c3y == gen_u->liv_o ) { - wir = u3nc(c3__lick, u3_nul); - dev = _lick_string_to_path(gen_u->nam_c+1); - cad = u3nt(c3__soak, dev, put); - u3_auto_peer( - u3_auto_plan(&lic_u->car_u, u3_ovum_init(0, c3__l, wir, cad)), - 0, 0, 0); - } else { - u3z(put); - } + wir = u3nc(c3__lick, u3_nul); + dev = _lick_string_to_path(gen_u->nam_c+1); + cad = u3nt(c3__soak, dev, put); + u3_auto_peer( + u3_auto_plan(&lic_u->car_u, u3_ovum_init(0, c3__l, wir, cad)), + 0, 0, 0); } /* _lick_close_chan(): close given channel, freeing. @@ -306,6 +302,12 @@ static void _lick_close_sock(u3_shan* san_u) { u3_lick* lic_u = san_u->gen_u->lic_u; + u3_chan* can_u = san_u->can_u; + + if ( NULL != can_u ) { + _lick_close_chan(san_u->can_u); + } + c3_w len_w = strlen(lic_u->fod_c) + strlen(san_u->gen_u->nam_c) + 2; c3_c* paf_c = c3_malloc(len_w); c3_i wit_i; From ee2624fd95724e8e3c3fdb7f48ba47235f04adca Mon Sep 17 00:00:00 2001 From: Amadeo Bellotti Date: Mon, 4 Dec 2023 10:55:28 -0500 Subject: [PATCH 16/19] cleanup --- pkg/vere/io/lick.c | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/vere/io/lick.c b/pkg/vere/io/lick.c index c47e1b63b4..da29a5b2a1 100644 --- a/pkg/vere/io/lick.c +++ b/pkg/vere/io/lick.c @@ -302,7 +302,6 @@ static void _lick_close_sock(u3_shan* san_u) { u3_lick* lic_u = san_u->gen_u->lic_u; - u3_chan* can_u = san_u->can_u; if ( NULL != can_u ) { _lick_close_chan(san_u->can_u); From 8240f12db68e762aa3266994de19138b6dbdf684 Mon Sep 17 00:00:00 2001 From: Amadeo Bellotti Date: Mon, 4 Dec 2023 11:50:49 -0500 Subject: [PATCH 17/19] oops didnt compile --- pkg/vere/io/lick.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/lick.c b/pkg/vere/io/lick.c index da29a5b2a1..1c0a2d7a21 100644 --- a/pkg/vere/io/lick.c +++ b/pkg/vere/io/lick.c @@ -303,7 +303,7 @@ _lick_close_sock(u3_shan* san_u) { u3_lick* lic_u = san_u->gen_u->lic_u; - if ( NULL != can_u ) { + if ( NULL != san_u->can_u ) { _lick_close_chan(san_u->can_u); } From e40ed89d6f08a8abae9f93dc2681f469d3874577 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 19 Dec 2023 14:42:20 +0200 Subject: [PATCH 18/19] bazel: change dbus source since the official site is down --- WORKSPACE.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index cb17e02a42..a97ecb09d5 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -159,7 +159,7 @@ versioned_http_archive( build_file = "//bazel/third_party/dbus:dbus.BUILD", sha256 = "a6bd5bac5cf19f0c3c594bdae2565a095696980a683a0ef37cb6212e093bde35", strip_prefix = "dbus-{version}", - url = "https://dbus.freedesktop.org/releases/dbus/dbus-{version}.tar.xz", + url = "https://src.fedoraproject.org/repo/pkgs/dbus/dbus-{version}.tar.xz/sha512/f3dfc73da28cbe20449d15bbe4166c3574f0e551dfd15fca7cce2b8c71e778360ed2dd391ee5c414a7a47ff4b958727b26ef4cabfee70564f8d0a34bf5ad2386/dbus-1.14.8.tar.xz", version = "1.14.8", ) From 6aec5ef816eefed1b1e50df6e993cacaa32286d2 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 19 Dec 2023 16:02:45 +0200 Subject: [PATCH 19/19] bazel: looks like this makes the docker CI work --- WORKSPACE.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index a97ecb09d5..d3c4848fd1 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -205,9 +205,9 @@ versioned_http_archive( versioned_http_archive( name = "io_bazel_rules_go", - sha256 = "f2dcd210c7095febe54b804bb1cd3a58fe8435a909db2ec04e31542631cf715c", + sha256 = "d6ab6b57e48c09523e93050f13698f708428cfd5e619252e369d377af6597707", url = "https://github.com/bazelbuild/rules_go/releases/download/v{version}/rules_go-v{version}.zip", - version = "0.31.0", + version = "0.43.0", ) versioned_http_file(