порты и ppp chat (ppp connect dialup)
Ключевые слова: ppp, connect, dialup, (найти похожие документы)
_ RU.UNIX.BSD (2:5077/15.22) _____________________________________ RU.UNIX.BSD _
From : Alex Zakirov 2:5029/5 03 Sep 99 09:26:56
Subj : порты и ppp chat
________________________________________________________________________________
Serg Ivanov <Serg.Ivanov@p113.f3.n5073.z2.fidonet.org> wrote:
SI> Hiya, Igor!
SI> At 01 Sep 99 10:37:43, Igor Sysoev wrote to All:
IS>> Святым духом, что ли ?
IS>> Чат прекрасно работает без оператора.
SI> Он прекрасно это делает из-под ppp. Как ему объяснить где модем ?
SI> Собственно надо в 8 утра говорить S0=8, в 17:00 S0=1 ;)
резать ниже
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <syslog.h>
#include <signal.h>
#include <err.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
int crtscts = 0;
int clocal = 1;
int baud_rate = 57600;
int restore_term;
int cverbose = 0;
int nolock = 0;
struct termios inittermios;
struct winsize wsinfo;
char *devnam = "/dev/ttyd1";
char *connector = "chat -vs -f chat.txt";
char *chatfile = NULL;
char *myname;
char *lock_file;
void set_up_tty(int,int);
int lock(char *);
void unlock(int);
void usage();
int
main(int argc, char **argv)
{
int ttyfd,c,fdflags;
int pid,status;
if ( (myname = strrchr(argv[0],'/')) )
myname++;
else
myname = argv[0];
/* Parsing options */
if ( argc < 2 )
usage();
while ( (c = getopt(argc,argv,"hl:s:c:mrv")) != -1 ) {
switch (c) {
case 'l':
devnam = optarg;
break;
case 's':
baud_rate = atoi(optarg);
break;
case 'c':
chatfile = optarg;
break;
case 'v':
cverbose = 1;
break;
case 'm':
clocal = 1;
break;
case 'r':
crtscts = 1;
break;
case 'n':
nolock = 1;
break;
case 'h':
default:
usage();
}
}
argc -= optind; argv += optind;
if ( argc < 1 && !chatfile )
usage();
connector = argv[0];
signal(SIGQUIT,unlock);
signal(SIGTERM,unlock);
signal(SIGINT,unlock);
if ( !nolock )
lock(devnam);
/* open tty */
if ( (ttyfd = open(devnam,O_NONBLOCK|O_RDWR,0)) < 0 ) {
errx(-1, "Failed to open %s: %s",devnam,strerror(errno));
}
/* reset nonblocking mode */
if ( (fdflags = fcntl(ttyfd, F_GETFL)) == -1 ||
fcntl(ttyfd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
errx(-1,"can't reset O_NONBLOCK on %s: %s",devnam,strerror(errno));
/* setup tty taken from sys-bsd.c (pppd) */
set_up_tty(ttyfd,clocal);
/* run program */
pid = fork();
if ( pid < 0 )
errx(-1,"can't fork");
if ( pid == 0 ) {
/* redirect stdin/stdout to opened TTY */
dup2(ttyfd,0);
close(ttyfd);
dup2(0,1);
if ( chatfile ) {
if ( cverbose )
execl("/usr/bin/chat","chat","-vsf",chatfile,(char*)0);
else
execl("/usr/bin/chat","chat","-Sf",chatfile,(char*)0);
} else {
execl("/bin/sh","sh","-c",connector,(char*)0);
}
fprintf(stderr,"can't exec /bin/sh: %s",strerror(errno));
_exit(99);
}
while ( waitpid(pid, &status, 0) < 0 ) {
if ( errno == EINTR )
continue;
errx(1,"error waiting for scripting process");
}
unlock(1);
return(status == 0 ? 0: -1);
}
void
usage()
{
printf("usage: %s [options] <progname>\n"\
" -m - reset CLOCAL\n"\
" -r - set CRTSCTS\n"\
" -n - don't lock device\n"\
" -s <speed> - serial port baud rate [57600]\n"\
" -l <tty> - tty [/dev/ttyd1]\n"\
" -c <file> - run '/usr/bin/chat -Sf <file>' instead progname\n"\
" -v - run '-c' mode with '-vsf' options\n"\
" progname - run this program with stdin/stdout on tty\n",
myname);
exit(1);
}
/*
* set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
* at the requested speed, etc. If `local' is true, set CLOCAL
* regardless of whether the modem option was specified.
*
* For *BSD, we assume that speed_t values numerically equal bits/second.
*/
void
set_up_tty(fd, local)
int fd, local;
{
struct termios tios;
if (tcgetattr(fd, &tios) < 0) {
errx(1,"tcgetattr: %s",strerror(errno));
}
if (!restore_term) {
inittermios = tios;
ioctl(fd, TIOCGWINSZ, &wsinfo);
}
tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
if (crtscts)
tios.c_cflag |= CRTSCTS;
else
tios.c_cflag &= ~CRTSCTS;
tios.c_cflag |= CS8 | CREAD | HUPCL;
if (local)
tios.c_cflag |= CLOCAL;
tios.c_iflag = IGNBRK | IGNPAR;
tios.c_oflag = 0;
tios.c_lflag = 0;
tios.c_cc[VMIN] = 1;
tios.c_cc[VTIME] = 0;
cfsetospeed(&tios, baud_rate);
cfsetispeed(&tios, baud_rate);
if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
errx(1,"tcsetattr: %s",strerror(errno));
}
restore_term = 1;
}
/*
* lock - create a lock file for the named lock device
*/
#define LOCK_PREFIX "/var/spool/lock/LCK.."
int
lock(dev)
char *dev;
{
char hdb_lock_buffer[12];
int fd, pid, n;
char *p;
if ((p = strrchr(dev, '/')) != NULL)
dev = p + 1;
lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
if (lock_file == NULL)
errx(-1,"lock file name");
strcat(strcpy(lock_file, LOCK_PREFIX), dev);
while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
if (errno == EEXIST
&& (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
/* Read the lock file to find out who has the device locked */
n = read(fd, hdb_lock_buffer, 11);
if (n <= 0) {
syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
close(fd);
} else {
hdb_lock_buffer[n] = 0;
pid = atoi(hdb_lock_buffer);
if (kill(pid, 0) == -1 && errno == ESRCH) {
/* pid no longer exists - remove the lock file */
if (unlink(lock_file) == 0) {
close(fd);
syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
dev, pid);
continue;
} else
syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
dev);
} else
syslog(LOG_NOTICE, "Device %s is locked by pid %d",
dev, pid);
}
close(fd);
} else
syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
free(lock_file);
lock_file = NULL;
return -1;
}
sprintf(hdb_lock_buffer, "%10d\n", getpid());
write(fd, hdb_lock_buffer, 11);
close(fd);
return 0;
}
/*
* unlock - remove our lockfile
*/
void
unlock(int sig)
{
if (lock_file) {
unlink(lock_file);
free(lock_file);
lock_file = NULL;
}
}
*** WBR, Alex Zakirov (frank@pptus.ru, ICQ:38685959, 2:5029/5)
...UNIX _is_ user friendly. It's just selective about who it's friends are.
(c) Marco Molteni
--- tin/pre-1.4-19990517 ("Psychonaut") (UNIX) (FreeBSD/3.1-19990324-STABLE
(i386))
* Origin: RSI Station (2:5029/5@fidonet)