slirp: Do not allow to remove non-hostfwd sockets

Prevent that the users accidentally shoots down dynamic sockets. This
allows to remove looping for removals as there can now only be one
match.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Jan Kiszka 2009-06-24 14:42:29 +02:00 committed by Anthony Liguori
parent 6dd5ffb661
commit 9c12a6f24d
2 changed files with 9 additions and 13 deletions

8
net.c
View file

@ -880,7 +880,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
char buf[256] = "";
const char *p = src_str;
int is_udp = 0;
int n;
int err;
if (!slirp_inited) {
monitor_printf(mon, "user mode network stack not in use\n");
@ -909,10 +909,10 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str)
host_port = atoi(p);
n = slirp_remove_hostfwd(is_udp, host_addr, host_port);
err = slirp_remove_hostfwd(is_udp, host_addr, host_port);
monitor_printf(mon, "removed %d host forwarding rules for %s\n", n,
src_str);
monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
err ? "removed" : "not found");
return;
fail_syntax:

View file

@ -757,9 +757,7 @@ void if_encap(const uint8_t *ip_data, int ip_data_len)
}
}
/* Unlistens a redirection
*
* Return value: number of redirs removed */
/* Drop host forwarding rule, return 0 if found. */
int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port)
{
struct socket *so;
@ -767,22 +765,20 @@ int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port)
struct sockaddr_in addr;
int port = htons(host_port);
socklen_t addr_len;
int n = 0;
loop_again:
for (so = head->so_next; so != head; so = so->so_next) {
addr_len = sizeof(addr);
if (getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
if ((so->so_state & SS_HOSTFWD) &&
getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
addr.sin_addr.s_addr == host_addr.s_addr &&
addr.sin_port == port) {
close(so->s);
sofree(so);
n++;
goto loop_again;
return 0;
}
}
return n;
return -1;
}
int slirp_add_hostfwd(int is_udp, struct in_addr host_addr, int host_port,