Last active
March 8, 2018 18:50
-
-
Save vdorr/0dde5693f28155da03c291a53fd06c26 to your computer and use it in GitHub Desktop.
Bind socket to device in Haskell on Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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