2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
11 #include "monitor/monitor.h"
12 #include "qemu/main-loop.h"
15 int slirp_debug
= DBG_CALL
|DBG_MISC
|DBG_ERROR
;
19 struct quehead
*qh_link
;
20 struct quehead
*qh_rlink
;
24 insque(void *a
, void *b
)
26 register struct quehead
*element
= (struct quehead
*) a
;
27 register struct quehead
*head
= (struct quehead
*) b
;
28 element
->qh_link
= head
->qh_link
;
29 head
->qh_link
= (struct quehead
*)element
;
30 element
->qh_rlink
= (struct quehead
*)head
;
31 ((struct quehead
*)(element
->qh_link
))->qh_rlink
32 = (struct quehead
*)element
;
38 register struct quehead
*element
= (struct quehead
*) a
;
39 ((struct quehead
*)(element
->qh_link
))->qh_rlink
= element
->qh_rlink
;
40 ((struct quehead
*)(element
->qh_rlink
))->qh_link
= element
->qh_link
;
41 element
->qh_rlink
= NULL
;
44 int add_exec(struct ex_list
**ex_ptr
, int do_pty
, char *exec
,
45 struct in_addr addr
, int port
)
47 struct ex_list
*tmp_ptr
;
49 /* First, check if the port is "bound" */
50 for (tmp_ptr
= *ex_ptr
; tmp_ptr
; tmp_ptr
= tmp_ptr
->ex_next
) {
51 if (port
== tmp_ptr
->ex_fport
&&
52 addr
.s_addr
== tmp_ptr
->ex_addr
.s_addr
)
57 *ex_ptr
= (struct ex_list
*)malloc(sizeof(struct ex_list
));
58 (*ex_ptr
)->ex_fport
= port
;
59 (*ex_ptr
)->ex_addr
= addr
;
60 (*ex_ptr
)->ex_pty
= do_pty
;
61 (*ex_ptr
)->ex_exec
= (do_pty
== 3) ? exec
: strdup(exec
);
62 (*ex_ptr
)->ex_next
= tmp_ptr
;
69 * For systems with no strerror
73 extern char *sys_errlist
[];
80 return sys_errlist
[error
];
82 return "Unknown error.";
91 fork_exec(struct socket
*so
, const char *ex
, int do_pty
)
101 * We create and bind a socket, then fork off to another
102 * process, which connects to this socket, after which we
103 * exec the wanted program. If something (strange) happens,
104 * the accept() call could block us forever.
106 * do_pty = 0 Fork/exec inetd style
107 * do_pty = 1 Fork/exec using slirp.telnetd
108 * do_ptr = 2 Fork/exec using pty
111 fork_exec(struct socket
*so
, const char *ex
, int do_pty
)
114 struct sockaddr_in addr
;
115 socklen_t addrlen
= sizeof(addr
);
117 const char *argv
[256];
118 /* don't want to clobber the original */
124 DEBUG_CALL("fork_exec");
125 DEBUG_ARG("so = %lx", (long)so
);
126 DEBUG_ARG("ex = %lx", (long)ex
);
127 DEBUG_ARG("do_pty = %lx", (long)do_pty
);
132 addr
.sin_family
= AF_INET
;
134 addr
.sin_addr
.s_addr
= INADDR_ANY
;
136 if ((s
= qemu_socket(AF_INET
, SOCK_STREAM
, 0)) < 0 ||
137 bind(s
, (struct sockaddr
*)&addr
, addrlen
) < 0 ||
139 error_report("Error: inet socket: %s", strerror(errno
));
149 error_report("Error: fork failed: %s", strerror(errno
));
156 /* Set the DISPLAY */
157 getsockname(s
, (struct sockaddr
*)&addr
, &addrlen
);
160 * Connect to the socket
161 * XXX If any of these fail, we're in trouble!
163 s
= qemu_socket(AF_INET
, SOCK_STREAM
, 0);
164 addr
.sin_addr
= loopback_addr
;
166 ret
= connect(s
, (struct sockaddr
*)&addr
, addrlen
);
167 } while (ret
< 0 && errno
== EINTR
);
172 for (s
= getdtablesize() - 1; s
>= 3; s
--)
176 bptr
= g_strdup(ex
); /* No need to free() this */
178 /* Setup "slirp.telnetd -x" */
179 argv
[i
++] = "slirp.telnetd";
184 /* Change the string into argv[] */
186 while (*bptr
!= ' ' && *bptr
!= (char)0)
190 argv
[i
++] = strdup(curarg
);
194 execvp(argv
[0], (char **)argv
);
196 /* Ooops, failed, let's tell the user why */
197 fprintf(stderr
, "Error: execvp of %s failed: %s\n",
198 argv
[0], strerror(errno
));
199 close(0); close(1); close(2); /* XXX */
203 qemu_add_child_watch(pid
);
205 * XXX this could block us...
206 * XXX Should set a timer here, and if accept() doesn't
207 * return after X seconds, declare it a failure
208 * The only reason this will block forever is if socket()
209 * of connect() fail in the child process
212 so
->s
= accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
213 } while (so
->s
< 0 && errno
== EINTR
);
215 socket_set_fast_reuse(so
->s
);
217 qemu_setsockopt(so
->s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
218 qemu_set_nonblock(so
->s
);
220 /* Append the telnet options now */
221 if (so
->so_m
!= NULL
&& do_pty
== 1) {
222 sbappend(so
, so
->so_m
);
238 bptr
= (char *)malloc(strlen(str
)+1);
245 void slirp_connection_info(Slirp
*slirp
, Monitor
*mon
)
247 const char * const tcpstates
[] = {
248 [TCPS_CLOSED
] = "CLOSED",
249 [TCPS_LISTEN
] = "LISTEN",
250 [TCPS_SYN_SENT
] = "SYN_SENT",
251 [TCPS_SYN_RECEIVED
] = "SYN_RCVD",
252 [TCPS_ESTABLISHED
] = "ESTABLISHED",
253 [TCPS_CLOSE_WAIT
] = "CLOSE_WAIT",
254 [TCPS_FIN_WAIT_1
] = "FIN_WAIT_1",
255 [TCPS_CLOSING
] = "CLOSING",
256 [TCPS_LAST_ACK
] = "LAST_ACK",
257 [TCPS_FIN_WAIT_2
] = "FIN_WAIT_2",
258 [TCPS_TIME_WAIT
] = "TIME_WAIT",
260 struct in_addr dst_addr
;
261 struct sockaddr_in src
;
268 monitor_printf(mon
, " Protocol[State] FD Source Address Port "
269 "Dest. Address Port RecvQ SendQ\n");
271 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
; so
= so
->so_next
) {
272 if (so
->so_state
& SS_HOSTFWD
) {
273 state
= "HOST_FORWARD";
274 } else if (so
->so_tcpcb
) {
275 state
= tcpstates
[so
->so_tcpcb
->t_state
];
279 if (so
->so_state
& (SS_HOSTFWD
| SS_INCOMING
)) {
280 src_len
= sizeof(src
);
281 getsockname(so
->s
, (struct sockaddr
*)&src
, &src_len
);
282 dst_addr
= so
->so_laddr
;
283 dst_port
= so
->so_lport
;
285 src
.sin_addr
= so
->so_laddr
;
286 src
.sin_port
= so
->so_lport
;
287 dst_addr
= so
->so_faddr
;
288 dst_port
= so
->so_fport
;
290 snprintf(buf
, sizeof(buf
), " TCP[%s]", state
);
291 monitor_printf(mon
, "%-19s %3d %15s %5d ", buf
, so
->s
,
292 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*",
293 ntohs(src
.sin_port
));
294 monitor_printf(mon
, "%15s %5d %5d %5d\n",
295 inet_ntoa(dst_addr
), ntohs(dst_port
),
296 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
299 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
; so
= so
->so_next
) {
300 if (so
->so_state
& SS_HOSTFWD
) {
301 snprintf(buf
, sizeof(buf
), " UDP[HOST_FORWARD]");
302 src_len
= sizeof(src
);
303 getsockname(so
->s
, (struct sockaddr
*)&src
, &src_len
);
304 dst_addr
= so
->so_laddr
;
305 dst_port
= so
->so_lport
;
307 snprintf(buf
, sizeof(buf
), " UDP[%d sec]",
308 (so
->so_expire
- curtime
) / 1000);
309 src
.sin_addr
= so
->so_laddr
;
310 src
.sin_port
= so
->so_lport
;
311 dst_addr
= so
->so_faddr
;
312 dst_port
= so
->so_fport
;
314 monitor_printf(mon
, "%-19s %3d %15s %5d ", buf
, so
->s
,
315 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*",
316 ntohs(src
.sin_port
));
317 monitor_printf(mon
, "%15s %5d %5d %5d\n",
318 inet_ntoa(dst_addr
), ntohs(dst_port
),
319 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
322 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
; so
= so
->so_next
) {
323 snprintf(buf
, sizeof(buf
), " ICMP[%d sec]",
324 (so
->so_expire
- curtime
) / 1000);
325 src
.sin_addr
= so
->so_laddr
;
326 dst_addr
= so
->so_faddr
;
327 monitor_printf(mon
, "%-19s %3d %15s - ", buf
, so
->s
,
328 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*");
329 monitor_printf(mon
, "%15s - %5d %5d\n", inet_ntoa(dst_addr
),
330 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);