szymonwieloch / rust-rawsock Goto Github PK
View Code? Open in Web Editor NEWRust library for obtaining and sending raw network packets from interfaces.
License: MIT License
Rust library for obtaining and sending raw network packets from interfaces.
License: MIT License
I'm using wpcap
as backend. My program uses multithreading to send packets, but it panics when the rate is fast.
thread '<unnamed>' panicked at 'assertion failed: `(left == right)`
left: `-1`,
right: `0`', .....\rawsock-0.3.0\src\wpcap\interface.rs:85:13
Maybe instead of panic here, return a Result::Err()
. This is helpful for error handling
It seems that we prefer npcap
. But when WinPcap
and npcap
coexist, rawsock
actually loads WinPcap
.
rust-rawsock/src/wpcap/paths.rs
Lines 3 to 8 in 53408bd
Since Npcap\wpcap.dll
searchs Packet.dll
instead of Npcap\Packet.dll
(could be seen using procmon), and Npcap\wpcap.dll
wouldn't load correctly with wrong Packet.dll
.
And because what we need is wpcap.dll
, I think all Packet.dll
could be removed from default paths.
Solution:
I think it should be an issue about dlopen.
According to the document (npcap-sdk-1.03/docs/npcap-devguide.html#npcap-feature-native
in npcap-sdk-1.03.zip), we need to call SetDllDirectory
to make wpcap.dll
loading right dll
.
You need to do the following one step:
Before calling LoadLibrary to load wpcap.dll, call SetDllDirectory to add C:\Windows\System32\Npcap\ to DLL search path.
Command: cargo run --example dynamic
Opening packet capturing library
Library opened, version is pcap libpcap version 1.8.1 -- Apple version 79.200.4
Opening the en0 interface
Interface opened, data link: ethernet
Sending 5 packets:
Sending ICMP ping packet no 0
Sending ICMP ping packet no 1
Sending ICMP ping packet no 2
Sending ICMP ping packet no 3
Sending ICMP ping packet no 4
Receiving 5 packets:
[1] 20518 segmentation fault cargo run --example dynamic
I think uncomment this two line should work.
rust-rawsock/src/pcap_common.rs
Lines 63 to 64 in b55d6b2
pcap.h
struct pcap_pkthdr {
struct timeval ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
#ifdef __APPLE__
char comment[256];
#endif
};
tv_usec
is 64bit unsigned integer.
rust-rawsock/src/pcap_common/structs.rs
Line 73 in 88801a2
But on macOS, tv_usec
is 32bit unsigned integer with 32bit padding:
/usr/include/sys/_types/_timeval.h
#ifndef _STRUCT_TIMEVAL
#define _STRUCT_TIMEVAL struct timeval
#include <machine/types.h> /* __darwin_time_t */
#include <sys/_types.h> /* __darwin_suseconds_t */
_STRUCT_TIMEVAL
{
__darwin_time_t tv_sec; /* seconds */
__darwin_suseconds_t tv_usec; /* and microseconds */
};
#endif /* _STRUCT_TIMEVAL */
/usr/include/sys/_types.h
typedef __int32_t __darwin_suseconds_t; /* [???] microseconds */
And using ida to see how the struct looks like:
00000000 timeval struc ; (sizeof=0x10, align=0x8, copyof_63)
00000000 ; XREF: _uv__fs_futime/r
00000000 ; _uv__fs_utime/r ...
00000000 tv_sec dq ? ; XREF: _uv_getrusage:loc_1001258B9/r
00000000 ; _uv_getrusage+56/r ...
00000008 tv_usec dd ? ; XREF: _uv_getrusage+47/r
00000008 ; _uv_getrusage+65/r ...
0000000C db ? ; undefined
0000000D db ? ; undefined
0000000E db ? ; undefined
0000000F db ? ; undefined
00000010 timeval ends
With this declaration, rawsock
works fine on my machine.
///Equivalent of C struct timeval_t
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct TimeVal {
pub tv_sec: c_long, /* seconds */
pub tv_usec: c_uint /* and microseconds */
}
the tag name should be v0.2.1
Hey,
I was going to look into making this work with PF_RING ZC if it doesn't already...
Is that something you've considered / looked at?
Very new to both Rust and userspace networking but currently building a low-latency network application and loving this library already!
Cheers
btw I also want trait Interface
and Library
impls Send
and Sync
for multi-thread. Because I need to break loop from other thread.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.