]>
Commit | Line | Data |
---|---|---|
f0cbd3ec FB |
1 | /* |
2 | * Copyright (c) 1995 Danny Gasparovski. | |
5fafdf24 | 3 | * |
f0cbd3ec FB |
4 | * Please read the file COPYRIGHT for the |
5 | * terms and conditions of the copyright. | |
6 | */ | |
7 | ||
a9c94277 | 8 | #include "slirp.h" |
f0cbd3ec | 9 | |
f0cbd3ec | 10 | inline void |
511d2b14 | 11 | insque(void *a, void *b) |
f0cbd3ec FB |
12 | { |
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; | |
20 | } | |
21 | ||
22 | inline void | |
511d2b14 | 23 | remque(void *a) |
f0cbd3ec FB |
24 | { |
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; | |
f0cbd3ec FB |
29 | } |
30 | ||
ffe02f55 | 31 | /* TODO: IPv6 */ |
44b4ff24 MAL |
32 | struct gfwd_list * |
33 | add_guestfwd(struct gfwd_list **ex_ptr, | |
34 | SlirpWriteCb write_cb, void *opaque, | |
a13a4126 | 35 | struct in_addr addr, int port) |
f0cbd3ec | 36 | { |
44b4ff24 | 37 | struct gfwd_list *f = g_new0(struct gfwd_list, 1); |
5fafdf24 | 38 | |
44b4ff24 MAL |
39 | f->write_cb = write_cb; |
40 | f->opaque = opaque; | |
41 | f->ex_fport = port; | |
42 | f->ex_addr = addr; | |
43 | f->ex_next = *ex_ptr; | |
44 | *ex_ptr = f; | |
45 | ||
46 | return f; | |
f0cbd3ec FB |
47 | } |
48 | ||
44b4ff24 MAL |
49 | struct gfwd_list * |
50 | add_exec(struct gfwd_list **ex_ptr, const char *cmdline, | |
51 | struct in_addr addr, int port) | |
52 | { | |
53 | struct gfwd_list *f = add_guestfwd(ex_ptr, NULL, NULL, addr, port); | |
54 | ||
55 | f->ex_exec = g_strdup(cmdline); | |
56 | ||
57 | return f; | |
58 | } | |
f0cbd3ec | 59 | |
7f514092 MAL |
60 | static int |
61 | slirp_socketpair_with_oob(int sv[2]) | |
62 | { | |
63 | struct sockaddr_in addr = { | |
64 | .sin_family = AF_INET, | |
65 | .sin_port = 0, | |
66 | .sin_addr.s_addr = INADDR_ANY, | |
67 | }; | |
68 | socklen_t addrlen = sizeof(addr); | |
69 | int ret, s; | |
70 | ||
71 | sv[1] = -1; | |
707bd47e | 72 | s = slirp_socket(AF_INET, SOCK_STREAM, 0); |
7f514092 MAL |
73 | if (s < 0 || bind(s, (struct sockaddr *)&addr, addrlen) < 0 || |
74 | listen(s, 1) < 0 || | |
75 | getsockname(s, (struct sockaddr *)&addr, &addrlen) < 0) { | |
76 | goto err; | |
77 | } | |
78 | ||
707bd47e | 79 | sv[1] = slirp_socket(AF_INET, SOCK_STREAM, 0); |
7f514092 MAL |
80 | if (sv[1] < 0) { |
81 | goto err; | |
82 | } | |
83 | /* | |
84 | * This connect won't block because we've already listen()ed on | |
85 | * the server end (even though we won't accept() the connection | |
86 | * until later on). | |
87 | */ | |
88 | do { | |
89 | ret = connect(sv[1], (struct sockaddr *)&addr, addrlen); | |
90 | } while (ret < 0 && errno == EINTR); | |
91 | if (ret < 0) { | |
92 | goto err; | |
93 | } | |
94 | ||
95 | do { | |
96 | sv[0] = accept(s, (struct sockaddr *)&addr, &addrlen); | |
97 | } while (sv[0] < 0 && errno == EINTR); | |
98 | if (sv[0] < 0) { | |
99 | goto err; | |
100 | } | |
101 | ||
fdbfba8c | 102 | closesocket(s); |
7f514092 MAL |
103 | return 0; |
104 | ||
105 | err: | |
90dfa278 | 106 | g_critical("slirp_socketpair(): %s", strerror(errno)); |
7f514092 | 107 | if (s >= 0) { |
fdbfba8c | 108 | closesocket(s); |
7f514092 MAL |
109 | } |
110 | if (sv[1] >= 0) { | |
fdbfba8c | 111 | closesocket(sv[1]); |
7f514092 MAL |
112 | } |
113 | return -1; | |
114 | } | |
115 | ||
2bdb920e MAL |
116 | static void |
117 | fork_exec_child_setup(gpointer data) | |
118 | { | |
e589a442 | 119 | #ifndef _WIN32 |
2bdb920e | 120 | setsid(); |
e589a442 | 121 | #endif |
2bdb920e MAL |
122 | } |
123 | ||
4ffa6325 ST |
124 | #pragma GCC diagnostic push |
125 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
126 | ||
127 | #if !GLIB_CHECK_VERSION(2, 58, 0) | |
128 | typedef struct SlirpGSpawnFds { | |
129 | GSpawnChildSetupFunc child_setup; | |
130 | gpointer user_data; | |
131 | gint stdin_fd; | |
132 | gint stdout_fd; | |
133 | gint stderr_fd; | |
134 | } SlirpGSpawnFds; | |
135 | ||
136 | static inline void | |
137 | slirp_gspawn_fds_setup(gpointer user_data) | |
138 | { | |
139 | SlirpGSpawnFds *q = (SlirpGSpawnFds *)user_data; | |
140 | ||
141 | dup2(q->stdin_fd, 0); | |
142 | dup2(q->stdout_fd, 1); | |
143 | dup2(q->stderr_fd, 2); | |
144 | q->child_setup(q->user_data); | |
145 | } | |
146 | #endif | |
147 | ||
148 | static inline gboolean | |
149 | g_spawn_async_with_fds_slirp(const gchar *working_directory, | |
150 | gchar **argv, | |
151 | gchar **envp, | |
152 | GSpawnFlags flags, | |
153 | GSpawnChildSetupFunc child_setup, | |
154 | gpointer user_data, | |
155 | GPid *child_pid, | |
156 | gint stdin_fd, | |
157 | gint stdout_fd, | |
158 | gint stderr_fd, | |
159 | GError **error) | |
160 | { | |
161 | #if GLIB_CHECK_VERSION(2, 58, 0) | |
162 | return g_spawn_async_with_fds(working_directory, argv, envp, flags, | |
163 | child_setup, user_data, | |
164 | child_pid, stdin_fd, stdout_fd, stderr_fd, | |
165 | error); | |
166 | #else | |
167 | SlirpGSpawnFds setup = { | |
168 | .child_setup = child_setup, | |
169 | .user_data = user_data, | |
170 | .stdin_fd = stdin_fd, | |
171 | .stdout_fd = stdout_fd, | |
172 | .stderr_fd = stderr_fd, | |
173 | }; | |
174 | ||
175 | return g_spawn_async(working_directory, argv, envp, flags, | |
176 | slirp_gspawn_fds_setup, &setup, | |
177 | child_pid, error); | |
178 | #endif | |
179 | } | |
180 | ||
181 | #define g_spawn_async_with_fds(wd, argv, env, f, c, d, p, ifd, ofd, efd, err) \ | |
182 | g_spawn_async_with_fds_slirp(wd, argv, env, f, c, d, p, ifd, ofd, efd, err) | |
183 | ||
184 | #pragma GCC diagnostic pop | |
185 | ||
f0cbd3ec | 186 | int |
43bc7340 | 187 | fork_exec(struct socket *so, const char *ex) |
f0cbd3ec | 188 | { |
2bdb920e MAL |
189 | GError *err = NULL; |
190 | char **argv; | |
191 | int opt, sp[2]; | |
5fafdf24 | 192 | |
2bdb920e MAL |
193 | DEBUG_CALL("fork_exec"); |
194 | DEBUG_ARG("so = %p", so); | |
195 | DEBUG_ARG("ex = %p", ex); | |
43bc7340 | 196 | |
7f514092 | 197 | if (slirp_socketpair_with_oob(sp) < 0) { |
43bc7340 MAL |
198 | return 0; |
199 | } | |
5fafdf24 | 200 | |
2bdb920e MAL |
201 | argv = g_strsplit(ex, " ", -1); |
202 | g_spawn_async_with_fds(NULL /* cwd */, | |
203 | argv, | |
204 | NULL /* env */, | |
205 | G_SPAWN_SEARCH_PATH, | |
206 | fork_exec_child_setup, NULL /* data */, | |
207 | NULL /* child_pid */, | |
208 | sp[1], sp[1], sp[1], | |
209 | &err); | |
210 | g_strfreev(argv); | |
211 | ||
212 | if (err) { | |
6b744ea4 | 213 | g_critical("fork_exec: %s", err->message); |
2bdb920e | 214 | g_error_free(err); |
fdbfba8c MAL |
215 | closesocket(sp[0]); |
216 | closesocket(sp[1]); | |
2bdb920e MAL |
217 | return 0; |
218 | } | |
219 | ||
220 | so->s = sp[0]; | |
fdbfba8c | 221 | closesocket(sp[1]); |
707bd47e | 222 | slirp_socket_set_fast_reuse(so->s); |
2bdb920e | 223 | opt = 1; |
fdbfba8c | 224 | setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int)); |
848c7092 | 225 | slirp_set_nonblock(so->s); |
3e0fad3a | 226 | so->slirp->cb->register_poll_fd(so->s, so->slirp->opaque); |
2bdb920e | 227 | return 1; |
f0cbd3ec | 228 | } |
f0cbd3ec | 229 | |
b7f43bf2 | 230 | char *slirp_connection_info(Slirp *slirp) |
6dbe553f | 231 | { |
b7f43bf2 | 232 | GString *str = g_string_new(NULL); |
6dbe553f JK |
233 | const char * const tcpstates[] = { |
234 | [TCPS_CLOSED] = "CLOSED", | |
235 | [TCPS_LISTEN] = "LISTEN", | |
236 | [TCPS_SYN_SENT] = "SYN_SENT", | |
237 | [TCPS_SYN_RECEIVED] = "SYN_RCVD", | |
238 | [TCPS_ESTABLISHED] = "ESTABLISHED", | |
239 | [TCPS_CLOSE_WAIT] = "CLOSE_WAIT", | |
240 | [TCPS_FIN_WAIT_1] = "FIN_WAIT_1", | |
241 | [TCPS_CLOSING] = "CLOSING", | |
242 | [TCPS_LAST_ACK] = "LAST_ACK", | |
243 | [TCPS_FIN_WAIT_2] = "FIN_WAIT_2", | |
244 | [TCPS_TIME_WAIT] = "TIME_WAIT", | |
245 | }; | |
246 | struct in_addr dst_addr; | |
247 | struct sockaddr_in src; | |
248 | socklen_t src_len; | |
249 | uint16_t dst_port; | |
250 | struct socket *so; | |
251 | const char *state; | |
252 | char buf[20]; | |
6dbe553f | 253 | |
b7f43bf2 MAL |
254 | g_string_append_printf(str, |
255 | " Protocol[State] FD Source Address Port " | |
256 | "Dest. Address Port RecvQ SendQ\n"); | |
6dbe553f | 257 | |
ffe02f55 ST |
258 | /* TODO: IPv6 */ |
259 | ||
460fec67 | 260 | for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) { |
6dbe553f JK |
261 | if (so->so_state & SS_HOSTFWD) { |
262 | state = "HOST_FORWARD"; | |
263 | } else if (so->so_tcpcb) { | |
264 | state = tcpstates[so->so_tcpcb->t_state]; | |
265 | } else { | |
266 | state = "NONE"; | |
267 | } | |
268 | if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) { | |
269 | src_len = sizeof(src); | |
270 | getsockname(so->s, (struct sockaddr *)&src, &src_len); | |
271 | dst_addr = so->so_laddr; | |
272 | dst_port = so->so_lport; | |
273 | } else { | |
274 | src.sin_addr = so->so_laddr; | |
275 | src.sin_port = so->so_lport; | |
276 | dst_addr = so->so_faddr; | |
277 | dst_port = so->so_fport; | |
278 | } | |
f293d8b1 | 279 | snprintf(buf, sizeof(buf), " TCP[%s]", state); |
b7f43bf2 | 280 | g_string_append_printf(str, "%-19s %3d %15s %5d ", buf, so->s, |
6dbe553f JK |
281 | src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*", |
282 | ntohs(src.sin_port)); | |
b7f43bf2 | 283 | g_string_append_printf(str, "%15s %5d %5d %5d\n", |
6dbe553f JK |
284 | inet_ntoa(dst_addr), ntohs(dst_port), |
285 | so->so_rcv.sb_cc, so->so_snd.sb_cc); | |
286 | } | |
287 | ||
460fec67 | 288 | for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) { |
6dbe553f | 289 | if (so->so_state & SS_HOSTFWD) { |
f293d8b1 | 290 | snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]"); |
6dbe553f JK |
291 | src_len = sizeof(src); |
292 | getsockname(so->s, (struct sockaddr *)&src, &src_len); | |
293 | dst_addr = so->so_laddr; | |
294 | dst_port = so->so_lport; | |
295 | } else { | |
f293d8b1 | 296 | snprintf(buf, sizeof(buf), " UDP[%d sec]", |
6dbe553f JK |
297 | (so->so_expire - curtime) / 1000); |
298 | src.sin_addr = so->so_laddr; | |
299 | src.sin_port = so->so_lport; | |
300 | dst_addr = so->so_faddr; | |
301 | dst_port = so->so_fport; | |
302 | } | |
b7f43bf2 | 303 | g_string_append_printf(str, "%-19s %3d %15s %5d ", buf, so->s, |
6dbe553f JK |
304 | src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*", |
305 | ntohs(src.sin_port)); | |
b7f43bf2 | 306 | g_string_append_printf(str, "%15s %5d %5d %5d\n", |
6dbe553f JK |
307 | inet_ntoa(dst_addr), ntohs(dst_port), |
308 | so->so_rcv.sb_cc, so->so_snd.sb_cc); | |
309 | } | |
e6d43cfb JK |
310 | |
311 | for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) { | |
f293d8b1 | 312 | snprintf(buf, sizeof(buf), " ICMP[%d sec]", |
e6d43cfb JK |
313 | (so->so_expire - curtime) / 1000); |
314 | src.sin_addr = so->so_laddr; | |
315 | dst_addr = so->so_faddr; | |
b7f43bf2 | 316 | g_string_append_printf(str, "%-19s %3d %15s - ", buf, so->s, |
e6d43cfb | 317 | src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*"); |
b7f43bf2 | 318 | g_string_append_printf(str, "%15s - %5d %5d\n", inet_ntoa(dst_addr), |
e6d43cfb JK |
319 | so->so_rcv.sb_cc, so->so_snd.sb_cc); |
320 | } | |
b7f43bf2 MAL |
321 | |
322 | return g_string_free(str, FALSE); | |
6dbe553f | 323 | } |