[erlang-questions] UDP Headers

Michael Santos michael.santos@REDACTED
Tue Jul 8 23:47:10 CEST 2014


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



More information about the erlang-questions mailing list