err = getaddrinfo(address, service, &hint, &ai);
if(err) {
- logger(NULL, MESHLINK_WARNING, "Error looking up %s port %s: %s", address, service, err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
+ logger(NULL, MESHLINK_WARNING, "Error looking up %s port %s: %s", address, service, strerror(errno));
return NULL;
}
}
void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) {
- char address[NI_MAXHOST];
- char port[NI_MAXSERV];
- char *scopeid;
- int err;
+ char address[48];
+ char port[16];
+ bool success;
if(sa->sa.sa_family == AF_UNKNOWN) {
if(addrstr) {
return;
}
- err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
+ switch(sa->sa.sa_family) {
+ case AF_INET:
+ snprintf(port, sizeof port, "%d", sa->in.sin_port);
+ success = inet_ntop(AF_INET, &sa->in.sin_addr, address, sizeof(address));
+ break;
- if(err) {
- logger(NULL, MESHLINK_ERROR, "Error while translating addresses: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
- abort();
- }
+ case AF_INET6:
+ snprintf(port, sizeof port, "%d", sa->in6.sin6_port);
+ success = inet_ntop(AF_INET6, &sa->in6.sin6_addr, address, sizeof(address));
+ break;
- scopeid = strchr(address, '%');
+ default:
+ success = false;
+ break;
+ }
- if(scopeid) {
- *scopeid = '\0'; /* Descope. */
+ if(!success) {
+ logger(NULL, MESHLINK_ERROR, "Error while translating addresses: %s", strerror(errno));
+ abort();
}
if(addrstr) {
char *sockaddr2hostname(const sockaddr_t *sa) {
char *str;
- char address[NI_MAXHOST] = "unknown";
- char port[NI_MAXSERV] = "unknown";
- int err;
+ char address[48] = "unknown";
+ char port[16] = "unknown";
+ bool success;
if(sa->sa.sa_family == AF_UNKNOWN) {
xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port);
return str;
}
- err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
+ switch(sa->sa.sa_family) {
+ case AF_INET:
+ snprintf(port, sizeof port, "%d", sa->in.sin_port);
+ success = inet_ntop(AF_INET, &sa->in.sin_addr, address, sizeof(address));
+ break;
- if(err) {
- logger(NULL, MESHLINK_ERROR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err));
+ case AF_INET6:
+ snprintf(port, sizeof port, "%d", sa->in6.sin6_port);
+ success = inet_ntop(AF_INET6, &sa->in6.sin6_addr, address, sizeof(address));
+ break;
+
+ default:
+ success = false;
+ break;
+ }
+
+ if(!success) {
+ logger(NULL, MESHLINK_ERROR, "Error while resolving address to hostname: %s", strerror(errno));
abort();
}
}
}
-void sockaddrunmap(sockaddr_t *sa) {
- if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) {
- sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3];
- sa->in.sin_family = AF_INET;
- }
-}
-
void packmsg_add_sockaddr(packmsg_output_t *out, const sockaddr_t *sa) {
switch(sa->sa.sa_family) {
case AF_INET: {