华为portal2.0协议封装

说明

本文档提供封装协议的代码,详情请参考华为portal2.0协议标准

封装结构

华为portal2.0协议封装_第1张图片

使用说明

依据需要发送的阶段,调用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 = new ArrayList();
    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 getAttr() {
        return attr;
    }

    public void setAttr(List 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,
            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) {
        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 = new ArrayList();

    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 getAttr() {
        return attr;
    }

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

    public TlvBean() {
    }

    public TlvBean(int ver, int type, int serialNo, int reqId, String userIp, int errCode, int attrNum, List 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) {
        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){
        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){
        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){
        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){
        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){
        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 = new ArrayList();
        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){
        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){
        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){
        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){
        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){
        TlvBean b = new TlvBean(1, 9, serialNo, reqId, userIp, 0, attr.size(), attr);
        return b.toByteArray();
    }
    public static TlvBean parse(byte[] a){
        List attr = new ArrayList();
        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

你可能感兴趣的:(AAA)