Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module provides the Mac data type and functions for working with it.
Synopsis
- mac :: Word64 -> Mac
- fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Mac
- toOctets :: Mac -> (Word8, Word8, Word8, Word8, Word8, Word8)
- encode :: Mac -> Text
- encodeWith :: MacCodec -> Mac -> Text
- decode :: Text -> Maybe Mac
- decodeWith :: MacCodec -> Text -> Maybe Mac
- builder :: Mac -> Builder
- parser :: Parser Mac
- parserWith :: MacCodec -> Parser Mac
- encodeUtf8 :: Mac -> ByteString
- encodeWithUtf8 :: MacCodec -> Mac -> ByteString
- decodeUtf8 :: ByteString -> Maybe Mac
- decodeWithUtf8 :: MacCodec -> ByteString -> Maybe Mac
- builderUtf8 :: Mac -> Builder
- parserUtf8 :: Parser Mac
- parserWithUtf8 :: MacCodec -> Parser Mac
- decodeBytes :: ByteString -> Maybe Mac
- boundedBuilderUtf8 :: Mac -> Builder 17
- decodeUtf8Bytes :: Bytes -> Maybe Mac
- parserUtf8Bytes :: e -> Parser e s Mac
- print :: Mac -> IO ()
- newtype Mac = Mac Word64
- data MacCodec = MacCodec {}
- data MacGrouping
Convert
fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Mac Source #
Create a Mac
address from six octets.
toOctets :: Mac -> (Word8, Word8, Word8, Word8, Word8, Word8) Source #
Convert a Mac
address to the six octets that make it up.
This function and fromOctets
are inverses:
m == (let (a,b,c,d,e,f) = toOctets m in fromOctets a b c d e f)
Textual Conversion
Text
UTF-8 ByteString
encodeUtf8 :: Mac -> ByteString Source #
encodeWithUtf8 :: MacCodec -> Mac -> ByteString Source #
Encode a Mac
address as a ByteString
using the given MacCodec
.
>>>
m = Mac 0xA47F247AB423
>>>
BC.putStrLn $ encodeWithUtf8 defCodec m
a4:7f:24:7a:b4:23
>>>
BC.putStrLn $ encodeWithUtf8 (MacCodec (MacGroupingTriples '-') True) m
A47-F24-7AB-423
decodeUtf8 :: ByteString -> Maybe Mac Source #
Lenient decoding of MAC address that accepts lowercase, uppercase, and any kind of separator.
>>>
decodeUtf8 "A2:DE:AD:BE:EF:67"
Just (mac 0xa2deadbeef67)>>>
decodeUtf8 "13-a2-fe-a4-17-96"
Just (mac 0x13a2fea41796)>>>
decodeUtf8 "0A42.47BA.67C2"
Just (mac 0x0a4247ba67c2)
decodeWithUtf8 :: MacCodec -> ByteString -> Maybe Mac Source #
Decode a ByteString
as a Mac
address using the given MacCodec
.
>>>
decodeWithUtf8 defCodec (BC.pack "64:25:5a:0f:2c:47")
Just (mac 0x64255a0f2c47)
>>>
decodeWithUtf8 (MacCodec MacGroupingNoSeparator False) (BC.pack "64255a0f2c47")
Just (mac 0x64255a0f2c47)
builderUtf8 :: Mac -> Builder Source #
Make a bytestring builder from a Mac
address
using a colon as the separator.
parserUtf8 :: Parser Mac Source #
Lenient parser for a Mac
address using any character
as the separator and accepting any digit grouping
(i.e. FA:43:B2:C0:0F:99
or A065.647B.87FA
).
parserWithUtf8 :: MacCodec -> Parser Mac Source #
Parser for a Mac
address using the provided settings.
ByteString
decodeBytes :: ByteString -> Maybe Mac Source #
Deprecated: Prefer decodeOctets
This function is deprecated. It will be renamed in a future release since the name is misleading.
UTF-8 Bytes
boundedBuilderUtf8 :: Mac -> Builder 17 Source #
Encode a Mac
address as colon-separated hexadecimal octets,
preferring lowercase for alphabetical characters.
>>>
BBB.run Nat.constant $ boundedBuilderUtf8 $ mac 0xDEADBEEF1609
[0x64, 0x65, 0x3a, 0x61, 0x64, 0x3a, 0x62, 0x65, 0x3a, 0x65, 0x66, 0x3a, 0x31, 0x36, 0x3a, 0x30, 0x39]
decodeUtf8Bytes :: Bytes -> Maybe Mac Source #
Lenient decoding of MAC address. This
is case insensitive and allows either :
or -
as the separator.
It also allows leading zeroes to be missing.
>>>
decodeUtf8Bytes (Bytes.fromAsciiString "A2:DE:AD:BE:EF:67")
Just (mac 0xa2deadbeef67)>>>
decodeUtf8Bytes (Bytes.fromAsciiString "13-a2-FE-A4-17-96")
Just (mac 0x13a2fea41796)
parserUtf8Bytes :: e -> Parser e s Mac Source #
Leniently parse UTF-8-encoded Bytes
as a Mac
address. This
is case insensitive and allows either :
or -
as the separator.
It also allows leading zeroes to be missing.
>>>
Parser.parseBytes (parserUtf8Bytes ()) (Bytes.fromAsciiString "de:ad:BE:EF:1:23")
Success (Slice {offset = 16, length = 0, value = mac 0xdeadbeef0123})
Printing
Types
A 48-bit MAC address. Do not use the data constructor for this type. It is not considered part of the stable API, and it allows you to construct invalid MAC addresses.
Instances
Bounded Mac Source # | |
Enum Mac Source # | |
Eq Mac Source # | |
Data Mac Source # | |
Defined in Net.Mac gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Mac -> c Mac # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Mac # dataTypeOf :: Mac -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Mac) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Mac) # gmapT :: (forall b. Data b => b -> b) -> Mac -> Mac # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Mac -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Mac -> r # gmapQ :: (forall d. Data d => d -> u) -> Mac -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Mac -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Mac -> m Mac # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Mac -> m Mac # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Mac -> m Mac # | |
Ord Mac Source # | |
Read Mac Source # | |
Show Mac Source # | |
Ix Mac Source # | |
Generic Mac Source # | |
Hashable Mac Source # | |
ToJSON Mac Source # | |
ToJSONKey Mac Source # | |
Defined in Net.Mac | |
FromJSON Mac Source # | |
FromJSONKey Mac Source # | |
Defined in Net.Mac | |
NFData Mac Source # | |
Prim Mac Source # | This only preserves the lower 6 bytes of the 8-byte word that backs a mac address.
It runs slower than it would if it used a full 8-byte word, but it consumes less
space. When storing millions of mac addresses, this is a good trade to make. When
storing a small number of mac address, it might be preferable to make a primitive
array of |
Defined in Net.Mac alignment# :: Mac -> Int# # indexByteArray# :: ByteArray# -> Int# -> Mac # readByteArray# :: MutableByteArray# s -> Int# -> State# s -> (#State# s, Mac#) # writeByteArray# :: MutableByteArray# s -> Int# -> Mac -> State# s -> State# s # setByteArray# :: MutableByteArray# s -> Int# -> Int# -> Mac -> State# s -> State# s # indexOffAddr# :: Addr# -> Int# -> Mac # readOffAddr# :: Addr# -> Int# -> State# s -> (#State# s, Mac#) # writeOffAddr# :: Addr# -> Int# -> Mac -> State# s -> State# s # setOffAddr# :: Addr# -> Int# -> Int# -> Mac -> State# s -> State# s # | |
type Rep Mac Source # | |
Instances
Eq MacCodec Source # | |
Data MacCodec Source # | |
Defined in Net.Mac gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> MacCodec -> c MacCodec # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c MacCodec # toConstr :: MacCodec -> Constr # dataTypeOf :: MacCodec -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c MacCodec) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c MacCodec) # gmapT :: (forall b. Data b => b -> b) -> MacCodec -> MacCodec # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> MacCodec -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> MacCodec -> r # gmapQ :: (forall d. Data d => d -> u) -> MacCodec -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> MacCodec -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> MacCodec -> m MacCodec # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> MacCodec -> m MacCodec # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> MacCodec -> m MacCodec # | |
Ord MacCodec Source # | |
Read MacCodec Source # | |
Show MacCodec Source # | |
Generic MacCodec Source # | |
type Rep MacCodec Source # | |
Defined in Net.Mac type Rep MacCodec = D1 (MetaData "MacCodec" "Net.Mac" "ip-1.7.1-77j8dcfdnD5D6aoS2e9Z3R" False) (C1 (MetaCons "MacCodec" PrefixI True) (S1 (MetaSel (Just "macCodecGrouping") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 MacGrouping) :*: S1 (MetaSel (Just "macCodecUpperCase") NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 Bool))) |
data MacGrouping Source #
The format expected by the mac address parser. The Word8
taken
by some of these constructors is the ascii value of the character
to be used as the separator. This is typically a colon, a hyphen, or
a space character. All decoding functions are case insensitive.
MacGroupingPairs !Char | Two-character groups, |
MacGroupingTriples !Char | Three-character groups, |
MacGroupingQuadruples !Char | Four-character groups, |
MacGroupingNoSeparator | No separator, |