mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
190 lines
7.3 KiB
Java
190 lines
7.3 KiB
Java
package encoding;
|
||
|
||
|
||
import util.ByteHelper;
|
||
import util.ByteHelperException;
|
||
|
||
/*
|
||
* @Author: Wang Feng
|
||
* @Description:
|
||
* @Version: 1.0.0
|
||
* @Date: 17:54 2021/3/1
|
||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||
*/
|
||
public class TLV {
|
||
////// 一级:网络分组级别TLV号分配(0 ~ 49)
|
||
public static final int TlvInvalid = 0; // 无效包
|
||
public static final int TlvPacketMINCommon = 5; // 通用MIN包
|
||
public static final int TlvPacketMINManagement = 6; // MIN管理包
|
||
public static final int TlvPacketMINIP = 7; // MIN支持IP包
|
||
|
||
////// 二级:网络包分区TLV号分配 (50 ~ 99)
|
||
public static final int TlvIdentifierField = 50; // 目的标识区
|
||
public static final int TlvSignatureField = 51; // 数字签名区
|
||
public static final int TlvReadOnlyField = 52; // 只读数据区
|
||
public static final int TlvMutableField = 53; // 可变数据区
|
||
public static final int TlvMutableProtectField = 54; // 可变数据区中的保护区
|
||
public static final int TlvMutableDangerousField = 55; // 可变数据区中的非受保护区
|
||
|
||
////// 组件 (100 -> 500)
|
||
// 标识分配
|
||
public static final int TlvIdentifierComponent = 100; // 标识名称组件
|
||
public static final int TlvIdentifier = 101; // 标识
|
||
public static final int TlvIdentifierCommon = 102; // 身份标识
|
||
public static final int TlvIdentifierContentInterest = 103; // 内容兴趣标识
|
||
public static final int TlvIdentifierContentData = 104; // 内容数据标识
|
||
|
||
// 数字签名
|
||
public static final int TlvSignatureValue = 200; // 签名值
|
||
public static final int TlvSignatureInfo = 201; // 签名参数
|
||
public static final int TlvSignatureType = 202; // 签名类型
|
||
public static final int TlvKeyLocator = 203; // 指示证书或公钥的位置
|
||
|
||
public static final int TlvPayload = 204; // 网络包负载
|
||
public static final int TlvCanBePrefix = 205; // 是否可以被较长的数据包前缀匹配
|
||
public static final int TlvMustBeRefresh = 206; // 是否强制只拉取新鲜的数据
|
||
public static final int TlvInterestLifeTime = 207; // 兴趣包生存期
|
||
public static final int TlvNonce = 208; // 随机数
|
||
public static final int TlvHopLimit = 209; // 跳数限制
|
||
public static final int TlvFreshnessPeriod = 210; // 数据包新鲜时间
|
||
public static final int TlvCongestionMark = 211; // 拥塞标记
|
||
public static final int TlvTTL = 212; // Time to live
|
||
public static final int TlvNackHeader = 213; // Nack header
|
||
|
||
// 管理通信协议
|
||
public static final int TlvSliceNumber = 211; // 分片数量
|
||
public static final int TlvSliceSize = 212; // 分片大小
|
||
public static final int TlvTlvLogicFaceId = 213; // 逻辑接口id
|
||
public static final int TlvRemoteAddr = 214; // 源端地址
|
||
public static final int TlvLocalAddr = 215; // 本地地址
|
||
public static final int TlvMtu = 216; // 最大传输单元
|
||
public static final int TlvRet = 217; // bool类型,成功或失败
|
||
public static final int TlvOrigin = 218; //
|
||
public static final int TlvCost = 219; // 链路开销
|
||
public static final int TlvExpires = 220; // LogicFace超时时间,-1为不超时(never)
|
||
|
||
// 目前已分配的最大的Tlv值,第一轮分配的最大值为500,超过该值则断定为不合法
|
||
// TODO: 目前TLV值的分配,第一轮分配都在500以内,如果以后分配更高值时,需要修改此处的限制
|
||
public static final int MaxTlvNum = 500;
|
||
|
||
/**
|
||
* 从字节数组中读出一个变长整型
|
||
* @param buffer
|
||
* @param start
|
||
* @return
|
||
*/
|
||
public static VlInt readVarNumber(byte[] buffer,VlInt start) throws ByteHelperException {
|
||
VlInt length=new VlInt(buffer.length);
|
||
if(start.compareTo(length)>=0){
|
||
return null;
|
||
}
|
||
|
||
int firstbyte= 0;
|
||
firstbyte = ByteHelper.uint8ToInt(ByteHelper.getFirstByte(buffer,start));
|
||
|
||
if(firstbyte<241){
|
||
return new VlInt(firstbyte);
|
||
}else if(firstbyte==241){
|
||
if(length.subtract(start)<3){
|
||
return null;
|
||
}
|
||
return new VlInt(ByteHelper.getLenBytes(buffer,start,3));
|
||
}else if(firstbyte==242){
|
||
if(length.subtract(start)<5){
|
||
return null;
|
||
}
|
||
return new VlInt(ByteHelper.getLenBytes(buffer,start,5));
|
||
}else if(firstbyte==243){
|
||
if(length.subtract(start)<9){
|
||
return null;
|
||
}
|
||
return new VlInt(ByteHelper.getLenBytes(buffer,start,9));
|
||
}else{ //MIN1.0中算越界
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 从字节数组的指定位置中读取一个变长的整型作为 TLV Type
|
||
* @param buffer
|
||
* @param start
|
||
* @return
|
||
*/
|
||
public static VlInt readType(byte[] buffer,VlInt start){
|
||
VlInt tlvType= null;
|
||
try {
|
||
tlvType = readVarNumber(buffer,start);
|
||
} catch (ByteHelperException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
if(tlvType==null||tlvType.isEqual(0)||tlvType.compareTo(MaxTlvNum)>0){
|
||
return null;
|
||
}
|
||
|
||
return tlvType;
|
||
}
|
||
|
||
/**
|
||
* 从字节数组的指定位置中读取一个非负整型
|
||
* todo: 越界问题
|
||
*/
|
||
public static long readNonNegativeInteger(byte[] buffer,int start,int end){
|
||
if(start==end){
|
||
return -1;
|
||
}
|
||
if((start<0)||(end>buffer.length)){
|
||
return -1;
|
||
}
|
||
int len=end-start;
|
||
byte[] bytes=new byte[len];
|
||
System.arraycopy(buffer,start,bytes,0,len);
|
||
if(len==1){
|
||
return ByteHelper.uint8ToLong(buffer[start]);
|
||
}else if(len==2){
|
||
return ByteHelper.uint16ToLong(bytes);
|
||
}else if(len==4){
|
||
return ByteHelper.uint32ToLong(bytes);
|
||
}else if(len==8){
|
||
return ByteHelper.uint64ToLong(bytes);
|
||
}else{
|
||
return -1;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 检查某个type值是否是指定的 TLV-TYPE
|
||
* @param current
|
||
* @param target
|
||
* @return
|
||
*/
|
||
public static boolean expectType(VlInt current,VlInt target){
|
||
if(current.isEqual(target)){
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 根据 TLV 分配规则,判断某个tlvType是否是合规的标识类型
|
||
* TODO: 目前TLV的分配是500为一轮,如果标识范围扩展了,本方法需要对应修改
|
||
* @param tlvType
|
||
* @return
|
||
*/
|
||
public static boolean isValidIdentifierType(VlInt tlvType){
|
||
return tlvType.compareTo(102)>=0 && tlvType.compareTo(199)<=0;
|
||
}
|
||
|
||
/**
|
||
* 根据 TLV 分配规则,判断某个tlvType是否是合规的包类型
|
||
* TODO: 目前TLV的分配是500为一轮,如果标识范围扩展了,本方法需要对应修改
|
||
* @param tlvType
|
||
* @return
|
||
*/
|
||
public static boolean isValidPacketType(VlInt tlvType){
|
||
return tlvType.compareTo(0)>0 && tlvType.compareTo(50)<0;
|
||
}
|
||
|
||
//todo: 错误处理
|
||
}
|