maifeeulasad

draft-test-latest.md

Jan 26th, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smarty 18.46 KB | None | 0 0
  1. ---
  2. title: STUN/TURN using PHP in Despair
  3. abbrev: STuPiD
  4. docname: draft-hartke-xmpp-stupid-latest
  5. date: 2009-07-05
  6. category: info
  7.  
  8. ipr: trust200902
  9. area: General
  10. workgroup: XMPP Working Group
  11. keyword: Internet-Draft
  12.  
  13. stand_alone: yes
  14. pi: [toc, sortrefs, symrefs]
  15.  
  16. author:
  17.  -
  18.     ins: K. Hartke
  19.     name: Klaus Hartke
  20.     organization: Universität Bremen TZI
  21.     email: hartke@tzi.org
  22.  -
  23.     ins: C. Bormann
  24.     name: Carsten Bormann
  25.     org: Universität Bremen TZI
  26.     street: Postfach 330440
  27.     city: Bremen
  28.     code: D-28359
  29.     country: Germany
  30.     phone: +49-421-218-63921
  31.     facsimile: +49-421-218-7000
  32.     email: cabo@tzi.org
  33.  
  34. normative:
  35.   RFC2119:
  36.   RFC3986:
  37.   RFC4086:
  38.   RFC4648:
  39.  
  40. informative:
  41.   RFC5389:
  42.   I-D.ietf-behave-turn:
  43.   STUNT:
  44.     target: http://deusty.blogspot.com/2007/09/stunt-out-of-band-channels.html
  45.     title: STUNT & out-of-band channels
  46.     author:
  47.       name: Robbie Hanson
  48.       ins: R. Hanson
  49.     date: 2007-09-17
  50.   I-D.meyer-xmpp-e2e-encryption:
  51.   I-D.ietf-xmpp-3920bis:
  52.  
  53.  
  54.  
  55. --- abstract
  56.  
  57. NAT (Network Address Translator) Traversal may require TURN
  58. (Traversal Using Relays around NAT) functionality in certain
  59. cases that are not unlikely to occur.  There is little
  60. incentive to deploy TURN servers, except by those who need
  61. them — who may not be in a position to deploy a new protocol
  62. on an Internet-connected node, in particular not one with
  63. deployment requirements as high as those of TURN.
  64.  
  65. "STUN/TURN using PHP in Despair" is a highly deployable
  66. protocol for obtaining TURN-like functionality, while also
  67. providing the most important function of STUN.
  68.  
  69. --- middle
  70.  
  71. Introduction        {#problems}
  72. ============
  73.  
  74. NAT (Network Address Translator) Traversal may require TURN
  75. (Traversal Using Relays around NAT)
  76. {{I-D.ietf-behave-turn}}
  77. functionality in certain
  78. cases that are not unlikely to occur.  There is little
  79. incentive to deploy TURN servers, except by those who need
  80. them — who may not be in a position to deploy a new protocol
  81. on an Internet-connected node, in particular not one with
  82. deployment requirements as high as those of TURN.
  83.  
  84. "STUN/TURN using PHP in Despair" is a highly deployable
  85. protocol for obtaining TURN-like functionality, while also
  86. providing the most important function of STUN
  87. {{RFC5389}}.
  88.  
  89. The high degree of deployability is achieved by making STuPiD
  90. a Web service, implementable in any Web application deployment
  91. scheme.  As PHP appears to be the solution of choice for
  92. avoiding deployment problems in the Web world, a PHP-based
  93. sample implementation of STuPiD is presented in {{figimpl}} in {{impl}}.
  94. (This single-page script has been tested with a free-of-charge
  95. web hoster, so it should be deployable by literally everyone.)
  96.  
  97.  
  98. The Need for Standardization   {#need}
  99. ----------------------------
  100.  
  101. If STuPiD is so easy to deploy, why standardize on it?
  102. First of all, STuPiD server implementations will be done by
  103. other people than the clients making use of the service.
  104. Clearly communicating between these communities is a good
  105. idea, in particular if there are security considerations.
  106.  
  107. Having one standard form of STuPiD service instead of one
  108. specific to each kind of client also creates an incentive
  109. for optimized implementations.
  110.  
  111. Finally, where STuPiD becomes part of a client standard
  112. (such as a potential extension to XMPP's in-band byte-stream
  113. protocol as hinted in {{xmpp}}), it is a good
  114. thing if STuPiD is already defined.
  115.  
  116. Hence, this document focuses on the definition of the STuPiD
  117. service itself, tries to make this as general as possible
  118. without increasing complexity or cost and leaves the details
  119. of any client standards to future documents.
  120.  
  121.  
  122. Basic Protocol Operation   {#ops}
  123. ========================
  124.  
  125. The STuPiD protocol will typically be used with application
  126. instances that first attempt to obtain connectivity using
  127. mechanisms similar to those described in the STUN
  128. specification {{RFC5389}}.  However, with STuPiD,
  129. STUN is not really needed for TCP, as was demonstrated in
  130. previous STUN-like implementations {{STUNT}}.
  131. Instead, STuPiD (like {{STUNT}}) provides a
  132. simple Web service that
  133. echoes the remote address and port of an incoming HTTP
  134. request; in most cases, this is enough to get the job done.
  135.  
  136. In case no connection can be established with this simple
  137. STUN(T)-like mechanism, a TURN-like relay is needed as a final
  138. fall-back.
  139. The STuPiD protocol supports this, but solely provides a way
  140. for the data to be
  141. relayed.  STuPiD relies on an out-of-band channel to notify
  142. the peer whenever new data is available (synchronization signal).
  143. See {{xmpp}} for one likely example of such an
  144. out-of-band channel.
  145. (Note that the out-of-band channel may have a much lower
  146. throughput than the STuPiD relay channel — this is exactly
  147. the case in the example provided in {{xmpp}},
  148. where the out-of-band channel is typically throughput-limited
  149. to on the order of a few kilobits per second.)
  150.  
  151. By designing the STuPiD web service in such a way that it can
  152. be implemented by a simple PHP script such as that presented
  153. in {{impl}}, it is easy to deploy by those who
  154. need the STuPiD services.
  155. The combination of the low-throughput out-of-band channel for
  156. synchronization and the STuPiD web service for bulk data
  157. relaying is somewhat silly but gets the job done.
  158.  
  159. The STuPiD data relay is implemented as follows (see {{figops}}):
  160.  
  161. 1. Peer A, the source of the data to be relayed, stores a chunk of
  162.    data at the STuPiD server using an opaque identifier, the "chunk
  163.    identifier". How that chunk identifier is chosen is local to Peer
  164.    A; it could be composed of a random session id and a sequence number.
  165.  
  166. 2. Peer A notifies the receiver of the data, Peer
  167.    B, that a new data chunk is available, specifying the URI needed
  168.    for retrieval.
  169.    This notification is provided through an out-out-band channel.
  170.    (As an optimization for multiple consecutive transfers, A might
  171.    inform B once of a constant prefix of that URI and only send a
  172.    varying part such as a sequence number in each notification —
  173.    this is something to be decided in the client-client notification
  174.    protocol.)
  175.  
  176. 3. Peer B retrieves the data from the STuPiD server using the URI
  177.    provided by Peer A.
  178.  
  179. Note that the data transfer mechanism is one-way, i.e. to send
  180. data in the other direction as well, Peer B needs to perform
  181. the same steps using a STuPiD server at the same or a
  182. different location.
  183.  
  184. ~~~~~~~~~~
  185.  
  186.  
  187.         STuPiD   ```````````````````````````````,
  188.         Script   <----------------------------. ,
  189.                                              | ,
  190.          ^ ,                                 | ,
  191.          | ,                                 | ,
  192.    (1)   | ,                                 | ,  (3)
  193.    POST  | ,                                 | ,  GET
  194.          | ,                                 | ,
  195.          | v                                 | v
  196.  
  197.        Peer A   ----------------------->   Peer B
  198.                            (2)
  199.                        out-of-band
  200.                        Notification
  201. ~~~~~~~~~~
  202. {: #figops title="STuPiD Protocol Operation"}
  203.  
  204.  
  205. Protocol Definition
  206. ===================
  207.  
  208. Terminology          {#Terminology}
  209. -----------
  210. In this document, the key words "MUST", "MUST NOT", "REQUIRED",
  211. "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY",
  212. and "OPTIONAL" are to be interpreted as described in BCP 14, RFC 2119
  213. {{RFC2119}} and indicate requirement levels for compliant STuPiD
  214. implementations.
  215.  
  216.  
  217. Discovering External IP Address and Port
  218. ----------------------------------------
  219.  
  220. A client may discover its external IP address and the port
  221. required for port prediction by performing a HTTP GET
  222. request to a STuPiD server. The STuPiD server MUST reply
  223. with the remote address and remote port in the following
  224. format:
  225.  
  226. host ":" port
  227.  
  228. where 'host' and 'port' are defined as in {{RFC3986}}.
  229.  
  230.  
  231. Storing Data
  232. ------------
  233.  
  234. Data chunks are stored using the POST request of HTTP. The
  235. STuPiD server MUST support one URI parameter which is passed
  236. as query-string:
  237.  
  238. 'chid':  A unique ID identifying the data chunk to be stored.
  239. The ID SHOULD be chosen from the characters of the base64url
  240. set {{RFC4648}}.
  241.  
  242. The payload of the POST request MUST be the data to be
  243. stored. The 'Content-Type' SHOULD be
  244. 'application/octet-stream', although a STuPiD server
  245. implementation SHOULD simply ignore the 'Content-Type' as a
  246. client implementation may be restricted and may not able to
  247. specify a specific 'Content-Type'.  (E.g., in certain cases,
  248. the peer may be limited to sending the data as
  249. multipart-form-encoded — still, the data is stored as a
  250. byte stream.)
  251.  
  252. STuPiD servers may reject data chunks that are larger than
  253. some predefined limit.
  254. This maximum size in bytes of each data chunk is RECOMMENDED
  255. to be 65536 or more.
  256.  
  257. As HTTP already provides data transparency,
  258. the data chunk SHOULD NOT be encoded using Base64 or any
  259. other data transparency mechanism; in any case, the STuPiD
  260. server will not attempt to decode the chunk.
  261.  
  262. The sender MUST wait for the HTTP response before
  263. going on to notify the receiver.
  264.  
  265.  
  266. Notification
  267. ------------
  268.  
  269. The sender notifies the receiver of the data chunk by passing
  270. via an out-of-band channel (which is not part of the STuPiD
  271. protocol):
  272.  
  273. The full URL from which the data chunk can be retrieved,
  274. i.e. the same URL that was used to store the data chunk,
  275. including the chunk ID parameter.
  276.  
  277. The exact notification mechanism over the out-of-band channel
  278. and the definition of a session is dependent on the
  279. out-of-band channel.  See {{xmpp}} for one
  280. example of such an out-of-band channel.
  281.  
  282.  
  283. Retrieving Data
  284. ---------------
  285.  
  286. The notified peer retrieves the data chunk using a GET request
  287. with the URL supplied by the sender. The STuPiD server MUST
  288. set the 'Content-Type' of the returned body to
  289. 'application/octet-stream'.
  290.  
  291.  
  292. Implementation Notes
  293. ====================
  294.  
  295. A STuPiD server implementation SHOULD delete stored data some
  296. time after it was stored. It is RECOMMENDED not to delete the
  297. data before five minutes have elapsed after it was stored.
  298. Different client protocols will have different reactions to
  299. data that have been deleted prematurely and cannot be
  300. retrieved by the notified peer; this may be as trivial as
  301. packet loss or it may cause a reliable byte-stream to fail
  302. ({{impl}}).
  303. (TODO: It may be useful to provide some hints in the storing
  304. POST request.)
  305.  
  306. STuPiD clients should aggregate data in order to minimize the
  307. number of requests to the STuPiD server per second.
  308. The specific aggregation method chosen depends on the data
  309. rate required (and the maximum chunk size), the latency
  310. requirements, and the application semantics.
  311.  
  312. Clearly, it is up to the implementation to decide how the data
  313. chunks are actually stored.  A sufficiently silly STuPiD server
  314. implementation might for instance use a MySQL database.
  315.  
  316.  
  317. Security Considerations
  318. =======================
  319.  
  320. The security objectives of STuPiD are to be as secure as if
  321. NAT traversal had succeeded, i.e., an on-path attacker can
  322. overhear and fake messages, but an off-path attacker cannot.
  323. If a higher level of security is desired, it should be
  324. provided on top of the data relayed by STuPiD, e.g. by using
  325. XTLS {{I-D.meyer-xmpp-e2e-encryption}}.
  326.  
  327. Much of the security of STuPiD is based on the assumption that
  328. an off-path attacker cannot guess the chunk identifiers.  A
  329. suitable source of randomness {{RFC4086}} should
  330. be used to generate at least a sufficiently large part of the
  331. chunk identifiers (e.g., the chunk identifier could be a hard
  332. to guess prefix followed by a serial number).
  333.  
  334. To protect the STuPiD server against denial of service and
  335. possibly some forms of theft of service, it is RECOMMENDED
  336. that the POST side of the STuPiD server be protected by some
  337. form of authentication such as HTTP authentication.  There is
  338. little need to protect the GET side.
  339.  
  340. --- back
  341.  
  342.  
  343. Examples  {#xmp}
  344. ========
  345.  
  346. This appendix provides some examples of the STuPiD protocol operation.
  347.  
  348. ~~~~~~~~~~
  349.    Request:
  350.  
  351.       GET /stupid.php HTTP/1.0
  352.       User-Agent: Example/1.11.4
  353.       Accept: */*
  354.       Host: example.org
  355.       Connection: Keep-Alive
  356.  
  357.    Response:
  358.  
  359.       HTTP/1.1 200 OK
  360.       Date: Sun, 05 Jul 2009 00:30:37 GMT
  361.       Server: Apache/2.2
  362.       Cache-Control: no-cache, must-revalidate
  363.       Expires: Sat, 26 Jul 1997 05:00:00 GMT
  364.       Vary: Accept-Encoding
  365.       Content-Length: 17
  366.       Keep-Alive: timeout=1, max=400
  367.       Connection: Keep-Alive
  368.       Content-Type: application/octet-stream
  369.  
  370.       192.0.2.239:36654
  371. ~~~~~~~~~~
  372. {: #figxmpdisco title="Discovering External IP Address and Port"}
  373.  
  374. ~~~~~~~~~~
  375.    Request:
  376.  
  377.       POST /stupid.php?chid=i781hf64-0 HTTP/1.0
  378.       User-Agent: Example/1.11.4
  379.       Accept: */*
  380.       Host: example.org
  381.       Connection: Keep-Alive
  382.       Content-Type: application/octet-stream
  383.       Content-Length: 11
  384.  
  385.       Hello World
  386.  
  387.    Response:
  388.  
  389.       HTTP/1.1 200 OK
  390.       Date: Sun, 05 Jul 2009 00:20:34 GMT
  391.       Server: Apache/2.2
  392.       Cache-Control: no-cache, must-revalidate
  393.       Expires: Sat, 26 Jul 1997 05:00:00 GMT
  394.       Vary: Accept-Encoding
  395.       Content-Length: 0
  396.       Keep-Alive: timeout=1, max=400
  397.       Connection: Keep-Alive
  398.       Content-Type: application/octet-stream
  399. ~~~~~~~~~~
  400. {: #figxmpstore title="Storing Data"}
  401.  
  402. ~~~~~~~~~~
  403.    Request:
  404.  
  405.       GET /stupid.php?chid=i781hf64-0 HTTP/1.0
  406.       User-Agent: Example/1.11.4
  407.       Accept: */*
  408.       Host: example.org
  409.       Connection: Keep-Alive
  410.  
  411.    Response:
  412.  
  413.       HTTP/1.1 200 OK
  414.       Date: Sun, 05 Jul 2009 00:21:29 GMT
  415.       Server: Apache/2.2
  416.       Cache-Control: no-cache, must-revalidate
  417.       Expires: Sat, 26 Jul 1997 05:00:00 GMT
  418.       Vary: Accept-Encoding
  419.       Content-Length: 11
  420.       Keep-Alive: timeout=1, max=400
  421.       Connection: Keep-Alive
  422.       Content-Type: application/octet-stream
  423.  
  424.       Hello World
  425. ~~~~~~~~~~
  426. {: #figxmpretr title="Retrieving Data"}
  427.  
  428.  
  429. Sample Implementation     {#impl}
  430. =====================
  431.  
  432. ~~~~~~~~~~
  433. <?php
  434. header("Cache-Control: no-cache, must-revalidate");
  435. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  436. header("Content-Type: application/octet-stream");
  437.  
  438. mysql_connect(localhost, "username", "password");
  439. mysql_select_db("stupid");
  440.  
  441. $chid = mysql_real_escape_string($_GET["chid"]);
  442.  
  443. if ($_SERVER["REQUEST_METHOD"] == "GET") {
  444.   if (empty($chid)) {
  445.      echo $_SERVER["REMOTE_ADDR"] . ":" . $_SERVER["REMOTE_PORT"];
  446.   } elseif ($result = mysql_query("SELECT `data` FROM `Data` " .
  447.                         "WHERE `chid` = '$chid'")) {
  448.      if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  449.         echo base64_decode($row["data"]);
  450.      } else {
  451.         header("HTTP/1.0 404 Not Found");
  452.      }
  453.      mysql_free_result($result);
  454.   } else {
  455.      header("HTTP/1.0 404 Not Found");
  456.   }
  457. } elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
  458.   if (empty($chid)) {
  459.      header("HTTP/1.0 404 Not Found");
  460.   } else {
  461.      mysql_query("DELETE FROM `Data` " .
  462.                  "WHERE `timestamp` < DATE_SUB(NOW(), INTERVAL 5 MINUTE)");
  463.      $data = base64_encode(file_get_contents("php://input"));
  464.      if (!mysql_query("INSERT INTO `Data` (`chid`, `data`) " .
  465.                       "VALUES ('$chid', '$data')")) {
  466.         header("HTTP/1.0 403 Bad Request");
  467.      }
  468.   }
  469. } else {
  470.   header("HTTP/1.0 405 Method Not Allowed");
  471.   header("Allow: GET, HEAD, POST");
  472. }
  473. mysql_close();
  474. ?>
  475. ~~~~~~~~~~
  476. {: #figimpl title="STuPiD Sample Implementation"}
  477.  
  478.  
  479. Using XMPP as Out-Of-Band Channel  {#xmpp}
  480. =================================
  481.  
  482. XMPP {{I-D.ietf-xmpp-3920bis}} is a good choice for
  483. an out-of-band channel.
  484.  
  485. The notification protocol is closely modeled after XMPP's
  486. In-Band Bytestreams (IBB, see
  487. http://xmpp.org/extensions/xep-0047.html). Just replace the
  488. namespace and insert the STuPiD Retrieval URI instead of the
  489. actual Base64 encoded data, see {{figxmpnots}}.
  490. (Note that the current proposal redundantly sends a sid and a
  491. seq as well as the chid composed of these two; it may be
  492. possible to optimize this, possibly sending the constant prefix
  493. of the URI once at bytestream creation time.)
  494.  
  495. Notifications MUST be processed in the order they are
  496. received. If an out-of-sequence notification is received for a
  497. particular session (determined by checking the 'seq' attribute),
  498. then this indicates that a notification has been lost. The
  499. recipient MUST NOT process such an out-of-sequence notification,
  500. nor any that follow it within the same session; instead, the
  501. recipient MUST consider the session invalid.  (Adapted from
  502. http://xmpp.org/extensions/xep-0047.html#send)
  503.  
  504. Of course, other methods can be used for setup and teardown, such as Jingle
  505. (see http://xmpp.org/extensions/xep-0261.html).
  506.  
  507. ~~~~~~~~~~
  508.       <iq from='romeo@montague.net/orchard'
  509.          id='jn3h8g65'
  510.          to='juliet@capulet.com/balcony'
  511.          type='set'>
  512.         <open xmlns='urn:xmpp:tmp:stupid'
  513.              block-size='65536'
  514.              sid='i781hf64'
  515.              stanza='iq'/>
  516.       </iq>
  517. ~~~~~~~~~~
  518. {: #figxmpcri title="Creating a Bytestream: Initiator requests session"}
  519.  
  520.  
  521. ~~~~~~~~~~
  522.       <iq from='juliet@capulet.com/balcony'
  523.          id='jn3h8g65'
  524.          to='romeo@montague.net/orchard'
  525.          type='result'/>
  526. ~~~~~~~~~~
  527. {: #figxmpcrr title="Creating a Bytestream: Responder accepts session"}
  528.  
  529.  
  530.  
  531. ~~~~~~~~~~
  532.       <iq from='romeo@montague.net/orchard'
  533.          id='kr91n475'
  534.          to='juliet@capulet.com/balcony'
  535.          type='set'>
  536.         <data xmlns='urn:xmpp:tmp:stupid'
  537.              seq='0'
  538.              sid='i781hf64'
  539.              url='http://example.org/stupid.php?chid=i781hf64-0'/>
  540.       </iq>
  541. ~~~~~~~~~~
  542. {: #figxmpnots title="Sending Notifications: Notification in an IQ stanza"}
  543.  
  544. ~~~~~~~~~~
  545.       <iq from='juliet@capulet.com/balcony'
  546.          id='kr91n475'
  547.          to='romeo@montague.net/orchard'
  548.          type='result'/>
  549. ~~~~~~~~~~
  550. {: #figxmpnota title="Sending Notifications: Acknowledging notification using IQ"}
  551.  
  552. ~~~~~~~~~~
  553.       <iq from='romeo@montague.net/orchard'
  554.          id='us71g45j'
  555.          to='juliet@capulet.com/balcony'
  556.          type='set'>
  557.         <close xmlns='urn:xmpp:tmp:stupid'
  558.               sid='i781hf64'/>
  559.       </iq>
  560. ~~~~~~~~~~
  561. {: #figxmpclor title="Closing the Bytestream: Request"}
  562.  
  563. ~~~~~~~~~~
  564.       <iq from='juliet@capulet.com/balcony'
  565.          id='us71g45j'
  566.          to='romeo@montague.net/orchard'
  567.          type='result'/>
  568. ~~~~~~~~~~
  569. {: #figxmpclos title="Closing the Bytestream: Success response"}
  570.  
Add Comment
Please, Sign In to add comment