Add verigraph code base
[parser.git] / verigraph / service / src / mcnet / netobjs / PolitoWebClient.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
13 import java.util.ArrayList;
14 import java.util.List;
15
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
24 import mcnet.components.NetContext;
25 import mcnet.components.Network;
26 import mcnet.components.NetworkObject;
27
28 /**
29  * WebClient
30  */
31 public class PolitoWebClient extends NetworkObject{
32
33                 List<BoolExpr> constraints;
34                 Context ctx;
35                 DatatypeExpr politoWebClient;
36                 Network net;
37                 NetContext nctx;
38                 FuncDecl isInBlacklist;
39
40                 public PolitoWebClient(Context ctx, Object[]... args) {
41                         super(ctx, args);
42                 }
43
44                 @Override
45                 protected void init(Context ctx, Object[]... args) {
46                         this.ctx = ctx;
47                     isEndHost=true;
48                         constraints = new ArrayList<BoolExpr>();
49                         z3Node = ((NetworkObject)args[0][0]).getZ3Node();
50                         politoWebClient = z3Node;
51                         net = (Network)args[0][1];
52                         nctx = (NetContext)args[0][2];
53                         DatatypeExpr ipServer = (DatatypeExpr) args[0][3];
54                         webClientRules(ipServer);
55                         //net.saneSend(this);
56             }
57
58                 @Override
59                 public DatatypeExpr getZ3Node() {
60                         return politoWebClient;
61                 }
62
63                 @Override
64                 protected void addConstraints(Solver solver) {
65 //                      System.out.println("[MailClient] Installing rules.");
66                         BoolExpr[] constr = new BoolExpr[constraints.size()];
67                         solver.add(constraints.toArray(constr));
68                 }
69
70             private void webClientRules (DatatypeExpr ipServer){
71                 Expr n_0 = ctx.mkConst("PolitoWebClient_"+politoWebClient+"_n_0", nctx.node);
72                 Expr p_0 = ctx.mkConst("PolitoWebClient_"+politoWebClient+"_p_0", nctx.packet);
73                 IntExpr t_0 = ctx.mkIntConst("PolitoWebClient_"+politoWebClient+"_t_0");
74
75                 //Constraint1           send(politoWebClient, n_0, p, t_0) -> nodeHasAddr(politoWebClient,p.src)
76                         constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
77                     ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
78                                 (BoolExpr)nctx.nodeHasAddr.apply(politoWebClient,nctx.pf.get("src").apply(p_0))),1,null,null,null,null));
79
80                         //Constraint2           send(politoWebClient, n_0, p, t_0) -> p.origin == politoWebClient
81                         constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
82                     ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
83                                 ctx.mkEq(nctx.pf.get("origin").apply(p_0),politoWebClient)),1,null,null,null,null));
84
85                         //Constraint3           send(politoWebClient, n_0, p, t_0) -> p.orig_body == p.body
86                         constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
87                     ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
88                                 ctx.mkEq(nctx.pf.get("orig_body").apply(p_0),nctx.pf.get("body").apply(p_0))),1,null,null,null,null));
89
90                         //Constraint4           recv(n_0, politoWebClient, p, t_0) -> nodeHasAddr(politoWebClient,p.dest)
91                         constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
92                     ctx.mkImplies((BoolExpr)nctx.recv.apply(n_0,politoWebClient, p_0, t_0),
93                                 (BoolExpr)nctx.nodeHasAddr.apply(politoWebClient,nctx.pf.get("dest").apply(p_0))),1,null,null,null,null));
94
95
96                         //Constraint5           This client is only able to produce HTTP requests
97                         //                                      send(politoWebClient, n_0, p, t_0) -> p.proto == HTTP_REQ
98             constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
99                     ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
100                         ctx.mkEq(nctx.pf.get("proto").apply(p_0), ctx.mkInt(nctx.HTTP_REQUEST))),1,null,null,null,null));
101
102             //Constraint6               send(politoWebClient, n_0, p, t_0) -> p.dest == ipServer
103             constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
104                     ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
105                             ctx.mkEq(nctx.pf.get("dest").apply(p_0), ipServer)),1,null,null,null,null));
106             }
107         }