2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
14 int slirp_debug
= DBG_CALL
|DBG_MISC
|DBG_ERROR
;
18 struct quehead
*qh_link
;
19 struct quehead
*qh_rlink
;
23 insque(void *a
, void *b
)
25 register struct quehead
*element
= (struct quehead
*) a
;
26 register struct quehead
*head
= (struct quehead
*) b
;
27 element
->qh_link
= head
->qh_link
;
28 head
->qh_link
= (struct quehead
*)element
;
29 element
->qh_rlink
= (struct quehead
*)head
;
30 ((struct quehead
*)(element
->qh_link
))->qh_rlink
31 = (struct quehead
*)element
;
37 register struct quehead
*element
= (struct quehead
*) a
;
38 ((struct quehead
*)(element
->qh_link
))->qh_rlink
= element
->qh_rlink
;
39 ((struct quehead
*)(element
->qh_rlink
))->qh_link
= element
->qh_link
;
40 element
->qh_rlink
= NULL
;
43 int add_exec(struct ex_list
**ex_ptr
, int do_pty
, char *exec
,
44 struct in_addr addr
, int port
)
46 struct ex_list
*tmp_ptr
;
48 /* First, check if the port is "bound" */
49 for (tmp_ptr
= *ex_ptr
; tmp_ptr
; tmp_ptr
= tmp_ptr
->ex_next
) {
50 if (port
== tmp_ptr
->ex_fport
&&
51 addr
.s_addr
== tmp_ptr
->ex_addr
.s_addr
)
56 *ex_ptr
= (struct ex_list
*)malloc(sizeof(struct ex_list
));
57 (*ex_ptr
)->ex_fport
= port
;
58 (*ex_ptr
)->ex_addr
= addr
;
59 (*ex_ptr
)->ex_pty
= do_pty
;
60 (*ex_ptr
)->ex_exec
= (do_pty
== 3) ? exec
: strdup(exec
);
61 (*ex_ptr
)->ex_next
= tmp_ptr
;
68 * For systems with no strerror
72 extern char *sys_errlist
[];
79 return sys_errlist
[error
];
81 return "Unknown error.";
90 fork_exec(struct socket
*so
, const char *ex
, int do_pty
)
100 * We create and bind a socket, then fork off to another
101 * process, which connects to this socket, after which we
102 * exec the wanted program. If something (strange) happens,
103 * the accept() call could block us forever.
105 * do_pty = 0 Fork/exec inetd style
106 * do_pty = 1 Fork/exec using slirp.telnetd
107 * do_ptr = 2 Fork/exec using pty
110 fork_exec(struct socket
*so
, const char *ex
, int do_pty
)
113 struct sockaddr_in addr
;
114 socklen_t addrlen
= sizeof(addr
);
117 const char *argv
[256];
118 /* don't want to clobber the original */
124 DEBUG_CALL("fork_exec");
125 DEBUG_ARG("so = %lx", (long)so
);
126 DEBUG_ARG("ex = %lx", (long)ex
);
127 DEBUG_ARG("do_pty = %lx", (long)do_pty
);
132 addr
.sin_family
= AF_INET
;
134 addr
.sin_addr
.s_addr
= INADDR_ANY
;
136 if ((s
= qemu_socket(AF_INET
, SOCK_STREAM
, 0)) < 0 ||
137 bind(s
, (struct sockaddr
*)&addr
, addrlen
) < 0 ||
139 lprint("Error: inet socket: %s\n", strerror(errno
));
149 lprint("Error: fork failed: %s\n", strerror(errno
));
156 /* Set the DISPLAY */
158 (void) close(master
);
159 #ifdef TIOCSCTTY /* XXXXX */
161 ioctl(s
, TIOCSCTTY
, (char *)NULL
);
164 getsockname(s
, (struct sockaddr
*)&addr
, &addrlen
);
167 * Connect to the socket
168 * XXX If any of these fail, we're in trouble!
170 s
= qemu_socket(AF_INET
, SOCK_STREAM
, 0);
171 addr
.sin_addr
= loopback_addr
;
173 ret
= connect(s
, (struct sockaddr
*)&addr
, addrlen
);
174 } while (ret
< 0 && errno
== EINTR
);
180 for (s
= getdtablesize() - 1; s
>= 3; s
--)
184 bptr
= qemu_strdup(ex
); /* No need to free() this */
186 /* Setup "slirp.telnetd -x" */
187 argv
[i
++] = "slirp.telnetd";
192 /* Change the string into argv[] */
194 while (*bptr
!= ' ' && *bptr
!= (char)0)
198 argv
[i
++] = strdup(curarg
);
202 execvp(argv
[0], (char **)argv
);
204 /* Ooops, failed, let's tell the user why */
205 fprintf(stderr
, "Error: execvp of %s failed: %s\n",
206 argv
[0], strerror(errno
));
207 close(0); close(1); close(2); /* XXX */
211 qemu_add_child_watch(pid
);
217 * XXX this could block us...
218 * XXX Should set a timer here, and if accept() doesn't
219 * return after X seconds, declare it a failure
220 * The only reason this will block forever is if socket()
221 * of connect() fail in the child process
224 so
->s
= accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
225 } while (so
->s
< 0 && errno
== EINTR
);
228 setsockopt(so
->s
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&opt
,sizeof(int));
230 setsockopt(so
->s
,SOL_SOCKET
,SO_OOBINLINE
,(char *)&opt
,sizeof(int));
234 /* Append the telnet options now */
235 if (so
->so_m
!= NULL
&& do_pty
== 1) {
236 sbappend(so
, so
->so_m
);
252 bptr
= (char *)malloc(strlen(str
)+1);
261 void lprint(const char *format
, ...)
265 va_start(args
, format
);
266 monitor_vprintf(default_mon
, format
, args
);
279 t
.tv_usec
= usec
* 1000;
281 select(0, &fdset
, &fdset
, &fdset
, &t
);
285 * Set fd blocking and non-blocking
293 unsigned long opt
= 1;
298 ioctlsocket(fd
, FIONBIO
, &opt
);
302 opt
= fcntl(fd
, F_GETFL
, 0);
304 fcntl(fd
, F_SETFL
, opt
);
313 unsigned long opt
= 0;
318 ioctlsocket(fd
, FIONBIO
, &opt
);
322 opt
= fcntl(fd
, F_GETFL
, 0);
324 fcntl(fd
, F_SETFL
, opt
);
328 void slirp_connection_info(Slirp
*slirp
, Monitor
*mon
)
330 const char * const tcpstates
[] = {
331 [TCPS_CLOSED
] = "CLOSED",
332 [TCPS_LISTEN
] = "LISTEN",
333 [TCPS_SYN_SENT
] = "SYN_SENT",
334 [TCPS_SYN_RECEIVED
] = "SYN_RCVD",
335 [TCPS_ESTABLISHED
] = "ESTABLISHED",
336 [TCPS_CLOSE_WAIT
] = "CLOSE_WAIT",
337 [TCPS_FIN_WAIT_1
] = "FIN_WAIT_1",
338 [TCPS_CLOSING
] = "CLOSING",
339 [TCPS_LAST_ACK
] = "LAST_ACK",
340 [TCPS_FIN_WAIT_2
] = "FIN_WAIT_2",
341 [TCPS_TIME_WAIT
] = "TIME_WAIT",
343 struct in_addr dst_addr
;
344 struct sockaddr_in src
;
352 monitor_printf(mon
, " Protocol[State] FD Source Address Port "
353 "Dest. Address Port RecvQ SendQ\n");
355 for (so
= slirp
->tcb
.so_next
; so
!= &slirp
->tcb
; so
= so
->so_next
) {
356 if (so
->so_state
& SS_HOSTFWD
) {
357 state
= "HOST_FORWARD";
358 } else if (so
->so_tcpcb
) {
359 state
= tcpstates
[so
->so_tcpcb
->t_state
];
363 if (so
->so_state
& (SS_HOSTFWD
| SS_INCOMING
)) {
364 src_len
= sizeof(src
);
365 getsockname(so
->s
, (struct sockaddr
*)&src
, &src_len
);
366 dst_addr
= so
->so_laddr
;
367 dst_port
= so
->so_lport
;
369 src
.sin_addr
= so
->so_laddr
;
370 src
.sin_port
= so
->so_lport
;
371 dst_addr
= so
->so_faddr
;
372 dst_port
= so
->so_fport
;
374 n
= snprintf(buf
, sizeof(buf
), " TCP[%s]", state
);
375 memset(&buf
[n
], ' ', 19 - n
);
377 monitor_printf(mon
, "%s %3d %15s %5d ", buf
, so
->s
,
378 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*",
379 ntohs(src
.sin_port
));
380 monitor_printf(mon
, "%15s %5d %5d %5d\n",
381 inet_ntoa(dst_addr
), ntohs(dst_port
),
382 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
385 for (so
= slirp
->udb
.so_next
; so
!= &slirp
->udb
; so
= so
->so_next
) {
386 if (so
->so_state
& SS_HOSTFWD
) {
387 n
= snprintf(buf
, sizeof(buf
), " UDP[HOST_FORWARD]");
388 src_len
= sizeof(src
);
389 getsockname(so
->s
, (struct sockaddr
*)&src
, &src_len
);
390 dst_addr
= so
->so_laddr
;
391 dst_port
= so
->so_lport
;
393 n
= snprintf(buf
, sizeof(buf
), " UDP[%d sec]",
394 (so
->so_expire
- curtime
) / 1000);
395 src
.sin_addr
= so
->so_laddr
;
396 src
.sin_port
= so
->so_lport
;
397 dst_addr
= so
->so_faddr
;
398 dst_port
= so
->so_fport
;
400 memset(&buf
[n
], ' ', 19 - n
);
402 monitor_printf(mon
, "%s %3d %15s %5d ", buf
, so
->s
,
403 src
.sin_addr
.s_addr
? inet_ntoa(src
.sin_addr
) : "*",
404 ntohs(src
.sin_port
));
405 monitor_printf(mon
, "%15s %5d %5d %5d\n",
406 inet_ntoa(dst_addr
), ntohs(dst_port
),
407 so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);