| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | getaddrinfo.c |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains tests for getaddrinfo(). |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <netdb.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <arpa/inet.h> |
| 22 | #include "lxtcommon.h" |
| 23 | #include "unittests.h" |
| 24 | |
| 25 | #define LXT_NAME "GetAddrInfo" |
| 26 | |
| 27 | int LookupHost(const char* Host) |
| 28 | { |
| 29 | |
| 30 | char AddressString[100]; |
| 31 | char* Argv[3]; |
| 32 | char* Envp[1]; |
| 33 | struct addrinfo Hints, *Info; |
| 34 | void* Ptr = NULL; |
| 35 | int Result = LXT_RESULT_FAILURE; |
| 36 | |
| 37 | memset(&Hints, 0, sizeof(Hints)); |
| 38 | Hints.ai_family = PF_UNSPEC; |
| 39 | Hints.ai_socktype = SOCK_STREAM; |
| 40 | Hints.ai_flags |= AI_CANONNAME; |
| 41 | |
| 42 | LxtCheckErrno(getaddrinfo(Host, NULL, &Hints, &Info)); |
| 43 | LxtLogInfo("Host: %s", Host); |
| 44 | while (Info) |
| 45 | { |
| 46 | inet_ntop(Info->ai_family, Info->ai_addr->sa_data, AddressString, 100); |
| 47 | switch (Info->ai_family) |
| 48 | { |
| 49 | case AF_INET: |
| 50 | Ptr = &((struct sockaddr_in*)Info->ai_addr)->sin_addr; |
| 51 | break; |
| 52 | |
| 53 | case AF_INET6: |
| 54 | Ptr = &((struct sockaddr_in6*)Info->ai_addr)->sin6_addr; |
| 55 | break; |
| 56 | |
| 57 | default: |
| 58 | LxtLogError("ai_family unexpected %d", Info->ai_family); |
| 59 | goto ErrorExit; |
| 60 | } |
| 61 | |
| 62 | inet_ntop(Info->ai_family, Ptr, AddressString, 100); |
| 63 | LxtLogInfo("IPv%d address: %s (%s)", Info->ai_family == PF_INET6 ? 6 : 4, AddressString, Info->ai_canonname); |
| 64 | |
| 65 | Info = Info->ai_next; |
| 66 | } |
| 67 | |
| 68 | Result = LXT_RESULT_SUCCESS; |
| 69 | |
| 70 | ErrorExit: |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | int GetAddrInfoTestEntry(int Argc, char* Argv[]) |
| 75 | { |
| 76 | |
| 77 | LXT_ARGS Args; |
| 78 | int Result = LXT_RESULT_FAILURE; |
| 79 | |
| 80 | LxtCheckErrno(LxtInitialize(Argc, Argv, &Args, LXT_NAME)); |
| 81 | if (Argc < 2) |
| 82 | { |
| 83 | LxtLogError("Requires HostName as argument"); |
| 84 | goto ErrorExit; |
| 85 | } |
| 86 | |
| 87 | Result = LookupHost(Argv[1]); |
| 88 | |
| 89 | ErrorExit: |
| 90 | return Result; |
| 91 | } |