339 lines
10 KiB
C
339 lines
10 KiB
C
/*
|
|
* 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 <netdb.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
|
|
#include <X11/Intrinsic.h>
|
|
#include <Xm/Form.h>
|
|
#include <Xm/Label.h>
|
|
#include <Xm/List.h>
|
|
#include <Xm/PushB.h>
|
|
#include <Xm/RowColumn.h>
|
|
#include <Xm/ScrolledW.h>
|
|
#include <Xm/Text.h>
|
|
#include <Xm/TextF.h>
|
|
#include <Xm/ToggleB.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
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;
|
|
}
|