1224090Sdougb/* 2254402Serwin * Copyright (C) 2009, 2012, 2013 Internet Systems Consortium, Inc. ("ISC") 3224090Sdougb * 4224090Sdougb * Permission to use, copy, modify, and/or distribute this software for any 5224090Sdougb * purpose with or without fee is hereby granted, provided that the above 6224090Sdougb * copyright notice and this permission notice appear in all copies. 7224090Sdougb * 8224090Sdougb * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 9224090Sdougb * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 10224090Sdougb * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 11224090Sdougb * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 12224090Sdougb * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 13224090Sdougb * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 14224090Sdougb * PERFORMANCE OF THIS SOFTWARE. 15224090Sdougb */ 16224090Sdougb 17234010Sdougb/* $Id: sample-gai.c,v 1.4 2009/09/02 23:48:02 tbox Exp $ */ 18224090Sdougb 19224090Sdougb#include <config.h> 20224090Sdougb 21224090Sdougb#include <sys/types.h> 22224090Sdougb#include <sys/socket.h> 23224090Sdougb 24224090Sdougb#include <irs/netdb.h> 25224090Sdougb 26224090Sdougb#include <stdlib.h> 27224090Sdougb#include <string.h> 28224090Sdougb#include <stdio.h> 29224090Sdougb 30224090Sdougbstatic void 31224090Sdougbdo_gai(int family, char *hostname) { 32224090Sdougb struct addrinfo hints, *res, *res0; 33224090Sdougb int error; 34224090Sdougb char namebuf[1024], addrbuf[1024], servbuf[1024]; 35224090Sdougb 36224090Sdougb memset(&hints, 0, sizeof(hints)); 37224090Sdougb hints.ai_family = family; 38224090Sdougb hints.ai_socktype = SOCK_STREAM; 39224090Sdougb hints.ai_flags = AI_CANONNAME; 40224090Sdougb error = getaddrinfo(hostname, "http", &hints, &res0); 41224090Sdougb if (error) { 42224090Sdougb fprintf(stderr, "getaddrinfo failed for %s,family=%d: %s\n", 43224090Sdougb hostname, family, gai_strerror(error)); 44224090Sdougb return; 45224090Sdougb } 46224090Sdougb 47224090Sdougb for (res = res0; res; res = res->ai_next) { 48224090Sdougb error = getnameinfo(res->ai_addr, res->ai_addrlen, 49224090Sdougb addrbuf, sizeof(addrbuf), 50224090Sdougb NULL, 0, NI_NUMERICHOST); 51224090Sdougb if (error == 0) 52224090Sdougb error = getnameinfo(res->ai_addr, res->ai_addrlen, 53224090Sdougb namebuf, sizeof(namebuf), 54224090Sdougb servbuf, sizeof(servbuf), 0); 55224090Sdougb if (error != 0) { 56224090Sdougb fprintf(stderr, "getnameinfo failed: %s\n", 57224090Sdougb gai_strerror(error)); 58224090Sdougb } else { 59224090Sdougb printf("%s(%s/%s)=%s:%s\n", hostname, 60224090Sdougb res->ai_canonname, addrbuf, namebuf, servbuf); 61224090Sdougb } 62224090Sdougb } 63224090Sdougb 64254402Serwin freeaddrinfo(res0); 65224090Sdougb} 66224090Sdougb 67224090Sdougbint 68224090Sdougbmain(int argc, char *argv[]) { 69224090Sdougb if (argc < 2) 70224090Sdougb exit(1); 71224090Sdougb 72224090Sdougb do_gai(AF_INET, argv[1]); 73224090Sdougb do_gai(AF_INET6, argv[1]); 74224090Sdougb do_gai(AF_UNSPEC, argv[1]); 75224090Sdougb 76254402Serwin return (0); 77224090Sdougb} 78