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

This commit is contained in:
Christof Schulze 2017-08-10 20:08:52 +02:00 committed by Matthias Schiffer
parent a92cfa3194
commit 41ab551518
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
2 changed files with 38 additions and 1 deletions

View File

@ -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");
}

View File

@ -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);