i need to display only IP address

October 5, 2010 at 00:05:09
Specs: Windows XP
// ScanDlg.cpp : implementation file
//

#include "stdafx.h"
#include "APTool.h"
#include "ScanDlg.h"
#include "MyAsyncSocket.h"
#include "ConfigureDlg.h"
#include "Iphlpapi.h"
#include "AdapterDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

ExploreFrame device_table[SCAN_TABLE_SIZE];

char first_init = TRUE;

int m_nSortColumn; //column number to sort

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CScanDlg dialog


CScanDlg::CScanDlg(CWnd* pParent /*=NULL*/)
: CDialog(CScanDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CScanDlg)
//}}AFX_DATA_INIT
}


void CScanDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CScanDlg)
DDX_Control(pDX, IDC_WEBCONFIGURE, m_WebConfig_Button);
DDX_Control(pDX, IDC_ADAPTER, m_Adapter_Button);
DDX_Control(pDX, IDCANCEL, m_Cancel_Button);
DDX_Control(pDX, IDC_BUTTON_SCAN, m_Scan_Button);
DDX_Control(pDX, IDC_ABOUT_BTN, m_About_Button);
DDX_Control(pDX, IDC_CONFIGURE, m_Configure_Button);
DDX_Control(pDX, IDC_LIST_SCAN_RESULTS, m_ScanResultsList);
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CScanDlg, CDialog)
//{{AFX_MSG_MAP(CScanDlg)
ON_BN_CLICKED(IDC_BUTTON_SCAN, OnButtonScan)
ON_BN_CLICKED(IDC_CONFIGURE, OnConfigure)
ON_NOTIFY(NM_DBLCLK, IDC_LIST_SCAN_RESULTS, OnDblclkListScanResults)
ON_BN_CLICKED(IDC_ABOUT_BTN, OnAboutBtn)
ON_WM_SIZE()
ON_WM_GETMINMAXINFO()
ON_BN_CLICKED(IDC_ADAPTER, OnAdapterBtn)
ON_NOTIFY(HDN_ITEMCLICKA, 0, OnHeaderClicked)
ON_NOTIFY(HDN_ITEMCLICKW, 0, OnHeaderClicked)
ON_BN_CLICKED(IDC_WEBCONFIGURE, OnWebConfigure)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScanDlg message handlers

void CScanDlg::OnButtonScan()
{
// TODO: Add your control notification handler code here
// "Scan" button was pressed.
// Send the scan request and look for replies.

BOOL bValue = TRUE; // Bool value to pass to Socket calls
BOOL bError = FALSE; // Bool value to indicate unrecoverable error
int i; // Just a counter
int nRead; // Number of bytes read from receive buffer
int nErrorCode; // Error code
MyAsyncSocket *MySocket; // Ptr to My Socket
ExploreFrame MyExploreFrame; // Structure containing data of frame to send
BYTE buff[1512]; // Receive buffer (Don't expect fragments)
CTime Time;
CWaitCursor wait;
int total_bytes;
const char COPYRIGHT_MESSAGE[] = " Copyright (c) Proxim Corporation 2006 ";
CAdapterDlg AdapterDlg;
int update;


// Clear the device list
m_ScanResultsList.DeleteAllItems();
m_Configure_Button.EnableWindow(FALSE);
m_WebConfig_Button.EnableWindow(FALSE);
m_ScanResultsList.UpdateData(FALSE);
m_ScanResultsList.UpdateWindow();

MySocket = new MyAsyncSocket(); // Create a new Socket


// If first scan, select network adapter
if (first_init)
{

// Display Dialog Box
update = AdapterDlg.DoModal();

// No adapters detected... exit
if (ERR_NO_NIC == update)
{
CScanDlg::EndDialog(IDABORT);
}

first_init = FALSE;
}

if (network_adapter[0] != 0)
{
// Create socket
if (! MySocket->Create(0, SOCK_DGRAM, 0, (const char *)network_adapter))
{
delete MySocket;
return;
}
}
else
{
// Create socket
if (! MySocket->Create(0, SOCK_DGRAM, 0))
{
delete MySocket;
return;
}
}

// Enable broadcasts for this socket
if (!MySocket->SetSockOpt(SO_BROADCAST,&bValue,sizeof(TRUE),SOL_SOCKET))
nErrorCode = MySocket->GetLastError();
// Enable reuse of this address
if (!MySocket->SetSockOpt(SO_REUSEADDR,&bValue,sizeof(TRUE),SOL_SOCKET))
nErrorCode = MySocket->GetLastError();

// Update frame structure
memset(&MyExploreFrame, 0, sizeof(MyExploreFrame));
MyExploreFrame.Type = SCAN_TYPE_EXPLORE;
MyExploreFrame.Opcode = SCAN_OPCODE_REQUEST;

// Send frame
for (i = 3; i > 0 ; i--)
{
MySocket->SendTo(&MyExploreFrame.Type,sizeof(MyExploreFrame),2719/*192*/,NULL,MSG_DONTROUTE);

// Now wait while reading in and processing Explore Response Frames
Time = Time.GetCurrentTime();
total_bytes = 0;

//AfxMessageBox("Entered");
//::MessageBox(NULL, "Entered", "Caption", MB_OK);
while ((Time == Time.GetCurrentTime()) && (bError==FALSE))
{
//TRACE0("Entered.");
//AfxMessageBox("Entered in while");
//::MessageBox(NULL, "Entered in while", "Caption", MB_OK);

nRead = MySocket->Receive(buff, sizeof(MyExploreFrame) - total_bytes);
total_bytes += nRead;
if (total_bytes >= sizeof(MyExploreFrame))
{
switch (nRead)
{
case 0:
// Connection was closed
// We shouldn't get here...
bError = TRUE;
break;
case SOCKET_ERROR:
// WSAEWOULDBLOCK means there is no data, so we try again...
if (GetLastError()!= WSAEWOULDBLOCK)
{
AfxMessageBox ("Socket Connection Error",MB_OK|MB_ICONEXCLAMATION);
bError=TRUE;
}
break;
default:
// We received some data, so call ScanPacketDigest to manage it
ScanPacketDigest(buff);
}//switch
}//if

total_bytes = 0;

}//while
}//for

delete MySocket;

// Select first item in list by default (if there is at least one item)
if (m_ScanResultsList.GetItemCount() > 0)
{
m_ScanResultsList.SetFocus();
m_ScanResultsList.SetSelectionMark(0);
m_ScanResultsList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
m_ScanResultsList.SetItemState( 0, LVIS_SELECTED | LVIS_FOCUSED , LVIS_SELECTED | LVIS_FOCUSED);

m_ScanResultsList.UpdateWindow();
m_Configure_Button.EnableWindow();
m_WebConfig_Button.EnableWindow();
}

return;

}

BOOL CScanDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}

m_ScanResultsList.InsertColumn(0,"MAC Address",LVCFMT_LEFT,100);
m_ScanResultsList.InsertColumn(1,"System Name",LVCFMT_LEFT,120);
m_ScanResultsList.InsertColumn(2,"IP Address",LVCFMT_LEFT,100);
m_ScanResultsList.InsertColumn(3,"Uptime",LVCFMT_LEFT,100);
m_ScanResultsList.InsertColumn(4,"System Description",LVCFMT_LEFT,252);
OnButtonScan();

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

void CScanDlg::ScanPacketDigest(BYTE *buff)
{
ExploreFrame *pEF;
char cString[32];
char cTempString[32];
int nIndex;
time_t time;
CTime TempTime;
CString String;

// Overlay buffer with ExploreFrame structure...
pEF = (ExploreFrame *)buff;

if( (pEF->Type != SCAN_TYPE_EXPLORE) && ( pEF->Type != SCAN_TYPE_AP) )
return;
if( pEF->Opcode != SCAN_OPCODE_REPLY)
return;

// It's a reply to a Scan request, so copy the info in our table

//Find the current top index
nIndex = m_ScanResultsList.GetItemCount();

// If the table is maxed out, return without adding this data
if (nIndex >= SCAN_TABLE_SIZE)
{
return;
}

#if 0
//Convert binary IP address into dotted decimal
ConvertIPtoASCII(cString,pEF->IPAddress);

// Compare with IP addresses already in the table
for (int i=0; i<nIndex; i++)
{
m_ScanResultsList.GetItemText(i,0,&cTempString[0],32);
if (!strcmp(cString,cTempString))
return;
}
#endif

// Convert MAC address into string
ConvertMACtoASCII(cString,pEF->MACAddress);

// Compare with MAC addresses already in the table
for (int i=0; i<nIndex; i++)
{
m_ScanResultsList.GetItemText(i,0,&cTempString[0],32);
if (!strcmp(cString,cTempString))
return;
}

// Save the information to the device table
memcpy(&device_table[nIndex], pEF, sizeof(ExploreFrame));

// The item apparently MUST have a constant label.....!!!!!!!
m_ScanResultsList.InsertItem(nIndex,"label");

// Convert binary MAC address into AA-BB-CC-DD-EE-FF form
ConvertMACtoASCII(cString,pEF->MACAddress);
m_ScanResultsList.SetItemText(nIndex,0,cString);

// Copy the System Name
m_ScanResultsList.SetItemText(nIndex,1,(const char *)pEF->SysName);

//The text of the item will be the IP Address in dotted decimal
ConvertIPtoASCII(cString,pEF->IPAddress);
m_ScanResultsList.SetItemText(nIndex,2,cString);

// Convert and copy the Sys Time value
time = (htonl(*(long *)pEF->SysUpTime)) / 100;
sprintf(String.GetBuffer(24), "%d d %d h %d m %d s",
(time / 86400), ((time % 86400) / 3600), ((time % 3600) / 60), (time % 60));
m_ScanResultsList.SetItemText(nIndex,3,String);

// Copy the System Description
m_ScanResultsList.SetItemText(nIndex,4,(const char *)pEF->SysDescription);
m_ScanResultsList.SetItemData(nIndex, (DWORD)nIndex);

return;
}

void ConvertMACtoASCII(char cString[],BYTE MACAddress[])
{
char cTempString[32];
//char cTempByteString[3];

cString[0]=NULL;
for (int i=0;i<6;i++)
{
//itoa((int)MACAddress[i],cTempString,16);
sprintf(cTempString, "%2.2x", MACAddress[i]);
strcat(cString,cTempString);
strcat(cString,"-");
}
cString[strlen(cString)-1] = '\0';

return;
}

void ConvertIPtoASCII(char cString[],BYTE IPAddress[])
{
char cTempString[32];

cString[0]=NULL;
for (int i=0;i<3;i++)
{
itoa((int)IPAddress[i],cTempString,10);
strcat(cString,cTempString);
strcat(cString,".");
}
itoa((int)IPAddress[3],cTempString,10);
strcat(cString,cTempString);

return;
}

void CScanDlg::OnConfigure()
{
int list_index;
char temp_string[32];
int update;
char cString[32];
time_t time;
//CTime TempTime;
CString String;
ExploreFrame *pEF;

// The "Configure" button was pressed.
// Now build the Configure Dialog.

// Declare a dialog class object
CConfigureDlg ConfigureDlg;

// Initialize dialog with data from current selection
list_index = m_ScanResultsList.GetSelectionMark();
ConfigureDlg.m_MAC_Address = m_ScanResultsList.GetItemText(list_index, 0);
ConfigureDlg.m_Device_Name = m_ScanResultsList.GetItemText(list_index, 1);
ConfigureDlg.m_IP_Address = m_ScanResultsList.GetItemText(list_index, 2);

ConvertIPtoASCII(temp_string, device_table[list_index].SubnetMask);
ConfigureDlg.m_Subnet_Mask = temp_string;

ConvertIPtoASCII(temp_string, device_table[list_index].Gateway);
ConfigureDlg.m_Gateway = temp_string;

ConvertIPtoASCII(temp_string, device_table[list_index].TFTPServerAddress);
ConfigureDlg.m_TFTP_Server_IP = temp_string;
ConfigureDlg.m_Image_File_Name = device_table[list_index].DownloadFileName;

if (1 == *(long *)device_table[list_index].IPType)
{
ConfigureDlg.m_Radio_Static = 0;
ConfigureDlg.m_Radio_Dynamic = 1;
}
else
{
ConfigureDlg.m_Radio_Static = 1;
ConfigureDlg.m_Radio_Dynamic = 0;
}

DWORD ourIndex = m_ScanResultsList.GetItemData(list_index);
memcpy(&ConfigureDlg.m_Device_Info, &device_table[ourIndex], sizeof(ExploreFrame));

// Display Dialog Box
update = ConfigureDlg.DoModal();

if (update != IDCANCEL)
{
// Initialize pointer
pEF = (ExploreFrame *)&ConfigureDlg.m_Device_Info;


// Save the information to the device table
memcpy(&device_table[list_index], pEF, sizeof(ExploreFrame));

// Convert binary MAC address into AA-BB-CC-DD-EE-FF form
ConvertMACtoASCII(cString,pEF->MACAddress);
m_ScanResultsList.SetItemText(list_index ,0,cString);

// Copy the System Name
m_ScanResultsList.SetItemText(list_index,1,(const char *)pEF->SysName);

//The text of the item will be the IP Address in dotted decimal
ConvertIPtoASCII(cString,pEF->IPAddress);
m_ScanResultsList.SetItemText(list_index,2,cString);

// Convert and copy the Sys Time value
time = (htonl(*(long *)pEF->SysUpTime)) / 100;
sprintf(String.GetBuffer(24), "%d d %d h %d m %d s",
(time / 86400), ((time % 86400) / 3600), ((time % 3600) / 60), (time % 60));
m_ScanResultsList.SetItemText(list_index,3,String);
UpdateData(FALSE);
}
}

void CScanDlg::OnDblclkListScanResults(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
NMHDR *unused;

unused = pNMHDR; // Get rid of unused param warning
OnConfigure();

*pResult = 0;
}

void CScanDlg::OnAboutBtn()
{
// TODO: Add your control notification handler code here
CAboutDlg AboutDlg;

AboutDlg.DoModal();
}

void CScanDlg::OnSize(UINT nType, int cx, int cy)
{
WINDOWPLACEMENT place;

CDialog::OnSize(nType, cx, cy);

// TODO: Add your message handler code here

// Move controls with resizing action
// - RESCAN button
m_Scan_Button.GetWindowPlacement(&place);
place.rcNormalPosition.left = cx - 85;
place.rcNormalPosition.right = place.rcNormalPosition.left + BUTTON_WIDTH;
//place.rcNormalPosition.top = cy - 190;
//place.rcNormalPosition.bottom = place.rcNormalPosition.top + BUTTON_HEIGHT;
m_Scan_Button.SetWindowPlacement(&place);

// - CHANGE button
m_Configure_Button.GetWindowPlacement(&place);
place.rcNormalPosition.left = cx - 85;
place.rcNormalPosition.right = place.rcNormalPosition.left + BUTTON_WIDTH;
//place.rcNormalPosition.top = cy - 156;
//place.rcNormalPosition.bottom = place.rcNormalPosition.top + BUTTON_HEIGHT;
m_Configure_Button.SetWindowPlacement(&place);


// - WEB Config button
m_WebConfig_Button.GetWindowPlacement(&place);
place.rcNormalPosition.left = cx - 85;
place.rcNormalPosition.right = place.rcNormalPosition.left + BUTTON_WIDTH;
//place.rcNormalPosition.top = cy - 156;
//place.rcNormalPosition.bottom = place.rcNormalPosition.top + BUTTON_HEIGHT;
m_WebConfig_Button.SetWindowPlacement(&place);

// - ADAPTER button
m_Adapter_Button.GetWindowPlacement(&place);
place.rcNormalPosition.left = cx - 85;
place.rcNormalPosition.right = place.rcNormalPosition.left + BUTTON_WIDTH;
m_Adapter_Button.SetWindowPlacement(&place);

// - CANCEL button
m_Cancel_Button.GetWindowPlacement(&place);
place.rcNormalPosition.left = cx - 85;
place.rcNormalPosition.right = place.rcNormalPosition.left + BUTTON_WIDTH;
place.rcNormalPosition.top = cy - 35;
place.rcNormalPosition.bottom = place.rcNormalPosition.top + BUTTON_HEIGHT;
m_Cancel_Button.SetWindowPlacement(&place);

// - ABOUT button
m_About_Button.GetWindowPlacement(&place);
//place.rcNormalPosition.left = cx - 685;
//place.rcNormalPosition.right = place.rcNormalPosition.left + BUTTON_WIDTH;
place.rcNormalPosition.top = cy - 35;
place.rcNormalPosition.bottom = place.rcNormalPosition.top + BUTTON_HEIGHT;
m_About_Button.SetWindowPlacement(&place);

// - Scan list
m_ScanResultsList.GetWindowPlacement(&place);
place.rcNormalPosition.right = cx - 113;
place.rcNormalPosition.bottom = cy - 76;
m_ScanResultsList.SetWindowPlacement(&place);
//m_ScanResultsList.MoveWindow(0, 0, cx - 100, cy - 100, TRUE );
}

void CScanDlg::OnGetMinMaxInfo(MINMAXINFO *minmax)
{
// Set the minimum dialog box size
minmax->ptMinTrackSize.x = 400;
minmax->ptMinTrackSize.y = 250;

CDialog::OnGetMinMaxInfo(minmax);
}

void CScanDlg::OnAdapterBtn()
{
// TODO: Add your control notification handler code here
first_init = TRUE;
OnButtonScan();
}

int CALLBACK CompareFunction(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, m_nSortColumn);
CString strItem2 = pListCtrl->GetItemText(lParam2, m_nSortColumn);

return strcmp(strItem1, strItem2);
//return strItem1.CompareNoCase(strItem2);
}

void CScanDlg::OnHeaderClicked(NMHDR* pNMHDR, LRESULT* pResult)
{
HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;

// TODO: Add your control notification handler code here

//m_nSortColumn = phdr->iItem;
m_nSortColumn = phdn->iItem;
for (int i=0;i < m_ScanResultsList.GetItemCount();i++){
m_ScanResultsList.SetItemData(i, (DWORD)i);
}
m_ScanResultsList.SortItems(CompareFunction, (LPARAM)&m_ScanResultsList);
*pResult = 0;
}


void CScanDlg::OnWebConfigure()
{
int list_index = m_ScanResultsList.GetSelectionMark();
ShellExecute(m_hWnd, NULL, "iexplore", m_ScanResultsList.GetItemText(list_index, 2), NULL, SW_SHOWNORMAL);

}


See More: i need to display only IP address

Report •


#1
October 5, 2010 at 13:32:09
I dont know what language you using, but so much scripting only to "display ip address"? :/

start -> run -> notepad (hit enter)

::COPY THIS
@echo off
for /f "tokens=11" %%j in ('ipconfig') do set GATEWAY=%%j
for /f "tokens=13" %%j in ('ipconfig') do set SUBNETMASK=%%j
for /f "tokens=15" %%j in ('ipconfig') do set IP=%%j
echo.
echo Gateway: %GATEWAY%
echo Subnetmask: %SUBNETMASK%
echo IP: %IP%
echo.
pause
exit

::TO HERE

Save as -> "Anything".bat (without quotes)

Kind Regards,

MUs

Happy is one who says I am an Ottoman.


Report •
Related Solutions


Ask Question