Skip to content
This repository has been archived by the owner on Oct 14, 2022. It is now read-only.

[WIP] Request/Accept Connection Packets #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/network/src/packets/accept_connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2018 The Purple Library Authors
This file is part of the Purple Library.

The Purple Library is free 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.

The Purple Library 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 the Purple Library. If not, see <http://www.gnu.org/licenses/>.
*/

use crate::error::NetworkErr;
use crate::interface::NetworkInterface;
use crate::packet::Packet;
use crate::peer::ConnectionType;
use byteorder::{ReadBytesExt, WriteBytesExt};
use chrono::prelude::*;
use crypto::NodeId;
use crypto::{PublicKey as Pk, SecretKey as Sk, Signature};
use std::io::Cursor;
use std::net::SocketAddr;
use std::str;
use std::sync::Arc;

#[derive(Debug, Clone, PartialEq)]
pub struct AcceptConnection {
/// The node id of the requester
node_id: NodeId,

/// The packet's timestamp
timestamp: DateTime<Utc>,

/// Packet signature
signature: Option<Signature>,
}

impl AcceptConnection {
pub const PACKET_TYPE: u8 = 6;

pub fn new(node_id: NodeId) -> AcceptConnection {
AcceptConnection {
node_id: node_id,
timestamp: Utc::now(),
signature: None,
}
}
}

impl Packet for AcceptConnection {
fn sign(&mut self, skey: &Sk) {
// Assemble data
let message = assemble_sign_message(&self);

// Sign data
let signature = crypto::sign(&message, skey);

// Attach signature to struct
self.signature = Some(signature);
}

fn verify_sig(&self) -> bool {
let message = assemble_sign_message(&self);

match self.signature {
Some(ref sig) => crypto::verify(&message, sig, &self.node_id.0),
None => false,
}
}

fn signature(&self) -> Option<&Signature> {
self.signature.as_ref()
}

fn timestamp(&self) -> DateTime<Utc> {
self.timestamp.clone()
}

fn to_bytes(&self) -> Vec<u8> {
unimplemented!();
}

fn from_bytes(bytes: &[u8]) -> Result<Arc<AcceptConnection>, NetworkErr> {
unimplemented!();
}

fn handle<N: NetworkInterface>(
network: &mut N,
addr: &SocketAddr,
packet: &AcceptConnection,
conn_type: ConnectionType,
) -> Result<(), NetworkErr> {
if !packet.verify_sig() {
return Err(NetworkErr::BadSignature);
}



unimplemented!();
}
}

fn assemble_sign_message(obj: &AcceptConnection) -> Vec<u8> {
unimplemented!();
}
6 changes: 5 additions & 1 deletion src/network/src/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ pub mod connect;
pub mod request_peers;
pub mod send_peers;
pub mod forward_block;
pub mod request_connection;
pub mod accept_connection;
pub use self::connect::*;
pub use self::request_peers::*;
pub use self::send_peers::*;
pub use self::forward_block::*;
pub use self::forward_block::*;
pub use self::request_connection::*;
pub use self::accept_connection::*;
111 changes: 111 additions & 0 deletions src/network/src/packets/request_connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2018 The Purple Library Authors
This file is part of the Purple Library.

The Purple Library is free 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.

The Purple Library 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 the Purple Library. If not, see <http://www.gnu.org/licenses/>.
*/

use crate::error::NetworkErr;
use crate::interface::NetworkInterface;
use crate::packet::Packet;
use crate::peer::ConnectionType;
use byteorder::{ReadBytesExt, WriteBytesExt};
use chrono::prelude::*;
use crypto::NodeId;
use crypto::{PublicKey as Pk, SecretKey as Sk, Signature};
use std::io::Cursor;
use std::net::SocketAddr;
use std::str;
use std::sync::Arc;

#[derive(Debug, Clone, PartialEq)]
pub struct RequestConnection {
/// The node id of the requester
node_id: NodeId,

/// The packet's timestamp
timestamp: DateTime<Utc>,

/// Packet signature
signature: Option<Signature>,
}

impl RequestConnection {
pub const PACKET_TYPE: u8 = 5;

pub fn new(node_id: NodeId) -> RequestConnection {
RequestConnection {
node_id: node_id,
timestamp: Utc::now(),
signature: None,
}
}
}

impl Packet for RequestConnection {
fn sign(&mut self, skey: &Sk) {
// Assemble data
let message = assemble_sign_message(&self);

// Sign data
let signature = crypto::sign(&message, skey);

// Attach signature to struct
self.signature = Some(signature);
}

fn verify_sig(&self) -> bool {
let message = assemble_sign_message(&self);

match self.signature {
Some(ref sig) => crypto::verify(&message, sig, &self.node_id.0),
None => false,
}
}

fn signature(&self) -> Option<&Signature> {
self.signature.as_ref()
}

fn timestamp(&self) -> DateTime<Utc> {
self.timestamp.clone()
}

fn to_bytes(&self) -> Vec<u8> {
unimplemented!();
}

fn from_bytes(bytes: &[u8]) -> Result<Arc<RequestConnection>, NetworkErr> {
unimplemented!();
}

fn handle<N: NetworkInterface>(
network: &mut N,
addr: &SocketAddr,
packet: &RequestConnection,
conn_type: ConnectionType,
) -> Result<(), NetworkErr> {
if !packet.verify_sig() {
return Err(NetworkErr::BadSignature);
}



unimplemented!();
}
}

fn assemble_sign_message(obj: &RequestConnection) -> Vec<u8> {
unimplemented!();
}