Last active
May 11, 2022 16:24
-
-
Save Low-power/bb9e4ad545d3d5b77d224e7c2339c6ae to your computer and use it in GitHub Desktop.
Workaround getaddrinfo(3) from the dumb AIX C library performing DNS requests on numeric IP addresses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright 2015-2022 Rivoreo | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE | |
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#include <dlfcn.h> | |
#include <stdlib.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <stdio.h> | |
static void *get_libc() { | |
static void *libc; | |
if(!libc) libc = dlopen("/usr/lib/libc.a(shr.o)", RTLD_NOW | RTLD_LOCAL | RTLD_MEMBER | RTLD_NOAUTODEFER); | |
return libc; | |
} | |
static int (*get_libc_getaddrinfo())(const char *, const char *, const struct addrinfo *, struct addrinfo **) { | |
static int (*f)(const char *, const char *, const struct addrinfo *, struct addrinfo **); | |
if(f) return f; | |
void *libc = get_libc(); | |
if(!libc) { | |
perror("dlopen"); | |
abort(); | |
} | |
*(void **)&f = dlsym(libc, "getaddrinfo"); | |
if(!f) { | |
perror("dlsym: getaddrinfo"); | |
abort(); | |
} | |
return f; | |
} | |
int getaddrinfo(const char *host, const char *serv, const struct addrinfo *hints, struct addrinfo **result) { | |
int (*libc_getaddrinfo)(const char *, const char *, const struct addrinfo *, struct addrinfo **) = get_libc_getaddrinfo(); | |
if(!host) return libc_getaddrinfo(host, serv, hints, result); | |
if(hints && (hints->ai_flags & AI_ADDRCONFIG)) return libc_getaddrinfo(host, serv, hints, result); | |
int family = hints ? hints->ai_family : AF_INET; | |
switch(family) { | |
case AF_UNSPEC: | |
family = AF_INET; | |
break; | |
case AF_INET: | |
case AF_INET6: | |
break; | |
default: | |
return EAI_FAMILY; | |
} | |
unsigned int port; | |
if(serv) { | |
char *end_p; | |
port = strtoul(serv, &end_p, 0); | |
if(*end_p) { | |
return hints && (hints->ai_flags & AI_NUMERICSERV) ? | |
EAI_NONAME : libc_getaddrinfo(host, serv, hints, result); | |
} | |
} | |
union { | |
struct in_addr inet4_address; | |
struct in6_addr inet6_address; | |
} address; | |
//if(host) { | |
if(inet_pton(family, host, &address) < 1) { | |
return hints && (hints->ai_flags & AI_NUMERICHOST) ? | |
EAI_NONAME : libc_getaddrinfo(host, serv, hints, result); | |
} | |
//} | |
*result = malloc(sizeof(struct addrinfo)); | |
if(!*result) return EAI_MEMORY; | |
memset(*result, 0, sizeof(struct addrinfo)); | |
(*result)->ai_family = family; | |
switch(family) { | |
case AF_INET: | |
(*result)->ai_addrlen = sizeof(struct sockaddr_in); | |
(*result)->ai_addr = malloc((*result)->ai_addrlen); | |
if(!(*result)->ai_addr) { | |
free(*result); | |
return EAI_MEMORY; | |
} | |
memset((*result)->ai_addr, 0, (*result)->ai_addrlen); | |
((struct sockaddr_in *)(*result)->ai_addr)->sin_addr = address.inet4_address; | |
if(serv) ((struct sockaddr_in *)(*result)->ai_addr)->sin_port = htons(port); | |
break; | |
case AF_INET6: | |
(*result)->ai_addrlen = sizeof(struct sockaddr_in6); | |
(*result)->ai_addr = malloc((*result)->ai_addrlen); | |
if(!(*result)->ai_addr) { | |
free(*result); | |
return EAI_MEMORY; | |
} | |
memset((*result)->ai_addr, 0, (*result)->ai_addrlen); | |
((struct sockaddr_in6 *)(*result)->ai_addr)->sin6_addr = address.inet6_address; | |
if(serv) ((struct sockaddr_in6 *)(*result)->ai_addr)->sin6_port = htons(port); | |
break; | |
} | |
(*result)->ai_addr->sa_family = family; | |
(*result)->ai_socktype = hints ? hints->ai_socktype : SOCK_STREAM; | |
if(hints) (*result)->ai_protocol = hints->ai_protocol; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment