Interpreting fields in a buffer

Hi,
I have a question related to a program I'm writing. I have to handle IP
packets,
which will be read into a buffer. What is the best haskell idiom for
getting access
to the fields in the buffer?
The IP header has a number of fixed format fields. In C, I would define
a struct,
then cast the pointer to the beginning of the buffer to a pointer to
the struct. I would then be
able to access each field in the header as

I have a question related to a program I'm writing. I have to handle IP packets, which will be read into a buffer. What is the best haskell idiom for getting access to the fields in the buffer?
There's no way in Haskell to define a datastructure with a particular memory layout. (Strictly, there's no way to do it in ANSI C either but it's easy enough to work with what C gives you.) To access the fields, you will need to write a bunch of functions to read them out (and write them if you need). There's basically two approaches: 1) Write access functions in C and use the ffi interface to call them. For example, you might have a function to read the TTL field given a pointer to the IP header. 2) Write access functions in Haskell using functions from the Storable class and associated libraries. http://www.haskell.org/ghc/docs/latest/html/libraries/base/Foreign.Storable.... To do this, you need to know the offset (in bytes) of each field of the data structure. hsc2hs is great for doing this kind of thing.
Is there a way in haskell to declare the format of the header, then access the components of the buffer as elements of those types? I'm only going to do read access on the buffer (an unboxed array).
I would lean towards leaving the buffer over in C land instead of loading it into a Haskell buffer. (Of course, it depends a bit on how much processing of the packet you will be doing in Haskell.) Hope this helps, -- Alastair Reid www.haskell-consulting.com

On Mon, Jan 26, 2004 at 10:34:06AM +0000, Alastair Reid wrote:
To access the fields, you will need to write a bunch of functions to read them out (and write them if you need). There's basically two approaches:
1) Write access functions in C and use the ffi interface to call them. For example, you might have a function to read the TTL field given a pointer to the IP header.
2) Write access functions in Haskell using functions from the Storable class and associated libraries.
3) Roll your own (de)serialization framework That's what I did. It's a bit complicated, but I will try to describe it within a couple of days. Right now all I can say is that it uses TH and has a couple of implementations of low-lever decoders, one of which reads directly from UArray Int Word8. I managed to achieve throughput of 3 MB / s for quite complicated binary protocols, and I think I can improve that even further. Best regards, Tom -- .signature: Too many levels of symbolic links

Alastair Reid wrote:
2) Write access functions in Haskell using functions from the Storable class and associated libraries.
In this case, using Storable probably isn't worth the trouble, given
that
1. The 16- and 32-bit fields are in network byte order, whereas
Storable assumes host byte order.
2. If you want to read certain IP options (e.g. record route), you
need to read unaligned words.
It would be simpler to just read bytes, and piece them together
yourself.
--
Glynn Clements
participants (4)
-
Alastair Reid
-
Glynn Clements
-
Gregory Wright
-
Tomasz Zielonka