Hello Sanjay,

I have attached a text file which contains some code
to draw a mapping btw interfaces obtained from PCAP
and MIB classes(ViewNIC).  The function GetDeviceList
I have made asumes that all the NICs connected to a
machine have atleast a different network number/ or my
function would fail.  But at least it is serving my
purpose.  I agree that this is not the best way to do
it.   Pls let me know if u have a better idea.

I simply ignore NdisWan because I dont need it

Nitesh


--- Sanjay <[EMAIL PROTECTED]> wrote:
> Hi Nitesh,
> 
> I also have a requirement where I need the mac
> address for the network devices identified by pcap.
> ViewNIC application does list the appropriate
> devices but so does microsoft API "GetIfTable" with
> a flag which indicates if you want to sort out the
> devices by index. Now my problem is that when I use
> PCAP it lists "NDIS WAN Adapter" as one of the
> devices which doesn't have a mac address as well as
> my 3COM Adapter, whereas ViewNIC application or
> GetIfTable API list "MS TCPLoopback interface" and
> 3COM as my adapters. Now what I am not sure is if
> there is any relation between "NDIS WAN Adapter" and
> " MS TCP Loopback interface" ? If there is then I
> can simply pick the second adapter with a valid mac
> address (3com) as my desired device else I am in
> trouble. i'll appreciate if you can give me some
> direction in this matter.
> 
> Thanks,
> Sanjay.
> 



__________________________________________________
Do you Yahoo!?
Yahoo! News - Today's headlines
http://news.yahoo.com
#define NIS_UP      0x1
#define NIS_DOWN    0x2
#define NIS_TESTING 0x3

#define INTERNAL FALSE
#define EXTERNAL TRUE

class CDevice {      //Structure to hold the details of an interface, more details 
like MAC addresses are to be added
public:
  char*   DevicePointer;
  CString Name;
  char    State;              //NIS_UP, NIS_DOWN...
  BYTE    IP[NI_MAXPRA];
  BYTE    Net[NI_MAXPRA];
  BYTE    Mask[NI_MAXPRA];
  BYTE    DecapIP[NI_MAXPRA]; //Decapsulator
  BYTE    GWIP[NI_MAXPRA];    //Gateway
  int     MTU;                //Max transfer unit
  int     HWType;
  CHWA    HWA;
  CHWA    HWB;
  BYTE    DeviceNo;
  bool    Flag;

  CDevice* Next;

  CDevice() {DevicePointer=NULL; Name=""; State=NIS_DOWN; Next=NULL; 
DeviceNo=0xFF;Flag=EXTERNAL;}
  ~CDevice() {Empty(Next); delete Next; Next=NULL;}
  
  void Empty(CDevice* _Device){
    if(!_Device) return;
    if(_Device->Next) Empty(_Device->Next);
    delete _Device->Next;
    _Device->Next=NULL;
  }

  void AddDevice(CDevice* _Device) {
    CDevice* Device=this;
    while(Device->Next) Device=Device->Next;
    Device->Next=_Device;
    _Device->DeviceNo=Device->DeviceNo+1;
  }

  CDevice* GetDevice(BYTE _DeviceNo) {
    CDevice* Device=this;
    while(Device) {
      if(Device->DeviceNo==_DeviceNo) return Device;
      Device=Device->Next;
    }
    return NULL;
  }

};




CDevice* GetDeviceList() {
        char *dev;
        char *temp;
        char errbuf[PCAP_ERRBUF_SIZE];
        int Count=0;
        dev = pcap_lookupdev(errbuf);
        if(!dev) return NULL;
        CDevice* DeviceList=new CDevice;
        CUChar UChar;
        do {
                temp=dev;
                dev=(char*)UChar.SetString((BYTE*)dev);
                CString String=UChar.GetAsciiString();
                int WanIP=String.Find("NdisWan");
                if(WanIP != -1) continue;
                ++Count;
                CDevice* Device=new CDevice;
                DeviceList->AddDevice(Device);
                Device->DevicePointer=temp;
                Device->Name=String;
                Device->DeviceNo=Count-1;
        } while(*dev);

        MibII   m;
        if(m.Init()) return NULL;
        int NICCount = m.GetNICCount(0, 0);
        if(Count!=NICCount) {AfxMessageBox("MIB PCAP Device Mismatch!"); return NULL;}
        tSTRUCTNICINFO* pNICInfo;
        pNICInfo = new tSTRUCTNICINFO [NICCount];
        m.GetNICInfo(pNICInfo);

        //Now Match the entries from pcap and MIB
    bpf_u_int32 netp;
    bpf_u_int32 maskp;
        for(int i=0; i<NICCount; ++i) {
                CDevice* Device=DeviceList->GetDevice(i);
                dev=Device->DevicePointer;
                pcap_lookupnet(dev, &netp, &maskp, errbuf);

                bool Found=false;
                for(int j=0; j<NICCount; ++j) {
                        BYTE Net[4];
                        memcpy(Net, pNICInfo[j].IP, 4);
                        NByteAnd(Net, pNICInfo[j].SubnetMask, 4);
                        if(NBytesSameRev(Net, (BYTE*)&netp, 4)) {Found=true; break;}
                }
                if(!Found) {AfxMessageBox("MIB PCAP Device Mismatch!"); return NULL;}

                Device->HWType=pNICInfo[j].type;
                memcpy(Device->IP, pNICInfo[j].IP, 4);
                memcpy(Device->Mask, pNICInfo[j].SubnetMask, 4);
                memcpy(Device->HWA.HWA, pNICInfo[j].MAC, pNICInfo[j].MACLength);
                Device->HWA.HWALen=pNICInfo[j].MACLength;
        }

        CDevice* DeviceToRet=DeviceList->Next;
        DeviceList->Next=NULL;
        delete DeviceList;
        return DeviceToRet;
}


void NByteAnd(BYTE* First, BYTE* Second, BYTE N) {for(int i=0; i<N; ++i) 
{First[i]=First[i] & Second[i];}}

bool NBytesSameRev(BYTE* First, BYTE* Second, BYTE N) {
  for(int i=0; i<N; ++i) if(First[i]!=Second[N-i-1]) return false;
  return true;
}



//The CUChar Class

#ifndef UCharh
#define UCharh

/*
/* This class handles Unicode Strings
*/

class CUChar {
private:
        BYTE value[2];
        CString String;
public:
        CUChar() {value[0]='\0'; value[1]='\0';}


        BYTE* GetStart() {return value;}
        bool IsNull() {return (value[0]=='\0' && value[1]=='\0')?true:false;}
        BYTE* SetChar(BYTE* Start) {value[0]=*Start; value[1]=*(Start+1); return 
Start+2;}//Reads a unicode character from memory location Start and returns a pointer 
to the next expected unicode character in the contigous memory locations
        BYTE* SetString(BYTE* Start) {//Reads a complete unicode string, saves it in 
ascii format in variable String and then returns pointer to the next expected unicode 
string in the next contigous mem locotions
                CUChar UChar;
                BYTE* NewStart=UChar.SetChar(Start);
                String="";
                while(!UChar.IsNull()) {
                        NewStart=UChar.SetChar(NewStart);
                        String += (char)(UChar.GetAscii());
                }
                return NewStart;
        }
        BYTE  GetAscii() {return value[0];}
        CString GetAsciiString() {return String;}
        
};
#endif //UCharh


Reply via email to