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/error-report.h"
13 #include "qemu/main-loop.h"
16 int slirp_debug
= DBG_CALL
|DBG_MISC
|DBG_ERROR
;
20 struct quehead
*qh_link
;
21 struct quehead
*qh_rlink
;
25 insque(void *a
, void *b
)
27 register struct quehead
*element
= (struct quehead
*) a
;
28 register struct quehead
*head
= (struct quehead
*) b
;
29 element
->qh_link
= head
->qh_link
;
30 head
->qh_link
= (struct quehead
*)element
;
31 element
->qh_rlink
= (struct quehead
*)head
;
32 ((struct quehead
*)(element
->qh_link
))->qh_rlink
33 = (struct quehead
*)element
;
39 register struct quehead
*element
= (struct quehead
*) a
;
40 ((struct quehead
*)(element
->qh_link
))->qh_rlink
= element
->qh_rlink
;
41 ((struct quehead
*)(element
->qh_rlink
))->qh_link
= element
->qh_link
;
42 element
->qh_rlink
= NULL
;
45 int add_exec(struct ex_list
**ex_ptr
, int do_pty
, char *exec
,
46 struct in_addr addr
, int port
)
48 struct ex_list
*tmp_ptr
;
50 /* First, check if the port is "bound" */
51 for (tmp_ptr
= *ex_ptr
; tmp_ptr
; tmp_ptr
= tmp_ptr
->ex_next
) {
52 if (port
== tmp_ptr
->ex_fport
&&
53 addr
.s_addr
== tmp_ptr
->ex_addr
.s_addr
)
58 *ex_ptr
= g_new(struct ex_list
, 1);
59 (*ex_ptr
)->ex_fport
= port
;
60 (*ex_ptr
)->ex_addr
= addr
;
61 (*ex_ptr
)->ex_pty
= do_pty
;
62 (*ex_ptr
)->ex_exec
= (do_pty
== 3) ? exec
: g_strdup(exec
);
63 (*ex_ptr
)->ex_next
= tmp_ptr
;
70 * For systems with no strerror
74 extern char *sys_errlist
[];
81 return sys_errlist
[error
];
83 return "Unknown error.";
92 fork_exec(struct socket
*so
, const char *ex
, int do_pty
)
102 * We create and bind a socket, then fork off to another
103 * process, which connects to this socket, after which we
104 * exec the wanted program. If something (strange) happens,
105 * the accept() call could block us forever.
107 * do_pty = 0 Fork/exec inetd style
108 * do_pty = 1 Fork/exec using slirp.telnetd
109 * do_ptr = 2 Fork/exec using pty
112 fork_exec(struct socket
*so
, const char *ex
, int do_pty
)
115 struct sockaddr_in addr
;
116 socklen_t addrlen
= sizeof(addr
);
118 const char *argv
[256];
119 /* don't want to clobber the original */
125 DEBUG_CALL("fork_exec");
126 DEBUG_ARG("so = %p", so
);
127 DEBUG_ARG("ex = %p", ex
);
128 DEBUG_ARG("do_pty = %x", do_pty
);
133 addr
.sin_family
= AF_INET
;
135 addr
.sin_addr
.s_addr
= INADDR_ANY
;
137 if ((s
= qemu_socket(AF_INET
, SOCK_STREAM
, 0)) < 0 ||
138 bind(s
, (struct sockaddr
*)&addr
, addrlen
) < 0 ||
140 error_report("Error: inet socket: %s", strerror(errno
));
150 error_report("Error: fork failed: %s", strerror(errno
));
157 /* Set the DISPLAY */
158 getsockname(s
, (struct sockaddr
*)&addr
, &addrlen
);
161 * Connect to the socket
162 * XXX If any of these fail, we're in trouble!
164 s
= qemu_socket(AF_INET
, SOCK_STREAM
, 0);
165 addr
.sin_addr
= loopback_addr
;
167 ret
= connect(s
, (struct sockaddr
*)&addr
, addrlen
);
168 } while (ret
< 0 && errno
== EINTR
);
173 for (s
= getdtablesize() - 1; s
>= 3; s
--)
177 bptr
= g_strdup(ex
); /* No need to free() this */
179 /* Setup "slirp.telnetd -x" */
180 argv
[i
++] = "slirp.telnetd";
185 /* Change the string into argv[] */
187 while (*bptr
!= ' ' && *bptr
!= (char)0)
191 argv
[i
++] = g_strdup(curarg
);
195 execvp(argv
[0], (char **)argv
);
197 /* Ooops, failed, let's tell the user why */
198 fprintf(stderr
, "Error: execvp of %s failed: %s\n",
199 argv
[0], strerror(errno
));
200 close(0); close(1); close(2); /* XXX */
204 qemu_add_child_watch(pid
);
206 * XXX this could block us...
207 * XXX Should set a timer here, and if accept() doesn't
208 * return after X seconds, declare it a failure
209 * The only reason this will block forever is if socket()
210 * of connect() fail in the child process
213 so
->s
= accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
214 } while (so
->s
< 0 && errno
== EINTR
);
216 socket_set_fast_reuse(so
->s
);
218 qemu_setsockopt(so
->s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
219 qemu_set_nonblock(so
->s
);
221 /* Append the telnet options now */
222 if (so
->so_m
!= NULL
&& do_pty
== 1) {
223 sbappend(so
, so
->so_m
);
232 void slirp_connection_info(Slirp
*slirp
, Monitor
*mon
)
234 const char * const tcpstates
[] = {
235 [TCPS_CLOSED
] = "CLOSED",
236 [TCPS_LISTEN
] = "LISTEN",
237 [TCPS_SYN_SENT
] = "SYN_SENT",
238 [TCPS_SYN_RECEIVED
] = "SYN_RCVD",
239 [TCPS_ESTABLISHED
] = "ESTABLISHED",
240 [TCPS_CLOSE_WAIT
] = "CLOSE_WAIT",
241 [TCPS_FIN_WAIT_1
] = "FIN_WAIT_1",
242 [TCPS_CLOSING
] = "CLOSING",
243 [TCPS_LAST_ACK
] = "LAST_ACK",
244 [TCPS_FIN_WAIT_2
] = "FIN_WAIT_2",
245 [TCPS_TIME_WAIT
] = "TIME_WAIT",
247 struct in_addr dst_addr
;
248 struct sockaddr_in src
;
255 monitor_printf(mon
, " Protocol[State] FD Source Address Port "
256 "Dest. Address Port RecvQ SendQ\n");
258 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
; so
= so
->so_next
) {
259 if (so
->so_state
& SS_HOSTFWD
) {
260 state
= "HOST_FORWARD";
261 } else if (so
->so_tcpcb
) {
262 state
= tcpstates
[so
->so_tcpcb
->t_state
];
266 if (so
->so_state
& (SS_HOSTFWD
| SS_INCOMING
)) {
267 src_len
= sizeof(src
);
268 getsockname(so
->s
, (struct sockaddr
*)&src
, &src_len
);
269 dst_addr
= so
->so_laddr
;
270 dst_port
= so
->so_lport
;
272 src
.sin_addr
= so
->so_laddr
;
273 src
.sin_port
= so
->so_lport
;
274 dst_addr
= so
->so_faddr
;
275 dst_port
= so
->so_fport
;
277 snprintf(buf
, sizeof(buf
), " TCP[%s]", state
);
278 monitor_printf(mon
, "%-19s %3d %15s %5d ", buf
, so
->s
,
279 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*",
280 ntohs(src
.sin_port
));
281 monitor_printf(mon
, "%15s %5d %5d %5d\n",
282 inet_ntoa(dst_addr
), ntohs(dst_port
),
283 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
286 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
; so
= so
->so_next
) {
287 if (so
->so_state
& SS_HOSTFWD
) {
288 snprintf(buf
, sizeof(buf
), " UDP[HOST_FORWARD]");
289 src_len
= sizeof(src
);
290 getsockname(so
->s
, (struct sockaddr
*)&src
, &src_len
);
291 dst_addr
= so
->so_laddr
;
292 dst_port
= so
->so_lport
;
294 snprintf(buf
, sizeof(buf
), " UDP[%d sec]",
295 (so
->so_expire
- curtime
) / 1000);
296 src
.sin_addr
= so
->so_laddr
;
297 src
.sin_port
= so
->so_lport
;
298 dst_addr
= so
->so_faddr
;
299 dst_port
= so
->so_fport
;
301 monitor_printf(mon
, "%-19s %3d %15s %5d ", buf
, so
->s
,
302 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*",
303 ntohs(src
.sin_port
));
304 monitor_printf(mon
, "%15s %5d %5d %5d\n",
305 inet_ntoa(dst_addr
), ntohs(dst_port
),
306 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
309 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
; so
= so
->so_next
) {
310 snprintf(buf
, sizeof(buf
), " ICMP[%d sec]",
311 (so
->so_expire
- curtime
) / 1000);
312 src
.sin_addr
= so
->so_laddr
;
313 dst_addr
= so
->so_faddr
;
314 monitor_printf(mon
, "%-19s %3d %15s - ", buf
, so
->s
,
315 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*");
316 monitor_printf(mon
, "%15s - %5d %5d\n", inet_ntoa(dst_addr
),
317 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);