-
Notifications
You must be signed in to change notification settings - Fork 14
[Refactor] Add network sockaddr data structure in sysdefs lib #162
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3c996da
fix & cleanup lindtool
qianxichen233 25217dd
remove commented code
qianxichen233 5479993
Fix missing shebang on lindtool.sh
m-hemmings 6bc34ef
Merge pull request #136 from Lind-Project/fix-lindtool
rennergade a6ae428
Add network sockaddr data structure in sysdefs lib
robinyuan1002 1ab152e
update net_struct
robinyuan1002 886d248
add a new line
robinyuan1002 72d0674
delete poll/epoll related things
robinyuan1002 081f443
update comment
robinyuan1002 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,241 +1,9 @@ | ||
| /// THIS FILE NEEDS TO BE REFACTORED LATER! | ||
| /// | ||
| use std::str::from_utf8; | ||
| use std::sync::atomic::{AtomicUsize, Ordering}; | ||
| use libc::*; | ||
|
|
||
| extern crate libc; | ||
|
|
||
| static mut UD_ID_COUNTER: AtomicUsize = AtomicUsize::new(0); | ||
|
|
||
| #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] | ||
| pub enum GenSockaddr { | ||
| Unix(SockaddrUnix), | ||
| V4(SockaddrV4), | ||
| V6(SockaddrV6), | ||
| } | ||
| impl GenSockaddr { | ||
| pub fn port(&self) -> u16 { | ||
| match self { | ||
| GenSockaddr::Unix(_) => panic!("Invalid function called for this type of Sockaddr."), | ||
| GenSockaddr::V4(v4addr) => v4addr.sin_port, | ||
| GenSockaddr::V6(v6addr) => v6addr.sin6_port, | ||
| } | ||
| } | ||
| pub fn set_port(&mut self, port: u16) { | ||
| match self { | ||
| GenSockaddr::Unix(_) => panic!("Invalid function called for this type of Sockaddr."), | ||
| GenSockaddr::V4(v4addr) => v4addr.sin_port = port, | ||
| GenSockaddr::V6(v6addr) => v6addr.sin6_port = port, | ||
| }; | ||
| } | ||
|
|
||
| pub fn addr(&self) -> GenIpaddr { | ||
| match self { | ||
| GenSockaddr::Unix(_) => panic!("Invalid function called for this type of Sockaddr."), | ||
| GenSockaddr::V4(v4addr) => GenIpaddr::V4(v4addr.sin_addr), | ||
| GenSockaddr::V6(v6addr) => GenIpaddr::V6(v6addr.sin6_addr), | ||
| } | ||
| } | ||
|
|
||
| pub fn set_addr(&mut self, ip: GenIpaddr) { | ||
| match self { | ||
| GenSockaddr::Unix(_unixaddr) => { | ||
| panic!("Invalid function called for this type of Sockaddr.") | ||
| } | ||
| GenSockaddr::V4(v4addr) => { | ||
| v4addr.sin_addr = if let GenIpaddr::V4(v4ip) = ip { | ||
| v4ip | ||
| } else { | ||
| unreachable!() | ||
| } | ||
| } | ||
| GenSockaddr::V6(v6addr) => { | ||
| v6addr.sin6_addr = if let GenIpaddr::V6(v6ip) = ip { | ||
| v6ip | ||
| } else { | ||
| unreachable!() | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| pub fn set_family(&mut self, family: u16) { | ||
| match self { | ||
| GenSockaddr::Unix(unixaddr) => unixaddr.sun_family = family, | ||
| GenSockaddr::V4(v4addr) => v4addr.sin_family = family, | ||
| GenSockaddr::V6(v6addr) => v6addr.sin6_family = family, | ||
| }; | ||
| } | ||
|
|
||
| pub fn get_family(&self) -> u16 { | ||
| match self { | ||
| GenSockaddr::Unix(unixaddr) => unixaddr.sun_family, | ||
| GenSockaddr::V4(v4addr) => v4addr.sin_family, | ||
| GenSockaddr::V6(v6addr) => v6addr.sin6_family, | ||
| } | ||
| } | ||
|
|
||
| pub fn path(&self) -> &str { | ||
| match self { | ||
| GenSockaddr::Unix(unixaddr) => { | ||
| let pathiter = &mut unixaddr.sun_path.split(|idx| *idx == 0); | ||
| let pathslice = pathiter.next().unwrap(); | ||
| let path = from_utf8(pathslice).unwrap(); | ||
| path | ||
| } | ||
| GenSockaddr::V4(_) => panic!("Invalid function called for this type of Sockaddr."), | ||
| GenSockaddr::V6(_) => panic!("Invalid function called for this type of Sockaddr."), | ||
| } | ||
| // create a sockaddr_un struct | ||
| pub fn create_sockaddr_un() -> sockaddr_un{ | ||
| sockaddr_un { | ||
| sun_family: 0, | ||
| sun_path: [0; 108], | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)] | ||
| pub enum GenIpaddr { | ||
| V4(V4Addr), | ||
| V6(V6Addr), | ||
| } | ||
|
|
||
| impl GenIpaddr { | ||
| pub fn is_unspecified(&self) -> bool { | ||
| match self { | ||
| GenIpaddr::V4(v4ip) => v4ip.s_addr == 0, | ||
| GenIpaddr::V6(v6ip) => v6ip.s6_addr == [0; 16], | ||
| } | ||
| } | ||
| pub fn from_string(string: &str) -> Option<Self> { | ||
| let v4candidate: Vec<&str> = string.split('.').collect(); | ||
| let v6candidate: Vec<&str> = string.split(':').collect(); | ||
| let v4l = v4candidate.len(); | ||
| let v6l = v6candidate.len(); | ||
| if v4l == 1 && v6l > 1 { | ||
| //then we should try parsing it as an ipv6 address | ||
| let mut shortarr = [0u8; 16]; | ||
| let mut shortindex = 0; | ||
| let mut encountered_doublecolon = false; | ||
| for short in v6candidate { | ||
| if short.is_empty() { | ||
| //you can only have a double colon once in an ipv6 address | ||
| if encountered_doublecolon { | ||
| return None; | ||
| } | ||
| encountered_doublecolon = true; | ||
|
|
||
| let numzeros = 8 - v6l + 1; //+1 to account for this empty string element | ||
| if numzeros == 0 { | ||
| return None; | ||
| } | ||
| shortindex += numzeros; | ||
| } else { | ||
| //ok we can actually parse the element in this case | ||
| if let Ok(b) = short.parse::<u16>() { | ||
| //manually handle big endianness | ||
| shortarr[2 * shortindex] = (b >> 8) as u8; | ||
| shortarr[2 * shortindex + 1] = (b & 0xff) as u8; | ||
| shortindex += 1; | ||
| } else { | ||
| return None; | ||
| } | ||
| } | ||
| } | ||
| return Some(Self::V6(V6Addr { s6_addr: shortarr })); | ||
| } else if v4l == 4 && v6l == 1 { | ||
| //then we should try parsing it as an ipv4 address | ||
| let mut bytearr = [0u8; 4]; | ||
| let mut shortindex = 0; | ||
| for byte in v4candidate { | ||
| if let Ok(b) = byte.parse::<u8>() { | ||
| bytearr[shortindex] = b; | ||
| shortindex += 1; | ||
| } else { | ||
| return None; | ||
| } | ||
| } | ||
| return Some(Self::V4(V4Addr { | ||
| s_addr: u32::from_ne_bytes(bytearr), | ||
| })); | ||
| } else { | ||
| return None; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] | ||
| pub struct SockaddrUnix { | ||
| pub sun_family: u16, | ||
| pub sun_path: [u8; 108], | ||
| } | ||
|
|
||
| impl Default for SockaddrUnix { | ||
| fn default() -> Self { | ||
| SockaddrUnix { | ||
| sun_family: 0, | ||
| sun_path: [0; 108], | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn new_sockaddr_unix(family: u16, path: &[u8]) -> SockaddrUnix { | ||
| let pathlen = path.len(); | ||
| if pathlen > 108 { | ||
| panic!("Unix domain paths cannot exceed 108 bytes.") | ||
| } | ||
| let mut array_path: [u8; 108] = [0; 108]; | ||
| array_path[0..pathlen].copy_from_slice(path); | ||
| SockaddrUnix { | ||
| sun_family: family, | ||
| sun_path: array_path, | ||
| } | ||
| } | ||
|
|
||
| pub fn gen_ud_path() -> String { | ||
| let mut owned_path: String = "/sock".to_owned(); | ||
| unsafe { | ||
| let id = UD_ID_COUNTER.fetch_add(1, Ordering::Relaxed); | ||
| owned_path.push_str(&id.to_string()); | ||
| } | ||
| owned_path.clone() | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Default)] | ||
| pub struct V4Addr { | ||
| pub s_addr: u32, | ||
| } | ||
| #[repr(C)] | ||
| #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Default)] | ||
| pub struct SockaddrV4 { | ||
| pub sin_family: u16, | ||
| pub sin_port: u16, | ||
| pub sin_addr: V4Addr, | ||
| pub padding: u64, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Default)] | ||
| pub struct V6Addr { | ||
| pub s6_addr: [u8; 16], | ||
| } | ||
| #[repr(C)] | ||
| #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Default)] | ||
| pub struct SockaddrV6 { | ||
| pub sin6_family: u16, | ||
| pub sin6_port: u16, | ||
| pub sin6_flowinfo: u32, | ||
| pub sin6_addr: V6Addr, | ||
| pub sin6_scope_id: u32, | ||
| } | ||
|
|
||
| #[derive(Debug, Default)] | ||
| #[repr(C)] | ||
| pub struct PollStruct { | ||
| pub fd: i32, | ||
| pub events: i16, | ||
| pub revents: i16, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| pub struct SockaddrDummy { | ||
| pub sa_family: u16, | ||
| pub _sa_data: [u16; 14], | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.