1 1.1 christos /* srp-api.h 2 1.1 christos * 3 1.1 christos * Copyright (c) 2019 Apple Computer, Inc. All rights reserved. 4 1.1 christos * 5 1.1 christos * Licensed under the Apache License, Version 2.0 (the "License"); 6 1.1 christos * you may not use this file except in compliance with the License. 7 1.1 christos * You may obtain a copy of the License at 8 1.1 christos * 9 1.1 christos * http://www.apache.org/licenses/LICENSE-2.0 10 1.1 christos * 11 1.1 christos * Unless required by applicable law or agreed to in writing, software 12 1.1 christos * distributed under the License is distributed on an "AS IS" BASIS, 13 1.1 christos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 1.1 christos * See the License for the specific language governing permissions and 15 1.1 christos * limitations under the License. 16 1.1 christos * 17 1.1 christos * Structure definitions for the Service Registration Protocol gateway. 18 1.1 christos */ 19 1.1 christos 20 1.1 christos #include "srp.h" 21 1.1 christos #if defined(THREAD_DEVKIT_ADK) || defined(LINUX) 22 1.1 christos #include "../mDNSShared/dns_sd.h" 23 1.1 christos #endif 24 1.1 christos 25 1.1 christos #ifdef __cplusplus 26 1.1 christos extern "C" { 27 1.1 christos #endif 28 1.1 christos 29 1.1 christos typedef void (*srp_hostname_conflict_callback_t)(const char *NONNULL hostname); 30 1.1 christos typedef void (*srp_wakeup_callback_t)(void *NONNULL state); 31 1.1 christos typedef void (*srp_datagram_callback_t)(void *NONNULL state, void *NONNULL message, size_t message_length); 32 1.1 christos typedef struct client_state client_state_t; 33 1.1 christos typedef struct dns_wire dns_wire_t; 34 1.1 christos 35 1.1 christos // The below functions provide a way for the host to inform the SRP service of the state of the network. 36 1.1 christos 37 1.1 christos // For testing 38 1.1 christos client_state_t *NULLABLE srp_client_get_current(void); 39 1.1 christos void srp_client_set_current(client_state_t *NONNULL new_client); 40 1.1 christos dns_wire_t *NULLABLE srp_client_generate_update(client_state_t *NONNULL client, 41 1.1 christos uint32_t update_lease_time, uint32_t update_key_lease_time, 42 1.1 christos size_t *NONNULL p_length, dns_wire_t *NULLABLE in_wire, 43 1.1 christos uint32_t serial, bool removing); 44 1.1 christos int srp_host_key_reset_for_client(client_state_t *NONNULL client); 45 1.1 christos 46 1.1 christos // Call this before calling anything else. Context will be passed back whenever the srp code 47 1.1 christos // calls any of the host functions. 48 1.1 christos int srp_host_init(void *NULLABLE host_context); 49 1.1 christos 50 1.1 christos // Call this to reset the host key (e.g. on factory reset) 51 1.1 christos int srp_host_key_reset(void); 52 1.1 christos 53 1.1 christos // This function can be called by accessories that have different requirements for lease intervals. 54 1.1 christos // Normally new_lease_time would be 3600 (1 hour) and new_key_lease_type would be 604800 (7 days). 55 1.1 christos int srp_set_lease_times(uint32_t new_lease_time, uint32_t new_key_lease_time); 56 1.1 christos 57 1.1 christos // Called when a new address is configured that should be advertised. This can be called during a refresh, 58 1.1 christos // in which case it doesn't mark the network state as changed if the address was already present. 59 1.1 christos int srp_add_interface_address(uint16_t rrtype, const uint8_t *NONNULL rdata, uint16_t rdlen); 60 1.1 christos 61 1.1 christos // Called whenever the SRP server address changes or the SRP server becomes newly reachable. This can be 62 1.1 christos // called during a refresh, in which case it doesn't mark the network state as changed if the address was 63 1.1 christos // already present. 64 1.1 christos int srp_add_server_address(const uint8_t *NONNULL port, uint16_t rrtype, const uint8_t *NONNULL rdata, uint16_t rdlen); 65 1.1 christos 66 1.1 christos // Called when the node knows its hostname (usually once). The callback is called if we try to do an SRP 67 1.1 christos // update and find out that the hostname is in use; in this case, the callback is expected to generate a new 68 1.1 christos // hostname and re-register it. It is permitted to call srp_set_hostname() from the callback. 69 1.1 christos // If the hostname is changed by the callback, then it is used immediately on return from the callback; 70 1.1 christos // if the hostname is changed in any other situation, nothing is done with the new name until 71 1.1 christos // srp_network_state_stable() is called. 72 1.1 christos int srp_set_hostname(const char *NONNULL hostname, srp_hostname_conflict_callback_t NULLABLE callback); 73 1.1 christos 74 1.1 christos // Called when a network state change is complete (that is, all new addresses have been saved and 75 1.1 christos // any update to the SRP server address has been provided). This is only needed when not using the 76 1.1 christos // refresh mechanism. 77 1.1 christos int srp_network_state_stable(bool *NULLABLE did_something); 78 1.1 christos 79 1.1 christos // Delete a previously-configured SRP server address. This should not be done during a refresh. 80 1.1 christos int srp_delete_interface_address(uint16_t rrtype, const uint8_t *NONNULL rdata, uint16_t rdlen); 81 1.1 christos 82 1.1 christos // Delete a previously-configured SRP server address. This should not be done during a refresh. 83 1.1 christos int srp_delete_server_address(uint16_t rrtype, const uint8_t *NONNULL port, const uint8_t *NONNULL rdata, 84 1.1 christos uint16_t rdlen); 85 1.1 christos 86 1.1 christos // Call this to start an address refresh. This makes sense to do in cases where the caller 87 1.1 christos // is not tracking changes, but rather is just doing a full refresh whenever the network state 88 1.1 christos // is seen to have changed. When the refresh is done, if any addresses were added or removed, 89 1.1 christos // network_state_changed will be true, and so a call to dnssd_network_state_change_finished() 90 1.1 christos // will trigger an update; if nothing changed, no update will be sent. 91 1.1 christos int srp_start_address_refresh(void); 92 1.1 christos 93 1.1 christos // Call this when the address refresh is done. This invokes srp_network_state_stable(). 94 1.1 christos int srp_finish_address_refresh(bool *NULLABLE did_something); 95 1.1 christos 96 1.1 christos // Call this to deregister everything that's currently registered. A return value other than kDNSServiceErr_NoError 97 1.1 christos // means that there's nothing to deregister. 98 1.1 christos int srp_deregister(void *NULLABLE os_context); 99 1.1 christos 100 1.1 christos // Call this to deregister a specific service instance, identified by the DNSServiceRef. A return value 101 1.1 christos // other than kDNSServiceErr_NoError means that the specified service instance wasn't found. 102 1.1 christos int srp_deregister_instance(DNSServiceRef NULLABLE sdRef); 103 1.1 christos 104 1.1 christos // Call this to update the service type on an existing registration. This only makes sense for a subtype: if 105 1.1 christos // this changes the base type, it will look like a new service instance to the SRP server. 106 1.1 christos DNSServiceErrorType srp_update_service_type(DNSServiceRef NONNULL sdRef, const char *NONNULL regtype, DNSServiceRegisterReply NULLABLE callback, void *NULLABLE context); 107 1.1 christos 108 1.1 christos // The below functions must be provided by the host. 109 1.1 christos 110 1.1 christos // This function fetches a key with the specified name for use in signing SRP updates. 111 1.1 christos // At present, only ECDSA is supported. If a key with the specified name doesn't exist, 112 1.1 christos // the host is expected to generate and store it. 113 1.1 christos srp_key_t *NULLABLE srp_get_key(const char *NONNULL key_name, void *NULLABLE host_context); 114 1.1 christos 115 1.1 christos // This function clears the key with the specified name. 116 1.1 christos int srp_reset_key(const char *NONNULL key_name, void *NULLABLE host_context); 117 1.1 christos 118 1.1 christos // This function fetches the IP address type (rrtype), address (rrdata x rdlength) and port (port[0], 119 1.1 christos // port[1]) of the most recent server with which the SRP client has successfully registered from stable 120 1.1 christos // storage. If the fetch is successful and there was a server recorded in stable storage, it returns true; 121 1.1 christos // otherwise it returns false. A false status can mean that there's no way to fetch this information, that 122 1.1 christos // no registration has happened in the past, or that there was some other error accessing stable storage. 123 1.1 christos bool srp_get_last_server(uint16_t *NONNULL rrtype, uint8_t *NONNULL rrdata, uint16_t rdlength, 124 1.1 christos uint8_t *NONNULL port, void *NULLABLE host_context); 125 1.1 christos 126 1.1 christos // This function stores the IP address type (rrtype), address (rrdata x rdlength) and port (port[0], 127 1.1 christos // port[1]) of the most recent server with which the SRP client has successfully registered to stable 128 1.1 christos // storage. If the store is successful, it returns true; otherwise it returns false. A false status can 129 1.1 christos // mean that there's no way to store this information, or that there was an error writing this information 130 1.1 christos // to stable storage. 131 1.1 christos bool srp_save_last_server(uint16_t rrtype, uint8_t *NONNULL rrdata, uint16_t rdlength, 132 1.1 christos uint8_t *NONNULL port, void *NULLABLE host_context); 133 1.1 christos 134 1.1 christos // This is called to create a context for sending and receiving UDP messages to and from a specified 135 1.1 christos // remote host address and port. The context passed is to be used whenever the srp host implementation 136 1.1 christos // does a callback, e.g. when a datagram arrives or when a wakeup occurs (see srp_set_wakeup()). 137 1.1 christos // The context is not actually connected to a specific address and port until srp_connect_udp() is 138 1.1 christos // invoked on it. 139 1.1 christos int srp_make_udp_context(void *NULLABLE host_context, void *NULLABLE *NONNULL p_context, 140 1.1 christos srp_datagram_callback_t NONNULL callback, void *NONNULL context); 141 1.1 christos 142 1.1 christos // Connect a udp context to a particular destination. The context has to have already been created by 143 1.1 christos // srp_make_udp_context(). When packets are received, they will be passed to the callback set 144 1.1 christos // in srp_make_udp_context(). This must not be called on a context that is already bound to 145 1.1 christos // some other destination--call srp_disconnect_udp() first if reusing. 146 1.1 christos int 147 1.1 christos srp_connect_udp(void *NONNULL context, const uint8_t *NONNULL port, uint16_t address_type, 148 1.1 christos const uint8_t *NONNULL address, uint16_t addrlen); 149 1.1 christos 150 1.1 christos // Disconnect a udp context. This is used to dissociate from the udp context state that was created 151 1.1 christos // by a previous call to srp_connect_udp 152 1.1 christos int 153 1.1 christos srp_disconnect_udp(void *NONNULL context); 154 1.1 christos 155 1.1 christos // This gets rid of the UDP context, frees any associated memory, cancels any outstanding wakeups. 156 1.1 christos // The freeing may occur later than the deactivating, depending on how the underlying event loop 157 1.1 christos // works. 158 1.1 christos int srp_deactivate_udp_context(void *NONNULL host_context, void *NONNULL context); 159 1.1 christos 160 1.1 christos // This is called to send a datagram to a UDP connection. The UDP connection is identified by the 161 1.1 christos // anonymous pointer that was returned by srp_make_udp_context(). 162 1.1 christos int srp_send_datagram(void *NULLABLE host_context, 163 1.1 christos void *NONNULL context, void *NONNULL message, size_t message_length); 164 1.1 christos 165 1.1 christos // This is called with the context returned by srp_make_udp_context. The caller is expected to schedule 166 1.1 christos // a wakeup event <milliseconds> in the future, when when that event occurs, it's expected to call the 167 1.1 christos // callback with the context that was passed to srp_make_udp_context. 168 1.1 christos int srp_set_wakeup(void *NULLABLE host_context, 169 1.1 christos void *NONNULL context, int milliseconds, srp_wakeup_callback_t NONNULL callback); 170 1.1 christos 171 1.1 christos // This is called to cancel a wakeup, and should not fail even if there is no wakeup pending. 172 1.1 christos int srp_cancel_wakeup(void *NULLABLE host_context, void *NONNULL context); 173 1.1 christos 174 1.1 christos // Returns the current wall clock time in seconds since 1970 175 1.1 christos uint32_t srp_timenow(void); 176 1.1 christos 177 1.1 christos #ifdef __cplusplus 178 1.1 christos } // extern "C" 179 1.1 christos #endif 180 1.1 christos 181 1.1 christos // Local Variables: 182 1.1 christos // mode: C 183 1.1 christos // tab-width: 4 184 1.1 christos // c-file-style: "bsd" 185 1.1 christos // c-basic-offset: 4 186 1.1 christos // fill-column: 108 187 1.1 christos // indent-tabs-mode: nil 188 1.1 christos // End: 189