Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

more fun

  • Loading branch information...
commit 84106287466af3608d38e34d804f3598c4cf0f8c 1 parent f1d9fc9
@liweinan authored
Showing with 21 additions and 0 deletions.
  1. +3 −0  c/Makefile
  2. +18 −0 c/gethostbyaddr.c
View
3  c/Makefile
@@ -41,6 +41,9 @@ null.o: null.c
hostname.o: hostname.c
cc hostname.c -o hostname.o
+gethostbyaddr: gethostbyaddr.c
+ cc $< -o $@
+
clean:
rm *.o
rm *.tmp
View
18 c/gethostbyaddr.c
@@ -0,0 +1,18 @@
+#include <arpa/inet.h>
+#include <err.h>
+#include <netdb.h>
+#include <stdio.h>
+
+int main() {
+ const char *ipstr = "127.0.0.1";
+ struct in_addr ip;
+ struct hostent *hp;
+
+ if (!inet_aton(ipstr, &ip))
+ errx(1, "can't parse IP address %s", ipstr);
+
+ if ((hp = gethostbyaddr((const void *)&ip, sizeof ip, AF_INET)) == NULL)
+ errx(1, "no name associated with %s", ipstr);
+
+ printf("name associated with %s is %s\n", ipstr, hp->h_name);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.