1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Apache License, Version 2.0
6 * which accompanies this distribution, and is available at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *******************************************************************************/
10 package mcnet.netobjs;
13 import java.util.ArrayList;
14 import java.util.List;
16 import com.microsoft.z3.BoolExpr;
17 import com.microsoft.z3.Context;
18 import com.microsoft.z3.DatatypeExpr;
19 import com.microsoft.z3.Expr;
20 import com.microsoft.z3.FuncDecl;
21 import com.microsoft.z3.IntExpr;
22 import com.microsoft.z3.Solver;
23 import com.microsoft.z3.Sort;
25 import mcnet.components.NetContext;
26 import mcnet.components.Network;
27 import mcnet.components.NetworkObject;
29 /** First example of custom network function: a simple filter
32 public class PolitoNF extends NetworkObject{
34 List<BoolExpr> constraints;
36 DatatypeExpr politoNF;
39 FuncDecl isInBlacklist;
42 public PolitoNF(Context ctx, Object[]... args) {
47 protected void init(Context ctx, Object[]... args) {
50 constraints = new ArrayList<BoolExpr>();
51 z3Node = ((NetworkObject)args[0][0]).getZ3Node();
53 net = (Network)args[0][1];
54 nctx = (NetContext)args[0][2];
58 public void politoNFRules (DatatypeExpr ipA,DatatypeExpr ipB){
59 // System.out.println("[PolitoNf] Installing rules");
60 Expr n_0 = ctx.mkConst("politoNF_"+politoNF+"_n_0", nctx.node);
61 Expr n_1 = ctx.mkConst("politoNF_"+politoNF+"_n_1", nctx.node);
62 Expr p_0 = ctx.mkConst("politoNF_"+politoNF+"_p_0", nctx.packet);
63 IntExpr t_0 = ctx.mkIntConst("politoNF_"+politoNF+"_t_0");
64 IntExpr t_1 = ctx.mkIntConst("politoNF_"+politoNF+"_t_1");
65 Expr a_0 = ctx.mkConst(politoNF+"_politoNF_a_0", nctx.address);
66 Expr a_1 = ctx.mkConst(politoNF+"_politoNF_a_1", nctx.address);
68 FuncDecl myFunction = ctx.mkFuncDecl(politoNF+"_myFunction", new Sort[]{nctx.address,nctx.address}, ctx.mkBoolSort());
70 BoolExpr myConstraint = ctx.mkForall(new Expr[]{n_0, p_0, t_0},
71 ctx.mkImplies((BoolExpr)nctx.send.apply(politoNF, n_0, p_0, t_0),
72 ctx.mkExists(new Expr[]{n_1, t_1},
73 ctx.mkAnd((BoolExpr)nctx.recv.apply(n_1, politoNF, p_0, t_1),
75 (BoolExpr)myFunction.apply(nctx.pf.get("src").apply(p_0), nctx.pf.get("dest").apply(p_0))),1,null,null,null,null)),1,null,null,null,null);
77 BoolExpr funcConstraint = ctx.mkOr(ctx.mkAnd(ctx.mkEq(a_0, ipA), ctx.mkEq(a_1, ipB)), ctx.mkAnd(ctx.mkEq(a_0,ipB), ctx.mkEq(a_1,ipA)));
79 // Constraint1 myFunction(a_0,a_1) == ((a_0 == ipA && a_1 == ipB) || (a_0 == ipB && a_1 == ipA))
81 ctx.mkForall(new Expr[]{a_0,a_1},
82 ctx.mkEq(myFunction.apply(a_0, a_1), funcConstraint),1,null,null,null,null));
84 //Constraint2 send(politoNF, n_0, p, t_0) ->
85 // (exist n_1,t_1 : (t_1 < t_0 && recv(n_1, politoNF, p, t_1) && myFunction(p.src,p.dest))
86 constraints.add(myConstraint);
91 protected void addConstraints(Solver solver) {
92 BoolExpr[] constr = new BoolExpr[constraints.size()];
93 solver.add(constraints.toArray(constr));
97 public DatatypeExpr getZ3Node() {