copernica_packets/
link_id.rs1use {
2 std::{fmt},
3 crate::{
4 PrivateIdentityInterface, PublicIdentity, PublicIdentityInterface, SharedSecret, Nonce, ReplyTo,
5 },
6 anyhow::{Result, anyhow},
7 rand::Rng,
8};
9#[derive(Clone, Eq, Hash, PartialEq)]
10pub enum LinkId {
11 Identity {
12 lookup_id: u32,
13 link_sid: PrivateIdentityInterface,
14 remote_link_pid: PublicIdentityInterface,
15 reply_to: ReplyTo,
16 },
17 Choke,
18}
19impl LinkId {
20 pub fn new(lookup_id: u32, link_sid: PrivateIdentityInterface, remote_link_pid: PublicIdentityInterface, reply_to: ReplyTo) -> Self {
21 LinkId::Identity { lookup_id, link_sid, remote_link_pid, reply_to }
22 }
23 pub fn link_with_type(link_sid: PrivateIdentityInterface, remote_link_pid: PublicIdentityInterface, reply_to: ReplyTo) -> Self {
24 let mut rng = rand::thread_rng();
25 let i: u32 = rng.gen();
26 LinkId::Identity { lookup_id: i, link_sid, remote_link_pid, reply_to }
27 }
28 pub fn lookup_id(&self) -> Result<u32> {
29 match self {
30 LinkId::Identity { lookup_id, .. } => Ok(*lookup_id),
31 LinkId::Choke => {
32 Err(anyhow!("Requesting a ReplyTo when in state Choke. Not going to happen buddy"))
33 }
34 }
35 }
36 pub fn shared_secret(&self, nonce: Nonce, remote_link_pid: PublicIdentity) -> Result<SharedSecret> {
37 match self {
38 LinkId::Identity { link_sid, .. } => {
39 Ok(link_sid.shared_secret(nonce, remote_link_pid))
40 },
41 LinkId::Choke => {
42 Err(anyhow!("Requesting a ReplyTo when in state Choke. Not going to happen buddy"))
43 }
44 }
45 }
46 pub fn choke() -> Self {
47 LinkId::Choke
48 }
49 pub fn remote(&self, reply_to: ReplyTo) -> Result<Self> {
50 match self {
51 LinkId::Identity { lookup_id, link_sid, remote_link_pid, .. } => {
52 Ok(LinkId::Identity { lookup_id: lookup_id.clone(), link_sid: link_sid.clone(), remote_link_pid: remote_link_pid.clone(), reply_to })
53 },
54 LinkId::Choke => {
55 Err(anyhow!("Requesting a ReplyTo when in state Choke. Not going to happen buddy"))
56 }
57 }
58 }
59 pub fn reply_to(&self) -> Result<ReplyTo> {
60 match self {
61 LinkId::Identity { reply_to, .. } => {
62 Ok(reply_to.clone())
63 },
64 LinkId::Choke => {
65 Err(anyhow!("Requesting a ReplyTo when in state Choke. Not going to happen buddy"))
66 }
67 }
68 }
69 pub fn link_sid(&self) -> Result<PrivateIdentityInterface> {
70 match self {
71 LinkId::Identity { link_sid, ..} => {
72 Ok(link_sid.clone())
73 },
74 LinkId::Choke => {
75 Err(anyhow!("Requesting a PrivateIdentityInterface when in state Choke. Not going to happen buddy"))
76 }
77 }
78 }
79 pub fn link_pid(&self) -> Result<PublicIdentity> {
80 match self {
81 LinkId::Identity { link_sid, .. } => {
82 Ok(link_sid.public_id())
83 },
84 LinkId::Choke => {
85 Err(anyhow!("Requesting a PrivateIdentityInterface when in state Choke. Not going to happen buddy"))
86 }
87 }
88 }
89 pub fn remote_link_pid(&self) -> Result<PublicIdentityInterface> {
90 match self {
91 LinkId::Identity { remote_link_pid, ..} => {
92 Ok(remote_link_pid.clone())
93 },
94 LinkId::Choke => {
95 Err(anyhow!("Requesting a PublicIdentity when in state Choke. Not going to happen buddy"))
96 }
97 }
98 }
99}
100impl fmt::Debug for LinkId {
101 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
102 match self {
103 LinkId::Identity { lookup_id, link_sid, remote_link_pid, reply_to} => {
104 write!(f, "LinkId:({}, {:?}, {:?}, {:?})", lookup_id, link_sid, remote_link_pid, reply_to)
105 },
106 LinkId::Choke => {
107 write!(f, "LinkId: CHOKED")
108 }
109 }
110 }
111}