華爲portal2.0協議封裝

說明

本文檔提供封裝協議的代碼,詳情請參考華爲portal2.0協議標準

封裝結構

結構

使用說明

依據需要發送的階段,調用TlvTools中的方法即可,返回的數據類型爲byte[]

代碼

Bean

TlvHwBean.java

針對華爲portal2.0協議封裝的類(用於西加雲杉設備)

package tlv;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

public class TlvHwBean {
    private int ver;
    private int type;
    private int papChap = 0;
    private int rsvd = 0;
    private int serialNo;
    private int reqId;
    private String userIp;
    private int userPort = 0;
    private int errCode;
    private int attrNum;
    private List<Attr> attr = new ArrayList<Attr>();
    private String secret;

    public int getVer() {
        return ver;
    }

    public void setVer(int ver) {
        this.ver = ver;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getSerialNo() {
        return serialNo;
    }

    public void setSerialNo(int serialNo) {
        this.serialNo = serialNo;
    }

    public int getReqId() {
        return reqId;
    }

    public void setReqId(int reqId) {
        this.reqId = reqId;
    }

    public String getUserIp() {
        return userIp;
    }

    public void setUserIp(String userIp) {
        this.userIp = userIp;
    }

    public int getErrCode() {
        return errCode;
    }

    public void setErrCode(int errCode) {
        this.errCode = errCode;
    }

    public int getAttrNum() {
        return attrNum;
    }

    public void setAttrNum(int attrNum) {
        this.attrNum = attrNum;
    }

    public List<Attr> getAttr() {
        return attr;
    }

    public void setAttr(List<Attr> attr) {
        this.attr = attr;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public TlvHwBean() {
    }

    public byte[] toByteArray() {
        int dLen = 32;
        if (!(attr == null && attr.isEmpty())) {
            for (Attr a : attr) {
                dLen += a.getAttrLen();
            }
        }
        byte[] b = new byte[dLen];
        b[0] = (byte) ver;
        b[1] = (byte) type;
        b[2] = (byte) papChap;
        b[3] = (byte) rsvd;
        b[4] = (byte) (serialNo >> 8 & 0xff);
        b[5] = (byte) (serialNo & 0xff);
        b[6] = (byte) (reqId >> 8 & 0xff);
        b[7] = (byte) (reqId & 0xff);
        byte[] ip = ipv4Address2BinaryArray(userIp);
        System.arraycopy(ip, 0, b, 8, 4);
        b[12] = (byte) (userPort >> 8 & 0xff);
        b[13] = (byte) (userPort & 0xff);
        b[14] = (byte) errCode;
        b[15] = (byte) attrNum;
        // 生成附加屬性
        int t1 = 32;
        if (!(attr == null && attr.isEmpty())) {
            for (Attr a : attr) {
                b[t1] = (byte) a.getAttrType();
                b[t1 + 1] = (byte) a.getAttrLen();
                System.arraycopy(a.getAttrValue().getBytes(), 0, b, t1 + 2, a.getAttrValue().getBytes().length);
                t1 += a.getAttrLen();
            }
        }
        byte[] au = getAuthenticator(b);
        System.arraycopy(au, 0, b, 16, 16);
        return b;
    }

    private byte[] ipv4Address2BinaryArray(String ipAdd) {
        byte[] binIP = new byte[4];
        String[] strs = ipAdd.split("\\.");
        for (int i = 0; i < strs.length; i++) {
            binIP[i] = (byte) Integer.parseInt(strs[i]);
        }
        return binIP;
    }

    /**
     * 對字符串md5加密
     *
     * @param str
     * @return
     */
    private byte[] getMD5(byte[] a) {
        try {
            // 生成一個MD5加密計算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 計算md5函數
            md.update(a);
            // digest()最後確定返回md5 hash值,返回值爲8爲字符串。因爲md5 hash值是16位的hex值,實際上就是8位的字符
            // BigInteger函數則將8位的字符串轉換成16位hex值,用字符串來表示;得到字符串形式的hash值
            return md.digest();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private byte[] getAuthenticator(byte[] a) {
        byte[] b = new byte[a.length + secret.getBytes().length];
        System.arraycopy(a, 0, b, 0, a.length);
        System.arraycopy(secret.getBytes(), 0, b, a.length, secret.getBytes().length);
        return getMD5(b);
    }

    public TlvHwBean(int ver, int type, int serialNo, int reqId, String userIp, int errCode, int attrNum, List<Attr> attr,
            String secret) {
        super();
        this.ver = ver;
        this.type = type;
        this.serialNo = serialNo;
        this.reqId = reqId;
        this.userIp = userIp;
        this.errCode = errCode;
        this.attrNum = attrNum;
        this.attr = attr;
        this.secret = secret;
    }

    public TlvHwBean(int ver, int type, int papChap, int rsvd, int serialNo, int reqId, String userIp, int userPort,int errCode, int attrNum, List<Attr> attr) {
        super();
        this.ver = ver;
        this.type = type;
        this.papChap = papChap;
        this.rsvd = rsvd;
        this.serialNo = serialNo;
        this.reqId = reqId;
        this.userIp = userIp;
        this.userPort = userPort;
        this.errCode = errCode;
        this.attrNum = attrNum;
        this.attr = attr;
    }
}

TlvBean.java

針對CMCC portal2.0協議封裝的類

package tlv;

import java.util.ArrayList;
import java.util.List;

public class TlvBean {
    private int ver;
    private int type;
    private int papChap = 0;
    private int rsvd = 0;
    private int serialNo;
    private int reqId;
    private String userIp;
    private int userPort = 0;
    private int errCode;
    private int attrNum;
    private List<Attr> attr = new ArrayList<Attr>();

    public int getVer() {
        return ver;
    }

    public void setVer(int ver) {
        this.ver = ver;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getSerialNo() {
        return serialNo;
    }

    public void setSerialNo(int serialNo) {
        this.serialNo = serialNo;
    }

    public int getReqId() {
        return reqId;
    }

    public void setReqId(int reqId) {
        this.reqId = reqId;
    }

    public String getUserIp() {
        return userIp;
    }

    public void setUserIp(String userIp) {
        this.userIp = userIp;
    }

    public int getErrCode() {
        return errCode;
    }

    public void setErrCode(int errCode) {
        this.errCode = errCode;
    }

    public int getAttrNum() {
        return attrNum;
    }

    public void setAttrNum(int attrNum) {
        this.attrNum = attrNum;
    }

    public List<Attr> getAttr() {
        return attr;
    }

    public void setAttr(List<Attr> attr) {
        this.attr = attr;
    }

    public TlvBean() {
    }

    public TlvBean(int ver, int type, int serialNo, int reqId, String userIp, int errCode, int attrNum, List<Attr> attr) {
        super();
        this.ver = ver;
        this.type = type;
        this.serialNo = serialNo;
        this.reqId = reqId;
        this.userIp = userIp;
        this.errCode = errCode;
        this.attrNum = attrNum;
        this.attr = attr;
    }

    public TlvBean(int ver, int type, int papChap, int rsvd, int serialNo, int reqId, String userIp, int userPort,
            int errCode, int attrNum, List<Attr> attr) {
        super();
        this.ver = ver;
        this.type = type;
        this.papChap = papChap;
        this.rsvd = rsvd;
        this.serialNo = serialNo;
        this.reqId = reqId;
        this.userIp = userIp;
        this.userPort = userPort;
        this.errCode = errCode;
        this.attrNum = attrNum;
        this.attr = attr;
    }

    public byte[] toByteArray() {
        int dLen = 16;
        if (!(attr==null&&attr.isEmpty())) {
            for (Attr a : attr) {
                dLen += a.getAttrLen();
            }
        }
        byte[] b = new byte[dLen];
        b[0] = (byte) ver;
        b[1] = (byte) type;
        b[2] = (byte) papChap;
        b[3] = (byte) rsvd;
        b[4] = (byte) (serialNo & 0xff);
        b[5] = (byte) (serialNo >> 8 & 0xff);
        b[6] = (byte) (reqId & 0xff);
        b[7] = (byte) (reqId >> 8 & 0xff);
        byte[] ip = ipv4Address2BinaryArray(userIp);
        System.arraycopy(ip, 0, b, 8, 4);
        b[12] = (byte) (userPort & 0xff);
        b[13] = (byte) (userPort >> 8 & 0xff);
        b[14] = (byte) errCode;
        b[15] = (byte) attrNum;
        // 生成附加屬性
        int t1 = 16;
        if (!(attr==null&&attr.isEmpty())) {
            for (Attr a : attr) {
                b[t1] = (byte) a.getAttrType();
                b[t1 + 1] = (byte) a.getAttrLen();
                System.arraycopy(a.getAttrValue().getBytes(), 0, b, t1 + 2, a.getAttrValue().getBytes().length);
                t1 += a.getAttrLen();
            }
        }
        return b;
    }

    private byte[] ipv4Address2BinaryArray(String ipAdd) {
        byte[] binIP = new byte[4];
        String[] strs = ipAdd.split("\\.");
        for (int i = 0; i < strs.length; i++) {
            binIP[i] = (byte) Integer.parseInt(strs[i]);
        }
        return binIP;
    }
}

Attr.java

package tlv;

public class Attr {
    private int attrType;
    private int attrLen;
    private String attrValue;

    public int getAttrType() {
        return attrType;
    }

    public void setAttrType(int attrType) {
        this.attrType = attrType;
    }

    public int getAttrLen() {
        return attrLen;
    }

    public void setAttrLen(int attrLen) {
        this.attrLen = attrLen;
    }

    public String getAttrValue() {
        return attrValue;
    }

    public void setAttrValue(String attrValue) {
        this.attrValue = attrValue;
    }

    public Attr() {
        super();
    }

    public Attr(int attrType, String attrValue) {
        super();
        this.attrType = attrType;
        this.attrLen = 2+attrValue.getBytes().length;
        this.attrValue = attrValue;
    }
}

常量

Constants.java

package tlv;

public class Constants {
     public static String SECRET = "123456789";
}

Tools

package tlv;

import java.util.ArrayList;
import java.util.List;

public class TlvTools {
    public static byte[] buildHwReqChallenge(int serialNo,String userIp,List<Attr> attr){
        TlvHwBean b = new TlvHwBean(2, 1, serialNo, 0, userIp, 0, attr.size(), attr, Constants.SECRET);
        return b.toByteArray();
    }
    public static byte[] buildHwReqAuth(int serialNo,int reqId,String userIp,List<Attr> attr){
        TlvHwBean b = new TlvHwBean(2, 3, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
        return b.toByteArray();
    }
    public static byte[] buildHwReqLogout(int serialNo,int reqId,String userIp,List<Attr> attr){
        TlvHwBean b = new TlvHwBean(2, 5, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
        return b.toByteArray();
    }
    public static byte[] buildHwAffAckauth(int serialNo,int reqId,String userIp,List<Attr> attr){
        TlvHwBean b = new TlvHwBean(2, 7, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
        return b.toByteArray();
    }
    public static byte[] buildHwReqInfo(int serialNo,int reqId,String userIp,List<Attr> attr){
        TlvHwBean b = new TlvHwBean(2, 9, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
        return b.toByteArray();
    }
    public static TlvHwBean parseHw(byte[] a){
        List<Attr> attr = new ArrayList<Attr>();
        int count = a[15];
        if(count>0){
            int t = 32;
            for (int i = 0; i < count; i++) {
                Attr c = new Attr();
                c.setAttrType(a[t]);
                c.setAttrLen(a[t+1]);
                byte[] d = new byte[c.getAttrLen()-2];
                System.arraycopy(a, t+2, d, 0, c.getAttrLen()-2);
                c.setAttrValue(new String(d));
                attr.add(c);
            }
        }
        TlvHwBean tlv = new TlvHwBean(a[0],a[1],a[2],a[3],(int)((a[5]<<8)|(a[4])), (int)((a[6]<<8)|(a[7])), (int)a[8]+"."+(int)a[9]+"."+(int)a[10]+"."+(int)a[11], (int)((a[13]<<8)|(a[12])),a[14], a[15], attr);
        return tlv;
    }

    public static byte[] buildReqChallenge(int serialNo,String userIp,List<Attr> attr){
        TlvBean b = new TlvBean(1, 1, serialNo, 0, userIp, 0, attr.size(), attr);
        return b.toByteArray();
    }
    public static byte[] buildReqAuth(int serialNo,int reqId,String userIp,List<Attr> attr){
        TlvBean b = new TlvBean(1, 3, serialNo, reqId, userIp, 0, attr.size(), attr);
        return b.toByteArray();
    }
    public static byte[] buildReqLogout(int serialNo,int reqId,String userIp,List<Attr> attr){
        TlvBean b = new TlvBean(1, 5, serialNo, reqId, userIp, 0, attr.size(), attr);
        return b.toByteArray();
    }
    public static byte[] buildAffAckauth(int serialNo,int reqId,String userIp,List<Attr> attr){
        TlvBean b = new TlvBean(1, 7, serialNo, reqId, userIp, 0, attr.size(), attr);
        return b.toByteArray();
    }
    public static byte[] buildReqInfo(int serialNo,int reqId,String userIp,List<Attr> attr){
        TlvBean b = new TlvBean(1, 9, serialNo, reqId, userIp, 0, attr.size(), attr);
        return b.toByteArray();
    }
    public static TlvBean parse(byte[] a){
        List<Attr> attr = new ArrayList<Attr>();
        int count = a[15];
        if(count>0){
            int t = 16;
            for (int i = 0; i < count; i++) {
                Attr c = new Attr();
                c.setAttrType(a[t]);
                c.setAttrLen(a[t+1]);
                byte[] d = new byte[c.getAttrLen()];
                System.arraycopy(a, t+2, d, 0, a.length);
                c.setAttrValue(new String(d));
                attr.add(c);
            }
        }
        TlvBean tlv = new TlvBean(a[0],a[1],a[2],a[3],(int)((a[5]<<8)|(a[4])), (int)((a[6]<<8)|(a[7])), (int)a[8]+"."+(int)a[9]+"."+(int)a[10]+"."+(int)a[11], (int)((a[13]<<8)|(a[12])),a[14], a[15], attr);
        return tlv;
    }
}

源碼下載地址

http://download.csdn.net/detail/qq_17616169/9824573

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章