add error handle: ComponentException & PacketException

This commit is contained in:
free will committed 2021-03-24 11:14:54 +08:00
1 parent d7f063cbb9
commit 7bf0906291
35 files changed
+955 -362

No files matched your search

+12 -3
View File
@@ -43,7 +43,7 @@ public class CanBePrefix implements TlvComponentBase,InitialAble, IEncodingAble
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
// 这是一个存在性组件,所以如果值为false,则无需编码
if(!this.canBePrefix){
return -1;
@@ -52,14 +52,23 @@ public class CanBePrefix implements TlvComponentBase,InitialAble, IEncodingAble
int totalLength=0;
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
int tmplen= 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("CanBePrefix.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvCanBePrefix));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvCanBePrefix));
} catch (EncoderException e) {
throw new ComponentException("CanBePrefix.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -0,0 +1,14 @@
package component;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 9:51 2021/3/24
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class ComponentException extends Exception {
public ComponentException(String msg){
super("Component-"+msg);
}
}
+24 -7
View File
@@ -43,25 +43,38 @@ public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAb
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.congestionMark);
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.congestionMark);
} catch (EncoderException e) {
throw new ComponentException("CongestionMark.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("CongestionMark.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvCongestionMark));
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvCongestionMark));
} catch (EncoderException e) {
throw new ComponentException("CongestionMark.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -76,15 +89,19 @@ public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAb
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvCongestionMark))){
return false;
}
// 读取 CongestionMark
long value= 0;
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
long value = 0;
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (TLVException | VlIntException e) {
throw new ComponentException("CongestionMark.wireDecode: "+e.getMessage());
}
if(value<0){
return false;
}
+23 -6
View File
@@ -43,25 +43,38 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.freshnessPeriod);
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.freshnessPeriod);
} catch (EncoderException e) {
throw new ComponentException("FreshnessPeriod.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("FreshnessPeriod.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvFreshnessPeriod));
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvFreshnessPeriod));
} catch (EncoderException e) {
throw new ComponentException("FreshnessPeriod.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -76,7 +89,7 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvFreshnessPeriod))){
return false;
@@ -84,7 +97,11 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA
// 读取 value
long value= 0;
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (TLVException | VlIntException e) {
throw new ComponentException("FreshnessPeriod.wireDecode: "+e.getMessage());
}
if(value<0){
return false;
}
+23 -6
View File
@@ -43,25 +43,38 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.hopLimit);
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.hopLimit);
} catch (EncoderException e) {
throw new ComponentException("HopLimit.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("HopLimit.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvHopLimit));
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvHopLimit));
} catch (EncoderException e) {
throw new ComponentException("HopLimit.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -71,7 +84,7 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
}
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvHopLimit))){
return false;
@@ -79,7 +92,11 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
// 读取 HopLimit
long value= 0;
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (TLVException | VlIntException e) {
throw new ComponentException("HopLimit.wireDecode: "+e.getMessage());
}
if(value<0){
return false;
}
+33 -11
View File
@@ -103,7 +103,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
* 根据一个 TLV Block 创建一个Identifier
* @param block
*/
public Identifier(Block block){
public Identifier(Block block) throws ComponentException {
this.buildIdentifierByBlock(block);
}
@@ -128,10 +128,14 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
* 根据一个 TLV Block 初始化当前Identifier
* @param block
*/
public boolean buildIdentifierByBlock(Block block){
public boolean buildIdentifierByBlock(Block block) throws ComponentException {
// 如果不存在子 TLV, 则不可能是一个 Identifier
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("Identifier.buildIdentifierByBlock: "+e.getMessage());
}
if(block.getSubElements().length()<1){
return false;
@@ -184,11 +188,16 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
for (int i = 0; i < components.length(); i++) {
int tmpLen=components.getElement(i).wireEncode(encoder);
int tmpLen= 0;
try {
tmpLen = components.getElement(i).wireEncode(encoder);
} catch (EncoderException e) {
throw new ComponentException("Identifier.wireEncode: "+e.getMessage());
}
if(tmpLen<=0){
return -1;
}
@@ -196,14 +205,23 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
}
// 编码 TLV-LENGTH
int tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
int tmpLen= 0;
try {
tmpLen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("Identifier.wireEncode: "+e.getMessage());
}
if(tmpLen<=0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifier));
try {
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifier));
} catch (EncoderException e) {
throw new ComponentException("Identifier.wireEncode: "+e.getMessage());
}
if(tmpLen<=0){
return -1;
}
@@ -218,15 +236,19 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvIdentifier))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("Identifier.wireDecode: "+e.getMessage());
}
this.components.clear();
ElementContainer elementContainer=block.getSubElements();
@@ -119,12 +119,8 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
* @param block
* @return
*/
public IdentifierComponent(Block block){
try {
this.wireDecode(block);
} catch (VlIntException e) {
throw new Error("IdentifierComponent: wireDecode error. "+e.getMessage());
}
public IdentifierComponent(Block block) throws ComponentException {
this.wireDecode(block);
}
/**
@@ -181,7 +177,7 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
return this.identifierBlock.encode(encoder);
}
@@ -191,12 +187,16 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
this.identifierBlock=new Block(block);
//todo:越界问题
int length= 0;
length = this.identifierBlock.getLength().getVlIntValue2Int();
try {
length = this.identifierBlock.getLength().getVlIntValue2Int();
} catch (VlIntException e) {
throw new ComponentException("IdentifierComponent.wireDecode: "+e.getMessage());
}
byte[] value=this.identifierBlock.getValue();
// 要求TLV-LENGTH至少为1,因为 IdentifierComponent 使用 TLV-VALUE 中的第一个字节作为Marker
@@ -224,7 +224,11 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
if((length!=2)&&(length!=3)&&(length!=5)&&(length!=9)){
return false;
}
this.intValue=TLV.readNonNegativeInteger(v,0,v.length);
try {
this.intValue=TLV.readNonNegativeInteger(v,0,v.length);
} catch (TLVException e) {
throw new ComponentException("IdentifierComponent.wireDecode: "+e.getMessage());
}
}else{ // byte[]
this.byteArrayValue=v;
}
+25 -7
View File
@@ -139,7 +139,7 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
// 如果标识区不存在标识,则是一个无效的MIN包,直接抛出错误
if(this.identifiers.length()==0){
return 0;
@@ -148,7 +148,12 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
// 编码 TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for (int i = this.identifiers.length()-1; i >= 0; i--) {
int tmplen=this.identifiers.getElement(i).wireEncode(encoder);
int tmplen= 0;
try {
tmplen = this.identifiers.getElement(i).wireEncode(encoder);
} catch (EncoderException e) {
throw new ComponentException("IdentifierField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -156,14 +161,23 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
}
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
int tmplen= 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("IdentifierField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifierField));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifierField));
} catch (EncoderException e) {
throw new ComponentException("IdentifierField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -178,15 +192,19 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 TLV-TYPE 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvIdentifierField))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("IdentifierField.wireDecode: "+e.getMessage());
}
IdentifierContainer identifierContainer=new IdentifierContainer();
int len=block.getSubElements().length();
+43 -19
View File
@@ -23,13 +23,18 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
/**
* 从一个 IdentifierWrapper 编码成的 TLV Block 中解析出一个 IdentifierWrapper
*/
public IdentifierWrapper(Block block){
if(!block.parseSubElements()){
// todo: 错误处理
public IdentifierWrapper(Block block) throws ComponentException {
try {
if(!block.parseSubElements()){
throw new ComponentException("IdentifierWrapper(Block): block.parseSubElements error");
}
} catch (BlockException e) {
throw new ComponentException("IdentifierWrapper(Block): "+e.getMessage());
}
// 检查是否包含有且仅有一个 Identifier TLV
if((block.getSubElements().length()!=1)&&block.getSubElements().getBlock(0).getType().isEqual(TLV.TlvIdentifier)){
// todo: 错误处理
throw new ComponentException("IdentifierWrapper(Block): (block.getSubElements().length()!=1)" +
"&&block.getSubElements().getBlock(0).getType().isEqual(TLV.TlvIdentifier)");
}
this.tlvType=block.getType();
this.identifier.buildIdentifierByBlock(block.getSubElements().getBlock(0));
@@ -45,9 +50,10 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* @param identifierString
* @return
*/
public IdentifierWrapper(VlInt tlvType,String identifierString){
public IdentifierWrapper(VlInt tlvType,String identifierString) throws ComponentException {
if(TLV.isValidIdentifierType(tlvType)){
// todo: 报错
throw new ComponentException("IdentifierWrapper(VlInt tlvType,String identifierString): "
+"tlvType invalid");
}
this.tlvType=tlvType;
this.identifier.buildIdentifierByString(identifierString);
@@ -63,9 +69,10 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* @param container
* @return
*/
public IdentifierWrapper(VlInt tlvType,IdentifierComponentContainer container){
public IdentifierWrapper(VlInt tlvType,IdentifierComponentContainer container) throws ComponentException {
if(TLV.isValidIdentifierType(tlvType)){
// todo: 报错
throw new ComponentException("IdentifierWrapper(VlInt tlvType,IdentifierComponentContainer container): "
+"tlvType invalid");
}
this.tlvType=tlvType;
this.identifier.buildIdentifierByComponents(container);
@@ -77,7 +84,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* @param block
* @return
*/
public IdentifierWrapper(VlInt tlvType,Block block){
public IdentifierWrapper(VlInt tlvType,Block block) throws ComponentException {
if(TLV.isValidIdentifierType(tlvType)){
// todo: 报错
}
@@ -128,7 +135,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* // @param block 传递 Identifier Block,而非 IdentifierWrapper Block
* @return
*/
public IdentifierWrapper createCommonIdentifierByBlock(Block block){
public IdentifierWrapper createCommonIdentifierByBlock(Block block) throws ComponentException {
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierCommon);
identifierWrapper.identifier.buildIdentifierByBlock(block);
@@ -179,7 +186,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* @param block 传递 Identifier Block,而非 IdentifierWrapper Block
* @return
*/
public IdentifierWrapper createContentInterestIdentifierByBlock(Block block){
public IdentifierWrapper createContentInterestIdentifierByBlock(Block block) throws ComponentException {
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentInterest);
identifierWrapper.identifier.buildIdentifierByBlock(block);
@@ -230,7 +237,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* @param block 传递 Identifier Block,而非 IdentifierWrapper Block
* @return
*/
public static IdentifierWrapper createContentDataIdentifierByBlock(Block block){
public static IdentifierWrapper createContentDataIdentifierByBlock(Block block) throws ComponentException {
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentData);
identifierWrapper.identifier.buildIdentifierByBlock(block);
@@ -264,25 +271,38 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=this.identifier.wireEncode(encoder);
int tmplen= 0;
try {
tmplen = this.identifier.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("IdentifierWrapper.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("IdentifierWrapper.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(this.tlvType);
try {
tmplen=encoder.prependVarNumber(this.tlvType);
} catch (EncoderException e) {
throw new ComponentException("IdentifierWrapper.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -297,14 +317,18 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws ComponentException {
// 检查TLV值是否有效
if(!TLV.isValidIdentifierType(block.getType())){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("IdentifierWrapper.wireDecode: "+e.getMessage());
}
// 判断是否有且仅有一个子TLV
if(block.getSubElements().length()!=1){
+23 -6
View File
@@ -43,25 +43,38 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.interestLifeTime);
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.interestLifeTime);
} catch (EncoderException e) {
throw new ComponentException("InterestLifeTime.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("InterestLifeTime.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvInterestLifeTime));
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvInterestLifeTime));
} catch (EncoderException e) {
throw new ComponentException("InterestLifeTime.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -76,7 +89,7 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvInterestLifeTime))){
return false;
@@ -84,7 +97,11 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
// 读取 value
long value= 0;
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (TLVException | VlIntException e) {
throw new ComponentException("InterestLifeTime.wireDecode: "+e.getMessage());
}
if(value<0){
return false;
}
+26 -9
View File
@@ -19,7 +19,7 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
* 根据一个 TLV Block 创建一个 KeyLocator
* @param block
*/
public KeyLocator(Block block){
public KeyLocator(Block block) throws ComponentException {
this.buildKeyLocatorByBlock(block);
}
@@ -52,7 +52,7 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
* @param block
* @return
*/
public boolean buildKeyLocatorByBlock(Block block){
public boolean buildKeyLocatorByBlock(Block block) throws ComponentException {
return this.wireDecode(block);
}
@@ -105,24 +105,37 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=this.identifier.wireEncode(encoder);
int tmplen= 0;
try {
tmplen = this.identifier.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -137,14 +150,18 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws ComponentException {
// 检查Type是否正确
if(!TLV.isValidIdentifierType(block.getType())){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("KeyLocator.wireDecode: "+e.getMessage());
}
// 判断是否有且仅有一个子TLV
if(block.getSubElements().length()!=1){
+12 -3
View File
@@ -43,7 +43,7 @@ public class MustBeRefresh implements TlvComponentBase,InitialAble, IEncodingAbl
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
// 这是一个存在性组件,所以如果值为false,则无需编码
if(!this.mustBeRefresh){
return -1;
@@ -52,14 +52,23 @@ public class MustBeRefresh implements TlvComponentBase,InitialAble, IEncodingAbl
int totalLength=0;
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
int tmplen= 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("MustBeRefresh.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMustBeRefresh));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMustBeRefresh));
} catch (EncoderException e) {
throw new ComponentException("MustBeRefresh.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -57,7 +57,7 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
// 如果可变非受保护区没有任何子 TLV,则可以去掉可变非受保护区 TLV
if(this.blocks.length()==0){
return -1;
@@ -67,7 +67,12 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen=encoder.prependBlock(this.blocks.getBlock(i));
int tmplen= 0;
try {
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
} catch (EncoderException e) {
throw new ComponentException("MutableDangerousField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -75,14 +80,23 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
}
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
int tmplen= 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("MutableDangerousField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
} catch (EncoderException e) {
throw new ComponentException("MutableDangerousField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -97,15 +111,19 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("MutableDangerousField.wireDecode: "+e.getMessage());
}
this.blocks=block.getSubElements();
+29 -8
View File
@@ -24,16 +24,25 @@ public class MutableField implements TlvComponentBase,IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV—VALUE
int tmplen=this.mutableDangerousField.wireEncode(encoder);
int tmplen= 0;
try {
tmplen = this.mutableDangerousField.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("MutableField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
tmplen=this.mutableProtectField.wireEncode(encoder);
try {
tmplen=this.mutableProtectField.wireEncode(encoder);
} catch (EncoderException e) {
throw new ComponentException("MutableField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -45,14 +54,22 @@ public class MutableField implements TlvComponentBase,IEncodingAble {
}
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("MutableField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMutableField));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMutableField));
} catch (EncoderException e) {
throw new ComponentException("MutableField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -67,15 +84,19 @@ public class MutableField implements TlvComponentBase,IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvMutableField))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("MutableField.wireDecode: "+e.getMessage());
}
for (int i = 0; i < block.getSubElements().length(); i++) {
@@ -57,7 +57,7 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
// 如果没有任何子 TLV,则可以去掉它
if(this.blocks.length()==0){
return -1;
@@ -67,7 +67,12 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen=encoder.prependBlock(this.blocks.getBlock(i));
int tmplen= 0;
try {
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
} catch (EncoderException e) {
throw new ComponentException("MutableProtectField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -75,14 +80,23 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
}
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
int tmplen= 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("MutableProtectField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
} catch (EncoderException e) {
throw new ComponentException("MutableProtectField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -97,15 +111,19 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("MutableProtectField.wireDecode: "+e.getMessage());
}
this.blocks=block.getSubElements();
+23 -6
View File
@@ -43,25 +43,38 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.nackReason);
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.nackReason);
} catch (EncoderException e) {
throw new ComponentException("NackHeader.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("NackHeader.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvNackHeader));
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvNackHeader));
} catch (EncoderException e) {
throw new ComponentException("NackHeader.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -76,7 +89,7 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvNackHeader))){
return false;
@@ -84,7 +97,11 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
// 读取 value
long value= 0;
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (TLVException | VlIntException e) {
throw new ComponentException("NackHeader.wireDecode: "+e.getMessage());
}
if(value<0){
return false;
}
+23 -6
View File
@@ -53,25 +53,38 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.nonce);
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.nonce);
} catch (EncoderException e) {
throw new ComponentException("Nonce.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("Nonce.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvNonce));
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvNonce));
} catch (EncoderException e) {
throw new ComponentException("Nonce.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -86,7 +99,7 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvNonce))){
return false;
@@ -94,7 +107,11 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble {
// 读取 value
long value= 0;
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (TLVException | VlIntException e) {
throw new ComponentException("Nonce.wireDecode: "+e.getMessage());
}
if(value<0){
return false;
}
+17 -7
View File
@@ -63,25 +63,38 @@ public class Payload implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependByteArray(this.value,new SizeT(this.value.length));
int tmplen= 0;
try {
tmplen = encoder.prependByteArray(this.value,new SizeT(this.value.length));
} catch (EncoderException e) {
throw new ComponentException("Payload.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("Payload.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvPayload));
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvPayload));
} catch (EncoderException e) {
throw new ComponentException("Payload.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -98,9 +111,6 @@ public class Payload implements TlvComponentBase,InitialAble, IEncodingAble {
}
// 存储Value
// int len=block.getLength().getVlIntValue2Int();
// byte[] bytes=new byte[len];
// System.arraycopy(block.getValue(),0,bytes,0,len);
this.setValue(block.getValue());
return true;
}
+25 -7
View File
@@ -57,7 +57,7 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
// 如果只读区没有任何子 TLV,则可以去掉只读区 TLV
if(this.blocks.length()==0){
return 0;
@@ -67,7 +67,12 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen=encoder.prependBlock(this.blocks.getBlock(i));
int tmplen= 0;
try {
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
} catch (EncoderException e) {
throw new ComponentException("ReadOnlyField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -75,14 +80,23 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
}
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
int tmplen= 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("ReadOnlyField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
} catch (EncoderException e) {
throw new ComponentException("ReadOnlyField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -97,15 +111,19 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("ReadOnlyField.wireDecode: "+e.getMessage());
}
this.blocks=block.getSubElements();
+41 -14
View File
@@ -102,7 +102,7 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
// 如果签名区没有签名,则可以直接去掉签名区 TLV,因为它不是必须的
if(signatures.length()==0){
return -1;
@@ -112,12 +112,21 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
// 反向遍历,可以保证解码的时候正向输出
for(int i=signatures.length()-1;i>=0;i--){
Signature newSignature=signatures.getElement(i);
int tmplen=newSignature.getSigValue().wireEncode(encoder);
int tmplen= 0;
try {
tmplen = newSignature.getSigValue().wireEncode(encoder);
} catch (EncoderException e) {
throw new ComponentException("SignatureField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
tmplen=newSignature.getSigInfo().wireEncode(encoder);
try {
tmplen=newSignature.getSigInfo().wireEncode(encoder);
} catch (EncoderException e) {
throw new ComponentException("SignatureField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -125,14 +134,23 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
}
// 编码签名区 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
int tmplen= 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码签名区 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureField));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureField));
} catch (EncoderException e) {
throw new ComponentException("SignatureField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -147,15 +165,19 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 TYPE 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureField))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("SignatureField.wireDecode: "+e.getMessage());
}
ElementContainer subElements=block.getSubElements();
int subElementNum=subElements.length();
@@ -169,12 +191,17 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
// 解析每一个签名
for (int i = 0; i < subElementNum; i+=2) {
// 第奇数个子 TLV 解析为 SignatureInfo
SignatureInfo signatureInfo=new SignatureInfo(subElements.getBlock(i));
// 第偶数个子 TLV 解析为 SignatureValue
SignatureValue signatureValue=new SignatureValue(subElements.getBlock(i+1));
// 构造Signature
Signature signature=new Signature(signatureInfo,signatureValue);
signatureContainer.addElement(signature);
SignatureInfo signatureInfo= null;
try {
signatureInfo = new SignatureInfo(subElements.getBlock(i));
// 第偶数个子 TLV 解析为 SignatureValue
SignatureValue signatureValue=new SignatureValue(subElements.getBlock(i+1));
// 构造Signature
Signature signature=new Signature(signatureInfo,signatureValue);
signatureContainer.addElement(signature);
} catch (VlIntException e) {
throw new ComponentException("SignatureField.wireDecode: "+e.getMessage());
}
}
// 赋值
signatures.clear();
+39 -13
View File
@@ -38,7 +38,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* 根据 TLV Block 创建一个 SignatureInfo
* @param block
*/
public SignatureInfo(Block block) throws VlIntException {
public SignatureInfo(Block block) throws ComponentException {
this.buildSignatureInfoByBlock(block);
}
@@ -74,7 +74,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* @param block
* @return
*/
public boolean buildSignatureInfoByBlock(Block block) throws VlIntException {
public boolean buildSignatureInfoByBlock(Block block) throws ComponentException {
return this.wireDecode(block);
}
@@ -100,32 +100,50 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
if(!this.keyLocator.isValid()){
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
int tmplen=this.keyLocator.wireEncode(encoder);
int tmplen= 0;
try {
tmplen = this.keyLocator.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
}
int tmplen=this.signatureType.wireEncode(encoder);
int tmplen= 0;
try {
tmplen = this.signatureType.wireEncode(encoder);
} catch (EncoderException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
} catch (EncoderException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -140,15 +158,19 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureInfo))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new ComponentException("SignatureInfo.wireDecode: "+e.getMessage());
}
boolean findSignatureType=false;
@@ -163,8 +185,12 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
}
switch (type){
case TLV.TlvSignatureType:
if(!this.signatureType.wireDecode(cacheBlock)){
return false;
try {
if(!this.signatureType.wireDecode(cacheBlock)){
return false;
}
} catch (VlIntException e) {
throw new ComponentException("SignatureInfo.wireDecode: "+e.getMessage());
}
findSignatureType=true;
case TLV.TlvKeyLocator:
@@ -172,7 +198,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
return false;
}
default:
throw new Error("SignatureInfo.wireDecode: SignatureInfo sub element type error");
throw new ComponentException("SignatureInfo.wireDecode: SignatureInfo sub element type error");
// return false;
}
+24 -11
View File
@@ -57,7 +57,7 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
* 根据 TLV Block 创建一个 SignatureType
* @param block
*/
public SignatureType(Block block) throws VlIntException {
public SignatureType(Block block) throws ComponentException {
this.buildSignatureTypeByBlock(block);
}
@@ -83,7 +83,7 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
* @param block
* @return
*/
public boolean buildSignatureTypeByBlock(Block block) throws VlIntException {
public boolean buildSignatureTypeByBlock(Block block) throws ComponentException {
return this.wireDecode(block);
}
@@ -101,25 +101,38 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm());
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm());
} catch (EncoderException e) {
throw new ComponentException("SignatureType.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureType.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureType));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureType));
} catch (EncoderException e) {
throw new ComponentException("SignatureType.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -134,7 +147,7 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureType))){
return false;
@@ -142,11 +155,11 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
// 尝试从 TLE-VALUE 中读取出一个非负整数
long result= 0;
// try {
try {
result = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
// } catch (VlIntException e) {
// throw new Error("SignatureType.wireDecode: Invalid SignatureType TLV");
// }
} catch (TLVException | VlIntException e) {
throw new ComponentException("SignatureType.wireDecode: " + e.getMessage());
}
if(result<0){
return false;
}
+24 -11
View File
@@ -27,7 +27,7 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* 根据 TLV Block 创建一个 SignatureValue
* @param block
*/
public SignatureValue(Block block) throws VlIntException {
public SignatureValue(Block block) throws ComponentException {
this.buildSignatureValueByBlock(block);
}
@@ -47,7 +47,7 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* @param block
* @return
*/
public boolean buildSignatureValueByBlock(Block block) throws VlIntException {
public boolean buildSignatureValueByBlock(Block block) throws ComponentException {
return this.wireDecode(block);
}
@@ -65,24 +65,37 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependByteArray(this.value,new SizeT(this.value.length));
int tmplen= 0;
try {
tmplen = encoder.prependByteArray(this.value,new SizeT(this.value.length));
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureValue));
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureValue));
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -97,18 +110,18 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureValue))){
return false;
}
int len= 0;
// try {
try {
len = block.getLength().getVlIntValue2Int();
// } catch (VlIntException e) {
// e.printStackTrace();
// }
} catch (VlIntException e) {
throw new ComponentException("SignatureValue.wireDecode: "+e.getMessage());
}
this.value=new byte[len];
System.arraycopy(block.getValue(),0,this.value,0,len);
return true;
+24 -11
View File
@@ -22,7 +22,7 @@ public class TTL implements IEncodingAble {
public TTL(){
}
public TTL(Block block) throws VlIntException {
public TTL(Block block) throws ComponentException {
buildTTLByBlock(block);
}
@@ -31,7 +31,7 @@ public class TTL implements IEncodingAble {
* @param block
* @return
*/
public boolean buildTTLByBlock(Block block) throws VlIntException {
public boolean buildTTLByBlock(Block block) throws ComponentException {
return this.wireDecode(block);
}
@@ -57,24 +57,37 @@ public class TTL implements IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmpLen=encoder.prependNonNegativeInteger(this.ttl);
int tmpLen= 0;
try {
tmpLen = encoder.prependNonNegativeInteger(this.ttl);
} catch (EncoderException e) {
throw new ComponentException("TTL.wireEncode: "+e.getMessage());
}
if(tmpLen<=0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-LENGTH
tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("TTL.wireEncode: "+e.getMessage());
}
if(tmpLen<=0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvTTL));
try {
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvTTL));
} catch (EncoderException e) {
throw new ComponentException("TTL.wireEncode: "+e.getMessage());
}
if(tmpLen<=0){
return -1;
}
@@ -89,17 +102,17 @@ public class TTL implements IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查Type是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvTTL))){
return false;
}
long value= 0;
// try {
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
// } catch (VlIntException e) {
// e.printStackTrace();
// }
} catch (TLVException | VlIntException e) {
throw new ComponentException("TTL.wireDecode: "+e.getMessage());
}
if(value<0){
return false;
}
+41 -12
View File
@@ -1,5 +1,6 @@
package encoding;
import component.ComponentException;
import util.ByteHelper;
import util.ByteHelperException;
@@ -40,7 +41,12 @@ public class Block {
}
// 读取type
VlInt tlvType=TLV.readType(buffer,new VlInt(0));
VlInt tlvType= null;
try {
tlvType = TLV.readType(buffer,new VlInt(0));
} catch (TLVException e) {
throw new BlockException("createBlockByBuffer: tlvType read error & "+e.getMessage());
}
if(tlvType==null){
throw new BlockException("createBlockByBuffer: tlvType read error");
}
@@ -49,8 +55,8 @@ public class Block {
VlInt tlvLength= null;
try {
tlvLength = TLV.readVarNumber(buffer,new VlInt(tlvType.size()));
} catch (ByteHelperException e) {
throw new BlockException("createBlockByBuffer: tlvLength read error");
} catch (TLVException e) {
throw new BlockException("createBlockByBuffer: tlvLength read error"+e.getMessage());
}
if(tlvLength==null){
throw new BlockException("createBlockByBuffer: tlvLength read error");
@@ -227,7 +233,7 @@ public class Block {
* 2. 如果TLV的value不为空,且不存在嵌套的子TLV,则会抛出错误
* @return
*/
public boolean parseSubElements() {
public boolean parseSubElements() throws BlockException {
if((this.value.length==0)||this.elements.length()!=0){
return true;
}
@@ -235,7 +241,12 @@ public class Block {
// 循环解析所有的一级子TLV
for (int start = 0; start < this.value.length; start++) {
// 读取 TLV-TYPE
VlInt tlvType=TLV.readType(this.value,new VlInt(start));
VlInt tlvType= null;
try {
tlvType = TLV.readType(this.value,new VlInt(start));
} catch (TLVException e) {
throw new BlockException("parseSubElements"+e.getMessage());
}
if(tlvType==null){
this.clearElements();
return false;
@@ -246,8 +257,8 @@ public class Block {
VlInt tlvLength= null;
try {
tlvLength = TLV.readVarNumber(this.value,new VlInt(start));
} catch (ByteHelperException e) {
e.printStackTrace();
} catch (TLVException e) {
throw new BlockException("parseSubElements"+e.getMessage());
}
if(tlvType==null){
this.clearElements();
@@ -331,10 +342,14 @@ public class Block {
* @param encoder
* @return
*/
public int encode(Encoder encoder) throws EncoderException {
public int encode(Encoder encoder) throws ComponentException {
// 如果已经有raw,则说明编码过,或者是解码而来的,不用在此Encode
if(this.hasRaw()){
return encoder.prependByteArray(this.raw,new SizeT(this.raw.length));
try {
return encoder.prependByteArray(this.raw,new SizeT(this.raw.length));
} catch (EncoderException e) {
throw new ComponentException("encode: "+e.getMessage());
}
}
int len=0;
@@ -342,7 +357,12 @@ public class Block {
// 编码 TLV-VALUE
if(this.hasValue()){
// 如果设置了 value ,则直接使用
int tmplen=encoder.appendByteArray(this.value,new SizeT(this.value.length));
int tmplen= 0;
try {
tmplen = encoder.appendByteArray(this.value,new SizeT(this.value.length));
} catch (EncoderException e) {
throw new ComponentException("encode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -359,14 +379,23 @@ public class Block {
}
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(len));
int tmplen= 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(len));
} catch (EncoderException e) {
throw new ComponentException("encode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
len+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(this.tlvType);
try {
tmplen=encoder.prependVarNumber(this.tlvType);
} catch (EncoderException e) {
throw new ComponentException("encode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
+5 -2
View File
@@ -1,5 +1,8 @@
package encoding;
import component.ComponentException;
import packet.PacketException;
/*
* @Author: Wang Feng
* @Description:
@@ -15,13 +18,13 @@ public interface IEncodingAble {
* @param encoder
* @return
*/
public int wireEncode(Encoder encoder) throws EncoderException, BlockException;
public int wireEncode(Encoder encoder) throws ComponentException, PacketException;
/**
* 线速解码,将一个TLV解码成一个网络包
* @param block
* @return
*/
public boolean wireDecode(Block block) throws VlIntException;
public boolean wireDecode(Block block) throws ComponentException, PacketException;
}
+18 -20
View File
@@ -73,34 +73,38 @@ public class TLV {
* @param start
* @return
*/
public static VlInt readVarNumber(byte[] buffer,VlInt start) throws ByteHelperException {
public static VlInt readVarNumber(byte[] buffer,VlInt start) throws TLVException {
VlInt length=new VlInt(buffer.length);
if(start.compareTo(length)>=0){
return null;
throw new TLVException("readVarNumber: start>=buffer.length");
}
int firstbyte= 0;
firstbyte = ByteHelper.uint8ToInt(ByteHelper.getFirstByte(buffer,start));
try {
firstbyte = ByteHelper.uint8ToInt(ByteHelper.getFirstByte(buffer,start));
} catch (ByteHelperException e) {
throw new TLVException("readVarNumber: "+e.getMessage());
}
if(firstbyte<241){
return new VlInt(firstbyte);
}else if(firstbyte==241){
if(length.subtract(start)<3){
return null;
throw new TLVException("readVarNumber: firstByte==241 & buffer.length<3");
}
return new VlInt(ByteHelper.getLenBytes(buffer,start,3));
}else if(firstbyte==242){
if(length.subtract(start)<5){
return null;
throw new TLVException("readVarNumber: firstByte==242 & buffer.length<5");
}
return new VlInt(ByteHelper.getLenBytes(buffer,start,5));
}else if(firstbyte==243){
if(length.subtract(start)<9){
return null;
throw new TLVException("readVarNumber: firstByte==243 & buffer.length<9");
}
return new VlInt(ByteHelper.getLenBytes(buffer,start,9));
}else{ //MIN1.0中算越界
return null;
throw new TLVException("readVarNumber: firstByte>243");
}
}
@@ -110,16 +114,11 @@ public class TLV {
* @param start
* @return
*/
public static VlInt readType(byte[] buffer,VlInt start){
VlInt tlvType= null;
try {
tlvType = readVarNumber(buffer,start);
} catch (ByteHelperException e) {
e.printStackTrace();
}
public static VlInt readType(byte[] buffer,VlInt start) throws TLVException {
VlInt tlvType = readVarNumber(buffer,start);
if(tlvType==null||tlvType.isEqual(0)||tlvType.compareTo(MaxTlvNum)>0){
return null;
throw new TLVException("readType: tlvType invalid");
}
return tlvType;
@@ -129,12 +128,12 @@ public class TLV {
* 从字节数组的指定位置中读取一个非负整型
* todo: 越界问题
*/
public static long readNonNegativeInteger(byte[] buffer,int start,int end){
public static long readNonNegativeInteger(byte[] buffer,int start,int end) throws TLVException {
if(start==end){
return -1;
throw new TLVException("readNonNegativeInteger: start==end.");
}
if((start<0)||(end>buffer.length)){
return -1;
throw new TLVException("readNonNegativeInteger: start<0 || end>buffer.length.");
}
int len=end-start;
byte[] bytes=new byte[len];
@@ -148,7 +147,7 @@ public class TLV {
}else if(len==8){
return ByteHelper.uint64ToLong(bytes);
}else{
return -1;
throw new TLVException("readNonNegativeInteger: invalid length ");
}
}
@@ -185,5 +184,4 @@ public class TLV {
return tlvType.compareTo(0)>0 && tlvType.compareTo(50)<0;
}
//todo: 错误处理
}
+14
View File
@@ -0,0 +1,14 @@
package encoding;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 21:02 2021/3/23
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class TLVException extends Exception{
public TLVException(String msg){
super("TLV."+msg);
}
}
+33 -11
View File
@@ -1,5 +1,6 @@
package packet;
import component.ComponentException;
import component.IdentifierWrapper;
import component.Payload;
import component.TTL;
@@ -51,13 +52,18 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public boolean fillDataToFields() throws EncoderException, BlockException {
public boolean fillDataToFields() throws PacketException {
// 可变区 => 受保护区(无)
// 填充可变区 => 非受保护区
// 首先清除所有的Block
this.minPacket.mutableField.mutableDangerousField.clearBlocks();
Block block=new SelfEncodingBase().selfWireEncode(this.ttl);
Block block= null;
try {
block = new SelfEncodingBase().selfWireEncode(this.ttl);
} catch (EncoderException | BlockException e) {
throw new PacketException("CPacket.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -66,7 +72,11 @@ public class CPacket implements InteractWithField, IEncodingAble {
// 填充只读区
// 首先清除所有的Block
this.minPacket.readOnlyField.clearBlocks();
block=new SelfEncodingBase().selfWireEncode(this.payload);
try {
block=new SelfEncodingBase().selfWireEncode(this.payload);
} catch (EncoderException | BlockException e) {
throw new PacketException("CPacket.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -90,7 +100,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public boolean extraDataFromFields() throws VlIntException {
public boolean extraDataFromFields() throws PacketException {
// 提取 Payload
Block payloadBlock=this.minPacket.readOnlyField.getBlockByType(new VlInt(TLV.TlvPayload));
if(payloadBlock==null){
@@ -105,8 +115,12 @@ public class CPacket implements InteractWithField, IEncodingAble {
if(ttlBlock==null){
return false;
}
if(this.ttl.wireDecode(ttlBlock)){
return false;
try {
if(this.ttl.wireDecode(ttlBlock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("CPacket.extraDataFromFields: "+e.getMessage());
}
// 提取标识
@@ -128,7 +142,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
public int wireEncode(Encoder encoder) throws PacketException {
// 首先将属性填充到分区当中
if(!this.fillDataToFields()){
return 0;
@@ -137,7 +151,11 @@ public class CPacket implements InteractWithField, IEncodingAble {
// 指定包类型
this.minPacket.packetType=new VlInt(TLV.TlvPacketMINCommon);
return this.minPacket.wireEncode(encoder);
try {
return this.minPacket.wireEncode(encoder);
} catch (EncoderException e) {
throw new PacketException("CPacket.wireEncode: "+e.getMessage());
}
}
/**
@@ -146,9 +164,13 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
if(!this.minPacket.wireDecode(block)){
return false;
public boolean wireDecode(Block block) throws PacketException {
try {
if(!this.minPacket.wireDecode(block)){
return false;
}
} catch (VlIntException e) {
throw new PacketException("CPacket.wireDecode: "+e.getMessage());
}
return this.extraDataFromFields();
}
+38 -13
View File
@@ -68,7 +68,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public boolean fillDataToFields() throws EncoderException, BlockException {
public boolean fillDataToFields() throws PacketException {
/////////////////////////////////////////////////////////////
//// 填充可变区
// { => 可变区
@@ -84,7 +84,12 @@ public class Data implements IEncodingAble,InteractWithField {
this.minPacket.mutableField.mutableProtectField.clearBlocks();
// CongestionMark
Block block=new SelfEncodingBase().selfWireEncode(this.congestionMark);
Block block= null;
try {
block = new SelfEncodingBase().selfWireEncode(this.congestionMark);
} catch (EncoderException | BlockException e) {
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -104,7 +109,11 @@ public class Data implements IEncodingAble,InteractWithField {
// FreshnessPeriod
if(this.freshnessPeriod.isInitial()){
block=new SelfEncodingBase().selfWireEncode(this.freshnessPeriod);
try {
block=new SelfEncodingBase().selfWireEncode(this.freshnessPeriod);
} catch (EncoderException | BlockException e) {
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -113,7 +122,11 @@ public class Data implements IEncodingAble,InteractWithField {
// NackHeader
if(this.nackHeader.isInitial()){
block=new SelfEncodingBase().selfWireEncode(this.nackHeader);
try {
block=new SelfEncodingBase().selfWireEncode(this.nackHeader);
} catch (EncoderException | BlockException e) {
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -121,7 +134,11 @@ public class Data implements IEncodingAble,InteractWithField {
}
// Payload
block=new SelfEncodingBase().selfWireEncode(this.payload);
try {
block=new SelfEncodingBase().selfWireEncode(this.payload);
} catch (EncoderException | BlockException e) {
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -146,7 +163,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public boolean extraDataFromFields() throws VlIntException {
public boolean extraDataFromFields() throws PacketException {
/////////////////////////////////////////////////////////////
//// 解析可变区
// { => 可变区
@@ -160,8 +177,12 @@ public class Data implements IEncodingAble,InteractWithField {
/////////////////////////////////////////////////////////////
Block block=this.minPacket.mutableField.mutableProtectField.getBlockByType(new VlInt(TLV.TlvCongestionMark));
if(block!=null){
if(!this.congestionMark.wireDecode(block)){
return false;
try {
if(!this.congestionMark.wireDecode(block)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Data.extraDataFromFields: "+e.getMessage());
}
}
@@ -184,8 +205,12 @@ public class Data implements IEncodingAble,InteractWithField {
}
switch (type){
case TLV.TlvFreshnessPeriod:
if(!this.freshnessPeriod.wireDecode(newblock)){
return false;
try {
if(!this.freshnessPeriod.wireDecode(newblock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Data.extraDataFromFields: "+e.getMessage());
}
case TLV.TlvPayload:
if(!this.payload.wireDecode(newblock)){
@@ -216,10 +241,10 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
public int wireEncode(Encoder encoder) throws PacketException {
// 首先将各项属性填充到分区当中
if(this.fillDataToFields()){
return 0;
return -1;
}
// 指定包类型
@@ -234,7 +259,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws PacketException {
if(!this.minPacket.wireDecode(block)){
return false;
}
+2 -2
View File
@@ -16,11 +16,11 @@ public interface InteractWithField {
* 将包的属性填充到网络包的四个分区当中
* @return
*/
boolean fillDataToFields() throws EncoderException, BlockException;
boolean fillDataToFields() throws EncoderException, BlockException, PacketException;
/**
* 从网络包的四个分区当中提取出特定网络包的相关属性
* @return
*/
boolean extraDataFromFields() throws VlIntException;
boolean extraDataFromFields() throws VlIntException, PacketException;
}
+103 -82
View File
@@ -104,96 +104,101 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public boolean fillDataToFields() throws EncoderException, BlockException {
/////////////////////////////////////////////////////////////
//// 填充可变区
/////////////////////////////////////////////////////////////
public boolean fillDataToFields() throws PacketException {
try {
/////////////////////////////////////////////////////////////
//// 填充可变区
/////////////////////////////////////////////////////////////
// 填充可变受保护区
this.minPacket.mutableField.mutableProtectField.clearBlocks();
// 填充可变受保护区
this.minPacket.mutableField.mutableProtectField.clearBlocks();
// CongestionMark
Block block=new SelfEncodingBase().selfWireEncode(this.congestionMark);
if(block==null){
return false;
}
this.minPacket.mutableField.mutableProtectField.addBlock(block);
// CongestionMark
Block block = new SelfEncodingBase().selfWireEncode(this.congestionMark);
if (block == null) {
return false;
}
this.minPacket.mutableField.mutableProtectField.addBlock(block);
// 填充可变非受保护区(无)
// 填充可变非受保护区(无)
/////////////////////////////////////////////////////////////
//// 填充只读区
/////////////////////////////////////////////////////////////
this.minPacket.readOnlyField.clearBlocks();
/////////////////////////////////////////////////////////////
//// 填充只读区
/////////////////////////////////////////////////////////////
this.minPacket.readOnlyField.clearBlocks();
// CanBePrefix
if(this.canBePrefix.getCanBePrefix()){
block=new SelfEncodingBase().selfWireEncode(this.canBePrefix);
if(block==null){
// CanBePrefix
if (this.canBePrefix.getCanBePrefix()) {
block = new SelfEncodingBase().selfWireEncode(this.canBePrefix);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// MustBeRefresh
if (this.mustBeRefresh.getMustBeRefresh()) {
block = new SelfEncodingBase().selfWireEncode(this.mustBeRefresh);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// InterestLifeTime
if (!this.interestLifeTime.isInitial()) {
// 默认生存期为4s
this.interestLifeTime.setInterestLifeTime(4000);
}
block = new SelfEncodingBase().selfWireEncode(this.interestLifeTime);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// MustBeRefresh
if(this.mustBeRefresh.getMustBeRefresh()){
block=new SelfEncodingBase().selfWireEncode(this.mustBeRefresh);
if(block==null){
// Nonce
if (!this.nonce.isInitial()) {
// 如果没有指定随机数,随机生成一个随机数
this.nonce.refreshNonce();
}
block = new SelfEncodingBase().selfWireEncode(this.nonce);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// InterestLifeTime
if(!this.interestLifeTime.isInitial()){
// 默认生存期为4s
this.interestLifeTime.setInterestLifeTime(4000);
}
block=new SelfEncodingBase().selfWireEncode(this.interestLifeTime);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
// HopLimit
if (this.hopLimit.isInitial()) {
block = new SelfEncodingBase().selfWireEncode(this.hopLimit);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// Nonce
if(!this.nonce.isInitial()){
// 如果没有指定随机数,随机生成一个随机数
this.nonce.refreshNonce();
}
block=new SelfEncodingBase().selfWireEncode(this.nonce);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
// HopLimit
if(this.hopLimit.isInitial()){
block=new SelfEncodingBase().selfWireEncode(this.hopLimit);
if(block==null){
// Payload
block = new SelfEncodingBase().selfWireEncode(this.payload);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// Payload
block=new SelfEncodingBase().selfWireEncode(this.payload);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
/////////////////////////////////////////////////////////////
//// 填充标识区
/////////////////////////////////////////////////////////////
this.minPacket.identifierField.clearIdentifiers();
IdentifierWrapper identifierWrapper =
new IdentifierWrapper().createContentInterestIdentifierByComponents(this.name.getComponents());
if (identifierWrapper == null) {
return false;
}
this.minPacket.identifierField.addIdentifier(identifierWrapper);
/////////////////////////////////////////////////////////////
//// 填充标识区
/////////////////////////////////////////////////////////////
this.minPacket.identifierField.clearIdentifiers();
IdentifierWrapper identifierWrapper=
new IdentifierWrapper().createContentInterestIdentifierByComponents(this.name.getComponents());
if(identifierWrapper==null){
return false;
}
this.minPacket.identifierField.addIdentifier(identifierWrapper);
return true;
return true;
}catch (EncoderException | BlockException e){
throw new PacketException("Interest.fillDataToFields: "+e.getMessage());
}
}
/**
@@ -201,7 +206,7 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public boolean extraDataFromFields() throws VlIntException {
public boolean extraDataFromFields() throws PacketException {
/////////////////////////////////////////////////////////////
//// 解析可变区
// { => 可变区
@@ -215,8 +220,12 @@ public class Interest implements InteractWithField,IEncodingAble{
/////////////////////////////////////////////////////////////
Block block=this.minPacket.mutableField.mutableProtectField.getBlockByType(new VlInt(TLV.TlvCongestionMark));
if(block!=null){
if(!this.congestionMark.wireDecode(block)){
return false;
try {
if(!this.congestionMark.wireDecode(block)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
}
}
@@ -251,16 +260,28 @@ public class Interest implements InteractWithField,IEncodingAble{
return false;
}
case TLV.TlvInterestLifeTime:
if(!this.interestLifeTime.wireDecode(newblock)){
return false;
try {
if(!this.interestLifeTime.wireDecode(newblock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
}
case TLV.TlvNonce:
if(!this.nonce.wireDecode(newblock)){
return false;
try {
if(!this.nonce.wireDecode(newblock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
}
case TLV.TlvHopLimit:
if(!this.hopLimit.wireDecode(newblock)){
return false;
try {
if(!this.hopLimit.wireDecode(newblock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
}
case TLV.TlvPayload:
if(!this.payload.wireDecode(newblock)){
@@ -292,10 +313,10 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
public int wireEncode(Encoder encoder) throws PacketException {
// 首先将各项属性填充到分区当中
if(!this.fillDataToFields()){
return 0;
return -1;
}
// 指定包类型
@@ -310,7 +331,7 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws PacketException {
if(!this.minPacket.wireDecode(block)){
return false;
}
+71 -25
View File
@@ -1,9 +1,6 @@
package packet;
import component.IdentifierField;
import component.MutableField;
import component.ReadOnlyField;
import component.SignatureField;
import component.*;
import encoding.*;
/*
@@ -35,7 +32,7 @@ public class MINPacket implements IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws PacketException {
if(!this.isPacketValid()){
return -1;
}
@@ -44,42 +41,67 @@ public class MINPacket implements IEncodingAble {
// 编码 TLV-VALUE
// 可变区
int tmplen=this.mutableField.wireEncode(encoder);
int tmplen= 0;
try {
tmplen = this.mutableField.wireEncode(encoder);
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 只读区
tmplen=this.readOnlyField.wireEncode(encoder);
try {
tmplen=this.readOnlyField.wireEncode(encoder);
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 签名区
tmplen=this.signatureField.wireEncode(encoder);
try {
tmplen=this.signatureField.wireEncode(encoder);
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 标识区
tmplen=this.identifierField.wireEncode(encoder);
try {
tmplen=this.identifierField.wireEncode(encoder);
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码包 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码包 TLV-TYPE
tmplen=encoder.prependVarNumber(this.packetType);
try {
tmplen=encoder.prependVarNumber(this.packetType);
} catch (EncoderException e) {
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -98,14 +120,18 @@ public class MINPacket implements IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws PacketException {
// 首先判断包是否有效
if(!block.isValid()){
return false;
}
// 首先解析子 Block
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
// 标记,用来记录是否满足必要条件
@@ -124,36 +150,56 @@ public class MINPacket implements IEncodingAble {
switch (judgeType){
case TLV.TlvIdentifierField: // 目的标识区
// 解析标识区,保证其中至少存在一个标识
if(!cacheBlock.parseSubElements()){
return false;
try {
if(!cacheBlock.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
if(!cacheBlock.hasSubElement()){
return false;
}
// 解析标识区
if(!this.identifierField.wireDecode(cacheBlock)){
return false;
try {
if(!this.identifierField.wireDecode(cacheBlock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
// 标示解析到了至少一个以上的合法标识
existIdentifier=true;
case TLV.TlvSignatureField: // 签名区
// 解析签名区,保证如果存在签名,则签名均符合格式
if(!this.signatureField.wireDecode(cacheBlock)){
return false;
try {
if(!this.signatureField.wireDecode(cacheBlock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
case TLV.TlvReadOnlyField: // 只读区
// 解析只读区
if(!this.readOnlyField.wireDecode(cacheBlock)){
return false;
try {
if(!this.readOnlyField.wireDecode(cacheBlock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
case TLV.TlvMutableField: // 可变区
// 解析可变区
if(!this.mutableField.wireDecode(cacheBlock)){
return false;
try {
if(!this.mutableField.wireDecode(cacheBlock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
default:
default:
return false;
}
+14
View File
@@ -0,0 +1,14 @@
package packet;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 10:59 2021/3/24
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class PacketException extends Exception {
public PacketException(String msg){
super("Packet-"+msg);
}
}
+25
View File
@@ -0,0 +1,25 @@
package encoding;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 20:59 2021/3/23
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class TLVTest {
@Test
public void testReadVarNumber() throws TLVException {
byte[] buf=new byte[10];
// 测试单个字节表示
buf[0]=(byte)240;
VlInt res=TLV.readVarNumber(buf,new VlInt(0));
assertTrue(res.isEqual(240));
// assertEquals();
}
}