Articles tagged #ffi
From Inkscape to poppler
What's next? Well... poppler is the library Inkscape uses to import PDFs.
Cool bear's hot tip
Yes, the name comes from Futurama.
Turns out, poppler comes with a bunch of CLI tools, including pdftocairo
!
Halfway through this article, I realized the "regular weight" on my system was in fact Iosevka SS01 (Andale Mono Style) (see ), but the "bold weight" was the default Iosevka.
Crafting ARP packets to find a remote host's MAC address
Alright. ALRIGHT. I know, we're all excited, but let's think about what we're doing again.
So we've managed to look at real network traffic and parse it completely. We've also taken some ICMP packets, parsed them, and then serialized them right back and we got the exact same result.
So I know what you're thinking - let's just move our way down the stack again - stuff that ICMP packet in an IP packet, then in an Ethernet frame, and then serialize the whole thing.
Binding C APIs with variable-length structs and UTF-16
Okay, I lied.
I'm deciding - right this instant - that using wmic is cheating too. Oh, it was fair game when we were learning about Windows, but we're past that now.
We know there's IPv4 routing tables, and we know network interfaces have indices (yes, they do change when you disable/enable one, so ill-timed configuration changes may make our program blow up).
A simple ping library, parsing strings into IPv4 address
We've just spent a lot of time abstracting over LoadLibrary, but we still have all the gory details of the Win32 ICMP API straight in our main.rs file! That won't do.
This time will be much quicker, since we already learned about carefully designing an API, hiding the low-level bits and so on.
Let's add an icmp
module to our program. Actually, we've been dealing with
an all this time, it also sounds like it could use its own package:
Designing and implementing a safer API on top of LoadLibrary
It's refactor time!
Our complete program is now about a hundred lines, counting blank lines (see the end of part 3 for a complete listing).
While this is pretty good for a zero-dependency project (save for
pretty-hex
), we can do better.
First off, concerns are mixed up. In the same file, we:
- Expose
LoadLibraryA
/GetProcAddress
- Expose the Win32 ICMP API
FFI-safe types in Rust, newtypes and MaybeUninit
It's time to make sup
, our own take on ping
, use the Win32 APIs to send
an ICMP echo. Earlier we discovered that Windows's ping.exe
used
IcmpSendEcho2Ex
. But for our purposes, the simpler IcmpSendEcho
will do
just fine.
As we mentioned earlier, it's provided by IPHLPAPI.dll
, and its C
declaration is:
IPHLPAPI_DLL_LINKAGE DWORD IcmpSendEcho(
,
,
,
,
,
,
,
);
Windows dynamic libraries, calling conventions, and transmute
So, how does ping.exe
actually send a ping? It seems unrealistic that
ping.exe
itself implements all the protocols involved in sending a ping.
So it must be calling some sort of library. Also, since it ends up
talking to the outside world via a NIC (network interface controller),
the kernel is probably involved at some point.
In reading files the hard way - part 2, we learned about dynamic libraries (like libc), and the Linux kernel, and how syscalls allowed us to ask the Linux kernel to do our bidding. For this series, we're going to have to look at the Windows equivalents.
Go back to the homepage.