Saifi,
That's Ez Yaar....
import BitSyntax
import qualified Data.ByteString as BS
decodeOptions bs ([_, hlen], _, _, _, _, _, _, _, _, _) =
if hlen > 5
then BS.splitAt (fromIntegral ((hlen - 5) * 4)) bs
else (BS.empty, bs)
ipDecode = $(bitSyn [PackedBits [4, 4], Unsigned 1, Unsigned 2, Unsigned 2,
PackedBits [3, 13], Unsigned 1, Unsigned 1, Unsigned 2,
Fixed 4, Fixed 4, Context "decodeOptions", Rest])
ipPacket = BS.pack [0x45, 0, 0, 0x34, 0xd8, 0xd2, 0x40, 0, 0x40, 0x06,
0xa0, 0xca, 0xac, 0x12, 0x68, 0x4d, 0xac, 0x18,
0x00, 0xaf]
Also, I am not a Haskell expert, that's a presumption you are making.
This example uses the bitsyntax library which is not native to Haskell
but very popular and was built to counter Erlang's native bit pattern
matching capability.
You could use Data.Binary and it would be native and even quicker, but
much more gruesome. I don't plan on using Haskell for any networking
apps so this was just something I found to counter your challenge
:)...
I do however, love learning atleast 2 new languages a year, because it
makes me better at my job - which at this point involves unlocking
value for our customers through pragmatic compromises.
You can't be pragmatic without understanding the world around you. :)
Also, you are showing me an example that is probably the single most
popular example I have ever seen with Erlang. Everytime you see Joe
Armstrong talk about it - this is his showcase example.
Erlang, as you know, was born for telecom switches and if it couldn't
parse a network header right then what good would it be?
I am trying to understand the purpose of this thread, but perhaps the
next response from you will clear it for me.
Cheers,
Zubin.
On Thu, Jul 17, 2008 at 5:16 AM, Saifi Khan <[EMAIL PROTECTED]>
wrote:
> Hi Zubin:
>
> Here is a code fragment from my Erlang server codebase.
>
> <<IPVer:4,
> HLen:4,
> SrvcType:8,
> TotLen:16,
> ID:16,
> Flgs:3,
> FragOff:13,
> TTL:8,
> Proto:8,
> HdrChkSum:16,
> SrcIP:32,
> DestIP:32,
> RestDgram/binary>> = Datagram
>
> i'm using the Erlang Bit syntax expression to pattern match
> and extract individual bits in the binary IP datagram.
>
> As you are Haskell expert, could you please share your
> knowledge of how to write this piece of code in Haskell ?
>
> Thank you in advance.
>
> thanks
> Saifi.
>