#include <stdio.h>
#include <windows.h>
#include <lm.h>
#pragma hdrstop



int main( void );
void print_si( SERVER_INFO_101 *pSI );
void build_typestr( DWORD type, char *str );



int main( void )
{
	//*********************************	change these:
	wchar_t *pPdcName = L"\\\\BABYA";	// server to run on -- I use my PDC
	wchar_t *pDomain = L"FOO";			// domain to query
	DWORD wantedbytes = 256;			// desired buffer size per call
	DWORD servertype = SV_TYPE_ALL;		// server types to ask for
	//*********************************

	BYTE *pbuf; // will be filled in by NSE()
	DWORD entriesread, totalentries; // this call, total
	DWORD resume; // resume handle -- leave this alone
	NET_API_STATUS rc; // result code
	SERVER_INFO_101 *pSI;
	DWORD i; // loop index for returned SERVER_INFO_101 array

	do
	{
		pbuf = NULL;
		
		rc = NetServerEnum( pPdcName, 101, &pbuf, wantedbytes, &entriesread,
			&totalentries, servertype, pDomain, &resume );

		if ( ( rc == NERR_Success || rc == ERROR_MORE_DATA ) && pbuf != NULL )
		{
			pSI = (SERVER_INFO_101 *) pbuf;
			for ( i = 0; i < entriesread; i ++ )
				print_si( &pSI[i] );
		}

		if ( pbuf != NULL )
			NetApiBufferFree( pbuf );

	} while ( rc == ERROR_MORE_DATA );

	if ( rc != NERR_Success )
		printf( "Oops! Error %lu encountered!\n", rc );

	return 0;
}



void print_si( SERVER_INFO_101 *pSI )
{
	char typestr[256];

	build_typestr( pSI->sv101_type, typestr );

	printf( "%-16.16S  %2lu.%02lu  %-.54s\n", pSI->sv101_name,
		pSI->sv101_version_major & MAJOR_VERSION_MASK,
		pSI->sv101_version_minor, typestr );
}



void build_typestr( DWORD type, char *str )
{
	char *t;
	struct _type_t {
		DWORD flag;
		char *str;
	} *curtype, types[] = {
		{ SV_TYPE_WORKSTATION, "WS" },
		{ SV_TYPE_SERVER, "SRV" },
		{ SV_TYPE_SQLSERVER, "SQL" },
		{ SV_TYPE_DOMAIN_CTRL, "PDC" },
		{ SV_TYPE_DOMAIN_BAKCTRL, "BDC" },
		{ SV_TYPE_AFP, "AFP" },
		{ SV_TYPE_NOVELL, "NW" },
		{ SV_TYPE_DOMAIN_MEMBER, "MEMB" },
		{ SV_TYPE_LOCAL_LIST_ONLY, "LOCAL" },
		{ SV_TYPE_XENIX_SERVER, "XENIX" },
		{ SV_TYPE_NT, "NT" },
		{ SV_TYPE_WFW, "WFW" },
		{ SV_TYPE_SERVER_NT, "NTS" },
		{ SV_TYPE_POTENTIAL_BROWSER, "PBR" },
		{ SV_TYPE_BACKUP_BROWSER, "BBR" },
		{ SV_TYPE_MASTER_BROWSER, "MBR" },
		{ SV_TYPE_DOMAIN_MASTER, "DMBR" },
		{ SV_TYPE_DOMAIN_ENUM, "PDOM" },
		{ SV_TYPE_WINDOWS, "WIN" },
		{ SV_TYPE_ALL, "Sentinel" }
	};

	t = str;
	for ( curtype = &types[0]; curtype->flag != SV_TYPE_ALL; curtype ++ )
	{
		if ( ( type & curtype->flag ) == curtype->flag ) // match?
		{
			if ( t != str )
				*( t ++ ) = ' ';
			strcpy( t, curtype->str );
			t += strlen( t );
		}
	}
}

