summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2025-02-23 09:24:31 -0700
committerLuke T. Shumaker <lukeshu@lukeshu.com>2025-03-25 19:03:06 -0600
commit467c9e2bdbe1192635c786b4ae2120e2ffc7fb63 (patch)
tree19150835e332d47133c7e372bcb8513c6e9f76a6
parent654b1940705197f22ffeebd73e62cea2282bdbda (diff)
libhw/generic/net.h: Tidy up the interfaces
-rw-r--r--lib9p/srv.c8
-rw-r--r--libdhcp/dhcp_client.c10
-rw-r--r--libhw/host_net.c32
-rw-r--r--libhw/rp2040_include/libhw/w5500.h7
-rw-r--r--libhw/w5500.c17
-rw-r--r--libhw_generic/include/libhw/generic/io.h23
-rw-r--r--libhw_generic/include/libhw/generic/net.h26
-rw-r--r--libhw_generic/include/libhw/generic/spi.h1
8 files changed, 82 insertions, 42 deletions
diff --git a/lib9p/srv.c b/lib9p/srv.c
index c624fa8..580c5f5 100644
--- a/lib9p/srv.c
+++ b/lib9p/srv.c
@@ -293,12 +293,14 @@ static void handle_message(struct _lib9p_srv_req *ctx);
_lib9p_srv_reqch_send_req(&srv->_reqch, &req);
}
close:
- LO_CALL(conn.fd, close, true, sess.reqs.len == 0);
- if (sess.reqs.len) {
+ if (sess.reqs.len == 0)
+ io_close(conn.fd);
+ else {
+ io_close_read(conn.fd);
sess.closing = true;
cr_pause_and_yield();
assert(sess.reqs.len == 0);
- LO_CALL(conn.fd, close, true, true);
+ io_close_write(conn.fd);
}
}
}
diff --git a/libdhcp/dhcp_client.c b/libdhcp/dhcp_client.c
index e50c5f3..8ec3647 100644
--- a/libdhcp/dhcp_client.c
+++ b/libdhcp/dhcp_client.c
@@ -775,7 +775,7 @@ static void dhcp_client_setstate(struct dhcp_client *client,
dhcp_client_setstate(client, STATE_SELECTING, DHCP_MSGTYP_DISCOVER, NULL, scratch_msg);
break;
case STATE_SELECTING:
- LO_CALL(client->sock, set_read_deadline, client->time_ns_init+CONFIG_DHCP_SELECTING_NS);
+ LO_CALL(client->sock, set_recv_deadline, client->time_ns_init+CONFIG_DHCP_SELECTING_NS);
switch ((r = dhcp_client_recv(client, scratch_msg))) {
case 0:
switch (scratch_msg->option_dat[scratch_msg->options[DHCP_OPT_DHCP_MSG_TYPE].off]) {
@@ -797,7 +797,7 @@ static void dhcp_client_setstate(struct dhcp_client *client,
}
break;
case STATE_REQUESTING:
- LO_CALL(client->sock, set_read_deadline, 0);
+ LO_CALL(client->sock, set_recv_deadline, 0);
switch ((r = dhcp_client_recv(client, scratch_msg))) {
case 0:
switch (scratch_msg->option_dat[scratch_msg->options[DHCP_OPT_DHCP_MSG_TYPE].off]) {
@@ -824,7 +824,7 @@ static void dhcp_client_setstate(struct dhcp_client *client,
}
break;
case STATE_BOUND:
- LO_CALL(client->sock, set_read_deadline, client->lease_time_ns_t1);
+ LO_CALL(client->sock, set_recv_deadline, client->lease_time_ns_t1);
switch ((r = dhcp_client_recv(client, scratch_msg))) {
case 0:
/* discard */
@@ -841,7 +841,7 @@ static void dhcp_client_setstate(struct dhcp_client *client,
client->xid = rand_uint63n(UINT32_MAX);
client->time_ns_init = LO_CALL(bootclock, get_time_ns);
- LO_CALL(client->sock, set_read_deadline, client->lease_time_ns_t2);
+ LO_CALL(client->sock, set_recv_deadline, client->lease_time_ns_t2);
switch ((r = dhcp_client_recv(client, scratch_msg))) {
case 0:
switch (scratch_msg->option_dat[scratch_msg->options[DHCP_OPT_DHCP_MSG_TYPE].off]) {
@@ -867,7 +867,7 @@ static void dhcp_client_setstate(struct dhcp_client *client,
}
break;
case STATE_REBINDING:
- LO_CALL(client->sock, set_read_deadline, client->lease_time_ns_end);
+ LO_CALL(client->sock, set_recv_deadline, client->lease_time_ns_end);
switch ((r = dhcp_client_recv(client, scratch_msg))) {
case 0:
switch (scratch_msg->option_dat[scratch_msg->options[DHCP_OPT_DHCP_MSG_TYPE].off]) {
diff --git a/libhw/host_net.c b/libhw/host_net.c
index eee293e..f977739 100644
--- a/libhw/host_net.c
+++ b/libhw/host_net.c
@@ -30,8 +30,14 @@
#include "host_util.h" /* for host_sigrt_alloc(), ns_to_host_us_time() */
-LO_IMPLEMENTATION_C(net_stream_conn, struct _hostnet_tcp_conn, hostnet_tcp, static)
+LO_IMPLEMENTATION_C(io_closer, struct hostnet_tcp_listener, hostnet_tcplist, static)
LO_IMPLEMENTATION_C(net_stream_listener, struct hostnet_tcp_listener, hostnet_tcplist, static)
+
+LO_IMPLEMENTATION_C(io_closer, struct _hostnet_tcp_conn, hostnet_tcp, static)
+LO_IMPLEMENTATION_C(io_bidi_closer, struct _hostnet_tcp_conn, hostnet_tcp, static)
+LO_IMPLEMENTATION_C(net_stream_conn, struct _hostnet_tcp_conn, hostnet_tcp, static)
+
+LO_IMPLEMENTATION_C(io_closer, struct hostnet_udp_conn, hostnet_udp, static)
LO_IMPLEMENTATION_C(net_packet_conn, struct hostnet_udp_conn, hostnet_udp, static)
/* common *********************************************************************/
@@ -312,19 +318,17 @@ static ssize_t hostnet_tcp_write(struct _hostnet_tcp_conn *conn, void *buf, size
/* TCP close() ****************************************************************/
-static int hostnet_tcp_close(struct _hostnet_tcp_conn *conn, bool rd, bool wr) {
+static int hostnet_tcp_close(struct _hostnet_tcp_conn *conn) {
assert(conn);
-
- int how;
- if (rd && wr)
- how = SHUT_RDWR;
- else if (rd && !wr)
- how = SHUT_RD;
- else if (!rd && wr)
- how = SHUT_WR;
- else
- assert_notreached("invalid arguments to stream_conn.close()");
- return hostnet_map_negerrno(shutdown(conn->fd, how) ? -errno : 0, OP_NONE);
+ return hostnet_map_negerrno(shutdown(conn->fd, SHUT_RDWR) ? -errno : 0, OP_NONE);
+}
+static int hostnet_tcp_close_read(struct _hostnet_tcp_conn *conn) {
+ assert(conn);
+ return hostnet_map_negerrno(shutdown(conn->fd, SHUT_RD) ? -errno : 0, OP_NONE);
+}
+static int hostnet_tcp_close_write(struct _hostnet_tcp_conn *conn) {
+ assert(conn);
+ return hostnet_map_negerrno(shutdown(conn->fd, SHUT_WR) ? -errno : 0, OP_NONE);
}
/* UDP init() *****************************************************************/
@@ -412,7 +416,7 @@ static ssize_t hostnet_udp_sendto(struct hostnet_udp_conn *conn, void *buf, size
/* UDP recvfrom() *************************************************************/
-static void hostnet_udp_set_read_deadline(struct hostnet_udp_conn *conn,
+static void hostnet_udp_set_recv_deadline(struct hostnet_udp_conn *conn,
uint64_t ts_ns) {
assert(conn);
diff --git a/libhw/rp2040_include/libhw/w5500.h b/libhw/rp2040_include/libhw/w5500.h
index 98e54ec..a035069 100644
--- a/libhw/rp2040_include/libhw/w5500.h
+++ b/libhw/rp2040_include/libhw/w5500.h
@@ -40,8 +40,15 @@ struct _w5500_socket {
END_PRIVATE(LIBHW_W5500_H)
};
+
+LO_IMPLEMENTATION_H(io_closer, struct _w5500_socket, w5500_tcplist)
LO_IMPLEMENTATION_H(net_stream_listener, struct _w5500_socket, w5500_tcplist)
+
+LO_IMPLEMENTATION_H(io_closer, struct _w5500_socket, w5500_tcp)
+LO_IMPLEMENTATION_H(io_bidi_closer, struct _w5500_socket, w5500_tcp)
LO_IMPLEMENTATION_H(net_stream_conn, struct _w5500_socket, w5500_tcp)
+
+LO_IMPLEMENTATION_H(io_closer, struct _w5500_socket, w5500_udp)
LO_IMPLEMENTATION_H(net_packet_conn, struct _w5500_socket, w5500_udp)
struct w5500 {
diff --git a/libhw/w5500.c b/libhw/w5500.c
index a8777db..cd96696 100644
--- a/libhw/w5500.c
+++ b/libhw/w5500.c
@@ -125,9 +125,16 @@ static const char *w5500_state_str(uint8_t state) {
/* libobj *********************************************************************/
+LO_IMPLEMENTATION_C(io_closer, struct _w5500_socket, w5500_tcplist, static)
LO_IMPLEMENTATION_C(net_stream_listener, struct _w5500_socket, w5500_tcplist, static)
+
+LO_IMPLEMENTATION_C(io_closer, struct _w5500_socket, w5500_tcp, static)
+LO_IMPLEMENTATION_C(io_bidi_closer, struct _w5500_socket, w5500_tcp, static)
LO_IMPLEMENTATION_C(net_stream_conn, struct _w5500_socket, w5500_tcp, static)
+
+LO_IMPLEMENTATION_C(io_closer, struct _w5500_socket, w5500_udp, static)
LO_IMPLEMENTATION_C(net_packet_conn, struct _w5500_socket, w5500_udp, static)
+
LO_IMPLEMENTATION_C(net_iface, struct w5500, w5500_if, static)
/* mid-level utilities ********************************************************/
@@ -746,7 +753,7 @@ static ssize_t w5500_tcp_read(struct _w5500_socket *socket, void *buf, size_t co
return avail;
}
-static int w5500_tcp_close(struct _w5500_socket *socket, bool rd, bool wr) {
+static int w5500_tcp_close_inner(struct _w5500_socket *socket, bool rd, bool wr) {
debugf("tcp_conn.close(rd=%s, wr=%s)", rd ? "true" : "false", wr ? "true" : "false");
ASSERT_SELF(stream_conn, TCP);
@@ -777,6 +784,10 @@ static int w5500_tcp_close(struct _w5500_socket *socket, bool rd, bool wr) {
return 0;
}
+static int w5500_tcp_close(struct _w5500_socket *socket) { return w5500_tcp_close_inner(socket, true, true); }
+static int w5500_tcp_close_read(struct _w5500_socket *socket) { return w5500_tcp_close_inner(socket, true, false); }
+static int w5500_tcp_close_write(struct _w5500_socket *socket) { return w5500_tcp_close_inner(socket, false, true); }
+
/* udp_conn methods ***********************************************************/
static ssize_t w5500_udp_sendto(struct _w5500_socket *socket, void *buf, size_t count,
@@ -836,8 +847,8 @@ static ssize_t w5500_udp_sendto(struct _w5500_socket *socket, void *buf, size_t
}
}
-static void w5500_udp_set_read_deadline(struct _w5500_socket *socket, uint64_t ns) {
- debugf("udp_conn.set_read_deadline(%"PRIu64")", ns);
+static void w5500_udp_set_recv_deadline(struct _w5500_socket *socket, uint64_t ns) {
+ debugf("udp_conn.set_recv_deadline(%"PRIu64")", ns);
ASSERT_SELF(packet_conn, UDP);
socket->read_deadline_ns = ns;
}
diff --git a/libhw_generic/include/libhw/generic/io.h b/libhw_generic/include/libhw/generic/io.h
index 681c5ef..9128bd3 100644
--- a/libhw_generic/include/libhw/generic/io.h
+++ b/libhw_generic/include/libhw/generic/io.h
@@ -9,6 +9,8 @@
#include <stddef.h> /* for size_t */
+#include <libobj/obj.h>
+
/* structs ********************************************************************/
#if __unix__
@@ -26,4 +28,25 @@ struct duplex_iovec {
size_t iov_len;
};
+/* basic interfaces ***********************************************************/
+
+/**
+ * Return 0 on success, -errno on error.
+ */
+#define io_closer_LO_IFACE \
+ LO_FUNC(int, close)
+LO_INTERFACE(io_closer)
+#define io_close(c) LO_CALL(c, close)
+
+/**
+ * All methods return 0 on success, -errno on error.
+ */
+#define io_bidi_closer_LO_IFACE \
+ LO_NEST(io_closer) \
+ LO_FUNC(int, close_read) \
+ LO_FUNC(int, close_write)
+LO_INTERFACE(io_bidi_closer)
+#define io_close_read(c) LO_CALL(c, close_read)
+#define io_close_write(c) LO_CALL(c, close_write)
+
#endif /* _LIBHW_GENERIC_IO_H_ */
diff --git a/libhw_generic/include/libhw/generic/net.h b/libhw_generic/include/libhw/generic/net.h
index 0386e12..d70034e 100644
--- a/libhw_generic/include/libhw/generic/net.h
+++ b/libhw_generic/include/libhw/generic/net.h
@@ -13,7 +13,6 @@
#include <stdint.h> /* for uint{n}_t} */
#include <sys/types.h> /* for ssize_t */
-#include <libobj/obj.h>
#include <libhw/generic/io.h>
/* Errnos *********************************************************************/
@@ -69,13 +68,13 @@ lo_interface net_stream_conn;
/** \
* The net_stream_conn returned from accept() may still be \
* valid after the listener is closed. \
- * \
- * Return 0 on success, -errno on error. \
*/ \
- LO_FUNC(int, close)
+ LO_NEST(io_closer)
LO_INTERFACE(net_stream_listener)
#define net_stream_conn_LO_IFACE \
+ LO_NEST(io_bidi_closer) \
+ \
/** \
* Return bytes-read on success, 0 on EOF, -errno on error; a \
* short read is *not* an error. \
@@ -103,12 +102,7 @@ LO_INTERFACE(net_stream_listener)
* expensive to implement), so if you have concurrent writers then you \
* should arrange for a mutex to protect the connection. \
*/ \
- LO_FUNC(ssize_t, write, void *buf, size_t count) \
- \
- /** \
- * Return 0 on success, -errno on error. \
- */ \
- LO_FUNC(int, close, bool rd, bool wr)
+ LO_FUNC(ssize_t, write, void *buf, size_t count)
LO_INTERFACE(net_stream_conn)
/* Packets (e.g. UDP) *********************************************************/
@@ -117,7 +111,7 @@ LO_INTERFACE(net_stream_conn)
LO_FUNC(ssize_t, sendto, \
void *buf, size_t len, \
struct net_ip4_addr node, uint16_t port) \
- \
+ \
/** \
* @return The full length of the message, which may be more \
* than the given `len` (as if the Linux MSG_TRUNC flag were \
@@ -126,11 +120,11 @@ LO_INTERFACE(net_stream_conn)
LO_FUNC(ssize_t, recvfrom, \
void *buf, size_t len, \
struct net_ip4_addr *ret_node, uint16_t *ret_port) \
- \
- LO_FUNC(void, set_read_deadline, \
+ \
+ LO_FUNC(void, set_recv_deadline, \
uint64_t ns_since_boot) \
- \
- LO_FUNC(int, close)
+ \
+ LO_NEST(io_closer)
LO_INTERFACE(net_packet_conn)
/* Interfaces *****************************************************************/
@@ -145,7 +139,7 @@ struct net_iface_config {
LO_FUNC(struct net_eth_addr , hwaddr ) \
LO_FUNC(void , ifup , struct net_iface_config) \
LO_FUNC(void , ifdown ) \
- \
+ \
LO_FUNC(lo_interface net_stream_listener, tcp_listen, uint16_t local_port) \
LO_FUNC(lo_interface net_stream_conn , tcp_dial , struct net_ip4_addr remote_node, uint16_t remote_port) \
LO_FUNC(lo_interface net_packet_conn , udp_conn , uint16_t local_port)
diff --git a/libhw_generic/include/libhw/generic/spi.h b/libhw_generic/include/libhw/generic/spi.h
index c97232a..a4b0b22 100644
--- a/libhw_generic/include/libhw/generic/spi.h
+++ b/libhw_generic/include/libhw/generic/spi.h
@@ -9,7 +9,6 @@
#include <stddef.h> /* for size_t */
-#include <libobj/obj.h>
#include <libhw/generic/io.h>
enum spi_mode {