aboutsummaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
authorStefano Garzarella <sgarzare@redhat.com>2019-05-17 15:47:47 +0200
committerJason Wang <jasowang@redhat.com>2019-07-02 10:21:06 +0800
commitadd993477bb2bc17e547912c6530745d1dd58ddc (patch)
tree038077c28a1b918a6ee4bccfd3ff898691aba30a /net
parentc1112b2d3dfb2c4102f73eea8198c4fe9ff50883 (diff)
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>
Diffstat (limited to 'net')
-rw-r--r--net/net.c43
1 files changed, 27 insertions, 16 deletions
diff --git a/net/net.c b/net/net.c
index 76ba0b7800..5ce3996b8d 100644
--- a/net/net.c
+++ b/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,
Error **errp)
{
- char buf[512];
+ gchar **substrings;
struct hostent *he;
- const char *p, *r;
- int port;
+ const char *addr, *p, *r;
+ int port, ret = 0;
- p = str;
- if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
+ substrings = g_strsplit(str, ":", 2);
+ if (!substrings || !substrings[0] || !substrings[1]) {
error_setg(errp, "host address '%s' doesn't contain ':' "
"separating host from port", str);
- return -1;
+ ret = -1;
+ goto out;
}
+
+ addr = substrings[0];
+ p = substrings[1];
+
saddr->sin_family = AF_INET;
- if (buf[0] == '\0') {
+ if (addr[0] == '\0') {
saddr->sin_addr.s_addr = 0;
} else {
- if (qemu_isdigit(buf[0])) {
- if (!inet_aton(buf, &saddr->sin_addr)) {
+ if (qemu_isdigit(addr[0])) {
+ if (!inet_aton(addr, &saddr->sin_addr)) {
error_setg(errp, "host address '%s' is not a valid "
- "IPv4 address", buf);
- return -1;
+ "IPv4 address", addr);
+ ret = -1;
+ goto out;
}
} else {
- he = gethostbyname(buf);
+ he = gethostbyname(addr);
if (he == NULL) {
- error_setg(errp, "can't resolve host address '%s'", buf);
- return - 1;
+ error_setg(errp, "can't resolve host address '%s'", addr);
+ ret = -1;
+ goto out;
}
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);
if (r == p) {
error_setg(errp, "port number '%s' is invalid", p);
- return -1;
+ ret = -1;
+ goto out;
}
saddr->sin_port = htons(port);
- return 0;
+
+out:
+ g_strfreev(substrings);
+ return ret;
}
char *qemu_mac_strdup_printf(const uint8_t *macaddr)