Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode/
.VSCodeCounter/
/target/
**/*.rs.bk
Cargo.lock
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
name = "packet"
name = "packet"
version = "0.1.2"
edition = "2018"

authors = ["meh. <meh@schizofreni.co>"]
license = "WTFPL"

description = "Network packet handling."
repository = "https://github.com/meh/rust-packet"
keywords = ["packet", "network", "ip", "tcp", "udp"]
repository = "https://github.com/meh/rust-packet"
keywords = ["packet", "network", "ip", "tcp", "udp"]

[dependencies]
thiserror = "1"
bitflags = "1"
bitflags = "2.4"
byteorder = "1"
hwaddr = "0.1"
hwaddr = "0.1"
130 changes: 65 additions & 65 deletions src/buffer/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,103 +19,103 @@ use crate::error::*;
/// A growable buffer.
#[derive(Clone, Eq, PartialEq, Default, Debug)]
pub struct Buffer {
inner: Vec<u8>,
inner: Vec<u8>,

offset: usize,
length: usize,
offset: usize,
length: usize,
}

impl Buffer {
/// Create a new growable buffer.
pub fn new() -> Self {
Default::default()
}
/// Create a new growable buffer.
pub fn new() -> Self {
Default::default()
}
}

impl super::Buffer for Buffer {
type Inner = Vec<u8>;
type Inner = Vec<u8>;

fn into_inner(self) -> Self::Inner {
self.inner
}
fn into_inner(self) -> Self::Inner {
self.inner
}

fn next(&mut self, size: usize) -> Result<()> {
self.offset += self.length;
self.length = size;
fn next(&mut self, size: usize) -> Result<()> {
self.offset += self.length;
self.length = size;

let current = self.inner.len();
self.inner.resize(current + size, 0);
let current = self.inner.len();
self.inner.resize(current + size, 0);

Ok(())
}
Ok(())
}

fn more(&mut self, size: usize) -> Result<()> {
let current = self.inner.len();
self.inner.resize(current + size, 0);
self.length += size;
fn more(&mut self, size: usize) -> Result<()> {
let current = self.inner.len();
self.inner.resize(current + size, 0);
self.length += size;

Ok(())
}
Ok(())
}

fn clear(&mut self) {
self.inner.clear();
self.offset = 0;
self.length = 0;
}
fn clear(&mut self) {
self.inner.clear();
self.offset = 0;
self.length = 0;
}

fn used(&self) -> usize {
self.inner.len()
}
fn used(&self) -> usize {
self.inner.len()
}

fn offset(&self) -> usize {
self.offset
}
fn offset(&self) -> usize {
self.offset
}

fn length(&self) -> usize {
self.length
}
fn length(&self) -> usize {
self.length
}

fn data(&self) -> &[u8] {
&self.inner[self.offset .. self.offset + self.length]
}
fn data(&self) -> &[u8] {
&self.inner[self.offset..self.offset + self.length]
}

fn data_mut(&mut self) -> &mut [u8] {
&mut self.inner[self.offset .. self.offset + self.length]
}
fn data_mut(&mut self) -> &mut [u8] {
&mut self.inner[self.offset..self.offset + self.length]
}
}

impl Into<Vec<u8>> for Buffer {
fn into(self) -> Vec<u8> {
self.inner
}
impl From<Buffer> for Vec<u8> {
fn from(buffer: Buffer) -> Self {
buffer.inner
}
}

impl AsRef<[u8]> for Buffer {
fn as_ref(&self) -> &[u8] {
use super::Buffer;
self.data()
}
fn as_ref(&self) -> &[u8] {
use super::Buffer;
self.data()
}
}

impl AsMut<[u8]> for Buffer {
fn as_mut(&mut self) -> &mut [u8] {
use super::Buffer;
self.data_mut()
}
fn as_mut(&mut self) -> &mut [u8] {
use super::Buffer;
self.data_mut()
}
}

impl Deref for Buffer {
type Target = [u8];
type Target = [u8];

fn deref(&self) -> &Self::Target {
use super::Buffer;
self.data()
}
fn deref(&self) -> &Self::Target {
use super::Buffer;
self.data()
}
}

impl DerefMut for Buffer {
fn deref_mut(&mut self) -> &mut Self::Target {
use super::Buffer;
self.data_mut()
}
fn deref_mut(&mut self) -> &mut Self::Target {
use super::Buffer;
self.data_mut()
}
}
40 changes: 20 additions & 20 deletions src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,35 @@ use crate::error::*;
/// assert_eq!(data.len(), 24);
/// ```
pub trait Buffer {
/// Inner type used by the buffer.
type Inner: AsMut<[u8]>;
/// Inner type used by the buffer.
type Inner: AsMut<[u8]>;

/// Convert the buffer into the inner type.
fn into_inner(self) -> Self::Inner;
/// Convert the buffer into the inner type.
fn into_inner(self) -> Self::Inner;

/// Go to the next layer requesting the given size, zeroeing the layer.
fn next(&mut self, size: usize) -> Result<()>;
/// Go to the next layer requesting the given size, zeroeing the layer.
fn next(&mut self, size: usize) -> Result<()>;

/// Request more memory for the same layer, zeroeing the new buffer area.
fn more(&mut self, size: usize) -> Result<()>;
/// Request more memory for the same layer, zeroeing the new buffer area.
fn more(&mut self, size: usize) -> Result<()>;

/// Clear the buffer.
fn clear(&mut self);
/// Clear the buffer.
fn clear(&mut self);

/// Number of bytes used by the whole buffer.
fn used(&self) -> usize;
/// Number of bytes used by the whole buffer.
fn used(&self) -> usize;

/// Offset from the beginning of the whole buffer.
fn offset(&self) -> usize;
/// Offset from the beginning of the whole buffer.
fn offset(&self) -> usize;

/// Length of the current layer.
fn length(&self) -> usize;
/// Length of the current layer.
fn length(&self) -> usize;

/// Get a slice over the current layer.
fn data(&self) -> &[u8];
/// Get a slice over the current layer.
fn data(&self) -> &[u8];

/// Get a mutable slice over the current layer.
fn data_mut(&mut self) -> &mut [u8];
/// Get a mutable slice over the current layer.
fn data_mut(&mut self) -> &mut [u8];
}

mod dynamic;
Expand Down
Loading