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 Sep 3, 2024
1 parent 46fa5dd commit 56bb686
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
16 changes: 13 additions & 3 deletions lib/cbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ 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);

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

return bytes;
}


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

Expand All @@ -76,8 +89,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(const cbuffer_t *buf, void *data, size_t sz);

#endif
27 changes: 20 additions & 7 deletions posix/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ static ssize_t recv(unsigned socket, void *buf, size_t len, int flags, struct so
size_t rlen = 0;
int err;
spinlock_ctx_t sc;
int peek;

peek = (flags & MSG_PEEK) != 0;

if ((s = unixsock_get(socket)) == NULL)
return -ENOTSOCK;
Expand All @@ -694,24 +697,34 @@ 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) {
err = _cbuffer_read(&s->buffer, buf, len);
if (peek) {
err = _cbuffer_peek(&s->buffer, buf, len);
}
else {
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));

if (len < rlen)
_cbuffer_discard(&s->buffer, rlen - len);
}
if (err > 0 && control && controllen && *controllen > 0)
fdpass_unpack(&s->fdpacks, control, controllen);
/* TODO: peek control data */
if (!peek) {
if (err > 0 && control && controllen && *controllen > 0)
fdpass_unpack(&s->fdpacks, control, controllen);
}
proc_lockClear(&s->lock);

if (err > 0) {
hal_spinlockSet(&s->spinlock, &sc);
proc_threadWakeup(&s->writeq);
hal_spinlockClear(&s->spinlock, &sc);

if (!peek) {
hal_spinlockSet(&s->spinlock, &sc);
proc_threadWakeup(&s->writeq);
hal_spinlockClear(&s->spinlock, &sc);
}
break;
}
else if (s->type != SOCK_DGRAM && (s->state & US_PEER_CLOSED)) {
Expand Down

0 comments on commit 56bb686

Please sign in to comment.