[erlang-questions] UDP Headers

Michael Santos michael.santos@REDACTED
Wed Jul 9 00:20:00 CEST 2014


On Tue, Jul 08, 2014 at 10:53:18PM +0100, Lee Sylvester wrote:
> Hi Michael,
> 
> Thanks for responding.  I was actually just looking through your repositories to see if you had written something to work with this.
> 
> What I’d like to do is to be able to set the DF or TTL flags.  What would you suggest for such things?

Raw sockets would be overkill for this. Have a look at the raw option
to inet:setopts/2. There's an example in the docs for inet of setting
TCP_LINGER2 on a TCP socket:

    inet:setopts(Sock,[{raw,6,8,<<30:32/native>>}])

The values will depend on the OS. So for setting the TTL, on linux/x86_64,
maybe something like:

    setsockopt(send_socket, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));

    % IPPROTO_IP = 0
    % IP_TTL = 2
    % TTL = 64
    inet:setopts(Sock,[{raw,0,2,<<64:32/native>>}])

DF is interesting. There's a good discussion about the DF bit here:

    http://stackoverflow.com/questions/973439/how-to-set-the-dont-fragment-df-flag-on-a-socket

So on a linux system, maybe this would work:

    int val = IP_PMTUDISC_DO;
    setsockopt(sd, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val));

    % IPPROTO = 0
    % IP_MTU_DISCOVER = 10
    % IP_PMTUDISC_DO = 2
    inet:setopts(Sock,[{raw,0,10,<<2:32/native>>}])

I haven't tested if these work so let us know if you run into problems
and I'll try to come up with some working code!

> > On Tue, Jul 08, 2014 at 07:04:12PM +0100, Lee Sylvester wrote:
> >> Hey guys,
> >> 
> >> So, I’m using gen_udp for a server I’m building, but I really need to be able to update the headers of incoming packets.  Does anyone know how I can do this with gen_udp without having to resort to using raw sockets?
> > 
> > It depends which header needs to be changed:
> > 
> > * for UDP (source/destination port, length, checksum), the simplest
> >  method is to receive and resend the packet
> > 
> > * for IPv4/IPv6, some of the headers may be able to be influenced by setting
> >  socket options. For example, by using inet:setopts/2 or by using
> >  setsockopt(2) from an NIF.
> > 
> > * otherwise, there's the raw socket interface.
> > 
> >  For IPv4, the socket can be opened in raw mode and passed back into
> >  gen_udp.
> > 
> >  For IPv6 headers, if the headers can be changed, you generally have to
> >  use sendmsg(2)/recvmsg(2) which means using a port or an NIF.
> > 
> > * for ethernet, it will depend on the OS: PF_PACKET for linux, BPF for
> >  the BSDs
> > 
> > * another option is routing the packets through a tun/tap device and
> >  having some erlang code re-write the headers
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> 



More information about the erlang-questions mailing list