libgluonutil: add function that returns the mesh-prefix from site.conf

This commit is contained in:
Christof Schulze 2017-08-10 20:08:52 +02:00
parent 9136562517
commit a39260272d
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_mesh_prefix(struct in6_addr *result) {
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 rc = inet_pton(AF_INET6, prefix_addr, result);
free(prefix_addr);
json_object_put(site);
if (rc != 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_mesh_prefix(struct in6_addr *retval);
struct json_object * gluonutil_wrap_string(const char *str);
struct json_object * gluonutil_wrap_and_free_string(char *str);