vl: Use exit(1) when requested VGA interface is unavailable
[qemu/ar7.git] / slirp / misc.c
blob1a0ea1be4ef23a3be5d336ef8e0a84f7278c7425
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 "qemu/osdep.h"
9 #include <slirp.h>
10 #include <libslirp.h>
12 #include "monitor/monitor.h"
13 #include "qemu/error-report.h"
14 #include "qemu/main-loop.h"
16 #ifdef DEBUG
17 int slirp_debug = DBG_CALL|DBG_MISC|DBG_ERROR;
18 #endif
20 inline void
21 insque(void *a, void *b)
23 register struct quehead *element = (struct quehead *) a;
24 register struct quehead *head = (struct quehead *) b;
25 element->qh_link = head->qh_link;
26 head->qh_link = (struct quehead *)element;
27 element->qh_rlink = (struct quehead *)head;
28 ((struct quehead *)(element->qh_link))->qh_rlink
29 = (struct quehead *)element;
32 inline void
33 remque(void *a)
35 register struct quehead *element = (struct quehead *) a;
36 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
37 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
38 element->qh_rlink = NULL;
41 int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
42 struct in_addr addr, int port)
44 struct ex_list *tmp_ptr;
46 /* First, check if the port is "bound" */
47 for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
48 if (port == tmp_ptr->ex_fport &&
49 addr.s_addr == tmp_ptr->ex_addr.s_addr)
50 return -1;
53 tmp_ptr = *ex_ptr;
54 *ex_ptr = g_new(struct ex_list, 1);
55 (*ex_ptr)->ex_fport = port;
56 (*ex_ptr)->ex_addr = addr;
57 (*ex_ptr)->ex_pty = do_pty;
58 (*ex_ptr)->ex_exec = (do_pty == 3) ? exec : g_strdup(exec);
59 (*ex_ptr)->ex_next = tmp_ptr;
60 return 0;
64 #ifdef _WIN32
66 int
67 fork_exec(struct socket *so, const char *ex, int do_pty)
69 /* not implemented */
70 return 0;
73 #else
76 * XXX This is ugly
77 * We create and bind a socket, then fork off to another
78 * process, which connects to this socket, after which we
79 * exec the wanted program. If something (strange) happens,
80 * the accept() call could block us forever.
82 * do_pty = 0 Fork/exec inetd style
83 * do_pty = 1 Fork/exec using slirp.telnetd
84 * do_ptr = 2 Fork/exec using pty
86 int
87 fork_exec(struct socket *so, const char *ex, int do_pty)
89 int s;
90 struct sockaddr_in addr;
91 socklen_t addrlen = sizeof(addr);
92 int opt;
93 const char *argv[256];
94 /* don't want to clobber the original */
95 char *bptr;
96 const char *curarg;
97 int c, i, ret;
98 pid_t pid;
100 DEBUG_CALL("fork_exec");
101 DEBUG_ARG("so = %p", so);
102 DEBUG_ARG("ex = %p", ex);
103 DEBUG_ARG("do_pty = %x", do_pty);
105 if (do_pty == 2) {
106 return 0;
107 } else {
108 addr.sin_family = AF_INET;
109 addr.sin_port = 0;
110 addr.sin_addr.s_addr = INADDR_ANY;
112 if ((s = qemu_socket(AF_INET, SOCK_STREAM, 0)) < 0 ||
113 bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
114 listen(s, 1) < 0) {
115 error_report("Error: inet socket: %s", strerror(errno));
116 closesocket(s);
118 return 0;
122 pid = fork();
123 switch(pid) {
124 case -1:
125 error_report("Error: fork failed: %s", strerror(errno));
126 close(s);
127 return 0;
129 case 0:
130 setsid();
132 /* Set the DISPLAY */
133 getsockname(s, (struct sockaddr *)&addr, &addrlen);
134 close(s);
136 * Connect to the socket
137 * XXX If any of these fail, we're in trouble!
139 s = qemu_socket(AF_INET, SOCK_STREAM, 0);
140 addr.sin_addr = loopback_addr;
141 do {
142 ret = connect(s, (struct sockaddr *)&addr, addrlen);
143 } while (ret < 0 && errno == EINTR);
145 dup2(s, 0);
146 dup2(s, 1);
147 dup2(s, 2);
148 for (s = getdtablesize() - 1; s >= 3; s--)
149 close(s);
151 i = 0;
152 bptr = g_strdup(ex); /* No need to free() this */
153 if (do_pty == 1) {
154 /* Setup "slirp.telnetd -x" */
155 argv[i++] = "slirp.telnetd";
156 argv[i++] = "-x";
157 argv[i++] = bptr;
158 } else
159 do {
160 /* Change the string into argv[] */
161 curarg = bptr;
162 while (*bptr != ' ' && *bptr != (char)0)
163 bptr++;
164 c = *bptr;
165 *bptr++ = (char)0;
166 argv[i++] = g_strdup(curarg);
167 } while (c);
169 argv[i] = NULL;
170 execvp(argv[0], (char **)argv);
172 /* Ooops, failed, let's tell the user why */
173 fprintf(stderr, "Error: execvp of %s failed: %s\n",
174 argv[0], strerror(errno));
175 close(0); close(1); close(2); /* XXX */
176 exit(1);
178 default:
179 qemu_add_child_watch(pid);
181 * XXX this could block us...
182 * XXX Should set a timer here, and if accept() doesn't
183 * return after X seconds, declare it a failure
184 * The only reason this will block forever is if socket()
185 * of connect() fail in the child process
187 do {
188 so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
189 } while (so->s < 0 && errno == EINTR);
190 closesocket(s);
191 socket_set_fast_reuse(so->s);
192 opt = 1;
193 qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
194 qemu_set_nonblock(so->s);
196 /* Append the telnet options now */
197 if (so->so_m != NULL && do_pty == 1) {
198 sbappend(so, so->so_m);
199 so->so_m = NULL;
202 return 1;
205 #endif
207 void slirp_connection_info(Slirp *slirp, Monitor *mon)
209 const char * const tcpstates[] = {
210 [TCPS_CLOSED] = "CLOSED",
211 [TCPS_LISTEN] = "LISTEN",
212 [TCPS_SYN_SENT] = "SYN_SENT",
213 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
214 [TCPS_ESTABLISHED] = "ESTABLISHED",
215 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
216 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
217 [TCPS_CLOSING] = "CLOSING",
218 [TCPS_LAST_ACK] = "LAST_ACK",
219 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
220 [TCPS_TIME_WAIT] = "TIME_WAIT",
222 struct in_addr dst_addr;
223 struct sockaddr_in src;
224 socklen_t src_len;
225 uint16_t dst_port;
226 struct socket *so;
227 const char *state;
228 char buf[20];
230 monitor_printf(mon, " Protocol[State] FD Source Address Port "
231 "Dest. Address Port RecvQ SendQ\n");
233 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
234 if (so->so_state & SS_HOSTFWD) {
235 state = "HOST_FORWARD";
236 } else if (so->so_tcpcb) {
237 state = tcpstates[so->so_tcpcb->t_state];
238 } else {
239 state = "NONE";
241 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
242 src_len = sizeof(src);
243 getsockname(so->s, (struct sockaddr *)&src, &src_len);
244 dst_addr = so->so_laddr;
245 dst_port = so->so_lport;
246 } else {
247 src.sin_addr = so->so_laddr;
248 src.sin_port = so->so_lport;
249 dst_addr = so->so_faddr;
250 dst_port = so->so_fport;
252 snprintf(buf, sizeof(buf), " TCP[%s]", state);
253 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
254 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
255 ntohs(src.sin_port));
256 monitor_printf(mon, "%15s %5d %5d %5d\n",
257 inet_ntoa(dst_addr), ntohs(dst_port),
258 so->so_rcv.sb_cc, so->so_snd.sb_cc);
261 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
262 if (so->so_state & SS_HOSTFWD) {
263 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
264 src_len = sizeof(src);
265 getsockname(so->s, (struct sockaddr *)&src, &src_len);
266 dst_addr = so->so_laddr;
267 dst_port = so->so_lport;
268 } else {
269 snprintf(buf, sizeof(buf), " UDP[%d sec]",
270 (so->so_expire - curtime) / 1000);
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 monitor_printf(mon, "%-19s %3d %15s %5d ", buf, so->s,
277 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
278 ntohs(src.sin_port));
279 monitor_printf(mon, "%15s %5d %5d %5d\n",
280 inet_ntoa(dst_addr), ntohs(dst_port),
281 so->so_rcv.sb_cc, so->so_snd.sb_cc);
284 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
285 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
286 (so->so_expire - curtime) / 1000);
287 src.sin_addr = so->so_laddr;
288 dst_addr = so->so_faddr;
289 monitor_printf(mon, "%-19s %3d %15s - ", buf, so->s,
290 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
291 monitor_printf(mon, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
292 so->so_rcv.sb_cc, so->so_snd.sb_cc);