X-Git-Url: https://git.decadent.org.uk/gitweb/?p=odhcp6c.git;a=blobdiff_plain;f=src%2Fscript.c;h=0e01f3aa6d094c2fb78d4b440b73660e91835942;hp=ff81278bf59b6f3e1dd1082634f5086aed1da505;hb=806ebbc1f4dd848c2d19b56f5be84db16fada612;hpb=4be3f666fde73be0d7046fac1ab8075aa5b1e8db diff --git a/src/script.c b/src/script.c index ff81278..0e01f3a 100644 --- a/src/script.c +++ b/src/script.c @@ -24,7 +24,7 @@ #include "odhcp6c.h" static const char hexdigits[] = "0123456789abcdef"; -static const char hexvals[] = { +static const int8_t hexvals[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, @@ -58,7 +58,8 @@ ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src) || y < 0 || (y = hexvals[y]) < 0) return -1; dst[c] = x << 4 | y; - while (*src < 0 || (*src && hexvals[(uint8_t)*src] < 0)) + while (((int8_t)*src) < 0 || + (*src && hexvals[(uint8_t)*src] < 0)) src++; } @@ -66,7 +67,7 @@ ssize_t script_unhexlify(uint8_t *dst, size_t len, const char *src) } -void script_hexlify(char *dst, const uint8_t *src, size_t len) { +static void script_hexlify(char *dst, const uint8_t *src, size_t len) { for (size_t i = 0; i < len; ++i) { *dst++ = hexdigits[src[i] >> 4]; *dst++ = hexdigits[src[i] & 0x0f]; @@ -128,28 +129,30 @@ static void bin_to_env(uint8_t *opts, size_t len) } -static void prefix_to_env(const char *name, const uint8_t *fqdn, size_t len) +static void entry_to_env(const char *name, const void *data, size_t len) { size_t buf_len = strlen(name); - struct dhcpv6_ia_prefix *p = NULL; - char *buf = realloc(NULL, buf_len + 2 + - (len / sizeof(*p)) * (INET6_ADDRSTRLEN + 32)); + const struct odhcp6c_entry *e = data; + const struct in6_addr any = IN6ADDR_ANY_INIT; + char *buf = realloc(NULL, buf_len + 2 + (len / sizeof(*e)) * 144); memcpy(buf, name, buf_len); buf[buf_len++] = '='; - uint16_t otype, olen; - uint8_t *odata; - dhcpv6_for_each_option(fqdn, &fqdn[len], otype, olen, odata) { - if (otype != DHCPV6_OPT_IA_PREFIX || olen + 4U < sizeof(*p)) - continue; - - p = (struct dhcpv6_ia_prefix*)&odata[-4]; - inet_ntop(AF_INET6, &p->addr, &buf[buf_len], INET6_ADDRSTRLEN); + for (size_t i = 0; i < len / sizeof(*e); ++i) { + inet_ntop(AF_INET6, &e[i].target, &buf[buf_len], INET6_ADDRSTRLEN); buf_len += strlen(&buf[buf_len]); - buf_len += snprintf(&buf[buf_len], 32, "/%hhu,%u,%u ", - p->prefix, ntohl(p->preferred), - ntohl(p->valid)); + buf_len += snprintf(&buf[buf_len], 6, "/%hhu", e[i].length); + if (!IN6_ARE_ADDR_EQUAL(&any, &e[i].router)) { + buf[buf_len++] = '@'; + inet_ntop(AF_INET6, &e[i].router, &buf[buf_len], INET6_ADDRSTRLEN); + buf_len += strlen(&buf[buf_len]); + } + buf_len += snprintf(&buf[buf_len], 24, ",%u,%u", e[i].preferred, e[i].valid); + if (e[i].priority) + buf_len += snprintf(&buf[buf_len], 12, ",%u", e[i].priority); + buf[buf_len++] = ' '; } + buf[buf_len - 1] = '\0'; putenv(buf); } @@ -158,22 +161,36 @@ static void prefix_to_env(const char *name, const uint8_t *fqdn, size_t len) void script_call(const char *status) { syslog(LOG_WARNING, "State for %s changed to %s", argv[1], status); - size_t dns_len, search_len, custom_len; + size_t dns_len, search_len, custom_len, sntp_ip_len, sntp_dns_len; + size_t sip_ip_len, sip_fqdn_len; + struct in6_addr *dns = odhcp6c_get_state(STATE_DNS, &dns_len); uint8_t *search = odhcp6c_get_state(STATE_SEARCH, &search_len); uint8_t *custom = odhcp6c_get_state(STATE_CUSTOM_OPTS, &custom_len); + struct in6_addr *sntp = odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len); + uint8_t *sntp_dns = odhcp6c_get_state(STATE_SNTP_FQDN, &sntp_dns_len); + struct in6_addr *sip = odhcp6c_get_state(STATE_SIP_IP, &sip_ip_len); + uint8_t *sip_fqdn = odhcp6c_get_state(STATE_SIP_FQDN, &sip_fqdn_len); - size_t prefix_len, lost_pd_len; + size_t prefix_len, address_len, ra_pref_len, ra_route_len; uint8_t *prefix = odhcp6c_get_state(STATE_IA_PD, &prefix_len); - uint8_t *lost_pd = odhcp6c_get_state(STATE_IA_PD_LOST, &lost_pd_len); + uint8_t *address = odhcp6c_get_state(STATE_IA_NA, &address_len); + uint8_t *ra_pref = odhcp6c_get_state(STATE_RA_PREFIX, &ra_pref_len); + uint8_t *ra_route = odhcp6c_get_state(STATE_RA_ROUTE, &ra_route_len); // Don't set environment before forking, because env is leaky. if (fork() == 0) { ipv6_to_env("RDNSS", dns, dns_len / sizeof(*dns)); + ipv6_to_env("SNTP_IP", sntp, sntp_ip_len / sizeof(*sntp)); + ipv6_to_env("SIP_IP", sip, sip_ip_len / sizeof(*sip)); fqdn_to_env("DOMAINS", search, search_len); + fqdn_to_env("SNTP_FQDN", sntp_dns, sntp_dns_len); + fqdn_to_env("SIP_DOMAIN", sip_fqdn, sip_fqdn_len); bin_to_env(custom, custom_len); - prefix_to_env("PREFIXES", prefix, prefix_len); - prefix_to_env("PREFIXES_LOST", lost_pd, lost_pd_len); + entry_to_env("PREFIXES", prefix, prefix_len); + entry_to_env("ADDRESSES", address, address_len); + entry_to_env("RA_ADDRESSES", ra_pref, ra_pref_len); + entry_to_env("RA_ROUTES", ra_route, ra_route_len); argv[2] = (char*)status; execv(argv[0], argv); @@ -181,6 +198,5 @@ void script_call(const char *status) } // Delete lost prefixes and user opts - odhcp6c_clear_state(STATE_IA_PD_LOST); odhcp6c_clear_state(STATE_CUSTOM_OPTS); }