@@ -130,18 +130,15 @@ static int tcp_client_connect(int fd, struct addrinfo *rp)
return ret;
}
-static int tcp_start_common(const char *str, int *fd, bool server)
+static int parse_host_port_info(struct addrinfo **result, const char *str,
+ bool server)
{
char hostname[512];
const char *service;
const char *name;
struct addrinfo hints;
- struct addrinfo *result, *rp;
int s;
- int sfd;
- int ret = -EINVAL;
- *fd = -1;
service = str;
if (get_str_sep(hostname, sizeof(hostname), &service, ':') < 0) {
@@ -164,12 +161,29 @@ static int tcp_start_common(const char *str, int *fd, bool server)
hints.ai_flags = AI_PASSIVE;
}
- s = getaddrinfo(name, service, &hints, &result);
+ s = getaddrinfo(name, service, &hints, result);
if (s != 0) {
error_report("qemu: getaddrinfo: %s", gai_strerror(s));
return -EINVAL;
}
+ return 0;
+}
+
+static int tcp_start_common(const char *str, int *fd, bool server)
+{
+ struct addrinfo *rp;
+ int sfd;
+ int ret = -EINVAL;
+ struct addrinfo *result;
+
+ *fd = -1;
+
+ ret = parse_host_port_info(&result, str, server);
+ if (ret < 0) {
+ return -EINVAL;
+ }
+
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully bind/connect).
If socket(2) (or bind/connect(2)) fails, we (close the socket
int parse_host_port(struct sockaddr_in *saddr, const char *str) Parsed address info will be restored into 'saddr', it only support ipv4. This function is used by net_socket_mcast_init() and net_socket_udp_init(). int parse_host_port_info(struct addrinfo *result, const char *str) Parsed address info will be restored into 'result', it's an address list. It can be used to parse IPv6 address/port. Signed-off-by: Amos Kong <akong@redhat.com> --- net.c | 26 ++++++++++++++++++++------ 1 files changed, 20 insertions(+), 6 deletions(-)