[erlang-questions] UDP Headers
Michael Santos
michael.santos@REDACTED
Wed Jul 9 00:37:56 CEST 2014
On Tue, Jul 08, 2014 at 11:25:49PM +0100, Lee Sylvester wrote:
> Thank you for your help, Michael. I’ll give these a test in the morning and will let you know.
FYI, just confirmed it works:
1> {ok,S} = gen_udp:open(0).
{ok,#Port<0.800>}
2> gen_udp:send(S, {8,8,8,8}, 7777, <<"hello">>).
tcpdump shows a default TTL of 64 and the DF bit is set:
18:33:28.006857 IP (tos 0x0, ttl 64, id 22899, offset 0, flags [DF], proto UDP (17), length 33)
Setting the TTL to 1 and generating another packet:
3> inet:setopts(S,[{raw,0,2,<<1:32/native>>}]).
ok
4> gen_udp:send(S, {8,8,8,8}, 7777, <<"hello">>).
ok
Checking tcpdump, the TTL is now set to 1.
18:34:03.735254 IP (tos 0x0, ttl 1, id 22900, offset 0, flags [DF], proto UDP (17), length 33)
Since the DF bit is set, I'll unset it by passing in 0 (instead of 2):
5> inet:setopts(S,[{raw,0,10,<<0:32/native>>}]).
ok
And tcpdump shows no flags are now set on the packet:
18:35:01.542090 IP (tos 0x0, ttl 1, id 22901, offset 0, flags [none], proto UDP (17), length 33)
> On 8 Jul 2014, at 23:20, Michael Santos <michael.santos@REDACTED> wrote:
>
> > 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
> >>
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
>
More information about the erlang-questions
mailing list