modemu2k 0.2.3
Library that provides telnet capability to a comm program
Loading...
Searching...
No Matches
main.c

The full source of the bundled modemu2k executable.

/*
* main.c
* Modemu2k adds Telnet capability to a comm program.
* It can redirect Telnet I/O to a pty so that a comm program
* can handle the pty as a tty with a real modem.
*
* Copyright 2018-2020 Andy Alt <arch_stanton5995@protonmail.com>
*
* modemu2k is a fork of modemu
* Originally developed by Toru Egashira
* Copyright (c) 1995, 1996
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <modemu2k.h>
#include "cmdarg.h"
#include "config.h"
static void
stderr_log_fn(const char *msg, void *userdata)
{
(void) userdata;
fputs("modemu2k: ", stderr);
fputs(msg, stderr);
}
int
main(int argc, char *const argv[])
{
#ifdef SOCKS
SOCKSinit(argv[0]);
#endif
if (argc == 1)
{
showUsage(argv);
return 0;
}
m2k_t *ctx = m2k_new();
if (ctx == NULL)
return EXIT_FAILURE;
struct st_cmdarg cmdarg;
cmdargParse(argc, argv, &cmdarg);
/* -v / --verbose describes how to run, not what to do. If no mode
flag (-c/-d/-l/-s) and no -e were given, there's no operation
to narrate — treat like a no-arg invocation and print usage. */
if (cmdarg.ttymode == CA_STDINOUT && cmdarg.atcmd == NULL)
{
showUsage(argv);
m2k_free(ctx);
return 0;
}
if (cmdarg.verbose && cmdarg.ttymode == CA_COMM_PROGRAM && isatty(2))
{
fputs("modemu2k: -v with -c writes verbose output to the same terminal\n"
"as the comm program and would corrupt its display. Redirect\n"
"stderr to a file before retrying, e.g.:\n"
" modemu2k -v -c \"minicom ...\" 2>/tmp/m2k.log\n"
"and watch it live in another terminal with:\n"
" tail -f /tmp/m2k.log\n",
stderr);
m2k_free(ctx);
return 2;
}
if (cmdarg.verbose)
{
m2k_set_log_fn(ctx, stderr_log_fn, NULL);
fputs("modemu2k: verbose logging enabled (MISC|TELOPT)\n", stderr);
}
puts(PACKAGE_STRING " " VERSION);
puts("Enter 'at%q' (or Ctrl-C twice) to quit\n");
switch (cmdarg.ttymode)
{
case CA_SHOWDEV:
{
const char *slave;
if (m2k_setup_pty(ctx, &slave) != M2K_OK)
{
m2k_free(ctx);
return EXIT_FAILURE;
}
puts(slave);
m2k_free(ctx);
return 0;
}
case CA_COMM_PROGRAM:
if (m2k_setup_comm_program(ctx, cmdarg.comm_program) != M2K_OK)
{
m2k_free(ctx);
return EXIT_FAILURE;
}
break;
case CA_STDINOUT:
break;
case CA_DEVGIVEN:
if (m2k_setup_dev(ctx, cmdarg.dev) != M2K_OK)
{
m2k_free(ctx);
return EXIT_FAILURE;
}
break;
case CA_LISTEN:
if (m2k_setup_listen(ctx, cmdarg.listen_port) != M2K_OK || m2k_listen_accept(ctx) != M2K_OK)
{
m2k_free(ctx);
return EXIT_FAILURE;
}
break;
}
if (cmdarg.verbose)
if (cmdarg.atcmd != NULL && m2k_atcmd(ctx, cmdarg.atcmd) != M2K_OK)
fprintf(stderr, "Error in initialization commands.\r\n");
m2k_run(ctx);
m2k_free(ctx);
return 0;
}
Public API for the modemu2k PTY-based modem emulator library.
M2K_API void m2k_set_force_verbose(m2k_t *ctx, int on)
Bypass the ATV verbose mask in verboseOut() / verbosePerror().
M2K_API m2k_err_t m2k_listen_accept(m2k_t *ctx)
Accept a single incoming connection on the listening socket opened by m2k_setup_listen() and adopt it...
M2K_API m2k_err_t m2k_run(m2k_t *ctx)
Run the modem command/online loop until the PTY closes.
M2K_API void m2k_free(m2k_t *ctx)
Release all resources held by ctx.
M2K_API m2k_err_t m2k_setup_comm_program(m2k_t *ctx, const char *cmd)
Allocate a PTY and fork/exec a comm program on the slave.
M2K_API m2k_err_t m2k_setup_stdin(m2k_t *ctx)
Use stdin/stdout as the TTY (standalone mode).
M2K_API void m2k_set_log_fn(m2k_t *ctx, m2k_log_fn fn, void *userdata)
Install a log callback.
M2K_API m2k_err_t m2k_setup_dev(m2k_t *ctx, const char *dev)
Open an existing PTY device as the TTY.
struct m2k_s m2k_t
Opaque modem emulator context. Create with m2k_new(), destroy with m2k_free().
Definition modemu2k.h:95
M2K_API m2k_err_t m2k_atcmd(m2k_t *ctx, const char *cmd)
Feed a Hayes AT command string to the modem.
M2K_API m2k_t * m2k_new(void)
Allocate and initialise a new modem context.
@ M2K_OK
Definition modemu2k.h:123
M2K_API m2k_err_t m2k_setup_pty(m2k_t *ctx, const char **slave_out)
Allocate a PTY master and return the slave device path.
M2K_API m2k_err_t m2k_setup_listen(m2k_t *ctx, const char *port)
Bind a TCP listening socket on port.