PDA

View Full Version : ONLINEDATATA Usage example


Mouton
09-16-2003, 01:53 PM
Here's what i use to access the ONLINEDATA for sitewho...

The format i printf is the format needed for ioBMonitor btw, just in case u'd want to make a compatible command! ;)


// SHMEM
#include <ServerLimits.h>
#include <VFS.h>
#include <Userfile.h>
#include <WinMessages.h>
#include <DataCopy.h>
#include "ioMessages.h"

LRESULT lResult;
HWND Target;
HANDLE (WINAPI *ShMem_Allocate)(LPVOID, ULONG, DWORD);
LPVOID (WINAPI *ShMem_Lock)(HANDLE, DWORD);
BOOL (WINAPI *ShMem_Unlock)(LPVOID);
BOOL (WINAPI *ShMem_Free)(HANDLE, DWORD);
LPDC_MESSAGE lpMessage;
HMODULE hShell32;
HANDLE hSharedMemory, hEvent;
LPVOID lpContext, hMemory;
DWORD dwProcessId, dwAllocationSize;
DWORD dwReturn;
HWND hIoFTPD;

int initSharedMem(DWORD dwAllocationAsked=4096);
int closeSharedMem();

int initSharedMem(DWORD dwAllocationAsked)
{
hEvent = INVALID_HANDLE_VALUE;
lpMessage = NULL;
hSharedMemory = NULL;
hShell32 = LoadLibrary("shell32.dll");
hIoFTPD = FindWindow("ioFTPD::MessageWindow", NULL);
dwProcessId = GetCurrentProcessId();
if (!hIoFTPD)
return false;

// Determinate amount of memory to allocate for vfs reader
if (dwAllocationSize < dwAllocationAsked + sizeof(DC_MESSAGE)) // Compare against amount of memory needed for shmem
dwAllocationSize = dwAllocationAsked + sizeof(DC_MESSAGE);

if (hShell32) {
// Get shared memory functions
/*
ShMem_Allocate = (HANDLE (WINAPI *)(LPVOID, ULONG, DWORD))GetProcAddress(hShell32, "SHAllocShared");
ShMem_Free = (BOOL (WINAPI *)(HANDLE, DWORD))GetProcAddress(hShell32, "SHFreeShared");
ShMem_Unlock = (BOOL (WINAPI *)(LPVOID))GetProcAddress(hShell32, "SHUnlockShared");
ShMem_Lock = (LPVOID (WINAPI *)(HANDLE, DWORD))GetProcAddress(hShell32, "SHLockShared");
*/
ShMem_Allocate = (HANDLE (WINAPI *)(LPVOID, ULONG, DWORD))GetProcAddress(hShell32, (char *)520);
ShMem_Free = (BOOL (WINAPI *)(HANDLE, DWORD))GetProcAddress(hShell32, (char *)523);
ShMem_Unlock = (BOOL (WINAPI *)(LPVOID))GetProcAddress(hShell32, (char *)522);
ShMem_Lock = (LPVOID (WINAPI *)(HANDLE, DWORD))GetProcAddress(hShell32, (char *)521);

if (ShMem_Allocate && ShMem_Free && ShMem_Lock && ShMem_Unlock)
{
// Shared memory exists - allocate memory
hSharedMemory = ShMem_Allocate(NULL, dwAllocationSize, dwProcessId);
if (hSharedMemory == INVALID_HANDLE_VALUE)
return false;
// Create event
if ((hEvent = CreateEvent(NULL, FALSE, FALSE, NULL)) == INVALID_HANDLE_VALUE)
return false;
lpMessage = (LPDC_MESSAGE)ShMem_Lock(hSharedMemory, dwProcessId); // Lock allocation
}
}
// Allocation failed
if (!lpMessage)
return false;

// Copy Message id
lpMessage->hEvent = hEvent;
lpMessage->lpMemoryBase = lpMessage;
lpMessage->lpContext = (LPVOID)((ULONG)lpMessage + sizeof(DC_MESSAGE));

// Send initialization message
hMemory = (LPVOID)SendMessage(hIoFTPD, WM_SHMEM_ALLOC, (WPARAM)dwProcessId, (LPARAM)hSharedMemory);

if (!hMemory) // Verify handle
return false;

return true;
}

int closeSharedMem()
{
// Free shared memory
if (hSharedMemory)
{
if (hMemory) SendMessage(hIoFTPD, WM_SHMEM_FREE, NULL, (LPARAM)hMemory);
if (lpMessage) ShMem_Unlock(lpMessage);
ShMem_Free(hSharedMemory, dwProcessId);
}
if (hEvent != INVALID_HANDLE_VALUE) CloseHandle(hEvent);
if (hShell32) FreeLibrary(hShell32);

return true;
}

void main (...)
{
int iPos = 0, nPos = 0, Users = 0;

if (!initSharedMem())
exit(1);
while (nPos != -1 && Users < 1000 ) {
// Initial setup for DC_GET_ONLINEDATA
lpMessage->dwIdentifier = DC_GET_ONLINEDATA;
PDC_ONLINEDATA pOnlineData = (PDC_ONLINEDATA)lpMessage->lpContext;
pOnlineData->iOffset = nPos;
pOnlineData->dwSharedMemorySize = sizeof(DC_MESSAGE) + sizeof(DC_ONLINEDATA) + (_MAX_PATH + 1) * 2;

char username[64]="Unknown",groupname[64]="Unknown",action[20]="Unknown",lastaction[1024]="Unknown",ident[MAX_IDENT], pwd[_MAX_PWD + 1], ip[16];

// Send Message
SendMessage(hIoFTPD, WM_SHMEM, NULL, (LPARAM)hMemory);
// Wait until processed (5 secs)
if (WaitForSingleObject(hEvent, 5000) == WAIT_TIMEOUT) break;
if (!lpMessage->dwReturn) {
nPos = pOnlineData->iOffset;
if (iPos>=nPos || nPos == 1024)
break;

int actionId = (int)pOnlineData->OnlineData.bTransferStatus;
strcpy(lastaction,pOnlineData->OnlineData.szAction);
int interval = pOnlineData->OnlineData.dwIntervalLength;
DWORD bytesTransfered = pOnlineData->OnlineData.dwBytesTransfered;
strcpy(ident,pOnlineData->OnlineData.szIdent);
strcpy(pwd,pOnlineData->OnlineData.szVirtualPath);
INT64 totalBytesTransfered = pOnlineData->OnlineData.i64TotalBytesTransfered;
struct in_addr temp; temp.s_addr = pOnlineData->OnlineData.ulClientIp; strcpy(ip, inet_ntoa(temp));

int UserId = pOnlineData->OnlineData.Uid;
if (UserId==-1) continue; // -1 occurs when user is login resolveUidToName(UserId,username);

// Get groupid
PUSERFILE pUserFile = (PUSERFILE)lpMessage->lpContext;
lpMessage->dwIdentifier = DC_USERFILE_OPEN;
pUserFile->Uid = UserId;
dwReturn = SendMessage(hIoFTPD, WM_SHMEM, NULL, (LPARAM)hMemory);
if (!dwReturn) {
// Wait until processed (5 secs)
if (WaitForSingleObject(hEvent, 5000) == WAIT_TIMEOUT) break;

resolveGidToName(pUserFile->Gid,groupname);
}
lpMessage->dwIdentifier = DC_USERFILE_CLOSE;;
dwReturn = SendMessage(hIoFTPD, WM_SHMEM, NULL, (LPARAM)hMemory);
if (!dwReturn) {
if (WaitForSingleObject(hEvent, 5000) == WAIT_TIMEOUT) break;
}

switch(actionId) {
case 0: strcpy(action,"idle"); break;
case 1: strcpy(action,"download"); break;
case 2: strcpy(action,"upload"); break;
case 3: strcpy(action,"list"); break;
}
if (stristr(lastaction,"PASS ")!=NULL)
strcpy(lastaction,"PASS ******");
int speed = 0;
if (interval!=0)
speed = bytesTransfered/interval;
printf("CIDX;%i;%s;%s;%s;%s;%s;%s;%s;%i;%i\n",iPos,username,groupname,ident,ip,action,lastactio n,pwd,speed,totalBytesTransfered);

Users++;
iPos = nPos;
}
if (lpMessage->dwReturn == (DWORD)-1)
break;
pOnlineData->iOffset++;
}
closeSharedMem();
}