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