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