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