mirror of https://gitee.com/openkylin/qemu.git
net: use g_strsplit() for parsing host address and port
Use the glib function to split host address and port in the parse_host_port() function. Suggested-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Jason Wang <jasowang@redhat.com>
This commit is contained in:
parent
c1112b2d3d
commit
add993477b
43
net/net.c
43
net/net.c
|
@ -87,32 +87,39 @@ static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
|
||||||
int parse_host_port(struct sockaddr_in *saddr, const char *str,
|
int parse_host_port(struct sockaddr_in *saddr, const char *str,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
char buf[512];
|
gchar **substrings;
|
||||||
struct hostent *he;
|
struct hostent *he;
|
||||||
const char *p, *r;
|
const char *addr, *p, *r;
|
||||||
int port;
|
int port, ret = 0;
|
||||||
|
|
||||||
p = str;
|
substrings = g_strsplit(str, ":", 2);
|
||||||
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
|
if (!substrings || !substrings[0] || !substrings[1]) {
|
||||||
error_setg(errp, "host address '%s' doesn't contain ':' "
|
error_setg(errp, "host address '%s' doesn't contain ':' "
|
||||||
"separating host from port", str);
|
"separating host from port", str);
|
||||||
return -1;
|
ret = -1;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addr = substrings[0];
|
||||||
|
p = substrings[1];
|
||||||
|
|
||||||
saddr->sin_family = AF_INET;
|
saddr->sin_family = AF_INET;
|
||||||
if (buf[0] == '\0') {
|
if (addr[0] == '\0') {
|
||||||
saddr->sin_addr.s_addr = 0;
|
saddr->sin_addr.s_addr = 0;
|
||||||
} else {
|
} else {
|
||||||
if (qemu_isdigit(buf[0])) {
|
if (qemu_isdigit(addr[0])) {
|
||||||
if (!inet_aton(buf, &saddr->sin_addr)) {
|
if (!inet_aton(addr, &saddr->sin_addr)) {
|
||||||
error_setg(errp, "host address '%s' is not a valid "
|
error_setg(errp, "host address '%s' is not a valid "
|
||||||
"IPv4 address", buf);
|
"IPv4 address", addr);
|
||||||
return -1;
|
ret = -1;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
he = gethostbyname(buf);
|
he = gethostbyname(addr);
|
||||||
if (he == NULL) {
|
if (he == NULL) {
|
||||||
error_setg(errp, "can't resolve host address '%s'", buf);
|
error_setg(errp, "can't resolve host address '%s'", addr);
|
||||||
return - 1;
|
ret = -1;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
||||||
}
|
}
|
||||||
|
@ -120,10 +127,14 @@ int parse_host_port(struct sockaddr_in *saddr, const char *str,
|
||||||
port = strtol(p, (char **)&r, 0);
|
port = strtol(p, (char **)&r, 0);
|
||||||
if (r == p) {
|
if (r == p) {
|
||||||
error_setg(errp, "port number '%s' is invalid", p);
|
error_setg(errp, "port number '%s' is invalid", p);
|
||||||
return -1;
|
ret = -1;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
saddr->sin_port = htons(port);
|
saddr->sin_port = htons(port);
|
||||||
return 0;
|
|
||||||
|
out:
|
||||||
|
g_strfreev(substrings);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *qemu_mac_strdup_printf(const uint8_t *macaddr)
|
char *qemu_mac_strdup_printf(const uint8_t *macaddr)
|
||||||
|
|
Loading…
Reference in New Issue