1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
3 | *
|
---|
4 | * Please read the file COPYRIGHT for the
|
---|
5 | * terms and conditions of the copyright.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define WANT_SYS_IOCTL_H
|
---|
9 | #include <slirp.h>
|
---|
10 |
|
---|
11 | u_int curtime, time_fasttimo, last_slowtimo, detach_time;
|
---|
12 | u_int detach_wait = 600000; /* 10 minutes */
|
---|
13 |
|
---|
14 | #if 0
|
---|
15 | int x_port = -1;
|
---|
16 | int x_display = 0;
|
---|
17 | int x_screen = 0;
|
---|
18 |
|
---|
19 | int
|
---|
20 | show_x(buff, inso)
|
---|
21 | char *buff;
|
---|
22 | struct socket *inso;
|
---|
23 | {
|
---|
24 | if (x_port < 0) {
|
---|
25 | lprint("X Redir: X not being redirected.\r\n");
|
---|
26 | } else {
|
---|
27 | lprint("X Redir: In sh/bash/zsh/etc. type: DISPLAY=%s:%d.%d; export DISPLAY\r\n",
|
---|
28 | inet_ntoa(our_addr), x_port, x_screen);
|
---|
29 | lprint("X Redir: In csh/tcsh/etc. type: setenv DISPLAY %s:%d.%d\r\n",
|
---|
30 | inet_ntoa(our_addr), x_port, x_screen);
|
---|
31 | if (x_display)
|
---|
32 | lprint("X Redir: Redirecting to display %d\r\n", x_display);
|
---|
33 | }
|
---|
34 |
|
---|
35 | return CFG_OK;
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * XXX Allow more than one X redirection?
|
---|
41 | */
|
---|
42 | void
|
---|
43 | redir_x(inaddr, start_port, display, screen)
|
---|
44 | u_int32_t inaddr;
|
---|
45 | int start_port;
|
---|
46 | int display;
|
---|
47 | int screen;
|
---|
48 | {
|
---|
49 | int i;
|
---|
50 |
|
---|
51 | if (x_port >= 0) {
|
---|
52 | lprint("X Redir: X already being redirected.\r\n");
|
---|
53 | show_x(0, 0);
|
---|
54 | } else {
|
---|
55 | for (i = 6001 + (start_port-1); i <= 6100; i++) {
|
---|
56 | if (solisten(htons(i), inaddr, htons(6000 + display), 0)) {
|
---|
57 | /* Success */
|
---|
58 | x_port = i - 6000;
|
---|
59 | x_display = display;
|
---|
60 | x_screen = screen;
|
---|
61 | show_x(0, 0);
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | lprint("X Redir: Error: Couldn't redirect a port for X. Weird.\r\n");
|
---|
66 | }
|
---|
67 | }
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | #ifndef HAVE_INET_ATON
|
---|
71 | int
|
---|
72 | inet_aton(cp, ia)
|
---|
73 | const char *cp;
|
---|
74 | struct in_addr *ia;
|
---|
75 | {
|
---|
76 | u_int32_t addr = inet_addr(cp);
|
---|
77 | if (addr == 0xffffffff)
|
---|
78 | return 0;
|
---|
79 | ia->s_addr = addr;
|
---|
80 | return 1;
|
---|
81 | }
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * Get our IP address and put it in our_addr
|
---|
86 | */
|
---|
87 | void
|
---|
88 | getouraddr()
|
---|
89 | {
|
---|
90 | char buff[256];
|
---|
91 | struct hostent *he = NULL;
|
---|
92 |
|
---|
93 | if (gethostname(buff,256) == 0)
|
---|
94 | he = gethostbyname(buff);
|
---|
95 | if (he)
|
---|
96 | our_addr = *(struct in_addr *)he->h_addr;
|
---|
97 | if (our_addr.s_addr == 0)
|
---|
98 | our_addr.s_addr = loopback_addr.s_addr;
|
---|
99 | }
|
---|
100 |
|
---|
101 | #if SIZEOF_CHAR_P == 8
|
---|
102 |
|
---|
103 | struct quehead_32 {
|
---|
104 | u_int32_t qh_link;
|
---|
105 | u_int32_t qh_rlink;
|
---|
106 | };
|
---|
107 |
|
---|
108 | inline void
|
---|
109 | insque_32(a, b)
|
---|
110 | void *a;
|
---|
111 | void *b;
|
---|
112 | {
|
---|
113 | register struct quehead_32 *element = (struct quehead_32 *) a;
|
---|
114 | register struct quehead_32 *head = (struct quehead_32 *) b;
|
---|
115 | struct quehead_32 *link = u32_to_ptr(head->qh_link, struct quehead_32 *);
|
---|
116 |
|
---|
117 | element->qh_link = head->qh_link;
|
---|
118 | element->qh_rlink = ptr_to_u32(head);
|
---|
119 | Assert(link->qh_rlink == element->qh_rlink);
|
---|
120 | link->qh_rlink = head->qh_link = ptr_to_u32(element);
|
---|
121 | }
|
---|
122 |
|
---|
123 | inline void
|
---|
124 | remque_32(a)
|
---|
125 | void *a;
|
---|
126 | {
|
---|
127 | register struct quehead_32 *element = (struct quehead_32 *) a;
|
---|
128 | struct quehead_32 *link = u32_to_ptr(element->qh_link, struct quehead_32 *);
|
---|
129 | struct quehead_32 *rlink = u32_to_ptr(element->qh_rlink, struct quehead_32 *);
|
---|
130 |
|
---|
131 | u32ptr_done(link->qh_rlink, element);
|
---|
132 | link->qh_rlink = element->qh_rlink;
|
---|
133 | rlink->qh_link = element->qh_link;
|
---|
134 | element->qh_rlink = 0;
|
---|
135 | }
|
---|
136 |
|
---|
137 | #endif /* SIZEOF_CHAR_P == 8 */
|
---|
138 |
|
---|
139 | struct quehead {
|
---|
140 | struct quehead *qh_link;
|
---|
141 | struct quehead *qh_rlink;
|
---|
142 | };
|
---|
143 |
|
---|
144 | #if defined(VBOX) && defined(_MSC_VER)
|
---|
145 | void
|
---|
146 | #else
|
---|
147 | inline void
|
---|
148 | #endif
|
---|
149 | insque(a, b)
|
---|
150 | void *a, *b;
|
---|
151 | {
|
---|
152 | register struct quehead *element = (struct quehead *) a;
|
---|
153 | register struct quehead *head = (struct quehead *) b;
|
---|
154 | element->qh_link = head->qh_link;
|
---|
155 | head->qh_link = (struct quehead *)element;
|
---|
156 | element->qh_rlink = (struct quehead *)head;
|
---|
157 | ((struct quehead *)(element->qh_link))->qh_rlink
|
---|
158 | = (struct quehead *)element;
|
---|
159 | }
|
---|
160 |
|
---|
161 | #if defined(VBOX) && defined(_MSC_VER)
|
---|
162 | void
|
---|
163 | #else
|
---|
164 | inline void
|
---|
165 | #endif
|
---|
166 | remque(a)
|
---|
167 | void *a;
|
---|
168 | {
|
---|
169 | register struct quehead *element = (struct quehead *) a;
|
---|
170 | ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
|
---|
171 | ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
|
---|
172 | element->qh_rlink = NULL;
|
---|
173 | /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
|
---|
174 | }
|
---|
175 |
|
---|
176 | /* #endif */
|
---|
177 |
|
---|
178 |
|
---|
179 | int
|
---|
180 | add_exec(ex_ptr, do_pty, exec, addr, port)
|
---|
181 | struct ex_list **ex_ptr;
|
---|
182 | int do_pty;
|
---|
183 | char *exec;
|
---|
184 | int addr;
|
---|
185 | int port;
|
---|
186 | {
|
---|
187 | struct ex_list *tmp_ptr;
|
---|
188 |
|
---|
189 | /* First, check if the port is "bound" */
|
---|
190 | for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
|
---|
191 | if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
|
---|
192 | return -1;
|
---|
193 | }
|
---|
194 |
|
---|
195 | tmp_ptr = *ex_ptr;
|
---|
196 | *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
|
---|
197 | (*ex_ptr)->ex_fport = port;
|
---|
198 | (*ex_ptr)->ex_addr = addr;
|
---|
199 | (*ex_ptr)->ex_pty = do_pty;
|
---|
200 | (*ex_ptr)->ex_exec = strdup(exec);
|
---|
201 | (*ex_ptr)->ex_next = tmp_ptr;
|
---|
202 | return 0;
|
---|
203 | }
|
---|
204 |
|
---|
205 | #ifndef HAVE_STRERROR
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * For systems with no strerror
|
---|
209 | */
|
---|
210 |
|
---|
211 | extern int sys_nerr;
|
---|
212 | extern char *sys_errlist[];
|
---|
213 |
|
---|
214 | char *
|
---|
215 | strerror(error)
|
---|
216 | int error;
|
---|
217 | {
|
---|
218 | if (error < sys_nerr)
|
---|
219 | return sys_errlist[error];
|
---|
220 | else
|
---|
221 | return "Unknown error.";
|
---|
222 | }
|
---|
223 |
|
---|
224 | #endif
|
---|
225 |
|
---|
226 |
|
---|
227 | #ifdef _WIN32
|
---|
228 |
|
---|
229 | int
|
---|
230 | fork_exec(so, ex, do_pty)
|
---|
231 | struct socket *so;
|
---|
232 | char *ex;
|
---|
233 | int do_pty;
|
---|
234 | {
|
---|
235 | /* not implemented */
|
---|
236 | return 0;
|
---|
237 | }
|
---|
238 |
|
---|
239 | #else
|
---|
240 |
|
---|
241 | int
|
---|
242 | slirp_openpty(amaster, aslave)
|
---|
243 | int *amaster, *aslave;
|
---|
244 | {
|
---|
245 | register int master, slave;
|
---|
246 |
|
---|
247 | #ifdef HAVE_GRANTPT
|
---|
248 | char *ptr;
|
---|
249 |
|
---|
250 | if ((master = open("/dev/ptmx", O_RDWR)) < 0 ||
|
---|
251 | grantpt(master) < 0 ||
|
---|
252 | unlockpt(master) < 0 ||
|
---|
253 | (ptr = ptsname(master)) == NULL) {
|
---|
254 | close(master);
|
---|
255 | return -1;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if ((slave = open(ptr, O_RDWR)) < 0 ||
|
---|
259 | ioctl(slave, I_PUSH, "ptem") < 0 ||
|
---|
260 | ioctl(slave, I_PUSH, "ldterm") < 0 ||
|
---|
261 | ioctl(slave, I_PUSH, "ttcompat") < 0) {
|
---|
262 | close(master);
|
---|
263 | close(slave);
|
---|
264 | return -1;
|
---|
265 | }
|
---|
266 |
|
---|
267 | *amaster = master;
|
---|
268 | *aslave = slave;
|
---|
269 | return 0;
|
---|
270 |
|
---|
271 | #else
|
---|
272 |
|
---|
273 | static char line[] = "/dev/ptyXX";
|
---|
274 | register const char *cp1, *cp2;
|
---|
275 |
|
---|
276 | for (cp1 = "pqrsPQRS"; *cp1; cp1++) {
|
---|
277 | line[8] = *cp1;
|
---|
278 | for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
|
---|
279 | line[9] = *cp2;
|
---|
280 | if ((master = open(line, O_RDWR, 0)) == -1) {
|
---|
281 | if (errno == ENOENT)
|
---|
282 | return (-1); /* out of ptys */
|
---|
283 | } else {
|
---|
284 | line[5] = 't';
|
---|
285 | /* These will fail */
|
---|
286 | (void) chown(line, getuid(), 0);
|
---|
287 | (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
|
---|
288 | #ifdef HAVE_REVOKE
|
---|
289 | (void) revoke(line);
|
---|
290 | #endif
|
---|
291 | if ((slave = open(line, O_RDWR, 0)) != -1) {
|
---|
292 | *amaster = master;
|
---|
293 | *aslave = slave;
|
---|
294 | return 0;
|
---|
295 | }
|
---|
296 | (void) close(master);
|
---|
297 | line[5] = 'p';
|
---|
298 | }
|
---|
299 | }
|
---|
300 | }
|
---|
301 | errno = ENOENT; /* out of ptys */
|
---|
302 | return (-1);
|
---|
303 | #endif
|
---|
304 | }
|
---|
305 |
|
---|
306 | /*
|
---|
307 | * XXX This is ugly
|
---|
308 | * We create and bind a socket, then fork off to another
|
---|
309 | * process, which connects to this socket, after which we
|
---|
310 | * exec the wanted program. If something (strange) happens,
|
---|
311 | * the accept() call could block us forever.
|
---|
312 | *
|
---|
313 | * do_pty = 0 Fork/exec inetd style
|
---|
314 | * do_pty = 1 Fork/exec using slirp.telnetd
|
---|
315 | * do_ptr = 2 Fork/exec using pty
|
---|
316 | */
|
---|
317 | int
|
---|
318 | fork_exec(so, ex, do_pty)
|
---|
319 | struct socket *so;
|
---|
320 | char *ex;
|
---|
321 | int do_pty;
|
---|
322 | {
|
---|
323 | int s;
|
---|
324 | struct sockaddr_in addr;
|
---|
325 | #ifndef VBOX
|
---|
326 | int addrlen = sizeof(addr);
|
---|
327 | #else /* VBOX */
|
---|
328 | socklen_t addrlen = sizeof(addr);
|
---|
329 | #endif /* VBOX */
|
---|
330 | int opt;
|
---|
331 | int master;
|
---|
332 | char *argv[256];
|
---|
333 | #if 0
|
---|
334 | char buff[256];
|
---|
335 | #endif
|
---|
336 | /* don't want to clobber the original */
|
---|
337 | char *bptr;
|
---|
338 | char *curarg;
|
---|
339 | int c, i, ret;
|
---|
340 |
|
---|
341 | DEBUG_CALL("fork_exec");
|
---|
342 | DEBUG_ARG("so = %lx", (long)so);
|
---|
343 | DEBUG_ARG("ex = %lx", (long)ex);
|
---|
344 | DEBUG_ARG("do_pty = %lx", (long)do_pty);
|
---|
345 |
|
---|
346 | if (do_pty == 2) {
|
---|
347 | if (slirp_openpty(&master, &s) == -1) {
|
---|
348 | lprint("Error: openpty failed: %s\n", strerror(errno));
|
---|
349 | return 0;
|
---|
350 | }
|
---|
351 | } else {
|
---|
352 | addr.sin_family = AF_INET;
|
---|
353 | addr.sin_port = 0;
|
---|
354 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
355 |
|
---|
356 | if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
|
---|
357 | bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
|
---|
358 | listen(s, 1) < 0) {
|
---|
359 | lprint("Error: inet socket: %s\n", strerror(errno));
|
---|
360 | closesocket(s);
|
---|
361 |
|
---|
362 | return 0;
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | switch(fork()) {
|
---|
367 | case -1:
|
---|
368 | lprint("Error: fork failed: %s\n", strerror(errno));
|
---|
369 | close(s);
|
---|
370 | if (do_pty == 2)
|
---|
371 | close(master);
|
---|
372 | return 0;
|
---|
373 |
|
---|
374 | case 0:
|
---|
375 | /* Set the DISPLAY */
|
---|
376 | if (do_pty == 2) {
|
---|
377 | (void) close(master);
|
---|
378 | #ifdef TIOCSCTTY /* XXXXX */
|
---|
379 | (void) setsid();
|
---|
380 | ioctl(s, TIOCSCTTY, (char *)NULL);
|
---|
381 | #endif
|
---|
382 | } else {
|
---|
383 | getsockname(s, (struct sockaddr *)&addr, &addrlen);
|
---|
384 | close(s);
|
---|
385 | /*
|
---|
386 | * Connect to the socket
|
---|
387 | * XXX If any of these fail, we're in trouble!
|
---|
388 | */
|
---|
389 | s = socket(AF_INET, SOCK_STREAM, 0);
|
---|
390 | addr.sin_addr = loopback_addr;
|
---|
391 | do {
|
---|
392 | ret = connect(s, (struct sockaddr *)&addr, addrlen);
|
---|
393 | } while (ret < 0 && errno == EINTR);
|
---|
394 | }
|
---|
395 |
|
---|
396 | #if 0
|
---|
397 | if (x_port >= 0) {
|
---|
398 | #ifdef HAVE_SETENV
|
---|
399 | sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
|
---|
400 | setenv("DISPLAY", buff, 1);
|
---|
401 | #else
|
---|
402 | sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
|
---|
403 | putenv(buff);
|
---|
404 | #endif
|
---|
405 | }
|
---|
406 | #endif
|
---|
407 | dup2(s, 0);
|
---|
408 | dup2(s, 1);
|
---|
409 | dup2(s, 2);
|
---|
410 | for (s = 3; s <= 255; s++)
|
---|
411 | close(s);
|
---|
412 |
|
---|
413 | i = 0;
|
---|
414 | bptr = strdup(ex); /* No need to free() this */
|
---|
415 | if (do_pty == 1) {
|
---|
416 | /* Setup "slirp.telnetd -x" */
|
---|
417 | argv[i++] = "slirp.telnetd";
|
---|
418 | argv[i++] = "-x";
|
---|
419 | argv[i++] = bptr;
|
---|
420 | } else
|
---|
421 | do {
|
---|
422 | /* Change the string into argv[] */
|
---|
423 | curarg = bptr;
|
---|
424 | while (*bptr != ' ' && *bptr != (char)0)
|
---|
425 | bptr++;
|
---|
426 | c = *bptr;
|
---|
427 | *bptr++ = (char)0;
|
---|
428 | argv[i++] = strdup(curarg);
|
---|
429 | } while (c);
|
---|
430 |
|
---|
431 | argv[i] = 0;
|
---|
432 | execvp(argv[0], argv);
|
---|
433 |
|
---|
434 | /* Ooops, failed, let's tell the user why */
|
---|
435 | {
|
---|
436 | char buff[256];
|
---|
437 |
|
---|
438 | sprintf(buff, "Error: execvp of %s failed: %s\n",
|
---|
439 | argv[0], strerror(errno));
|
---|
440 | write(2, buff, strlen(buff)+1);
|
---|
441 | }
|
---|
442 | close(0); close(1); close(2); /* XXX */
|
---|
443 | exit(1);
|
---|
444 |
|
---|
445 | default:
|
---|
446 | if (do_pty == 2) {
|
---|
447 | close(s);
|
---|
448 | so->s = master;
|
---|
449 | } else {
|
---|
450 | /*
|
---|
451 | * XXX this could block us...
|
---|
452 | * XXX Should set a timer here, and if accept() doesn't
|
---|
453 | * return after X seconds, declare it a failure
|
---|
454 | * The only reason this will block forever is if socket()
|
---|
455 | * of connect() fail in the child process
|
---|
456 | */
|
---|
457 | do {
|
---|
458 | so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
|
---|
459 | } while (so->s < 0 && errno == EINTR);
|
---|
460 | closesocket(s);
|
---|
461 | opt = 1;
|
---|
462 | setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
|
---|
463 | opt = 1;
|
---|
464 | setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
|
---|
465 | }
|
---|
466 | fd_nonblock(so->s);
|
---|
467 |
|
---|
468 | /* Append the telnet options now */
|
---|
469 | if (so->so_m != 0 && do_pty == 1) {
|
---|
470 | sbappend(so, so->so_m);
|
---|
471 | so->so_m = 0;
|
---|
472 | }
|
---|
473 |
|
---|
474 | return 1;
|
---|
475 | }
|
---|
476 | }
|
---|
477 | #endif
|
---|
478 |
|
---|
479 | #ifndef HAVE_STRDUP
|
---|
480 | char *
|
---|
481 | strdup(str)
|
---|
482 | const char *str;
|
---|
483 | {
|
---|
484 | char *bptr;
|
---|
485 |
|
---|
486 | bptr = (char *)malloc(strlen(str)+1);
|
---|
487 | strcpy(bptr, str);
|
---|
488 |
|
---|
489 | return bptr;
|
---|
490 | }
|
---|
491 | #endif
|
---|
492 |
|
---|
493 | #if 0
|
---|
494 | void
|
---|
495 | snooze_hup(num)
|
---|
496 | int num;
|
---|
497 | {
|
---|
498 | int s, ret;
|
---|
499 | #ifndef NO_UNIX_SOCKETS
|
---|
500 | struct sockaddr_un sock_un;
|
---|
501 | #endif
|
---|
502 | struct sockaddr_in sock_in;
|
---|
503 | char buff[256];
|
---|
504 |
|
---|
505 | ret = -1;
|
---|
506 | if (slirp_socket_passwd) {
|
---|
507 | s = socket(AF_INET, SOCK_STREAM, 0);
|
---|
508 | if (s < 0)
|
---|
509 | slirp_exit(1);
|
---|
510 | sock_in.sin_family = AF_INET;
|
---|
511 | sock_in.sin_addr.s_addr = slirp_socket_addr;
|
---|
512 | sock_in.sin_port = htons(slirp_socket_port);
|
---|
513 | if (connect(s, (struct sockaddr *)&sock_in, sizeof(sock_in)) != 0)
|
---|
514 | slirp_exit(1); /* just exit...*/
|
---|
515 | sprintf(buff, "kill %s:%d", slirp_socket_passwd, slirp_socket_unit);
|
---|
516 | write(s, buff, strlen(buff)+1);
|
---|
517 | }
|
---|
518 | #ifndef NO_UNIX_SOCKETS
|
---|
519 | else {
|
---|
520 | s = socket(AF_UNIX, SOCK_STREAM, 0);
|
---|
521 | if (s < 0)
|
---|
522 | slirp_exit(1);
|
---|
523 | sock_un.sun_family = AF_UNIX;
|
---|
524 | strcpy(sock_un.sun_path, socket_path);
|
---|
525 | if (connect(s, (struct sockaddr *)&sock_un,
|
---|
526 | sizeof(sock_un.sun_family) + sizeof(sock_un.sun_path)) != 0)
|
---|
527 | slirp_exit(1);
|
---|
528 | sprintf(buff, "kill none:%d", slirp_socket_unit);
|
---|
529 | write(s, buff, strlen(buff)+1);
|
---|
530 | }
|
---|
531 | #endif
|
---|
532 | slirp_exit(0);
|
---|
533 | }
|
---|
534 |
|
---|
535 |
|
---|
536 | void
|
---|
537 | snooze()
|
---|
538 | {
|
---|
539 | sigset_t s;
|
---|
540 | int i;
|
---|
541 |
|
---|
542 | /* Don't need our data anymore */
|
---|
543 | /* XXX This makes SunOS barf */
|
---|
544 | /* brk(0); */
|
---|
545 |
|
---|
546 | /* Close all fd's */
|
---|
547 | for (i = 255; i >= 0; i--)
|
---|
548 | close(i);
|
---|
549 |
|
---|
550 | signal(SIGQUIT, slirp_exit);
|
---|
551 | signal(SIGHUP, snooze_hup);
|
---|
552 | sigemptyset(&s);
|
---|
553 |
|
---|
554 | /* Wait for any signal */
|
---|
555 | sigsuspend(&s);
|
---|
556 |
|
---|
557 | /* Just in case ... */
|
---|
558 | exit(255);
|
---|
559 | }
|
---|
560 |
|
---|
561 | void
|
---|
562 | relay(s)
|
---|
563 | int s;
|
---|
564 | {
|
---|
565 | char buf[8192];
|
---|
566 | int n;
|
---|
567 | fd_set readfds;
|
---|
568 | struct ttys *ttyp;
|
---|
569 |
|
---|
570 | /* Don't need our data anymore */
|
---|
571 | /* XXX This makes SunOS barf */
|
---|
572 | /* brk(0); */
|
---|
573 |
|
---|
574 | signal(SIGQUIT, slirp_exit);
|
---|
575 | signal(SIGHUP, slirp_exit);
|
---|
576 | signal(SIGINT, slirp_exit);
|
---|
577 | signal(SIGTERM, slirp_exit);
|
---|
578 |
|
---|
579 | /* Fudge to get term_raw and term_restore to work */
|
---|
580 | if (NULL == (ttyp = tty_attach (0, slirp_tty))) {
|
---|
581 | lprint ("Error: tty_attach failed in misc.c:relay()\r\n");
|
---|
582 | slirp_exit (1);
|
---|
583 | }
|
---|
584 | ttyp->fd = 0;
|
---|
585 | ttyp->flags |= TTY_CTTY;
|
---|
586 | term_raw(ttyp);
|
---|
587 |
|
---|
588 | while (1) {
|
---|
589 | FD_ZERO(&readfds);
|
---|
590 |
|
---|
591 | FD_SET(0, &readfds);
|
---|
592 | FD_SET(s, &readfds);
|
---|
593 |
|
---|
594 | n = select(s+1, &readfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
|
---|
595 |
|
---|
596 | if (n <= 0)
|
---|
597 | slirp_exit(0);
|
---|
598 |
|
---|
599 | if (FD_ISSET(0, &readfds)) {
|
---|
600 | n = read(0, buf, 8192);
|
---|
601 | if (n <= 0)
|
---|
602 | slirp_exit(0);
|
---|
603 | n = writen(s, buf, n);
|
---|
604 | if (n <= 0)
|
---|
605 | slirp_exit(0);
|
---|
606 | }
|
---|
607 |
|
---|
608 | if (FD_ISSET(s, &readfds)) {
|
---|
609 | n = read(s, buf, 8192);
|
---|
610 | if (n <= 0)
|
---|
611 | slirp_exit(0);
|
---|
612 | n = writen(0, buf, n);
|
---|
613 | if (n <= 0)
|
---|
614 | slirp_exit(0);
|
---|
615 | }
|
---|
616 | }
|
---|
617 |
|
---|
618 | /* Just in case.... */
|
---|
619 | exit(1);
|
---|
620 | }
|
---|
621 | #endif
|
---|
622 |
|
---|
623 | #ifndef VBOX
|
---|
624 | int (*lprint_print) _P((void *, const char *, va_list));
|
---|
625 | char *lprint_ptr, *lprint_ptr2, **lprint_arg;
|
---|
626 |
|
---|
627 | void
|
---|
628 | #ifdef __STDC__
|
---|
629 | lprint(const char *format, ...)
|
---|
630 | #else
|
---|
631 | lprint(va_alist) va_dcl
|
---|
632 | #endif
|
---|
633 | {
|
---|
634 | va_list args;
|
---|
635 |
|
---|
636 | #ifdef __STDC__
|
---|
637 | va_start(args, format);
|
---|
638 | #else
|
---|
639 | char *format;
|
---|
640 | va_start(args);
|
---|
641 | format = va_arg(args, char *);
|
---|
642 | #endif
|
---|
643 | #if 0
|
---|
644 | /* If we're printing to an sbuf, make sure there's enough room */
|
---|
645 | /* XXX +100? */
|
---|
646 | if (lprint_sb) {
|
---|
647 | if ((lprint_ptr - lprint_sb->sb_wptr) >=
|
---|
648 | (lprint_sb->sb_datalen - (strlen(format) + 100))) {
|
---|
649 | int deltaw = lprint_sb->sb_wptr - lprint_sb->sb_data;
|
---|
650 | int deltar = lprint_sb->sb_rptr - lprint_sb->sb_data;
|
---|
651 | int deltap = lprint_ptr - lprint_sb->sb_data;
|
---|
652 |
|
---|
653 | lprint_sb->sb_data = (char *)realloc(lprint_sb->sb_data,
|
---|
654 | lprint_sb->sb_datalen + TCP_SNDSPACE);
|
---|
655 |
|
---|
656 | /* Adjust all values */
|
---|
657 | lprint_sb->sb_wptr = lprint_sb->sb_data + deltaw;
|
---|
658 | lprint_sb->sb_rptr = lprint_sb->sb_data + deltar;
|
---|
659 | lprint_ptr = lprint_sb->sb_data + deltap;
|
---|
660 |
|
---|
661 | lprint_sb->sb_datalen += TCP_SNDSPACE;
|
---|
662 | }
|
---|
663 | }
|
---|
664 | #endif
|
---|
665 | if (lprint_print)
|
---|
666 | lprint_ptr += (*lprint_print)(*lprint_arg, format, args);
|
---|
667 |
|
---|
668 | /* Check if they want output to be logged to file as well */
|
---|
669 | if (lfd) {
|
---|
670 | /*
|
---|
671 | * Remove \r's
|
---|
672 | * otherwise you'll get ^M all over the file
|
---|
673 | */
|
---|
674 | int len = strlen(format);
|
---|
675 | char *bptr1, *bptr2;
|
---|
676 |
|
---|
677 | bptr1 = bptr2 = strdup(format);
|
---|
678 |
|
---|
679 | while (len--) {
|
---|
680 | if (*bptr1 == '\r')
|
---|
681 | memcpy(bptr1, bptr1+1, len+1);
|
---|
682 | else
|
---|
683 | bptr1++;
|
---|
684 | }
|
---|
685 | vfprintf(lfd, bptr2, args);
|
---|
686 | free(bptr2);
|
---|
687 | }
|
---|
688 | va_end(args);
|
---|
689 | }
|
---|
690 |
|
---|
691 | void
|
---|
692 | add_emu(buff)
|
---|
693 | char *buff;
|
---|
694 | {
|
---|
695 | u_int lport, fport;
|
---|
696 | u_int8_t tos = 0, emu = 0;
|
---|
697 | char buff1[256], buff2[256], buff4[128];
|
---|
698 | char *buff3 = buff4;
|
---|
699 | struct emu_t *emup;
|
---|
700 | struct socket *so;
|
---|
701 |
|
---|
702 | if (sscanf(buff, "%256s %256s", buff2, buff1) != 2) {
|
---|
703 | lprint("Error: Bad arguments\r\n");
|
---|
704 | return;
|
---|
705 | }
|
---|
706 |
|
---|
707 | if (sscanf(buff1, "%d:%d", &lport, &fport) != 2) {
|
---|
708 | lport = 0;
|
---|
709 | if (sscanf(buff1, "%d", &fport) != 1) {
|
---|
710 | lprint("Error: Bad first argument\r\n");
|
---|
711 | return;
|
---|
712 | }
|
---|
713 | }
|
---|
714 |
|
---|
715 | if (sscanf(buff2, "%128[^:]:%128s", buff1, buff3) != 2) {
|
---|
716 | buff3 = 0;
|
---|
717 | if (sscanf(buff2, "%256s", buff1) != 1) {
|
---|
718 | lprint("Error: Bad second argument\r\n");
|
---|
719 | return;
|
---|
720 | }
|
---|
721 | }
|
---|
722 |
|
---|
723 | if (buff3) {
|
---|
724 | if (strcmp(buff3, "lowdelay") == 0)
|
---|
725 | tos = IPTOS_LOWDELAY;
|
---|
726 | else if (strcmp(buff3, "throughput") == 0)
|
---|
727 | tos = IPTOS_THROUGHPUT;
|
---|
728 | else {
|
---|
729 | lprint("Error: Expecting \"lowdelay\"/\"throughput\"\r\n");
|
---|
730 | return;
|
---|
731 | }
|
---|
732 | }
|
---|
733 |
|
---|
734 | if (strcmp(buff1, "ftp") == 0)
|
---|
735 | emu = EMU_FTP;
|
---|
736 | else if (strcmp(buff1, "irc") == 0)
|
---|
737 | emu = EMU_IRC;
|
---|
738 | else if (strcmp(buff1, "none") == 0)
|
---|
739 | emu = EMU_NONE; /* ie: no emulation */
|
---|
740 | else {
|
---|
741 | lprint("Error: Unknown service\r\n");
|
---|
742 | return;
|
---|
743 | }
|
---|
744 |
|
---|
745 | /* First, check that it isn't already emulated */
|
---|
746 | for (emup = tcpemu; emup; emup = emup->next) {
|
---|
747 | if (emup->lport == lport && emup->fport == fport) {
|
---|
748 | lprint("Error: port already emulated\r\n");
|
---|
749 | return;
|
---|
750 | }
|
---|
751 | }
|
---|
752 |
|
---|
753 | /* link it */
|
---|
754 | emup = (struct emu_t *)malloc(sizeof (struct emu_t));
|
---|
755 | emup->lport = (u_int16_t)lport;
|
---|
756 | emup->fport = (u_int16_t)fport;
|
---|
757 | emup->tos = tos;
|
---|
758 | emup->emu = emu;
|
---|
759 | emup->next = tcpemu;
|
---|
760 | tcpemu = emup;
|
---|
761 |
|
---|
762 | /* And finally, mark all current sessions, if any, as being emulated */
|
---|
763 | for (so = tcb.so_next; so != &tcb; so = so->so_next) {
|
---|
764 | if ((lport && lport == ntohs(so->so_lport)) ||
|
---|
765 | (fport && fport == ntohs(so->so_fport))) {
|
---|
766 | if (emu)
|
---|
767 | so->so_emu = emu;
|
---|
768 | if (tos)
|
---|
769 | so->so_iptos = tos;
|
---|
770 | }
|
---|
771 | }
|
---|
772 |
|
---|
773 | lprint("Adding emulation for %s to port %d/%d\r\n", buff1, emup->lport, emup->fport);
|
---|
774 | }
|
---|
775 | #endif /* !VBOX */
|
---|
776 |
|
---|
777 | #ifdef BAD_SPRINTF
|
---|
778 |
|
---|
779 | #undef vsprintf
|
---|
780 | #undef sprintf
|
---|
781 |
|
---|
782 | /*
|
---|
783 | * Some BSD-derived systems have a sprintf which returns char *
|
---|
784 | */
|
---|
785 |
|
---|
786 | int
|
---|
787 | vsprintf_len(string, format, args)
|
---|
788 | char *string;
|
---|
789 | const char *format;
|
---|
790 | va_list args;
|
---|
791 | {
|
---|
792 | vsprintf(string, format, args);
|
---|
793 | return strlen(string);
|
---|
794 | }
|
---|
795 |
|
---|
796 | int
|
---|
797 | #ifdef __STDC__
|
---|
798 | sprintf_len(char *string, const char *format, ...)
|
---|
799 | #else
|
---|
800 | sprintf_len(va_alist) va_dcl
|
---|
801 | #endif
|
---|
802 | {
|
---|
803 | va_list args;
|
---|
804 | #ifdef __STDC__
|
---|
805 | va_start(args, format);
|
---|
806 | #else
|
---|
807 | char *string;
|
---|
808 | char *format;
|
---|
809 | va_start(args);
|
---|
810 | string = va_arg(args, char *);
|
---|
811 | format = va_arg(args, char *);
|
---|
812 | #endif
|
---|
813 | vsprintf(string, format, args);
|
---|
814 | return strlen(string);
|
---|
815 | }
|
---|
816 |
|
---|
817 | #endif
|
---|
818 |
|
---|
819 | void
|
---|
820 | u_sleep(usec)
|
---|
821 | int usec;
|
---|
822 | {
|
---|
823 | struct timeval t;
|
---|
824 | fd_set fdset;
|
---|
825 |
|
---|
826 | FD_ZERO(&fdset);
|
---|
827 |
|
---|
828 | t.tv_sec = 0;
|
---|
829 | t.tv_usec = usec * 1000;
|
---|
830 |
|
---|
831 | select(0, &fdset, &fdset, &fdset, &t);
|
---|
832 | }
|
---|
833 |
|
---|
834 | /*
|
---|
835 | * Set fd blocking and non-blocking
|
---|
836 | */
|
---|
837 |
|
---|
838 | void
|
---|
839 | fd_nonblock(fd)
|
---|
840 | int fd;
|
---|
841 | {
|
---|
842 | #ifdef FIONBIO
|
---|
843 | int opt = 1;
|
---|
844 |
|
---|
845 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
846 | #else
|
---|
847 | int opt;
|
---|
848 |
|
---|
849 | opt = fcntl(fd, F_GETFL, 0);
|
---|
850 | opt |= O_NONBLOCK;
|
---|
851 | fcntl(fd, F_SETFL, opt);
|
---|
852 | #endif
|
---|
853 | }
|
---|
854 |
|
---|
855 | void
|
---|
856 | fd_block(fd)
|
---|
857 | int fd;
|
---|
858 | {
|
---|
859 | #ifdef FIONBIO
|
---|
860 | int opt = 0;
|
---|
861 |
|
---|
862 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
863 | #else
|
---|
864 | int opt;
|
---|
865 |
|
---|
866 | opt = fcntl(fd, F_GETFL, 0);
|
---|
867 | opt &= ~O_NONBLOCK;
|
---|
868 | fcntl(fd, F_SETFL, opt);
|
---|
869 | #endif
|
---|
870 | }
|
---|
871 |
|
---|
872 |
|
---|
873 | #if 0
|
---|
874 | /*
|
---|
875 | * invoke RSH
|
---|
876 | */
|
---|
877 | int
|
---|
878 | rsh_exec(so,ns, user, host, args)
|
---|
879 | struct socket *so;
|
---|
880 | struct socket *ns;
|
---|
881 | char *user;
|
---|
882 | char *host;
|
---|
883 | char *args;
|
---|
884 | {
|
---|
885 | int fd[2];
|
---|
886 | int fd0[2];
|
---|
887 | int s;
|
---|
888 | char buff[256];
|
---|
889 |
|
---|
890 | DEBUG_CALL("rsh_exec");
|
---|
891 | DEBUG_ARG("so = %lx", (long)so);
|
---|
892 |
|
---|
893 | if (pipe(fd)<0) {
|
---|
894 | lprint("Error: pipe failed: %s\n", strerror(errno));
|
---|
895 | return 0;
|
---|
896 | }
|
---|
897 | /* #ifdef HAVE_SOCKETPAIR */
|
---|
898 | #if 1
|
---|
899 | if (socketpair(PF_UNIX,SOCK_STREAM,0, fd0) == -1) {
|
---|
900 | close(fd[0]);
|
---|
901 | close(fd[1]);
|
---|
902 | lprint("Error: openpty failed: %s\n", strerror(errno));
|
---|
903 | return 0;
|
---|
904 | }
|
---|
905 | #else
|
---|
906 | if (slirp_openpty(&fd0[0], &fd0[1]) == -1) {
|
---|
907 | close(fd[0]);
|
---|
908 | close(fd[1]);
|
---|
909 | lprint("Error: openpty failed: %s\n", strerror(errno));
|
---|
910 | return 0;
|
---|
911 | }
|
---|
912 | #endif
|
---|
913 |
|
---|
914 | switch(fork()) {
|
---|
915 | case -1:
|
---|
916 | lprint("Error: fork failed: %s\n", strerror(errno));
|
---|
917 | close(fd[0]);
|
---|
918 | close(fd[1]);
|
---|
919 | close(fd0[0]);
|
---|
920 | close(fd0[1]);
|
---|
921 | return 0;
|
---|
922 |
|
---|
923 | case 0:
|
---|
924 | close(fd[0]);
|
---|
925 | close(fd0[0]);
|
---|
926 |
|
---|
927 | /* Set the DISPLAY */
|
---|
928 | if (x_port >= 0) {
|
---|
929 | #ifdef HAVE_SETENV
|
---|
930 | sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
|
---|
931 | setenv("DISPLAY", buff, 1);
|
---|
932 | #else
|
---|
933 | sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
|
---|
934 | putenv(buff);
|
---|
935 | #endif
|
---|
936 | }
|
---|
937 |
|
---|
938 | dup2(fd0[1], 0);
|
---|
939 | dup2(fd0[1], 1);
|
---|
940 | dup2(fd[1], 2);
|
---|
941 | for (s = 3; s <= 255; s++)
|
---|
942 | close(s);
|
---|
943 |
|
---|
944 | execlp("rsh","rsh","-l", user, host, args, NULL);
|
---|
945 |
|
---|
946 | /* Ooops, failed, let's tell the user why */
|
---|
947 |
|
---|
948 | sprintf(buff, "Error: execlp of %s failed: %s\n",
|
---|
949 | "rsh", strerror(errno));
|
---|
950 | write(2, buff, strlen(buff)+1);
|
---|
951 | close(0); close(1); close(2); /* XXX */
|
---|
952 | exit(1);
|
---|
953 |
|
---|
954 | default:
|
---|
955 | close(fd[1]);
|
---|
956 | close(fd0[1]);
|
---|
957 | ns->s=fd[0];
|
---|
958 | so->s=fd0[0];
|
---|
959 |
|
---|
960 | return 1;
|
---|
961 | }
|
---|
962 | }
|
---|
963 | #endif
|
---|