szymonwieloch / rust-rawsock

Rust library for obtaining and sending raw network packets from interfaces.
MIT License
64 stars 16 forks source link

Crash on macOS #2

Closed spacemeowx2 closed 4 years ago

spacemeowx2 commented 4 years ago

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.

https://github.com/szymonwieloch/rust-rawsock/blob/b55d6b2d5d35f9eda9961b9716a66a8c1d8a5798/src/pcap_common.rs#L63-L64

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
};
szymonwieloch commented 4 years ago

@spacemeowx2 Hi! Thank you for the report. I have already fixed it in the newest version. But I have problems with testing because I work on PC. I have Linux and Windows but not Apple machine. Automatic tests on Travis are possible but they allow only unit tests, sending and receiving packets is not possible (or at least I don't know how to setup the environment). Could you please test my change and let me know if it works? I'll be very grateful.

Regards, Szymon

spacemeowx2 commented 4 years ago

Thank you for the fix. This fix has compile time error:

error[E0063]: missing field `comment` in initializer of `pcap_common::structs::PCapPacketHeader`
  --> src/wpcap/interface.rs:69:22
   |
69 |         let header = PCapPacketHeader {
   |                      ^^^^^^^^^^^^^^^^ missing `comment`

error[E0277]: `[i8; 256]` doesn't implement `std::fmt::Debug`
  --> src/pcap_common/structs.rs:65:5
   |
65 |     pub comment: [c_char; 256]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ `[i8; 256]` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
   |
   = help: the trait `std::fmt::Debug` is not implemented for `[i8; 256]`
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `&[i8; 256]`
   = note: required for the cast to the object type `dyn std::fmt::Debug`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0063, E0277.
For more information about an error, try `rustc --explain E0063`.
error: Could not compile `rawsock`.

To learn more, run the command again with --verbose.

You can see how did I fix these issues, but it breaks Windows compile because wpcap builds on both macOS and Windows. I think wpcap is only available on Windows so could you make wpcap only build on Windows?

https://github.com/spacemeowx2/rust-rawsock/commit/7f0d42526d3061e1d2ad7eb270fca1e4a1864d8e

spacemeowx2 commented 4 years ago

https://github.com/spacemeowx2/rust-rawsock/commit/b38742ad127426d6f8d57242ac5f70b7b44c6ac6

I tried to disable wpcap on non-Windows OS. Not sure am I doing right thing. I'm new to rust.

szymonwieloch commented 4 years ago

@spacemeowx2 You are right - my fix broke the build on MacOS. It was more tricky than I thought. But now Travis shows correct build, I think it should be OK now, you can try it.

Your approach with disabling wpcap on MacOS is very problematic. Writing multiplatform code is super hard if some symbols are available on one platform but not on the other. This is the purpose of rawsock - to cover all platforms with just a single API, so that nobody has problems writing multiplatform code. This is why wpcap is available on all platforms but loading the library will always fail except on Windows. In similar way pfring library is only available on Linux but the module compiles on all platforms.

Travis shows that the build works now on all platforms. Should be OK.