Skip to content

Instantly share code, notes, and snippets.

@vdorr
Last active March 8, 2018 18:50
Show Gist options
  • Save vdorr/0dde5693f28155da03c291a53fd06c26 to your computer and use it in GitHub Desktop.
Save vdorr/0dde5693f28155da03c291a53fd06c26 to your computer and use it in GitHub Desktop.
Bind socket to device in Haskell on Linux
{-# LANGUAGE ForeignFunctionInterface #-}
module RawSocket where
import Foreign.C
import Network.Socket
foreign import ccall "setsockopt"
setsockopt_cstr :: CInt -> CInt -> CInt -> CString -> CInt -> IO CInt
sO_BINDTODEVICE :: CInt
sO_BINDTODEVICE = 25
bindToDevice :: Socket -> String -> IO (Int, Int)
bindToDevice (MkSocket s _family _stype _protocol _status) addr
= withCString addr $ \optval -> do
retVal <- setsockopt_cstr s (fromIntegral sOL_SOCKET) sO_BINDTODEVICE optval (fromIntegral $ length addr)
Errno errno <- getErrno
return (fromIntegral retVal, fromIntegral errno)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment