Skip to content

Commit

Permalink
posix: implement MSG_PEEK handling in unix socket recv
Browse files Browse the repository at this point in the history
JIRA: RTOS-877
  • Loading branch information
adamgreloch committed Aug 2, 2024
1 parent 132ad8a commit 544de87
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
14 changes: 11 additions & 3 deletions lib/cbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ int _cbuffer_write(cbuffer_t *buf, const void *data, size_t sz)


int _cbuffer_read(cbuffer_t *buf, void *data, size_t sz)
{
int bytes = _cbuffer_peek(buf, data, sz);

buf->r = (buf->r + bytes) & (buf->sz - 1);
buf->full = 0;

return bytes;
}


int _cbuffer_peek(cbuffer_t *buf, void *data, size_t sz)
{
int bytes = 0;

Expand All @@ -76,8 +87,5 @@ int _cbuffer_read(cbuffer_t *buf, void *data, size_t sz)
}
}

buf->r = (buf->r + bytes) & (buf->sz - 1);
buf->full = 0;

return bytes;
}
4 changes: 3 additions & 1 deletion lib/cbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ extern int _cbuffer_read(cbuffer_t *buf, void *data, size_t sz);
extern int _cbuffer_write(cbuffer_t *buf, const void *data, size_t sz);


#endif
extern int _cbuffer_peek(cbuffer_t *buf, void *data, size_t sz);

#endif
7 changes: 7 additions & 0 deletions posix/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ int unix_getsockopt(unsigned socket, int level, int optname, void *optval, sockl
}


/* TODO: peek fdpacks on MSG_PEEK */
static ssize_t recv(unsigned socket, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *src_len, void *control, socklen_t *controllen)
{
unixsock_t *s;
Expand All @@ -688,9 +689,15 @@ static ssize_t recv(unsigned socket, void *buf, size_t len, int flags, struct so
for (;;) {
proc_lockSet(&s->lock);
if (s->type == SOCK_STREAM) {
if (flags & MSG_PEEK) {
err = _cbuffer_peek(&s->buffer, buf, len);
proc_lockClear(&s->lock);
break;
}
err = _cbuffer_read(&s->buffer, buf, len);
}
else if (_cbuffer_avail(&s->buffer) > 0) { /* SOCK_DGRAM or SOCK_SEQPACKET */
/* TODO: handle MSG_PEEK */
_cbuffer_read(&s->buffer, &rlen, sizeof(rlen));
_cbuffer_read(&s->buffer, buf, err = min(len, rlen));

Expand Down

0 comments on commit 544de87

Please sign in to comment.