From 9ce1fe0ddb9ad6fea3ea82d3be3b8af4c2e647ee Mon Sep 17 00:00:00 2001 From: Art Yerkes Date: Thu, 15 Jul 2004 02:03:07 +0000 Subject: [PATCH] Small changes to debug output in catalog, handle, upcall. ns.c -- getservbyport contributed by Cameron Palmer. svn path=/trunk/; revision=10121 --- reactos/lib/ws2_32/include/ws2_32.h | 9 ++ reactos/lib/ws2_32/misc/catalog.c | 4 +- reactos/lib/ws2_32/misc/dllmain.c | 1 + reactos/lib/ws2_32/misc/handle.c | 8 +- reactos/lib/ws2_32/misc/ns.c | 161 +++++++++++++++++++++++++++- reactos/lib/ws2_32/misc/upcall.c | 2 +- 6 files changed, 176 insertions(+), 9 deletions(-) diff --git a/reactos/lib/ws2_32/include/ws2_32.h b/reactos/lib/ws2_32/include/ws2_32.h index 3abe94b777f..820171d9ed6 100644 --- a/reactos/lib/ws2_32/include/ws2_32.h +++ b/reactos/lib/ws2_32/include/ws2_32.h @@ -37,11 +37,20 @@ typedef struct _WINSOCK_GETSERVBYNAME_CACHE { CHAR Data[1]; } WINSOCK_GETSERVBYNAME_CACHE, *PWINSOCK_GETSERVBYNAME_CACHE; +typedef struct _WINSOCK_GETSERVBYPORT_CACHE { + UINT Size; + SERVENT ServerEntry; + PCHAR Aliases[WS2_INTERNAL_MAX_ALIAS]; + CHAR Data[1]; +} WINSOCK_GETSERVBYPORT_CACHE, *PWINSOCK_GETSERVBYPORT_CACHE; + typedef struct _WINSOCK_THREAD_BLOCK { INT LastErrorValue; /* Error value from last function that failed */ CHAR Intoa[16]; /* Buffer for inet_ntoa() */ PWINSOCK_GETSERVBYNAME_CACHE Getservbyname; /* Buffer used by getservbyname */ + PWINSOCK_GETSERVBYPORT_CACHE + Getservbyport; /* Buffer used by getservbyname */ } WINSOCK_THREAD_BLOCK, *PWINSOCK_THREAD_BLOCK; diff --git a/reactos/lib/ws2_32/misc/catalog.c b/reactos/lib/ws2_32/misc/catalog.c index e94e8d29290..6ad9caf230e 100644 --- a/reactos/lib/ws2_32/misc/catalog.c +++ b/reactos/lib/ws2_32/misc/catalog.c @@ -188,7 +188,7 @@ INT LoadProvider( { INT Status; - WS_DbgPrint(MAX_TRACE, ("Loading provider at (0x%X) Name (%wZ).\n", + WS_DbgPrint(MID_TRACE, ("Loading provider at (0x%X) Name (%wZ).\n", Provider, &Provider->LibraryName)); if (NULL == Provider->hModule) @@ -221,7 +221,7 @@ INT LoadProvider( } else Status = NO_ERROR; - WS_DbgPrint(MAX_TRACE, ("Status (%d).\n", Status)); + WS_DbgPrint(MID_TRACE, ("Status (%d).\n", Status)); return Status; } diff --git a/reactos/lib/ws2_32/misc/dllmain.c b/reactos/lib/ws2_32/misc/dllmain.c index 0b79ab9d019..46a907f5a75 100644 --- a/reactos/lib/ws2_32/misc/dllmain.c +++ b/reactos/lib/ws2_32/misc/dllmain.c @@ -668,6 +668,7 @@ DllMain(HANDLE hInstDll, p->LastErrorValue = NO_ERROR; p->Getservbyname = NULL; + p->Getservbyport = NULL; NtCurrentTeb()->WinSockData = p; break; diff --git a/reactos/lib/ws2_32/misc/handle.c b/reactos/lib/ws2_32/misc/handle.c index 7e6dcc52819..4590574083b 100644 --- a/reactos/lib/ws2_32/misc/handle.c +++ b/reactos/lib/ws2_32/misc/handle.c @@ -161,6 +161,8 @@ CreateProviderHandleTable( NewBlock = (PPROVIDER_HANDLE_BLOCK)HeapAlloc( GlobalHeap, 0, sizeof(PROVIDER_HANDLE_BLOCK)); + WS_DbgPrint(MID_TRACE,("using table entry %x\n", NewBlock)); + if (!NewBlock) return (HANDLE)0; @@ -209,12 +211,14 @@ ReferenceProviderByHandle( { PPROVIDER_HANDLE ProviderHandle; - WS_DbgPrint(MAX_TRACE, ("Handle (0x%X) Provider (0x%X).\n", Handle, Provider)); + WS_DbgPrint(MID_TRACE, ("Handle (0x%X) Provider (0x%X).\n", Handle, Provider)); EnterCriticalSection(&ProviderHandleTableLock); ProviderHandle = GetProviderByHandle(ProviderHandleTable, Handle); + WS_DbgPrint(MID_TRACE, ("ProviderHanddle is %x\n", ProviderHandle)); + LeaveCriticalSection(&ProviderHandleTableLock); if (ProviderHandle) { @@ -256,6 +260,8 @@ InitProviderHandleTable(VOID) if (!ProviderHandleTable) return FALSE; + WS_DbgPrint(MID_TRACE,("Called\n")); + ZeroMemory(ProviderHandleTable, sizeof(PROVIDER_HANDLE_BLOCK)); InitializeListHead(&ProviderHandleTable->Entry); diff --git a/reactos/lib/ws2_32/misc/ns.c b/reactos/lib/ws2_32/misc/ns.c index 3121a3cda20..177039ccb4b 100644 --- a/reactos/lib/ws2_32/misc/ns.c +++ b/reactos/lib/ws2_32/misc/ns.c @@ -7,7 +7,6 @@ * REVISIONS: * CSH 01/09-2000 Created */ -#define __NO_CTYPE_INLINES #include #include @@ -482,6 +481,7 @@ static BOOL DecodeServEntFromString( IN PCHAR ServiceString, LPSERVENT EXPORT getservbyname( + IN CONST CHAR FAR* name, IN CONST CHAR FAR* proto) { @@ -568,8 +568,8 @@ getservbyname( WS2_INTERNAL_MAX_ALIAS ) && !strcmp( ServiceName, name ) && (proto ? !strcmp( ProtocolStr, proto ) : TRUE) ) { - WS_DbgPrint(MAX_TRACE,("Found the service entry.\n")); + WS_DbgPrint(MAX_TRACE,("Found the service entry.\n")); Found = TRUE; SizeNeeded = sizeof(WINSOCK_GETSERVBYNAME_CACHE) + (NextLine - ThisLine); @@ -640,7 +640,7 @@ getservbyname( /* - * @unimplemented + * @implemented */ LPSERVENT EXPORT @@ -648,9 +648,160 @@ getservbyport( IN INT port, IN CONST CHAR FAR* proto) { - UNIMPLEMENTED + BOOL Found = FALSE; + HANDLE ServicesFile; + CHAR ServiceDBData[BUFSIZ] = { 0 }; + PCHAR SystemDirectory = ServiceDBData; /* Reuse this stack space */ + PCHAR ServicesFileLocation = "\\drivers\\etc\\services"; + PCHAR ThisLine = 0, NextLine = 0, ServiceName = 0, PortNumberStr = 0, + ProtocolStr = 0, Comment = 0; + PCHAR Aliases[WS2_INTERNAL_MAX_ALIAS] = { 0 }; + UINT i,SizeNeeded = 0, + SystemDirSize = sizeof(ServiceDBData) - 1; + DWORD ReadSize = 0, ValidData = 0; + PWINSOCK_THREAD_BLOCK p = NtCurrentTeb()->WinSockData; + + if( !p ) { + WSASetLastError( WSANOTINITIALISED ); + return NULL; + } + + if ( !port ) { + WSASetLastError( WSANO_RECOVERY ); + return NULL; + } + + if( !GetSystemDirectoryA( SystemDirectory, SystemDirSize ) ) { + WSASetLastError( WSANO_RECOVERY ); + WS_DbgPrint(MIN_TRACE, ("Could not get windows system directory.\n")); + return NULL; /* Can't get system directory */ + } + + strncat( SystemDirectory, ServicesFileLocation, SystemDirSize ); + + ServicesFile = CreateFileA( SystemDirectory, + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | + FILE_FLAG_SEQUENTIAL_SCAN, + NULL ); + + if( ServicesFile == INVALID_HANDLE_VALUE ) { + WSASetLastError( WSANO_RECOVERY ); + return NULL; + } + + /* Scan the services file ... + * + * We will read up to BUFSIZ bytes per pass, until the buffer does not + * contain a full line, then we will try to read more. + * + * We fall from the loop if the buffer does not have a line terminator. + */ + + /* Initial Read */ + while( !Found && + ReadFile( ServicesFile, ServiceDBData + ValidData, + sizeof( ServiceDBData ) - ValidData, + &ReadSize, NULL ) ) { + ValidData += ReadSize; + ReadSize = 0; + NextLine = ThisLine = ServiceDBData; + + /* Find the beginning of the next line */ + while( NextLine < ServiceDBData + ValidData && + *NextLine != '\r' && *NextLine != '\n' ) NextLine++; + + /* Zero and skip, so we can treat what we have as a string */ + if( NextLine >= ServiceDBData + ValidData ) + break; + + *NextLine = 0; NextLine++; + + Comment = strchr( ThisLine, '#' ); + if( Comment ) *Comment = 0; /* Terminate at comment start */ + + if( DecodeServEntFromString( ThisLine, + &ServiceName, + &PortNumberStr, + &ProtocolStr, + Aliases, + WS2_INTERNAL_MAX_ALIAS ) && + (htons(atoi( PortNumberStr )) == port ) && + (proto ? !strcmp( ProtocolStr, proto ) : TRUE) ) { + + WS_DbgPrint(MAX_TRACE,("Found the port entry.\n")); + + Found = TRUE; + SizeNeeded = sizeof(WINSOCK_GETSERVBYPORT_CACHE) + + (NextLine - ThisLine); + break; + } + + /* Get rid of everything we read so far */ + while( NextLine <= ServiceDBData + ValidData && + isspace( *NextLine ) ) NextLine++; + + WS_DbgPrint(MAX_TRACE,("About to move %d chars\n", + ServiceDBData + ValidData - NextLine)); + + memmove( ServiceDBData, NextLine, + ServiceDBData + ValidData - NextLine ); + ValidData -= NextLine - ServiceDBData; + WS_DbgPrint(MAX_TRACE,("Valid bytes: %d\n", ValidData)); + } + + /* This we'll do no matter what */ + CloseHandle( ServicesFile ); + + if( !Found ) { + WS_DbgPrint(MAX_TRACE,("Not found\n")); + WSASetLastError( WSANO_DATA ); + return NULL; + } + + if( !p->Getservbyport || p->Getservbyport->Size < SizeNeeded ) { + /* Free previous getservbyport buffer, allocate bigger */ + if( p->Getservbyport ) + HeapFree(GlobalHeap, 0, p->Getservbyport); + p->Getservbyport = HeapAlloc(GlobalHeap, 0, SizeNeeded); + if( !p->Getservbyport ) { + WS_DbgPrint(MIN_TRACE,("Couldn't allocate %d bytes\n", + SizeNeeded)); + WSASetLastError( WSATRY_AGAIN ); + return NULL; + } + p->Getservbyport->Size = SizeNeeded; + } + /* Copy the data */ + memmove( p->Getservbyport->Data, + ThisLine, + NextLine - ThisLine ); + + ADJ_PTR(PortNumberStr,ThisLine,p->Getservbyport->Data); + ADJ_PTR(ProtocolStr,ThisLine,p->Getservbyport->Data); + WS_DbgPrint(MAX_TRACE, + ("Port Number: %s, Protocol: %s\n", PortNumberStr, ProtocolStr)); + + for( i = 0; Aliases[i]; i++ ) { + ADJ_PTR(Aliases[i],ThisLine,p->Getservbyport->Data); + WS_DbgPrint(MAX_TRACE,("Aliases %d: %s\n", i, Aliases[i])); + } + + memcpy(p->Getservbyport,Aliases,sizeof(Aliases)); + + /* Create the struct proper */ + p->Getservbyport->ServerEntry.s_name = ServiceName; + p->Getservbyport->ServerEntry.s_aliases = p->Getservbyport->Aliases; + p->Getservbyport->ServerEntry.s_port = port; + p->Getservbyport->ServerEntry.s_proto = ProtocolStr; + + WS_DbgPrint(MID_TRACE,("s_name: %s\n", ServiceName)); + + return &p->Getservbyport->ServerEntry; - return (LPSERVENT)NULL; } diff --git a/reactos/lib/ws2_32/misc/upcall.c b/reactos/lib/ws2_32/misc/upcall.c index 4264206930a..cfcf804dac3 100644 --- a/reactos/lib/ws2_32/misc/upcall.c +++ b/reactos/lib/ws2_32/misc/upcall.c @@ -131,7 +131,7 @@ WPUModifyIFSHandle( PCATALOG_ENTRY Provider; SOCKET Socket; - WS_DbgPrint(MAX_TRACE, ("dwCatalogEntryId (%d) ProposedHandle (0x%X).\n", + WS_DbgPrint(MID_TRACE, ("dwCatalogEntryId (%d) ProposedHandle (0x%X).\n", dwCatalogEntryId, ProposedHandle)); Provider = LocateProviderById(dwCatalogEntryId);