network-packet-linux-0.1.1.0: Types for working with Linux packet sockets
Safe HaskellNone
LanguageHaskell2010

Network.Socket.Linux

Description

Typical use cases for this module are:

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

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.

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.

type IfIndex = Int Source #

The index of a network interface

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

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.

Bundled Patterns

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

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.

Bundled Patterns

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

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.