2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
8 #include "qemu/osdep.h"
13 insque(void *a
, void *b
)
15 register struct quehead
*element
= (struct quehead
*) a
;
16 register struct quehead
*head
= (struct quehead
*) b
;
17 element
->qh_link
= head
->qh_link
;
18 head
->qh_link
= (struct quehead
*)element
;
19 element
->qh_rlink
= (struct quehead
*)head
;
20 ((struct quehead
*)(element
->qh_link
))->qh_rlink
21 = (struct quehead
*)element
;
27 register struct quehead
*element
= (struct quehead
*) a
;
28 ((struct quehead
*)(element
->qh_link
))->qh_rlink
= element
->qh_rlink
;
29 ((struct quehead
*)(element
->qh_rlink
))->qh_link
= element
->qh_link
;
30 element
->qh_rlink
= NULL
;
34 add_guestfwd(struct gfwd_list
**ex_ptr
,
35 SlirpWriteCb write_cb
, void *opaque
,
36 struct in_addr addr
, int port
)
38 struct gfwd_list
*f
= g_new0(struct gfwd_list
, 1);
40 f
->write_cb
= write_cb
;
51 add_exec(struct gfwd_list
**ex_ptr
, const char *cmdline
,
52 struct in_addr addr
, int port
)
54 struct gfwd_list
*f
= add_guestfwd(ex_ptr
, NULL
, NULL
, addr
, port
);
56 f
->ex_exec
= g_strdup(cmdline
);
62 slirp_socketpair_with_oob(int sv
[2])
64 struct sockaddr_in addr
= {
65 .sin_family
= AF_INET
,
67 .sin_addr
.s_addr
= INADDR_ANY
,
69 socklen_t addrlen
= sizeof(addr
);
73 s
= slirp_socket(AF_INET
, SOCK_STREAM
, 0);
74 if (s
< 0 || bind(s
, (struct sockaddr
*)&addr
, addrlen
) < 0 ||
76 getsockname(s
, (struct sockaddr
*)&addr
, &addrlen
) < 0) {
80 sv
[1] = slirp_socket(AF_INET
, SOCK_STREAM
, 0);
85 * This connect won't block because we've already listen()ed on
86 * the server end (even though we won't accept() the connection
90 ret
= connect(sv
[1], (struct sockaddr
*)&addr
, addrlen
);
91 } while (ret
< 0 && errno
== EINTR
);
97 sv
[0] = accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
98 } while (sv
[0] < 0 && errno
== EINTR
);
103 slirp_closesocket(s
);
107 g_critical("slirp_socketpair(): %s", strerror(errno
));
109 slirp_closesocket(s
);
112 slirp_closesocket(sv
[1]);
118 fork_exec_child_setup(gpointer data
)
125 #pragma GCC diagnostic push
126 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
128 #if !GLIB_CHECK_VERSION(2, 58, 0)
129 typedef struct SlirpGSpawnFds
{
130 GSpawnChildSetupFunc child_setup
;
138 slirp_gspawn_fds_setup(gpointer user_data
)
140 SlirpGSpawnFds
*q
= (SlirpGSpawnFds
*)user_data
;
142 dup2(q
->stdin_fd
, 0);
143 dup2(q
->stdout_fd
, 1);
144 dup2(q
->stderr_fd
, 2);
145 q
->child_setup(q
->user_data
);
149 static inline gboolean
150 g_spawn_async_with_fds_slirp(const gchar
*working_directory
,
154 GSpawnChildSetupFunc child_setup
,
162 #if GLIB_CHECK_VERSION(2, 58, 0)
163 return g_spawn_async_with_fds(working_directory
, argv
, envp
, flags
,
164 child_setup
, user_data
,
165 child_pid
, stdin_fd
, stdout_fd
, stderr_fd
,
168 SlirpGSpawnFds setup
= {
169 .child_setup
= child_setup
,
170 .user_data
= user_data
,
171 .stdin_fd
= stdin_fd
,
172 .stdout_fd
= stdout_fd
,
173 .stderr_fd
= stderr_fd
,
176 return g_spawn_async(working_directory
, argv
, envp
, flags
,
177 slirp_gspawn_fds_setup
, &setup
,
182 #define g_spawn_async_with_fds(wd, argv, env, f, c, d, p, ifd, ofd, efd, err) \
183 g_spawn_async_with_fds_slirp(wd, argv, env, f, c, d, p, ifd, ofd, efd, err)
185 #pragma GCC diagnostic pop
188 fork_exec(struct socket
*so
, const char *ex
)
194 DEBUG_CALL("fork_exec");
195 DEBUG_ARG("so = %p", so
);
196 DEBUG_ARG("ex = %p", ex
);
198 if (slirp_socketpair_with_oob(sp
) < 0) {
202 argv
= g_strsplit(ex
, " ", -1);
203 g_spawn_async_with_fds(NULL
/* cwd */,
207 fork_exec_child_setup
, NULL
/* data */,
208 NULL
/* child_pid */,
214 g_critical("fork_exec: %s", err
->message
);
216 slirp_closesocket(sp
[0]);
217 slirp_closesocket(sp
[1]);
222 slirp_closesocket(sp
[1]);
223 slirp_socket_set_fast_reuse(so
->s
);
225 slirp_setsockopt(so
->s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
226 slirp_set_nonblock(so
->s
);
227 so
->slirp
->cb
->register_poll_fd(so
->s
);
231 char *slirp_connection_info(Slirp
*slirp
)
233 GString
*str
= g_string_new(NULL
);
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 g_string_append_printf(str
,
256 " Protocol[State] FD Source Address Port "
257 "Dest. Address Port RecvQ SendQ\n");
259 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
; so
= so
->so_next
) {
260 if (so
->so_state
& SS_HOSTFWD
) {
261 state
= "HOST_FORWARD";
262 } else if (so
->so_tcpcb
) {
263 state
= tcpstates
[so
->so_tcpcb
->t_state
];
267 if (so
->so_state
& (SS_HOSTFWD
| SS_INCOMING
)) {
268 src_len
= sizeof(src
);
269 getsockname(so
->s
, (struct sockaddr
*)&src
, &src_len
);
270 dst_addr
= so
->so_laddr
;
271 dst_port
= so
->so_lport
;
273 src
.sin_addr
= so
->so_laddr
;
274 src
.sin_port
= so
->so_lport
;
275 dst_addr
= so
->so_faddr
;
276 dst_port
= so
->so_fport
;
278 snprintf(buf
, sizeof(buf
), " TCP[%s]", state
);
279 g_string_append_printf(str
, "%-19s %3d %15s %5d ", buf
, so
->s
,
280 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*",
281 ntohs(src
.sin_port
));
282 g_string_append_printf(str
, "%15s %5d %5d %5d\n",
283 inet_ntoa(dst_addr
), ntohs(dst_port
),
284 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
287 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
; so
= so
->so_next
) {
288 if (so
->so_state
& SS_HOSTFWD
) {
289 snprintf(buf
, sizeof(buf
), " UDP[HOST_FORWARD]");
290 src_len
= sizeof(src
);
291 getsockname(so
->s
, (struct sockaddr
*)&src
, &src_len
);
292 dst_addr
= so
->so_laddr
;
293 dst_port
= so
->so_lport
;
295 snprintf(buf
, sizeof(buf
), " UDP[%d sec]",
296 (so
->so_expire
- curtime
) / 1000);
297 src
.sin_addr
= so
->so_laddr
;
298 src
.sin_port
= so
->so_lport
;
299 dst_addr
= so
->so_faddr
;
300 dst_port
= so
->so_fport
;
302 g_string_append_printf(str
, "%-19s %3d %15s %5d ", buf
, so
->s
,
303 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*",
304 ntohs(src
.sin_port
));
305 g_string_append_printf(str
, "%15s %5d %5d %5d\n",
306 inet_ntoa(dst_addr
), ntohs(dst_port
),
307 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
310 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
; so
= so
->so_next
) {
311 snprintf(buf
, sizeof(buf
), " ICMP[%d sec]",
312 (so
->so_expire
- curtime
) / 1000);
313 src
.sin_addr
= so
->so_laddr
;
314 dst_addr
= so
->so_faddr
;
315 g_string_append_printf(str
, "%-19s %3d %15s - ", buf
, so
->s
,
316 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*");
317 g_string_append_printf(str
, "%15s - %5d %5d\n", inet_ntoa(dst_addr
),
318 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
321 return g_string_free(str
, FALSE
);