diff --git a/CMakeInputs/adaptation.cmake b/CMakeInputs/adaptation.cmake new file mode 100644 index 0000000..4072fae --- /dev/null +++ b/CMakeInputs/adaptation.cmake @@ -0,0 +1,8 @@ +target_sources(ndn-lite PUBLIC + ${DIR_ADAPTATION}/adapt-consts.h + ${DIR_ADAPTATION}/udp/udp-face.h +) +target_sources(ndn-lite PRIVATE + ${DIR_ADAPTATION}/uniform-time.c + ${DIR_ADAPTATION}/udp/udp-face.c +) diff --git a/CMakeInputs/encode.cmake b/CMakeInputs/encode.cmake index 5739d88..0c4ab39 100644 --- a/CMakeInputs/encode.cmake +++ b/CMakeInputs/encode.cmake @@ -1,4 +1,5 @@ set(DIR_ENCODE "${DIR_NDN_LITE}/encode") +set(DIR_TRUST_SCHEMA "${DIR_ENCODE}/trust-schema") target_sources(ndn-lite PUBLIC ${DIR_ENCODE}/data.h ${DIR_ENCODE}/decoder.h @@ -12,6 +13,12 @@ target_sources(ndn-lite PUBLIC ${DIR_ENCODE}/signature.h ${DIR_ENCODE}/signed-interest.h ${DIR_ENCODE}/tlv.h + ${DIR_ENCODE}/forwarder-helper.h + ${DIR_ENCODE}/ndn-rule-storage.h + ${DIR_TRUST_SCHEMA}/ndn-trust-schema-common.h + ${DIR_TRUST_SCHEMA}/ndn-trust-schema-pattern-component.h + ${DIR_TRUST_SCHEMA}/ndn-trust-schema-pattern.h + ${DIR_TRUST_SCHEMA}/ndn-trust-schema-rule.h ) target_sources(ndn-lite PRIVATE ${DIR_ENCODE}/data.c @@ -22,5 +29,11 @@ target_sources(ndn-lite PRIVATE ${DIR_ENCODE}/name.c ${DIR_ENCODE}/signature.c ${DIR_ENCODE}/signed-interest.c + ${DIR_ENCODE}/forwarder-helper.c + ${DIR_ENCODE}/ndn-rule-storage.c + ${DIR_TRUST_SCHEMA}/ndn-trust-schema-pattern-component.c + ${DIR_TRUST_SCHEMA}/ndn-trust-schema-pattern.c + ${DIR_TRUST_SCHEMA}/ndn-trust-schema-rule.c ) +unset(DIR_TRUST_SCHEMA) unset(DIR_ENCODE) diff --git a/CMakeInputs/examples.cmake b/CMakeInputs/examples.cmake new file mode 100644 index 0000000..edbd238 --- /dev/null +++ b/CMakeInputs/examples.cmake @@ -0,0 +1,20 @@ +set(DIR_EXAMPLES_OUTPUT "${PROJECT_BINARY_DIR}/examples") + +# Single-file examples +set(LIST_EXAMPLES + "udp-basic-producer" + "udp-basic-consumer" +# "udp-group-producer" +# "udp-group-consumer" +# "access-control-producer" +# "access-control-consumer" +# "access-control-controller" +) +foreach(EXAM_NAME IN LISTS LIST_EXAMPLES) + add_executable(${EXAM_NAME} "${DIR_EXAMPLES}/${EXAM_NAME}.c") + target_link_libraries(${EXAM_NAME} ndn-lite) + set_target_properties(${EXAM_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${DIR_EXAMPLES_OUTPUT}) +endforeach() +unset(LIST_EXAMPLES) + +unset(DIR_EXAMPLES_OUTPUT) \ No newline at end of file diff --git a/CMakeInputs/face.cmake b/CMakeInputs/face.cmake index 0bfb381..21013b4 100644 --- a/CMakeInputs/face.cmake +++ b/CMakeInputs/face.cmake @@ -1,10 +1,8 @@ set(DIR_FACE "${DIR_NDN_LITE}/face") target_sources(ndn-lite PUBLIC - ${DIR_FACE}/direct-face.h ${DIR_FACE}/dummy-face.h ) target_sources(ndn-lite PRIVATE - ${DIR_FACE}/direct-face.c ${DIR_FACE}/dummy-face.c ) unset(DIR_FACE) diff --git a/CMakeInputs/forwarder.cmake b/CMakeInputs/forwarder.cmake index 6a62882..dbad601 100644 --- a/CMakeInputs/forwarder.cmake +++ b/CMakeInputs/forwarder.cmake @@ -1,13 +1,18 @@ set(DIR_FORWARDER "${DIR_NDN_LITE}/forwarder") target_sources(ndn-lite PUBLIC + ${DIR_FORWARDER}/callback-funcs.h + ${DIR_FORWARDER}/face-table.h ${DIR_FORWARDER}/face.h ${DIR_FORWARDER}/fib.h ${DIR_FORWARDER}/forwarder.h + ${DIR_FORWARDER}/name-tree.h ${DIR_FORWARDER}/pit.h ) target_sources(ndn-lite PRIVATE - ${DIR_FORWARDER}/face.c + ${DIR_FORWARDER}/face-table.c + ${DIR_FORWARDER}/fib.c ${DIR_FORWARDER}/forwarder.c + ${DIR_FORWARDER}/name-tree.c ${DIR_FORWARDER}/pit.c ) unset(DIR_FORWARDER) diff --git a/CMakeInputs/ndnlite.cmake b/CMakeInputs/ndnlite.cmake index 09124b9..5e7c6f8 100644 --- a/CMakeInputs/ndnlite.cmake +++ b/CMakeInputs/ndnlite.cmake @@ -3,6 +3,7 @@ target_sources(ndn-lite PUBLIC ${DIR_NDN_LITE}/ndn-enums.h ${DIR_NDN_LITE}/ndn-error-code.h ${DIR_NDN_LITE}/ndn-services.h + ${PROJECT_SOURCE_DIR}/ndn-lite.h ) include(${DIR_CMAKEFILES}/app-support.cmake) include(${DIR_CMAKEFILES}/encode.cmake) diff --git a/CMakeInputs/security.cmake b/CMakeInputs/security.cmake index b6e48c5..bb2ad86 100644 --- a/CMakeInputs/security.cmake +++ b/CMakeInputs/security.cmake @@ -11,6 +11,7 @@ target_sources(ndn-lite PUBLIC ${DIR_SECURITY}/ndn-lite-sec-config.h ${DIR_SECURITY}/ndn-lite-sec-utils.h ${DIR_SECURITY}/ndn-lite-sha.h + ${DIR_SECURITY}/ndn-trust-schema.h ) target_sources(ndn-lite PRIVATE ${DIR_SECURITY}/ndn-lite-aes.c @@ -20,6 +21,7 @@ target_sources(ndn-lite PRIVATE ${DIR_SECURITY}/ndn-lite-sec-config.c ${DIR_SECURITY}/ndn-lite-sec-utils.c ${DIR_SECURITY}/ndn-lite-sha.c + ${DIR_SECURITY}/ndn-trust-schema.c ${DIR_DEFAULT_BACKEND}/ndn-lite-default-aes-impl.h ${DIR_DEFAULT_BACKEND}/ndn-lite-default-aes-impl.c ${DIR_DEFAULT_BACKEND}/ndn-lite-default-ecc-impl.h diff --git a/CMakeInputs/unittest.cmake b/CMakeInputs/unittest.cmake index b1e56ba..2cb86e8 100644 --- a/CMakeInputs/unittest.cmake +++ b/CMakeInputs/unittest.cmake @@ -68,9 +68,9 @@ target_sources(unittest PRIVATE "${DIR_UNITTESTS}/sign-verify/ecdsa-sign-verify-tests/test-secp256k1-def.c" ) -#target_sources(unittest PRIVATE -# "${DIR_UNITTESTS}/schematized-trust/trust-schema-tests.h" -# "${DIR_UNITTESTS}/schematized-trust/trust-schema-tests.c" -# "${DIR_UNITTESTS}/schematized-trust/trust-schema-tests-def.h" -# "${DIR_UNITTESTS}/schematized-trust/trust-schema-tests-def.c" -#) +target_sources(unittest PRIVATE + "${DIR_UNITTESTS}/schematized-trust/trust-schema-tests.h" + "${DIR_UNITTESTS}/schematized-trust/trust-schema-tests.c" + "${DIR_UNITTESTS}/schematized-trust/trust-schema-tests-def.h" + "${DIR_UNITTESTS}/schematized-trust/trust-schema-tests-def.c" +) diff --git a/CMakeInputs/util.cmake b/CMakeInputs/util.cmake index b13c846..3f17595 100644 --- a/CMakeInputs/util.cmake +++ b/CMakeInputs/util.cmake @@ -2,9 +2,13 @@ set(DIR_UTIL "${DIR_NDN_LITE}/util") target_sources(ndn-lite PUBLIC ${DIR_UTIL}/memory-pool.h ${DIR_UTIL}/msg-queue.h + ${DIR_UTIL}/uniform-time.h + ${DIR_UTIL}/bit-operations.h + ${DIR_UTIL}/re.h ) target_sources(ndn-lite PRIVATE ${DIR_UTIL}/memory-pool.c ${DIR_UTIL}/msg-queue.c + ${DIR_UTIL}/re.c ) unset(DIR_UTIL) diff --git a/CMakeLists.txt b/CMakeLists.txt index 49cb37a..67af0c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,23 +2,32 @@ cmake_minimum_required (VERSION 3.11) project(ndn-lite) +add_definitions(-D_GNU_SOURCE) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -Wall -O0 -ggdb") set(CMAKE_C_FLAGS_DEBUG "-O0 -g") set(CMAKE_C_FLAGS_RELEASE "-O3") set(DIR_CMAKEFILES "${PROJECT_SOURCE_DIR}/CMakeInputs") set(DIR_NDN_LITE "${PROJECT_SOURCE_DIR}/ndn-lite") +set(DIR_ADAPTATION "${PROJECT_SOURCE_DIR}/adaptation") +set(DIR_EXAMPLES "${PROJECT_SOURCE_DIR}/examples") include_directories(${PROJECT_SOURCE_DIR}) # NDN-Lite static library add_library(ndn-lite STATIC) include(${DIR_CMAKEFILES}/ndnlite.cmake) +# Adaptation +include(${DIR_CMAKEFILES}/adaptation.cmake) + # Unit test program add_executable(unittest) target_link_libraries(unittest ndn-lite) include(${DIR_CMAKEFILES}/unittest.cmake) +# Examples +include(${DIR_CMAKEFILES}/examples.cmake) + # Doxygen # first we can indicate the documentation build as an option and set it to ON by default option(BUILD_DOCS "Build documentation" OFF) diff --git a/adaptation/udp-unicast/ndn-udp-unicast-face.h b/adaptation/adapt-consts.h similarity index 60% rename from adaptation/udp-unicast/ndn-udp-unicast-face.h rename to adaptation/adapt-consts.h index 3e35b89..cbb4fa0 100644 --- a/adaptation/udp-unicast/ndn-udp-unicast-face.h +++ b/adaptation/adapt-consts.h @@ -5,18 +5,9 @@ * General Public License v3.0. See the file LICENSE in the top level * directory for more details. */ +#ifndef NDN_ADAPT_CONSTS_H +#define NDN_ADAPT_CONSTS_H -#ifndef NDN_UDP_UNICAST_FACE_H_ -#define NDN_UDP_UNICAST_FACE_H_ +#define NDN_UDP_FACE_SOCKET_ERROR 1 -#ifdef __cplusplus -extern "C" { -#endif - - - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file +#endif // NDN_ADAPT_CONSTS_H diff --git a/adaptation/udp/udp-face.c b/adaptation/udp/udp-face.c new file mode 100644 index 0000000..6ffd947 --- /dev/null +++ b/adaptation/udp/udp-face.c @@ -0,0 +1,218 @@ +/* + * Copyright (C) 2019 Xinyu Ma + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v3.0. See the file LICENSE in the top level + * directory for more details. + */ + +#include +#include +#include +#include +#include +#include "udp-face.h" +#include "ndn-lite/ndn-error-code.h" +#include "ndn-lite/ndn-constants.h" + +static int +ndn_udp_face_up(struct ndn_face_intf* self); + +static int +ndn_udp_face_down(struct ndn_face_intf* self); + +static void +ndn_udp_face_destroy(ndn_face_intf_t* self); + +static int +ndn_udp_face_send(ndn_face_intf_t* self, const uint8_t* packet, uint32_t size); + +static ndn_udp_face_t* +ndn_udp_face_construct( + in_addr_t local_addr, + in_port_t local_port, + in_addr_t remote_addr, + in_port_t remote_port, + bool multicast); + +static void +ndn_udp_face_recv(void *self, size_t param_len, void *param); + +/////////////////////////// /////////////////////////// /////////////////////////// + +static int +ndn_udp_face_up(struct ndn_face_intf* self){ + ndn_udp_face_t* ptr = container_of(self, ndn_udp_face_t, intf); + int iyes = 1; + u_char ttl = 5; + struct ip_mreq mreq; + + if(self->state == NDN_FACE_STATE_UP){ + return NDN_SUCCESS; + } + ptr->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if(ptr->sock == -1){ + return NDN_UDP_FACE_SOCKET_ERROR; + } + setsockopt(ptr->sock, SOL_SOCKET, SO_REUSEADDR, &iyes, sizeof(int)); + if(ioctl(ptr->sock, FIONBIO, (char *)&iyes) == -1){ + ndn_face_down(self); + return NDN_UDP_FACE_SOCKET_ERROR; + } + + if(bind(ptr->sock, (struct sockaddr*)&ptr->local_addr, sizeof(ptr->local_addr)) == -1){ + ndn_face_down(self); + return NDN_UDP_FACE_SOCKET_ERROR; + } + + if(ptr->multicast){ + setsockopt(ptr->sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); + + mreq.imr_interface = ptr->local_addr.sin_addr; + mreq.imr_multiaddr = ptr->remote_addr.sin_addr; + if(setsockopt(ptr->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1){ + ndn_face_down(self); + return NDN_UDP_FACE_SOCKET_ERROR; + } + } + + ptr->process_event = ndn_msgqueue_post(ptr, ndn_udp_face_recv, 0, NULL); + if(ptr->process_event == NULL){ + ndn_face_down(self); + return NDN_FWD_MSGQUEUE_FULL; + } + + self->state = NDN_FACE_STATE_UP; + return NDN_SUCCESS; +} + +static int +ndn_udp_face_down(struct ndn_face_intf* self){ + ndn_udp_face_t* ptr = (ndn_udp_face_t*)self; + self->state = NDN_FACE_STATE_DOWN; + + if(ptr->sock != -1){ + close(ptr->sock); + ptr->sock = -1; + } + + if(ptr->process_event != NULL){ + ndn_msgqueue_cancel(ptr->process_event); + ptr->process_event = NULL; + } + + return NDN_SUCCESS; +} + +static void +ndn_udp_face_destroy(ndn_face_intf_t* self){ + ndn_face_down(self); + ndn_forwarder_unregister_face(self); + free(self); +} + +static int +ndn_udp_face_send(ndn_face_intf_t* self, const uint8_t* packet, uint32_t size){ + ndn_udp_face_t* ptr = (ndn_udp_face_t*)self; + ssize_t ret; + ret = sendto(ptr->sock, packet, size, 0, + (struct sockaddr*)&ptr->remote_addr, sizeof(ptr->remote_addr)); + if(ret != size){ + return NDN_UDP_FACE_SOCKET_ERROR; + }else{ + return NDN_SUCCESS; + } +} + +static ndn_udp_face_t* +ndn_udp_face_construct( + in_addr_t local_addr, + in_port_t local_port, + in_addr_t remote_addr, + in_port_t remote_port, + bool multicast) +{ + ndn_udp_face_t* ret; + int iret; + + ret = (ndn_udp_face_t*)malloc(sizeof(ndn_udp_face_t)); + if(!ret){ + return NULL; + } + + ret->intf.face_id = NDN_INVALID_ID; + iret = ndn_forwarder_register_face(&ret->intf); + if(iret != NDN_SUCCESS){ + free(ret); + return NULL; + } + + ret->intf.type = NDN_FACE_TYPE_NET; + ret->intf.state = NDN_FACE_STATE_DOWN; + ret->intf.up = ndn_udp_face_up; + ret->intf.down = ndn_udp_face_down; + ret->intf.send = ndn_udp_face_send; + ret->intf.destroy = ndn_udp_face_destroy; + + ret->local_addr.sin_family = AF_INET; + ret->local_addr.sin_port = local_port; + ret->local_addr.sin_addr.s_addr = local_addr; + memset(ret->local_addr.sin_zero, 0, sizeof(ret->local_addr.sin_zero)); + + ret->remote_addr.sin_family = AF_INET; + ret->remote_addr.sin_port = remote_port; + ret->remote_addr.sin_addr.s_addr = remote_addr; + memset(ret->remote_addr.sin_zero, 0, sizeof(ret->remote_addr.sin_zero)); + + ret->sock = -1; + ret->multicast = multicast; + ret->process_event = NULL; + ndn_face_up(&ret->intf); + + return ret; +} + +ndn_udp_face_t* +ndn_udp_unicast_face_construct( + in_addr_t local_addr, + in_port_t local_port, + in_addr_t remote_addr, + in_port_t remote_port) +{ + return ndn_udp_face_construct(local_addr, local_port, remote_addr, remote_port, false); +} + +ndn_udp_face_t* +ndn_udp_multicast_face_construct( + in_addr_t local_addr, + in_addr_t group_addr, + in_port_t port) +{ + return ndn_udp_face_construct(local_addr, port, group_addr, port, true); +} + +static void +ndn_udp_face_recv(void *self, size_t param_len, void *param){ + struct sockaddr_in client_addr; + socklen_t addr_len; + ssize_t size; + int ret; + ndn_udp_face_t* ptr = (ndn_udp_face_t*)self; + + while(true){ + size = recvfrom(ptr->sock, ptr->buf, sizeof(ptr->buf), MSG_DONTWAIT, + (struct sockaddr*)&client_addr, &addr_len); + if(size >= 0){ + // A packet recved + ret = ndn_forwarder_receive(&ptr->intf, ptr->buf, size); + }else if(size == -1 && errno == EWOULDBLOCK){ + // No more packet + break; + }else{ + ndn_face_down(&ptr->intf); + return; + } + } + + ptr->process_event = ndn_msgqueue_post(self, ndn_udp_face_recv, param_len, param); +} \ No newline at end of file diff --git a/adaptation/udp/udp-face.h b/adaptation/udp/udp-face.h new file mode 100644 index 0000000..31e6f95 --- /dev/null +++ b/adaptation/udp/udp-face.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2019 Xinyu Ma + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v3.0. See the file LICENSE in the top level + * directory for more details. + */ + +#ifndef NDN_UDP_FACE_H_ +#define NDN_UDP_FACE_H_ + +#include +#include "ndn-lite/forwarder/forwarder.h" +#include "ndn-lite/util/msg-queue.h" +#include "../adapt-consts.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// This face is different because we can create multiple faces safely + +// Generally MTU < 2048 +// Given that we don't cache +#define NDN_UDP_BUFFER_SIZE 4096 + +/** + * Udp face + */ +typedef struct ndn_udp_face { + /** + * The inherited interface. + */ + ndn_face_intf_t intf; + + struct sockaddr_in local_addr; + struct sockaddr_in remote_addr; + struct ndn_msg* process_event; + int sock; + bool multicast; + uint8_t buf[NDN_UDP_BUFFER_SIZE]; +} ndn_udp_face_t; + +ndn_udp_face_t* +ndn_udp_unicast_face_construct( + in_addr_t local_addr, + in_port_t local_port, + in_addr_t remote_addr, + in_port_t remote_port); + +ndn_udp_face_t* +ndn_udp_multicast_face_construct( + in_addr_t local_addr, + in_addr_t group_addr, + in_port_t port); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/adaptation/uniform-time.c b/adaptation/uniform-time.c new file mode 100644 index 0000000..b00cbbc --- /dev/null +++ b/adaptation/uniform-time.c @@ -0,0 +1,23 @@ +#include +#include +#include + +ndn_time_ms_t ndn_time_now_ms(void){ + struct timespec time; + clock_gettime(CLOCK_REALTIME, &time); + return (uint64_t)time.tv_sec * 1000 + (uint64_t)time.tv_nsec / 1000000; +} + +ndn_time_us_t ndn_time_now_us(void){ + struct timespec time; + clock_gettime(CLOCK_REALTIME, &time); + return (uint64_t)time.tv_sec * 1000000 + (uint64_t)time.tv_nsec / 1000; +} + +void ndn_time_delay(ndn_time_ms_t delay){ + if(delay < 1000000){ + usleep(delay * 1000); + }else{ + sleep(delay / 1000); + } +} diff --git a/examples/access-control-consumer.c b/examples/access-control-consumer.c new file mode 100644 index 0000000..9b488a2 --- /dev/null +++ b/examples/access-control-consumer.c @@ -0,0 +1,191 @@ +/* + * Copyright (C) 2019 Zhiyi Zhang + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v3.0. See the file LICENSE in the top level + * directory for more details. + */ + +#include "ndn-lite/app-support/access-control.h" +#include "ndn-lite/encode/signed-interest.h" +#include "ndn-lite/encode/key-storage.h" +#include "ndn-lite/forwarder/forwarder.h" +#include "ndn-lite/face/direct-face.h" +#include "adaptation/udp-multicast/ndn-udp-multicast-face.h" +#include +#include +#include +#include + +const uint8_t prv[] = { + 0x5D, 0xC7, 0x6B, 0xAB, 0xEE, 0xD4, 0xEB, 0xB7, 0xBA, 0xFC, + 0x64, 0xE7, 0x8B, 0xDB, 0x22, 0xE1, 0xF4, 0x37, 0x10, 0xC2, + 0xEA, 0xE9, 0xDD, 0xAF, 0xF4, 0x74, 0xB3, 0x18, 0x08, 0x56, + 0x5E, 0x4C +}; + +const uint8_t pub[] = { + 0x36, 0xF7, 0xEF, 0x7C, 0x05, 0x10, 0x68, 0xC4, 0x6C, 0x67, + 0x63, 0x2A, 0xF5, 0x82, 0x1D, 0x14, 0xBA, 0xCC, 0x50, 0x12, + 0x73, 0x73, 0xED, 0xDE, 0x7D, 0x23, 0x5D, 0x20, 0xA8, 0x5E, + 0xD1, 0x83, 0x3C, 0x0F, 0xB7, 0xD2, 0x6E, 0xB2, 0x0F, 0x8B, + 0x09, 0x1D, 0xD0, 0xF3, 0xB9, 0xAA, 0x56, 0x11, 0x1D, 0x15, + 0x0C, 0xAC, 0xE4, 0xFA, 0x9F, 0x6C, 0x61, 0xB4, 0xFF, 0x41, + 0xE8, 0xBA, 0x21, 0x89 +}; + +ndn_ecc_pub_t* pub_key = NULL; +ndn_ecc_prv_t* prv_key = NULL; +in_addr_t multicast_ip; + +int +parseArgs(int argc, char *argv[]) { + char *sz_addr; + struct hostent *host_addr; + struct in_addr **paddrs; + + if (argc < 1) { + char defaultaddr[] = "225.0.0.37"; + sz_addr = defaultaddr; + return 1; + } + else + sz_addr = argv[1]; + + if (strlen(sz_addr) <= 0) { + fprintf(stderr, "ERROR: wrong arguments.\n"); + return 1; + } + + host_addr = gethostbyname(sz_addr); + if(host_addr == NULL){ + fprintf(stderr, "ERROR: wrong hostname.\n"); + return 2; + } + paddrs = (struct in_addr **)host_addr->h_addr_list; + if(paddrs[0] == NULL){ + fprintf(stderr, "ERROR: wrong hostname.\n"); + return 2; + } + multicast_ip = paddrs[0]->s_addr; + return 0; +} + +void +print_error(const char *test_name, const char *fnct_name, const char *funct_failed, int err_code) { + printf("In %s test, within call to %s, call to %s failed, error code: %d\n", + test_name, fnct_name, funct_failed, err_code); +} + +int +on_data(const uint8_t* data, uint32_t data_size) +{ + printf("Get DK Data\n"); + + // parse response Data + ndn_data_t response; + int ret_val = ndn_data_tlv_decode_ecdsa_verify(&response, data, data_size, pub_key); + if (ret_val != 0) { + print_error("producer", "on_data", "ndn_data_tlv_decode_ecdsa_verify", ret_val); + } + + // update ac state + ret_val = ndn_ac_on_ek_response_process(&response); + if (ret_val != 0) { + print_error("producer", "on_data", "ndn_ac_on_ek_response", ret_val); + } + + return 0; +} + +int +main(int argc, char *argv[]) +{ + int ret_val = -1; + if ((ret_val = parseArgs(argc, argv)) != 0) { + return ret_val; + } + + // init security + ndn_security_init(); + + // set home prefix + ndn_name_t home_prefix; + char* home_prefix_str = "/ndn"; + ret_val = ndn_name_from_string(&home_prefix, home_prefix_str, sizeof(home_prefix_str)); + if (ret_val != 0) { + print_error("consumer", "set home prefix", "ndn_name_from_string", ret_val); + return -1; + } + + // set identity name + char comp_consumer[] = "consumer"; + name_component_t component_consumer; + ret_val = name_component_from_string(&component_consumer, comp_consumer, sizeof(comp_consumer)); + if (ret_val != 0) { + print_error("consumer", "set identity name", "name_component_from_string", ret_val); + return -1; + } + ndn_name_t consumer_identity = home_prefix; + ret_val = ndn_name_append_component(&consumer_identity, &component_consumer); + if (ret_val != 0) { + print_error("consumer", "set identity name", "ndn_name_append_component", ret_val); + return -1; + } + + // init keys + ndn_key_storage_init(); + ndn_key_storage_get_empty_ecc_key(&pub_key, &prv_key); + ret_val = ndn_ecc_prv_init(prv_key, prv, sizeof(prv), + NDN_ECDSA_CURVE_SECP256R1, 123); + if (ret_val != 0) { + print_error("consumer", "init keys", "ndn_ecc_prv_init", ret_val); + return -1; + } + ret_val = ndn_ecc_pub_init(pub_key, pub, sizeof(pub), + NDN_ECDSA_CURVE_SECP256R1, 123); + if (ret_val != 0) { + print_error("consumer", "init keys", "ndn_ecc_pub_init", ret_val); + return -1; + } + + // init ac state + ndn_ac_state_init(&consumer_identity, pub_key, prv_key); + + // prepare DK Interest + uint8_t buffer[1024]; + ndn_encoder_t interest_encoder; + encoder_init(&interest_encoder, buffer, sizeof(buffer)); + ret_val = ndn_ac_prepare_key_request_interest(&interest_encoder, &home_prefix, + &component_consumer, 100, prv_key, 0); + if (ret_val != 0) { + print_error("consumer", "prepare DK Interest", "ndn_ac_prepare_key_request", ret_val); + return -1; + } + + // set up direct face and forwarder + ndn_forwarder_init(); + ndn_direct_face_construct(666); + + // add routes + ndn_udp_multicast_face_t* udp_face; + udp_face = ndn_udp_muticast_face_construct(667, INADDR_ANY, 6363, multicast_ip); + + char controller_prefix_string[] = "/ndn/AC"; + ndn_name_t controller_prefix; + ret_val = ndn_name_from_string(&controller_prefix, controller_prefix_string, sizeof(controller_prefix_string)); + if (ret_val != 0) { + print_error("consumer", "add routes", "ndn_name_from_string", ret_val); + } + ndn_forwarder_fib_insert(&controller_prefix, udp_face, 0); + + char producer_prefix_string[] = "/ndn/producer"; + ndn_name_t producer_prefix; + ret_val = ndn_name_from_string(&producer_prefix, producer_prefix_string, sizeof(producer_prefix_string)); + if (ret_val != 0) { + print_error("consumer", "add routes", "ndn_name_from_string", ret_val); + } + ndn_forwarder_fib_insert(&producer_prefix, udp_face, 0); + + return 0; +} diff --git a/examples/access-control-controller.c b/examples/access-control-controller.c new file mode 100644 index 0000000..f9e2fe2 --- /dev/null +++ b/examples/access-control-controller.c @@ -0,0 +1,191 @@ +/* + * Copyright (C) 2019 Zhiyi Zhang + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v3.0. See the file LICENSE in the top level + * directory for more details. + */ + +#include "ndn-lite/app-support/access-control.h" +#include "ndn-lite/encode/signed-interest.h" +#include "ndn-lite/encode/key-storage.h" +#include "ndn-lite/forwarder/forwarder.h" +#include "ndn-lite/face/direct-face.h" +#include "adaptation/udp-multicast/ndn-udp-multicast-face.h" +#include +#include +#include +#include + +const uint8_t prv[] = { + 0x5D, 0xC7, 0x6B, 0xAB, 0xEE, 0xD4, 0xEB, 0xB7, 0xBA, 0xFC, + 0x64, 0xE7, 0x8B, 0xDB, 0x22, 0xE1, 0xF4, 0x37, 0x10, 0xC2, + 0xEA, 0xE9, 0xDD, 0xAF, 0xF4, 0x74, 0xB3, 0x18, 0x08, 0x56, + 0x5E, 0x4C +}; + +const uint8_t pub[] = { + 0x36, 0xF7, 0xEF, 0x7C, 0x05, 0x10, 0x68, 0xC4, 0x6C, 0x67, + 0x63, 0x2A, 0xF5, 0x82, 0x1D, 0x14, 0xBA, 0xCC, 0x50, 0x12, + 0x73, 0x73, 0xED, 0xDE, 0x7D, 0x23, 0x5D, 0x20, 0xA8, 0x5E, + 0xD1, 0x83, 0x3C, 0x0F, 0xB7, 0xD2, 0x6E, 0xB2, 0x0F, 0x8B, + 0x09, 0x1D, 0xD0, 0xF3, 0xB9, 0xAA, 0x56, 0x11, 0x1D, 0x15, + 0x0C, 0xAC, 0xE4, 0xFA, 0x9F, 0x6C, 0x61, 0xB4, 0xFF, 0x41, + 0xE8, 0xBA, 0x21, 0x89 +}; + +ndn_ecc_pub_t* pub_key = NULL; +ndn_ecc_prv_t* prv_key = NULL; +in_addr_t multicast_ip; + +ndn_name_t controller_identity; +ndn_interest_t ek_interest; + +int +parseArgs(int argc, char *argv[]) { + char *sz_addr; + struct hostent *host_addr; + struct in_addr **paddrs; + + if (argc < 1) { + char defaultaddr[] = "225.0.0.37"; + sz_addr = defaultaddr; + return 1; + } + else + sz_addr = argv[1]; + + if (strlen(sz_addr) <= 0) { + fprintf(stderr, "ERROR: wrong arguments.\n"); + return 1; + } + + host_addr = gethostbyname(sz_addr); + if(host_addr == NULL){ + fprintf(stderr, "ERROR: wrong hostname.\n"); + return 2; + } + paddrs = (struct in_addr **)host_addr->h_addr_list; + if(paddrs[0] == NULL){ + fprintf(stderr, "ERROR: wrong hostname.\n"); + return 2; + } + multicast_ip = paddrs[0]->s_addr; + return 0; +} + +void +print_error(const char *test_name, const char *fnct_name, const char *funct_failed, int err_code) { + printf("In %s test, within call to %s, call to %s failed, error code: %d\n", + test_name, fnct_name, funct_failed, err_code); +} + +int +on_interest(const uint8_t* interest, uint32_t interest_size) +{ + // parse incoming Interest + printf("Get EK/DK Interest\n"); + int ret_val = ndn_interest_from_block(&ek_interest, interest, interest_size); + if (ret_val != 0) { + print_error("controller", "on_EKInterest", "ndn_interest_from_block", ret_val); + } + ret_val = ndn_signed_interest_ecdsa_verify(&ek_interest, pub_key); + if (ret_val != 0) { + print_error("controller", "on_EKInterest", "ndn_signed_interest_ecdsa_verify", ret_val); + } + + // react on the Interest + ndn_data_t response; + ret_val = ndn_ac_on_interest_process(&response, &ek_interest); + if (ret_val != 0) { + print_error("controller", "on_EKInterest", "ndn_ac_on_interest_process", ret_val); + } + + // reply the Data packet + ndn_encoder_t encoder; + ret_val = ndn_data_tlv_encode_ecdsa_sign(&encoder, &response, &controller_identity, + prv_key); + + return 0; +} + +int +main(int argc, char *argv[]) +{ + int ret_val = -1; + if ((ret_val = parseArgs(argc, argv)) != 0) { + return ret_val; + } + + // init security + ndn_security_init(); + + // set home prefix + ndn_name_t home_prefix; + char* home_prefix_str = "/ndn"; + ret_val = ndn_name_from_string(&home_prefix, home_prefix_str, sizeof(home_prefix_str)); + if (ret_val != 0) { + print_error("controller", "set home prefix", "ndn_name_from_string", ret_val); + return -1; + } + + // set identity name + char comp_controller[] = "controller"; + name_component_t component_controller; + ret_val = name_component_from_string(&component_controller, comp_controller, sizeof(comp_controller)); + if (ret_val != 0) { + print_error("controller", "set identity name", "name_component_from_string", ret_val); + return -1; + } + controller_identity = home_prefix; + ret_val = ndn_name_append_component(&controller_identity, &component_controller); + if (ret_val != 0) { + print_error("controller", "set identity name", "ndn_name_append_component", ret_val); + return -1; + } + + // generate controller keys + ndn_key_storage_init(); + ndn_key_storage_get_empty_ecc_key(&pub_key, &prv_key); + ret_val = ndn_ecc_prv_init(prv_key, prv, sizeof(prv), + NDN_ECDSA_CURVE_SECP256R1, 123); + if (ret_val != 0) { + print_error("controller", "init keys", "ndn_ecc_prv_init", ret_val); + return -1; + } + ret_val = ndn_ecc_pub_init(pub_key, pub, sizeof(pub), + NDN_ECDSA_CURVE_SECP256R1, 123); + if (ret_val != 0) { + print_error("controller", "init keys", "ndn_ecc_pub_init", ret_val); + return -1; + } + + // init ac state + ndn_ac_state_init(&controller_identity, pub_key, prv_key); + + // set up direct face and forwarder + ndn_forwarder_init(); + ndn_direct_face_construct(666); + ndn_udp_muticast_face_t* udp_face; + udp_face = ndn_udp_muticast_face_construct(667, INADDR_ANY, 6363, multicast_ip); + + // register prefix + char prefix_string[] = "/ndn/AC"; + ndn_name_t prefix; + ret_val = ndn_name_from_string(&prefix, prefix_string, sizeof(prefix_string)); + if (ret_val != 0) { + print_error("controller", "register prefix", "ndn_name_from_string", ret_val); + } + ret_val = ndn_direct_face_register_prefix(&prefix, on_interest); + if (ret_val != 0) { + print_error("controller", "register prefix", "ndn_direct_face_register_prefix", ret_val); + } + + while (running) { + ndn_udp_unicast_face_recv(consumer_udp_face); + ndn_udp_unicast_face_recv(producer_udp_face); + usleep(10); + } + + return 0; +} diff --git a/examples/access-control-producer.c b/examples/access-control-producer.c new file mode 100644 index 0000000..64b9fa1 --- /dev/null +++ b/examples/access-control-producer.c @@ -0,0 +1,186 @@ +/* + * Copyright (C) 2019 Zhiyi Zhang + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v3.0. See the file LICENSE in the top level + * directory for more details. + */ + +#include "ndn-lite/app-support/access-control.h" +#include "ndn-lite/encode/signed-interest.h" +#include "ndn-lite/encode/key-storage.h" +#include "ndn-lite/forwarder/forwarder.h" +#include "ndn-lite/face/direct-face.h" +#include "adaptation/udp-multicast/ndn-udp-multicast-face.h" +#include +#include +#include +#include + +const uint8_t prv[] = { + 0x5D, 0xC7, 0x6B, 0xAB, 0xEE, 0xD4, 0xEB, 0xB7, 0xBA, 0xFC, + 0x64, 0xE7, 0x8B, 0xDB, 0x22, 0xE1, 0xF4, 0x37, 0x10, 0xC2, + 0xEA, 0xE9, 0xDD, 0xAF, 0xF4, 0x74, 0xB3, 0x18, 0x08, 0x56, + 0x5E, 0x4C +}; + +const uint8_t pub[] = { + 0x36, 0xF7, 0xEF, 0x7C, 0x05, 0x10, 0x68, 0xC4, 0x6C, 0x67, + 0x63, 0x2A, 0xF5, 0x82, 0x1D, 0x14, 0xBA, 0xCC, 0x50, 0x12, + 0x73, 0x73, 0xED, 0xDE, 0x7D, 0x23, 0x5D, 0x20, 0xA8, 0x5E, + 0xD1, 0x83, 0x3C, 0x0F, 0xB7, 0xD2, 0x6E, 0xB2, 0x0F, 0x8B, + 0x09, 0x1D, 0xD0, 0xF3, 0xB9, 0xAA, 0x56, 0x11, 0x1D, 0x15, + 0x0C, 0xAC, 0xE4, 0xFA, 0x9F, 0x6C, 0x61, 0xB4, 0xFF, 0x41, + 0xE8, 0xBA, 0x21, 0x89 +}; + +ndn_ecc_pub_t* pub_key = NULL; +ndn_ecc_prv_t* prv_key = NULL; +in_addr_t multicast_ip; + +int +parseArgs(int argc, char *argv[]) { + char *sz_addr; + struct hostent *host_addr; + struct in_addr **paddrs; + + if (argc < 1) { + char defaultaddr[] = "225.0.0.37"; + sz_addr = defaultaddr; + return 1; + } + else + sz_addr = argv[1]; + + if (strlen(sz_addr) <= 0) { + fprintf(stderr, "ERROR: wrong arguments.\n"); + return 1; + } + + host_addr = gethostbyname(sz_addr); + if(host_addr == NULL){ + fprintf(stderr, "ERROR: wrong hostname.\n"); + return 2; + } + paddrs = (struct in_addr **)host_addr->h_addr_list; + if(paddrs[0] == NULL){ + fprintf(stderr, "ERROR: wrong hostname.\n"); + return 2; + } + multicast_ip = paddrs[0]->s_addr; + return 0; +} + +void +print_error(const char *test_name, const char *fnct_name, const char *funct_failed, int err_code) { + printf("In %s test, within call to %s, call to %s failed, error code: %d\n", + test_name, fnct_name, funct_failed, err_code); +} + +int +on_data(const uint8_t* data, uint32_t data_size) +{ + printf("Get EK Data\n"); + + // parse response Data + ndn_data_t response; + int ret_val = ndn_data_tlv_decode_ecdsa_verify(&response, data, data_size, pub_key); + if (ret_val != 0) { + print_error("producer", "on_data", "ndn_data_tlv_decode_ecdsa_verify", ret_val); + } + + // update ac state + ret_val = ndn_ac_on_ek_response_process(&response); + if (ret_val != 0) { + print_error("producer", "on_data", "ndn_ac_on_ek_response", ret_val); + } + + return 0; +} + +int +main(int argc, char *argv[]) +{ + int ret_val = -1; + if ((ret_val = parseArgs(argc, argv)) != 0) { + return ret_val; + } + + // init security + ndn_security_init(); + + // set home prefix + ndn_name_t home_prefix; + char* home_prefix_str = "/ndn"; + ret_val = ndn_name_from_string(&home_prefix, home_prefix_str, sizeof(home_prefix_str)); + if (ret_val != 0) { + print_error("producer", "set home prefix", "ndn_name_from_string", ret_val); + return -1; + } + + // set identity name + char comp_producer[] = "producer"; + name_component_t component_producer; + ret_val = name_component_from_string(&component_producer, comp_producer, sizeof(comp_producer)); + if (ret_val != 0) { + print_error("producer", "set identity name", "name_component_from_string", ret_val); + return -1; + } + ndn_name_t producer_identity = home_prefix; + ret_val = ndn_name_append_component(&producer_identity, &component_producer); + if (ret_val != 0) { + print_error("producer", "set identity name", "ndn_name_append_component", ret_val); + return -1; + } + + // init keys + ndn_key_storage_init(); + ndn_key_storage_get_empty_ecc_key(&pub_key, &prv_key); + ret_val = ndn_ecc_prv_init(prv_key, prv, sizeof(prv), + NDN_ECDSA_CURVE_SECP256R1, 123); + if (ret_val != 0) { + print_error("producer", "init keys", "ndn_ecc_prv_init", ret_val); + return -1; + } + ret_val = ndn_ecc_pub_init(pub_key, pub, sizeof(pub), + NDN_ECDSA_CURVE_SECP256R1, 123); + if (ret_val != 0) { + print_error("producer", "init keys", "ndn_ecc_pub_init", ret_val); + return -1; + } + + // init ac state + ndn_ac_state_init(&producer_identity, pub_key, prv_key); + + // prepare EK Interest + uint8_t buffer[1024]; + ndn_encoder_t interest_encoder; + encoder_init(&interest_encoder, buffer, sizeof(buffer)); + ret_val = ndn_ac_prepare_key_request_interest(&interest_encoder, &home_prefix, &component_producer, + 100, prv_key, 1); + if (ret_val != 0) { + print_error("producer", "prepare EK Interest", "ndn_ac_prepare_key_request", ret_val); + } + + // set up direct face and forwarder + ndn_forwarder_init(); + ndn_direct_face_construct(666); + ndn_udp_muticast_face_t* udp_face; + udp_face = ndn_udp_muticast_face_construct(667, INADDR_ANY, 6363, multicast_ip); + + // add route + char prefix_string[] = "/ndn/AC"; + ndn_name_t controller_prefix; + ret_val = ndn_name_from_string(&controller_prefix, prefix_string, sizeof(prefix_string)); + if (ret_val != 0) { + print_error("consumer", "add route", "ndn_name_from_string", ret_val); + } + ndn_forwarder_fib_insert(&controller_prefix, udp_face, 0); + + while (running) { + ndn_udp_multicast_face_recv(consumer_udp_face); + usleep(10); + } + + return 0; +} diff --git a/examples/udp-basic-consumer.c b/examples/udp-basic-consumer.c new file mode 100644 index 0000000..db580c4 --- /dev/null +++ b/examples/udp-basic-consumer.c @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2019 Xinyu Ma + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v3.0. See the file LICENSE in the top level + * directory for more details. + * + * See AUTHORS.md for complete list of NDN IOT PKG authors and contributors. + */ +#include +#include +#include +#include +#include +#include "ndn-lite/encode/name.h" +#include "ndn-lite/encode/data.h" +#include "ndn-lite/encode/interest.h" + +in_port_t port1, port2; +in_addr_t server_ip; +ndn_name_t name_prefix; +uint8_t buf[4096]; +bool running; + +int parseArgs(int argc, char *argv[]){ + char *sz_port1, *sz_port2, *sz_addr; + uint32_t ul_port; + struct hostent * host_addr; + struct in_addr ** paddrs; + + if(argc < 5){ + fprintf(stderr, "ERROR: wrong arguments.\n"); + printf("Usage: \n"); + return 1; + } + sz_port1 = argv[1]; + sz_addr = argv[2]; + sz_port2 = argv[3]; + //sz_prefix = argv[4]; + //data_need = argv[5]; + + if(strlen(sz_port1) <= 0 || strlen(sz_addr) <= 0 || strlen(sz_port2) <= 0){ + fprintf(stderr, "ERROR: wrong arguments.\n"); + return 1; + } + + host_addr = gethostbyname(sz_addr); + if(host_addr == NULL){ + fprintf(stderr, "ERROR: wrong hostname.\n"); + return 2; + } + + paddrs = (struct in_addr **)host_addr->h_addr_list; + if(paddrs[0] == NULL){ + fprintf(stderr, "ERROR: wrong hostname.\n"); + return 2; + } + server_ip = paddrs[0]->s_addr; + + ul_port = strtoul(sz_port1, NULL, 10); + if(ul_port < 1024 || ul_port >= 65536){ + fprintf(stderr, "ERROR: wrong port number.\n"); + return 3; + } + port1 = htons((uint16_t) ul_port); + + ul_port = strtoul(sz_port2, NULL, 10); + if(ul_port < 1024 || ul_port >= 65536){ + fprintf(stderr, "ERROR: wrong port number.\n"); + return 3; + } + port2 = htons((uint16_t) ul_port); + + if(ndn_name_from_string(&name_prefix, argv[4], strlen(argv[4])) != NDN_SUCCESS){ + fprintf(stderr, "ERROR: wrong name.\n"); + return 4; + } + + return 0; +} + +void on_data(const uint8_t* rawdata, uint32_t data_size, void* userdata){ + ndn_data_t data; + printf("On data\n"); + if(ndn_data_tlv_decode_digest_verify(&data, rawdata, data_size)){ + printf("Decoding failed.\n"); + } + + printf("It says: %s\n", data.content_value); + running = false; +} + +void on_timeout(void* userdata){ + printf("On timeout\n"); + running = false; +} + +int main(int argc, char *argv[]){ + ndn_udp_face_t *face; + ndn_interest_t interest; + ndn_encoder_t encoder; + int ret; + + if((ret = parseArgs(argc, argv)) != 0){ + return ret; + } + + ndn_lite_startup(); + face = ndn_udp_unicast_face_construct(INADDR_ANY, port1, server_ip, port2); + + running = true; + encoder_init(&encoder, buf, 4096); + ndn_name_tlv_encode(&encoder, &name_prefix); + ndn_forwarder_add_route(&face->intf, buf, encoder.offset); + + ndn_interest_from_name(&interest, &name_prefix); + encoder_init(&encoder, buf, 4096); + ndn_interest_tlv_encode(&encoder, &interest); + + ndn_forwarder_express_interest(encoder.output_value, encoder.offset, on_data, on_timeout, NULL); + + while(running){ + ndn_forwarder_process(); + usleep(10000); + } + + ndn_face_destroy(&face->intf); + + return 0; +} diff --git a/examples/udp-basic-producer.c b/examples/udp-basic-producer.c new file mode 100644 index 0000000..6fe05ea --- /dev/null +++ b/examples/udp-basic-producer.c @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2019 Xinyu Ma + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v3.0. See the file LICENSE in the top level + * directory for more details. + * + * See AUTHORS.md for complete list of NDN IOT PKG authors and contributors. + */ +#include +#include +#include +#include +#include +#include "ndn-lite/encode/name.h" +#include "ndn-lite/encode/data.h" +#include "ndn-lite/encode/interest.h" + +in_port_t port1, port2; +in_addr_t server_ip; +ndn_name_t name_prefix; +uint8_t buf[4096]; +ndn_udp_face_t *face; +bool running; + +int parseArgs(int argc, char *argv[]){ + char *sz_port1, *sz_port2, *sz_addr; + uint32_t ul_port; + struct hostent * host_addr; + struct in_addr ** paddrs; + + if(argc < 5){ + fprintf(stderr, "ERROR: wrong arguments.\n"); + printf("Usage: \n"); + return 1; + } + sz_port1 = argv[1]; + sz_addr = argv[2]; + sz_port2 = argv[3]; + //sz_prefix = argv[4]; + //data_need = argv[5]; + + if(strlen(sz_port1) <= 0 || strlen(sz_addr) <= 0 || strlen(sz_port2) <= 0){ + fprintf(stderr, "ERROR: wrong arguments.\n"); + return 1; + } + + host_addr = gethostbyname(sz_addr); + if(host_addr == NULL){ + fprintf(stderr, "ERROR: wrong hostname.\n"); + return 2; + } + + paddrs = (struct in_addr **)host_addr->h_addr_list; + if(paddrs[0] == NULL){ + fprintf(stderr, "ERROR: wrong hostname.\n"); + return 2; + } + server_ip = paddrs[0]->s_addr; + + ul_port = strtoul(sz_port1, NULL, 10); + if(ul_port < 1024 || ul_port >= 65536){ + fprintf(stderr, "ERROR: wrong port number.\n"); + return 3; + } + port1 = htons((uint16_t) ul_port); + + ul_port = strtoul(sz_port2, NULL, 10); + if(ul_port < 1024 || ul_port >= 65536){ + fprintf(stderr, "ERROR: wrong port number.\n"); + return 3; + } + port2 = htons((uint16_t) ul_port); + + if(ndn_name_from_string(&name_prefix, argv[4], strlen(argv[4])) != NDN_SUCCESS){ + fprintf(stderr, "ERROR: wrong name.\n"); + return 4; + } + + return 0; +} + +int on_interest(const uint8_t* interest, uint32_t interest_size, void* userdata){ + ndn_data_t data; + ndn_encoder_t encoder; + char * str = "I'm a Data packet."; + + printf("On interest\n"); + data.name = name_prefix; + ndn_data_set_content(&data, (uint8_t*)str, strlen(str) + 1); + ndn_metainfo_init(&data.metainfo); + ndn_metainfo_set_content_type(&data.metainfo, NDN_CONTENT_TYPE_BLOB); + encoder_init(&encoder, buf, 4096); + ndn_data_tlv_encode_digest_sign(&encoder, &data); + ndn_forwarder_put_data(encoder.output_value, encoder.offset); + + return 0; +} + +int main(int argc, char *argv[]){ + int ret; + ndn_encoder_t encoder; + + if((ret = parseArgs(argc, argv)) != 0){ + return ret; + } + + ndn_lite_startup(); + face = ndn_udp_unicast_face_construct(INADDR_ANY, port1, server_ip, port2); + + running = true; + encoder_init(&encoder, buf, sizeof(buf)); + ndn_name_tlv_encode(&encoder, &name_prefix); + ndn_forwarder_register_prefix(encoder.output_value, encoder.offset, on_interest, NULL); + while(running){ + ndn_forwarder_process(); + usleep(10000); + } + + ndn_face_destroy(&face->intf); + + return 0; +} \ No newline at end of file diff --git a/examples/udp-group-consumer.c b/examples/udp-group-consumer.c new file mode 100644 index 0000000..7efc531 --- /dev/null +++ b/examples/udp-group-consumer.c @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2019 Xinyu Ma + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v3.0. See the file LICENSE in the top level + * directory for more details. + * + * See AUTHORS.md for complete list of NDN IOT PKG authors and contributors. + */ +#include +#include +#include +#include +#include +#include "adaptation/udp-multicast/ndn-udp-multicast-face.h" +#include "ndn-lite/forwarder/forwarder.h" +#include "ndn-lite/face/direct-face.h" +#include "ndn-lite/encode/encoder.h" +#include "ndn-lite/encode/data.h" + +in_port_t port; +in_addr_t server_ip; +ndn_name_t name_prefix; +uint8_t buf[4096]; +bool running; + +int parseArgs(int argc, char *argv[]){ + char *sz_port, *sz_addr; + uint32_t ul_port; + + if(argc < 4){ + fprintf(stderr, "ERROR: wrong arguments.\n"); + printf("Usage: \n"); + return 1; + } + sz_port = argv[1]; + sz_addr = argv[2]; + //sz_prefix = argv[3]; + + if(strlen(sz_port) <= 0 || strlen(sz_addr) <= 0){ + fprintf(stderr, "ERROR: wrong arguments.\n"); + return 1; + } + + server_ip = inet_addr(sz_addr); + + ul_port = strtoul(sz_port, NULL, 10); + if(ul_port < 1024 || ul_port >= 65536){ + fprintf(stderr, "ERROR: wrong port number.\n"); + return 3; + } + port = htons((uint16_t) ul_port); + + if(ndn_name_from_string(&name_prefix, argv[3], strlen(argv[3])) != NDN_SUCCESS){ + fprintf(stderr, "ERROR: wrong name.\n"); + return 4; + } + + return 0; +} + +int on_data(const uint8_t* rawdata, uint32_t data_size){ + ndn_data_t data; + printf("On data\n"); + if(ndn_data_tlv_decode_digest_verify(&data, rawdata, data_size)){ + printf("Decoding failed.\n"); + return -1; + } + + printf("It says: %s\n", data.content_value); + + return NDN_SUCCESS; +} + +int main(int argc, char *argv[]){ + ndn_udp_multicast_face_t *face; + ndn_interest_t interest; + ndn_encoder_t encoder; + int ret; + + if((ret = parseArgs(argc, argv)) != 0){ + return ret; + } + + ndn_forwarder_init(); + ndn_security_init(); + face = ndn_udp_multicast_face_construct(1, INADDR_ANY, port, server_ip); + ndn_direct_face_construct(2); + + running = true; + ndn_forwarder_fib_insert(&name_prefix, &face->intf, 0); + + ndn_interest_from_name(&interest, &name_prefix); + encoder_init(&encoder, buf, 4096); + ndn_interest_tlv_encode(&encoder, &interest); + + ndn_direct_face_express_interest(&name_prefix, encoder.output_value, encoder.offset, on_data, NULL); + + while(running){ + ndn_udp_multicast_face_recv(face); + usleep(10); + } + + ndn_face_destroy(&face->intf); + + return 0; +} \ No newline at end of file diff --git a/examples/udp-group-producer.c b/examples/udp-group-producer.c new file mode 100644 index 0000000..e0d7f4a --- /dev/null +++ b/examples/udp-group-producer.c @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2019 Xinyu Ma + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v3.0. See the file LICENSE in the top level + * directory for more details. + * + * See AUTHORS.md for complete list of NDN IOT PKG authors and contributors. + */ +#include +#include +#include +#include +#include +#include "adaptation/udp-multicast/ndn-udp-multicast-face.h" +#include "ndn-lite/forwarder/forwarder.h" +#include "ndn-lite/face/direct-face.h" +#include "ndn-lite/encode/data.h" + +in_port_t port; +in_addr_t server_ip; +ndn_name_t name_prefix; +uint8_t buf[4096]; +ndn_udp_multicast_face_t *face; +bool running; + +int parseArgs(int argc, char *argv[]){ + char *sz_port, *sz_addr; + uint32_t ul_port; + + if(argc < 4){ + fprintf(stderr, "ERROR: wrong arguments.\n"); + printf("Usage: \n"); + return 1; + } + sz_port = argv[1]; + sz_addr = argv[2]; + //sz_prefix = argv[3]; + + if(strlen(sz_port) <= 0 || strlen(sz_addr) <= 0){ + fprintf(stderr, "ERROR: wrong arguments.\n"); + return 1; + } + + server_ip = inet_addr(sz_addr); + + ul_port = strtoul(sz_port, NULL, 10); + if(ul_port < 1024 || ul_port >= 65536){ + fprintf(stderr, "ERROR: wrong port number.\n"); + return 3; + } + port = htons((uint16_t) ul_port); + + if(ndn_name_from_string(&name_prefix, argv[3], strlen(argv[3])) != NDN_SUCCESS){ + fprintf(stderr, "ERROR: wrong name.\n"); + return 4; + } + + return 0; +} + +int on_interest(const uint8_t* interest, uint32_t interest_size){ + ndn_data_t data; + ndn_encoder_t encoder; + char * str = "I'm a Data packet."; + + printf("On interest\n"); + data.name = name_prefix; + ndn_data_set_content(&data, str, strlen(str)); + ndn_metainfo_init(&data.metainfo); + ndn_metainfo_set_content_type(&data.metainfo, NDN_CONTENT_TYPE_BLOB); + encoder_init(&encoder, buf, 4096); + ndn_data_tlv_encode_digest_sign(&encoder, &data); + ndn_forwarder_on_incoming_data(ndn_forwarder_get_instance(), + &face->intf, + &name_prefix, + encoder.output_value, + encoder.offset); + + return NDN_SUCCESS; +} + +int main(int argc, char *argv[]){ + int ret; + + if((ret = parseArgs(argc, argv)) != 0){ + return ret; + } + + ndn_forwarder_init(); + ndn_security_init(); + face = ndn_udp_multicast_face_construct(1, INADDR_ANY, port, server_ip); + ndn_direct_face_construct(2); + + running = true; + ndn_direct_face_register_prefix(&name_prefix, on_interest); + while(running){ + ndn_udp_multicast_face_recv(face); + usleep(10); + } + + ndn_face_destroy(&face->intf); + + return 0; +} \ No newline at end of file diff --git a/ndn-lite b/ndn-lite index 5441b5f..14a8d35 160000 --- a/ndn-lite +++ b/ndn-lite @@ -1 +1 @@ -Subproject commit 5441b5f42d7926e10931e9ae36f4ded9a412fb7c +Subproject commit 14a8d35645017207f5e21e7ac2d33d9a6ae73844 diff --git a/ndn-lite.h b/ndn-lite.h new file mode 100644 index 0000000..d1d21f1 --- /dev/null +++ b/ndn-lite.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2019 Xinyu Ma + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v3.0. See the file LICENSE in the top level + * directory for more details. + */ +#ifndef NDN_LITE_H +#define NDN_LITE_H + +#include "ndn-lite/ndn-constants.h" +#include "ndn-lite/ndn-enums.h" +#include "ndn-lite/ndn-error-code.h" +#include "ndn-lite/ndn-services.h" +#include "ndn-lite/forwarder/forwarder.h" +#include "adaptation/adapt-consts.h" +#include "adaptation/udp/udp-face.h" +#include "ndn-lite/security/ndn-lite-sec-config.h" +//#include "ndn-lite/encode/forwarder-helper.h" + +static void +ndn_lite_startup(){ + ndn_security_init(); + ndn_forwarder_init(); +} + +#endif // NDN_LITE_H diff --git a/ndn-riot-tests b/ndn-riot-tests index 9b7904b..97e072f 160000 --- a/ndn-riot-tests +++ b/ndn-riot-tests @@ -1 +1 @@ -Subproject commit 9b7904b16d1cfada5e76577075ea85095d733e29 +Subproject commit 97e072fa96bbbfd53a272e69a05c54ecfa9a4340