#pragma option w32c

#define _WINERROR_ TRUE
#define _WINBASE_ TRUE
#define _WINSOCK_ TRUE
#define _ADVAPI_ TRUE
#define _WINNETWK_ TRUE

#define SIZEBUF 16384

#includepath "c:\c--\open_lib"

#include "windows.h--"
#include "write.h--"

void doEnum(dword pnr,domain);
byte bufmes[256];
byte bufmes2[256];
byte copyright="\n\t(C) Mishel Sheker. KNPP. "__DATE__"\n";
dword handl=-1;

void WriteInfo(dword buf)
{
dword butesRW;
	$push EBX
	IF(handl!=-1)WriteFile(handl,buf,lstrlen(buf),#butesRW,0);
	WriteStr(buf);
	$pop EBX
}

void doEnum(dword pnr,domain)
{
DWORD ui;
HANDLE hEnum;
DWORD count,bufsize;
dword sadr;
dword buf;

	WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,0,pnr,#hEnum);
	if(!EAX){
		for(;;){
			count=-1L;
			bufsize=sizeof(buf);
			bufsize=SIZEBUF;
			buf=GlobalAlloc(GPTR,bufsize);
			WNetEnumResource(hEnum,#count,buf,#bufsize);
			IF(EAX!=NO_ERROR)break;
			for ( ui = 0; ui < count; ui++ ){
				EBX=sizeof(NETRESOURCE)*ui+buf;
				IF(EBX.NETRESOURCE.dwDisplayType==RESOURCEDISPLAYTYPE_SERVER){
					$push EBX
					sadr=GetAddr(EBX.NETRESOURCE.lpRemoteName+2);
					$pop EBX
					IF((EAX!=INADDR_ANY)&&(EAX!=INADDR_NONE)){
						wsprintf(#bufmes2,"%s%s%",domain,EBX.NETRESOURCE.lpRemoteName);
						wsprintf(#bufmes,"%-20s%d.%d.%d.%d ",#bufmes2,
						SSBYTE[#sadr],
						SSBYTE[#sadr+1],
						SSBYTE[#sadr+2],
						SSBYTE[#sadr+3]);
						WriteInfo(#bufmes);
						view(EBX.NETRESOURCE.lpComment);
						WriteInfo("\n");
					}
				}
				ELSE IF(EBX.NETRESOURCE.dwDisplayType==RESOURCEDISPLAYTYPE_DOMAIN){
					domain=EBX.NETRESOURCE.lpRemoteName;
				}
				IF(EBX.NETRESOURCE.dwUsage & RESOURCEUSAGE_CONTAINER)&&
				(EBX.NETRESOURCE.dwDisplayType!=RESOURCEDISPLAYTYPE_SERVER)doEnum(EBX,domain);
			}
		}
		GlobalFree(buf);
		WNetCloseEnum(hEnum);
	}
}

void view(dword buf)
{
	IF(buf!=0){
		CharToOem(buf,buf);
		WriteInfo("\t");
		WriteInfo(buf);
	}
}

void main()
{
WSADATA WSAData;
	IF(WSAStartup(0x101,#WSAData)==0){
		handl=CreateFileA("ip_list.txt",GENERIC_WRITE,
				FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
		WriteInfo("\n\tThe full list TCP/IP of address of all users:\n");
		doEnum(NULL,"");
		WriteInfo(#copyright);
		IF(handl!=-1)CloseHandle(handl);
		WSACleanup();
	}
}

DWORD GetAddr(dword szHost)
{
/* check that we have a string */
	EAX=INADDR_ANY;
	IF(szHost){
		gethostbyname(szHost);
		IF(EAX){  /* success */
			EAX=DSDWORD[EAX+12];
			EAX=DSDWORD[EAX];
			EAX=DSDWORD[EAX];
		}
		ELSE EAX=INADDR_ANY;   /* failure */
	}
}
