From f50f57947cd40e2281652a03423fef980ceb3b0f Mon Sep 17 00:00:00 2001
From: chrysn <chrysn@fsfe.org>
Date: Thu, 22 Jan 2015 18:10:38 +0100
Subject: [PATCH] udp, ip: `const` declaration for destination addresses

functions like udp_sendto take address arguments which they
conceptionally won't modify, but do not declare them as const. this
causes issues with libraries like libcoap that treat their addresses,
which might be shared among different parts of the program, as const.

this patch adds `const` declarations to everything that needs them in
order for `udp_sendto` to be able to receive const arguments. more areas
might need similar modifications.
---
 src/core/ipv4/inet_chksum.c         |  4 ++--
 src/core/ipv4/ip.c                  | 10 +++++-----
 src/core/ipv4/ip_frag.c             |  2 +-
 src/core/udp.c                      | 10 +++++-----
 src/include/ipv4/lwip/inet_chksum.h |  4 ++--
 src/include/ipv4/lwip/ip.h          | 10 +++++-----
 src/include/ipv4/lwip/ip_frag.h     |  2 +-
 src/include/lwip/netif.h            |  2 +-
 src/include/lwip/udp.h              |  8 ++++----
 src/include/netif/etharp.h          |  6 +++---
 src/netif/etharp.c                  | 10 +++++-----
 11 files changed, 34 insertions(+), 34 deletions(-)

diff --git a/src/core/ipv4/inet_chksum.c b/src/core/ipv4/inet_chksum.c
index 960252f..1bc870c 100644
--- a/src/core/ipv4/inet_chksum.c
+++ b/src/core/ipv4/inet_chksum.c
@@ -270,7 +270,7 @@ lwip_standard_chksum(void *dataptr, int len)
  */
 u16_t
 inet_chksum_pseudo(struct pbuf *p,
-       ip_addr_t *src, ip_addr_t *dest,
+       const ip_addr_t *src, const ip_addr_t *dest,
        u8_t proto, u16_t proto_len)
 {
   u32_t acc;
@@ -330,7 +330,7 @@ inet_chksum_pseudo(struct pbuf *p,
  */
 u16_t
 inet_chksum_pseudo_partial(struct pbuf *p,
-       ip_addr_t *src, ip_addr_t *dest,
+       const ip_addr_t *src, const ip_addr_t *dest,
        u8_t proto, u16_t proto_len, u16_t chksum_len)
 {
   u32_t acc;
diff --git a/src/core/ipv4/ip.c b/src/core/ipv4/ip.c
index 95d2db4..76b71b7 100644
--- a/src/core/ipv4/ip.c
+++ b/src/core/ipv4/ip.c
@@ -121,7 +121,7 @@ static u16_t ip_id;
  * @return the netif on which to send to reach dest
  */
 struct netif *
-ip_route(ip_addr_t *dest)
+ip_route(const ip_addr_t *dest)
 {
   struct netif *netif;
 
@@ -638,7 +638,7 @@ ip_input(struct pbuf *p, struct netif *inp)
  *  unique identifiers independent of destination"
  */
 err_t
-ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
+ip_output_if(struct pbuf *p, const ip_addr_t *src, const ip_addr_t *dest,
              u8_t ttl, u8_t tos,
              u8_t proto, struct netif *netif)
 {
@@ -652,7 +652,7 @@ ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
  * @ param ip_options pointer to the IP options, copied into the IP header
  * @ param optlen length of ip_options
  */
-err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
+err_t ip_output_if_opt(struct pbuf *p, const ip_addr_t *src, const ip_addr_t *dest,
        u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
        u16_t optlen)
 {
@@ -815,7 +815,7 @@ err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
  *         see ip_output_if() for more return values
  */
 err_t
-ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
+ip_output(struct pbuf *p, const ip_addr_t *src, const ip_addr_t *dest,
           u8_t ttl, u8_t tos, u8_t proto)
 {
   struct netif *netif;
@@ -854,7 +854,7 @@ ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
  *         see ip_output_if() for more return values
  */
 err_t
-ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
+ip_output_hinted(struct pbuf *p, const ip_addr_t *src, const ip_addr_t *dest,
           u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
 {
   struct netif *netif;
diff --git a/src/core/ipv4/ip_frag.c b/src/core/ipv4/ip_frag.c
index 8d18434..d6aa7ea 100644
--- a/src/core/ipv4/ip_frag.c
+++ b/src/core/ipv4/ip_frag.c
@@ -664,7 +664,7 @@ ipfrag_free_pbuf_custom(struct pbuf *p)
  * @return ERR_OK if sent successfully, err_t otherwise
  */
 err_t 
-ip_frag(struct pbuf *p, struct netif *netif, ip_addr_t *dest)
+ip_frag(struct pbuf *p, struct netif *netif, const ip_addr_t *dest)
 {
   struct pbuf *rambuf;
 #if IP_FRAG_USES_STATIC_BUF
diff --git a/src/core/udp.c b/src/core/udp.c
index 32c7d38..a90ebea 100644
--- a/src/core/udp.c
+++ b/src/core/udp.c
@@ -489,7 +489,7 @@ udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
  */
 err_t
 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
-  ip_addr_t *dst_ip, u16_t dst_port)
+  const ip_addr_t *dst_ip, u16_t dst_port)
 {
 #if LWIP_CHECKSUM_ON_COPY
   return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
@@ -497,7 +497,7 @@ udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
 
 /** Same as udp_sendto(), but with checksum */
 err_t
-udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
+udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
                   u16_t dst_port, u8_t have_chksum, u16_t chksum)
 {
 #endif /* LWIP_CHECKSUM_ON_COPY */
@@ -547,7 +547,7 @@ udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
  */
 err_t
 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
-  ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
+  const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
 {
 #if LWIP_CHECKSUM_ON_COPY
   return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
@@ -555,7 +555,7 @@ udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
 
 /** Same as udp_sendto_if(), but with checksum */
 err_t
-udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
+udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
                      u16_t dst_port, struct netif *netif, u8_t have_chksum,
                      u16_t chksum)
 {
@@ -763,7 +763,7 @@ udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
  * @see udp_disconnect()
  */
 err_t
-udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
+udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
 {
   struct udp_pcb *ipcb;
   u8_t rebind;
diff --git a/src/include/ipv4/lwip/inet_chksum.h b/src/include/ipv4/lwip/inet_chksum.h
index 79a2d90..da7172e 100644
--- a/src/include/ipv4/lwip/inet_chksum.h
+++ b/src/include/ipv4/lwip/inet_chksum.h
@@ -73,10 +73,10 @@ extern "C" {
 u16_t inet_chksum(void *dataptr, u16_t len);
 u16_t inet_chksum_pbuf(struct pbuf *p);
 u16_t inet_chksum_pseudo(struct pbuf *p,
-       ip_addr_t *src, ip_addr_t *dest,
+       const ip_addr_t *src, const ip_addr_t *dest,
        u8_t proto, u16_t proto_len);
 u16_t inet_chksum_pseudo_partial(struct pbuf *p,
-       ip_addr_t *src, ip_addr_t *dest,
+       const ip_addr_t *src, const ip_addr_t *dest,
        u8_t proto, u16_t proto_len, u16_t chksum_len);
 #if LWIP_CHKSUM_COPY_ALGORITHM
 u16_t lwip_chksum_copy(void *dst, const void *src, u16_t len);
diff --git a/src/include/ipv4/lwip/ip.h b/src/include/ipv4/lwip/ip.h
index 00c83a0..757996c 100644
--- a/src/include/ipv4/lwip/ip.h
+++ b/src/include/ipv4/lwip/ip.h
@@ -172,19 +172,19 @@ extern ip_addr_t current_iphdr_src;
 extern ip_addr_t current_iphdr_dest;
 
 #define ip_init() /* Compatibility define, not init needed. */
-struct netif *ip_route(ip_addr_t *dest);
+struct netif *ip_route(const ip_addr_t *dest);
 err_t ip_input(struct pbuf *p, struct netif *inp);
-err_t ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
+err_t ip_output(struct pbuf *p, const ip_addr_t *src, const ip_addr_t *dest,
        u8_t ttl, u8_t tos, u8_t proto);
-err_t ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
+err_t ip_output_if(struct pbuf *p, const ip_addr_t *src, const ip_addr_t *dest,
        u8_t ttl, u8_t tos, u8_t proto,
        struct netif *netif);
 #if LWIP_NETIF_HWADDRHINT
-err_t ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
+err_t ip_output_hinted(struct pbuf *p, const ip_addr_t *src, const ip_addr_t *dest,
        u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint);
 #endif /* LWIP_NETIF_HWADDRHINT */
 #if IP_OPTIONS_SEND
-err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
+err_t ip_output_if_opt(struct pbuf *p, const ip_addr_t *src, const ip_addr_t *dest,
        u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
        u16_t optlen);
 #endif /* IP_OPTIONS_SEND */
diff --git a/src/include/ipv4/lwip/ip_frag.h b/src/include/ipv4/lwip/ip_frag.h
index 77b5eb1..5d84973 100644
--- a/src/include/ipv4/lwip/ip_frag.h
+++ b/src/include/ipv4/lwip/ip_frag.h
@@ -78,7 +78,7 @@ struct pbuf_custom_ref {
 };
 #endif /* !IP_FRAG_USES_STATIC_BUF && !LWIP_NETIF_TX_SINGLE_PBUF */
 
-err_t ip_frag(struct pbuf *p, struct netif *netif, ip_addr_t *dest);
+err_t ip_frag(struct pbuf *p, struct netif *netif, const ip_addr_t *dest);
 #endif /* IP_FRAG */
 
 #ifdef __cplusplus
diff --git a/src/include/lwip/netif.h b/src/include/lwip/netif.h
index f7e4937..d7894e2 100644
--- a/src/include/lwip/netif.h
+++ b/src/include/lwip/netif.h
@@ -116,7 +116,7 @@ typedef err_t (*netif_input_fn)(struct pbuf *p, struct netif *inp);
  * @param ipaddr The IP address to which the packet shall be sent
  */
 typedef err_t (*netif_output_fn)(struct netif *netif, struct pbuf *p,
-       ip_addr_t *ipaddr);
+       const ip_addr_t *ipaddr);
 /** Function prototype for netif->linkoutput functions. Only used for ethernet
  * netifs. This function is called by ARP when a packet shall be sent.
  *
diff --git a/src/include/lwip/udp.h b/src/include/lwip/udp.h
index f1e6d3f..901f6ab 100644
--- a/src/include/lwip/udp.h
+++ b/src/include/lwip/udp.h
@@ -122,7 +122,7 @@ extern struct udp_pcb *udp_pcbs;
    UDP code. */
 struct udp_pcb * udp_new        (void);
 void             udp_remove     (struct udp_pcb *pcb);
-err_t            udp_bind       (struct udp_pcb *pcb, ip_addr_t *ipaddr,
+err_t            udp_bind       (struct udp_pcb *pcb, const ip_addr_t *ipaddr,
                                  u16_t port);
 err_t            udp_connect    (struct udp_pcb *pcb, ip_addr_t *ipaddr,
                                  u16_t port);
@@ -130,10 +130,10 @@ void             udp_disconnect (struct udp_pcb *pcb);
 void             udp_recv       (struct udp_pcb *pcb, udp_recv_fn recv,
                                  void *recv_arg);
 err_t            udp_sendto_if  (struct udp_pcb *pcb, struct pbuf *p,
-                                 ip_addr_t *dst_ip, u16_t dst_port,
+                                 const ip_addr_t *dst_ip, u16_t dst_port,
                                  struct netif *netif);
 err_t            udp_sendto     (struct udp_pcb *pcb, struct pbuf *p,
-                                 ip_addr_t *dst_ip, u16_t dst_port);
+                                 const ip_addr_t *dst_ip, u16_t dst_port);
 err_t            udp_send       (struct udp_pcb *pcb, struct pbuf *p);
 
 #if LWIP_CHECKSUM_ON_COPY
@@ -142,7 +142,7 @@ err_t            udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p,
                                  struct netif *netif, u8_t have_chksum,
                                  u16_t chksum);
 err_t            udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p,
-                                 ip_addr_t *dst_ip, u16_t dst_port,
+                                 const ip_addr_t *dst_ip, u16_t dst_port,
                                  u8_t have_chksum, u16_t chksum);
 err_t            udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
                                  u8_t have_chksum, u16_t chksum);
diff --git a/src/include/netif/etharp.h b/src/include/netif/etharp.h
index 859608d..6e50d74 100644
--- a/src/include/netif/etharp.h
+++ b/src/include/netif/etharp.h
@@ -182,9 +182,9 @@ struct etharp_q_entry {
 void etharp_tmr(void);
 s8_t etharp_find_addr(struct netif *netif, ip_addr_t *ipaddr,
          struct eth_addr **eth_ret, ip_addr_t **ip_ret);
-err_t etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr);
-err_t etharp_query(struct netif *netif, ip_addr_t *ipaddr, struct pbuf *q);
-err_t etharp_request(struct netif *netif, ip_addr_t *ipaddr);
+err_t etharp_output(struct netif *netif, struct pbuf *q, const ip_addr_t *ipaddr);
+err_t etharp_query(struct netif *netif, const ip_addr_t *ipaddr, struct pbuf *q);
+err_t etharp_request(struct netif *netif, const ip_addr_t *ipaddr);
 /** For Ethernet network interfaces, we might want to send "gratuitous ARP";
  *  this is an ARP packet sent by a node in order to spontaneously cause other
  *  nodes to update an entry in their ARP cache.
diff --git a/src/netif/etharp.c b/src/netif/etharp.c
index 5e382d1..8d1ca87 100644
--- a/src/netif/etharp.c
+++ b/src/netif/etharp.c
@@ -263,7 +263,7 @@ etharp_tmr(void)
  * entry is found or could be recycled.
  */
 static s8_t
-etharp_find_entry(ip_addr_t *ipaddr, u8_t flags)
+etharp_find_entry(const ip_addr_t *ipaddr, u8_t flags)
 {
   s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
   s8_t empty = ARP_TABLE_SIZE;
@@ -876,11 +876,11 @@ etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, u8_t arp_idx)
  * or the return type of either etharp_query() or etharp_send_ip().
  */
 err_t
-etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr)
+etharp_output(struct netif *netif, struct pbuf *q, const ip_addr_t *ipaddr)
 {
   struct eth_addr *dest;
   struct eth_addr mcastaddr;
-  ip_addr_t *dst_addr = ipaddr;
+  const ip_addr_t *dst_addr = ipaddr;
 
   LWIP_ASSERT("netif != NULL", netif != NULL);
   LWIP_ASSERT("q != NULL", q != NULL);
@@ -1013,7 +1013,7 @@ etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr)
  *
  */
 err_t
-etharp_query(struct netif *netif, ip_addr_t *ipaddr, struct pbuf *q)
+etharp_query(struct netif *netif, const ip_addr_t *ipaddr, struct pbuf *q)
 {
   struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
   err_t result = ERR_MEM;
@@ -1256,7 +1256,7 @@ etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
  *         any other err_t on failure
  */
 err_t
-etharp_request(struct netif *netif, ip_addr_t *ipaddr)
+etharp_request(struct netif *netif, const ip_addr_t *ipaddr)
 {
   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
   return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, &ethbroadcast,
-- 
2.1.4

