From 057dbebb24cf4a9d0b81219bdfb2caa687fdc1c8 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sat, 10 Nov 2018 21:36:50 +0000 Subject: [PATCH 1/2] Remove redundant code: prefix is always NULL --- arch/platform/native/platform.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/arch/platform/native/platform.c b/arch/platform/native/platform.c index d00efaac1..330380cd8 100644 --- a/arch/platform/native/platform.c +++ b/arch/platform/native/platform.c @@ -197,15 +197,11 @@ static void set_global_address(void) { static uip_ipaddr_t ipaddr; - static uip_ipaddr_t *prefix = NULL; /* Assign a unique local address (RFC4193, http://tools.ietf.org/html/rfc4193). */ - if(prefix == NULL) { - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); - } else { - memcpy(&ipaddr, prefix, 8); - } + uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + /* Assumes that the uip_lladdr is set */ uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); From 15f6e1d7a300a6077639032f0d95b7b4478d44dd Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sat, 10 Nov 2018 23:33:31 +0000 Subject: [PATCH 2/2] Change the way of accessing the default v6 prefix This commit changes the method of accessing the default v6 prefix. Instead of always using `UIP_DS6_DEFAULT_PREFIX`, we store the default prefix in a variable. We subsequently retrieve the prefix by calling `uip_ds6_default_prefix()`. `uip_ds6_init()` will set this variable to the value of `UIP_DS6_DEFAULT_PREFIX`, but only if the startup code has not set a different default prefix before `uip_ds6_init()` gets called. This approach has the following benefits: * It allows us to change the default prefix at run time. * It allows the startup code to set a prefix different than the one specified by `UIP_DS6_DEFAULT_PREFIX`, which can be useful if the default prefix comes from a different source (e.g. the command line for native, or a configuration stored on a node's flash) * In many places the current code assumes that the prefix contains 6 bytes of zeros (e.g. `FDxx::/64`). Changing to a different prefix (e.g. `FD00:ABCD::`) would need extensive code changes. This change here makes it easy to use a prefix of any length. --- arch/platform/native/platform.c | 8 +++++--- examples/multicast/sink.c | 3 ++- os/net/ipv6/uip-ds6.c | 19 +++++++++++++++++++ os/net/ipv6/uip-ds6.h | 16 ++++++++++++++++ os/net/routing/routing.h | 2 +- os/net/routing/rpl-classic/rpl-dag-root.c | 9 +++++++-- os/net/routing/rpl-lite/rpl-dag-root.c | 5 ++++- os/net/routing/rpl-lite/rpl-dag-root.h | 2 +- os/services/shell/shell-commands.c | 3 ++- .../code-ipv6/receiver/udp-receiver.c | 5 ++++- .../code-ipv6/sender/unicast-sender.c | 5 ++++- tests/09-ipv6/code/node.c | 4 +++- tests/14-rpl-lite/code/receiver-node.c | 3 ++- tests/14-rpl-lite/code/sender-node.c | 12 ++++++++++-- tests/15-rpl-classic/code/receiver-node.c | 3 ++- tests/15-rpl-classic/code/sender-node.c | 12 ++++++++++-- 16 files changed, 92 insertions(+), 19 deletions(-) diff --git a/arch/platform/native/platform.c b/arch/platform/native/platform.c index 330380cd8..b2c25e223 100644 --- a/arch/platform/native/platform.c +++ b/arch/platform/native/platform.c @@ -196,11 +196,12 @@ set_lladdr(void) static void set_global_address(void) { - static uip_ipaddr_t ipaddr; + uip_ipaddr_t ipaddr; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); /* Assign a unique local address (RFC4193, http://tools.ietf.org/html/rfc4193). */ - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&ipaddr, default_prefix); /* Assumes that the uip_lladdr is set */ uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); @@ -211,7 +212,8 @@ set_global_address(void) LOG_INFO_("\n"); /* set the PREFIX::1 address to the IF */ - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 1); + uip_ip6addr_copy(&ipaddr, default_prefix); + ipaddr.u8[15] = 1; uip_ds6_defrt_add(&ipaddr, 0); } #endif diff --git a/examples/multicast/sink.c b/examples/multicast/sink.c index 49180b69a..2b6f48846 100644 --- a/examples/multicast/sink.c +++ b/examples/multicast/sink.c @@ -80,9 +80,10 @@ join_mcast_group(void) { uip_ipaddr_t addr; uip_ds6_maddr_t *rv; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); /* First, set our v6 global */ - uip_ip6addr(&addr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&addr, default_prefix); uip_ds6_set_addr_iid(&addr, &uip_lladdr); uip_ds6_addr_add(&addr, 0, ADDR_AUTOCONF); diff --git a/os/net/ipv6/uip-ds6.c b/os/net/ipv6/uip-ds6.c index 4e82b3745..bcfb9b0c8 100644 --- a/os/net/ipv6/uip-ds6.c +++ b/os/net/ipv6/uip-ds6.c @@ -95,10 +95,29 @@ static uip_ds6_prefix_t *locprefix; static const uint8_t iid_prefix[] = { 0x00, 0x00 , 0x00 , 0xff , 0xfe , 0x00 }; #endif /* (UIP_LLADDR_LEN == 2) */ +/* The default prefix */ +static uip_ip6addr_t default_prefix = { + .u16 = { 0, 0, 0, 0, 0, 0, 0, 0 } +}; +/*---------------------------------------------------------------------------*/ +const uip_ip6addr_t * +uip_ds6_default_prefix() +{ + return &default_prefix; +} +/*---------------------------------------------------------------------------*/ +void +uip_ds6_set_default_prefix(const uip_ip6addr_t *prefix) +{ + uip_ip6addr_copy(&default_prefix, prefix); +} /*---------------------------------------------------------------------------*/ void uip_ds6_init(void) { + if(uip_is_addr_unspecified(&default_prefix)) { + uip_ip6addr(&default_prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + } uip_ds6_neighbors_init(); uip_ds6_route_init(); diff --git a/os/net/ipv6/uip-ds6.h b/os/net/ipv6/uip-ds6.h index 45394fec3..0edc1fd6d 100644 --- a/os/net/ipv6/uip-ds6.h +++ b/os/net/ipv6/uip-ds6.h @@ -296,6 +296,22 @@ uip_ds6_prefix_t *uip_ds6_prefix_lookup(uip_ipaddr_t *ipaddr, uint8_t ipaddrlen); uint8_t uip_ds6_is_addr_onlink(uip_ipaddr_t *ipaddr); +/** + * \brief Retrieve the Default IPv6 prefix + * \retval A pointer to the default prefix + */ +const uip_ip6addr_t *uip_ds6_default_prefix(void); + +/** + * \brief Set the Default IPv6 prefix + * \param prefix A pointer to the new default prefix + * + * uip_ds6_init() will set the default prefix to UIP_DS6_DEFAULT_PREFIX + * unless this function here has been called beforehand to set a new default + * prefix. + */ +void uip_ds6_set_default_prefix(const uip_ip6addr_t *prefix); + /** @} */ /** \name Unicast address list basic routines */ diff --git a/os/net/routing/routing.h b/os/net/routing/routing.h index 4508f0c4e..3cee3d9b2 100644 --- a/os/net/routing/routing.h +++ b/os/net/routing/routing.h @@ -57,7 +57,7 @@ struct routing_driver { /** * Set the prefix, for nodes that will operate as root * - * \param prefix The prefix. If NULL, UIP_DS6_DEFAULT_PREFIX is used instead + * \param prefix The prefix. If NULL, uip_ds6_default_prefix() is used instead * \param iid The IID. If NULL, it will be built from uip_ds6_set_addr_iid. */ void (* root_set_prefix)(uip_ipaddr_t *prefix, uip_ipaddr_t *iid); diff --git a/os/net/routing/rpl-classic/rpl-dag-root.c b/os/net/routing/rpl-classic/rpl-dag-root.c index 3a0162627..f45c2d317 100644 --- a/os/net/routing/rpl-classic/rpl-dag-root.c +++ b/os/net/routing/rpl-classic/rpl-dag-root.c @@ -48,12 +48,15 @@ static void set_global_address(uip_ipaddr_t *prefix, uip_ipaddr_t *iid) { static uip_ipaddr_t root_ipaddr; + const uip_ipaddr_t *default_prefix; int i; + default_prefix = uip_ds6_default_prefix(); + /* Assign a unique local address (RFC4193, http://tools.ietf.org/html/rfc4193). */ if(prefix == NULL) { - uip_ip6addr(&root_ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&root_ipaddr, default_prefix); } else { memcpy(&root_ipaddr, prefix, 8); } @@ -115,9 +118,11 @@ rpl_dag_root_start(void) if(root_if != NULL) { rpl_dag_t *dag; uip_ipaddr_t prefix; + const uip_ipaddr_t *default_prefix; rpl_set_root(RPL_DEFAULT_INSTANCE, ipaddr); dag = rpl_get_any_dag(); + default_prefix = uip_ds6_default_prefix(); /* If there are routes in this dag, we remove them all as we are from now on the new dag root and the old routes are wrong */ @@ -129,7 +134,7 @@ rpl_dag_root_start(void) dag->instance->def_route = NULL; } - uip_ip6addr(&prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&prefix, default_prefix); rpl_set_prefix(dag, &prefix, 64); LOG_INFO("root_set_prefix: created a new RPL dag\n"); return 0; diff --git a/os/net/routing/rpl-lite/rpl-dag-root.c b/os/net/routing/rpl-lite/rpl-dag-root.c index c3e356828..38f5697e7 100644 --- a/os/net/routing/rpl-lite/rpl-dag-root.c +++ b/os/net/routing/rpl-lite/rpl-dag-root.c @@ -76,13 +76,16 @@ static void set_global_address(uip_ipaddr_t *prefix, uip_ipaddr_t *iid) { static uip_ipaddr_t root_ipaddr; + const uip_ipaddr_t *default_prefix; int i; uint8_t state; + default_prefix = uip_ds6_default_prefix(); + /* Assign a unique local address (RFC4193, http://tools.ietf.org/html/rfc4193). */ if(prefix == NULL) { - uip_ip6addr(&root_ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&root_ipaddr, default_prefix); } else { memcpy(&root_ipaddr, prefix, 8); } diff --git a/os/net/routing/rpl-lite/rpl-dag-root.h b/os/net/routing/rpl-lite/rpl-dag-root.h index 71b999765..f035e8595 100644 --- a/os/net/routing/rpl-lite/rpl-dag-root.h +++ b/os/net/routing/rpl-lite/rpl-dag-root.h @@ -45,7 +45,7 @@ /** * Set a prefix in case the node is later set as dag root. * - * \param prefix The prefix. If NULL, UIP_DS6_DEFAULT_PREFIX is used instead + * \param prefix The prefix. If NULL, uip_ds6_default_prefix() is used instead * \param iid The IID. If NULL, it will be built from uip_ds6_set_addr_iid. */ void rpl_dag_root_set_prefix(uip_ipaddr_t *prefix, uip_ipaddr_t *iid); diff --git a/os/services/shell/shell-commands.c b/os/services/shell/shell-commands.c index c8b7d862e..851743a0d 100644 --- a/os/services/shell/shell-commands.c +++ b/os/services/shell/shell-commands.c @@ -397,7 +397,8 @@ PT_THREAD(cmd_rpl_set_root(struct pt *pt, shell_output_func output, char *args)) PT_EXIT(pt); } } else { - uip_ip6addr(&prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&prefix, default_prefix); } if(is_on) { diff --git a/tests/07-simulation-base/code-ipv6/receiver/udp-receiver.c b/tests/07-simulation-base/code-ipv6/receiver/udp-receiver.c index e84c57637..0a3b16b15 100644 --- a/tests/07-simulation-base/code-ipv6/receiver/udp-receiver.c +++ b/tests/07-simulation-base/code-ipv6/receiver/udp-receiver.c @@ -36,10 +36,13 @@ PROCESS_THREAD(udp_process, ev, data) static struct etimer send_timer; uip_ipaddr_t addr; static int alive; + const uip_ipaddr_t *default_prefix; PROCESS_BEGIN(); - uip_ip6addr(&addr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 2); + default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&addr, default_prefix); + addr.u16[7] = UIP_HTONS(2); uip_ds6_addr_add(&addr, 0, ADDR_AUTOCONF); simple_udp_register(&broadcast_connection, UDP_PORT, diff --git a/tests/07-simulation-base/code-ipv6/sender/unicast-sender.c b/tests/07-simulation-base/code-ipv6/sender/unicast-sender.c index 4e2e664b3..e56daa342 100644 --- a/tests/07-simulation-base/code-ipv6/sender/unicast-sender.c +++ b/tests/07-simulation-base/code-ipv6/sender/unicast-sender.c @@ -51,12 +51,15 @@ PROCESS_THREAD(udp_process, ev, data) etimer_set(&periodic_timer, SEND_INTERVAL); while(1) { + const uip_ipaddr_t *default_prefix; uint8_t buf[SIZE]; PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer)); etimer_reset(&periodic_timer); printf("Sending unicast\n"); - uip_ip6addr(&addr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 2); + default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&addr, default_prefix); + addr.u16[7] = UIP_HTONS(2); simple_udp_sendto(&broadcast_connection, buf, sizeof(buf), &addr); } diff --git a/tests/09-ipv6/code/node.c b/tests/09-ipv6/code/node.c index f465e2642..a99261865 100644 --- a/tests/09-ipv6/code/node.c +++ b/tests/09-ipv6/code/node.c @@ -44,6 +44,7 @@ PROCESS_THREAD(node_process, ev, data) static struct etimer et; #if WITH_ULA static uip_ipaddr_t ipaddr; + const uip_ipaddr_t *default_prefix; #endif /* WITH_ULA */ #if WITH_TSCH @@ -60,7 +61,8 @@ PROCESS_THREAD(node_process, ev, data) #endif /* WITH_TSCH */ #if WITH_ULA - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&ipaddr, default_prefix); uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); diff --git a/tests/14-rpl-lite/code/receiver-node.c b/tests/14-rpl-lite/code/receiver-node.c index b5265d3f7..b5cd26a15 100644 --- a/tests/14-rpl-lite/code/receiver-node.c +++ b/tests/14-rpl-lite/code/receiver-node.c @@ -73,8 +73,9 @@ set_global_address(void) static uip_ipaddr_t ipaddr; int i; uint8_t state; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&ipaddr, default_prefix); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); diff --git a/tests/14-rpl-lite/code/sender-node.c b/tests/14-rpl-lite/code/sender-node.c index 350758110..00bd6d5c5 100644 --- a/tests/14-rpl-lite/code/sender-node.c +++ b/tests/14-rpl-lite/code/sender-node.c @@ -71,8 +71,9 @@ set_global_address(void) uip_ipaddr_t ipaddr; int i; uint8_t state; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&ipaddr, default_prefix); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); @@ -92,6 +93,7 @@ PROCESS_THREAD(sender_node_process, ev, data) static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t addr; + const uip_ipaddr_t *default_prefix; PROCESS_BEGIN(); @@ -109,7 +111,13 @@ PROCESS_THREAD(sender_node_process, ev, data) PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); - uip_ip6addr(&addr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0x0201, 0x001, 0x001, 0x001); + default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&addr, default_prefix); + + addr.u16[4] = UIP_HTONS(0x0201); + addr.u16[5] = UIP_HTONS(0x0001); + addr.u16[6] = UIP_HTONS(0x0001); + addr.u16[7] = UIP_HTONS(0x0001); { static unsigned int message_number; diff --git a/tests/15-rpl-classic/code/receiver-node.c b/tests/15-rpl-classic/code/receiver-node.c index 798aea84d..3854784d0 100644 --- a/tests/15-rpl-classic/code/receiver-node.c +++ b/tests/15-rpl-classic/code/receiver-node.c @@ -73,8 +73,9 @@ set_global_address(void) static uip_ipaddr_t ipaddr; int i; uint8_t state; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&ipaddr, default_prefix); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); diff --git a/tests/15-rpl-classic/code/sender-node.c b/tests/15-rpl-classic/code/sender-node.c index 350758110..00bd6d5c5 100644 --- a/tests/15-rpl-classic/code/sender-node.c +++ b/tests/15-rpl-classic/code/sender-node.c @@ -71,8 +71,9 @@ set_global_address(void) uip_ipaddr_t ipaddr; int i; uint8_t state; + const uip_ipaddr_t *default_prefix = uip_ds6_default_prefix(); - uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0); + uip_ip6addr_copy(&ipaddr, default_prefix); uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); @@ -92,6 +93,7 @@ PROCESS_THREAD(sender_node_process, ev, data) static struct etimer periodic_timer; static struct etimer send_timer; uip_ipaddr_t addr; + const uip_ipaddr_t *default_prefix; PROCESS_BEGIN(); @@ -109,7 +111,13 @@ PROCESS_THREAD(sender_node_process, ev, data) PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer)); - uip_ip6addr(&addr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0x0201, 0x001, 0x001, 0x001); + default_prefix = uip_ds6_default_prefix(); + uip_ip6addr_copy(&addr, default_prefix); + + addr.u16[4] = UIP_HTONS(0x0201); + addr.u16[5] = UIP_HTONS(0x0001); + addr.u16[6] = UIP_HTONS(0x0001); + addr.u16[7] = UIP_HTONS(0x0001); { static unsigned int message_number;