[Haskell-cafe] Bit fiddling

Thomas Hallgren hallgren at cse.ogi.edu
Tue May 17 19:29:34 EDT 2005


Hi,

Florian Weimer wrote:

>I'm toying a bit with Haskell and wondering what's the best way to
>implement bit fiddling.  Most of my applications involve serializing
>and deserializing small blobs (IP packets, for instance), and after
>browsing the GHC library documentation, I'm not sure which appraoch I
>should use.  That's why I'd appreciate pointers to sample code.
>  
>
In the protocol stack in House [1], I have used parsing/unparsing 
combinators to (de)serialize network packets. This allows the code to be 
simple, pure and modular, even though you are dealing with data down to 
the bit level.

The way things work at the moment, the network device driver returns 
incoming packets as unboxed arrays of bytes (values of type UArray Int 
Word8), and the parsing combinators maintain these along with position 
information down to the bit level, allowing you to parse input bit by 
bit. The process is reversed in the unparsing combinators. (What would 
be nice to have for this is efficient library functions for manipulation 
of bit vectors of arbitrary dynamic size...)

The source code is available on the web, see for example modules 
Net.Ethernet, Net.ARP, Net.ICMP, Net.IPv4:

    http://www.cse.ogi.edu/~hallgren/House/kernel/#Net

Note though that this is work in progress, there is not much 
documentation, and the code for several of the protocols is incomplete.

-- 
Thomas H

[1] http://www.cse.ogi.edu/~hallgren/House/



More information about the Haskell-Cafe mailing list