diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-08-30 10:32:57 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-08-30 10:32:57 -0700 |
commit | 3c8dc3131c6de3f7816746bec174e82b6b833275 (patch) | |
tree | 97d89c77b2d5da02de1b125418795ff86113b01c /tests/sockets/test_gethostbyname.c | |
parent | 15f4ad9b5ba8095ce1596f8733f03ff39030cd79 (diff) | |
parent | df89e4a8ea28aa3f0bcdd9116d348fb63f6cc406 (diff) |
Merge pull request #1557 from inolen/sockfs
getaddrinfo, freeaddrinfo, getnameinfo support and sockfs
Diffstat (limited to 'tests/sockets/test_gethostbyname.c')
-rw-r--r-- | tests/sockets/test_gethostbyname.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/sockets/test_gethostbyname.c b/tests/sockets/test_gethostbyname.c new file mode 100644 index 00000000..de7da706 --- /dev/null +++ b/tests/sockets/test_gethostbyname.c @@ -0,0 +1,43 @@ +#include <arpa/inet.h> +#include <sys/socket.h> +#include <assert.h> +#include <errno.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +int main() { + char str[INET_ADDRSTRLEN]; + struct in_addr addr; + const char *res; + int err; + + // resolve the hostname ot an actual address + struct hostent *host = gethostbyname("slashdot.org"); + assert(host->h_addrtype == AF_INET); + assert(host->h_length == sizeof(uint32_t)); + + // convert the raw address to a string + char **raw_addr_list = host->h_addr_list; + int *raw_addr = (int*)*raw_addr_list; + res = inet_ntop(host->h_addrtype, raw_addr, str, INET_ADDRSTRLEN); + assert(res); + + // convert the string to an in_addr structure + err = inet_pton(AF_INET, str, &addr); + assert(err == 1); + + // do a reverse lookup on the ip address + struct hostent *host1 = gethostbyaddr(&addr, sizeof(addr), host->h_addrtype); + assert(strstr(host1->h_name, "slashdot.org")); + + puts("success"); + + return EXIT_SUCCESS; +} + |