<?xml version="1.0" encoding="US-ASCII"?>
<!-- vim: set ts=2 expandtab: -->
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
<!ENTITY RFC2119 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml">
<!ENTITY RFC4627 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.4627.xml">
<!ENTITY I-D.ietf-core-coap SYSTEM "http://xml.resource.org/public/rfc/bibxml3/reference.I-D.draft-ietf-core-coap-18.xml">
]>
<?xml-stylesheet type='text/xsl' href='rfc2629.xslt' ?>
<?rfc strict="yes" ?>
<?rfc toc="yes"?>
<?rfc tocdepth="4"?>
<?rfc symrefs="yes"?>
<?rfc sortrefs="yes" ?>
<?rfc compact="yes" ?>
<?rfc subcompact="no" ?>
<?rfc comments="yes" ?>
<?rfc inline="yes" ?>
<rfc category="std" docName="draft-fossati-core-multipart-ct-02" ipr="trust200902">
  <front>
    <title abbrev="Multipart Content-Format for CoAP">
      Multipart Content-Format Encoding for CoAP
    </title>
    <author
      fullname="Thomas Fossati"
      initials="T.F."
      surname="Fossati"
    >
      <organization>KoanLogic</organization>
        <address>
          <email>tho@koanlogic.com</email>
      </address>
    </author>

    <date year="2013" month="October" />

    <area>General</area>
    <workgroup>Internet Engineering Task Force</workgroup>

    <keyword>CoAP, Multipart Content-Format</keyword>

    <abstract>
      <t>This memo defines Multipart, an "anonymous" Content-Format that can be used to combine several different media types into a single CoAP message-body with minimal framing overhead.</t>
    </abstract>
  </front>

  <middle>
    <section title="Introduction">
      <t>This memo defines Multipart, an "anonymous" Content-Format that can be used to combine several different media types into a single CoAP message-body with minimal framing overhead.</t>
      <t>This simple and pretty efficient binary framing mechanism can be employed as an alternative to fully fledged marshalling mechanisms (e.g. JSON <xref target="RFC4627" />), to create application specific formats which build on already existing types by assigning to them individual semantics.</t>
      <t>Applications using the Multipart Content-Format are supposed to define the internal structure of the Multipart representation, as well as registering its outermost type -- typically one in range 10000-64999.</t>
      <t>Specific sub-types in a Multipart container are always found at the same fixed position corresponding to their implicit name.  Thus, the way to allow for optional parts, is to carry them as zero-length values for their respective types.</t>
      <section title="Requirements Language">
        <t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in <xref target="RFC2119" />.</t>
      </section>
    </section>

    <section title="Multipart Content-Format Encoding">
      <t>Multipart encoding uses multiple adjacent frames each of which represents a single media.  Every frame can be broken down into three logical pieces: the type of the framed media (T), its length in bytes (L), and the media payload itself (V) as depicted in the following figure.</t>

      <figure>
        <artwork align="center"><![CDATA[
,------------------ Multipart ------------------.
+------+------+------+     +------+------+------+
| T[1] | L[1] | V[1] | ... | T[n] | L[n] | V[n] |
+------+------+------+     +------+------+------+
`------ part 1 ------'     `------ part n ------'
        ]]></artwork>
      </figure>
    
      <t>
        The syntax and semantics associated to the TLV frames is as follows:
        <list style="hanging">
          <t hangText="T:"> is one of the numeric content format identifiers defined in the CoAP Content-Format Registry (Section 12.3 of <xref target="I-D.ietf-core-coap" />), and is encoded as a 16-bit unsigned integer.</t>
          <t hangText="L:"> is the length in bytes of the following V frame, encoded as defined in <xref target="length-encoding"/>.  It determines the offset of the next part, or the end of the multipart representation when applied to the last part.</t>
          <t hangText="V:"> is the media, encoded as implied by the preceding T field, yet opaquely to the Multipart encoder/decoder.</t>
        </list>
        The T and L fields are in network byte order.
      </t>
    </section>

    <section title="Length Encoding" anchor="length-encoding">
      <t>Three different encodings are defined for the L value depending on the actual size of the corresponding V: Small for V whose size in bytes is in range [0, 127], Medium for sizes in [128, 16383], and Large to cover the [16384, 2^63 - 1] range.</t>
      <t>An encoder MUST always use the most compact encoding, i.e. Small for size less than 128 bytes, Medium for size less then 16384 bytes, and Large in all other cases.  A decoder MAY discard a received Multipart payload if any of its L fields does not use the most compact encoding for the given size.</t>

      <section title="Small" anchor="small-length-encoding">
        <t>The Small encoding uses exactly one byte.  The MSB is set to 0, and the next 7 bits are used to represent an unsigned integer in range [0, 127].</t>
        <figure>
          <artwork align="center"><![CDATA[
 0   1 2 3 4 5 6 7 
+-+ +-+-+-+-+-+-+-+
|0| | 0x00 - 0x7F |
+-+ +-+-+-+-+-+-+-+
          ]]></artwork>
        </figure>
      </section>

      <section title="Medium" anchor="medium-length-encoding">
        <t>The Medium encoding uses exactly two bytes.  The two upper bits of the first byte are set to 1 and 0 respectively.  The following 14 bits are used to represent an unsigned integer in range [128, 16383].  Values outside this range MUST not be encoded using the Medium format.</t>
        <figure>
          <artwork align="center"><![CDATA[
 0 1   2 3 4 5 6 7 8 9 0 1 2 3 4 5
+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|1 0| |      0x0080 - 0x3FFF      |
+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      ]]></artwork>
        </figure>
      </section>

      <section title="Large" anchor="large-length-encoding">
        <t>The Large encoding uses a variable number of bytes (at least three) and is logically split into two parts.  The first part is exactly one byte with the two upper bits set to 1.  The lower 6 bits of the first byte encode the length's length (LL) as an unsigned integer which MUST NOT be less than 2.</t>
        <figure>
          <artwork align="center"><![CDATA[
 0 1   2 3 4 5 6 7 
+-+-+ +-+-+-+-+-+-+
|1 1| |0x02 - 0x3F|
+-+-+ +-+-+-+-+-+-+
      ]]></artwork>
        </figure>
        <t>The actual length of V -- which consumes at least 2 other bytes -- follows, encoded using as many bytes as declared by the preceding LL.</t>
            
        <t>When LL is 0x02, then the length MUST NOT be less than 0x4000.</t>
      </section>
    </section>

 <!--
    <section title="Examples" anchor="examples">
      <t>TODO</t>
      <figure>
        <artwork align="center"><![CDATA[
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|             TODO              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      ]]></artwork>
      </figure>
    </section>
-->

    <!-- This PI places the pagebreak correctly (before the section title) in the text output. -->
    <?rfc needLines="8" ?>

    <section anchor="IANA" title="IANA Considerations">
      <t>Applications using the Multipart Content-Format are supposed to define the internal structure of the Multipart representation, as well as registering its outermost type (typically one in range 10000-64999) in the "CoAP Content-Formats" sub-registry, within the "CoRE Parameters" registry.</t>
    </section>

    <section anchor="Security" title="Security Considerations">
      <t>The Large encoding may trigger insanely huge buffer allocations on the receiving party.  Receivers of Multipart SHOULD put a cap on the maximum allowed size of the whole Multipart.  A CoAP server MAY respond with a 4.13 (Request Entity Too Large) status code to such requests, and refuse to proceed further (i.e. processing remaining parts).</t>

      <t>A CoAP client can't tell whether a 4.15 (Unsupported Content-Format) status code applies to the whole Multipart or just to one of its sub-types.  An attacker may leverage on this ambiguity to craft application specific attacks (e.g. to cause downgraded behaviour).  Applications built on top of Multipart need to handle such eventuality in a safe way.</t>
    </section>
  </middle>

  <back>
    <references title="Normative References">
      &RFC2119;
      &I-D.ietf-core-coap;
    </references>
    <references title="Informative References">
      &RFC4627;
    </references>
  </back>
</rfc>

<!-- vim: set ts=2 : -->
