Răsfoiți Sursa

Rename ping to pong; ping resolves names; lookup becomes host

The responder ("answer ARP/ICMP for our IP for 30 s") is now `pong`, so
the active command can take the `ping` name back:

- `pong` -- the old responder, unchanged apart from the name and its
  `pong:` messages.
- `ping <host|a.b.c.d>` -- the old `netping`: the same four ICMP echoes
  with RTT (and its verbose ARP trail), but a non-dotted argument is
  resolved through dns.c first, so `ping example.com` works.  The
  argument buffer grows 16 -> 64 for hostnames.
- `host <name>` -- the old `lookup`, unchanged: one DNS A query, all
  answers printed.

Help entries, tab completion, the dispatch chain, process names and the
usage/messages all follow the new names; the stale "netping" mentions in
the netdev.h/net.c comments are updated too.

Verified on QEMU (slirp + ne2k_pci): `host example.com` returns both A
records, `ping 10.0.2.2` answers 4/4, and `ping example.com` resolves
and answers 4/4 via the gateway.

AGENTS.md: the new command names.
Johannes Zellner 1 lună în urmă
părinte
comite
ebbde0b1e3
7 a modificat fișierele cu 91 adăugiri și 81 ștergeri
  1. 8 7
      AGENTS.md
  2. 2 2
      dns.c
  3. 2 2
      dns.h
  4. 1 1
      net.c
  5. 48 40
      netcmd.c
  6. 1 1
      netdev.h
  7. 29 28
      terminal.c

+ 8 - 7
AGENTS.md

@@ -123,9 +123,9 @@ make clean
   OFFER → REQUEST → ACK; runs at boot when a NIC is present, 3 s budget, keeps
   the static address when no server answers; `dhcp` re-runs it; learns the DNS
   server from option 6). `netcmd.c` — the
-  `netinfo`/`netrecv`/`netsend`/`netip`/`dhcp`/`ping`/`netping`/`netregs`
+  `netinfo`/`netrecv`/`netsend`/`netip`/`dhcp`/`pong`/`ping`/`netregs`
   commands.  `dns.c/h` — a minimal resolver (one recursive A query over UDP,
-  name-compression aware; `netdns` shows/sets the server, `lookup <name>`
+  name-compression aware; `netdns` shows/sets the server, `host <name>`
   resolves).  `tcp.c/h` — a minimal TCP *client* (connect/send/recv/close,
   in-order receive, one segment of retransmit; the caller owns the NIC).
   `curl.c` — the `curl` command: `http://host[:port]/path` GET with DNS, ARP,
@@ -180,13 +180,14 @@ make clean
 `ps`, `kill <pid>`, `help`, `clear`, `serial test|recv`, `kexec`, `dbg`.
 
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
-DHCP client), `ping` (responds to ARP/ICMP for our IP for 30 s),
-`netping <a.b.c.d>` (active ARP + ICMP echo with RTT, via the gateway when the
-target is off-subnet), `netloop` (PHY loopback TX/RX self-test), `netreset`
+DHCP client), `pong` (responds to ARP/ICMP for our IP for 30 s),
+`ping <host|a.b.c.d>` (active ARP + ICMP echo with RTT; resolves DNS names,
+goes via the gateway when the target is off-subnet), `netloop` (PHY loopback
+TX/RX self-test), `netreset`
 (full NIC re-init), `netlog <text>` (one UDP datagram to port 19290),
 `netconsole on|off|host <a.b.c.d>` (the debug-output mirror, see below)
 `netregs` (register dump, decoded ISR + counters), `netdns [a.b.c.d]` (show or
-set the resolver) and `lookup <name>` (one DNS A query).  `curl <url>` fetches
+set the resolver) and `host <name>` (one DNS A query).  `curl <url>` fetches
 `http://host[:port]/path` with a minimal HTTP/1.0 GET and dumps the status
 line, headers and body (no TLS, no redirects; numeric hosts skip DNS).  On
 boot, if a NIC is present, a DHCP client runs with a 3 s budget (requesting the
@@ -265,7 +266,7 @@ ARP/ICMP for its IP, and auto-detects the `"KXNU"` header datagram (UDP port
 19288).  The host tool (`netkexec_send.c`) then does a stop-and-wait transfer:
 the board acks every datagram with the next expected offset, verifies the
 32-bit checksum and jumps.  ~130 KB takes about a second on a wired LAN.
-While a net command owns the receiver (`netrecv`/`ping`/`netping`/`netloop`)
+While a net command owns the receiver (`netrecv`/`ping`/`netloop`)
 or a kexec is in progress, the auto-detect pauses.
 
 The board's address comes from DHCP when a server is present (the DHCP client

+ 2 - 2
dns.c

@@ -333,12 +333,12 @@ void program_netdns_main(const char *arg)
 	io_putchar('\n');
 }
 
-void program_lookup_main(const char *arg)
+void program_host_main(const char *arg)
 {
 	unsigned char ip[4];
 
 	if (arg[0] == '\0') {
-		io_putstr("lookup: usage lookup <name>\n");
+		io_putstr("host: usage host <name>\n");
 		return;
 	}
 

+ 2 - 2
dns.h

@@ -7,8 +7,8 @@
  * Returns 0 on success.  The caller must own the NIC (netdev_acquire). */
 int dns_resolve(const char *name, unsigned char ip[4]);
 
-/* `netdns [a.b.c.d]` (show/set the resolver) and `lookup <name>`. */
+/* `netdns [a.b.c.d]` (show/set the resolver) and `host <name>`. */
 void program_netdns_main(const char *arg);
-void program_lookup_main(const char *arg);
+void program_host_main(const char *arg);
 
 #endif

+ 1 - 1
net.c

@@ -190,7 +190,7 @@ void net_next_hop(const unsigned char dst[4], unsigned char hop[4])
 		hop[i] = dst[i];
 
 	/* Without a mask/gateway (manual netip) keep treating the target as
-	 * directly reachable, like netping does. */
+	 * directly reachable, like ping does. */
 	if (!(my_mask[0] || my_mask[1] || my_mask[2] || my_mask[3]) ||
 	    !(my_gw[0] || my_gw[1] || my_gw[2] || my_gw[3]))
 		return;

+ 48 - 40
netcmd.c

@@ -1,11 +1,12 @@
-/* Network commands: netinfo, netrecv, netsend, netip, ping (responder) and
- * netping (active ARP + ICMP echo), over the netdev NIC layer. */
+/* Network commands: netinfo, netrecv, netsend, netip, pong (responder) and
+ * ping (active ARP + ICMP echo), over the netdev NIC layer. */
 
 #include "io.h"
 #include "timer.h"
 #include "netdev.h"
 #include "net.h"
 #include "dhcp.h"
+#include "dns.h"
 #include "netlog.h"
 
 /* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
@@ -260,19 +261,19 @@ void program_netip_main(const char *arg)
 	io_putchar('\n');
 }
 
-/* ---- ping: answer ARP/ICMP for our IP (responder mode) ---- */
+/* ---- pong: answer ARP/ICMP for our IP (responder mode) ---- */
 
-void program_ping_main(void)
+void program_pong_main(void)
 {
 	const struct netdev *nd = netdev_active();
 	unsigned int deadline, count = 0;
 
 	if (!nd) {
-		io_putstr("ping: no NIC found\n");
+		io_putstr("pong: no NIC found\n");
 		return;
 	}
 
-	io_putstr("ping: answering ARP/ICMP for 30s (ip=");
+	io_putstr("pong: answering ARP/ICMP for 30s (ip=");
 	net_print_ip();
 	io_putstr(")...\n");
 	netdev_acquire();
@@ -297,7 +298,7 @@ void program_ping_main(void)
 		}
 	}
 
-	io_putstr("ping: done (");
+	io_putstr("pong: done (");
 	put_dec(count);
 	io_putstr(" replies)\n");
 	netdev_release();
@@ -482,13 +483,13 @@ void program_netloop_main(void)
 	netdev_release();
 }
 
-/* ---- netping: active ARP + ICMP echo ---- */
+/* ---- ping: active ARP + ICMP echo ---- */
 
-#define NETPING_COUNT  4
-#define NETPING_ID     0x4E42   /* "NB" */
-#define NETPING_WAIT_MS 1000
+#define PING_COUNT  4
+#define PING_ID     0x4E42   /* "NB" */
+#define PING_WAIT_MS 1000
 
-static unsigned int netping_sent_ms[NETPING_COUNT];
+static unsigned int ping_sent_ms[PING_COUNT];
 
 /* ARP request "who has target? tell my_ip" -> 42-byte frame. */
 static unsigned int build_arp_request(const unsigned char *my_mac,
@@ -579,8 +580,8 @@ static unsigned int build_icmp_echo(const unsigned char *src_mac,
 	ic[0] = ICMP_ECHO_REQUEST;
 	ic[1] = 0;
 	ic[2] = 0; ic[3] = 0;
-	ic[4] = (unsigned char)(NETPING_ID >> 8);
-	ic[5] = (unsigned char)(NETPING_ID & 0xFF);
+	ic[4] = (unsigned char)(PING_ID >> 8);
+	ic[5] = (unsigned char)(PING_ID & 0xFF);
 	ic[6] = (unsigned char)(seq >> 8);
 	ic[7] = (unsigned char)(seq & 0xFF);
 	pl[0] = 'n'; pl[1] = 'e'; pl[2] = 'b'; pl[3] = 'o';
@@ -623,10 +624,10 @@ static int parse_icmp_reply(const unsigned char *f, unsigned int len,
 	if (ic[0] != ICMP_ECHO_REPLY || ic[1] != 0)
 		return 0;
 	id = ((unsigned short)ic[4] << 8) | ic[5];
-	if (id != NETPING_ID)
+	if (id != PING_ID)
 		return 0;
 	*seq = ((unsigned short)ic[6] << 8) | ic[7];
-	if (*seq < 1 || *seq > NETPING_COUNT)
+	if (*seq < 1 || *seq > PING_COUNT)
 		return 0;
 	return 1;
 }
@@ -635,7 +636,7 @@ static int parse_icmp_reply(const unsigned char *f, unsigned int len,
  * Returns 0 = resolved, -1 = no reply, -2 = transmit failed.
  * Prints the frames seen while waiting (capped) so a silent peer, a filtered
  * reply and a reply we refuse to parse can be told apart on screen. */
-static int netping_arp(const struct netdev *nd, const unsigned char *my_mac,
+static int ping_arp(const struct netdev *nd, const unsigned char *my_mac,
 		       const unsigned char *my_ip, const unsigned char *target_ip,
 		       unsigned char *target_mac)
 {
@@ -643,12 +644,12 @@ static int netping_arp(const struct netdev *nd, const unsigned char *my_mac,
 	unsigned int tries, seen = 0;
 
 	for (tries = 0; tries < 3; tries++) {
-		unsigned int deadline = get_ticks_ms() + NETPING_WAIT_MS;
+		unsigned int deadline = get_ticks_ms() + PING_WAIT_MS;
 		unsigned int flen = build_arp_request(my_mac, my_ip, target_ip, frame);
 
 		if (nd->tx(frame, flen) != 0)
 			return -2;
-		io_putstr("netping: ARP request sent (try ");
+		io_putstr("ping: ARP request sent (try ");
 		put_dec(tries + 1);
 		io_putstr("/3)\n");
 		while (get_ticks_ms() < deadline) {
@@ -657,7 +658,7 @@ static int netping_arp(const struct netdev *nd, const unsigned char *my_mac,
 			if (len < 0)
 				continue;
 			if (seen < 8) {
-				io_putstr("netping: rx ");
+				io_putstr("ping: rx ");
 				net_decode_frame(net_rx_buf, (unsigned int)len);
 				io_putchar('\n');
 				seen++;
@@ -667,28 +668,35 @@ static int netping_arp(const struct netdev *nd, const unsigned char *my_mac,
 				return 0;
 		}
 	}
-	io_putstr("netping: saw ");
+	io_putstr("ping: saw ");
 	put_dec(seen);
 	io_putstr(" frame(s) while waiting\n");
 	return -1;
 }
 
-static void netping_run(const char *arg)
+static void ping_run(const char *arg)
 {
 	const struct netdev *nd;
 	unsigned char target[4], my_ip[4], my_mac[6], target_mac[6];
 	unsigned int i, replies = 0;
 
-	if (arg[0] == '\0' || net_parse_ip(arg, target) != 0) {
-		io_putstr("netping: usage netping <a.b.c.d>\n");
+	if (arg[0] == '\0') {
+		io_putstr("ping: usage ping <host|a.b.c.d>\n");
 		return;
 	}
 
 	nd = netdev_active();
 	if (!nd) {
-		io_putstr("netping: no NIC found\n");
+		io_putstr("ping: no NIC found\n");
 		return;
 	}
+
+	/* Dotted IP, or a name to resolve first. */
+	if (net_parse_ip(arg, target) != 0) {
+		if (dns_resolve(arg, target) != 0)
+			return;
+	}
+
 	nd->get_mac(my_mac);
 	net_get_ip(my_ip);
 
@@ -712,7 +720,7 @@ static void netping_run(const char *arg)
 		if (off) {
 			for (j = 0; j < 4; j++)
 				hop[j] = gw[j];
-			io_putstr("netping: ");
+			io_putstr("ping: ");
 			put_ip(target);
 			io_putstr(" via ");
 			put_ip(hop);
@@ -720,40 +728,40 @@ static void netping_run(const char *arg)
 		}
 
 		{
-			int r = netping_arp(nd, my_mac, my_ip, hop, target_mac);
+			int r = ping_arp(nd, my_mac, my_ip, hop, target_mac);
 
 			if (r == -2) {
-				io_putstr("netping: transmit failed (see netregs)\n");
+				io_putstr("ping: transmit failed (see netregs)\n");
 				return;
 			}
 			if (r != 0) {
-				io_putstr("netping: no ARP reply from ");
+				io_putstr("ping: no ARP reply from ");
 				put_ip(hop);
 				io_putchar('\n');
 				return;
 			}
 		}
 	}
-	io_putstr("netping: ");
+	io_putstr("ping: ");
 	put_ip(target);
 	io_putstr(" is-at ");
 	put_mac(target_mac);
 	io_putchar('\n');
 
-	for (i = 0; i < NETPING_COUNT; i++) {
+	for (i = 0; i < PING_COUNT; i++) {
 		unsigned char frame[64];
 		unsigned int flen, deadline;
 		int got = 0;
 
 		flen = build_icmp_echo(my_mac, target_mac, my_ip, target,
 				       (unsigned short)(i + 1), frame);
-		netping_sent_ms[i] = get_ticks_ms();
+		ping_sent_ms[i] = get_ticks_ms();
 		if (nd->tx(frame, flen) != 0) {
-			io_putstr("netping: transmit failed\n");
+			io_putstr("ping: transmit failed\n");
 			return;
 		}
 
-		deadline = get_ticks_ms() + NETPING_WAIT_MS;
+		deadline = get_ticks_ms() + PING_WAIT_MS;
 		while (get_ticks_ms() < deadline && !got) {
 			unsigned short seq;
 			int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
@@ -764,7 +772,7 @@ static void netping_run(const char *arg)
 			if (!parse_icmp_reply(net_rx_buf, (unsigned int)len,
 					      my_ip, target, &seq))
 				continue;
-			rtt = get_ticks_ms() - netping_sent_ms[seq - 1];
+			rtt = get_ticks_ms() - ping_sent_ms[seq - 1];
 			io_putstr("reply: seq=");
 			put_dec(seq);
 			io_putstr(" rtt=");
@@ -777,24 +785,24 @@ static void netping_run(const char *arg)
 			replies++;
 		}
 		if (!got) {
-			io_putstr("netping: timeout, seq=");
+			io_putstr("ping: timeout, seq=");
 			put_dec(i + 1);
 			io_putchar('\n');
 		}
 	}
 
-	io_putstr("netping: ");
+	io_putstr("ping: ");
 	put_dec(replies);
 	io_putstr("/");
-	put_dec(NETPING_COUNT);
+	put_dec(PING_COUNT);
 	io_putstr(" replies from ");
 	put_ip(target);
 	io_putchar('\n');
 }
 
-void program_netping_main(const char *arg)
+void program_ping_main(const char *arg)
 {
 	netdev_acquire();
-	netping_run(arg);
+	ping_run(arg);
 	netdev_release();
 }

+ 1 - 1
netdev.h

@@ -24,7 +24,7 @@ struct netdev {
 const struct netdev *netdev_active(void);
 
 /* Busy gate for commands that poll the NIC for a while (netrecv, ping,
- * netping, netloop, netkexec): the background kexec watcher must not steal
+ * netloop, netkexec): the background kexec watcher must not steal
  * frames while one of them owns the receiver. */
 void netdev_acquire(void);
 void netdev_release(void);

+ 29 - 28
terminal.c

@@ -40,17 +40,17 @@ extern void program_netinfo_main(void);
 extern void program_netrecv_main(void);
 extern void program_netsend_main(void);
 extern void program_netip_main(const char *arg);
-extern void program_netping_main(const char *arg);
 extern void program_netlog_main(const char *arg);
 extern void program_netconsole_main(const char *arg);
 extern void program_dhcp_main(void);
 extern void program_netregs_main(void);
 extern void program_netloop_main(void);
 extern void program_netreset_main(void);
-extern void program_ping_main(void);
+extern void program_pong_main(void);
+extern void program_ping_main(const char *arg);
 extern void program_curl_main(const char *arg);
 extern void program_netdns_main(const char *arg);
-extern void program_lookup_main(const char *arg);
+extern void program_host_main(const char *arg);
 
 /* Print the prompt (with the current directory) and set line_start. */
 static void term_print_prompt(struct term *t, int first)
@@ -564,20 +564,20 @@ static int is_cmd_netip(const char *line, unsigned int line_len, char *arg_buf,
 	return (i == line_len);
 }
 
-static int is_cmd_netping(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
+/* One optional single-token argument (hostname or dotted IP). */
+static int is_cmd_ping(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
 {
 	unsigned int i = 0, j = 0;
 	while (i < line_len && line[i] == ' ')
 		i++;
-	if (i + 7 > line_len)
+	if (i + 4 > line_len)
 		return 0;
-	if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
-	    line[i+3] != 'p' || line[i+4] != 'i' || line[i+5] != 'n' ||
-	    line[i+6] != 'g')
+	if (line[i] != 'p' || line[i+1] != 'i' || line[i+2] != 'n' ||
+	    line[i+3] != 'g')
 		return 0;
-	i += 7;
+	i += 4;
 	if (i < line_len && line[i] != ' ')
-		return 0;  /* "netping" must be a whole word */
+		return 0;  /* "ping" must be a whole word */
 	while (i < line_len && line[i] == ' ')
 		i++;
 	if (i >= line_len) {
@@ -650,17 +650,18 @@ static int is_cmd_netdns(const char *line, unsigned int line_len, char *arg_buf,
 	return (i == line_len);
 }
 
-static int is_cmd_lookup(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
+/* One optional single-token argument (DNS name). */
+static int is_cmd_host(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
 {
 	unsigned int i = 0, j = 0;
 	while (i < line_len && line[i] == ' ')
 		i++;
-	if (i + 6 > line_len)
+	if (i + 4 > line_len)
 		return 0;
-	if (line[i] != 'l' || line[i+1] != 'o' || line[i+2] != 'o' ||
-	    line[i+3] != 'k' || line[i+4] != 'u' || line[i+5] != 'p')
+	if (line[i] != 'h' || line[i+1] != 'o' || line[i+2] != 's' ||
+	    line[i+3] != 't')
 		return 0;
-	i += 6;
+	i += 4;
 	if (i < line_len && line[i] != ' ')
 		return 0;
 	while (i < line_len && line[i] == ' ')
@@ -678,14 +679,14 @@ static int is_cmd_lookup(const char *line, unsigned int line_len, char *arg_buf,
 	return (i == line_len);
 }
 
-static int is_cmd_ping(const char *line, unsigned int line_len)
+static int is_cmd_pong(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
 	while (i < line_len && line[i] == ' ')
 		i++;
 	if (i + 4 > line_len)
 		return 0;
-	if (line[i] != 'p' || line[i+1] != 'i' || line[i+2] != 'n' || line[i+3] != 'g')
+	if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'n' || line[i+3] != 'g')
 		return 0;
 	i += 4;
 	while (i < line_len && line[i] == ' ')
@@ -912,11 +913,11 @@ static const char *const help_items[] = {
 	"display", "cpuinfo", "lsusb", "mouseinfo", "storage", "meminfo",
 	"format <device>", "lspci [id]", "peek <addr> [len]", "poke <addr> <val>",
 	"netinfo",
-	"netrecv", "netsend", "netip [a.b.c.d]", "netping <a.b.c.d>",
+	"netrecv", "netsend", "netip [a.b.c.d]", "ping <host|a.b.c.d>",
 	"netlog <text>", "netconsole [on|off|host <ip>]", "dhcp", "netregs",
-	"netloop", "netreset", "ping", "ps", "kill <pid>", "kexec", "dbg",
+	"netloop", "netreset", "pong", "ps", "kill <pid>", "kexec", "dbg",
 	"serial test|recv", "clear", "curl <url>", "netdns [a.b.c.d]",
-	"lookup <name>",
+	"host <name>",
 };
 
 static void help_puts(const char *s)
@@ -1728,12 +1729,12 @@ void term_run(struct term *t)
 					char ls_path[LS_PATH_MAX];
 					char format_dev[FORMAT_DEV_MAX];
 					char netip_arg[16];
-					char netping_arg[16];
+					char ping_arg[64];
 					char netlog_arg[NET_UDP_ARG_MAX];
 					char netconsole_arg[NET_UDP_ARG_MAX];
 					char curl_arg[128];
 					char netdns_arg[32];
-					char lookup_arg[64];
+					char host_arg[64];
 					char lspci_arg[16];
 					char peek_arg[32];
 					char resolved[VFS_PATH_MAX];
@@ -1780,8 +1781,8 @@ void term_run(struct term *t)
 						term_spawn(t, "netsend", program_netsend_main);
 					} else if (is_cmd_netip(t->line, t->line_len, netip_arg, sizeof(netip_arg))) {
 						term_spawn_arg(t, "netip", program_netip_main, netip_arg);
-					} else if (is_cmd_netping(t->line, t->line_len, netping_arg, sizeof(netping_arg))) {
-						term_spawn_arg(t, "netping", program_netping_main, netping_arg);
+					} else if (is_cmd_ping(t->line, t->line_len, ping_arg, sizeof(ping_arg))) {
+						term_spawn_arg(t, "ping", program_ping_main, ping_arg);
 					} else if (is_cmd_netlog(t->line, t->line_len, netlog_arg, sizeof(netlog_arg))) {
 						term_spawn_arg(t, "netlog", program_netlog_main, netlog_arg);
 					} else if (is_cmd_netconsole(t->line, t->line_len, netconsole_arg, sizeof(netconsole_arg))) {
@@ -1798,10 +1799,10 @@ void term_run(struct term *t)
 						term_spawn_arg(t, "netdns", program_netdns_main, netdns_arg);
 					} else if (is_cmd_curl(t->line, t->line_len, curl_arg, sizeof(curl_arg))) {
 						term_spawn_arg(t, "curl", program_curl_main, curl_arg);
-					} else if (is_cmd_lookup(t->line, t->line_len, lookup_arg, sizeof(lookup_arg))) {
-						term_spawn_arg(t, "lookup", program_lookup_main, lookup_arg);
-					} else if (is_cmd_ping(t->line, t->line_len)) {
-						term_spawn(t, "ping", program_ping_main);
+					} else if (is_cmd_host(t->line, t->line_len, host_arg, sizeof(host_arg))) {
+						term_spawn_arg(t, "host", program_host_main, host_arg);
+					} else if (is_cmd_pong(t->line, t->line_len)) {
+						term_spawn(t, "pong", program_pong_main);
 					} else if (is_cmd_storage(t->line, t->line_len)) {
 						term_spawn(t, "storage", program_storage_main);
 					} else if (is_cmd_meminfo(t->line, t->line_len)) {