Skip to content

Commit

Permalink
slirp: Add explanation for hostfwd parsing failure
Browse files Browse the repository at this point in the history
e.g.
./x86_64-softmmu/qemu-system-x86_64 -nographic -netdev 'user,id=vnet,hostfwd=:555.0.0.0:0-:22'
qemu-system-x86_64: -netdev user,id=vnet,hostfwd=:555.0.0.0:0-:22: Invalid host forwarding rule ':555.0.0.0:0-:22' (Bad host address)

Signed-off-by: Dr. David Alan Gilbert <[email protected]>
Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
Signed-off-by: Samuel Thibault <[email protected]>
  • Loading branch information
dagrh authored and sthibaul committed Sep 24, 2017
1 parent 460b6c8 commit 0e7e4fb
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion net/slirp.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,45 +496,55 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str,
char buf[256];
int is_udp;
char *end;
const char *fail_reason = "Unknown reason";

p = redir_str;
if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
fail_reason = "No : separators";
goto fail_syntax;
}
if (!strcmp(buf, "tcp") || buf[0] == '\0') {
is_udp = 0;
} else if (!strcmp(buf, "udp")) {
is_udp = 1;
} else {
fail_reason = "Bad protocol name";
goto fail_syntax;
}

if (!legacy_format) {
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
fail_reason = "Missing : separator";
goto fail_syntax;
}
if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
fail_reason = "Bad host address";
goto fail_syntax;
}
}

if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) {
fail_reason = "Bad host port separator";
goto fail_syntax;
}
host_port = strtol(buf, &end, 0);
if (*end != '\0' || host_port < 0 || host_port > 65535) {
fail_reason = "Bad host port";
goto fail_syntax;
}

if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
fail_reason = "Missing guest address";
goto fail_syntax;
}
if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
fail_reason = "Bad guest address";
goto fail_syntax;
}

guest_port = strtol(p, &end, 0);
if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
fail_reason = "Bad guest port";
goto fail_syntax;
}

Expand All @@ -547,7 +557,8 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str,
return 0;

fail_syntax:
error_setg(errp, "Invalid host forwarding rule '%s'", redir_str);
error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
fail_reason);
return -1;
}

Expand Down

0 comments on commit 0e7e4fb

Please sign in to comment.