target/ppc: Re-enable RMLS on POWER9 for virtual hypervisors
[qemu/kevin.git] / slirp / misc.c
blobd9fc586a244bae25a73528edc3a22901f3d56362
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 #include "slirp.h"
10 inline void
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;
22 inline void
23 remque(void *a)
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;
31 struct gfwd_list *
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;
39 f->opaque = opaque;
40 f->ex_fport = port;
41 f->ex_addr = addr;
42 f->ex_next = *ex_ptr;
43 *ex_ptr = f;
45 return f;
48 struct gfwd_list *
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);
56 return f;
59 static int
60 slirp_socketpair_with_oob(int sv[2])
62 struct sockaddr_in addr = {
63 .sin_family = AF_INET,
64 .sin_port = 0,
65 .sin_addr.s_addr = INADDR_ANY,
67 socklen_t addrlen = sizeof(addr);
68 int ret, s;
70 sv[1] = -1;
71 s = slirp_socket(AF_INET, SOCK_STREAM, 0);
72 if (s < 0 || bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
73 listen(s, 1) < 0 ||
74 getsockname(s, (struct sockaddr *)&addr, &addrlen) < 0) {
75 goto err;
78 sv[1] = slirp_socket(AF_INET, SOCK_STREAM, 0);
79 if (sv[1] < 0) {
80 goto err;
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
85 * until later on).
87 do {
88 ret = connect(sv[1], (struct sockaddr *)&addr, addrlen);
89 } while (ret < 0 && errno == EINTR);
90 if (ret < 0) {
91 goto err;
94 do {
95 sv[0] = accept(s, (struct sockaddr *)&addr, &addrlen);
96 } while (sv[0] < 0 && errno == EINTR);
97 if (sv[0] < 0) {
98 goto err;
101 closesocket(s);
102 return 0;
104 err:
105 g_critical("slirp_socketpair(): %s", strerror(errno));
106 if (s >= 0) {
107 closesocket(s);
109 if (sv[1] >= 0) {
110 closesocket(sv[1]);
112 return -1;
115 static void
116 fork_exec_child_setup(gpointer data)
118 #ifndef _WIN32
119 setsid();
120 #endif
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;
129 gpointer user_data;
130 gint stdin_fd;
131 gint stdout_fd;
132 gint stderr_fd;
133 } SlirpGSpawnFds;
135 static inline void
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);
145 #endif
147 static inline gboolean
148 g_spawn_async_with_fds_slirp(const gchar *working_directory,
149 gchar **argv,
150 gchar **envp,
151 GSpawnFlags flags,
152 GSpawnChildSetupFunc child_setup,
153 gpointer user_data,
154 GPid *child_pid,
155 gint stdin_fd,
156 gint stdout_fd,
157 gint stderr_fd,
158 GError **error)
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,
164 error);
165 #else
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,
176 child_pid, error);
177 #endif
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)
188 GError *err = NULL;
189 char **argv;
190 int opt, sp[2];
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) {
197 return 0;
200 argv = g_strsplit(ex, " ", -1);
201 g_spawn_async_with_fds(NULL /* cwd */,
202 argv,
203 NULL /* env */,
204 G_SPAWN_SEARCH_PATH,
205 fork_exec_child_setup, NULL /* data */,
206 NULL /* child_pid */,
207 sp[1], sp[1], sp[1],
208 &err);
209 g_strfreev(argv);
211 if (err) {
212 g_critical("fork_exec: %s", err->message);
213 g_error_free(err);
214 closesocket(sp[0]);
215 closesocket(sp[1]);
216 return 0;
219 so->s = sp[0];
220 closesocket(sp[1]);
221 slirp_socket_set_fast_reuse(so->s);
222 opt = 1;
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);
226 return 1;
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;
247 socklen_t src_len;
248 uint16_t dst_port;
249 struct socket *so;
250 const char *state;
251 char buf[20];
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];
262 } else {
263 state = "NONE";
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;
270 } else {
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;
292 } else {
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);