Merge "Add verigraph code base"
[parser.git] / verigraph / service / src / mcnet / netobjs / PolitoFieldModifier.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
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  *******************************************************************************/
9
10 package mcnet.netobjs;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import com.microsoft.z3.BoolExpr;
16 import com.microsoft.z3.Context;
17 import com.microsoft.z3.DatatypeExpr;
18 import com.microsoft.z3.Expr;
19 import com.microsoft.z3.IntExpr;
20 import com.microsoft.z3.Solver;
21
22 import mcnet.components.NetContext;
23 import mcnet.components.Network;
24 import mcnet.components.NetworkObject;
25
26 public class PolitoFieldModifier extends NetworkObject {
27
28         List<BoolExpr> constraints;
29         Context ctx;
30         DatatypeExpr politoFieldModifier;
31         Network net;
32         NetContext nctx;
33
34         public PolitoFieldModifier(Context ctx, Object[]... args) {
35                 super(ctx, args);
36         }
37
38         @Override
39         public DatatypeExpr getZ3Node() {
40                 return politoFieldModifier;
41         }
42
43         @Override
44         protected void init(Context ctx, Object[]... args) {
45                 this.ctx = ctx;
46                 isEndHost=false;
47                 constraints = new ArrayList<BoolExpr>();
48         z3Node = ((NetworkObject)args[0][0]).getZ3Node();
49         politoFieldModifier = z3Node;
50         net = (Network)args[0][1];
51         nctx = (NetContext)args[0][2];
52         }
53
54         @Override
55         protected void addConstraints(Solver solver) {
56                 BoolExpr[] constr = new BoolExpr[constraints.size()];
57             solver.add(constraints.toArray(constr));
58         }
59
60         public void installFieldModifier (){
61                 Expr x = ctx.mkConst("politoFieldModifier_"+politoFieldModifier+"_x", nctx.node);
62         Expr y = ctx.mkConst("politoFieldModifier_"+politoFieldModifier+"_y", nctx.node);
63         Expr p_0 = ctx.mkConst("politoFieldModifier_"+politoFieldModifier+"_p_0", nctx.packet);
64         Expr p_1 = ctx.mkConst("politoFieldModifier_"+politoFieldModifier+"_p_1", nctx.packet);
65
66         IntExpr t_0 = ctx.mkIntConst("politoFieldModifier_"+politoFieldModifier+"_t_0");
67         IntExpr t_1 = ctx.mkIntConst("politoFieldModifier_"+politoFieldModifier+"_t_1");
68
69         constraints.add(
70                         ctx.mkForall(new Expr[]{t_0, p_0, x},
71                                 ctx.mkImplies((BoolExpr)nctx.send.apply(politoFieldModifier,x,p_0,t_0),
72                                                           ctx.mkExists(new Expr[]{y, p_1, t_1},
73                                                                  ctx.mkAnd(ctx.mkLt(t_1, t_0),
74                                                                                    (BoolExpr)nctx.recv.apply(y, politoFieldModifier, p_1, t_1),
75                                                                                    ctx.mkEq(nctx.pf.get("encrypted").apply(p_0), nctx.pf.get("encrypted").apply(p_1)),
76                                                                                    ctx.mkEq(nctx.pf.get("src").apply(p_0), nctx.pf.get("src").apply(p_1)),
77                                                                                    ctx.mkEq(nctx.pf.get("dest").apply(p_0), nctx.pf.get("dest").apply(p_1)),
78                                                                                    ctx.mkEq(nctx.pf.get("inner_src").apply(p_0), nctx.pf.get("inner_src").apply(p_1)),
79                                                                                    ctx.mkEq(nctx.pf.get("inner_dest").apply(p_0), nctx.pf.get("inner_dest").apply(p_1)),
80                                                                                    ctx.mkEq(nctx.pf.get("origin").apply(p_0), nctx.pf.get("origin").apply(p_1)),
81                                                                                    ctx.mkEq(nctx.pf.get("orig_body").apply(p_0), nctx.pf.get("orig_body").apply(p_1)),
82                                                                                    ctx.mkEq(nctx.pf.get("body").apply(p_0), nctx.pf.get("body").apply(p_1)),
83                                                                                    ctx.mkEq(nctx.pf.get("seq").apply(p_0), nctx.pf.get("seq").apply(p_1)),
84                                                                                    ctx.mkEq(nctx.pf.get("proto").apply(p_0), nctx.pf.get("proto").apply(p_1)),
85                                                                                    ctx.mkEq(nctx.pf.get("emailFrom").apply(p_0), nctx.pf.get("emailFrom").apply(p_1)),
86                                                                                    ctx.mkNot(ctx.mkEq(nctx.pf.get("url").apply(p_0), nctx.pf.get("url").apply(p_1))),
87                                                                                    ctx.mkEq(nctx.pf.get("options").apply(p_0), nctx.pf.get("options").apply(p_1))),1,null,null,null,null)),
88                         1,null,null,null,null));
89         }
90
91 }