1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:57:44 +00:00

LookupServer+LibC: Add support for reverse DNS lookups via gethostbyaddr().

LookupServer can now take two types of requests:

* L: Lookup
* R: Reverse lookup

The /bin/host program now does a reverse lookup if the input string is a
valid IPv4 address. :^)
This commit is contained in:
Andreas Kling 2019-06-06 05:35:03 +02:00
parent d03505bc29
commit 01f1333856
4 changed files with 156 additions and 46 deletions

View file

@ -1,6 +1,7 @@
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char** argv)
@ -10,9 +11,26 @@ int main(int argc, char** argv)
return 0;
}
// If input looks like an IPv4 address, we should do a reverse lookup.
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(53);
int rc = inet_pton(AF_INET, argv[1], &addr.sin_addr);
if (rc == 1) {
// Okay, let's do a reverse lookup.
auto* hostent = gethostbyaddr(&addr.sin_addr, sizeof(in_addr), AF_INET);
if (!hostent) {
fprintf(stderr, "Reverse lookup failed for '%s'\n", argv[1]);
return 1;
}
printf("%s is %s\n", argv[1], hostent->h_name);
return 0;
}
auto* hostent = gethostbyname(argv[1]);
if (!hostent) {
printf("Lookup failed for '%s'\n", argv[1]);
fprintf(stderr, "Lookup failed for '%s'\n", argv[1]);
return 1;
}