Index: base/services/dhcpcsvc/dhcp/dhclient.c =================================================================== --- base/services/dhcpcsvc/dhcp/dhclient.c (revision 72643) +++ base/services/dhcpcsvc/dhcp/dhclient.c (working copy) @@ -447,6 +447,37 @@ } +void set_domain( PDHCP_ADAPTER Adapter, struct client_lease *new_lease ) { + CHAR Buffer1[MAX_PATH] = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"; + CHAR Buffer2[MAX_PATH] = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"; + HKEY RegKey1, RegKey2; + + strcat(Buffer1, Adapter->DhclientInfo.name); + if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, Buffer1, 0, KEY_WRITE, &RegKey1 ) != ERROR_SUCCESS) + return; + if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, Buffer2, 0, KEY_WRITE, &RegKey2 ) != ERROR_SUCCESS) { + RegCloseKey( RegKey1 ); + return; + } + + + if( new_lease->options[DHO_DOMAIN_NAME].len ) { + DH_DbgPrint(MID_TRACE,("Setting DhcpDomain: %s\n", new_lease->options[DHO_DOMAIN_NAME].data)); + + RegSetValueExA( RegKey1, "DhcpDomain", 0, REG_SZ, + (LPBYTE)new_lease->options[DHO_DOMAIN_NAME].data, new_lease->options[DHO_DOMAIN_NAME].len ); + RegSetValueExA( RegKey2, "DhcpDomain", 0, REG_SZ, + (LPBYTE)new_lease->options[DHO_DOMAIN_NAME].data, new_lease->options[DHO_DOMAIN_NAME].len ); + } else { + RegDeleteValueW( RegKey1, L"DhcpDomain" ); + RegDeleteValueW( RegKey2, L"DhcpDomain" ); + } + + RegCloseKey( RegKey1 ); + RegCloseKey( RegKey2 ); + +} + void setup_adapter( PDHCP_ADAPTER Adapter, struct client_lease *new_lease ) { CHAR Buffer[200] = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"; struct iaddr netmask; @@ -569,6 +600,7 @@ return; } set_name_servers( Adapter, new_lease ); + set_domain( Adapter, new_lease ); } /* Index: dll/win32/CMakeLists.txt =================================================================== --- dll/win32/CMakeLists.txt (revision 72643) +++ dll/win32/CMakeLists.txt (working copy) @@ -246,7 +246,7 @@ add_subdirectory(wmi) add_subdirectory(wmiutils) add_subdirectory(wmvcore) -add_subdirectory(ws2_32) +#add_subdirectory(ws2_32) add_subdirectory(ws2_32_new) add_subdirectory(ws2help) add_subdirectory(wshirda) Index: dll/win32/dnsapi/dnsapi/query.c =================================================================== --- dll/win32/dnsapi/dnsapi/query.c (revision 72643) +++ dll/win32/dnsapi/dnsapi/query.c (working copy) @@ -53,7 +53,7 @@ PVOID *Reserved) { adns_state astate; - int quflags = 0; + int quflags = adns_qf_search; int adns_error; adns_answer *answer; LPSTR CurrentName; Index: dll/win32/kernel32/client/compname.c =================================================================== --- dll/win32/kernel32/client/compname.c (revision 72643) +++ dll/win32/kernel32/client/compname.c (working copy) @@ -155,11 +155,18 @@ nSize); case ComputerNameDnsDomain: - return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" - L"\\Services\\Tcpip\\Parameters", - L"Domain", - lpBuffer, - nSize); + if (!GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Services\\Tcpip\\Parameters", + L"DhcpDomain", + lpBuffer, + nSize)) + return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Services\\Tcpip\\Parameters", + L"Domain", + lpBuffer, + nSize); + else + return TRUE; case ComputerNameDnsFullyQualified: ResultString.Length = 0; @@ -195,7 +202,7 @@ RtlFreeUnicodeString(&DomainPart); RtlInitUnicodeString(&DomainPart, NULL); - QueryTable[0].Name = L"Domain"; + QueryTable[0].Name = L"DhcpDomain"; QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT; QueryTable[0].EntryContext = &DomainPart; @@ -207,7 +214,7 @@ NULL, NULL); - if (NT_SUCCESS(Status)) + if (NT_SUCCESS(Status) && DomainPart.Buffer != NULL && wcslen(DomainPart.Buffer) > 0) { Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart); if ((!NT_SUCCESS(Status)) || (!ret)) @@ -221,6 +228,36 @@ *nSize = ResultString.Length / sizeof(WCHAR) - 1; return TRUE; } + else + { + RtlInitUnicodeString(&DomainPart, NULL); + QueryTable[0].Name = L"Domain"; + QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT; + QueryTable[0].EntryContext = &DomainPart; + + Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE, + L"\\Registry\\Machine\\System" + L"\\CurrentControlSet\\Services\\Tcpip" + L"\\Parameters", + QueryTable, + NULL, + NULL); + + if (NT_SUCCESS(Status)) + { + Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart); + if ((!NT_SUCCESS(Status)) || (!ret)) + { + *nSize = HostSize + DomainPart.Length; + SetLastError(ERROR_MORE_DATA); + RtlFreeUnicodeString(&DomainPart); + return FALSE; + } + RtlFreeUnicodeString(&DomainPart); + *nSize = ResultString.Length / sizeof(WCHAR) - 1; + return TRUE; + } + } } return FALSE; @@ -232,11 +269,18 @@ nSize); case ComputerNamePhysicalDnsDomain: - return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" - L"\\Services\\Tcpip\\Parameters", - L"Domain", - lpBuffer, - nSize); + if (!GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Services\\Tcpip\\Parameters", + L"DhcpDomain", + lpBuffer, + nSize)) + return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet" + L"\\Services\\Tcpip\\Parameters", + L"Domain", + lpBuffer, + nSize); + else + return TRUE; /* XXX Redo these */ case ComputerNamePhysicalDnsFullyQualified: Index: dll/win32/msafd/misc/dllmain.c =================================================================== --- dll/win32/msafd/misc/dllmain.c (revision 72643) +++ dll/win32/msafd/misc/dllmain.c (working copy) @@ -19,6 +19,7 @@ HANDLE GlobalHeap; WSPUPCALLTABLE Upcalls; +DWORD CatalogEntryId; /* CatalogEntryId for upcalls */ LPWPUCOMPLETEOVERLAPPEDREQUEST lpWPUCompleteOverlappedRequest; PSOCKET_INFORMATION SocketListHead = NULL; CRITICAL_SECTION SocketListLock; @@ -72,6 +73,22 @@ TRACE("Creating Socket, getting TDI Name - AddressFamily (%d) SocketType (%d) Protocol (%d).\n", AddressFamily, SocketType, Protocol); + if (AddressFamily == AF_UNSPEC && SocketType == 0 && Protocol == 0) + return WSAEINVAL; + + /* Set the defaults */ + if (AddressFamily == AF_UNSPEC) + AddressFamily = AF_INET; + if (SocketType == 0) + SocketType = SOCK_STREAM; + if (Protocol == 0) + { + if (SocketType == SOCK_STREAM) + Protocol = IPPROTO_TCP; + if (SocketType == SOCK_DGRAM) + Protocol = IPPROTO_UDP; + } + /* Get Helper Data and Transport */ Status = SockGetTdiName (&AddressFamily, &SocketType, @@ -2207,7 +2224,8 @@ return 0; default: - break; + *lpErrno = WSAEINVAL; + return SOCKET_ERROR; } SendToHelper: @@ -2244,8 +2262,13 @@ /* FIXME: We should handle some more cases here */ - if (level == SOL_SOCKET) + if (level != SOL_SOCKET) { + *lpErrno = WSAEINVAL; + return SOCKET_ERROR; + } + else + { switch (optname) { case SO_BROADCAST: @@ -2364,6 +2387,7 @@ * lpProcTable = Address of procedure table to initialize * RETURNS: * Status of operation + * @IMPLEMENT */ INT WSPAPI @@ -2376,8 +2400,14 @@ { NTSTATUS Status; - ERR("wVersionRequested (0x%X) \n", wVersionRequested); - Status = NO_ERROR; + if (((LOBYTE(wVersionRequested) == 2) && (HIBYTE(wVersionRequested) < 2)) || (LOBYTE(wVersionRequested) < 2)) + { + ERR("WSPStartup NOT SUPPORTED for version 0x%X\n", wVersionRequested); + return WSAVERNOTSUPPORTED; + } + else + Status = NO_ERROR; + /* FIXME: Enable all cases of WSPStartup status */ Upcalls = UpcallTable; if (Status == NO_ERROR) @@ -2414,6 +2444,8 @@ lpProcTable->lpWSPStringToAddress = WSPStringToAddress; lpWSPData->wVersion = MAKEWORD(2, 2); lpWSPData->wHighVersion = MAKEWORD(2, 2); + /* Save CatalogEntryId for all upcalls */ + CatalogEntryId = lpProtocolInfo->dwCatalogEntryId; } TRACE("Status (%d).\n", Status); Index: dll/win32/mswsock/CMakeLists.txt =================================================================== --- dll/win32/mswsock/CMakeLists.txt (revision 72643) +++ dll/win32/mswsock/CMakeLists.txt (working copy) @@ -5,6 +5,8 @@ list(APPEND SOURCE extensions.c + mswhelper.c + nsplookup.c stubs.c precomp.h) @@ -13,7 +15,7 @@ mswsock.rc ${CMAKE_CURRENT_BINARY_DIR}/mswsock.def) -set_module_type(mswsock win32dll UNICODE) -add_importlibs(mswsock ws2_32 msvcrt kernel32) +set_module_type(mswsock win32dll) +add_importlibs(mswsock ws2_32 dnsapi msvcrt kernel32) add_pch(mswsock precomp.h SOURCE) add_cd_file(TARGET mswsock DESTINATION reactos/system32 FOR all) Index: dll/win32/mswsock/mswhelper.c =================================================================== --- dll/win32/mswsock/mswhelper.c (nonexistent) +++ dll/win32/mswsock/mswhelper.c (working copy) @@ -0,0 +1,535 @@ +#include "precomp.h" + +#include +#include +#include +#include "sal.h" + +#include "mswhelper.h" + +#define MSW_BUFSIZE 512 +#define MAX_ARRAY_SIZE 5 + +void mswBufferInit( + _Inout_ PMSW_BUFFER mswBuf, + _In_ BYTE* buffer, + _In_ DWORD bufferSize) +{ + RtlZeroMemory(mswBuf,sizeof(*mswBuf)); + RtlZeroMemory(buffer,bufferSize); + mswBuf->bytesMax = bufferSize; + mswBuf->buffer = buffer; + mswBuf->bufendptr = buffer; + mswBuf->bufok = TRUE; +} + +BOOL mswBufferCheck( + _Inout_ PMSW_BUFFER mswBuf, + _In_ DWORD count) +{ + if (mswBuf->bytesUsed + count <= mswBuf->bytesMax) + return TRUE; + + mswBuf->bufok = FALSE; + return FALSE; +} + +BOOL mswBufferIncUsed( + _Inout_ PMSW_BUFFER mswBuf, + _In_ DWORD count) +{ + if (!mswBufferCheck(mswBuf, count)) + return FALSE; + mswBuf->bytesUsed += count; + mswBuf->bufendptr += count; + return TRUE; +} + +inline BYTE* mswBufferEndPtr( + _Inout_ PMSW_BUFFER mswBuf) +{ + return mswBuf->bufendptr; +} + +BOOL mswBufferAppend( + _Inout_ PMSW_BUFFER mswBuf, + _In_ void *dataToAppend, + _In_ DWORD dataSize) +{ + if (!mswBufferCheck(mswBuf, dataSize)) + return FALSE; + + RtlCopyMemory(mswBuf->bufendptr,dataToAppend,dataSize); + mswBuf->bytesUsed += dataSize; + mswBuf->bufendptr += dataSize; + + return TRUE; +} + +BOOL mswBufferAppendStrA( + _Inout_ PMSW_BUFFER mswBuf, + _In_ char* str) +{ + return mswBufferAppend(mswBuf,str,strlen(str)+sizeof(char)); +} + +BOOL mswBufferAppendStrW( + _Inout_ PMSW_BUFFER mswBuf, + _In_ WCHAR* str) +{ + int bytelen; + bytelen = (wcslen(str) + 1) * sizeof(WCHAR); + return mswBufferAppend(mswBuf,str,bytelen); +} + +BOOL mswBufferAppendPtr( + _Inout_ PMSW_BUFFER mswBuf, + _In_ void* ptr) +{ + return mswBufferAppend(mswBuf,&ptr,sizeof(ptr)); +} + +/* lst = pointer to pointer of items + + *lst[0] = 1st item + *lst[1] = 2nd item + ... + lst[n] = NULL = End of List + + itemLength = data in Bytes for each item. + + ptrofs = delta relative to mswBuf.buffer +*/ +BOOL mswBufferAppendLst( + _Inout_ PMSW_BUFFER mswBuf, + _In_ void **lst, + _In_ DWORD itemByteLength, + _In_opt_ int ptrofs) +{ + DWORD lstItemCount; + DWORD lstByteSize; + DWORD lstDataPos; + DWORD i1; + UINT_PTR *ptrSrcLstPos; + + /* calculate size of list */ + ptrSrcLstPos = (UINT_PTR*)lst; + lstItemCount = 0; + while (*ptrSrcLstPos != (UINT_PTR)NULL) + { + lstItemCount++; + ptrSrcLstPos++; + } + + lstByteSize = + /* item-pointer + null-ptr (for end) */ + ( (lstItemCount + 1) * sizeof(UINT_PTR) ) + + /* item-data */ + ( lstItemCount * itemByteLength ); + + if (mswBuf->bytesUsed + lstByteSize > mswBuf->bytesMax) + return FALSE; + + /* calculate position for the data of the first item */ + lstDataPos = + ( (lstItemCount + 1) * sizeof(UINT_PTR) ) + + (DWORD)mswBufferEndPtr(mswBuf); + /* add ptrofs */ + lstDataPos += ptrofs; + + /* write array of Pointer to data */ + for (i1 = 0; i1 < lstItemCount; i1++) + { + if (!mswBufferAppendPtr(mswBuf,(void*)lstDataPos)) + return FALSE; + lstDataPos += sizeof(UINT_PTR); + } + /* end of list */ + if (!mswBufferAppendPtr(mswBuf,NULL)) + return FALSE; + + /* write data */ + ptrSrcLstPos = (UINT_PTR*)lst; + for (i1 = 0; i1 < lstItemCount; i1++) + { + mswBufferAppend(mswBuf,*(BYTE**)ptrSrcLstPos,itemByteLength); + ptrSrcLstPos++; + } + return mswBuf->bufok; +} + +BOOL mswBufferAppendStrLstA( + _Inout_ PMSW_BUFFER mswBuf, + _In_ void **lst, + _In_opt_ int ptrofs) +{ + DWORD lstItemLen[MAX_ARRAY_SIZE]; + DWORD lstItemCount; + DWORD lstByteSize; + DWORD lstDataPos; + DWORD lstDataSize; + DWORD i1; + UINT_PTR *ptrSrcLstPos; + + /* calculate size of list */ + ptrSrcLstPos = (UINT_PTR*)lst; + lstItemCount = 0; + lstDataSize = 0; + while (*ptrSrcLstPos != (UINT_PTR)NULL) + { + if (lstItemCount >= MAX_ARRAY_SIZE) + return FALSE; + i1 = strlen((char*)*ptrSrcLstPos)+sizeof(char); + lstItemLen[lstItemCount] = i1; + lstItemCount++; + lstDataSize += i1; + ptrSrcLstPos++; + } + + lstByteSize = + /* item-pointer + null-ptr (for end) */ + ( (lstItemCount + 1) * sizeof(UINT_PTR) ) + + /* item-data */ + lstDataSize; + + if (mswBuf->bytesUsed + lstByteSize > mswBuf->bytesMax) + return FALSE; + + /* calculate position for the data of the first item */ + lstDataPos = + ( (lstItemCount + 1) * sizeof(UINT_PTR) ) + + (DWORD)mswBufferEndPtr(mswBuf); + /* add ptrofs */ + lstDataPos += ptrofs; + + for (i1 = 0; i1 < lstItemCount; i1++) + { + if (!mswBufferAppendPtr(mswBuf,(void*)lstDataPos)) + return FALSE; + lstDataPos += lstItemLen[i1]; + } + /* end of list */ + if (!mswBufferAppendPtr(mswBuf,NULL)) + return FALSE; + + /* write data */ + ptrSrcLstPos = (UINT_PTR*)lst; + for (i1 = 0; i1 < lstItemCount; i1++) + { + if (!mswBufferAppendStrA(mswBuf,*(char**)ptrSrcLstPos)) + return FALSE; + ptrSrcLstPos++; + } + return mswBuf->bufok; +} + +BOOL mswBufferAppendBlob_Hostent( + _Inout_ PMSW_BUFFER mswBuf, + _Inout_ LPWSAQUERYSETW lpRes, + _In_ char* hostnameA, + _In_ DWORD ip4addr) +{ + PHOSTENT phe; + void* lst[2]; + BYTE* bytesOfs; + + /* blob */ + lpRes->lpBlob = (LPBLOB)mswBufferEndPtr(mswBuf); + if(!mswBufferIncUsed(mswBuf,sizeof(*lpRes->lpBlob))) return FALSE; + + /* cbSize will be set later */ + lpRes->lpBlob->cbSize = 0; + lpRes->lpBlob->pBlobData = mswBufferEndPtr(mswBuf); + + /* hostent */ + phe = (PHOSTENT)lpRes->lpBlob->pBlobData; + bytesOfs = mswBufferEndPtr(mswBuf); + if(!mswBufferIncUsed(mswBuf,sizeof(*phe))) return FALSE; + + phe->h_addrtype = AF_INET; + phe->h_length = 4; /* 4 Byte (IPv4) */ + + /* aliases */ + phe->h_aliases = (char**)(mswBufferEndPtr(mswBuf) - bytesOfs); + if(!mswBufferAppendPtr(mswBuf,NULL)) return FALSE; + + /* addr_list */ + RtlZeroMemory(lst,sizeof(lst)); + if (ip4addr != 0) + lst[0] = (void*)&ip4addr; + phe->h_addr_list = (char**)(mswBufferEndPtr(mswBuf) - bytesOfs); + if(!mswBufferAppendLst(mswBuf,lst,4,-(DWORD)bytesOfs)) return FALSE; + + /* name */ + phe->h_name = (char*)(mswBufferEndPtr(mswBuf) - bytesOfs); + if(!mswBufferAppendStrA(mswBuf,hostnameA)) return FALSE; + + lpRes->lpBlob->cbSize = (DWORD)(mswBufferEndPtr(mswBuf) - bytesOfs); + return mswBuf->bufok; +} + +BOOL mswBufferAppendBlob_Servent( + _Inout_ PMSW_BUFFER mswBuf, + _Inout_ LPWSAQUERYSETW lpRes, + _In_ char* serviceNameA, + _In_ char** serviceAliasesA, + _In_ char* protocolNameA, + _In_ WORD port) +{ + PSERVENT pse; + BYTE* bytesOfs; + + /* blob */ + lpRes->lpBlob = (LPBLOB)mswBufferEndPtr(mswBuf); + if(!mswBufferIncUsed(mswBuf,sizeof(*lpRes->lpBlob))) return FALSE; + + lpRes->lpBlob->cbSize = 0;//later + lpRes->lpBlob->pBlobData = mswBufferEndPtr(mswBuf); + + /* servent */ + pse = (LPSERVENT)lpRes->lpBlob->pBlobData; + bytesOfs = mswBufferEndPtr(mswBuf); + if(!mswBufferIncUsed(mswBuf,sizeof(*pse))) return FALSE; + + pse->s_aliases = (char**)(mswBufferEndPtr(mswBuf) - bytesOfs); + if(!mswBufferAppendStrLstA(mswBuf, + (void**)serviceAliasesA, + -(DWORD)bytesOfs)) return FALSE; + + pse->s_name = (char*)(mswBufferEndPtr(mswBuf) - bytesOfs); + if(!mswBufferAppendStrA(mswBuf,serviceNameA)) return FALSE; + + pse->s_port = htons(port); + + pse->s_proto = (char*)(mswBufferEndPtr(mswBuf) - bytesOfs); + if(!mswBufferAppendStrA(mswBuf,protocolNameA)) return FALSE; + + lpRes->lpBlob->cbSize = (DWORD)(mswBufferEndPtr(mswBuf) - bytesOfs); + return mswBuf->bufok; +} + +BOOL mswBufferAppendAddr_AddrInfoW( + _Inout_ PMSW_BUFFER mswBuf, + _Inout_ LPWSAQUERYSETW lpRes, + _In_ DWORD ip4addr) +{ + LPCSADDR_INFO paddrinfo; + LPSOCKADDR_IN psa; + + lpRes->dwNumberOfCsAddrs = 1; + lpRes->lpcsaBuffer = (LPCSADDR_INFO)mswBufferEndPtr(mswBuf); + + paddrinfo = lpRes->lpcsaBuffer; + if(!mswBufferIncUsed(mswBuf,sizeof(*paddrinfo))) return FALSE; + + paddrinfo->LocalAddr.lpSockaddr = (LPSOCKADDR)mswBufferEndPtr(mswBuf); + if(!mswBufferIncUsed(mswBuf,sizeof(*paddrinfo->LocalAddr.lpSockaddr))) return FALSE; + + paddrinfo->RemoteAddr.lpSockaddr = (LPSOCKADDR)mswBufferEndPtr(mswBuf); + if(!mswBufferIncUsed(mswBuf,sizeof(*paddrinfo->RemoteAddr.lpSockaddr))) return FALSE; + + paddrinfo->iSocketType = SOCK_DGRAM; + paddrinfo->iProtocol = IPPROTO_UDP; + + psa = (LPSOCKADDR_IN)paddrinfo->LocalAddr.lpSockaddr; + paddrinfo->LocalAddr.iSockaddrLength = sizeof(*psa); + psa->sin_family = AF_INET; + psa->sin_port = 0; + psa->sin_addr.s_addr = 0; + RtlZeroMemory(psa->sin_zero,sizeof(psa->sin_zero)); + + psa = (LPSOCKADDR_IN)paddrinfo->RemoteAddr.lpSockaddr; + paddrinfo->RemoteAddr.iSockaddrLength = sizeof(*psa); + psa->sin_family = AF_INET; + psa->sin_port = 0; + psa->sin_addr.s_addr = ip4addr; + RtlZeroMemory(psa->sin_zero,sizeof(psa->sin_zero)); + + return TRUE; +} + +/* ansicode <-> unicode */ +WCHAR* StrA2WHeapAlloc( + _In_opt_ HANDLE hHeap, + _In_ char* aStr) +{ + int aStrByteLen; + int wStrByteLen; + int charLen; + int ret; + WCHAR* wStr; + + if (aStr == NULL) + return NULL; + charLen = strlen(aStr) + 1; + + aStrByteLen = (charLen * sizeof(char)); + wStrByteLen = (charLen * sizeof(WCHAR)); + if (hHeap == 0) + hHeap = GetProcessHeap(); + wStr = HeapAlloc(hHeap,0,wStrByteLen); + if (wStr == NULL) + { + HeapFree(hHeap, 0, wStr); + return NULL; + } + + ret = MultiByteToWideChar(1252, + 0, + aStr, + aStrByteLen, + wStr, + charLen); + + if (ret != charLen) + { + HeapFree(hHeap, 0, wStr); + return NULL; + } + return wStr; +} + +char* StrW2AHeapAlloc( + _In_opt_ HANDLE hHeap, + _In_ WCHAR* wStr) +{ + int charLen; + int aStrByteLen; + int ret; + char* aStr; + + if (wStr == NULL) + return NULL; + charLen = wcslen(wStr) + 1; + + aStrByteLen = (charLen * sizeof(char)); + if (hHeap == 0) + hHeap = GetProcessHeap(); + aStr = HeapAlloc(hHeap,0,aStrByteLen); + if (aStr == NULL) + { + HeapFree(hHeap, 0, aStr); + return NULL; + } + + ret = WideCharToMultiByte(1252, + 0, + wStr, + charLen, + aStr, + aStrByteLen, + NULL, + NULL); + if (ret != aStrByteLen) + { + HeapFree(hHeap, 0, aStr); + return NULL; + } + return aStr; +} + +WCHAR* StrCpyHeapAllocW( + _In_opt_ HANDLE hHeap, + _In_ WCHAR* wStr) +{ + int chLen; + int bLen; + WCHAR* resW; + + if (wStr == NULL) + return NULL; + + if (hHeap == 0) + hHeap = GetProcessHeap(); + chLen = wcslen(wStr); + + bLen = (chLen + 1) * sizeof(WCHAR); + + resW = HeapAlloc(hHeap, 0, bLen); + RtlCopyMemory(resW,wStr,bLen); + return resW; +} + +char* StrCpyHeapAllocA( + _In_opt_ HANDLE hHeap, + _In_ char* aStr) +{ + int chLen; + int bLen; + char* resA; + + if (aStr == NULL) + return NULL; + + if (hHeap == 0) + hHeap = GetProcessHeap(); + chLen = strlen(aStr); + + bLen = (chLen + 1) * sizeof(char); + + resA = HeapAlloc(hHeap, 0, bLen); + RtlCopyMemory(resA,aStr,bLen); + return resA; +} + +char** StrAryCpyHeapAllocA( + _In_opt_ HANDLE hHeap, + _In_ char** aStrAry) +{ + char** aSrcPtr; + char** aDstPtr; + char* aDstNextStr; + DWORD aStrByteLen[MAX_ARRAY_SIZE]; + int bLen; + int bItmLen; + int aCount; + int i1; + char** resA; + + if (hHeap == 0) + hHeap = GetProcessHeap(); + + /* Calculating size of array ... */ + aSrcPtr = aStrAry; + bLen = 0; + aCount = 0; + while (*aSrcPtr != NULL) + { + if (aCount >= MAX_ARRAY_SIZE) + return NULL; + + bItmLen = (strlen(*aSrcPtr) + 1) * sizeof(char); + aStrByteLen[aCount] = bItmLen; + + bLen += sizeof(*aSrcPtr) + bItmLen; + + aSrcPtr++; + aCount++; + } + /* size for NULL-terminator */ + bLen += sizeof(*aSrcPtr); + + /* get memory */ + resA = HeapAlloc(hHeap, 0, bLen); + + /* copy data */ + aSrcPtr = aStrAry; + aDstPtr = resA; + /* pos for the first string */ + aDstNextStr = (char*)(resA + aCount + 1); + for (i1 = 0; i1 < aCount; i1++) + { + bItmLen = aStrByteLen[i1]; + + *aDstPtr = aDstNextStr; + RtlCopyMemory(*aDstPtr,*aSrcPtr,bItmLen); + + aDstNextStr = (char*)((DWORD)aDstNextStr + (DWORD)bItmLen); + aDstPtr++; + aSrcPtr++; + } + /* terminate with NULL */ + *aDstPtr = NULL; + + return resA; +} Index: dll/win32/mswsock/mswhelper.h =================================================================== --- dll/win32/mswsock/mswhelper.h (nonexistent) +++ dll/win32/mswsock/mswhelper.h (working copy) @@ -0,0 +1,95 @@ +#ifndef _MSWHELPER_H +#define _MSWHELPER_H + +#include + +typedef struct +{ + DWORD bytesUsed; + DWORD bytesMax; + BYTE* buffer; + // Pointer to the first "unused" byte + BYTE* bufendptr; + // FALSE if on mswBuffer-Function fails + BOOL bufok; +} MSW_BUFFER, *PMSW_BUFFER; + +void mswBufferInit( + _Out_ PMSW_BUFFER mswBuf, + _In_ BYTE* buffer, + _In_ DWORD bufferSize); + +inline BOOL mswBufferCheck( + _Inout_ PMSW_BUFFER mswBuf, + _In_ DWORD count); + +BOOL mswBufferIncUsed( + _Inout_ PMSW_BUFFER mswBuf, + _In_ DWORD count); + +inline BYTE* mswBufferEndPtr( + _Inout_ PMSW_BUFFER mswBuf); + +BOOL mswBufferAppend( + _Inout_ PMSW_BUFFER mswBuf, + _In_ void *dataToAppend, + _In_ DWORD dataSize); + +BOOL mswBufferAppendStrA( + _Inout_ PMSW_BUFFER mswBuf, + _In_ char* str); +BOOL mswBufferAppendStrW( + _Inout_ PMSW_BUFFER mswBuf, + _In_ WCHAR* str); + +BOOL mswBufferAppendPtr( + _Inout_ PMSW_BUFFER mswBuf, + _In_ void* ptr); + +BOOL mswBufferAppendLst( + _Inout_ PMSW_BUFFER mswBuf, + _In_ void **lst, + _In_ DWORD itemByteLength, + _In_opt_ int deltaofs); +BOOL mswBufferAppendStrLstA( + _Inout_ PMSW_BUFFER mswBuf, + _In_ void **lst, + _In_opt_ int ptrofs); +BOOL mswBufferAppendBlob_Hostent( + _Inout_ PMSW_BUFFER mswBuf, + _Inout_ LPWSAQUERYSETW lpRes, + _In_ char* hostnameA, + _In_ DWORD ip4addr); +BOOL mswBufferAppendBlob_Servent( + _Inout_ PMSW_BUFFER mswBuf, + _Inout_ LPWSAQUERYSETW lpRes, + _In_ char* serviceNameA, + _In_ char** serviceAliasesA, + _In_ char* protocolNameA, + _In_ WORD port); +BOOL mswBufferAppendAddr_AddrInfoW( + _Inout_ PMSW_BUFFER mswBuf, + _Inout_ LPWSAQUERYSETW lpRes, + _In_ DWORD ip4addr); + +WCHAR* StrA2WHeapAlloc( + _In_opt_ HANDLE hHeap, + _In_ char* aStr); +char* StrW2AHeapAlloc( + _In_opt_ HANDLE hHeap, + _In_ WCHAR* wStr); +WCHAR* StrCpyHeapAllocW( + _In_opt_ HANDLE hHeap, + _In_ WCHAR* wStr); +char* StrCpyHeapAllocA( + _In_opt_ HANDLE hHeap, + _In_ char* aStr); +/* strary: + ptr1 ... ptrn \0 + data1 ... datan +*/ +char** StrAryCpyHeapAllocA( + _In_opt_ HANDLE hHeap, + _In_ char** aStrAry); + +#endif // _MSWHELPER_H Index: dll/win32/mswsock/nsplookup.c =================================================================== --- dll/win32/mswsock/nsplookup.c (nonexistent) +++ dll/win32/mswsock/nsplookup.c (working copy) @@ -0,0 +1,1219 @@ +#include "precomp.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define NSP_CALLID_DNS 0x0001 +#define NSP_CALLID_HOSTNAME 0x0002 +#define NSP_CALLID_HOSTBYNAME 0x0003 +#define NSP_CALLID_SERVICEBYNAME 0x0004 + +#ifndef BUFSIZ +#define BUFSIZ 1024 +#endif // BUFSIZ +#ifndef WS2_INTERNAL_MAX_ALIAS +#define WS2_INTERNAL_MAX_ALIAS 512 +#endif // WS2_INTERNAL_MAX_ALIAS + +//#define IP_LOCALHOST 0x0100007F + +//#define NSP_REDIRECT + +typedef struct +{ + WCHAR* hostnameW; + DWORD addr4; + + WCHAR* servnameW; + WCHAR* servprotoW; + CHAR** servaliasesA; /* array */ + WORD servport; +} WSHOSTINFOINTERN, *PWSHOSTINFOINTERN; + +typedef struct +{ + /* Provider-ID */ + GUID providerId; + /* dwControlFlags (WSALookupServiceBegin) */ + DWORD dwControlFlags; + + /* List for LookupServiceNext-Calls */ + DWORD CallID; + /* call-count of the current CallID. */ + DWORD CallIDCounter; + + /* hostbyname */ + WCHAR* hostnameW; + +#ifdef NSP_REDIRECT + HANDLE rdrLookup; + NSP_ROUTINE rdrproc; +#endif +} WSHANDLEINTERN, *PWSHANDLEINTERN; + +static const GUID guid_NULL = {0}; +static const GUID guid_HOSTNAME = SVCID_HOSTNAME; +static const GUID guid_INET_HOSTADDRBYINETSTRING = SVCID_INET_HOSTADDRBYINETSTRING; +static const GUID guid_INET_HOSTADDRBYNAME = SVCID_INET_HOSTADDRBYNAME; +static const GUID guid_INET_SERVICEBYNAME = SVCID_INET_SERVICEBYNAME; + +/* GUIDs - maybe they should be loaded from registry? */ +/* Namespace: 32 */ +static const GUID guid_mswsock_TcpIp = + { /*Data1:*/ 0x22059D40, + /*Data2:*/ 0x7E9E, + /*Data3:*/ 0x11CF, + /*Data4:*/ { 0xAE, 0x5A, 0x00, 0xAA, 0x00, 0xA7, 0x11, 0x2B } + }; + +/* {6642243A-3BA8-4AA6-BAA5-2E0BD71FDD83} */ +/* Namespace: 15 */ +static const GUID guid_mswsock_NLA = + { /*Data1:*/ 0x6642243A, + /*Data2:*/ 0x3BA8, + /*Data3:*/ 0x4AA6, + /*Data4:*/ { 0xBA, 0xA5, 0x2E, 0x0B, 0xD7, 0x1F, 0xDD, 0x83 } + }; + + +#ifdef NSP_REDIRECT +typedef INT (CALLBACK *lpRdrNSPStartup)( + LPGUID lpProviderId, + LPNSP_ROUTINE lpRout); +const rdrLib = "mswsock.dll-original"; +lpRdrNSPStartup rdrNSPStartup; +HANDLE hLib; +NSP_ROUTINE rdrproc_tcpip; +NSP_ROUTINE rdrproc_nla; +#endif + +/* Forwards */ +INT +WINAPI +mswNSPStartup( + LPGUID lpProviderId, + LPNSP_ROUTINE lpRout + ); +INT +NSP_LookupServiceBeginW(PWSHANDLEINTERN data, + CHAR* hostnameA, + WCHAR* hostnameW, + DWORD CallID); + +INT +NSP_LookupServiceNextW( + _In_ PWSHANDLEINTERN data, + _In_ DWORD CallID, + _Inout_ LPWSAQUERYSETW lpRes, + _Inout_ LPDWORD lpResLen); +INT +NSP_GetHostNameHeapAllocW( + _Out_ WCHAR** hostname); +INT +NSP_GetHostByNameHeapAllocW( + _In_ WCHAR* name, + _In_ GUID* lpProviderId, + _Out_ PWSHOSTINFOINTERN hostinfo); +INT +NSP_GetServiceByNameHeapAllocW( + _In_ WCHAR* nameW, + _In_ GUID* lpProviderId, + _Out_ PWSHOSTINFOINTERN hostinfo); + +/* Implementations - Internal */ + +INT +WSAAPI +mwsNSPCleanUp( + _In_ LPGUID lpProviderId) +{ + //WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED); + //return ERROR_CALL_NOT_IMPLEMENTED; + return ERROR_SUCCESS; +} + +INT +mwsNSPInit() +{ + return ERROR_SUCCESS; +} + +INT +WSAAPI +mwsNSPLookupServiceBegin( + _In_ LPGUID lpProviderId, + _In_ LPWSAQUERYSETW lpqsRestrictions, + _In_ LPWSASERVICECLASSINFOW lpServiceClassInfo, + _In_ DWORD dwControlFlags, + _Out_ LPHANDLE lphLookup) +{ + PWSHANDLEINTERN pLook; + int wsaErr; + + if (IsEqualGUID(lpProviderId,&guid_mswsock_TcpIp)) + { + //OK + } + else if (IsEqualGUID(lpProviderId,&guid_mswsock_NLA)) + { + WSASetLastError(WSASERVICE_NOT_FOUND); + return SOCKET_ERROR; + } + else return ERROR_CALL_NOT_IMPLEMENTED; + + + /* allocate internal structure */ + pLook = HeapAlloc(GetProcessHeap(),0,sizeof(WSHANDLEINTERN)); + if (!pLook) + { + WSASetLastError(WSAEFAULT); + return SOCKET_ERROR; + } + + *lphLookup = (HANDLE)pLook; + + RtlZeroMemory(pLook,sizeof(*pLook)); + /* Anyway the ControlFlags "should" be needed + in NSPLookupServiceNext. (see doku) But + thats not the fact ATM. */ + pLook->dwControlFlags = dwControlFlags; + pLook->providerId = *lpProviderId; + +#ifdef NSP_REDIRECT + if (IsEqualGUID(lpProviderId,&guid_mswsock_TcpIp)) + { + pLook->rdrproc = rdrproc_tcpip; + } + else if (IsEqualGUID(lpProviderId,&guid_mswsock_NLA)) + { + pLook->rdrproc = rdrproc_nla; + } + else return ERROR_CALL_NOT_IMPLEMENTED; + + if (pLook->rdrproc.NSPLookupServiceBegin( + lpProviderId, + lpqsRestrictions, + lpServiceClassInfo, + dwControlFlags, + &pLook->rdrLookup) == NO_ERROR) + wsaErr = NO_ERROR; + else + wsaErr = WSAGetLastError(); + /* + if (res) + res = WSAGetLastError(); + */ +#else + wsaErr = ERROR_CALL_NOT_IMPLEMENTED; + if (IsEqualGUID(lpqsRestrictions->lpServiceClassId, + &guid_NULL)) + { + wsaErr = ERROR_CALL_NOT_IMPLEMENTED; + } + else if (IsEqualGUID(lpqsRestrictions->lpServiceClassId, + &guid_HOSTNAME)) + { + wsaErr = NSP_LookupServiceBeginW(pLook, + NULL,NULL, + NSP_CALLID_HOSTNAME); + } + else if (IsEqualGUID(lpqsRestrictions->lpServiceClassId, + &guid_INET_HOSTADDRBYNAME)) + { + wsaErr = NSP_LookupServiceBeginW(pLook, + NULL, + lpqsRestrictions->lpszServiceInstanceName, + NSP_CALLID_HOSTBYNAME); + } + else if (IsEqualGUID(lpqsRestrictions->lpServiceClassId, + &guid_INET_SERVICEBYNAME)) + { + wsaErr = NSP_LookupServiceBeginW(pLook, + NULL, + lpqsRestrictions->lpszServiceInstanceName, + NSP_CALLID_SERVICEBYNAME); + } + else if (IsEqualGUID(lpqsRestrictions->lpServiceClassId, + &guid_INET_HOSTADDRBYINETSTRING)) + { + wsaErr = ERROR_CALL_NOT_IMPLEMENTED; + }; +#endif + if (wsaErr != NO_ERROR) + { + WSASetLastError(wsaErr); + return SOCKET_ERROR; + } + return NO_ERROR; +} + +INT +WSAAPI mwsNSPLookupServiceNext( + _In_ HANDLE hLookup, + _In_ DWORD dwControlFlags, + _Inout_ LPDWORD lpdwBufferLength, + //_Out_writes_bytes_to_(*lpdwBufferLength, *lpdwBufferLength) + LPWSAQUERYSETW lpqsResults) +{ + PWSHANDLEINTERN pLook = hLookup; + int wsaErr = 0; + +#ifdef NSP_REDIRECT + INT res; + res = pLook->rdrproc.NSPLookupServiceNext( + pLook->rdrLookup, + dwControlFlags, + lpdwBufferLength, + lpqsResults); + wsaErr = WSAGetLastError(); + if (res != ERROR_SUCCESS) + { + wsaErr = WSAGetLastError(); + if (wsaErr == 0) + wsaErr = 0xFFFFFFFF; + } +#else + + if ( (lpdwBufferLength == NULL) || (*lpdwBufferLength == 0) ) + { + wsaErr = WSA_NOT_ENOUGH_MEMORY; + goto End; + } + + RtlZeroMemory(lpqsResults,*lpdwBufferLength); + lpqsResults->dwSize = sizeof(*lpqsResults); + + wsaErr = NSP_LookupServiceNextW(pLook, + pLook->CallID, + lpqsResults, + lpdwBufferLength); + + +#endif +End: + if (wsaErr != 0) + { + WSASetLastError(wsaErr); + return SOCKET_ERROR; + } + return NO_ERROR; +} + +INT +WSAAPI +mwsNSPIoCtl( + _In_ HANDLE hLookup, + _In_ DWORD dwControlCode, + _In_reads_bytes_(cbInBuffer) LPVOID lpvInBuffer, + _In_ DWORD cbInBuffer, + _Out_writes_bytes_to_(cbOutBuffer, *lpcbBytesReturned) LPVOID lpvOutBuffer, + _In_ DWORD cbOutBuffer, + _Out_ LPDWORD lpcbBytesReturned, + _In_opt_ LPWSACOMPLETION lpCompletion, + _In_ LPWSATHREADID lpThreadId) +{ + WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED); + return ERROR_CALL_NOT_IMPLEMENTED; +} + +INT +WSAAPI +mwsNSPLookupServiceEnd( + _In_ HANDLE hLookup) +{ + PWSHANDLEINTERN pLook; + HANDLE hHeap; + INT res; + + res = NO_ERROR; + pLook = (PWSHANDLEINTERN)hLookup; + hHeap = GetProcessHeap(); +#ifdef NSP_REDIRECT + res = pLook->rdrproc.NSPLookupServiceEnd(pLook->rdrLookup); +#endif + if (pLook->hostnameW != NULL) + HeapFree(hHeap,0,pLook->hostnameW); + HeapFree(hHeap,0,pLook); + return res; +} + +INT +WSAAPI mwsNSPSetService( + _In_ LPGUID lpProviderId, + _In_ LPWSASERVICECLASSINFOW lpServiceClassInfo, + _In_ LPWSAQUERYSETW lpqsRegInfo, + _In_ WSAESETSERVICEOP essOperation, + _In_ DWORD dwControlFlags) +{ + WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED); + return ERROR_CALL_NOT_IMPLEMENTED; +} +INT +WSAAPI mwsNSPInstallServiceClass( + _In_ LPGUID lpProviderId, + _In_ LPWSASERVICECLASSINFOW lpServiceClassInfo) +{ + WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED); + return ERROR_CALL_NOT_IMPLEMENTED; +} + +INT +WSAAPI +mwsNSPRemoveServiceClass( + _In_ LPGUID lpProviderId, + _In_ LPGUID lpServiceClassId) +{ + WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED); + return ERROR_CALL_NOT_IMPLEMENTED; +} + +INT +WSAAPI mwsNSPGetServiceClassInfo( + _In_ LPGUID lpProviderId, + _In_ LPDWORD lpdwBufSize, + _In_ LPWSASERVICECLASSINFOW lpServiceClassInfo) +{ + WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED); + return ERROR_CALL_NOT_IMPLEMENTED; +} + +/* + hostnameA / hostnameW + * only used by HOSTBYNAME + * only one should be set + +*/ +INT +NSP_LookupServiceBeginW(PWSHANDLEINTERN data, + CHAR* hostnameA, + WCHAR* hostnameW, + DWORD CallID) +{ + HANDLE hHeap; + + if (data->CallID != 0) + return WSAEFAULT; + data->CallID = CallID; + + if ( (CallID == NSP_CALLID_HOSTBYNAME) || + (CallID == NSP_CALLID_SERVICEBYNAME) ) + { + hHeap = GetProcessHeap(); + + if (data->hostnameW != NULL) + HeapFree(hHeap,0,data->hostnameW); + if (hostnameA != NULL) + { + data->hostnameW = StrA2WHeapAlloc(hHeap,hostnameA); + } + else + { + data->hostnameW = StrCpyHeapAllocW(hHeap, hostnameW); + } + } + + WSASetLastError(0); + + return ERROR_SUCCESS; +} + +INT +NSP_GetHostNameHeapAllocW( + _Out_ WCHAR** hostname) +{ + WCHAR* name; + HANDLE hHeap = GetProcessHeap(); + DWORD bufCharLen = MAX_COMPUTERNAME_LENGTH + 1; + DWORD bufByteLen = bufCharLen * sizeof(WCHAR); + + name = HeapAlloc(hHeap,0,bufByteLen); + + if (!GetComputerNameExW(ComputerNameDnsHostname, + name, + &bufCharLen)) + { + HeapFree(hHeap,0,name); + WSASetLastError(WSAEFAULT); + return SOCKET_ERROR; + } + + *hostname = name; + return ERROR_SUCCESS; +} + +/* This function is far from perfect but it works enough */ +IP4_ADDRESS +FindEntryInHosts(IN CONST WCHAR FAR* wname) +{ + BOOL Found = FALSE; + HANDLE HostsFile; + CHAR HostsDBData[BUFSIZ] = { 0 }; + PCHAR SystemDirectory = HostsDBData; + PCHAR HostsLocation = "\\drivers\\etc\\hosts"; + PCHAR AddressStr, DnsName = NULL, AddrTerm, NameSt, NextLine, ThisLine, Comment; + UINT SystemDirSize = sizeof(HostsDBData) - 1, ValidData = 0; + DWORD ReadSize; + DWORD Address; + CHAR name[MAX_HOSTNAME_LEN+1]; + wcstombs(name, wname, MAX_HOSTNAME_LEN); + + /* We assume that the parameters are valid */ + + if (!GetSystemDirectoryA(SystemDirectory, SystemDirSize)) + { + WSASetLastError(WSANO_RECOVERY); + //WS_DbgPrint(MIN_TRACE, ("Could not get windows system directory.\n")); + return 0; /* Can't get system directory */ + } + + strncat(SystemDirectory, + HostsLocation, + SystemDirSize ); + + HostsFile = CreateFileA(SystemDirectory, + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, + NULL); + if (HostsFile == INVALID_HANDLE_VALUE) + { + WSASetLastError(WSANO_RECOVERY); + return 0; + } + + while(!Found && + ReadFile(HostsFile, + HostsDBData + ValidData, + sizeof(HostsDBData) - ValidData, + &ReadSize, + NULL)) + { + ValidData += ReadSize; + ReadSize = 0; + NextLine = ThisLine = HostsDBData; + + /* Find the beginning of the next line */ + while(NextLine < HostsDBData + ValidData && + *NextLine != '\r' && *NextLine != '\n' ) + { + NextLine++; + } + + /* Zero and skip, so we can treat what we have as a string */ + if( NextLine > HostsDBData + ValidData ) + break; + + *NextLine = 0; NextLine++; + + Comment = strchr( ThisLine, '#' ); + if( Comment ) *Comment = 0; /* Terminate at comment start */ + + AddressStr = ThisLine; + /* Find the first space separating the IP address from the DNS name */ + AddrTerm = strchr(ThisLine, ' '); + if (AddrTerm) + { + /* Terminate the address string */ + *AddrTerm = 0; + + /* Find the last space before the DNS name */ + NameSt = strrchr(ThisLine, ' '); + + /* If there is only one space (the one we removed above), then just use the address terminator */ + if (!NameSt) + NameSt = AddrTerm; + + /* Move from the space to the first character of the DNS name */ + NameSt++; + + DnsName = NameSt; + + if (!strcmp(name, DnsName)) + { + Found = TRUE; + break; + } + } + + /* Get rid of everything we read so far */ + while( NextLine <= HostsDBData + ValidData && + isspace (*NextLine)) + { + NextLine++; + } + + if (HostsDBData + ValidData - NextLine <= 0) + break; + + //WS_DbgPrint(MAX_TRACE,("About to move %d chars\n", + // HostsDBData + ValidData - NextLine)); + + memmove(HostsDBData, + NextLine, + HostsDBData + ValidData - NextLine ); + ValidData -= NextLine - HostsDBData; + //WS_DbgPrint(MAX_TRACE,("Valid bytes: %d\n", ValidData)); + } + + CloseHandle(HostsFile); + + if (!Found) + { + //WS_DbgPrint(MAX_TRACE,("Not found\n")); + WSASetLastError(WSANO_DATA); + return 0; + } + + if (strstr(AddressStr, ":")) + { + //DbgPrint("AF_INET6 NOT SUPPORTED!\n"); + WSASetLastError(WSAEINVAL); + return 0; + } + + Address = inet_addr(AddressStr); + if (Address == INADDR_NONE) + { + WSASetLastError(WSAEINVAL); + return 0; + } + + return Address; +} + +INT +NSP_GetHostByNameHeapAllocW( + _In_ WCHAR* name, + _In_ GUID* lpProviderId, + _Out_ PWSHOSTINFOINTERN hostinfo) +{ + HANDLE hHeap = GetProcessHeap(); + enum addr_type + { + GH_INVALID, + GH_IPV6, + GH_IPV4, + GH_RFC1123_DNS + }; + typedef enum addr_type addr_type; + addr_type addr; + INT ret = 0; + WCHAR* found = 0; + DNS_STATUS dns_status = {0}; + /* include/WinDNS.h -- look up DNS_RECORD on MSDN */ + PDNS_RECORD dp; + PDNS_RECORD curr; + WCHAR* tmpHostnameW; + CHAR* tmpHostnameA; + IP4_ADDRESS address; + INT result = ERROR_SUCCESS; + + /* neded to be cleand up if != NULL */ + tmpHostnameW = NULL; + dp = NULL; + + addr = GH_INVALID; + + if (name == NULL) + { + result = ERROR_INVALID_PARAMETER; + goto cleanup; + } + + /* Hostname "" / "localhost" + - convert to "computername" */ + if ( (wcscmp(L"",name) == 0) /*|| + (wcsicmp(L"localhost",name) == 0)*/ ) + { + ret = NSP_GetHostNameHeapAllocW(&tmpHostnameW); + if (ret != ERROR_SUCCESS) + { + result = ret; + goto cleanup; + } + name = tmpHostnameW; + } + + /* Is it an IPv6 address? */ + found = wcschr(name,L':'); + if( found != NULL ) + { + addr = GH_IPV6; + goto act; + } + + /* Is it an IPv4 address? */ + if (!iswalpha(name[0])) + { + addr = GH_IPV4; + goto act; + } + + addr = GH_RFC1123_DNS; + + /* Broken out in case we want to get fancy later */ + act: + switch(addr) + { + case GH_IPV6: + WSASetLastError(ERROR_CALL_NOT_IMPLEMENTED); + result = ERROR_CALL_NOT_IMPLEMENTED; + goto cleanup; + break; + + case GH_INVALID: + WSASetLastError(WSAEFAULT); + result = ERROR_INVALID_PARAMETER; + goto cleanup; + break; + + /* Note: If passed an IP address, MSDN says that gethostbyname() + treats it as an unknown host. + This is different from the unix implementation. Use inet_addr() + */ + case GH_IPV4: + case GH_RFC1123_DNS: + /* DNS_TYPE_A: include/WinDNS.h */ + /* DnsQuery -- lib/dnsapi/dnsapi/query.c */ + + /* Look for the DNS name in the hosts file */ + if ((address = FindEntryInHosts(name)) != 0) + { + hostinfo->hostnameW = StrCpyHeapAllocW(hHeap,name); + hostinfo->addr4 = address; + result = ERROR_SUCCESS; + goto cleanup; + } + + tmpHostnameA = StrW2AHeapAlloc(hHeap, name); + dns_status = DnsQuery_A(tmpHostnameA, + DNS_TYPE_A, + DNS_QUERY_STANDARD, + 0, + /* extra dns servers */ &dp, + 0); + HeapFree(hHeap, 0, tmpHostnameA); + + if ( (dns_status != 0) || (dp == NULL) ) + { + result = WSAHOST_NOT_FOUND; + goto cleanup; + } + + //ASSERT(dp->wType == DNS_TYPE_A); + //ASSERT(dp->wDataLength == sizeof(DNS_A_DATA)); + curr = dp; + while ( (curr->pNext != NULL) || (curr->wType != DNS_TYPE_A) ) + curr = curr->pNext; + + if (curr->wType != DNS_TYPE_A) + { + result = WSASERVICE_NOT_FOUND; + goto cleanup; + } + + //WS_DbgPrint(MID_TRACE,("populating hostent\n")); + //WS_DbgPrint(MID_TRACE,("pName is (%s)\n", curr->pName)); + //populate_hostent(p->Hostent, + // (PCHAR)curr->pName, + // curr->Data.A.IpAddress); + hostinfo->hostnameW = StrA2WHeapAlloc(hHeap,curr->pName); + hostinfo->addr4 = curr->Data.A.IpAddress; + result = ERROR_SUCCESS; + goto cleanup; + + //WS_DbgPrint(MID_TRACE,("Called DnsQuery, but host not found. Err: %i\n", + // dns_status)); + //WSASetLastError(WSAHOST_NOT_FOUND); + //return NULL; + + break; + + default: + result = WSANO_RECOVERY; + goto cleanup; + break; + } + + result = WSANO_RECOVERY; + +cleanup: + if (dp != NULL) + DnsRecordListFree(dp, DnsFreeRecordList); + if (tmpHostnameW != NULL) + HeapFree(hHeap,0,tmpHostnameW); + return result; +} + +#define SKIPWS(ptr,act) \ +{while(*ptr && isspace(*ptr)) ptr++; if(!*ptr) act;} +#define SKIPANDMARKSTR(ptr,act) \ +{while(*ptr && !isspace(*ptr)) ptr++; \ + if(!*ptr) {act;} else { *ptr = 0; ptr++; }} + +static BOOL +DecodeServEntFromString(IN PCHAR ServiceString, + OUT PCHAR *ServiceName, + OUT PCHAR *PortNumberStr, + OUT PCHAR *ProtocolStr, + IN PCHAR *Aliases, + IN DWORD MaxAlias) +{ + UINT NAliases = 0; + + //WS_DbgPrint(MAX_TRACE, ("Parsing service ent [%s]\n", ServiceString)); + + SKIPWS(ServiceString, return FALSE); + *ServiceName = ServiceString; + SKIPANDMARKSTR(ServiceString, return FALSE); + SKIPWS(ServiceString, return FALSE); + *PortNumberStr = ServiceString; + SKIPANDMARKSTR(ServiceString, ;); + + while( *ServiceString && NAliases < MaxAlias - 1 ) + { + SKIPWS(ServiceString, break); + if( *ServiceString ) + { + SKIPWS(ServiceString, ;); + if( strlen(ServiceString) ) + { + //WS_DbgPrint(MAX_TRACE, ("Alias: %s\n", ServiceString)); + *Aliases++ = ServiceString; + NAliases++; + } + SKIPANDMARKSTR(ServiceString, ;); + } + } + *Aliases = NULL; + + *ProtocolStr = strchr(*PortNumberStr,'/'); + if( !*ProtocolStr ) return FALSE; + **ProtocolStr = 0; (*ProtocolStr)++; + + //WS_DbgPrint(MAX_TRACE, ("Parsing done: %s %s %s %d\n", + // *ServiceName, *ProtocolStr, *PortNumberStr, + // NAliases)); + + return TRUE; +} + +INT +NSP_GetServiceByNameHeapAllocW( + _In_ WCHAR* nameW, + _In_ GUID* lpProviderId, + _Out_ PWSHOSTINFOINTERN hostinfo) +{ + BOOL Found = FALSE; + HANDLE ServicesFile; + CHAR ServiceDBData[BUFSIZ * sizeof(WCHAR)] = { 0 }; + PWCHAR SystemDirectory = (PWCHAR)ServiceDBData; /* Reuse this stack space */ + PWCHAR ServicesFileLocation = L"\\drivers\\etc\\services"; + PCHAR ThisLine = 0, NextLine = 0, ServiceName = 0, PortNumberStr = 0, + ProtocolStr = 0, Comment = 0, EndValid; + PCHAR Aliases[WS2_INTERNAL_MAX_ALIAS] = { 0 }; + PCHAR* AliasPtr; + UINT i = 0, + SystemDirSize = (sizeof(ServiceDBData) / sizeof(WCHAR)) - 1; + DWORD ReadSize = 0; + HANDLE hHeap; + PCHAR nameA = NULL; + PCHAR nameServiceA = NULL; + PCHAR nameProtoA = NULL; + INT res = WSANO_RECOVERY; + + if ( !nameW ) + { + res = WSANO_RECOVERY; + goto End; + } + + hHeap = GetProcessHeap(); + nameA = StrW2AHeapAlloc(hHeap, nameW); + + /* nameA has the form / + we split these now */ + nameProtoA = strchr(nameA,'/'); + if (nameProtoA == NULL) + { + res = WSANO_RECOVERY; + goto End; + } + nameProtoA++; + i = (DWORD)(nameProtoA - nameA - 1); + nameServiceA = (PCHAR)HeapAlloc(hHeap,0,i+1); + strncpy(nameServiceA, nameA, i); + nameServiceA[i] = '\0'; + + if ( !GetSystemDirectoryW( SystemDirectory, SystemDirSize ) ) + { + /* Can't get system directory */ + res = WSANO_RECOVERY; + goto End; + } + + wcsncat( SystemDirectory, + ServicesFileLocation, + SystemDirSize ); + + ServicesFile = CreateFileW(SystemDirectory, + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, + NULL ); + + if ( ServicesFile == INVALID_HANDLE_VALUE ) + { + return WSANO_RECOVERY; + } + + /* Scan the services file ... + * + * We will be share the buffer on the lines. If the line does not fit in + * the buffer, then moving it to the beginning of the buffer and read + * the remnants of line from file. + */ + + /* Initial Read */ + ReadFile(ServicesFile, + ServiceDBData, + sizeof( ServiceDBData ) - 1, + &ReadSize, NULL ); + ThisLine = NextLine = ServiceDBData; + EndValid = ServiceDBData + ReadSize; + ServiceDBData[sizeof(ServiceDBData) - 1] = '\0'; + + while(ReadSize) + { + for(; *NextLine != '\r' && *NextLine != '\n'; NextLine++) + { + if(NextLine == EndValid) + { + int LineLen = NextLine - ThisLine; + + if(ThisLine == ServiceDBData) + { + //WS_DbgPrint(MIN_TRACE,("Line too long")); + return WSANO_RECOVERY; + } + + memmove(ServiceDBData, ThisLine, LineLen); + + ReadFile(ServicesFile, ServiceDBData + LineLen, + sizeof( ServiceDBData )-1 - LineLen, + &ReadSize, NULL ); + + EndValid = ServiceDBData + LineLen + ReadSize; + NextLine = ServiceDBData + LineLen; + ThisLine = ServiceDBData; + + if(!ReadSize) break; + } + } + + *NextLine = '\0'; + Comment = strchr( ThisLine, '#' ); + if( Comment ) *Comment = '\0'; /* Terminate at comment start */ + + if (DecodeServEntFromString(ThisLine, + &ServiceName, + &PortNumberStr, + &ProtocolStr, + Aliases, + WS2_INTERNAL_MAX_ALIAS) && + !strcmp(ProtocolStr, nameProtoA)) + { + Found = (strcmp( ServiceName, nameServiceA) == 0); + AliasPtr = Aliases; + while ((!Found) && (*AliasPtr != NULL)) + { + Found = (strcmp( *AliasPtr, nameServiceA) == 0); + AliasPtr++; + } + if (Found) + break; + } + NextLine++; + ThisLine = NextLine; + } + + /* This we'll do no matter what */ + CloseHandle( ServicesFile ); + + if ( !Found ) + { + return WSANO_DATA; + } + + hostinfo->addr4 = 0; + hostinfo->servnameW = StrA2WHeapAlloc(hHeap,ServiceName); + hostinfo->servprotoW = StrA2WHeapAlloc(hHeap,ProtocolStr); + hostinfo->servaliasesA = StrAryCpyHeapAllocA(hHeap,(char**)&Aliases); + hostinfo->servport = atoi(PortNumberStr); + + res = NO_ERROR; + +End: + if (nameA != NULL) + HeapFree(hHeap,0,nameA); + if (nameServiceA != NULL) + HeapFree(hHeap,0,nameServiceA); + //if (nameProtoA != NULL) + // HeapFree(hHeap,nameProtoA); + return res; +} + +INT +NSP_LookupServiceNextW( + _In_ PWSHANDLEINTERN data, + _In_ DWORD CallID, + _Inout_ LPWSAQUERYSETW lpRes, + _Inout_ LPDWORD lpResLen) +{ + MSW_BUFFER buf; + WSHOSTINFOINTERN hostinfo; + INT result; + HANDLE hHeap = GetProcessHeap(); + /* cleanup-vars */ + CHAR* ServiceInstanceNameA = NULL; + CHAR* ServiceProtocolNameA = NULL; + + RtlZeroMemory(&hostinfo,sizeof(hostinfo)); + + /* init and build result-buffer */ + mswBufferInit(&buf,(BYTE*)lpRes,*lpResLen); + mswBufferIncUsed(&buf,sizeof(*lpRes)); + /* QueryDataSet-Size without "blob-data"-size! */ + lpRes->dwSize = sizeof(*lpRes); + lpRes->dwNameSpace = NS_DNS; + + if ( (CallID == NSP_CALLID_HOSTNAME) || + (CallID == NSP_CALLID_HOSTBYNAME) || + (CallID == NSP_CALLID_SERVICEBYNAME) ) + { + if (data->CallIDCounter >= 1) + { + result = WSAENOMORE; + goto End; + } + } + else + { + result = WSANO_RECOVERY; + goto End; + } + data->CallIDCounter++; + + if (CallID == NSP_CALLID_HOSTNAME) + { + result = NSP_GetHostNameHeapAllocW(&hostinfo.hostnameW); + if (result != ERROR_SUCCESS) + goto End; + hostinfo.addr4 = 0; + } + else if (CallID == NSP_CALLID_HOSTBYNAME) + { + result = NSP_GetHostByNameHeapAllocW(data->hostnameW, + &data->providerId, + &hostinfo); + if (result != ERROR_SUCCESS) + goto End; + } + else if (CallID == NSP_CALLID_SERVICEBYNAME) + { + result = NSP_GetServiceByNameHeapAllocW(data->hostnameW, + &data->providerId, + &hostinfo); + if (result != ERROR_SUCCESS) + goto End; + } + else + { + result = WSANO_RECOVERY; // Internal error! + goto End; + } + + if ( ( (LUP_RETURN_BLOB & data->dwControlFlags) != 0) || + ( (LUP_RETURN_NAME & data->dwControlFlags) != 0) ) + { + ServiceInstanceNameA = StrW2AHeapAlloc(hHeap,hostinfo.hostnameW); + if (ServiceInstanceNameA == NULL) + { + result = WSAEFAULT; + goto End; + + } + if (CallID == NSP_CALLID_SERVICEBYNAME) + { + ServiceProtocolNameA = StrW2AHeapAlloc(hHeap,hostinfo.servprotoW); + if (ServiceProtocolNameA == NULL) + { + result = WSAEFAULT; + goto End; + + } + } + } + + if ((LUP_RETURN_ADDR & data->dwControlFlags) != 0) + { + if(!mswBufferAppendAddr_AddrInfoW(&buf,lpRes,hostinfo.addr4)) + { + *lpResLen = buf.bytesUsed; + result = WSAEFAULT; + goto End; + } + } + + if ((LUP_RETURN_BLOB & data->dwControlFlags) != 0) + { + if (CallID == NSP_CALLID_HOSTBYNAME) + { + /* Write data for PBLOB (hostent) */ + if(!mswBufferAppendBlob_Hostent(&buf, + lpRes, + ServiceInstanceNameA, + hostinfo.addr4)) + { + *lpResLen = buf.bytesUsed; + result = WSAEFAULT; + goto End; + } + } + else if (CallID == NSP_CALLID_SERVICEBYNAME) + { + /* Write data for PBLOB (servent) */ + if(!mswBufferAppendBlob_Servent(&buf, + lpRes, + ServiceInstanceNameA,/* ServiceName */ + hostinfo.servaliasesA, + ServiceProtocolNameA, + hostinfo.servport)) + { + *lpResLen = buf.bytesUsed; + result = WSAEFAULT; + goto End; + } + } + else + { + result = WSANO_RECOVERY; + goto End; + } + } + + if ((LUP_RETURN_NAME & data->dwControlFlags) != 0) + { + /* HostByName sets the ServiceInstanceName to a + (UNICODE)copy of hostent.h_name */ + lpRes->lpszServiceInstanceName = (LPWSTR)mswBufferEndPtr(&buf); + if(!mswBufferAppendStrW(&buf,hostinfo.hostnameW)) + { + lpRes->lpszServiceInstanceName = NULL; + *lpResLen = buf.bytesUsed; + result = WSAEFAULT; + goto End; + } + } + + *lpResLen = buf.bytesUsed; + + result = ERROR_SUCCESS; +End: + /* cleanup */ + if (ServiceInstanceNameA != NULL) + HeapFree(hHeap,0,ServiceInstanceNameA); + if (ServiceProtocolNameA != NULL) + HeapFree(hHeap,0,ServiceProtocolNameA); + if (hostinfo.hostnameW != NULL) + HeapFree(hHeap,0,hostinfo.hostnameW); + if (hostinfo.servnameW != NULL) + HeapFree(hHeap,0,hostinfo.servnameW); + if (hostinfo.servprotoW != NULL) + HeapFree(hHeap,0,hostinfo.servprotoW); + return result; +} + +/* Implementations - Exports */ +/* + * @implemented + */ +int +WINAPI +NSPStartup( + _In_ LPGUID lpProviderId, + _Out_ LPNSP_ROUTINE lpRout + ) +{ + INT ret; + + if ( (lpRout == NULL) || + (lpRout->cbSize != sizeof(NSP_ROUTINE)) ) + { + WSASetLastError(ERROR_INVALID_PARAMETER); + return ERROR_INVALID_PARAMETER; + } + + mwsNSPInit(); + + /* set own Provider GUID - maybe we need + here to set the original mswsock-GUID?! */ + + /* Win2k3 returns + - Version 1.1 + - no NSPIoctl + - sets cbSize to 44! */ + lpRout->dwMajorVersion = 1; + lpRout->dwMinorVersion = 1; + lpRout->cbSize = sizeof(*lpRout)-sizeof(lpRout->NSPIoctl); + lpRout->NSPCleanup = &mwsNSPCleanUp; + lpRout->NSPLookupServiceBegin = &mwsNSPLookupServiceBegin; + lpRout->NSPLookupServiceNext = &mwsNSPLookupServiceNext; + lpRout->NSPLookupServiceEnd = &mwsNSPLookupServiceEnd; + lpRout->NSPSetService = &mwsNSPSetService; + lpRout->NSPInstallServiceClass = &mwsNSPInstallServiceClass; + lpRout->NSPRemoveServiceClass = &mwsNSPRemoveServiceClass; + lpRout->NSPGetServiceClassInfo = &mwsNSPGetServiceClassInfo; + lpRout->NSPIoctl = NULL;// &mwsNSPIoCtl; + + ret = NO_ERROR; + +#ifdef NSP_REDIRECT + hLib = LoadLibraryA(rdrLib); + if (hLib) + rdrNSPStartup = (lpRdrNSPStartup)GetProcAddress(hLib,"NSPStartup"); + if (rdrNSPStartup) + { + if (IsEqualGUID(lpProviderId,&guid_mswsock_TcpIp)) + { + RtlZeroMemory(&rdrproc_tcpip,sizeof(rdrproc_tcpip)); + rdrproc_tcpip.cbSize = sizeof(rdrproc_tcpip); + ret = rdrNSPStartup(lpProviderId,&rdrproc_tcpip); + } + else if (IsEqualGUID(lpProviderId,&guid_mswsock_NLA)) + { + RtlZeroMemory(&rdrproc_nla,sizeof(rdrproc_nla)); + rdrproc_nla.cbSize = sizeof(rdrproc_nla); + ret = rdrNSPStartup(lpProviderId,&rdrproc_nla); + } + else ret = SOCKET_ERROR; + //*lpRout = rdrproc; + //return r; + //lpRout->NSPLookupServiceBegin = rdrproc.NSPLookupServiceBegin; + } +#endif // NSP_REDIRECT + + return ret; +} Index: dll/win32/mswsock/stubs.c =================================================================== --- dll/win32/mswsock/stubs.c (revision 72643) +++ dll/win32/mswsock/stubs.c (working copy) @@ -9,6 +9,7 @@ #include "precomp.h" +#include #include #include @@ -417,24 +418,9 @@ return TRUE; } - /* * @unimplemented */ -INT -WINAPI -NSPStartup( - LPGUID lpProviderId, - LPNSP_ROUTINE lpnspRoutines - ) -{ - return TRUE; -} - - -/* - * @unimplemented - */ int WINAPI WSPStartup( Index: dll/win32/wininet/urlcache.c =================================================================== --- dll/win32/wininet/urlcache.c (revision 72643) +++ dll/win32/wininet/urlcache.c (working copy) @@ -1786,6 +1786,11 @@ TRACE("(%s, %p, %p, %x, %x)\n", debugstr_a(url), entry_info, size, flags, unicode); + if (!entry_info) { + SetLastError(ERROR_INSUFFICIENT_BUFFER); + return FALSE; + } + if(flags & ~GET_INSTALLED_ENTRY) FIXME("ignoring unsupported flags: %x\n", flags); Index: dll/win32/ws2_32_new/CMakeLists.txt =================================================================== --- dll/win32/ws2_32_new/CMakeLists.txt (revision 72643) +++ dll/win32/ws2_32_new/CMakeLists.txt (working copy) @@ -1,6 +1,6 @@ add_definitions(-DLE) -spec2def(ws2_32_new.dll ws2_32.spec) +spec2def(ws2_32.dll ws2_32.spec ADD_IMPORTLIB) include_directories( inc @@ -45,13 +45,13 @@ src/wsautil.c inc/ws2_32.h) -add_library(ws2_32_new SHARED +add_library(ws2_32 SHARED ${SOURCE} ws2_32.rc - ${CMAKE_CURRENT_BINARY_DIR}/ws2_32_new.def) + ${CMAKE_CURRENT_BINARY_DIR}/ws2_32.def) -set_module_type(ws2_32_new win32dll) -target_link_libraries(ws2_32_new ${PSEH_LIB}) -add_importlibs(ws2_32_new user32 advapi32 ws2help msvcrt kernel32 ntdll) -add_pch(ws2_32_new inc/ws2_32.h SOURCE) -add_cd_file(TARGET ws2_32_new DESTINATION reactos/system32 FOR all) +set_module_type(ws2_32 win32dll) +target_link_libraries(ws2_32 ${PSEH_LIB}) +add_importlibs(ws2_32 user32 advapi32 ws2help msvcrt kernel32 ntdll) +add_pch(ws2_32 inc/ws2_32.h SOURCE) +add_cd_file(TARGET ws2_32 DESTINATION reactos/system32 FOR all) Index: dll/win32/ws2_32_new/inc/ws2_32.h =================================================================== --- dll/win32/ws2_32_new/inc/ws2_32.h (revision 72643) +++ dll/win32/ws2_32_new/inc/ws2_32.h (working copy) @@ -35,6 +35,7 @@ #include #include +#include /* Missing definitions */ #define SO_OPENTYPE 0x7008 Index: dll/win32/ws2_32_new/inc/ws2_32p.h =================================================================== --- dll/win32/ws2_32_new/inc/ws2_32p.h (revision 72643) +++ dll/win32/ws2_32_new/inc/ws2_32p.h (working copy) @@ -79,6 +79,7 @@ LONG RefCount; WSPPROC_TABLE Service; HINSTANCE DllHandle; + WSPUPCALLTABLE UpcallTable; } TPROVIDER, *PTPROVIDER; typedef struct _TCATALOG_ENTRY Index: dll/win32/ws2_32_new/src/addrconv.c =================================================================== --- dll/win32/ws2_32_new/src/addrconv.c (revision 72643) +++ dll/win32/ws2_32_new/src/addrconv.c (working copy) @@ -73,6 +73,7 @@ register u_long val, base, n; register unsigned char c; u_long parts[4], *pp = parts; + if (!cp) return INADDR_ANY; again: /* Index: dll/win32/ws2_32_new/src/addrinfo.c =================================================================== --- dll/win32/ws2_32_new/src/addrinfo.c (revision 72643) +++ dll/win32/ws2_32_new/src/addrinfo.c (working copy) @@ -90,18 +90,24 @@ ParseV4Address(IN PCWSTR AddressString, OUT PDWORD pAddress) { - IN_ADDR Address; - PCWSTR Terminator; - NTSTATUS Status; - - *pAddress = 0; - Status = RtlIpv4StringToAddressW(AddressString, FALSE, &Terminator, &Address); - - if (!NT_SUCCESS(Status)) + DWORD Address; + CHAR AnsiAddressString[MAX_HOSTNAME_LEN]; + if (!AddressString) return FALSE; + WideCharToMultiByte(CP_ACP, + 0, + AddressString, + -1, + AnsiAddressString, + sizeof(AnsiAddressString), + NULL, + 0); + /* inet_addr returns address long in network long */ + Address = inet_addr(AnsiAddressString); + if (*pAddress) + *pAddress = Address; - *pAddress = Address.S_un.S_addr; - return TRUE; + return (Address != INADDR_NONE); } static @@ -530,7 +536,7 @@ pszServiceName, -1, AnsiServiceName, - 256, + sizeof(AnsiServiceName), NULL, 0); @@ -576,7 +582,7 @@ /* If we got 0, then fail */ if (wPort == 0) { - return iSocketType ? EAI_SERVICE : EAI_NONAME; + return EAI_SERVICE; } /* Check if this was for both */ @@ -657,7 +663,7 @@ pszNodeName, -1, AnsiNodeName, - 256, + sizeof(AnsiNodeName), NULL, 0); @@ -736,7 +742,7 @@ struct addrinfo FAR * FAR *res) { INT ErrorCode; - LPWSTR UnicodeNodeName; + LPWSTR UnicodeNodeName = NULL; LPWSTR UnicodeServName = NULL; DPRINT("getaddrinfo: %s, %s, %p, %p\n", nodename, servname, hints, res); @@ -743,16 +749,16 @@ /* Check for WSAStartup */ if ((ErrorCode = WsQuickProlog()) != ERROR_SUCCESS) return ErrorCode; - /* Assume NULL */ - *res = NULL; - /* Convert the node name */ - UnicodeNodeName = UnicodeDupFromAnsi((LPSTR)nodename); - if (!UnicodeNodeName) + if (nodename) { - /* Prepare to fail */ - ErrorCode = GetLastError(); - goto Quickie; + UnicodeNodeName = UnicodeDupFromAnsi((LPSTR)nodename); + if (!UnicodeNodeName) + { + /* Prepare to fail */ + ErrorCode = GetLastError(); + goto Quickie; + } } /* Convert the servname too, if we have one */ @@ -937,7 +943,7 @@ { /* Setup the data for it */ ServiceString = ServiceBuffer; - ServLength = sizeof(ServiceBuffer) / sizeof(WCHAR); + ServLength = sizeof(ServiceBuffer) - 1; } /* Now call the unicode function */ Index: dll/win32/ws2_32_new/src/dcatalog.c =================================================================== --- dll/win32/ws2_32_new/src/dcatalog.c (revision 72643) +++ dll/win32/ws2_32_new/src/dcatalog.c (working copy) @@ -10,10 +10,11 @@ #include +#define NDEBUG +#include + /* DATA **********************************************************************/ -#define TCCATALOG_NAME "Protocol_Catalog9" - #define WsTcLock() EnterCriticalSection((LPCRITICAL_SECTION)&Catalog->Lock); #define WsTcUnlock() LeaveCriticalSection((LPCRITICAL_SECTION)&Catalog->Lock); @@ -45,14 +46,33 @@ DWORD RegSize = sizeof(DWORD); DWORD UniqueId = 0; DWORD NewData = 0; + CHAR* CatalogKeyName; /* Initialize the catalog lock and namespace list */ InitializeCriticalSection((LPCRITICAL_SECTION)&Catalog->Lock); InitializeListHead(&Catalog->ProtocolList); + /* Read the catalog name */ + ErrorCode = RegQueryValueEx(ParentKey, + "Current_Protocol_Catalog", + 0, + &RegType, + NULL, + &RegSize); + + CatalogKeyName = HeapAlloc(WsSockHeap, 0, RegSize); + + /* Read the catalog name */ + ErrorCode = RegQueryValueEx(ParentKey, + "Current_Protocol_Catalog", + 0, + &RegType, + (LPBYTE)CatalogKeyName, + &RegSize); + /* Open the Catalog Key */ ErrorCode = RegOpenKeyEx(ParentKey, - TCCATALOG_NAME, + CatalogKeyName, 0, MAXIMUM_ALLOWED, &CatalogKey); @@ -67,7 +87,7 @@ { /* Create the Catalog Name */ ErrorCode = RegCreateKeyEx(ParentKey, - TCCATALOG_NAME, + CatalogKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, @@ -77,6 +97,10 @@ &CreateDisposition); } + HeapFree(WsSockHeap, 0, CatalogKeyName); + RegType = REG_DWORD; + RegSize = sizeof(DWORD); + /* Fail if that didn't work */ if (ErrorCode != ERROR_SUCCESS) return FALSE; @@ -149,6 +173,7 @@ } else { + RegSize = sizeof(DWORD); /* Read the serial number */ ErrorCode = RegQueryValueEx(CatalogKey, "Serial_Access_Num", @@ -409,6 +434,7 @@ IN DWORD CatalogEntryId, IN PTCATALOG_ENTRY *CatalogEntry) { + INT ErrorCode = WSAEINVAL; PLIST_ENTRY NextEntry = Catalog->ProtocolList.Flink; PTCATALOG_ENTRY Entry; @@ -435,6 +461,7 @@ /* Reference the entry and return it */ InterlockedIncrement(&Entry->RefCount); *CatalogEntry = Entry; + ErrorCode = ERROR_SUCCESS; break; } } @@ -443,7 +470,7 @@ WsTcUnlock(); /* Return */ - return ERROR_SUCCESS; + return ErrorCode; } DWORD @@ -458,6 +485,7 @@ INT ErrorCode = WSAEINVAL; PLIST_ENTRY NextEntry = Catalog->ProtocolList.Flink; PTCATALOG_ENTRY Entry; + DPRINT("WsTcGetEntryFromTriplet: %lx, %lx, %lx, %lx\n", af, type, protocol, StartId); /* Assume failure */ *CatalogEntry = NULL; @@ -487,18 +515,18 @@ Entry = CONTAINING_RECORD(NextEntry, TCATALOG_ENTRY, CatalogLink); NextEntry = NextEntry->Flink; - /* Check if Address Family Matches or if it's wildcard */ - if ((Entry->ProtocolInfo.iAddressFamily == af) || (af == AF_UNSPEC)) - { + /* Check if Protocol is In Range or if it's wildcard */ + if (((Entry->ProtocolInfo.iProtocol >= protocol) && + ((Entry->ProtocolInfo.iProtocol + + Entry->ProtocolInfo.iProtocolMaxOffset) <= protocol)) || + (protocol == 0)) + { /* Check if Socket Type Matches or if it's wildcard */ if ((Entry->ProtocolInfo.iSocketType == type) || (type == 0)) { - /* Check if Protocol is In Range or if it's wildcard */ - if (((Entry->ProtocolInfo.iProtocol >= protocol) && - ((Entry->ProtocolInfo.iProtocol + - Entry->ProtocolInfo.iProtocolMaxOffset) <= protocol)) || - (protocol == 0)) - { + /* Check if Address Family Matches or if it's wildcard */ + if ((Entry->ProtocolInfo.iAddressFamily == af) || (af == AF_UNSPEC)) + { /* Check if it doesn't already have a provider */ if (!Entry->Provider) { @@ -517,17 +545,17 @@ } else { - ErrorCode = WSAEPROTONOSUPPORT; + ErrorCode = (af < 0) ? WSAEINVAL : WSAEAFNOSUPPORT; } } else { - ErrorCode = WSAESOCKTNOSUPPORT; + ErrorCode = (type < 0) ? WSAEINVAL : WSAESOCKTNOSUPPORT; } } else { - ErrorCode = WSAEAFNOSUPPORT; + ErrorCode = (protocol < 0) ? WSAEINVAL : WSAEPROTONOSUPPORT; } } @@ -580,6 +608,7 @@ { INT ErrorCode = ERROR_SUCCESS; PTPROVIDER Provider; + DPRINT("WsTcLoadProvider: %p, %p\n", Catalog, CatalogEntry); /* Lock the catalog */ WsTcLock(); Index: dll/win32/ws2_32_new/src/dcatitem.c =================================================================== --- dll/win32/ws2_32_new/src/dcatitem.c (revision 72643) +++ dll/win32/ws2_32_new/src/dcatitem.c (working copy) @@ -67,6 +67,8 @@ DWORD RegType = REG_BINARY; HKEY EntryKey; DWORD Return; + LPBYTE Buf; + DWORD index; /* Convert to a 00000xxx string */ sprintf(CatalogEntryName, "%0""12""lu", UniqueId); @@ -79,21 +81,34 @@ &EntryKey); /* Get Size of Catalog Entry Structure */ - Return = RegQueryValueExW(EntryKey, - L"PackedCatalogItem", + Return = RegQueryValueEx(EntryKey, + "PackedCatalogItem", 0, NULL, NULL, &RegSize); + if(!(Buf = HeapAlloc(WsSockHeap, HEAP_ZERO_MEMORY, RegSize))) + return ERROR_NOT_ENOUGH_MEMORY; + /* Read the Whole Catalog Entry Structure */ - Return = RegQueryValueExW(EntryKey, - L"PackedCatalogItem", + Return = RegQueryValueEx(EntryKey, + "PackedCatalogItem", 0, &RegType, - (LPBYTE)&CatalogEntry->DllPath, + Buf, &RegSize); + + memcpy(CatalogEntry->DllPath, (LPCSTR)Buf, sizeof(CatalogEntry->DllPath)); + index = sizeof(CatalogEntry->DllPath); + if(index < RegSize) + { + memcpy(&CatalogEntry->ProtocolInfo, &Buf[index], sizeof(WSAPROTOCOL_INFOW)); + index += sizeof(WSAPROTOCOL_INFOW); + } + HeapFree(WsSockHeap, 0, Buf); + /* Done */ RegCloseKey(EntryKey); return Return; Index: dll/win32/ws2_32_new/src/dprovide.c =================================================================== --- dll/win32/ws2_32_new/src/dprovide.c (revision 72643) +++ dll/win32/ws2_32_new/src/dprovide.c (working copy) @@ -10,6 +10,9 @@ #include +#define NDEBUG +#include + /* FUNCTIONS *****************************************************************/ PTPROVIDER @@ -18,6 +21,7 @@ { PTPROVIDER Provider; + DPRINT("WsTpAllocate: WsSockHeap %d\n", WsSockHeap); /* Allocate the object */ Provider = HeapAlloc(WsSockHeap, HEAP_ZERO_MEMORY, sizeof(*Provider)); @@ -35,31 +39,32 @@ IN LPWSAPROTOCOL_INFOW ProtocolInfo) { WORD VersionRequested = MAKEWORD(2,2); - WSPUPCALLTABLE UpcallTable; LPWSPSTARTUP WSPStartupProc; WSPDATA WspData; CHAR ExpandedDllPath[MAX_PATH]; + DWORD ErrorCode; + DPRINT("WsTpInitialize: %p, %p, %p\n", Provider, DllName, ProtocolInfo); /* Clear the tables */ - RtlZeroMemory(&UpcallTable, sizeof(UpcallTable)); + RtlZeroMemory(&Provider->UpcallTable, sizeof(WSPUPCALLTABLE)); RtlZeroMemory(&Provider->Service.lpWSPAccept, sizeof(WSPPROC_TABLE)); /* Set up the Upcall Table */ - UpcallTable.lpWPUCloseEvent = WPUCloseEvent; - UpcallTable.lpWPUCloseSocketHandle = WPUCloseSocketHandle; - UpcallTable.lpWPUCreateEvent = WPUCreateEvent; - UpcallTable.lpWPUCreateSocketHandle = WPUCreateSocketHandle; - UpcallTable.lpWPUFDIsSet = WPUFDIsSet; - UpcallTable.lpWPUGetProviderPath = WPUGetProviderPath; - UpcallTable.lpWPUModifyIFSHandle = WPUModifyIFSHandle; - UpcallTable.lpWPUPostMessage = WPUPostMessage; - UpcallTable.lpWPUQueryBlockingCallback = WPUQueryBlockingCallback; - UpcallTable.lpWPUQuerySocketHandleContext = WPUQuerySocketHandleContext; - UpcallTable.lpWPUQueueApc = WPUQueueApc; - UpcallTable.lpWPUResetEvent = WPUResetEvent; - UpcallTable.lpWPUSetEvent = WPUSetEvent; - UpcallTable.lpWPUOpenCurrentThread = WPUOpenCurrentThread; - UpcallTable.lpWPUCloseThread = WPUCloseThread; + Provider->UpcallTable.lpWPUCloseEvent = WPUCloseEvent; + Provider->UpcallTable.lpWPUCloseSocketHandle = WPUCloseSocketHandle; + Provider->UpcallTable.lpWPUCreateEvent = WPUCreateEvent; + Provider->UpcallTable.lpWPUCreateSocketHandle = WPUCreateSocketHandle; + Provider->UpcallTable.lpWPUFDIsSet = WPUFDIsSet; + Provider->UpcallTable.lpWPUGetProviderPath = WPUGetProviderPath; + Provider->UpcallTable.lpWPUModifyIFSHandle = WPUModifyIFSHandle; + Provider->UpcallTable.lpWPUPostMessage = WPUPostMessage; + Provider->UpcallTable.lpWPUQueryBlockingCallback = WPUQueryBlockingCallback; + Provider->UpcallTable.lpWPUQuerySocketHandleContext = WPUQuerySocketHandleContext; + Provider->UpcallTable.lpWPUQueueApc = WPUQueueApc; + Provider->UpcallTable.lpWPUResetEvent = WPUResetEvent; + Provider->UpcallTable.lpWPUSetEvent = WPUSetEvent; + Provider->UpcallTable.lpWPUOpenCurrentThread = WPUOpenCurrentThread; + Provider->UpcallTable.lpWPUCloseThread = WPUCloseThread; /* Expand the DLL Path */ ExpandEnvironmentStrings(DllName, ExpandedDllPath, MAX_PATH); @@ -67,18 +72,26 @@ /* Load the DLL */ Provider->DllHandle = LoadLibrary(ExpandedDllPath); + if(!Provider->DllHandle) + { + return SOCKET_ERROR; + } /* Get the pointer to WSPStartup */ WSPStartupProc = (LPWSPSTARTUP)GetProcAddress(Provider->DllHandle, "WSPStartup"); + if(!WSPStartupProc) + { + return SOCKET_ERROR; + } /* Call it */ - (*WSPStartupProc)(VersionRequested, + ErrorCode = (*WSPStartupProc)(VersionRequested, &WspData, ProtocolInfo, - UpcallTable, + Provider->UpcallTable, (LPWSPPROC_TABLE)&Provider->Service.lpWSPAccept); /* Return */ - return ERROR_SUCCESS; + return ErrorCode; } DWORD Index: dll/win32/ws2_32_new/src/enumprot.c =================================================================== --- dll/win32/ws2_32_new/src/enumprot.c (revision 72643) +++ dll/win32/ws2_32_new/src/enumprot.c (working copy) @@ -188,7 +188,7 @@ } /* - * @unimplemented + * @implemented */ INT WSAAPI @@ -196,14 +196,46 @@ OUT LPWSAPROTOCOL_INFOA lpProtocolBuffer, IN OUT LPDWORD lpdwBufferLength) { - DPRINT("WSAEnumProtocolsA: %p\n", lpiProtocols); - UNIMPLEMENTED; - SetLastError(WSAEINVAL); - return SOCKET_ERROR; + INT error, i, count; + LPWSAPROTOCOL_INFOW protocolInfoW; + DWORD size; + DPRINT("WSAEnumProtocolsA: %p %p %p\n", lpiProtocols, lpProtocolBuffer, lpdwBufferLength); + if (!lpdwBufferLength) + { + SetLastError(WSAENOBUFS); + return SOCKET_ERROR; + } + count = WSCEnumProtocols(lpiProtocols, NULL, &size, &error); + if (!lpProtocolBuffer || *lpdwBufferLength < (size/sizeof(WSAPROTOCOL_INFOW))*sizeof(WSAPROTOCOL_INFOA)) + { + *lpdwBufferLength = (size/sizeof(WSAPROTOCOL_INFOW))*sizeof(WSAPROTOCOL_INFOA); + SetLastError(WSAENOBUFS); + return SOCKET_ERROR; + } + protocolInfoW = HeapAlloc(WsSockHeap, 0, size); + count = WSCEnumProtocols(lpiProtocols, protocolInfoW, &size, &error); + if (SOCKET_ERROR == count) + { + HeapFree(WsSockHeap, 0, protocolInfoW); + SetLastError(error); + return SOCKET_ERROR; + } + *lpdwBufferLength = 0; + for (i = 0; i < count; i++) + { + /* Copy the data */ + RtlMoveMemory(&lpProtocolBuffer[i], + &protocolInfoW[i], + sizeof(lpProtocolBuffer[0])-sizeof(lpProtocolBuffer[0].szProtocol)); + wcstombs(lpProtocolBuffer[i].szProtocol, protocolInfoW[i].szProtocol, sizeof(lpProtocolBuffer[0].szProtocol)); + *lpdwBufferLength += sizeof(WSAPROTOCOL_INFOA); + } + HeapFree(WsSockHeap, 0, protocolInfoW); + return i; } /* - * @unimplemented + * @implemented */ INT WSAAPI @@ -211,14 +243,20 @@ OUT LPWSAPROTOCOL_INFOW lpProtocolBuffer, IN OUT LPDWORD lpdwBufferLength) { - DPRINT("WSAEnumProtocolsW: %p\n", lpiProtocols); - UNIMPLEMENTED; - SetLastError(WSAEINVAL); - return SOCKET_ERROR; + INT error, count; + DPRINT("WSAEnumProtocolsW: %p %p %p\n", lpiProtocols, lpProtocolBuffer, lpdwBufferLength); + count = WSCEnumProtocols(lpiProtocols, lpProtocolBuffer, lpdwBufferLength, &error); + if (SOCKET_ERROR == count) + { + SetLastError(error); + return SOCKET_ERROR; + } + return count; } + /* - * @unimplemented + * @implemented */ INT WSPAPI @@ -227,13 +265,11 @@ IN OUT LPINT lpProviderDllPathLen, OUT LPINT lpErrno) { - DPRINT("WPUGetProviderPath: %p\n", lpProviderId); - UNIMPLEMENTED; - return 0; + return WSCGetProviderPath(lpProviderId, lpszProviderDllPath, lpProviderDllPathLen, lpErrno); } /* - * @unimplemented + * @implemented */ INT WSAAPI @@ -257,7 +293,7 @@ IN OUT LPINT lpProviderDllPathLen, OUT LPINT lpErrno) { - DPRINT("WSCGetProviderPath: %p\n", lpProviderId); + DPRINT("WSCGetProviderPath: %p %p %p %p\n", lpProviderId, lpszProviderDllPath, lpProviderDllPathLen, lpErrno); UNIMPLEMENTED; SetLastError(WSAEINVAL); return SOCKET_ERROR; Index: dll/win32/ws2_32_new/src/getproto.c =================================================================== --- dll/win32/ws2_32_new/src/getproto.c (revision 72643) +++ dll/win32/ws2_32_new/src/getproto.c (working copy) @@ -19,13 +19,18 @@ WSAAPI GetProtoOpenNetworkDatabase(PCHAR Name) { - CHAR ExpandedPath[MAX_PATH]; - CHAR DatabasePath[MAX_PATH]; + PCHAR ExpandedPath; + PCHAR DatabasePath; INT ErrorCode; HKEY DatabaseKey; DWORD RegType; - DWORD RegSize = sizeof(DatabasePath); + DWORD RegSize = 0; + HANDLE ret; + ExpandedPath = HeapAlloc(WsSockHeap, 0, MAX_PATH); + if (!ExpandedPath) + return INVALID_HANDLE_VALUE; + /* Open the database path key */ ErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Services\\Tcpip\\Parameters", @@ -39,20 +44,43 @@ "DatabasePath", NULL, &RegType, + NULL, + &RegSize); + + DatabasePath = HeapAlloc(WsSockHeap, 0, RegSize); + if (!DatabasePath) + { + HeapFree(WsSockHeap, 0, ExpandedPath); + return INVALID_HANDLE_VALUE; + } + + /* Read the actual path */ + ErrorCode = RegQueryValueEx(DatabaseKey, + "DatabasePath", + NULL, + &RegType, (LPBYTE)DatabasePath, &RegSize); + /* Close the key */ RegCloseKey(DatabaseKey); /* Expand the name */ ExpandEnvironmentStrings(DatabasePath, ExpandedPath, MAX_PATH); + + HeapFree(WsSockHeap, 0, DatabasePath); } else { /* Use defalt path */ GetSystemDirectory(ExpandedPath, MAX_PATH); - strcat(ExpandedPath, "DRIVERS\\ETC\\"); + if (ExpandedPath[strlen(ExpandedPath) - 1] != '\\') + { + /* It isn't, so add it ourselves */ + strncat(ExpandedPath, "\\", MAX_PATH); + } + strncat(ExpandedPath, "DRIVERS\\ETC\\", MAX_PATH); } /* Make sure that the path is backslash-terminated */ @@ -59,14 +87,14 @@ if (ExpandedPath[strlen(ExpandedPath) - 1] != '\\') { /* It isn't, so add it ourselves */ - strcat(ExpandedPath, "\\"); + strncat(ExpandedPath, "\\", MAX_PATH); } /* Add the database name */ - strcat(ExpandedPath, Name); + strncat(ExpandedPath, Name, MAX_PATH); /* Return a handle to the file */ - return CreateFile(ExpandedPath, + ret = CreateFile(ExpandedPath, FILE_READ_ACCESS, 0, NULL, @@ -73,6 +101,9 @@ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + + HeapFree(WsSockHeap, 0, ExpandedPath); + return ret; } PCHAR @@ -118,9 +149,6 @@ &Read, NULL)) return NULL; - /* Null terminate LineBuffer */ - Buffer->LineBuffer[Read] = ANSI_NULL; - /* Find out where the line ends */ p1 = Buffer->LineBuffer; p = strchr(Buffer->LineBuffer, '\n'); @@ -215,7 +243,7 @@ PWSTHREAD Thread; INT ErrorCode; PPROTOENT Protoent; - PVOID GetProtoBuffer; + PVOID GetProtoBuffer; HANDLE DbHandle; DPRINT("getprotobynumber: %lx\n", number); @@ -279,7 +307,7 @@ PWSTHREAD Thread; INT ErrorCode; PPROTOENT Protoent; - PVOID GetProtoBuffer; + PVOID GetProtoBuffer; HANDLE DbHandle; DPRINT("getprotobyname: %s\n", name); Index: dll/win32/ws2_32_new/src/getxbyxx.c =================================================================== --- dll/win32/ws2_32_new/src/getxbyxx.c (revision 72643) +++ dll/win32/ws2_32_new/src/getxbyxx.c (working copy) @@ -108,9 +108,11 @@ { PWSAQUERYSETA WsaQuery = (PWSAQUERYSETA)*Results; INT ErrorCode; + DWORD NewLength = Length; HANDLE RnRHandle; LPBLOB Blob = NULL; - PVOID NewResults; + PVOID NewResults = NULL; + DWORD dwControlFlags = LUP_RETURN_NAME; /* Assume empty return name */ if (NewName) *NewName = NULL; @@ -121,12 +123,15 @@ WsaQuery->lpszServiceInstanceName = Name; WsaQuery->lpServiceClassId = (LPGUID)Type; WsaQuery->dwNameSpace = NS_ALL; - WsaQuery->dwNumberOfProtocols = 2; - WsaQuery->lpafpProtocols = &afp[0]; + WsaQuery->dwNumberOfProtocols = sizeof(afp)/sizeof(afp[0]); + WsaQuery->lpafpProtocols = afp; + if(!IsEqualGUID(Type, &HostnameGuid)) + dwControlFlags |= LUP_RETURN_BLOB; + /* Send the Query Request to find a Service */ ErrorCode = WSALookupServiceBeginA(WsaQuery, - LUP_RETURN_BLOB | LUP_RETURN_NAME, + dwControlFlags, &RnRHandle); if(ErrorCode == ERROR_SUCCESS) @@ -136,7 +141,7 @@ /* Service was found, send the real query */ ErrorCode = WSALookupServiceNextA(RnRHandle, 0, - &Length, + &NewLength, WsaQuery); /* Return the information requested */ @@ -152,7 +157,7 @@ else { /* Check if this was a Hostname lookup */ - if (Type == &HostnameGuid) + if (IsEqualGUID(Type, &HostnameGuid)) { /* Return the name anyways */ if(NewName) *NewName = WsaQuery->lpszServiceInstanceName; @@ -368,7 +373,7 @@ else { /* We failed, so zero it out */ - Hostent = 0; + Hostent = NULL; /* Normalize the error message */ if(GetLastError() == WSASERVICE_NOT_FOUND) @@ -389,19 +394,26 @@ */ INT WSAAPI -gethostname(OUT char FAR * name, - IN int namelen) +gethostname(OUT CHAR FAR * name, + IN INT namelen) { - PCHAR Name; + PCHAR Name = NULL; CHAR ResultsBuffer[RNR_BUFFER_SIZE]; PCHAR Results = ResultsBuffer; DPRINT("gethostname: %p\n", name); + if (!name || namelen < 1) + { + SetLastError(WSAEFAULT); + return SOCKET_ERROR; + } /* Get the Hostname in a String */ - if(getxyDataEnt(&Results, RNR_BUFFER_SIZE, NULL, &HostnameGuid, &Name)) + /* getxyDataEnt does not return blob for HostnameGuid */ + getxyDataEnt(&Results, RNR_BUFFER_SIZE, NULL, &HostnameGuid, &Name); + if(Name) { /* Copy it */ - strcpy((LPSTR)name, Name); + strncpy((LPSTR)name, Name, namelen-1); } /* Check if we received a newly allocated buffer; free it. */ @@ -471,9 +483,6 @@ { /* We failed, so zero it out */ Servent = 0; - - /* Normalize the error message */ - if(GetLastError() == WSATYPE_NOT_FOUND) SetLastError(WSANO_DATA); } /* Check if we received a newly allocated buffer; free it. */ @@ -543,9 +552,6 @@ { /* We failed, so zero it out */ Servent = 0; - - /* Normalize the error message */ - if(GetLastError() == WSATYPE_NOT_FOUND) SetLastError(WSANO_DATA); } /* Check if we received a newly allocated buffer; free it. */ Index: dll/win32/ws2_32_new/src/nscatalo.c =================================================================== --- dll/win32/ws2_32_new/src/nscatalo.c (revision 72643) +++ dll/win32/ws2_32_new/src/nscatalo.c (working copy) @@ -10,9 +10,11 @@ #include +#define NDEBUG +#include + /* DATA **********************************************************************/ -#define NSCATALOG_NAME "NameSpace_Catalog5" #define WsNcLock() EnterCriticalSection((LPCRITICAL_SECTION)&Catalog->Lock); #define WsNcUnlock() LeaveCriticalSection((LPCRITICAL_SECTION)&Catalog->Lock); @@ -39,19 +41,37 @@ LONG ErrorCode; DWORD CreateDisposition; HKEY CatalogKey, NewKey; - //DWORD CatalogEntries = 0; DWORD RegType = REG_DWORD; DWORD RegSize = sizeof(DWORD); DWORD UniqueId = 0; DWORD NewData = 0; + CHAR* CatalogKeyName; /* Initialize the catalog lock and namespace list */ InitializeCriticalSection((LPCRITICAL_SECTION)&Catalog->Lock); InitializeListHead(&Catalog->CatalogList); + /* Read the catalog name */ + ErrorCode = RegQueryValueEx(ParentKey, + "Current_NameSpace_Catalog", + 0, + &RegType, + NULL, + &RegSize); + + CatalogKeyName = HeapAlloc(WsSockHeap, 0, RegSize); + + /* Read the catalog name */ + ErrorCode = RegQueryValueEx(ParentKey, + "Current_NameSpace_Catalog", + 0, + &RegType, + (LPBYTE)CatalogKeyName, + &RegSize); + /* Open the Catalog Key */ ErrorCode = RegOpenKeyEx(ParentKey, - NSCATALOG_NAME, + CatalogKeyName, 0, MAXIMUM_ALLOWED, &CatalogKey); @@ -66,7 +86,7 @@ { /* Create the Catalog Name */ ErrorCode = RegCreateKeyEx(ParentKey, - NSCATALOG_NAME, + CatalogKeyName, 0, NULL, REG_OPTION_NON_VOLATILE, @@ -76,6 +96,10 @@ &CreateDisposition); } + HeapFree(WsSockHeap, 0, CatalogKeyName); + RegType = REG_DWORD; + RegSize = sizeof(DWORD); + /* Fail if that didn't work */ if (ErrorCode != ERROR_SUCCESS) return FALSE; @@ -133,6 +157,7 @@ } else { + RegSize = sizeof(UniqueId); /* Read the serial number */ ErrorCode = RegQueryValueEx(CatalogKey, "Serial_Access_Num", @@ -328,7 +353,7 @@ { PLIST_ENTRY Entry; PNSCATALOG_ENTRY CatalogEntry; - BOOL GoOn = TRUE; + BOOL GoOn; /* Lock the catalog */ WsNcLock(); @@ -335,7 +360,7 @@ /* Loop the entries */ Entry = Catalog->CatalogList.Flink; - while (GoOn && (Entry != &Catalog->CatalogList)) + do { /* Get the entry */ CatalogEntry = CONTAINING_RECORD(Entry, NSCATALOG_ENTRY, CatalogLink); @@ -343,7 +368,7 @@ /* Move to the next one and call the callback */ Entry = Entry->Flink; GoOn = Callback(Context, CatalogEntry); - } + } while (GoOn && (Entry != &Catalog->CatalogList)); /* Release lock */ WsNcUnlock(); Index: dll/win32/ws2_32_new/src/nscatent.c =================================================================== --- dll/win32/ws2_32_new/src/nscatent.c (revision 72643) +++ dll/win32/ws2_32_new/src/nscatent.c (working copy) @@ -64,103 +64,108 @@ IN HKEY ParentKey, IN ULONG UniqueId) { + INT ErrorCode; CHAR CatalogEntryName[13]; HKEY EntryKey; ULONG RegType = REG_SZ; ULONG RegSize = MAX_PATH; + ULONG RegValue; /* Convert to a 00000xxx string */ sprintf(CatalogEntryName, "%0""12""i", (int)UniqueId); /* Open the Entry */ - RegOpenKeyEx(ParentKey, - CatalogEntryName, - 0, - KEY_READ, - &EntryKey); - + ErrorCode = RegOpenKeyEx(ParentKey, + CatalogEntryName, + 0, + KEY_READ, + &EntryKey); + if (ErrorCode != ERROR_SUCCESS) return ErrorCode; /* Read the Library Path */ - RegQueryValueExW(EntryKey, - L"LibraryPath", - 0, - &RegType, - (LPBYTE)&CatalogEntry->DllPath, - &RegSize); - + ErrorCode = RegQueryValueExW(EntryKey, + L"LibraryPath", + 0, + &RegType, + (LPBYTE)&CatalogEntry->DllPath, + &RegSize); + if (ErrorCode != ERROR_SUCCESS) goto out; /* Query Display String Size*/ - RegQueryValueExW(EntryKey, - L"DisplayString", - 0, - NULL, - NULL, - &RegSize); - + ErrorCode = RegQueryValueExW(EntryKey, + L"DisplayString", + 0, + NULL, + NULL, + &RegSize); + if (ErrorCode != ERROR_SUCCESS) goto out; /* Allocate it */ CatalogEntry->ProviderName = (LPWSTR)HeapAlloc(WsSockHeap, 0, RegSize); /* Read it */ - RegQueryValueExW(EntryKey, - L"DisplayString", - 0, - &RegType, - (LPBYTE)CatalogEntry->ProviderName, - &RegSize); - + ErrorCode = RegQueryValueExW(EntryKey, + L"DisplayString", + 0, + &RegType, + (LPBYTE)CatalogEntry->ProviderName, + &RegSize); + if (ErrorCode != ERROR_SUCCESS) goto out; /* Read the Provider Id */ RegType = REG_BINARY; RegSize = sizeof(GUID); - RegQueryValueEx(EntryKey, - "ProviderId", - 0, - &RegType, - (LPBYTE)&CatalogEntry->ProviderId, - &RegSize); - + ErrorCode = RegQueryValueEx(EntryKey, + "ProviderId", + 0, + &RegType, + (LPBYTE)&CatalogEntry->ProviderId, + &RegSize); + if (ErrorCode != ERROR_SUCCESS) goto out; /* Read the Address Family */ RegType = REG_DWORD; RegSize = sizeof(DWORD); - RegQueryValueEx(EntryKey, - "AddressFamily", - 0, - &RegType, - (LPBYTE)&CatalogEntry->AddressFamily, - &RegSize); - + ErrorCode = RegQueryValueEx(EntryKey, + "AddressFamily", + 0, + &RegType, + (LPBYTE)&CatalogEntry->AddressFamily, + &RegSize); + if (ErrorCode != ERROR_SUCCESS) goto out; /* Read the Namespace Id */ - RegQueryValueEx(EntryKey, - "SupportedNamespace", - 0, - &RegType, - (LPBYTE)&CatalogEntry->NamespaceId, - &RegSize); - + ErrorCode = RegQueryValueEx(EntryKey, + "SupportedNamespace", + 0, + &RegType, + (LPBYTE)&CatalogEntry->NamespaceId, + &RegSize); + if (ErrorCode != ERROR_SUCCESS) goto out; /* Read the Enabled Flag */ - RegQueryValueEx(EntryKey, - "Enabled", - 0, - &RegType, - (LPBYTE)&CatalogEntry->Enabled, - &RegSize); + ErrorCode = RegQueryValueEx(EntryKey, + "Enabled", + 0, + &RegType, + (LPBYTE)&RegValue, + &RegSize); + if (ErrorCode != ERROR_SUCCESS) goto out; + CatalogEntry->Enabled = RegValue != 0; /* Read the Version */ - RegQueryValueEx(EntryKey, - "Version", - 0, - &RegType, - (LPBYTE)&CatalogEntry->Version, - &RegSize); - + ErrorCode = RegQueryValueEx(EntryKey, + "Version", + 0, + &RegType, + (LPBYTE)&CatalogEntry->Version, + &RegSize); + if (ErrorCode != ERROR_SUCCESS) goto out; /* Read the Support Service Class Info Flag */ - RegQueryValueEx(EntryKey, - "Version", - 0, - &RegType, - (LPBYTE)&CatalogEntry->StoresServiceClassInfo, - &RegSize); - + ErrorCode = RegQueryValueEx(EntryKey, + "StoresServiceClassInfo", + 0, + &RegType, + (LPBYTE)&RegValue, + &RegSize); + CatalogEntry->StoresServiceClassInfo = RegValue != 0; +out: /* Done */ RegCloseKey(EntryKey); - return ERROR_SUCCESS; + return ErrorCode; } VOID Index: dll/win32/ws2_32_new/src/nsquery.c =================================================================== --- dll/win32/ws2_32_new/src/nsquery.c (revision 72643) +++ dll/win32/ws2_32_new/src/nsquery.c (working copy) @@ -360,7 +360,7 @@ { WSASERVICECLASSINFOW ClassInfo; PNSQUERY_PROVIDER Provider; - LPWSASERVICECLASSINFOW pClassInfo = NULL; + LPWSASERVICECLASSINFOW pClassInfo = &ClassInfo; PNSQUERY_PROVIDER NextProvider; PLIST_ENTRY Entry; INT ErrorCode; @@ -438,7 +438,7 @@ /* Get the class information */ ClassInfo.lpServiceClassId = Restrictions->lpServiceClassId; - ErrorCode = WsNcGetServiceClassInfo(Catalog, &ClassInfoSize, &ClassInfo); + ErrorCode = WsNcGetServiceClassInfo(Catalog, &ClassInfoSize, pClassInfo); /* Check if more buffer space is needed */ if ((ErrorCode == SOCKET_ERROR) && (GetLastError() == WSAEFAULT)) Index: dll/win32/ws2_32_new/src/rnr.c =================================================================== --- dll/win32/ws2_32_new/src/rnr.c (revision 72643) +++ dll/win32/ws2_32_new/src/rnr.c (working copy) @@ -214,7 +214,7 @@ } /* Check for a valid handle, then validate and reference it */ - if (!(Query) || !(WsNqValidateAndReference(Query))) + if (IsBadReadPtr(Query, sizeof(*Query)) || !WsNqValidateAndReference(Query)) { /* Fail */ SetLastError(WSA_INVALID_HANDLE); @@ -249,7 +249,8 @@ DPRINT("WSALookupServiceBeginA: %p\n", lpqsRestrictions); /* Verifiy pointer */ - if (IsBadReadPtr(lpqsRestrictions, sizeof(*lpqsRestrictions))) + if (IsBadReadPtr(lpqsRestrictions, sizeof(*lpqsRestrictions)) || + IsBadReadPtr(lpqsRestrictions->lpServiceClassId, sizeof(*lpqsRestrictions->lpServiceClassId))) { /* Invalid */ SetLastError(WSAEFAULT); @@ -334,7 +335,8 @@ /* Verify pointers */ if (IsBadWritePtr(lphLookup, sizeof(*lphLookup)) || - IsBadReadPtr(lpqsRestrictions, sizeof(*lpqsRestrictions))) + IsBadReadPtr(lpqsRestrictions, sizeof(*lpqsRestrictions)) || + IsBadReadPtr(lpqsRestrictions->lpServiceClassId, sizeof(*lpqsRestrictions->lpServiceClassId))) { /* They are invalid; fail */ SetLastError(WSAEFAULT); @@ -401,8 +403,16 @@ return SOCKET_ERROR; } + /* Verify pointer */ + if (IsBadWritePtr(lpqsResults, sizeof(*lpqsResults))) + { + /* It is invalid; fail */ + SetLastError(WSAEFAULT); + return SOCKET_ERROR; + } + /* Check for a valid handle, then validate and reference it */ - if (!(Query) || !(WsNqValidateAndReference(Query))) + if (IsBadReadPtr(Query, sizeof(*Query)) || !WsNqValidateAndReference(Query)) { /* Fail */ SetLastError(WSA_INVALID_HANDLE); Index: dll/win32/ws2_32_new/src/send.c =================================================================== --- dll/win32/ws2_32_new/src/send.c (revision 72643) +++ dll/win32/ws2_32_new/src/send.c (working copy) @@ -31,7 +31,7 @@ LPWSATHREADID ThreadId; WSABUF Buffers; DWORD BytesSent; - DPRINT("send: %lx, %lx, %lx, %p\n", s, flags, len, buf); + DPRINT("sendto: %lx, %lx, %lx, %p\n", s, flags, len, buf); /* Check for WSAStartup */ if ((ErrorCode = WsQuickPrologTid(&ThreadId)) == ERROR_SUCCESS) Index: dll/win32/ws2_32_new/src/sockctrl.c =================================================================== --- dll/win32/ws2_32_new/src/sockctrl.c (revision 72643) +++ dll/win32/ws2_32_new/src/sockctrl.c (working copy) @@ -441,6 +441,11 @@ return Status; } + if (!optval && optlen > 0) + { + SetLastError(WSAEFAULT); + return SOCKET_ERROR; + } /* Get the Socket Context */ if ((Socket = WsSockGetSocket(s))) Index: dll/win32/ws2_32_new/src/socklife.c =================================================================== --- dll/win32/ws2_32_new/src/socklife.c (revision 72643) +++ dll/win32/ws2_32_new/src/socklife.c (working copy) @@ -543,10 +543,10 @@ 0 : SO_SYNCHRONOUS_NONALERT; /* Call the Provider to create the Socket */ - Status = CatalogEntry->Provider->Service.lpWSPSocket(af, - type, - protocol, - ProtocolInfo, + Status = CatalogEntry->Provider->Service.lpWSPSocket(CatalogEntry->ProtocolInfo.iAddressFamily, + CatalogEntry->ProtocolInfo.iSocketType, + CatalogEntry->ProtocolInfo.iProtocol, + &CatalogEntry->ProtocolInfo, g, dwFlags, &ErrorCode); @@ -567,6 +567,20 @@ } /* Check if we got a valid socket */ + if (Status == WSAEINVAL) + { + Status = INVALID_SOCKET; + ErrorCode = WSAEINVAL; + } + + /* Check if we got a valid socket */ + if (Status == WSAEINVAL) + { + Status = INVALID_SOCKET; + ErrorCode = WSAEINVAL; + } + + /* Check if we got a valid socket */ if (Status != INVALID_SOCKET) { /* Add an API reference and return */ Index: dll/win32/ws2_32_new/src/startup.c =================================================================== --- dll/win32/ws2_32_new/src/startup.c (revision 72643) +++ dll/win32/ws2_32_new/src/startup.c (working copy) @@ -85,6 +85,9 @@ /* Return success */ ErrorCode = ERROR_SUCCESS; + + /* Clear last error */ + SetLastError(ERROR_SUCCESS); } else { @@ -111,7 +114,7 @@ WORD VersionReturned = 0; DWORD ErrorCode = ERROR_SUCCESS; PWSPROCESS CurrentProcess; - DPRINT("WSAStartup: %wx\n", wVersionRequested); + DPRINT("WSAStartup: %wx %d.%d\n", wVersionRequested, LOBYTE(wVersionRequested), HIBYTE(wVersionRequested)); /* Make sure that we went through DLL Init */ if (!WsDllHandle) return WSASYSNOTREADY; @@ -123,14 +126,15 @@ /* We don't support this unknown version */ ErrorCode = WSAVERNOTSUPPORTED; + VersionReturned = MAKEWORD(2, 2); break; case 1: /* We support only 1.0 and 1.1 */ - if (HIBYTE(wVersionRequested) == 0) + if (HIBYTE(wVersionRequested) <= 1) { /* Caller wants 1.0, return it */ - VersionReturned = wVersionRequested; + VersionReturned = MAKEWORD(1, HIBYTE(wVersionRequested)); } else { @@ -182,6 +186,13 @@ lpWSAData->iMaxSockets = 0; lpWSAData->iMaxUdpDg = 0; } + + /* Requested invalid version (0) */ + if (ErrorCode != ERROR_SUCCESS) + { + SetLastError(WSANOTINITIALISED); + return ErrorCode; + } /* Enter the startup synchronization lock */ WsStartupLock(); @@ -223,7 +234,14 @@ /* Increase the reference count */ InterlockedIncrement(&CurrentProcess->RefCount); + + /* Clear last error */ + SetLastError(ERROR_SUCCESS); } + else + { + SetLastError(WSANOTINITIALISED); + } /* Leave the startup lock */ WsStartupUnlock(); Index: sdk/lib/3rdparty/adns/src/setup.c =================================================================== --- sdk/lib/3rdparty/adns/src/setup.c (revision 72643) +++ sdk/lib/3rdparty/adns/src/setup.c (working copy) @@ -572,9 +572,8 @@ #define SECURE_PATH_LEN (MAX_PATH - 64) char PathBuf[MAX_PATH]; struct in_addr addr; - #define ADNS_PFIXED_INFO_BLEN (2048) - PFIXED_INFO network_info = (PFIXED_INFO)_alloca(ADNS_PFIXED_INFO_BLEN); - ULONG network_info_blen = ADNS_PFIXED_INFO_BLEN; + PFIXED_INFO network_info; + ULONG network_info_blen = 0; DWORD network_info_result; PIP_ADDR_STRING pip; const char *network_err_str = ""; @@ -601,6 +600,8 @@ GetWindowsDirectory(PathBuf, SECURE_PATH_LEN); strcat(PathBuf,"\\System32\\Drivers\\etc\\resolv-adns.conf"); readconfig(ads,PathBuf,0); + network_info_result = GetNetworkParams(NULL, &network_info_blen); + network_info = (PFIXED_INFO)_alloca((size_t)network_info_blen); network_info_result = GetNetworkParams(network_info, &network_info_blen); if (network_info_result != ERROR_SUCCESS){ switch(network_info_result) { @@ -616,6 +617,8 @@ addr.s_addr = inet_addr(pip->IpAddress.String); if ((addr.s_addr != INADDR_ANY) && (addr.s_addr != INADDR_NONE)) addserver(ads, addr); + if (network_info->DomainName) + ccf_search(ads, "LOCALDOMAIN", -1, network_info->DomainName); } } #else Index: modules/rostests/winetests/ws2_32/sock.c =================================================================== --- modules/rostests/winetests/ws2_32/sock.c (revision 72643) +++ modules/rostests/winetests/ws2_32/sock.c (working copy) @@ -9264,6 +9264,7 @@ CloseHandle(previous_port); } +#ifndef __REACTOS__ /* WSHIoctl is not supported by wshtcpip.dll and crashes. Test should be fixed also! */ static void test_address_list_query(void) { SOCKET_ADDRESS_LIST *address_list; @@ -9326,6 +9327,7 @@ HeapFree(GetProcessHeap(), 0, address_list); closesocket(s); } +#endif static DWORD WINAPI inet_ntoa_thread_proc(void *param) { @@ -9784,7 +9786,9 @@ test_WSAAsyncGetServByName(); test_completion_port(); +#ifndef __REACTOS__ /* WSHIoctl is not supported by wshtcpip.dll and crashes. Test should be fixed also! */ test_address_list_query(); +#endif /* this is an io heavy test, do it at the end so the kernel doesn't start dropping packets */ test_send();