<?xml version="1.0" encoding="UTF-8"?>
<!-- This template is for creating an Internet Draft using xml2rfc,
     which is available here: http://xml.resource.org. -->
<!DOCTYPE rfc SYSTEM "rfc2629xslt/rfc2629.dtd" [
<!-- One method to get references from the online citation libraries.
     There has to be one entity for each item to be referenced.
     An alternate method (rfc include) is described in the references. -->

 <!-- Normative References -->
<!ENTITY RFC2119 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml">
<!ENTITY RFC2782 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.2782.xml">
<!ENTITY RFC3986 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.3986.xml">
<!ENTITY I-D.ietf-hybi-thewebsocketprotocol SYSTEM "http://xml.resource.org/public/rfc/bibxml3/reference.I-D.draft-ietf-hybi-thewebsocketprotocol-06.xml">
<!-- Informative References -->
 <!ENTITY RFC3261 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.3261.xml">
 <!ENTITY RFC6120 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.6120.xml">
 <!ENTITY I-D.barnes-hard-problem SYSTEM "http://xml.resource.org/public/rfc/bibxml3/reference.I-D.draft-barnes-hard-problem-00.xml">
 <!ENTITY RFC4033 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.4033.xml">
 <!ENTITY RFC5246 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.5246.xml">
 <!ENTITY RFC2616 SYSTEM "http://xml.resource.org/public/rfc/bibxml/reference.RFC.2616.xml">
 
 
]>

<?xml-stylesheet type='text/xsl' href='rfc2629.xslt' ?>
<!-- used by XSLT processors -->
<!-- For a complete list and description of processing instructions (PIs),
     please see http://xml.resource.org/authoring/README.html. -->
<!-- Below are generally applicable Processing Instructions (PIs) that most I-Ds might want to use.
     (Here they are set differently than their defaults in xml2rfc v1.32) -->
<?rfc strict="yes" ?>
<!-- give errors regarding ID-nits and DTD validation -->
<!-- control the table of contents (ToC) -->
<?rfc toc="yes"?>
<!-- generate a ToC -->
<?rfc tocdepth="4"?>
<!-- the number of levels of subsections in ToC. default: 3 -->
<!-- control references -->
<?rfc symrefs="yes"?>
<!-- use symbolic references tags, i.e, [RFC2119] instead of [1] -->
<?rfc sortrefs="yes" ?>
<!-- sort the reference entries alphabetically -->
<!-- control vertical white space
     (using these PIs as follows is recommended by the RFC Editor) -->
<?rfc compact="no" ?>
<!-- do not start each main section on a new page -->
<?rfc subcompact="no" ?>
<!-- keep one blank line between list items -->
<!-- end of list of popular I-D processing instructions -->

 <rfc category="std" docName="draft-ibc-websocket-dns-srv-02" ipr="trust200902">
<!-- category values: std, bcp, info, exp, and historic
     ipr values: full3667, noModification3667, noDerivatives3667
     you can add the attributes updates="NNNN" and obsoletes="NNNN"
     they will automatically be output with "(if approved)" -->

  <!-- ***** FRONT MATTER ***** -->

  <front>

    <!-- The abbreviated title is used in the page header - it is only necessary if the
         full title is longer than 39 characters -->
    <title abbrev="DNS SRV for WebSocket">DNS SRV Resource Records for the WebSocket Protocol</title>

    <!-- add 'role="editor"' below for the editors if appropriate -->

    <!-- Another author who claims to be an editor -->

    <author fullname="Inaki Baz Castillo" initials="I.B.C." surname="Baz Castillo">
      <organization>XtraTelecom S.A.</organization>

      <address>
        <postal>
          <street></street>
          <city>Barakaldo</city>
          <region>Basque Country</region>
          <code></code>
          <country>Spain</country>
        </postal>
        <email>ibc@aliax.net</email>
      </address>
    </author>

    <date month="April" year="2011" />
    <!-- If the month and year are both specified and are the current ones, xml2rfc will fill
         in the current day for you. If only the current year is specified, xml2rfc will fill
         in the current day and month for you. If the year is not the current one, it is
         necessary to specify at least a month (xml2rfc assumes day="1" if not specified for the
         purpose of calculating the expiry date).  With drafts it is normally sufficient to
         specify just the year. -->

    <!-- Meta-data Declarations -->

    <area>General</area>
    <!--<workgroup>HyBi Working Group</workgroup>-->
    <!-- WG name at the upperleft corner of the doc,
         IETF is fine for individual submissions.
         If this element is not present, the default is "Network Working Group",
         which is used by the RFC Editor as a nod to the history of the IETF. -->

    <keyword>WebSocket</keyword>
    <keyword>DNS</keyword>
    <keyword>SRV</keyword>
    <!-- Keywords will be incorporated into HTML output
         files in a meta tag but they have no effect on text or nroff
         output. If you submit your draft to the RFC Editor, the
         keywords will be used for the search engine. -->

    <abstract>
      <t>This document specifies the usage of DNS SRV resource records by WebSocket clients when resolving a "ws:" or "wss:" Uniform Resource Identifier (URI). The DNS SRV mechanism confers load-balancing and failover capabilities for WebSocket service providers.</t>
    </abstract>

  </front>



  <middle>

    <section title="Introduction" anchor="introduction">

      <t>DNS SRV <xref target="RFC2782"/> is widely implemented in realtime communication protocols as SIP <xref target="RFC3261"/> and XMPP <xref target="RFC6120"/>. In both protocols the clients perform a DNS SRV query to get a list of connection addresses (pairs of IP address and port) for the given domain. The administrator of the domain can configure its DNS SRV records in a way that they provide automatic load-balancing along with redundancy/failover capability.</t>

      <t>DNS SRV mechanism facilitates network applications scalability without requiring an intermediary node distributing the traffic in load-balancing or failover fashion. Instead, DNS SRV mechanism just requires a proper DNS setup.</t>

      <t>By introducing DNS SRV records into WebSocket protocol <xref target="I-D.ietf-hybi-thewebsocketprotocol"/>, WebSocket providers can, optionally, take same advantages and provide scalable services with a minimal infrastructure.</t>

      <t>This specification mandates the usage of DNS SRV resource records by WebSocket clients when resolving a "ws:" or "wss:" URI <xref target="RFC3986"/>, but still leaves the decision of using SRV records up to the service administrator.</t>

    </section>

    <section title="Conventions" anchor="conventions">

      <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 title="Implementation" anchor="implementation">

      <t>This specification mandates the implementation of DNS SRV <xref target="RFC2782"/> in WebSocket <xref target="I-D.ietf-hybi-thewebsocketprotocol"/> clients (usually web browsers). Said that, WebSocket clients MUST implement this specification.</t>

      <t>The client application (usually JavaScript code executed by the web browser) is not aware of the mechanism described in this document which is fully transparent for web developers and JavaScript developers. This is, the client application (usually JavaScript code) does not deal with DNS SRV resolution but just passes the given "ws:" or "wss:" URI to the WebSocket client which MUST perform steps in <xref target="client_usage"/>.</t>

      <t>It is up to the system administrator whether to set, or not, DNS SRV resource records for the WebSocket protocol within the provided service. This specification allows the system administrator to use the DNS SRV <xref target="RFC2782"/> mechanism to improve the service reliability by providing load-balancing and failover capabilities, but does not mandate it (the system administrator could choose whichever scalability strategy).</t>

    </section>
    

    <section title="Client Usage" anchor="client_usage">

      <t>DNS SRV lookup just applies when the host component of a WebSocket URI <xref target="RFC3986"/> is a domain and the URI does not contain an explicit port. If this is not the case, the WebSocket client MUST attempt the fallback process described in <xref target="fallback_process"/>.</t>

      <t>To clarify it, a WebSocket URI like "ws://example.org/myservice" requires the client to perform SRV resolution while "ws://example.org:80/myservice" does not (as the port is explicitly present in the URI).</t>
 
      <section title="SRV Lookup" anchor="srv_lookup">

        <t>Given a WebSocket URI ("ws:" or "wss:") in which the host component is a domain ("example.org") and the port is not present, the WebSocket client MUST perform the following steps:
          <list style="numbers">
            
            <t>If the scheme is "ws:", perform a DNS SRV query whose inputs are:
              <list style="symbols">
                <t>Service: "ws"</t>
                <t>Proto: "tcp"</t>
                <t>Name: The host component of the URI</t>
              </list>
              The resulting query looks like "_ws._tcp.example.org".
            </t>

            <t>If the scheme is "wss:", perform a DNS SRV query whose inputs are:
              <list style="symbols">
                <t>Service: "wss"</t>
                <t>Proto: "tcp"</t>
                <t>Name: The host component of the URI</t>
              </list>
              The resulting query looks like "_wss._tcp.example.org".
            </t>

            <t>If there is no SRV result, attempt the fallback process described in <xref target="fallback_process"/> and omit next steps.</t>

            <t>If there is SRV result, it will contain one or more DNS SRV resource records (combinations of a domain target, port, priority attribute and weight attribute as described in <xref target="RFC2782"/>).</t>

            <t>Choose one of the returned DNS SRV resource records (following the rules in <xref target="RFC2782"/>) and perform DNS A or AAAA lookups on the corresponding domain target. This will result in a list of one or more IPv4 or IPv6 addresses.

              <list style="empty">
                <t>If the DNS A or AAAA lookup returns no result, it is considered an error and next DNS SRV resource record (according to rules in <xref target="RFC2782"/>) MUST be tried.</t>
              </list>
            </t>

            <t>Use the first resolved IP address (with the corresponding port number in the DNS SRV resource record) as the connection address for the WebSocket service.
            
              <list style="empty">
                <t>The client MAY now perform steps in <xref target="reusing_tcp_connection"/> and reuse an existing TCP connection if available.</t>
              </list>
            </t>

            <t>If the WebSocket establishment fails using that connection address because of a server failure (according to <xref target="server_failure"/>) but the A or AAAA lookups returned more than one IP address, then use the next resolved IP address for the connection address (keeping same port).</t>

            <t>If the WebSocket establishment fails using all the resolved IP addresses for a given DNS SRV resource record, then repeat the process for the next DNS SRV resource record based on priority and weight attributes as defined in <xref target="RFC2782"/> until all the DNS SRV resource records have been tried.</t>

            <t>If all the attempts fail, internally report the WebSocket establishment error.</t>

          </list>
        </t>

        <t>When the client constructs the WebSocket handshake HTTP request, the URI MUST be set as described in Section 3.2 of <xref target="I-D.ietf-hybi-thewebsocketprotocol"/> regardless of the usage of SRV mechanism. This is, DNS SRV resolution for a "ws:" or "wss:" URI does not alter the usual construction of the WebSocket handshake request.</t>

      </section>

      <section title="Fallback Process" anchor="fallback_process">

        <t>The fallback process SHOULD be a normal A or AAAA address record resolution to determine the IPv4 or IPv6 address of the URI host component (or URI host value without DNS resolution if it contains an IP address).</t>

        <t>The server connection port is obtained as stated in Section 3.1 of <xref target="I-D.ietf-hybi-thewebsocketprotocol"/>.</t>

        <t>If multiple IP addresses have been obtained from a DNS A or AAAA lookup, the client MUST choose the first one and try to establish a WebSocket communication with it. In case such attempt fails because of a server failure (as defined in <xref target="server_failure"/>) the client MUST repeat the process for each remaining IP address.</t>

      </section>

      <section title="Reusing TCP Connection" anchor="reusing_tcp_connection">

        <t>A web browser is able to maintain persistent TCP connections with the HTTP <xref target="RFC2616"/> server and reuse them for sending new HTTP requests. Reusing an existing connection (when available) for WebSocket communication is a desirable behavior which just can take place when both the HTTP server and WebSocket server listen on the same IP address and port.</t>

        <t>This section defines how to reuse an existing connection after resolving the location of the WebSocket server using the DNS SRV procedures:

          <list style="numbers">
            <t>The WebSocket client performs the steps in <xref target="client_usage"/> and gets an ordered list of connection addresses (pairs of IP address and port) by following rules in <xref target="RFC2782"/>.</t>
            <t>For each connection address the client selects to communicate with, it first checks whether there already exists an established TCP connection against same IP address and port.</t>
            <t>If so, the client MAY reuse the existing TCP connection for initiating the WebSocket handshake rather than opening a new one.</t>
          </list>
        </t>

      </section>

      <section title="Server Failure" anchor="server_failure">

        <t>A WebSocket server failure occurs if the WebSocket establishment (TCP connection and WebSocket handshake procedure) fails because of a cause listed below:
          <list style="symbols">
            <t>TCP connection is not possible due to timeout or server side rejection.</t>
            <t>The server does not return a valid HTTP response for the WebSocket handshake request within a specified ammount of time (TODO: specify such ammount).</t>
            <t>The server replies a 500 or 503 HTTP error response during the WebSocket handshake meaning that it suffers of internal problems (i.e. congestion) so it is not currently capable of handling the request.
              <list style="empty">
                <t>If HTTP response code other than 101 (success), 500 or 503 is returned by the server, it MUST NOT be considered a server failure.</t>
                <t>TODO: <xref target="I-D.ietf-hybi-thewebsocketprotocol"/> should describe how to handle different HTTP response codes (as 401 or 302).</t>
              </list>
            </t> 
          </list>
        </t>

      </section>
              
    </section>
    

    <section title="Examples" anchor="examples">

      <t>By properly configuring domain SRV records, the WebSocket service administrator can take advantage of load-balancing and failover capabilities inherent in DNS SRV <xref target="RFC2782"/>. Sections below show some usage cases.</t>

        <section title="Load Balancing and Failover">

          <t>Assuming there are three hosts providing the WebSocket service for the URI "ws://example.org/myservice", the following zone file for a fictional example.org domain provides load-balancing and failover for the WebSocket traffic:

            <figure>
              <artwork>
                <![CDATA[
$ORIGIN example.org.
@         SOA   dns.example.org. root.example.org.
(2011040501 3600 3600 604800 86400)
NS        dns.example.org.
_ws._tcp  SRV   0 3 80 ws1.example.org.
_ws._tcp  SRV   0 1 90 ws2.example.org.
_ws._tcp  SRV   1 0 80 ws3.example.org.

dns       A     1.1.1.100
ws1       A     1.1.1.1
ws2       A     1.1.1.2
ws2       A     1.1.1.3
                 ]]>
              </artwork>
            </figure>
          </t>

          <t>
            <list style="symbols">
              <t>The first server with domain ws1.example.org listens on IP address 1.1.1.1 and port 80, and its associated DNS SRV record has priority 0 and weight 3.</t>
              <t>The second server with domain ws2.example.org listens on IP address 1.1.1.2 and port 90, and its associated DNS SRV record has priority 0 and weight 1.</t>
              <t>The third server with domain ws3.example.org listens on IP address 1.1.1.3 and port 80, and its associated DNS SRV record has priority 1 and weight 0.</t>
            </list>
          </t>
              
          <t>By following the steps in <xref target="client_usage"/>, 75% of WebSocket clients would choose the first server and the other 25% would choose the second server to communicate with (as both have the higest SRV priority 0 in their respective DNS SRV resource records, and the first server has a SRV weight value which triples the value of the second server).</t>

          <t>In case the WebSocket establishment fails because of a server failure (as defined in <xref target="server_failure"/>), WebSocket clients would try the other one.</t>

          <t>If the WebSocket establishment fails with both the first and second servers, WebSocket clients would then try the third server (as the priority value in its respective DNS SRV resource record is lower).</t>
 
      </section>


      <section title="Reusing TCP Connection">

        <t>In this case a server resolving to www.example.org is used for both HTTP and WebSocket traffic, while a second server resolving to ws2.example.com is used for balancing the WebSocket traffic.</t>

        <t>
          <figure>
            <artwork>
              <![CDATA[
$ORIGIN example.org.
@         SOA   dns.example.org. root.example.org.
(2011040501 3600 3600 604800 86400)
NS        dns.example.org.
_ws._tcp  SRV   0 1 80 www.example.org.
_ws._tcp  SRV   0 1 80 ws2.example.org.

dns       A     1.1.1.100
www       A     1.1.1.1
ws2       A     1.1.1.2
              ]]>
            </artwork>
          </figure>
        </t>
   
        <t>The client (presumably a web browser) would open one or more TCP connections with www.example.org and port 80 for the usual HTTP communication. As the retrieved data contains a WebSocket URI "ws://example.org/myservice" the client would also initialize a WebSocket communication so would perform steps in <xref target="client_usage"/>.</t>

        <t>Such DNS resolution would return two DNS SRV resource records (the first one with www.example.org as domain target and the second one with ws2.example.org as domain target), both of them with same priority and weight attributes.</t>

        <t>As per target selection rules in <xref target="RFC2782"/> it is expected that half of the clients would choose www.example.org domain target and port 80 as the WebSocket communication address so they MAY reuse an existing TCP connection previously opened rather than creating a new one.</t>

      </section>
   
    </section>



    <section title="Security Considerations" anchor="security_considerations">

      <t>Any Internet protocol offering DNS SRV resource records for locating servers is sensitive to security issues described in <xref target="I-D.barnes-hard-problem"/>. Usage of DNS security extensions (DNSSEC) as described in <xref target="RFC4033"/> is recommended to mitigate the problem.</t>

    </section>


    <section title="IANA Considerations" anchor="iana_considerations">

      <t>This specification registers two new SRV Service Labels:
        <list style="hanging" hangIndent="6">
          <t hangText="ws:">MUST be used when constructing a DNS SRV query to locate the WebSocket service address (for regular WebSocket connections).</t>
          <t hangText="wss:">MUST be used when constructing a DNS SRV query to locate the WebSocket service address (for WebSocket connections tunneled over TLS <xref target="RFC5246"/>).</t>
        </list>
      </t>
    </section>
    

  </middle>

  <!--  *****BACK MATTER ***** -->

  <back>
    <!-- References split into informative and normative -->

    <!-- There are 2 ways to insert reference entries from the citation libraries:
     1. define an ENTITY at the top, and use "ampersand character"RFC2629; here (as shown)
     2. simply use a PI "less than character"?rfc include="reference.RFC.2119.xml"?> here
        (for I-Ds: include="reference.I-D.narten-iana-considerations-rfc2434bis.xml")

     Both are cited textually in the same manner: by using xref elements.
     If you use the PI option, xml2rfc will, by default, try to find included files in the same
     directory as the including file. You can also define the XML_LIBRARY environment variable
     with a value containing a set of directories to search.  These can be either in the local
     filing system or remote ones accessed by http (http://domain/dir/... ).-->

    <references title="Normative References">
      &RFC2119;
      &RFC2782;
      &RFC3986;
      &I-D.ietf-hybi-thewebsocketprotocol;
    </references>

    <references title="Informative References">
      &RFC3261;
      &RFC6120;
      &RFC5246;
      &I-D.barnes-hard-problem;
      &RFC4033;
      &RFC2616;
    </references>


    <section anchor="changelog" title="Change Log (to be removed by RFC Editor prior to publication)">

      <section title="Changes in -02">
        <t>
          <list style="symbols">
            <t>Category changed to "std" (Standards-Track document).</t>
            <t>Editorial fixes.</t>
            <t>Section "Introduction" extended.</t>
            <t>Added a section "Implementation".</t>
            <t>Use "DNS SRV resource record" to refer a record in the DNS SRV lookup.</t>
            <t>Improvements in section "Fallback Process".</t>
            <t>Section "Websocket Establishment Fails" renamed to "Server Failure".</t>
            <t>Section "Examples" simplified.</t>
          </list>
        </t>
      </section>
 
      <section title="Changes in -01">
        <t>
          <list style="symbols">
            <t>Editorial fixes.</t>
            <t>Avoid the word "target" when referring to connection addresses.</t>
            <t>Improvements in section "Examples".</t>
          </list>
        </t>
      </section>

    </section>

  </back>

</rfc>
