1/*
2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
5 *
6 * Mark Spencer
7 *
8 * This software is distributed under the terms
9 * of the GPL, which you should have received
10 * along with this source.
11 *
12 * Pseudo-pty allocation routines...  Concepts and code borrowed
13 * from pty-redir by Magosanyi Arpad.
14 *
15 */
16
17#include "l2tp.h"
18#include <fcntl.h>
19
20#ifdef SOLARIS
21#define PTY00 "/dev/ptyXX"
22#define PTY10 "pqrstuvwxyz"
23#define PTY01 "0123456789abcdef"
24#endif
25
26#ifdef LINUX
27#define PTY00 "/dev/ptyXX"
28#define PTY10 "pqrstuvwxyzabcde"
29#define PTY01 "0123456789abcdef"
30#endif
31
32#ifdef FREEBSD
33#define PTY00 "/dev/ptyXX"
34#define PTY10 "p"
35#define PTY01 "0123456789abcdefghijklmnopqrstuv"
36#endif
37
38int getPtyMaster (char *tty10, char *tty01)
39{
40    char *p10;
41    char *p01;
42    static char dev[] = PTY00;
43    int fd;
44
45    for (p10 = PTY10; *p10; p10++)
46    {
47        dev[8] = *p10;
48        for (p01 = PTY01; *p01; p01++)
49        {
50            dev[9] = *p01;
51            fd = open (dev, O_RDWR | O_NONBLOCK);
52            if (fd >= 0)
53            {
54                *tty10 = *p10;
55                *tty01 = *p01;
56                return fd;
57            }
58        }
59    }
60    log (LOG_CRIT, "%s: No more free pseudo-tty's\n", __FUNCTION__);
61    return -1;
62}
63