summaryrefslogtreecommitdiff
path: root/Userland/host.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-06-06 05:35:03 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-06-06 05:35:03 +0200
commit01f13338566211b3d20bd922597f546d184a70c3 (patch)
treefe073abe3b41e85d713c4d82fa0298a72d3533c6 /Userland/host.cpp
parentd03505bc29e138ee31e04e69c31d4097d236cd40 (diff)
downloadserenity-01f13338566211b3d20bd922597f546d184a70c3.zip
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. :^)
Diffstat (limited to 'Userland/host.cpp')
-rw-r--r--Userland/host.cpp20
1 files changed, 19 insertions, 1 deletions
diff --git a/Userland/host.cpp b/Userland/host.cpp
index 179e5a649e..52e307ad9b 100644
--- a/Userland/host.cpp
+++ b/Userland/host.cpp
@@ -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;
}