This repository has been archived by the owner on May 16, 2023. It is now read-only.
forked from mohnkhan/xavl2tp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpty.c
executable file
·139 lines (118 loc) · 2.66 KB
/
pty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Layer Two Tunnelling Protocol Daemon
* Copyright (C) 1998 Adtran, Inc.
* Copyright (C) 2002 Jeff McAdams
*
* Mark Spencer
*
* This software is distributed under the terms
* of the GPL, which you should have received
* along with this source.
*
* Pseudo-pty allocation routines... Concepts and code borrowed
* from pty-redir by Magosanyi Arpad.
*
*/
#define _ISOC99_SOURCE
#define _XOPEN_SOURCE
#define _BSD_SOURCE
#define _XOPEN_SOURCE_EXTENDED
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include "l2tp.h"
#ifdef SOLARIS
#define PTY00 "/dev/ptyXX"
#define PTY10 "pqrstuvwxyz"
#define PTY01 "0123456789abcdef"
#endif
#ifdef LINUX
#define PTY00 "/dev/ptyXX"
#define PTY10 "pqrstuvwxyzabcde"
#define PTY01 "0123456789abcdef"
#endif
//#ifdef FREEBSD
#define PTY00 "/dev/ptyXX"
#define PTY10 "p"
#define PTY01 "0123456789abcdefghijklmnopqrstuv"
//#endif
int getPtyMaster_pty (char *tty10, char *tty01)
{
char *p10;
char *p01;
static char dev[] = PTY00;
int fd;
for (p10 = PTY10; *p10; p10++)
{
dev[8] = *p10;
for (p01 = PTY01; *p01; p01++)
{
dev[9] = *p01;
fd = open (dev, O_RDWR | O_NONBLOCK);
if (fd >= 0)
{
*tty10 = *p10;
*tty01 = *p01;
return fd;
}
}
}
l2tp_log (LOG_CRIT, "%s: No more free pseudo-tty's\n", __FUNCTION__);
return -1;
}
int getPtyMaster_ptmx(char *ttybuf, int ttybuflen)
{
int fd;
char *tty;
fd = open("/dev/ptmx", O_RDWR);
if (fd == -1)
{
l2tp_log (LOG_WARNING, "%s: unable to open /dev/ptmx to allocate pty\n",
__FUNCTION__);
return -EINVAL;
}
/* change the onwership */
if (grantpt(fd))
{
l2tp_log (LOG_WARNING, "%s: unable to grantpt() on pty\n",
__FUNCTION__);
close(fd);
return -EINVAL;
}
if (unlockpt(fd))
{
l2tp_log (LOG_WARNING, "%s: unable to unlockpt() on pty\n",
__FUNCTION__);
close(fd);
return -EINVAL;
}
tty = ptsname(fd);
if (tty == NULL)
{
l2tp_log (LOG_WARNING, "%s: unable to obtain name of slave tty\n",
__FUNCTION__);
close(fd);
return -EINVAL;
}
ttybuf[0]='\0';
strncat(ttybuf, tty, ttybuflen);
return fd;
}
int getPtyMaster(char *ttybuf, int ttybuflen)
{
int fd = getPtyMaster_ptmx(ttybuf, ttybuflen);
char a, b;
if(fd >= 0) {
return fd;
}
l2tp_log (LOG_WARNING, "%s: failed to use pts -- using legacy ptys\n", __FUNCTION__);
fd = getPtyMaster_pty(&a,&b);
if(fd >= 0) {
snprintf(ttybuf, ttybuflen, "/dev/tty%c%c", a, b);
return fd;
}
return -EINVAL;
}