[Haskell-cafe] Interpreting fields in a buffer

Gregory Wright gwright at comcast.net
Mon Jan 26 12:22:47 EST 2004



Hi Dominic,

First, thanks to everyone for their help.

RIght now, I'm leaning toward Dominic's solution of a collection
of helper functions, but I have the feeling that we should be generating
them automatically. After all, given a buffer that consists of packed,
fixed width fields, if we specify a name and width of the field, making
the helper functions should be straightforward. (I understand the
network byte order issues. Since I won't always have to examine each
field, I wasn't going to convert all fields to host byte order, rather 
only
convert the ones I needed to examine.)

It's a little strange that we seem to have to use template haskell or
some other preprocessor to do this. Rather like using a sledgehammer
to crack a walnut.

I'll try to come up with something of general use. I'm very interested 
to
see Tom's (de)serialization framework. As long as this problem is going
to require TH, we may as well solve more than one problem.

And now I let you return to your discussion of the tab character ;-)

Best Wishes,
Greg



On Jan 26, 2004, at 11:13 AM, Dominic Steinitz wrote:

> Gregory,
>
> I don't know if this helps but I ended creating functions like the ones
> below (I didn't need to use the C structures because the IP definition 
> is
> language independent). I'm sure there are better ways and I didn't 
> test the
> throughput but I was able to develop a Haskell version of ping and
> traceroute (both multi-threaded). It would be nice if there were a 
> library
> so that we didn't end up re-inventing the wheel.
>
> Dominic.
>
> ipHeaderLength :: String -> Int
> ipHeaderLength s = (fromIntegral (ord (s !! 0)) .&. 0x0f) * 4
>
> ipTTL :: String -> Int
> ipTTL s = fromIntegral (ord (s !! 8))
>
> ipProtocol :: String -> IPProto
> ipProtocol s = toEnum (ord (s !! 9))
>
> ipDestAddr :: String -> HostAddress
> ipDestAddr s = fromIntegral (ord (s !! 16)) `shiftL` 24 +
>                fromIntegral (ord (s !! 17)) `shiftL` 16 +
>                fromIntegral (ord (s !! 18)) `shiftL` 8 +
>                fromIntegral (ord (s !! 19))
>



More information about the Haskell-Cafe mailing list