From 8e50917e51f7f21c53f9021063670065c8fa06d8 Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Sat, 31 Aug 2013 10:48:56 +0200 Subject: [PATCH] Merge preliminary prefix class support --- CMakeLists.txt | 4 ++++ src/dhcpv6.c | 26 +++++++++++++++----------- src/odhcp6c.c | 2 +- src/odhcp6c.h | 8 +++++--- src/script.c | 6 +++--- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index db7ef65..8112859 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,10 @@ set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -std=c99") add_definitions(-D_GNU_SOURCE -Wall -Werror -Wextra -pedantic) +if(${EXT_PREFIX_CLASS}) + add_definitions(-DEXT_PREFIX_CLASS=${EXT_PREFIX_CLASS}) +endif(${EXT_PREFIX_CLASS}) + add_executable(odhcp6c src/odhcp6c.c src/dhcpv6.c src/ra.c src/script.c src/md5.c) target_link_libraries(odhcp6c resolv) diff --git a/src/dhcpv6.c b/src/dhcpv6.c index fe4cc0b..785b57b 100644 --- a/src/dhcpv6.c +++ b/src/dhcpv6.c @@ -145,7 +145,9 @@ int init_dhcpv6(const char *ifname, int request_pd) htons(DHCPV6_OPT_SIP_SERVER_A), htons(DHCPV6_OPT_AFTR_NAME), htons(DHCPV6_OPT_PD_EXCLUDE), +#ifdef EXT_PREFIX_CLASS htons(DHCPV6_OPT_PREFIX_CLASS), +#endif }; odhcp6c_add_state(STATE_ORO, oro, sizeof(oro)); @@ -831,8 +833,8 @@ static uint32_t dhcpv6_parse_ia(void *opt, void *end) // Update address IA dhcpv6_for_each_option(opt, end, otype, olen, odata) { - struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, - 0, 0, IN6ADDR_ANY_INIT, 0, 0, 0}; + struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, 0, 0, + IN6ADDR_ANY_INIT, 0, 0, 0}; if (otype == DHCPV6_OPT_IA_PREFIX) { struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4]; @@ -850,11 +852,13 @@ static uint32_t dhcpv6_parse_ia(void *opt, void *end) uint16_t stype, slen; uint8_t *sdata; +#ifdef EXT_PREFIX_CLASS // Find prefix class, if any dhcpv6_for_each_option(&prefix[1], odata + olen, - stype, slen, sdata) - if (stype == DHCPV6_OPT_PREFIX_CLASS && slen == 2) - entry.prefix_class = ntohs(*((uint16_t*)sdata)); + stype, slen, sdata) + if (stype == DHCPV6_OPT_PREFIX_CLASS && slen == 2) + entry.class = sdata[0] << 8 | sdata[1]; +#endif // Parse PD-exclude bool ok = true; @@ -912,18 +916,18 @@ static uint32_t dhcpv6_parse_ia(void *opt, void *end) entry.length = 128; entry.target = addr->addr; +#ifdef EXT_PREFIX_CLASS uint16_t stype, slen; uint8_t *sdata; - // Find prefix class, if any dhcpv6_for_each_option(&addr[1], odata + olen, - stype, slen, sdata) - if (stype == DHCPV6_OPT_PREFIX_CLASS && slen == 2) - entry.prefix_class = ntohs(*((uint16_t*)sdata)); - + stype, slen, sdata) + if (stype == DHCPV6_OPT_PREFIX_CLASS && slen == 2) + entry.class = sdata[0] << 8 | sdata[1]; +#endif + odhcp6c_update_entry(STATE_IA_NA, &entry); } - if (entry.valid > 0 && timeout > entry.valid) timeout = entry.valid; } diff --git a/src/odhcp6c.c b/src/odhcp6c.c index b2c6b98..d35da2e 100644 --- a/src/odhcp6c.c +++ b/src/odhcp6c.c @@ -459,7 +459,7 @@ void odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *n if (x) { x->valid = new->valid; x->preferred = new->preferred; - x->prefix_class = new->prefix_class; + x->class = new->class; } else { odhcp6c_add_state(state, new, sizeof(*new)); } diff --git a/src/odhcp6c.h b/src/odhcp6c.h index e0f6f62..eb76b6b 100644 --- a/src/odhcp6c.h +++ b/src/odhcp6c.h @@ -53,8 +53,10 @@ enum dhcvp6_opt { DHCPV6_OPT_SIP_SERVER_A = 22, DHCPV6_OPT_AFTR_NAME = 64, DHCPV6_OPT_PD_EXCLUDE = 67, - /* draft-bhandari-dhc-class-based-prefix */ - DHCPV6_OPT_PREFIX_CLASS = 200, /* NOT STANDARDIZED! */ +#ifdef EXT_PREFIX_CLASS + /* draft-bhandari-dhc-class-based-prefix, not yet standardized */ + DHCPV6_OPT_PREFIX_CLASS = EXT_PREFIX_CLASS, +#endif }; enum dhcpv6_opt_npt { @@ -217,7 +219,7 @@ struct odhcp6c_entry { struct in6_addr target; uint32_t valid; uint32_t preferred; - uint32_t prefix_class; + uint16_t class; }; diff --git a/src/script.c b/src/script.c index 74aea64..e5b8341 100644 --- a/src/script.c +++ b/src/script.c @@ -187,9 +187,9 @@ static void entry_to_env(const char *name, const void *data, size_t len, enum en } else { buf_len += snprintf(&buf[buf_len], 24, ",%u,%u", e[i].preferred, e[i].valid); } - if ((type == ENTRY_PREFIX || type == ENTRY_ADDRESS) && e[i].prefix_class) { - buf_len += snprintf(&buf[buf_len], 12, ",class=%u", e[i].prefix_class); - } + + if ((type == ENTRY_PREFIX || type == ENTRY_ADDRESS) && e[i].class) + buf_len += snprintf(&buf[buf_len], 12, ",class=%u", e[i].class); if (type == ENTRY_PREFIX && e[i].priority) { // priority and router are abused for prefix exclusion -- 2.30.2