+static void search_to_env(const char *name, const uint8_t *start, size_t len)
+{
+ size_t buf_len = strlen(name);
+ char *buf = realloc(NULL, buf_len + 2 + len);
+ char *c = mempcpy(buf, name, buf_len);
+ *c++ = '=';
+
+ for (struct odhcp6c_entry *e = (struct odhcp6c_entry*)start;
+ (uint8_t*)e < &start[len] && &e->auxtarget[e->auxlen] <= &start[len];
+ e = (struct odhcp6c_entry*)(&e->auxtarget[e->auxlen])) {
+ c = mempcpy(c, e->auxtarget, e->auxlen);
+ *c++ = ' ';
+ }
+
+ c[-1] = '\0';
+ putenv(buf);
+}
+
+
+static void int_to_env(const char *name, int value)
+{
+ size_t len = 12 + strlen(name);
+ char *buf = realloc(NULL, len);
+ snprintf(buf, len, "%s=%d", name, value);
+ putenv(buf);
+}
+
+
+static void s46_to_env_portparams(const uint8_t *data, size_t len, FILE *fp)
+{
+ uint8_t *odata;
+ uint16_t otype, olen;
+ dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
+ if (otype == DHCPV6_OPT_S46_PORTPARAMS &&
+ olen == sizeof(struct dhcpv6_s46_portparams)) {
+ struct dhcpv6_s46_portparams *params = (void*)odata;
+ fprintf(fp, "offset=%d,psidlen=%d,psid=%d,",
+ params->offset, params->psid_len, ntohs(params->psid));
+ }
+ }
+}
+
+
+static void s46_to_env(enum odhcp6c_state state, const uint8_t *data, size_t len)
+{
+ const char *name = (state == STATE_S46_MAPE) ? "MAPE" :
+ (state == STATE_S46_MAPT) ? "MAPT" : "LW4O6";
+
+ if (len == 0)
+ return;
+
+ char *str;
+ size_t strsize;
+
+ FILE *fp = open_memstream(&str, &strsize);
+ fputs(name, fp);
+ fputc('=', fp);
+
+ const char *type = (state == STATE_S46_MAPE) ? "map-e" :
+ (state == STATE_S46_MAPT) ? "map-t" : "lw4o6";
+
+ uint8_t *odata;
+ uint16_t otype, olen;
+ dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
+ struct dhcpv6_s46_rule *rule = (struct dhcpv6_s46_rule*)odata;
+ struct dhcpv6_s46_v4v6bind *bind = (struct dhcpv6_s46_v4v6bind*)odata;
+
+ if (state != STATE_S46_LW && otype == DHCPV6_OPT_S46_RULE &&
+ olen >= sizeof(struct dhcpv6_s46_rule)) {
+ char buf4[INET_ADDRSTRLEN];
+ char buf6[INET6_ADDRSTRLEN];
+ struct in6_addr in6 = IN6ADDR_ANY_INIT;
+
+ size_t prefix6len = rule->prefix6_len;
+ prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1;
+
+ if (olen < sizeof(struct dhcpv6_s46_rule) + prefix6len)
+ continue;
+
+ memcpy(&in6, rule->ipv6_prefix, prefix6len);
+
+ inet_ntop(AF_INET, &rule->ipv4_prefix, buf4, sizeof(buf4));
+ inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6));
+
+ if (rule->flags & 1)
+ fputs("fmr,", fp);
+
+ fprintf(fp, "type=%s,ealen=%d,prefix4len=%d,prefix6len=%d,ipv4prefix=%s,ipv6prefix=%s,",
+ type, rule->ea_len, rule->prefix4_len, rule->prefix6_len, buf4, buf6);
+
+ s46_to_env_portparams(&rule->ipv6_prefix[prefix6len],
+ olen - sizeof(*rule) - prefix6len, fp);
+
+ dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
+ if (state != STATE_S46_MAPT && otype == DHCPV6_OPT_S46_BR &&
+ olen == sizeof(struct in6_addr)) {
+ inet_ntop(AF_INET6, odata, buf6, sizeof(buf6));
+ fprintf(fp, "br=%s,", buf6);
+ } else if (state == STATE_S46_MAPT && otype == DHCPV6_OPT_S46_DMR &&
+ olen >= sizeof(struct dhcpv6_s46_dmr)) {
+ struct dhcpv6_s46_dmr *dmr = (struct dhcpv6_s46_dmr*)odata;
+ memset(&in6, 0, sizeof(in6));
+ size_t prefix6len = dmr->dmr_prefix6_len;
+ prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1;
+
+ if (olen < sizeof(struct dhcpv6_s46_dmr) + prefix6len)
+ continue;
+
+ memcpy(&in6, dmr->dmr_ipv6_prefix, prefix6len);
+ inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6));
+ fprintf(fp, "dmr=%s/%d,", buf6, dmr->dmr_prefix6_len);
+ }
+ }
+
+ fputc(' ', fp);
+ } else if (state == STATE_S46_LW && otype == DHCPV6_OPT_S46_V4V6BIND &&
+ olen >= sizeof(struct dhcpv6_s46_v4v6bind)) {
+ char buf4[INET_ADDRSTRLEN];
+ char buf6[INET6_ADDRSTRLEN];
+ struct in6_addr in6 = IN6ADDR_ANY_INIT;
+
+ size_t prefix6len = bind->bindprefix6_len;
+ prefix6len = (prefix6len % 8 == 0) ? prefix6len / 8 : prefix6len / 8 + 1;
+
+ if (olen < sizeof(struct dhcpv6_s46_v4v6bind) + prefix6len)
+ continue;
+
+ memcpy(&in6, bind->bind_ipv6_prefix, prefix6len);
+
+ inet_ntop(AF_INET, &bind->ipv4_address, buf4, sizeof(buf4));
+ inet_ntop(AF_INET6, &in6, buf6, sizeof(buf6));
+
+ fprintf(fp, "type=%s,prefix4len=32,prefix6len=%d,ipv4prefix=%s,ipv6prefix=%s,",
+ type, bind->bindprefix6_len, buf4, buf6);
+
+ s46_to_env_portparams(&bind->bind_ipv6_prefix[prefix6len],
+ olen - sizeof(*bind) - prefix6len, fp);
+
+ dhcpv6_for_each_option(data, &data[len], otype, olen, odata) {
+ if (otype == DHCPV6_OPT_S46_BR && olen == sizeof(struct in6_addr)) {
+ inet_ntop(AF_INET6, odata, buf6, sizeof(buf6));
+ fprintf(fp, "br=%s,", buf6);
+ }
+ }
+
+ fputc(' ', fp);
+ }
+ }
+
+ fclose(fp);
+ putenv(str);
+}
+
+
+void script_call(const char *status, int delay, bool resume)