From 41ab5515185bf92ce47356821f430ad858b3171d Mon Sep 17 00:00:00 2001 From: Christof Schulze Date: Thu, 10 Aug 2017 20:08:52 +0200 Subject: [PATCH] libgluonutil: add function that retrieves the node prefix from site.conf --- package/libgluonutil/src/libgluonutil.c | 34 ++++++++++++++++++++++++- package/libgluonutil/src/libgluonutil.h | 5 ++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/package/libgluonutil/src/libgluonutil.c b/package/libgluonutil/src/libgluonutil.c index 5932c544..db46b9c9 100644 --- a/package/libgluonutil/src/libgluonutil.c +++ b/package/libgluonutil/src/libgluonutil.c @@ -27,7 +27,7 @@ #include "libgluonutil.h" #include - +#include #include #include #include @@ -109,6 +109,38 @@ struct json_object * gluonutil_wrap_and_free_string(char *str) { return ret; } + +bool gluonutil_get_node_prefix6(struct in6_addr *prefix) { + struct json_object *site = gluonutil_load_site_config(); + if (!site) + return false; + + struct json_object *node_prefix = NULL; + if (!json_object_object_get_ex(site, "node_prefix6", &node_prefix)) { + json_object_put(site); + return false; + } + + const char *str_prefix = json_object_get_string(node_prefix); + if (!str_prefix) { + json_object_put(site); + return false; + } + + char *prefix_addr = strndup(str_prefix, strchrnul(str_prefix, '/')-str_prefix); + + int ret = inet_pton(AF_INET6, prefix_addr, prefix); + + free(prefix_addr); + json_object_put(site); + + if (ret != 1) + return false; + + return true; +} + + struct json_object * gluonutil_load_site_config(void) { return json_object_from_file("/lib/gluon/site.json"); } diff --git a/package/libgluonutil/src/libgluonutil.h b/package/libgluonutil/src/libgluonutil.h index 96195281..b2f90d69 100644 --- a/package/libgluonutil/src/libgluonutil.h +++ b/package/libgluonutil/src/libgluonutil.h @@ -27,10 +27,15 @@ #ifndef _LIBGLUON_LIBGLUON_H_ #define _LIBGLUON_LIBGLUON_H_ +#include +#include + + char * gluonutil_read_line(const char *filename); char * gluonutil_get_sysconfig(const char *key); char * gluonutil_get_node_id(void); char * gluonutil_get_interface_address(const char *ifname); +bool gluonutil_get_node_prefix6(struct in6_addr *prefix); struct json_object * gluonutil_wrap_string(const char *str); struct json_object * gluonutil_wrap_and_free_string(char *str);