mirror of
https://github.com/yarrick/iodine.git
synced 2024-12-04 15:35:13 +00:00
Merge pull request #101 from andcscott/98-get_addr-should-return-neg
Some checks failed
freebsd / build (push) Failing after 5m9s
ubuntu / build (push) Failing after 11s
ubuntu / build-android (push) Successful in 2m37s
openbsd / build (push) Failing after 5m9s
macos / build (push) Has been cancelled
windows / build (push) Has been cancelled
Some checks failed
freebsd / build (push) Failing after 5m9s
ubuntu / build (push) Failing after 11s
ubuntu / build-android (push) Successful in 2m37s
openbsd / build (push) Failing after 5m9s
macos / build (push) Has been cancelled
windows / build (push) Has been cancelled
common: get_addr should return -1 on error
This commit is contained in:
commit
2b65972693
16
src/common.c
16
src/common.c
@ -142,6 +142,7 @@ get_addr(char *host, int port, int addr_family, int flags, struct sockaddr_stora
|
|||||||
struct addrinfo hints, *addr;
|
struct addrinfo hints, *addr;
|
||||||
int res;
|
int res;
|
||||||
char portnum[8];
|
char portnum[8];
|
||||||
|
int addrlen;
|
||||||
|
|
||||||
memset(portnum, 0, sizeof(portnum));
|
memset(portnum, 0, sizeof(portnum));
|
||||||
snprintf(portnum, sizeof(portnum) - 1, "%d", port);
|
snprintf(portnum, sizeof(portnum) - 1, "%d", port);
|
||||||
@ -158,14 +159,15 @@ get_addr(char *host, int port, int addr_family, int flags, struct sockaddr_stora
|
|||||||
hints.ai_protocol = IPPROTO_UDP;
|
hints.ai_protocol = IPPROTO_UDP;
|
||||||
|
|
||||||
res = getaddrinfo(host, portnum, &hints, &addr);
|
res = getaddrinfo(host, portnum, &hints, &addr);
|
||||||
if (res == 0) {
|
if (res != 0) {
|
||||||
int addrlen = addr->ai_addrlen;
|
return -1;
|
||||||
/* Grab first result */
|
|
||||||
memcpy(out, addr->ai_addr, addr->ai_addrlen);
|
|
||||||
freeaddrinfo(addr);
|
|
||||||
return addrlen;
|
|
||||||
}
|
}
|
||||||
return res;
|
|
||||||
|
addrlen = addr->ai_addrlen;
|
||||||
|
/* Grab first result */
|
||||||
|
memcpy(out, addr->ai_addr, addr->ai_addrlen);
|
||||||
|
freeaddrinfo(addr);
|
||||||
|
return addrlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -280,6 +280,28 @@ START_TEST(test_parse_format_ipv4_mapped_ipv6)
|
|||||||
}
|
}
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
START_TEST(test_get_addr_err)
|
||||||
|
{
|
||||||
|
char *host = "192.168.2.10";
|
||||||
|
struct sockaddr_storage addr;
|
||||||
|
int addr_len;
|
||||||
|
int flags = AI_PASSIVE;
|
||||||
|
|
||||||
|
/* Invalid host */
|
||||||
|
addr_len = get_addr(NULL, -1, flags, 0, &addr);
|
||||||
|
ck_assert(addr_len == -1);
|
||||||
|
/* Invalid port */
|
||||||
|
addr_len = get_addr(host, -1, flags, 0, &addr);
|
||||||
|
ck_assert(addr_len == -1);
|
||||||
|
/* Invalid flag */
|
||||||
|
addr_len = get_addr(host, 53, flags | 0xFFF, 0, &addr);
|
||||||
|
ck_assert(addr_len == -1);
|
||||||
|
/* Invalid addr */
|
||||||
|
addr_len = get_addr(host, 53, flags, 0, (struct sockaddr_storage *)NULL);
|
||||||
|
ck_assert(addr_len == -1);
|
||||||
|
}
|
||||||
|
END_TEST
|
||||||
|
|
||||||
TCase *
|
TCase *
|
||||||
test_common_create_tests(void)
|
test_common_create_tests(void)
|
||||||
{
|
{
|
||||||
@ -295,6 +317,7 @@ test_common_create_tests(void)
|
|||||||
tcase_add_test(tc, test_query_datalen_wild);
|
tcase_add_test(tc, test_query_datalen_wild);
|
||||||
tcase_add_test(tc, test_parse_format_ipv4);
|
tcase_add_test(tc, test_parse_format_ipv4);
|
||||||
tcase_add_test(tc, test_parse_format_ipv4_listen_all);
|
tcase_add_test(tc, test_parse_format_ipv4_listen_all);
|
||||||
|
tcase_add_test(tc, test_get_addr_err);
|
||||||
|
|
||||||
/* Tests require IPv6 support */
|
/* Tests require IPv6 support */
|
||||||
sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
Loading…
Reference in New Issue
Block a user