public void GetAddrInfoWTest() { var hints = new ADDRINFOW { ai_flags = ADDRINFO_FLAGS.AI_CANONNAME, ai_family = ADDRESS_FAMILY.AF_UNSPEC }; Assert.That(GetAddrInfoW(localIP4.ToString(), null, hints, out var res), ResultIs.Successful); TestContext.Write(string.Join(", ", res)); }
private static Win32Error CreateTcpConnection(bool v6, out SafeSOCKET serviceSocket, out SafeSOCKET clientSocket, out SOCKET acceptSocket, out ushort serverPort, out ushort clientPort) { Win32Error status; var aiFamily = v6 ? ADDRESS_FAMILY.AF_INET6 : ADDRESS_FAMILY.AF_INET; var hints = new ADDRINFOW { ai_family = aiFamily, ai_socktype = SOCK.SOCK_STREAM, ai_protocol = IPPROTO.IPPROTO_TCP }; var loopback = v6 ? "::1" : "127.0.0.1"; serviceSocket = SafeSOCKET.INVALID_SOCKET; clientSocket = SafeSOCKET.INVALID_SOCKET; acceptSocket = SOCKET.INVALID_SOCKET; serverPort = clientPort = 0; status = GetAddrInfoW(loopback, "", hints, out var result); if (status.Failed) { goto bail; } using (result) { var localhost = result.FirstOrDefault(); serviceSocket = socket(aiFamily, SOCK.SOCK_STREAM, IPPROTO.IPPROTO_TCP); clientSocket = socket(aiFamily, SOCK.SOCK_STREAM, IPPROTO.IPPROTO_TCP); if (serviceSocket == SOCKET.INVALID_SOCKET || clientSocket == SOCKET.INVALID_SOCKET) { goto bail; } status = bind(serviceSocket, localhost.addr, localhost.ai_addrlen); if (status.Failed) { goto bail; } } var serverSockName = SOCKADDR.Empty; var nameLen = serverSockName.Size; status = getsockname(serviceSocket, serverSockName, ref nameLen); if (status.Failed) { goto bail; } serverPort = v6 ? ((SOCKADDR_IN6)serverSockName).sin6_port : ((SOCKADDR_IN)serverSockName).sin_port; status = listen(serviceSocket, SOMAXCONN); if (status.Failed) { goto bail; } status = connect(clientSocket, serverSockName, nameLen); if (status.Failed) { goto bail; } var clientSockName = SOCKADDR.Empty; nameLen = clientSockName.Size; status = getsockname(clientSocket, clientSockName, ref nameLen); if (status.Failed) { goto bail; } clientPort = v6 ? ((SOCKADDR_IN6)clientSockName).sin6_port : ((SOCKADDR_IN)clientSockName).sin_port; acceptSocket = accept(serviceSocket); if (acceptSocket == SOCKET.INVALID_SOCKET) { goto bail; } return(Win32Error.ERROR_SUCCESS); bail: return(status == -1 ? WSAGetLastError() : status); }