virnetsockettest: refactor checkProtocols

Reduce variable scope, use g_auto and remove pointless labels.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Ján Tomko 2021-09-04 20:45:33 +02:00
parent 0fc4a43d24
commit 8120df4e85

View File

@ -47,28 +47,29 @@ checkProtocols(bool *hasIPv4, bool *hasIPv6,
{
struct sockaddr_in in4;
struct sockaddr_in6 in6;
int s4 = -1, s6 = -1;
size_t i;
int ret = -1;
*freePort = 0;
if (virNetSocketCheckProtocols(hasIPv4, hasIPv6) < 0)
return -1;
for (i = 0; i < 50; i++) {
VIR_AUTOCLOSE s4 = -1;
VIR_AUTOCLOSE s6 = -1;
if (*hasIPv4) {
if ((s4 = socket(AF_INET, SOCK_STREAM, 0)) < 0)
goto cleanup;
return -1;
}
if (*hasIPv6) {
int only = 1;
if ((s6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0)
goto cleanup;
return -1;
if (setsockopt(s6, IPPROTO_IPV6, IPV6_V6ONLY, &only, sizeof(only)) < 0)
goto cleanup;
return -1;
}
memset(&in4, 0, sizeof(in4));
@ -84,22 +85,18 @@ checkProtocols(bool *hasIPv4, bool *hasIPv6,
if (*hasIPv4) {
if (bind(s4, (struct sockaddr *)&in4, sizeof(in4)) < 0) {
if (errno == EADDRINUSE) {
VIR_FORCE_CLOSE(s4);
VIR_FORCE_CLOSE(s6);
continue;
}
goto cleanup;
return -1;
}
}
if (*hasIPv6) {
if (bind(s6, (struct sockaddr *)&in6, sizeof(in6)) < 0) {
if (errno == EADDRINUSE) {
VIR_FORCE_CLOSE(s4);
VIR_FORCE_CLOSE(s6);
continue;
}
goto cleanup;
return -1;
}
}
@ -109,12 +106,7 @@ checkProtocols(bool *hasIPv4, bool *hasIPv6,
VIR_DEBUG("Choose port %d", *freePort);
ret = 0;
cleanup:
VIR_FORCE_CLOSE(s4);
VIR_FORCE_CLOSE(s6);
return ret;
return 0;
}
struct testClientData {