Skip to content

Commit

Permalink
emulation on host: fix UDP seek&peek (#8827)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-a-v committed Jan 24, 2023
1 parent cf24024 commit a9675ca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
25 changes: 5 additions & 20 deletions tests/host/common/UdpContextSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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& 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;
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)
Expand All @@ -183,25 +183,10 @@ 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
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)
{
Expand Down
15 changes: 10 additions & 5 deletions tests/host/common/include/UdpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -165,6 +165,7 @@ class UdpContext
bool next()
{
_inbufsize = 0;
_inoffset = 0;
mockUDPFillInBuf(_sock, _inbuf, _inbufsize, addrsize, addr, _dstport);
if (_inbufsize > 0)
{
Expand All @@ -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()
Expand Down Expand Up @@ -280,6 +284,7 @@ class UdpContext

char _inbuf[CCBUFSIZE];
size_t _inbufsize = 0;
size_t _inoffset = 0;
char _outbuf[CCBUFSIZE];
size_t _outbufsize = 0;

Expand Down
7 changes: 2 additions & 5 deletions tests/host/common/mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,10 @@ 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& ccinbufsize);
size_t mockUDPRead(int sock, char* dst, size_t size, 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);
void mockUDPSwallow(size_t copied, char* ccinbuf, size_t& ccinbufsize);

class UdpContext;
void register_udp(int sock, UdpContext* udp = nullptr);
Expand Down

0 comments on commit a9675ca

Please sign in to comment.