diff options
author | dimitri staessens <[email protected]> | 2017-04-12 16:57:48 +0200 |
---|---|---|
committer | dimitri staessens <[email protected]> | 2017-04-13 11:30:20 +0200 |
commit | fc10a7587b1a642748ae0fd69f08d92b4a902248 (patch) | |
tree | e0b570cf30753a564855242c94d242f597b5c499 | |
parent | a3d550ff972121641562d375f75bcf188fc7fe59 (diff) | |
download | ouroboros-fc10a7587b1a642748ae0fd69f08d92b4a902248.tar.gz ouroboros-fc10a7587b1a642748ae0fd69f08d92b4a902248.zip |
lib, ipcpd, irmd: Register hash instead of name
All information passed over the IRMd/IPCP boundary for using IPC
services (flow allocation, registration) is now hashed. This
effectively fixes the shared namespace between DIFs and the IRMDs.
This PR also fixes some API issues (adding const identifiers),
shuffles the include headers a bit and some small bugs.
56 files changed, 1054 insertions, 1081 deletions
diff --git a/include/ouroboros/CMakeLists.txt b/include/ouroboros/CMakeLists.txt index a8589da1..748d5e2d 100644 --- a/include/ouroboros/CMakeLists.txt +++ b/include/ouroboros/CMakeLists.txt @@ -6,12 +6,10 @@ set(HEADER_FILES cacep.h cdap.h dev.h - endian.h errno.h fcntl.h fqueue.h irm.h - irm_config.h nsm.h qos.h) diff --git a/src/lib/btree.h b/include/ouroboros/btree.h index f7c293c5..f7c293c5 100644 --- a/src/lib/btree.h +++ b/include/ouroboros/btree.h diff --git a/include/ouroboros/endian.h b/include/ouroboros/endian.h index d712b1b3..691e3f8b 100644 --- a/include/ouroboros/endian.h +++ b/include/ouroboros/endian.h @@ -84,5 +84,7 @@ #define hton32(x) htobe32(x) #define ntoh64(x) betoh64(x) #define noth32(x) betoh32(x) +#define hton16(x) htobe16(x) +#define ntoh16(x) betoh16(x) #endif /* OUROBOROS_ENDIAN_H */ diff --git a/include/ouroboros/errno.h b/include/ouroboros/errno.h index d2117992..fa1a33f2 100644 --- a/include/ouroboros/errno.h +++ b/include/ouroboros/errno.h @@ -28,6 +28,8 @@ #define ENOTALLOC 1000 /* Flow is not allocated */ #define EIPCPTYPE 1001 /* Unknown IPCP type */ -#define EIRMD 1002 /* Failed to contact IRMD */ +#define EIRMD 1002 /* Failed to communicate with IRMD */ +#define EIPCP 1003 /* Failed to communicate with IPCP */ +#define EIPCPSTATE 1004 /* Target in wrong state */ -#endif +#endif /* OUROBOROS_ERRNO_H */ diff --git a/include/ouroboros/hash.h b/include/ouroboros/hash.h new file mode 100644 index 00000000..4779a9a6 --- /dev/null +++ b/include/ouroboros/hash.h @@ -0,0 +1,41 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2017 + * + * Hashing functions + * + * Dimitri Staessens <[email protected]> + * Sander Vrijders <[email protected]> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef OUROBOROS_LIB_HASH_H +#define OUROBOROS_LIB_HASH_H + +#include <ouroboros/sha3.h> +#include <ouroboros/crc32.h> + +#define HASH_FMT "%02x%02x%02x%02x" +#define HASH_VAL(hash) \ + ((*(unsigned int *) hash) & 0xFF000000) >> 24, \ + ((*(unsigned int *) hash) & 0x00FF0000) >> 16, \ + ((*(unsigned int *) hash) & 0x0000FF00) >> 8, \ + ((*(unsigned int *) hash) & 0x000000FF) + +/* FIXME: Implement specifying algorithm */ +void get_hash(uint8_t buf[], + const char * name); + +#endif /* OUROBOROS_LIB_HASH_H */ diff --git a/include/ouroboros/ipcp-dev.h b/include/ouroboros/ipcp-dev.h index 89ef733b..571689ca 100644 --- a/include/ouroboros/ipcp-dev.h +++ b/include/ouroboros/ipcp-dev.h @@ -29,9 +29,10 @@ int ipcp_create_r(pid_t api, int result); -int ipcp_flow_req_arr(pid_t api, - char * dst_name, - qoscube_t cube); +int ipcp_flow_req_arr(pid_t api, + const uint8_t * dst, + size_t len, + qoscube_t cube); int ipcp_flow_alloc_reply(int fd, int response); diff --git a/include/ouroboros/irm_config.h b/include/ouroboros/ipcp.h index 3dd55823..f439f29b 100644 --- a/include/ouroboros/irm_config.h +++ b/include/ouroboros/ipcp.h @@ -1,7 +1,7 @@ /* * Ouroboros - Copyright (C) 2016 - 2017 * - * Configuration information for the IPC Resource Manager + * IPCP definitions and policies * * Dimitri Staessens <[email protected]> * Sander Vrijders <[email protected]> @@ -21,25 +21,25 @@ * 02110-1301 USA */ +#ifndef OUROBOROS_IPCP_H +#define OUROBOROS_IPCP_H + #include <stdint.h> #include <unistd.h> #include <stdbool.h> -#ifndef OUROBOROS_IRM_CONFIG_H -#define OUROBOROS_IRM_CONFIG_H - -/* Name binding options */ - -#define BIND_AP_AUTO 0x01 -#define BIND_AP_UNIQUE 0x02 - +/* + * NOTE: the IRMd uses this order to select an IPCP + * for flow allocation + */ enum ipcp_type { - IPCP_NORMAL = 0, - IPCP_LOCAL, + IPCP_LOCAL = 0, + IPCP_SHIM_ETH_LLC, IPCP_SHIM_UDP, - IPCP_SHIM_ETH_LLC + IPCP_NORMAL }; +/* IPCP policies */ enum pol_addr_auth { FLAT_RANDOM = 0 }; @@ -48,9 +48,10 @@ enum pol_gam { COMPLETE = 0 }; -struct dif_config { - char * dif_name; - enum ipcp_type type; +struct ipcp_config { + char * dif_name; + enum ipcp_type type; + uint16_t dir_hash_len; /* Normal DIF */ uint8_t addr_size; @@ -76,4 +77,4 @@ struct dif_config { char * if_name; }; -#endif /* OUROBOROS_IRM_CONFIG_H */ +#endif /* OUROBOROS_IPCP_H */ diff --git a/include/ouroboros/irm.h b/include/ouroboros/irm.h index 9bd0c93b..d1aa5230 100644 --- a/include/ouroboros/irm.h +++ b/include/ouroboros/irm.h @@ -24,9 +24,15 @@ #ifndef OUROBOROS_IRM_H #define OUROBOROS_IRM_H -#include <ouroboros/irm_config.h> +#include <ouroboros/ipcp.h> + #include <sys/types.h> +/* Name binding options */ + +#define BIND_AP_AUTO 0x01 +#define BIND_AP_UNIQUE 0x02 + pid_t irm_create_ipcp(const char * name, enum ipcp_type ipcp_type); @@ -39,8 +45,8 @@ ssize_t irm_list_ipcps(const char * name, int irm_enroll_ipcp(pid_t api, const char * dif_name); -int irm_bootstrap_ipcp(pid_t api, - const struct dif_config * conf); +int irm_bootstrap_ipcp(pid_t api, + const struct ipcp_config * conf); int irm_bind_ap(const char * ap, const char * name, diff --git a/src/lib/sha3.h b/include/ouroboros/sha3.h index 6ce67482..17888870 100644 --- a/src/lib/sha3.h +++ b/include/ouroboros/sha3.h @@ -44,24 +44,24 @@ * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk! */ -#ifndef OUROBOROS_LIB_SHA3_H -#define OUROBOROS_LIB_SHA3_H +#ifndef OUROBOROS_SHA3_H +#define OUROBOROS_SHA3_H #include <unistd.h> #include <stdint.h> -#define sha3_224_hash_size 28 -#define sha3_256_hash_size 32 -#define sha3_384_hash_size 48 -#define sha3_512_hash_size 64 -#define sha3_max_permutation_size 25 -#define sha3_max_rate_in_qwords 24 +#define SHA3_224_HASH_LEN 28 +#define SHA3_256_HASH_LEN 32 +#define SHA3_384_HASH_LEN 48 +#define SHA3_512_HASH_LEN 64 +#define SHA3_MAX_PERMUTATION_SIZE 25 +#define SHA3_MAX_RATE_IN_QWORDS 24 struct sha3_ctx { /* 1600 bits algorithm hashing state */ - uint64_t hash[sha3_max_permutation_size]; + uint64_t hash[SHA3_MAX_PERMUTATION_SIZE]; /* 1536-bit buffer for leftovers */ - uint64_t message[sha3_max_rate_in_qwords]; + uint64_t message[SHA3_MAX_RATE_IN_QWORDS]; /* count of bytes in the message[] buffer */ unsigned rest; /* size of a message block processed at once */ @@ -77,10 +77,10 @@ void rhash_sha3_384_init(struct sha3_ctx * ctx); void rhash_sha3_512_init(struct sha3_ctx * ctx); void rhash_sha3_update(struct sha3_ctx * ctx, - const uint8_t * msg, + const void * msg, size_t size); void rhash_sha3_final(struct sha3_ctx * ctx, uint8_t * res); -#endif /* OUROBOROS_LIB_SHA3_H */ +#endif /* OUROBOROS_SHA3_H */ diff --git a/include/ouroboros/sockets.h b/include/ouroboros/sockets.h index 8237efb6..bb8f6d87 100644 --- a/include/ouroboros/sockets.h +++ b/include/ouroboros/sockets.h @@ -26,8 +26,8 @@ #include <sys/types.h> -#include "dif_config.pb-c.h" -typedef DifConfigMsg dif_config_msg_t; +#include "ipcp_config.pb-c.h" +typedef IpcpConfigMsg ipcp_config_msg_t; #include "irmd_messages.pb-c.h" typedef IrmMsg irm_msg_t; diff --git a/include/ouroboros/wrap/ouroboros.i b/include/ouroboros/wrap/ouroboros.i index b156eb36..62a829e2 100644 --- a/include/ouroboros/wrap/ouroboros.i +++ b/include/ouroboros/wrap/ouroboros.i @@ -29,7 +29,7 @@ #include "ouroboros/fcntl.h" #include "ouroboros/fqueue.h" #include "ouroboros/irm.h" -#include "ouroboros/irm_config.h" +#include "ouroboros/ipcp.h" #include "ouroboros/nsm.h" #include "ouroboros/qos.h" %} @@ -43,6 +43,6 @@ typedef int pid_t; %include "ouroboros/fcntl.h" %include "ouroboros/fqueue.h" %include "ouroboros/irm.h" -%include "ouroboros/irm_config.h" +%include "ouroboros/ipcp.h" %include "ouroboros/nsm.h" %include "ouroboros/qos.h" diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index 587d70c5..795ce42c 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -38,6 +38,54 @@ #include <sys/socket.h> #include <stdlib.h> +void ipcp_sig_handler(int sig, + siginfo_t * info, + void * c) +{ + (void) c; + + switch(sig) { + case SIGINT: + case SIGTERM: + case SIGHUP: + case SIGQUIT: + if (info->si_pid == ipcpi.irmd_api) { + if (ipcp_get_state() == IPCP_INIT) + ipcp_set_state(IPCP_NULL); + + if (ipcp_get_state() == IPCP_OPERATIONAL) + ipcp_set_state(IPCP_SHUTDOWN); + } + default: + return; + } +} + +uint8_t * ipcp_hash_dup(const uint8_t * hash) +{ + uint8_t * dup = malloc(ipcpi.dir_hash_len); + if (dup == NULL) + return NULL; + + memcpy(dup, hash, ipcpi.dir_hash_len); + + return dup; +} + +void ipcp_hash_str(char buf[DIR_HASH_STRLEN + 1], + const uint8_t * hash) +{ + size_t i; + + char * HEX = "0123456789abcdef"; + + for (i = 0; i < ipcpi.dir_hash_len; ++i) { + buf[i * 2] = HEX[(hash[i] & 0xF0) >> 4]; + buf[i * 2 + 1] = HEX[hash[i] & 0x0F]; + } + + buf[2 * i] = '\0'; +} static void thread_inc(void) { @@ -93,8 +141,8 @@ static void * ipcp_main_loop(void * o) buffer_t buffer; ipcp_msg_t ret_msg = IPCP_MSG__INIT; - dif_config_msg_t * conf_msg; - struct dif_config conf; + ipcp_config_msg_t * conf_msg; + struct ipcp_config conf; struct timeval ltv = {(SOCKET_TIMEOUT / 1000), (SOCKET_TIMEOUT % 1000) * 1000}; @@ -109,18 +157,13 @@ static void * ipcp_main_loop(void * o) #endif int fd = -1; - pthread_rwlock_rdlock(&ipcpi.state_lock); - if (ipcp_get_state() == IPCP_SHUTDOWN || ipcp_get_state() == IPCP_NULL || thread_check()) { thread_exit(id); - pthread_rwlock_unlock(&ipcpi.state_lock); break; } - pthread_rwlock_unlock(&ipcpi.state_lock); - ret_msg.code = IPCP_MSG_CODE__IPCP_REPLY; #ifdef __FreeBSD__ FD_ZERO(&fds); @@ -153,12 +196,23 @@ static void * ipcp_main_loop(void * o) switch (msg->code) { case IPCP_MSG_CODE__IPCP_BOOTSTRAP: + ret_msg.has_result = true; + if (ipcpi.ops->ipcp_bootstrap == NULL) { log_err("Bootstrap unsupported."); + ret_msg.result = -ENOTSUP; break; } + + if (ipcp_get_state() != IPCP_INIT) { + log_err("IPCP in wrong state."); + ret_msg.result = -EIPCPSTATE; + break; + } + conf_msg = msg->conf; conf.type = conf_msg->ipcp_type; + conf.dir_hash_len = conf_msg->dir_hash_len; conf.dif_name = conf_msg->dif_name; if (conf.dif_name == NULL) { ret_msg.has_result = true; @@ -179,74 +233,123 @@ static void * ipcp_main_loop(void * o) conf.dt_gam_type = conf_msg->dt_gam_type; conf.rm_gam_type = conf_msg->rm_gam_type; } + if (conf_msg->ipcp_type == IPCP_SHIM_UDP) { conf.ip_addr = conf_msg->ip_addr; conf.dns_addr = conf_msg->dns_addr; } + if (conf_msg->ipcp_type == IPCP_SHIM_ETH_LLC) conf.if_name = conf_msg->if_name; - ret_msg.has_result = true; ret_msg.result = ipcpi.ops->ipcp_bootstrap(&conf); break; case IPCP_MSG_CODE__IPCP_ENROLL: + ret_msg.has_result = true; + if (ipcpi.ops->ipcp_enroll == NULL) { log_err("Enroll unsupported."); + ret_msg.result = -ENOTSUP; break; } - ret_msg.has_result = true; - ret_msg.result = ipcpi.ops->ipcp_enroll(msg->dif_name); - break; - case IPCP_MSG_CODE__IPCP_NAME_REG: - if (ipcpi.ops->ipcp_name_reg == NULL) { - log_err("Ap_reg unsupported."); + + if (ipcp_get_state() != IPCP_INIT) { + log_err("IPCP in wrong state."); + ret_msg.result = -EIPCPSTATE; break; } + + ret_msg.result = ipcpi.ops->ipcp_enroll(msg->dst_name); + break; + case IPCP_MSG_CODE__IPCP_REG: ret_msg.has_result = true; + + if (ipcpi.ops->ipcp_reg == NULL) { + log_err("Registration unsupported."); + ret_msg.result = -ENOTSUP; + break; + } + + assert(msg->hash.len == ipcpi.dir_hash_len); + ret_msg.result = - ipcpi.ops->ipcp_name_reg(msg->name); + ipcpi.ops->ipcp_reg(msg->hash.data); break; - case IPCP_MSG_CODE__IPCP_NAME_UNREG: - if (ipcpi.ops->ipcp_name_unreg == NULL) { - log_err("Ap_unreg unsupported."); + case IPCP_MSG_CODE__IPCP_UNREG: + ret_msg.has_result = true; + + if (ipcpi.ops->ipcp_unreg == NULL) { + log_err("Unregistration unsupported."); + ret_msg.result = -ENOTSUP; break; } - ret_msg.has_result = true; + + assert(msg->hash.len == ipcpi.dir_hash_len); + ret_msg.result = - ipcpi.ops->ipcp_name_unreg(msg->name); + ipcpi.ops->ipcp_unreg(msg->hash.data); break; - case IPCP_MSG_CODE__IPCP_NAME_QUERY: - if (ipcpi.ops->ipcp_name_query == NULL) { - log_err("Ap_query unsupported."); + case IPCP_MSG_CODE__IPCP_QUERY: + ret_msg.has_result = true; + + if (ipcpi.ops->ipcp_query == NULL) { + log_err("Directory query unsupported."); + ret_msg.result = -ENOTSUP; break; } - ret_msg.has_result = true; + + assert(msg->hash.len == ipcpi.dir_hash_len); + + if (ipcp_get_state() != IPCP_OPERATIONAL) { + log_err("IPCP in wrong state."); + ret_msg.result = -EIPCPSTATE; + break; + } + ret_msg.result = - ipcpi.ops->ipcp_name_query(msg->name); + ipcpi.ops->ipcp_query(msg->hash.data); break; case IPCP_MSG_CODE__IPCP_FLOW_ALLOC: + ret_msg.has_result = true; + if (ipcpi.ops->ipcp_flow_alloc == NULL) { - log_err("Flow_alloc unsupported."); + log_err("Flow allocation unsupported."); + ret_msg.result = -ENOTSUP; + break; + } + + assert(msg->hash.len == ipcpi.dir_hash_len); + + if (ipcp_get_state() != IPCP_OPERATIONAL) { + log_err("IPCP in wrong state."); + ret_msg.result = -EIPCPSTATE; break; } + fd = np1_flow_alloc(msg->api, msg->port_id); if (fd < 0) { log_err("Failed allocating fd on port_id %d.", msg->port_id); - ret_msg.has_result = true; ret_msg.result = -1; break; } - ret_msg.has_result = true; ret_msg.result = ipcpi.ops->ipcp_flow_alloc(fd, - msg->dst_name, + msg->hash.data, msg->qoscube); break; case IPCP_MSG_CODE__IPCP_FLOW_ALLOC_RESP: + ret_msg.has_result = true; if (ipcpi.ops->ipcp_flow_alloc_resp == NULL) { log_err("Flow_alloc_resp unsupported."); + ret_msg.result = -ENOTSUP; + break; + } + + if (ipcp_get_state() != IPCP_OPERATIONAL) { + log_err("IPCP in wrong state."); + ret_msg.result = -EIPCPSTATE; break; } @@ -255,20 +358,26 @@ static void * ipcp_main_loop(void * o) if (fd < 0) { log_warn("Port_id %d is not known.", msg->port_id); - ret_msg.has_result = true; ret_msg.result = -1; break; } } - ret_msg.has_result = true; ret_msg.result = ipcpi.ops->ipcp_flow_alloc_resp(fd, msg->response); break; case IPCP_MSG_CODE__IPCP_FLOW_DEALLOC: + ret_msg.has_result = true; if (ipcpi.ops->ipcp_flow_dealloc == NULL) { - log_err("Flow_dealloc unsupported."); + log_err("Flow deallocation unsupported."); + ret_msg.result = -ENOTSUP; + break; + } + + if (ipcp_get_state() != IPCP_OPERATIONAL) { + log_err("IPCP in wrong state."); + ret_msg.result = -EIPCPSTATE; break; } @@ -276,12 +385,10 @@ static void * ipcp_main_loop(void * o) if (fd < 0) { log_warn("Could not deallocate port_id %d.", msg->port_id); - ret_msg.has_result = true; ret_msg.result = -1; break; } - ret_msg.has_result = true; ret_msg.result = ipcpi.ops->ipcp_flow_dealloc(fd); break; @@ -409,11 +516,6 @@ int ipcp_init(int argc, ipcpi.ops = ops; - if (pthread_rwlock_init(&ipcpi.state_lock, NULL)) { - log_err("Could not create rwlock."); - goto fail_state_rwlock; - } - if (pthread_mutex_init(&ipcpi.state_mtx, NULL)) { log_err("Could not create mutex."); goto fail_state_mtx; @@ -492,8 +594,6 @@ int ipcp_init(int argc, fail_thread_lock: pthread_mutex_destroy(&ipcpi.state_mtx); fail_state_mtx: - pthread_rwlock_destroy(&ipcpi.state_lock); - fail_state_rwlock: close(ipcpi.sockfd); fail_serv_sock: free(ipcpi.sock_path); @@ -522,10 +622,8 @@ void * threadpoolmgr(void * o) clock_gettime(PTHREAD_COND_CLOCK, &dl); ts_add(&dl, &to, &dl); - pthread_rwlock_rdlock(&ipcpi.state_lock); if (ipcp_get_state() == IPCP_SHUTDOWN || ipcp_get_state() == IPCP_NULL) { - pthread_rwlock_unlock(&ipcpi.state_lock); pthread_attr_destroy(&pattr); log_dbg("Waiting for threads to exit."); pthread_mutex_lock(&ipcpi.threads_lock); @@ -538,8 +636,6 @@ void * threadpoolmgr(void * o) break; } - pthread_rwlock_unlock(&ipcpi.state_lock); - pthread_mutex_lock(&ipcpi.threads_lock); if (ipcpi.threads < IPCP_MIN_AV_THREADS) { @@ -576,10 +672,34 @@ void * threadpoolmgr(void * o) int ipcp_boot() { + struct sigaction sig_act; + sigset_t sigset; + sigemptyset(&sigset); + sigaddset(&sigset, SIGINT); + sigaddset(&sigset, SIGQUIT); + sigaddset(&sigset, SIGHUP); + sigaddset(&sigset, SIGPIPE); + + /* init sig_act */ + memset(&sig_act, 0, sizeof(sig_act)); + + /* install signal traps */ + sig_act.sa_sigaction = &ipcp_sig_handler; + sig_act.sa_flags = SA_SIGINFO; + + sigaction(SIGINT, &sig_act, NULL); + sigaction(SIGTERM, &sig_act, NULL); + sigaction(SIGHUP, &sig_act, NULL); + sigaction(SIGPIPE, &sig_act, NULL); + + pthread_sigmask(SIG_BLOCK, &sigset, NULL); + ipcp_set_state(IPCP_INIT); pthread_create(&ipcpi.tpm, NULL, threadpoolmgr, NULL); + pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); + return 0; } @@ -607,7 +727,6 @@ void ipcp_fini() pthread_cond_destroy(&ipcpi.threads_cond); pthread_mutex_destroy(&ipcpi.threads_lock); pthread_mutex_destroy(&ipcpi.state_mtx); - pthread_rwlock_destroy(&ipcpi.state_lock); pthread_cond_destroy(&ipcpi.alloc_cond); pthread_mutex_destroy(&ipcpi.alloc_lock); diff --git a/src/ipcpd/ipcp.h b/src/ipcpd/ipcp.h index e5c4b9af..d6e2aa7c 100644 --- a/src/ipcpd/ipcp.h +++ b/src/ipcpd/ipcp.h @@ -24,12 +24,13 @@ #define IPCPD_IPCP_H #include <ouroboros/config.h> -#include <ouroboros/irm_config.h> +#include <ouroboros/ipcp.h> #include "shim-data.h" #include <pthread.h> #include <time.h> +#include <signal.h> enum ipcp_state { IPCP_NULL = 0, @@ -39,19 +40,19 @@ enum ipcp_state { }; struct ipcp_ops { - int (* ipcp_bootstrap)(struct dif_config * conf); + int (* ipcp_bootstrap)(const struct ipcp_config * conf); - int (* ipcp_enroll)(char * dif_name); + int (* ipcp_enroll)(const char * dst); - int (* ipcp_name_reg)(char * name); + int (* ipcp_reg)(const uint8_t * hash); - int (* ipcp_name_unreg)(char * name); + int (* ipcp_unreg)(const uint8_t * hash); - int (* ipcp_name_query)(char * name); + int (* ipcp_query)(const uint8_t * hash); - int (* ipcp_flow_alloc)(int fd, - char * dst_ap_name, - qoscube_t qos); + int (* ipcp_flow_alloc)(int fd, + const uint8_t * dst, + qoscube_t qos); int (* ipcp_flow_alloc_resp)(int fd, int response); @@ -59,6 +60,8 @@ struct ipcp_ops { int (* ipcp_flow_dealloc)(int fd); }; +#define DIR_HASH_STRLEN (ipcpi.dir_hash_len * 2) + struct ipcp { int irmd_api; char * name; @@ -67,6 +70,7 @@ struct ipcp { char * dif_name; uint64_t dt_addr; + uint16_t dir_hash_len; struct ipcp_ops * ops; int irmd_fd; @@ -117,4 +121,15 @@ int ipcp_wait_state(enum ipcp_state state, int ipcp_parse_arg(int argc, char * argv[]); +/* Handle shutdown of IPCP */ +void ipcp_sig_handler(int sig, + siginfo_t * info, + void * c); + +/* Helper functions for directory entries, could be moved */ +uint8_t * ipcp_hash_dup(const uint8_t * hash); + +void ipcp_hash_str(char buf[], + const uint8_t * hash); + #endif diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c index 897ec3a0..21ca7400 100644 --- a/src/ipcpd/local/main.c +++ b/src/ipcpd/local/main.c @@ -27,13 +27,14 @@ #include <ouroboros/errno.h> #include <ouroboros/dev.h> #include <ouroboros/fqueue.h> +#include <ouroboros/ipcp.h> #include <ouroboros/ipcp-dev.h> #include <ouroboros/local-dev.h> +#include <ouroboros/hash.h> #include "ipcp.h" #include <string.h> -#include <signal.h> #include <stdlib.h> #include <pthread.h> #include <sys/wait.h> @@ -113,110 +114,74 @@ static void * ipcp_local_sdu_loop(void * o) return (void *) 0; } -void ipcp_sig_handler(int sig, - siginfo_t * info, - void * c) +static int ipcp_local_bootstrap(const struct ipcp_config * conf) { - (void) c; - - switch(sig) { - case SIGINT: - case SIGTERM: - case SIGHUP: - case SIGQUIT: - if (info->si_pid == ipcpi.irmd_api) { - if (ipcp_get_state() == IPCP_INIT) - ipcp_set_state(IPCP_NULL); - - if (ipcp_get_state() == IPCP_OPERATIONAL) - ipcp_set_state(IPCP_SHUTDOWN); - } - default: - return; - } -} - -static int ipcp_local_bootstrap(struct dif_config * conf) -{ - (void) conf; - assert(conf); assert(conf->type == THIS_TYPE); - if (ipcp_get_state() != IPCP_INIT) { - log_err("IPCP in wrong state."); - return -1; - } + ipcpi.dir_hash_len = conf->dir_hash_len; ipcp_set_state(IPCP_OPERATIONAL); - pthread_create(&local_data.sduloop, NULL, ipcp_local_sdu_loop, NULL); + if (pthread_create(&local_data.sduloop, NULL, + ipcp_local_sdu_loop, NULL)) { + ipcp_set_state(IPCP_INIT); + return -1; + } log_info("Bootstrapped local IPCP with api %d.", getpid()); return 0; } -static int ipcp_local_name_reg(char * name) +static int ipcp_local_reg(const uint8_t * hash) { - char * name_dup = strdup(name); - if (name_dup == NULL) { - log_err("Failed to duplicate name."); + uint8_t * hash_dup = ipcp_hash_dup(hash); + if (hash_dup == NULL) { + log_err("Failed to duplicate hash."); return -ENOMEM; } - pthread_rwlock_rdlock(&ipcpi.state_lock); - - if (shim_data_reg_add_entry(ipcpi.shim_data, name_dup)) { - pthread_rwlock_unlock(&ipcpi.state_lock); - log_dbg("Failed to add %s to local registry.", name); - free(name_dup); + if (shim_data_reg_add_entry(ipcpi.shim_data, hash_dup)) { + log_dbg("Failed to add " HASH_FMT " to local registry.", + HASH_VAL(hash)); + free(hash_dup); return -1; } - pthread_rwlock_unlock(&ipcpi.state_lock); - - log_info("Registered %s.", name); + log_info("Registered " HASH_FMT ".", HASH_VAL(hash)); return 0; } -static int ipcp_local_name_unreg(char * name) +static int ipcp_local_unreg(const uint8_t * hash) { - pthread_rwlock_rdlock(&ipcpi.state_lock); - - shim_data_reg_del_entry(ipcpi.shim_data, name); + shim_data_reg_del_entry(ipcpi.shim_data, hash); - pthread_rwlock_unlock(&ipcpi.state_lock); - - log_info("Unregistered %s.", name); + log_info("Unregistered " HASH_FMT ".", HASH_VAL(hash)); return 0; } -static int ipcp_local_name_query(char * name) +static int ipcp_local_query(const uint8_t * hash) { int ret; - pthread_rwlock_rdlock(&ipcpi.state_lock); - - ret = (shim_data_reg_has(ipcpi.shim_data, name) ? 0 : -1); - - pthread_rwlock_unlock(&ipcpi.state_lock); + ret = (shim_data_reg_has(ipcpi.shim_data, hash) ? 0 : -1); return ret; } -static int ipcp_local_flow_alloc(int fd, - char * dst_name, - qoscube_t cube) +static int ipcp_local_flow_alloc(int fd, + const uint8_t * dst, + qoscube_t cube) { struct timespec ts = {0, EVENT_WAIT_TIMEOUT * 1000}; int out_fd = -1; - log_dbg("Allocating flow to %s on fd %d.", dst_name, fd); + log_dbg("Allocating flow to " HASH_FMT " on fd %d.", HASH_VAL(dst), fd); - assert(dst_name); + assert(dst); pthread_mutex_lock(&ipcpi.alloc_lock); @@ -233,7 +198,7 @@ static int ipcp_local_flow_alloc(int fd, assert(ipcpi.alloc_id == -1); - out_fd = ipcp_flow_req_arr(getpid(), dst_name, cube); + out_fd = ipcp_flow_req_arr(getpid(), dst, ipcpi.dir_hash_len, cube); if (out_fd < 0) { pthread_mutex_unlock(&ipcpi.alloc_lock); log_dbg("Flow allocation failed: %d", out_fd); @@ -335,9 +300,9 @@ static int ipcp_local_flow_dealloc(int fd) static struct ipcp_ops local_ops = { .ipcp_bootstrap = ipcp_local_bootstrap, .ipcp_enroll = NULL, /* shim */ - .ipcp_name_reg = ipcp_local_name_reg, - .ipcp_name_unreg = ipcp_local_name_unreg, - .ipcp_name_query = ipcp_local_name_query, + .ipcp_reg = ipcp_local_reg, + .ipcp_unreg = ipcp_local_unreg, + .ipcp_query = ipcp_local_query, .ipcp_flow_alloc = ipcp_local_flow_alloc, .ipcp_flow_alloc_resp = ipcp_local_flow_alloc_resp, .ipcp_flow_dealloc = ipcp_local_flow_dealloc @@ -346,26 +311,6 @@ static struct ipcp_ops local_ops = { int main(int argc, char * argv[]) { - struct sigaction sig_act; - sigset_t sigset; - sigemptyset(&sigset); - sigaddset(&sigset, SIGINT); - sigaddset(&sigset, SIGQUIT); - sigaddset(&sigset, SIGHUP); - sigaddset(&sigset, SIGPIPE); - - /* init sig_act */ - memset(&sig_act, 0, sizeof(sig_act)); - - /* install signal traps */ - sig_act.sa_sigaction = &ipcp_sig_handler; - sig_act.sa_flags = SA_SIGINFO; - - sigaction(SIGINT, &sig_act, NULL); - sigaction(SIGTERM, &sig_act, NULL); - sigaction(SIGHUP, &sig_act, NULL); - sigaction(SIGPIPE, &sig_act, NULL); - if (ipcp_init(argc, argv, THIS_TYPE, &local_ops) < 0) { ipcp_create_r(getpid(), -1); exit(EXIT_FAILURE); @@ -378,8 +323,6 @@ int main(int argc, exit(EXIT_FAILURE); } - pthread_sigmask(SIG_BLOCK, &sigset, NULL); - if (ipcp_boot() < 0) { log_err("Failed to boot IPCP."); ipcp_create_r(getpid(), -1); @@ -388,8 +331,6 @@ int main(int argc, exit(EXIT_FAILURE); } - pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); - if (ipcp_create_r(getpid(), 0)) { log_err("Failed to notify IRMd we are initialized."); ipcp_set_state(IPCP_NULL); diff --git a/src/ipcpd/normal/addr_auth.h b/src/ipcpd/normal/addr_auth.h index fbe7d790..6883b4b3 100644 --- a/src/ipcpd/normal/addr_auth.h +++ b/src/ipcpd/normal/addr_auth.h @@ -23,7 +23,7 @@ #ifndef OUROBOROS_IPCPD_NORMAL_ADDR_AUTH_H #define OUROBOROS_IPCPD_NORMAL_ADDR_AUTH_H -#include <ouroboros/irm_config.h> +#include <ouroboros/ipcp.h> #include <stdint.h> diff --git a/src/ipcpd/normal/connmgr.c b/src/ipcpd/normal/connmgr.c index 8068d173..421bc5b0 100644 --- a/src/ipcpd/normal/connmgr.c +++ b/src/ipcpd/normal/connmgr.c @@ -116,16 +116,11 @@ static void * flow_acceptor(void * o) memset(&fail_info, 0, sizeof(fail_info)); while (true) { - pthread_rwlock_rdlock(&ipcpi.state_lock); - if (ipcp_get_state() != IPCP_OPERATIONAL) { - pthread_rwlock_unlock(&ipcpi.state_lock); log_info("Shutting down flow acceptor."); return 0; } - pthread_rwlock_unlock(&ipcpi.state_lock); - fd = flow_accept(&qs, NULL); if (fd < 0) { if (fd != -EIRMD) @@ -271,7 +266,7 @@ void connmgr_ae_destroy(struct ae * ae) } int connmgr_alloc(struct ae * ae, - char * dst_name, + const char * dst_name, qosspec_t * qs, struct conn * conn) { diff --git a/src/ipcpd/normal/connmgr.h b/src/ipcpd/normal/connmgr.h index c0356f6d..12992ef6 100644 --- a/src/ipcpd/normal/connmgr.h +++ b/src/ipcpd/normal/connmgr.h @@ -47,7 +47,7 @@ struct ae * connmgr_ae_create(struct conn_info info); void connmgr_ae_destroy(struct ae * ae); int connmgr_alloc(struct ae * ae, - char * dst_name, + const char * dst, qosspec_t * qs, struct conn * conn); diff --git a/src/ipcpd/normal/dir.c b/src/ipcpd/normal/dir.c index ae9793c6..cbc50ba4 100644 --- a/src/ipcpd/normal/dir.c +++ b/src/ipcpd/normal/dir.c @@ -60,22 +60,23 @@ int dir_fini(void) return 0; } -int dir_name_reg(char * name) +int dir_reg(const uint8_t * hash) { + char hashstr[DIR_HASH_STRLEN + 1]; int ret; - assert(name); - - if (ipcp_get_state() != IPCP_OPERATIONAL) - return -EPERM; + assert(hash); dir_path_reset(); - ret = rib_add(dir_path, name); + ipcp_hash_str(hashstr, hash); + + ret = rib_add(dir_path, hashstr); if (ret == -ENOMEM) - return -ENOMEM; + return -ENOMEM; + + rib_path_append(dir_path, hashstr); - rib_path_append(dir_path, name); ret = rib_add(dir_path, ipcpi.name); if (ret == -EPERM) return -EPERM; @@ -88,18 +89,16 @@ int dir_name_reg(char * name) return 0; } -int dir_name_unreg(char * name) +int dir_unreg(const uint8_t * hash) { + char hashstr[DIR_HASH_STRLEN + 1]; size_t len; - assert(name); - - if (ipcp_get_state() != IPCP_OPERATIONAL) - return -EPERM; + assert(hash); dir_path_reset(); - rib_path_append(dir_path, name); + rib_path_append(dir_path, hashstr); if (!rib_has(dir_path)) return 0; @@ -118,16 +117,16 @@ int dir_name_unreg(char * name) return 0; } -int dir_name_query(char * name) +int dir_query(const uint8_t * hash) { + char hashstr[DIR_HASH_STRLEN + 1]; size_t len; - if (ipcp_get_state() != IPCP_OPERATIONAL) - return -EPERM; - dir_path_reset(); - rib_path_append(dir_path, name); + ipcp_hash_str(hashstr, hash); + + rib_path_append(dir_path, hashstr); if (!rib_has(dir_path)) return -1; diff --git a/src/ipcpd/normal/dir.h b/src/ipcpd/normal/dir.h index 04e722f3..1b28a5c0 100644 --- a/src/ipcpd/normal/dir.h +++ b/src/ipcpd/normal/dir.h @@ -27,10 +27,10 @@ int dir_init(void); int dir_fini(void); -int dir_name_reg(char * name); +int dir_reg(const uint8_t * hash); -int dir_name_unreg(char * name); +int dir_unreg(const uint8_t * hash); -int dir_name_query(char * name); +int dir_query(const uint8_t * hash); #endif /* OUROBOROS_IPCPD_NORMAL_DIR_H */ diff --git a/src/ipcpd/normal/enroll.c b/src/ipcpd/normal/enroll.c index 3e6a0197..f1f804c6 100644 --- a/src/ipcpd/normal/enroll.c +++ b/src/ipcpd/normal/enroll.c @@ -65,11 +65,9 @@ static void * enroll_handle(void * o) bool boot_r = false; bool members_r = false; - bool dif_name_r = false; char * boot_ro = BOOT_PATH; char * members_ro = MEMBERS_PATH; - char * dif_ro = DIF_PATH; cdap = (struct cdap *) o; @@ -87,7 +85,7 @@ static void * enroll_handle(void * o) continue; } - while (!(boot_r && members_r && dif_name_r)) { + while (!(boot_r && members_r)) { key = cdap_request_wait(cdap, &oc, &name, &data, (size_t *) &len , &flags); assert(key >= 0); @@ -109,8 +107,6 @@ static void * enroll_handle(void * o) boot_r = true; } else if (strcmp(name, members_ro) == 0) { members_r = true; - } else if (strcmp(name, dif_ro) == 0) { - dif_name_r = true; } else if (strcmp(name, TIME_PATH) == 0) { struct timespec t; uint64_t buf[2]; @@ -153,13 +149,13 @@ static void * enroll_handle(void * o) cdap_del_flow(cdap, conn.flow_info.fd); flow_dealloc(conn.flow_info.fd); - boot_r = members_r = dif_name_r = false; + boot_r = members_r = false; } return 0; } -int enroll_boot(char * dst_name) +int enroll_boot(const char * dst) { struct cdap * cdap; cdap_key_t * key; @@ -174,7 +170,6 @@ int enroll_boot(char * dst_name) char * boot_ro = BOOT_PATH; char * members_ro = MEMBERS_PATH; - char * dif_ro = DIF_PATH; cdap = cdap_create(); if (cdap == NULL) { @@ -182,7 +177,7 @@ int enroll_boot(char * dst_name) return -1; } - if (connmgr_alloc(enroll.ae, dst_name, NULL, &conn)) { + if (connmgr_alloc(enroll.ae, dst, NULL, &conn)) { log_err("Failed to get connection."); cdap_destroy(cdap); return -1; @@ -195,7 +190,7 @@ int enroll_boot(char * dst_name) return -1; } - log_dbg("Getting boot information from %s.", dst_name); + log_dbg("Getting boot information from %s.", dst); clock_gettime(CLOCK_REALTIME, &t0); @@ -293,37 +288,6 @@ int enroll_boot(char * dst_name) log_dbg("Packed information inserted into RIB."); - key = cdap_request_send(cdap, CDAP_READ, dif_ro, NULL, 0, 0); - if (key == NULL || key[0] == INVALID_CDAP_KEY) { - log_err("Failed to send CDAP request."); - cdap_destroy(cdap); - flow_dealloc(conn.flow_info.fd); - return -1; - } - - if (cdap_reply_wait(cdap, key[0], &data, &len)) { - log_err("Failed to get CDAP reply."); - free(key); - cdap_destroy(cdap); - flow_dealloc(conn.flow_info.fd); - return -1; - } - - free(key); - - log_dbg("Packed information received (%zu bytes).", len); - - if (rib_unpack(data, len, UNPACK_CREATE)) { - log_warn("Error unpacking RIB data."); - rib_del(boot_ro); - free(data); - cdap_destroy(cdap); - flow_dealloc(conn.flow_info.fd); - return -1; - } - - log_dbg("Packed information inserted into RIB."); - cdap_destroy(cdap); flow_dealloc(conn.flow_info.fd); diff --git a/src/ipcpd/normal/enroll.h b/src/ipcpd/normal/enroll.h index 05f950ba..bed4bf9f 100644 --- a/src/ipcpd/normal/enroll.h +++ b/src/ipcpd/normal/enroll.h @@ -31,6 +31,6 @@ int enroll_start(void); void enroll_stop(void); -int enroll_boot(char * dst_name); +int enroll_boot(const char * dst); #endif /* OUROBOROS_IPCPD_NORMAL_ENROLL_H */ diff --git a/src/ipcpd/normal/flow_alloc.proto b/src/ipcpd/normal/flow_alloc.proto index 3b08f047..35624799 100644 --- a/src/ipcpd/normal/flow_alloc.proto +++ b/src/ipcpd/normal/flow_alloc.proto @@ -30,7 +30,7 @@ enum flow_alloc_code { message flow_alloc_msg { required flow_alloc_code code = 1; - optional string dst_name = 2; + optional bytes hash = 2; optional uint32 qoscube = 3; optional sint32 response = 4; }; diff --git a/src/ipcpd/normal/fmgr.c b/src/ipcpd/normal/fmgr.c index 19c329af..5166cc5d 100644 --- a/src/ipcpd/normal/fmgr.c +++ b/src/ipcpd/normal/fmgr.c @@ -427,9 +427,9 @@ void fmgr_stop(void) gam_destroy(fmgr.gam); } -int fmgr_np1_alloc(int fd, - char * dst_ap_name, - qoscube_t cube) +int fmgr_np1_alloc(int fd, + const uint8_t * dst, + qoscube_t cube) { cep_id_t cep_id; buffer_t buf; @@ -439,14 +439,17 @@ int fmgr_np1_alloc(int fd, ssize_t ch; ssize_t i; char ** children; + char hashstr[DIR_HASH_STRLEN + 1]; char * dst_ipcp = NULL; - assert(strlen(dst_ap_name) + strlen("/" DIR_NAME) + 1 + ipcp_hash_str(hashstr, dst); + + assert(strlen(hashstr) + strlen(DIR_PATH) + 1 < RIB_MAX_PATH_LEN); strcpy(path, DIR_PATH); - rib_path_append(path, dst_ap_name); + rib_path_append(path, hashstr); ch = rib_children(path, &children); if (ch <= 0) @@ -463,7 +466,7 @@ int fmgr_np1_alloc(int fd, if (dst_ipcp == NULL) return -1; - strcpy(path, "/" MEMBERS_NAME); + strcpy(path, MEMBERS_PATH); rib_path_append(path, dst_ipcp); @@ -472,10 +475,12 @@ int fmgr_np1_alloc(int fd, if (rib_read(path, &addr, sizeof(addr)) < 0) return -1; - msg.code = FLOW_ALLOC_CODE__FLOW_REQ; - msg.dst_name = dst_ap_name; + msg.code = FLOW_ALLOC_CODE__FLOW_REQ; + msg.has_hash = true; + msg.hash.len = ipcpi.dir_hash_len; + msg.hash.data = (uint8_t *) dst; msg.has_qoscube = true; - msg.qoscube = cube; + msg.qoscube = cube; buf.len = flow_alloc_msg__get_packed_size(&msg); if (buf.len == 0) @@ -637,6 +642,11 @@ int fmgr_np1_post_buf(cep_id_t cep_id, case FLOW_ALLOC_CODE__FLOW_REQ: pthread_mutex_lock(&ipcpi.alloc_lock); + if (!msg->has_hash) { + log_err("Bad flow request."); + return -1; + } + while (ipcpi.alloc_id != -1 && ipcp_get_state() == IPCP_OPERATIONAL) pthread_cond_timedwait(&ipcpi.alloc_cond, @@ -652,7 +662,8 @@ int fmgr_np1_post_buf(cep_id_t cep_id, assert(ipcpi.alloc_id == -1); fd = ipcp_flow_req_arr(getpid(), - msg->dst_name, + msg->hash.data, + ipcpi.dir_hash_len, msg->qoscube); if (fd < 0) { pthread_mutex_unlock(&ipcpi.alloc_lock); diff --git a/src/ipcpd/normal/fmgr.h b/src/ipcpd/normal/fmgr.h index b4d0b65a..c59c0875 100644 --- a/src/ipcpd/normal/fmgr.h +++ b/src/ipcpd/normal/fmgr.h @@ -37,9 +37,9 @@ int fmgr_start(void); void fmgr_stop(void); -int fmgr_np1_alloc(int fd, - char * dst_ap_name, - qoscube_t qos); +int fmgr_np1_alloc(int fd, + const uint8_t * dst, + qoscube_t qos); int fmgr_np1_alloc_resp(int fd, int response); diff --git a/src/ipcpd/normal/gam.h b/src/ipcpd/normal/gam.h index 4ae0b1b3..752d8e37 100644 --- a/src/ipcpd/normal/gam.h +++ b/src/ipcpd/normal/gam.h @@ -23,8 +23,8 @@ #ifndef OUROBOROS_IPCPD_NORMAL_GAM_H #define OUROBOROS_IPCPD_NORMAL_GAM_H +#include <ouroboros/ipcp.h> #include <ouroboros/cacep.h> -#include <ouroboros/irm_config.h> #include "neighbors.h" diff --git a/src/ipcpd/normal/main.c b/src/ipcpd/normal/main.c index ef7f07cf..e37a0fbc 100644 --- a/src/ipcpd/normal/main.c +++ b/src/ipcpd/normal/main.c @@ -23,12 +23,13 @@ #define OUROBOROS_PREFIX "normal-ipcp" #include <ouroboros/config.h> +#include <ouroboros/endian.h> #include <ouroboros/logs.h> #include <ouroboros/ipcp-dev.h> #include <ouroboros/time_utils.h> #include <ouroboros/irm.h> #include <ouroboros/rib.h> -#include <ouroboros/irm_config.h> +#include <ouroboros/hash.h> #include <ouroboros/errno.h> #include "addr_auth.h" @@ -49,37 +50,6 @@ #define THIS_TYPE IPCP_NORMAL -void ipcp_sig_handler(int sig, - siginfo_t * info, - void * c) -{ - (void) c; - - switch(sig) { - case SIGINT: - case SIGTERM: - case SIGHUP: - if (info->si_pid == ipcpi.irmd_api) { - pthread_rwlock_wrlock(&ipcpi.state_lock); - - if (ipcp_get_state() == IPCP_INIT) - ipcp_set_state(IPCP_NULL); - - if (ipcp_get_state() == IPCP_OPERATIONAL) - ipcp_set_state(IPCP_SHUTDOWN); - - pthread_rwlock_unlock(&ipcpi.state_lock); - } - default: - return; - } -} - -/* - * Boots the IPCP off information in the rib. - * Common function after bootstrap or enroll. - * Call under ipcpi.state_lock - */ static int boot_components(void) { char buf[256]; @@ -87,7 +57,7 @@ static int boot_components(void) enum pol_addr_auth pa; char path[RIB_MAX_PATH_LEN + 1]; - len = rib_read(DIF_PATH, &buf, 256); + len = rib_read(BOOT_PATH "/general/dif_name", buf, 256); if (len < 0) { log_err("Failed to read DIF name: %zd.", len); return -1; @@ -99,6 +69,17 @@ static int boot_components(void) return -1; } + len = rib_read(BOOT_PATH "/general/dir_hash_len", + &ipcpi.dir_hash_len, sizeof(ipcpi.dir_hash_len)); + if (len < 0) { + log_err("Failed to read hash length: %zd.", len); + return -1; + } + + ipcpi.dir_hash_len = ntoh16(ipcpi.dir_hash_len); + + assert(ipcpi.dir_hash_len != 0); + if (rib_add(MEMBERS_PATH, ipcpi.name)) { log_err("Failed to add name to " MEMBERS_PATH); return -1; @@ -160,12 +141,11 @@ static int boot_components(void) } if (fmgr_init()) { - log_err("Failed to initialize flow manager component."); frct_fini(); dir_fini(); ribmgr_fini(); addr_auth_fini(); - log_err("Failed to start flow manager."); + log_err("Failed to initialize flow manager component."); return -1; } @@ -229,38 +209,25 @@ void shutdown_components(void) free(ipcpi.dif_name); } -static int normal_ipcp_enroll(char * dst_name) +static int normal_ipcp_enroll(const char * dst) { - pthread_rwlock_wrlock(&ipcpi.state_lock); - - if (ipcp_get_state() != IPCP_INIT) { - pthread_rwlock_unlock(&ipcpi.state_lock); - log_err("IPCP in wrong state."); - return -1; /* -ENOTINIT */ - } - if (rib_add(RIB_ROOT, MEMBERS_NAME)) { - pthread_rwlock_unlock(&ipcpi.state_lock); log_err("Failed to create members."); return -1; } /* Get boot state from peer */ - if (enroll_boot(dst_name)) { - pthread_rwlock_unlock(&ipcpi.state_lock); + if (enroll_boot(dst)) { log_err("Failed to boot IPCP components."); return -1; } if (boot_components()) { - pthread_rwlock_unlock(&ipcpi.state_lock); log_err("Failed to boot IPCP components."); return -1; } - pthread_rwlock_unlock(&ipcpi.state_lock); - - log_dbg("Enrolled with %s.", dst_name); + log_dbg("Enrolled with " HASH_FMT, HASH_VAL(dst)); return 0; } @@ -269,12 +236,17 @@ const struct ros { char * parent; char * child; } ros[] = { - /* GENERAL IPCP INFO */ - {RIB_ROOT, DIF_NAME}, /* BOOT INFO */ {RIB_ROOT, BOOT_NAME}, /* OTHER RIB STRUCTURES */ {RIB_ROOT, MEMBERS_NAME}, + + /* GENERAL IPCP INFO */ + {BOOT_PATH, "general"}, + + {BOOT_PATH "/general", "dif_name"}, + {BOOT_PATH "/general", "dir_hash_len"}, + /* DT COMPONENT */ {BOOT_PATH, "dt"}, @@ -319,28 +291,28 @@ int normal_rib_init(void) return 0; } -static int normal_ipcp_bootstrap(struct dif_config * conf) +static int normal_ipcp_bootstrap(const struct ipcp_config * conf) { + uint16_t hash_len; + assert(conf); assert(conf->type == THIS_TYPE); - pthread_rwlock_wrlock(&ipcpi.state_lock); + hash_len = hton16((uint16_t) conf->dir_hash_len); - if (ipcp_get_state() != IPCP_INIT) { - pthread_rwlock_unlock(&ipcpi.state_lock); - log_err("IPCP in wrong state."); - return -1; /* -ENOTINIT */ - } + assert(ntoh16(hash_len) != 0); if (normal_rib_init()) { - pthread_rwlock_unlock(&ipcpi.state_lock); log_err("Failed to write initial structure to the RIB."); return -1; } - if (rib_write(DIF_PATH, + if (rib_write(BOOT_PATH "/general/dif_name", conf->dif_name, strlen(conf->dif_name) + 1) || + rib_write(BOOT_PATH "/general/dir_hash_len", + &hash_len, + sizeof(hash_len)) || rib_write(BOOT_PATH "/dt/const/addr_size", &conf->addr_size, sizeof(conf->addr_size)) || @@ -375,18 +347,14 @@ static int normal_ipcp_bootstrap(struct dif_config * conf) &conf->addr_auth_type, sizeof(conf->addr_auth_type))) { log_err("Failed to write boot info to RIB."); - pthread_rwlock_unlock(&ipcpi.state_lock); return -1; } if (boot_components()) { log_err("Failed to boot IPCP components."); - pthread_rwlock_unlock(&ipcpi.state_lock); return -1; } - pthread_rwlock_unlock(&ipcpi.state_lock); - log_dbg("Bootstrapped in DIF %s.", conf->dif_name); return 0; @@ -395,9 +363,9 @@ static int normal_ipcp_bootstrap(struct dif_config * conf) static struct ipcp_ops normal_ops = { .ipcp_bootstrap = normal_ipcp_bootstrap, .ipcp_enroll = normal_ipcp_enroll, - .ipcp_name_reg = dir_name_reg, - .ipcp_name_unreg = dir_name_unreg, - .ipcp_name_query = dir_name_query, + .ipcp_reg = dir_reg, + .ipcp_unreg = dir_unreg, + .ipcp_query = dir_query, .ipcp_flow_alloc = fmgr_np1_alloc, .ipcp_flow_alloc_resp = fmgr_np1_alloc_resp, .ipcp_flow_dealloc = fmgr_np1_dealloc @@ -406,27 +374,6 @@ static struct ipcp_ops normal_ops = { int main(int argc, char * argv[]) { - struct sigaction sig_act; - sigset_t sigset; - - sigemptyset(&sigset); - sigaddset(&sigset, SIGINT); - sigaddset(&sigset, SIGQUIT); - sigaddset(&sigset, SIGHUP); - sigaddset(&sigset, SIGPIPE); - - /* init sig_act */ - memset(&sig_act, 0, sizeof(sig_act)); - - /* install signal traps */ - sig_act.sa_sigaction = &ipcp_sig_handler; - sig_act.sa_flags = SA_SIGINFO; - - sigaction(SIGINT, &sig_act, NULL); - sigaction(SIGTERM, &sig_act, NULL); - sigaction(SIGHUP, &sig_act, NULL); - sigaction(SIGPIPE, &sig_act, NULL); - if (ipcp_init(argc, argv, THIS_TYPE, &normal_ops) < 0) { ipcp_create_r(getpid(), -1); exit(EXIT_FAILURE); @@ -466,7 +413,6 @@ int main(int argc, exit(EXIT_FAILURE); } - pthread_sigmask(SIG_BLOCK, &sigset, NULL); if (ipcp_boot() < 0) { log_err("Failed to boot IPCP."); @@ -479,8 +425,6 @@ int main(int argc, exit(EXIT_FAILURE); } - pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); - if (ipcp_create_r(getpid(), 0)) { log_err("Failed to notify IRMd we are initialized."); ipcp_set_state(IPCP_NULL); diff --git a/src/ipcpd/normal/neighbors.h b/src/ipcpd/normal/neighbors.h index 8714a9aa..c958affc 100644 --- a/src/ipcpd/normal/neighbors.h +++ b/src/ipcpd/normal/neighbors.h @@ -23,7 +23,7 @@ #ifndef OUROBOROS_IPCPD_NORMAL_NEIGHBORS_H #define OUROBOROS_IPCPD_NORMAL_NEIGHBORS_H -#include <ouroboros/irm_config.h> +#include <ouroboros/ipcp.h> #include <ouroboros/list.h> #include <ouroboros/qos.h> #include <ouroboros/fqueue.h> diff --git a/src/ipcpd/normal/pol/complete.h b/src/ipcpd/normal/pol/complete.h index 46a535c2..230fecb9 100644 --- a/src/ipcpd/normal/pol/complete.h +++ b/src/ipcpd/normal/pol/complete.h @@ -23,7 +23,7 @@ #ifndef OUROBOROS_IPCPD_NORMAL_POL_COMPLETE_H #define OUROBOROS_IPCPD_NORMAL_POL_COMPLETE_H -#include <ouroboros/irm_config.h> +#include <ouroboros/ipcp.h> #include <ouroboros/qos.h> #include "pol-gam-ops.h" diff --git a/src/ipcpd/normal/ribconfig.h b/src/ipcpd/normal/ribconfig.h index 5ecdaab3..2f5daf72 100644 --- a/src/ipcpd/normal/ribconfig.h +++ b/src/ipcpd/normal/ribconfig.h @@ -29,10 +29,8 @@ #define DLR "/" #define BOOT_NAME "boot" #define MEMBERS_NAME "members" -#define DIF_NAME "dif_name" #define DIR_NAME "directory" #define ROUTING_NAME "fsdb" -#define DIF_PATH DLR DIF_NAME #define DIR_PATH DLR DIR_NAME #define BOOT_PATH DLR BOOT_NAME #define MEMBERS_PATH DLR MEMBERS_NAME diff --git a/src/ipcpd/normal/ribmgr.h b/src/ipcpd/normal/ribmgr.h index 8922688a..83d5ec3a 100644 --- a/src/ipcpd/normal/ribmgr.h +++ b/src/ipcpd/normal/ribmgr.h @@ -23,7 +23,7 @@ #ifndef OUROBOROS_IPCPD_NORMAL_RIBMGR_H #define OUROBOROS_IPCPD_NORMAL_RIBMGR_H -#include <ouroboros/irm_config.h> +#include <ouroboros/ipcp.h> #include <ouroboros/utils.h> #include <ouroboros/qos.h> diff --git a/src/ipcpd/shim-data.c b/src/ipcpd/shim-data.c index eb4ec33f..4459837d 100644 --- a/src/ipcpd/shim-data.c +++ b/src/ipcpd/shim-data.c @@ -34,26 +34,24 @@ struct reg_entry { struct list_head list; - char * name; + uint8_t * hash; }; struct dir_entry { struct list_head list; - char * name; + uint8_t * hash; uint64_t addr; }; -static struct reg_entry * reg_entry_create(char * name) +static struct reg_entry * reg_entry_create(uint8_t * hash) { struct reg_entry * entry = malloc(sizeof(*entry)); if (entry == NULL) return NULL; - assert(name); + assert(hash); - entry->name = name; - if (entry->name == NULL) - return NULL; + entry->hash = hash; return entry; } @@ -62,25 +60,23 @@ static void reg_entry_destroy(struct reg_entry * entry) { assert(entry); - if (entry->name != NULL) - free(entry->name); + if (entry->hash != NULL) + free(entry->hash); free(entry); } -static struct dir_entry * dir_entry_create(char * name, - uint64_t addr) +static struct dir_entry * dir_entry_create(uint8_t * hash, + uint64_t addr) { struct dir_entry * entry = malloc(sizeof(*entry)); if (entry == NULL) return NULL; - assert(name); + assert(hash); - entry->addr = addr; - entry->name = name; - if (entry->name == NULL) - return NULL; + entry->addr = addr; + entry->hash = hash; return entry; } @@ -89,8 +85,8 @@ static void dir_entry_destroy(struct dir_entry * entry) { assert(entry); - if (entry->name != NULL) - free(entry->name); + if (entry->hash != NULL) + free(entry->hash); free(entry); } @@ -181,17 +177,17 @@ void shim_data_destroy(struct shim_data * data) free(data); } -static struct reg_entry * find_reg_entry_by_name(struct shim_data * data, - const char * name) +static struct reg_entry * find_reg_entry_by_hash(struct shim_data * data, + const uint8_t * hash) { struct list_head * h; assert(data); - assert(name); + assert(hash); list_for_each(h, &data->registry) { struct reg_entry * e = list_entry(h, struct reg_entry, list); - if (!strcmp(e->name, name)) + if (!memcmp(e->hash, hash, ipcpi.dir_hash_len)) return e; } @@ -199,13 +195,14 @@ static struct reg_entry * find_reg_entry_by_name(struct shim_data * data, } static struct dir_entry * find_dir_entry(struct shim_data * data, - const char * name, + const uint8_t * hash, uint64_t addr) { struct list_head * h; list_for_each(h, &data->directory) { struct dir_entry * e = list_entry(h, struct dir_entry, list); - if (e->addr == addr && !strcmp(e->name, name)) + if (e->addr == addr && + !memcmp(e->hash, hash, ipcpi.dir_hash_len)) return e; } @@ -213,12 +210,12 @@ static struct dir_entry * find_dir_entry(struct shim_data * data, } static struct dir_entry * find_dir_entry_any(struct shim_data * data, - const char * name) + const uint8_t * hash) { struct list_head * h; list_for_each(h, &data->directory) { struct dir_entry * e = list_entry(h, struct dir_entry, list); - if (!strcmp(e->name, name)) + if (!memcmp(e->hash, hash, ipcpi.dir_hash_len)) return e; } @@ -226,21 +223,21 @@ static struct dir_entry * find_dir_entry_any(struct shim_data * data, } int shim_data_reg_add_entry(struct shim_data * data, - char * name) + uint8_t * hash) { struct reg_entry * entry; - if (data == NULL || name == NULL) - return -1; + assert(data); + assert(hash); pthread_rwlock_wrlock(&data->reg_lock); - if (find_reg_entry_by_name(data, name)) { + if (find_reg_entry_by_hash(data, hash)) { pthread_rwlock_unlock(&data->reg_lock); return -1; } - entry = reg_entry_create(name); + entry = reg_entry_create(hash); if (entry == NULL) { pthread_rwlock_unlock(&data->reg_lock); return -1; @@ -254,7 +251,7 @@ int shim_data_reg_add_entry(struct shim_data * data, } int shim_data_reg_del_entry(struct shim_data * data, - const char * name) + const uint8_t * hash) { struct reg_entry * e; if (data == NULL) @@ -262,7 +259,7 @@ int shim_data_reg_del_entry(struct shim_data * data, pthread_rwlock_wrlock(&data->reg_lock); - e = find_reg_entry_by_name(data, name); + e = find_reg_entry_by_hash(data, hash); if (e == NULL) { pthread_rwlock_unlock(&data->reg_lock); return 0; /* nothing to do */ @@ -278,16 +275,16 @@ int shim_data_reg_del_entry(struct shim_data * data, } bool shim_data_reg_has(struct shim_data * data, - const char * name) + const uint8_t * hash) { bool ret = false; - if (data == NULL || name == NULL) - return false; + assert(data); + assert(hash); pthread_rwlock_rdlock(&data->reg_lock); - ret = (find_reg_entry_by_name(data, name) != NULL); + ret = (find_reg_entry_by_hash(data, hash) != NULL); pthread_rwlock_unlock(&data->reg_lock); @@ -295,29 +292,29 @@ bool shim_data_reg_has(struct shim_data * data, } int shim_data_dir_add_entry(struct shim_data * data, - char * name, + const uint8_t * hash, uint64_t addr) { struct dir_entry * entry; - char * entry_name; + uint8_t * entry_hash; - if (data == NULL || name == NULL) - return -1; + assert(data); + assert(hash); pthread_rwlock_wrlock(&data->dir_lock); - if (find_dir_entry(data, name, addr) != NULL) { + if (find_dir_entry(data, hash, addr) != NULL) { pthread_rwlock_unlock(&data->dir_lock); return -1; } - entry_name = strdup(name); - if (entry_name == NULL) { + entry_hash = ipcp_hash_dup(hash); + if (entry_hash == NULL) { pthread_rwlock_unlock(&data->dir_lock); return -1; } - entry = dir_entry_create(entry_name, addr); + entry = dir_entry_create(entry_hash, addr); if (entry == NULL) { pthread_rwlock_unlock(&data->dir_lock); return -1; @@ -331,7 +328,7 @@ int shim_data_dir_add_entry(struct shim_data * data, } int shim_data_dir_del_entry(struct shim_data * data, - const char * name, + const uint8_t * hash, uint64_t addr) { struct dir_entry * e; @@ -340,7 +337,7 @@ int shim_data_dir_del_entry(struct shim_data * data, pthread_rwlock_wrlock(&data->dir_lock); - e = find_dir_entry(data, name, addr); + e = find_dir_entry(data, hash, addr); if (e == NULL) { pthread_rwlock_unlock(&data->dir_lock); return 0; /* nothing to do */ @@ -356,13 +353,13 @@ int shim_data_dir_del_entry(struct shim_data * data, } bool shim_data_dir_has(struct shim_data * data, - const char * name) + const uint8_t * hash) { bool ret = false; pthread_rwlock_rdlock(&data->dir_lock); - ret = (find_dir_entry_any(data, name) != NULL); + ret = (find_dir_entry_any(data, hash) != NULL); pthread_rwlock_unlock(&data->dir_lock); @@ -370,14 +367,14 @@ bool shim_data_dir_has(struct shim_data * data, } uint64_t shim_data_dir_get_addr(struct shim_data * data, - const char * name) + const uint8_t * hash) { struct dir_entry * entry; uint64_t addr; pthread_rwlock_rdlock(&data->dir_lock); - entry = find_dir_entry_any(data, name); + entry = find_dir_entry_any(data, hash); if (entry == NULL) { pthread_rwlock_unlock(&data->dir_lock); @@ -391,7 +388,7 @@ uint64_t shim_data_dir_get_addr(struct shim_data * data, return addr; } -struct dir_query * shim_data_dir_query_create(char * name) +struct dir_query * shim_data_dir_query_create(const uint8_t * hash) { struct dir_query * query; pthread_condattr_t cattr; @@ -400,8 +397,8 @@ struct dir_query * shim_data_dir_query_create(char * name) if (query == NULL) return NULL; - query->name = strdup(name); - if (query->name == NULL) { + query->hash = ipcp_hash_dup(hash); + if (query->hash == NULL) { free(query); return NULL; } @@ -467,7 +464,7 @@ void shim_data_dir_query_destroy(struct dir_query * query) pthread_cond_destroy(&query->cond); pthread_mutex_destroy(&query->lock); - free(query->name); + free(query->hash); free(query); } diff --git a/src/ipcpd/shim-data.h b/src/ipcpd/shim-data.h index ac670b43..d53373df 100644 --- a/src/ipcpd/shim-data.h +++ b/src/ipcpd/shim-data.h @@ -30,7 +30,6 @@ #include <pthread.h> #include <stdint.h> - enum dir_query_state { QUERY_INIT = 0, QUERY_PENDING, @@ -41,7 +40,7 @@ enum dir_query_state { struct dir_query { struct list_head next; - char * name; + uint8_t * hash; enum dir_query_state state; pthread_mutex_t lock; @@ -49,14 +48,14 @@ struct dir_query { }; struct shim_data { - struct list_head registry; - pthread_rwlock_t reg_lock; + struct list_head registry; + pthread_rwlock_t reg_lock; - struct list_head directory; - pthread_rwlock_t dir_lock; + struct list_head directory; + pthread_rwlock_t dir_lock; - struct list_head dir_queries; - pthread_mutex_t dir_queries_lock; + struct list_head dir_queries; + pthread_mutex_t dir_queries_lock; }; struct shim_data * shim_data_create(void); @@ -64,29 +63,29 @@ struct shim_data * shim_data_create(void); void shim_data_destroy(struct shim_data * data); int shim_data_reg_add_entry(struct shim_data * data, - char * name); + uint8_t * hash); int shim_data_reg_del_entry(struct shim_data * data, - const char * name); + const uint8_t * hash); bool shim_data_reg_has(struct shim_data * data, - const char * name); + const uint8_t * hash); int shim_data_dir_add_entry(struct shim_data * data, - char * name, + const uint8_t * hash, uint64_t addr); int shim_data_dir_del_entry(struct shim_data * data, - const char * name, + const uint8_t * hash, uint64_t addr); bool shim_data_dir_has(struct shim_data * data, - const char * name); + const uint8_t * hash); uint64_t shim_data_dir_get_addr(struct shim_data * data, - const char * name); + const uint8_t * hash); -struct dir_query * shim_data_dir_query_create(char * name); +struct dir_query * shim_data_dir_query_create(const uint8_t * hash); void shim_data_dir_query_respond(struct dir_query * query); diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c index 27456eb7..9a9e11b5 100644 --- a/src/ipcpd/shim-eth-llc/main.c +++ b/src/ipcpd/shim-eth-llc/main.c @@ -34,6 +34,7 @@ #include <ouroboros/fqueue.h> #include <ouroboros/logs.h> #include <ouroboros/time_utils.h> +#include <ouroboros/hash.h> #include "ipcp.h" #include "shim_eth_llc_messages.pb-c.h" @@ -248,11 +249,11 @@ static uint8_t reverse_bits(uint8_t b) return b; } -static int eth_llc_ipcp_send_frame(uint8_t * dst_addr, - uint8_t dsap, - uint8_t ssap, - uint8_t * payload, - size_t len) +static int eth_llc_ipcp_send_frame(const uint8_t * dst_addr, + uint8_t dsap, + uint8_t ssap, + const uint8_t * payload, + size_t len) { uint32_t frame_len = 0; uint8_t cf = 0x03; @@ -311,7 +312,7 @@ static int eth_llc_ipcp_send_frame(uint8_t * dst_addr, } static int eth_llc_ipcp_send_mgmt_frame(shim_eth_llc_msg_t * msg, - uint8_t * dst_addr) + const uint8_t * dst_addr) { size_t len; uint8_t * buf; @@ -338,17 +339,19 @@ static int eth_llc_ipcp_send_mgmt_frame(shim_eth_llc_msg_t * msg, return 0; } -static int eth_llc_ipcp_sap_alloc(uint8_t * dst_addr, - uint8_t ssap, - char * dst_name, - qoscube_t cube) +static int eth_llc_ipcp_sap_alloc(const uint8_t * dst_addr, + uint8_t ssap, + const uint8_t * hash, + qoscube_t cube) { shim_eth_llc_msg_t msg = SHIM_ETH_LLC_MSG__INIT; msg.code = SHIM_ETH_LLC_MSG_CODE__FLOW_REQ; msg.has_ssap = true; msg.ssap = ssap; - msg.dst_name = dst_name; + msg.has_hash = true; + msg.hash.len = ipcpi.dir_hash_len; + msg.hash.data = (uint8_t *) hash; msg.has_qoscube = true; msg.qoscube = cube; @@ -373,10 +376,10 @@ static int eth_llc_ipcp_sap_alloc_resp(uint8_t * dst_addr, return eth_llc_ipcp_send_mgmt_frame(&msg, dst_addr); } -static int eth_llc_ipcp_sap_req(uint8_t r_sap, - uint8_t * r_addr, - char * dst_name, - qoscube_t cube) +static int eth_llc_ipcp_sap_req(uint8_t r_sap, + uint8_t * r_addr, + const uint8_t * dst, + qoscube_t cube) { struct timespec ts = {0, EVENT_WAIT_TIMEOUT * 1000}; int fd; @@ -395,7 +398,7 @@ static int eth_llc_ipcp_sap_req(uint8_t r_sap, } /* reply to IRM, called under lock to prevent race */ - fd = ipcp_flow_req_arr(getpid(), dst_name, cube); + fd = ipcp_flow_req_arr(getpid(), dst, ipcpi.dir_hash_len, cube); if (fd < 0) { pthread_mutex_unlock(&ipcpi.alloc_lock); log_err("Could not get new flow from IRMd."); @@ -453,14 +456,16 @@ static int eth_llc_ipcp_sap_alloc_reply(uint8_t ssap, } -static int eth_llc_ipcp_name_query_req(char * name, - uint8_t * r_addr) +static int eth_llc_ipcp_name_query_req(const uint8_t * hash, + uint8_t * r_addr) { shim_eth_llc_msg_t msg = SHIM_ETH_LLC_MSG__INIT; - if (shim_data_reg_has(ipcpi.shim_data, name)) { - msg.code = SHIM_ETH_LLC_MSG_CODE__NAME_QUERY_REPLY; - msg.dst_name = name; + if (shim_data_reg_has(ipcpi.shim_data, hash)) { + msg.code = SHIM_ETH_LLC_MSG_CODE__NAME_QUERY_REPLY; + msg.has_hash = true; + msg.hash.len = ipcpi.dir_hash_len; + msg.hash.data = (uint8_t *) hash; eth_llc_ipcp_send_mgmt_frame(&msg, r_addr); } @@ -468,21 +473,21 @@ static int eth_llc_ipcp_name_query_req(char * name, return 0; } -static int eth_llc_ipcp_name_query_reply(char * name, - uint8_t * r_addr) +static int eth_llc_ipcp_name_query_reply(const uint8_t * hash, + uint8_t * r_addr) { uint64_t address = 0; struct list_head * pos; memcpy(&address, r_addr, MAC_SIZE); - shim_data_dir_add_entry(ipcpi.shim_data, name, address); + shim_data_dir_add_entry(ipcpi.shim_data, hash, address); pthread_mutex_lock(&ipcpi.shim_data->dir_queries_lock); list_for_each(pos, &ipcpi.shim_data->dir_queries) { struct dir_query * e = list_entry(pos, struct dir_query, next); - if (strcmp(e->name, name) == 0) { + if (memcmp(e->hash, hash, ipcpi.dir_hash_len) == 0) { shim_data_dir_query_respond(e); } } @@ -491,9 +496,9 @@ static int eth_llc_ipcp_name_query_reply(char * name, return 0; } -static int eth_llc_ipcp_mgmt_frame(uint8_t * buf, - size_t len, - uint8_t * r_addr) +static int eth_llc_ipcp_mgmt_frame(const uint8_t * buf, + size_t len, + uint8_t * r_addr) { shim_eth_llc_msg_t * msg; @@ -505,10 +510,10 @@ static int eth_llc_ipcp_mgmt_frame(uint8_t * buf, switch (msg->code) { case SHIM_ETH_LLC_MSG_CODE__FLOW_REQ: - if (shim_data_reg_has(ipcpi.shim_data, msg->dst_name)) { + if (shim_data_reg_has(ipcpi.shim_data, msg->hash.data)) { eth_llc_ipcp_sap_req(msg->ssap, r_addr, - msg->dst_name, + msg->hash.data, msg->qoscube); } break; @@ -519,10 +524,10 @@ static int eth_llc_ipcp_mgmt_frame(uint8_t * buf, msg->response); break; case SHIM_ETH_LLC_MSG_CODE__NAME_QUERY_REQ: - eth_llc_ipcp_name_query_req(msg->dst_name, r_addr); + eth_llc_ipcp_name_query_req(msg->hash.data, r_addr); break; case SHIM_ETH_LLC_MSG_CODE__NAME_QUERY_REPLY: - eth_llc_ipcp_name_query_reply(msg->dst_name, r_addr); + eth_llc_ipcp_name_query_reply(msg->hash.data, r_addr); break; default: log_err("Unknown message received %d.", msg->code); @@ -734,29 +739,7 @@ static void * eth_llc_ipcp_sdu_writer(void * o) return (void *) 1; } -void ipcp_sig_handler(int sig, - siginfo_t * info, - void * c) -{ - (void) c; - - switch(sig) { - case SIGINT: - case SIGTERM: - case SIGHUP: - if (info->si_pid == ipcpi.irmd_api) { - if (ipcp_get_state() == IPCP_INIT) - ipcp_set_state(IPCP_NULL); - - if (ipcp_get_state() == IPCP_OPERATIONAL) - ipcp_set_state(IPCP_SHUTDOWN); - } - default: - return; - } -} - -static int eth_llc_ipcp_bootstrap(struct dif_config * conf) +static int eth_llc_ipcp_bootstrap(const struct ipcp_config * conf) { int idx; struct ifreq ifr; @@ -777,10 +760,7 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) assert(conf); assert(conf->type == THIS_TYPE); - if (ipcp_get_state() != IPCP_INIT) { - log_err("IPCP in wrong state."); - return -1; - } + ipcpi.dir_hash_len = conf->dir_hash_len; if (conf->if_name == NULL) { log_err("Interface name is NULL."); @@ -921,35 +901,36 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) return 0; } -static int eth_llc_ipcp_name_reg(char * name) +static int eth_llc_ipcp_reg(const uint8_t * hash) { - char * name_dup; + uint8_t * hash_dup; - name_dup = strdup(name); - if (name_dup == NULL) { - log_err("Failed to duplicate name."); + hash_dup = ipcp_hash_dup(hash); + if (hash_dup == NULL) { + log_err("Failed to duplicate hash."); return -ENOMEM; } - if (shim_data_reg_add_entry(ipcpi.shim_data, name_dup)) { - log_err("Failed to add %s to local registry.", name); - free(name_dup); + if (shim_data_reg_add_entry(ipcpi.shim_data, hash_dup)) { + log_err("Failed to add " HASH_FMT " to local registry.", + HASH_VAL(hash)); + free(hash_dup); return -1; } - log_dbg("Registered %s.", name); + log_dbg("Registered " HASH_FMT ".", HASH_VAL(hash)); return 0; } -static int eth_llc_ipcp_name_unreg(char * name) +static int eth_llc_ipcp_unreg(const uint8_t * hash) { - shim_data_reg_del_entry(ipcpi.shim_data, name); + shim_data_reg_del_entry(ipcpi.shim_data, hash); return 0; } -static int eth_llc_ipcp_name_query(char * name) +static int eth_llc_ipcp_query(const uint8_t * hash) { uint8_t r_addr[MAC_SIZE]; struct timespec timeout = {(NAME_QUERY_TIMEOUT / 1000), @@ -958,15 +939,17 @@ static int eth_llc_ipcp_name_query(char * name) struct dir_query * query; int ret; - if (shim_data_dir_has(ipcpi.shim_data, name)) + if (shim_data_dir_has(ipcpi.shim_data, hash)) return 0; - msg.code = SHIM_ETH_LLC_MSG_CODE__NAME_QUERY_REQ; - msg.dst_name = name; + msg.code = SHIM_ETH_LLC_MSG_CODE__NAME_QUERY_REQ; + msg.has_hash = true; + msg.hash.len = ipcpi.dir_hash_len; + msg.hash.data = (uint8_t *) hash; memset(r_addr, 0xff, MAC_SIZE); - query = shim_data_dir_query_create(name); + query = shim_data_dir_query_create(hash); if (query == NULL) return -1; @@ -986,34 +969,28 @@ static int eth_llc_ipcp_name_query(char * name) return ret; } -static int eth_llc_ipcp_flow_alloc(int fd, - char * dst_name, - qoscube_t cube) +static int eth_llc_ipcp_flow_alloc(int fd, + const uint8_t * hash, + qoscube_t cube) { uint8_t ssap = 0; uint8_t r_addr[MAC_SIZE]; uint64_t addr = 0; - log_dbg("Allocating flow to %s.", dst_name); + log_dbg("Allocating flow to " HASH_FMT ".", HASH_VAL(hash)); - if (dst_name == NULL) - return -1; + assert(hash); if (cube != QOS_CUBE_BE && cube != QOS_CUBE_FRC) { log_dbg("Unsupported QoS requested."); return -1; } - if (ipcp_get_state() != IPCP_OPERATIONAL) { - log_dbg("Won't allocate flow with non-enrolled IPCP."); - return -1; /* -ENOTENROLLED */ - } - - if (!shim_data_dir_has(ipcpi.shim_data, dst_name)) { + if (!shim_data_dir_has(ipcpi.shim_data, hash)) { log_err("Destination unreachable."); return -1; } - addr = shim_data_dir_get_addr(ipcpi.shim_data, dst_name); + addr = shim_data_dir_get_addr(ipcpi.shim_data, hash); pthread_rwlock_wrlock(ð_llc_data.flows_lock); @@ -1030,7 +1007,7 @@ static int eth_llc_ipcp_flow_alloc(int fd, memcpy(r_addr, &addr, MAC_SIZE); - if (eth_llc_ipcp_sap_alloc(r_addr, ssap, dst_name, cube) < 0) { + if (eth_llc_ipcp_sap_alloc(r_addr, ssap, hash, cube) < 0) { pthread_rwlock_wrlock(ð_llc_data.flows_lock); bmp_release(eth_llc_data.saps, eth_llc_data.fd_to_ef[fd].sap); eth_llc_data.fd_to_ef[fd].sap = -1; @@ -1107,11 +1084,6 @@ static int eth_llc_ipcp_flow_dealloc(int fd) ipcp_flow_fini(fd); - if (ipcp_get_state() != IPCP_OPERATIONAL) { - log_dbg("Won't register with non-enrolled IPCP."); - return -1; /* -ENOTENROLLED */ - } - pthread_rwlock_wrlock(ð_llc_data.flows_lock); flow_set_del(eth_llc_data.np1_flows, fd); @@ -1137,9 +1109,9 @@ static int eth_llc_ipcp_flow_dealloc(int fd) static struct ipcp_ops eth_llc_ops = { .ipcp_bootstrap = eth_llc_ipcp_bootstrap, .ipcp_enroll = NULL, - .ipcp_name_reg = eth_llc_ipcp_name_reg, - .ipcp_name_unreg = eth_llc_ipcp_name_unreg, - .ipcp_name_query = eth_llc_ipcp_name_query, + .ipcp_reg = eth_llc_ipcp_reg, + .ipcp_unreg = eth_llc_ipcp_unreg, + .ipcp_query = eth_llc_ipcp_query, .ipcp_flow_alloc = eth_llc_ipcp_flow_alloc, .ipcp_flow_alloc_resp = eth_llc_ipcp_flow_alloc_resp, .ipcp_flow_dealloc = eth_llc_ipcp_flow_dealloc @@ -1148,27 +1120,6 @@ static struct ipcp_ops eth_llc_ops = { int main(int argc, char * argv[]) { - struct sigaction sig_act; - sigset_t sigset; - - sigemptyset(&sigset); - sigaddset(&sigset, SIGINT); - sigaddset(&sigset, SIGQUIT); - sigaddset(&sigset, SIGHUP); - sigaddset(&sigset, SIGPIPE); - - /* init sig_act */ - memset(&sig_act, 0, sizeof(sig_act)); - - /* install signal traps */ - sig_act.sa_sigaction = &ipcp_sig_handler; - sig_act.sa_flags = SA_SIGINFO; - - sigaction(SIGINT, &sig_act, NULL); - sigaction(SIGTERM, &sig_act, NULL); - sigaction(SIGHUP, &sig_act, NULL); - sigaction(SIGPIPE, &sig_act, NULL); - if (ipcp_init(argc, argv, THIS_TYPE, ð_llc_ops) < 0) { ipcp_create_r(getpid(), -1); exit(EXIT_FAILURE); @@ -1181,9 +1132,6 @@ int main(int argc, exit(EXIT_FAILURE); } - - pthread_sigmask(SIG_BLOCK, &sigset, NULL); - if (ipcp_boot() < 0) { log_err("Failed to boot IPCP."); ipcp_create_r(getpid(), -1); @@ -1192,8 +1140,6 @@ int main(int argc, exit(EXIT_FAILURE); } - pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); - if (ipcp_create_r(getpid(), 0)) { log_err("Failed to notify IRMd we are initialized."); ipcp_set_state(IPCP_NULL); diff --git a/src/ipcpd/shim-eth-llc/shim_eth_llc_messages.proto b/src/ipcpd/shim-eth-llc/shim_eth_llc_messages.proto index 2d66428c..09281698 100644 --- a/src/ipcpd/shim-eth-llc/shim_eth_llc_messages.proto +++ b/src/ipcpd/shim-eth-llc/shim_eth_llc_messages.proto @@ -31,7 +31,7 @@ enum shim_eth_llc_msg_code { message shim_eth_llc_msg { required shim_eth_llc_msg_code code = 1; - optional string dst_name = 2; + optional bytes hash = 2; optional uint32 ssap = 3; optional uint32 dsap = 4; optional uint32 qoscube = 5; diff --git a/src/ipcpd/shim-udp/main.c b/src/ipcpd/shim-udp/main.c index b1a88fae..e1fe5c7c 100644 --- a/src/ipcpd/shim-udp/main.c +++ b/src/ipcpd/shim-udp/main.c @@ -30,6 +30,7 @@ #include <ouroboros/fqueue.h> #include <ouroboros/errno.h> #include <ouroboros/logs.h> +#include <ouroboros/hash.h> #include "shim_udp_messages.pb-c.h" #include "ipcp.h" @@ -197,14 +198,16 @@ static int send_shim_udp_msg(shim_udp_msg_t * msg, static int ipcp_udp_port_alloc(uint32_t dst_ip_addr, uint16_t src_udp_port, - char * dst_name, + const uint8_t * dst, qoscube_t cube) { shim_udp_msg_t msg = SHIM_UDP_MSG__INIT; msg.code = SHIM_UDP_MSG_CODE__FLOW_REQ; msg.src_udp_port = src_udp_port; - msg.dst_name = dst_name; + msg.has_hash = true; + msg.hash.len = ipcpi.dir_hash_len; + msg.hash.data = (uint8_t *) dst; msg.has_qoscube = true; msg.qoscube = cube; @@ -229,7 +232,7 @@ static int ipcp_udp_port_alloc_resp(uint32_t dst_ip_addr, } static int ipcp_udp_port_req(struct sockaddr_in * c_saddr, - char * dst_name, + const uint8_t * dst, qoscube_t cube) { struct timespec ts = {0, FD_UPDATE_TIMEOUT * 1000}; @@ -283,7 +286,7 @@ static int ipcp_udp_port_req(struct sockaddr_in * c_saddr, } /* reply to IRM */ - fd = ipcp_flow_req_arr(getpid(), dst_name, cube); + fd = ipcp_flow_req_arr(getpid(), dst, ipcpi.dir_hash_len, cube); if (fd < 0) { pthread_mutex_unlock(&ipcpi.alloc_lock); log_err("Could not get new flow from IRMd."); @@ -291,7 +294,6 @@ static int ipcp_udp_port_req(struct sockaddr_in * c_saddr, return -1; } - pthread_rwlock_rdlock(&ipcpi.state_lock); pthread_rwlock_wrlock(&udp_data.flows_lock); udp_data.uf_to_fd[skfd] = fd; @@ -299,7 +301,6 @@ static int ipcp_udp_port_req(struct sockaddr_in * c_saddr, udp_data.fd_to_uf[fd].udp = f_saddr.sin_port; pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); pthread_mutex_unlock(&ipcpi.alloc_lock); ipcpi.alloc_id = fd; @@ -337,14 +338,12 @@ static int ipcp_udp_port_alloc_reply(uint16_t src_udp_port, log_dbg("Received reply for flow on udp port %d.", ntohs(dst_udp_port)); - pthread_rwlock_rdlock(&ipcpi.state_lock); pthread_rwlock_rdlock(&udp_data.flows_lock); fd = udp_port_to_fd(dst_udp_port); skfd = udp_data.fd_to_uf[fd].skfd; pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); /* get the original address with the LISTEN PORT */ if (getpeername(skfd, (struct sockaddr *) &t_saddr, &t_saddr_len) < 0) { @@ -360,13 +359,11 @@ static int ipcp_udp_port_alloc_reply(uint16_t src_udp_port, return -1; } - pthread_rwlock_rdlock(&ipcpi.state_lock); pthread_rwlock_rdlock(&udp_data.flows_lock); set_fd(skfd); pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); if (ipcp_flow_alloc_reply(fd, response) < 0) return -1; @@ -410,7 +407,7 @@ static void * ipcp_udp_listener(void * o) case SHIM_UDP_MSG_CODE__FLOW_REQ: c_saddr.sin_port = msg->src_udp_port; ipcp_udp_port_req(&c_saddr, - msg->dst_name, + msg->hash.data, msg->qoscube); break; case SHIM_UDP_MSG_CODE__FLOW_REPLY: @@ -447,7 +444,6 @@ static void * ipcp_udp_sdu_reader(void * o) (void) o; while (true) { - pthread_rwlock_rdlock(&ipcpi.state_lock); pthread_rwlock_rdlock(&udp_data.flows_lock); pthread_mutex_lock(&udp_data.fd_set_lock); @@ -457,7 +453,6 @@ static void * ipcp_udp_sdu_reader(void * o) pthread_mutex_unlock(&udp_data.fd_set_lock); pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); if (select(FD_SETSIZE, &read_fds, NULL, NULL, &tv) <= 0) continue; @@ -476,13 +471,11 @@ static void * ipcp_udp_sdu_reader(void * o) (unsigned *) &n)) <= 0) continue; - pthread_rwlock_rdlock(&ipcpi.state_lock); pthread_rwlock_rdlock(&udp_data.flows_lock); fd = udp_data.uf_to_fd[skfd]; pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); flow_write(fd, buf, n); } @@ -506,12 +499,10 @@ static void * ipcp_udp_sdu_loop(void * o) continue; } - pthread_rwlock_rdlock(&ipcpi.state_lock); if (ipcp_get_state() != IPCP_OPERATIONAL) { - pthread_rwlock_unlock(&ipcpi.state_lock); ipcp_flow_del(sdb); - return (void *) -1; /* -ENOTENROLLED */ + return (void *) 0; /* -ENOTENROLLED */ } pthread_rwlock_rdlock(&udp_data.flows_lock); @@ -519,7 +510,6 @@ static void * ipcp_udp_sdu_loop(void * o) fd = udp_data.fd_to_uf[fd].skfd; pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); if (send(fd, shm_du_buff_head(sdb), shm_du_buff_tail(sdb) - shm_du_buff_head(sdb), @@ -533,33 +523,7 @@ static void * ipcp_udp_sdu_loop(void * o) return (void *) 1; } -void ipcp_sig_handler(int sig, - siginfo_t * info, - void * c) -{ - (void) c; - - switch(sig) { - case SIGINT: - case SIGTERM: - case SIGHUP: - if (info->si_pid == ipcpi.irmd_api) { - pthread_rwlock_wrlock(&ipcpi.state_lock); - - if (ipcp_get_state() == IPCP_INIT) - ipcp_set_state(IPCP_NULL); - - if (ipcp_get_state() == IPCP_OPERATIONAL) - ipcp_set_state(IPCP_SHUTDOWN); - - pthread_rwlock_unlock(&ipcpi.state_lock); - } - default: - return; - } -} - -static int ipcp_udp_bootstrap(struct dif_config * conf) +static int ipcp_udp_bootstrap(const struct ipcp_config * conf) { struct sockaddr_in s_saddr; char ipstr[INET_ADDRSTRLEN]; @@ -570,6 +534,8 @@ static int ipcp_udp_bootstrap(struct dif_config * conf) assert(conf); assert(conf->type == THIS_TYPE); + ipcpi.dir_hash_len = conf->dir_hash_len; + if (inet_ntop(AF_INET, &conf->ip_addr, ipstr, @@ -619,15 +585,6 @@ static int ipcp_udp_bootstrap(struct dif_config * conf) return -1; } - pthread_rwlock_wrlock(&ipcpi.state_lock); - - if (ipcp_get_state() != IPCP_INIT) { - pthread_rwlock_unlock(&ipcpi.state_lock); - log_err("IPCP in wrong state."); - close(fd); - return -1; - } - udp_data.s_fd = fd; udp_data.ip_addr = conf->ip_addr; udp_data.dns_addr = conf->dns_addr; @@ -650,8 +607,6 @@ static int ipcp_udp_bootstrap(struct dif_config * conf) ipcp_udp_sdu_loop, NULL); - pthread_rwlock_unlock(&ipcpi.state_lock); - log_dbg("Bootstrapped shim IPCP over UDP with api %d.", getpid()); log_dbg("Bound to IP address %s.", ipstr); log_dbg("DNS server address is %s.", dnsstr); @@ -784,7 +739,7 @@ static uint32_t ddns_resolve(char * name, } #endif -static int ipcp_udp_name_reg(char * name) +static int ipcp_udp_reg(const uint8_t * hash) { #ifdef CONFIG_OUROBOROS_ENABLE_DNS char ipstr[INET_ADDRSTRLEN]; @@ -794,25 +749,23 @@ static int ipcp_udp_name_reg(char * name) uint32_t dns_addr; uint32_t ip_addr; #endif - char * name_dup; + char hashstr[DIR_HASH_STRLEN + 1]; + uint8_t * hash_dup; - if (strlen(name) > 24) { - log_err("DNS names cannot be longer than 24 chars."); - return -1; - } + assert(hash); - name_dup = strdup(name); - if (name_dup == NULL) { - log_err("Failed to duplicate name."); + ipcp_hash_str(hashstr, hash); + + hash_dup = ipcp_hash_dup(hash); + if (hash_dup == NULL) { + log_err("Failed to duplicate hash."); return -ENOMEM; } - pthread_rwlock_rdlock(&ipcpi.state_lock); - - if (shim_data_reg_add_entry(ipcpi.shim_data, name_dup)) { - pthread_rwlock_unlock(&ipcpi.state_lock); - log_err("Failed to add %s to local registry.", name); - free(name_dup); + if (shim_data_reg_add_entry(ipcpi.shim_data, hash_dup)) { + log_err("Failed to add " HASH_FMT " to local registry.", + HASH_VAL(hash)); + free(hash_dup); return -1; } @@ -821,8 +774,6 @@ static int ipcp_udp_name_reg(char * name) dns_addr = udp_data.dns_addr; - pthread_rwlock_unlock(&ipcpi.state_lock); - if (dns_addr != 0) { ip_addr = udp_data.ip_addr; @@ -837,24 +788,20 @@ static int ipcp_udp_name_reg(char * name) } sprintf(cmd, "server %s\nupdate add %s %d A %s\nsend\nquit\n", - dnsstr, name, DNS_TTL, ipstr); + dnsstr, hashstr, DNS_TTL, ipstr); if (ddns_send(cmd)) { - pthread_rwlock_rdlock(&ipcpi.state_lock); - shim_data_reg_del_entry(ipcpi.shim_data, name); - pthread_rwlock_unlock(&ipcpi.state_lock); + shim_data_reg_del_entry(ipcpi.shim_data, hash_dup); return -1; } } -#else - pthread_rwlock_unlock(&ipcpi.state_lock); #endif - log_dbg("Registered %s.", name); + log_dbg("Registered " HASH_FMT ".", HASH_VAL(hash)); return 0; } -static int ipcp_udp_name_unreg(char * name) +static int ipcp_udp_unreg(const uint8_t * hash) { #ifdef CONFIG_OUROBOROS_ENABLE_DNS char dnsstr[INET_ADDRSTRLEN]; @@ -862,94 +809,66 @@ static int ipcp_udp_name_unreg(char * name) char cmd[100]; uint32_t dns_addr; #endif - if (strlen(name) > 24) { - log_err("DNS names cannot be longer than 24 chars."); - return -1; - } + char hashstr[DIR_HASH_STRLEN + 1]; + + assert(hash); + + ipcp_hash_str(hashstr, hash); #ifdef CONFIG_OUROBOROS_ENABLE_DNS /* unregister application with DNS server */ - pthread_rwlock_rdlock(&ipcpi.state_lock); - dns_addr = udp_data.dns_addr; - pthread_rwlock_unlock(&ipcpi.state_lock); - if (dns_addr != 0) { if (inet_ntop(AF_INET, &dns_addr, dnsstr, INET_ADDRSTRLEN) == NULL) { return -1; } sprintf(cmd, "server %s\nupdate delete %s A\nsend\nquit\n", - dnsstr, name); + dnsstr, hashstr); ddns_send(cmd); } #endif - pthread_rwlock_rdlock(&ipcpi.state_lock); - - shim_data_reg_del_entry(ipcpi.shim_data, name); + shim_data_reg_del_entry(ipcpi.shim_data, hash); - pthread_rwlock_unlock(&ipcpi.state_lock); + log_dbg("Unregistered " HASH_FMT ".", HASH_VAL(hash)); return 0; } -static int ipcp_udp_name_query(char * name) +static int ipcp_udp_query(const uint8_t * hash) { uint32_t ip_addr = 0; struct hostent * h; #ifdef CONFIG_OUROBOROS_ENABLE_DNS uint32_t dns_addr = 0; #endif + char hashstr[DIR_HASH_STRLEN + 1]; - assert(name); - - if (strlen(name) > 24) { - log_err("DNS names cannot be longer than 24 chars."); - return -1; - } - - pthread_rwlock_rdlock(&ipcpi.state_lock); + assert(hash); - if (ipcp_get_state() != IPCP_OPERATIONAL) { - pthread_rwlock_unlock(&ipcpi.state_lock); - log_dbg("Won't query a name on a non-enrolled IPCP."); - return -1; /* -ENOTENROLLED */ - } + ipcp_hash_str(hashstr, hash); - if (shim_data_dir_has(ipcpi.shim_data, name)) { - pthread_rwlock_unlock(&ipcpi.state_lock); + if (shim_data_dir_has(ipcpi.shim_data, hash)) return 0; - } #ifdef CONFIG_OUROBOROS_ENABLE_DNS dns_addr = udp_data.dns_addr; if (dns_addr != 0) { - pthread_rwlock_unlock(&ipcpi.state_lock); - - ip_addr = ddns_resolve(name, dns_addr); + ip_addr = ddns_resolve(hashstr, dns_addr); if (ip_addr == 0) { - log_dbg("Could not resolve %s.", name); + log_dbg("Could not resolve %s.", hashstr); return -1; } - - pthread_rwlock_rdlock(&ipcpi.state_lock); - - if (ipcp_get_state() != IPCP_OPERATIONAL) { - pthread_rwlock_unlock(&ipcpi.state_lock); - log_dbg("Won't add name to the directory."); - return -1; /* -ENOTENROLLED */ - } } else { #endif - h = gethostbyname(name); + h = gethostbyname(hashstr); if (h == NULL) { - pthread_rwlock_unlock(&ipcpi.state_lock); - log_dbg("Could not resolve %s.", name); + log_dbg("Could not resolve %s.", hashstr); return -1; } @@ -958,20 +877,17 @@ static int ipcp_udp_name_query(char * name) } #endif - if (shim_data_dir_add_entry(ipcpi.shim_data, name, ip_addr)) { - pthread_rwlock_unlock(&ipcpi.state_lock); + if (shim_data_dir_add_entry(ipcpi.shim_data, hash, ip_addr)) { log_err("Failed to add directory entry."); return -1; } - pthread_rwlock_unlock(&ipcpi.state_lock); - return 0; } -static int ipcp_udp_flow_alloc(int fd, - char * dst_name, - qoscube_t cube) +static int ipcp_udp_flow_alloc(int fd, + const uint8_t * dst, + qoscube_t cube) { struct sockaddr_in r_saddr; /* server address */ struct sockaddr_in f_saddr; /* flow */ @@ -979,14 +895,9 @@ static int ipcp_udp_flow_alloc(int fd, int skfd; uint32_t ip_addr = 0; - log_dbg("Allocating flow to %s.", dst_name); + log_dbg("Allocating flow to " HASH_FMT ".", HASH_VAL(dst)); - assert(dst_name); - - if (strlen(dst_name) > 255) { - log_err("Name too long for this shim."); - return -1; - } + assert(dst); if (cube != QOS_CUBE_BE && cube != QOS_CUBE_FRC) { log_dbg("Unsupported QoS requested."); @@ -1012,22 +923,13 @@ static int ipcp_udp_flow_alloc(int fd, return -1; } - pthread_rwlock_rdlock(&ipcpi.state_lock); - if (ipcp_get_state() != IPCP_OPERATIONAL) { - pthread_rwlock_unlock(&ipcpi.state_lock); - log_dbg("Won't allocate flow with non-enrolled IPCP."); - close(skfd); - return -1; /* -ENOTENROLLED */ - } - - if (!shim_data_dir_has(ipcpi.shim_data, dst_name)) { - pthread_rwlock_unlock(&ipcpi.state_lock); + if (!shim_data_dir_has(ipcpi.shim_data, dst)) { log_dbg("Could not resolve destination."); close(skfd); return -1; } - ip_addr = (uint32_t) shim_data_dir_get_addr(ipcpi.shim_data, dst_name); + ip_addr = (uint32_t) shim_data_dir_get_addr(ipcpi.shim_data, dst); /* connect to server (store the remote IP address in the fd) */ memset((char *) &r_saddr, 0, sizeof(r_saddr)); @@ -1049,13 +951,11 @@ static int ipcp_udp_flow_alloc(int fd, flow_set_add(udp_data.np1_flows, fd); pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); if (ipcp_udp_port_alloc(ip_addr, f_saddr.sin_port, - dst_name, + dst, cube) < 0) { - pthread_rwlock_rdlock(&ipcpi.state_lock); pthread_rwlock_wrlock(&udp_data.flows_lock); udp_data.fd_to_uf[fd].udp = -1; @@ -1063,7 +963,6 @@ static int ipcp_udp_flow_alloc(int fd, udp_data.uf_to_fd[skfd] = -1; pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); close(skfd); return -1; } @@ -1103,11 +1002,12 @@ static int ipcp_udp_flow_alloc_resp(int fd, pthread_mutex_unlock(&ipcpi.alloc_lock); - pthread_rwlock_rdlock(&ipcpi.state_lock); - pthread_rwlock_wrlock(&udp_data.flows_lock); + pthread_rwlock_rdlock(&udp_data.flows_lock); skfd = udp_data.fd_to_uf[fd].skfd; + pthread_rwlock_unlock(&udp_data.flows_lock); + if (getsockname(skfd, (struct sockaddr *) &f_saddr, &len) < 0) { log_dbg("Socket with fd %d has no address.", skfd); return -1; @@ -1118,7 +1018,6 @@ static int ipcp_udp_flow_alloc_resp(int fd, return -1; } - pthread_rwlock_unlock(&udp_data.flows_lock); pthread_rwlock_rdlock(&udp_data.flows_lock); set_fd(skfd); @@ -1126,18 +1025,12 @@ static int ipcp_udp_flow_alloc_resp(int fd, flow_set_add(udp_data.np1_flows, fd); pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); - if (ipcp_udp_port_alloc_resp(r_saddr.sin_addr.s_addr, - f_saddr.sin_port, - r_saddr.sin_port, - response) < 0) { - pthread_rwlock_rdlock(&ipcpi.state_lock); + if (ipcp_udp_port_alloc_resp(r_saddr.sin_addr.s_addr, f_saddr.sin_port, + r_saddr.sin_port, response) < 0) { pthread_rwlock_rdlock(&udp_data.flows_lock); clr_fd(skfd); pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); - return -1; } @@ -1153,14 +1046,6 @@ static int ipcp_udp_flow_dealloc(int fd) ipcp_flow_fini(fd); - pthread_rwlock_rdlock(&ipcpi.state_lock); - - if (ipcp_get_state() != IPCP_OPERATIONAL) { - pthread_rwlock_unlock(&ipcpi.state_lock); - log_dbg("Won't register with non-enrolled IPCP."); - return -1; /* -ENOTENROLLED */ - } - pthread_rwlock_wrlock(&udp_data.flows_lock); flow_set_del(udp_data.np1_flows, fd); @@ -1179,7 +1064,6 @@ static int ipcp_udp_flow_dealloc(int fd) clr_fd(skfd); pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); flow_dealloc(fd); @@ -1191,9 +1075,9 @@ static int ipcp_udp_flow_dealloc(int fd) static struct ipcp_ops udp_ops = { .ipcp_bootstrap = ipcp_udp_bootstrap, .ipcp_enroll = NULL, /* shim */ - .ipcp_name_reg = ipcp_udp_name_reg, - .ipcp_name_unreg = ipcp_udp_name_unreg, - .ipcp_name_query = ipcp_udp_name_query, + .ipcp_reg = ipcp_udp_reg, + .ipcp_unreg = ipcp_udp_unreg, + .ipcp_query = ipcp_udp_query, .ipcp_flow_alloc = ipcp_udp_flow_alloc, .ipcp_flow_alloc_resp = ipcp_udp_flow_alloc_resp, .ipcp_flow_dealloc = ipcp_udp_flow_dealloc @@ -1202,26 +1086,6 @@ static struct ipcp_ops udp_ops = { int main(int argc, char * argv[]) { - struct sigaction sig_act; - sigset_t sigset; - sigemptyset(&sigset); - sigaddset(&sigset, SIGINT); - sigaddset(&sigset, SIGQUIT); - sigaddset(&sigset, SIGHUP); - sigaddset(&sigset, SIGPIPE); - - /* init sig_act */ - memset(&sig_act, 0, sizeof(sig_act)); - - /* install signal traps */ - sig_act.sa_sigaction = &ipcp_sig_handler; - sig_act.sa_flags = SA_SIGINFO; - - sigaction(SIGINT, &sig_act, NULL); - sigaction(SIGTERM, &sig_act, NULL); - sigaction(SIGHUP, &sig_act, NULL); - sigaction(SIGPIPE, &sig_act, NULL); - if (ipcp_init(argc, argv, THIS_TYPE, &udp_ops) < 0) { ipcp_create_r(getpid(), -1); exit(EXIT_FAILURE); @@ -1234,9 +1098,6 @@ int main(int argc, exit(EXIT_FAILURE); } - - pthread_sigmask(SIG_BLOCK, &sigset, NULL); - if (ipcp_boot() < 0) { log_err("Failed to boot IPCP."); ipcp_create_r(getpid(), -1); @@ -1245,8 +1106,6 @@ int main(int argc, exit(EXIT_FAILURE); } - pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); - if (ipcp_create_r(getpid(), 0)) { log_err("Failed to notify IRMd we are initialized."); ipcp_set_state(IPCP_NULL); diff --git a/src/ipcpd/shim-udp/shim_udp_messages.proto b/src/ipcpd/shim-udp/shim_udp_messages.proto index 75f0cb64..ae89a119 100644 --- a/src/ipcpd/shim-udp/shim_udp_messages.proto +++ b/src/ipcpd/shim-udp/shim_udp_messages.proto @@ -29,10 +29,9 @@ enum shim_udp_msg_code { message shim_udp_msg { required shim_udp_msg_code code = 1; - optional string dst_name = 2; - optional string src_ae_name = 4; - required uint32 src_udp_port = 5; - optional uint32 dst_udp_port = 6; - optional uint32 qoscube = 7; - optional sint32 response = 8; + optional bytes hash = 2; + required uint32 src_udp_port = 3; + optional uint32 dst_udp_port = 4; + optional uint32 qoscube = 5; + optional sint32 response = 6; }; diff --git a/src/ipcpd/shim-udp/tests/shim_udp_test.c b/src/ipcpd/shim-udp/tests/shim_udp_test.c index d7bd0bb7..015bb49f 100644 --- a/src/ipcpd/shim-udp/tests/shim_udp_test.c +++ b/src/ipcpd/shim-udp/tests/shim_udp_test.c @@ -21,7 +21,7 @@ */ #include <ouroboros/config.h> -#include <ouroboros/dif_config.h> +#include <ouroboros/ipcp.h> #include <ouroboros/utils.h> #include <ouroboros/shm_du_map.h> #include <sys/types.h> @@ -41,7 +41,7 @@ int shim_udp_test(int argc, char ** argv) char bogus[16]; memset(&bogus, 0, 16); - struct dif_config conf; + struct ipcp_config conf; memset(&conf, 0, sizeof conf); conf.dif_name = strdup("test-dif"); conf.type = IPCP_SHIM_UDP; diff --git a/src/irmd/apn_table.c b/src/irmd/apn_table.c index d265e2f0..c77e7128 100644 --- a/src/irmd/apn_table.c +++ b/src/irmd/apn_table.c @@ -21,7 +21,7 @@ */ #include <ouroboros/errno.h> -#include <ouroboros/irm_config.h> +#include <ouroboros/irm.h> #include "apn_table.h" #include "utils.h" diff --git a/src/irmd/ipcp.c b/src/irmd/ipcp.c index eb0c2de0..f0e57dc8 100644 --- a/src/irmd/ipcp.c +++ b/src/irmd/ipcp.c @@ -104,15 +104,15 @@ ipcp_msg_t * send_recv_ipcp_msg(pid_t api, return recv_msg; } -pid_t ipcp_create(char * name, +pid_t ipcp_create(const char * name, enum ipcp_type ipcp_type) { - pid_t api = -1; - char irmd_api[10]; - size_t len = 0; - char * ipcp_dir = "/sbin/"; + pid_t api = -1; + size_t len = 0; + char * ipcp_dir = "/sbin/"; char * full_name = NULL; char * exec_name = NULL; + char irmd_api[10]; char * argv[5]; sprintf(irmd_api, "%u", getpid()); @@ -157,13 +157,11 @@ pid_t ipcp_create(char * name, /* log_file to be placed at the end */ argv[0] = full_name; argv[1] = irmd_api; - argv[2] = name; - if (log_syslog) { + argv[2] = (char *) name; + if (log_syslog) argv[3] = "1"; - argv[4] = NULL; - } else { + else argv[3] = NULL; - } argv[4] = NULL; @@ -187,7 +185,7 @@ int ipcp_destroy(pid_t api) } int ipcp_bootstrap(pid_t api, - dif_config_msg_t * conf) + ipcp_config_msg_t * conf) { ipcp_msg_t msg = IPCP_MSG__INIT; ipcp_msg_t * recv_msg = NULL; @@ -201,11 +199,11 @@ int ipcp_bootstrap(pid_t api, recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) - return -1; + return -EIPCP; if (recv_msg->has_result == false) { ipcp_msg__free_unpacked(recv_msg, NULL); - return -1; + return -EIPCP; } ret = recv_msg->result; @@ -214,26 +212,26 @@ int ipcp_bootstrap(pid_t api, return ret; } -int ipcp_enroll(pid_t api, - char * dif_name) +int ipcp_enroll(pid_t api, + const char * dst) { ipcp_msg_t msg = IPCP_MSG__INIT; ipcp_msg_t * recv_msg = NULL; int ret = -1; - if (dif_name == NULL) + if (dst == NULL) return -EINVAL; - msg.code = IPCP_MSG_CODE__IPCP_ENROLL; - msg.dif_name = dif_name; + msg.code = IPCP_MSG_CODE__IPCP_ENROLL; + msg.dst_name = (char *) dst; recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) - return -1; + return -EIPCP; if (recv_msg->has_result == false) { ipcp_msg__free_unpacked(recv_msg, NULL); - return -1; + return -EIPCP; } ret = recv_msg->result; @@ -242,26 +240,28 @@ int ipcp_enroll(pid_t api, return ret; } -int ipcp_name_reg(pid_t api, - char * name) +int ipcp_reg(pid_t api, + const uint8_t * hash, + size_t len) { ipcp_msg_t msg = IPCP_MSG__INIT; ipcp_msg_t * recv_msg = NULL; int ret = -1; - if (name == NULL) - return -1; + assert(hash); - msg.code = IPCP_MSG_CODE__IPCP_NAME_REG; - msg.name = name; + msg.code = IPCP_MSG_CODE__IPCP_REG; + msg.has_hash = true; + msg.hash.len = len; + msg.hash.data = (uint8_t *)hash; recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) - return -1; + return -EIPCP; if (recv_msg->has_result == false) { ipcp_msg__free_unpacked(recv_msg, NULL); - return -1; + return -EIPCP; } ret = recv_msg->result; @@ -270,23 +270,26 @@ int ipcp_name_reg(pid_t api, return ret; } -int ipcp_name_unreg(pid_t api, - char * name) +int ipcp_unreg(pid_t api, + const uint8_t * hash, + size_t len) { ipcp_msg_t msg = IPCP_MSG__INIT; ipcp_msg_t * recv_msg = NULL; int ret = -1; - msg.code = IPCP_MSG_CODE__IPCP_NAME_UNREG; - msg.name = name; + msg.code = IPCP_MSG_CODE__IPCP_UNREG; + msg.has_hash = true; + msg.hash.len = len; + msg.hash.data = (uint8_t *) hash; recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) - return -1; + return -EIPCP; if (recv_msg->has_result == false) { ipcp_msg__free_unpacked(recv_msg, NULL); - return -1; + return -EIPCP; } ret = recv_msg->result; @@ -295,23 +298,26 @@ int ipcp_name_unreg(pid_t api, return ret; } -int ipcp_name_query(pid_t api, - char * name) +int ipcp_query(pid_t api, + const uint8_t * hash, + size_t len) { ipcp_msg_t msg = IPCP_MSG__INIT; ipcp_msg_t * recv_msg = NULL; int ret = -1; - msg.code = IPCP_MSG_CODE__IPCP_NAME_QUERY; - msg.name = name; + msg.code = IPCP_MSG_CODE__IPCP_QUERY; + msg.has_hash = true; + msg.hash.len = len; + msg.hash.data = (uint8_t *) hash; recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) - return -1; + return -EIPCP; if (recv_msg->has_result == false) { ipcp_msg__free_unpacked(recv_msg, NULL); - return -1; + return -EIPCP; } ret = recv_msg->result; @@ -320,35 +326,37 @@ int ipcp_name_query(pid_t api, return ret; } -int ipcp_flow_alloc(pid_t api, - int port_id, - pid_t n_api, - char * dst_name, - qoscube_t cube) +int ipcp_flow_alloc(pid_t api, + int port_id, + pid_t n_api, + const uint8_t * dst, + size_t len, + qoscube_t cube) { ipcp_msg_t msg = IPCP_MSG__INIT; ipcp_msg_t * recv_msg = NULL; int ret = -1; - if (dst_name == NULL) - return -EINVAL; + assert(dst); msg.code = IPCP_MSG_CODE__IPCP_FLOW_ALLOC; msg.has_port_id = true; msg.port_id = port_id; msg.has_api = true; msg.api = n_api; - msg.dst_name = dst_name; + msg.has_hash = true; + msg.hash.len = len; + msg.hash.data = (uint8_t *) dst; msg.has_qoscube = true; msg.qoscube = cube; recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) - return -1; + return -EIPCP; if (!recv_msg->has_result) { ipcp_msg__free_unpacked(recv_msg, NULL); - return -1; + return -EIPCP; } ret = recv_msg->result; @@ -376,11 +384,11 @@ int ipcp_flow_alloc_resp(pid_t api, recv_msg = send_recv_ipcp_msg(api, &msg); if (recv_msg == NULL) - return -1; + return -EIPCP; if (recv_msg->has_result == false) { ipcp_msg__free_unpacked(recv_msg, NULL); - return -1; + return -EIPCP; } ret = recv_msg->result; @@ -392,7 +400,6 @@ int ipcp_flow_alloc_resp(pid_t api, int ipcp_flow_dealloc(pid_t api, int port_id) { - ipcp_msg_t msg = IPCP_MSG__INIT; ipcp_msg_t * recv_msg = NULL; int ret = -1; diff --git a/src/irmd/ipcp.h b/src/irmd/ipcp.h index bb868191..11adad7d 100644 --- a/src/irmd/ipcp.h +++ b/src/irmd/ipcp.h @@ -20,40 +20,44 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include <ouroboros/irm_config.h> +#include <ouroboros/ipcp.h> #include <ouroboros/sockets.h> #include <ouroboros/shared.h> #include <sys/types.h> -#ifndef OUROBOROS_IPCP_H -#define OUROBOROS_IPCP_H +#ifndef OUROBOROS_IRMD_IPCP_H +#define OUROBOROS_IRMD_IPCP_H -pid_t ipcp_create(char * name, +pid_t ipcp_create(const char * name, enum ipcp_type ipcp_type); int ipcp_destroy(pid_t api); -int ipcp_enroll(pid_t api, - char * dif_name); +int ipcp_enroll(pid_t api, + const char * dst); int ipcp_bootstrap(pid_t api, - dif_config_msg_t * conf); + ipcp_config_msg_t * conf); -int ipcp_name_reg(pid_t api, - char * name); +int ipcp_reg(pid_t api, + const uint8_t * hash, + size_t len); -int ipcp_name_unreg(pid_t api, - char * name); +int ipcp_unreg(pid_t api, + const uint8_t * hash, + size_t len); -int ipcp_name_query(pid_t api, - char * name); +int ipcp_query(pid_t api, + const uint8_t * hash, + size_t len); -int ipcp_flow_alloc(pid_t api, - int port_id, - pid_t n_api, - char * dst_name, - qoscube_t qos); +int ipcp_flow_alloc(pid_t api, + int port_id, + pid_t n_api, + const uint8_t * dst, + size_t len, + qoscube_t qos); int ipcp_flow_alloc_resp(pid_t api, int port_id, @@ -63,4 +67,4 @@ int ipcp_flow_alloc_resp(pid_t api, int ipcp_flow_dealloc(pid_t api, int port_id); -#endif /* OUROBOROS_IPCP_H */ +#endif /* OUROBOROS_IRMD_IPCP_H */ diff --git a/src/irmd/main.c b/src/irmd/main.c index 5e5039b1..125061fb 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -27,7 +27,8 @@ #include <ouroboros/sockets.h> #include <ouroboros/list.h> #include <ouroboros/utils.h> -#include <ouroboros/irm_config.h> +#include <ouroboros/hash.h> +#include <ouroboros/irm.h> #include <ouroboros/lockfile.h> #include <ouroboros/shm_flow_set.h> #include <ouroboros/shm_rbuff.h> @@ -36,6 +37,7 @@ #include <ouroboros/qos.h> #include <ouroboros/time_utils.h> #include <ouroboros/logs.h> +#include <ouroboros/sha3.h> #include "utils.h" #include "registry.h" @@ -62,6 +64,8 @@ struct ipcp_entry { char * name; pid_t api; enum ipcp_type type; + uint16_t dir_hash_len; + /* FIXME: add an enum to specify hash algo */ char * dif_name; pthread_cond_t init_cond; @@ -223,48 +227,33 @@ static struct ipcp_entry * get_ipcp_entry_by_name(const char * name) return NULL; } -/* Check if the name exists anywhere in a DIF. */ -static pid_t get_ipcp_by_dst_name(char * dst_name) +/* + * Check if the hash is reachable anywhere in a DIF. + * FIXME: specify algorithm used + */ +static struct ipcp_entry * get_ipcp_by_dst_name(const char * name) { struct list_head * p = NULL; + uint8_t * hash; list_for_each(p, &irmd.ipcps) { struct ipcp_entry * e = list_entry(p, struct ipcp_entry, next); - if (e->type == IPCP_LOCAL) { - if (ipcp_name_query(e->api, dst_name) == 0) - return e->api; - } - } + hash = malloc(e->dir_hash_len); + if (hash == NULL) + return NULL; - list_for_each(p, &irmd.ipcps) { - struct ipcp_entry * e = - list_entry(p, struct ipcp_entry, next); - if (e->type == IPCP_NORMAL) { - if (ipcp_name_query(e->api, dst_name) == 0) - return e->api; - } - } + get_hash(hash, name); - list_for_each(p, &irmd.ipcps) { - struct ipcp_entry * e = - list_entry(p, struct ipcp_entry, next); - if (e->type == IPCP_SHIM_ETH_LLC) { - if (ipcp_name_query(e->api, dst_name) == 0) - return e->api; + if (ipcp_query(e->api, hash, e->dir_hash_len) == 0) { + free(hash); + return e; } - } - list_for_each(p, &irmd.ipcps) { - struct ipcp_entry * e = - list_entry(p, struct ipcp_entry, next); - if (e->type == IPCP_SHIM_UDP) { - if (ipcp_name_query(e->api, dst_name) == 0) - return e->api; - } + free(hash); } - return -1; + return NULL; } static pid_t create_ipcp(char * name, @@ -317,14 +306,16 @@ static pid_t create_ipcp(char * name, tmp->dif_name = NULL; tmp->type = ipcp_type; tmp->init = false; + /* FIXME: ipcp dir_hash_len should be configurable */ + tmp->dir_hash_len = SHA3_256_HASH_LEN; list_for_each(p, &irmd.ipcps) { struct ipcp_entry * e = list_entry(p, struct ipcp_entry, next); - if (e->type < ipcp_type) + if (e->type > ipcp_type) break; } - list_add(&tmp->next, &irmd.ipcps); + list_add_tail(&tmp->next, &irmd.ipcps); list_add(&api->next, &irmd.spawned_apis); @@ -411,7 +402,7 @@ static int destroy_ipcp(pid_t api) } static int bootstrap_ipcp(pid_t api, - dif_config_msg_t * conf) + ipcp_config_msg_t * conf) { struct ipcp_entry * entry = NULL; @@ -709,16 +700,18 @@ static ssize_t list_ipcps(char * name, return count; } -static int name_reg(char * name, - char ** difs, - size_t len) +static int name_reg(const char * name, + char ** difs, + size_t len) { size_t i; int ret = 0; struct list_head * p = NULL; - if (name == NULL || difs == NULL || len == 0 || difs[0] == NULL) - return -EINVAL; + assert(name); + assert(len); + assert(difs); + assert(difs[0]); pthread_rwlock_wrlock(&irmd.reg_lock); @@ -729,7 +722,7 @@ static int name_reg(char * name, if (!registry_has_name(&irmd.registry, name)) { struct reg_entry * re = - registry_add_name(&irmd.registry, strdup(name)); + registry_add_name(&irmd.registry, name); if (re == NULL) { log_err("Failed creating registry entry for %s.", name); pthread_rwlock_unlock(&irmd.reg_lock); @@ -768,12 +761,21 @@ static int name_reg(char * name, continue; for (i = 0; i < len; ++i) { + uint8_t * hash; + if (wildcard_match(difs[i], e->dif_name)) continue; - if (ipcp_name_reg(e->api, name)) { - log_err("Could not register %s in DIF %s.", - name, e->dif_name); + hash = malloc(e->dir_hash_len); + if (hash == NULL) + break; + + get_hash(hash, name); + + if (ipcp_reg(e->api, hash, e->dir_hash_len)) { + log_err("Could not register " HASH_FMT + " in DIF %s.", + HASH_VAL(hash), e->dif_name); } else { if (registry_add_name_to_dif(&irmd.registry, name, @@ -782,10 +784,12 @@ static int name_reg(char * name, log_warn("Registered unbound name %s. " "Registry may be corrupt.", name); - log_info("Registered %s in %s as %s.", - name, e->dif_name, name); + log_info("Registered %s in %s as " HASH_FMT ".", + name, e->dif_name, HASH_VAL(hash)); ++ret; } + + free(hash); } } @@ -794,16 +798,18 @@ static int name_reg(char * name, return (ret > 0 ? 0 : -1); } -static int name_unreg(char * name, - char ** difs, - size_t len) +static int name_unreg(const char * name, + char ** difs, + size_t len) { size_t i; int ret = 0; struct list_head * pos = NULL; - if (name == NULL || len == 0 || difs == NULL || difs[0] == NULL) - return -1; + assert(name); + assert(len); + assert(difs); + assert(difs[0]); pthread_rwlock_wrlock(&irmd.reg_lock); @@ -815,10 +821,18 @@ static int name_unreg(char * name, continue; for (i = 0; i < len; ++i) { + uint8_t * hash; + if (wildcard_match(difs[i], e->dif_name)) continue; - if (ipcp_name_unreg(e->api, name)) { + hash = malloc(e->dir_hash_len); + if (hash == NULL) + break; + + get_hash(hash, name); + + if (ipcp_unreg(e->api, hash, e->dir_hash_len)) { log_err("Could not unregister %s in DIF %s.", name, e->dif_name); } else { @@ -829,6 +843,8 @@ static int name_unreg(char * name, name, e->dif_name); ++ret; } + + free(hash); } } @@ -1038,22 +1054,23 @@ static int flow_accept(pid_t api, } static int flow_alloc(pid_t api, - char * dst_name, + const char * dst, qoscube_t cube, struct timespec * timeo, struct irm_flow ** e) { - struct irm_flow * f; - pid_t ipcp; - int port_id; - int state; + struct irm_flow * f; + struct ipcp_entry * ipcp; + int port_id; + int state; + uint8_t * hash; pthread_rwlock_rdlock(&irmd.reg_lock); - ipcp = get_ipcp_by_dst_name(dst_name); - if (ipcp == -1) { + ipcp = get_ipcp_by_dst_name(dst); + if (ipcp == NULL) { pthread_rwlock_unlock(&irmd.reg_lock); - log_info("Destination unreachable."); + log_info("Destination %s unreachable.", dst); return -1; } @@ -1066,7 +1083,7 @@ static int flow_alloc(pid_t api, return -EBADF; } - f = irm_flow_create(api, ipcp, port_id, cube); + f = irm_flow_create(api, ipcp->api, port_id, cube); if (f == NULL) { bmp_release(irmd.port_ids, port_id); pthread_rwlock_unlock(&irmd.flows_lock); @@ -1080,12 +1097,24 @@ static int flow_alloc(pid_t api, assert(irm_flow_get_state(f) == FLOW_ALLOC_PENDING); - if (ipcp_flow_alloc(ipcp, port_id, api, dst_name, cube)) { + hash = malloc(ipcp->dir_hash_len); + if (hash == NULL) { + /* sanitizer cleans this */ + return -ENOMEM; + } + + get_hash(hash, dst); + + if (ipcp_flow_alloc(ipcp->api, port_id, api, hash, + ipcp->dir_hash_len, cube)) { /* sanitizer cleans this */ log_info("Flow_allocation failed."); + free(hash); return -EAGAIN; } + free(hash); + state = irm_flow_wait_state(f, FLOW_ALLOCATED, timeo); if (state != FLOW_ALLOCATED) { if (state == -ETIMEDOUT) { @@ -1093,7 +1122,7 @@ static int flow_alloc(pid_t api, return -ETIMEDOUT; } - log_info("Pending flow to %s torn down.", dst_name); + log_info("Pending flow to %s torn down.", dst); return -EPIPE; } @@ -1191,38 +1220,49 @@ static pid_t auto_execute(char ** argv) exit(EXIT_FAILURE); } -static struct irm_flow * flow_req_arr(pid_t api, - char * dst_name, - qoscube_t cube) +static struct irm_flow * flow_req_arr(pid_t api, + const uint8_t * hash, + qoscube_t cube) { struct reg_entry * re = NULL; struct apn_entry * a = NULL; struct api_entry * e = NULL; struct irm_flow * f = NULL; - struct pid_el * c_api; - pid_t h_api = -1; - int port_id = -1; + struct pid_el * c_api; + struct ipcp_entry * ipcp; + pid_t h_api = -1; + int port_id = -1; struct timespec wt = {IRMD_REQ_ARR_TIMEOUT / 1000, (IRMD_REQ_ARR_TIMEOUT % 1000) * MILLION}; - log_dbg("Flow req arrived from IPCP %d for %s.", api, dst_name); + log_dbg("Flow req arrived from IPCP %d for " HASH_FMT ".", + api, HASH_VAL(hash)); pthread_rwlock_rdlock(&irmd.reg_lock); - re = registry_get_entry(&irmd.registry, dst_name); + ipcp = get_ipcp_entry_by_api(api); + if (ipcp == NULL) { + log_err("IPCP died."); + return NULL; + } + + re = registry_get_entry_by_hash(&irmd.registry, hash, + ipcp->dir_hash_len); if (re == NULL) { pthread_rwlock_unlock(&irmd.reg_lock); - log_err("Unknown name: %s.", dst_name); + log_err("Unknown hash: " HASH_FMT ".", HASH_VAL(hash)); return NULL; } + log_info("Flow request arrived for %s.", re->name); + pthread_rwlock_unlock(&irmd.reg_lock); /* Give the AP a bit of slop time to call accept */ if (reg_entry_leave_state(re, REG_NAME_IDLE, &wt) == -1) { - log_err("No APs for %s.", dst_name); + log_err("No APs for " HASH_FMT ".", HASH_VAL(hash)); return NULL; } @@ -1231,7 +1271,7 @@ static struct irm_flow * flow_req_arr(pid_t api, switch (reg_entry_get_state(re)) { case REG_NAME_IDLE: pthread_rwlock_unlock(&irmd.reg_lock); - log_err("No APs for %s.", dst_name); + log_err("No APs for " HASH_FMT ".", HASH_VAL(hash)); return NULL; case REG_NAME_AUTO_ACCEPT: c_api = malloc(sizeof(*c_api)); @@ -1830,7 +1870,7 @@ void * mainloop(void * o) break; case IRM_MSG_CODE__IPCP_FLOW_REQ_ARR: e = flow_req_arr(msg->api, - msg->dst_name, + msg->hash.data, msg->qoscube); ret_msg.has_result = true; if (e == NULL) { diff --git a/src/irmd/registry.c b/src/irmd/registry.c index 53be77cd..71e6ea8a 100644 --- a/src/irmd/registry.c +++ b/src/irmd/registry.c @@ -25,8 +25,9 @@ #include <ouroboros/config.h> #include <ouroboros/errno.h> #include <ouroboros/logs.h> -#include <ouroboros/irm_config.h> +#include <ouroboros/irm.h> #include <ouroboros/time_utils.h> +#include <ouroboros/hash.h> #include "registry.h" #include "utils.h" @@ -145,7 +146,8 @@ static void reg_entry_destroy(struct reg_entry * e) free(e); } -static bool reg_entry_is_local_in_dif(struct reg_entry * e, char * dif_name) +static bool reg_entry_is_local_in_dif(struct reg_entry * e, + const char * dif_name) { struct list_head * p = NULL; @@ -159,7 +161,7 @@ static bool reg_entry_is_local_in_dif(struct reg_entry * e, char * dif_name) } static int reg_entry_add_local_in_dif(struct reg_entry * e, - char * dif_name, + const char * dif_name, enum ipcp_type type) { if (!reg_entry_is_local_in_dif(e, dif_name)) { @@ -176,7 +178,7 @@ static int reg_entry_add_local_in_dif(struct reg_entry * e, } static void reg_entry_del_local_from_dif(struct reg_entry * e, - char * dif_name) + const char * dif_name) { struct list_head * p = NULL; struct list_head * h = NULL; @@ -192,7 +194,7 @@ static void reg_entry_del_local_from_dif(struct reg_entry * e, } static bool reg_entry_has_apn(struct reg_entry * e, - char * apn) + const char * apn) { struct list_head * p; @@ -242,7 +244,7 @@ int reg_entry_add_apn(struct reg_entry * e, } void reg_entry_del_apn(struct reg_entry * e, - char * apn) + const char * apn) { struct list_head * p = NULL; struct list_head * h = NULL; @@ -504,9 +506,9 @@ int reg_entry_wait_state(struct reg_entry * e, } struct reg_entry * registry_get_entry(struct list_head * registry, - char * name) + const char * name) { - struct list_head * p = NULL; + struct list_head * p = NULL; assert(registry); @@ -519,8 +521,33 @@ struct reg_entry * registry_get_entry(struct list_head * registry, return NULL; } +struct reg_entry * registry_get_entry_by_hash(struct list_head * registry, + const uint8_t * hash, + size_t len) +{ + struct list_head * p = NULL; + uint8_t * thash; + + thash = malloc(len); + if (thash == NULL) + return NULL; + + assert(registry); + + list_for_each(p, registry) { + struct reg_entry * e = list_entry(p, struct reg_entry, next); + get_hash(thash, e->name); + if (memcmp(thash, hash, len) == 0) { + free(thash); + return e; + } + } + + return NULL; +} + struct reg_entry * registry_add_name(struct list_head * registry, - char * name) + const char * name) { struct reg_entry * e = NULL; @@ -538,7 +565,7 @@ struct reg_entry * registry_add_name(struct list_head * registry, return NULL; } - e = reg_entry_init(e, name); + e = reg_entry_init(e, strdup(name)); if (e == NULL) { log_dbg("Could not initialize registry entry."); reg_entry_destroy(e); @@ -551,7 +578,7 @@ struct reg_entry * registry_add_name(struct list_head * registry, } void registry_del_name(struct list_head * registry, - char * name) + const char * name) { struct reg_entry * e = registry_get_entry(registry, name); if (e == NULL) @@ -583,8 +610,8 @@ void registry_del_api(struct list_head * registry, } int registry_add_name_to_dif(struct list_head * registry, - char * name, - char * dif_name, + const char * name, + const char * dif_name, enum ipcp_type type) { struct reg_entry * re = registry_get_entry(registry, name); @@ -595,8 +622,8 @@ int registry_add_name_to_dif(struct list_head * registry, } void registry_del_name_from_dif(struct list_head * registry, - char * name, - char * dif_name) + const char * name, + const char * dif_name) { struct reg_entry * re = registry_get_entry(registry, name); if (re == NULL) diff --git a/src/irmd/registry.h b/src/irmd/registry.h index 08e78019..771ca83c 100644 --- a/src/irmd/registry.h +++ b/src/irmd/registry.h @@ -24,8 +24,8 @@ #define OUROBOROS_IRMD_REGISTRY_H #include <ouroboros/config.h> +#include <ouroboros/ipcp.h> #include <ouroboros/list.h> -#include <ouroboros/irm_config.h> #include <ouroboros/shared.h> #include "api_table.h" @@ -71,7 +71,7 @@ int reg_entry_add_apn(struct reg_entry * e, struct apn_entry * a); void reg_entry_del_apn(struct reg_entry * e, - char * apn); + const char * apn); char * reg_entry_get_apn(struct reg_entry * e); @@ -101,10 +101,10 @@ int reg_entry_wait_state(struct reg_entry * e, struct timespec * timeout); struct reg_entry * registry_add_name(struct list_head * registry, - char * name); + const char * name); void registry_del_name(struct list_head * registry, - char * name); + const char * name); void registry_del_api(struct list_head * registry, pid_t api); @@ -112,16 +112,20 @@ void registry_del_api(struct list_head * registry, void registry_sanitize_apis(struct list_head * registry); struct reg_entry * registry_get_entry(struct list_head * registry, - char * name); + const char * name); + +struct reg_entry * registry_get_entry_by_hash(struct list_head * registry, + const uint8_t * hash, + size_t len); int registry_add_name_to_dif(struct list_head * registry, - char * name, - char * dif_name, + const char * name, + const char * dif_name, enum ipcp_type type); void registry_del_name_from_dif(struct list_head * registry, - char * name, - char * dif_name); + const char * name, + const char * dif_name); void registry_destroy(struct list_head * registry); diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index f6a30ef7..cb94ef53 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -7,7 +7,7 @@ include_directories(${CMAKE_BINARY_DIR}/include) protobuf_generate_c(IRM_PROTO_SRCS IRM_PROTO_HDRS irmd_messages.proto) protobuf_generate_c(IPCP_PROTO_SRCS IPCP_PROTO_HDRS ipcpd_messages.proto) protobuf_generate_c(DIF_CONFIG_PROTO_SRCS DIF_CONFIG_PROTO_HDRS - dif_config.proto) + ipcp_config.proto) protobuf_generate_c(CDAP_PROTO_SRCS CDAP_PROTO_HDRS cdap.proto) protobuf_generate_c(RO_PROTO_SRCS RO_PROTO_HDRS ro.proto) protobuf_generate_c(CACEP_PROTO_SRCS CACEP_PROTO_HDRS cacep.proto) @@ -35,6 +35,7 @@ set(SOURCE_FILES cdap_req.c crc32.c dev.c + hash.c hashtable.c irm.c list.c diff --git a/src/lib/btree.c b/src/lib/btree.c index 48df8e39..03047a18 100644 --- a/src/lib/btree.c +++ b/src/lib/btree.c @@ -22,8 +22,7 @@ */ #include <ouroboros/errno.h> - -#include "btree.h" +#include <ouroboros/btree.h> #include <stdlib.h> #include <stdbool.h> diff --git a/src/lib/dev.c b/src/lib/dev.c index 433fb93b..a4707dc3 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -1210,7 +1210,8 @@ int ipcp_create_r(pid_t api, } int ipcp_flow_req_arr(pid_t api, - char * dst_name, + uint8_t * dst, + size_t len, qoscube_t cube) { irm_msg_t msg = IRM_MSG__INIT; @@ -1218,13 +1219,15 @@ int ipcp_flow_req_arr(pid_t api, int port_id = -1; int fd = -1; - if (dst_name == NULL) + if (dst == NULL) return -EINVAL; msg.code = IRM_MSG_CODE__IPCP_FLOW_REQ_ARR; msg.has_api = true; msg.api = api; - msg.dst_name = dst_name; + msg.has_hash = true; + msg.hash.len = len; + msg.hash.data = dst; msg.has_qoscube = true; msg.qoscube = cube; diff --git a/src/lib/hash.c b/src/lib/hash.c new file mode 100644 index 00000000..f4fd75ea --- /dev/null +++ b/src/lib/hash.c @@ -0,0 +1,43 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2017 + * + * Hashing + * + * Dimitri Staessens <[email protected]> + * Sander Vrijders <[email protected]> + * + * This implementation is adapted and redistributed from the RHASH + * project + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include <ouroboros/config.h> +#include <ouroboros/hash.h> + +#include <string.h> + +void get_hash(uint8_t buf[], + const char * name) +{ + /* currently we only support 256 bit SHA-3 */ + struct sha3_ctx ctx; + + rhash_sha3_256_init(&ctx); + + rhash_sha3_update(&ctx, name, strlen(name)); + + rhash_sha3_final(&ctx, buf); +} diff --git a/src/lib/dif_config.proto b/src/lib/ipcp_config.proto index d47af049..a0c14c41 100644 --- a/src/lib/dif_config.proto +++ b/src/lib/ipcp_config.proto @@ -23,24 +23,25 @@ syntax = "proto2"; -message dif_config_msg { +message ipcp_config_msg { required string dif_name = 1; - required int32 ipcp_type = 2; + required uint32 dir_hash_len = 2; + required int32 ipcp_type = 3; // Config for normal IPCP - optional uint32 addr_size = 3; - optional uint32 cep_id_size = 4; - optional uint32 pdu_length_size = 5; - optional uint32 seqno_size = 6; - optional bool has_ttl = 7; - optional bool has_chk = 8; - optional uint32 min_pdu_size = 9; - optional uint32 max_pdu_size = 10; - optional uint32 addr_auth_type = 11; - optional uint32 dt_gam_type = 12; - optional uint32 rm_gam_type = 13; + optional uint32 addr_size = 4; + optional uint32 cep_id_size = 5; + optional uint32 pdu_length_size = 6; + optional uint32 seqno_size = 7; + optional bool has_ttl = 8; + optional bool has_chk = 9; + optional uint32 min_pdu_size = 10; + optional uint32 max_pdu_size = 11; + optional uint32 addr_auth_type = 12; + optional uint32 dt_gam_type = 13; + optional uint32 rm_gam_type = 14; // Config for shim UDP - optional uint32 ip_addr = 14; - optional uint32 dns_addr = 15; + optional uint32 ip_addr = 15; + optional uint32 dns_addr = 16; // Config for the shim Ethernet LLC - optional string if_name = 16; + optional string if_name = 17; }
\ No newline at end of file diff --git a/src/lib/ipcpd_messages.proto b/src/lib/ipcpd_messages.proto index da817944..bd71d3f3 100644 --- a/src/lib/ipcpd_messages.proto +++ b/src/lib/ipcpd_messages.proto @@ -23,14 +23,14 @@ syntax = "proto2"; -import "dif_config.proto"; +import "ipcp_config.proto"; enum ipcp_msg_code { IPCP_BOOTSTRAP = 1; IPCP_ENROLL = 2; - IPCP_NAME_REG = 3; - IPCP_NAME_UNREG = 4; - IPCP_NAME_QUERY = 5; + IPCP_REG = 3; + IPCP_UNREG = 4; + IPCP_QUERY = 5; IPCP_FLOW_ALLOC = 6; IPCP_FLOW_ALLOC_RESP = 7; IPCP_FLOW_DEALLOC = 8; @@ -38,17 +38,14 @@ enum ipcp_msg_code { }; message ipcp_msg { - required ipcp_msg_code code = 1; - optional string dif_name = 2; - repeated string dif_names = 3; - optional int32 len = 4; - optional string name = 5; - optional sint32 port_id = 6; - optional string dst_name = 7; - optional sint32 qoscube = 8; - optional dif_config_msg conf = 9; - optional sint32 fd = 10; - optional sint32 api = 11; - optional sint32 response = 12; - optional sint32 result = 13; + required ipcp_msg_code code = 1; + optional string name = 2; + optional bytes hash = 3; + optional int32 port_id = 4; + optional string dst_name = 5; + optional uint32 qoscube = 6; + optional ipcp_config_msg conf = 7; + optional int32 api = 8; + optional int32 response = 9; + optional int32 result = 10; }; diff --git a/src/lib/irm.c b/src/lib/irm.c index 57e09369..a2fd5d0b 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -87,13 +87,13 @@ int irm_destroy_ipcp(pid_t api) return ret; } -int irm_bootstrap_ipcp(pid_t api, - const struct dif_config * conf) +int irm_bootstrap_ipcp(pid_t api, + const struct ipcp_config * conf) { - irm_msg_t msg = IRM_MSG__INIT; - dif_config_msg_t config = DIF_CONFIG_MSG__INIT; - irm_msg_t * recv_msg = NULL; - int ret = -1; + irm_msg_t msg = IRM_MSG__INIT; + ipcp_config_msg_t config = IPCP_CONFIG_MSG__INIT; + irm_msg_t * recv_msg = NULL; + int ret = -1; if (api == -1 || conf == NULL) return -EINVAL; @@ -105,6 +105,7 @@ int irm_bootstrap_ipcp(pid_t api, msg.conf = &config; config.dif_name = conf->dif_name; config.ipcp_type = conf->type; + config.dir_hash_len = (uint16_t) conf->dir_hash_len; switch (conf->type) { case IPCP_NORMAL: diff --git a/src/lib/irmd_messages.proto b/src/lib/irmd_messages.proto index e218f6f6..138810da 100644 --- a/src/lib/irmd_messages.proto +++ b/src/lib/irmd_messages.proto @@ -23,7 +23,7 @@ syntax = "proto2"; -import "dif_config.proto"; +import "ipcp_config.proto"; enum irm_msg_code { IRM_CREATE_IPCP = 1; @@ -48,20 +48,21 @@ enum irm_msg_code { }; message irm_msg { - required irm_msg_code code = 1; - optional string ap_name = 2; - optional sint32 api = 3; - optional uint32 ipcp_type = 4; - repeated string dif_name = 5; - repeated string args = 6; - optional sint32 response = 7; - optional string dst_name = 8; - optional sint32 port_id = 9; - optional sint32 qoscube = 10; - optional dif_config_msg conf = 11; - optional uint32 opts = 12; - repeated sint32 apis = 13; - optional uint32 timeo_sec = 14; - optional uint32 timeo_nsec = 15; - optional sint32 result = 16; + required irm_msg_code code = 1; + optional string ap_name = 2; + optional sint32 api = 3; + optional uint32 ipcp_type = 4; + repeated string dif_name = 5; + repeated string args = 6; + optional sint32 response = 7; + optional string dst_name = 8; + optional bytes hash = 9; + optional sint32 port_id = 10; + optional sint32 qoscube = 11; + optional ipcp_config_msg conf = 12; + optional uint32 opts = 13; + repeated sint32 apis = 14; + optional uint32 timeo_sec = 15; + optional uint32 timeo_nsec = 16; + optional sint32 result = 17; }; diff --git a/src/lib/rib.c b/src/lib/rib.c index d39a17d2..2645e90b 100644 --- a/src/lib/rib.c +++ b/src/lib/rib.c @@ -29,9 +29,8 @@ #include <ouroboros/bitmap.h> #include <ouroboros/crc32.h> #include <ouroboros/time_utils.h> - -#include "sha3.h" -#include "btree.h" +#include <ouroboros/sha3.h> +#include <ouroboros/btree.h> #include "ro.pb-c.h" typedef RoMsg ro_msg_t; @@ -94,7 +93,7 @@ struct rnode { uint8_t * data; size_t len; - uint8_t sha3[sha3_256_hash_size]; + uint8_t sha3[SHA3_256_HASH_LEN]; struct rnode * parent; @@ -142,7 +141,7 @@ static void rnode_hash(struct rnode * node) list_for_each(p, &node->children) { struct child * c = list_entry(p, struct child, next); - rhash_sha3_update(&ctx, c->node->sha3, sha3_256_hash_size); + rhash_sha3_update(&ctx, c->node->sha3, SHA3_256_HASH_LEN); } rhash_sha3_final(&ctx, node->sha3); @@ -654,7 +653,7 @@ int rib_write(const char * path, uint8_t * cdata; - if (path == NULL) + if (path == NULL || data == NULL || len == 0) return -EINVAL; cdata = malloc(len); @@ -666,8 +665,13 @@ int rib_write(const char * path, pthread_rwlock_rdlock(&rib.lock); node = find_rnode_by_path(path); - if (node != NULL) - rnode_update(node, cdata, len); + if (node == NULL) { + pthread_rwlock_unlock(&rib.lock); + free(cdata); + return -1; + } + + rnode_update(node, cdata, len); pthread_rwlock_unlock(&rib.lock); @@ -1226,7 +1230,7 @@ static ro_msg_t * rnode_pack(struct rnode * node, (flags & PACK_HASH_ALL)) { msg->has_hash = true; msg->hash.data = node->sha3; - msg->hash.len = sha3_256_hash_size; + msg->hash.len = SHA3_256_HASH_LEN; } if (node->data != NULL) { @@ -1407,7 +1411,7 @@ int rib_unpack(uint8_t * packed, if (ret == 0 && msg->has_hash) { root = rnode_get_child(root, msg->name); - if (memcmp(msg->hash.data, root->sha3, sha3_256_hash_size)) { + if (memcmp(msg->hash.data, root->sha3, SHA3_256_HASH_LEN)) { ro_msg__free_unpacked(msg, NULL); return -EFAULT; } diff --git a/src/lib/sha3.c b/src/lib/sha3.c index 750038f2..f80cc4bd 100644 --- a/src/lib/sha3.c +++ b/src/lib/sha3.c @@ -44,12 +44,11 @@ */ #include <ouroboros/endian.h> +#include <ouroboros/sha3.h> #include <assert.h> #include <string.h> -#include "sha3.h" - #define IS_ALIGNED_64(p) (0 == (7 & ((const uint8_t *) (p) \ - (const uint8_t *) 0))) #define I64(x) x##LL @@ -262,11 +261,12 @@ static void rhash_sha3_process_block(uint64_t hash[25], #define SHA3_FINALIZED 0x80000000 void rhash_sha3_update(struct sha3_ctx * ctx, - const uint8_t * msg, + const void * pmsg, size_t size) { size_t idx = (size_t) ctx->rest; size_t block_size = (size_t) ctx->block_size; + uint8_t * msg = (uint8_t *) pmsg; if (ctx->rest & SHA3_FINALIZED) return; ctx->rest = (unsigned) ((ctx->rest + size) % block_size); diff --git a/src/lib/tests/btree_test.c b/src/lib/tests/btree_test.c index a6344060..83fafabd 100644 --- a/src/lib/tests/btree_test.c +++ b/src/lib/tests/btree_test.c @@ -21,7 +21,7 @@ */ -#include "btree.h" +#include <ouroboros/btree.h> #include <stdio.h> #include <stdlib.h> diff --git a/src/lib/tests/sha3_test.c b/src/lib/tests/sha3_test.c index 212452ef..8f1bce05 100644 --- a/src/lib/tests/sha3_test.c +++ b/src/lib/tests/sha3_test.c @@ -20,7 +20,7 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include "sha3.h" +#include <ouroboros/sha3.h> #include <stdlib.h> #include <stdint.h> @@ -84,7 +84,7 @@ int sha3_test(int argc, struct sha3_ctx ctx; /* Storage for result. */ - uint8_t res[sha3_512_hash_size]; + uint8_t res[SHA3_512_HASH_LEN]; /* SHA3 test vectors */ char * str1_inp = "abc"; @@ -172,124 +172,124 @@ int sha3_test(int argc, printf("test: %s.\n\n", str1_inp); rhash_sha3_224_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str1_inp, strlen(str1_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str1_inp, strlen(str1_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str1_224, res, sha3_224_hash_size)) + if (check_hash(str1_224, res, SHA3_224_HASH_LEN)) return -1; rhash_sha3_256_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str1_inp, strlen(str1_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str1_inp, strlen(str1_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str1_256, res, sha3_256_hash_size)) + if (check_hash(str1_256, res, SHA3_256_HASH_LEN)) return -1; rhash_sha3_384_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str1_inp, strlen(str1_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str1_inp, strlen(str1_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str1_384, res, sha3_384_hash_size)) + if (check_hash(str1_384, res, SHA3_384_HASH_LEN)) return -1; rhash_sha3_512_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str1_inp, strlen(str1_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str1_inp, strlen(str1_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str1_512, res, sha3_512_hash_size)) + if (check_hash(str1_512, res, SHA3_512_HASH_LEN)) return -1; /* 2nd input string. */ printf("test: <empty string>.\n\n"); rhash_sha3_224_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str2_inp, strlen(str2_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str2_inp, strlen(str2_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str2_224, res, sha3_224_hash_size)) + if (check_hash(str2_224, res, SHA3_224_HASH_LEN)) return -1; rhash_sha3_256_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str2_inp, strlen(str2_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str2_inp, strlen(str2_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str2_256, res, sha3_256_hash_size)) + if (check_hash(str2_256, res, SHA3_256_HASH_LEN)) return -1; rhash_sha3_384_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str2_inp, strlen(str2_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str2_inp, strlen(str2_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str2_384, res, sha3_384_hash_size)) + if (check_hash(str2_384, res, SHA3_384_HASH_LEN)) return -1; rhash_sha3_512_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str2_inp, strlen(str2_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str2_inp, strlen(str2_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str2_512, res, sha3_512_hash_size)) + if (check_hash(str2_512, res, SHA3_512_HASH_LEN)) return -1; /* 3rd input string */ printf("test: %s.\n\n", str3_inp); rhash_sha3_224_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str3_inp, strlen(str3_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str3_inp, strlen(str3_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str3_224, res, sha3_224_hash_size)) + if (check_hash(str3_224, res, SHA3_224_HASH_LEN)) return -1; rhash_sha3_256_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str3_inp, strlen(str3_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str3_inp, strlen(str3_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str3_256, res, sha3_256_hash_size)) + if (check_hash(str3_256, res, SHA3_256_HASH_LEN)) return -1; rhash_sha3_384_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str3_inp, strlen(str3_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str3_inp, strlen(str3_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str3_384, res, sha3_384_hash_size)) + if (check_hash(str3_384, res, SHA3_384_HASH_LEN)) return -1; rhash_sha3_512_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str3_inp, strlen(str3_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str3_inp, strlen(str3_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str3_512, res, sha3_512_hash_size)) + if (check_hash(str3_512, res, SHA3_512_HASH_LEN)) return -1; /* 4th input string. */ printf("test: %s.\n\n", str4_inp); rhash_sha3_224_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str4_inp, strlen(str4_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str4_inp, strlen(str4_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str4_224, res, sha3_224_hash_size)) + if (check_hash(str4_224, res, SHA3_224_HASH_LEN)) return -1; rhash_sha3_256_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str4_inp, strlen(str4_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str4_inp, strlen(str4_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str4_256, res, sha3_256_hash_size)) + if (check_hash(str4_256, res, SHA3_256_HASH_LEN)) return -1; rhash_sha3_384_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str4_inp, strlen(str4_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str4_inp, strlen(str4_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str4_384, res, sha3_384_hash_size)) + if (check_hash(str4_384, res, SHA3_384_HASH_LEN)) return -1; rhash_sha3_512_init(&ctx); - rhash_sha3_update(&ctx, (uint8_t *) str4_inp, strlen(str4_inp)); - rhash_sha3_final(&ctx, (uint8_t *) res); + rhash_sha3_update(&ctx, str4_inp, strlen(str4_inp)); + rhash_sha3_final(&ctx, res); - if (check_hash(str4_512, res, sha3_512_hash_size)) + if (check_hash(str4_512, res, SHA3_512_HASH_LEN)) return -1; return 0; diff --git a/src/tools/irm/irm_ipcp_bootstrap.c b/src/tools/irm/irm_ipcp_bootstrap.c index ac52c6c1..489f98b9 100644 --- a/src/tools/irm/irm_ipcp_bootstrap.c +++ b/src/tools/irm/irm_ipcp_bootstrap.c @@ -28,7 +28,8 @@ #include <sys/socket.h> #endif #include <ouroboros/irm.h> -#include <ouroboros/irm_config.h> +#include <ouroboros/ipcp.h> +#include <ouroboros/hash.h> #include "irm_ops.h" #include "irm_utils.h" @@ -55,11 +56,12 @@ static void usage(void) { - /* FIXME: Add dif_config stuff */ + /* FIXME: Add ipcp_config stuff */ printf("Usage: irm ipcp bootstrap\n" " name <ipcp name>\n" " dif <DIF name>\n" - " type [TYPE]\n\n" + " type [TYPE]\n" +/* FIXME: add option to set hash algorithm and length for directory */ "where TYPE = {" NORMAL " " LOCAL " " SHIM_UDP " " SHIM_ETH_LLC"}\n\n" "if TYPE == " NORMAL "\n" @@ -92,7 +94,7 @@ int do_bootstrap_ipcp(int argc, char ** argv) { char * name = NULL; pid_t api; - struct dif_config conf; + struct ipcp_config conf; uint8_t addr_size = DEFAULT_ADDR_SIZE; uint8_t cep_id_size = DEFAULT_CEP_ID_SIZE; uint8_t pdu_length_size = DEFAULT_PDU_LEN_SIZE; @@ -112,6 +114,7 @@ int do_bootstrap_ipcp(int argc, char ** argv) pid_t * apis = NULL; ssize_t len = 0; int i = 0; + uint16_t dir_hash_len = SHA3_256_HASH_LEN; while (argc > 0) { if (matches(*argv, "type") == 0) { @@ -177,6 +180,7 @@ int do_bootstrap_ipcp(int argc, char ** argv) } conf.dif_name = dif_name; + conf.dir_hash_len = dir_hash_len; if (strcmp(ipcp_type, NORMAL) == 0) { conf.type = IPCP_NORMAL; |