// Source: https://basepath.com/aup/ex/adi_8c-source.html
// Rochkind's Book, page 574

#include <stdio.h>
#include <sys/socket.h> 
#include <arpa/inet.h>  
#include <unistd.h>     
#include <string.h>     
#include <stdlib.h>
#include <netdb.h>

int main (int argc, char * argv[]) {
    int retVal; char err[50];
    struct addrinfo *infop = NULL, hint;
    memset(&hint, 0, sizeof(hint));
    hint.ai_family = AF_INET;
    hint.ai_socktype = SOCK_STREAM;
    if (argc!=2) {
       printf ("Usage: inetExample xxx.xxx.xxx.xxx\n");
       exit (-1); }    
    retVal = getaddrinfo(argv[1], "80", &hint, &infop);
    if (retVal!=0) {
        strcpy (err,gai_strerror(retVal));
        printf ("Error found ==> %s\n", err);
        exit (-1); }
    for ( ; infop != NULL; infop = infop->ai_next) {
        struct sockaddr_in *sa = (struct sockaddr_in *)infop->ai_addr;
        printf("%s port: %d protocol: %d\n", inet_ntoa(sa->sin_addr),
               ntohs(sa->sin_port), infop->ai_protocol); }
    exit(0);}
    
    
    
    
