enet: Use const for pointers to receive buffer

This commit is contained in:
Jan Schär 2022-05-28 16:22:21 +02:00
parent 50cb528a71
commit cdba8db5e2
4 changed files with 36 additions and 36 deletions

View File

@ -3,9 +3,9 @@
/*
* Copyright (c) 2001, 2002 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
@ -14,21 +14,21 @@
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
@ -39,6 +39,6 @@
/**
* Calculate the internet checksum according to RFC1071
*/
uint16_t inet_checksum(void *dataptr, uint16_t len);
uint16_t inet_checksum(const void *dataptr, uint16_t len);
#endif

View File

@ -3,15 +3,15 @@
static uint16_t
lwip_standard_chksum(void *dataptr, uint16_t len)
lwip_standard_chksum(const void *dataptr, uint16_t len)
{
uint32_t acc;
uint16_t src;
uint8_t *octetptr;
const uint8_t *octetptr;
acc = 0;
/* dataptr may be at odd or even addresses */
octetptr = (uint8_t*)dataptr;
octetptr = (const uint8_t*)dataptr;
while (len > 1) {
/* declare first octet as most significant
thus assume network order, ignoring host order */
@ -39,7 +39,7 @@ lwip_standard_chksum(void *dataptr, uint16_t len)
/**
* Calculate a short such that ret + dataptr[..] becomes 0
*/
uint16_t inet_checksum(void *dataptr, uint16_t len)
uint16_t inet_checksum(const void *dataptr, uint16_t len)
{
return ~lwip_standard_chksum(dataptr, len);
};

View File

@ -162,9 +162,9 @@ struct ump_client {
struct icmp_echo_reply_meta {
struct devq_buf rx_buf;
struct tx_alloc_queue_entry tx_entry;
struct icmp_echo_hdr *icmp_echo_hdr;
struct eth_hdr *eth_hdr;
struct ip_hdr *ip_hdr;
const struct icmp_echo_hdr *icmp_echo_hdr;
const struct eth_hdr *eth_hdr;
const struct ip_hdr *ip_hdr;
};
struct udp_recv {

View File

@ -122,7 +122,7 @@ static void arp_reply (void *cb_arg, struct devq_buf *buf, void *vaddr) {
st->arp.tx_queue_len--;
}
static void arp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_hdr) {
static void arp_handle (struct devq_buf *buf, const void *vaddr, const struct eth_hdr *eth_hdr) {
ENET_DEBUG("Received ARP packet\n");
if (buf->valid_length < sizeof(struct arp_hdr)) {
@ -130,7 +130,7 @@ static void arp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_h
rx_release(buf);
return;
}
struct arp_hdr *arp_hdr = vaddr + buf->valid_data;
const struct arp_hdr *arp_hdr = vaddr + buf->valid_data;
uint16_t opcode = uint16_rd(arp_hdr->opcode);
if (
uint16_rd(arp_hdr->hwtype) != ARP_HW_TYPE_ETH ||
@ -218,7 +218,7 @@ static void icmp_reply (void *cb_arg, struct devq_buf *buf, void *vaddr) {
tx_send(buf);
}
static void icmp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_hdr, struct ip_hdr *ip_hdr) {
static void icmp_handle (struct devq_buf *buf, const void *vaddr, const struct eth_hdr *eth_hdr, const struct ip_hdr *ip_hdr) {
if (buf->valid_length < 4) {
ENET_WARN("Received ICMP packet too small\n");
rx_release(buf);
@ -231,7 +231,7 @@ static void icmp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_
return;
}
uint8_t type = *(uint8_t*)(vaddr + buf->valid_data);
uint8_t type = *(const uint8_t*)(vaddr + buf->valid_data);
if (type == ICMP_ECHO) {
if (buf->valid_length < sizeof(struct icmp_echo_hdr)) {
ENET_WARN("Received ICMP packet too small\n");
@ -239,7 +239,7 @@ static void icmp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_
return;
}
struct icmp_echo_hdr *icmp_echo_hdr = vaddr + buf->valid_data;
const struct icmp_echo_hdr *icmp_echo_hdr = vaddr + buf->valid_data;
ENET_DEBUG("Received ICMP echo, seq=%d\n", uint16_rd(icmp_echo_hdr->seqno));
struct icmp_echo_reply_meta *meta = simpleslab_alloc(&st->rx_meta_slab);
@ -265,7 +265,7 @@ static void udp_recv_ump_callback (void *arg, struct ump_send_queue_entry *entry
simpleslab_free(&st->rx_meta_slab, meta);
}
static uint16_t udp_checksum (struct ip_hdr *ip_hdr, struct udp_hdr *udp_hdr) {
static uint16_t udp_checksum (const struct ip_hdr *ip_hdr, const struct udp_hdr *udp_hdr) {
uint16_t udp_len = uint16_rd(udp_hdr->len);
uint32_t chksum = inet_checksum(udp_hdr, udp_len) ^ 0x0000ffffUL;
// add pseudo header
@ -280,13 +280,13 @@ static uint16_t udp_checksum (struct ip_hdr *ip_hdr, struct udp_hdr *udp_hdr) {
return ~(uint16_t)chksum;
}
static void udp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_hdr, struct ip_hdr *ip_hdr) {
static void udp_handle (struct devq_buf *buf, const void *vaddr, const struct eth_hdr *eth_hdr, const struct ip_hdr *ip_hdr) {
if (buf->valid_length < UDP_HLEN) {
ENET_WARN("Received UDP packet too small\n");
rx_release(buf);
return;
}
struct udp_hdr *udp_hdr = vaddr + buf->valid_data;
const struct udp_hdr *udp_hdr = vaddr + buf->valid_data;
uint16_t udp_len = uint16_rd(udp_hdr->len);
if (udp_len < UDP_HLEN || buf->valid_length < udp_len) {
ENET_WARN("Received UDP packet too small\n");
@ -305,7 +305,7 @@ static void udp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_h
uint16_t src_port = uint16_rd(udp_hdr->src);
uint16_t dest_port = uint16_rd(udp_hdr->dest);
void *payload = (void*)udp_hdr + UDP_HLEN;
const void *payload = (const void*)udp_hdr + UDP_HLEN;
uint16_t payload_len = udp_len - UDP_HLEN;
ENET_DEBUG("Received UDP packet from %d to %d, len %d\n", src_port, dest_port, payload_len);
@ -336,13 +336,13 @@ static void udp_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_h
// IP: https://datatracker.ietf.org/doc/html/rfc791#section-3.1
static void ip_handle (struct devq_buf *buf, void *vaddr, struct eth_hdr *eth_hdr) {
static void ip_handle (struct devq_buf *buf, const void *vaddr, const struct eth_hdr *eth_hdr) {
if (buf->valid_length < sizeof(struct ip_hdr)) {
ENET_WARN("Received IP packet too small\n");
rx_release(buf);
return;
}
struct ip_hdr *ip_hdr = vaddr + buf->valid_data;
const struct ip_hdr *ip_hdr = vaddr + buf->valid_data;
uint16_t ip_len = uint16_rd(ip_hdr->len);
uint16_t header_len = IPH_HL(ip_hdr) * 4;
if (
@ -434,8 +434,8 @@ static void write_eth_ip_header (
static void rx_handle (struct devq_buf *buf) {
struct region_entry *entry = enet_get_region(st->rxq, buf->rid);
assert(entry != NULL);
void *vaddr = (void*)entry->mem.vbase + buf->offset;
void *eth_vaddr = vaddr + buf->valid_data;
const void *vaddr = (void*)entry->mem.vbase + buf->offset;
const void *eth_vaddr = vaddr + buf->valid_data;
#if defined(ENET_DEBUG_OPTION)
debug_printf("Received Packet of size %lu:", buf->valid_length);
@ -451,7 +451,7 @@ static void rx_handle (struct devq_buf *buf) {
return;
}
struct eth_hdr *eth_hdr = eth_vaddr;
const struct eth_hdr *eth_hdr = eth_vaddr;
uint16_t type = uint16_rd(eth_hdr->type);
buf->valid_data += ETH_HLEN;