Konrad, 2026-11-13: "panel fehlt, top leiste is lila", the network settings "zu rudimentaer, keine ip config, kein dns", the global menu only while a program that has one is in front. The bar (and the launcher, the drop-down terminal, the login items) came from Xsession.d, before dtsession had loaded the session's resources and the colour server: dtbar took *background from ~/.Xdefaults (#6e1e7d, the user's base16 theme; dtbar PID 1270788 started before dtsession 1270795). Restarting dtbar in the running session gave the palette's colours. Now dtsession runs sys.sessionetc -- the first of CDE_CONFIGURATION_TOP/config and CDE_INSTALLATION_TOP/config, as sys.session is found -- before the user's sessionetc (SmRestore.c, StartEtc); it sources config/sessionetc.d, where 0080.dtlaunch, 0085.dtdropdown, 0090.dtbar and 0095.loginitems moved. The bar takes colour set 5, the programs' background, not set 6 of a menu bar (Dtbar*secondaryColorSetId: 5; measured #c6b2a8 with Default.dp). dtwm publishes the focused client as _NET_ACTIVE_WINDOW (EWMH, WmEwmh.c SetNetActiveWindow from SetKeyboardFocus and where the focus is dropped); dtbar shows a program's menus only while the active window is the menus' owner, a dialog of it (WM_TRANSIENT_FOR) or a window of the same program (WM_CLIENT_LEADER). Measured on Xvfb with dtwm, dtbar, dtpad and xterm, clicking: dtpad -> its menus, xterm -> M only, dtpad -> its menus again. Network (dtsystemsettings/network.c, docs/reference/netconfig.txt): Show: Network Status (a light and a sentence per card, Configure...) or one card with the tabs TCP/IP (Using DHCP, with manual address, Manually, Off; address, mask, router, DHCP client ID, Renew DHCP Lease; IPv6), DNS (servers, search domains, what resolv.conf has now), Wi-Fi and Ethernet; the lock and Apply Now as Accounts. McdeSys mcde_sys_net_config, _configure, _renew (replacing mcde_sys_net_dhcp, which was declared and implemented nowhere): NetBSD one block per interface in dhcpcd.conf, as ifconfig.if(5) recommends, sysinst's static address in ifconfig.IF read and moved there, applied with dhcpcd -n IF (cross-compiled with the tree's x86_64--netbsd-gcc, not run); Linux the NetworkManager profile (nmcli). mcde_sys_interfaces took the last IPv4 address (the secondary one), now the first. Measured on this host, read only: the pane shows the three cards, 172.20.10.2/28 by DHCP, router 172.20.10.1, DNS 1.1.1.1, MAC, MTU 1500; test_sysinfo (new checks: settings read, 300.1.2.3 refused) exit 0. Nothing was applied on the development host. From the review of 2026-11-12's commits: libDtTerm links libMcdeSound (977a7347c made the bell call McdeSoundEvent; dtcodegen did not link); the PLIST lacked dtphoto, dtpaint, dttoolbox, mcdesoundd, libMcdeSound, their data and the sound themes (51 entries, from the installed tree); mcdesoundd was started by nothing (sessionetc.d/0075.mcdesoundd) and took the selection from a running server; dttoolbox kept only the first line of the Selected menu and rebuilt it every 500 ms, and NewMenu returned an uninitialised pointer past 64 menus. Built and installed with make mcde-host MCDE_DIRS=install, exit 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
312 lines
8.6 KiB
C
312 lines
8.6 KiB
C
/*
|
|
* net.c -- MCDE: the network interfaces (McdeSys.h, "Network")
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* getifaddrs(3) -- not POSIX, but the same on NetBSD (where it comes from)
|
|
* and on Linux -- gives the interfaces and their addresses; whether one is
|
|
* Wi-Fi, and which network it has joined, the backend tells
|
|
* (mcde_sys_if_kind). The loopback is left out.
|
|
*/
|
|
|
|
#define _NETBSD_SOURCE /* getifaddrs(3) on NetBSD */
|
|
#ifndef _DEFAULT_SOURCE
|
|
#define _DEFAULT_SOURCE /* and on Linux */
|
|
#endif
|
|
|
|
#include <ctype.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h>
|
|
#include <ifaddrs.h>
|
|
#include <net/if.h>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "private.h"
|
|
|
|
static McdeSysInterface *
|
|
Find(McdeSysInterface *ifs, size_t n, const char *name)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; i++)
|
|
if (strcmp(ifs[i].name, name) == 0)
|
|
return &ifs[i];
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
mcde_sys_interfaces(McdeSysInterface *ifs, size_t max)
|
|
{
|
|
struct ifaddrs *all, *a;
|
|
size_t n = 0;
|
|
|
|
if (getifaddrs(&all) != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "No network interfaces.");
|
|
for (a = all; a != NULL; a = a->ifa_next) {
|
|
McdeSysInterface *it;
|
|
int fam;
|
|
|
|
if (a->ifa_flags & IFF_LOOPBACK)
|
|
continue;
|
|
if ((it = Find(ifs, n, a->ifa_name)) == NULL) {
|
|
if (n >= max)
|
|
continue;
|
|
it = &ifs[n++];
|
|
memset(it, 0, sizeof *it);
|
|
snprintf(it->name, sizeof it->name, "%s", a->ifa_name);
|
|
it->kind = mcde_sys_if_kind(it->name, it->ssid,
|
|
sizeof it->ssid);
|
|
}
|
|
if (a->ifa_addr == NULL || !(a->ifa_flags & IFF_UP))
|
|
continue;
|
|
fam = a->ifa_addr->sa_family;
|
|
/* an IPv4 address first; an IPv6 one only if there is none,
|
|
* and not the link-local one every interface has */
|
|
/* the first IPv4 address is the primary one (ip(8) lists it
|
|
* first; later ones are "secondary") */
|
|
if ((fam == AF_INET && (it->address[0] == '\0' ||
|
|
strchr(it->address, ':') != NULL)) ||
|
|
(fam == AF_INET6 && it->address[0] == '\0')) {
|
|
char host[NI_MAXHOST];
|
|
|
|
if (fam == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(
|
|
&((struct sockaddr_in6 *) a->ifa_addr)->sin6_addr))
|
|
continue;
|
|
if (getnameinfo(a->ifa_addr, fam == AF_INET ?
|
|
sizeof(struct sockaddr_in) :
|
|
sizeof(struct sockaddr_in6), host, sizeof host,
|
|
NULL, 0, NI_NUMERICHOST) == 0) {
|
|
snprintf(it->address, sizeof it->address, "%s",
|
|
host);
|
|
it->up = 1;
|
|
}
|
|
}
|
|
}
|
|
freeifaddrs(all);
|
|
return (int) n;
|
|
}
|
|
|
|
/* ---- the settings of one interface (System Settings > Network) ---------
|
|
* What every system has the same way is here: the addresses the interface
|
|
* has now (getifaddrs), its MTU (SIOCGIFMTU, ioctl(2) on a socket, as
|
|
* ifconfig(8) asks it on both), the DNS servers in use (resolv.conf(5),
|
|
* written by resolvconf on NetBSD and by NetworkManager or systemd on
|
|
* Linux), and the checks of what the user typed. Where the configuration
|
|
* lives -- dhcpcd.conf(5) on NetBSD, NetworkManager on Linux -- the
|
|
* backend knows (docs/reference/netconfig.txt in the Makronix tree). */
|
|
|
|
int
|
|
mcde_sys_net_ifname_ok(const char *name)
|
|
{
|
|
const char *p;
|
|
|
|
if (name == NULL || name[0] == '\0' || name[0] == '-' ||
|
|
strlen(name) >= IFNAMSIZ)
|
|
return 0;
|
|
for (p = name; *p != '\0'; p++)
|
|
if (!isalnum((unsigned char) *p) && *p != '_' && *p != '-' &&
|
|
*p != '.')
|
|
return 0;
|
|
return 1;
|
|
}
|
|
|
|
/* the prefix length of a dotted netmask, -1 when it is none */
|
|
int
|
|
mcde_sys_net_prefix(const char *netmask)
|
|
{
|
|
struct in_addr a;
|
|
unsigned long m;
|
|
int n = 0;
|
|
|
|
if (inet_pton(AF_INET, netmask, &a) != 1)
|
|
return -1;
|
|
m = ntohl(a.s_addr);
|
|
while (m & 0x80000000UL) {
|
|
n++;
|
|
m = (m << 1) & 0xffffffffUL;
|
|
}
|
|
return m == 0 ? n : -1;
|
|
}
|
|
|
|
void
|
|
mcde_sys_net_mask(int prefix, char *buf, size_t len)
|
|
{
|
|
unsigned long m = prefix <= 0 ? 0 :
|
|
(0xffffffffUL << (32 - (prefix > 32 ? 32 : prefix))) & 0xffffffffUL;
|
|
|
|
snprintf(buf, len, "%lu.%lu.%lu.%lu", (m >> 24) & 255, (m >> 16) & 255,
|
|
(m >> 8) & 255, m & 255);
|
|
}
|
|
|
|
static int
|
|
Ipv4(const char *s)
|
|
{
|
|
struct in_addr a;
|
|
|
|
return s != NULL && inet_pton(AF_INET, s, &a) == 1;
|
|
}
|
|
|
|
static int
|
|
Ipv6(const char *s)
|
|
{
|
|
struct in6_addr a;
|
|
|
|
return s != NULL && inet_pton(AF_INET6, s, &a) == 1;
|
|
}
|
|
|
|
/* a domain name: letters, digits, '-' and '.', not beginning with '-' */
|
|
static int
|
|
Domain(const char *s)
|
|
{
|
|
const char *p;
|
|
|
|
if (s[0] == '\0' || s[0] == '-' || s[0] == '.' || strlen(s) > 95)
|
|
return 0;
|
|
for (p = s; *p != '\0'; p++)
|
|
if (!isalnum((unsigned char) *p) && *p != '-' && *p != '.')
|
|
return 0;
|
|
return 1;
|
|
}
|
|
|
|
static void
|
|
Current(const char *ifname, McdeSysNetConfig *c)
|
|
{
|
|
struct ifaddrs *all, *a;
|
|
struct ifreq ifr;
|
|
char line[256];
|
|
FILE *fp;
|
|
int s;
|
|
|
|
if (getifaddrs(&all) == 0) {
|
|
for (a = all; a != NULL; a = a->ifa_next) {
|
|
if (a->ifa_addr == NULL ||
|
|
strcmp(a->ifa_name, ifname) != 0)
|
|
continue;
|
|
if (a->ifa_addr->sa_family == AF_INET &&
|
|
c->cur_address[0] == '\0') {
|
|
inet_ntop(AF_INET, &((struct sockaddr_in *)
|
|
a->ifa_addr)->sin_addr, c->cur_address,
|
|
sizeof c->cur_address);
|
|
if (a->ifa_netmask != NULL)
|
|
inet_ntop(AF_INET, &((struct sockaddr_in *)
|
|
a->ifa_netmask)->sin_addr,
|
|
c->cur_netmask,
|
|
sizeof c->cur_netmask);
|
|
} else if (a->ifa_addr->sa_family == AF_INET6 &&
|
|
c->cur_address6[0] == '\0') {
|
|
struct in6_addr *in6 = &((struct sockaddr_in6 *)
|
|
a->ifa_addr)->sin6_addr;
|
|
int i, bits = 0;
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(in6))
|
|
continue;
|
|
inet_ntop(AF_INET6, in6, c->cur_address6,
|
|
sizeof c->cur_address6);
|
|
if (a->ifa_netmask != NULL) {
|
|
unsigned char *m = ((struct sockaddr_in6 *)
|
|
a->ifa_netmask)->sin6_addr.s6_addr;
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
unsigned char b = m[i];
|
|
|
|
while (b & 0x80) {
|
|
bits++;
|
|
b = (unsigned char) (b << 1);
|
|
}
|
|
}
|
|
}
|
|
c->cur_prefix6 = bits;
|
|
}
|
|
}
|
|
freeifaddrs(all);
|
|
}
|
|
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
|
|
memset(&ifr, 0, sizeof ifr);
|
|
snprintf(ifr.ifr_name, sizeof ifr.ifr_name, "%s", ifname);
|
|
if (ioctl(s, SIOCGIFMTU, &ifr) == 0)
|
|
c->mtu = ifr.ifr_mtu;
|
|
close(s);
|
|
}
|
|
if ((fp = fopen("/etc/resolv.conf", "r")) != NULL) {
|
|
while (fgets(line, sizeof line, fp) != NULL &&
|
|
c->ncur_dns < MCDE_NET_DNS_MAX) {
|
|
char *last = NULL, *k = strtok_r(line, " \t\r\n", &last),
|
|
*v = strtok_r(NULL, " \t\r\n", &last);
|
|
|
|
if (k != NULL && v != NULL &&
|
|
strcmp(k, "nameserver") == 0)
|
|
snprintf(c->cur_dns[c->ncur_dns++],
|
|
sizeof c->cur_dns[0], "%s", v);
|
|
}
|
|
fclose(fp);
|
|
}
|
|
}
|
|
|
|
int
|
|
mcde_sys_net_config(const char *ifname, McdeSysNetConfig *c)
|
|
{
|
|
if (!mcde_sys_net_ifname_ok(ifname))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "There is no network "
|
|
"card of that name.");
|
|
memset(c, 0, sizeof *c);
|
|
c->prefix6 = 64;
|
|
Current(ifname, c);
|
|
return mcde_sys_net_config_backend(ifname, c);
|
|
}
|
|
|
|
int
|
|
mcde_sys_net_configure(const char *ifname, const McdeSysNetConfig *c)
|
|
{
|
|
const char *p;
|
|
int i;
|
|
|
|
if (!mcde_sys_net_ifname_ok(ifname))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "There is no network "
|
|
"card of that name.");
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
|
"can change the network settings: click the lock first.");
|
|
if (c->ipv4 == MCDE_IPV4_MANUAL || c->ipv4 == MCDE_IPV4_DHCP_MANUAL) {
|
|
if (!Ipv4(c->address))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no "
|
|
"IPv4 address.", c->address);
|
|
if (mcde_sys_net_prefix(c->netmask) < 1)
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no "
|
|
"subnet mask (255.255.255.0, for example).",
|
|
c->netmask);
|
|
}
|
|
if (c->ipv4 == MCDE_IPV4_MANUAL && c->router[0] != '\0' &&
|
|
!Ipv4(c->router))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no router "
|
|
"address.", c->router);
|
|
if (c->ipv6 == MCDE_IPV6_MANUAL && (!Ipv6(c->address6) ||
|
|
c->prefix6 < 1 || c->prefix6 > 128))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s/%d\" is no IPv6 "
|
|
"address with its prefix length.", c->address6,
|
|
c->prefix6);
|
|
for (i = 0; i < c->ndns; i++)
|
|
if (!Ipv4(c->dns[i]) && !Ipv6(c->dns[i]))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no "
|
|
"DNS server address.", c->dns[i]);
|
|
for (i = 0; i < c->nsearch; i++)
|
|
if (!Domain(c->search[i]))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no "
|
|
"domain name.", c->search[i]);
|
|
for (p = c->clientid; *p != '\0'; p++)
|
|
if (!isgraph((unsigned char) *p) || *p == '"' || *p == '\\' ||
|
|
*p == '\'')
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "The DHCP client "
|
|
"ID may hold letters, digits and signs, no blanks "
|
|
"or quotes.");
|
|
return mcde_sys_net_configure_backend(ifname, c);
|
|
}
|