Java tutorial
// // 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. Added more // debug statements // 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.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Enumeration; import java.util.Vector; import cai.flow.packets.v9.TemplateManager; import cai.sql.SQL; import cai.utils.DoneException; import cai.utils.Params; import cai.utils.Util; /* V5 Flow Packet UDPV5_Flow *-------*---------------*------------------------------------------------------* | Bytes | Contents | Description | *-------*---------------*------------------------------------------------------* | 0-1 | version | NetFlow export format version number | *-------*---------------*------------------------------------------------------* | 2-3 | count | Number of flows exported in this packet (1-30) | *-------*---------------*------------------------------------------------------* | 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-15 | unix_nsecs | Residual nanoseconds since 0000 UTC 1970 | *-------*---------------*------------------------------------------------------* | 16-19 | flow_sequence | Sequence counter of total flows seen | *-------*---------------*------------------------------------------------------* | 20 | engine_type | Type of flow-switching engine | *-------*---------------*-------------------------------------------Source ID--* | 21 | engine_id | Slot number of the flow-switching engine | *-------*---------------*------------------------------------------------------* | 22-23 | reserved | Unused (zero) bytes | *-------*---------------*------------------------------------------------------* */ import com.javaforge.styx.utils.AppConfiguration; import org.apache.commons.configuration.Configuration; import org.apache.log4j.Logger; public class V5_Packet implements FlowPacket { static Configuration config = AppConfiguration.getConfig(); static Logger logger = Logger.getLogger(cai.flow.packets.V5_Packet.class); long count; String RouterIP; long SysUptime, unix_secs, unix_nsecs, flow_sequence; long engine_type, engine_id; Vector flows; public static final int V5_Header_Size = 24; public static final int V5_Flow_Size = 48; /** * UDPflowsVector * * @param RouterIP * @param buf * @param len * @throws DoneException */ @SuppressWarnings("unchecked") public V5_Packet(String RouterIP, byte[] buf, int len) throws DoneException { if (false) {//(Params.DEBUG) { // File tmpFile = new File(Params.path + File.separator + "cache.tmp"); if (tmpFile.exists()) { try { ObjectInputStream fIn = new ObjectInputStream(new FileInputStream(tmpFile)); try { buf = (byte[]) fIn.readObject(); len = ((Integer) fIn.readObject()).intValue(); } catch (ClassNotFoundException e) { e.printStackTrace(); } fIn.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { try { ObjectOutputStream fOut; fOut = new ObjectOutputStream(new FileOutputStream(tmpFile)); fOut.writeObject(buf); fOut.writeObject(new Integer(len)); fOut.flush(); fOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } // } if (len < V5_Header_Size) throw new DoneException(" * incomplete header *"); this.RouterIP = RouterIP; count = Util.to_number(buf, 2, 2); if (count <= 0 || len != V5_Header_Size + count * V5_Flow_Size) throw new DoneException(" * corrupted packet " + len + "/" + count + "/" + (V5_Header_Size + count * V5_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); flow_sequence = Util.to_number(buf, 16, 4); engine_type = buf[20]; engine_id = buf[21]; logger.debug(" uptime: " + Util.uptime(SysUptime / 1000) + ", date: " + unix_secs + "." + unix_nsecs); logger.debug(" sequence: " + flow_sequence + ", count: " + count + ", engine: " + engine_type + "/" + engine_id); flows = new Vector((int) count); for (int i = 0, p = V5_Header_Size; i < count; i++, p += V5_Flow_Size) { V5_Flow f; try { f = new V5_Flow(RouterIP, buf, p); if (Params.DEBUG) { if (!f.equals( new V5_Flow(RouterIP, buf, p, TemplateManager.getTemplateManager().getV5Template()))) { logger.error("ERROR: Data inconsistency with different algorithm"); } } if (f.srcaddr != null && f.dstaddr != null) { flows.add(f); } else { if (Params.DEBUG) { logger.error(f.srcaddr + " " + f.dstaddr + " "); } } } catch (DoneException e) { if (Params.DEBUG) { logger.debug("", e); } if (e.getMessage() != null && (!e.getMessage().equals(""))) { logger.debug("", e); } } } } 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.RawV5"); } logger.debug("Using SQL: " + add_raw_sql); for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();) ((V5_Flow) flowenum.nextElement()).save_raw(SysUptime, unix_secs, unix_nsecs, flow_sequence, engine_type, engine_id, sql.prepareStatement("Prepare INSERT to V5 raw table", add_raw_sql)); } @SuppressWarnings("unchecked") public Vector getSrcASVector() { Vector v = new Vector((int) count, (int) count); for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();) v.add(((V5_Flow) flowenum.nextElement()).getDataSrcAS()); return v; } @SuppressWarnings("unchecked") public Vector getDstASVector() { Vector v = new Vector((int) count, (int) count); for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();) v.add(((V5_Flow) flowenum.nextElement()).getDataDstAS()); return v; } @SuppressWarnings("unchecked") public Vector getASMatrixVector() { Vector v = new Vector((int) count, (int) count); for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();) v.add(((V5_Flow) flowenum.nextElement()).getDataASMatrix()); return v; } @SuppressWarnings("unchecked") public Vector getSrcNodeVector() { Vector v = new Vector((int) count, (int) count); for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();) v.add(((V5_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(((V5_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(((V5_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(((V5_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(((V5_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(((V5_Flow) flowenum.nextElement()).getDataInterfaceMatrix()); return v; } @SuppressWarnings("unchecked") public Vector getSrcPrefixVector() { Vector v = new Vector((int) count, (int) count); for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();) v.add(((V5_Flow) flowenum.nextElement()).getDataSrcPrefix()); return v; } @SuppressWarnings("unchecked") public Vector getDstPrefixVector() { Vector v = new Vector((int) count, (int) count); for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();) v.add(((V5_Flow) flowenum.nextElement()).getDataDstPrefix()); return v; } @SuppressWarnings("unchecked") public Vector getPrefixMatrixVector() { Vector v = new Vector((int) count, (int) count); for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();) v.add(((V5_Flow) flowenum.nextElement()).getDataPrefixMatrix()); return v; } @SuppressWarnings("unchecked") public Vector getProtocolVector() { Vector v = new Vector((int) count, (int) count); for (Enumeration flowenum = flows.elements(); flowenum.hasMoreElements();) v.add(((V5_Flow) flowenum.nextElement()).getDataProtocol()); return v; } }