Skip to content

Commit

Permalink
Add networking syscalls to syscalls.md
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Nov 6, 2023
1 parent d1da967 commit 0dbed15
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 15 deletions.
123 changes: 118 additions & 5 deletions api/syscalls.json
Original file line number Diff line number Diff line change
Expand Up @@ -703,14 +703,127 @@
]
},
{
"subsystem": "fs",
"description": "File I/O and filesystem-related functionality. This subsystem is separated out from the general-purpose io subsystem for security reasons.",
"syscalls": [],
"subsystem": "net",
"description": "Network-related functionality.",
"syscalls": [
{
"name": "net_listen",
"args": [
[
"const char*",
"listen_addr"
],
[
"void*",
"on_new_conn"
]
],
"returns": [
"u64",
"socket_id"
],
"permission": "net_server",
"const_idx": 21,
"description": "Open a listening TCP socket to accept incoming connections. A callback function is called when a new connection request is received."
},
{
"name": "net_accept",
"args": [
[
"u64",
"socket_id"
],
[
"char*",
"client_addr_buf"
],
[
"u64",
"addr_buf_len"
],
[
"void*",
"on_incoming_data"
]
],
"returns": [
"u64",
"socket_id"
],
"permission": "net_server",
"const_idx": 22,
"description": "Accept an incoming connection and creates a new socket. A callback function is called when incoming data is received on the new socket."
},
{
"name": "net_read",
"args": [
[
"u64",
"socket_id"
],
[
"u8*",
"buf_ptr"
],
[
"u64",
"buf_len"
]
],
"returns": [
"u64",
"num_bytes"
],
"permission": "net_io",
"const_idx": 23,
"description": "Read data from a socket into a buffer with specified capacity. Data can only be read if available."
},
{
"name": "net_write",
"args": [
[
"u64",
"socket_id"
],
[
"const u8*",
"buf_ptr"
],
[
"u64",
"buf_len"
]
],
"returns": [
"void",
""
],
"permission": "net_io",
"const_idx": 24,
"description": "Write data to an open socket."
},
{
"name": "net_close",
"args": [
[
"u64",
"socket_id"
]
],
"returns": [
"void",
""
],
"permission": "net_io",
"const_idx": 25,
"description": "Close an open socket."
}
],
"constants": []
},
{
"subsystem": "net",
"description": "Network-related functionality.",
"subsystem": "fs",
"description": "File I/O and filesystem-related functionality. This subsystem is separated out from the general-purpose io subsystem for security reasons.",
"syscalls": [],
"constants": []
}
Expand Down
54 changes: 50 additions & 4 deletions doc/syscalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,57 @@ These are the constants associated with the audio subsystem:

- `u16 AUDIO_FORMAT_I16 = 0`

# fs

File I/O and filesystem-related functionality. This subsystem is separated out from the general-purpose io subsystem for security reasons.

# net

Network-related functionality.

## net_listen

```
u64 net_listen(const char* listen_addr, void* on_new_conn)
```

**Returns:** `u64 socket_id`

Open a listening TCP socket to accept incoming connections. A callback function is called when a new connection request is received.

## net_accept

```
u64 net_accept(u64 socket_id, char* client_addr_buf, u64 addr_buf_len, void* on_incoming_data)
```

**Returns:** `u64 socket_id`

Accept an incoming connection and creates a new socket. A callback function is called when incoming data is received on the new socket.

## net_read

```
u64 net_read(u64 socket_id, u8* buf_ptr, u64 buf_len)
```

**Returns:** `u64 num_bytes`

Read data from a socket into a buffer with specified capacity. Data can only be read if available.

## net_write

```
void net_write(u64 socket_id, const u8* buf_ptr, u64 buf_len)
```

Write data to an open socket.

## net_close

```
void net_close(u64 socket_id)
```

Close an open socket.

# fs

File I/O and filesystem-related functionality. This subsystem is separated out from the general-purpose io subsystem for security reasons.

20 changes: 20 additions & 0 deletions ncc/include/uvm/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@
// Open an audio output device.
#define audio_open_output(__sample_rate, __num_channels, __format, __callback) asm (__sample_rate, __num_channels, __format, __callback) -> u32 { syscall audio_open_output; }

// u64 net_listen(const char* listen_addr, void* on_new_conn)
// Open a listening TCP socket to accept incoming connections. A callback function is called when a new connection request is received.
#define net_listen(__listen_addr, __on_new_conn) asm (__listen_addr, __on_new_conn) -> u64 { syscall net_listen; }

// u64 net_accept(u64 socket_id, char* client_addr_buf, u64 addr_buf_len, void* on_incoming_data)
// Accept an incoming connection and creates a new socket. A callback function is called when incoming data is received on the new socket.
#define net_accept(__socket_id, __client_addr_buf, __addr_buf_len, __on_incoming_data) asm (__socket_id, __client_addr_buf, __addr_buf_len, __on_incoming_data) -> u64 { syscall net_accept; }

// u64 net_read(u64 socket_id, u8* buf_ptr, u64 buf_len)
// Read data from a socket into a buffer with specified capacity. Data can only be read if available.
#define net_read(__socket_id, __buf_ptr, __buf_len) asm (__socket_id, __buf_ptr, __buf_len) -> u64 { syscall net_read; }

// void net_write(u64 socket_id, const u8* buf_ptr, u64 buf_len)
// Write data to an open socket.
#define net_write(__socket_id, __buf_ptr, __buf_len) asm (__socket_id, __buf_ptr, __buf_len) -> void { syscall net_write; }

// void net_close(u64 socket_id)
// Close an open socket.
#define net_close(__socket_id) asm (__socket_id) -> void { syscall net_close; }

#define KEY_BACKSPACE 8
#define KEY_TAB 9
#define KEY_RETURN 10
Expand Down
12 changes: 11 additions & 1 deletion vm/src/sys/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#![allow(unused)]

pub const SYSCALL_TBL_LEN: usize = 21;
pub const SYSCALL_TBL_LEN: usize = 26;

pub const TIME_CURRENT_MS: u16 = 0;
pub const WINDOW_CREATE: u16 = 1;
Expand All @@ -27,6 +27,11 @@ pub const VM_RESIZE_HEAP: u16 = 17;
pub const AUDIO_OPEN_OUTPUT: u16 = 18;
pub const WINDOW_ON_TEXTINPUT: u16 = 19;
pub const PRINT_F32: u16 = 20;
pub const NET_LISTEN: u16 = 21;
pub const NET_ACCEPT: u16 = 22;
pub const NET_READ: u16 = 23;
pub const NET_WRITE: u16 = 24;
pub const NET_CLOSE: u16 = 25;

pub struct SysCallDesc
{
Expand Down Expand Up @@ -58,6 +63,11 @@ pub const SYSCALL_DESCS: [Option<SysCallDesc>; SYSCALL_TBL_LEN] = [
Some(SysCallDesc { name: "audio_open_output", const_idx: 18, argc: 4, has_ret: true }),
Some(SysCallDesc { name: "window_on_textinput", const_idx: 19, argc: 2, has_ret: false }),
Some(SysCallDesc { name: "print_f32", const_idx: 20, argc: 1, has_ret: false }),
Some(SysCallDesc { name: "net_listen", const_idx: 21, argc: 2, has_ret: true }),
Some(SysCallDesc { name: "net_accept", const_idx: 22, argc: 4, has_ret: true }),
Some(SysCallDesc { name: "net_read", const_idx: 23, argc: 3, has_ret: true }),
Some(SysCallDesc { name: "net_write", const_idx: 24, argc: 3, has_ret: false }),
Some(SysCallDesc { name: "net_close", const_idx: 25, argc: 1, has_ret: false }),
];

pub const KEY_BACKSPACE: u16 = 8;
Expand Down
10 changes: 5 additions & 5 deletions vm/src/sys/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ fn listen_thread(
}

// Syscall to create a TCP listening socket to accept incoming connections
// u64 socket_id = net_listen_tcp(
// const char* bind_address, // Network interface address to listen on, null for any address
// u64 socket_id = net_listen(
// const char* listen_addr, // Network interface address to listen on, null for any address
// callback on_new_connection, // Called on new incoming connection
// )
pub fn net_listen_tcp(
pub fn net_listen(
vm: &mut VM,
bind_address: Value,
listen_addr: Value,
on_new_conn: Value,
) -> Value
{
// Get the input address and port to listen on
let listen_addr = vm.get_heap_str(bind_address.as_usize());
let listen_addr = vm.get_heap_str(listen_addr.as_usize());

// TODO: return 0 on failure
let listener = TcpListener::bind(listen_addr).unwrap();
Expand Down

0 comments on commit 0dbed15

Please sign in to comment.