RFC 3958:Domain-Based Application Service Location...
RFC-Ref

11. Appendix A. Pseudo-Pseudocode for S-NAPTR

11.1. A.1. Finding the First (Best) Target


   Assuming the client supports 1 protocol for a particular application
   service, the following pseudocode outlines the expected process to
   find the first (best) target for the client, using S-NAPTR.

   target = [initial domain]
   naptr-done = false


   while (not naptr-done)
    {
     NAPTR-RRset = [DNSlookup of NAPTR RRs for target]
     [sort NAPTR-RRset by ORDER,and PREF within each ORDER]
     rr-done = false
     cur-rr = [first NAPTR RR]


     while (not rr-done)
        if ([SERVICE field of cur-rr contains desired application
             service and application protocol])
           rr-done = true
           target= [REPLACEMENT target of NAPTR RR]
        else
           cur-rr = [next rr in list]


        if (not empty [FLAG in cur-rr])
           naptr-done = true
    }


   port = -1


   if ([FLAG in cur-rr is "S"])
    {
     SRV-RRset = [DNSlookup of SRV RRs for target]
     [sort SRV-RRset based on PREF]
     target = [target of first RR of SRV-RRset]
     port = [port in first RR of SRV-RRset]
    }


   ; now, whether it was an "S" or an "A" in the NAPTR, we
   ; have the target for an A record lookup

   host = [DNSlookup of target]


   return (host, port)


11.2. A.2. Finding Subsequent Targets


   The pseudocode in Appendix A is crafted to find the first, most
   preferred host-port pair for a particular application service and
   protocol.  If, for any reason, that host-port pair did not work
   (connection refused, application-level error), the client is expected
   to try the next host-port in the S-NAPTR tree.

   The pseudocode above does not permit retries -- once complete, it
   sheds all context of where in the S-NAPTR tree it finished.
   Therefore, client software writers could

   o  entwine the application-specific protocol with the DNS lookup and
      RRset processing described in the pseudocode and continue the S-
      NAPTR processing if the application code fails to connect to a
      located host-port pair;

   o  use callbacks for the S-NAPTR processing; or

   o  use an S-NAPTR resolution routine that finds *all* valid servers
      for the required application service and protocol from the
      originating domain and that provides them in a sorted order for
      the application to try.



Google
Web
RFC-Ref