WSL/test/linux/unit_tests/getaddrinfo.c
WSL Team 697572d664 Initial open source commit for WSL.
Many Microsoft employees have contributed to the Windows Subsystem for Linux, this commit is the result of their work since 2016.

The entire history of the Windows Subsystem for Linux can't be shared here, but here's an overview of WSL's history after it moved to it own repository in 2021:

Number of commits on the main branch: 2930
Number of contributors: 31

Head over https://github.com/microsoft/WSL/releases for a more detailed history of the features added to WSL since 2021.
2025-05-15 12:09:45 -07:00

91 lines
2 KiB
C

/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
getaddrinfo.c
Abstract:
This file contains tests for getaddrinfo().
--*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "lxtcommon.h"
#include "unittests.h"
#define LXT_NAME "GetAddrInfo"
int LookupHost(const char* Host)
{
char AddressString[100];
char* Argv[3];
char* Envp[1];
struct addrinfo Hints, *Info;
void* Ptr = NULL;
int Result = LXT_RESULT_FAILURE;
memset(&Hints, 0, sizeof(Hints));
Hints.ai_family = PF_UNSPEC;
Hints.ai_socktype = SOCK_STREAM;
Hints.ai_flags |= AI_CANONNAME;
LxtCheckErrno(getaddrinfo(Host, NULL, &Hints, &Info));
LxtLogInfo("Host: %s", Host);
while (Info)
{
inet_ntop(Info->ai_family, Info->ai_addr->sa_data, AddressString, 100);
switch (Info->ai_family)
{
case AF_INET:
Ptr = &((struct sockaddr_in*)Info->ai_addr)->sin_addr;
break;
case AF_INET6:
Ptr = &((struct sockaddr_in6*)Info->ai_addr)->sin6_addr;
break;
default:
LxtLogError("ai_family unexpected %d", Info->ai_family);
goto ErrorExit;
}
inet_ntop(Info->ai_family, Ptr, AddressString, 100);
LxtLogInfo("IPv%d address: %s (%s)", Info->ai_family == PF_INET6 ? 6 : 4, AddressString, Info->ai_canonname);
Info = Info->ai_next;
}
Result = LXT_RESULT_SUCCESS;
ErrorExit:
return 0;
}
int GetAddrInfoTestEntry(int Argc, char* Argv[])
{
LXT_ARGS Args;
int Result = LXT_RESULT_FAILURE;
LxtCheckErrno(LxtInitialize(Argc, Argv, &Args, LXT_NAME));
if (Argc < 2)
{
LxtLogError("Requires HostName as argument");
goto ErrorExit;
}
Result = LookupHost(Argv[1]);
ErrorExit:
return Result;
}