Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thread_resolution_server.c Source File

thread_resolution_server.c

00001 /*
00002  * Copyright (c) 2014-2018, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: BSD-3-Clause
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holder nor the
00014  *    names of its contributors may be used to endorse or promote products
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 
00031 #include "nsconfig.h"
00032 #ifdef HAVE_THREAD_NEIGHBOR_DISCOVERY
00033 #include <string.h>
00034 #include <ns_types.h>
00035 #include <ns_list.h>
00036 #include <ns_trace.h>
00037 #include "nsdynmemLIB.h"
00038 #include "common_functions.h"
00039 #include "6LoWPAN/Thread/thread_resolution_server.h"
00040 
00041 #define TRACE_GROUP "TRsS"
00042 
00043 #include "coap_service_api.h"
00044 
00045 #include "net_interface.h"
00046 #include "socket_api.h"
00047 #include "thread_config.h"
00048 #include "thread_tmfcop_lib.h"
00049 #include "thread_management_if.h"
00050 #include "thread_management_server.h"
00051 #include "thread_commissioning_if.h"
00052 
00053 
00054 typedef struct thread_management_server {
00055     int8_t interface_id;
00056     int8_t coap_service_id;
00057     thread_resolution_server_addr_query_cb *query_cb_ptr;
00058     ns_list_link_t link;
00059 } thread_resolution_server_t;
00060 
00061 static NS_LIST_DEFINE(instance_list, thread_resolution_server_t, link);
00062 
00063 static thread_resolution_server_t *thread_resolution_server_find(int8_t interface_id)
00064 {
00065     ns_list_foreach(thread_resolution_server_t, cur_ptr, &instance_list) {
00066         if (cur_ptr->interface_id == interface_id) {
00067             return cur_ptr;
00068         }
00069     }
00070     return NULL;
00071 }
00072 
00073 static thread_resolution_server_t *thread_management_find_by_service(int8_t service_id)
00074 {
00075     ns_list_foreach(thread_resolution_server_t, cur_ptr, &instance_list) {
00076         if (cur_ptr->coap_service_id == service_id) {
00077             return cur_ptr;
00078         }
00079     }
00080     return NULL;
00081 }
00082 
00083 /**
00084  * Address query
00085  * uri = tn/d/aq
00086  */
00087 static int thread_resolution_server_query_cb(int8_t service_id, uint8_t source_address[16], uint16_t source_port, sn_coap_hdr_s *request_ptr)
00088 {
00089     thread_resolution_server_t *this = thread_management_find_by_service(service_id);
00090     uint8_t payload[2 + 16 + 2 + 2 + 2 + 8 + 2 + 4];
00091     uint8_t *target_ip_ptr;
00092     uint8_t mlEID[8];
00093     bool proxy;
00094     uint16_t rloc16;
00095     uint16_t requestor_rloc;
00096     uint32_t last_transaction_time = 0;
00097     uint8_t *ptr;
00098     (void)source_port;
00099 
00100     /* Address queries should be a multicast, non-confirmed POST. Basic CoAP
00101      * protocol dictates that we should respond in some way if it's not multicast,
00102      * but we currently can't distinguish. (RFC 7390 says "A server may choose
00103      * not to respond to an IP multicast request if there's nothing useful to
00104      * respond back", but that doesn't extend to unicast). So we just drop in
00105      * all cases of non-resolution. People shouldn't(TM) be sending unicast
00106      * queries anyway...
00107      *
00108      * Note that even when we have an answer, we don't actually send a CoAP
00109      * response to the POST, but we instead trigger a new unicast confirmed
00110      * POST to the sender.
00111      */
00112 
00113     if (!this || !this->query_cb_ptr) {
00114         /* XXX "Internal Server Error" response? */
00115         return -1;
00116     }
00117 
00118     if (16 > thread_tmfcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, TMFCOP_TLV_TARGET_EID, &target_ip_ptr)) {
00119         /* XXX "Bad Request" response? */
00120         return -1;
00121     }
00122 
00123     requestor_rloc = common_read_16_bit(source_address + 14);
00124 
00125     tr_debug("Thread address query %s", trace_ipv6(target_ip_ptr));
00126 
00127     int ret = this->query_cb_ptr(this->interface_id, target_ip_ptr, &requestor_rloc, &rloc16, &proxy, &last_transaction_time, mlEID);
00128     if (ret < 0) {
00129         /* XXX "Forbidden" response? */
00130         return -1;
00131     }
00132 
00133     tr_debug("Thread query locator %x", rloc16);
00134 
00135     ptr = payload;
00136     ptr = thread_tmfcop_tlv_data_write(ptr, TMFCOP_TLV_TARGET_EID, 16, target_ip_ptr);
00137     ptr = thread_tmfcop_tlv_data_write_uint16(ptr, TMFCOP_TLV_RLOC16, rloc16);
00138     ptr = thread_tmfcop_tlv_data_write(ptr, TMFCOP_TLV_ML_EID, 8, mlEID);
00139     if (proxy) {
00140         tr_debug("    proxy: last transaction %" PRIu32, last_transaction_time);
00141         ptr = thread_tmfcop_tlv_data_write_uint32(ptr, TMFCOP_TLV_LAST_TRANSACTION_TIME, last_transaction_time);
00142     }
00143 
00144     // XXX "Accepted" response?
00145 
00146     /* We don't require a response, so we don't specify a callback. Library
00147      * should retry itself until it gets an ACK.
00148      */
00149     coap_service_request_send(this->coap_service_id, COAP_REQUEST_OPTIONS_ADDRESS_SHORT,
00150                               source_address, THREAD_MANAGEMENT_PORT,
00151                               COAP_MSG_TYPE_CONFIRMABLE, COAP_MSG_CODE_REQUEST_POST,
00152                               THREAD_URI_ADDRESS_NOTIFICATION, COAP_CT_OCTET_STREAM,
00153                               payload, ptr - payload, NULL);
00154 
00155     return -1;// 0 is expecting response to the message as this is different behaviour need to handle it
00156 }
00157 
00158 /**
00159  * Public interface functions
00160  */
00161 int thread_resolution_server_init(int8_t interface_id, thread_resolution_server_addr_query_cb *query_cb)
00162 {
00163     thread_resolution_server_t *this = thread_resolution_server_find(interface_id);
00164     if (this) {
00165         return -1;
00166     }
00167 
00168     this = ns_dyn_mem_alloc(sizeof(thread_resolution_server_t));
00169     if (!this) {
00170         return -2;
00171     }
00172 
00173     this->interface_id = interface_id;
00174     this->coap_service_id = thread_management_server_service_id_get(interface_id);
00175     if (this->coap_service_id < 0) {
00176         tr_err("Thread resolution srv init failed");
00177         ns_dyn_mem_free(this);
00178         return -3;
00179     }
00180     this->query_cb_ptr = query_cb;
00181     ns_list_add_to_start(&instance_list, this);
00182 
00183     coap_service_register_uri(this->coap_service_id, THREAD_URI_ADDRESS_QUERY_REQUEST, COAP_SERVICE_ACCESS_POST_ALLOWED, thread_resolution_server_query_cb);
00184 
00185     return 0;
00186 }
00187 
00188 void thread_resolution_server_delete(int8_t interface_id)
00189 {
00190     thread_resolution_server_t *this = thread_resolution_server_find(interface_id);
00191     if (!this) {
00192         return;
00193     }
00194 
00195     coap_service_unregister_uri(this->coap_service_id, THREAD_URI_ADDRESS_QUERY_REQUEST);
00196 
00197     ns_list_remove(&instance_list, this);
00198     ns_dyn_mem_free(this);
00199 }
00200 
00201 #endif //HAVE_THREAD_NEIGHBOR_DISCOVERY
00202