summaryrefslogtreecommitdiff
path: root/dht/LLCSNAP.hs
blob: 417c36ba310b203cddd3bfd0cfba1d7871074e03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module LLCSNAP where

import Data.Bits
import Data.Word

import Net.PacketParsing


-- 802.2 LLC Header                                  : SNAP extension
-- --------------------------------------------------------------------------
-- DSAP             : SSAP           : Control       : OUI      : Protocol ID
-- 1 octet          : 1 octet        : 1 or 2 octets : 3 octets : 2 octets

data LLCSNAP = LLCSNAP
    { dsap    :: Word8
    , ssap    :: Word8
    , control :: Word16 -- 1 or 2 bytes
    , oui     :: Word32 -- 3 bytes
    , protoid :: Word16
    }
 deriving (Eq,Ord,Read,Show)

instance Parse LLCSNAP where
    parse = do
        -- LLC
        dsap <- word8
        ssap <- word8
        control <- fromIntegral <$> word8
        -- Based on "Understanding Logical Link Control" at
        --   https://www.cisco.com/c/en/us/support/docs/ibm-technologies/logical-link-control-llc/12247-45.html
        --
        -- Unless the low two bits are set, there's
        -- another byte of the control field.
        nr <- if 0x0003 == control .&. 0x0003
                then return 0
                else fromIntegral <$> word8
        -- SNAP
        ouilo <- fromIntegral <$> word16
        ouihi <- fromIntegral <$> word8
        protoid <- word16
        return LLCSNAP
            { dsap    = dsap
            , ssap    = ssap
            , control = control + 256 * nr
            , oui     = ouilo + 65536 * ouihi
            , protoid = protoid
            }