root / slirp / misc.c @ 8631b608
History | View | Annotate | Download (10.2 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 1995 Danny Gasparovski.
|
3 |
*
|
4 |
* Please read the file COPYRIGHT for the
|
5 |
* terms and conditions of the copyright.
|
6 |
*/
|
7 |
|
8 |
#include <slirp.h> |
9 |
#include <libslirp.h> |
10 |
|
11 |
#include "monitor.h" |
12 |
|
13 |
#ifdef DEBUG
|
14 |
int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
|
15 |
#endif
|
16 |
|
17 |
struct quehead {
|
18 |
struct quehead *qh_link;
|
19 |
struct quehead *qh_rlink;
|
20 |
}; |
21 |
|
22 |
inline void |
23 |
insque(void *a, void *b) |
24 |
{ |
25 |
register struct quehead *element = (struct quehead *) a; |
26 |
register struct quehead *head = (struct quehead *) b; |
27 |
element->qh_link = head->qh_link; |
28 |
head->qh_link = (struct quehead *)element;
|
29 |
element->qh_rlink = (struct quehead *)head;
|
30 |
((struct quehead *)(element->qh_link))->qh_rlink
|
31 |
= (struct quehead *)element;
|
32 |
} |
33 |
|
34 |
inline void |
35 |
remque(void *a)
|
36 |
{ |
37 |
register struct quehead *element = (struct quehead *) a; |
38 |
((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
|
39 |
((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
|
40 |
element->qh_rlink = NULL;
|
41 |
} |
42 |
|
43 |
int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec, |
44 |
struct in_addr addr, int port) |
45 |
{ |
46 |
struct ex_list *tmp_ptr;
|
47 |
|
48 |
/* First, check if the port is "bound" */
|
49 |
for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
|
50 |
if (port == tmp_ptr->ex_fport &&
|
51 |
addr.s_addr == tmp_ptr->ex_addr.s_addr) |
52 |
return -1; |
53 |
} |
54 |
|
55 |
tmp_ptr = *ex_ptr; |
56 |
*ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list)); |
57 |
(*ex_ptr)->ex_fport = port; |
58 |
(*ex_ptr)->ex_addr = addr; |
59 |
(*ex_ptr)->ex_pty = do_pty; |
60 |
(*ex_ptr)->ex_exec = (do_pty == 3) ? exec : strdup(exec);
|
61 |
(*ex_ptr)->ex_next = tmp_ptr; |
62 |
return 0; |
63 |
} |
64 |
|
65 |
#ifndef HAVE_STRERROR
|
66 |
|
67 |
/*
|
68 |
* For systems with no strerror
|
69 |
*/
|
70 |
|
71 |
extern int sys_nerr; |
72 |
extern char *sys_errlist[]; |
73 |
|
74 |
char *
|
75 |
strerror(error) |
76 |
int error;
|
77 |
{ |
78 |
if (error < sys_nerr)
|
79 |
return sys_errlist[error];
|
80 |
else
|
81 |
return "Unknown error."; |
82 |
} |
83 |
|
84 |
#endif
|
85 |
|
86 |
|
87 |
#ifdef _WIN32
|
88 |
|
89 |
int
|
90 |
fork_exec(struct socket *so, const char *ex, int do_pty) |
91 |
{ |
92 |
/* not implemented */
|
93 |
return 0; |
94 |
} |
95 |
|
96 |
#else
|
97 |
|
98 |
/*
|
99 |
* XXX This is ugly
|
100 |
* We create and bind a socket, then fork off to another
|
101 |
* process, which connects to this socket, after which we
|
102 |
* exec the wanted program. If something (strange) happens,
|
103 |
* the accept() call could block us forever.
|
104 |
*
|
105 |
* do_pty = 0 Fork/exec inetd style
|
106 |
* do_pty = 1 Fork/exec using slirp.telnetd
|
107 |
* do_ptr = 2 Fork/exec using pty
|
108 |
*/
|
109 |
int
|
110 |
fork_exec(struct socket *so, const char *ex, int do_pty) |
111 |
{ |
112 |
int s;
|
113 |
struct sockaddr_in addr;
|
114 |
socklen_t addrlen = sizeof(addr);
|
115 |
int opt;
|
116 |
int master = -1; |
117 |
const char *argv[256]; |
118 |
/* don't want to clobber the original */
|
119 |
char *bptr;
|
120 |
const char *curarg; |
121 |
int c, i, ret;
|
122 |
|
123 |
DEBUG_CALL("fork_exec");
|
124 |
DEBUG_ARG("so = %lx", (long)so); |
125 |
DEBUG_ARG("ex = %lx", (long)ex); |
126 |
DEBUG_ARG("do_pty = %lx", (long)do_pty); |
127 |
|
128 |
if (do_pty == 2) { |
129 |
return 0; |
130 |
} else {
|
131 |
addr.sin_family = AF_INET; |
132 |
addr.sin_port = 0;
|
133 |
addr.sin_addr.s_addr = INADDR_ANY; |
134 |
|
135 |
if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 || |
136 |
bind(s, (struct sockaddr *)&addr, addrlen) < 0 || |
137 |
listen(s, 1) < 0) { |
138 |
lprint("Error: inet socket: %s\n", strerror(errno));
|
139 |
closesocket(s); |
140 |
|
141 |
return 0; |
142 |
} |
143 |
} |
144 |
|
145 |
switch(fork()) {
|
146 |
case -1: |
147 |
lprint("Error: fork failed: %s\n", strerror(errno));
|
148 |
close(s); |
149 |
if (do_pty == 2) |
150 |
close(master); |
151 |
return 0; |
152 |
|
153 |
case 0: |
154 |
/* Set the DISPLAY */
|
155 |
if (do_pty == 2) { |
156 |
(void) close(master);
|
157 |
#ifdef TIOCSCTTY /* XXXXX */ |
158 |
(void) setsid();
|
159 |
ioctl(s, TIOCSCTTY, (char *)NULL); |
160 |
#endif
|
161 |
} else {
|
162 |
getsockname(s, (struct sockaddr *)&addr, &addrlen);
|
163 |
close(s); |
164 |
/*
|
165 |
* Connect to the socket
|
166 |
* XXX If any of these fail, we're in trouble!
|
167 |
*/
|
168 |
s = qemu_socket(AF_INET, SOCK_STREAM, 0);
|
169 |
addr.sin_addr = loopback_addr; |
170 |
do {
|
171 |
ret = connect(s, (struct sockaddr *)&addr, addrlen);
|
172 |
} while (ret < 0 && errno == EINTR); |
173 |
} |
174 |
|
175 |
dup2(s, 0);
|
176 |
dup2(s, 1);
|
177 |
dup2(s, 2);
|
178 |
for (s = getdtablesize() - 1; s >= 3; s--) |
179 |
close(s); |
180 |
|
181 |
i = 0;
|
182 |
bptr = qemu_strdup(ex); /* No need to free() this */
|
183 |
if (do_pty == 1) { |
184 |
/* Setup "slirp.telnetd -x" */
|
185 |
argv[i++] = "slirp.telnetd";
|
186 |
argv[i++] = "-x";
|
187 |
argv[i++] = bptr; |
188 |
} else
|
189 |
do {
|
190 |
/* Change the string into argv[] */
|
191 |
curarg = bptr; |
192 |
while (*bptr != ' ' && *bptr != (char)0) |
193 |
bptr++; |
194 |
c = *bptr; |
195 |
*bptr++ = (char)0; |
196 |
argv[i++] = strdup(curarg); |
197 |
} while (c);
|
198 |
|
199 |
argv[i] = NULL;
|
200 |
execvp(argv[0], (char **)argv); |
201 |
|
202 |
/* Ooops, failed, let's tell the user why */
|
203 |
fprintf(stderr, "Error: execvp of %s failed: %s\n",
|
204 |
argv[0], strerror(errno));
|
205 |
close(0); close(1); close(2); /* XXX */ |
206 |
exit(1);
|
207 |
|
208 |
default:
|
209 |
if (do_pty == 2) { |
210 |
close(s); |
211 |
so->s = master; |
212 |
} else {
|
213 |
/*
|
214 |
* XXX this could block us...
|
215 |
* XXX Should set a timer here, and if accept() doesn't
|
216 |
* return after X seconds, declare it a failure
|
217 |
* The only reason this will block forever is if socket()
|
218 |
* of connect() fail in the child process
|
219 |
*/
|
220 |
do {
|
221 |
so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
|
222 |
} while (so->s < 0 && errno == EINTR); |
223 |
closesocket(s); |
224 |
opt = 1;
|
225 |
setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)); |
226 |
opt = 1;
|
227 |
setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); |
228 |
} |
229 |
fd_nonblock(so->s); |
230 |
|
231 |
/* Append the telnet options now */
|
232 |
if (so->so_m != NULL && do_pty == 1) { |
233 |
sbappend(so, so->so_m); |
234 |
so->so_m = NULL;
|
235 |
} |
236 |
|
237 |
return 1; |
238 |
} |
239 |
} |
240 |
#endif
|
241 |
|
242 |
#ifndef HAVE_STRDUP
|
243 |
char *
|
244 |
strdup(str) |
245 |
const char *str; |
246 |
{ |
247 |
char *bptr;
|
248 |
|
249 |
bptr = (char *)malloc(strlen(str)+1); |
250 |
strcpy(bptr, str); |
251 |
|
252 |
return bptr;
|
253 |
} |
254 |
#endif
|
255 |
|
256 |
#include "monitor.h" |
257 |
|
258 |
void lprint(const char *format, ...) |
259 |
{ |
260 |
va_list args; |
261 |
|
262 |
va_start(args, format); |
263 |
monitor_vprintf(default_mon, format, args); |
264 |
va_end(args); |
265 |
} |
266 |
|
267 |
#ifdef BAD_SPRINTF
|
268 |
|
269 |
#undef vsprintf
|
270 |
#undef sprintf
|
271 |
|
272 |
/*
|
273 |
* Some BSD-derived systems have a sprintf which returns char *
|
274 |
*/
|
275 |
|
276 |
int
|
277 |
vsprintf_len(string, format, args) |
278 |
char *string;
|
279 |
const char *format; |
280 |
va_list args; |
281 |
{ |
282 |
vsprintf(string, format, args); |
283 |
return strlen(string);
|
284 |
} |
285 |
|
286 |
int
|
287 |
#ifdef __STDC__
|
288 |
sprintf_len(char *string, const char *format, ...) |
289 |
#else
|
290 |
sprintf_len(va_alist) va_dcl |
291 |
#endif
|
292 |
{ |
293 |
va_list args; |
294 |
#ifdef __STDC__
|
295 |
va_start(args, format); |
296 |
#else
|
297 |
char *string;
|
298 |
char *format;
|
299 |
va_start(args); |
300 |
string = va_arg(args, char *);
|
301 |
format = va_arg(args, char *);
|
302 |
#endif
|
303 |
vsprintf(string, format, args); |
304 |
return strlen(string);
|
305 |
} |
306 |
|
307 |
#endif
|
308 |
|
309 |
void
|
310 |
u_sleep(int usec)
|
311 |
{ |
312 |
struct timeval t;
|
313 |
fd_set fdset; |
314 |
|
315 |
FD_ZERO(&fdset); |
316 |
|
317 |
t.tv_sec = 0;
|
318 |
t.tv_usec = usec * 1000;
|
319 |
|
320 |
select(0, &fdset, &fdset, &fdset, &t);
|
321 |
} |
322 |
|
323 |
/*
|
324 |
* Set fd blocking and non-blocking
|
325 |
*/
|
326 |
|
327 |
void
|
328 |
fd_nonblock(int fd)
|
329 |
{ |
330 |
#ifdef FIONBIO
|
331 |
#ifdef _WIN32
|
332 |
unsigned long opt = 1; |
333 |
#else
|
334 |
int opt = 1; |
335 |
#endif
|
336 |
|
337 |
ioctlsocket(fd, FIONBIO, &opt); |
338 |
#else
|
339 |
int opt;
|
340 |
|
341 |
opt = fcntl(fd, F_GETFL, 0);
|
342 |
opt |= O_NONBLOCK; |
343 |
fcntl(fd, F_SETFL, opt); |
344 |
#endif
|
345 |
} |
346 |
|
347 |
void
|
348 |
fd_block(int fd)
|
349 |
{ |
350 |
#ifdef FIONBIO
|
351 |
#ifdef _WIN32
|
352 |
unsigned long opt = 0; |
353 |
#else
|
354 |
int opt = 0; |
355 |
#endif
|
356 |
|
357 |
ioctlsocket(fd, FIONBIO, &opt); |
358 |
#else
|
359 |
int opt;
|
360 |
|
361 |
opt = fcntl(fd, F_GETFL, 0);
|
362 |
opt &= ~O_NONBLOCK; |
363 |
fcntl(fd, F_SETFL, opt); |
364 |
#endif
|
365 |
} |
366 |
|
367 |
void slirp_connection_info(Slirp *slirp, Monitor *mon)
|
368 |
{ |
369 |
const char * const tcpstates[] = { |
370 |
[TCPS_CLOSED] = "CLOSED",
|
371 |
[TCPS_LISTEN] = "LISTEN",
|
372 |
[TCPS_SYN_SENT] = "SYN_SENT",
|
373 |
[TCPS_SYN_RECEIVED] = "SYN_RCVD",
|
374 |
[TCPS_ESTABLISHED] = "ESTABLISHED",
|
375 |
[TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
|
376 |
[TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
|
377 |
[TCPS_CLOSING] = "CLOSING",
|
378 |
[TCPS_LAST_ACK] = "LAST_ACK",
|
379 |
[TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
|
380 |
[TCPS_TIME_WAIT] = "TIME_WAIT",
|
381 |
}; |
382 |
struct in_addr dst_addr;
|
383 |
struct sockaddr_in src;
|
384 |
socklen_t src_len; |
385 |
uint16_t dst_port; |
386 |
struct socket *so;
|
387 |
const char *state; |
388 |
char buf[20]; |
389 |
int n;
|
390 |
|
391 |
monitor_printf(mon, " Protocol[State] FD Source Address Port "
|
392 |
"Dest. Address Port RecvQ SendQ\n");
|
393 |
|
394 |
for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
|
395 |
if (so->so_state & SS_HOSTFWD) {
|
396 |
state = "HOST_FORWARD";
|
397 |
} else if (so->so_tcpcb) { |
398 |
state = tcpstates[so->so_tcpcb->t_state]; |
399 |
} else {
|
400 |
state = "NONE";
|
401 |
} |
402 |
if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
|
403 |
src_len = sizeof(src);
|
404 |
getsockname(so->s, (struct sockaddr *)&src, &src_len);
|
405 |
dst_addr = so->so_laddr; |
406 |
dst_port = so->so_lport; |
407 |
} else {
|
408 |
src.sin_addr = so->so_laddr; |
409 |
src.sin_port = so->so_lport; |
410 |
dst_addr = so->so_faddr; |
411 |
dst_port = so->so_fport; |
412 |
} |
413 |
n = snprintf(buf, sizeof(buf), " TCP[%s]", state); |
414 |
memset(&buf[n], ' ', 19 - n); |
415 |
buf[19] = 0; |
416 |
monitor_printf(mon, "%s %3d %15s %5d ", buf, so->s,
|
417 |
src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
|
418 |
ntohs(src.sin_port)); |
419 |
monitor_printf(mon, "%15s %5d %5d %5d\n",
|
420 |
inet_ntoa(dst_addr), ntohs(dst_port), |
421 |
so->so_rcv.sb_cc, so->so_snd.sb_cc); |
422 |
} |
423 |
|
424 |
for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
|
425 |
if (so->so_state & SS_HOSTFWD) {
|
426 |
n = snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]"); |
427 |
src_len = sizeof(src);
|
428 |
getsockname(so->s, (struct sockaddr *)&src, &src_len);
|
429 |
dst_addr = so->so_laddr; |
430 |
dst_port = so->so_lport; |
431 |
} else {
|
432 |
n = snprintf(buf, sizeof(buf), " UDP[%d sec]", |
433 |
(so->so_expire - curtime) / 1000);
|
434 |
src.sin_addr = so->so_laddr; |
435 |
src.sin_port = so->so_lport; |
436 |
dst_addr = so->so_faddr; |
437 |
dst_port = so->so_fport; |
438 |
} |
439 |
memset(&buf[n], ' ', 19 - n); |
440 |
buf[19] = 0; |
441 |
monitor_printf(mon, "%s %3d %15s %5d ", buf, so->s,
|
442 |
src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
|
443 |
ntohs(src.sin_port)); |
444 |
monitor_printf(mon, "%15s %5d %5d %5d\n",
|
445 |
inet_ntoa(dst_addr), ntohs(dst_port), |
446 |
so->so_rcv.sb_cc, so->so_snd.sb_cc); |
447 |
} |
448 |
} |