Skip to content

Instantly share code, notes, and snippets.

@colmmacc
Last active December 27, 2015 13:19
Show Gist options
  • Save colmmacc/7332057 to your computer and use it in GitHub Desktop.
Save colmmacc/7332057 to your computer and use it in GitHub Desktop.
Trivial test program to investigate multiple addresses returned by gethostbyname. To run do: gcc -o tester tester.c ; ./tester beta.allcosts.net
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char ** argv)
{
int i;
struct hostent * answer = gethostbyname(argv[1]);
if (!answer) {
return 1;
}
for(i = 0; answer->h_addr_list[i] != NULL; i++) {
struct in_addr in;
memcpy(&in.s_addr, answer->h_addr_list[i], sizeof(in.s_addr));
printf("%s\n", inet_ntoa(in));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment