Skip to content

Instantly share code, notes, and snippets.

@voluntas
Created January 15, 2011 17:05
Show Gist options
  • Save voluntas/781051 to your computer and use it in GitHub Desktop.
Save voluntas/781051 to your computer and use it in GitHub Desktop.
-module(ipv6mcast).
-compile([export_all]).
-include_lib("eunit/include/eunit.hrl").
%% struct ipv6_mreq {
%% struct in6_addr ipv6mr_multiaddr; /* IPv6 multicast addr */
%% unsigned int ipv6mr_interface; /* interface index */
%% };
-define(IPPROTO_IPV6, 41).
%% BSD
-define(IPV6_JOIN_GROUP, 12).
-define(IPV6_LEAVE_GROUP, 13).
%% Linux
-define(IPV6_ADD_MEMBERSHIP, 20).
-define(IPV6_DROP_MEMBERSHIP, 21).
ipv6_join_group(BinaryAddress) ->
case os:type() of
{unix,darwin} ->
{raw, ?IPPROTO_IPV6, ?IPV6_JOIN_GROUP, <<BinaryAddress/binary, 0:32/native>>};
{unix,linux} ->
{raw, ?IPPROTO_IPV6, ?IPV6_ADD_MEMBERSHIP, <<BinaryAddress/binary, 0:32/native>>}
end.
ipv6_to_binary(IPv6AddressStr) when is_list(IPv6AddressStr) ->
{ok, {N1,N2,N3,N4,N5,N6,N7,N8}} = inet_parse:ipv6strict_address(IPv6AddressStr),
<<N1:16,N2:16,N3:16,N4:16,N5:16,N6:16,N7:16,N8:16>>.
main() ->
BinaryAddress = ipv6_to_binary("FF02::1"),
{ok, Socket} = gen_udp:open(9876, [inet6, {active, false},
{raw,
?IPPROTO_IPV6,
?IPV6_JOIN_GROUP,
<<BinaryAddress/binary, 0:32/native>>}]),
{ok, {IP, Port, Binary}} = gen_udp:recv(Socket, 8192),
?debugVal(IP),
?debugVal(Port),
?debugVal(Binary),
Socket.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment