Statistics
| Branch: | Revision:

root / slirp / misc.c @ 9c22a623

History | View | Annotate | Download (18.3 kB)

1 f0cbd3ec bellard
/*
2 f0cbd3ec bellard
 * Copyright (c) 1995 Danny Gasparovski.
3 5fafdf24 ths
 *
4 f0cbd3ec bellard
 * Please read the file COPYRIGHT for the
5 f0cbd3ec bellard
 * terms and conditions of the copyright.
6 f0cbd3ec bellard
 */
7 f0cbd3ec bellard
8 f0cbd3ec bellard
#include <slirp.h>
9 f0cbd3ec bellard
10 9634d903 blueswir1
u_int curtime, time_fasttimo, last_slowtimo;
11 f0cbd3ec bellard
12 f0cbd3ec bellard
#if 0
13 f0cbd3ec bellard
int x_port = -1;
14 f0cbd3ec bellard
int x_display = 0;
15 f0cbd3ec bellard
int x_screen = 0;
16 f0cbd3ec bellard

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

33 f0cbd3ec bellard
        return CFG_OK;
34 f0cbd3ec bellard
}
35 f0cbd3ec bellard

36 f0cbd3ec bellard

37 f0cbd3ec bellard
/*
38 f0cbd3ec bellard
 * XXX Allow more than one X redirection?
39 f0cbd3ec bellard
 */
40 f0cbd3ec bellard
void
41 f0cbd3ec bellard
redir_x(inaddr, start_port, display, screen)
42 f0cbd3ec bellard
        u_int32_t inaddr;
43 f0cbd3ec bellard
        int start_port;
44 f0cbd3ec bellard
        int display;
45 f0cbd3ec bellard
        int screen;
46 f0cbd3ec bellard
{
47 f0cbd3ec bellard
        int i;
48 5fafdf24 ths

49 f0cbd3ec bellard
        if (x_port >= 0) {
50 f0cbd3ec bellard
                lprint("X Redir: X already being redirected.\r\n");
51 f0cbd3ec bellard
                show_x(0, 0);
52 f0cbd3ec bellard
        } else {
53 f0cbd3ec bellard
                for (i = 6001 + (start_port-1); i <= 6100; i++) {
54 f0cbd3ec bellard
                        if (solisten(htons(i), inaddr, htons(6000 + display), 0)) {
55 f0cbd3ec bellard
                                /* Success */
56 f0cbd3ec bellard
                                x_port = i - 6000;
57 f0cbd3ec bellard
                                x_display = display;
58 f0cbd3ec bellard
                                x_screen = screen;
59 f0cbd3ec bellard
                                show_x(0, 0);
60 f0cbd3ec bellard
                                return;
61 f0cbd3ec bellard
                        }
62 f0cbd3ec bellard
                }
63 f0cbd3ec bellard
                lprint("X Redir: Error: Couldn't redirect a port for X. Weird.\r\n");
64 f0cbd3ec bellard
        }
65 f0cbd3ec bellard
}
66 f0cbd3ec bellard
#endif
67 f0cbd3ec bellard
68 f0cbd3ec bellard
/*
69 f0cbd3ec bellard
 * Get our IP address and put it in our_addr
70 f0cbd3ec bellard
 */
71 f0cbd3ec bellard
void
72 511d2b14 blueswir1
getouraddr(void)
73 f0cbd3ec bellard
{
74 f0cbd3ec bellard
        char buff[256];
75 f4e15b4b pbrook
        struct hostent *he = NULL;
76 5fafdf24 ths
77 f4e15b4b pbrook
        if (gethostname(buff,256) == 0)
78 f4e15b4b pbrook
            he = gethostbyname(buff);
79 f4e15b4b pbrook
        if (he)
80 f4e15b4b pbrook
            our_addr = *(struct in_addr *)he->h_addr;
81 8dbca8dd bellard
        if (our_addr.s_addr == 0)
82 8dbca8dd bellard
            our_addr.s_addr = loopback_addr.s_addr;
83 f0cbd3ec bellard
}
84 f0cbd3ec bellard
85 f0cbd3ec bellard
struct quehead {
86 f0cbd3ec bellard
        struct quehead *qh_link;
87 f0cbd3ec bellard
        struct quehead *qh_rlink;
88 f0cbd3ec bellard
};
89 f0cbd3ec bellard
90 f0cbd3ec bellard
inline void
91 511d2b14 blueswir1
insque(void *a, void *b)
92 f0cbd3ec bellard
{
93 f0cbd3ec bellard
        register struct quehead *element = (struct quehead *) a;
94 f0cbd3ec bellard
        register struct quehead *head = (struct quehead *) b;
95 f0cbd3ec bellard
        element->qh_link = head->qh_link;
96 f0cbd3ec bellard
        head->qh_link = (struct quehead *)element;
97 f0cbd3ec bellard
        element->qh_rlink = (struct quehead *)head;
98 f0cbd3ec bellard
        ((struct quehead *)(element->qh_link))->qh_rlink
99 f0cbd3ec bellard
        = (struct quehead *)element;
100 f0cbd3ec bellard
}
101 f0cbd3ec bellard
102 f0cbd3ec bellard
inline void
103 511d2b14 blueswir1
remque(void *a)
104 f0cbd3ec bellard
{
105 f0cbd3ec bellard
  register struct quehead *element = (struct quehead *) a;
106 f0cbd3ec bellard
  ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
107 f0cbd3ec bellard
  ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
108 f0cbd3ec bellard
  element->qh_rlink = NULL;
109 f0cbd3ec bellard
  /*  element->qh_link = NULL;  TCP FIN1 crashes if you do this.  Why ? */
110 f0cbd3ec bellard
}
111 f0cbd3ec bellard
112 f0cbd3ec bellard
/* #endif */
113 f0cbd3ec bellard
114 f0cbd3ec bellard
115 f0cbd3ec bellard
int
116 511d2b14 blueswir1
add_exec(struct ex_list **ex_ptr, int do_pty, char *exec, int addr, int port)
117 f0cbd3ec bellard
{
118 f0cbd3ec bellard
        struct ex_list *tmp_ptr;
119 5fafdf24 ths
120 f0cbd3ec bellard
        /* First, check if the port is "bound" */
121 f0cbd3ec bellard
        for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
122 f0cbd3ec bellard
                if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
123 f0cbd3ec bellard
                   return -1;
124 f0cbd3ec bellard
        }
125 5fafdf24 ths
126 f0cbd3ec bellard
        tmp_ptr = *ex_ptr;
127 f0cbd3ec bellard
        *ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
128 f0cbd3ec bellard
        (*ex_ptr)->ex_fport = port;
129 f0cbd3ec bellard
        (*ex_ptr)->ex_addr = addr;
130 f0cbd3ec bellard
        (*ex_ptr)->ex_pty = do_pty;
131 e1c5a2b3 aliguori
        (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : strdup(exec);
132 f0cbd3ec bellard
        (*ex_ptr)->ex_next = tmp_ptr;
133 f0cbd3ec bellard
        return 0;
134 f0cbd3ec bellard
}
135 f0cbd3ec bellard
136 f0cbd3ec bellard
#ifndef HAVE_STRERROR
137 f0cbd3ec bellard
138 f0cbd3ec bellard
/*
139 f0cbd3ec bellard
 * For systems with no strerror
140 f0cbd3ec bellard
 */
141 f0cbd3ec bellard
142 f0cbd3ec bellard
extern int sys_nerr;
143 f0cbd3ec bellard
extern char *sys_errlist[];
144 f0cbd3ec bellard
145 f0cbd3ec bellard
char *
146 f0cbd3ec bellard
strerror(error)
147 f0cbd3ec bellard
        int error;
148 f0cbd3ec bellard
{
149 f0cbd3ec bellard
        if (error < sys_nerr)
150 f0cbd3ec bellard
           return sys_errlist[error];
151 f0cbd3ec bellard
        else
152 f0cbd3ec bellard
           return "Unknown error.";
153 f0cbd3ec bellard
}
154 f0cbd3ec bellard
155 f0cbd3ec bellard
#endif
156 f0cbd3ec bellard
157 f0cbd3ec bellard
158 a3d4af03 bellard
#ifdef _WIN32
159 a3d4af03 bellard
160 a3d4af03 bellard
int
161 9634d903 blueswir1
fork_exec(struct socket *so, const char *ex, int do_pty)
162 a3d4af03 bellard
{
163 a3d4af03 bellard
    /* not implemented */
164 a3d4af03 bellard
    return 0;
165 a3d4af03 bellard
}
166 a3d4af03 bellard
167 a3d4af03 bellard
#else
168 a3d4af03 bellard
169 9634d903 blueswir1
#ifndef CONFIG_QEMU
170 f0cbd3ec bellard
int
171 f3ff649d bellard
slirp_openpty(amaster, aslave)
172 f3ff649d bellard
     int *amaster, *aslave;
173 f0cbd3ec bellard
{
174 f0cbd3ec bellard
        register int master, slave;
175 f0cbd3ec bellard
176 f0cbd3ec bellard
#ifdef HAVE_GRANTPT
177 f0cbd3ec bellard
        char *ptr;
178 5fafdf24 ths
179 f0cbd3ec bellard
        if ((master = open("/dev/ptmx", O_RDWR)) < 0 ||
180 f0cbd3ec bellard
            grantpt(master) < 0 ||
181 f0cbd3ec bellard
            unlockpt(master) < 0 ||
182 f0cbd3ec bellard
            (ptr = ptsname(master)) == NULL)  {
183 f0cbd3ec bellard
                close(master);
184 f0cbd3ec bellard
                return -1;
185 f0cbd3ec bellard
        }
186 5fafdf24 ths
187 f0cbd3ec bellard
        if ((slave = open(ptr, O_RDWR)) < 0 ||
188 f0cbd3ec bellard
            ioctl(slave, I_PUSH, "ptem") < 0 ||
189 f0cbd3ec bellard
            ioctl(slave, I_PUSH, "ldterm") < 0 ||
190 f0cbd3ec bellard
            ioctl(slave, I_PUSH, "ttcompat") < 0) {
191 f0cbd3ec bellard
                close(master);
192 f0cbd3ec bellard
                close(slave);
193 f0cbd3ec bellard
                return -1;
194 f0cbd3ec bellard
        }
195 5fafdf24 ths
196 f0cbd3ec bellard
        *amaster = master;
197 f0cbd3ec bellard
        *aslave = slave;
198 f0cbd3ec bellard
        return 0;
199 5fafdf24 ths
200 f0cbd3ec bellard
#else
201 5fafdf24 ths
202 f0cbd3ec bellard
        static char line[] = "/dev/ptyXX";
203 f0cbd3ec bellard
        register const char *cp1, *cp2;
204 5fafdf24 ths
205 f0cbd3ec bellard
        for (cp1 = "pqrsPQRS"; *cp1; cp1++) {
206 f0cbd3ec bellard
                line[8] = *cp1;
207 f0cbd3ec bellard
                for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
208 f0cbd3ec bellard
                        line[9] = *cp2;
209 f0cbd3ec bellard
                        if ((master = open(line, O_RDWR, 0)) == -1) {
210 f0cbd3ec bellard
                                if (errno == ENOENT)
211 f0cbd3ec bellard
                                   return (-1);    /* out of ptys */
212 f0cbd3ec bellard
                        } else {
213 f0cbd3ec bellard
                                line[5] = 't';
214 f0cbd3ec bellard
                                /* These will fail */
215 f0cbd3ec bellard
                                (void) chown(line, getuid(), 0);
216 f0cbd3ec bellard
                                (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
217 f0cbd3ec bellard
#ifdef HAVE_REVOKE
218 f0cbd3ec bellard
                                (void) revoke(line);
219 f0cbd3ec bellard
#endif
220 f0cbd3ec bellard
                                if ((slave = open(line, O_RDWR, 0)) != -1) {
221 f0cbd3ec bellard
                                        *amaster = master;
222 f0cbd3ec bellard
                                        *aslave = slave;
223 f0cbd3ec bellard
                                        return 0;
224 f0cbd3ec bellard
                                }
225 f0cbd3ec bellard
                                (void) close(master);
226 f0cbd3ec bellard
                                line[5] = 'p';
227 f0cbd3ec bellard
                        }
228 f0cbd3ec bellard
                }
229 f0cbd3ec bellard
        }
230 f0cbd3ec bellard
        errno = ENOENT; /* out of ptys */
231 f0cbd3ec bellard
        return (-1);
232 f0cbd3ec bellard
#endif
233 f0cbd3ec bellard
}
234 9634d903 blueswir1
#endif
235 f0cbd3ec bellard
236 f0cbd3ec bellard
/*
237 f0cbd3ec bellard
 * XXX This is ugly
238 f0cbd3ec bellard
 * We create and bind a socket, then fork off to another
239 f0cbd3ec bellard
 * process, which connects to this socket, after which we
240 f0cbd3ec bellard
 * exec the wanted program.  If something (strange) happens,
241 f0cbd3ec bellard
 * the accept() call could block us forever.
242 5fafdf24 ths
 *
243 f0cbd3ec bellard
 * do_pty = 0   Fork/exec inetd style
244 f0cbd3ec bellard
 * do_pty = 1   Fork/exec using slirp.telnetd
245 f0cbd3ec bellard
 * do_ptr = 2   Fork/exec using pty
246 f0cbd3ec bellard
 */
247 f0cbd3ec bellard
int
248 9634d903 blueswir1
fork_exec(struct socket *so, const char *ex, int do_pty)
249 f0cbd3ec bellard
{
250 f0cbd3ec bellard
        int s;
251 f0cbd3ec bellard
        struct sockaddr_in addr;
252 242acf3a balrog
        socklen_t addrlen = sizeof(addr);
253 f0cbd3ec bellard
        int opt;
254 9634d903 blueswir1
        int master = -1;
255 7ccfb2eb blueswir1
        const char *argv[256];
256 a3d4af03 bellard
#if 0
257 f0cbd3ec bellard
        char buff[256];
258 a3d4af03 bellard
#endif
259 f0cbd3ec bellard
        /* don't want to clobber the original */
260 f0cbd3ec bellard
        char *bptr;
261 9634d903 blueswir1
        const char *curarg;
262 7b91a172 bellard
        int c, i, ret;
263 5fafdf24 ths
264 f0cbd3ec bellard
        DEBUG_CALL("fork_exec");
265 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
266 f0cbd3ec bellard
        DEBUG_ARG("ex = %lx", (long)ex);
267 f0cbd3ec bellard
        DEBUG_ARG("do_pty = %lx", (long)do_pty);
268 5fafdf24 ths
269 f0cbd3ec bellard
        if (do_pty == 2) {
270 9634d903 blueswir1
#if 0
271 f3ff649d bellard
                if (slirp_openpty(&master, &s) == -1) {
272 f0cbd3ec bellard
                        lprint("Error: openpty failed: %s\n", strerror(errno));
273 f0cbd3ec bellard
                        return 0;
274 f0cbd3ec bellard
                }
275 3f9b2b1f balrog
#else
276 3f9b2b1f balrog
                return 0;
277 9634d903 blueswir1
#endif
278 f0cbd3ec bellard
        } else {
279 f0cbd3ec bellard
                addr.sin_family = AF_INET;
280 f0cbd3ec bellard
                addr.sin_port = 0;
281 f0cbd3ec bellard
                addr.sin_addr.s_addr = INADDR_ANY;
282 3b46e624 ths
283 f0cbd3ec bellard
                if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
284 f0cbd3ec bellard
                    bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
285 f0cbd3ec bellard
                    listen(s, 1) < 0) {
286 f0cbd3ec bellard
                        lprint("Error: inet socket: %s\n", strerror(errno));
287 379ff53d bellard
                        closesocket(s);
288 3b46e624 ths
289 f0cbd3ec bellard
                        return 0;
290 f0cbd3ec bellard
                }
291 f0cbd3ec bellard
        }
292 5fafdf24 ths
293 f0cbd3ec bellard
        switch(fork()) {
294 f0cbd3ec bellard
         case -1:
295 f0cbd3ec bellard
                lprint("Error: fork failed: %s\n", strerror(errno));
296 f0cbd3ec bellard
                close(s);
297 f0cbd3ec bellard
                if (do_pty == 2)
298 f0cbd3ec bellard
                   close(master);
299 f0cbd3ec bellard
                return 0;
300 3b46e624 ths
301 f0cbd3ec bellard
         case 0:
302 f0cbd3ec bellard
                /* Set the DISPLAY */
303 f0cbd3ec bellard
                if (do_pty == 2) {
304 f0cbd3ec bellard
                        (void) close(master);
305 f0cbd3ec bellard
#ifdef TIOCSCTTY /* XXXXX */
306 f0cbd3ec bellard
                        (void) setsid();
307 f0cbd3ec bellard
                        ioctl(s, TIOCSCTTY, (char *)NULL);
308 f0cbd3ec bellard
#endif
309 f0cbd3ec bellard
                } else {
310 f0cbd3ec bellard
                        getsockname(s, (struct sockaddr *)&addr, &addrlen);
311 f0cbd3ec bellard
                        close(s);
312 f0cbd3ec bellard
                        /*
313 f0cbd3ec bellard
                         * Connect to the socket
314 f0cbd3ec bellard
                         * XXX If any of these fail, we're in trouble!
315 f0cbd3ec bellard
                          */
316 f0cbd3ec bellard
                        s = socket(AF_INET, SOCK_STREAM, 0);
317 f0cbd3ec bellard
                        addr.sin_addr = loopback_addr;
318 7b91a172 bellard
                        do {
319 7b91a172 bellard
                            ret = connect(s, (struct sockaddr *)&addr, addrlen);
320 7b91a172 bellard
                        } while (ret < 0 && errno == EINTR);
321 f0cbd3ec bellard
                }
322 3b46e624 ths
323 a3d4af03 bellard
#if 0
324 f0cbd3ec bellard
                if (x_port >= 0) {
325 f0cbd3ec bellard
#ifdef HAVE_SETENV
326 f0cbd3ec bellard
                        sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
327 f0cbd3ec bellard
                        setenv("DISPLAY", buff, 1);
328 f0cbd3ec bellard
#else
329 f0cbd3ec bellard
                        sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
330 f0cbd3ec bellard
                        putenv(buff);
331 f0cbd3ec bellard
#endif
332 f0cbd3ec bellard
                }
333 5fafdf24 ths
#endif
334 f0cbd3ec bellard
                dup2(s, 0);
335 f0cbd3ec bellard
                dup2(s, 1);
336 f0cbd3ec bellard
                dup2(s, 2);
337 9634d903 blueswir1
                for (s = getdtablesize() - 1; s >= 3; s--)
338 f0cbd3ec bellard
                   close(s);
339 3b46e624 ths
340 f0cbd3ec bellard
                i = 0;
341 f0cbd3ec bellard
                bptr = strdup(ex); /* No need to free() this */
342 f0cbd3ec bellard
                if (do_pty == 1) {
343 f0cbd3ec bellard
                        /* Setup "slirp.telnetd -x" */
344 f0cbd3ec bellard
                        argv[i++] = "slirp.telnetd";
345 f0cbd3ec bellard
                        argv[i++] = "-x";
346 f0cbd3ec bellard
                        argv[i++] = bptr;
347 f0cbd3ec bellard
                } else
348 f0cbd3ec bellard
                   do {
349 f0cbd3ec bellard
                        /* Change the string into argv[] */
350 f0cbd3ec bellard
                        curarg = bptr;
351 f0cbd3ec bellard
                        while (*bptr != ' ' && *bptr != (char)0)
352 f0cbd3ec bellard
                           bptr++;
353 f0cbd3ec bellard
                        c = *bptr;
354 f0cbd3ec bellard
                        *bptr++ = (char)0;
355 f0cbd3ec bellard
                        argv[i++] = strdup(curarg);
356 f0cbd3ec bellard
                   } while (c);
357 3b46e624 ths
358 511d2b14 blueswir1
                argv[i] = NULL;
359 7ccfb2eb blueswir1
                execvp(argv[0], (char **)argv);
360 3b46e624 ths
361 f0cbd3ec bellard
                /* Ooops, failed, let's tell the user why */
362 f0cbd3ec bellard
                  {
363 f0cbd3ec bellard
                          char buff[256];
364 3b46e624 ths
365 363a37d5 blueswir1
                          snprintf(buff, sizeof(buff),
366 363a37d5 blueswir1
                                   "Error: execvp of %s failed: %s\n",
367 363a37d5 blueswir1
                                   argv[0], strerror(errno));
368 f0cbd3ec bellard
                          write(2, buff, strlen(buff)+1);
369 f0cbd3ec bellard
                  }
370 f0cbd3ec bellard
                close(0); close(1); close(2); /* XXX */
371 f0cbd3ec bellard
                exit(1);
372 3b46e624 ths
373 f0cbd3ec bellard
         default:
374 f0cbd3ec bellard
                if (do_pty == 2) {
375 f0cbd3ec bellard
                        close(s);
376 f0cbd3ec bellard
                        so->s = master;
377 f0cbd3ec bellard
                } else {
378 f0cbd3ec bellard
                        /*
379 f0cbd3ec bellard
                         * XXX this could block us...
380 f0cbd3ec bellard
                         * XXX Should set a timer here, and if accept() doesn't
381 f0cbd3ec bellard
                          * return after X seconds, declare it a failure
382 f0cbd3ec bellard
                          * The only reason this will block forever is if socket()
383 f0cbd3ec bellard
                          * of connect() fail in the child process
384 f0cbd3ec bellard
                          */
385 7b91a172 bellard
                        do {
386 7b91a172 bellard
                            so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
387 7b91a172 bellard
                        } while (so->s < 0 && errno == EINTR);
388 7b91a172 bellard
                        closesocket(s);
389 f0cbd3ec bellard
                        opt = 1;
390 f0cbd3ec bellard
                        setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
391 f0cbd3ec bellard
                        opt = 1;
392 f0cbd3ec bellard
                        setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
393 f0cbd3ec bellard
                }
394 f0cbd3ec bellard
                fd_nonblock(so->s);
395 3b46e624 ths
396 f0cbd3ec bellard
                /* Append the telnet options now */
397 511d2b14 blueswir1
                if (so->so_m != NULL && do_pty == 1)  {
398 f0cbd3ec bellard
                        sbappend(so, so->so_m);
399 511d2b14 blueswir1
                        so->so_m = NULL;
400 f0cbd3ec bellard
                }
401 3b46e624 ths
402 f0cbd3ec bellard
                return 1;
403 f0cbd3ec bellard
        }
404 f0cbd3ec bellard
}
405 f0cbd3ec bellard
#endif
406 f0cbd3ec bellard
407 f0cbd3ec bellard
#ifndef HAVE_STRDUP
408 f0cbd3ec bellard
char *
409 f0cbd3ec bellard
strdup(str)
410 f0cbd3ec bellard
        const char *str;
411 f0cbd3ec bellard
{
412 f0cbd3ec bellard
        char *bptr;
413 5fafdf24 ths
414 f0cbd3ec bellard
        bptr = (char *)malloc(strlen(str)+1);
415 f0cbd3ec bellard
        strcpy(bptr, str);
416 5fafdf24 ths
417 f0cbd3ec bellard
        return bptr;
418 f0cbd3ec bellard
}
419 f0cbd3ec bellard
#endif
420 f0cbd3ec bellard
421 f0cbd3ec bellard
#if 0
422 f0cbd3ec bellard
void
423 f0cbd3ec bellard
snooze_hup(num)
424 f0cbd3ec bellard
        int num;
425 f0cbd3ec bellard
{
426 f0cbd3ec bellard
        int s, ret;
427 f0cbd3ec bellard
#ifndef NO_UNIX_SOCKETS
428 f0cbd3ec bellard
        struct sockaddr_un sock_un;
429 f0cbd3ec bellard
#endif
430 f0cbd3ec bellard
        struct sockaddr_in sock_in;
431 f0cbd3ec bellard
        char buff[256];
432 5fafdf24 ths
433 f0cbd3ec bellard
        ret = -1;
434 f0cbd3ec bellard
        if (slirp_socket_passwd) {
435 f0cbd3ec bellard
                s = socket(AF_INET, SOCK_STREAM, 0);
436 f0cbd3ec bellard
                if (s < 0)
437 f0cbd3ec bellard
                   slirp_exit(1);
438 f0cbd3ec bellard
                sock_in.sin_family = AF_INET;
439 f0cbd3ec bellard
                sock_in.sin_addr.s_addr = slirp_socket_addr;
440 f0cbd3ec bellard
                sock_in.sin_port = htons(slirp_socket_port);
441 f0cbd3ec bellard
                if (connect(s, (struct sockaddr *)&sock_in, sizeof(sock_in)) != 0)
442 f0cbd3ec bellard
                   slirp_exit(1); /* just exit...*/
443 f0cbd3ec bellard
                sprintf(buff, "kill %s:%d", slirp_socket_passwd, slirp_socket_unit);
444 f0cbd3ec bellard
                write(s, buff, strlen(buff)+1);
445 f0cbd3ec bellard
        }
446 f0cbd3ec bellard
#ifndef NO_UNIX_SOCKETS
447 f0cbd3ec bellard
          else {
448 f0cbd3ec bellard
                s = socket(AF_UNIX, SOCK_STREAM, 0);
449 f0cbd3ec bellard
                if (s < 0)
450 f0cbd3ec bellard
                   slirp_exit(1);
451 f0cbd3ec bellard
                sock_un.sun_family = AF_UNIX;
452 f0cbd3ec bellard
                strcpy(sock_un.sun_path, socket_path);
453 f0cbd3ec bellard
                if (connect(s, (struct sockaddr *)&sock_un,
454 f0cbd3ec bellard
                              sizeof(sock_un.sun_family) + sizeof(sock_un.sun_path)) != 0)
455 f0cbd3ec bellard
                   slirp_exit(1);
456 f0cbd3ec bellard
                sprintf(buff, "kill none:%d", slirp_socket_unit);
457 f0cbd3ec bellard
                write(s, buff, strlen(buff)+1);
458 f0cbd3ec bellard
        }
459 f0cbd3ec bellard
#endif
460 f0cbd3ec bellard
        slirp_exit(0);
461 f0cbd3ec bellard
}
462 5fafdf24 ths
463 5fafdf24 ths
464 f0cbd3ec bellard
void
465 f0cbd3ec bellard
snooze()
466 f0cbd3ec bellard
{
467 f0cbd3ec bellard
        sigset_t s;
468 f0cbd3ec bellard
        int i;
469 5fafdf24 ths
470 f0cbd3ec bellard
        /* Don't need our data anymore */
471 f0cbd3ec bellard
        /* XXX This makes SunOS barf */
472 f0cbd3ec bellard
/*        brk(0); */
473 5fafdf24 ths
474 f0cbd3ec bellard
        /* Close all fd's */
475 f0cbd3ec bellard
        for (i = 255; i >= 0; i--)
476 f0cbd3ec bellard
           close(i);
477 5fafdf24 ths
478 f0cbd3ec bellard
        signal(SIGQUIT, slirp_exit);
479 f0cbd3ec bellard
        signal(SIGHUP, snooze_hup);
480 f0cbd3ec bellard
        sigemptyset(&s);
481 5fafdf24 ths
482 f0cbd3ec bellard
        /* Wait for any signal */
483 f0cbd3ec bellard
        sigsuspend(&s);
484 5fafdf24 ths
485 f0cbd3ec bellard
        /* Just in case ... */
486 f0cbd3ec bellard
        exit(255);
487 f0cbd3ec bellard
}
488 f0cbd3ec bellard
489 f0cbd3ec bellard
void
490 f0cbd3ec bellard
relay(s)
491 f0cbd3ec bellard
        int s;
492 f0cbd3ec bellard
{
493 f0cbd3ec bellard
        char buf[8192];
494 f0cbd3ec bellard
        int n;
495 f0cbd3ec bellard
        fd_set readfds;
496 f0cbd3ec bellard
        struct ttys *ttyp;
497 5fafdf24 ths
498 f0cbd3ec bellard
        /* Don't need our data anymore */
499 f0cbd3ec bellard
        /* XXX This makes SunOS barf */
500 f0cbd3ec bellard
/*        brk(0); */
501 5fafdf24 ths
502 f0cbd3ec bellard
        signal(SIGQUIT, slirp_exit);
503 f0cbd3ec bellard
        signal(SIGHUP, slirp_exit);
504 f0cbd3ec bellard
        signal(SIGINT, slirp_exit);
505 f0cbd3ec bellard
        signal(SIGTERM, slirp_exit);
506 5fafdf24 ths
507 f0cbd3ec bellard
        /* Fudge to get term_raw and term_restore to work */
508 f0cbd3ec bellard
        if (NULL == (ttyp = tty_attach (0, slirp_tty))) {
509 f0cbd3ec bellard
         lprint ("Error: tty_attach failed in misc.c:relay()\r\n");
510 f0cbd3ec bellard
         slirp_exit (1);
511 f0cbd3ec bellard
    }
512 f0cbd3ec bellard
        ttyp->fd = 0;
513 f0cbd3ec bellard
        ttyp->flags |= TTY_CTTY;
514 f0cbd3ec bellard
        term_raw(ttyp);
515 5fafdf24 ths
516 f0cbd3ec bellard
        while (1) {
517 f0cbd3ec bellard
                FD_ZERO(&readfds);
518 3b46e624 ths
519 f0cbd3ec bellard
                FD_SET(0, &readfds);
520 f0cbd3ec bellard
                FD_SET(s, &readfds);
521 3b46e624 ths
522 f0cbd3ec bellard
                n = select(s+1, &readfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
523 3b46e624 ths
524 f0cbd3ec bellard
                if (n <= 0)
525 f0cbd3ec bellard
                   slirp_exit(0);
526 3b46e624 ths
527 f0cbd3ec bellard
                if (FD_ISSET(0, &readfds)) {
528 f0cbd3ec bellard
                        n = read(0, buf, 8192);
529 f0cbd3ec bellard
                        if (n <= 0)
530 f0cbd3ec bellard
                           slirp_exit(0);
531 f0cbd3ec bellard
                        n = writen(s, buf, n);
532 f0cbd3ec bellard
                        if (n <= 0)
533 f0cbd3ec bellard
                           slirp_exit(0);
534 f0cbd3ec bellard
                }
535 3b46e624 ths
536 f0cbd3ec bellard
                if (FD_ISSET(s, &readfds)) {
537 f0cbd3ec bellard
                        n = read(s, buf, 8192);
538 f0cbd3ec bellard
                        if (n <= 0)
539 f0cbd3ec bellard
                           slirp_exit(0);
540 f0cbd3ec bellard
                        n = writen(0, buf, n);
541 f0cbd3ec bellard
                        if (n <= 0)
542 f0cbd3ec bellard
                           slirp_exit(0);
543 f0cbd3ec bellard
                }
544 f0cbd3ec bellard
        }
545 5fafdf24 ths
546 f0cbd3ec bellard
        /* Just in case.... */
547 f0cbd3ec bellard
        exit(1);
548 f0cbd3ec bellard
}
549 f0cbd3ec bellard
#endif
550 f0cbd3ec bellard
551 31a60e22 blueswir1
#ifdef CONFIG_QEMU
552 376253ec aliguori
#include "monitor.h"
553 c9f10306 ths
554 31a60e22 blueswir1
void lprint(const char *format, ...)
555 31a60e22 blueswir1
{
556 31a60e22 blueswir1
    va_list args;
557 31a60e22 blueswir1
558 31a60e22 blueswir1
    va_start(args, format);
559 376253ec aliguori
    monitor_vprintf(cur_mon, format, args);
560 31a60e22 blueswir1
    va_end(args);
561 31a60e22 blueswir1
}
562 31a60e22 blueswir1
#else
563 f0cbd3ec bellard
int (*lprint_print) _P((void *, const char *, va_list));
564 f0cbd3ec bellard
char *lprint_ptr, *lprint_ptr2, **lprint_arg;
565 f0cbd3ec bellard
566 f0cbd3ec bellard
void
567 f0cbd3ec bellard
#ifdef __STDC__
568 f0cbd3ec bellard
lprint(const char *format, ...)
569 f0cbd3ec bellard
#else
570 f0cbd3ec bellard
lprint(va_alist) va_dcl
571 f0cbd3ec bellard
#endif
572 f0cbd3ec bellard
{
573 f0cbd3ec bellard
        va_list args;
574 3b46e624 ths
575 f0cbd3ec bellard
#ifdef __STDC__
576 f0cbd3ec bellard
        va_start(args, format);
577 f0cbd3ec bellard
#else
578 f0cbd3ec bellard
        char *format;
579 f0cbd3ec bellard
        va_start(args);
580 f0cbd3ec bellard
        format = va_arg(args, char *);
581 f0cbd3ec bellard
#endif
582 f0cbd3ec bellard
#if 0
583 f0cbd3ec bellard
        /* If we're printing to an sbuf, make sure there's enough room */
584 f0cbd3ec bellard
        /* XXX +100? */
585 f0cbd3ec bellard
        if (lprint_sb) {
586 f0cbd3ec bellard
                if ((lprint_ptr - lprint_sb->sb_wptr) >=
587 f0cbd3ec bellard
                    (lprint_sb->sb_datalen - (strlen(format) + 100))) {
588 f0cbd3ec bellard
                        int deltaw = lprint_sb->sb_wptr - lprint_sb->sb_data;
589 f0cbd3ec bellard
                        int deltar = lprint_sb->sb_rptr - lprint_sb->sb_data;
590 f0cbd3ec bellard
                        int deltap = lprint_ptr -         lprint_sb->sb_data;
591 3b46e624 ths

592 f0cbd3ec bellard
                        lprint_sb->sb_data = (char *)realloc(lprint_sb->sb_data,
593 f0cbd3ec bellard
                                                             lprint_sb->sb_datalen + TCP_SNDSPACE);
594 3b46e624 ths

595 f0cbd3ec bellard
                        /* Adjust all values */
596 f0cbd3ec bellard
                        lprint_sb->sb_wptr = lprint_sb->sb_data + deltaw;
597 f0cbd3ec bellard
                        lprint_sb->sb_rptr = lprint_sb->sb_data + deltar;
598 f0cbd3ec bellard
                        lprint_ptr =         lprint_sb->sb_data + deltap;
599 3b46e624 ths

600 f0cbd3ec bellard
                        lprint_sb->sb_datalen += TCP_SNDSPACE;
601 f0cbd3ec bellard
                }
602 f0cbd3ec bellard
        }
603 5fafdf24 ths
#endif
604 f0cbd3ec bellard
        if (lprint_print)
605 f0cbd3ec bellard
           lprint_ptr += (*lprint_print)(*lprint_arg, format, args);
606 5fafdf24 ths
607 f0cbd3ec bellard
        /* Check if they want output to be logged to file as well */
608 f0cbd3ec bellard
        if (lfd) {
609 5fafdf24 ths
                /*
610 f0cbd3ec bellard
                 * Remove \r's
611 f0cbd3ec bellard
                 * otherwise you'll get ^M all over the file
612 f0cbd3ec bellard
                 */
613 f0cbd3ec bellard
                int len = strlen(format);
614 f0cbd3ec bellard
                char *bptr1, *bptr2;
615 3b46e624 ths
616 f0cbd3ec bellard
                bptr1 = bptr2 = strdup(format);
617 3b46e624 ths
618 f0cbd3ec bellard
                while (len--) {
619 f0cbd3ec bellard
                        if (*bptr1 == '\r')
620 f0cbd3ec bellard
                           memcpy(bptr1, bptr1+1, len+1);
621 f0cbd3ec bellard
                        else
622 f0cbd3ec bellard
                           bptr1++;
623 f0cbd3ec bellard
                }
624 f0cbd3ec bellard
                vfprintf(lfd, bptr2, args);
625 f0cbd3ec bellard
                free(bptr2);
626 f0cbd3ec bellard
        }
627 f0cbd3ec bellard
        va_end(args);
628 f0cbd3ec bellard
}
629 f0cbd3ec bellard
630 f0cbd3ec bellard
void
631 f0cbd3ec bellard
add_emu(buff)
632 f0cbd3ec bellard
        char *buff;
633 f0cbd3ec bellard
{
634 f0cbd3ec bellard
        u_int lport, fport;
635 f0cbd3ec bellard
        u_int8_t tos = 0, emu = 0;
636 f0cbd3ec bellard
        char buff1[256], buff2[256], buff4[128];
637 f0cbd3ec bellard
        char *buff3 = buff4;
638 f0cbd3ec bellard
        struct emu_t *emup;
639 f0cbd3ec bellard
        struct socket *so;
640 5fafdf24 ths
641 f0cbd3ec bellard
        if (sscanf(buff, "%256s %256s", buff2, buff1) != 2) {
642 f0cbd3ec bellard
                lprint("Error: Bad arguments\r\n");
643 f0cbd3ec bellard
                return;
644 f0cbd3ec bellard
        }
645 5fafdf24 ths
646 f0cbd3ec bellard
        if (sscanf(buff1, "%d:%d", &lport, &fport) != 2) {
647 f0cbd3ec bellard
                lport = 0;
648 f0cbd3ec bellard
                if (sscanf(buff1, "%d", &fport) != 1) {
649 f0cbd3ec bellard
                        lprint("Error: Bad first argument\r\n");
650 f0cbd3ec bellard
                        return;
651 f0cbd3ec bellard
                }
652 f0cbd3ec bellard
        }
653 5fafdf24 ths
654 f0cbd3ec bellard
        if (sscanf(buff2, "%128[^:]:%128s", buff1, buff3) != 2) {
655 f0cbd3ec bellard
                buff3 = 0;
656 f0cbd3ec bellard
                if (sscanf(buff2, "%256s", buff1) != 1) {
657 f0cbd3ec bellard
                        lprint("Error: Bad second argument\r\n");
658 f0cbd3ec bellard
                        return;
659 f0cbd3ec bellard
                }
660 f0cbd3ec bellard
        }
661 5fafdf24 ths
662 f0cbd3ec bellard
        if (buff3) {
663 f0cbd3ec bellard
                if (strcmp(buff3, "lowdelay") == 0)
664 f0cbd3ec bellard
                   tos = IPTOS_LOWDELAY;
665 f0cbd3ec bellard
                else if (strcmp(buff3, "throughput") == 0)
666 f0cbd3ec bellard
                   tos = IPTOS_THROUGHPUT;
667 f0cbd3ec bellard
                else {
668 f0cbd3ec bellard
                        lprint("Error: Expecting \"lowdelay\"/\"throughput\"\r\n");
669 f0cbd3ec bellard
                        return;
670 f0cbd3ec bellard
                }
671 f0cbd3ec bellard
        }
672 5fafdf24 ths
673 f0cbd3ec bellard
        if (strcmp(buff1, "ftp") == 0)
674 f0cbd3ec bellard
           emu = EMU_FTP;
675 f0cbd3ec bellard
        else if (strcmp(buff1, "irc") == 0)
676 f0cbd3ec bellard
           emu = EMU_IRC;
677 f0cbd3ec bellard
        else if (strcmp(buff1, "none") == 0)
678 f0cbd3ec bellard
           emu = EMU_NONE; /* ie: no emulation */
679 f0cbd3ec bellard
        else {
680 f0cbd3ec bellard
                lprint("Error: Unknown service\r\n");
681 f0cbd3ec bellard
                return;
682 f0cbd3ec bellard
        }
683 5fafdf24 ths
684 f0cbd3ec bellard
        /* First, check that it isn't already emulated */
685 f0cbd3ec bellard
        for (emup = tcpemu; emup; emup = emup->next) {
686 f0cbd3ec bellard
                if (emup->lport == lport && emup->fport == fport) {
687 f0cbd3ec bellard
                        lprint("Error: port already emulated\r\n");
688 f0cbd3ec bellard
                        return;
689 f0cbd3ec bellard
                }
690 f0cbd3ec bellard
        }
691 5fafdf24 ths
692 f0cbd3ec bellard
        /* link it */
693 f0cbd3ec bellard
        emup = (struct emu_t *)malloc(sizeof (struct emu_t));
694 f0cbd3ec bellard
        emup->lport = (u_int16_t)lport;
695 f0cbd3ec bellard
        emup->fport = (u_int16_t)fport;
696 f0cbd3ec bellard
        emup->tos = tos;
697 f0cbd3ec bellard
        emup->emu = emu;
698 f0cbd3ec bellard
        emup->next = tcpemu;
699 f0cbd3ec bellard
        tcpemu = emup;
700 5fafdf24 ths
701 f0cbd3ec bellard
        /* And finally, mark all current sessions, if any, as being emulated */
702 f0cbd3ec bellard
        for (so = tcb.so_next; so != &tcb; so = so->so_next) {
703 f0cbd3ec bellard
                if ((lport && lport == ntohs(so->so_lport)) ||
704 f0cbd3ec bellard
                    (fport && fport == ntohs(so->so_fport))) {
705 f0cbd3ec bellard
                        if (emu)
706 f0cbd3ec bellard
                           so->so_emu = emu;
707 f0cbd3ec bellard
                        if (tos)
708 f0cbd3ec bellard
                           so->so_iptos = tos;
709 f0cbd3ec bellard
                }
710 f0cbd3ec bellard
        }
711 5fafdf24 ths
712 f0cbd3ec bellard
        lprint("Adding emulation for %s to port %d/%d\r\n", buff1, emup->lport, emup->fport);
713 f0cbd3ec bellard
}
714 31a60e22 blueswir1
#endif
715 f0cbd3ec bellard
716 f0cbd3ec bellard
#ifdef BAD_SPRINTF
717 f0cbd3ec bellard
718 f0cbd3ec bellard
#undef vsprintf
719 f0cbd3ec bellard
#undef sprintf
720 f0cbd3ec bellard
721 f0cbd3ec bellard
/*
722 f0cbd3ec bellard
 * Some BSD-derived systems have a sprintf which returns char *
723 f0cbd3ec bellard
 */
724 f0cbd3ec bellard
725 f0cbd3ec bellard
int
726 f0cbd3ec bellard
vsprintf_len(string, format, args)
727 f0cbd3ec bellard
        char *string;
728 f0cbd3ec bellard
        const char *format;
729 f0cbd3ec bellard
        va_list args;
730 f0cbd3ec bellard
{
731 f0cbd3ec bellard
        vsprintf(string, format, args);
732 f0cbd3ec bellard
        return strlen(string);
733 f0cbd3ec bellard
}
734 f0cbd3ec bellard
735 f0cbd3ec bellard
int
736 f0cbd3ec bellard
#ifdef __STDC__
737 f0cbd3ec bellard
sprintf_len(char *string, const char *format, ...)
738 f0cbd3ec bellard
#else
739 f0cbd3ec bellard
sprintf_len(va_alist) va_dcl
740 f0cbd3ec bellard
#endif
741 f0cbd3ec bellard
{
742 f0cbd3ec bellard
        va_list args;
743 f0cbd3ec bellard
#ifdef __STDC__
744 f0cbd3ec bellard
        va_start(args, format);
745 f0cbd3ec bellard
#else
746 f0cbd3ec bellard
        char *string;
747 f0cbd3ec bellard
        char *format;
748 f0cbd3ec bellard
        va_start(args);
749 f0cbd3ec bellard
        string = va_arg(args, char *);
750 f0cbd3ec bellard
        format = va_arg(args, char *);
751 f0cbd3ec bellard
#endif
752 f0cbd3ec bellard
        vsprintf(string, format, args);
753 f0cbd3ec bellard
        return strlen(string);
754 f0cbd3ec bellard
}
755 f0cbd3ec bellard
756 f0cbd3ec bellard
#endif
757 f0cbd3ec bellard
758 f0cbd3ec bellard
void
759 511d2b14 blueswir1
u_sleep(int usec)
760 f0cbd3ec bellard
{
761 f0cbd3ec bellard
        struct timeval t;
762 f0cbd3ec bellard
        fd_set fdset;
763 5fafdf24 ths
764 f0cbd3ec bellard
        FD_ZERO(&fdset);
765 5fafdf24 ths
766 f0cbd3ec bellard
        t.tv_sec = 0;
767 f0cbd3ec bellard
        t.tv_usec = usec * 1000;
768 5fafdf24 ths
769 f0cbd3ec bellard
        select(0, &fdset, &fdset, &fdset, &t);
770 f0cbd3ec bellard
}
771 f0cbd3ec bellard
772 f0cbd3ec bellard
/*
773 f0cbd3ec bellard
 * Set fd blocking and non-blocking
774 f0cbd3ec bellard
 */
775 f0cbd3ec bellard
776 f0cbd3ec bellard
void
777 511d2b14 blueswir1
fd_nonblock(int fd)
778 f0cbd3ec bellard
{
779 f0cbd3ec bellard
#ifdef FIONBIO
780 b9e82a59 blueswir1
#ifdef _WIN32
781 b9e82a59 blueswir1
        long opt = 1;
782 b9e82a59 blueswir1
#else
783 b9e82a59 blueswir1
        int opt = 1;
784 b9e82a59 blueswir1
#endif
785 5fafdf24 ths
786 379ff53d bellard
        ioctlsocket(fd, FIONBIO, &opt);
787 f0cbd3ec bellard
#else
788 f0cbd3ec bellard
        int opt;
789 5fafdf24 ths
790 f0cbd3ec bellard
        opt = fcntl(fd, F_GETFL, 0);
791 f0cbd3ec bellard
        opt |= O_NONBLOCK;
792 f0cbd3ec bellard
        fcntl(fd, F_SETFL, opt);
793 f0cbd3ec bellard
#endif
794 f0cbd3ec bellard
}
795 f0cbd3ec bellard
796 f0cbd3ec bellard
void
797 511d2b14 blueswir1
fd_block(int fd)
798 f0cbd3ec bellard
{
799 f0cbd3ec bellard
#ifdef FIONBIO
800 f0cbd3ec bellard
        int opt = 0;
801 5fafdf24 ths
802 379ff53d bellard
        ioctlsocket(fd, FIONBIO, &opt);
803 f0cbd3ec bellard
#else
804 f0cbd3ec bellard
        int opt;
805 5fafdf24 ths
806 f0cbd3ec bellard
        opt = fcntl(fd, F_GETFL, 0);
807 f0cbd3ec bellard
        opt &= ~O_NONBLOCK;
808 f0cbd3ec bellard
        fcntl(fd, F_SETFL, opt);
809 f0cbd3ec bellard
#endif
810 f0cbd3ec bellard
}
811 f0cbd3ec bellard
812 f0cbd3ec bellard
813 f0cbd3ec bellard
#if 0
814 f0cbd3ec bellard
/*
815 f0cbd3ec bellard
 * invoke RSH
816 f0cbd3ec bellard
 */
817 f0cbd3ec bellard
int
818 f0cbd3ec bellard
rsh_exec(so,ns, user, host, args)
819 f0cbd3ec bellard
        struct socket *so;
820 f0cbd3ec bellard
        struct socket *ns;
821 f0cbd3ec bellard
        char *user;
822 f0cbd3ec bellard
        char *host;
823 f0cbd3ec bellard
        char *args;
824 f0cbd3ec bellard
{
825 f0cbd3ec bellard
        int fd[2];
826 f0cbd3ec bellard
        int fd0[2];
827 f0cbd3ec bellard
        int s;
828 f0cbd3ec bellard
        char buff[256];
829 5fafdf24 ths

830 f0cbd3ec bellard
        DEBUG_CALL("rsh_exec");
831 f0cbd3ec bellard
        DEBUG_ARG("so = %lx", (long)so);
832 5fafdf24 ths

833 f0cbd3ec bellard
        if (pipe(fd)<0) {
834 f0cbd3ec bellard
          lprint("Error: pipe failed: %s\n", strerror(errno));
835 f0cbd3ec bellard
          return 0;
836 f0cbd3ec bellard
        }
837 f0cbd3ec bellard
/* #ifdef HAVE_SOCKETPAIR */
838 f0cbd3ec bellard
#if 1
839 f0cbd3ec bellard
        if (socketpair(PF_UNIX,SOCK_STREAM,0, fd0) == -1) {
840 f0cbd3ec bellard
          close(fd[0]);
841 f0cbd3ec bellard
          close(fd[1]);
842 f0cbd3ec bellard
          lprint("Error: openpty failed: %s\n", strerror(errno));
843 f0cbd3ec bellard
          return 0;
844 f0cbd3ec bellard
        }
845 f0cbd3ec bellard
#else
846 f3ff649d bellard
        if (slirp_openpty(&fd0[0], &fd0[1]) == -1) {
847 f0cbd3ec bellard
          close(fd[0]);
848 f0cbd3ec bellard
          close(fd[1]);
849 f0cbd3ec bellard
          lprint("Error: openpty failed: %s\n", strerror(errno));
850 f0cbd3ec bellard
          return 0;
851 f0cbd3ec bellard
        }
852 f0cbd3ec bellard
#endif
853 5fafdf24 ths
854 f0cbd3ec bellard
        switch(fork()) {
855 f0cbd3ec bellard
         case -1:
856 f0cbd3ec bellard
           lprint("Error: fork failed: %s\n", strerror(errno));
857 f0cbd3ec bellard
           close(fd[0]);
858 f0cbd3ec bellard
           close(fd[1]);
859 f0cbd3ec bellard
           close(fd0[0]);
860 f0cbd3ec bellard
           close(fd0[1]);
861 f0cbd3ec bellard
           return 0;
862 3b46e624 ths
863 f0cbd3ec bellard
         case 0:
864 f0cbd3ec bellard
           close(fd[0]);
865 f0cbd3ec bellard
           close(fd0[0]);
866 3b46e624 ths
867 f0cbd3ec bellard
                /* Set the DISPLAY */
868 f0cbd3ec bellard
           if (x_port >= 0) {
869 f0cbd3ec bellard
#ifdef HAVE_SETENV
870 f0cbd3ec bellard
             sprintf(buff, "%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
871 f0cbd3ec bellard
             setenv("DISPLAY", buff, 1);
872 f0cbd3ec bellard
#else
873 f0cbd3ec bellard
             sprintf(buff, "DISPLAY=%s:%d.%d", inet_ntoa(our_addr), x_port, x_screen);
874 f0cbd3ec bellard
             putenv(buff);
875 f0cbd3ec bellard
#endif
876 f0cbd3ec bellard
           }
877 3b46e624 ths
878 f0cbd3ec bellard
           dup2(fd0[1], 0);
879 f0cbd3ec bellard
           dup2(fd0[1], 1);
880 f0cbd3ec bellard
           dup2(fd[1], 2);
881 f0cbd3ec bellard
           for (s = 3; s <= 255; s++)
882 f0cbd3ec bellard
             close(s);
883 3b46e624 ths
884 f0cbd3ec bellard
           execlp("rsh","rsh","-l", user, host, args, NULL);
885 3b46e624 ths
886 f0cbd3ec bellard
           /* Ooops, failed, let's tell the user why */
887 3b46e624 ths
888 5fafdf24 ths
           sprintf(buff, "Error: execlp of %s failed: %s\n",
889 f0cbd3ec bellard
                   "rsh", strerror(errno));
890 f0cbd3ec bellard
           write(2, buff, strlen(buff)+1);
891 f0cbd3ec bellard
           close(0); close(1); close(2); /* XXX */
892 f0cbd3ec bellard
           exit(1);
893 3b46e624 ths
894 f0cbd3ec bellard
        default:
895 f0cbd3ec bellard
          close(fd[1]);
896 f0cbd3ec bellard
          close(fd0[1]);
897 f0cbd3ec bellard
          ns->s=fd[0];
898 f0cbd3ec bellard
          so->s=fd0[0];
899 3b46e624 ths
900 f0cbd3ec bellard
          return 1;
901 f0cbd3ec bellard
        }
902 f0cbd3ec bellard
}
903 f0cbd3ec bellard
#endif