diff --git a/AGENTS.md b/AGENTS.md index 03736cbd..e18c585a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -155,6 +155,13 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt toggle and the text field exist unmanaged (`G_rename_toggle`, `G_rename_text`). The multi-collision and the folder-merge dialog are unchanged (follow-up). +- **dtnetutility** (`programs/dtnetutility`): tabs Info (name and the + interfaces with their addresses, McdeSys), Lookup (getaddrinfo(3) in the + program -- a name to its IPv4 and IPv6 addresses, no program run, no + shell) and Netstat (the open TCP sockets, /proc/net/tcp on Linux; + sysctl net.inet.tcp.pcblist on NetBSD is the follow-up). Ping, traceroute, + whois and a port scan need privileges or a network a test session does not + have; they are named as follow-ups in the program's comment. - **Device events** (`mcde_sys_disk_events`, `lib/McdeSys/events.c`): a non-blocking descriptor of the kernel's uevent socket (Linux, AF_NETLINK NETLINK_KOBJECT_UEVENT group 1) for the programs that must know when a diff --git a/configure.ac b/configure.ac index 399800c3..61126a93 100644 --- a/configure.ac +++ b/configure.ac @@ -899,6 +899,7 @@ programs/dtdiskutility/Makefile programs/dtbackup/Makefile programs/dtphoto/Makefile programs/dtconsole/Makefile +programs/dtnetutility/Makefile programs/dtpaint/Makefile programs/dttoolbox/Makefile programs/mcdesoundd/Makefile diff --git a/programs/Makefile.am b/programs/Makefile.am index 3cbca239..7bce3aa8 100644 --- a/programs/Makefile.am +++ b/programs/Makefile.am @@ -6,7 +6,7 @@ SUBDIRS = backdrops icons dsdm dtmail dtpad dtfile dterror dtwm dtlogin \ palettes dtappintegrate dtprintegrate dtconfig \ dtcreate dtprintinfo fontaliases dtdspmsg dtimsstart \ dtpdm dtsr dtpdmd dtinfo types tttypes util dtopen localized \ - dthelp dtdocbook dtlaunch dtbar dtsystemsettings dtdiskutility dtbackup dtappscan dtkeys dtthemesync dtphoto dtpaint dttoolbox mcdesoundd dtconsole + dthelp dtdocbook dtlaunch dtbar dtsystemsettings dtdiskutility dtbackup dtappscan dtkeys dtthemesync dtphoto dtpaint dttoolbox mcdesoundd dtconsole dtnetutility # The application builder and ttsnoop, its demonstration, need dtcodegen, a # program the build runs: not in a cross build (--disable-appbuilder; the native diff --git a/programs/dtnetutility/Makefile.am b/programs/dtnetutility/Makefile.am new file mode 100644 index 00000000..a1cdf4bb --- /dev/null +++ b/programs/dtnetutility/Makefile.am @@ -0,0 +1,14 @@ +MAINTAINERCLEANFILES = Makefile.in + +bin_PROGRAMS = dtnetutility + +dtnetutility_CPPFLAGS = -I$(top_srcdir)/include \ + -DCDE_INSTALLATION_TOP=\"$(CDE_INSTALLATION_TOP)\" + +dtnetutility_LDADD = $(LIBMCDESYS) $(DTCLIENTLIBS) $(XTOOLLIB) + +dtnetutility_SOURCES = dtnetutility.c + +if SOLARIS +dtnetutility_LDADD += -lintl +endif diff --git a/programs/dtnetutility/dtnetutility.c b/programs/dtnetutility/dtnetutility.c new file mode 100644 index 00000000..38ea8dee --- /dev/null +++ b/programs/dtnetutility/dtnetutility.c @@ -0,0 +1,338 @@ +/* + * dtnetutility.c -- MCDE: the network tools (t0146) + * + * Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE. + * + * The reference's window with tabs. Three of them are here: + * + * Info what this computer is in the network: name and the interfaces + * with their addresses (McdeSys). + * Lookup a name to its addresses -- getaddrinfo(3) in the program, no + * program run, no shell. + * Netstat the open TCP sockets, read from /proc/net/tcp on Linux (NetBSD's + * sysctl net.inet.tcp.pcblist is the follow-up; there the tab says + * so). + * + * Ping, traceroute, whois and a port scan need privileges or a network that + * a test session does not have; they are follow-ups, and the window says + * which tabs are missing rather than pretending. + * + * C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md). + */ + +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static XtAppContext app; +static Widget shell, tabs, pageBox, page[3], infoText, lookField, lookText, + statText, status; +static int shown = 0; + +static void +SetText(Widget w, const char *text) +{ + XmString s = XmStringCreateLocalized((char *) text); + + XtVaSetValues(w, XmNlabelString, s, NULL); + XmStringFree(s); +} + +static Widget +Text(Widget parent, const char *name, const char *text) +{ + XmString s = XmStringCreateLocalized((char *) text); + Widget w = XtVaCreateManagedWidget((char *) name, + xmLabelWidgetClass, parent, XmNlabelString, s, + XmNalignment, XmALIGNMENT_BEGINNING, NULL); + + XmStringFree(s); + return w; +} + +static Widget +Button(Widget parent, const char *label, XtCallbackProc cb, XtPointer data) +{ + XmString s = XmStringCreateLocalized((char *) label); + Widget b = XtVaCreateManagedWidget("button", xmPushButtonWidgetClass, + parent, XmNlabelString, s, NULL); + + XmStringFree(s); + if (cb != NULL) + XtAddCallback(b, XmNactivateCallback, cb, data); + return b; +} + +/* ---- Info --------------------------------------------------------------- */ + +static void +FillInfo(void) +{ + McdeSysInterface ifs[16]; + char host[256]; + char buf[4096] = ""; + int n, i; + + if (mcde_sys_hostname_get(host, sizeof host) == MCDE_SYS_OK) + (void)snprintf(buf + strlen(buf), sizeof buf - strlen(buf), + "Name:\t%s\n\n", host); + n = mcde_sys_interfaces(ifs, 16); + for (i = 0; i < n; i++) + (void)snprintf(buf + strlen(buf), sizeof buf - strlen(buf), + "%s\t%s%s\n", ifs[i].name, + ifs[i].address[0] != '\0' ? ifs[i].address : "no address", + ifs[i].ssid[0] != '\0' ? ifs[i].ssid : ""); + if (n <= 0) + (void)snprintf(buf, sizeof buf, "No interface answered.\n"); + XmTextSetString(infoText, buf); +} + +/* ---- Lookup ------------------------------------------------------------- */ + +static void +Lookup(Widget w, XtPointer data, XtPointer call) +{ + char *name = XmTextFieldGetString(lookField); + struct addrinfo hints, *res = NULL, *p; + char buf[4096] = ""; + int r; + + (void) w; + (void) data; + (void) call; + if (name == NULL || name[0] == '\0') { + XmTextSetString(lookText, "Type a name."); + if (name != NULL) + XtFree(name); + return; + } + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + r = getaddrinfo(name, NULL, &hints, &res); + if (r != 0) { + (void)snprintf(buf, sizeof buf, "%s: %s\n", name, + gai_strerror(r)); + } else { + for (p = res; p != NULL; p = p->ai_next) { + char host[256] = "?", port[32] = "?"; + + if (getnameinfo(p->ai_addr, p->ai_addrlen, host, + sizeof host, port, sizeof port, + NI_NUMERICHOST | NI_NUMERICSERV) == 0) + (void)snprintf(buf + strlen(buf), + sizeof buf - strlen(buf), "%s (%s)\n", + host, p->ai_family == AF_INET ? "IPv4" : + p->ai_family == AF_INET6 ? "IPv6" : + "other"); + } + freeaddrinfo(res); + } + XmTextSetString(lookText, buf); + XtFree(name); +} + +/* ---- Netstat ------------------------------------------------------------ */ + +static void +FillNetstat(void) +{ +#if defined(__linux__) + FILE *f = fopen("/proc/net/tcp", "r"); + char line[512], buf[20000] = ""; + int header = 0; + + if (f == NULL) { + XmTextSetString(statText, "The socket table cannot be read."); + return; + } + while (fgets(line, sizeof line, f) != NULL) { + char local[64], remote[64], state[16]; + int lport = 0, rport = 0, st = 0; + + if (!header) { + header = 1; + continue; /* the column names */ + } + /* "sl local_address rem_address st ..." */ + if (sscanf(line, " %*d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X", + local, &lport, remote, &rport, &st) != 5) + continue; + (void)snprintf(state, sizeof state, "%s", + st == 0x01 ? "ESTABLISHED" : st == 0x0A ? "LISTEN" : + st == 0x06 ? "TIME_WAIT" : st == 0x08 ? "CLOSE_WAIT" : + "other"); + (void)snprintf(buf + strlen(buf), sizeof buf - strlen(buf), + "%s:%d -> %s:%d %s\n", local, lport, remote, rport, + state); + } + fclose(f); + if (buf[0] == '\0') + (void)snprintf(buf, sizeof buf, "No TCP socket is open.\n"); + XmTextSetString(statText, buf); +#else + XmTextSetString(statText, "The socket table is sysctl " + "net.inet.tcp.pcblist on NetBSD; that part is a follow-up."); +#endif +} + +/* ---- the tabs ----------------------------------------------------------- */ + +static Widget +InfoPage(Widget parent) +{ + Widget p = XtVaCreateWidget("infoPage", xmRowColumnWidgetClass, + parent, XmNorientation, XmVERTICAL, XmNspacing, 6, NULL); + Widget sw = XtVaCreateManagedWidget("infoBox", + xmScrolledWindowWidgetClass, p, XmNscrollingPolicy, XmAUTOMATIC, + XmNwidth, 560, XmNheight, 320, NULL); + + infoText = XtVaCreateManagedWidget("infoText", xmTextWidgetClass, sw, + XmNeditable, False, XmNcursorPositionVisible, False, + XmNwordWrap, False, NULL); + return p; +} + +static Widget +LookupPage(Widget parent) +{ + Widget p = XtVaCreateWidget("lookupPage", xmRowColumnWidgetClass, + parent, XmNorientation, XmVERTICAL, XmNspacing, 6, NULL); + Widget row = XtVaCreateManagedWidget("lookRow", + xmRowColumnWidgetClass, p, XmNorientation, XmHORIZONTAL, + XmNspacing, 8, XmNmarginWidth, 0, NULL); + Widget sw; + + (void) Text(row, "lookLabel", "Name:"); + lookField = XtVaCreateManagedWidget("lookName", xmTextFieldWidgetClass, + row, XmNcolumns, 28, NULL); + (void) Button(row, "Look up", Lookup, NULL); + sw = XtVaCreateManagedWidget("lookBox", xmScrolledWindowWidgetClass, + p, XmNscrollingPolicy, XmAUTOMATIC, XmNwidth, 560, + XmNheight, 280, NULL); + lookText = XtVaCreateManagedWidget("lookText", xmTextWidgetClass, sw, + XmNeditable, False, XmNcursorPositionVisible, False, + XmNwordWrap, False, NULL); + return p; +} + +static Widget +NetstatPage(Widget parent) +{ + Widget p = XtVaCreateWidget("netstatPage", xmRowColumnWidgetClass, + parent, XmNorientation, XmVERTICAL, XmNspacing, 6, NULL); + Widget sw = XtVaCreateManagedWidget("statBox", + xmScrolledWindowWidgetClass, p, XmNscrollingPolicy, XmAUTOMATIC, + XmNwidth, 560, XmNheight, 320, NULL); + + statText = XtVaCreateManagedWidget("statText", xmTextWidgetClass, sw, + XmNeditable, False, XmNcursorPositionVisible, False, + XmNwordWrap, False, NULL); + return p; +} + +static void +Show(int which) +{ + int i; + + shown = which; + for (i = 0; i < 3; i++) + if (i == which) + XtManageChild(page[i]); + else + XtUnmanageChild(page[i]); + if (which == 0) + FillInfo(); + else if (which == 2) + FillNetstat(); + SetText(status, which == 0 ? "What this computer is in the network." : + which == 1 ? "A name to its addresses (getaddrinfo)." : + "The open TCP sockets."); +} + +static void +TabChosen(Widget w, XtPointer data, XtPointer call) +{ + int which = (int) (size_t) data; + + (void) w; + if (call != NULL && !((XmToggleButtonCallbackStruct *) call)->set) + return; + if (which != shown) + Show(which); +} + +int +main(int argc, char **argv) +{ + Widget form, bar; + static String fallback[] = { + "*renderTable: mcde", + "*XmText*fontList: fixed", + NULL + }; + static const char *const labels[3] = { "Info", "Lookup", "Netstat" }; + int i; + + XtSetLanguageProc(NULL, NULL, NULL); + shell = XtVaAppInitialize(&app, "Dtnetutility", NULL, 0, &argc, argv, + (String *) fallback, XmNtitle, "Network Utility", NULL); + form = XtVaCreateManagedWidget("form", xmFormWidgetClass, shell, + XmNwidth, 640, XmNheight, 440, NULL); + bar = XtVaCreateManagedWidget("toolbar", xmRowColumnWidgetClass, form, + XmNorientation, XmHORIZONTAL, XmNspacing, 8, XmNmarginHeight, 4, + XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, NULL); + tabs = XtVaCreateManagedWidget("tabs", xmRowColumnWidgetClass, bar, + XmNorientation, XmHORIZONTAL, XmNspacing, 4, + XmNradioBehavior, True, XmNmarginWidth, 0, NULL); + for (i = 0; i < 3; i++) { + XmString s = XmStringCreateLocalized((char *) labels[i]); + Widget t = XtVaCreateManagedWidget("tab", + xmToggleButtonWidgetClass, tabs, XmNlabelString, s, + XmNindicatorOn, False, XmNshadowThickness, 2, + XmNfillOnSelect, True, XmNmarginWidth, 10, NULL); + + XmStringFree(s); + XtAddCallback(t, XmNvalueChangedCallback, TabChosen, + (XtPointer) (size_t) i); + } + pageBox = XtVaCreateManagedWidget("pages", xmRowColumnWidgetClass, + form, XmNorientation, XmVERTICAL, XmNspacing, 4, XmNmarginWidth, + 8, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, bar, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, + status = Text(form, "status", ""), XmNbottomOffset, 6, NULL); + page[0] = InfoPage(pageBox); + page[1] = LookupPage(pageBox); + page[2] = NetstatPage(pageBox); + XtVaSetValues(status, XmNbottomAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, + XmATTACH_FORM, NULL); + XmToggleButtonSetState(XtNameToWidget(tabs, "tab"), True, False); + XtManageChild(form); + Show(0); + XtRealizeWidget(shell); + XtAppMainLoop(app); + return 0; +}