mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2024-12-17 17:20:21 +01:00
use multicast address for announce (#3241)
* use multicast address for announce * Add explicit multicast group membership * join multicast group on a correct side --------- Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
93800268e4
commit
e1bd6a93af
@@ -0,0 +1,43 @@
|
||||
module Simplex.Chat.Remote.Multicast (setMembership) where
|
||||
|
||||
import Foreign (Ptr, allocaBytes, castPtr, pokeByteOff)
|
||||
import Foreign.C.Types (CInt (..))
|
||||
import Network.Socket
|
||||
|
||||
#include <HsNet.h>
|
||||
|
||||
{- | Toggle multicast group membership.
|
||||
|
||||
NB: Group membership is per-host, not per-process. A socket is only used to access system interface for groups.
|
||||
-}
|
||||
setMembership :: Socket -> HostAddress -> Bool -> IO Bool
|
||||
setMembership sock group membership = allocaBytes #{size struct ip_mreq} $ \mReqPtr -> do
|
||||
#{poke struct ip_mreq, imr_multiaddr} mReqPtr group
|
||||
#{poke struct ip_mreq, imr_interface} mReqPtr (0 :: HostAddress) -- attempt to contact the group on ANY interface
|
||||
withFdSocket sock $ \fd ->
|
||||
(/= 0) <$> c_setsockopt fd c_IPPROTO_IP flag (castPtr mReqPtr) (#{size struct ip_mreq})
|
||||
where
|
||||
flag = if membership then c_IP_ADD_MEMBERSHIP else c_IP_DROP_MEMBERSHIP
|
||||
|
||||
#ifdef mingw32_HOST_OS
|
||||
|
||||
foreign import stdcall unsafe "setsockopt"
|
||||
c_setsockopt :: CInt -> CInt -> CInt -> Ptr CInt -> CInt -> IO CInt
|
||||
|
||||
c_IP_ADD_MEMBERSHIP, c_IP_DROP_MEMBERSHIP :: CInt
|
||||
c_IP_ADD_MEMBERSHIP = 12
|
||||
c_IP_DROP_MEMBERSHIP = 13
|
||||
|
||||
#else
|
||||
|
||||
foreign import ccall unsafe "setsockopt"
|
||||
c_setsockopt :: CInt -> CInt -> CInt -> Ptr CInt -> CInt -> IO CInt
|
||||
|
||||
c_IP_ADD_MEMBERSHIP, c_IP_DROP_MEMBERSHIP :: CInt
|
||||
c_IP_ADD_MEMBERSHIP = #const IP_ADD_MEMBERSHIP
|
||||
c_IP_DROP_MEMBERSHIP = #const IP_DROP_MEMBERSHIP
|
||||
|
||||
#endif
|
||||
|
||||
c_IPPROTO_IP :: CInt
|
||||
c_IPPROTO_IP = #const IPPROTO_IP
|
||||
Reference in New Issue
Block a user