commit 969d54db90a136b30d9fd7cfd4ff38f7f09efe35
parent 3ccbce7163d62e5c59a5c0c01f4b4588de689a31
Author: Drew DeVault <sir@cmpwn.com>
Date: Wed, 31 Mar 2021 12:16:34 -0400
net: sketch out datagram socket interface
Diffstat:
M | net/socket.ha | | | 61 | +++++++++++++++++++++++++++++++++++++++++++++++++------------ |
1 file changed, 49 insertions(+), 12 deletions(-)
diff --git a/net/socket.ha b/net/socket.ha
@@ -1,4 +1,5 @@
use io;
+use net::ip;
// Enables keep-alive for a socket.
export type keepalive = void;
@@ -13,19 +14,12 @@ export type reuseaddr = void;
// default (10) is used.
export type backlog = u32;
-// To have the system select an arbitrary unused TCP port for [listen], set port
-// to zero. To retrieve the assigned port, provide this as one of the options
-// and the addressed u16 will be filled in with the port.
+// To have the system select an arbitrary unused port for [listen], set port to
+// zero. To retrieve the assigned port, provide this as one of the options and
+// the addressed u16 will be filled in with the port.
export type portassignment = *u16;
-// A listener binds a socket on the system and listens for incoming traffic for
-// some specific protocol.
-export type listener = struct {
- accept: nullable *fn(l: *listener) (*io::stream | io::error),
- shutdown: nullable *fn(l: *listener) void,
-};
-
-// Options for [listen].
+// Options for the [listen] family of functions.
export type listen_option = (
keepalive |
reuseport |
@@ -33,9 +27,27 @@ export type listen_option = (
backlog |
portassignment);
-// Options for [connect].
+// Options for the [connect] family of functions.
export type connect_option = keepalive;
+// Indicates that a send or recv operation should include out-of-band data.
+export type oob = []u8;
+
+// Options for [send] and [recv].
+export type dgram_option = oob;
+
+// A listener binds a socket and listens for incoming traffic for some
+// unspecified protocol.
+export type listener = struct {
+ accept: nullable *fn(l: *listener) (*io::stream | io::error),
+ send: nullable *fn(l: *listener, to: ip::addr, port: u16,
+ buf: []u8, options: dgram_option...) (size | io::error),
+ recv: nullable *fn(l: *listener,
+ from: nullable *ip::addr, port: nullable *u16,
+ buf: []u8, options: dgram_option...) (size | io::error),
+ shutdown: nullable *fn(l: *listener) void,
+};
+
// Accepts the next connection from a listener. Blocks until a new connection is
// available.
export fn accept(l: *listener) (*io::stream | io::error) = {
@@ -52,3 +64,28 @@ export fn shutdown(l: *listener) void = {
null => void,
};
};
+
+// Sends a datagram to the given destination, returning the number of bytes sent
+// (or an error).
+export fn send(
+ l: *listener,
+ to: ip::addr,
+ port: u16,
+ buf: []u8,
+ options: dgram_option...
+) (size | io::error) = {
+ abort(); // TODO
+};
+
+// Blocks until a datagram is received from a listener, writing it to the
+// provided buffer and returning the number of bytes received. Populates the
+// sender address and port in 'from' and 'port' if non-null.
+export fn recv(
+ l: *listener,
+ from: nullable *ip::addr,
+ port: nullable *u16,
+ buf: []u8,
+ options: dgram_option...
+) (size | io::error) = {
+ abort(); // TODO
+};