Knight KE / Mbed OS Game_Master
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-2017, 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_commissioning_if.h"
00051 
00052 
00053 typedef struct thread_management_server {
00054     int8_t interface_id;
00055     int8_t coap_service_id;
00056     thread_resolution_server_addr_query_cb *query_cb_ptr;
00057     ns_list_link_t link;
00058 } thread_resolution_server_t;
00059 
00060 static NS_LIST_DEFINE(instance_list, thread_resolution_server_t, link);
00061 
00062 static thread_resolution_server_t *thread_resolution_server_find(int8_t interface_id)
00063 {
00064     ns_list_foreach(thread_resolution_server_t, cur_ptr, &instance_list) {
00065         if (cur_ptr->interface_id == interface_id) {
00066             return cur_ptr;
00067         }
00068     }
00069     return NULL;
00070 }
00071 
00072 static thread_resolution_server_t *thread_management_find_by_service(int8_t service_id)
00073 {
00074     ns_list_foreach(thread_resolution_server_t, cur_ptr, &instance_list) {
00075         if (cur_ptr->coap_service_id == service_id) {
00076             return cur_ptr;
00077         }
00078     }
00079     return NULL;
00080 }
00081 
00082 /**
00083  * Address query
00084  * uri = tn/d/aq
00085  */
00086 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)
00087 {
00088     thread_resolution_server_t *this = thread_management_find_by_service(service_id);
00089     uint8_t payload[2 + 16 + 2 + 2 + 2 + 8 + 2 + 4];
00090     uint8_t *target_ip_ptr;
00091     uint8_t mlEID[8];
00092     bool proxy;
00093     uint16_t rloc16;
00094     uint16_t requestor_rloc;
00095     uint32_t last_transaction_time = 0;
00096     uint8_t *ptr;
00097     (void)source_port;
00098 
00099     /* Address queries should be a multicast, non-confirmed POST. Basic CoAP
00100      * protocol dictates that we should respond in some way if it's not multicast,
00101      * but we currently can't distinguish. (RFC 7390 says "A server may choose
00102      * not to respond to an IP multicast request if there's nothing useful to
00103      * respond back", but that doesn't extend to unicast). So we just drop in
00104      * all cases of non-resolution. People shouldn't(TM) be sending unicast
00105      * queries anyway...
00106      *
00107      * Note that even when we have an answer, we don't actually send a CoAP
00108      * response to the POST, but we instead trigger a new unicast confirmed
00109      * POST to the sender.
00110      */
00111 
00112     if (!this || !this->query_cb_ptr) {
00113         /* XXX "Internal Server Error" response? */
00114         return -1;
00115     }
00116 
00117     if (16 > thread_tmfcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, TMFCOP_TLV_TARGET_EID, &target_ip_ptr)) {
00118         /* XXX "Bad Request" response? */
00119         return -1;
00120     }
00121 
00122     requestor_rloc = common_read_16_bit(source_address + 14);
00123 
00124     tr_debug("Thread address query %s", trace_ipv6(target_ip_ptr));
00125 
00126     int ret = this->query_cb_ptr(this->interface_id, target_ip_ptr, &requestor_rloc, &rloc16, &proxy, &last_transaction_time, mlEID);
00127     if (ret < 0) {
00128         /* XXX "Forbidden" response? */
00129         return -1;
00130     }
00131 
00132     tr_debug("Thread query locator %x", rloc16);
00133 
00134     ptr = payload;
00135     ptr = thread_tmfcop_tlv_data_write(ptr, TMFCOP_TLV_TARGET_EID, 16, target_ip_ptr);
00136     ptr = thread_tmfcop_tlv_data_write_uint16(ptr, TMFCOP_TLV_RLOC16, rloc16);
00137     ptr = thread_tmfcop_tlv_data_write(ptr, TMFCOP_TLV_ML_EID, 8, mlEID);
00138     if (proxy) {
00139         tr_debug("    proxy: last transaction %" PRIu32, last_transaction_time);
00140         ptr = thread_tmfcop_tlv_data_write_uint32(ptr, TMFCOP_TLV_LAST_TRANSACTION_TIME, last_transaction_time);
00141     }
00142 
00143     // XXX "Accepted" response?
00144 
00145     /* We don't require a response, so we don't specify a callback. Library
00146      * should retry itself until it gets an ACK.
00147      */
00148     coap_service_request_send(this->coap_service_id, COAP_REQUEST_OPTIONS_ADDRESS_SHORT,
00149                               source_address, THREAD_MANAGEMENT_PORT,
00150                               COAP_MSG_TYPE_CONFIRMABLE, COAP_MSG_CODE_REQUEST_POST,
00151                               THREAD_URI_ADDRESS_NOTIFICATION, COAP_CT_OCTET_STREAM,
00152                               payload, ptr - payload, NULL);
00153 
00154     return -1;// 0 is expecting response to the message as this is different behaviour need to handle it
00155 }
00156 
00157 /**
00158  * Public interface functions
00159  */
00160 int thread_resolution_server_init(int8_t interface_id, thread_resolution_server_addr_query_cb *query_cb)
00161 {
00162     thread_resolution_server_t *this = thread_resolution_server_find(interface_id);
00163     if (this) {
00164         return -1;
00165     }
00166 
00167     this = ns_dyn_mem_alloc(sizeof(thread_resolution_server_t));
00168     if (!this) {
00169         return -2;
00170     }
00171 
00172     this->interface_id = interface_id;
00173     this->coap_service_id = coap_service_initialize(this->interface_id, THREAD_MANAGEMENT_PORT, COAP_SERVICE_OPTIONS_NONE, NULL, NULL);
00174     if (this->coap_service_id < 0) {
00175         tr_warn("Thread resolution init failed");
00176         ns_dyn_mem_free(this);
00177         return -3;
00178     }
00179     this->query_cb_ptr = query_cb;
00180     ns_list_add_to_start(&instance_list, this);
00181 
00182     coap_service_register_uri(this->coap_service_id, THREAD_URI_ADDRESS_QUERY_REQUEST, COAP_SERVICE_ACCESS_POST_ALLOWED, thread_resolution_server_query_cb);
00183 
00184     return 0;
00185 }
00186 
00187 void thread_resolution_server_delete(int8_t interface_id)
00188 {
00189     thread_resolution_server_t *this = thread_resolution_server_find(interface_id);
00190     if (!this) {
00191         return;
00192     }
00193 
00194     coap_service_unregister_uri(this->coap_service_id, THREAD_URI_ADDRESS_QUERY_REQUEST);
00195 
00196     coap_service_delete(this->coap_service_id);
00197     ns_list_remove(&instance_list, this);
00198     ns_dyn_mem_free(this);
00199 }
00200 
00201 #endif //HAVE_THREAD_NEIGHBOR_DISCOVERY
00202