ソースを参照

libgluonutil: add function that retrieves the node prefix from site.conf

Christof Schulze 6 年 前
コミット
41ab551518

+ 33 - 1
package/libgluonutil/src/libgluonutil.c

@@ -27,7 +27,7 @@
 #include "libgluonutil.h"
 
 #include <json-c/json.h>
-
+#include <arpa/inet.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -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");
 }

+ 5 - 0
package/libgluonutil/src/libgluonutil.h

@@ -27,10 +27,15 @@
 #ifndef _LIBGLUON_LIBGLUON_H_
 #define _LIBGLUON_LIBGLUON_H_
 
+#include <netinet/in.h>
+#include <stdbool.h>
+
+
 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);