From 842d091a9e7694ac4013265b794b8d6e880a0c00 Mon Sep 17 00:00:00 2001 From: Amin Ben Ramdhane Date: Fri, 29 May 2020 16:42:29 +0100 Subject: [PATCH] Device.IP. object: fix the get/set of IPv6Enable and IPv6Status parameters --- dmtree/tr181/ip.c | 23 +++++++++++++++++++++-- libbbf_api/dmcommon.c | 9 +++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/dmtree/tr181/ip.c b/dmtree/tr181/ip.c index 4291a0e1..1de3b258 100644 --- a/dmtree/tr181/ip.c +++ b/dmtree/tr181/ip.c @@ -186,18 +186,34 @@ static int get_IP_IPv6Capable(char *refparam, struct dmctx *ctx, void *data, cha static int get_IP_IPv6Enable(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - *value = "1"; + char buf[64] = {0}; + + *value = "0"; + int pp = dmcmd("sysctl", 1, "net.ipv6.conf.all.disable_ipv6"); + if (pp) { + int r = dmcmd_read(pp, buf, sizeof(buf)); + close(pp); + char *val = NULL; + if (r > 0 && (val = strchr(buf, '='))) + *value = (strcmp(val+2, "1") == 0) ? "0" : "1"; + } return 0; } static int set_IP_IPv6Enable(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) { + char buf[64] = {0}; + bool b; + switch (action) { case VALUECHECK: if (dm_validate_boolean(value)) return FAULT_9007; break; case VALUESET: + string_to_bool(value, &b); + snprintf(buf, sizeof(buf), "net.ipv6.conf.all.disable_ipv6=%d", b ? 0 : 1); + DMCMD("sysctl", 2, "-w", buf); break; } return 0; @@ -205,7 +221,10 @@ static int set_IP_IPv6Enable(char *refparam, struct dmctx *ctx, void *data, char static int get_IP_IPv6Status(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { - *value = "Enabled"; + char buf[16]; + + dm_read_sysfs_file("/proc/sys/net/ipv6/conf/all/disable_ipv6", buf, sizeof(buf)); + *value = (strcmp(buf, "1") == 0) ? "Disabled" : "Enabled"; return 0; } diff --git a/libbbf_api/dmcommon.c b/libbbf_api/dmcommon.c index 7b1eb36c..2a369295 100644 --- a/libbbf_api/dmcommon.c +++ b/libbbf_api/dmcommon.c @@ -201,6 +201,14 @@ bool is_strword_in_optionvalue(char *optionvalue, char *str) return false; } +void remove_new_line(char *buf) +{ + int len; + len = strlen(buf) - 1; + if (buf[len] == '\n') + buf[len] = 0; +} + int dmcmd(char *cmd, int n, ...) { va_list arg; @@ -303,6 +311,7 @@ int dmcmd_read(int pipe, char *buffer, int size) int rd; if (size < 2) return -1; if ((rd = read(pipe, buffer, (size-1))) > 0) { + remove_new_line(buffer); buffer[rd] = '\0'; return (rd + 1); } else {