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 *******************************************************************************/
9 package it.polito.verigraph.mcnet.netobjs;
11 import java.util.ArrayList;
12 import java.util.List;
14 import com.microsoft.z3.BoolExpr;
15 import com.microsoft.z3.Context;
16 import com.microsoft.z3.DatatypeExpr;
17 import com.microsoft.z3.Expr;
18 import com.microsoft.z3.FuncDecl;
19 import com.microsoft.z3.IntExpr;
20 import com.microsoft.z3.Solver;
22 import it.polito.verigraph.mcnet.components.NetContext;
23 import it.polito.verigraph.mcnet.components.Network;
24 import it.polito.verigraph.mcnet.components.NetworkObject;
29 public class PolitoWebClient extends NetworkObject{
31 List<BoolExpr> constraints;
33 DatatypeExpr politoWebClient;
36 FuncDecl isInBlacklist;
38 public PolitoWebClient(Context ctx, Object[]... args) {
43 protected void init(Context ctx, Object[]... args) {
46 constraints = new ArrayList<BoolExpr>();
47 z3Node = ((NetworkObject)args[0][0]).getZ3Node();
48 politoWebClient = z3Node;
49 net = (Network)args[0][1];
50 nctx = (NetContext)args[0][2];
51 DatatypeExpr ipServer = (DatatypeExpr) args[0][3];
52 webClientRules(ipServer);
57 public DatatypeExpr getZ3Node() {
58 return politoWebClient;
62 protected void addConstraints(Solver solver) {
63 //System.out.println("[MailClient] Installing rules.");
64 BoolExpr[] constr = new BoolExpr[constraints.size()];
65 solver.add(constraints.toArray(constr));
68 private void webClientRules (DatatypeExpr ipServer){
69 Expr n_0 = ctx.mkConst("PolitoWebClient_"+politoWebClient+"_n_0", nctx.node);
70 Expr p_0 = ctx.mkConst("PolitoWebClient_"+politoWebClient+"_p_0", nctx.packet);
71 IntExpr t_0 = ctx.mkIntConst("PolitoWebClient_"+politoWebClient+"_t_0");
73 //Constraint1send(politoWebClient, n_0, p, t_0) -> nodeHasAddr(politoWebClient,p.src)
74 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
75 ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
76 (BoolExpr)nctx.nodeHasAddr.apply(politoWebClient,nctx.pf.get("src").apply(p_0))),1,null,null,null,null));
78 //Constraint2send(politoWebClient, n_0, p, t_0) -> p.origin == politoWebClient
79 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
80 ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
81 ctx.mkEq(nctx.pf.get("origin").apply(p_0),politoWebClient)),1,null,null,null,null));
83 //Constraint3send(politoWebClient, n_0, p, t_0) -> p.orig_body == p.body
84 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
85 ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
86 ctx.mkEq(nctx.pf.get("orig_body").apply(p_0),nctx.pf.get("body").apply(p_0))),1,null,null,null,null));
88 //Constraint4recv(n_0, politoWebClient, p, t_0) -> nodeHasAddr(politoWebClient,p.dest)
89 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
90 ctx.mkImplies((BoolExpr)nctx.recv.apply(n_0,politoWebClient, p_0, t_0),
91 (BoolExpr)nctx.nodeHasAddr.apply(politoWebClient,nctx.pf.get("dest").apply(p_0))),1,null,null,null,null));
94 //Constraint5This client is only able to produce HTTP requests
95 //send(politoWebClient, n_0, p, t_0) -> p.proto == HTTP_REQ
96 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
97 ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
98 ctx.mkEq(nctx.pf.get("proto").apply(p_0), ctx.mkInt(nctx.HTTP_REQUEST))),1,null,null,null,null));
100 //Constraint6send(politoWebClient, n_0, p, t_0) -> p.dest == ipServer
101 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
102 ctx.mkImplies((BoolExpr)nctx.send.apply(politoWebClient, n_0, p_0, t_0),
103 ctx.mkEq(nctx.pf.get("dest").apply(p_0), ipServer)),1,null,null,null,null));