# Context-Aware Trust and Reputation Routing Protocol for Opportunistic IoT Networks
## 3 Proposed CATR Routing Protocol for OppIoT Networks
### Direct Trust Computation
It is computed based on the time elapsed since the last contact between a node \(m\) with its neighbor \(n\) and that between \(n\) with the destination \(D\). Its lower value indicates that these nodes have been contacted more recently. It is represented by \(RC_{mn}\), computed as \[RC_{mn}=\frac{rc_{mn}+rc_{nD}}{\sum\limits_{k=1}^{K}rc_{mk}+\sum\limits_{k=1}^ {K}rc_{nk}}\] (13) where \(K\) is the total number of nodes encountered by a node until that instance of time, \(rc_{mn}\) is the latest elapsed contact time of \(m\) and \(n\), \(rc_{nD}\) is the latest elapsed contact time of \(n\) and \(D\), \(\sum\limits_{k=1}^{K}rc_{mk}\) is the total time elapsed since the last contact of \(m\) with other nodes, and \(\sum\limits_{k=1}^{K}rc_{mk}\) is the total time elapsed since the last contact of \(n\) with other nodes. The procedure to calculate \(RC_{nD}\) is given in Algorithm 4. * \(CD\) calculation: \(CD\) refers to the contact duration between two nodes. It is computed based on the minimum duration between a node \(m\) with its neighbor \(n\) and that between the neighbor \(n\) with the destination \(D\). The higher the contact durability between two nodes, the higher the reliability and trustworthiness among them. It is represented by \(CD_{mn}\), computed as \[CD_{mn}=\frac{cd_{mn}+cd_{nD}}{\sum\limits_{k=1}^{K}cd_{mk}+\sum\limits_{k=1}^ {K}cd_{nk}}\] (14) where \(K\) is the total number of nodes encountered by a node till that instance of time, \(cd_{mn}\) is the contact duration between \(m\) and \(n\), \(cd_{nD}\) is the contact duration between \(n\) and destination \(D\), \(\sum\limits_{k=1}^{K}cd_{mk}\) represents total contact duration between \(m\) with all other nodes, and \(\sum\limits_{k=1}^{K}cd_{nk}\) represents total contact duration between \(n\) with all other nodes. The procedure to calculate \(CD_{nD}\) is given in Algorithm 5. ```
1:\(\zeta,\lambda\)
2:Beta Trust value (\(BT_{mn}\))
3:procedurecalcBT(\(\zeta,\lambda\))
4:\(BT_{mn}=(\zeta+1)/(\zeta+\lambda+2)\)
5:return (\(BT_{mn}\))
6:endprocedure
```  
**Algorithm 1** Beta Trust value (\(BT_{mn}\))```
0: n, D Output: Encounter frequency index (\(EF_{nD}\))
1:procedurecalcEF(\(n,D\))
2:foreach NId-\(k\) in \(\mathit{EncounterList}\)do
3:// Calculate total encounter frequency of \(n\) with all \(k\). 4:\(f_{nk}=f_{nk}+f\)
5:if\(k=D\)then
6:// Calculate encounter frequency of \(n\) with \(D\). 7:\(f_{nD}=f_{nD}+f\)
8:endif
9:endfor
10:return (\(f_{nD}/f_{nk}\))
11:endprocedure
```

**Algorithm 2** Encounter frequency index (\(EF_{nD}\)). ```
0: n
0: Packet forwarding degree (\(PF_{mn}\))
1:procedurecalcPF(\(n\))
2:foreach sid-\(k\) in \(\mathit{ForwardingList}\)do
3:// Calculate the total number of packets of all nodes that are forwarded by \(n\). 4:\(num_{mn}=num_{mn}+np\)
5:if\(\mathit{sil}=m\) and \(\mathit{rid}=n\)then
6:// Calculate the total of packets of \(m\) that are forwarded by \(n\)
7:\(num_{n}=num_{n}+np\)
8:endif
9:endfor
10:return (\(num_{mn}/num_{n}\))
11:endprocedure
```

**Algorithm 3** Packet forwarding degree (\(PF_{mn}\)). ```
0: m, n, D, Current time (as \(t_{c}\))
0:Recent contact degree (\(RC_{mn}\))
1:procedurecalcRC(\(m,n,D\))
2:for each NId-\(k\) in \(\mathit{EncounterList}\) of node \(m\)do
3:// Determine the latest elapsed contact time of \(m\) with \(n\). 4:if\(NId=n\)then
5:\(rc_{mm}=min(t_{c}-t_{lc})\)
6:endif
7:// Determine the total time elapsed since last contact of \(m\) with other nodes. 8:\(rc_{mk}=rc_{mk}+min(t_{c}-t_{lc})\)
9:endfor
10:\(\sum\limits_{k=1}^{n}rc_{ik}=s\)
11:for each NId-\(k\) in \(\mathit{EncounterList}\) of node \(n\)do
12:// Determine the latest elapsed contact time of \(n\) with destination \(D\). 13:if\(k=D\)then
14:\(rc_{nD}=min(t_{c}-t_{lc})\)
15:endif
16:// Determine the total time elapsed since last contact of \(n\) with other nodes. 17:\(rc_{nk}=rc_{mk}+min(t_{c}-t_{lc})\)
18:endfor
19:\(RC_{mn}=\{rc_{mn}+rc_{nD}\}/\{\sum\limits_{k=1}^{K}rc_{mk}+\sum\limits_{k=1}^{K}rc_{ mk}\}\)
20:return \(RC_{mn}\)
21:endprocedure
```

**Algorithm 4** Recent contact degree (\(RC_{mn}\)).