mirror of
https://gitlab.com/niansa/nosni.git
synced 2025-03-06 20:53:26 +01:00
29 lines
748 B
C
29 lines
748 B
C
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
const char *resolve_hostname_to_str(const char *hostname) {
|
|
struct addrinfo *addrInfo;
|
|
char str[INET_ADDRSTRLEN];
|
|
|
|
// Set up hints
|
|
struct addrinfo hints;
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
hints.ai_family = AF_INET; //TODO: Care about IPv6
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
hints.ai_flags = AI_CANONNAME;
|
|
|
|
// Get addrinfo
|
|
int error = getaddrinfo(hostname, "https", &hints, &addrInfo);
|
|
if (addrInfo == NULL) return NULL;
|
|
|
|
// Return addr
|
|
struct sockaddr_in *addr = (struct sockaddr_in *)addrInfo->ai_addr;
|
|
return inet_ntoa((struct in_addr)addr->sin_addr);
|
|
}
|