Skip to content

Commit

Permalink
net: add IP regex check for net_resolve_address()
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 committed Jun 22, 2021
1 parent c3a585f commit f11b995
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/net/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,28 @@ int net_resolve_address(const char *addr, struct sa *sa)
{
struct addrinfo *res, *res0 = NULL;
struct addrinfo hints;
struct pl pl;
int err;

if (!str_isset(addr) || !sa)
return EINVAL;

/* Try IPv6 first */
err = re_regex(addr, strlen(addr), "[0-9a-f]+:[0-9a-f]*:[0-9a-f:]+",
&pl, NULL, NULL);

if (!err && pl.p>addr)
return EINVAL;

/* Then non-IPv6 host */
if (err)
err = re_regex(addr, strlen(addr),
"[0-9]+.[0-9]+.[0-9]+.[0-9]+",
NULL, NULL, NULL, NULL);

if (err)
return EINVAL;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_NUMERICHOST;
Expand Down

0 comments on commit f11b995

Please sign in to comment.