/**
 * Copyright (c) 2009 Andrei Sochirca, All Rights Reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library 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
 * Lesser General Public License for more details.  
 */

import com.smaxe.os.jna.pcap.support.IPacketProcessor;
import com.smaxe.os.jna.pcap.support.NetworkDevice;
import com.smaxe.os.jna.pcap.support.PacketCaptureLibrary;

import java.util.List;

/**
 * <code>ExPacketCapturer</code> - example that shows how to retrieve
 * network devices and start packet capture.
 * 
 * @author Andrei Sochirca
 */
public final class ExPacketCapturer extends Object
{
    /**
     * Entry point.
     * 
     * @param args
     * @throws Exception if an exception occurred
     */
    public static void main(final String[] args) throws Exception
    {
        PacketCaptureLibrary pcap = new PacketCaptureLibrary();
        
        List<NetworkDevice> networkDevices = pcap.findAllNetworkDevices();
        
        for (NetworkDevice networkDevice : networkDevices)
        {
            System.out.println("device: " + networkDevice.getName() + " (" + networkDevice.getDescription() + ")");
            System.out.println("address: " + networkDevice.getAddress());
            System.out.println("netmask: " + networkDevice.getNetmask());
            System.out.println("broadcast address: " + networkDevice.getBroadcastAddress());
            System.out.println("destination address: " + networkDevice.getDestinationAddress());
        }
        
        // get one device and start packet capture
        NetworkDevice networkDevice = networkDevices.get(networkDevices.size() - 1);
        
        networkDevice.setFilter("tcp port 80");
        
        networkDevice.startPacketCapture(new IPacketProcessor()
        {
            public void onPacket(final int dataLinkType, final long timestamp, final byte[] data, final int dataOffset, final int dataLength)
            {
                System.out.println("A raw packet is received");
            }
        });
        
//        networkDevice.stopPacketCapture();
    }
    
    /**
     * Constructor.
     */
    private ExPacketCapturer()
    {
    }
}