2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
11 insque(void *a
, void *b
)
13 register struct quehead
*element
= (struct quehead
*) a
;
14 register struct quehead
*head
= (struct quehead
*) b
;
15 element
->qh_link
= head
->qh_link
;
16 head
->qh_link
= (struct quehead
*)element
;
17 element
->qh_rlink
= (struct quehead
*)head
;
18 ((struct quehead
*)(element
->qh_link
))->qh_rlink
19 = (struct quehead
*)element
;
25 register struct quehead
*element
= (struct quehead
*) a
;
26 ((struct quehead
*)(element
->qh_link
))->qh_rlink
= element
->qh_rlink
;
27 ((struct quehead
*)(element
->qh_rlink
))->qh_link
= element
->qh_link
;
28 element
->qh_rlink
= NULL
;
32 add_guestfwd(struct gfwd_list
**ex_ptr
,
33 SlirpWriteCb write_cb
, void *opaque
,
34 struct in_addr addr
, int port
)
36 struct gfwd_list
*f
= g_new0(struct gfwd_list
, 1);
38 f
->write_cb
= write_cb
;
49 add_exec(struct gfwd_list
**ex_ptr
, const char *cmdline
,
50 struct in_addr addr
, int port
)
52 struct gfwd_list
*f
= add_guestfwd(ex_ptr
, NULL
, NULL
, addr
, port
);
54 f
->ex_exec
= g_strdup(cmdline
);
60 slirp_socketpair_with_oob(int sv
[2])
62 struct sockaddr_in addr
= {
63 .sin_family
= AF_INET
,
65 .sin_addr
.s_addr
= INADDR_ANY
,
67 socklen_t addrlen
= sizeof(addr
);
71 s
= slirp_socket(AF_INET
, SOCK_STREAM
, 0);
72 if (s
< 0 || bind(s
, (struct sockaddr
*)&addr
, addrlen
) < 0 ||
74 getsockname(s
, (struct sockaddr
*)&addr
, &addrlen
) < 0) {
78 sv
[1] = slirp_socket(AF_INET
, SOCK_STREAM
, 0);
83 * This connect won't block because we've already listen()ed on
84 * the server end (even though we won't accept() the connection
88 ret
= connect(sv
[1], (struct sockaddr
*)&addr
, addrlen
);
89 } while (ret
< 0 && errno
== EINTR
);
95 sv
[0] = accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
96 } while (sv
[0] < 0 && errno
== EINTR
);
105 g_critical("slirp_socketpair(): %s", strerror(errno
));
116 fork_exec_child_setup(gpointer data
)
123 #pragma GCC diagnostic push
124 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
126 #if !GLIB_CHECK_VERSION(2, 58, 0)
127 typedef struct SlirpGSpawnFds
{
128 GSpawnChildSetupFunc child_setup
;
136 slirp_gspawn_fds_setup(gpointer user_data
)
138 SlirpGSpawnFds
*q
= (SlirpGSpawnFds
*)user_data
;
140 dup2(q
->stdin_fd
, 0);
141 dup2(q
->stdout_fd
, 1);
142 dup2(q
->stderr_fd
, 2);
143 q
->child_setup(q
->user_data
);
147 static inline gboolean
148 g_spawn_async_with_fds_slirp(const gchar
*working_directory
,
152 GSpawnChildSetupFunc child_setup
,
160 #if GLIB_CHECK_VERSION(2, 58, 0)
161 return g_spawn_async_with_fds(working_directory
, argv
, envp
, flags
,
162 child_setup
, user_data
,
163 child_pid
, stdin_fd
, stdout_fd
, stderr_fd
,
166 SlirpGSpawnFds setup
= {
167 .child_setup
= child_setup
,
168 .user_data
= user_data
,
169 .stdin_fd
= stdin_fd
,
170 .stdout_fd
= stdout_fd
,
171 .stderr_fd
= stderr_fd
,
174 return g_spawn_async(working_directory
, argv
, envp
, flags
,
175 slirp_gspawn_fds_setup
, &setup
,
180 #define g_spawn_async_with_fds(wd, argv, env, f, c, d, p, ifd, ofd, efd, err) \
181 g_spawn_async_with_fds_slirp(wd, argv, env, f, c, d, p, ifd, ofd, efd, err)
183 #pragma GCC diagnostic pop
186 fork_exec(struct socket
*so
, const char *ex
)
192 DEBUG_CALL("fork_exec");
193 DEBUG_ARG("so = %p", so
);
194 DEBUG_ARG("ex = %p", ex
);
196 if (slirp_socketpair_with_oob(sp
) < 0) {
200 argv
= g_strsplit(ex
, " ", -1);
201 g_spawn_async_with_fds(NULL
/* cwd */,
205 fork_exec_child_setup
, NULL
/* data */,
206 NULL
/* child_pid */,
212 g_critical("fork_exec: %s", err
->message
);
221 slirp_socket_set_fast_reuse(so
->s
);
223 setsockopt(so
->s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
224 slirp_set_nonblock(so
->s
);
225 so
->slirp
->cb
->register_poll_fd(so
->s
, so
->slirp
->opaque
);
229 char *slirp_connection_info(Slirp
*slirp
)
231 GString
*str
= g_string_new(NULL
);
232 const char * const tcpstates
[] = {
233 [TCPS_CLOSED
] = "CLOSED",
234 [TCPS_LISTEN
] = "LISTEN",
235 [TCPS_SYN_SENT
] = "SYN_SENT",
236 [TCPS_SYN_RECEIVED
] = "SYN_RCVD",
237 [TCPS_ESTABLISHED
] = "ESTABLISHED",
238 [TCPS_CLOSE_WAIT
] = "CLOSE_WAIT",
239 [TCPS_FIN_WAIT_1
] = "FIN_WAIT_1",
240 [TCPS_CLOSING
] = "CLOSING",
241 [TCPS_LAST_ACK
] = "LAST_ACK",
242 [TCPS_FIN_WAIT_2
] = "FIN_WAIT_2",
243 [TCPS_TIME_WAIT
] = "TIME_WAIT",
245 struct in_addr dst_addr
;
246 struct sockaddr_in src
;
253 g_string_append_printf(str
,
254 " Protocol[State] FD Source Address Port "
255 "Dest. Address Port RecvQ SendQ\n");
257 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
; so
= so
->so_next
) {
258 if (so
->so_state
& SS_HOSTFWD
) {
259 state
= "HOST_FORWARD";
260 } else if (so
->so_tcpcb
) {
261 state
= tcpstates
[so
->so_tcpcb
->t_state
];
265 if (so
->so_state
& (SS_HOSTFWD
| SS_INCOMING
)) {
266 src_len
= sizeof(src
);
267 getsockname(so
->s
, (struct sockaddr
*)&src
, &src_len
);
268 dst_addr
= so
->so_laddr
;
269 dst_port
= so
->so_lport
;
271 src
.sin_addr
= so
->so_laddr
;
272 src
.sin_port
= so
->so_lport
;
273 dst_addr
= so
->so_faddr
;
274 dst_port
= so
->so_fport
;
276 snprintf(buf
, sizeof(buf
), " TCP[%s]", state
);
277 g_string_append_printf(str
, "%-19s %3d %15s %5d ", buf
, so
->s
,
278 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*",
279 ntohs(src
.sin_port
));
280 g_string_append_printf(str
, "%15s %5d %5d %5d\n",
281 inet_ntoa(dst_addr
), ntohs(dst_port
),
282 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
285 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
; so
= so
->so_next
) {
286 if (so
->so_state
& SS_HOSTFWD
) {
287 snprintf(buf
, sizeof(buf
), " UDP[HOST_FORWARD]");
288 src_len
= sizeof(src
);
289 getsockname(so
->s
, (struct sockaddr
*)&src
, &src_len
);
290 dst_addr
= so
->so_laddr
;
291 dst_port
= so
->so_lport
;
293 snprintf(buf
, sizeof(buf
), " UDP[%d sec]",
294 (so
->so_expire
- curtime
) / 1000);
295 src
.sin_addr
= so
->so_laddr
;
296 src
.sin_port
= so
->so_lport
;
297 dst_addr
= so
->so_faddr
;
298 dst_port
= so
->so_fport
;
300 g_string_append_printf(str
, "%-19s %3d %15s %5d ", buf
, so
->s
,
301 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*",
302 ntohs(src
.sin_port
));
303 g_string_append_printf(str
, "%15s %5d %5d %5d\n",
304 inet_ntoa(dst_addr
), ntohs(dst_port
),
305 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
308 for (so
= slirp
->icmp
.so_next
; so
!= &slirp
->icmp
; so
= so
->so_next
) {
309 snprintf(buf
, sizeof(buf
), " ICMP[%d sec]",
310 (so
->so_expire
- curtime
) / 1000);
311 src
.sin_addr
= so
->so_laddr
;
312 dst_addr
= so
->so_faddr
;
313 g_string_append_printf(str
, "%-19s %3d %15s - ", buf
, so
->s
,
314 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*");
315 g_string_append_printf(str
, "%15s - %5d %5d\n", inet_ntoa(dst_addr
),
316 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
319 return g_string_free(str
, FALSE
);