Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic errors #39

Merged
merged 4 commits into from
Oct 12, 2018
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Introduce data::ReadError to wrap file IO and parser errors
str4d committed Oct 12, 2018
commit dfacff1b77a57f21b21e99b630ed7a2c88101a3d
2 changes: 1 addition & 1 deletion src/data/frame.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cookie_factory::*;
use itertools::Itertools;
use nom::{be_u16, be_u32, be_u64, be_u8, Err, ErrorKind};
use nom::{be_u16, be_u32, be_u64, be_u8, Err, ErrorKind, IResult};

use super::*;
use constants;
88 changes: 43 additions & 45 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
//! [Common structures specification](https://geti2p.net/spec/common-structures)

use cookie_factory::GenError;
use nom::{Err, IResult};
use nom::{self, Needed};
use rand::{OsRng, Rng};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
@@ -23,6 +23,39 @@ use crypto::{
#[allow(needless_pass_by_value)]
pub(crate) mod frame;

/// Data read errors
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ReadError {
FileIo(String),
Incomplete(Needed),
Parser,
}

impl fmt::Display for ReadError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ReadError::FileIo(e) => format!("File IO error: {}", e).fmt(f),
ReadError::Incomplete(n) => format!("Data is incomplete (needed: {:?})", n).fmt(f),
ReadError::Parser => "Parser error".fmt(f),
}
}
}

impl From<io::Error> for ReadError {
fn from(e: io::Error) -> Self {
ReadError::FileIo(format!("{}", e))
}
}

impl<T> From<nom::Err<T>> for ReadError {
fn from(e: nom::Err<T>) -> Self {
match e {
nom::Err::Incomplete(n) => ReadError::Incomplete(n),
_ => ReadError::Parser,
}
}
}

//
// Simple data types
//
@@ -135,14 +168,6 @@ pub enum Certificate {
}

impl Certificate {
pub fn from(buf: &[u8]) -> Option<Self> {
match frame::certificate(buf) {
Ok((_, s)) => Some(s),
Err(Err::Incomplete(_)) => None,
Err(Err::Error(_)) | Err(Err::Failure(_)) => panic!("Unsupported Certificate"),
}
}

pub fn code(&self) -> u8 {
match *self {
Certificate::Null => constants::NULL_CERT,
@@ -165,21 +190,12 @@ pub struct RouterIdentity {
}

impl RouterIdentity {
pub fn from_file(path: &str) -> io::Result<Self> {
pub fn from_file(path: &str) -> Result<Self, ReadError> {
let mut rid = File::open(path)?;
let mut data: Vec<u8> = Vec::new();
rid.read_to_end(&mut data)?;
match frame::router_identity(&data[..]) {
Ok((_, res)) => Ok(res),
Err(Err::Incomplete(n)) => Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
format!("needed: {:?}", n),
)),
Err(Err::Error(e)) | Err(Err::Failure(e)) => Err(io::Error::new(
io::ErrorKind::Other,
e.into_error_kind().description(),
)),
}
let (_, res) = frame::router_identity(&data[..])?;
Ok(res)
}

fn from_keys(public_key: PublicKey, signing_key: SigningPublicKey) -> Self {
@@ -261,21 +277,12 @@ impl RouterSecretKeys {
}
}

pub fn from_file(path: &str) -> io::Result<Self> {
pub fn from_file(path: &str) -> Result<Self, ReadError> {
let mut rsk = File::open(path)?;
let mut data: Vec<u8> = Vec::new();
rsk.read_to_end(&mut data)?;
match frame::router_secret_keys(&data[..]) {
Ok((_, res)) => Ok(res),
Err(Err::Incomplete(n)) => Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
format!("needed: {:?}", n),
)),
Err(Err::Error(e)) | Err(Err::Failure(e)) => Err(io::Error::new(
io::ErrorKind::Other,
e.into_error_kind().description(),
)),
}
let (_, res) = frame::router_secret_keys(&data[..])?;
Ok(res)
}

pub fn to_bytes(&self) -> Vec<u8> {
@@ -437,21 +444,12 @@ impl RouterInfo {
.map(|a| (*a).clone())
}

pub fn from_file(path: &str) -> io::Result<Self> {
pub fn from_file(path: &str) -> Result<Self, ReadError> {
let mut ri = File::open(path)?;
let mut data: Vec<u8> = Vec::new();
ri.read_to_end(&mut data)?;
match frame::router_info(&data[..]) {
Ok((_, res)) => Ok(res),
Err(Err::Incomplete(n)) => Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
format!("needed: {:?}", n),
)),
Err(Err::Error(e)) | Err(Err::Failure(e)) => Err(io::Error::new(
io::ErrorKind::Other,
e.into_error_kind().description(),
)),
}
let (_, res) = frame::router_info(&data[..])?;
Ok(res)
}

pub fn to_bytes(&self) -> Vec<u8> {
4 changes: 2 additions & 2 deletions src/router/builder.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ use super::{
types::{CommSystem, InboundMessageHandler, NetworkDatabase},
Config, Inner, MessageHandler, Router,
};
use data::{RouterInfo, RouterSecretKeys};
use data::{ReadError, RouterInfo, RouterSecretKeys};
use netdb::LocalNetworkDatabase;
use transport;

@@ -31,7 +31,7 @@ impl<'a> Builder<'a> {
}

/// Create a Builder from the given Config.
pub fn from_config(cfg: Config) -> io::Result<Self> {
pub fn from_config(cfg: Config) -> Result<Self, ReadError> {
let ntcp_addr = cfg.ntcp_addr;
let ntcp2_addr = cfg.ntcp2_addr;
let ntcp2_keyfile = cfg.ntcp2_keyfile;