[erlang-questions] PATCH for genericised TCP-socket packet parsing

Tony Garnock-Jones tonyg@REDACTED
Sat Feb 9 18:20:13 CET 2008

Hi all,

Tony Garnock-Jones wrote:
> How difficult might it be to support, in the C inet runtime, a new kind
> of {packet, N} option for sockets, [...]

The answer is: given a bit of digging, not too difficult. See attached 
patch, which is against R12B-0, and which implements a new TCP socket option

   {gen_packet, SizeLength, SizeOffset, SizeDelta}

which only applies to inbound packets, just like the asn1, cdr, sunrm, 
tpkt types, and decodes packets by

  - requiring SizeLength + SizeOffset bytes to be available
  - reading the big-endian SizeLength-byte-long number at
  - adding SizeDelta to that value, and
  - considering the resulting value to be the expected length
    in bytes of the next packet to be returned, including its

For the examples I've collected so far,

    This protocol ...               ... works with this option
    IPv4 (just an example!)         {gen_packet, 2, 2, 0}
    AMQP 0-8                        {gen_packet, 4, 3, 8}
    AMQP 0-10 (recent draft)        {gen_packet, 2, 2, 0}
    PostgreSQL (from Christian)     {gen_packet, 4, 1, 1}

The existing TPKT/RFC1006 parser is the same as {gen_packet,2,2,0}, plus 
a check (on the receiver) of the value of the version byte.

Problems with the patch:

  - I'm new to erlang internals. Have I missed anything?

  - The {packet_size, Integer} option works fine, but can be
    a bit tricky to reason about because of the effect the
    gen_packet parameters have on it.

Note that this patch does nothing to outbound packets, requiring the 
user to ensure they're correctly formatted. This is just the same as the 
existing asn1, cdr, sunrm, tpkt etc. parsers.


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: genpacket.patch
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080209/a970a5f7/attachment.ksh>

More information about the erlang-questions mailing list