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