📄 ruleset.java
字号:
}
}
class PT_A_CompException extends Exception {
/**
*
*/
private static final long serialVersionUID = 3928376356730099133L;
}
abstract class PT_A_Compr extends PT_A_Instr {
protected PT_A_Field field;
protected PT_A_Number number;
protected PT_O_Comp comp;
private static PT_A_Field createField(PT_Var f) throws PT_A_CompException {
if (f instanceof PT_V_Port)
return new PT_A_Port(f);
if (f instanceof PT_V_SrcPort)
return new PT_A_SrcPort(f);
if (f instanceof PT_V_DstPort)
return new PT_A_DstPort(f);
if (f instanceof PT_V_Proto)
return new PT_A_Proto(f);
if (f instanceof PT_V_TOS)
return new PT_A_TOS(f);
if (f instanceof PT_V_AS)
return new PT_A_AS(f);
if (f instanceof PT_V_SrcAS)
return new PT_A_SrcAS(f);
if (f instanceof PT_V_DstAS)
return new PT_A_DstAS(f);
if (f instanceof PT_V_Addr)
return new PT_A_Addr(f);
if (f instanceof PT_V_SrcAddr)
return new PT_A_SrcAddr(f);
if (f instanceof PT_V_DstAddr)
return new PT_A_DstAddr(f);
if (f instanceof PT_V_NextHop)
return new PT_A_NextHop(f);
if (f instanceof PT_V_If)
return new PT_A_If(f);
if (f instanceof PT_V_InIf)
return new PT_A_InIf(f);
if (f instanceof PT_V_OutIf)
return new PT_A_OutIf(f);
if (f instanceof PT_V_Prefix)
return new PT_A_Prefix(f);
if (f instanceof PT_V_SrcPrefix)
return new PT_A_SrcPrefix(f);
if (f instanceof PT_V_DstPrefix)
return new PT_A_DstPrefix(f);
throw new PT_A_CompException();
}
protected PT_A_Compr(PT_Var f, PT_O_Comp c) throws PT_A_CompException {
field = createField(f);
comp = c;
}
public PT_A_Compr(PT_Var f, PT_V_Number n, PT_O_Comp c)
throws PT_A_CompException {
this(f, c);
number = new PT_A_Number(n);
}
public String toString(int bl) {
return filler(bl) + toString() + "\n";
}
public String toString() {
return field.toString() + " " + comp.toString() + " "
+ number.toString();
}
public boolean exec(Flow flow) throws PT_ExecError {
Object o = field.getValue(flow);
if (o instanceof Vector) {
Enumeration e = ((Vector) o).elements();
while (e.hasMoreElements())
if (compare(e.nextElement()))
return true;
return false;
}
return compare(o);
}
protected boolean compare(Object value) throws PT_ExecError {
throw new PT_ExecError("BUG: Opcode `" + this.getClass().getName()
+ "' (" + this.toString() + ") not implemented yet");
}
}
class PT_A_In extends PT_A_Compr {
PT_V_Range range;
public PT_A_In(PT_Var f, PT_V_Range r, PT_O_In i) throws PT_A_CompException {
super(f, (PT_O_Comp) i);
range = r;
}
public String toString() {
return field.toString() + " " + comp.toString() + " "
+ range.toString();
}
protected boolean compare(Object value) throws PT_ExecError {
if (value == null)
return false;
if (!(value instanceof Long))
throw new PT_ExecError("BUG: PT_A_In: not a Long value: "
+ value.getClass().getName());
long val = ((Long) value).longValue();
return range.s <= val && val <= range.e;
}
}
class PT_A_InPrefix extends PT_A_Compr {
PT_V_VPrefix prefix;
public PT_A_InPrefix(PT_Var f, PT_V_VPrefix p, PT_O_In i)
throws PT_A_CompException {
super(f, (PT_O_Comp) i);
prefix = p;
}
public String toString() {
return field.toString() + " " + comp.toString() + " "
+ prefix.toString();
}
protected boolean compare(Object value) throws PT_ExecError {
if (value == null)
return false;
// do not reorder!
if (value instanceof Prefix)
return prefix.includes((Prefix) value);
if (value instanceof Address)
return prefix.consists((Address) value);
throw new PT_ExecError(
"BUG: PT_A_InPrefix: not a Address/Prefix value: "
+ value.getClass().getName());
}
}
class PT_A_Equal extends PT_A_Compr {
public PT_A_Equal(PT_Var f, PT_V_Number n, PT_O_Comp c)
throws PT_A_CompException {
super(f, n, c);
}
protected boolean compare(Object value) throws PT_ExecError {
if (value == null)
return false;
if (!(value instanceof Long))
throw new PT_ExecError("BUG: PT_A_Equal: not a Long value: "
+ value.getClass().getName());
return ((Long) value).longValue() == number.num.v;
}
}
class PT_A_NotEqual extends PT_A_Equal {
public PT_A_NotEqual(PT_Var f, PT_V_Number n, PT_O_Comp c)
throws PT_A_CompException {
super(f, n, c);
}
protected boolean compare(Object value) throws PT_ExecError {
return !super.compare(value);
}
}
class PT_A_EqualAddress extends PT_A_Compr {
PT_V_Address address;
public PT_A_EqualAddress(PT_Var f, PT_V_Address address, PT_O_Comp c)
throws PT_A_CompException {
super(f, c);
this.address = address;
}
public String toString() {
return field.toString() + " " + comp.toString() + " "
+ address.toString();
}
protected boolean compare(Object value) throws PT_ExecError {
if (value == null)
return false;
if (!(value instanceof String))
throw new PT_ExecError(
"BUG: PT_A_EqualAddress: not a String value: "
+ value.getClass().getName());
return ((String) value).equals(address.getValue());
}
}
class PT_A_NotEqualAddress extends PT_A_EqualAddress {
public PT_A_NotEqualAddress(PT_Var f, PT_V_Address address, PT_O_Comp c)
throws PT_A_CompException {
super(f, address, c);
}
protected boolean compare(Object value) throws PT_ExecError {
return !super.compare(value);
}
}
class PT_A_EqualPrefix extends PT_A_Compr {
PT_V_VPrefix prefix;
public PT_A_EqualPrefix(PT_Var f, PT_V_VPrefix prefix, PT_O_Comp c)
throws PT_A_CompException {
super(f, c);
this.prefix = prefix;
}
public String toString() {
return field.toString() + " " + comp.toString() + " "
+ prefix.toString();
}
protected boolean compare(Object value) throws PT_ExecError {
if (value == null)
return false;
if (!(value instanceof Prefix))
throw new PT_ExecError(
"BUG: PT_A_EqualPrefix: not a Prefix value: "
+ value.getClass().getName());
return ((Prefix) value).equals(prefix);
}
}
class PT_A_NotEqualPrefix extends PT_A_EqualPrefix {
public PT_A_NotEqualPrefix(PT_Var f, PT_V_VPrefix prefix, PT_O_Comp c)
throws PT_A_CompException {
super(f, prefix, c);
}
protected boolean compare(Object value) throws PT_ExecError {
return !super.compare(value);
}
}
class PT_A_Less extends PT_A_Compr {
public PT_A_Less(PT_Var f, PT_V_Number n, PT_O_Comp c)
throws PT_A_CompException {
super(f, n, c);
}
protected boolean compare(Object value) throws PT_ExecError {
if (value == null)
return false;
if (!(value instanceof Long))
throw new PT_ExecError("BUG: PT_A_Less: not a Long value: "
+ value.getClass().getName());
return ((Long) value).longValue() < number.num.v;
}
}
class PT_A_LessEqual extends PT_A_Compr {
public PT_A_LessEqual(PT_Var f, PT_V_Number n, PT_O_Comp c)
throws PT_A_CompException {
super(f, n, c);
}
protected boolean compare(Object value) throws PT_ExecError {
if (value == null)
return false;
if (!(value instanceof Long))
throw new PT_ExecError("BUG: PT_A_LessEqual: not a Long value: "
+ value.getClass().getName());
return ((Long) value).longValue() <= number.num.v;
}
}
class PT_A_Great extends PT_A_Compr {
public PT_A_Great(PT_Var f, PT_V_Number n, PT_O_Comp c)
throws PT_A_CompException {
super(f, n, c);
}
protected boolean compare(Object value) throws PT_ExecError {
if (value == null)
return false;
if (!(value instanceof Long))
throw new PT_ExecError("BUG: PT_A_Great: not a Long value: "
+ value.getClass().getName());
return ((Long) value).longValue() > number.num.v;
}
}
class PT_A_GreatEqual extends PT_A_Compr {
public PT_A_GreatEqual(PT_Var f, PT_V_Number n, PT_O_Comp c)
throws PT_A_CompException {
super(f, n, c);
}
protected boolean compare(Object value) throws PT_ExecError {
if (value == null)
return false;
if (!(value instanceof Long))
throw new PT_ExecError("BUG: PT_A_GreatEqual: not a Long value: "
+ value.getClass().getName());
return ((Long) value).longValue() >= number.num.v;
}
}
public class RuleSet {
private PT_A_Instr prog;
private RuleSet(PT_A_Instr prog) {
this.prog = prog;
}
public String toString() {
return prog.toString(0);
}
public boolean exec(Flow flow) throws PT_ExecError {
return prog.exec(flow);
}
public static RuleSet create(String str, String who_i_am) throws PT_Error {
return new RuleSet(compile_prog(new PT_Lexer(str, who_i_am)));
}
private static PT_A_Instr compile_prog(PT_Lexer lexer) throws PT_Error {
Object o = lexer.get_symbol();
if (o instanceof PT_O_EndOfLine)
lexer.error("It is an empty expression", lexer.cur_word);
else {
o = compile_or(o, lexer);
if (o instanceof PT_A_Instr) {
Object o1 = lexer.get_symbol();
if (!(o1 instanceof PT_O_EndOfLine)) {
lexer.pos = lexer.length;
lexer.error("Unexpected text after expression");
}
}
}
if (o instanceof PT_A_Instr)
return (PT_A_Instr) o;
String old_text = o.toString();
lexer.pos = lexer.length;
lexer.cur_word = 0;
lexer.error("BUG: Compiler completed with `" + old_text + "'");
return null; // unreachable return
}
private static Object compile_or(Object oi, PT_Lexer lexer) throws PT_Error {
Object left = compile_and(oi, lexer);
if (!(left instanceof PT_A_Instr))
return left;
Object o = lexer.get_symbol();
if (o instanceof PT_O_Or) {
Object right = compile_or(lexer.get_symbol(), lexer);
if (!(right instanceof PT_A_Instr))
return right;
return new PT_A_Or((PT_A_Instr) left, (PT_A_Instr) right);
}
lexer.back(o);
return left;
}
private static Object compile_and(Object oi, PT_Lexer lexer)
throws PT_Error {
Object left = compile_expr(oi, lexer);
if (!(left instanceof PT_A_Instr))
return left;
Object o = lexer.get_symbol();
if (o instanceof PT_O_And) {
Object right = compile_and(lexer.get_symbol(), lexer);
if (!(right instanceof PT_A_Instr))
return right;
return new PT_A_And((PT_A_Instr) left, (PT_A_Instr) right);
}
lexer.back(o);
return left;
}
private static Object compile_expr(Object o, PT_Lexer lexer)
throws PT_Error {
if (o instanceof PT_O_LParen) {
int start = lexer.cur_word;
Object inner = compile_or(lexer.get_symbol(), lexer);
if (!(inner instanceof PT_A_Instr))
return inner;
Object o1 = lexer.get_symbol();
if (!(o1 instanceof PT_O_RParen))
lexer.error("There is expected `)', not `" + o1.toString()
+ "'", start);
return inner;
} else if (o instanceof PT_O_Not) {
Object inner = compile_expr(lexer.get_symbol(), lexer);
if (!(inner instanceof PT_A_Instr))
return inner;
return new PT_A_Not((PT_A_Instr) inner);
}
return compile_comp(o, lexer);
}
private static Object compile_comp(Object left_i, PT_Lexer lexer)
throws PT_Error {
if (!(left_i instanceof PT_Var))
lexer.error("There is expected `field', not `" + left_i.toString()
+ "'");
PT_Var left = (PT_Var) left_i;
Object o = lexer.get_symbol();
try {
if (o instanceof PT_O_In) {
Object right = lexer.get_symbol();
if (left.isNumeric()) {
if (!(right instanceof PT_V_Range))
lexer.error("There is expected `range', not `"
+ right.toString() + "'");
return new PT_A_In(left, (PT_V_Range) right, (PT_O_In) o);
}
if (left.isAddress() || left.isPrefix()) {
if (!(right instanceof PT_V_VPrefix))
lexer.error("There is expected `prefix', not `"
+ right.toString() + "'");
return new PT_A_InPrefix(left, (PT_V_VPrefix) right,
(PT_O_In) o);
}
lexer.error("BUG: While parsing `" + left.toString()
+ "' and `" + right.toString() + "' for `"
+ o.toString() + "'");
} else if (o instanceof PT_O_Comp) {
Object right = lexer.get_symbol();
if (o instanceof PT_O_Weak) {
if (left.isNumeric() && right instanceof PT_V_Number) {
if (o instanceof PT_O_Equal)
return new PT_A_Equal(left, (PT_V_Number) right,
(PT_O_Comp) o);
if (o instanceof PT_O_NotEqual)
return new PT_A_NotEqual(left, (PT_V_Number) right,
(PT_O_Comp) o);
}
if (left.isAddress() && right instanceof PT_V_Address) {
if (o instanceof PT_O_Equal)
return new PT_A_EqualAddress(left,
(PT_V_Address) right, (PT_O_Comp) o);
if (o instanceof PT_O_NotEqual)
return new PT_A_NotEqualAddress(left,
(PT_V_Address) right, (PT_O_Comp) o);
}
if (left.isPrefix() && right instanceof PT_V_VPrefix) {
if (o instanceof PT_O_Equal)
return new PT_A_EqualPrefix(left,
(PT_V_VPrefix) right, (PT_O_Comp) o);
if (o instanceof PT_O_NotEqual)
return new PT_A_NotEqualPrefix(left,
(PT_V_VPrefix) right, (PT_O_Comp) o);
}
lexer.error("Incompatible arguments for `" + o.toString()
+ "' - `" + left.toString() + "' and `"
+ right.toString() + "'");
} else if (o instanceof PT_O_Strong) {
if (!(right instanceof PT_V_Number))
lexer.error("There is expected `number', not `"
+ right.toString() + "'");
if (o instanceof PT_O_Less)
return new PT_A_Less(left, (PT_V_Number) right,
(PT_O_Comp) o);
if (o instanceof PT_O_LessEqual)
return new PT_A_LessEqual(left, (PT_V_Number) right,
(PT_O_Comp) o);
if (o instanceof PT_O_Great)
return new PT_A_Great(left, (PT_V_Number) right,
(PT_O_Comp) o);
if (o instanceof PT_O_GreatEqual)
return new PT_A_GreatEqual(left, (PT_V_Number) right,
(PT_O_Comp) o);
}
lexer.error("BUG: Instancing source failed for operator: `"
+ o.toString() + "'");
}
} catch (PT_A_CompException e) {
lexer.error("BUG: Instancing code failed for operator: `"
+ o.toString() + "'");
}
lexer.error("There is expected `operator', not `" + o.toString() + "'");
return null; // unreachable return
}
/*
* static public void main( String arg[] ) throws PT_ExecError, PT_Error {
* String ar = ""; int i=0;
*
* for( i=0; i<arg.length; i++ ) { if( !ar.equals( "" ) ) ar += " ";
*
* ar += arg[i]; }
*
* int count = 10000; RuleSet rules = null; long start =
* System.currentTimeMillis();
*
* for( i=0; i<count; i++ ) rules = RuleSet.create( ar, "line:" );
*
* System.out.println( "Compiled "+count+" times in "+
* (System.currentTimeMillis()-start)+ " mills to\n"+ rules.toString() );
*
* boolean ret = false; oj ooo = new oj();
*
* start = System.currentTimeMillis();
*
* for( i=0; i<count; i++ ) ret=rules.exec( ooo );
*
* System.out.println( "Evaluated "+count+" times in "+
* (System.currentTimeMillis()-start)+ " mills to ["+ret+"]" ); }
*/
}
/*
* class oj extends Flow { public Long getSrcPort() { return new Long( 1 ); }
* public Long getDstPort() { return new Long( 2 ); } public Long getProto() {
* return new Long( 3 ); } public Long getTOS() { return new Long( 4 ); } public
* Long getSrcAS() { return new Long( 5 ); } public Long getDstAS() { return new
* Long( 6 ); }
*
* public Address getSrcAddr() { return new Address( 0x01010101 ); } public
* Address getDstAddr() { return new Address( 0x02020202 ); } public Address
* getNextHop() { return new Address( 0x03030303 ); }
*
* public Long getInIf() { return new Long( 7 ); } public Long getOutIf() {
* return new Long( 8 ); }
*
* public Prefix getSrcPrefix() { return new Prefix( (long)0x01020304, (byte)8 ); }
* public Prefix getDstPrefix() { return new Prefix( (long)0x04030201, (byte)24 ); } }
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -