summaryrefslogtreecommitdiff
path: root/pppd/tty.c
diff options
context:
space:
mode:
authorEivind Næss <eivnaes@yahoo.com>2023-01-20 22:12:58 -0800
committerGitHub <noreply@github.com>2023-01-21 17:12:58 +1100
commitba7f7e053daae846a54a1d08d3d133a5f1266ace (patch)
tree3da27cf3ca6d6eb76e3f9a05db4c5676591d5002 /pppd/tty.c
parent76016e1b948b7d9675b4e0750d1f943d96d9523b (diff)
downloadppp-ba7f7e053daae846a54a1d08d3d133a5f1266ace.tar.gz
Header file reorganization and cleaning up the public API for pppd version 2.5.0 (#379)
This commit does several things, being a squash-and-merge of a series of changes; squashed in order not to break bisection. * Clean up pppd.h, moving declarations that should only be accessed by pppd code (not by users of pppd) to a new pppd-private.h. Also, other parts of pppd.h were moved to multilink.h, chap.h, eap.h, eui64.h, and a new options.h. * Provide an API for access to data that is needed by plugins (in no particular order): - ifname - ifunit - remote_name - remote_number - peer_authname - status (now called "code" internally) - phase - doing_multilink - multilink_master - idle_time_limit - link_connect_time - max_connect_time - link_stats - ipparam - hostname - got_sigterm - got_sigusr2 - got_sighup - session_number - maxoctets - maxoctets_dir - debug - persist - devnam - modem - peer_authname - sync_serial * Update the version number to 2.5.0. * Detect availability of stddef.h and stdarg.h. * Rename some headers: - pppcrypt.c/h to crypto_ms.c/h - ppp-crypto.c/h to crypto.c/h - ppp-crypto-priv.h to crypto-priv.h - chap-new.c/h to chap.c/h * Remove chap-md5.h, crypto-priv.h, eap-tls.h, etc. from the list of header files to be installed. * Provide typedefs for the hook functions. * Provide a typedef for the "phase" variable. * Provide a typedef for the link statistics array. * Remove the option_t typedef. * Rename the following functions by adding a "ppp_" prefix (with the intention that these are a "public" API for use by plugins): - option_error - add_options - int_option, - options_from_file - script_setenv - bad_ip_adrs, - netif_get/set_mtu (renamed to ppp_get/set_mtu) - get_time - timeout - untimeout - safe_fork - sys_close - set_session_number - update_link_stats (renamed to ppp_get_link_stats) - add_notifier (renamed to ppp_add_notify) - remove_notifier (renamed to ppp_del_notify) - generic_[dis]establish_ppp (to ppp_generic_[dis]establish) * Rename ppp_devnam to ppp_devname. * Rename ppp_available() to ppp_check_kernel_support(). * Use unsigned char instead of u_char, unsigned short instead of u_short, uint32_t instead of u_int32_t. * Add const to some declarations * Update comments * Change the interface for notifiers to use an enum to identify which notifier is to be modified. * Provide an API for getting the path to a file, with an enum to identify different types of file. * Link plugins with the -DPLUGIN flag [paulus@ozlabs.org - wrote commit message] Signed-off-by: Eivind Næss <eivnaes@yahoo.com> Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
Diffstat (limited to 'pppd/tty.c')
-rw-r--r--pppd/tty.c116
1 files changed, 87 insertions, 29 deletions
diff --git a/pppd/tty.c b/pppd/tty.c
index ad74353..1278781 100644
--- a/pppd/tty.c
+++ b/pppd/tty.c
@@ -95,7 +95,8 @@
#include <netinet/in.h>
#include <arpa/inet.h>
-#include "pppd.h"
+#include "pppd-private.h"
+#include "options.h"
#include "fsm.h"
#include "lcp.h"
@@ -111,7 +112,7 @@ static int setdevname(char *, char **, int);
static int setspeed(char *, char **, int);
static int setxonxoff(char **);
static int setescape(char **);
-static void printescape(option_t *, void (*)(void *, char *,...),void *);
+static void printescape(struct option *, void (*)(void *, char *,...),void *);
static void finish_tty(void);
static int start_charshunt(int, int);
static void stop_charshunt(void *, int);
@@ -136,6 +137,8 @@ int locked; /* lock() has succeeded */
struct stat devstat; /* result of stat() on devnam */
/* option variables */
+char devnam[MAXPATHLEN]; /* Device name */
+char ppp_devname[MAXPATHLEN];/* name of PPP tty (maybe ttypx) */
int crtscts = 0; /* Use hardware flow control */
int stop_bits = 1; /* Number of serial port stop bits */
bool modem = 1; /* Use modem control lines */
@@ -164,7 +167,7 @@ extern int privopen; /* don't lock, open device as root */
u_int32_t xmit_accm[8]; /* extended transmit ACCM */
/* option descriptors */
-option_t tty_options[] = {
+static struct option tty_options[] = {
/* device name must be first, or change connect_tty() below! */
{ "device name", o_wild, (void *) &setdevname,
"Serial port device name",
@@ -261,6 +264,61 @@ struct channel tty_channel = {
&tty_close_fds
};
+bool
+ppp_sync_serial()
+{
+ return sync_serial;
+}
+
+bool
+ppp_get_modem()
+{
+ return modem;
+}
+
+void
+ppp_set_modem(bool on)
+{
+ modem = on;
+}
+
+bool
+ppp_using_pty()
+{
+ return using_pty;
+}
+
+int
+ppp_set_pppdevnam(const char *name)
+{
+ if (name) {
+ return strlcpy(ppp_devname, name, sizeof(ppp_devname));
+ }
+ return -1;
+}
+
+const char *
+ppp_pppdevnam()
+{
+ return ppp_devname;
+}
+
+const char *
+ppp_devnam()
+{
+ return devnam;
+}
+
+int
+ppp_set_devnam(const char *name)
+{
+ if (name) {
+ return strlcpy(devnam, name, sizeof(devnam));
+ }
+ return -1;
+}
+
+
/*
* setspeed - Set the serial port baud rate.
* If doit is 0, the call is to check whether this option is
@@ -309,12 +367,12 @@ setdevname(char *cp, char **argv, int doit)
if (stat(cp, &statbuf) < 0) {
if (!doit)
return errno != ENOENT;
- option_error("Couldn't stat %s: %m", cp);
+ ppp_option_error("Couldn't stat %s: %m", cp);
return 0;
}
if (!S_ISCHR(statbuf.st_mode)) {
if (doit)
- option_error("%s is not a character device", cp);
+ ppp_option_error("%s is not a character device", cp);
return 0;
}
@@ -351,13 +409,13 @@ setescape(char **argv)
while (*p) {
n = strtol(p, &endp, 16);
if (p == endp) {
- option_error("escape parameter contains invalid hex number '%s'",
+ ppp_option_error("escape parameter contains invalid hex number '%s'",
p);
return 0;
}
p = endp;
if (n < 0 || n == 0x5E || n > 0xFF) {
- option_error("can't escape character 0x%x", n);
+ ppp_option_error("can't escape character 0x%x", n);
ret = 0;
} else
xmit_accm[n >> 5] |= 1 << (n & 0x1F);
@@ -369,7 +427,7 @@ setescape(char **argv)
}
static void
-printescape(option_t *opt, void (*printer)(void *, char *, ...), void *arg)
+printescape(struct option *opt, void (*printer)(void *, char *, ...), void *arg)
{
int n;
int first = 1;
@@ -394,7 +452,7 @@ printescape(option_t *opt, void (*printer)(void *, char *, ...), void *arg)
*/
void tty_init(void)
{
- add_notifier(&pidchange, maybe_relock, 0);
+ ppp_add_notify(NF_PID_CHANGE, maybe_relock, 0);
the_channel = &tty_channel;
xmit_accm[3] = 0x60000000;
}
@@ -411,7 +469,7 @@ void tty_process_extra_options(void)
if (default_device) {
char *p;
if (!isatty(0) || (p = ttyname(0)) == NULL) {
- option_error("no device specified and stdin is not a tty");
+ ppp_option_error("no device specified and stdin is not a tty");
exit(EXIT_OPTION_ERROR);
}
strlcpy(devnam, p, MAXPATHLEN);
@@ -441,12 +499,12 @@ tty_check_options(void)
int fdflags;
if (demand && notty) {
- option_error("demand-dialling is incompatible with notty");
+ ppp_option_error("demand-dialling is incompatible with notty");
exit(EXIT_OPTION_ERROR);
}
if (demand && connect_script == 0 && ptycommand == NULL
&& pty_socket == NULL) {
- option_error("connect script is required for demand-dialling\n");
+ ppp_option_error("connect script is required for demand-dialling\n");
exit(EXIT_OPTION_ERROR);
}
/* default holdoff to 0 if no connect script has been given */
@@ -455,16 +513,16 @@ tty_check_options(void)
if (using_pty) {
if (!default_device) {
- option_error("%s option precludes specifying device name",
+ ppp_option_error("%s option precludes specifying device name",
pty_socket? "socket": notty? "notty": "pty");
exit(EXIT_OPTION_ERROR);
}
if (ptycommand != NULL && notty) {
- option_error("pty option is incompatible with notty option");
+ ppp_option_error("pty option is incompatible with notty option");
exit(EXIT_OPTION_ERROR);
}
if (pty_socket != NULL && (ptycommand != NULL || notty)) {
- option_error("socket option is incompatible with pty and notty");
+ ppp_option_error("socket option is incompatible with pty and notty");
exit(EXIT_OPTION_ERROR);
}
default_device = notty;
@@ -518,14 +576,14 @@ int connect_tty(void)
* Get a pty master/slave pair if the pty, notty, socket,
* or record options were specified.
*/
- strlcpy(ppp_devnam, devnam, MAXPATHLEN);
+ strlcpy(ppp_devname, devnam, MAXPATHLEN);
pty_master = -1;
pty_slave = -1;
real_ttyfd = -1;
if (using_pty || record_file != NULL) {
- if (!get_pty(&pty_master, &pty_slave, ppp_devnam, uid)) {
+ if (!get_pty(&pty_master, &pty_slave, ppp_devname, uid)) {
error("Couldn't allocate pseudo-tty");
- status = EXIT_FATAL_ERROR;
+ ppp_set_status(EXIT_FATAL_ERROR);
return -1;
}
set_up_tty(pty_slave, 1);
@@ -534,7 +592,7 @@ int connect_tty(void)
/*
* Lock the device if we've been asked to.
*/
- status = EXIT_LOCK_FAILED;
+ ppp_set_status(EXIT_LOCK_FAILED);
if (lockflag && !privopen) {
if (lock(devnam) < 0)
goto errret;
@@ -560,7 +618,7 @@ int connect_tty(void)
if (prio < OPRIO_ROOT && seteuid(uid) == -1) {
error("Unable to drop privileges before opening %s: %m\n",
devnam);
- status = EXIT_OPEN_FAILED;
+ ppp_set_status(EXIT_OPEN_FAILED);
goto errret;
}
real_ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0);
@@ -572,7 +630,7 @@ int connect_tty(void)
errno = err;
if (err != EINTR) {
error("Failed to open %s: %m", devnam);
- status = EXIT_OPEN_FAILED;
+ ppp_set_status(EXIT_OPEN_FAILED);
}
if (!persist || err != EINTR)
goto errret;
@@ -615,7 +673,7 @@ int connect_tty(void)
* If the pty, socket, notty and/or record option was specified,
* start up the character shunt now.
*/
- status = EXIT_PTYCMD_FAILED;
+ ppp_set_status(EXIT_PTYCMD_FAILED);
if (ptycommand != NULL) {
if (record_file != NULL) {
int ipipe[2], opipe[2], ok;
@@ -681,7 +739,7 @@ int connect_tty(void)
if (initializer && initializer[0]) {
if (device_script(initializer, ttyfd, ttyfd, 0) < 0) {
error("Initializer script failed");
- status = EXIT_INIT_FAILED;
+ ppp_set_status(EXIT_INIT_FAILED);
goto errretf;
}
if (got_sigterm) {
@@ -694,7 +752,7 @@ int connect_tty(void)
if (connector && connector[0]) {
if (device_script(connector, ttyfd, ttyfd, 0) < 0) {
error("Connect script failed");
- status = EXIT_CONNECT_FAILED;
+ ppp_set_status(EXIT_CONNECT_FAILED);
goto errretf;
}
if (got_sigterm) {
@@ -721,7 +779,7 @@ int connect_tty(void)
break;
if (errno != EINTR) {
error("Failed to reopen %s: %m", devnam);
- status = EXIT_OPEN_FAILED;
+ ppp_set_status(EXIT_OPEN_FAILED);
}
if (!persist || errno != EINTR || hungup || got_sigterm)
goto errret;
@@ -730,7 +788,7 @@ int connect_tty(void)
}
slprintf(numbuf, sizeof(numbuf), "%d", baud_rate);
- script_setenv("SPEED", numbuf, 0);
+ ppp_script_setenv("SPEED", numbuf, 0);
/* run welcome script, if any */
if (welcomer && welcomer[0]) {
@@ -911,7 +969,7 @@ start_charshunt(int ifd, int ofd)
{
int cpid, ret;
- cpid = safe_fork(ifd, ofd, (log_to_fd >= 0? log_to_fd: 2));
+ cpid = ppp_safe_fork(ifd, ofd, (log_to_fd >= 0? log_to_fd: 2));
if (cpid == -1) {
error("Can't fork process for character shunt: %m");
return 0;
@@ -1054,7 +1112,7 @@ charshunt(int ifd, int ofd, char *record_file)
pty_readable = stdin_readable = 1;
ilevel = olevel = 0;
- get_time(&levelt);
+ ppp_get_time(&levelt);
if (max_data_rate) {
max_level = max_data_rate / 10;
if (max_level < 100)
@@ -1103,7 +1161,7 @@ charshunt(int ifd, int ofd, char *record_file)
int nbt;
struct timeval now;
- get_time(&now);
+ ppp_get_time(&now);
dt = (now.tv_sec - levelt.tv_sec
+ (now.tv_usec - levelt.tv_usec) / 1e6);
nbt = (int)(dt * max_data_rate);