-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlib.rs
156 lines (123 loc) · 4.56 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright © 2016 Zetok Zalbavar <[email protected]>
This file is part of Tox.
Tox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Tox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tox. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
Rust implementation of the [Tox protocol](https://toktok.github.io/spec).
Current API allows one to e.g. find info about DHT nodes from bootstrap
nodes by sending [`GetNodes`](./toxcore/dht/struct.GetNodes.html) or request
[`Ping`](./toxcore/dht/struct.Ping.html) response.
To request a ping response:
```
// to get bytes from PK in hex and to make PK from them
extern crate rustc_serialize;
use rustc_serialize::hex::FromHex;
extern crate tox;
use tox::toxcore::binary_io::*;
use tox::toxcore::crypto_core::*;
use tox::toxcore::dht::*;
use tox::toxcore::network::*;
fn main() {
// get PK bytes from some "random" bootstrap node (Impyy's)
let bootstrap_pk_bytes = FromHex::from_hex("788236D34978D1D5BD822F0A5BEBD2C53C64CC31CD3149350EE27D4D9A2F9B6B").unwrap();
// create PK from bytes
let bootstrap_pk = PublicKey::from_slice(&bootstrap_pk_bytes).unwrap();
// generate own PublicKey, SecretKey keypair
let (pk, sk) = gen_keypair();
// and to encrypt data there precomputed symmetric key is needed, created
// from PK of the peer you want to send data to, and your own secret key.
let precomp = precompute(&bootstrap_pk, &sk);
// also generate nonce that will be needed to make the encryption happen
let nonce = gen_nonce();
// now create Ping request
let ping = Ping::new()
.as_packet(); // and make Ping usable by DhtPacket
// with Ping packet create DhtPacket, and serialize it to bytes
let dhtpacket = DhtPacket::new(&precomp, &pk, &nonce, ping).to_bytes();
// and since packet is ready, prepare the network part;
// bind to some UDP socket
let socket = match bind_udp() {
Some(s) => s,
None => {
println!("Failed to bind to socket, exiting.");
return;
},
};
// send DhtPacket via socket to the node (Imppy's)
let sent_bytes = match socket.send_to(&dhtpacket, "178.62.250.138:33445") {
Ok(bytes) => bytes,
Err(e) => {
println!("Failed to send bytes: {}", e);
return;
},
};
println!("Sent {} bytes of Ping request to the bootstrap node", sent_bytes);
// since data was sent, now receive response – for that, first prepare
// buffer to receive data into
let mut buf = [0; 2048]; // Tox UDP packet won't be bigger
// and wait for the answer
let (bytes, sender) = match socket.recv_from(&mut buf) {
Ok(d) => d,
Err(e) => {
println!("Failed to receive data from socket: {}", e);
return;
},
};
// try to de-serialize received bytes as `DhtPacket`
let recv_packet = match DhtPacket::from_bytes(&buf[..bytes]) {
Some(p) => p,
// if parsing fails ↓
None => {
println!("Received packet could not have been parsed!\n{:?}",
&buf[..bytes]);
return;
},
};
println!("Received packet from {}, with an encrypted payload:\n{:?}",
sender, recv_packet);
// decrypt payload of the received packet
let payload = match recv_packet.get_packet(&sk) {
Some(p) => p,
None => {
println!("Failed to decrypt payload!");
return;
},
};
println!("And contents of payload:\n{:?}", payload);
}
```
*/
#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]
#[macro_use]
extern crate log;
extern crate sodiumoxide;
extern crate ip;
/// Core Tox module. Provides an API on top of which other modules and
/// applications may be build.
#[warn(missing_docs)]
pub mod toxcore {
pub mod binary_io;
pub mod crypto_core;
pub mod dht;
pub mod network;
}
#[cfg(test)]
mod toxcore_tests {
extern crate quickcheck;
extern crate rustc_serialize;
mod binary_io_tests;
mod crypto_core_tests;
mod dht_tests;
mod network_tests;
}