Many const fixes throughout the stack (although these are not all, yet)

This commit is contained in:
goldsimon
2015-04-22 10:29:43 +02:00
parent 0142f113a3
commit 902d190a11
15 changed files with 134 additions and 111 deletions

View File

@@ -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);
}

View File

@@ -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,

View File

@@ -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;
}

View File

@@ -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) {