From 6b0dc272ea8f8191ba7e3fd20c1f835b975bb699 Mon Sep 17 00:00:00 2001 From: pmantoine Date: Wed, 27 Nov 2024 19:59:15 +0800 Subject: [PATCH] Fixes for Mega, HTONS/L macro --- EXmDNS.cpp | 23 +++++++++++++---------- EXmDNS.h | 6 ++++++ EthernetInterface.cpp | 2 +- EthernetInterface.h | 8 ++++++-- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/EXmDNS.cpp b/EXmDNS.cpp index 14e824b..b5250df 100644 --- a/EXmDNS.cpp +++ b/EXmDNS.cpp @@ -1,5 +1,6 @@ /* * © 2024 Harald Barth + * © 2024 Paul M. Antoine * All rights reserved. * * This file is part of CommandStation-EX @@ -87,7 +88,8 @@ int MDNS::begin(const IPAddress& ip, char* name) { int MDNS::addServiceRecord(const char* name, uint16_t port, MDNSServiceProtocol_t proto) { // we ignore proto, assume TCP - _serviceName = (char *)malloc(strlen(name)+2); + (void)proto; + _serviceName = (char *)malloc(strlen(name) + 2); byte n; for(n = 0; nbeginPacket(mdnsMulticastIPAddr, MDNS_SERVER_PORT); // dns header - dnsHeader.flags = lwip_htons(0x8400); // Response, authorative - dnsHeader.answerCount = lwip_htons(4 /*5 if TXT but we do not do that */); + dnsHeader.flags = HTONS((uint16_t)0x8400); // Response, authorative + dnsHeader.answerCount = HTONS(4 /*5 if TXT but we do not do that */); _udp->write((uint8_t*)&dnsHeader, sizeof(DNSHeader_t)); // rr #1, the PTR record from generic _services.x.local to service.x.local @@ -142,8 +145,8 @@ void MDNS::run() { buf[1] = 0x0c; //PTR buf[2] = 0x00; buf[3] = 0x01; //IN - *((uint32_t*)(buf+4)) = lwip_htonl(120); //TTL in sec - *((uint16_t*)(buf+8)) = lwip_htons( _serviceProto[0] + 1 + strlen(dns_rr_tcplocal) + 1); + *((uint32_t*)(buf+4)) = HTONL(120); //TTL in sec + *((uint16_t*)(buf+8)) = HTONS( _serviceProto[0] + 1 + strlen(dns_rr_tcplocal) + 1); _udp->write(buf, 10); _udp->write(_serviceProto,_serviceProto[0]+1); @@ -152,7 +155,7 @@ void MDNS::run() { // rr #2, the PTR record from proto.x to name.proto.x _udp->write(_serviceProto,_serviceProto[0]+1); _udp->write(dns_rr_tcplocal, strlen(dns_rr_tcplocal)+1); - *((uint16_t*)(buf+8)) = lwip_htons(strlen(_serviceName) + strlen(dns_rr_tcplocal) + 1); // recycle most of buf + *((uint16_t*)(buf+8)) = HTONS(strlen(_serviceName) + strlen(dns_rr_tcplocal) + 1); // recycle most of buf _udp->write(buf, 10); _udp->write(_serviceName, strlen(_serviceName)); @@ -163,14 +166,14 @@ void MDNS::run() { buf[1] = 0x21; // recycle most of buf but here SRV buf[2] = 0x80; // cache flush - *((uint16_t*)(buf+8)) = lwip_htons(strlen(_name) + strlen(dns_rr_local) + 1 + 6); + *((uint16_t*)(buf+8)) = HTONS(strlen(_name) + strlen(dns_rr_local) + 1 + 6); _udp->write(buf, 10); byte srv[6]; // priority and weight srv[0] = srv[1] = srv[2] = srv[3] = 0; // port - *((uint16_t*)(srv+4)) = lwip_htons(_servicePort); + *((uint16_t*)(srv+4)) = HTONS(_servicePort); _udp->write(srv, 6); // target _udp->write(_name, _name[0]+1); @@ -181,7 +184,7 @@ void MDNS::run() { _udp->write(dns_rr_local, strlen(dns_rr_local)+1); buf[1] = 0x01; // recycle most of buf but here A - *((uint16_t*)(buf+8)) = lwip_htons(4); + *((uint16_t*)(buf+8)) = HTONS(4); _udp->write(buf, 10); byte ip[4]; ip[0] = _ipAddress[0]; diff --git a/EXmDNS.h b/EXmDNS.h index 676dbe3..0ec1e5e 100644 --- a/EXmDNS.h +++ b/EXmDNS.h @@ -1,5 +1,6 @@ /* * © 2024 Harald Barth + * © 2024 Paul M. Antoine * All rights reserved. * * This file is part of CommandStation-EX @@ -20,6 +21,11 @@ #ifdef DO_MDNS #define BROADCASTTIME 15 //seconds +// We do this ourselves because every library is different and/or broken... +#define HTONS(x) ((uint16_t)(((x) << 8) | (((x) >> 8) & 0xFF))) +#define HTONL(x) ( ((uint32_t)(x) << 24) | (((uint32_t)(x) << 8) & 0xFF0000) | \ + (((uint32_t)(x) >> 8) & 0xFF00) | ((uint32_t)(x) >> 24) ) + typedef enum _MDNSServiceProtocol_t { MDNSServiceTCP, diff --git a/EthernetInterface.cpp b/EthernetInterface.cpp index f8a43f2..266afd8 100644 --- a/EthernetInterface.cpp +++ b/EthernetInterface.cpp @@ -116,7 +116,7 @@ void EthernetInterface::setup() outboundRing=new RingStream(OUTBOUND_RING_SIZE); #ifdef DO_MDNS if (!mdns.begin(Ethernet.localIP(), (char *)WIFI_HOSTNAME)) - DIAG("mdns.begin fail"); // hostname + DIAG(F("mdns.begin fail")); // hostname mdns.addServiceRecord(WIFI_HOSTNAME "._withrottle", IP_PORT, MDNSServiceTCP); mdns.run(); // run it right away to get out info ASAP #endif diff --git a/EthernetInterface.h b/EthernetInterface.h index 13a8b10..e5458d9 100644 --- a/EthernetInterface.h +++ b/EthernetInterface.h @@ -3,7 +3,7 @@ * © 2021 Neil McKechnie * © 2021 Mike S * © 2021 Fred Decker - * © 2020-2022 Harald Barth + * © 2020-2024 Harald Barth * © 2020-2024 Chris Harlow * © 2020 Gregor Baues * All rights reserved. @@ -41,9 +41,10 @@ #ifndef MAX_SOCK_NUM #define MAX_SOCK_NUM 4 #endif + // can't use our MDNS because of a namespace clash with Teensy's NativeEthernet library! + // #define DO_MDNS #elif defined (ARDUINO_NUCLEO_F429ZI) || defined (ARDUINO_NUCLEO_F439ZI) || defined (ARDUINO_NUCLEO_F4X9ZI) #include -// #include "STM32lwipopts.h" #include #include extern "C" struct netif gnetif; @@ -52,7 +53,10 @@ #define DO_MDNS #else #include "Ethernet.h" + #define DO_MDNS #endif + + #include "RingStream.h" /**