Move common definitions into netutils
This commit is contained in:
parent
63cff7bc48
commit
d0abe6335a
21 changed files with 64 additions and 808 deletions
|
@ -3,5 +3,6 @@ name = "ethernetd"
|
|||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
netutils = { path = "../../programs/netutils/" }
|
||||
resource_scheme = { path = "../../crates/resource_scheme/" }
|
||||
syscall = { path = "../../syscall/" }
|
||||
|
|
|
@ -1,105 +0,0 @@
|
|||
use std::{mem, slice, u8, u16};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(packed)]
|
||||
pub struct n16(u16);
|
||||
|
||||
impl n16 {
|
||||
pub fn new(value: u16) -> Self {
|
||||
n16(value.to_be())
|
||||
}
|
||||
|
||||
pub fn get(&self) -> u16 {
|
||||
u16::from_be(self.0)
|
||||
}
|
||||
|
||||
pub fn set(&mut self, value: u16) {
|
||||
self.0 = value.to_be();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct MacAddr {
|
||||
pub bytes: [u8; 6],
|
||||
}
|
||||
|
||||
impl MacAddr {
|
||||
pub fn equals(&self, other: Self) -> bool {
|
||||
for i in 0..6 {
|
||||
if self.bytes[i] != other.bytes[i] {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn from_str(string: &str) -> Self {
|
||||
let mut addr = MacAddr { bytes: [0, 0, 0, 0, 0, 0] };
|
||||
|
||||
let mut i = 0;
|
||||
for part in string.split('.') {
|
||||
let octet = u8::from_str_radix(part, 16).unwrap_or(0);
|
||||
match i {
|
||||
0 => addr.bytes[0] = octet,
|
||||
1 => addr.bytes[1] = octet,
|
||||
2 => addr.bytes[2] = octet,
|
||||
3 => addr.bytes[3] = octet,
|
||||
4 => addr.bytes[4] = octet,
|
||||
5 => addr.bytes[5] = octet,
|
||||
_ => break,
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
addr
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
let mut string = String::new();
|
||||
for i in 0..6 {
|
||||
if i > 0 {
|
||||
string.push('.');
|
||||
}
|
||||
string.push_str(&format!("{:X}", self.bytes[i]));
|
||||
}
|
||||
string
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(packed)]
|
||||
pub struct EthernetIIHeader {
|
||||
pub dst: MacAddr,
|
||||
pub src: MacAddr,
|
||||
pub ethertype: n16,
|
||||
}
|
||||
|
||||
pub struct EthernetII {
|
||||
pub header: EthernetIIHeader,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl EthernetII {
|
||||
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
|
||||
if bytes.len() >= mem::size_of::<EthernetIIHeader>() {
|
||||
unsafe {
|
||||
return Some(EthernetII {
|
||||
header: *(bytes.as_ptr() as *const EthernetIIHeader),
|
||||
data: bytes[mem::size_of::<EthernetIIHeader>() ..].to_vec(),
|
||||
});
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
unsafe {
|
||||
let header_ptr: *const EthernetIIHeader = &self.header;
|
||||
let mut ret = Vec::from(slice::from_raw_parts(header_ptr as *const u8,
|
||||
mem::size_of::<EthernetIIHeader>()));
|
||||
ret.extend_from_slice(&self.data);
|
||||
ret
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
extern crate netutils;
|
||||
extern crate resource_scheme;
|
||||
extern crate syscall;
|
||||
|
||||
|
@ -10,9 +11,8 @@ use syscall::Packet;
|
|||
|
||||
use scheme::EthernetScheme;
|
||||
|
||||
pub mod common;
|
||||
pub mod resource;
|
||||
pub mod scheme;
|
||||
mod resource;
|
||||
mod scheme;
|
||||
|
||||
fn main() {
|
||||
thread::spawn(move || {
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
use std::{cmp, mem};
|
||||
|
||||
use netutils::{n16, MacAddr, EthernetIIHeader, EthernetII};
|
||||
use resource_scheme::Resource;
|
||||
use syscall;
|
||||
use syscall::error::*;
|
||||
|
||||
use common::{n16, MacAddr, EthernetIIHeader, EthernetII};
|
||||
|
||||
/// A ethernet resource
|
||||
pub struct EthernetResource {
|
||||
/// The network
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use std::{str, u16};
|
||||
|
||||
use netutils::{MacAddr, EthernetII};
|
||||
use resource_scheme::ResourceScheme;
|
||||
use syscall;
|
||||
use syscall::error::{Error, Result, EACCES, ENOENT, EINVAL};
|
||||
use syscall::flag::O_RDWR;
|
||||
|
||||
use common::{MacAddr, EthernetII};
|
||||
use resource::EthernetResource;
|
||||
|
||||
pub struct EthernetScheme;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue