Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | |
| 9 | #include <memory> |
| 10 | |
| 11 | /*** |
| 12 | This file is part of avahi. |
| 13 | avahi is free software; you can redistribute it and/or modify it |
| 14 | under the terms of the GNU Lesser General Public License as |
| 15 | published by the Free Software Foundation; either version 2.1 of the |
| 16 | License, or (at your option) any later version. |
| 17 | avahi is distributed in the hope that it will be useful, but WITHOUT |
| 18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 19 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General |
| 20 | Public License for more details. |
| 21 | You should have received a copy of the GNU Lesser General Public |
| 22 | License along with avahi; if not, write to the Free Software |
| 23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 24 | USA. |
| 25 | ***/ |
| 26 | |
| 27 | typedef struct AvahiPoll AvahiPoll; |
| 28 | |
| 29 | typedef enum { |
| 30 | AVAHI_SERVER_INVALID, |
| 31 | AVAHI_SERVER_REGISTERING, |
| 32 | AVAHI_SERVER_RUNNING, |
| 33 | AVAHI_SERVER_COLLISION, |
| 34 | AVAHI_SERVER_FAILURE |
| 35 | } AvahiServerState; |
| 36 | |
| 37 | typedef struct AvahiClient AvahiClient; |
| 38 | |
| 39 | typedef enum { |
| 40 | AVAHI_CLIENT_S_REGISTERING = AVAHI_SERVER_REGISTERING, |
| 41 | AVAHI_CLIENT_S_RUNNING = AVAHI_SERVER_RUNNING, |
| 42 | AVAHI_CLIENT_S_COLLISION = AVAHI_SERVER_COLLISION, |
| 43 | AVAHI_CLIENT_FAILURE = 100, |
| 44 | AVAHI_CLIENT_CONNECTING = 101 |
| 45 | } AvahiClientState; |
| 46 | |
| 47 | typedef enum { |
| 48 | AVAHI_CLIENT_IGNORE_USER_CONFIG = 1, |
| 49 | AVAHI_CLIENT_NO_FAIL = 2 |
| 50 | } AvahiClientFlags; |
| 51 | |
| 52 | typedef void (*AvahiClientCallback)(AvahiClient* s, AvahiClientState state, |
| 53 | void* userdata); |
| 54 | |
| 55 | typedef struct AvahiServiceBrowser AvahiServiceBrowser; |
| 56 | |
| 57 | typedef int AvahiProtocol; |
| 58 | |
| 59 | typedef int AvahiIfIndex; |
| 60 | |
| 61 | typedef enum { |
| 62 | AVAHI_BROWSER_NEW, |
| 63 | AVAHI_BROWSER_REMOVE, |
| 64 | AVAHI_BROWSER_CACHE_EXHAUSTED, |
| 65 | AVAHI_BROWSER_ALL_FOR_NOW, |
| 66 | AVAHI_BROWSER_FAILURE |
| 67 | } AvahiBrowserEvent; |
| 68 | |
| 69 | typedef enum { |
| 70 | AVAHI_LOOKUP_RESULT_CACHED = 1, |
| 71 | AVAHI_LOOKUP_RESULT_WIDE_AREA = 2, |
| 72 | AVAHI_LOOKUP_RESULT_MULTICAST = 4, |
| 73 | AVAHI_LOOKUP_RESULT_LOCAL = 8, |
| 74 | AVAHI_LOOKUP_RESULT_OUR_OWN = 16, |
| 75 | AVAHI_LOOKUP_RESULT_STATIC = 32 |
| 76 | } AvahiLookupResultFlags; |
| 77 | |
| 78 | typedef void (*AvahiServiceBrowserCallback)( |
| 79 | AvahiServiceBrowser* b, AvahiIfIndex interface, AvahiProtocol protocol, |
| 80 | AvahiBrowserEvent event, const char* name, const char* type, |
| 81 | const char* domain, AvahiLookupResultFlags flags, void* userdata); |
| 82 | |
| 83 | typedef enum { |
| 84 | AVAHI_LOOKUP_USE_WIDE_AREA = 1, |
| 85 | AVAHI_LOOKUP_USE_MULTICAST = 2, |
| 86 | |
| 87 | AVAHI_LOOKUP_NO_TXT = 4, |
| 88 | AVAHI_LOOKUP_NO_ADDRESS = 8 |
| 89 | } AvahiLookupFlags; |
| 90 | |
| 91 | typedef struct AvahiServiceResolver AvahiServiceResolver; |
| 92 | |
| 93 | typedef enum { |
| 94 | AVAHI_RESOLVER_FOUND, |
| 95 | AVAHI_RESOLVER_FAILURE |
| 96 | } AvahiResolverEvent; |
| 97 | |
| 98 | typedef struct AvahiIPv4Address { |
| 99 | uint32_t address; |
| 100 | } AvahiIPv4Address; |
| 101 | |
| 102 | typedef struct AvahiIPv6Address { |
| 103 | uint8_t address[16]; |
| 104 | } AvahiIPv6Address; |
| 105 | |
| 106 | typedef struct AvahiAddress { |
| 107 | AvahiProtocol proto; |
| 108 | |
| 109 | union { |
| 110 | AvahiIPv6Address ipv6; |
| 111 | AvahiIPv4Address ipv4; |
| 112 | uint8_t data[1]; |
| 113 | } data; |
| 114 | } AvahiAddress; |
| 115 | |
| 116 | typedef struct AvahiStringList { |
| 117 | struct AvahiStringList* next; |
| 118 | size_t size; |
| 119 | uint8_t text[1]; |
| 120 | } AvahiStringList; |
| 121 | |
| 122 | typedef void (*AvahiServiceResolverCallback)( |
| 123 | AvahiServiceResolver* r, AvahiIfIndex interface, AvahiProtocol protocol, |
| 124 | AvahiResolverEvent event, const char* name, const char* type, |
| 125 | const char* domain, const char* host_name, const AvahiAddress* a, |
| 126 | uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags flags, |
| 127 | void* userdata); |
| 128 | |
| 129 | typedef struct AvahiThreadedPoll AvahiThreadedPoll; |
| 130 | |
| 131 | typedef struct AvahiEntryGroup AvahiEntryGroup; |
| 132 | |
| 133 | typedef enum { |
| 134 | AVAHI_ENTRY_GROUP_UNCOMMITED, |
| 135 | AVAHI_ENTRY_GROUP_REGISTERING, |
| 136 | AVAHI_ENTRY_GROUP_ESTABLISHED, |
| 137 | AVAHI_ENTRY_GROUP_COLLISION, |
| 138 | AVAHI_ENTRY_GROUP_FAILURE |
| 139 | } AvahiEntryGroupState; |
| 140 | |
| 141 | typedef void (*AvahiEntryGroupCallback)(AvahiEntryGroup* g, |
| 142 | AvahiEntryGroupState state, |
| 143 | void* userdata); |
| 144 | |
| 145 | typedef enum { |
| 146 | AVAHI_PUBLISH_UNIQUE = 1, |
| 147 | AVAHI_PUBLISH_NO_PROBE = 2, |
| 148 | AVAHI_PUBLISH_NO_ANNOUNCE = 4, |
| 149 | AVAHI_PUBLISH_ALLOW_MULTIPLE = 8, |
| 150 | |
| 151 | AVAHI_PUBLISH_NO_REVERSE = 16, |
| 152 | AVAHI_PUBLISH_NO_COOKIE = 32, |
| 153 | AVAHI_PUBLISH_UPDATE = 64, |
| 154 | AVAHI_PUBLISH_USE_WIDE_AREA = 128, |
| 155 | AVAHI_PUBLISH_USE_MULTICAST = 256 |
| 156 | } AvahiPublishFlags; |
| 157 | |
| 158 | enum { AVAHI_IF_UNSPEC = -1 }; |
| 159 | |
| 160 | enum { AVAHI_PROTO_INET = 0, AVAHI_PROTO_INET6 = 1, AVAHI_PROTO_UNSPEC = -1 }; |
| 161 | |
| 162 | enum { |
| 163 | AVAHI_OK = 0, |
| 164 | AVAHI_ERR_FAILURE = -1, |
| 165 | AVAHI_ERR_BAD_STATE = -2, |
| 166 | AVAHI_ERR_INVALID_HOST_NAME = -3, |
| 167 | AVAHI_ERR_INVALID_DOMAIN_NAME = -4, |
| 168 | AVAHI_ERR_NO_NETWORK = -5, |
| 169 | AVAHI_ERR_INVALID_TTL = -6, |
| 170 | AVAHI_ERR_IS_PATTERN = -7, |
| 171 | AVAHI_ERR_COLLISION = -8, |
| 172 | AVAHI_ERR_INVALID_RECORD = -9, |
| 173 | |
| 174 | AVAHI_ERR_INVALID_SERVICE_NAME = -10, |
| 175 | AVAHI_ERR_INVALID_SERVICE_TYPE = -11, |
| 176 | AVAHI_ERR_INVALID_PORT = -12, |
| 177 | AVAHI_ERR_INVALID_KEY = -13, |
| 178 | AVAHI_ERR_INVALID_ADDRESS = -14, |
| 179 | AVAHI_ERR_TIMEOUT = -15, |
| 180 | AVAHI_ERR_TOO_MANY_CLIENTS = -16, |
| 181 | AVAHI_ERR_TOO_MANY_OBJECTS = -17, |
| 182 | AVAHI_ERR_TOO_MANY_ENTRIES = -18, |
| 183 | AVAHI_ERR_OS = -19, |
| 184 | |
| 185 | AVAHI_ERR_ACCESS_DENIED = -20, |
| 186 | AVAHI_ERR_INVALID_OPERATION = -21, |
| 187 | AVAHI_ERR_DBUS_ERROR = -22, |
| 188 | AVAHI_ERR_DISCONNECTED = -23, |
| 189 | AVAHI_ERR_NO_MEMORY = -24, |
| 190 | AVAHI_ERR_INVALID_OBJECT = -25, |
| 191 | AVAHI_ERR_NO_DAEMON = -26, |
| 192 | AVAHI_ERR_INVALID_INTERFACE = -27, |
| 193 | AVAHI_ERR_INVALID_PROTOCOL = -28, |
| 194 | AVAHI_ERR_INVALID_FLAGS = -29, |
| 195 | |
| 196 | AVAHI_ERR_NOT_FOUND = -30, |
| 197 | AVAHI_ERR_INVALID_CONFIG = -31, |
| 198 | AVAHI_ERR_VERSION_MISMATCH = -32, |
| 199 | AVAHI_ERR_INVALID_SERVICE_SUBTYPE = -33, |
| 200 | AVAHI_ERR_INVALID_PACKET = -34, |
| 201 | AVAHI_ERR_INVALID_DNS_ERROR = -35, |
| 202 | AVAHI_ERR_DNS_FORMERR = -36, |
| 203 | AVAHI_ERR_DNS_SERVFAIL = -37, |
| 204 | AVAHI_ERR_DNS_NXDOMAIN = -38, |
| 205 | AVAHI_ERR_DNS_NOTIMP = -39, |
| 206 | |
| 207 | AVAHI_ERR_DNS_REFUSED = -40, |
| 208 | AVAHI_ERR_DNS_YXDOMAIN = -41, |
| 209 | AVAHI_ERR_DNS_YXRRSET = -42, |
| 210 | AVAHI_ERR_DNS_NXRRSET = -43, |
| 211 | AVAHI_ERR_DNS_NOTAUTH = -44, |
| 212 | AVAHI_ERR_DNS_NOTZONE = -45, |
| 213 | AVAHI_ERR_INVALID_RDATA = -46, |
| 214 | AVAHI_ERR_INVALID_DNS_CLASS = -47, |
| 215 | AVAHI_ERR_INVALID_DNS_TYPE = -48, |
| 216 | AVAHI_ERR_NOT_SUPPORTED = -49, |
| 217 | |
| 218 | AVAHI_ERR_NOT_PERMITTED = -50, |
| 219 | AVAHI_ERR_INVALID_ARGUMENT = -51, |
| 220 | AVAHI_ERR_IS_EMPTY = -52, |
| 221 | AVAHI_ERR_NO_CHANGE = -53, |
| 222 | |
| 223 | AVAHI_ERR_MAX = -54 |
| 224 | }; |
| 225 | |
| 226 | namespace wpi { |
| 227 | class AvahiFunctionTable { |
| 228 | public: |
| 229 | #define AvahiFunction(CapName, RetType, Parameters) \ |
| 230 | using CapName##_func = RetType(*) Parameters; \ |
| 231 | CapName##_func CapName = nullptr |
| 232 | |
| 233 | AvahiFunction(threaded_poll_new, AvahiThreadedPoll*, (void)); |
| 234 | AvahiFunction(threaded_poll_free, void, (AvahiThreadedPoll*)); |
| 235 | AvahiFunction(threaded_poll_get, const AvahiPoll*, (AvahiThreadedPoll*)); |
| 236 | AvahiFunction(threaded_poll_start, int, (AvahiThreadedPoll*)); |
| 237 | AvahiFunction(threaded_poll_stop, int, (AvahiThreadedPoll*)); |
| 238 | AvahiFunction(threaded_poll_lock, int, (AvahiThreadedPoll*)); |
| 239 | AvahiFunction(threaded_poll_unlock, int, (AvahiThreadedPoll*)); |
| 240 | |
| 241 | AvahiFunction(client_new, AvahiClient*, |
| 242 | (const AvahiPoll* poll_api, AvahiClientFlags flags, |
| 243 | AvahiClientCallback callback, void* userdata, int* error)); |
| 244 | AvahiFunction(client_free, void, (AvahiClient*)); |
| 245 | |
| 246 | AvahiFunction(service_browser_new, AvahiServiceBrowser*, |
| 247 | (AvahiClient * client, AvahiIfIndex interface, |
| 248 | AvahiProtocol protocol, const char* type, const char* domain, |
| 249 | AvahiLookupFlags flags, AvahiServiceBrowserCallback callback, |
| 250 | void* userdata)); |
| 251 | |
| 252 | AvahiFunction(service_browser_free, int, (AvahiServiceBrowser*)); |
| 253 | |
| 254 | AvahiFunction(service_resolver_new, AvahiServiceResolver*, |
| 255 | (AvahiClient * client, AvahiIfIndex interface, |
| 256 | AvahiProtocol protocol, const char* name, const char* type, |
| 257 | const char* domain, AvahiProtocol aprotocol, |
| 258 | AvahiLookupFlags flags, AvahiServiceResolverCallback callback, |
| 259 | void* userdata)); |
| 260 | AvahiFunction(service_resolver_free, int, (AvahiServiceResolver*)); |
| 261 | |
| 262 | AvahiFunction(entry_group_new, AvahiEntryGroup*, |
| 263 | (AvahiClient*, AvahiEntryGroupCallback, void*)); |
| 264 | AvahiFunction(entry_group_free, int, (AvahiEntryGroup*)); |
| 265 | |
| 266 | AvahiFunction(entry_group_add_service_strlst, int, |
| 267 | (AvahiEntryGroup * group, AvahiIfIndex interface, |
| 268 | AvahiProtocol protocol, AvahiPublishFlags flags, |
| 269 | const char* name, const char* type, const char* domain, |
| 270 | const char* host, uint16_t port, AvahiStringList*)); |
| 271 | |
| 272 | AvahiFunction(entry_group_reset, int, (AvahiEntryGroup*)); |
| 273 | AvahiFunction(entry_group_is_empty, int, (AvahiEntryGroup*)); |
| 274 | AvahiFunction(entry_group_commit, int, (AvahiEntryGroup*)); |
| 275 | AvahiFunction(entry_group_get_client, AvahiClient*, (AvahiEntryGroup*)); |
| 276 | |
| 277 | AvahiFunction(string_list_new_from_array, AvahiStringList*, |
| 278 | (const char** array, int len)); |
| 279 | AvahiFunction(string_list_free, void, (AvahiStringList*)); |
| 280 | |
| 281 | AvahiFunction(service_browser_get_client, AvahiClient*, |
| 282 | (AvahiServiceBrowser*)); |
| 283 | |
| 284 | AvahiFunction(unescape_label, char*, (const char**, char*, size_t)); |
| 285 | AvahiFunction(alternative_service_name, char*, (const char*)); |
| 286 | AvahiFunction(free, void, (void*)); |
| 287 | |
| 288 | bool IsValid() const { return valid; } |
| 289 | |
| 290 | static AvahiFunctionTable& Get(); |
| 291 | |
| 292 | private: |
| 293 | AvahiFunctionTable(); |
| 294 | bool valid; |
| 295 | }; |
| 296 | |
| 297 | class AvahiThread { |
| 298 | private: |
| 299 | struct private_init {}; |
| 300 | |
| 301 | public: |
| 302 | explicit AvahiThread(const private_init&); |
| 303 | ~AvahiThread() noexcept; |
| 304 | |
| 305 | void lock(); |
| 306 | const AvahiPoll* GetPoll() const; |
| 307 | void unlock(); |
| 308 | |
| 309 | static std::shared_ptr<AvahiThread> Get(); |
| 310 | |
| 311 | private: |
| 312 | AvahiThreadedPoll* threadedPoll; |
| 313 | AvahiFunctionTable& table = AvahiFunctionTable::Get(); |
| 314 | }; |
| 315 | |
| 316 | } // namespace wpi |