VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/misc.c@ 1076

Last change on this file since 1076 was 1076, checked in by vboxsync, 18 years ago

Removed tons of ifdef VBOX conditionals to make slirp readable again

  • Property svn:eol-style set to native
File size: 14.5 KB
Line 
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
12#if 0
13int x_port = -1;
14int x_display = 0;
15int x_screen = 0;
16
17int
18show_x(buff, inso)
19 char *buff;
20 struct socket *inso;
21{
22 if (x_port < 0) {
23 lprint("X Redir: X not being redirected.\r\n");
24 } else {
25 lprint("X Redir: In sh/bash/zsh/etc. type: DISPLAY=%s:%d.%d; export DISPLAY\r\n",
26 inet_ntoa(our_addr), x_port, x_screen);
27 lprint("X Redir: In csh/tcsh/etc. type: setenv DISPLAY %s:%d.%d\r\n",
28 inet_ntoa(our_addr), x_port, x_screen);
29 if (x_display)
30 lprint("X Redir: Redirecting to display %d\r\n", x_display);
31 }
32
33 return CFG_OK;
34}
35
36
37/*
38 * XXX Allow more than one X redirection?
39 */
40void
41redir_x(inaddr, start_port, display, screen)
42 u_int32_t inaddr;
43 int start_port;
44 int display;
45 int screen;
46{
47 int i;
48
49 if (x_port >= 0) {
50 lprint("X Redir: X already being redirected.\r\n");
51 show_x(0, 0);
52 } else {
53 for (i = 6001 + (start_port-1); i <= 6100; i++) {
54 if (solisten(htons(i), inaddr, htons(6000 + display), 0)) {
55 /* Success */
56 x_port = i - 6000;
57 x_display = display;
58 x_screen = screen;
59 show_x(0, 0);
60 return;
61 }
62 }
63 lprint("X Redir: Error: Couldn't redirect a port for X. Weird.\r\n");
64 }
65}
66#endif
67
68#ifndef HAVE_INET_ATON
69int
70inet_aton(cp, ia)
71 const char *cp;
72 struct in_addr *ia;
73{
74 u_int32_t addr = inet_addr(cp);
75 if (addr == 0xffffffff)
76 return 0;
77 ia->s_addr = addr;
78 return 1;
79}
80#endif
81
82/*
83 * Get our IP address and put it in our_addr
84 */
85void
86getouraddr(PNATState pData)
87{
88 char buff[256];
89 struct hostent *he = NULL;
90
91 if (gethostname(buff,256) == 0)
92 he = gethostbyname(buff);
93 if (he)
94 our_addr = *(struct in_addr *)he->h_addr;
95 if (our_addr.s_addr == 0)
96 our_addr.s_addr = loopback_addr.s_addr;
97}
98
99#if SIZEOF_CHAR_P == 8
100
101struct quehead_32 {
102 u_int32_t qh_link;
103 u_int32_t qh_rlink;
104};
105
106#ifndef _MSC_VER
107inline
108#endif
109void
110insque_32(PNATState pData, void *a, void *b)
111{
112 register struct quehead_32 *element = (struct quehead_32 *) a;
113 register struct quehead_32 *head = (struct quehead_32 *) b;
114 struct quehead_32 *link = u32_to_ptr(pData, head->qh_link, struct quehead_32 *);
115
116 element->qh_link = head->qh_link;
117 element->qh_rlink = ptr_to_u32(pData, head);
118 Assert(link->qh_rlink == element->qh_rlink);
119 link->qh_rlink = head->qh_link = ptr_to_u32(pData, element);
120}
121
122#ifndef _MSC_VER
123inline
124#endif
125void
126remque_32(PNATState pData, void *a)
127{
128 register struct quehead_32 *element = (struct quehead_32 *) a;
129 struct quehead_32 *link = u32_to_ptr(pData, element->qh_link, struct quehead_32 *);
130 struct quehead_32 *rlink = u32_to_ptr(pData, element->qh_rlink, struct quehead_32 *);
131
132 u32ptr_done(pData, link->qh_rlink, element);
133 link->qh_rlink = element->qh_rlink;
134 rlink->qh_link = element->qh_link;
135 element->qh_rlink = 0;
136}
137
138#endif /* SIZEOF_CHAR_P == 8 */
139
140struct quehead {
141 struct quehead *qh_link;
142 struct quehead *qh_rlink;
143};
144
145#ifdef _MSC_VER
146void
147#else
148inline void
149#endif
150insque(PNATState pData, void *a, void *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#ifdef _MSC_VER
162void
163#else
164inline void
165#endif
166remque(PNATState pData, void *a)
167{
168 register struct quehead *element = (struct quehead *) a;
169 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
170 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
171 element->qh_rlink = NULL;
172 /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
173}
174
175/* #endif */
176
177
178int
179add_exec(ex_ptr, do_pty, exec, addr, port)
180 struct ex_list **ex_ptr;
181 int do_pty;
182 char *exec;
183 int addr;
184 int port;
185{
186 struct ex_list *tmp_ptr;
187
188 /* First, check if the port is "bound" */
189 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
190 if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
191 return -1;
192 }
193
194 tmp_ptr = *ex_ptr;
195 *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
196 (*ex_ptr)->ex_fport = port;
197 (*ex_ptr)->ex_addr = addr;
198 (*ex_ptr)->ex_pty = do_pty;
199 (*ex_ptr)->ex_exec = strdup(exec);
200 (*ex_ptr)->ex_next = tmp_ptr;
201 return 0;
202}
203
204#ifndef HAVE_STRERROR
205
206/*
207 * For systems with no strerror
208 */
209
210extern int sys_nerr;
211extern char *sys_errlist[];
212
213char *
214strerror(error)
215 int error;
216{
217 if (error < sys_nerr)
218 return sys_errlist[error];
219 else
220 return "Unknown error.";
221}
222
223#endif
224
225
226#ifdef _WIN32
227
228int
229fork_exec(PNATState pData, struct socket *so, char *ex, int do_pty)
230{
231 /* not implemented */
232 return 0;
233}
234
235#else
236
237/*
238 * XXX This is ugly
239 * We create and bind a socket, then fork off to another
240 * process, which connects to this socket, after which we
241 * exec the wanted program. If something (strange) happens,
242 * the accept() call could block us forever.
243 *
244 * do_pty = 0 Fork/exec inetd style
245 * do_pty = 1 Fork/exec using slirp.telnetd
246 * do_ptr = 2 Fork/exec using pty
247 */
248int
249fork_exec(PNATState pData, struct socket *so, char *ex, int do_pty)
250{
251 int s;
252 struct sockaddr_in addr;
253 socklen_t addrlen = sizeof(addr);
254 int opt;
255 int master;
256 char *argv[256];
257#if 0
258 char buff[256];
259#endif
260 /* don't want to clobber the original */
261 char *bptr;
262 char *curarg;
263 int c, i, ret;
264
265 DEBUG_CALL("fork_exec");
266 DEBUG_ARG("so = %lx", (long)so);
267 DEBUG_ARG("ex = %lx", (long)ex);
268 DEBUG_ARG("do_pty = %lx", (long)do_pty);
269
270 if (do_pty == 2) {
271 AssertRelease(do_pty != 2);
272 /* shut up gcc */
273 s = 0;
274 master = 0;
275 } else {
276 addr.sin_family = AF_INET;
277 addr.sin_port = 0;
278 addr.sin_addr.s_addr = INADDR_ANY;
279
280 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
281 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
282 listen(s, 1) < 0) {
283 lprint("Error: inet socket: %s\n", strerror(errno));
284 closesocket(s);
285
286 return 0;
287 }
288 }
289
290 switch(fork()) {
291 case -1:
292 lprint("Error: fork failed: %s\n", strerror(errno));
293 close(s);
294 if (do_pty == 2)
295 close(master);
296 return 0;
297
298 case 0:
299 /* Set the DISPLAY */
300 if (do_pty == 2) {
301 (void) close(master);
302#ifdef TIOCSCTTY /* XXXXX */
303 (void) setsid();
304 ioctl(s, TIOCSCTTY, (char *)NULL);
305#endif
306 } else {
307 getsockname(s, (struct sockaddr *)&addr, &addrlen);
308 close(s);
309 /*
310 * Connect to the socket
311 * XXX If any of these fail, we're in trouble!
312 */
313 s = socket(AF_INET, SOCK_STREAM, 0);
314 addr.sin_addr = loopback_addr;
315 do {
316 ret = connect(s, (struct sockaddr *)&addr, addrlen);
317 } while (ret < 0 && errno == EINTR);
318 }
319
320#if 0
321 if (x_port >= 0) {
322#ifdef HAVE_SETENV
323 sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
324 setenv("DISPLAY", buff, 1);
325#else
326 sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
327 putenv(buff);
328#endif
329 }
330#endif
331 dup2(s, 0);
332 dup2(s, 1);
333 dup2(s, 2);
334 for (s = 3; s <= 255; s++)
335 close(s);
336
337 i = 0;
338 bptr = strdup(ex); /* No need to free() this */
339 if (do_pty == 1) {
340 /* Setup "slirp.telnetd -x" */
341 argv[i++] = "slirp.telnetd";
342 argv[i++] = "-x";
343 argv[i++] = bptr;
344 } else
345 do {
346 /* Change the string into argv[] */
347 curarg = bptr;
348 while (*bptr != ' ' && *bptr != (char)0)
349 bptr++;
350 c = *bptr;
351 *bptr++ = (char)0;
352 argv[i++] = strdup(curarg);
353 } while (c);
354
355 argv[i] = 0;
356 execvp(argv[0], argv);
357
358 /* Ooops, failed, let's tell the user why */
359 {
360 char buff[256];
361
362 sprintf(buff, "Error: execvp of %s failed: %s\n",
363 argv[0], strerror(errno));
364 write(2, buff, strlen(buff)+1);
365 }
366 close(0); close(1); close(2); /* XXX */
367 exit(1);
368
369 default:
370 if (do_pty == 2) {
371 close(s);
372 so->s = master;
373 } else {
374 /*
375 * XXX this could block us...
376 * XXX Should set a timer here, and if accept() doesn't
377 * return after X seconds, declare it a failure
378 * The only reason this will block forever is if socket()
379 * of connect() fail in the child process
380 */
381 do {
382 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
383 } while (so->s < 0 && errno == EINTR);
384 closesocket(s);
385 opt = 1;
386 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
387 opt = 1;
388 setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
389 }
390 fd_nonblock(so->s);
391
392 /* Append the telnet options now */
393 if (so->so_m != 0 && do_pty == 1) {
394 sbappend(pData, so, so->so_m);
395 so->so_m = 0;
396 }
397
398 return 1;
399 }
400}
401#endif
402
403#ifndef HAVE_STRDUP
404char *
405strdup(str)
406 const char *str;
407{
408 char *bptr;
409
410 bptr = (char *)malloc(strlen(str)+1);
411 strcpy(bptr, str);
412
413 return bptr;
414}
415#endif
416
417#if 0
418void
419snooze_hup(num)
420 int num;
421{
422 int s, ret;
423#ifndef NO_UNIX_SOCKETS
424 struct sockaddr_un sock_un;
425#endif
426 struct sockaddr_in sock_in;
427 char buff[256];
428
429 ret = -1;
430 if (slirp_socket_passwd) {
431 s = socket(AF_INET, SOCK_STREAM, 0);
432 if (s < 0)
433 slirp_exit(1);
434 sock_in.sin_family = AF_INET;
435 sock_in.sin_addr.s_addr = slirp_socket_addr;
436 sock_in.sin_port = htons(slirp_socket_port);
437 if (connect(s, (struct sockaddr *)&sock_in, sizeof(sock_in)) != 0)
438 slirp_exit(1); /* just exit...*/
439 sprintf(buff, "kill %s:%d", slirp_socket_passwd, slirp_socket_unit);
440 write(s, buff, strlen(buff)+1);
441 }
442#ifndef NO_UNIX_SOCKETS
443 else {
444 s = socket(AF_UNIX, SOCK_STREAM, 0);
445 if (s < 0)
446 slirp_exit(1);
447 sock_un.sun_family = AF_UNIX;
448 strcpy(sock_un.sun_path, socket_path);
449 if (connect(s, (struct sockaddr *)&sock_un,
450 sizeof(sock_un.sun_family) + sizeof(sock_un.sun_path)) != 0)
451 slirp_exit(1);
452 sprintf(buff, "kill none:%d", slirp_socket_unit);
453 write(s, buff, strlen(buff)+1);
454 }
455#endif
456 slirp_exit(0);
457}
458
459
460void
461snooze()
462{
463 sigset_t s;
464 int i;
465
466 /* Don't need our data anymore */
467 /* XXX This makes SunOS barf */
468/* brk(0); */
469
470 /* Close all fd's */
471 for (i = 255; i >= 0; i--)
472 close(i);
473
474 signal(SIGQUIT, slirp_exit);
475 signal(SIGHUP, snooze_hup);
476 sigemptyset(&s);
477
478 /* Wait for any signal */
479 sigsuspend(&s);
480
481 /* Just in case ... */
482 exit(255);
483}
484
485void
486relay(s)
487 int s;
488{
489 char buf[8192];
490 int n;
491 fd_set readfds;
492 struct ttys *ttyp;
493
494 /* Don't need our data anymore */
495 /* XXX This makes SunOS barf */
496/* brk(0); */
497
498 signal(SIGQUIT, slirp_exit);
499 signal(SIGHUP, slirp_exit);
500 signal(SIGINT, slirp_exit);
501 signal(SIGTERM, slirp_exit);
502
503 /* Fudge to get term_raw and term_restore to work */
504 if (NULL == (ttyp = tty_attach (0, slirp_tty))) {
505 lprint ("Error: tty_attach failed in misc.c:relay()\r\n");
506 slirp_exit (1);
507 }
508 ttyp->fd = 0;
509 ttyp->flags |= TTY_CTTY;
510 term_raw(ttyp);
511
512 while (1) {
513 FD_ZERO(&readfds);
514
515 FD_SET(0, &readfds);
516 FD_SET(s, &readfds);
517
518 n = select(s+1, &readfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
519
520 if (n <= 0)
521 slirp_exit(0);
522
523 if (FD_ISSET(0, &readfds)) {
524 n = read(0, buf, 8192);
525 if (n <= 0)
526 slirp_exit(0);
527 n = writen(s, buf, n);
528 if (n <= 0)
529 slirp_exit(0);
530 }
531
532 if (FD_ISSET(s, &readfds)) {
533 n = read(s, buf, 8192);
534 if (n <= 0)
535 slirp_exit(0);
536 n = writen(0, buf, n);
537 if (n <= 0)
538 slirp_exit(0);
539 }
540 }
541
542 /* Just in case.... */
543 exit(1);
544}
545#endif
546
547#ifdef BAD_SPRINTF
548
549#undef vsprintf
550#undef sprintf
551
552/*
553 * Some BSD-derived systems have a sprintf which returns char *
554 */
555
556int
557vsprintf_len(string, format, args)
558 char *string;
559 const char *format;
560 va_list args;
561{
562 vsprintf(string, format, args);
563 return strlen(string);
564}
565
566int
567#ifdef __STDC__
568sprintf_len(char *string, const char *format, ...)
569#else
570sprintf_len(va_alist) va_dcl
571#endif
572{
573 va_list args;
574#ifdef __STDC__
575 va_start(args, format);
576#else
577 char *string;
578 char *format;
579 va_start(args);
580 string = va_arg(args, char *);
581 format = va_arg(args, char *);
582#endif
583 vsprintf(string, format, args);
584 return strlen(string);
585}
586
587#endif
588
589void
590u_sleep(usec)
591 int usec;
592{
593 struct timeval t;
594 fd_set fdset;
595
596 FD_ZERO(&fdset);
597
598 t.tv_sec = 0;
599 t.tv_usec = usec * 1000;
600
601 select(0, &fdset, &fdset, &fdset, &t);
602}
603
604/*
605 * Set fd blocking and non-blocking
606 */
607
608void
609fd_nonblock(fd)
610 int fd;
611{
612#ifdef FIONBIO
613 int opt = 1;
614
615 ioctlsocket(fd, FIONBIO, &opt);
616#else
617 int opt;
618
619 opt = fcntl(fd, F_GETFL, 0);
620 opt |= O_NONBLOCK;
621 fcntl(fd, F_SETFL, opt);
622#endif
623}
624
625void
626fd_block(fd)
627 int fd;
628{
629#ifdef FIONBIO
630 int opt = 0;
631
632 ioctlsocket(fd, FIONBIO, &opt);
633#else
634 int opt;
635
636 opt = fcntl(fd, F_GETFL, 0);
637 opt &= ~O_NONBLOCK;
638 fcntl(fd, F_SETFL, opt);
639#endif
640}
641
642
643#if 0
644/*
645 * invoke RSH
646 */
647int
648rsh_exec(so,ns, user, host, args)
649 struct socket *so;
650 struct socket *ns;
651 char *user;
652 char *host;
653 char *args;
654{
655 int fd[2];
656 int fd0[2];
657 int s;
658 char buff[256];
659
660 DEBUG_CALL("rsh_exec");
661 DEBUG_ARG("so = %lx", (long)so);
662
663 if (pipe(fd)<0) {
664 lprint("Error: pipe failed: %s\n", strerror(errno));
665 return 0;
666 }
667/* #ifdef HAVE_SOCKETPAIR */
668#if 1
669 if (socketpair(PF_UNIX,SOCK_STREAM,0, fd0) == -1) {
670 close(fd[0]);
671 close(fd[1]);
672 lprint("Error: openpty failed: %s\n", strerror(errno));
673 return 0;
674 }
675#else
676 if (slirp_openpty(&fd0[0], &fd0[1]) == -1) {
677 close(fd[0]);
678 close(fd[1]);
679 lprint("Error: openpty failed: %s\n", strerror(errno));
680 return 0;
681 }
682#endif
683
684 switch(fork()) {
685 case -1:
686 lprint("Error: fork failed: %s\n", strerror(errno));
687 close(fd[0]);
688 close(fd[1]);
689 close(fd0[0]);
690 close(fd0[1]);
691 return 0;
692
693 case 0:
694 close(fd[0]);
695 close(fd0[0]);
696
697 /* Set the DISPLAY */
698 if (x_port >= 0) {
699#ifdef HAVE_SETENV
700 sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
701 setenv("DISPLAY", buff, 1);
702#else
703 sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
704 putenv(buff);
705#endif
706 }
707
708 dup2(fd0[1], 0);
709 dup2(fd0[1], 1);
710 dup2(fd[1], 2);
711 for (s = 3; s <= 255; s++)
712 close(s);
713
714 execlp("rsh","rsh","-l", user, host, args, NULL);
715
716 /* Ooops, failed, let's tell the user why */
717
718 sprintf(buff, "Error: execlp of %s failed: %s\n",
719 "rsh", strerror(errno));
720 write(2, buff, strlen(buff)+1);
721 close(0); close(1); close(2); /* XXX */
722 exit(1);
723
724 default:
725 close(fd[1]);
726 close(fd0[1]);
727 ns->s=fd[0];
728 so->s=fd0[0];
729
730 return 1;
731 }
732}
733#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette