⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netflow.java

📁 流量分析 可以对SNIFFER抓的包进行分析
💻 JAVA
字号:
package com.tianxun.NEI.sniffer.util;

import java.io.OutputStream;
import java.util.StringTokenizer;

/**
 * @author 聂军
 * @version 1.0 2004-9-13
 */

public abstract class NetFlow {

    public static final String SRCADDR = "srcaddr";

    public static final String DSTADDR = "dstaddr";

    public static final String NEXTHOP = "nexthop";

    public static final String INPUT = "input";

    public static final String OUTPUT = "output";

    public static final String DPKTS = "dPkts";

    public static final String DOCTETS = "dOctets";

    public static final String FIRST = "First";

    public static final String LAST = "Last";

    public static final String SRCPORT = "srcport";

    public static final String DSTPORT = "dstport";

    public static final String PAD1 = "pad1";

    public static final String TCP_FLAGS = "tcp_flags";

    public static final String PROT = "prot";

    public static final String TOS = "tos";

    public static final String SRCAS = "src_as";

    public static final String DSTAS = "dst_as";

    public static final String SRCMASK = "src_mask";

    public static final String DSTMASK = "dst_mask";

    public static final String PAD2 = "pad2";

    public static final String LENGTH = "length";

    /**
     * 将IP地址转化为字节数组表示.
     *
     * @param ip - IP地址.
     * @return 字节数组.
     */
    public static byte[] ip2byte(String ip) {
        try {
            StringTokenizer st = new StringTokenizer(ip, ".");
            byte[] ary = new byte[4];
            for (int i = 0; i < ary.length; i++) {
                ary[i] = (byte) Integer.parseInt(st.nextToken());
            }
            return ary;
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 将字节数组转化为IP地址.
     *
     * @param bytes - 字节数组.
     * @return IP地址.
     */
    public static String byte2ip(byte[] bytes) {
        int a = (int) (bytes[0] & 0x00FF);
        int b = (int) (bytes[1] & 0x00FF);
        int c = (int) (bytes[2] & 0x00FF);
        int d = (int) (bytes[3] & 0x00FF);
        return a + "." + b + "." + c + "." + d;
    }

    /**
     * 将IP地址转化为整数数字.
     *
     * @param ip - IP地址.
     * @return long.
     */
    public static long ip2long(String ip) {
        try {
            StringTokenizer stier = new StringTokenizer(ip, ".");
            long[] ary = new long[4];
            for(int i = 0; i < ary.length; i++)
                ary[i] = Long.parseLong(stier.nextToken());
            long a = ary[0] << 24;
            long b = ary[1] << 16;
            long c = ary[2] << 8;
            long d = ary[3];
            return a + b + c + d;
        } catch (Exception ex) {
            return 0;
        }
    }

    /**
     * 根据整数值得到IP地址的字符串表示.
     *
     * @param seed - IP地址的整数表示.
     * @return String.
     */
    public static String long2ip(long seed) {
        int a = (int) ((seed & 0xFF000000L) >> 24);
        int b = (int) ((seed & 0xFF0000L) >> 16);
        int c = (int) ((seed & 0xFF00L) >> 8);
        int d = (int) (seed & 0xFFL);
        return a + "." + b + "." + c + "." + d;
    }

    /**
     * 将IP地址写入字节数组的具体位置处.
     *
     * @param rs - byte[].
     * @param index - index.
     * @param ip - IP地址.
     */
    public static void writeIP(byte[] rs, int index, String ip) {
        writeNumber(rs, index, ip2long(ip), 4);
    }

    /**
     * 将整数值以具体的位数写入字节数组的具体位置处.
     *
     * @param rs - byte[].
     * @param index - index.
     * @param value - 整数值.
     * @param nByte - 位数.
     */
    public static void writeNumber(byte[] rs, int index, long value, int nByte) {
        for(int i = nByte; i > 0; i--) {
            rs[index++] = getByte(value, i);
        }
    }

    /**
     * 得到某个整数的某位字节表示.
     *
     * @param value - 整数值.
     * @param index - 位.
     * @return byte.
     */
    public static byte getByte(long value, int index) {
        switch(index) {
            case 1: 
                return (byte) (value & 0xFF);
            case 2:
                return (byte) ((value & 0xFF00) >> 8);
            case 3:
                return (byte) ((value & 0xFF0000) >> 16);
            case 4:
                return (byte) ((value & 0xFF000000) >> 24);
            default:
                return 0;
        }
    }

    /**
     * 将字节序列写入输出流中.
     *
     * @param os - 输出流.
     * @throws Exception - .
     */
    public abstract void writeBytes(OutputStream os) throws Exception;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -