From 71f4349bcde782baf4ae468dfb5cb3db7e9bab6c Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Mon, 23 Jan 2023 11:51:19 +0100 Subject: [PATCH 1/5] core mock: implement udp peek --- tests/host/common/UdpContextSocket.cpp | 10 ++++++---- tests/host/common/include/UdpContext.h | 15 ++++++++++----- tests/host/common/mock.h | 8 ++++---- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/tests/host/common/UdpContextSocket.cpp b/tests/host/common/UdpContextSocket.cpp index c598460180..775d21b7a6 100644 --- a/tests/host/common/UdpContextSocket.cpp +++ b/tests/host/common/UdpContextSocket.cpp @@ -166,14 +166,14 @@ size_t mockUDPFillInBuf(int sock, char* ccinbuf, size_t& ccinbufsize, uint8_t& a return ccinbufsize += ret; } -size_t mockUDPPeekBytes(int sock, char* dst, size_t usersize, int timeout_ms, char* ccinbuf, +size_t mockUDPPeekBytes(int sock, char* dst, size_t offset, size_t usersize, int timeout_ms, char* ccinbuf, size_t& ccinbufsize) { (void)sock; (void)timeout_ms; - if (usersize > CCBUFSIZE) + if (offset + usersize > CCBUFSIZE) fprintf(stderr, MOCK "CCBUFSIZE(%d) should be increased by %zd bytes (-> %zd)\n", CCBUFSIZE, - usersize - CCBUFSIZE, usersize); + offset + usersize - CCBUFSIZE, offset + usersize); size_t retsize = 0; if (ccinbufsize) @@ -183,10 +183,11 @@ size_t mockUDPPeekBytes(int sock, char* dst, size_t usersize, int timeout_ms, ch if (retsize > ccinbufsize) retsize = ccinbufsize; } - memcpy(dst, ccinbuf, retsize); + memcpy(dst, ccinbuf + offset, retsize); return retsize; } +/* void mockUDPSwallow(size_t copied, char* ccinbuf, size_t& ccinbufsize) { // poor man buffer @@ -201,6 +202,7 @@ size_t mockUDPRead(int sock, char* dst, size_t size, int timeout_ms, char* ccinb mockUDPSwallow(copied, ccinbuf, ccinbufsize); return copied; } +*/ size_t mockUDPWrite(int sock, const uint8_t* data, size_t size, int timeout_ms, uint32_t ipv4, uint16_t port) diff --git a/tests/host/common/include/UdpContext.h b/tests/host/common/include/UdpContext.h index 88cb46f9ae..95b76604ac 100644 --- a/tests/host/common/include/UdpContext.h +++ b/tests/host/common/include/UdpContext.h @@ -117,12 +117,12 @@ class UdpContext size_t getSize() { - return _inbufsize; + return _inbufsize - _inoffset; } size_t tell() const { - return 0; + return _inoffset; } void seek(const size_t pos) @@ -132,7 +132,7 @@ class UdpContext mockverbose("UDPContext::seek too far (%zd >= %zd)\n", pos, _inbufsize); exit(EXIT_FAILURE); } - mockUDPSwallow(pos, _inbuf, _inbufsize); + _inoffset = pos; } bool isValidOffset(const size_t pos) const @@ -165,6 +165,7 @@ class UdpContext bool next() { _inbufsize = 0; + _inoffset = 0; mockUDPFillInBuf(_sock, _inbuf, _inbufsize, addrsize, addr, _dstport); if (_inbufsize > 0) { @@ -182,13 +183,16 @@ class UdpContext size_t read(char* dst, size_t size) { - return mockUDPRead(_sock, dst, size, _timeout_ms, _inbuf, _inbufsize); + //return mockUDPRead(_sock, dst, size, _timeout_ms, _inbuf, _inbufsize); + auto ret = mockUDPPeekBytes(_sock, dst, _inoffset, size, _timeout_ms, _inbuf, _inbufsize); + _inoffset += ret; + return ret; } int peek() { char c; - return mockUDPPeekBytes(_sock, &c, 1, _timeout_ms, _inbuf, _inbufsize) ?: -1; + return mockUDPPeekBytes(_sock, &c, _inoffset, 1, _timeout_ms, _inbuf, _inbufsize) ?: -1; } void flush() @@ -280,6 +284,7 @@ class UdpContext char _inbuf[CCBUFSIZE]; size_t _inbufsize = 0; + size_t _inoffset = 0; char _outbuf[CCBUFSIZE]; size_t _outbufsize = 0; diff --git a/tests/host/common/mock.h b/tests/host/common/mock.h index dbcb1dbf31..6c7aeec562 100644 --- a/tests/host/common/mock.h +++ b/tests/host/common/mock.h @@ -160,13 +160,13 @@ int mockUDPSocket(); bool mockUDPListen(int sock, uint32_t dstaddr, uint16_t port, uint32_t mcast = 0); size_t mockUDPFillInBuf(int sock, char* ccinbuf, size_t& ccinbufsize, uint8_t& addrsize, uint8_t addr[16], uint16_t& port); -size_t mockUDPPeekBytes(int sock, char* dst, size_t usersize, int timeout_ms, char* ccinbuf, +size_t mockUDPPeekBytes(int sock, char* dst, size_t offset, size_t usersize, int timeout_ms, char* ccinbuf, size_t& ccinbufsize); -size_t mockUDPRead(int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, - size_t& ccinbufsize); +//size_t mockUDPRead(int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, +// size_t& ccinbufsize); size_t mockUDPWrite(int sock, const uint8_t* data, size_t size, int timeout_ms, uint32_t ipv4, uint16_t port); -void mockUDPSwallow(size_t copied, char* ccinbuf, size_t& ccinbufsize); +//void mockUDPSwallow(size_t copied, char* ccinbuf, size_t& ccinbufsize); class UdpContext; void register_udp(int sock, UdpContext* udp = nullptr); From 56e612ac4e273128e6d8ea08f35a99540a4fb3f9 Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Tue, 24 Jan 2023 00:39:09 +0100 Subject: [PATCH 2/5] mDNS hack: force publishing service on request answers --- libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp index f575983ef7..724ee27718 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp @@ -234,6 +234,13 @@ namespace MDNSImplementation = (((ProbingStatus_Done == pService->m_ProbeInformation.m_ProbingStatus)) ? _replyMaskForService(questionRR.m_Header, *pService, 0) : 0); + + if (pService->m_ProbeInformation.m_ProbingStatus != ProbingStatus_InProgress) + { + // reply with service description (check MDNSResponder::_announce() comments) + u8ReplyMaskForQuestion |= (ContentFlag_SRV | ContentFlag_TXT | ContentFlag_PTR_NAME | ContentFlag_PTR_TYPE); + } + u8HostOrServiceReplies |= (pService->m_u8ReplyMask |= u8ReplyMaskForQuestion); DEBUG_EX_INFO(if (u8ReplyMaskForQuestion) { DEBUG_OUTPUT.printf_P( From e1deea56b72c8481d611b5ada51c70184a44b57b Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Tue, 24 Jan 2023 00:45:38 +0100 Subject: [PATCH 3/5] mock: cleaning --- tests/host/common/UdpContextSocket.cpp | 17 ----------------- tests/host/common/mock.h | 3 --- 2 files changed, 20 deletions(-) diff --git a/tests/host/common/UdpContextSocket.cpp b/tests/host/common/UdpContextSocket.cpp index 775d21b7a6..f435b1d4a9 100644 --- a/tests/host/common/UdpContextSocket.cpp +++ b/tests/host/common/UdpContextSocket.cpp @@ -187,23 +187,6 @@ size_t mockUDPPeekBytes(int sock, char* dst, size_t offset, size_t usersize, int return retsize; } -/* -void mockUDPSwallow(size_t copied, char* ccinbuf, size_t& ccinbufsize) -{ - // poor man buffer - memmove(ccinbuf, ccinbuf + copied, ccinbufsize - copied); - ccinbufsize -= copied; -} - -size_t mockUDPRead(int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, - size_t& ccinbufsize) -{ - size_t copied = mockUDPPeekBytes(sock, dst, size, timeout_ms, ccinbuf, ccinbufsize); - mockUDPSwallow(copied, ccinbuf, ccinbufsize); - return copied; -} -*/ - size_t mockUDPWrite(int sock, const uint8_t* data, size_t size, int timeout_ms, uint32_t ipv4, uint16_t port) { diff --git a/tests/host/common/mock.h b/tests/host/common/mock.h index 6c7aeec562..97b71b2acb 100644 --- a/tests/host/common/mock.h +++ b/tests/host/common/mock.h @@ -162,11 +162,8 @@ size_t mockUDPFillInBuf(int sock, char* ccinbuf, size_t& ccinbufsize, uint8_t& a uint8_t addr[16], uint16_t& port); size_t mockUDPPeekBytes(int sock, char* dst, size_t offset, size_t usersize, int timeout_ms, char* ccinbuf, size_t& ccinbufsize); -//size_t mockUDPRead(int sock, char* dst, size_t size, int timeout_ms, char* ccinbuf, -// size_t& ccinbufsize); size_t mockUDPWrite(int sock, const uint8_t* data, size_t size, int timeout_ms, uint32_t ipv4, uint16_t port); -//void mockUDPSwallow(size_t copied, char* ccinbuf, size_t& ccinbufsize); class UdpContext; void register_udp(int sock, UdpContext* udp = nullptr); From 1f76458531e77f66a484a568a274f599a6e40c7b Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Tue, 24 Jan 2023 00:58:20 +0100 Subject: [PATCH 4/5] style --- libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp | 3 ++- tests/host/common/UdpContextSocket.cpp | 4 ++-- tests/host/common/include/UdpContext.h | 4 ++-- tests/host/common/mock.h | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp index 724ee27718..8c2e227f01 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp @@ -238,7 +238,8 @@ namespace MDNSImplementation if (pService->m_ProbeInformation.m_ProbingStatus != ProbingStatus_InProgress) { // reply with service description (check MDNSResponder::_announce() comments) - u8ReplyMaskForQuestion |= (ContentFlag_SRV | ContentFlag_TXT | ContentFlag_PTR_NAME | ContentFlag_PTR_TYPE); + u8ReplyMaskForQuestion |= (ContentFlag_SRV | ContentFlag_TXT + | ContentFlag_PTR_NAME | ContentFlag_PTR_TYPE); } u8HostOrServiceReplies |= (pService->m_u8ReplyMask |= u8ReplyMaskForQuestion); diff --git a/tests/host/common/UdpContextSocket.cpp b/tests/host/common/UdpContextSocket.cpp index f435b1d4a9..2ffa4eb22f 100644 --- a/tests/host/common/UdpContextSocket.cpp +++ b/tests/host/common/UdpContextSocket.cpp @@ -166,8 +166,8 @@ size_t mockUDPFillInBuf(int sock, char* ccinbuf, size_t& ccinbufsize, uint8_t& a return ccinbufsize += ret; } -size_t mockUDPPeekBytes(int sock, char* dst, size_t offset, size_t usersize, int timeout_ms, char* ccinbuf, - size_t& ccinbufsize) +size_t mockUDPPeekBytes(int sock, char* dst, size_t offset, size_t usersize, int timeout_ms, + char* ccinbuf, size_t& ccinbufsize) { (void)sock; (void)timeout_ms; diff --git a/tests/host/common/include/UdpContext.h b/tests/host/common/include/UdpContext.h index 95b76604ac..e8ae9461e9 100644 --- a/tests/host/common/include/UdpContext.h +++ b/tests/host/common/include/UdpContext.h @@ -165,7 +165,7 @@ class UdpContext bool next() { _inbufsize = 0; - _inoffset = 0; + _inoffset = 0; mockUDPFillInBuf(_sock, _inbuf, _inbufsize, addrsize, addr, _dstport); if (_inbufsize > 0) { @@ -284,7 +284,7 @@ class UdpContext char _inbuf[CCBUFSIZE]; size_t _inbufsize = 0; - size_t _inoffset = 0; + size_t _inoffset = 0; char _outbuf[CCBUFSIZE]; size_t _outbufsize = 0; diff --git a/tests/host/common/mock.h b/tests/host/common/mock.h index 97b71b2acb..b3308282f0 100644 --- a/tests/host/common/mock.h +++ b/tests/host/common/mock.h @@ -160,8 +160,8 @@ int mockUDPSocket(); bool mockUDPListen(int sock, uint32_t dstaddr, uint16_t port, uint32_t mcast = 0); size_t mockUDPFillInBuf(int sock, char* ccinbuf, size_t& ccinbufsize, uint8_t& addrsize, uint8_t addr[16], uint16_t& port); -size_t mockUDPPeekBytes(int sock, char* dst, size_t offset, size_t usersize, int timeout_ms, char* ccinbuf, - size_t& ccinbufsize); +size_t mockUDPPeekBytes(int sock, char* dst, size_t offset, size_t usersize, int timeout_ms, + char* ccinbuf, size_t& ccinbufsize); size_t mockUDPWrite(int sock, const uint8_t* data, size_t size, int timeout_ms, uint32_t ipv4, uint16_t port); From 192e7e5e7872515723f6f2818ea2dcc880603dac Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Tue, 24 Jan 2023 13:22:57 +0100 Subject: [PATCH 5/5] revert mDNS change to make this a pure mock UDP fix --- libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp index 8c2e227f01..f575983ef7 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp @@ -234,14 +234,6 @@ namespace MDNSImplementation = (((ProbingStatus_Done == pService->m_ProbeInformation.m_ProbingStatus)) ? _replyMaskForService(questionRR.m_Header, *pService, 0) : 0); - - if (pService->m_ProbeInformation.m_ProbingStatus != ProbingStatus_InProgress) - { - // reply with service description (check MDNSResponder::_announce() comments) - u8ReplyMaskForQuestion |= (ContentFlag_SRV | ContentFlag_TXT - | ContentFlag_PTR_NAME | ContentFlag_PTR_TYPE); - } - u8HostOrServiceReplies |= (pService->m_u8ReplyMask |= u8ReplyMaskForQuestion); DEBUG_EX_INFO(if (u8ReplyMaskForQuestion) { DEBUG_OUTPUT.printf_P(