forked from xelerance/xl2tpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pty.c
180 lines (155 loc) · 3.46 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* 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 _DEFAULT_SOURCE
#define _XOPEN_SOURCE_EXTENDED 1
#define _GNU_SOURCE
#include <stdlib.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <netdb.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.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
#if defined(FREEBSD) || defined(NETBSD)
#define PTY00 "/dev/ptyXX"
#define PTY10 "p"
#define PTY01 "0123456789abcdefghijklmnopqrstuv"
#endif
#ifndef OPENBSD
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;
}
/* No need to call grantpt */
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;
}
#endif
#ifdef OPENBSD
int getPtyMaster_ptm(char *ttybuf, int ttybuflen)
{
int amaster, aslave;
char *tty = malloc(64);
if((openpty(&amaster, &aslave, tty, NULL, NULL)) == -1)
{
l2tp_log (LOG_WARNING, "%s: openpty() returned %s\n",
__FUNCTION__, strerror(errno));
free(tty);
return -EINVAL;
}
ttybuf[0] = '\0';
strncat(ttybuf, tty, ttybuflen);
free(tty);
return amaster;
}
#endif /* OPENBSD */
int getPtyMaster(char *ttybuf, int ttybuflen)
{
int fd;
#ifndef OPENBSD
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;
}
#endif
#ifdef OPENBSD
fd = getPtyMaster_ptm(ttybuf, ttybuflen);
if(fd >= 0) {
return fd;
}
#endif /* OPENBSD */
return -EINVAL;
}