X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=src%2Fscript.c;h=a56d9b4df42c62947ec9d9c5d6b1f05668aa70da;hb=d09e94fff82440cedf04f2e0cfe1ede8a0ab0600;hp=f62ea5ef319fe965f489fd4456e77547ada56626;hpb=285abfbe5dbad63c499252df829332b969d4b746;p=odhcp6c.git diff --git a/src/script.c b/src/script.c index f62ea5e..a56d9b4 100644 --- a/src/script.c +++ b/src/script.c @@ -1,5 +1,5 @@ /** - * Copyright (C) 2012 Steven Barth + * Copyright (C) 2012-2013 Steven Barth * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License v2 as published by @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,7 @@ static const int8_t hexvals[] = { static char *argv[4] = {NULL, NULL, NULL, NULL}; +static volatile char *delayed_call = NULL; int script_init(const char *path, const char *ifname) @@ -128,8 +130,14 @@ static void bin_to_env(uint8_t *opts, size_t len) } } +enum entry_type { + ENTRY_ADDRESS, + ENTRY_HOST, + ENTRY_ROUTE, + ENTRY_PREFIX +}; -static void entry_to_env(const char *name, const void *data, size_t len, bool host, bool route) +static void entry_to_env(const char *name, const void *data, size_t len, enum entry_type type) { size_t buf_len = strlen(name); const struct odhcp6c_entry *e = data; @@ -140,20 +148,27 @@ static void entry_to_env(const char *name, const void *data, size_t len, bool ho 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]); - if (!host) { + if (type != ENTRY_HOST) { buf_len += snprintf(&buf[buf_len], 6, "/%hhu", e[i].length); - if (route) { + if (type == ENTRY_ROUTE) { buf[buf_len++] = ','; if (!IN6_IS_ADDR_UNSPECIFIED(&e[i].router)) { 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", e[i].valid); + buf_len += snprintf(&buf[buf_len], 12, ",%u", e[i].priority); } else { 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); + + if (type == ENTRY_PREFIX && e[i].priority) { + // priority and router are abused for prefix exclusion + 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", e[i].priority); + } } buf[buf_len++] = ' '; } @@ -163,12 +178,32 @@ static void entry_to_env(const char *name, const void *data, size_t len, bool ho } +static void script_call_delayed(int signal __attribute__((unused))) +{ + if (delayed_call) + script_call((char*)delayed_call); +} + + +void script_delay_call(const char *status, int timeout) +{ + if (!delayed_call) { + delayed_call = strdup(status); + signal(SIGALRM, script_call_delayed); + alarm(timeout); + } +} + + 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, sntp_ip_len, sntp_dns_len; size_t sip_ip_len, sip_fqdn_len; + odhcp6c_expire(); + if (delayed_call) + alarm(0); + 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); @@ -193,11 +228,11 @@ void script_call(const char *status) 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); - entry_to_env("PREFIXES", prefix, prefix_len, false, false); - entry_to_env("ADDRESSES", address, address_len, false, false); - entry_to_env("RA_ADDRESSES", ra_pref, ra_pref_len, false, false); - entry_to_env("RA_ROUTES", ra_route, ra_route_len, false, true); - entry_to_env("RA_DNS", ra_dns, ra_dns_len, true, false); + entry_to_env("PREFIXES", prefix, prefix_len, ENTRY_PREFIX); + entry_to_env("ADDRESSES", address, address_len, ENTRY_ADDRESS); + entry_to_env("RA_ADDRESSES", ra_pref, ra_pref_len, ENTRY_ADDRESS); + entry_to_env("RA_ROUTES", ra_route, ra_route_len, ENTRY_ROUTE); + entry_to_env("RA_DNS", ra_dns, ra_dns_len, ENTRY_HOST); argv[2] = (char*)status; execv(argv[0], argv);