Skip to content

Commit

Permalink
sa: add support for interface suffix for IPv6ll
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 authored and sreimers committed Jul 1, 2021
1 parent f58ec9b commit 14cbecf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/re_sa.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ int sa_decode(struct sa *sa, const char *str, size_t len);
int sa_af(const struct sa *sa);
uint32_t sa_in(const struct sa *sa);
void sa_in6(const struct sa *sa, uint8_t *addr);
int sa_addrinfo(const char *addr, struct sa *sa);

int sa_ntop(const struct sa *sa, char *buf, int size);
int sa_pton(const char *addr, struct sa *sa);
uint16_t sa_port(const struct sa *sa);
Expand Down
39 changes: 37 additions & 2 deletions src/sa/sa.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include <arpa/inet.h>
#endif
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <re_types.h>
#include <re_fmt.h>
#include <re_list.h>
Expand Down Expand Up @@ -58,6 +61,33 @@ int sa_set(struct sa *sa, const struct pl *addr, uint16_t port)
}


int sa_addrinfo(const char *addr, struct sa *sa)
{
struct addrinfo *res, *res0 = NULL;
struct addrinfo hints;
int err = 0;

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

if (getaddrinfo(addr, NULL, &hints, &res0))
return EADDRNOTAVAIL;

for (res = res0; res; res = res->ai_next) {

err = sa_set_sa(sa, res->ai_addr);
if (err)
continue;

break;
}

freeaddrinfo(res0);
return err;
}


/**
* Convert character string to a network address structure
*
Expand All @@ -68,13 +98,18 @@ int sa_set(struct sa *sa, const struct pl *addr, uint16_t port)
*/
int sa_pton(const char *addr, struct sa *sa)
{
if (!addr)
int err = 0;

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

if (inet_pton(AF_INET, addr, &sa->u.in.sin_addr) > 0) {
sa->u.in.sin_family = AF_INET;
}
#ifdef HAVE_INET6
else if (!strncmp(addr, "fe80:", 5)) {
err = sa_addrinfo(addr, sa);
}
else if (inet_pton(AF_INET6, addr, &sa->u.in6.sin6_addr) > 0) {

if (IN6_IS_ADDR_V4MAPPED(&sa->u.in6.sin6_addr)) {
Expand All @@ -91,7 +126,7 @@ int sa_pton(const char *addr, struct sa *sa)
return EINVAL;
}

return 0;
return err;
}


Expand Down

0 comments on commit 14cbecf

Please sign in to comment.