Remote page on Linux: SSH, Telnet and rlogin; the protocols in libdmodemctl (dmc_net)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRSV65geuGd6zMt2V6r
This commit is contained in:
Claude committed 2026-11-12 12:21:00 +00:00
1 parent 24c05d658b
commit f1451eeaaa
10 files changed
+1092 -2

No files matched your search

+1
View File
@@ -10,6 +10,7 @@ set(DMODEMCTL_SOURCES
src/vt.c
src/daemon.c
src/voice.c
src/net.c
src/util.c)
if(DMODEMCTL_SHARED)
+38
View File
@@ -25,6 +25,8 @@
* has no terminal widget (Windows and macOS): the AT console and the
* full-screen configurator (AT!CF).
* - dmc_daemon_*: starting dmodemd and reading what it prints.
* - dmc_net: a remote login's protocol (Telnet, rlogin) on a TCP
* connection the app makes, for the Remote page.
* - dmc_voice_*: voice calls on the session (V.253's voice mode: the
* call's audio both ways, 8 kHz), the answering machine that takes
* a message, and WAV files for greetings and messages.
@@ -384,6 +386,42 @@ DMC_API int dmc_daemon_ready(const char *line, char *buf, int max);
/* a line that is an error of dmodemd's ("error: ..."): 1 */
DMC_API int dmc_daemon_error(const char *line);
/*
* Remote logins
*
* Telnet and rlogin on a TCP connection the app makes (SSH is the
* system's ssh in a pty, so it needs nothing here). Like the session it
* does no I/O: hand it what the socket read, write what dmc_net_output()
* gives, show what dmc_net_screen() gives.
*/
typedef struct dmc_net dmc_net;
enum { DMC_NET_TELNET = 1, DMC_NET_RLOGIN, DMC_NET_SSH };
/* the protocol's port (23, 513, 22) and its name ("Telnet") */
DMC_API int dmc_net_default_port(int proto);
DMC_API const char *dmc_net_name(int proto);
/* a login by Telnet or rlogin: user is rlogin's (Telnet's may be ""),
term the terminal type the server is told, the screen's size */
DMC_API dmc_net *dmc_net_new(int proto, const char *user, const char *term,
int cols, int rows);
DMC_API void dmc_net_free(dmc_net *n);
/* the connection made: rlogin's handshake, Telnet's first offers */
DMC_API void dmc_net_connected(dmc_net *n);
/* bytes the socket read */
DMC_API void dmc_net_feed(dmc_net *n, const char *data, int len);
/* an urgent (out-of-band) byte the socket read: rlogin's control byte */
DMC_API void dmc_net_urgent(dmc_net *n, int byte);
/* the bytes for the terminal: how many */
DMC_API int dmc_net_screen(dmc_net *n, char *buf, int max);
/* bytes typed */
DMC_API void dmc_net_type(dmc_net *n, const char *data, int len);
/* the bytes to write to the socket: how many */
DMC_API int dmc_net_output(dmc_net *n, char *buf, int max);
/* the screen's new size, told to the server */
DMC_API void dmc_net_resize(dmc_net *n, int cols, int rows);
#ifdef __cplusplus
}
#endif
+349
View File
@@ -0,0 +1,349 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 D-Modem Authors
*
* net.c - a remote login's protocol over a TCP connection the app makes:
* Telnet (RFC 854) and rlogin (RFC 1282). Like the session, it
* does no I/O: the app hands it what the socket read
* (dmc_net_feed), writes what dmc_net_output gives, shows what
* dmc_net_screen gives and passes on what is typed (dmc_net_type).
*
* Telnet: the server's options answered (BINARY, ECHO and
* SUPPRESS-GO-AHEAD taken; TERMINAL-TYPE and NAWS offered; the rest
* refused), each option answered once per change so that no two ends
* loop; IAC doubled both ways; a CR typed sent as CR NUL outside binary
* mode; the window's size sent (NAWS) as it changes.
*
* rlogin: the handshake (NUL, the user twice, "term/speed"), the
* server's NUL that answers it taken off the stream, and the window's
* size sent ("\377\377ss" and four 16-bit numbers) once the server has
* asked for it with the urgent byte 0x80.
*/
#include <stdlib.h>
#include <string.h>
#include "dmodemctl.h"
#include "util.h"
enum {
IAC = 255, DONT = 254, DO = 253, WONT = 252, WILL = 251, SB = 250,
SE = 240,
O_BINARY = 0, O_ECHO = 1, O_SGA = 3, O_TTYPE = 24, O_NAWS = 31
};
/* where the Telnet reader is */
enum { T_DATA, T_IAC, T_OPT, T_SB, T_SB_IAC };
struct dmc_net {
int proto;
char *user, *term;
int cols, rows;
dmc_buf screen, out;
/* Telnet */
int st, verb, last_cr;
unsigned char remote[256], local[256];
dmc_buf sb;
/* Telnet: the server asked for our size (DO NAWS) */
int naws;
/* rlogin: the server's NUL seen; it asked for the window's size */
int ready, want_size;
};
DMC_API int dmc_net_default_port(int proto)
{
switch (proto) {
case DMC_NET_TELNET:
return 23;
case DMC_NET_RLOGIN:
return 513;
case DMC_NET_SSH:
return 22;
}
return 0;
}
DMC_API const char *dmc_net_name(int proto)
{
switch (proto) {
case DMC_NET_TELNET:
return "Telnet";
case DMC_NET_RLOGIN:
return "rlogin";
case DMC_NET_SSH:
return "SSH";
}
return "";
}
DMC_API dmc_net *dmc_net_new(int proto, const char *user, const char *term,
int cols, int rows)
{
dmc_net *n = calloc(1, sizeof(*n));
if (!n)
return NULL;
n->proto = proto;
n->user = dup_str(user ? user : "");
n->term = dup_str(term && *term ? term : "xterm");
n->cols = cols;
n->rows = rows;
return n;
}
DMC_API void dmc_net_free(dmc_net *n)
{
if (!n)
return;
free(n->user);
free(n->term);
buf_free(&n->screen);
buf_free(&n->out);
buf_free(&n->sb);
free(n);
}
static void put(dmc_net *n, int a, int b, int c)
{
char v[3];
v[0] = (char)a;
v[1] = (char)b;
v[2] = (char)c;
buf_add(&n->out, v, 3);
}
/* a 16-bit number, big-endian; for Telnet an 0xff in it doubled */
static void put16(dmc_net *n, int v, int telnet)
{
int i;
if (v < 0)
v = 0;
if (v > 65535)
v = 65535;
for (i = 0; i < 2; i++) {
int b = i ? v & 0xff : v >> 8;
buf_addc(&n->out, (char)b);
if (telnet && b == IAC)
buf_addc(&n->out, (char)IAC);
}
}
static void send_naws(dmc_net *n)
{
put(n, IAC, SB, O_NAWS);
put16(n, n->cols, 1);
put16(n, n->rows, 1);
buf_addc(&n->out, (char)IAC);
buf_addc(&n->out, (char)SE);
}
static void send_window(dmc_net *n)
{
buf_add(&n->out, "\377\377ss", 4);
put16(n, n->rows, 0);
put16(n, n->cols, 0);
put16(n, n->cols * 8, 0);
put16(n, n->rows * 16, 0);
}
DMC_API void dmc_net_connected(dmc_net *n)
{
if (n->proto == DMC_NET_RLOGIN) {
/* NUL, the local and the remote user, "term/speed" */
buf_addc(&n->out, '\0');
buf_add(&n->out, n->user, (int)strlen(n->user) + 1);
buf_add(&n->out, n->user, (int)strlen(n->user) + 1);
buf_adds(&n->out, n->term);
buf_add(&n->out, "/38400", 7);
} else if (n->proto == DMC_NET_TELNET) {
/* what a Telnet client offers at once: its type and size,
and no go-aheads; marked as asked, so that the server's
yes is not answered again */
put(n, IAC, WILL, O_TTYPE);
put(n, IAC, WILL, O_NAWS);
put(n, IAC, DO, O_SGA);
n->local[O_TTYPE] = n->local[O_NAWS] = 1;
n->remote[O_SGA] = 1;
}
}
/* the server's WILL, WONT, DO or DONT */
static void option(dmc_net *n, int verb, int opt)
{
int ok;
switch (verb) {
case WILL:
ok = opt == O_BINARY || opt == O_ECHO || opt == O_SGA;
if (ok != n->remote[opt] || !ok) {
n->remote[opt] = (unsigned char)ok;
put(n, IAC, ok ? DO : DONT, opt);
}
break;
case WONT:
if (n->remote[opt]) {
n->remote[opt] = 0;
put(n, IAC, DONT, opt);
}
break;
case DO:
ok = opt == O_BINARY || opt == O_SGA || opt == O_TTYPE ||
opt == O_NAWS;
if (ok != n->local[opt] || !ok) {
n->local[opt] = (unsigned char)ok;
put(n, IAC, ok ? WILL : WONT, opt);
}
/* (its size, each time it asks) */
if (ok && opt == O_NAWS) {
n->naws = 1;
send_naws(n);
}
break;
case DONT:
if (opt == O_NAWS)
n->naws = 0;
if (n->local[opt]) {
n->local[opt] = 0;
put(n, IAC, WONT, opt);
}
break;
}
}
/* the server's subnegotiation: TERMINAL-TYPE SEND answered with IS */
static void subneg(dmc_net *n)
{
const unsigned char *p = (const unsigned char *)n->sb.p;
if (n->sb.n >= 2 && p[0] == O_TTYPE && p[1] == 1) {
put(n, IAC, SB, O_TTYPE);
buf_addc(&n->out, '\0');
buf_adds(&n->out, n->term);
buf_addc(&n->out, (char)IAC);
buf_addc(&n->out, (char)SE);
}
}
static void feed_telnet(dmc_net *n, const unsigned char *p, int len)
{
int i;
for (i = 0; i < len; i++) {
int b = p[i];
switch (n->st) {
case T_DATA:
if (b == IAC)
n->st = T_IAC;
/* CR NUL is a CR: the NUL goes */
else if (!(b == 0 && n->last_cr && !n->remote[O_BINARY]))
buf_addc(&n->screen, (char)b);
break;
case T_IAC:
if (b == IAC) {
buf_addc(&n->screen, (char)IAC);
n->st = T_DATA;
} else if (b == WILL || b == WONT || b == DO ||
b == DONT) {
n->verb = b;
n->st = T_OPT;
} else if (b == SB) {
buf_clear(&n->sb);
n->st = T_SB;
} else {
/* NOP, GA, AYT and the rest: nothing to do */
n->st = T_DATA;
}
break;
case T_OPT:
option(n, n->verb, b);
n->st = T_DATA;
break;
case T_SB:
if (b == IAC)
n->st = T_SB_IAC;
else if (n->sb.n < 1024)
buf_addc(&n->sb, (char)b);
break;
case T_SB_IAC:
if (b == SE) {
subneg(n);
n->st = T_DATA;
} else {
if (b == IAC && n->sb.n < 1024)
buf_addc(&n->sb, (char)IAC);
n->st = T_SB;
}
break;
}
n->last_cr = n->st == T_DATA && b == '\r';
}
}
DMC_API void dmc_net_feed(dmc_net *n, const char *data, int len)
{
if (len <= 0)
return;
if (n->proto == DMC_NET_TELNET) {
feed_telnet(n, (const unsigned char *)data, len);
return;
}
if (n->proto == DMC_NET_RLOGIN && !n->ready) {
/* the server's NUL: the handshake taken */
n->ready = 1;
if (data[0] == '\0') {
data++;
len--;
}
}
buf_add(&n->screen, data, len);
}
DMC_API void dmc_net_urgent(dmc_net *n, int byte)
{
if (n->proto != DMC_NET_RLOGIN)
return;
/* TIOCPKT_WINDOW: the server takes window sizes, and wants ours */
if (byte & 0x80) {
n->want_size = 1;
send_window(n);
}
/* TIOCPKT_FLUSHWRITE: what it sent before is to be dropped */
if (byte & 0x02)
buf_clear(&n->screen);
}
DMC_API int dmc_net_screen(dmc_net *n, char *buf, int max)
{
return buf_take(&n->screen, buf, max);
}
DMC_API void dmc_net_type(dmc_net *n, const char *data, int len)
{
int i;
if (n->proto != DMC_NET_TELNET) {
buf_add(&n->out, data, len);
return;
}
for (i = 0; i < len; i++) {
unsigned char b = (unsigned char)data[i];
buf_addc(&n->out, (char)b);
if (b == IAC)
buf_addc(&n->out, (char)IAC);
/* a lone CR is CR NUL, outside binary mode */
if (b == '\r' && !n->local[O_BINARY] &&
(i + 1 >= len || data[i + 1] != '\n'))
buf_addc(&n->out, '\0');
}
}
DMC_API int dmc_net_output(dmc_net *n, char *buf, int max)
{
return buf_take(&n->out, buf, max);
}
DMC_API void dmc_net_resize(dmc_net *n, int cols, int rows)
{
if (cols == n->cols && rows == n->rows)
return;
n->cols = cols;
n->rows = rows;
if (n->proto == DMC_NET_TELNET && n->naws)
send_naws(n);
else if (n->proto == DMC_NET_RLOGIN && n->want_size)
send_window(n);
}
+83
View File
@@ -878,6 +878,88 @@ static void test_formats(void)
CHECK(dmc_audio_decode("ID3\4\0\0\0\0\0\0\0\0", 12, &pcm) == -1);
}
/* what the codec has for the socket, as a string of escaped bytes */
static int out_is(dmc_net *n, const char *want, int wn)
{
char b[512];
int k = dmc_net_output(n, b, sizeof(b));
return k == wn && !memcmp(b, want, (size_t)wn);
}
static int screen_is(dmc_net *n, const char *want, int wn)
{
char b[512];
int k = dmc_net_screen(n, b, sizeof(b));
return k == wn && !memcmp(b, want, (size_t)wn);
}
static void test_net(void)
{
CHECK(dmc_net_default_port(DMC_NET_TELNET) == 23);
CHECK(dmc_net_default_port(DMC_NET_RLOGIN) == 513);
CHECK(dmc_net_default_port(DMC_NET_SSH) == 22);
CHECK_STR(dmc_net_name(DMC_NET_RLOGIN), "rlogin");
/* Telnet: the first offers, then the server's */
dmc_net *n = dmc_net_new(DMC_NET_TELNET, "", "ANSI", 80, 24);
dmc_net_connected(n);
CHECK(out_is(n, "\xff\xfb\x18\xff\xfb\x1f\xff\xfd\x03", 9));
/* DO TTYPE, DO NAWS (our size), WILL ECHO, WILL SGA (asked:
not answered again), DO LINEMODE (refused), text, IAC IAC */
const char *in = "\xff\xfd\x18\xff\xfd\x1f\xff\xfb\x01\xff\xfb\x03"
"\xff\xfd\x22Hi\xff\xff!";
dmc_net_feed(n, in, 20);
CHECK(out_is(n, "\xff\xfa\x1f\x00\x50\x00\x18\xff\xf0"
"\xff\xfd\x01\xff\xfc\x22", 15));
CHECK(screen_is(n, "Hi\xff!", 4));
/* TERMINAL-TYPE SEND, split across two reads */
dmc_net_feed(n, "\xff\xfa\x18", 3);
dmc_net_feed(n, "\x01\xff\xf0", 3);
CHECK(out_is(n, "\xff\xfa\x18\x00" "ANSI\xff\xf0", 10));
/* CR NUL is a CR */
dmc_net_feed(n, "a\r\0b", 4);
CHECK(screen_is(n, "a\rb", 3));
/* typed: a lone CR as CR NUL, IAC doubled */
dmc_net_type(n, "ls\r\xff", 4);
CHECK(out_is(n, "ls\r\0\xff\xff", 6));
dmc_net_type(n, "\r\n", 2);
CHECK(out_is(n, "\r\n", 2));
/* resized: NAWS, an 0xff in it doubled */
dmc_net_resize(n, 255, 50);
CHECK(out_is(n, "\xff\xfa\x1f\x00\xff\xff\x00\x32\xff\xf0", 10));
dmc_net_resize(n, 255, 50);
CHECK(out_is(n, "", 0));
/* WONT ECHO: DONT; and again: nothing */
dmc_net_feed(n, "\xff\xfc\x01\xff\xfc\x01", 6);
CHECK(out_is(n, "\xff\xfe\x01", 3));
dmc_net_free(n);
/* rlogin */
n = dmc_net_new(DMC_NET_RLOGIN, "alice", "xterm", 80, 24);
dmc_net_connected(n);
CHECK(out_is(n, "\0alice\0alice\0xterm/38400\0", 25));
/* the server's NUL, then its text */
dmc_net_feed(n, "\0Password:", 10);
CHECK(screen_is(n, "Password:", 9));
dmc_net_feed(n, "\0", 1);
CHECK(screen_is(n, "\0", 1));
/* no size until it is asked for */
dmc_net_resize(n, 100, 30);
CHECK(out_is(n, "", 0));
dmc_net_urgent(n, 0x80);
CHECK(out_is(n, "\xff\xffss\x00\x1e\x00\x64\x03\x20\x01\xe0", 12));
dmc_net_resize(n, 80, 24);
CHECK(out_is(n, "\xff\xffss\x00\x18\x00\x50\x02\x80\x01\x80", 12));
/* typed as it is */
dmc_net_type(n, "\r\xff", 2);
CHECK(out_is(n, "\r\xff", 2));
/* flush */
dmc_net_feed(n, "old", 3);
dmc_net_urgent(n, 0x02);
CHECK(screen_is(n, "", 0));
dmc_net_free(n);
}
int main(int argc, char **argv)
{
const char *dir = argc > 1 ? argv[1] : ".";
@@ -894,6 +976,7 @@ int main(int argc, char **argv)
test_voicemail();
test_wav();
test_formats();
test_net();
printf("%d checks, %d failed\n", checks, failures);
return failures ? 1 : 0;
}
+8
View File
@@ -7,6 +7,7 @@ add_executable(dmodem-gui
src/settings.c
src/stats.c
src/voice.c
src/remote.c
src/audio.c)
target_link_libraries(dmodem-gui PRIVATE dmodemctl PkgConfig::GTK)
# the sound of voice calls: PulseAudio's client (PipeWire serves it
@@ -27,6 +28,13 @@ if(SNDFILE_FOUND)
else()
message(STATUS "libsndfile not found: greetings are read from WAV, AIFF and .au files only")
endif()
# forkpty(), for the Remote page's ssh: in libutil on the BSDs and older
# glibc, in libc itself since glibc 2.34
include(CheckFunctionExists)
check_function_exists(forkpty HAVE_FORKPTY_IN_LIBC)
if(NOT HAVE_FORKPTY_IN_LIBC)
target_link_libraries(dmodem-gui PRIVATE util)
endif()
target_compile_options(dmodem-gui PRIVATE -Wall -Wextra -Wno-unused-parameter)
include(GNUInstallDirs)
+9
View File
@@ -31,6 +31,10 @@ typedef struct Config {
message's longest (s) */
gboolean voicemail;
int vm_rings, vm_max;
/* the Remote page's last login ([remote]): "SSH", "Telnet" or
"rlogin", the server, its port (0: the protocol's), the user */
char *remote_proto, *remote_host, *remote_user;
int remote_port;
} Config;
typedef struct App App;
@@ -137,6 +141,11 @@ gboolean audio_start(App *a, AudioPull pull, AudioPush push);
void audio_stop(App *a);
gboolean audio_available(void);
/* remote.c: the Remote page, a login by SSH, Telnet or rlogin */
GtkWidget *remote_page_new(App *a);
/* its connection ended (the app is quitting) */
void remote_stop(App *a);
/* stats.c */
GtkWidget *stats_page_new(App *a);
void stats_refresh(App *a);
+17
View File
@@ -67,6 +67,16 @@ void config_load(Config *c)
if (e || c->vm_max < 10)
c->vm_max = 120;
g_clear_error(&e);
c->remote_proto = g_key_file_get_string(k, "remote", "protocol", NULL);
if (!c->remote_proto)
c->remote_proto = g_strdup("SSH");
c->remote_host = g_key_file_get_string(k, "remote", "host", NULL);
if (!c->remote_host)
c->remote_host = g_strdup("");
c->remote_port = g_key_file_get_integer(k, "remote", "port", NULL);
c->remote_user = g_key_file_get_string(k, "remote", "user", NULL);
if (!c->remote_user)
c->remote_user = g_strdup("");
g_key_file_unref(k);
}
@@ -92,6 +102,10 @@ void config_save(const Config *c)
g_key_file_set_boolean(k, "voice", "voicemail", c->voicemail);
g_key_file_set_integer(k, "voice", "rings", c->vm_rings);
g_key_file_set_integer(k, "voice", "max-seconds", c->vm_max);
g_key_file_set_string(k, "remote", "protocol", c->remote_proto);
g_key_file_set_string(k, "remote", "host", c->remote_host);
g_key_file_set_integer(k, "remote", "port", c->remote_port);
g_key_file_set_string(k, "remote", "user", c->remote_user);
char *p = path();
char *dir = g_path_get_dirname(p);
g_mkdir_with_parents(dir, 0700);
@@ -119,6 +133,9 @@ void config_free(Config *c)
g_free(c->device);
g_free(c->dmodemd);
g_free(c->init);
g_free(c->remote_proto);
g_free(c->remote_host);
g_free(c->remote_user);
}
char *config_summary(const Config *c)
+1
View File
@@ -29,6 +29,7 @@ static void shutdown(GApplication *g, gpointer data)
{
App *a = data;
(void)g;
remote_stop(a);
if (modem_running(a))
modem_stop(a);
g_cancellable_cancel(a->cancel);
+581
View File
@@ -0,0 +1,581 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 D-Modem Authors
*
* remote.c - the Remote page: a login to a server by SSH, Telnet or
* rlogin, in a terminal of its own, beside the modem's
*
* Telnet and rlogin are a TCP connection made here and libdmodemctl's
* dmc_net for the protocol. SSH is the system's ssh in a pty of this
* page's own, so that its host keys, agent and config are the user's.
* Either way the terminal only shows bytes and gives what is typed.
*/
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#if defined(__linux__)
#include <pty.h>
#elif defined(__FreeBSD__) || defined(__DragonFly__)
#include <libutil.h>
#else
#include <util.h>
#endif
#include <glib-unix.h>
#include "app.h"
/* the protocols in the drop-down's order */
static const int protos[] = { DMC_NET_SSH, DMC_NET_TELNET, DMC_NET_RLOGIN };
typedef struct Remote {
App *a;
GtkWidget *proto, *host, *port, *user, *go, *status;
VteTerminal *term;
int cols, rows;
/* what runs: its protocol (0: none) */
int running;
/* Telnet, rlogin: the socket and the protocol */
GSocketClient *client;
GCancellable *cancel;
GSocketConnection *conn;
GSocket *sock;
GSource *in_src, *out_src;
dmc_net *net;
GByteArray *pending;
/* SSH: the pty's master, ssh's pid */
int fd;
GPid pid;
guint fd_watch, child_watch, size_tick;
} Remote;
static Remote *R;
static void set_status(Remote *r, const char *text)
{
gtk_label_set_text(GTK_LABEL(r->status), text);
}
static void controls(Remote *r)
{
gboolean idle = !r->running;
gtk_widget_set_sensitive(r->proto, idle);
gtk_widget_set_sensitive(r->host, idle);
gtk_widget_set_sensitive(r->port, idle);
gtk_widget_set_sensitive(r->user, idle);
gtk_button_set_label(GTK_BUTTON(r->go), idle ? "Connect" : "Disconnect");
if (idle) {
gtk_widget_add_css_class(r->go, "suggested-action");
gtk_widget_remove_css_class(r->go, "destructive-action");
} else {
gtk_widget_remove_css_class(r->go, "suggested-action");
gtk_widget_add_css_class(r->go, "destructive-action");
}
}
static void show(Remote *r, const char *data, gssize n)
{
vte_terminal_feed(r->term, data, n);
}
/* the connection over, with why (NULL: the user's doing) */
static void ended(Remote *r, const char *why)
{
if (!r->running)
return;
r->running = 0;
if (r->cancel) {
g_cancellable_cancel(r->cancel);
g_clear_object(&r->cancel);
}
if (r->in_src) {
g_source_destroy(r->in_src);
g_clear_pointer(&r->in_src, g_source_unref);
}
if (r->out_src) {
g_source_destroy(r->out_src);
g_clear_pointer(&r->out_src, g_source_unref);
}
if (r->conn)
g_io_stream_close(G_IO_STREAM(r->conn), NULL, NULL);
g_clear_object(&r->conn);
r->sock = NULL;
g_clear_object(&r->client);
g_clear_pointer(&r->net, dmc_net_free);
if (r->pending)
g_byte_array_set_size(r->pending, 0);
if (r->fd_watch)
g_source_remove(r->fd_watch);
r->fd_watch = 0;
if (r->fd >= 0)
close(r->fd);
r->fd = -1;
if (r->pid > 0)
kill(r->pid, SIGHUP);
if (r->size_tick)
g_source_remove(r->size_tick);
r->size_tick = 0;
char *line = g_strdup_printf("\r\n\x1b[0m[%s]\r\n", why ? why :
"Disconnected");
show(r, line, -1);
g_free(line);
set_status(r, why ? why : "Disconnected");
controls(r);
}
/*
* Telnet and rlogin
*/
static gboolean writable(GSocket *s, GIOCondition c, gpointer data);
/* the protocol's output to the socket, as much as it takes now */
static void flush(Remote *r)
{
char buf[4096];
int n;
while ((n = dmc_net_output(r->net, buf, sizeof(buf))) > 0)
g_byte_array_append(r->pending, (guint8 *)buf, n);
while (r->pending->len > 0) {
GError *e = NULL;
gssize w = g_socket_send(r->sock, (char *)r->pending->data,
r->pending->len, NULL, &e);
if (w < 0) {
if (g_error_matches(e, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
g_error_free(e);
if (!r->out_src) {
r->out_src = g_socket_create_source(
r->sock, G_IO_OUT, NULL);
g_source_set_callback(r->out_src,
(GSourceFunc)(void (*)(void))writable,
r, NULL);
g_source_attach(r->out_src, NULL);
}
return;
}
char *why = g_strdup(e->message);
g_error_free(e);
ended(r, why);
g_free(why);
return;
}
g_byte_array_remove_range(r->pending, 0, (guint)w);
}
}
static gboolean writable(GSocket *s, GIOCondition c, gpointer data)
{
Remote *r = data;
(void)s;
(void)c;
g_clear_pointer(&r->out_src, g_source_unref);
flush(r);
return G_SOURCE_REMOVE;
}
static void pump(Remote *r)
{
char buf[8192];
int n;
while ((n = dmc_net_screen(r->net, buf, sizeof(buf))) > 0)
show(r, buf, n);
flush(r);
}
static gboolean readable(GSocket *s, GIOCondition c, gpointer data)
{
Remote *r = data;
char buf[8192];
/* rlogin's control byte, sent urgent */
if (c & G_IO_PRI) {
char b;
if (recv(g_socket_get_fd(s), &b, 1, MSG_OOB) == 1)
dmc_net_urgent(r->net, (unsigned char)b);
}
if (c & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
GError *e = NULL;
gssize n = g_socket_receive(s, buf, sizeof(buf), NULL, &e);
if (n < 0 && g_error_matches(e, G_IO_ERROR,
G_IO_ERROR_WOULD_BLOCK)) {
g_error_free(e);
} else if (n <= 0) {
char *why = e ? g_strdup(e->message) :
g_strdup("Connection closed by the server");
g_clear_error(&e);
pump(r);
ended(r, why);
g_free(why);
return G_SOURCE_REMOVE;
} else {
dmc_net_feed(r->net, buf, (int)n);
}
}
pump(r);
return G_SOURCE_CONTINUE;
}
static void connected(GObject *src, GAsyncResult *res, gpointer data)
{
Remote *r = data;
GError *e = NULL;
GSocketConnection *c = g_socket_client_connect_to_host_finish(
G_SOCKET_CLIENT(src), res, &e);
if (!c) {
/* (cancelled: ended() has said so) */
if (!g_error_matches(e, G_IO_ERROR, G_IO_ERROR_CANCELLED))
ended(r, e->message);
g_error_free(e);
return;
}
r->conn = c;
r->sock = g_socket_connection_get_socket(c);
g_socket_set_blocking(r->sock, FALSE);
g_socket_set_option(r->sock, IPPROTO_TCP, TCP_NODELAY, 1, NULL);
r->in_src = g_socket_create_source(r->sock, G_IO_IN | G_IO_PRI |
G_IO_HUP | G_IO_ERR, NULL);
g_source_set_callback(r->in_src, (GSourceFunc)(void (*)(void))readable,
r, NULL);
g_source_attach(r->in_src, NULL);
set_status(r, "Connected");
dmc_net_connected(r->net);
pump(r);
}
/*
* SSH: the system's ssh in a pty
*/
static gboolean pty_readable(int fd, GIOCondition c, gpointer data)
{
Remote *r = data;
char buf[8192];
ssize_t n = read(fd, buf, sizeof(buf));
if (n > 0) {
show(r, buf, n);
return G_SOURCE_CONTINUE;
}
if (n < 0 && (errno == EAGAIN || errno == EINTR))
return G_SOURCE_CONTINUE;
/* (EIO: ssh has gone and the pty is shut; its exit says why) */
r->fd_watch = 0;
return G_SOURCE_REMOVE;
}
static void child_exited(GPid pid, int status, gpointer data)
{
Remote *r = data;
g_spawn_close_pid(pid);
r->child_watch = 0;
if (r->pid != pid)
return;
r->pid = 0;
/* what is left in the pty first */
if (r->fd >= 0) {
char buf[8192];
ssize_t n;
while ((n = read(r->fd, buf, sizeof(buf))) > 0)
show(r, buf, n);
}
char *why = WIFEXITED(status) && WEXITSTATUS(status) ?
g_strdup_printf("ssh exited with status %d", WEXITSTATUS(status)) :
g_strdup("Connection closed");
ended(r, why);
g_free(why);
}
static void set_pty_size(Remote *r)
{
struct winsize ws = { 0 };
ws.ws_col = (unsigned short)r->cols;
ws.ws_row = (unsigned short)r->rows;
ioctl(r->fd, TIOCSWINSZ, &ws);
}
static gboolean start_ssh(Remote *r, const char *host, int port,
const char *user)
{
char *ssh = g_find_program_in_path("ssh");
if (!ssh) {
window_toast(r->a, "ssh was not found: install OpenSSH's client");
return FALSE;
}
char ps[16];
g_snprintf(ps, sizeof(ps), "%d", port);
struct winsize ws = { 0 };
ws.ws_col = (unsigned short)r->cols;
ws.ws_row = (unsigned short)r->rows;
int fd;
pid_t pid = forkpty(&fd, NULL, NULL, &ws);
if (pid < 0) {
g_free(ssh);
window_toast(r->a, "Could not make a pty for ssh");
return FALSE;
}
if (pid == 0) {
/* the child: ssh, with VTE's terminal type */
const char *argv[10];
int i = 0;
argv[i++] = "ssh";
argv[i++] = "-p";
argv[i++] = ps;
if (*user) {
argv[i++] = "-l";
argv[i++] = user;
}
argv[i++] = "--";
argv[i++] = host;
argv[i] = NULL;
setenv("TERM", "xterm-256color", 1);
execv(ssh, (char **)argv);
_exit(127);
}
g_free(ssh);
g_unix_set_fd_nonblocking(fd, TRUE, NULL);
r->fd = fd;
r->pid = pid;
r->fd_watch = g_unix_fd_add(fd, G_IO_IN | G_IO_HUP | G_IO_ERR,
pty_readable, r);
r->child_watch = g_child_watch_add(pid, child_exited, r);
return TRUE;
}
/*
* the page
*/
/* the terminal's size, told to the far end as it changes */
static gboolean size_check(gpointer data)
{
Remote *r = data;
int c = (int)vte_terminal_get_column_count(r->term);
int w = (int)vte_terminal_get_row_count(r->term);
if (c == r->cols && w == r->rows)
return G_SOURCE_CONTINUE;
r->cols = c;
r->rows = w;
if (r->net && r->sock) {
dmc_net_resize(r->net, c, w);
flush(r);
} else if (r->fd >= 0) {
set_pty_size(r);
}
return G_SOURCE_CONTINUE;
}
static void committed(VteTerminal *t, char *text, guint size, gpointer data)
{
Remote *r = data;
(void)t;
if (!r->running)
return;
if (r->fd >= 0) {
/* (a pty takes small writes whole) */
ssize_t w = write(r->fd, text, size);
(void)w;
} else if (r->net && r->sock) {
dmc_net_type(r->net, text, (int)size);
flush(r);
}
}
static int chosen_proto(Remote *r)
{
guint i = gtk_drop_down_get_selected(GTK_DROP_DOWN(r->proto));
return i < G_N_ELEMENTS(protos) ? protos[i] : DMC_NET_SSH;
}
/* the default port shown for the protocol */
static void proto_changed(GObject *o, GParamSpec *p, gpointer data)
{
Remote *r = data;
(void)o;
(void)p;
char ps[16];
g_snprintf(ps, sizeof(ps), "%d", dmc_net_default_port(chosen_proto(r)));
gtk_entry_set_placeholder_text(GTK_ENTRY(r->port), ps);
}
static void connect_clicked(GtkWidget *w, gpointer data)
{
Remote *r = data;
App *a = r->a;
(void)w;
if (r->running) {
ended(r, NULL);
return;
}
char *host = g_strstrip(g_strdup(
gtk_editable_get_text(GTK_EDITABLE(r->host))));
const char *user = gtk_editable_get_text(GTK_EDITABLE(r->user));
const char *pt = gtk_editable_get_text(GTK_EDITABLE(r->port));
int proto = chosen_proto(r);
int port = *pt ? atoi(pt) : dmc_net_default_port(proto);
if (!*host) {
window_toast(a, "Enter the server to connect to");
g_free(host);
return;
}
if (port <= 0 || port > 65535) {
window_toast(a, "The port is a number from 1 to 65535");
g_free(host);
return;
}
if (proto == DMC_NET_RLOGIN && !*user) {
window_toast(a, "rlogin needs a user name");
g_free(host);
return;
}
/* kept for next time */
g_free(a->cfg.remote_proto);
a->cfg.remote_proto = g_strdup(dmc_net_name(proto));
g_free(a->cfg.remote_host);
a->cfg.remote_host = g_strdup(host);
a->cfg.remote_port = *pt ? port : 0;
g_free(a->cfg.remote_user);
a->cfg.remote_user = g_strdup(user);
config_save(&a->cfg);
vte_terminal_reset(r->term, TRUE, TRUE);
r->cols = (int)vte_terminal_get_column_count(r->term);
r->rows = (int)vte_terminal_get_row_count(r->term);
char *what = g_strdup_printf("Connecting to %s port %d by %s", host,
port, dmc_net_name(proto));
set_status(r, what);
char *line = g_strdup_printf("[%s]\r\n", what);
show(r, line, -1);
g_free(line);
g_free(what);
if (proto == DMC_NET_SSH) {
if (!start_ssh(r, host, port, user)) {
set_status(r, "Not connected");
g_free(host);
return;
}
set_status(r, "Connected by SSH");
} else {
r->net = dmc_net_new(proto, user, "xterm-256color", r->cols,
r->rows);
r->client = g_socket_client_new();
g_socket_client_set_timeout(r->client, 20);
r->cancel = g_cancellable_new();
g_socket_client_connect_to_host_async(r->client, host,
(guint16)port, r->cancel,
connected, r);
}
r->running = proto;
r->size_tick = g_timeout_add(250, size_check, r);
controls(r);
gtk_widget_grab_focus(GTK_WIDGET(r->term));
g_free(host);
}
static int proto_index(const char *name)
{
guint i;
for (i = 0; i < G_N_ELEMENTS(protos); i++)
if (name && !g_ascii_strcasecmp(name, dmc_net_name(protos[i])))
return (int)i;
return 0;
}
GtkWidget *remote_page_new(App *a)
{
Remote *r = g_new0(Remote, 1);
R = r;
r->a = a;
r->fd = -1;
r->pending = g_byte_array_new();
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
GtkWidget *bar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
gtk_widget_set_margin_start(bar, 6);
gtk_widget_set_margin_end(bar, 6);
gtk_widget_set_margin_top(bar, 6);
gtk_widget_set_margin_bottom(bar, 6);
const char *names[] = { "SSH", "Telnet", "rlogin", NULL };
r->proto = gtk_drop_down_new_from_strings(names);
gtk_widget_set_tooltip_text(r->proto, "How to log in");
gtk_box_append(GTK_BOX(bar), r->proto);
r->host = gtk_entry_new();
gtk_entry_set_placeholder_text(GTK_ENTRY(r->host), "Server");
gtk_widget_set_hexpand(r->host, TRUE);
gtk_box_append(GTK_BOX(bar), r->host);
r->port = gtk_entry_new();
gtk_entry_set_input_purpose(GTK_ENTRY(r->port), GTK_INPUT_PURPOSE_DIGITS);
gtk_editable_set_width_chars(GTK_EDITABLE(r->port), 6);
gtk_editable_set_max_width_chars(GTK_EDITABLE(r->port), 6);
gtk_widget_set_tooltip_text(r->port, "Port (empty: the protocol's)");
gtk_box_append(GTK_BOX(bar), r->port);
r->user = gtk_entry_new();
gtk_entry_set_placeholder_text(GTK_ENTRY(r->user), "User");
gtk_editable_set_width_chars(GTK_EDITABLE(r->user), 12);
gtk_box_append(GTK_BOX(bar), r->user);
r->go = gtk_button_new_with_label("Connect");
g_signal_connect(r->go, "clicked", G_CALLBACK(connect_clicked), r);
g_signal_connect(r->host, "activate", G_CALLBACK(connect_clicked), r);
gtk_box_append(GTK_BOX(bar), r->go);
gtk_box_append(GTK_BOX(box), bar);
/* what was used last */
gtk_drop_down_set_selected(GTK_DROP_DOWN(r->proto),
(guint)proto_index(a->cfg.remote_proto));
gtk_editable_set_text(GTK_EDITABLE(r->host), a->cfg.remote_host);
if (a->cfg.remote_port > 0) {
char ps[16];
g_snprintf(ps, sizeof(ps), "%d", a->cfg.remote_port);
gtk_editable_set_text(GTK_EDITABLE(r->port), ps);
}
gtk_editable_set_text(GTK_EDITABLE(r->user), a->cfg.remote_user);
g_signal_connect(r->proto, "notify::selected", G_CALLBACK(proto_changed), r);
proto_changed(NULL, NULL, r);
r->term = VTE_TERMINAL(vte_terminal_new());
vte_terminal_set_scrollback_lines(r->term, 5000);
vte_terminal_set_backspace_binding(r->term, VTE_ERASE_ASCII_DELETE);
vte_terminal_set_size(r->term, 80, 24);
PangoFontDescription *f = pango_font_description_from_string("Monospace 11");
vte_terminal_set_font(r->term, f);
pango_font_description_free(f);
GdkRGBA fg, bg;
gdk_rgba_parse(&fg, "#e8f0ff");
gdk_rgba_parse(&bg, "#0a0f1c");
vte_terminal_set_colors(r->term, &fg, &bg, NULL, 0);
gtk_widget_set_vexpand(GTK_WIDGET(r->term), TRUE);
gtk_widget_set_hexpand(GTK_WIDGET(r->term), TRUE);
g_signal_connect(r->term, "commit", G_CALLBACK(committed), r);
GtkWidget *scroll = gtk_scrolled_window_new();
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scroll),
GTK_WIDGET(r->term));
gtk_widget_set_vexpand(scroll, TRUE);
gtk_box_append(GTK_BOX(box), scroll);
r->status = gtk_label_new("Not connected");
gtk_label_set_xalign(GTK_LABEL(r->status), 0);
gtk_widget_add_css_class(r->status, "dim-label");
gtk_widget_set_margin_start(r->status, 8);
gtk_widget_set_margin_top(r->status, 4);
gtk_widget_set_margin_bottom(r->status, 4);
gtk_box_append(GTK_BOX(box), r->status);
controls(r);
return box;
}
void remote_stop(App *a)
{
(void)a;
if (R && R->running)
ended(R, NULL);
}
+5 -2
View File
@@ -2,8 +2,8 @@
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (C) 2026 D-Modem Authors
*
* window.c - the main window: the Modem, Terminal, Settings, Call and
* Log pages, the same pages as the Windows and macOS apps
* window.c - the main window: the Modem, Phone, Terminal, Remote,
* Settings, Call and Log pages, the same pages as the Windows and macOS apps
* (docs/design.md)
*/
#include <string.h>
@@ -459,6 +459,9 @@ void window_build(App *a)
adw_view_stack_add_titled_with_icon(a->stack, terminal_page(a),
"terminal", "Terminal",
"utilities-terminal-symbolic");
adw_view_stack_add_titled_with_icon(a->stack, remote_page_new(a),
"remote", "Remote",
"network-server-symbolic");
adw_view_stack_add_titled_with_icon(a->stack, settings_page_new(a),
"settings", "Settings",
"preferences-system-symbolic");