[Haskell-cafe] Interpreting fields in a buffer

Dominic Steinitz dominic.steinitz at blueyonder.co.uk
Mon Jan 26 16:13:27 EST 2004


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))

----- Original Message ----- 
>
> Date: Sun, 25 Jan 2004 22:44:58 -0500
> From: Gregory Wright <gwright at antiope.com>
> To: haskell-cafe at haskell.org
> Subject: [Haskell-cafe] Interpreting fields in a buffer
> Message-ID: <0356DB09-4FB2-11D8-A3D2-00039398F084 at antiope.com>
> Content-Type: text/plain; charset=US-ASCII; format=flowed
> MIME-Version: 1.0 (Apple Message framework v609)
> Content-Transfer-Encoding: 7bit
> Precedence: list
> Message: 1
>
>
> 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 <packet_struct_ptr> ->
> <field>.
>
> 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). Most fields won't be examined
> but I can't
> tell in advance which ones will have to be looked at.
>
> I've not seen an example of this kind and was wondering if this was
> especially awkward.
>
> Thanks.
>
> Best Wishes,
> Greg
>
> Gregory Wright
> Antiope Associates LLC
> 18 Clay Street
> Fair Haven, New Jersey 07704
>
> gwright at antiope.com
>



More information about the Haskell-Cafe mailing list