From 902d190a11d6626b4c33bd2d36a222e821b31f35 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Wed, 22 Apr 2015 10:29:43 +0200 Subject: [PATCH] Many const fixes throughout the stack (although these are not all, yet) --- src/api/api_msg.c | 4 +- src/api/netbuf.c | 2 +- src/api/netdb.c | 5 ++- src/api/sockets.c | 70 ++++++++++++++++----------------- src/core/inet_chksum.c | 38 +++++++++--------- src/core/ipv4/icmp.c | 62 +++++++++++++++-------------- src/core/pbuf.c | 6 +-- src/core/tcp_out.c | 12 +++--- src/include/lwip/inet_chksum.h | 2 +- src/include/lwip/ip4_addr.h | 8 ++-- src/include/lwip/pbuf.h | 13 ++++++ src/include/lwip/snmp_asn1.h | 2 +- src/include/lwip/snmp_structs.h | 6 +-- src/include/lwip/sockets.h | 5 ++- src/netif/etharp.c | 10 ++--- 15 files changed, 134 insertions(+), 111 deletions(-) diff --git a/src/api/api_msg.c b/src/api/api_msg.c index 7035cdba..08fde62f 100644 --- a/src/api/api_msg.c +++ b/src/api/api_msg.c @@ -1419,7 +1419,7 @@ static err_t lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM) { err_t err; - void *dataptr; + const void *dataptr; u16_t len, available; u8_t write_finished = 0; size_t diff; @@ -1454,7 +1454,7 @@ lwip_netconn_do_writemore(struct netconn *conn WRITE_DELAYED_PARAM) } else #endif /* LWIP_SO_SNDTIMEO */ { - dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset; + dataptr = (const u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset; diff = conn->current_msg->msg.w.len - conn->write_offset; if (diff > 0xffffUL) { /* max_u16_t */ len = 0xffff; diff --git a/src/api/netbuf.c b/src/api/netbuf.c index cbacdd53..af160cdf 100644 --- a/src/api/netbuf.c +++ b/src/api/netbuf.c @@ -158,7 +158,7 @@ netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size) buf->ptr = NULL; return ERR_MEM; } - buf->p->payload = (void*)dataptr; + ((struct pbuf_rom*)buf->p)->payload = dataptr; buf->p->len = buf->p->tot_len = size; buf->ptr = buf->p; return ERR_OK; diff --git a/src/api/netdb.c b/src/api/netdb.c index 3c2c9eb0..673b8a7e 100644 --- a/src/api/netdb.c +++ b/src/api/netdb.c @@ -92,6 +92,7 @@ lwip_gethostbyname(const char *name) HOSTENT_STORAGE char *s_aliases; HOSTENT_STORAGE ip_addr_t s_hostent_addr; HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2]; + HOSTENT_STORAGE char s_hostname[DNS_MAX_NAME_LENGTH + 1]; /* query host IP address */ err = netconn_gethostbyname(name, &addr); @@ -105,7 +106,9 @@ lwip_gethostbyname(const char *name) s_hostent_addr = addr; s_phostent_addr[0] = &s_hostent_addr; s_phostent_addr[1] = NULL; - s_hostent.h_name = (char*)name; + strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH); + s_hostname[DNS_MAX_NAME_LENGTH] = 0; + s_hostent.h_name = s_hostname; s_aliases = NULL; s_hostent.h_aliases = &s_aliases; s_hostent.h_addrtype = AF_INET; diff --git a/src/api/sockets.c b/src/api/sockets.c index 93547119..64782ec8 100644 --- a/src/api/sockets.c +++ b/src/api/sockets.c @@ -111,9 +111,9 @@ } } while(0) #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) do { \ if (((sockaddr)->sa_family) == AF_INET6) { \ - SOCKADDR6_TO_IP6ADDR_PORT((struct sockaddr_in6*)(void*)(sockaddr), ipaddr, port); \ + SOCKADDR6_TO_IP6ADDR_PORT((const struct sockaddr_in6*)(const void*)(sockaddr), ipaddr, port); \ } else { \ - SOCKADDR4_TO_IP4ADDR_PORT((struct sockaddr_in*)(void*)(sockaddr), ipaddr, port); \ + SOCKADDR4_TO_IP4ADDR_PORT((const struct sockaddr_in*)(const void*)(sockaddr), ipaddr, port); \ } } while(0) #define DOMAIN_TO_NETCONN_TYPE(domain, type) (((domain) == AF_INET) ? \ (type) : (enum netconn_type)((type) | NETCONN_TYPE_IPV6)) @@ -124,7 +124,7 @@ #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \ IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6*)(void*)(sockaddr), ip_2_ip6(ipaddr), port) #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \ - SOCKADDR6_TO_IP6ADDR_PORT((struct sockaddr_in6*)(void*)(sockaddr), ipaddr, port) + SOCKADDR6_TO_IP6ADDR_PORT((const struct sockaddr_in6*)(const void*)(sockaddr), ipaddr, port) #define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type) #else /*-> LWIP_IPV4: LWIP_IPV4 && LWIP_IPV6 */ #define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in)) @@ -133,7 +133,7 @@ #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \ IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in*)(void*)(sockaddr), ip_2_ip4(ipaddr), port) #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \ - SOCKADDR4_TO_IP4ADDR_PORT((struct sockaddr_in*)(void*)(sockaddr), ipaddr, port) + SOCKADDR4_TO_IP4ADDR_PORT((const struct sockaddr_in*)(const void*)(sockaddr), ipaddr, port) #define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type) #endif /* LWIP_IPV6 */ @@ -1614,7 +1614,7 @@ lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optname = optname; LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen = *optlen; #if !LWIP_MPU_COMPATIBLE - LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval = optval; + LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval.p = optval; #endif /* !LWIP_MPU_COMPATIBLE */ LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err = 0; #if LWIP_NETCONN_SEM_PER_THREAD @@ -1652,7 +1652,7 @@ lwip_getsockopt_callback(void *arg) LWIP_ASSERT("arg != NULL", arg != NULL); data = (struct lwip_setgetsockopt_data*)arg; - data->err = lwip_getsockopt_impl(data->s, data->level, data->optname, data->optval, + data->err = lwip_getsockopt_impl(data->s, data->level, data->optname, data->optval.p, &data->optlen); sys_sem_signal((sys_sem_t*)(data->completed_sem)); @@ -2011,7 +2011,7 @@ lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t opt #if LWIP_MPU_COMPATIBLE memcpy(LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval, optval, optlen); #else /* LWIP_MPU_COMPATIBLE */ - LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval = (void*)optval; + LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval.pc = (const void*)optval; #endif /* LWIP_MPU_COMPATIBLE */ LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err = 0; #if LWIP_NETCONN_SEM_PER_THREAD @@ -2042,7 +2042,7 @@ lwip_setsockopt_callback(void *arg) LWIP_ASSERT("arg != NULL", arg != NULL); data = (struct lwip_setgetsockopt_data*)arg; - data->err = lwip_setsockopt_impl(data->s, data->level, data->optname, data->optval, + data->err = lwip_setsockopt_impl(data->s, data->level, data->optname, data->optval.pc, data->optlen); sys_sem_signal((sys_sem_t*)(data->completed_sem)); @@ -2076,13 +2076,13 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ case SO_REUSEADDR: #endif /* SO_REUSE */ LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int); - if (*(int*)optval) { + if (*(const int*)optval) { ip_set_option(sock->conn->pcb.ip, optname); } else { ip_reset_option(sock->conn->pcb.ip, optname); } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n", - s, optname, (*(int*)optval?"on":"off"))); + s, optname, (*(const int*)optval?"on":"off"))); break; /* SO_TYPE is get-only */ @@ -2103,13 +2103,13 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ #if LWIP_SO_RCVBUF case SO_RCVBUF: LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, int); - netconn_set_recvbufsize(sock->conn, *(int*)optval); + netconn_set_recvbufsize(sock->conn, *(const int*)optval); break; #endif /* LWIP_SO_RCVBUF */ #if LWIP_SO_LINGER case SO_LINGER: { - struct linger* linger = (struct linger*)optval; + const struct linger* linger = (const struct linger*)optval; LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, struct linger); if (linger->l_onoff) { int lingersec = linger->l_linger; @@ -2135,7 +2135,7 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ return EAFNOSUPPORT; } #endif /* LWIP_UDPLITE */ - if (*(int*)optval) { + if (*(const int*)optval) { udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) | UDP_FLAGS_NOCHKSUM); } else { udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) & ~UDP_FLAGS_NOCHKSUM); @@ -2155,32 +2155,32 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ switch (optname) { case IP_TTL: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int); - sock->conn->pcb.ip->ttl = (u8_t)(*(int*)optval); + sock->conn->pcb.ip->ttl = (u8_t)(*(const int*)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %d\n", s, sock->conn->pcb.ip->ttl)); break; case IP_TOS: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int); - sock->conn->pcb.ip->tos = (u8_t)(*(int*)optval); + sock->conn->pcb.ip->tos = (u8_t)(*(const int*)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %d\n", s, sock->conn->pcb.ip->tos)); break; #if LWIP_IGMP case IP_MULTICAST_TTL: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, u8_t, NETCONN_UDP); - sock->conn->pcb.udp->mcast_ttl = (u8_t)(*(u8_t*)optval); + sock->conn->pcb.udp->mcast_ttl = (u8_t)(*(const u8_t*)optval); break; case IP_MULTICAST_IF: { ip4_addr_t if_addr; LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct in_addr, NETCONN_UDP); - inet_addr_to_ipaddr(&if_addr, (struct in_addr*)optval); + inet_addr_to_ipaddr(&if_addr, (const struct in_addr*)optval); udp_set_multicast_netif_addr(sock->conn->pcb.udp, &if_addr); } break; case IP_MULTICAST_LOOP: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, u8_t, NETCONN_UDP); - if (*(u8_t*)optval) { + if (*(const u8_t*)optval) { udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) | UDP_FLAGS_MULTICAST_LOOP); } else { udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) & ~UDP_FLAGS_MULTICAST_LOOP); @@ -2192,7 +2192,7 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ /* If this is a TCP or a RAW socket, ignore these options. */ /* @todo: assign membership to this socket so that it is dropped when closing the socket */ err_t igmp_err; - struct ip_mreq *imr = (struct ip_mreq *)optval; + const struct ip_mreq *imr = (const struct ip_mreq *)optval; ip4_addr_t if_addr; ip4_addr_t multi_addr; LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct ip_mreq, NETCONN_UDP); @@ -2231,33 +2231,33 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_TCP); switch (optname) { case TCP_NODELAY: - if (*(int*)optval) { + if (*(const int*)optval) { tcp_nagle_disable(sock->conn->pcb.tcp); } else { tcp_nagle_enable(sock->conn->pcb.tcp); } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n", - s, (*(int *)optval)?"on":"off") ); + s, (*(const int *)optval)?"on":"off") ); break; case TCP_KEEPALIVE: - sock->conn->pcb.tcp->keep_idle = (u32_t)(*(int*)optval); + sock->conn->pcb.tcp->keep_idle = (u32_t)(*(const int*)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %"U32_F"\n", s, sock->conn->pcb.tcp->keep_idle)); break; #if LWIP_TCP_KEEPALIVE case TCP_KEEPIDLE: - sock->conn->pcb.tcp->keep_idle = 1000*(u32_t)(*(int*)optval); + sock->conn->pcb.tcp->keep_idle = 1000*(u32_t)(*(const int*)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) -> %"U32_F"\n", s, sock->conn->pcb.tcp->keep_idle)); break; case TCP_KEEPINTVL: - sock->conn->pcb.tcp->keep_intvl = 1000*(u32_t)(*(int*)optval); + sock->conn->pcb.tcp->keep_intvl = 1000*(u32_t)(*(const int*)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) -> %"U32_F"\n", s, sock->conn->pcb.tcp->keep_intvl)); break; case TCP_KEEPCNT: - sock->conn->pcb.tcp->keep_cnt = (u32_t)(*(int*)optval); + sock->conn->pcb.tcp->keep_cnt = (u32_t)(*(const int*)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) -> %"U32_F"\n", s, sock->conn->pcb.tcp->keep_cnt)); break; @@ -2278,7 +2278,7 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ case IPV6_V6ONLY: /* @todo: this does not work for datagram sockets, yet */ LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_TCP); - if (*(int*)optval) { + if (*(const int*)optval) { sock->conn->flags |= NETCONN_FLAG_IPV6_V6ONLY; } else { sock->conn->flags &= ~NETCONN_FLAG_IPV6_V6ONLY; @@ -2306,24 +2306,24 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ } switch (optname) { case UDPLITE_SEND_CSCOV: - if ((*(int*)optval != 0) && ((*(int*)optval < 8) || (*(int*)optval > 0xffff))) { + if ((*(const int*)optval != 0) && ((*(const int*)optval < 8) || (*(const int*)optval > 0xffff))) { /* don't allow illegal values! */ sock->conn->pcb.udp->chksum_len_tx = 8; } else { - sock->conn->pcb.udp->chksum_len_tx = (u16_t)*(int*)optval; + sock->conn->pcb.udp->chksum_len_tx = (u16_t)*(const int*)optval; } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) -> %d\n", - s, (*(int*)optval)) ); + s, (*(const int*)optval)) ); break; case UDPLITE_RECV_CSCOV: - if ((*(int*)optval != 0) && ((*(int*)optval < 8) || (*(int*)optval > 0xffff))) { + if ((*(const int*)optval != 0) && ((*(const int*)optval < 8) || (*(const int*)optval > 0xffff))) { /* don't allow illegal values! */ sock->conn->pcb.udp->chksum_len_rx = 8; } else { - sock->conn->pcb.udp->chksum_len_rx = (u16_t)*(int*)optval; + sock->conn->pcb.udp->chksum_len_rx = (u16_t)*(const int*)optval; } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) -> %d\n", - s, (*(int*)optval)) ); + s, (*(const int*)optval)) ); break; default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n", @@ -2339,14 +2339,14 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_ #if LWIP_IPV6 case IPV6_CHECKSUM: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_RAW); - if (*(int *)optval < 0) { + if (*(const int *)optval < 0) { sock->conn->pcb.raw->chksum_reqd = 0; - } else if (*(int *)optval & 1) { + } else if (*(const int *)optval & 1) { /* Per RFC3542, odd offsets are not allowed */ return EINVAL; } else { sock->conn->pcb.raw->chksum_reqd = 1; - sock->conn->pcb.raw->chksum_offset = *(int *)optval; + sock->conn->pcb.raw->chksum_offset = *(const int *)optval; } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_RAW, IPV6_CHECKSUM, ..) -> %d\n", s, sock->conn->pcb.raw->chksum_reqd)); diff --git a/src/core/inet_chksum.c b/src/core/inet_chksum.c index 27a16271..ef71343e 100644 --- a/src/core/inet_chksum.c +++ b/src/core/inet_chksum.c @@ -60,7 +60,7 @@ # ifndef LWIP_CHKSUM_ALGORITHM # define LWIP_CHKSUM_ALGORITHM 2 # endif -u16_t lwip_standard_chksum(void *dataptr, int len); +u16_t lwip_standard_chksum(const void *dataptr, int len); #endif /* If none set: */ #ifndef LWIP_CHKSUM_ALGORITHM @@ -79,15 +79,15 @@ u16_t lwip_standard_chksum(void *dataptr, int len); * @note host endianess is irrelevant (p3 RFC1071) */ u16_t -lwip_standard_chksum(void *dataptr, int len) +lwip_standard_chksum(const void *dataptr, int len) { u32_t acc; u16_t src; - u8_t *octetptr; + const u8_t *octetptr; acc = 0; /* dataptr may be at odd or even addresses */ - octetptr = (u8_t*)dataptr; + octetptr = (const u8_t*)dataptr; while (len > 1) { /* declare first octet as most significant thus assume network order, ignoring host order */ @@ -133,10 +133,11 @@ lwip_standard_chksum(void *dataptr, int len) */ u16_t -lwip_standard_chksum(void *dataptr, int len) +lwip_standard_chksum(const void *dataptr, int len) { - u8_t *pb = (u8_t *)dataptr; - u16_t *ps, t = 0; + const u8_t *pb = (const u8_t *)dataptr; + const u16_t *ps; + u16_t t = 0; u32_t sum = 0; int odd = ((mem_ptr_t)pb & 1); @@ -147,7 +148,7 @@ lwip_standard_chksum(void *dataptr, int len) } /* Add the bulk of the data */ - ps = (u16_t *)(void *)pb; + ps = (const u16_t *)(const void *)pb; while (len > 1) { sum += *ps++; len -= 2; @@ -155,7 +156,7 @@ lwip_standard_chksum(void *dataptr, int len) /* Consume left-over byte, if any */ if (len > 0) { - ((u8_t *)&t)[0] = *(u8_t *)ps; + ((u8_t *)&t)[0] = *(const u8_t *)ps; } /* Add end bytes */ @@ -189,11 +190,12 @@ lwip_standard_chksum(void *dataptr, int len) */ u16_t -lwip_standard_chksum(void *dataptr, int len) +lwip_standard_chksum(const void *dataptr, int len) { - u8_t *pb = (u8_t *)dataptr; - u16_t *ps, t = 0; - u32_t *pl; + const u8_t *pb = (const u8_t *)dataptr; + const u16_t *ps; + u16_t t = 0; + const u32_t *pl; u32_t sum = 0, tmp; /* starts at odd byte address? */ int odd = ((mem_ptr_t)pb & 1); @@ -203,14 +205,14 @@ lwip_standard_chksum(void *dataptr, int len) len--; } - ps = (u16_t *)pb; + ps = (const u16_t *)(const void*)pb; if (((mem_ptr_t)ps & 3) && len > 1) { sum += *ps++; len -= 2; } - pl = (u32_t *)ps; + pl = (const u32_t *)(const void*)ps; while (len > 7) { tmp = sum + *pl++; /* ping */ @@ -229,7 +231,7 @@ lwip_standard_chksum(void *dataptr, int len) /* make room in upper bits */ sum = FOLD_U32T(sum); - ps = (u16_t *)pl; + ps = (const u16_t *)pl; /* 16-bit aligned word remaining? */ while (len > 1) { @@ -239,7 +241,7 @@ lwip_standard_chksum(void *dataptr, int len) /* dangling tail byte remaining? */ if (len > 0) { /* include odd byte */ - ((u8_t *)&t)[0] = *(u8_t *)ps; + ((u8_t *)&t)[0] = *(const u8_t *)ps; } sum += t; /* add end bytes */ @@ -554,7 +556,7 @@ ip_chksum_pseudo_partial(struct pbuf *p, u8_t proto, u16_t proto_len, */ u16_t -inet_chksum(void *dataptr, u16_t len) +inet_chksum(const void *dataptr, u16_t len) { return ~LWIP_CHKSUM(dataptr, len); } diff --git a/src/core/ipv4/icmp.c b/src/core/ipv4/icmp.c index 9f876fef..fdd1445e 100644 --- a/src/core/ipv4/icmp.c +++ b/src/core/ipv4/icmp.c @@ -81,14 +81,15 @@ icmp_input(struct pbuf *p, struct netif *inp) u8_t code; #endif /* LWIP_DEBUG */ struct icmp_echo_hdr *iecho; + const struct ip_hdr *iphdr_in; struct ip_hdr *iphdr; s16_t hlen; ICMP_STATS_INC(icmp.recv); snmp_inc_icmpinmsgs(); - iphdr = (struct ip_hdr *)ip4_current_header(); - hlen = IPH_HL(iphdr) * 4; + iphdr_in = ip4_current_header(); + hlen = IPH_HL(iphdr_in) * 4; if (p->len < sizeof(u16_t)*2) { LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%"U16_F" bytes) received\n", p->tot_len)); goto lenerr; @@ -157,7 +158,7 @@ icmp_input(struct pbuf *p, struct netif *inp) LWIP_ASSERT("check that first pbuf can hold struct the ICMP header", (r->len >= hlen + sizeof(struct icmp_echo_hdr))); /* copy the ip header */ - MEMCPY(r->payload, iphdr, hlen); + MEMCPY(r->payload, iphdr_in, hlen); iphdr = (struct ip_hdr *)r->payload; /* switch r->payload back to icmp header */ if (pbuf_header(r, -hlen)) { @@ -185,36 +186,37 @@ icmp_input(struct pbuf *p, struct netif *inp) /* We generate an answer by switching the dest and src ip addresses, * setting the icmp type to ECHO_RESPONSE and updating the checksum. */ iecho = (struct icmp_echo_hdr *)p->payload; - ip4_addr_copy(iphdr->src, inp->ip_addr); - ip4_addr_copy(iphdr->dest, *ip4_current_src_addr()); - ICMPH_TYPE_SET(iecho, ICMP_ER); -#if CHECKSUM_GEN_ICMP - /* adjust the checksum */ - if (iecho->chksum >= PP_HTONS(0xffffU - (ICMP_ECHO << 8))) { - iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1; - } else { - iecho->chksum += PP_HTONS(ICMP_ECHO << 8); - } -#else /* CHECKSUM_GEN_ICMP */ - iecho->chksum = 0; -#endif /* CHECKSUM_GEN_ICMP */ - - /* Set the correct TTL and recalculate the header checksum. */ - IPH_TTL_SET(iphdr, ICMP_TTL); - IPH_CHKSUM_SET(iphdr, 0); -#if CHECKSUM_GEN_IP - IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN)); -#endif /* CHECKSUM_GEN_IP */ - - ICMP_STATS_INC(icmp.xmit); - /* increase number of messages attempted to send */ - snmp_inc_icmpoutmsgs(); - /* increase number of echo replies attempted to send */ - snmp_inc_icmpoutechoreps(); - if(pbuf_header(p, hlen)) { LWIP_ASSERT("Can't move over header in packet", 0); } else { + iphdr = (struct ip_hdr*)p->payload; + ip4_addr_copy(iphdr->src, inp->ip_addr); + ip4_addr_copy(iphdr->dest, *ip4_current_src_addr()); + ICMPH_TYPE_SET(iecho, ICMP_ER); +#if CHECKSUM_GEN_ICMP + /* adjust the checksum */ + if (iecho->chksum >= PP_HTONS(0xffffU - (ICMP_ECHO << 8))) { + iecho->chksum += PP_HTONS(ICMP_ECHO << 8) + 1; + } else { + iecho->chksum += PP_HTONS(ICMP_ECHO << 8); + } +#else /* CHECKSUM_GEN_ICMP */ + iecho->chksum = 0; +#endif /* CHECKSUM_GEN_ICMP */ + + /* Set the correct TTL and recalculate the header checksum. */ + IPH_TTL_SET(iphdr, ICMP_TTL); + IPH_CHKSUM_SET(iphdr, 0); +#if CHECKSUM_GEN_IP + IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN)); +#endif /* CHECKSUM_GEN_IP */ + + ICMP_STATS_INC(icmp.xmit); + /* increase number of messages attempted to send */ + snmp_inc_icmpoutmsgs(); + /* increase number of echo replies attempted to send */ + snmp_inc_icmpoutechoreps(); + err_t ret; /* send an ICMP packet, src addr is the dest addr of the current packet */ ret = ip4_output_if(p, ip4_current_dest_addr(), IP_HDRINCL, diff --git a/src/core/pbuf.c b/src/core/pbuf.c index 7126eac9..ef6812c9 100644 --- a/src/core/pbuf.c +++ b/src/core/pbuf.c @@ -1098,7 +1098,7 @@ pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len) buf_copy_len = p->len; } /* copy the necessary parts of the buffer */ - MEMCPY(p->payload, &((char*)dataptr)[copied_total], buf_copy_len); + MEMCPY(p->payload, &((const char*)dataptr)[copied_total], buf_copy_len); total_copy_len -= buf_copy_len; copied_total += buf_copy_len; } @@ -1124,7 +1124,7 @@ pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset) /* return requested data if pbuf is OK */ if ((q != NULL) && (q->tot_len >= target_offset + len)) { u16_t remaining_len = len; - u8_t* src_ptr = (u8_t*)dataptr; + const u8_t* src_ptr = (const u8_t*)dataptr; if (target_offset > 0) { /* copy the part that goes into the first pbuf */ u16_t first_copy_len = LWIP_MIN(q->len - target_offset, len); @@ -1276,7 +1276,7 @@ pbuf_memcmp(struct pbuf* p, u16_t offset, const void* s2, u16_t n) u16_t i; for(i = 0; i < n; i++) { u8_t a = pbuf_get_at(q, start + i); - u8_t b = ((u8_t*)s2)[i]; + u8_t b = ((const u8_t*)s2)[i]; if (a != b) { return i+1; } diff --git a/src/core/tcp_out.c b/src/core/tcp_out.c index b3894751..4ec6e5de 100644 --- a/src/core/tcp_out.c +++ b/src/core/tcp_out.c @@ -505,7 +505,7 @@ tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags) #if TCP_OVERSIZE_DBGCHECK last_unsent->oversize_left += oversize; #endif /* TCP_OVERSIZE_DBGCHECK */ - TCP_DATA_COPY2(concat_p->payload, (u8_t*)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped); + TCP_DATA_COPY2(concat_p->payload, (const u8_t*)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped); #if TCP_CHECKSUM_ON_COPY concat_chksummed += seglen; #endif /* TCP_CHECKSUM_ON_COPY */ @@ -518,12 +518,12 @@ tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags) } #if TCP_CHECKSUM_ON_COPY /* calculate the checksum of nocopy-data */ - tcp_seg_add_chksum(~inet_chksum((u8_t*)arg + pos, seglen), seglen, + tcp_seg_add_chksum(~inet_chksum((const u8_t*)arg + pos, seglen), seglen, &concat_chksum, &concat_chksum_swapped); concat_chksummed += seglen; #endif /* TCP_CHECKSUM_ON_COPY */ /* reference the non-volatile payload data */ - concat_p->payload = (u8_t*)arg + pos; + ((struct pbuf_rom*)concat_p)->payload = (const u8_t*)arg + pos; } pos += seglen; @@ -561,7 +561,7 @@ tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags) } LWIP_ASSERT("tcp_write: check that first pbuf can hold the complete seglen", (p->len >= seglen)); - TCP_DATA_COPY2((char *)p->payload + optlen, (u8_t*)arg + pos, seglen, &chksum, &chksum_swapped); + TCP_DATA_COPY2((char *)p->payload + optlen, (const u8_t*)arg + pos, seglen, &chksum, &chksum_swapped); } else { /* Copy is not set: First allocate a pbuf for holding the data. * Since the referenced data is available at least until it is @@ -578,14 +578,14 @@ tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags) } #if TCP_CHECKSUM_ON_COPY /* calculate the checksum of nocopy-data */ - chksum = ~inet_chksum((u8_t*)arg + pos, seglen); + chksum = ~inet_chksum((const u8_t*)arg + pos, seglen); if (seglen & 1) { chksum_swapped = 1; chksum = SWAP_BYTES_IN_WORD(chksum); } #endif /* TCP_CHECKSUM_ON_COPY */ /* reference the non-volatile payload data */ - p2->payload = (u8_t*)arg + pos; + ((struct pbuf_rom*)p2)->payload = (const u8_t*)arg + pos; /* Second, allocate a pbuf for the headers. */ if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) { diff --git a/src/include/lwip/inet_chksum.h b/src/include/lwip/inet_chksum.h index 8acefb51..856ff912 100644 --- a/src/include/lwip/inet_chksum.h +++ b/src/include/lwip/inet_chksum.h @@ -70,7 +70,7 @@ extern "C" { #endif -u16_t inet_chksum(void *dataptr, u16_t len); +u16_t inet_chksum(const void *dataptr, u16_t len); u16_t inet_chksum_pbuf(struct pbuf *p); #if LWIP_CHKSUM_COPY_ALGORITHM u16_t lwip_chksum_copy(void *dst, const void *src, u16_t len); diff --git a/src/include/lwip/ip4_addr.h b/src/include/lwip/ip4_addr.h index dc587f63..42d3b7bd 100644 --- a/src/include/lwip/ip4_addr.h +++ b/src/include/lwip/ip4_addr.h @@ -212,10 +212,10 @@ u8_t ip4_addr_netmask_valid(u32_t netmask); ipaddr != NULL ? ip4_addr4_16(ipaddr) : 0)) /* Get one byte from the 4-byte address */ -#define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0]) -#define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1]) -#define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2]) -#define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3]) +#define ip4_addr1(ipaddr) (((const u8_t*)(ipaddr))[0]) +#define ip4_addr2(ipaddr) (((const u8_t*)(ipaddr))[1]) +#define ip4_addr3(ipaddr) (((const u8_t*)(ipaddr))[2]) +#define ip4_addr4(ipaddr) (((const u8_t*)(ipaddr))[3]) /* These are cast to u16_t, with the intent that they are often arguments * to printf using the U16_F format from cc.h. */ #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr)) diff --git a/src/include/lwip/pbuf.h b/src/include/lwip/pbuf.h index 8e9240e9..5097aab9 100644 --- a/src/include/lwip/pbuf.h +++ b/src/include/lwip/pbuf.h @@ -119,6 +119,19 @@ struct pbuf { u16_t ref; }; + +/** Helper struct for const-correctness only. + * The only meaning of this one is to provide a const payload pointer + * for PBUF_ROM type. + */ +struct pbuf_rom { + /** next pbuf in singly linked pbuf chain */ + struct pbuf *next; + + /** pointer to the actual data in the buffer */ + const void *payload; +}; + #if LWIP_SUPPORT_CUSTOM_PBUF /** Prototype for a function to free a custom pbuf */ typedef void (*pbuf_free_custom_fn)(struct pbuf *p); diff --git a/src/include/lwip/snmp_asn1.h b/src/include/lwip/snmp_asn1.h index b05c8e72..147f5db9 100644 --- a/src/include/lwip/snmp_asn1.h +++ b/src/include/lwip/snmp_asn1.h @@ -90,7 +90,7 @@ err_t snmp_asn1_enc_length(struct pbuf *p, u16_t ofs, u16_t length); err_t snmp_asn1_enc_u32t(struct pbuf *p, u16_t ofs, u16_t octets_needed, u32_t value); err_t snmp_asn1_enc_s32t(struct pbuf *p, u16_t ofs, u16_t octets_needed, s32_t value); err_t snmp_asn1_enc_oid(struct pbuf *p, u16_t ofs, u8_t ident_len, const s32_t *ident); -err_t snmp_asn1_enc_raw(struct pbuf *p, u16_t ofs, u16_t raw_len, u8_t *raw); +err_t snmp_asn1_enc_raw(struct pbuf *p, u16_t ofs, u16_t raw_len, const u8_t *raw); #ifdef __cplusplus } diff --git a/src/include/lwip/snmp_structs.h b/src/include/lwip/snmp_structs.h index b735c71e..87786cfb 100644 --- a/src/include/lwip/snmp_structs.h +++ b/src/include/lwip/snmp_structs.h @@ -138,7 +138,7 @@ struct mib_array_node /* additional struct members */ const s32_t *objid; - struct mib_node* const *nptr; + const struct mib_node* const *nptr; }; /** derived node, points to a fixed size mem_malloced array @@ -254,8 +254,8 @@ s8_t snmp_mib_node_insert(struct mib_list_rootnode *rn, s32_t objid, struct mib_ s8_t snmp_mib_node_find(struct mib_list_rootnode *rn, s32_t objid, struct mib_list_node **fn); struct mib_list_rootnode *snmp_mib_node_delete(struct mib_list_rootnode *rn, struct mib_list_node *n); -struct mib_node* snmp_search_tree(struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_name_ptr *np); -struct mib_node* snmp_expand_tree(struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret); +struct mib_node* snmp_search_tree(const struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_name_ptr *np); +struct mib_node* snmp_expand_tree(const struct mib_node *node, u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret); u8_t snmp_iso_prefix_tst(u8_t ident_len, s32_t *ident); u8_t snmp_iso_prefix_expand(u8_t ident_len, s32_t *ident, struct snmp_obj_id *oidret); diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h index 9a179745..5833469d 100644 --- a/src/include/lwip/sockets.h +++ b/src/include/lwip/sockets.h @@ -125,7 +125,10 @@ struct lwip_setgetsockopt_data { #if LWIP_MPU_COMPATIBLE u8_t optval[LWIP_SETGETSOCKOPT_MAXOPTLEN]; #else - void* optval; + union { + void *p; + const void *pc; + } optval; #endif /** size of *optval */ socklen_t optlen; diff --git a/src/netif/etharp.c b/src/netif/etharp.c index e7baf5d1..371f6176 100644 --- a/src/netif/etharp.c +++ b/src/netif/etharp.c @@ -427,7 +427,7 @@ etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif* netif) * @return ERR_OK if the packet was sent, any other err_t on failure */ static err_t -etharp_send_ip(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst) +etharp_send_ip(struct netif *netif, struct pbuf *p, struct eth_addr *src, const struct eth_addr *dst) { struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload; #if ETHARP_SUPPORT_VLAN && defined(LWIP_HOOK_VLAN_SET) @@ -819,7 +819,7 @@ etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) /* If we are using Link-Local, all ARP packets that contain a Link-Local * 'sender IP address' MUST be sent using link-layer broadcast instead of * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */ - ethdst_hwaddr = ip4_addr_islinklocal(&netif->ip_addr) ? (u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr; + ethdst_hwaddr = ip4_addr_islinklocal(&netif->ip_addr) ? (const u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr; #endif /* LWIP_AUTOIP */ ETHADDR16_COPY(&hdr->dhwaddr, &hdr->shwaddr); @@ -916,7 +916,7 @@ etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, u8_t arp_idx) err_t etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) { - struct eth_addr *dest; + const struct eth_addr *dest; struct eth_addr mcastaddr; const ip4_addr_t *dst_addr = ipaddr; @@ -943,7 +943,7 @@ etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) /* broadcast destination IP address? */ if (ip4_addr_isbroadcast(ipaddr, netif)) { /* broadcast on Ethernet also */ - dest = (struct eth_addr *)ðbroadcast; + dest = (const struct eth_addr *)ðbroadcast; /* multicast destination IP address? */ } else if (ip4_addr_ismulticast(ipaddr)) { /* Hash IP multicast address to MAC address.*/ @@ -1288,7 +1288,7 @@ etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr, /* If we are using Link-Local, all ARP packets that contain a Link-Local * 'sender IP address' MUST be sent using link-layer broadcast instead of * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */ - ethdst_hwaddr = ip4_addr_islinklocal(ipsrc_addr) ? (u8_t*)(ethbroadcast.addr) : ethdst_addr->addr; + ethdst_hwaddr = ip4_addr_islinklocal(ipsrc_addr) ? (const u8_t*)(ethbroadcast.addr) : ethdst_addr->addr; #endif /* LWIP_AUTOIP */ /* Write the ARP MAC-Addresses */ ETHADDR16_COPY(&hdr->shwaddr, hwsrc_addr);