From f9cb6ec2fa2895fb206befda0eb4506e85ef5537 Mon Sep 17 00:00:00 2001 From: Adam Dunkels Date: Sat, 16 Nov 2013 14:42:57 +0100 Subject: [PATCH 1/2] Added the const keyword to IP address arguments that are not (and should not be) changed by the callee --- core/net/nbr-table.c | 6 +++--- core/net/nbr-table.h | 2 +- core/net/rpl/rpl-dag.c | 8 ++++---- core/net/rpl/rpl.h | 2 +- core/net/sicslowpan.c | 2 +- core/net/tcpip.c | 6 +++--- core/net/tcpip.h | 4 ++-- core/net/uip-ds6-nbr.c | 24 ++++++++++++------------ core/net/uip-ds6-nbr.h | 14 +++++++------- core/net/uip-ds6-route.c | 4 ++-- core/net/uip-nd6.c | 4 ++-- examples/ravenusbstick/fakeuip.c | 4 ++-- 12 files changed, 40 insertions(+), 40 deletions(-) diff --git a/core/net/nbr-table.c b/core/net/nbr-table.c index f5d53c333..d8eaaa385 100644 --- a/core/net/nbr-table.c +++ b/core/net/nbr-table.c @@ -86,7 +86,7 @@ index_from_key(nbr_table_key_t *key) /*---------------------------------------------------------------------------*/ /* Get the neighbor index of an item */ static int -index_from_item(nbr_table_t *table, nbr_table_item_t *item) +index_from_item(nbr_table_t *table, const nbr_table_item_t *item) { return table != NULL && item != NULL ? ((int)((char *)item - (char *)table->data)) / table->item_size : -1; } @@ -100,7 +100,7 @@ item_from_key(nbr_table_t *table, nbr_table_key_t *key) /*---------------------------------------------------------------------------*/ /* Get the key af an item */ static nbr_table_key_t * -key_from_item(nbr_table_t *table, nbr_table_item_t *item) +key_from_item(nbr_table_t *table, const nbr_table_item_t *item) { return key_from_index(index_from_item(table, item)); } @@ -343,7 +343,7 @@ nbr_table_unlock(nbr_table_t *table, void *item) /*---------------------------------------------------------------------------*/ /* Get link-layer address of an item */ rimeaddr_t * -nbr_table_get_lladdr(nbr_table_t *table, void *item) +nbr_table_get_lladdr(nbr_table_t *table, const void *item) { nbr_table_key_t *key = key_from_item(table, item); return key != NULL ? &key->lladdr : NULL; diff --git a/core/net/nbr-table.h b/core/net/nbr-table.h index 113db94b6..763c0d2a5 100644 --- a/core/net/nbr-table.h +++ b/core/net/nbr-table.h @@ -97,7 +97,7 @@ int nbr_table_unlock(nbr_table_t *table, nbr_table_item_t *item); /** \name Neighbor tables: address manipulation */ /** @{ */ -rimeaddr_t *nbr_table_get_lladdr(nbr_table_t *table, nbr_table_item_t *item); +rimeaddr_t *nbr_table_get_lladdr(nbr_table_t *table, const nbr_table_item_t *item); /** @} */ #endif /* _NBR_TABLE_H_ */ diff --git a/core/net/rpl/rpl-dag.c b/core/net/rpl/rpl-dag.c index 9f396202f..f7d37a6e5 100644 --- a/core/net/rpl/rpl-dag.c +++ b/core/net/rpl/rpl-dag.c @@ -96,9 +96,9 @@ rpl_get_parent_rank(uip_lladdr_t *addr) } /*---------------------------------------------------------------------------*/ uint16_t -rpl_get_parent_link_metric(uip_lladdr_t *addr) +rpl_get_parent_link_metric(const uip_lladdr_t *addr) { - rpl_parent_t *p = nbr_table_get_from_lladdr(rpl_parents, (rimeaddr_t *)addr); + rpl_parent_t *p = nbr_table_get_from_lladdr(rpl_parents, (const rimeaddr_t *)addr); if(p != NULL) { return p->link_metric; } else { @@ -539,7 +539,7 @@ rpl_add_parent(rpl_dag_t *dag, rpl_dio_t *dio, uip_ipaddr_t *addr) rpl_parent_t *p = NULL; /* Is the parent known by ds6? Drop this request if not. * Typically, the parent is added upon receiving a DIO. */ - uip_lladdr_t *lladdr = uip_ds6_nbr_lladdr_from_ipaddr(addr); + const uip_lladdr_t *lladdr = uip_ds6_nbr_lladdr_from_ipaddr(addr); PRINTF("RPL: rpl_add_parent lladdr %p\n", lladdr); if(lladdr != NULL) { @@ -561,7 +561,7 @@ static rpl_parent_t * find_parent_any_dag_any_instance(uip_ipaddr_t *addr) { uip_ds6_nbr_t *ds6_nbr = uip_ds6_nbr_lookup(addr); - uip_lladdr_t *lladdr = uip_ds6_nbr_get_ll(ds6_nbr); + const uip_lladdr_t *lladdr = uip_ds6_nbr_get_ll(ds6_nbr); return nbr_table_get_from_lladdr(rpl_parents, (rimeaddr_t *)lladdr); } /*---------------------------------------------------------------------------*/ diff --git a/core/net/rpl/rpl.h b/core/net/rpl/rpl.h index dd3ccd9eb..b83e140ed 100644 --- a/core/net/rpl/rpl.h +++ b/core/net/rpl/rpl.h @@ -245,7 +245,7 @@ void rpl_remove_header(void); uint8_t rpl_invert_header(void); uip_ipaddr_t *rpl_get_parent_ipaddr(rpl_parent_t *nbr); rpl_rank_t rpl_get_parent_rank(uip_lladdr_t *addr); -uint16_t rpl_get_parent_link_metric(uip_lladdr_t *addr); +uint16_t rpl_get_parent_link_metric(const uip_lladdr_t *addr); void rpl_dag_init(void); /*---------------------------------------------------------------------------*/ #endif /* RPL_H */ diff --git a/core/net/sicslowpan.c b/core/net/sicslowpan.c index cdbe33fc8..be6a2dc8d 100644 --- a/core/net/sicslowpan.c +++ b/core/net/sicslowpan.c @@ -1354,7 +1354,7 @@ send_packet(rimeaddr_t *dest) * MAC. */ static uint8_t -output(uip_lladdr_t *localdest) +output(const uip_lladdr_t *localdest) { int framer_hdrlen; diff --git a/core/net/tcpip.c b/core/net/tcpip.c index 1bd84f6e2..5d9d8858a 100644 --- a/core/net/tcpip.c +++ b/core/net/tcpip.c @@ -109,10 +109,10 @@ enum { /* Called on IP packet output. */ #if UIP_CONF_IPV6 -static uint8_t (* outputfunc)(uip_lladdr_t *a); +static uint8_t (* outputfunc)(const uip_lladdr_t *a); uint8_t -tcpip_output(uip_lladdr_t *a) +tcpip_output(const uip_lladdr_t *a) { int ret; if(outputfunc != NULL) { @@ -124,7 +124,7 @@ tcpip_output(uip_lladdr_t *a) } void -tcpip_set_outputfunc(uint8_t (*f)(uip_lladdr_t *)) +tcpip_set_outputfunc(uint8_t (*f)(const uip_lladdr_t *)) { outputfunc = f; } diff --git a/core/net/tcpip.h b/core/net/tcpip.h index 705fe4554..bb4b412b5 100644 --- a/core/net/tcpip.h +++ b/core/net/tcpip.h @@ -340,8 +340,8 @@ CCIF void tcpip_input(void); * The eventual parameter is the MAC address of the destination. */ #if UIP_CONF_IPV6 -uint8_t tcpip_output(uip_lladdr_t *); -void tcpip_set_outputfunc(uint8_t (* f)(uip_lladdr_t *)); +uint8_t tcpip_output(const uip_lladdr_t *); +void tcpip_set_outputfunc(uint8_t (* f)(const uip_lladdr_t *)); #else uint8_t tcpip_output(void); void tcpip_set_outputfunc(uint8_t (* f)(void)); diff --git a/core/net/uip-ds6-nbr.c b/core/net/uip-ds6-nbr.c index 924e9ba3d..2f07154dc 100644 --- a/core/net/uip-ds6-nbr.c +++ b/core/net/uip-ds6-nbr.c @@ -78,7 +78,7 @@ uip_ds6_neighbors_init(void) } /*---------------------------------------------------------------------------*/ uip_ds6_nbr_t * -uip_ds6_nbr_add(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr, +uip_ds6_nbr_add(const uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr, uint8_t isrouter, uint8_t state) { uip_ds6_nbr_t *nbr = nbr_table_add_lladdr(ds6_neighbors, (rimeaddr_t*)lladdr); @@ -125,17 +125,17 @@ uip_ds6_nbr_rm(uip_ds6_nbr_t *nbr) } /*---------------------------------------------------------------------------*/ -uip_ipaddr_t * -uip_ds6_nbr_get_ipaddr(uip_ds6_nbr_t *nbr) +const uip_ipaddr_t * +uip_ds6_nbr_get_ipaddr(const uip_ds6_nbr_t *nbr) { return (nbr != NULL) ? &nbr->ipaddr : NULL; } /*---------------------------------------------------------------------------*/ -uip_lladdr_t * -uip_ds6_nbr_get_ll(uip_ds6_nbr_t *nbr) +const uip_lladdr_t * +uip_ds6_nbr_get_ll(const uip_ds6_nbr_t *nbr) { - return (uip_lladdr_t *)nbr_table_get_lladdr(ds6_neighbors, nbr); + return (const uip_lladdr_t *)nbr_table_get_lladdr(ds6_neighbors, nbr); } /*---------------------------------------------------------------------------*/ int @@ -154,7 +154,7 @@ uip_ds6_nbr_num(void) } /*---------------------------------------------------------------------------*/ uip_ds6_nbr_t * -uip_ds6_nbr_lookup(uip_ipaddr_t *ipaddr) +uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr) { uip_ds6_nbr_t *nbr = nbr_table_head(ds6_neighbors); if(ipaddr != NULL) { @@ -169,22 +169,22 @@ uip_ds6_nbr_lookup(uip_ipaddr_t *ipaddr) } /*---------------------------------------------------------------------------*/ uip_ds6_nbr_t * -uip_ds6_nbr_ll_lookup(uip_lladdr_t *lladdr) +uip_ds6_nbr_ll_lookup(const uip_lladdr_t *lladdr) { return nbr_table_get_from_lladdr(ds6_neighbors, (rimeaddr_t*)lladdr); } /*---------------------------------------------------------------------------*/ uip_ipaddr_t * -uip_ds6_nbr_ipaddr_from_lladdr(uip_lladdr_t *lladdr) +uip_ds6_nbr_ipaddr_from_lladdr(const uip_lladdr_t *lladdr) { uip_ds6_nbr_t *nbr = uip_ds6_nbr_ll_lookup(lladdr); return nbr ? &nbr->ipaddr : NULL; } /*---------------------------------------------------------------------------*/ -uip_lladdr_t * -uip_ds6_nbr_lladdr_from_ipaddr(uip_ipaddr_t *ipaddr) +const uip_lladdr_t * +uip_ds6_nbr_lladdr_from_ipaddr(const uip_ipaddr_t *ipaddr) { uip_ds6_nbr_t *nbr = uip_ds6_nbr_lookup(ipaddr); return nbr ? uip_ds6_nbr_get_ll(nbr) : NULL; @@ -276,7 +276,6 @@ uip_ds6_neighbor_periodic(void) nbr = nbr_table_next(ds6_neighbors, nbr); } } - /*---------------------------------------------------------------------------*/ uip_ds6_nbr_t * uip_ds6_get_least_lifetime_neighbor(void) @@ -296,3 +295,4 @@ uip_ds6_get_least_lifetime_neighbor(void) } return nbr_expiring; } +/*---------------------------------------------------------------------------*/ diff --git a/core/net/uip-ds6-nbr.h b/core/net/uip-ds6-nbr.h index 18251e189..059162710 100644 --- a/core/net/uip-ds6-nbr.h +++ b/core/net/uip-ds6-nbr.h @@ -83,15 +83,15 @@ typedef struct uip_ds6_nbr { void uip_ds6_neighbors_init(void); /** \brief Neighbor Cache basic routines */ -uip_ds6_nbr_t *uip_ds6_nbr_add(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr, +uip_ds6_nbr_t *uip_ds6_nbr_add(const uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr, uint8_t isrouter, uint8_t state); void uip_ds6_nbr_rm(uip_ds6_nbr_t *nbr); -uip_lladdr_t *uip_ds6_nbr_get_ll(uip_ds6_nbr_t *nbr); -uip_ipaddr_t *uip_ds6_nbr_get_ipaddr(uip_ds6_nbr_t *nbr); -uip_ds6_nbr_t *uip_ds6_nbr_lookup(uip_ipaddr_t *ipaddr); -uip_ds6_nbr_t *uip_ds6_nbr_ll_lookup(uip_lladdr_t *lladdr); -uip_ipaddr_t *uip_ds6_nbr_ipaddr_from_lladdr(uip_lladdr_t *lladdr); -uip_lladdr_t *uip_ds6_nbr_lladdr_from_ipaddr(uip_ipaddr_t *ipaddr); +const uip_lladdr_t *uip_ds6_nbr_get_ll(const uip_ds6_nbr_t *nbr); +const uip_ipaddr_t *uip_ds6_nbr_get_ipaddr(const uip_ds6_nbr_t *nbr); +uip_ds6_nbr_t *uip_ds6_nbr_lookup(const uip_ipaddr_t *ipaddr); +uip_ds6_nbr_t *uip_ds6_nbr_ll_lookup(const uip_lladdr_t *lladdr); +uip_ipaddr_t *uip_ds6_nbr_ipaddr_from_lladdr(const uip_lladdr_t *lladdr); +const uip_lladdr_t *uip_ds6_nbr_lladdr_from_ipaddr(const uip_ipaddr_t *ipaddr); void uip_ds6_link_neighbor_callback(int status, int numtx); void uip_ds6_neighbor_periodic(void); int uip_ds6_nbr_num(void); diff --git a/core/net/uip-ds6-route.c b/core/net/uip-ds6-route.c index 917ee266e..137191f55 100644 --- a/core/net/uip-ds6-route.c +++ b/core/net/uip-ds6-route.c @@ -261,7 +261,7 @@ uip_ds6_route_add(uip_ipaddr_t *ipaddr, uint8_t length, #endif /* DEBUG != DEBUG_NONE */ /* Get link-layer address of next hop, make sure it is in neighbor table */ - uip_lladdr_t *nexthop_lladdr = uip_ds6_nbr_lladdr_from_ipaddr(nexthop); + const uip_lladdr_t *nexthop_lladdr = uip_ds6_nbr_lladdr_from_ipaddr(nexthop); if(nexthop_lladdr == NULL) { PRINTF("uip_ds6_route_add: neighbor link-local address unknown "); PRINT6ADDR(ipaddr); @@ -440,7 +440,7 @@ void uip_ds6_route_rm_by_nexthop(uip_ipaddr_t *nexthop) { /* Get routing entry list of this neighbor */ - uip_lladdr_t *nexthop_lladdr; + const uip_lladdr_t *nexthop_lladdr; struct uip_ds6_route_neighbor_routes *routes; nexthop_lladdr = uip_ds6_nbr_lladdr_from_ipaddr(nexthop); diff --git a/core/net/uip-nd6.c b/core/net/uip-nd6.c index 85b7e6154..4c9c1b775 100644 --- a/core/net/uip-nd6.c +++ b/core/net/uip-nd6.c @@ -191,7 +191,7 @@ uip_nd6_ns_input(void) (uip_lladdr_t *)&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], 0, NBR_STALE); } else { - uip_lladdr_t *lladdr = uip_ds6_nbr_get_ll(nbr); + uip_lladdr_t *lladdr = (uip_lladdr_t *)uip_ds6_nbr_get_ll(nbr); if(memcmp(&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], lladdr, UIP_LLADDR_LEN) != 0) { memcpy(lladdr, &nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET], @@ -463,7 +463,7 @@ uip_nd6_na_input(void) } else { uip_lladdr_t *lladdr; nbr = uip_ds6_nbr_lookup(&UIP_ND6_NA_BUF->tgtipaddr); - lladdr = uip_ds6_nbr_get_ll(nbr); + lladdr = (uip_lladdr_t *)uip_ds6_nbr_get_ll(nbr); if(nbr == NULL) { goto discard; } diff --git a/examples/ravenusbstick/fakeuip.c b/examples/ravenusbstick/fakeuip.c index f7d4fff81..a1e980aab 100644 --- a/examples/ravenusbstick/fakeuip.c +++ b/examples/ravenusbstick/fakeuip.c @@ -25,14 +25,14 @@ void tcpip_input( void ) mac_LowpanToEthernet(); } -uint8_t tcpip_output(uip_lladdr_t * lladdr){ +uint8_t tcpip_output(const uip_lladdr_t * lladdr){ if(output != NULL) { return output(lladdr); } return 0; } //Called from sicslowpan.c -void tcpip_set_outputfunc(uint8_t (* f)(uip_lladdr_t *)) { +void tcpip_set_outputfunc(uint8_t (* f)(const uip_lladdr_t *)) { output = f; } From 469884171a100197de264e6bf4cc5d182bab129a Mon Sep 17 00:00:00 2001 From: Adam Dunkels Date: Mon, 18 Nov 2013 23:26:37 +0100 Subject: [PATCH 2/2] Added the const keyword to functions that use the tcpip_set_outputfunc(), which now takes a const argument --- cpu/native/net/tapdev6.c | 5 +++-- cpu/native/net/tapdev6.h | 2 +- cpu/native/net/wpcap.c | 6 +++--- cpu/native/net/wpcap.h | 4 ++-- tools/sky/uip6-bridge/uip6-bridge-tap.c | 6 +++--- tools/stm32w/uip6_bridge/uip6-bridge-tap.c | 6 +++--- 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cpu/native/net/tapdev6.c b/cpu/native/net/tapdev6.c index 71a250792..22815dd42 100644 --- a/cpu/native/net/tapdev6.c +++ b/cpu/native/net/tapdev6.c @@ -98,7 +98,7 @@ static unsigned long lasttime; #endif static void do_send(void); -uint8_t tapdev_send(uip_lladdr_t *lladdr); +uint8_t tapdev_send(const uip_lladdr_t *lladdr); /*---------------------------------------------------------------------------*/ int @@ -371,7 +371,8 @@ do_send(void) } } /*---------------------------------------------------------------------------*/ -uint8_t tapdev_send(uip_lladdr_t *lladdr) +uint8_t +tapdev_send(const uip_lladdr_t *lladdr) { /* * If L3 dest is multicast, build L2 multicast address diff --git a/cpu/native/net/tapdev6.h b/cpu/native/net/tapdev6.h index 4e7fe0f63..5fbd2f9bd 100644 --- a/cpu/native/net/tapdev6.h +++ b/cpu/native/net/tapdev6.h @@ -39,7 +39,7 @@ #include "contiki-net.h" void tapdev_init(void); -uint8_t tapdev_send(uip_lladdr_t *lladdr); +uint8_t tapdev_send(const uip_lladdr_t *lladdr); uint16_t tapdev_poll(void); void tapdev_do_send(void); void tapdev_exit(void); //math diff --git a/cpu/native/net/wpcap.c b/cpu/native/net/wpcap.c index 062cc476b..ddb698a05 100644 --- a/cpu/native/net/wpcap.c +++ b/cpu/native/net/wpcap.c @@ -185,7 +185,7 @@ init(void) /* Nothing to do here */ } /*---------------------------------------------------------------------------*/ -uint8_t wfall_send(uip_lladdr_t *lladdr); +uint8_t wfall_send(const uip_lladdr_t *lladdr); #if FALLBACK_HAS_ETHERNET_HEADERS #undef IPBUF #define IPBUF ((struct uip_tcpip_hdr *)&uip_buf[14]) @@ -708,7 +708,7 @@ wfall_poll(void) /*---------------------------------------------------------------------------*/ #if UIP_CONF_IPV6 uint8_t -wpcap_send(uip_lladdr_t *lladdr) +wpcap_send(const uip_lladdr_t *lladdr) { if(lladdr == NULL) { /* the dest must be multicast*/ @@ -745,7 +745,7 @@ return 0; } #ifdef UIP_FALLBACK_INTERFACE uint8_t -wfall_send(uip_lladdr_t *lladdr) +wfall_send(const uip_lladdr_t *lladdr) { #if FALLBACK_HAS_ETHERNET_HEADERS //make room for ethernet header diff --git a/cpu/native/net/wpcap.h b/cpu/native/net/wpcap.h index e5b641f88..04a8ca0a7 100644 --- a/cpu/native/net/wpcap.h +++ b/cpu/native/net/wpcap.h @@ -39,8 +39,8 @@ void wpcap_init(void); uint16_t wpcap_poll(void); uint16_t wfall_poll(void); #if UIP_CONF_IPV6 -uint8_t wpcap_send(uip_lladdr_t *lladdr); -uint8_t wfall_send(uip_lladdr_t *lladdr); +uint8_t wpcap_send(const uip_lladdr_t *lladdr); +uint8_t wfall_send(const uip_lladdr_t *lladdr); #else void wpcap_send(void); #endif diff --git a/tools/sky/uip6-bridge/uip6-bridge-tap.c b/tools/sky/uip6-bridge/uip6-bridge-tap.c index 64c9f6cf7..375c5e045 100644 --- a/tools/sky/uip6-bridge/uip6-bridge-tap.c +++ b/tools/sky/uip6-bridge/uip6-bridge-tap.c @@ -55,9 +55,9 @@ PROCESS(tcpip_process, "tcpip dummy"); AUTOSTART_PROCESSES(&uip6_bridge); /*---------------------------------------------------------------------------*/ -static uint8_t (* outputfunc)(uip_lladdr_t *a); +static uint8_t (* outputfunc)(const uip_lladdr_t *a); uint8_t -tcpip_output(uip_lladdr_t *a) +tcpip_output(const uip_lladdr_t *a) { if(outputfunc != NULL) { outputfunc(a); @@ -73,7 +73,7 @@ tcpip_ipv6_output(void) { } void -tcpip_set_outputfunc(uint8_t (*f)(uip_lladdr_t *)) +tcpip_set_outputfunc(uint8_t (*f)(const uip_lladdr_t *)) { outputfunc = f; } diff --git a/tools/stm32w/uip6_bridge/uip6-bridge-tap.c b/tools/stm32w/uip6_bridge/uip6-bridge-tap.c index 0f9da30ee..e5d2cb695 100644 --- a/tools/stm32w/uip6_bridge/uip6-bridge-tap.c +++ b/tools/stm32w/uip6_bridge/uip6-bridge-tap.c @@ -57,9 +57,9 @@ PROCESS(tcpip_process, "tcpip dummy"); AUTOSTART_PROCESSES(&uip6_bridge); /*---------------------------------------------------------------------------*/ -static uint8_t (* outputfunc)(uip_lladdr_t *a); +static uint8_t (* outputfunc)(const uip_lladdr_t *a); uint8_t -tcpip_output(uip_lladdr_t *a) +tcpip_output(const uip_lladdr_t *a) { if(outputfunc != NULL) { leds_on(LEDS_GREEN); @@ -76,7 +76,7 @@ tcpip_ipv6_output(void) { } void -tcpip_set_outputfunc(uint8_t (*f)(uip_lladdr_t *)) +tcpip_set_outputfunc(uint8_t (*f)(const uip_lladdr_t *)) { outputfunc = f; }