Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / rp-l2tp / handlers / pty.c
blobed880a9407eec84516ed73b772219e98d6dd3298
1 /***********************************************************************
3 * pty.c
5 * Code for dealing with pseudo-tty's for running pppd.
7 * Copyright (C) 2002 Roaring Penguin Software Inc.
9 * This software may be distributed under the terms of the GNU General
10 * Public License, Version 2, or (at your option) any later version.
12 * LIC: GPL
14 ***********************************************************************/
16 static char const RCSID[] =
17 "$Id: pty.c,v 1.1.48.1 2005/08/08 12:05:25 honor Exp $";
19 #include "../l2tp.h"
20 #include <sys/ioctl.h>
21 #include <termios.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #ifndef N_HDLC
25 #include <linux/termios.h>
26 #endif
28 /**********************************************************************
29 * %FUNCTION: pty_get
30 * %ARGUMENTS:
31 * mfp -- pointer to master file descriptor
32 * sfp -- pointer to slave file descriptor
33 * %RETURNS:
34 * 0 on success, -1 on failure
35 * %DESCRIPTION:
36 * Opens a PTY and sets line discipline to N_HDLC for ppp.
37 * Taken almost verbatim from Linux pppd code.
38 ***********************************************************************/
39 int
40 pty_get(int *mfp, int *sfp)
42 char pty_name[24];
43 struct termios tios;
44 int mfd, sfd;
45 int disc = N_HDLC;
47 mfd = -1;
48 sfd = -1;
50 mfd = open("/dev/ptmx", O_RDWR);
51 if (mfd >= 0) {
52 int ptn;
53 if (ioctl(mfd, TIOCGPTN, &ptn) >= 0) {
54 snprintf(pty_name, sizeof(pty_name), "/dev/pts/%d", ptn);
55 ptn = 0;
56 if (ioctl(mfd, TIOCSPTLCK, &ptn) < 0) {
57 /* warn("Couldn't unlock pty slave %s: %m", pty_name); */
59 if ((sfd = open(pty_name, O_RDWR | O_NOCTTY)) < 0) {
60 /* warn("Couldn't open pty slave %s: %m", pty_name); */
65 if (sfd < 0 || mfd < 0) {
66 if (sfd >= 0) close(sfd);
67 if (mfd >= 0) close(mfd);
68 return -1;
71 *mfp = mfd;
72 *sfp = sfd;
73 if (tcgetattr(sfd, &tios) == 0) {
74 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
75 tios.c_cflag |= CS8 | CREAD | CLOCAL;
76 tios.c_iflag = IGNPAR;
77 tios.c_oflag = 0;
78 tios.c_lflag = 0;
79 tcsetattr(sfd, TCSAFLUSH, &tios);
81 if (ioctl(sfd, TIOCSETD, &disc) < 0) {
82 l2tp_set_errmsg("Unable to set line discipline to N_HDLC");
83 close(mfd);
84 close(sfd);
85 return -1;
87 disc = N_HDLC;
88 if (ioctl(mfd, TIOCSETD, &disc) < 0) {
89 l2tp_set_errmsg("Unable to set line discipline to N_HDLC");
90 close(mfd);
91 close(sfd);
92 return -1;
94 return 0;