cai.flow.packets.V1_Packet.java Source code

Java tutorial

Introduction

Here is the source code for cai.flow.packets.V1_Packet.java

Source

//
// This file is part of the Styx Application.
//
// Styx is a derivative work, containing both original code, included code
// and modified code that was published under the GNU General Public License.
// Copyrights for modified and included code are below.
//
// Original code base Copyright 2005 Cai Mao (Swingler). All rights reserved.
//
// Modifications:
//
// 2007-11-14 - Removed Syslog and replaced with Log4J
// 2007-12-02 - No longer using ResourceBundles for configuration
//
// This program 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 2 of the License, or
// (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//       
// For more information contact:
// Aaron Paxson <aj@thepaxson5.org>
//
package cai.flow.packets;

import java.util.Enumeration;
import java.util.Vector;

import cai.sql.SQL;
import cai.utils.DoneException;
import cai.utils.Util;

/*
    
 V1 Flow Packet
    
 *-------*------------*-------------------------------------------------------------*
 | Bytes | Contents   | Description                                                 |
 *-------*------------*-------------------------------------------------------------*
 | 0-1   | version    | NetFlow export format version number                        |
 *-------*------------*-------------------------------------------------------------*
 | 2-3   | count      | Number of flows exported in this packet (1-24)              |
 *-------*------------*-------------------------------------------------------------*
 | 4-7   | SysUptime  | Current time in milliseconds since the export device booted |
 *-------*------------*-------------------------------------------------------------*
 | 8-11  | unix_secs  | Current count of seconds since 0000 UTC 1970                |
 *-------*------------*-------------------------------------------------------------*
 | 12-16 | unix_nsecs | Residual nanoseconds since 0000 UTC 1970                    |
 *-------*------------*-------------------------------------------------------------*
    
 */
import com.javaforge.styx.utils.AppConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.log4j.Logger;

public class V1_Packet implements FlowPacket {

    static Configuration config = AppConfiguration.getConfig();

    static Logger logger = Logger.getLogger(cai.flow.packets.V1_Packet.class);

    long count;

    String RouterIP;

    long SysUptime, unix_secs, unix_nsecs;

    Vector flows;

    public final int V1_Header_Size = 16;

    public final int V1_Flow_Size = 48;

    @SuppressWarnings("unchecked")
    public V1_Packet(String RouterIP, byte[] buf, int len) throws DoneException {
        if (len < V1_Header_Size)
            throw new DoneException("    * incomplete header *");

        this.RouterIP = RouterIP;
        count = Util.to_number(buf, 2, 2);

        if (count <= 0 || len != V1_Header_Size + count * V1_Flow_Size)
            throw new DoneException("    * corrupted packet " + len + "/" + count + "/"
                    + (V1_Header_Size + count * V1_Flow_Size) + " *");

        SysUptime = Util.to_number(buf, 4, 4);
        unix_secs = Util.to_number(buf, 8, 4);
        unix_nsecs = Util.to_number(buf, 12, 4);

        logger.debug("    count: " + count + ", uptime: " + Util.uptime(SysUptime / 1000) + ", date: " + unix_secs
                + "." + unix_nsecs);

        flows = new Vector((int) count);

        for (int i = 0, p = V1_Header_Size; i < count; i++, p += V1_Flow_Size)
            flows.add(new V1_Flow(RouterIP, buf, p));
    }

    protected static String add_raw_sql = null;

    public void process_raw(SQL sql) {
        if (add_raw_sql == null) {
            add_raw_sql = config.getString("SQL.Add.RawV1");
        }

        for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();)
            ((V1_Flow) flowenum.nextElement()).save_raw(SysUptime, unix_secs, unix_nsecs,
                    sql.prepareStatement("Prepare INSERT to V1 raw table", add_raw_sql));
    }

    public Vector getSrcASVector() {
        return null;
    }

    public Vector getDstASVector() {
        return null;
    }

    public Vector getASMatrixVector() {
        return null;
    }

    @SuppressWarnings("unchecked")
    public Vector getSrcNodeVector() {
        Vector v = new Vector((int) count, (int) count);

        for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();)
            v.add(((V1_Flow) flowenum.nextElement()).getDataSrcNode());

        return v;
    }

    @SuppressWarnings("unchecked")
    public Vector getDstNodeVector() {
        Vector v = new Vector((int) count, (int) count);

        for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();)
            v.add(((V1_Flow) flowenum.nextElement()).getDataDstNode());

        return v;
    }

    @SuppressWarnings("unchecked")
    public Vector getHostMatrixVector() {
        Vector v = new Vector((int) count, (int) count);

        for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();)
            v.add(((V1_Flow) flowenum.nextElement()).getDataHostMatrix());

        return v;
    }

    @SuppressWarnings("unchecked")
    public Vector getSrcInterfaceVector() {
        Vector v = new Vector((int) count, (int) count);

        for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();)
            v.add(((V1_Flow) flowenum.nextElement()).getDataSrcInterface());

        return v;
    }

    @SuppressWarnings("unchecked")
    public Vector getDstInterfaceVector() {
        Vector v = new Vector((int) count, (int) count);

        for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();)
            v.add(((V1_Flow) flowenum.nextElement()).getDataDstInterface());

        return v;
    }

    @SuppressWarnings("unchecked")
    public Vector getInterfaceMatrixVector() {
        Vector v = new Vector((int) count, (int) count);

        for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();)
            v.add(((V1_Flow) flowenum.nextElement()).getDataInterfaceMatrix());

        return v;
    }

    public Vector getSrcPrefixVector() {
        return null;
    }

    public Vector getDstPrefixVector() {
        return null;
    }

    public Vector getPrefixMatrixVector() {
        return null;
    }

    @SuppressWarnings("unchecked")
    public Vector getProtocolVector() {
        Vector v = new Vector((int) count, (int) count);

        for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();)
            v.add(((V1_Flow) flowenum.nextElement()).getDataProtocol());

        return v;
    }
}