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