Commit 9c12a6f24d8bfd0e0d81a4a77f515e32d15547c1
Committed by
Anthony Liguori
1 parent
6dd5ffb6
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>
Showing
2 changed files
with
9 additions
and
13 deletions
net.c
... | ... | @@ -880,7 +880,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str) |
880 | 880 | char buf[256] = ""; |
881 | 881 | const char *p = src_str; |
882 | 882 | int is_udp = 0; |
883 | - int n; | |
883 | + int err; | |
884 | 884 | |
885 | 885 | if (!slirp_inited) { |
886 | 886 | 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) |
909 | 909 | |
910 | 910 | host_port = atoi(p); |
911 | 911 | |
912 | - n = slirp_remove_hostfwd(is_udp, host_addr, host_port); | |
912 | + err = slirp_remove_hostfwd(is_udp, host_addr, host_port); | |
913 | 913 | |
914 | - monitor_printf(mon, "removed %d host forwarding rules for %s\n", n, | |
915 | - src_str); | |
914 | + monitor_printf(mon, "host forwarding rule for %s %s\n", src_str, | |
915 | + err ? "removed" : "not found"); | |
916 | 916 | return; |
917 | 917 | |
918 | 918 | fail_syntax: | ... | ... |
slirp/slirp.c
... | ... | @@ -757,9 +757,7 @@ void if_encap(const uint8_t *ip_data, int ip_data_len) |
757 | 757 | } |
758 | 758 | } |
759 | 759 | |
760 | -/* Unlistens a redirection | |
761 | - * | |
762 | - * Return value: number of redirs removed */ | |
760 | +/* Drop host forwarding rule, return 0 if found. */ | |
763 | 761 | int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port) |
764 | 762 | { |
765 | 763 | struct socket *so; |
... | ... | @@ -767,22 +765,20 @@ int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port) |
767 | 765 | struct sockaddr_in addr; |
768 | 766 | int port = htons(host_port); |
769 | 767 | socklen_t addr_len; |
770 | - int n = 0; | |
771 | 768 | |
772 | - loop_again: | |
773 | 769 | for (so = head->so_next; so != head; so = so->so_next) { |
774 | 770 | addr_len = sizeof(addr); |
775 | - if (getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 && | |
771 | + if ((so->so_state & SS_HOSTFWD) && | |
772 | + getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 && | |
776 | 773 | addr.sin_addr.s_addr == host_addr.s_addr && |
777 | 774 | addr.sin_port == port) { |
778 | 775 | close(so->s); |
779 | 776 | sofree(so); |
780 | - n++; | |
781 | - goto loop_again; | |
777 | + return 0; | |
782 | 778 | } |
783 | 779 | } |
784 | 780 | |
785 | - return n; | |
781 | + return -1; | |
786 | 782 | } |
787 | 783 | |
788 | 784 | int slirp_add_hostfwd(int is_udp, struct in_addr host_addr, int host_port, | ... | ... |