/
usb2ppp.c
254 lines (232 loc) · 5.62 KB
/
usb2ppp.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Officially runs PPP over a USB serial port, but actually can run any
* program over a serial port, connecting its standard input and output.
*
* Usage: ./usb2ppp <path to port> <speed> <program> <arguments ...>
*
* Typical usage for pppd (note that you usually need to be root):
sudo ./usb2ppp /dev/ttyUSB0 38400 \
pppd :IP.ADDR.TO.ASSIGN local ms-dns D.N.S.SERVR netmask N.E.T.MASK \
passive noauth proxyarp notty debug persist nodetach asyncmap 0 ktune
* Copyright (C) 2023 Cameron Kaiser. All rights reserved.
* Floodgap Free Software License.
* http://oldvcr.blogspot.com/
*/
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <sys/select.h>
int port, pid = 0;
struct termios tty, tty_saved, tty_saved_in, tty_saved_out, tty_saved_err;
struct sigaction sig;
speed_t
baudr(int baud) {
switch (baud) {
case 300: return B300;
case 600: return B600;
case 1200: return B1200;
case 2400: return B2400;
case 4800: return B4800;
case 9600: return B9600;
case 19200: return B19200;
case 38400: return B38400;
case 57600: return B57600;
case 115200: return B115200;
default: return B0;
}
return B0; /* not reached */
}
static int
popen2(char **cmd, int *pid, int *infd, int *outfd)
{
int p1[2], p2[2];
if (!cmd || !pid || !infd || !outfd) return -1;
if (pipe(p1) == -1) return -1;
if (pipe(p2) == -1) {
close(p1[1]); close(p1[0]);
return -1;
}
if ((*pid = fork()) == -1) {
close(p2[1]); close(p2[0]);
close(p1[1]); close(p1[0]);
return -1;
}
if (*pid) {
/* parent */
*infd = p1[1];
*outfd = p2[0];
close(p1[0]); close(p2[1]);
return 0;
}
/* child */
(void)dup2(p1[0], 0);
(void)dup2(p2[1], 1);
close(p1[0]); close(p1[1]);
close(p2[0]); close(p2[1]);
execvp(cmd[0], cmd);
/* oops */
#if DEBUG
perror("execvp");
#endif
exit(255);
return -1;
}
void
cleanup()
{
/* handle child process, but don't pingpong */
sig.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &sig, NULL);
if (pid) {
fprintf(stderr, "killing subprocess\n");
(void)kill(pid, SIGHUP);
sleep(1);
(void)kill(pid, SIGTERM);
pid = 0;
}
fprintf(stderr, "restoring terminal settings\n");
(void)tcsetattr(port, TCSAFLUSH, &tty_saved);
(void)close(port);
(void)tcsetattr(STDIN_FILENO, TCSANOW, &tty_saved_in);
(void)tcsetattr(STDOUT_FILENO, TCSANOW, &tty_saved_out);
(void)tcsetattr(STDERR_FILENO, TCSANOW, &tty_saved_err);
}
void
deadkid()
{
pid = 0;
fprintf(stderr, "subprocess terminated\n");
exit(0);
}
void
deadme()
{
fprintf(stderr, "terminating on user signal\n");
exit(0);
}
#define SERBUF 8192
int
main(int argc, char **argv)
{
int port, speed, infd, outfd, bytes;
fd_set master_rfd, rfd, wfd;
struct timeval tv;
char buf[SERBUF];
speed_t speedd;
/* it's not enough to just restore the serial port settings */
if (tcgetattr(STDIN_FILENO, &tty_saved_in) ||
tcgetattr(STDOUT_FILENO, &tty_saved_out) ||
tcgetattr(STDERR_FILENO, &tty_saved_err)) {
perror("tcgetattr (tty)");
return 1;
}
if (argc < 2) {
fprintf(stderr, "missing path to serial port (e.g., /dev/cu.usbserial)\n");
return 1;
}
if (argc < 3) {
fprintf(stderr, "missing serial port speed\n");
return 1;
}
speed = atoi(argv[2]);
if (!speed || ((speedd = baudr(speed)) == B0)) {
fprintf(stderr, "unsupported baud rate %d\n", speed);
return 1;
}
fprintf(stderr, "opening %s\n", argv[1]);
port = open(argv[1], O_RDWR | O_NOCTTY);
if (!isatty(port)) {
fprintf(stderr, "warning: isatty returned false for this path\n");
}
fprintf(stderr, "setting up for serial access\n");
if (tcgetattr(port, &tty)) {
perror("tcgetattr");
close(port);
return 1;
}
tty_saved = tty;
fprintf(stderr, "setting flags on serial port fd=%d\n", port);
memset(&tty, 0, sizeof(tty));
tty.c_cflag |= (CS8 | CREAD | CLOCAL);
tty.c_cc[VTIME] = 5;
tty.c_cc[VMIN] = 1;
(void)cfsetospeed(&tty, speedd);
(void)cfsetispeed(&tty, speedd);
if (tcsetattr(port, TCSAFLUSH, &tty)) {
perror("tcsetattr (tty)");
close(port);
return 1;
}
atexit(cleanup);
sig.sa_handler = deadkid;
sigaction(SIGCHLD, &sig, NULL);
sig.sa_handler = deadme;
sigaction(SIGINT, &sig, NULL);
sigaction(SIGHUP, &sig, NULL);
sigaction(SIGTERM, &sig, NULL);
if (argc < 4) {
fprintf(stderr, "missing path to process\n");
return 1;
}
if (argc < 5) {
if (argv[4] != NULL) {
fprintf(stderr, "unexpected: no null termination\n");
return 255;
}
fprintf(stderr, "warning: no arguments being passed to %s\n",
argv[3]);
} else {
fprintf(stderr, "starting process %s\n", argv[3]);
}
if (popen2(&(argv[3]), &pid, &infd, &outfd)) {
perror("popen2");
close(port);
return 255;
}
fprintf(stderr, "subprocess pid= %d\n", pid);
FD_ZERO(&master_rfd);
FD_SET(port, &master_rfd);
FD_SET(outfd, &master_rfd);
for(;;) {
/* time out if we can't write. check individually */
FD_ZERO(&wfd);
FD_SET(port, &wfd);
tv.tv_sec = 5;
tv.tv_usec = 0;
if (select(outfd+1, NULL, &wfd, NULL, &tv) < 1) {
perror("timeout write select on port");
return 1;
}
FD_ZERO(&wfd);
FD_SET(infd, &wfd);
tv.tv_sec = 5;
tv.tv_usec = 0;
if (select(outfd+1, NULL, &wfd, NULL, &tv) < 1) {
perror("timeout write select on process");
return 1;
}
/* wait for something to read */
rfd = master_rfd;
if (select(outfd+1, &rfd, NULL, NULL, NULL) < 0)
perror("warning: read select");
if (FD_ISSET(port, &rfd)) {
if (bytes = read(port, &buf, SERBUF)) {
write(infd, &buf, bytes);
} else {
// XXX
}
}
if (FD_ISSET(outfd, &rfd)) {
if (bytes = read(outfd, &buf, SERBUF)) {
write(port, &buf, bytes);
} else {
// XXX
}
}
}
return 255; /* not reached */
}