Last active
September 7, 2021 03:29
-
-
Save RoyBellingan/530bacffa1757a53d2416a2d2f5ba13f to your computer and use it in GitHub Desktop.
poor man dig
This file contains 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
#include <arpa/inet.h> | |
#include <netdb.h> | |
#include <netinet/in.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#ifndef NI_MAXHOST | |
#define NI_MAXHOST 1025 | |
#endif | |
int main(void) { | |
struct addrinfo* result; | |
struct addrinfo hints; | |
memset(&hints, 0, sizeof(hints)); | |
hints.ai_protocol = 6; /* TCP See /etc/protocols */ | |
/* resolve the domain name into a list of addresses */ | |
auto error = getaddrinfo("google.com", NULL, &hints, &result); | |
if (error != 0) { | |
if (error == EAI_SYSTEM) { | |
perror("getaddrinfo"); | |
} else { | |
fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error)); | |
} | |
exit(EXIT_FAILURE); | |
} | |
/* loop over all returned results and print as human readable */ | |
for (auto res = result; res != NULL; res = res->ai_next) { | |
char address[INET6_ADDRSTRLEN + 5]; | |
switch (res->ai_family) { | |
case AF_INET: { | |
//Orrible cast which is legal even if ugly | |
auto addr = (struct sockaddr_in*)res->ai_addr; | |
if (NULL == inet_ntop(res->ai_family, | |
&addr->sin_addr, | |
address, | |
sizeof(address))) { | |
perror("inet_ntop"); | |
return EXIT_FAILURE; | |
} | |
} break; | |
case AF_INET6: { | |
//Orrible cast which is legal even if ugly | |
auto addr = (struct sockaddr_in6*)res->ai_addr; | |
if (NULL == inet_ntop(res->ai_family, | |
&addr->sin6_addr, | |
address, | |
sizeof(address))) { | |
perror("inet_ntop"); | |
return EXIT_FAILURE; | |
} | |
} break; | |
default: | |
//??? no idea how to read non IP address sorry (do they even exists any longer ?) | |
break; | |
} | |
printf("%s\n", address); | |
} | |
freeaddrinfo(result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment