Safe Haskell | None |
---|---|
Language | Haskell2010 |
Typical use cases for this module are:
- binding a packet socket to an interface (combining
mkBindSockAddrLl
andbind
) - sending raw packets (combining
mkSendSockAddrLl
andsendTo
) - receiving data from a packet socket (using
recvFrom
)
Here's an example of binding to an interface and receiving packets:
main :: IO () main = do -- Open a raw packet socket, receiving all protocols sock <- socket AF_PACKET Raw (toProtocolNumber ETH_P_ALL) -- Get the index of the eth0 interface, or fall back to "any interface" (0) if "eth0" doesn't exist ifIndex <- fromMaybe 0 <$> ifNameToIndex "eth0" -- Bind the socket to the interface index found above bind sock (mkBindSockAddrLl AF_PACKET ETH_P_ALL ifIndex) -- Print the length of the next 10 packets received on the interface: replicateM_ 10 $ do msg <- recv sock 4096 putStrLn $ "Received " ++ (show . BS.length) msg ++ " bytes" -- Close the socket close sock
See packet (7) for more information about packet sockets.
Synopsis
- data SockAddrLl
- sllPktType :: SockAddrLl -> PacketType
- physicalAddress :: SockAddrLl -> PhysicalAddress
- mkBindSockAddrLl :: Family -> ProtocolId -> IfIndex -> SockAddrLl
- mkSendSockAddrLl :: Family -> ProtocolId -> IfIndex -> PhysicalAddress -> SockAddrLl
- type IfIndex = Int
- data PhysicalAddress
- type PhysicalAddressBytes = (Word8, Word8, Word8, Word8, Word8, Word8, Word8, Word8)
- addressLength :: PhysicalAddress -> SllHaLen
- address :: PhysicalAddress -> PhysicalAddressBytes
- mkPhysicalAddress :: Int -> PhysicalAddressBytes -> Maybe PhysicalAddress
- macAddress :: (Word8, Word8, Word8, Word8, Word8, Word8) -> PhysicalAddress
- data ProtocolId where
- pattern GeneralProtocolId :: CUShort -> ProtocolId
- pattern UnsupportedProtocolId :: ProtocolId
- pattern ETH_P_LOOP :: ProtocolId
- pattern ETH_P_PUP :: ProtocolId
- pattern ETH_P_PUPAT :: ProtocolId
- pattern ETH_P_IP :: ProtocolId
- pattern ETH_P_X25 :: ProtocolId
- pattern ETH_P_ARP :: ProtocolId
- pattern ETH_P_BPQ :: ProtocolId
- pattern ETH_P_IEEEPUP :: ProtocolId
- pattern ETH_P_IEEEPUPAT :: ProtocolId
- pattern ETH_P_DEC :: ProtocolId
- pattern ETH_P_DNA_DL :: ProtocolId
- pattern ETH_P_DNA_RC :: ProtocolId
- pattern ETH_P_DNA_RT :: ProtocolId
- pattern ETH_P_LAT :: ProtocolId
- pattern ETH_P_DIAG :: ProtocolId
- pattern ETH_P_CUST :: ProtocolId
- pattern ETH_P_SCA :: ProtocolId
- pattern ETH_P_TEB :: ProtocolId
- pattern ETH_P_RARP :: ProtocolId
- pattern ETH_P_ATALK :: ProtocolId
- pattern ETH_P_AARP :: ProtocolId
- pattern ETH_P_8021Q :: ProtocolId
- pattern ETH_P_IPX :: ProtocolId
- pattern ETH_P_IPV6 :: ProtocolId
- pattern ETH_P_PAUSE :: ProtocolId
- pattern ETH_P_SLOW :: ProtocolId
- pattern ETH_P_WCCP :: ProtocolId
- pattern ETH_P_PPP_DISC :: ProtocolId
- pattern ETH_P_PPP_SES :: ProtocolId
- pattern ETH_P_MPLS_UC :: ProtocolId
- pattern ETH_P_MPLS_MC :: ProtocolId
- pattern ETH_P_ATMMPOA :: ProtocolId
- pattern ETH_P_ATMFATE :: ProtocolId
- pattern ETH_P_PAE :: ProtocolId
- pattern ETH_P_AOE :: ProtocolId
- pattern ETH_P_TIPC :: ProtocolId
- pattern ETH_P_1588 :: ProtocolId
- pattern ETH_P_FCOE :: ProtocolId
- pattern ETH_P_FIP :: ProtocolId
- pattern ETH_P_EDSA :: ProtocolId
- pattern ETH_P_802_3 :: ProtocolId
- pattern ETH_P_AX25 :: ProtocolId
- pattern ETH_P_ALL :: ProtocolId
- pattern ETH_P_802_2 :: ProtocolId
- pattern ETH_P_SNAP :: ProtocolId
- pattern ETH_P_DDCMP :: ProtocolId
- pattern ETH_P_WAN_PPP :: ProtocolId
- pattern ETH_P_PPP_MP :: ProtocolId
- pattern ETH_P_LOCALTALK :: ProtocolId
- pattern ETH_P_CAN :: ProtocolId
- pattern ETH_P_PPPTALK :: ProtocolId
- pattern ETH_P_TR_802_2 :: ProtocolId
- pattern ETH_P_MOBITEX :: ProtocolId
- pattern ETH_P_CONTROL :: ProtocolId
- pattern ETH_P_IRDA :: ProtocolId
- pattern ETH_P_ECONET :: ProtocolId
- pattern ETH_P_HDLC :: ProtocolId
- pattern ETH_P_ARCNET :: ProtocolId
- pattern ETH_P_DSA :: ProtocolId
- pattern ETH_P_TRAILER :: ProtocolId
- pattern ETH_P_PHONET :: ProtocolId
- pattern ETH_P_IEEE802154 :: ProtocolId
- isSupportedProtocolId :: ProtocolId -> Bool
- toProtocolNumber :: ProtocolId -> ProtocolNumber
- data PacketType where
- pattern GeneralPacketType :: CUChar -> PacketType
- pattern UnsupportedPacketType :: PacketType
- pattern PACKET_HOST :: PacketType
- pattern PACKET_BROADCAST :: PacketType
- pattern PACKET_MULTICAST :: PacketType
- pattern PACKET_OTHERHOST :: PacketType
- pattern PACKET_OUTGOING :: PacketType
- isSupportedPacketType :: PacketType -> Bool
sockaddr_ll type
data SockAddrLl Source #
A type representing the sockaddr_ll
struct defined in
<linux/if_ether.h>
.
This can be used by functions in Network.Socket.Address in order to
interact with packet sockets - see
packet (7) for
details. Note that passing this type to connect
or
accept
is an error.
Instances
SocketAddress SockAddrLl Source # | |
Defined in Network.Socket.Linux.Types sizeOfSocketAddress :: SockAddrLl -> Int # peekSocketAddress :: Ptr SockAddrLl -> IO SockAddrLl # pokeSocketAddress :: Ptr a -> SockAddrLl -> IO () # |
sllPktType :: SockAddrLl -> PacketType Source #
Get the type of a received packet
physicalAddress :: SockAddrLl -> PhysicalAddress Source #
Get the PhysicalAddress
corresponding to the the sll_halen
and sll_addr
fields of a sockaddr_ll
mkBindSockAddrLl :: Family -> ProtocolId -> IfIndex -> SockAddrLl Source #
Create a SockAddrLl for binding a packet socket to an interface.
mkSendSockAddrLl :: Family -> ProtocolId -> IfIndex -> PhysicalAddress -> SockAddrLl Source #
Create a SockAddrLl for sending data to a specific address.
data PhysicalAddress Source #
A physical address consisting of up to eight bytes.
This type encapsulates the sll_halen
and sll_addr
fields of
sockaddr_ll
(as defined in <linux/if_packet.h>
).
Instances
Eq PhysicalAddress Source # | |
Defined in Network.Socket.Linux.Types (==) :: PhysicalAddress -> PhysicalAddress -> Bool # (/=) :: PhysicalAddress -> PhysicalAddress -> Bool # | |
Show PhysicalAddress Source # | |
Defined in Network.Socket.Linux.Types showsPrec :: Int -> PhysicalAddress -> ShowS # show :: PhysicalAddress -> String # showList :: [PhysicalAddress] -> ShowS # |
type PhysicalAddressBytes = (Word8, Word8, Word8, Word8, Word8, Word8, Word8, Word8) Source #
Up to eight bytes, for use with a PhysicalAddress
addressLength :: PhysicalAddress -> SllHaLen Source #
Get the length of the given PhysicalAddress
.
address :: PhysicalAddress -> PhysicalAddressBytes Source #
Get the address bytes for the given PhysicalAddress
mkPhysicalAddress :: Int -> PhysicalAddressBytes -> Maybe PhysicalAddress Source #
Create a new PhysicalAddress
with the given length and bytes.
Note that addressLength
must be between 0 and 8
macAddress :: (Word8, Word8, Word8, Word8, Word8, Word8) -> PhysicalAddress Source #
Create a new PhysicalAddress
to represent the the given MAC address.
ProtocolId
data ProtocolId where Source #
Ethernet Protocol IDs.
These are IEEE 802.3 protocol numbers (as defined in <linux/if_ether.h>
),
for use in SockAddrLl
addresses when working with packet sockets.
Some of the defined patterns may be unsupported on some systems:
see isSupportedProtocolId
.
pattern GeneralProtocolId :: CUShort -> ProtocolId | |
pattern UnsupportedProtocolId :: ProtocolId | Unsupported protocol id, equal to any other protocol ids that are not supported on the system. |
pattern ETH_P_LOOP :: ProtocolId | Ethernet Loopback packet |
pattern ETH_P_PUP :: ProtocolId | Xerox PUP packet |
pattern ETH_P_PUPAT :: ProtocolId | Xerox PUP Addr Trans packet |
pattern ETH_P_IP :: ProtocolId | Internet Protocol packet |
pattern ETH_P_X25 :: ProtocolId | CCITT X.25 |
pattern ETH_P_ARP :: ProtocolId | Address Resolution packet |
pattern ETH_P_BPQ :: ProtocolId | G8BPQ AX.25 Ethernet Packet [ NOT AN OFFICIALLY REGISTERED ID ] |
pattern ETH_P_IEEEPUP :: ProtocolId | Xerox IEEE802.3 PUP packet |
pattern ETH_P_IEEEPUPAT :: ProtocolId | Xerox IEEE802.3 PUP Addr Trans packet |
pattern ETH_P_DEC :: ProtocolId | DEC Assigned proto |
pattern ETH_P_DNA_DL :: ProtocolId | DEC DNA Dump/Load |
pattern ETH_P_DNA_RC :: ProtocolId | DEC DNA Remote Console |
pattern ETH_P_DNA_RT :: ProtocolId | DEC DNA Routing |
pattern ETH_P_LAT :: ProtocolId | DEC LAT |
pattern ETH_P_DIAG :: ProtocolId | DEC Diagnostics |
pattern ETH_P_CUST :: ProtocolId | DEC Customer use |
pattern ETH_P_SCA :: ProtocolId | DEC Systems Comms Arch |
pattern ETH_P_TEB :: ProtocolId | Trans Ether Bridging |
pattern ETH_P_RARP :: ProtocolId | Reverse Addr Res packet |
pattern ETH_P_ATALK :: ProtocolId | Appletalk DDP |
pattern ETH_P_AARP :: ProtocolId | Appletalk AARP |
pattern ETH_P_8021Q :: ProtocolId | 802.1Q VLAN Extended Header |
pattern ETH_P_IPX :: ProtocolId | IPX over DIX |
pattern ETH_P_IPV6 :: ProtocolId | IPv6 over bluebook |
pattern ETH_P_PAUSE :: ProtocolId | IEEE Pause frames. See 802.3 31B |
pattern ETH_P_SLOW :: ProtocolId | Slow Protocol. See 802.3ad 43B |
pattern ETH_P_WCCP :: ProtocolId | Web-cache coordination protocol |
pattern ETH_P_PPP_DISC :: ProtocolId | PPPoE discovery messages |
pattern ETH_P_PPP_SES :: ProtocolId | PPPoE session messages |
pattern ETH_P_MPLS_UC :: ProtocolId | MPLS Unicast traffic |
pattern ETH_P_MPLS_MC :: ProtocolId | MPLS Multicast traffic |
pattern ETH_P_ATMMPOA :: ProtocolId | MultiProtocol Over ATM |
pattern ETH_P_ATMFATE :: ProtocolId | Frame-based ATM Transport |
pattern ETH_P_PAE :: ProtocolId | Port Access Entity (IEEE 802.1X) |
pattern ETH_P_AOE :: ProtocolId | ATA over Ethernet |
pattern ETH_P_TIPC :: ProtocolId | TIPC |
pattern ETH_P_1588 :: ProtocolId | IEEE 1588 Timesync |
pattern ETH_P_FCOE :: ProtocolId | Fibre Channel over Ethernet |
pattern ETH_P_FIP :: ProtocolId | FCoE Initialization Protocol |
pattern ETH_P_EDSA :: ProtocolId | Ethertype DSA [ NOT AN OFFICIALLY REGISTERED ID ] |
pattern ETH_P_802_3 :: ProtocolId | Dummy type for 802.3 frames |
pattern ETH_P_AX25 :: ProtocolId | Dummy protocol id for AX.25 |
pattern ETH_P_ALL :: ProtocolId | Every packet (be careful!!!) |
pattern ETH_P_802_2 :: ProtocolId | 802.2 frames |
pattern ETH_P_SNAP :: ProtocolId | Internal only |
pattern ETH_P_DDCMP :: ProtocolId | DEC DDCMP: Internal only |
pattern ETH_P_WAN_PPP :: ProtocolId | Dummy type for WAN PPP frames |
pattern ETH_P_PPP_MP :: ProtocolId | Dummy type for PPP MP frames |
pattern ETH_P_LOCALTALK :: ProtocolId | Localtalk pseudo type |
pattern ETH_P_CAN :: ProtocolId | Controller Area Network |
pattern ETH_P_PPPTALK :: ProtocolId | Dummy type for Atalk over PPP |
pattern ETH_P_TR_802_2 :: ProtocolId | 802.2 frames |
pattern ETH_P_MOBITEX :: ProtocolId | Mobitex (kaz@cafe.net) |
pattern ETH_P_CONTROL :: ProtocolId | Card specific control frames |
pattern ETH_P_IRDA :: ProtocolId | Linux-IrDA |
pattern ETH_P_ECONET :: ProtocolId | Acorn Econet |
pattern ETH_P_HDLC :: ProtocolId | HDLC frames |
pattern ETH_P_ARCNET :: ProtocolId | 1A for ArcNet :-) |
pattern ETH_P_DSA :: ProtocolId | Distributed Switch Arch. |
pattern ETH_P_TRAILER :: ProtocolId | Trailer switch tagging |
pattern ETH_P_PHONET :: ProtocolId | Nokia Phonet frames |
pattern ETH_P_IEEE802154 :: ProtocolId | IEEE802.15.4 frame |
Instances
Eq ProtocolId Source # | |
Defined in Network.Socket.Linux.Types (==) :: ProtocolId -> ProtocolId -> Bool # (/=) :: ProtocolId -> ProtocolId -> Bool # | |
Ord ProtocolId Source # | |
Defined in Network.Socket.Linux.Types compare :: ProtocolId -> ProtocolId -> Ordering # (<) :: ProtocolId -> ProtocolId -> Bool # (<=) :: ProtocolId -> ProtocolId -> Bool # (>) :: ProtocolId -> ProtocolId -> Bool # (>=) :: ProtocolId -> ProtocolId -> Bool # max :: ProtocolId -> ProtocolId -> ProtocolId # min :: ProtocolId -> ProtocolId -> ProtocolId # | |
Read ProtocolId Source # | |
Defined in Network.Socket.Linux.Types readsPrec :: Int -> ReadS ProtocolId # readList :: ReadS [ProtocolId] # readPrec :: ReadPrec ProtocolId # readListPrec :: ReadPrec [ProtocolId] # | |
Show ProtocolId Source # | |
Defined in Network.Socket.Linux.Types showsPrec :: Int -> ProtocolId -> ShowS # show :: ProtocolId -> String # showList :: [ProtocolId] -> ShowS # |
isSupportedProtocolId :: ProtocolId -> Bool Source #
Does one of the ETH_
constants correspond to a known Ethernet protocol id
on this system?
Just like for isSupportedFamily
, GeneralProtocolId
values
not equal to any of the named ETH_xxxxx
patterns or UnsupportedProtocolId
will return True
even when not known on this system.
toProtocolNumber :: ProtocolId -> ProtocolNumber Source #
Convert ProtocolId
to network byte order, for use with
socket
PacketType
data PacketType where Source #
Packet Types.
Linux packet types as defined in <linux/if_packet.h>
, for use in
SockAddrLl addresses when working with packet sockets.
Some of the defined patterns may be unsupported on some systems:
see isSupportedPacketType
.
pattern GeneralPacketType :: CUChar -> PacketType | |
pattern UnsupportedPacketType :: PacketType | Unsupported packet id, equal to any other packet ids that are not supported on the system. |
pattern PACKET_HOST :: PacketType | To us |
pattern PACKET_BROADCAST :: PacketType | To all |
pattern PACKET_MULTICAST :: PacketType | To group |
pattern PACKET_OTHERHOST :: PacketType | To someone else |
pattern PACKET_OUTGOING :: PacketType | Outgoing of any type |
Instances
Eq PacketType Source # | |
Defined in Network.Socket.Linux.Types (==) :: PacketType -> PacketType -> Bool # (/=) :: PacketType -> PacketType -> Bool # | |
Ord PacketType Source # | |
Defined in Network.Socket.Linux.Types compare :: PacketType -> PacketType -> Ordering # (<) :: PacketType -> PacketType -> Bool # (<=) :: PacketType -> PacketType -> Bool # (>) :: PacketType -> PacketType -> Bool # (>=) :: PacketType -> PacketType -> Bool # max :: PacketType -> PacketType -> PacketType # min :: PacketType -> PacketType -> PacketType # | |
Read PacketType Source # | |
Defined in Network.Socket.Linux.Types readsPrec :: Int -> ReadS PacketType # readList :: ReadS [PacketType] # readPrec :: ReadPrec PacketType # readListPrec :: ReadPrec [PacketType] # | |
Show PacketType Source # | |
Defined in Network.Socket.Linux.Types showsPrec :: Int -> PacketType -> ShowS # show :: PacketType -> String # showList :: [PacketType] -> ShowS # |
isSupportedPacketType :: PacketType -> Bool Source #
Does one of the PACKET_
constants correspond to a known packet type
on this system?
Like for isSupportedFamily
, GeneralPacketType
values
not equal to any of the named PACKET_
patterns or UnsupportedPacketType
will return True
even when not known on this system.