Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

Wireless Network trafic meter

I am looking for a free application for windows CE, that it will be meter at KBytes the Wireless trafic of PDA.

Best Regards
0
DareDevilE12
Asked:
DareDevilE12
  • 2
1 Solution
 
hjgodeCommented:
Hello

some time ago I wrote something to log the data using MIB adapters info. Its written in C++ for windows mobile and hardcoded to only log data for the adapter named "Cellular Line". To get the information about the WLAN adapter, you have to replace "Celleular Line" with the name of the adapter you want to watch:

see attached file

Are you able to use that?

regards

Josef
// AdapterMIBinfo.cpp : Defines the entry point for the application.
//


#include "stdafx.h"
#include "winsock2.h"
#pragma comment (lib, "ws2.lib")

#include "Iphlpapi.h"
#pragma comment (lib, "Iphlpapi.lib")

#include "log2file.h"

TCHAR logname[] = L"traffic.log.txt";
UINT mTimer1 = 0;
TCHAR *szFlagFile = L"\\Windows\\_stop_traffic_log.flag";
DWORD iTime = 30;

void add2log(TCHAR *txt)
{
	Add2Log(txt, false);
}

//use dumpType = 1 for csv dumps of Cellular Line only
int dumpMIBinfos(int dumpType=0)
{
	TCHAR str[2048]=L"";
	// Interface name enumeration
	IP_INTERFACE_INFO *pIpInterface = NULL;
	DWORD dwInterfaceSize = 0;

	// Find out the size of the interface table
	if(GetInterfaceInfo(NULL, &dwInterfaceSize) != ERROR_INSUFFICIENT_BUFFER)
	   return FALSE;

	pIpInterface = (IP_INTERFACE_INFO *)LocalAlloc(LPTR, dwInterfaceSize);
	if(!pIpInterface)
	   return FALSE;

	if(GetInterfaceInfo(pIpInterface, &dwInterfaceSize) != NO_ERROR) {
	   LocalFree(pIpInterface);
	   return FALSE;
	}

	// Walk through the available interfaces
	TCHAR tchInterfaceBuffer[256] = TEXT("\0");
	TCHAR tchStr[1024] = L"\0";
	char   chStr[1024] = "\0";
	for(int nInterface = 0; nInterface < pIpInterface->NumAdapters; nInterface++) 
	{
	   DEBUGMSG(true, (L"--------------------------------------------------------\n"));
		if (dumpType==0)
			add2log(L"--------------------------------------------------------\n");
	   IP_ADAPTER_INDEX_MAP *pIpAdapterMapEntry = NULL;

	   pIpAdapterMapEntry = (IP_ADAPTER_INDEX_MAP *)&pIpInterface->Adapter[nInterface];
	   wsprintf(tchInterfaceBuffer, TEXT("Interface Name: %s\nInterface Index: %d"),pIpAdapterMapEntry->Name, pIpAdapterMapEntry->Index);
	   DEBUGMSG(true, (L"%s\n",tchInterfaceBuffer));
		if (dumpType==0)
			add2log(tchInterfaceBuffer);

		//query the MIB table
	    MIB_IFROW mibInterface;
		memset(&mibInterface, 0, sizeof(MIB_IFROW));

		// To get a specific interface entry, just set the dwIndex
		// field before calling the GetIfEntry function
		mibInterface.dwIndex = pIpAdapterMapEntry->Index;
		if(GetIfEntry(&mibInterface) != NO_ERROR)
		   return FALSE;
		else{
			DEBUGMSG(true, (L"\tname: %s\n", mibInterface.wszName));
			DEBUGMSG(true, (L"\tindx: %u\n", mibInterface.dwIndex));
			DEBUGMSG(true, (L"\tMTU : %u\n", mibInterface.dwMtu));
			DEBUGMSG(true, (L"\tspeed:   %u\n", mibInterface.dwSpeed));
			DEBUGMSG(true, (L"======================================\n"));
			DEBUGMSG(true, (L"\top stat: %u\n", mibInterface.dwOperStatus));
			DEBUGMSG(true, (L"\tin  oct: %u\n", mibInterface.dwInOctets));
			DEBUGMSG(true, (L"\tout oct: %u\n", mibInterface.dwOutOctets));
			DEBUGMSG(true, (L"\tin  discarded: %u\n", mibInterface.dwInDiscards));
			DEBUGMSG(true, (L"\tout discarded: %u\n", mibInterface.dwOutDiscards));
			DEBUGMSG(true, (L"\tin errors:     %u\n", mibInterface.dwInErrors));
			DEBUGMSG(true, (L"\tout errors:     %u\n", mibInterface.dwOutErrors));
			if (dumpType==0)
			{
				wsprintf(str, L"\tname: %s\n", mibInterface.wszName);
				add2log(str);
				wsprintf(str, L"\tindx: %u\n", mibInterface.dwIndex);
				add2log(str);
				wsprintf(str, L"\tMTU : %u\n", mibInterface.dwMtu);
				add2log(str);
				wsprintf(str, L"\tspeed:   %u\n", mibInterface.dwSpeed);
				add2log(str);
				wsprintf(str, L"======================================\n");
				add2log(str);
				wsprintf(str, L"\top stat: %u\n", mibInterface.dwOperStatus);
				add2log(str);
				wsprintf(str, L"\tin  oct: %u\n", mibInterface.dwInOctets);
				add2log(str);
				wsprintf(str, L"\tout oct: %u\n", mibInterface.dwOutOctets);
				add2log(str);
				wsprintf(str, L"\tin  discarded: %u\n", mibInterface.dwInDiscards);
				add2log(str);
				wsprintf(str, L"\tout discarded: %u\n", mibInterface.dwOutDiscards);
				add2log(str);
				wsprintf(str, L"\tin errors:     %u\n", mibInterface.dwInErrors);
				add2log(str);
				wsprintf(str, L"\tout errors:     %u\n", mibInterface.dwOutErrors);
				add2log(str);
			}
			if ( (dumpType==1) && (wcsicmp(pIpAdapterMapEntry->Name, L"Cellular Line")==0) )
			{
				//prepare a time string
				TCHAR lpTimeStr[32];
				TCHAR lpDateStr[32];
				int res = GetTimeFormat(LOCALE_SYSTEM_DEFAULT,
										TIME_FORCE24HOURFORMAT,
										NULL,
										L"hh:mm:ss",
										lpTimeStr,
										sizeof (lpTimeStr ) * sizeof(TCHAR));
				if (res == 0)
				{
					wcscpy(lpTimeStr, L"00:00:00");
				}
				//Read the system date
				res = GetDateFormat(  LOCALE_SYSTEM_DEFAULT,
									  NULL,
									  NULL,
									  L"dd.MM.yyyy",
									  lpDateStr,
									  sizeof (lpDateStr) * sizeof(TCHAR));
				if (res == 0)
				{
					wcscpy(lpDateStr, L"01.01.1961");
				}

				wsprintf(str, L"%s\t %s\t %u\t %u\t %u\t %u\t %u\t %u\n",
									lpDateStr,
									lpTimeStr,
									mibInterface.dwSpeed, 
									mibInterface.dwOperStatus,
									mibInterface.dwInOctets,
									mibInterface.dwOutOctets,
									mibInterface.dwInErrors,
									mibInterface.dwOutErrors);
				add2log(str);
			}
		}

	}
	return true;
}

bool existFile(TCHAR f[MAX_PATH])
{
	FILE *stream;
	/* Open for read (will fail if file "data" does not exist) */
	if( (stream  = _wfopen( f, L"r" )) == NULL )
		return false;
	else
	{
		fclose(stream);
		return true;
	}
}

//global for arguments array
int          argc; 
TCHAR**       argv; 

int SplitArgs(LPTSTR lpCmdLine)
{
  unsigned int i; 
  int          j; 
  // parse a few of the command line arguments 
  // a space delimites an argument except when it is inside a quote 

  argc = 1; 
  int pos = 0; 
  for (i = 0; i < wcslen(lpCmdLine); i++) 
    { 
    while (lpCmdLine[i] == ' ' && i < wcslen(lpCmdLine)) 
      { 
      i++; 
      } 
    if (lpCmdLine[i] == '\"') 
      { 
      i++; 
      while (lpCmdLine[i] != '\"' && i < wcslen(lpCmdLine)) 
        { 
        i++; 
        pos++; 
        } 
      argc++; 
      pos = 0; 
      } 
    else 
      { 
      while (lpCmdLine[i] != ' ' && i < wcslen(lpCmdLine)) 
        { 
        i++; 
        pos++; 
        } 
      argc++; 
      pos = 0; 
      } 
    } 

  argv = (TCHAR**)malloc(sizeof(TCHAR*)* (argc+1)); 

  argv[0] = (TCHAR*)malloc(1024); 
  GetModuleFileName(0, argv[0],1024); 

  for(j=1; j<argc; j++) 
    { 
    argv[j] = (TCHAR*)malloc(wcslen(lpCmdLine)+10); 
    } 
  argv[argc] = 0; 

  argc = 1; 
  pos = 0; 
  for (i = 0; i < wcslen(lpCmdLine); i++) 
    { 
    while (lpCmdLine[i] == ' ' && i < wcslen(lpCmdLine)) 
      { 
      i++; 
      } 
    if (lpCmdLine[i] == '\"') 
      { 
      i++; 
      while (lpCmdLine[i] != '\"' && i < wcslen(lpCmdLine)) 
        { 
        argv[argc][pos] = lpCmdLine[i]; 
        i++; 
        pos++; 
        } 
      argv[argc][pos] = '\0'; 
      argc++; 
      pos = 0; 
      } 
    else 
      { 
      while (lpCmdLine[i] != ' ' && i < wcslen(lpCmdLine)) 
        { 
        argv[argc][pos] = lpCmdLine[i]; 
        i++; 
        pos++; 
        } 
      argv[argc][pos] = '\0'; 
      argc++; 
      pos = 0; 
      } 
    } 
  argv[argc] = 0; 

// Initialize the processes and start the application. 
//  retVal = MyMain(argc, argv); 

  // Delete arguments 
  TCHAR str[MAX_PATH+1];
  for(j=0; j<argc; j++) 
	{ 
		wsprintf(str, L"arg %i = %s\n", j, argv[j]);
		OutputDebugString(str);
		//free(argv[j]); 
	} 
  //free(argv); 
	return argc;
}

int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
	int argc;
	argc = SplitArgs(lpCmdLine); //argc is count of arguments, argv[i] are the arguments
	if (argc>=2)
	{
		if (wcsicmp(L"kill", argv[1])==0)
		{
			FILE *stream;
			stream = _wfopen ( szFlagFile, L"w" );
			if (stream != NULL)
			{
				fwprintf(stream, L"file to stop traffic logging");
				fclose(stream);
				return 1;
			}
			else
				return -2;
		}
		if (argc==3){
			DWORD d=_wtoi(argv[2]);
			if (d!=0)
				iTime = d;
		}
	}


 	// TODO: Place code here.
	//get the app dir
	TCHAR strPath[MAX_PATH];
	TCHAR startPath[MAX_PATH];
	GetModuleFileName (NULL, strPath, MAX_PATH);
	//find the last backslash
	TCHAR* p;
	if ( p = wcsrchr ( strPath, '\\')) 
		*++p = _T('\0'); // zero-terminate at the last backslash
	
	//define the log file
	wsprintf(startPath, L"%s%s", strPath, logname);
	appendfile(startPath);
	
	TCHAR str1[MAX_PATH*2];
	wsprintf(str1, L"date\t time\t speed\t status\t in\t out\t in error\t out error\n");
	add2log(str1);
	
	DWORD dwCount=0;
	do
	{
		if (dwCount == iTime){
			dumpMIBinfos(1);
			dwCount=0;
		}
		dwCount += 1;
		Sleep(1000);
	}
	while (!existFile(szFlagFile));

	//delete the flag file
	DeleteFile(szFlagFile);
	MessageBeep(MB_OK);

	return 0;
}

Open in new window

0
 
DareDevilE12Author Commented:
I will check it, but I prefer a application that will have a real time view of data use.

Best Regards
0
 
hjgodeCommented:
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now