Added peek option to dgram_recvfrom syscall

This commit is contained in:
warp03 2024-04-28 22:03:52 +02:00
parent 2c3a73311e
commit 3906201c4b
Signed by: warp03
GPG Key ID: 6ECEEC10CF69C60C
5 changed files with 26 additions and 18 deletions
buildNumber
include
shared/hadronshared
sock
sock

@ -1 +1 @@
703
704

@ -510,9 +510,11 @@
int fd: The socket descriptor to receive from
void* dest: The destination buffer
size_t* len: The buffer length; returns the number of bytes read. If the referenced value is lower than the datagram size, the datagram is truncated
char* srcaddr: The buffer to write the source address to (optional)
size_t* srcaddrlen: The source address buffer length. Returns the address length including the string termination character.
If the buffer is too small (max SOCK_MAX_ADDR_LEN), the address is truncated (optional with srcaddr)
<coalesced param ptr / 2>
char* srcaddr: The buffer to write the source address to (optional)
size_t* srcaddrlen: The source address buffer length. Returns the address length including the string termination character.
If the buffer is too small (max SOCK_MAX_ADDR_LEN), the address is truncated (optional with srcaddr)
int peek: If 1, data is not consumed and same data is read at next call
*/
#define SYSCALL_N_DGRAM_RECVFROM 52

@ -240,8 +240,9 @@ inline static status_t syscall_initfs_read(const char* filename, void* dest, siz
return syscall(SYSCALL_N_INITFS_READ, (size_t) filename, (size_t) dest, (size_t) len, (size_t) info, 0);
}
inline static status_t syscall_dgram_recvfrom(int fd, void* dest, size_t* len, char* srcaddr, size_t* srcaddrlen){
return syscall(SYSCALL_N_DGRAM_RECVFROM, (size_t) fd, (size_t) dest, (size_t) len, (size_t) srcaddr, (size_t) srcaddrlen);
inline static status_t syscall_dgram_recvfrom(int fd, void* dest, size_t* len, char* srcaddr, size_t* srcaddrlen, int peek){
size_t coalesced2[2] = { (size_t) srcaddr, (size_t) srcaddrlen };
return syscall(SYSCALL_N_DGRAM_RECVFROM, (size_t) fd, (size_t) dest, (size_t) len, (size_t) &coalesced2[0], (size_t) peek);
}
inline static status_t syscall_dgram_sendto(int fd, void* src, size_t* len, const char* address){

@ -51,7 +51,7 @@ status_t dgram_connect(sock_s* ssocket, const char* addr);
status_t dgram_read(sock_s* ssocket, void* dstBuf, size_t* len);
status_t dgram_write(sock_s* ssocket, void* srcBuf, size_t* len);
status_t dgram_recvfrom(sock_s* ssocket, void* dst_buf, size_t* len, char* address, size_t* addr_len);
status_t dgram_recvfrom(sock_s* ssocket, void* dst_buf, size_t* len, char* address, size_t* addr_len, int peek);
status_t dgram_sendto(sock_s* ssocket, void* src_buf, size_t* len, const char* address);
status_t dgram_close(sock_s* ssocket);

@ -106,7 +106,7 @@ status_t dgram_connect(sock_s* ssocket, const char* addr){
status_t dgram_read(sock_s* ssocket, void* dstBuf, size_t* len){
return dgram_recvfrom(ssocket, dstBuf, len, NULL, NULL);
return dgram_recvfrom(ssocket, dstBuf, len, NULL, NULL, 0);
}
status_t dgram_write(sock_s* ssocket, void* srcBuf, size_t* len){
@ -117,7 +117,7 @@ status_t dgram_write(sock_s* ssocket, void* srcBuf, size_t* len){
}
status_t dgram_recvfrom(sock_s* ssocket, void* dst_buf, size_t* len, char* address, size_t* addr_len){
status_t dgram_recvfrom(sock_s* ssocket, void* dst_buf, size_t* len, char* address, size_t* addr_len, int peek){
status_t status = 0;
prog_thread* thread = sched_get_current_running_thread();
dgram_socket* socket = ssocket->ptr;
@ -128,19 +128,21 @@ status_t dgram_recvfrom(sock_s* ssocket, void* dst_buf, size_t* len, char* addre
if(address && addr_len){
size_t addr_len_v = *addr_len;
if(addr_len_v < hdr.srcaddrlen){
circbuf_read(address, socket->messageBuf, addr_len_v);
circbuf_read(NULL, socket->messageBuf, hdr.srcaddrlen - addr_len_v);
circbuf_nextdata(address, socket->messageBuf, addr_len_v, !peek);
if(!peek)
circbuf_consume(socket->messageBuf, hdr.srcaddrlen - addr_len_v);
}else
circbuf_read(address, socket->messageBuf, hdr.srcaddrlen);
circbuf_nextdata(address, socket->messageBuf, hdr.srcaddrlen, !peek);
*addr_len = hdr.srcaddrlen;
}else
circbuf_read(NULL, socket->messageBuf, hdr.srcaddrlen);
circbuf_consume(socket->messageBuf, hdr.srcaddrlen);
size_t len_v = *len;
if(len_v < hdr.len){
circbuf_read(dst_buf, socket->messageBuf, len_v);
circbuf_read(NULL, socket->messageBuf, hdr.len - len_v);
circbuf_nextdata(dst_buf, socket->messageBuf, len_v, !peek);
if(!peek)
circbuf_consume(socket->messageBuf, hdr.len - len_v);
}else
circbuf_read(dst_buf, socket->messageBuf, hdr.len);
circbuf_nextdata(dst_buf, socket->messageBuf, hdr.len, !peek);
*len = hdr.len;
break;
}
@ -211,7 +213,10 @@ status_t dgram_close(sock_s* ssocket){
}
SYSCALL_DEFINE5(SYSCALL_N_DGRAM_RECVFROM, int, fd, void*, dest, size_t*, len, char*, srcaddr, size_t*, srcaddrlen){
SYSCALL_DEFINE5(SYSCALL_N_DGRAM_RECVFROM, int, fd, void*, dest, size_t*, len, size_t*, coalesced2, int, peek){
SYSCALL_VALIDATE_POINTER(coalesced2, sizeof(size_t) * 2);
char* srcaddr = (char*) coalesced2[0];
size_t* srcaddrlen = (size_t*) coalesced2[1];
SYSCALL_VALIDATE_POINTER(len, sizeof(size_t));
SYSCALL_VALIDATE_POINTER(dest, *len);
SYSCALL_VALIDATE_OPT_POINTER(srcaddrlen, sizeof(size_t));
@ -220,7 +225,7 @@ SYSCALL_DEFINE5(SYSCALL_N_DGRAM_RECVFROM, int, fd, void*, dest, size_t*, len, ch
fd_handle* handle = tree_get(sched_get_current_running_thread()->parent->fdHandles, fd);
if(!handle)
return HADRON_NOT_FOUND;
return dgram_recvfrom(handle->ihandle, dest, len, srcaddr, srcaddrlen);
return dgram_recvfrom(handle->ihandle, dest, len, srcaddr, srcaddrlen, peek);
}
SYSCALL_DEFINE4(SYSCALL_N_DGRAM_SENDTO, int, fd, void*, src, size_t*, len, const char*, address){