Created
January 18, 2013 17:39
-
-
Save karlr42/4566462 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#define BROKEN_DOMAIN "word-view.beta.officeapps.live.com" | |
#define BUF_SIZE 500 | |
/* | |
* This is adopted from the example echo client code in the getaddrinfo | |
* man page, just shortened and modified so it just resolves BROKEN_DOMAIN | |
* and prints the IP address to stderr. | |
* Compiled with gcc getaddrinfoTest.c ; Ran with ./a.out | |
*/ | |
void *get_in_addr(struct sockaddr *sa) | |
{ | |
if (sa->sa_family == AF_INET) | |
return &(((struct sockaddr_in*)sa)->sin_addr); | |
return &(((struct sockaddr_in6*)sa)->sin6_addr); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
struct addrinfo hints; | |
struct addrinfo *result, *rp; | |
int sfd, s, j; | |
size_t len; | |
ssize_t nread; | |
char buf[BUF_SIZE]; | |
/* Obtain address(es) matching host/port */ | |
/* | |
//How the hints stucture is initialised in the example code | |
memset(&hints, 0, sizeof(struct addrinfo)); | |
hints.ai_family = AF_UNSPEC; //Allow IPv4 or IPv6 | |
hints.ai_socktype = SOCK_DGRAM; // Datagram socket | |
hints.ai_flags = 0; | |
hints.ai_protocol = 0; // Any protocol | |
*/ | |
//How the hints stucture is initialised in DNS::hostByAddress | |
memset(&hints, 0, sizeof(hints)); | |
hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; //removing AI_ADDRCONFIG prevents the resolution delay | |
//make the system call | |
s = getaddrinfo(BROKEN_DOMAIN, NULL, &hints, &result); //hangs for about 5 seconds | |
if (s != 0) { | |
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); | |
exit(EXIT_FAILURE); | |
} | |
char addrString[INET6_ADDRSTRLEN]; | |
inet_ntop(result->ai_family, get_in_addr((struct sockaddr *)result->ai_addr), addrString, sizeof addrString); | |
printf(addrString,"%s"); printf("\n","%s"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment