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