Cleanup hton*/ntoh* function handling and platform abstraction

Let lwip use functions/macros prefixed by lwip_ internally to avoid naming clashes with external #includes.
Remove over-complicated #define handling in def.h
Make functions easier to override in cc.h. The following is sufficient now (no more LWIP_PLATFORM_BYTESWAP):
#define lwip_htons(x) <your_htons>
#define lwip_htonl(x) <your_htonl>
This commit is contained in:
Dirk Ziegelmeier
2016-10-06 12:55:57 +02:00
parent 0c06073819
commit 13fb616bb2
44 changed files with 370 additions and 426 deletions

View File

@@ -262,7 +262,7 @@ lwiperf_tcp_client_send_more(lwiperf_state_tcp_t* conn)
/* this session is time-limited */
u32_t now = sys_now();
u32_t diff_ms = now - conn->time_started;
u32_t time = (u32_t)-(s32_t)htonl(conn->settings.amount);
u32_t time = (u32_t)-(s32_t)lwip_htonl(conn->settings.amount);
u32_t time_ms = time * 10;
if (diff_ms >= time_ms) {
/* time specified by the client is over -> close the connection */
@@ -271,7 +271,7 @@ lwiperf_tcp_client_send_more(lwiperf_state_tcp_t* conn)
}
} else {
/* this session is byte-limited */
u32_t amount_bytes = htonl(conn->settings.amount);
u32_t amount_bytes = lwip_htonl(conn->settings.amount);
/* @todo: this can send up to 1*MSS more than requested... */
if (amount_bytes >= conn->bytes_transferred) {
/* all requested bytes transferred -> close the connection */
@@ -390,7 +390,7 @@ lwiperf_tx_start(lwiperf_state_tcp_t* conn)
tcp_err(newpcb, lwiperf_tcp_err);
ip_addr_copy(remote_addr, conn->conn_pcb->remote_ip);
remote_port = (u16_t)htonl(client_conn->settings.remote_port);
remote_port = (u16_t)lwip_htonl(client_conn->settings.remote_port);
err = tcp_connect(newpcb, &remote_addr, remote_port, lwiperf_tcp_client_connected);
if (err != ERR_OK) {

View File

@@ -820,7 +820,7 @@ mdns_write_domain(struct mdns_outpacket *outpkt, struct mdns_domain *domain)
}
if (jump_offset) {
/* Write jump */
jump = htons(DOMAIN_JUMP | jump_offset);
jump = lwip_htons(DOMAIN_JUMP | jump_offset);
res = pbuf_take_at(outpkt->pbuf, &jump, DOMAIN_JUMP_SIZE, outpkt->write_offset);
if (res != ERR_OK) {
return res;
@@ -872,7 +872,7 @@ mdns_add_question(struct mdns_outpacket *outpkt, struct mdns_domain *domain, u16
}
/* Write type */
field16 = htons(type);
field16 = lwip_htons(type);
res = pbuf_take_at(outpkt->pbuf, &field16, sizeof(field16), outpkt->write_offset);
if (res != ERR_OK) {
return res;
@@ -883,7 +883,7 @@ mdns_add_question(struct mdns_outpacket *outpkt, struct mdns_domain *domain, u16
if (unicast) {
klass |= 0x8000;
}
field16 = htons(klass);
field16 = lwip_htons(klass);
res = pbuf_take_at(outpkt->pbuf, &field16, sizeof(field16), outpkt->write_offset);
if (res != ERR_OK) {
return res;
@@ -947,7 +947,7 @@ mdns_add_answer(struct mdns_outpacket *reply, struct mdns_domain *domain, u16_t
mdns_add_question(reply, domain, type, klass, cache_flush);
/* Write TTL */
field32 = htonl(ttl);
field32 = lwip_htonl(ttl);
res = pbuf_take_at(reply->pbuf, &field32, sizeof(field32), reply->write_offset);
if (res != ERR_OK) {
return res;
@@ -977,7 +977,7 @@ mdns_add_answer(struct mdns_outpacket *reply, struct mdns_domain *domain, u16_t
}
/* Write rd_length after when we know the answer size */
field16 = htons(reply->write_offset - answer_offset);
field16 = lwip_htons(reply->write_offset - answer_offset);
res = pbuf_take_at(reply->pbuf, &field16, sizeof(field16), rdlen_offset);
return res;
@@ -1005,14 +1005,14 @@ mdns_read_rr_info(struct mdns_packet *pkt, struct mdns_rr_info *info)
return ERR_VAL;
}
pkt->parse_offset += copied;
info->type = ntohs(field16);
info->type = lwip_ntohs(field16);
copied = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), pkt->parse_offset);
if (copied != sizeof(field16)) {
return ERR_VAL;
}
pkt->parse_offset += copied;
info->klass = ntohs(field16);
info->klass = lwip_ntohs(field16);
return ERR_OK;
}
@@ -1094,14 +1094,14 @@ mdns_read_answer(struct mdns_packet *pkt, struct mdns_answer *answer)
return ERR_VAL;
}
pkt->parse_offset += copied;
answer->ttl = ntohl(ttl);
answer->ttl = lwip_ntohl(ttl);
copied = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), pkt->parse_offset);
if (copied != sizeof(field16)) {
return ERR_VAL;
}
pkt->parse_offset += copied;
answer->rd_length = ntohs(field16);
answer->rd_length = lwip_ntohs(field16);
answer->rd_offset = pkt->parse_offset;
pkt->parse_offset += answer->rd_length;
@@ -1194,9 +1194,9 @@ mdns_add_srv_answer(struct mdns_outpacket *reply, u16_t cache_flush, struct mdns
*/
srvhost.skip_compression = 1;
}
srvdata[0] = htons(SRV_PRIORITY);
srvdata[1] = htons(SRV_WEIGHT);
srvdata[2] = htons(service->port);
srvdata[0] = lwip_htons(SRV_PRIORITY);
srvdata[1] = lwip_htons(SRV_WEIGHT);
srvdata[2] = lwip_htons(service->port);
LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Responding with SRV record\n"));
return mdns_add_answer(reply, &service_instance, DNS_RRTYPE_SRV, DNS_RRCLASS_IN, cache_flush, service->dns_ttl,
(const u8_t *) &srvdata, sizeof(srvdata), &srvhost);
@@ -1411,11 +1411,11 @@ mdns_send_outpacket(struct mdns_outpacket *outpkt)
/* Write header */
memset(&hdr, 0, sizeof(hdr));
hdr.flags1 = DNS_FLAG1_RESPONSE | DNS_FLAG1_AUTHORATIVE;
hdr.numanswers = htons(outpkt->answers);
hdr.numextrarr = htons(outpkt->additional);
hdr.numanswers = lwip_htons(outpkt->answers);
hdr.numextrarr = lwip_htons(outpkt->additional);
if (outpkt->legacy_query) {
hdr.numquestions = htons(1);
hdr.id = htons(outpkt->tx_id);
hdr.numquestions = lwip_htons(1);
hdr.id = lwip_htons(outpkt->tx_id);
}
pbuf_take(outpkt->pbuf, &hdr, sizeof(hdr));
@@ -1657,19 +1657,19 @@ mdns_handle_question(struct mdns_packet *pkt)
do {
/* Check priority field */
len = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), read_pos);
if (len != sizeof(field16) || ntohs(field16) != SRV_PRIORITY) {
if (len != sizeof(field16) || lwip_ntohs(field16) != SRV_PRIORITY) {
break;
}
read_pos += len;
/* Check weight field */
len = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), read_pos);
if (len != sizeof(field16) || ntohs(field16) != SRV_WEIGHT) {
if (len != sizeof(field16) || lwip_ntohs(field16) != SRV_WEIGHT) {
break;
}
read_pos += len;
/* Check port field */
len = pbuf_copy_partial(pkt->pbuf, &field16, sizeof(field16), read_pos);
if (len != sizeof(field16) || ntohs(field16) != service->port) {
if (len != sizeof(field16) || lwip_ntohs(field16) != service->port) {
break;
}
read_pos += len;
@@ -1780,9 +1780,9 @@ mdns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr,
packet.netif = recv_netif;
packet.pbuf = p;
packet.parse_offset = offset;
packet.tx_id = ntohs(hdr.id);
packet.questions = packet.questions_left = ntohs(hdr.numquestions);
packet.answers = packet.answers_left = ntohs(hdr.numanswers) + ntohs(hdr.numauthrr) + ntohs(hdr.numextrarr);
packet.tx_id = lwip_ntohs(hdr.id);
packet.questions = packet.questions_left = lwip_ntohs(hdr.numquestions);
packet.answers = packet.answers_left = lwip_ntohs(hdr.numanswers) + lwip_ntohs(hdr.numauthrr) + lwip_ntohs(hdr.numextrarr);
#if LWIP_IPV6
if (IP_IS_V6(ip_current_dest_addr())) {

View File

@@ -212,13 +212,13 @@ sntp_process(u32_t *receive_timestamp)
/* convert SNTP time (1900-based) to unix GMT time (1970-based)
* if MSB is 0, SNTP time is 2036-based!
*/
u32_t rx_secs = ntohl(receive_timestamp[0]);
u32_t rx_secs = lwip_ntohl(receive_timestamp[0]);
int is_1900_based = ((rx_secs & 0x80000000) != 0);
u32_t t = is_1900_based ? (rx_secs - DIFF_SEC_1900_1970) : (rx_secs + DIFF_SEC_1970_2036);
time_t tim = t;
#if SNTP_CALC_TIME_US
u32_t us = ntohl(receive_timestamp[1]) / 4295;
u32_t us = lwip_ntohl(receive_timestamp[1]) / 4295;
SNTP_SET_SYSTEM_TIME_US(t, us);
/* display local time from GMT time */
LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s, %"U32_F" us", ctime(&tim), us));
@@ -247,10 +247,10 @@ sntp_initialize_request(struct sntp_msg *req)
u32_t sntp_time_sec, sntp_time_us;
/* fill in transmit timestamp and save it in 'sntp_last_timestamp_sent' */
SNTP_GET_SYSTEM_TIME(sntp_time_sec, sntp_time_us);
sntp_last_timestamp_sent[0] = htonl(sntp_time_sec + DIFF_SEC_1900_1970);
sntp_last_timestamp_sent[0] = lwip_htonl(sntp_time_sec + DIFF_SEC_1900_1970);
req->transmit_timestamp[0] = sntp_last_timestamp_sent[0];
/* we send/save us instead of fraction to be faster... */
sntp_last_timestamp_sent[1] = htonl(sntp_time_us);
sntp_last_timestamp_sent[1] = lwip_htonl(sntp_time_us);
req->transmit_timestamp[1] = sntp_last_timestamp_sent[1];
}
#endif /* SNTP_CHECK_RESPONSE >= 2 */

View File

@@ -128,7 +128,7 @@ send_error(const ip_addr_t *addr, u16_t port, enum tftp_error code, const char *
payload = (u16_t*) p->payload;
payload[0] = PP_HTONS(TFTP_ERROR);
payload[1] = htons(code);
payload[1] = lwip_htons(code);
MEMCPY(&payload[2], str, str_length + 1);
udp_sendto(tftp_state.upcb, p, addr, port);
@@ -148,7 +148,7 @@ send_ack(u16_t blknum)
payload = (u16_t*) p->payload;
payload[0] = PP_HTONS(TFTP_ACK);
payload[1] = htons(blknum);
payload[1] = lwip_htons(blknum);
udp_sendto(tftp_state.upcb, p, &tftp_state.addr, tftp_state.port);
pbuf_free(p);
}
@@ -187,7 +187,7 @@ send_data(void)
payload = (u16_t *) tftp_state.last_data->payload;
payload[0] = PP_HTONS(TFTP_DATA);
payload[1] = htons(tftp_state.blknum);
payload[1] = lwip_htons(tftp_state.blknum);
ret = tftp_state.ctx->read(tftp_state.handle, &payload[2], TFTP_MAX_PAYLOAD_SIZE);
if (ret < 0) {
@@ -295,7 +295,7 @@ recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16
break;
}
blknum = ntohs(sbuf[1]);
blknum = lwip_ntohs(sbuf[1]);
pbuf_header(p, -TFTP_HEADER_LENGTH);
ret = tftp_state.ctx->write(tftp_state.handle, p);
@@ -327,7 +327,7 @@ recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16
break;
}
blknum = ntohs(sbuf[1]);
blknum = lwip_ntohs(sbuf[1]);
if (blknum != tftp_state.blknum) {
send_error(addr, port, TFTP_ERROR_UNKNOWN_TRFR_ID, "Wrong block number");
break;