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;
24 import mcnet.components.NetContext;
25 import mcnet.components.Network;
26 import mcnet.components.NetworkObject;
31 public class PolitoMailClient extends NetworkObject{
33 List<BoolExpr> constraints;
35 DatatypeExpr politoMailClient;
38 FuncDecl isInBlacklist;
40 public PolitoMailClient(Context ctx, Object[]... args) {
45 protected void init(Context ctx, Object[]... args) {
48 constraints = new ArrayList<BoolExpr>();
49 z3Node = ((NetworkObject)args[0][0]).getZ3Node();
50 politoMailClient = z3Node;
51 net = (Network)args[0][1];
52 nctx = (NetContext)args[0][2];
53 DatatypeExpr ipServer = (DatatypeExpr) args[0][3];
54 mailClientRules(ipServer);
59 public DatatypeExpr getZ3Node() {
60 return politoMailClient;
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));
70 private void mailClientRules (DatatypeExpr ipServer){
71 Expr n_0 = ctx.mkConst("PolitoMailClient_"+politoMailClient+"_n_0", nctx.node);
72 Expr p_0 = ctx.mkConst("PolitoMailClient_"+politoMailClient+"_p_0", nctx.packet);
73 IntExpr t_0 = ctx.mkIntConst("PolitoMailClient_"+politoMailClient+"_t_0");
75 // Constraint1 send(politoMailClient, n_0, p, t_0) -> nodeHasAddr(politoMailClient,p.src)
76 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
77 ctx.mkImplies((BoolExpr)nctx.send.apply(politoMailClient, n_0, p_0, t_0),
78 (BoolExpr)nctx.nodeHasAddr.apply(politoMailClient,nctx.pf.get("src").apply(p_0))),1,null,null,null,null));
80 // Constraint2 send(politoMailClient, n_0, p, t_0) -> p.origin == politoMailClient
81 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
82 ctx.mkImplies((BoolExpr)nctx.send.apply(politoMailClient, n_0, p_0, t_0),
83 ctx.mkEq(nctx.pf.get("origin").apply(p_0),politoMailClient)),1,null,null,null,null));
85 // Constraint3 send(politoMailClient, 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(politoMailClient, 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));
90 // Constraint4 recv(n_0, politoMailClient, p, t_0) -> nodeHasAddr(politoMailClient,p.dest)
91 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
92 ctx.mkImplies((BoolExpr)nctx.recv.apply(n_0,politoMailClient, p_0, t_0),
93 (BoolExpr)nctx.nodeHasAddr.apply(politoMailClient,nctx.pf.get("dest").apply(p_0))),1,null,null,null,null));
95 // Constraint5 This client is only able to produce POP3 requests
96 // send(politoMailClient, n_0, p, t_0) -> p.proto == POP3_REQ
97 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
98 ctx.mkImplies((BoolExpr)nctx.send.apply(politoMailClient, n_0, p_0, t_0),
99 ctx.mkEq(nctx.pf.get("proto").apply(p_0), ctx.mkInt(nctx.POP3_REQUEST))),1,null,null,null,null));
101 // Constraint6 send(politoMailClient, n_0, p, t_0) -> p.dest == ip_mailServer
102 constraints.add( ctx.mkForall(new Expr[]{n_0, p_0, t_0},
103 ctx.mkImplies((BoolExpr)nctx.send.apply(politoMailClient, n_0, p_0, t_0),
104 ctx.mkEq(nctx.pf.get("dest").apply(p_0), ipServer)),1,null,null,null,null));