[erlang-questions] Matching IP address in socket module

Thomas Pircher thp+erlang@REDACTED
Sun Oct 27 20:19:32 CET 2019


Thomas Pircher wrote:
> I'm quite impressed with the socket module, it seems to be pretty
> complete, at least for my application.

Hi, I'm having another problem with the socket module, I'm hoping
someone could help me understanding what is going on here.

I'm attempting to send a packet from an arbitrary IP address. My
understanding is that this can be done using the pktinfo structure on a
Linux system. There is no direct support for this in the cmsghdr_send()
type, so I thought I can use the #{level := ip, type := integer(), …}
construct instead. (I know this is non-portable.)

Using the following code snippet I can get packets out of the system,
but the pktinfo data seems to be ignored; the output interface is
selected by the routing table (and not by the IfIndex variable) and the
source IP address is not the one I set in the control header.

-define(IP_PKTINFO, 8).
send(DstIp, IfIndex, Data) ->
    {ok, Socket} = socket:open(inet, dgram),
    DstAddr = #{family => inet, addr => DstIp, port => 42001},
    Ctrl = #{level => udp, type => ?IP_PKTINFO, data => <<IfIndex:32,  10, 42, 1, 1,  10, 42, 1, 1>>},
    MsgHdr = #{addr => DstAddr, ctrl => [Ctrl], flags => [], iov => [Data]},
    Res = socket:sendmsg(Socket, MsgHdr),
    io:format("Sent message ~p~n", [Res]).

Is there something I'm doing wrong?

Thanks,
Thomas



More information about the erlang-questions mailing list