Revision 0d62c4cf slirp/misc.c

b/slirp/misc.c
12 12

  
13 13
u_int curtime, time_fasttimo, last_slowtimo;
14 14

  
15
#if 0
16
int x_port = -1;
17
int x_display = 0;
18
int x_screen = 0;
19

  
20
int
21
show_x(buff, inso)
22
	char *buff;
23
	struct socket *inso;
24
{
25
	if (x_port < 0) {
26
		lprint("X Redir: X not being redirected.\r\n");
27
	} else {
28
		lprint("X Redir: In sh/bash/zsh/etc. type: DISPLAY=%s:%d.%d; export DISPLAY\r\n",
29
		      inet_ntoa(our_addr), x_port, x_screen);
30
		lprint("X Redir: In csh/tcsh/etc. type:    setenv DISPLAY %s:%d.%d\r\n",
31
		      inet_ntoa(our_addr), x_port, x_screen);
32
		if (x_display)
33
		   lprint("X Redir: Redirecting to display %d\r\n", x_display);
34
	}
35

  
36
	return CFG_OK;
37
}
38

  
39

  
40
/*
41
 * XXX Allow more than one X redirection?
42
 */
43
void
44
redir_x(inaddr, start_port, display, screen)
45
	u_int32_t inaddr;
46
	int start_port;
47
	int display;
48
	int screen;
49
{
50
	int i;
51

  
52
	if (x_port >= 0) {
53
		lprint("X Redir: X already being redirected.\r\n");
54
		show_x(0, 0);
55
	} else {
56
		for (i = 6001 + (start_port-1); i <= 6100; i++) {
57
			if (solisten(htons(i), inaddr, htons(6000 + display), 0)) {
58
				/* Success */
59
				x_port = i - 6000;
60
				x_display = display;
61
				x_screen = screen;
62
				show_x(0, 0);
63
				return;
64
			}
65
		}
66
		lprint("X Redir: Error: Couldn't redirect a port for X. Weird.\r\n");
67
	}
68
}
69
#endif
70

  
71 15
/*
72 16
 * Get our IP address and put it in our_addr
73 17
 */
......
109 53
  ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
110 54
  ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
111 55
  element->qh_rlink = NULL;
112
  /*  element->qh_link = NULL;  TCP FIN1 crashes if you do this.  Why ? */
113 56
}
114 57

  
115
/* #endif */
116

  
117

  
118 58
int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
119 59
             struct in_addr addr, int port)
120 60
{
......
170 110

  
171 111
#else
172 112

  
173
#ifndef CONFIG_QEMU
174
int
175
slirp_openpty(amaster, aslave)
176
     int *amaster, *aslave;
177
{
178
	register int master, slave;
179

  
180
#ifdef HAVE_GRANTPT
181
	char *ptr;
182

  
183
	if ((master = open("/dev/ptmx", O_RDWR)) < 0 ||
184
	    grantpt(master) < 0 ||
185
	    unlockpt(master) < 0 ||
186
	    (ptr = ptsname(master)) == NULL)  {
187
		close(master);
188
		return -1;
189
	}
190

  
191
	if ((slave = open(ptr, O_RDWR)) < 0 ||
192
	    ioctl(slave, I_PUSH, "ptem") < 0 ||
193
	    ioctl(slave, I_PUSH, "ldterm") < 0 ||
194
	    ioctl(slave, I_PUSH, "ttcompat") < 0) {
195
		close(master);
196
		close(slave);
197
		return -1;
198
	}
199

  
200
	*amaster = master;
201
	*aslave = slave;
202
	return 0;
203

  
204
#else
205

  
206
	static char line[] = "/dev/ptyXX";
207
	register const char *cp1, *cp2;
208

  
209
	for (cp1 = "pqrsPQRS"; *cp1; cp1++) {
210
		line[8] = *cp1;
211
		for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
212
			line[9] = *cp2;
213
			if ((master = open(line, O_RDWR, 0)) == -1) {
214
				if (errno == ENOENT)
215
				   return (-1);    /* out of ptys */
216
			} else {
217
				line[5] = 't';
218
				/* These will fail */
219
				(void) chown(line, getuid(), 0);
220
				(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
221
#ifdef HAVE_REVOKE
222
				(void) revoke(line);
223
#endif
224
				if ((slave = open(line, O_RDWR, 0)) != -1) {
225
					*amaster = master;
226
					*aslave = slave;
227
					return 0;
228
				}
229
				(void) close(master);
230
				line[5] = 'p';
231
			}
232
		}
233
	}
234
	errno = ENOENT; /* out of ptys */
235
	return (-1);
236
#endif
237
}
238
#endif
239

  
240 113
/*
241 114
 * XXX This is ugly
242 115
 * We create and bind a socket, then fork off to another
......
257 130
	int opt;
258 131
        int master = -1;
259 132
	const char *argv[256];
260
#if 0
261
	char buff[256];
262
#endif
263 133
	/* don't want to clobber the original */
264 134
	char *bptr;
265 135
	const char *curarg;
......
271 141
	DEBUG_ARG("do_pty = %lx", (long)do_pty);
272 142

  
273 143
	if (do_pty == 2) {
274
#if 0
275
		if (slirp_openpty(&master, &s) == -1) {
276
			lprint("Error: openpty failed: %s\n", strerror(errno));
277
			return 0;
278
		}
279
#else
280 144
                return 0;
281
#endif
282 145
	} else {
283 146
		addr.sin_family = AF_INET;
284 147
		addr.sin_port = 0;
......
324 187
                        } while (ret < 0 && errno == EINTR);
325 188
		}
326 189

  
327
#if 0
328
		if (x_port >= 0) {
329
#ifdef HAVE_SETENV
330
			sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
331
			setenv("DISPLAY", buff, 1);
332
#else
333
			sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
334
			putenv(buff);
335
#endif
336
		}
337
#endif
338 190
		dup2(s, 0);
339 191
		dup2(s, 1);
340 192
		dup2(s, 2);
......
422 274
}
423 275
#endif
424 276

  
425
#if 0
426
void
427
snooze_hup(num)
428
	int num;
429
{
430
	int s, ret;
431
#ifndef NO_UNIX_SOCKETS
432
	struct sockaddr_un sock_un;
433
#endif
434
	struct sockaddr_in sock_in;
435
	char buff[256];
436

  
437
	ret = -1;
438
	if (slirp_socket_passwd) {
439
		s = socket(AF_INET, SOCK_STREAM, 0);
440
		if (s < 0)
441
		   slirp_exit(1);
442
		sock_in.sin_family = AF_INET;
443
		sock_in.sin_addr.s_addr = slirp_socket_addr;
444
		sock_in.sin_port = htons(slirp_socket_port);
445
		if (connect(s, (struct sockaddr *)&sock_in, sizeof(sock_in)) != 0)
446
		   slirp_exit(1); /* just exit...*/
447
		sprintf(buff, "kill %s:%d", slirp_socket_passwd, slirp_socket_unit);
448
		write(s, buff, strlen(buff)+1);
449
	}
450
#ifndef NO_UNIX_SOCKETS
451
	  else {
452
		s = socket(AF_UNIX, SOCK_STREAM, 0);
453
		if (s < 0)
454
		   slirp_exit(1);
455
		sock_un.sun_family = AF_UNIX;
456
		strcpy(sock_un.sun_path, socket_path);
457
		if (connect(s, (struct sockaddr *)&sock_un,
458
			      sizeof(sock_un.sun_family) + sizeof(sock_un.sun_path)) != 0)
459
		   slirp_exit(1);
460
		sprintf(buff, "kill none:%d", slirp_socket_unit);
461
		write(s, buff, strlen(buff)+1);
462
	}
463
#endif
464
	slirp_exit(0);
465
}
466

  
467

  
468
void
469
snooze()
470
{
471
	sigset_t s;
472
	int i;
473

  
474
	/* Don't need our data anymore */
475
	/* XXX This makes SunOS barf */
476
/*	brk(0); */
477

  
478
	/* Close all fd's */
479
	for (i = 255; i >= 0; i--)
480
	   close(i);
481

  
482
	signal(SIGQUIT, slirp_exit);
483
	signal(SIGHUP, snooze_hup);
484
	sigemptyset(&s);
485

  
486
	/* Wait for any signal */
487
	sigsuspend(&s);
488

  
489
	/* Just in case ... */
490
	exit(255);
491
}
492

  
493
void
494
relay(s)
495
	int s;
496
{
497
	char buf[8192];
498
	int n;
499
	fd_set readfds;
500
	struct ttys *ttyp;
501

  
502
	/* Don't need our data anymore */
503
	/* XXX This makes SunOS barf */
504
/*	brk(0); */
505

  
506
	signal(SIGQUIT, slirp_exit);
507
	signal(SIGHUP, slirp_exit);
508
        signal(SIGINT, slirp_exit);
509
	signal(SIGTERM, slirp_exit);
510

  
511
	/* Fudge to get term_raw and term_restore to work */
512
	if (NULL == (ttyp = tty_attach (0, slirp_tty))) {
513
         lprint ("Error: tty_attach failed in misc.c:relay()\r\n");
514
         slirp_exit (1);
515
    }
516
	ttyp->fd = 0;
517
	ttyp->flags |= TTY_CTTY;
518
	term_raw(ttyp);
519

  
520
	while (1) {
521
		FD_ZERO(&readfds);
522

  
523
		FD_SET(0, &readfds);
524
		FD_SET(s, &readfds);
525

  
526
		n = select(s+1, &readfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
527

  
528
		if (n <= 0)
529
		   slirp_exit(0);
530

  
531
		if (FD_ISSET(0, &readfds)) {
532
			n = read(0, buf, 8192);
533
			if (n <= 0)
534
			   slirp_exit(0);
535
			n = writen(s, buf, n);
536
			if (n <= 0)
537
			   slirp_exit(0);
538
		}
539

  
540
		if (FD_ISSET(s, &readfds)) {
541
			n = read(s, buf, 8192);
542
			if (n <= 0)
543
			   slirp_exit(0);
544
			n = writen(0, buf, n);
545
			if (n <= 0)
546
			   slirp_exit(0);
547
		}
548
	}
549

  
550
	/* Just in case.... */
551
	exit(1);
552
}
553
#endif
554

  
555
#ifdef CONFIG_QEMU
556 277
#include "monitor.h"
557 278

  
558 279
void lprint(const char *format, ...)
......
563 284
    monitor_vprintf(cur_mon, format, args);
564 285
    va_end(args);
565 286
}
566
#else
567
int (*lprint_print) _P((void *, const char *, va_list));
568
char *lprint_ptr, *lprint_ptr2, **lprint_arg;
569

  
570
void
571
#ifdef __STDC__
572
lprint(const char *format, ...)
573
#else
574
lprint(va_alist) va_dcl
575
#endif
576
{
577
	va_list args;
578

  
579
#ifdef __STDC__
580
        va_start(args, format);
581
#else
582
        char *format;
583
        va_start(args);
584
        format = va_arg(args, char *);
585
#endif
586
#if 0
587
	/* If we're printing to an sbuf, make sure there's enough room */
588
	/* XXX +100? */
589
	if (lprint_sb) {
590
		if ((lprint_ptr - lprint_sb->sb_wptr) >=
591
		    (lprint_sb->sb_datalen - (strlen(format) + 100))) {
592
			int deltaw = lprint_sb->sb_wptr - lprint_sb->sb_data;
593
			int deltar = lprint_sb->sb_rptr - lprint_sb->sb_data;
594
			int deltap = lprint_ptr -         lprint_sb->sb_data;
595

  
596
			lprint_sb->sb_data = (char *)realloc(lprint_sb->sb_data,
597
							     lprint_sb->sb_datalen + TCP_SNDSPACE);
598

  
599
			/* Adjust all values */
600
			lprint_sb->sb_wptr = lprint_sb->sb_data + deltaw;
601
			lprint_sb->sb_rptr = lprint_sb->sb_data + deltar;
602
			lprint_ptr =         lprint_sb->sb_data + deltap;
603

  
604
			lprint_sb->sb_datalen += TCP_SNDSPACE;
605
		}
606
	}
607
#endif
608
	if (lprint_print)
609
	   lprint_ptr += (*lprint_print)(*lprint_arg, format, args);
610

  
611
	/* Check if they want output to be logged to file as well */
612
	if (lfd) {
613
		/*
614
		 * Remove \r's
615
		 * otherwise you'll get ^M all over the file
616
		 */
617
		int len = strlen(format);
618
		char *bptr1, *bptr2;
619

  
620
		bptr1 = bptr2 = strdup(format);
621

  
622
		while (len--) {
623
			if (*bptr1 == '\r')
624
			   memcpy(bptr1, bptr1+1, len+1);
625
			else
626
			   bptr1++;
627
		}
628
		vfprintf(lfd, bptr2, args);
629
		free(bptr2);
630
	}
631
	va_end(args);
632
}
633

  
634
void
635
add_emu(buff)
636
	char *buff;
637
{
638
	u_int lport, fport;
639
	u_int8_t tos = 0, emu = 0;
640
	char buff1[256], buff2[256], buff4[128];
641
	char *buff3 = buff4;
642
	struct emu_t *emup;
643
	struct socket *so;
644

  
645
	if (sscanf(buff, "%256s %256s", buff2, buff1) != 2) {
646
		lprint("Error: Bad arguments\r\n");
647
		return;
648
	}
649

  
650
	if (sscanf(buff1, "%d:%d", &lport, &fport) != 2) {
651
		lport = 0;
652
		if (sscanf(buff1, "%d", &fport) != 1) {
653
			lprint("Error: Bad first argument\r\n");
654
			return;
655
		}
656
	}
657

  
658
	if (sscanf(buff2, "%128[^:]:%128s", buff1, buff3) != 2) {
659
		buff3 = 0;
660
		if (sscanf(buff2, "%256s", buff1) != 1) {
661
			lprint("Error: Bad second argument\r\n");
662
			return;
663
		}
664
	}
665

  
666
	if (buff3) {
667
		if (strcmp(buff3, "lowdelay") == 0)
668
		   tos = IPTOS_LOWDELAY;
669
		else if (strcmp(buff3, "throughput") == 0)
670
		   tos = IPTOS_THROUGHPUT;
671
		else {
672
			lprint("Error: Expecting \"lowdelay\"/\"throughput\"\r\n");
673
			return;
674
		}
675
	}
676

  
677
	if (strcmp(buff1, "ftp") == 0)
678
	   emu = EMU_FTP;
679
	else if (strcmp(buff1, "irc") == 0)
680
	   emu = EMU_IRC;
681
	else if (strcmp(buff1, "none") == 0)
682
	   emu = EMU_NONE; /* ie: no emulation */
683
	else {
684
		lprint("Error: Unknown service\r\n");
685
		return;
686
	}
687

  
688
	/* First, check that it isn't already emulated */
689
	for (emup = tcpemu; emup; emup = emup->next) {
690
		if (emup->lport == lport && emup->fport == fport) {
691
			lprint("Error: port already emulated\r\n");
692
			return;
693
		}
694
	}
695

  
696
	/* link it */
697
	emup = (struct emu_t *)malloc(sizeof (struct emu_t));
698
	emup->lport = (u_int16_t)lport;
699
	emup->fport = (u_int16_t)fport;
700
	emup->tos = tos;
701
	emup->emu = emu;
702
	emup->next = tcpemu;
703
	tcpemu = emup;
704

  
705
	/* And finally, mark all current sessions, if any, as being emulated */
706
	for (so = tcb.so_next; so != &tcb; so = so->so_next) {
707
		if ((lport && lport == ntohs(so->so_lport)) ||
708
		    (fport && fport == ntohs(so->so_fport))) {
709
			if (emu)
710
			   so->so_emu = emu;
711
			if (tos)
712
			   so->so_iptos = tos;
713
		}
714
	}
715

  
716
	lprint("Adding emulation for %s to port %d/%d\r\n", buff1, emup->lport, emup->fport);
717
}
718
#endif
719 287

  
720 288
#ifdef BAD_SPRINTF
721 289

  
......
817 385
#endif
818 386
}
819 387

  
820

  
821
#if 0
822
/*
823
 * invoke RSH
824
 */
825
int
826
rsh_exec(so,ns, user, host, args)
827
	struct socket *so;
828
	struct socket *ns;
829
	char *user;
830
	char *host;
831
	char *args;
832
{
833
	int fd[2];
834
	int fd0[2];
835
	int s;
836
	char buff[256];
837

  
838
	DEBUG_CALL("rsh_exec");
839
	DEBUG_ARG("so = %lx", (long)so);
840

  
841
	if (pipe(fd)<0) {
842
          lprint("Error: pipe failed: %s\n", strerror(errno));
843
          return 0;
844
	}
845
/* #ifdef HAVE_SOCKETPAIR */
846
#if 1
847
        if (socketpair(PF_UNIX,SOCK_STREAM,0, fd0) == -1) {
848
          close(fd[0]);
849
          close(fd[1]);
850
          lprint("Error: openpty failed: %s\n", strerror(errno));
851
          return 0;
852
        }
853
#else
854
        if (slirp_openpty(&fd0[0], &fd0[1]) == -1) {
855
          close(fd[0]);
856
          close(fd[1]);
857
          lprint("Error: openpty failed: %s\n", strerror(errno));
858
          return 0;
859
        }
860
#endif
861

  
862
	switch(fork()) {
863
	 case -1:
864
           lprint("Error: fork failed: %s\n", strerror(errno));
865
           close(fd[0]);
866
           close(fd[1]);
867
           close(fd0[0]);
868
           close(fd0[1]);
869
           return 0;
870

  
871
	 case 0:
872
           close(fd[0]);
873
           close(fd0[0]);
874

  
875
		/* Set the DISPLAY */
876
           if (x_port >= 0) {
877
#ifdef HAVE_SETENV
878
             sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
879
             setenv("DISPLAY", buff, 1);
880
#else
881
             sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
882
             putenv(buff);
883
#endif
884
           }
885

  
886
           dup2(fd0[1], 0);
887
           dup2(fd0[1], 1);
888
           dup2(fd[1], 2);
889
           for (s = 3; s <= 255; s++)
890
             close(s);
891

  
892
           execlp("rsh","rsh","-l", user, host, args, NULL);
893

  
894
           /* Ooops, failed, let's tell the user why */
895

  
896
           sprintf(buff, "Error: execlp of %s failed: %s\n",
897
                   "rsh", strerror(errno));
898
           write(2, buff, strlen(buff)+1);
899
           close(0); close(1); close(2); /* XXX */
900
           exit(1);
901

  
902
        default:
903
          close(fd[1]);
904
          close(fd0[1]);
905
          ns->s=fd[0];
906
          so->s=fd0[0];
907

  
908
          return 1;
909
	}
910
}
911
#endif
912

  
913 388
void slirp_connection_info(Monitor *mon)
914 389
{
915 390
    const char * const tcpstates[] = {

Also available in: Unified diff