#include <sys/types.h>
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

main()
{
  int passive, gaierr, inet4 = 0, inet6 = 0;
  struct addrinfo hints, *ai, *aitop;
  char straddr[INET6_ADDRSTRLEN], strport[16];

  for (passive = 0; passive <= 1; passive++) {
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_flags = passive ? AI_PASSIVE : 0;
    hints.ai_socktype = SOCK_STREAM;
    if ((gaierr = getaddrinfo(NULL, "54321", &hints, &aitop)) != 0) {
      (void)gai_strerror(gaierr);
      printf("fail 1\n");
      goto bad;
    }
    for (ai = aitop; ai; ai = ai->ai_next) {
      if (ai->ai_addr == NULL ||
          ai->ai_addrlen == 0 ||
          getnameinfo(ai->ai_addr, ai->ai_addrlen,
                      straddr, sizeof(straddr), strport, sizeof(strport),
                      NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
        printf("fail 2\n");
        goto bad;
      }
      switch (ai->ai_family) {
      case AF_INET:
        if (strcmp(strport, "54321") != 0) {
          printf("fail 3\n");
          goto bad;
        }
        if (passive) {
          if (strcmp(straddr, "0.0.0.0") != 0) {
            printf("fail 4\n");
            goto bad;
          }
        } else {
          if (strcmp(straddr, "127.0.0.1") != 0) {
            printf("fail 5\n");
            goto bad;
          }
        }
        inet4++;
        break;
      case AF_INET6:
        if (strcmp(strport, "54321") != 0) {
          printf("fail 6\n");
          goto bad;
        }
        if (passive) {
          if (strcmp(straddr, "::") != 0) {
            printf("fail 7\n");
            goto bad;
          }
        } else {
          if (strcmp(straddr, "::1") != 0) {
            printf("fail 8\n");
            goto bad;
          }
        }
        inet6++;
        break;
      case AF_UNSPEC:
        printf("fail 9\n");
        goto bad;
        break;
      default:
        /* another family support? */
        break;
      }
    }
  }

  if (!(inet4 == 0 || inet4 == 2)){
    printf("fail 10\n");
    goto bad;
  }
  if (!(inet6 == 0 || inet6 == 2)){
    printf("fail 11\n");
    goto bad;
  }

  if (aitop)
    freeaddrinfo(aitop);
  exit(0);

 bad:
  if (aitop)
    freeaddrinfo(aitop);
  exit(1);
}

 	  	 
