wpcGetMac.cpp

Demostrates static functions of WpcAdapter and routing subsystem.

/*
    wpcobj is a C++ wrapper for libpcap (the packet capture library)
    Copyright (C) 2009 Positive Technologies

    This file is part of wpcobj

    wpcobj is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    wpcobj is is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with wpcobj is.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _WIN32_WINNT          // Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501   // Change this to the appropriate value to target other versions of Windows.
#endif                                    

#include <stdio.h>
#include <wpcobj.h>

int main(int argc, char* argv[])
{
    WpcMacAddr  mac;
    bpf_u_int32 ipaddr;

    try {
        if (argc < 2) {
            printf("Usage: %s <target>\n\n", argv[0]);
            printf("Sample tries to resolve MAC address of the given target.\n");
            printf("It may be host's address or the router address if the "
                   "target is outside local network.\n");
            return -1;
        }
        if (WpcGetArpRecord(argv[1], &mac)) {
            printf("MAC address for host \"%s\" found!\nResult is: %s\n", argv[1], mac.ToString().c_str());
        }
        else {
            ipaddr = WpcGetNextHop(argv[1]);
            if(WpcIsLocalIP(ipaddr)) {
                printf("Host \"%s\" is localhost!\n", argv[1]);
                return 0;
            }
            if (WpcGetArpRecord(ipaddr, &mac)) {
                WpcAddress routeAddr(ipaddr);
                printf("Host \"%s\" is out of local segment!\n", argv[1]);
                printf("MAC address for router \"%s\" is: %s\n",
                    routeAddr.GetAddressStr().c_str(), mac.ToString().c_str());
            }
            else {
                printf("Can't find MAC address of the target. Sorry.\n");
            }
        }
    }
    catch (...) {
        printf("Something strange happens! Exiting...\n");
    }
      return 0;
}



 
©2009 Positive Technologies
 
Generated on Fri May 22 18:17:00 2009 for wpcObj by 
 
 
1.5.7
 
Get wpcObj at SourceForge.net. Fast, secure and Free Open Source software downloads