PageRenderTime 101ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 1ms

/OverSim-20101103/doc/doxy/stun_8cc_source.html

https://bitbucket.org/indigopony/omnetproject
HTML | 2544 lines | 2543 code | 0 blank | 1 comment | 0 complexity | 8fed1a93e64c11b01c0e744ec6daf511 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, GPL-2.0, Apache-2.0, JSON
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <title>OverSim: stun.cc Source File</title>
  6. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  7. <link href="doxygen.css" rel="stylesheet" type="text/css"/>
  8. </head>
  9. <body>
  10. <!-- Generated by Doxygen 1.7.1 -->
  11. <div class="navigation" id="top">
  12. <div class="tabs">
  13. <ul class="tablist">
  14. <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
  15. <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
  16. <li><a href="namespaces.html"><span>Namespaces</span></a></li>
  17. <li><a href="annotated.html"><span>Classes</span></a></li>
  18. <li class="current"><a href="files.html"><span>Files</span></a></li>
  19. <li><a href="dirs.html"><span>Directories</span></a></li>
  20. </ul>
  21. </div>
  22. <div class="tabs2">
  23. <ul class="tablist">
  24. <li><a href="files.html"><span>File&nbsp;List</span></a></li>
  25. <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
  26. </ul>
  27. </div>
  28. <div class="navpath">
  29. <ul>
  30. <li><a class="el" href="dir_668dee661430860db5868c2621a9c372.html">src</a> </li>
  31. <li><a class="el" href="dir_12590045ec6dcd64f3c4ad499444ef83.html">underlay</a> </li>
  32. <li><a class="el" href="dir_e7abd501ef2699b72177966ee598d65b.html">singlehostunderlay</a> </li>
  33. <li><a class="el" href="dir_e172c4d23ca9cdab66e1c382ceb2ddaa.html">stun</a> </li>
  34. </ul>
  35. </div>
  36. </div>
  37. <div class="header">
  38. <div class="headertitle">
  39. <h1>stun.cc</h1> </div>
  40. </div>
  41. <div class="contents">
  42. <a href="stun_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#include &lt;cassert&gt;</span>
  43. <a name="l00002"></a>00002 <span class="preprocessor">#include &lt;cstring&gt;</span>
  44. <a name="l00003"></a>00003 <span class="preprocessor">#include &lt;iostream&gt;</span>
  45. <a name="l00004"></a>00004 <span class="preprocessor">#include &lt;cstdlib&gt;</span>
  46. <a name="l00005"></a>00005 <span class="preprocessor">#include &lt;errno.h&gt;</span>
  47. <a name="l00006"></a>00006
  48. <a name="l00007"></a>00007 <span class="preprocessor">#ifdef WIN32</span>
  49. <a name="l00008"></a>00008 <span class="preprocessor"></span><span class="preprocessor">#include &lt;winsock2.h&gt;</span>
  50. <a name="l00009"></a>00009 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
  51. <a name="l00010"></a>00010 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
  52. <a name="l00011"></a>00011 <span class="preprocessor">#include &lt;io.h&gt;</span>
  53. <a name="l00012"></a>00012 <span class="preprocessor">#include &lt;time.h&gt;</span>
  54. <a name="l00013"></a>00013 <span class="preprocessor">#else</span>
  55. <a name="l00014"></a>00014 <span class="preprocessor"></span>
  56. <a name="l00015"></a>00015 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
  57. <a name="l00016"></a>00016 <span class="preprocessor">#include &lt;unistd.h&gt;</span>
  58. <a name="l00017"></a>00017 <span class="preprocessor">#include &lt;string.h&gt;</span>
  59. <a name="l00018"></a>00018 <span class="preprocessor">#include &lt;sys/ioctl.h&gt;</span>
  60. <a name="l00019"></a>00019 <span class="preprocessor">#include &lt;sys/socket.h&gt;</span>
  61. <a name="l00020"></a>00020 <span class="preprocessor">#include &lt;sys/time.h&gt;</span>
  62. <a name="l00021"></a>00021 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
  63. <a name="l00022"></a>00022 <span class="preprocessor">#include &lt;arpa/inet.h&gt;</span>
  64. <a name="l00023"></a>00023 <span class="preprocessor">#include &lt;fcntl.h&gt;</span>
  65. <a name="l00024"></a>00024 <span class="preprocessor">#include &lt;netdb.h&gt;</span>
  66. <a name="l00025"></a>00025 <span class="preprocessor">#include &lt;netinet/in.h&gt;</span>
  67. <a name="l00026"></a>00026 <span class="preprocessor">#include &lt;arpa/nameser.h&gt;</span>
  68. <a name="l00027"></a>00027 <span class="preprocessor">#include &lt;resolv.h&gt;</span>
  69. <a name="l00028"></a>00028 <span class="preprocessor">#include &lt;net/if.h&gt;</span>
  70. <a name="l00029"></a>00029
  71. <a name="l00030"></a>00030 <span class="preprocessor">#endif</span>
  72. <a name="l00031"></a>00031 <span class="preprocessor"></span>
  73. <a name="l00032"></a>00032
  74. <a name="l00033"></a>00033 <span class="preprocessor">#if defined(__sparc__) || defined(WIN32)</span>
  75. <a name="l00034"></a>00034 <span class="preprocessor"></span><span class="preprocessor">#define NOSSL</span>
  76. <a name="l00035"></a>00035 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
  77. <a name="l00036"></a><a class="code" href="stun_8cc.html#a6072c634968940fb89f70b65cf072c6a">00036</a> <span class="preprocessor"></span><span class="preprocessor">#define NOSSL</span>
  78. <a name="l00037"></a>00037 <span class="preprocessor"></span>
  79. <a name="l00038"></a>00038 <span class="preprocessor">#include &quot;<a class="code" href="stun__udp_8h.html">stun_udp.h</a>&quot;</span>
  80. <a name="l00039"></a>00039 <span class="preprocessor">#include &quot;<a class="code" href="stun_8h.html">stun.h</a>&quot;</span>
  81. <a name="l00040"></a>00040
  82. <a name="l00041"></a>00041
  83. <a name="l00042"></a>00042 <span class="keyword">using namespace </span>std;
  84. <a name="l00043"></a>00043
  85. <a name="l00044"></a>00044
  86. <a name="l00045"></a>00045 <span class="keyword">static</span> <span class="keywordtype">void</span>
  87. <a name="l00046"></a>00046 <a class="code" href="stun_8cc.html#afdae90956a999518a90595e783525734">computeHmac</a>(<span class="keywordtype">char</span>* hmac, <span class="keyword">const</span> <span class="keywordtype">char</span>* input, <span class="keywordtype">int</span> length, <span class="keyword">const</span> <span class="keywordtype">char</span>* key, <span class="keywordtype">int</span> keySize);
  88. <a name="l00047"></a>00047
  89. <a name="l00048"></a>00048 <span class="keyword">static</span> <span class="keywordtype">bool</span>
  90. <a name="l00049"></a><a class="code" href="stun_8cc.html#a16624b9ba3d6b2f1c821571fa467c544">00049</a> <a class="code" href="stun_8cc.html#a16624b9ba3d6b2f1c821571fa467c544">stunParseAtrAddress</a>( <span class="keywordtype">char</span>* body, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hdrLen, <a class="code" href="structStunAtrAddress4.html">StunAtrAddress4</a>&amp; result )
  91. <a name="l00050"></a>00050 {
  92. <a name="l00051"></a>00051 <span class="keywordflow">if</span> ( hdrLen != 8 )
  93. <a name="l00052"></a>00052 {
  94. <a name="l00053"></a>00053 clog &lt;&lt; <span class="stringliteral">&quot;hdrLen wrong for Address&quot;</span> &lt;&lt;endl;
  95. <a name="l00054"></a>00054 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  96. <a name="l00055"></a>00055 }
  97. <a name="l00056"></a>00056 result.<a class="code" href="structStunAtrAddress4.html#afe6ed0534166b0fd194b23b01ddad7b4">pad</a> = *body++;
  98. <a name="l00057"></a>00057 result.<a class="code" href="structStunAtrAddress4.html#ab31f05e3cba900d7613bc9f226f171d0">family</a> = *body++;
  99. <a name="l00058"></a>00058 <span class="keywordflow">if</span> (result.<a class="code" href="structStunAtrAddress4.html#ab31f05e3cba900d7613bc9f226f171d0">family</a> == <a class="code" href="stun_8h.html#ac2c7bc41e1b918c98583006fd7025bc5" title="define a structure to hold a stun address">IPv4Family</a>)
  100. <a name="l00059"></a>00059 {
  101. <a name="l00060"></a>00060 <a class="code" href="stun_8h.html#accc14a7c843716dfed734f3a03e8d2b6">UInt16</a> nport;
  102. <a name="l00061"></a>00061 memcpy(&amp;nport, body, 2); body+=2;
  103. <a name="l00062"></a>00062 result.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = ntohs(nport);
  104. <a name="l00063"></a>00063
  105. <a name="l00064"></a>00064 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a> naddr;
  106. <a name="l00065"></a>00065 memcpy(&amp;naddr, body, 4); body+=4;
  107. <a name="l00066"></a>00066 result.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = ntohl(naddr);
  108. <a name="l00067"></a>00067 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  109. <a name="l00068"></a>00068 }
  110. <a name="l00069"></a>00069 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (result.<a class="code" href="structStunAtrAddress4.html#ab31f05e3cba900d7613bc9f226f171d0">family</a> == <a class="code" href="stun_8h.html#a5da56f5efebd69e84bf5fdb74a02bf8a">IPv6Family</a>)
  111. <a name="l00070"></a>00070 {
  112. <a name="l00071"></a>00071 clog &lt;&lt; <span class="stringliteral">&quot;ipv6 not supported&quot;</span> &lt;&lt; endl;
  113. <a name="l00072"></a>00072 }
  114. <a name="l00073"></a>00073 <span class="keywordflow">else</span>
  115. <a name="l00074"></a>00074 {
  116. <a name="l00075"></a>00075 clog &lt;&lt; <span class="stringliteral">&quot;bad address family: &quot;</span> &lt;&lt; result.<a class="code" href="structStunAtrAddress4.html#ab31f05e3cba900d7613bc9f226f171d0">family</a> &lt;&lt; endl;
  117. <a name="l00076"></a>00076 }
  118. <a name="l00077"></a>00077
  119. <a name="l00078"></a>00078 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  120. <a name="l00079"></a>00079 }
  121. <a name="l00080"></a>00080
  122. <a name="l00081"></a>00081 <span class="keyword">static</span> <span class="keywordtype">bool</span>
  123. <a name="l00082"></a><a class="code" href="stun_8cc.html#af2f9f10bd394cb42037f1997266b978a">00082</a> <a class="code" href="stun_8cc.html#af2f9f10bd394cb42037f1997266b978a">stunParseAtrChangeRequest</a>( <span class="keywordtype">char</span>* body, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hdrLen, <a class="code" href="structStunAtrChangeRequest.html">StunAtrChangeRequest</a>&amp; result )
  124. <a name="l00083"></a>00083 {
  125. <a name="l00084"></a>00084 <span class="keywordflow">if</span> ( hdrLen != 4 )
  126. <a name="l00085"></a>00085 {
  127. <a name="l00086"></a>00086 clog &lt;&lt; <span class="stringliteral">&quot;hdr length = &quot;</span> &lt;&lt; hdrLen &lt;&lt; <span class="stringliteral">&quot; expecting &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(result) &lt;&lt; endl;
  128. <a name="l00087"></a>00087
  129. <a name="l00088"></a>00088 clog &lt;&lt; <span class="stringliteral">&quot;Incorrect size for ChangeRequest&quot;</span> &lt;&lt; endl;
  130. <a name="l00089"></a>00089 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  131. <a name="l00090"></a>00090 }
  132. <a name="l00091"></a>00091 <span class="keywordflow">else</span>
  133. <a name="l00092"></a>00092 {
  134. <a name="l00093"></a>00093 memcpy(&amp;result.<a class="code" href="structStunAtrChangeRequest.html#adbb006a8e7cc0637d01959c8ba985c0c">value</a>, body, 4);
  135. <a name="l00094"></a>00094 result.<a class="code" href="structStunAtrChangeRequest.html#adbb006a8e7cc0637d01959c8ba985c0c">value</a> = ntohl(result.<a class="code" href="structStunAtrChangeRequest.html#adbb006a8e7cc0637d01959c8ba985c0c">value</a>);
  136. <a name="l00095"></a>00095 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  137. <a name="l00096"></a>00096 }
  138. <a name="l00097"></a>00097 }
  139. <a name="l00098"></a>00098
  140. <a name="l00099"></a>00099 <span class="keyword">static</span> <span class="keywordtype">bool</span>
  141. <a name="l00100"></a><a class="code" href="stun_8cc.html#afdf35a5dc5c3d6b7109c290308e32887">00100</a> <a class="code" href="stun_8cc.html#afdf35a5dc5c3d6b7109c290308e32887">stunParseAtrError</a>( <span class="keywordtype">char</span>* body, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hdrLen, <a class="code" href="structStunAtrError.html">StunAtrError</a>&amp; result )
  142. <a name="l00101"></a>00101 {
  143. <a name="l00102"></a>00102 <span class="keywordflow">if</span> ( hdrLen &gt;= <span class="keyword">sizeof</span>(result) )
  144. <a name="l00103"></a>00103 {
  145. <a name="l00104"></a>00104 clog &lt;&lt; <span class="stringliteral">&quot;head on Error too large&quot;</span> &lt;&lt; endl;
  146. <a name="l00105"></a>00105 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  147. <a name="l00106"></a>00106 }
  148. <a name="l00107"></a>00107 <span class="keywordflow">else</span>
  149. <a name="l00108"></a>00108 {
  150. <a name="l00109"></a>00109 memcpy(&amp;result.<a class="code" href="structStunAtrError.html#ae2d32994bcf7387452902e139f2b652f">pad</a>, body, 2); body+=2;
  151. <a name="l00110"></a>00110 result.<a class="code" href="structStunAtrError.html#ae2d32994bcf7387452902e139f2b652f">pad</a> = ntohs(result.<a class="code" href="structStunAtrError.html#ae2d32994bcf7387452902e139f2b652f">pad</a>);
  152. <a name="l00111"></a>00111 result.<a class="code" href="structStunAtrError.html#aef65890aa74d342d326ea74000064be5">errorClass</a> = *body++;
  153. <a name="l00112"></a>00112 result.<a class="code" href="structStunAtrError.html#abdee224c138f23ad06aeb97ed9d6e0fe">number</a> = *body++;
  154. <a name="l00113"></a>00113
  155. <a name="l00114"></a>00114 result.<a class="code" href="structStunAtrError.html#adcac61cdac715d64ebaa78e6847e1108">sizeReason</a> = hdrLen - 4;
  156. <a name="l00115"></a>00115 memcpy(&amp;result.<a class="code" href="structStunAtrError.html#a3bb11d3802d0bfe65a153d8a6bccd472">reason</a>, body, result.<a class="code" href="structStunAtrError.html#adcac61cdac715d64ebaa78e6847e1108">sizeReason</a>);
  157. <a name="l00116"></a>00116 result.<a class="code" href="structStunAtrError.html#a3bb11d3802d0bfe65a153d8a6bccd472">reason</a>[result.<a class="code" href="structStunAtrError.html#adcac61cdac715d64ebaa78e6847e1108">sizeReason</a>] = 0;
  158. <a name="l00117"></a>00117 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  159. <a name="l00118"></a>00118 }
  160. <a name="l00119"></a>00119 }
  161. <a name="l00120"></a>00120
  162. <a name="l00121"></a>00121 <span class="keyword">static</span> <span class="keywordtype">bool</span>
  163. <a name="l00122"></a><a class="code" href="stun_8cc.html#a739d3cc61a2a1609b96d535ae74a6221">00122</a> <a class="code" href="stun_8cc.html#a739d3cc61a2a1609b96d535ae74a6221">stunParseAtrUnknown</a>( <span class="keywordtype">char</span>* body, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hdrLen, <a class="code" href="structStunAtrUnknown.html">StunAtrUnknown</a>&amp; result )
  164. <a name="l00123"></a>00123 {
  165. <a name="l00124"></a>00124 <span class="keywordflow">if</span> ( hdrLen &gt;= <span class="keyword">sizeof</span>(result) )
  166. <a name="l00125"></a>00125 {
  167. <a name="l00126"></a>00126 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  168. <a name="l00127"></a>00127 }
  169. <a name="l00128"></a>00128 <span class="keywordflow">else</span>
  170. <a name="l00129"></a>00129 {
  171. <a name="l00130"></a>00130 <span class="keywordflow">if</span> (hdrLen % 4 != 0) <span class="keywordflow">return</span> <span class="keyword">false</span>;
  172. <a name="l00131"></a>00131 result.<a class="code" href="structStunAtrUnknown.html#a39f2cca60b76d9476cd2a61a349e72d6">numAttributes</a> = hdrLen / 4;
  173. <a name="l00132"></a>00132 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;result.<a class="code" href="structStunAtrUnknown.html#a39f2cca60b76d9476cd2a61a349e72d6">numAttributes</a>; i++)
  174. <a name="l00133"></a>00133 {
  175. <a name="l00134"></a>00134 memcpy(&amp;result.<a class="code" href="structStunAtrUnknown.html#a2a9ac6afb38e1f951bc7cb8ec0b2a888">attrType</a>[i], body, 2); body+=2;
  176. <a name="l00135"></a>00135 result.<a class="code" href="structStunAtrUnknown.html#a2a9ac6afb38e1f951bc7cb8ec0b2a888">attrType</a>[i] = ntohs(result.<a class="code" href="structStunAtrUnknown.html#a2a9ac6afb38e1f951bc7cb8ec0b2a888">attrType</a>[i]);
  177. <a name="l00136"></a>00136 }
  178. <a name="l00137"></a>00137 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  179. <a name="l00138"></a>00138 }
  180. <a name="l00139"></a>00139 }
  181. <a name="l00140"></a>00140
  182. <a name="l00141"></a>00141
  183. <a name="l00142"></a>00142 <span class="keyword">static</span> <span class="keywordtype">bool</span>
  184. <a name="l00143"></a><a class="code" href="stun_8cc.html#a2c161a60854ecb6fe959dff3fe422310">00143</a> <a class="code" href="stun_8cc.html#a2c161a60854ecb6fe959dff3fe422310">stunParseAtrString</a>( <span class="keywordtype">char</span>* body, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hdrLen, <a class="code" href="structStunAtrString.html">StunAtrString</a>&amp; result )
  185. <a name="l00144"></a>00144 {
  186. <a name="l00145"></a>00145 <span class="keywordflow">if</span> ( hdrLen &gt;= <a class="code" href="stun_8h.html#ac4d86797b42c1696b7acbd715481c0b1">STUN_MAX_STRING</a> )
  187. <a name="l00146"></a>00146 {
  188. <a name="l00147"></a>00147 clog &lt;&lt; <span class="stringliteral">&quot;String is too large&quot;</span> &lt;&lt; endl;
  189. <a name="l00148"></a>00148 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  190. <a name="l00149"></a>00149 }
  191. <a name="l00150"></a>00150 <span class="keywordflow">else</span>
  192. <a name="l00151"></a>00151 {
  193. <a name="l00152"></a>00152 <span class="keywordflow">if</span> (hdrLen % 4 != 0)
  194. <a name="l00153"></a>00153 {
  195. <a name="l00154"></a>00154 clog &lt;&lt; <span class="stringliteral">&quot;Bad length string &quot;</span> &lt;&lt; hdrLen &lt;&lt; endl;
  196. <a name="l00155"></a>00155 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  197. <a name="l00156"></a>00156 }
  198. <a name="l00157"></a>00157
  199. <a name="l00158"></a>00158 result.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = hdrLen;
  200. <a name="l00159"></a>00159 memcpy(&amp;result.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>, body, hdrLen);
  201. <a name="l00160"></a>00160 result.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>[hdrLen] = 0;
  202. <a name="l00161"></a>00161 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  203. <a name="l00162"></a>00162 }
  204. <a name="l00163"></a>00163 }
  205. <a name="l00164"></a>00164
  206. <a name="l00165"></a>00165
  207. <a name="l00166"></a>00166 <span class="keyword">static</span> <span class="keywordtype">bool</span>
  208. <a name="l00167"></a><a class="code" href="stun_8cc.html#a4e7e4c29e1424831e001c9ffa9b74b53">00167</a> <a class="code" href="stun_8cc.html#a4e7e4c29e1424831e001c9ffa9b74b53">stunParseAtrIntegrity</a>( <span class="keywordtype">char</span>* body, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hdrLen, <a class="code" href="structStunAtrIntegrity.html">StunAtrIntegrity</a>&amp; result )
  209. <a name="l00168"></a>00168 {
  210. <a name="l00169"></a>00169 <span class="keywordflow">if</span> ( hdrLen != 20)
  211. <a name="l00170"></a>00170 {
  212. <a name="l00171"></a>00171 clog &lt;&lt; <span class="stringliteral">&quot;MessageIntegrity must be 20 bytes&quot;</span> &lt;&lt; endl;
  213. <a name="l00172"></a>00172 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  214. <a name="l00173"></a>00173 }
  215. <a name="l00174"></a>00174 <span class="keywordflow">else</span>
  216. <a name="l00175"></a>00175 {
  217. <a name="l00176"></a>00176 memcpy(&amp;result.<a class="code" href="structStunAtrIntegrity.html#a584fa6b758583be87dfad5c10da9fd44">hash</a>, body, hdrLen);
  218. <a name="l00177"></a>00177 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  219. <a name="l00178"></a>00178 }
  220. <a name="l00179"></a>00179 }
  221. <a name="l00180"></a>00180
  222. <a name="l00181"></a>00181
  223. <a name="l00182"></a>00182 <span class="keywordtype">bool</span>
  224. <a name="l00183"></a><a class="code" href="stun_8h.html#a44a8d7b644cb0faa4107db47b2e06471">00183</a> <a class="code" href="stun_8cc.html#af81c30137f26a4cb158b48aa21f4f681">stunParseMessage</a>( <span class="keywordtype">char</span>* buf, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufLen, <a class="code" href="structStunMessage.html">StunMessage</a>&amp; msg, <span class="keywordtype">bool</span> verbose)
  225. <a name="l00184"></a>00184 {
  226. <a name="l00185"></a>00185 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Received stun message: &quot;</span> &lt;&lt; bufLen &lt;&lt; <span class="stringliteral">&quot; bytes&quot;</span> &lt;&lt; endl;
  227. <a name="l00186"></a>00186 memset(&amp;msg, 0, <span class="keyword">sizeof</span>(msg));
  228. <a name="l00187"></a>00187
  229. <a name="l00188"></a>00188 <span class="keywordflow">if</span> (<span class="keyword">sizeof</span>(<a class="code" href="structStunMsgHdr.html">StunMsgHdr</a>) &gt; bufLen)
  230. <a name="l00189"></a>00189 {
  231. <a name="l00190"></a>00190 clog &lt;&lt; <span class="stringliteral">&quot;Bad message&quot;</span> &lt;&lt; endl;
  232. <a name="l00191"></a>00191 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  233. <a name="l00192"></a>00192 }
  234. <a name="l00193"></a>00193
  235. <a name="l00194"></a>00194 memcpy(&amp;msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>, buf, <span class="keyword">sizeof</span>(<a class="code" href="structStunMsgHdr.html">StunMsgHdr</a>));
  236. <a name="l00195"></a>00195 msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a7cc96e6cbb87582e56f298eb891c43e2">msgType</a> = ntohs(msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a7cc96e6cbb87582e56f298eb891c43e2">msgType</a>);
  237. <a name="l00196"></a>00196 msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a619e2158af7fd1b3de12b88899a7c5dd">msgLength</a> = ntohs(msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a619e2158af7fd1b3de12b88899a7c5dd">msgLength</a>);
  238. <a name="l00197"></a>00197
  239. <a name="l00198"></a>00198 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a619e2158af7fd1b3de12b88899a7c5dd">msgLength</a> + <span class="keyword">sizeof</span>(<a class="code" href="structStunMsgHdr.html">StunMsgHdr</a>) != bufLen)
  240. <a name="l00199"></a>00199 {
  241. <a name="l00200"></a>00200 clog &lt;&lt; <span class="stringliteral">&quot;Message header length doesn&#39;t match message size: &quot;</span>
  242. <a name="l00201"></a>00201 &lt;&lt; msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a619e2158af7fd1b3de12b88899a7c5dd">msgLength</a> &lt;&lt; <span class="stringliteral">&quot; - &quot;</span> &lt;&lt; bufLen &lt;&lt; endl;
  243. <a name="l00202"></a>00202 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  244. <a name="l00203"></a>00203 }
  245. <a name="l00204"></a>00204
  246. <a name="l00205"></a>00205 <span class="keywordtype">char</span>* body = buf + <span class="keyword">sizeof</span>(<a class="code" href="structStunMsgHdr.html">StunMsgHdr</a>);
  247. <a name="l00206"></a>00206 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size = msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a619e2158af7fd1b3de12b88899a7c5dd">msgLength</a>;
  248. <a name="l00207"></a>00207
  249. <a name="l00208"></a>00208 <span class="comment">//clog &lt;&lt; &quot;bytes after header = &quot; &lt;&lt; size &lt;&lt; endl;</span>
  250. <a name="l00209"></a>00209
  251. <a name="l00210"></a>00210 <span class="keywordflow">while</span> ( size &gt; 0 )
  252. <a name="l00211"></a>00211 {
  253. <a name="l00212"></a>00212 <span class="comment">// !jf! should check that there are enough bytes left in the buffer</span>
  254. <a name="l00213"></a>00213
  255. <a name="l00214"></a>00214 <a class="code" href="structStunAtrHdr.html">StunAtrHdr</a>* attr = <span class="keyword">reinterpret_cast&lt;</span><a class="code" href="structStunAtrHdr.html">StunAtrHdr</a>*<span class="keyword">&gt;</span>(body);
  256. <a name="l00215"></a>00215
  257. <a name="l00216"></a>00216 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> attrLen = ntohs(attr-&gt;<a class="code" href="structStunAtrHdr.html#a979c6bd5a4c93128cc316bef605191f5">length</a>);
  258. <a name="l00217"></a>00217 <span class="keywordtype">int</span> atrType = ntohs(attr-&gt;<a class="code" href="structStunAtrHdr.html#a303835ac7916984e097587ad499b568e">type</a>);
  259. <a name="l00218"></a>00218
  260. <a name="l00219"></a>00219 <span class="comment">//if (verbose) clog &lt;&lt; &quot;Found attribute type=&quot; &lt;&lt; AttrNames[atrType] &lt;&lt; &quot; length=&quot; &lt;&lt; attrLen &lt;&lt; endl;</span>
  261. <a name="l00220"></a>00220 <span class="keywordflow">if</span> ( attrLen+4 &gt; size )
  262. <a name="l00221"></a>00221 {
  263. <a name="l00222"></a>00222 clog &lt;&lt; <span class="stringliteral">&quot;claims attribute is larger than size of message &quot;</span>
  264. <a name="l00223"></a>00223 &lt;&lt;<span class="stringliteral">&quot;(attribute type=&quot;</span>&lt;&lt;atrType&lt;&lt;<span class="stringliteral">&quot;)&quot;</span>&lt;&lt; endl;
  265. <a name="l00224"></a>00224 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  266. <a name="l00225"></a>00225 }
  267. <a name="l00226"></a>00226
  268. <a name="l00227"></a>00227 body += 4; <span class="comment">// skip the length and type in attribute header</span>
  269. <a name="l00228"></a>00228 size -= 4;
  270. <a name="l00229"></a>00229
  271. <a name="l00230"></a>00230 <span class="keywordflow">switch</span> ( atrType )
  272. <a name="l00231"></a>00231 {
  273. <a name="l00232"></a>00232 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a2479ede510923cc5e8a0c2da5558acc3">MappedAddress</a>:
  274. <a name="l00233"></a>00233 msg.<a class="code" href="structStunMessage.html#a605889708c9696c60f1284b3c486ca18">hasMappedAddress</a> = <span class="keyword">true</span>;
  275. <a name="l00234"></a>00234 <span class="keywordflow">if</span> ( <a class="code" href="stun_8cc.html#a16624b9ba3d6b2f1c821571fa467c544">stunParseAtrAddress</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a> )== <span class="keyword">false</span> )
  276. <a name="l00235"></a>00235 {
  277. <a name="l00236"></a>00236 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing MappedAddress&quot;</span> &lt;&lt; endl;
  278. <a name="l00237"></a>00237 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  279. <a name="l00238"></a>00238 }
  280. <a name="l00239"></a>00239 <span class="keywordflow">else</span>
  281. <a name="l00240"></a>00240 {
  282. <a name="l00241"></a>00241 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;MappedAddress = &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  283. <a name="l00242"></a>00242 }
  284. <a name="l00243"></a>00243
  285. <a name="l00244"></a>00244 <span class="keywordflow">break</span>;
  286. <a name="l00245"></a>00245
  287. <a name="l00246"></a>00246 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a91ab7c5c3d71a86a77679d0d2786fae8">ResponseAddress</a>:
  288. <a name="l00247"></a>00247 msg.<a class="code" href="structStunMessage.html#ab7926e7d5ceb2d9c3337439ec89ec87d">hasResponseAddress</a> = <span class="keyword">true</span>;
  289. <a name="l00248"></a>00248 <span class="keywordflow">if</span> ( <a class="code" href="stun_8cc.html#a16624b9ba3d6b2f1c821571fa467c544">stunParseAtrAddress</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#adb06537683f153438687995e8d1783c6">responseAddress</a> )== <span class="keyword">false</span> )
  290. <a name="l00249"></a>00249 {
  291. <a name="l00250"></a>00250 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing ResponseAddress&quot;</span> &lt;&lt; endl;
  292. <a name="l00251"></a>00251 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  293. <a name="l00252"></a>00252 }
  294. <a name="l00253"></a>00253 <span class="keywordflow">else</span>
  295. <a name="l00254"></a>00254 {
  296. <a name="l00255"></a>00255 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;ResponseAddress = &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#adb06537683f153438687995e8d1783c6">responseAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  297. <a name="l00256"></a>00256 }
  298. <a name="l00257"></a>00257 <span class="keywordflow">break</span>;
  299. <a name="l00258"></a>00258
  300. <a name="l00259"></a>00259 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a6183c3f6e20ddc5bf9acd531d37c4b08">ChangeRequest</a>:
  301. <a name="l00260"></a>00260 msg.<a class="code" href="structStunMessage.html#a0fe2488f060fe65507c9a58bbb66899f">hasChangeRequest</a> = <span class="keyword">true</span>;
  302. <a name="l00261"></a>00261 <span class="keywordflow">if</span> (<a class="code" href="stun_8cc.html#af2f9f10bd394cb42037f1997266b978a">stunParseAtrChangeRequest</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#a65e5de40376764ea12d96f5b5ebe2c29">changeRequest</a>) == <span class="keyword">false</span>)
  303. <a name="l00262"></a>00262 {
  304. <a name="l00263"></a>00263 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing ChangeRequest&quot;</span> &lt;&lt; endl;
  305. <a name="l00264"></a>00264 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  306. <a name="l00265"></a>00265 }
  307. <a name="l00266"></a>00266 <span class="keywordflow">else</span>
  308. <a name="l00267"></a>00267 {
  309. <a name="l00268"></a>00268 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;ChangeRequest = &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a65e5de40376764ea12d96f5b5ebe2c29">changeRequest</a>.<a class="code" href="structStunAtrChangeRequest.html#adbb006a8e7cc0637d01959c8ba985c0c">value</a> &lt;&lt; endl;
  310. <a name="l00269"></a>00269 }
  311. <a name="l00270"></a>00270 <span class="keywordflow">break</span>;
  312. <a name="l00271"></a>00271
  313. <a name="l00272"></a>00272 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#ab2b81c4c09625b39c5a1d57ee2b460a4">SourceAddress</a>:
  314. <a name="l00273"></a>00273 msg.<a class="code" href="structStunMessage.html#a5feb7431931118218d78642c4a7c83db">hasSourceAddress</a> = <span class="keyword">true</span>;
  315. <a name="l00274"></a>00274 <span class="keywordflow">if</span> ( <a class="code" href="stun_8cc.html#a16624b9ba3d6b2f1c821571fa467c544">stunParseAtrAddress</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#ab7203fc2e5a8b1248c9e14ce6e6ec313">sourceAddress</a> )== <span class="keyword">false</span> )
  316. <a name="l00275"></a>00275 {
  317. <a name="l00276"></a>00276 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing SourceAddress&quot;</span> &lt;&lt; endl;
  318. <a name="l00277"></a>00277 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  319. <a name="l00278"></a>00278 }
  320. <a name="l00279"></a>00279 <span class="keywordflow">else</span>
  321. <a name="l00280"></a>00280 {
  322. <a name="l00281"></a>00281 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;SourceAddress = &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#ab7203fc2e5a8b1248c9e14ce6e6ec313">sourceAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  323. <a name="l00282"></a>00282 }
  324. <a name="l00283"></a>00283 <span class="keywordflow">break</span>;
  325. <a name="l00284"></a>00284
  326. <a name="l00285"></a>00285 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a930a1eeda1f0620132bfdea315e711f7">ChangedAddress</a>:
  327. <a name="l00286"></a>00286 msg.<a class="code" href="structStunMessage.html#a4d3036e012cce04e6be670898f4e24eb">hasChangedAddress</a> = <span class="keyword">true</span>;
  328. <a name="l00287"></a>00287 <span class="keywordflow">if</span> ( <a class="code" href="stun_8cc.html#a16624b9ba3d6b2f1c821571fa467c544">stunParseAtrAddress</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a> )== <span class="keyword">false</span> )
  329. <a name="l00288"></a>00288 {
  330. <a name="l00289"></a>00289 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing ChangedAddress&quot;</span> &lt;&lt; endl;
  331. <a name="l00290"></a>00290 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  332. <a name="l00291"></a>00291 }
  333. <a name="l00292"></a>00292 <span class="keywordflow">else</span>
  334. <a name="l00293"></a>00293 {
  335. <a name="l00294"></a>00294 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;ChangedAddress = &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  336. <a name="l00295"></a>00295 }
  337. <a name="l00296"></a>00296 <span class="keywordflow">break</span>;
  338. <a name="l00297"></a>00297
  339. <a name="l00298"></a>00298 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a0845232ed07b469d1262348e1618b962">Username</a>:
  340. <a name="l00299"></a>00299 msg.<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a> = <span class="keyword">true</span>;
  341. <a name="l00300"></a>00300 <span class="keywordflow">if</span> (<a class="code" href="stun_8cc.html#a2c161a60854ecb6fe959dff3fe422310">stunParseAtrString</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>) == <span class="keyword">false</span>)
  342. <a name="l00301"></a>00301 {
  343. <a name="l00302"></a>00302 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing Username&quot;</span> &lt;&lt; endl;
  344. <a name="l00303"></a>00303 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  345. <a name="l00304"></a>00304 }
  346. <a name="l00305"></a>00305 <span class="keywordflow">else</span>
  347. <a name="l00306"></a>00306 {
  348. <a name="l00307"></a>00307 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Username = &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a> &lt;&lt; endl;
  349. <a name="l00308"></a>00308 }
  350. <a name="l00309"></a>00309
  351. <a name="l00310"></a>00310 <span class="keywordflow">break</span>;
  352. <a name="l00311"></a>00311
  353. <a name="l00312"></a>00312 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a1741d36983135a1f2eefa1b1473956a2">Password</a>:
  354. <a name="l00313"></a>00313 msg.<a class="code" href="structStunMessage.html#ac09b6e5f081db9f5ec2c1a4532676366">hasPassword</a> = <span class="keyword">true</span>;
  355. <a name="l00314"></a>00314 <span class="keywordflow">if</span> (<a class="code" href="stun_8cc.html#a2c161a60854ecb6fe959dff3fe422310">stunParseAtrString</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#aa15cab0d5b2a9a044895e81d527c1b68">password</a>) == <span class="keyword">false</span>)
  356. <a name="l00315"></a>00315 {
  357. <a name="l00316"></a>00316 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing Password&quot;</span> &lt;&lt; endl;
  358. <a name="l00317"></a>00317 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  359. <a name="l00318"></a>00318 }
  360. <a name="l00319"></a>00319 <span class="keywordflow">else</span>
  361. <a name="l00320"></a>00320 {
  362. <a name="l00321"></a>00321 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Password = &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#aa15cab0d5b2a9a044895e81d527c1b68">password</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a> &lt;&lt; endl;
  363. <a name="l00322"></a>00322 }
  364. <a name="l00323"></a>00323 <span class="keywordflow">break</span>;
  365. <a name="l00324"></a>00324
  366. <a name="l00325"></a>00325 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a0371caac677f72f19ea073dd152197f0">MessageIntegrity</a>:
  367. <a name="l00326"></a>00326 msg.<a class="code" href="structStunMessage.html#a2a47413181e29d0de7063d4bfa6609ff">hasMessageIntegrity</a> = <span class="keyword">true</span>;
  368. <a name="l00327"></a>00327 <span class="keywordflow">if</span> (<a class="code" href="stun_8cc.html#a4e7e4c29e1424831e001c9ffa9b74b53">stunParseAtrIntegrity</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#a1b2c965443dc78688973c049713072d0">messageIntegrity</a>) == <span class="keyword">false</span>)
  369. <a name="l00328"></a>00328 {
  370. <a name="l00329"></a>00329 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing MessageIntegrity&quot;</span> &lt;&lt; endl;
  371. <a name="l00330"></a>00330 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  372. <a name="l00331"></a>00331 }
  373. <a name="l00332"></a>00332 <span class="keywordflow">else</span>
  374. <a name="l00333"></a>00333 {
  375. <a name="l00334"></a>00334 <span class="comment">//if (verbose) clog &lt;&lt; &quot;MessageIntegrity = &quot; &lt;&lt; msg.messageIntegrity.hash &lt;&lt; endl;</span>
  376. <a name="l00335"></a>00335 }
  377. <a name="l00336"></a>00336
  378. <a name="l00337"></a>00337 <span class="comment">// read the current HMAC</span>
  379. <a name="l00338"></a>00338 <span class="comment">// look up the password given the user of given the transaction id</span>
  380. <a name="l00339"></a>00339 <span class="comment">// compute the HMAC on the buffer</span>
  381. <a name="l00340"></a>00340 <span class="comment">// decide if they match or not</span>
  382. <a name="l00341"></a>00341 <span class="keywordflow">break</span>;
  383. <a name="l00342"></a>00342
  384. <a name="l00343"></a>00343 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a750ec1534b2aebc05c90b9e6fe9532c7">ErrorCode</a>:
  385. <a name="l00344"></a>00344 msg.<a class="code" href="structStunMessage.html#afe66349979726d392fa4f0c9c84ebedf">hasErrorCode</a> = <span class="keyword">true</span>;
  386. <a name="l00345"></a>00345 <span class="keywordflow">if</span> (<a class="code" href="stun_8cc.html#afdf35a5dc5c3d6b7109c290308e32887">stunParseAtrError</a>(body, attrLen, msg.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>) == <span class="keyword">false</span>)
  387. <a name="l00346"></a>00346 {
  388. <a name="l00347"></a>00347 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing ErrorCode&quot;</span> &lt;&lt; endl;
  389. <a name="l00348"></a>00348 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  390. <a name="l00349"></a>00349 }
  391. <a name="l00350"></a>00350 <span class="keywordflow">else</span>
  392. <a name="l00351"></a>00351 {
  393. <a name="l00352"></a>00352 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;ErrorCode = &quot;</span> &lt;&lt; int(msg.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#aef65890aa74d342d326ea74000064be5">errorClass</a>)
  394. <a name="l00353"></a>00353 &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; int(msg.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#abdee224c138f23ad06aeb97ed9d6e0fe">number</a>)
  395. <a name="l00354"></a>00354 &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#a3bb11d3802d0bfe65a153d8a6bccd472">reason</a> &lt;&lt; endl;
  396. <a name="l00355"></a>00355 }
  397. <a name="l00356"></a>00356
  398. <a name="l00357"></a>00357 <span class="keywordflow">break</span>;
  399. <a name="l00358"></a>00358
  400. <a name="l00359"></a>00359 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#ac5d0f4ecd6bc741747a3ff79b6e5a96c">UnknownAttribute</a>:
  401. <a name="l00360"></a>00360 msg.<a class="code" href="structStunMessage.html#a29507d464e9900ced2ce92c75bb158a4">hasUnknownAttributes</a> = <span class="keyword">true</span>;
  402. <a name="l00361"></a>00361 <span class="keywordflow">if</span> (<a class="code" href="stun_8cc.html#a739d3cc61a2a1609b96d535ae74a6221">stunParseAtrUnknown</a>(body, attrLen, msg.<a class="code" href="structStunMessage.html#a40311e31449afb24c3e40db8c8fc6911">unknownAttributes</a>) == <span class="keyword">false</span>)
  403. <a name="l00362"></a>00362 {
  404. <a name="l00363"></a>00363 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing UnknownAttribute&quot;</span> &lt;&lt; endl;
  405. <a name="l00364"></a>00364 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  406. <a name="l00365"></a>00365 }
  407. <a name="l00366"></a>00366 <span class="keywordflow">break</span>;
  408. <a name="l00367"></a>00367
  409. <a name="l00368"></a>00368 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a11d1c4b717d340b20582ac7dead1cd49">ReflectedFrom</a>:
  410. <a name="l00369"></a>00369 msg.<a class="code" href="structStunMessage.html#ab88124a21ca8562a44a696ed4b39fa4f">hasReflectedFrom</a> = <span class="keyword">true</span>;
  411. <a name="l00370"></a>00370 <span class="keywordflow">if</span> ( <a class="code" href="stun_8cc.html#a16624b9ba3d6b2f1c821571fa467c544">stunParseAtrAddress</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#ab80a59189b1b938698339f788417f32b">reflectedFrom</a> ) == <span class="keyword">false</span> )
  412. <a name="l00371"></a>00371 {
  413. <a name="l00372"></a>00372 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing ReflectedFrom&quot;</span> &lt;&lt; endl;
  414. <a name="l00373"></a>00373 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  415. <a name="l00374"></a>00374 }
  416. <a name="l00375"></a>00375 <span class="keywordflow">break</span>;
  417. <a name="l00376"></a>00376
  418. <a name="l00377"></a>00377 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#afd742381f8d8131e21dac399b8a26c3a">XorMappedAddress</a>:
  419. <a name="l00378"></a>00378 msg.<a class="code" href="structStunMessage.html#a8e149870681eebee0a55b0f630c89471">hasXorMappedAddress</a> = <span class="keyword">true</span>;
  420. <a name="l00379"></a>00379 <span class="keywordflow">if</span> ( <a class="code" href="stun_8cc.html#a16624b9ba3d6b2f1c821571fa467c544">stunParseAtrAddress</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#a2ef5db0ff5bd395800262b8b9d49b6bb">xorMappedAddress</a> ) == <span class="keyword">false</span> )
  421. <a name="l00380"></a>00380 {
  422. <a name="l00381"></a>00381 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing XorMappedAddress&quot;</span> &lt;&lt; endl;
  423. <a name="l00382"></a>00382 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  424. <a name="l00383"></a>00383 }
  425. <a name="l00384"></a>00384 <span class="keywordflow">else</span>
  426. <a name="l00385"></a>00385 {
  427. <a name="l00386"></a>00386 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;XorMappedAddress = &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  428. <a name="l00387"></a>00387 }
  429. <a name="l00388"></a>00388 <span class="keywordflow">break</span>;
  430. <a name="l00389"></a>00389
  431. <a name="l00390"></a>00390 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a615f23f67cf9c716c91fa445e272da3c">XorOnly</a>:
  432. <a name="l00391"></a>00391 msg.<a class="code" href="structStunMessage.html#ad2d3b64955956272841f656fe354aa18">xorOnly</a> = <span class="keyword">true</span>;
  433. <a name="l00392"></a>00392 <span class="keywordflow">if</span> (verbose)
  434. <a name="l00393"></a>00393 {
  435. <a name="l00394"></a>00394 clog &lt;&lt; <span class="stringliteral">&quot;xorOnly = true&quot;</span> &lt;&lt; endl;
  436. <a name="l00395"></a>00395 }
  437. <a name="l00396"></a>00396 <span class="keywordflow">break</span>;
  438. <a name="l00397"></a>00397
  439. <a name="l00398"></a>00398 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a53f47b517798fa3e1b64313d04168ea2">ServerName</a>:
  440. <a name="l00399"></a>00399 msg.<a class="code" href="structStunMessage.html#afcf544b5b0663edad215525b8bcd67df">hasServerName</a> = <span class="keyword">true</span>;
  441. <a name="l00400"></a>00400 <span class="keywordflow">if</span> (<a class="code" href="stun_8cc.html#a2c161a60854ecb6fe959dff3fe422310">stunParseAtrString</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#a1998c160a79020e146957c14ae1add11">serverName</a>) == <span class="keyword">false</span>)
  442. <a name="l00401"></a>00401 {
  443. <a name="l00402"></a>00402 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing ServerName&quot;</span> &lt;&lt; endl;
  444. <a name="l00403"></a>00403 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  445. <a name="l00404"></a>00404 }
  446. <a name="l00405"></a>00405 <span class="keywordflow">else</span>
  447. <a name="l00406"></a>00406 {
  448. <a name="l00407"></a>00407 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;ServerName = &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a1998c160a79020e146957c14ae1add11">serverName</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a> &lt;&lt; endl;
  449. <a name="l00408"></a>00408 }
  450. <a name="l00409"></a>00409 <span class="keywordflow">break</span>;
  451. <a name="l00410"></a>00410
  452. <a name="l00411"></a>00411 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a4d77a59e0dc2c3c06deb3730baa78f80">SecondaryAddress</a>:
  453. <a name="l00412"></a>00412 msg.<a class="code" href="structStunMessage.html#ace894f36510487ec8151609513974eba">hasSecondaryAddress</a> = <span class="keyword">true</span>;
  454. <a name="l00413"></a>00413 <span class="keywordflow">if</span> ( <a class="code" href="stun_8cc.html#a16624b9ba3d6b2f1c821571fa467c544">stunParseAtrAddress</a>( body, attrLen, msg.<a class="code" href="structStunMessage.html#a8fc16bd394228aa6fa6e1f5f98c356c1">secondaryAddress</a> ) == <span class="keyword">false</span> )
  455. <a name="l00414"></a>00414 {
  456. <a name="l00415"></a>00415 clog &lt;&lt; <span class="stringliteral">&quot;problem parsing secondaryAddress&quot;</span> &lt;&lt; endl;
  457. <a name="l00416"></a>00416 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  458. <a name="l00417"></a>00417 }
  459. <a name="l00418"></a>00418 <span class="keywordflow">else</span>
  460. <a name="l00419"></a>00419 {
  461. <a name="l00420"></a>00420 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;SecondaryAddress = &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a8fc16bd394228aa6fa6e1f5f98c356c1">secondaryAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  462. <a name="l00421"></a>00421 }
  463. <a name="l00422"></a>00422 <span class="keywordflow">break</span>;
  464. <a name="l00423"></a>00423
  465. <a name="l00424"></a>00424 <span class="keywordflow">default</span>:
  466. <a name="l00425"></a>00425 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Unknown attribute: &quot;</span> &lt;&lt; atrType &lt;&lt; endl;
  467. <a name="l00426"></a>00426 <span class="keywordflow">if</span> ( atrType &lt;= 0x7FFF )
  468. <a name="l00427"></a>00427 {
  469. <a name="l00428"></a>00428 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  470. <a name="l00429"></a>00429 }
  471. <a name="l00430"></a>00430 }
  472. <a name="l00431"></a>00431
  473. <a name="l00432"></a>00432 body += attrLen;
  474. <a name="l00433"></a>00433 size -= attrLen;
  475. <a name="l00434"></a>00434 }
  476. <a name="l00435"></a>00435
  477. <a name="l00436"></a>00436 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  478. <a name="l00437"></a>00437 }
  479. <a name="l00438"></a>00438
  480. <a name="l00439"></a>00439
  481. <a name="l00440"></a>00440 <span class="keyword">static</span> <span class="keywordtype">char</span>*
  482. <a name="l00441"></a><a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">00441</a> <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(<span class="keywordtype">char</span>* buf, <a class="code" href="stun_8h.html#accc14a7c843716dfed734f3a03e8d2b6">UInt16</a> data)
  483. <a name="l00442"></a>00442 {
  484. <a name="l00443"></a>00443 <a class="code" href="stun_8h.html#accc14a7c843716dfed734f3a03e8d2b6">UInt16</a> ndata = htons(data);
  485. <a name="l00444"></a>00444 memcpy(buf, reinterpret_cast&lt;void*&gt;(&amp;ndata), <span class="keyword">sizeof</span>(<a class="code" href="stun_8h.html#accc14a7c843716dfed734f3a03e8d2b6">UInt16</a>));
  486. <a name="l00445"></a>00445 <span class="keywordflow">return</span> buf + <span class="keyword">sizeof</span>(UInt16);
  487. <a name="l00446"></a>00446 }
  488. <a name="l00447"></a>00447
  489. <a name="l00448"></a>00448 <span class="keyword">static</span> <span class="keywordtype">char</span>*
  490. <a name="l00449"></a><a class="code" href="stun_8cc.html#a9d2486d010a2edf7a8ef74cc44f18de1">00449</a> <a class="code" href="stun_8cc.html#a9d2486d010a2edf7a8ef74cc44f18de1">encode32</a>(<span class="keywordtype">char</span>* buf, <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a> data)
  491. <a name="l00450"></a>00450 {
  492. <a name="l00451"></a>00451 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a> ndata = htonl(data);
  493. <a name="l00452"></a>00452 memcpy(buf, reinterpret_cast&lt;void*&gt;(&amp;ndata), <span class="keyword">sizeof</span>(<a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a>));
  494. <a name="l00453"></a>00453 <span class="keywordflow">return</span> buf + <span class="keyword">sizeof</span>(UInt32);
  495. <a name="l00454"></a>00454 }
  496. <a name="l00455"></a>00455
  497. <a name="l00456"></a>00456
  498. <a name="l00457"></a>00457 <span class="keyword">static</span> <span class="keywordtype">char</span>*
  499. <a name="l00458"></a><a class="code" href="stun_8cc.html#a32bf1e0d5e4833c497c540b4681dd808">00458</a> <a class="code" href="stun_8cc.html#a32bf1e0d5e4833c497c540b4681dd808">encode</a>(<span class="keywordtype">char</span>* buf, <span class="keyword">const</span> <span class="keywordtype">char</span>* data, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)
  500. <a name="l00459"></a>00459 {
  501. <a name="l00460"></a>00460 memcpy(buf, data, length);
  502. <a name="l00461"></a>00461 <span class="keywordflow">return</span> buf + length;
  503. <a name="l00462"></a>00462 }
  504. <a name="l00463"></a>00463
  505. <a name="l00464"></a>00464
  506. <a name="l00465"></a>00465 <span class="keyword">static</span> <span class="keywordtype">char</span>*
  507. <a name="l00466"></a><a class="code" href="stun_8cc.html#a85ef119873a4b4a76012d89f1d6c23fb">00466</a> <a class="code" href="stun_8cc.html#a85ef119873a4b4a76012d89f1d6c23fb">encodeAtrAddress4</a>(<span class="keywordtype">char</span>* ptr, <a class="code" href="stun_8h.html#accc14a7c843716dfed734f3a03e8d2b6">UInt16</a> type, <span class="keyword">const</span> <a class="code" href="structStunAtrAddress4.html">StunAtrAddress4</a>&amp; atr)
  508. <a name="l00467"></a>00467 {
  509. <a name="l00468"></a>00468 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, type);
  510. <a name="l00469"></a>00469 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, 8);
  511. <a name="l00470"></a>00470 *ptr++ = atr.<a class="code" href="structStunAtrAddress4.html#afe6ed0534166b0fd194b23b01ddad7b4">pad</a>;
  512. <a name="l00471"></a>00471 *ptr++ = <a class="code" href="stun_8h.html#ac2c7bc41e1b918c98583006fd7025bc5" title="define a structure to hold a stun address">IPv4Family</a>;
  513. <a name="l00472"></a>00472 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, atr.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>);
  514. <a name="l00473"></a>00473 ptr = <a class="code" href="stun_8cc.html#a9d2486d010a2edf7a8ef74cc44f18de1">encode32</a>(ptr, atr.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>);
  515. <a name="l00474"></a>00474
  516. <a name="l00475"></a>00475 <span class="keywordflow">return</span> ptr;
  517. <a name="l00476"></a>00476 }
  518. <a name="l00477"></a>00477
  519. <a name="l00478"></a>00478 <span class="keyword">static</span> <span class="keywordtype">char</span>*
  520. <a name="l00479"></a><a class="code" href="stun_8cc.html#a076612a210669e11d7c284f08b3a34b2">00479</a> <a class="code" href="stun_8cc.html#a076612a210669e11d7c284f08b3a34b2">encodeAtrChangeRequest</a>(<span class="keywordtype">char</span>* ptr, <span class="keyword">const</span> <a class="code" href="structStunAtrChangeRequest.html">StunAtrChangeRequest</a>&amp; atr)
  521. <a name="l00480"></a>00480 {
  522. <a name="l00481"></a>00481 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, <a class="code" href="stun_8h.html#a6183c3f6e20ddc5bf9acd531d37c4b08">ChangeRequest</a>);
  523. <a name="l00482"></a>00482 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, 4);
  524. <a name="l00483"></a>00483 ptr = <a class="code" href="stun_8cc.html#a9d2486d010a2edf7a8ef74cc44f18de1">encode32</a>(ptr, atr.<a class="code" href="structStunAtrChangeRequest.html#adbb006a8e7cc0637d01959c8ba985c0c">value</a>);
  525. <a name="l00484"></a>00484 <span class="keywordflow">return</span> ptr;
  526. <a name="l00485"></a>00485 }
  527. <a name="l00486"></a>00486
  528. <a name="l00487"></a>00487 <span class="keyword">static</span> <span class="keywordtype">char</span>*
  529. <a name="l00488"></a><a class="code" href="stun_8cc.html#a61a6cc411fa597374048437e28ddffdf">00488</a> <a class="code" href="stun_8cc.html#a61a6cc411fa597374048437e28ddffdf">encodeAtrError</a>(<span class="keywordtype">char</span>* ptr, <span class="keyword">const</span> <a class="code" href="structStunAtrError.html">StunAtrError</a>&amp; atr)
  530. <a name="l00489"></a>00489 {
  531. <a name="l00490"></a>00490 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, <a class="code" href="stun_8h.html#a750ec1534b2aebc05c90b9e6fe9532c7">ErrorCode</a>);
  532. <a name="l00491"></a>00491 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, 6 + atr.<a class="code" href="structStunAtrError.html#adcac61cdac715d64ebaa78e6847e1108">sizeReason</a>);
  533. <a name="l00492"></a>00492 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, atr.<a class="code" href="structStunAtrError.html#ae2d32994bcf7387452902e139f2b652f">pad</a>);
  534. <a name="l00493"></a>00493 *ptr++ = atr.<a class="code" href="structStunAtrError.html#aef65890aa74d342d326ea74000064be5">errorClass</a>;
  535. <a name="l00494"></a>00494 *ptr++ = atr.<a class="code" href="structStunAtrError.html#abdee224c138f23ad06aeb97ed9d6e0fe">number</a>;
  536. <a name="l00495"></a>00495 ptr = <a class="code" href="stun_8cc.html#a32bf1e0d5e4833c497c540b4681dd808">encode</a>(ptr, atr.<a class="code" href="structStunAtrError.html#a3bb11d3802d0bfe65a153d8a6bccd472">reason</a>, atr.<a class="code" href="structStunAtrError.html#adcac61cdac715d64ebaa78e6847e1108">sizeReason</a>);
  537. <a name="l00496"></a>00496 <span class="keywordflow">return</span> ptr;
  538. <a name="l00497"></a>00497 }
  539. <a name="l00498"></a>00498
  540. <a name="l00499"></a>00499
  541. <a name="l00500"></a>00500 <span class="keyword">static</span> <span class="keywordtype">char</span>*
  542. <a name="l00501"></a><a class="code" href="stun_8cc.html#a09523c814cf26f59012e800713447143">00501</a> <a class="code" href="stun_8cc.html#a09523c814cf26f59012e800713447143">encodeAtrUnknown</a>(<span class="keywordtype">char</span>* ptr, <span class="keyword">const</span> <a class="code" href="structStunAtrUnknown.html">StunAtrUnknown</a>&amp; atr)
  543. <a name="l00502"></a>00502 {
  544. <a name="l00503"></a>00503 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, <a class="code" href="stun_8h.html#ac5d0f4ecd6bc741747a3ff79b6e5a96c">UnknownAttribute</a>);
  545. <a name="l00504"></a>00504 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, 2+2*atr.<a class="code" href="structStunAtrUnknown.html#a39f2cca60b76d9476cd2a61a349e72d6">numAttributes</a>);
  546. <a name="l00505"></a>00505 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;atr.<a class="code" href="structStunAtrUnknown.html#a39f2cca60b76d9476cd2a61a349e72d6">numAttributes</a>; i++)
  547. <a name="l00506"></a>00506 {
  548. <a name="l00507"></a>00507 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, atr.<a class="code" href="structStunAtrUnknown.html#a2a9ac6afb38e1f951bc7cb8ec0b2a888">attrType</a>[i]);
  549. <a name="l00508"></a>00508 }
  550. <a name="l00509"></a>00509 <span class="keywordflow">return</span> ptr;
  551. <a name="l00510"></a>00510 }
  552. <a name="l00511"></a>00511
  553. <a name="l00512"></a>00512
  554. <a name="l00513"></a>00513 <span class="keyword">static</span> <span class="keywordtype">char</span>*
  555. <a name="l00514"></a><a class="code" href="stun_8cc.html#a3d149acb0bcd480596ea26b73b47d05f">00514</a> <a class="code" href="stun_8cc.html#a3d149acb0bcd480596ea26b73b47d05f">encodeXorOnly</a>(<span class="keywordtype">char</span>* ptr)
  556. <a name="l00515"></a>00515 {
  557. <a name="l00516"></a>00516 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, <a class="code" href="stun_8h.html#a615f23f67cf9c716c91fa445e272da3c">XorOnly</a> );
  558. <a name="l00517"></a>00517 <span class="keywordflow">return</span> ptr;
  559. <a name="l00518"></a>00518 }
  560. <a name="l00519"></a>00519
  561. <a name="l00520"></a>00520
  562. <a name="l00521"></a>00521 <span class="keyword">static</span> <span class="keywordtype">char</span>*
  563. <a name="l00522"></a><a class="code" href="stun_8cc.html#a765920e3cdcc343b0ed69a8fab4a1736">00522</a> <a class="code" href="stun_8cc.html#a765920e3cdcc343b0ed69a8fab4a1736">encodeAtrString</a>(<span class="keywordtype">char</span>* ptr, <a class="code" href="stun_8h.html#accc14a7c843716dfed734f3a03e8d2b6">UInt16</a> type, <span class="keyword">const</span> <a class="code" href="structStunAtrString.html">StunAtrString</a>&amp; atr)
  564. <a name="l00523"></a>00523 {
  565. <a name="l00524"></a>00524 assert(atr.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> % 4 == 0);
  566. <a name="l00525"></a>00525
  567. <a name="l00526"></a>00526 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, type);
  568. <a name="l00527"></a>00527 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, atr.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a>);
  569. <a name="l00528"></a>00528 ptr = <a class="code" href="stun_8cc.html#a32bf1e0d5e4833c497c540b4681dd808">encode</a>(ptr, atr.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>, atr.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a>);
  570. <a name="l00529"></a>00529 <span class="keywordflow">return</span> ptr;
  571. <a name="l00530"></a>00530 }
  572. <a name="l00531"></a>00531
  573. <a name="l00532"></a>00532
  574. <a name="l00533"></a>00533 <span class="keyword">static</span> <span class="keywordtype">char</span>*
  575. <a name="l00534"></a><a class="code" href="stun_8cc.html#ad58a0afd3626cc03abb1d92edbf6c4d3">00534</a> <a class="code" href="stun_8cc.html#ad58a0afd3626cc03abb1d92edbf6c4d3">encodeAtrIntegrity</a>(<span class="keywordtype">char</span>* ptr, <span class="keyword">const</span> <a class="code" href="structStunAtrIntegrity.html">StunAtrIntegrity</a>&amp; atr)
  576. <a name="l00535"></a>00535 {
  577. <a name="l00536"></a>00536 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, <a class="code" href="stun_8h.html#a0371caac677f72f19ea073dd152197f0">MessageIntegrity</a>);
  578. <a name="l00537"></a>00537 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, 20);
  579. <a name="l00538"></a>00538 ptr = <a class="code" href="stun_8cc.html#a32bf1e0d5e4833c497c540b4681dd808">encode</a>(ptr, atr.<a class="code" href="structStunAtrIntegrity.html#a584fa6b758583be87dfad5c10da9fd44">hash</a>, <span class="keyword">sizeof</span>(atr.<a class="code" href="structStunAtrIntegrity.html#a584fa6b758583be87dfad5c10da9fd44">hash</a>));
  580. <a name="l00539"></a>00539 <span class="keywordflow">return</span> ptr;
  581. <a name="l00540"></a>00540 }
  582. <a name="l00541"></a>00541
  583. <a name="l00542"></a>00542
  584. <a name="l00543"></a>00543 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>
  585. <a name="l00544"></a><a class="code" href="stun_8h.html#a66298d81441f17d6fdd2aa4a4ced894d">00544</a> <a class="code" href="stun_8cc.html#a4f9e869279d41bbf1785d321b4360f61">stunEncodeMessage</a>( <span class="keyword">const</span> <a class="code" href="structStunMessage.html">StunMessage</a>&amp; msg,
  586. <a name="l00545"></a>00545 <span class="keywordtype">char</span>* buf,
  587. <a name="l00546"></a>00546 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufLen,
  588. <a name="l00547"></a>00547 <span class="keyword">const</span> <a class="code" href="structStunAtrString.html">StunAtrString</a>&amp; password,
  589. <a name="l00548"></a>00548 <span class="keywordtype">bool</span> verbose)
  590. <a name="l00549"></a>00549 {
  591. <a name="l00550"></a>00550 assert(bufLen &gt;= <span class="keyword">sizeof</span>(<a class="code" href="structStunMsgHdr.html">StunMsgHdr</a>));
  592. <a name="l00551"></a>00551 <span class="keywordtype">char</span>* ptr = buf;
  593. <a name="l00552"></a>00552
  594. <a name="l00553"></a>00553 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a7cc96e6cbb87582e56f298eb891c43e2">msgType</a>);
  595. <a name="l00554"></a>00554 <span class="keywordtype">char</span>* lengthp = ptr;
  596. <a name="l00555"></a>00555 ptr = <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(ptr, 0);
  597. <a name="l00556"></a>00556 ptr = <a class="code" href="stun_8cc.html#a32bf1e0d5e4833c497c540b4681dd808">encode</a>(ptr, reinterpret_cast&lt;const char*&gt;(msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>), <span class="keyword">sizeof</span>(msg.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>));
  598. <a name="l00557"></a>00557
  599. <a name="l00558"></a>00558 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding stun message: &quot;</span> &lt;&lt; endl;
  600. <a name="l00559"></a>00559 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#a605889708c9696c60f1284b3c486ca18">hasMappedAddress</a>)
  601. <a name="l00560"></a>00560 {
  602. <a name="l00561"></a>00561 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding MappedAddress: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  603. <a name="l00562"></a>00562 ptr = <a class="code" href="stun_8cc.html#a85ef119873a4b4a76012d89f1d6c23fb">encodeAtrAddress4</a> (ptr, <a class="code" href="stun_8h.html#a2479ede510923cc5e8a0c2da5558acc3">MappedAddress</a>, msg.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>);
  604. <a name="l00563"></a>00563 }
  605. <a name="l00564"></a>00564 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#ab7926e7d5ceb2d9c3337439ec89ec87d">hasResponseAddress</a>)
  606. <a name="l00565"></a>00565 {
  607. <a name="l00566"></a>00566 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding ResponseAddress: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#adb06537683f153438687995e8d1783c6">responseAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  608. <a name="l00567"></a>00567 ptr = <a class="code" href="stun_8cc.html#a85ef119873a4b4a76012d89f1d6c23fb">encodeAtrAddress4</a>(ptr, <a class="code" href="stun_8h.html#a91ab7c5c3d71a86a77679d0d2786fae8">ResponseAddress</a>, msg.<a class="code" href="structStunMessage.html#adb06537683f153438687995e8d1783c6">responseAddress</a>);
  609. <a name="l00568"></a>00568 }
  610. <a name="l00569"></a>00569 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#a0fe2488f060fe65507c9a58bbb66899f">hasChangeRequest</a>)
  611. <a name="l00570"></a>00570 {
  612. <a name="l00571"></a>00571 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding ChangeRequest: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a65e5de40376764ea12d96f5b5ebe2c29">changeRequest</a>.<a class="code" href="structStunAtrChangeRequest.html#adbb006a8e7cc0637d01959c8ba985c0c">value</a> &lt;&lt; endl;
  613. <a name="l00572"></a>00572 ptr = <a class="code" href="stun_8cc.html#a076612a210669e11d7c284f08b3a34b2">encodeAtrChangeRequest</a>(ptr, msg.<a class="code" href="structStunMessage.html#a65e5de40376764ea12d96f5b5ebe2c29">changeRequest</a>);
  614. <a name="l00573"></a>00573 }
  615. <a name="l00574"></a>00574 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#a5feb7431931118218d78642c4a7c83db">hasSourceAddress</a>)
  616. <a name="l00575"></a>00575 {
  617. <a name="l00576"></a>00576 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding SourceAddress: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#ab7203fc2e5a8b1248c9e14ce6e6ec313">sourceAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  618. <a name="l00577"></a>00577 ptr = <a class="code" href="stun_8cc.html#a85ef119873a4b4a76012d89f1d6c23fb">encodeAtrAddress4</a>(ptr, <a class="code" href="stun_8h.html#ab2b81c4c09625b39c5a1d57ee2b460a4">SourceAddress</a>, msg.<a class="code" href="structStunMessage.html#ab7203fc2e5a8b1248c9e14ce6e6ec313">sourceAddress</a>);
  619. <a name="l00578"></a>00578 }
  620. <a name="l00579"></a>00579 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#a4d3036e012cce04e6be670898f4e24eb">hasChangedAddress</a>)
  621. <a name="l00580"></a>00580 {
  622. <a name="l00581"></a>00581 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding ChangedAddress: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  623. <a name="l00582"></a>00582 ptr = <a class="code" href="stun_8cc.html#a85ef119873a4b4a76012d89f1d6c23fb">encodeAtrAddress4</a>(ptr, <a class="code" href="stun_8h.html#a930a1eeda1f0620132bfdea315e711f7">ChangedAddress</a>, msg.<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>);
  624. <a name="l00583"></a>00583 }
  625. <a name="l00584"></a>00584 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a>)
  626. <a name="l00585"></a>00585 {
  627. <a name="l00586"></a>00586 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding Username: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a> &lt;&lt; endl;
  628. <a name="l00587"></a>00587 ptr = <a class="code" href="stun_8cc.html#a765920e3cdcc343b0ed69a8fab4a1736">encodeAtrString</a>(ptr, <a class="code" href="stun_8h.html#a0845232ed07b469d1262348e1618b962">Username</a>, msg.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>);
  629. <a name="l00588"></a>00588 }
  630. <a name="l00589"></a>00589 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#ac09b6e5f081db9f5ec2c1a4532676366">hasPassword</a>)
  631. <a name="l00590"></a>00590 {
  632. <a name="l00591"></a>00591 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding Password: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#aa15cab0d5b2a9a044895e81d527c1b68">password</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a> &lt;&lt; endl;
  633. <a name="l00592"></a>00592 ptr = <a class="code" href="stun_8cc.html#a765920e3cdcc343b0ed69a8fab4a1736">encodeAtrString</a>(ptr, <a class="code" href="stun_8h.html#a1741d36983135a1f2eefa1b1473956a2">Password</a>, msg.<a class="code" href="structStunMessage.html#aa15cab0d5b2a9a044895e81d527c1b68">password</a>);
  634. <a name="l00593"></a>00593 }
  635. <a name="l00594"></a>00594 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#afe66349979726d392fa4f0c9c84ebedf">hasErrorCode</a>)
  636. <a name="l00595"></a>00595 {
  637. <a name="l00596"></a>00596 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding ErrorCode: class=&quot;</span>
  638. <a name="l00597"></a>00597 &lt;&lt; int(msg.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#aef65890aa74d342d326ea74000064be5">errorClass</a>)
  639. <a name="l00598"></a>00598 &lt;&lt; <span class="stringliteral">&quot; number=&quot;</span> &lt;&lt; int(msg.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#abdee224c138f23ad06aeb97ed9d6e0fe">number</a>)
  640. <a name="l00599"></a>00599 &lt;&lt; <span class="stringliteral">&quot; reason=&quot;</span>
  641. <a name="l00600"></a>00600 &lt;&lt; msg.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#a3bb11d3802d0bfe65a153d8a6bccd472">reason</a>
  642. <a name="l00601"></a>00601 &lt;&lt; endl;
  643. <a name="l00602"></a>00602
  644. <a name="l00603"></a>00603 ptr = <a class="code" href="stun_8cc.html#a61a6cc411fa597374048437e28ddffdf">encodeAtrError</a>(ptr, msg.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>);
  645. <a name="l00604"></a>00604 }
  646. <a name="l00605"></a>00605 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#a29507d464e9900ced2ce92c75bb158a4">hasUnknownAttributes</a>)
  647. <a name="l00606"></a>00606 {
  648. <a name="l00607"></a>00607 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding UnknownAttribute: ???&quot;</span> &lt;&lt; endl;
  649. <a name="l00608"></a>00608 ptr = <a class="code" href="stun_8cc.html#a09523c814cf26f59012e800713447143">encodeAtrUnknown</a>(ptr, msg.<a class="code" href="structStunMessage.html#a40311e31449afb24c3e40db8c8fc6911">unknownAttributes</a>);
  650. <a name="l00609"></a>00609 }
  651. <a name="l00610"></a>00610 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#ab88124a21ca8562a44a696ed4b39fa4f">hasReflectedFrom</a>)
  652. <a name="l00611"></a>00611 {
  653. <a name="l00612"></a>00612 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding ReflectedFrom: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#ab80a59189b1b938698339f788417f32b">reflectedFrom</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  654. <a name="l00613"></a>00613 ptr = <a class="code" href="stun_8cc.html#a85ef119873a4b4a76012d89f1d6c23fb">encodeAtrAddress4</a>(ptr, <a class="code" href="stun_8h.html#a11d1c4b717d340b20582ac7dead1cd49">ReflectedFrom</a>, msg.<a class="code" href="structStunMessage.html#ab80a59189b1b938698339f788417f32b">reflectedFrom</a>);
  655. <a name="l00614"></a>00614 }
  656. <a name="l00615"></a>00615 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#a8e149870681eebee0a55b0f630c89471">hasXorMappedAddress</a>)
  657. <a name="l00616"></a>00616 {
  658. <a name="l00617"></a>00617 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding XorMappedAddress: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a2ef5db0ff5bd395800262b8b9d49b6bb">xorMappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  659. <a name="l00618"></a>00618 ptr = <a class="code" href="stun_8cc.html#a85ef119873a4b4a76012d89f1d6c23fb">encodeAtrAddress4</a> (ptr, <a class="code" href="stun_8h.html#afd742381f8d8131e21dac399b8a26c3a">XorMappedAddress</a>, msg.<a class="code" href="structStunMessage.html#a2ef5db0ff5bd395800262b8b9d49b6bb">xorMappedAddress</a>);
  660. <a name="l00619"></a>00619 }
  661. <a name="l00620"></a>00620 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#ad2d3b64955956272841f656fe354aa18">xorOnly</a>)
  662. <a name="l00621"></a>00621 {
  663. <a name="l00622"></a>00622 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding xorOnly: &quot;</span> &lt;&lt; endl;
  664. <a name="l00623"></a>00623 ptr = <a class="code" href="stun_8cc.html#a3d149acb0bcd480596ea26b73b47d05f">encodeXorOnly</a>( ptr );
  665. <a name="l00624"></a>00624 }
  666. <a name="l00625"></a>00625 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#afcf544b5b0663edad215525b8bcd67df">hasServerName</a>)
  667. <a name="l00626"></a>00626 {
  668. <a name="l00627"></a>00627 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding ServerName: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a1998c160a79020e146957c14ae1add11">serverName</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a> &lt;&lt; endl;
  669. <a name="l00628"></a>00628 ptr = <a class="code" href="stun_8cc.html#a765920e3cdcc343b0ed69a8fab4a1736">encodeAtrString</a>(ptr, <a class="code" href="stun_8h.html#a53f47b517798fa3e1b64313d04168ea2">ServerName</a>, msg.<a class="code" href="structStunMessage.html#a1998c160a79020e146957c14ae1add11">serverName</a>);
  670. <a name="l00629"></a>00629 }
  671. <a name="l00630"></a>00630 <span class="keywordflow">if</span> (msg.<a class="code" href="structStunMessage.html#ace894f36510487ec8151609513974eba">hasSecondaryAddress</a>)
  672. <a name="l00631"></a>00631 {
  673. <a name="l00632"></a>00632 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Encoding SecondaryAddress: &quot;</span> &lt;&lt; msg.<a class="code" href="structStunMessage.html#a8fc16bd394228aa6fa6e1f5f98c356c1">secondaryAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  674. <a name="l00633"></a>00633 ptr = <a class="code" href="stun_8cc.html#a85ef119873a4b4a76012d89f1d6c23fb">encodeAtrAddress4</a> (ptr, <a class="code" href="stun_8h.html#a4d77a59e0dc2c3c06deb3730baa78f80">SecondaryAddress</a>, msg.<a class="code" href="structStunMessage.html#a8fc16bd394228aa6fa6e1f5f98c356c1">secondaryAddress</a>);
  675. <a name="l00634"></a>00634 }
  676. <a name="l00635"></a>00635
  677. <a name="l00636"></a>00636 <span class="keywordflow">if</span> (password.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> &gt; 0)
  678. <a name="l00637"></a>00637 {
  679. <a name="l00638"></a>00638 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;HMAC with password: &quot;</span> &lt;&lt; password.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a> &lt;&lt; endl;
  680. <a name="l00639"></a>00639
  681. <a name="l00640"></a>00640 <a class="code" href="structStunAtrIntegrity.html">StunAtrIntegrity</a> integrity;
  682. <a name="l00641"></a>00641 <a class="code" href="stun_8cc.html#afdae90956a999518a90595e783525734">computeHmac</a>(integrity.<a class="code" href="structStunAtrIntegrity.html#a584fa6b758583be87dfad5c10da9fd44">hash</a>, buf, <span class="keywordtype">int</span>(ptr-buf) , password.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>, password.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a>);
  683. <a name="l00642"></a>00642 ptr = <a class="code" href="stun_8cc.html#ad58a0afd3626cc03abb1d92edbf6c4d3">encodeAtrIntegrity</a>(ptr, integrity);
  684. <a name="l00643"></a>00643 }
  685. <a name="l00644"></a>00644 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; endl;
  686. <a name="l00645"></a>00645
  687. <a name="l00646"></a>00646 <a class="code" href="stun_8cc.html#a495405bc65a0788775febd87866ece36">encode16</a>(lengthp, <a class="code" href="stun_8h.html#accc14a7c843716dfed734f3a03e8d2b6">UInt16</a>(ptr - buf - <span class="keyword">sizeof</span>(<a class="code" href="structStunMsgHdr.html">StunMsgHdr</a>)));
  688. <a name="l00647"></a>00647 <span class="keywordflow">return</span> int(ptr - buf);
  689. <a name="l00648"></a>00648 }
  690. <a name="l00649"></a>00649
  691. <a name="l00650"></a>00650 <span class="keywordtype">int</span>
  692. <a name="l00651"></a><a class="code" href="stun_8h.html#a67e399c9420f09178de194bee0bde87a">00651</a> <a class="code" href="stun_8cc.html#a67e399c9420f09178de194bee0bde87a">stunRand</a>()
  693. <a name="l00652"></a>00652 {
  694. <a name="l00653"></a>00653 <span class="comment">// return 32 bits of random stuff</span>
  695. <a name="l00654"></a>00654 assert( <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>) == 4 );
  696. <a name="l00655"></a>00655 <span class="keyword">static</span> <span class="keywordtype">bool</span> init=<span class="keyword">false</span>;
  697. <a name="l00656"></a>00656 <span class="keywordflow">if</span> ( !init )
  698. <a name="l00657"></a>00657 {
  699. <a name="l00658"></a>00658 init = <span class="keyword">true</span>;
  700. <a name="l00659"></a>00659
  701. <a name="l00660"></a>00660 <a class="code" href="stun_8h.html#aa1d7d331b6d522d6d9840fece27108d8">UInt64</a> tick;
  702. <a name="l00661"></a>00661
  703. <a name="l00662"></a>00662 <span class="preprocessor">#if defined(MSVC_WIN32)</span>
  704. <a name="l00663"></a>00663 <span class="preprocessor"></span> <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> lowtick=0,hightick=0;
  705. <a name="l00664"></a>00664 __asm
  706. <a name="l00665"></a>00665 {
  707. <a name="l00666"></a>00666 rdtsc
  708. <a name="l00667"></a>00667 mov lowtick, eax
  709. <a name="l00668"></a>00668 mov hightick, edx
  710. <a name="l00669"></a>00669 }
  711. <a name="l00670"></a>00670 tick = hightick;
  712. <a name="l00671"></a>00671 tick &lt;&lt;= 32;
  713. <a name="l00672"></a>00672 tick |= lowtick;
  714. <a name="l00673"></a>00673 <span class="preprocessor">#elif (defined(WIN32) || defined(__GNUC__)) &amp;&amp; ( defined(__i686__) || defined(__i386__) || defined(__x86_64__) )</span>
  715. <a name="l00674"></a>00674 <span class="preprocessor"></span> <span class="keyword">asm</span>(<span class="stringliteral">&quot;rdtsc&quot;</span> : <span class="stringliteral">&quot;=A&quot;</span> (tick));
  716. <a name="l00675"></a>00675 <span class="preprocessor">#elif defined (__SUNPRO_CC) || defined( __sparc__ )</span>
  717. <a name="l00676"></a>00676 <span class="preprocessor"></span> tick = gethrtime();
  718. <a name="l00677"></a>00677 <span class="preprocessor">#elif defined(__MACH__)</span>
  719. <a name="l00678"></a>00678 <span class="preprocessor"></span> <span class="keywordtype">int</span> fd=open(<span class="stringliteral">&quot;/dev/random&quot;</span>,O_RDONLY);
  720. <a name="l00679"></a>00679 read(fd,&amp;tick,<span class="keyword">sizeof</span>(tick));
  721. <a name="l00680"></a>00680 <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>(fd);
  722. <a name="l00681"></a>00681 <span class="preprocessor">#else</span>
  723. <a name="l00682"></a>00682 <span class="preprocessor"></span><span class="preprocessor"># error Need some way to seed the random number generator</span>
  724. <a name="l00683"></a>00683 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
  725. <a name="l00684"></a>00684 <span class="preprocessor"></span> <span class="keywordtype">int</span> seed = int(tick);
  726. <a name="l00685"></a>00685 <span class="preprocessor">#ifdef WIN32</span>
  727. <a name="l00686"></a>00686 <span class="preprocessor"></span> srand(seed);
  728. <a name="l00687"></a>00687 <span class="preprocessor">#else</span>
  729. <a name="l00688"></a>00688 <span class="preprocessor"></span> srandom(seed);
  730. <a name="l00689"></a>00689 <span class="preprocessor">#endif</span>
  731. <a name="l00690"></a>00690 <span class="preprocessor"></span> }
  732. <a name="l00691"></a>00691
  733. <a name="l00692"></a>00692 <span class="preprocessor">#ifdef WIN32</span>
  734. <a name="l00693"></a>00693 <span class="preprocessor"></span> assert( RAND_MAX == 0x7fff );
  735. <a name="l00694"></a>00694 <span class="keywordtype">int</span> r1 = rand();
  736. <a name="l00695"></a>00695 <span class="keywordtype">int</span> r2 = rand();
  737. <a name="l00696"></a>00696
  738. <a name="l00697"></a>00697 <span class="keywordtype">int</span> ret = (r1&lt;&lt;16) + r2;
  739. <a name="l00698"></a>00698
  740. <a name="l00699"></a>00699 <span class="keywordflow">return</span> ret;
  741. <a name="l00700"></a>00700 <span class="preprocessor">#else</span>
  742. <a name="l00701"></a>00701 <span class="preprocessor"></span> <span class="keywordflow">return</span> random();
  743. <a name="l00702"></a>00702 <span class="preprocessor">#endif</span>
  744. <a name="l00703"></a>00703 <span class="preprocessor"></span>}
  745. <a name="l00704"></a>00704
  746. <a name="l00705"></a>00705
  747. <a name="l00707"></a>00707 <span class="keywordtype">int</span>
  748. <a name="l00708"></a><a class="code" href="stun_8h.html#ad4a19c401bea76c471959a32a19b777e">00708</a> <a class="code" href="stun_8cc.html#ad4a19c401bea76c471959a32a19b777e" title="return a random number to use as a port">stunRandomPort</a>()
  749. <a name="l00709"></a>00709 {
  750. <a name="l00710"></a>00710 <span class="keywordtype">int</span> min=0x4000;
  751. <a name="l00711"></a>00711 <span class="keywordtype">int</span> max=0x7FFF;
  752. <a name="l00712"></a>00712
  753. <a name="l00713"></a>00713 <span class="keywordtype">int</span> ret = <a class="code" href="stun_8cc.html#a67e399c9420f09178de194bee0bde87a">stunRand</a>();
  754. <a name="l00714"></a>00714 ret = ret|min;
  755. <a name="l00715"></a>00715 ret = ret&amp;max;
  756. <a name="l00716"></a>00716
  757. <a name="l00717"></a>00717 <span class="keywordflow">return</span> ret;
  758. <a name="l00718"></a>00718 }
  759. <a name="l00719"></a>00719
  760. <a name="l00720"></a>00720
  761. <a name="l00721"></a>00721 <span class="preprocessor">#ifdef NOSSL</span>
  762. <a name="l00722"></a>00722 <span class="preprocessor"></span><span class="keyword">static</span> <span class="keywordtype">void</span>
  763. <a name="l00723"></a><a class="code" href="stun_8cc.html#afdae90956a999518a90595e783525734">00723</a> <a class="code" href="stun_8cc.html#afdae90956a999518a90595e783525734">computeHmac</a>(<span class="keywordtype">char</span>* hmac, <span class="keyword">const</span> <span class="keywordtype">char</span>* input, <span class="keywordtype">int</span> length, <span class="keyword">const</span> <span class="keywordtype">char</span>* key, <span class="keywordtype">int</span> sizeKey)
  764. <a name="l00724"></a>00724 {
  765. <a name="l00725"></a>00725 strncpy(hmac,<span class="stringliteral">&quot;hmac-not-implemented&quot;</span>,20);
  766. <a name="l00726"></a>00726 }
  767. <a name="l00727"></a>00727 <span class="preprocessor">#else</span>
  768. <a name="l00728"></a>00728 <span class="preprocessor"></span><span class="preprocessor">#include &lt;openssl/hmac.h&gt;</span>
  769. <a name="l00729"></a>00729
  770. <a name="l00730"></a>00730 <span class="keyword">static</span> <span class="keywordtype">void</span>
  771. <a name="l00731"></a>00731 <a class="code" href="stun_8cc.html#afdae90956a999518a90595e783525734">computeHmac</a>(<span class="keywordtype">char</span>* hmac, <span class="keyword">const</span> <span class="keywordtype">char</span>* input, <span class="keywordtype">int</span> length, <span class="keyword">const</span> <span class="keywordtype">char</span>* key, <span class="keywordtype">int</span> sizeKey)
  772. <a name="l00732"></a>00732 {
  773. <a name="l00733"></a>00733 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> resultSize=0;
  774. <a name="l00734"></a>00734 HMAC(EVP_sha1(),
  775. <a name="l00735"></a>00735 key, sizeKey,
  776. <a name="l00736"></a>00736 reinterpret_cast&lt;const unsigned char*&gt;(input), length,
  777. <a name="l00737"></a>00737 reinterpret_cast&lt;unsigned char*&gt;(hmac), &amp;resultSize);
  778. <a name="l00738"></a>00738 assert(resultSize == 20);
  779. <a name="l00739"></a>00739 }
  780. <a name="l00740"></a>00740 <span class="preprocessor">#endif</span>
  781. <a name="l00741"></a>00741 <span class="preprocessor"></span>
  782. <a name="l00742"></a>00742
  783. <a name="l00743"></a>00743 <span class="keyword">static</span> <span class="keywordtype">void</span>
  784. <a name="l00744"></a><a class="code" href="stun_8cc.html#a2c6016ed098c5abccd8fd044c02fb6dc">00744</a> <a class="code" href="stun_8cc.html#a2c6016ed098c5abccd8fd044c02fb6dc">toHex</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* buffer, <span class="keywordtype">int</span> bufferSize, <span class="keywordtype">char</span>* output)
  785. <a name="l00745"></a>00745 {
  786. <a name="l00746"></a>00746 <span class="keyword">static</span> <span class="keywordtype">char</span> hexmap[] = <span class="stringliteral">&quot;0123456789abcdef&quot;</span>;
  787. <a name="l00747"></a>00747
  788. <a name="l00748"></a>00748 <span class="keyword">const</span> <span class="keywordtype">char</span>* p = buffer;
  789. <a name="l00749"></a>00749 <span class="keywordtype">char</span>* r = output;
  790. <a name="l00750"></a>00750 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i &lt; bufferSize; i++)
  791. <a name="l00751"></a>00751 {
  792. <a name="l00752"></a>00752 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> temp = *p++;
  793. <a name="l00753"></a>00753
  794. <a name="l00754"></a>00754 <span class="keywordtype">int</span> hi = (temp &amp; 0xf0)&gt;&gt;4;
  795. <a name="l00755"></a>00755 <span class="keywordtype">int</span> low = (temp &amp; 0xf);
  796. <a name="l00756"></a>00756
  797. <a name="l00757"></a>00757 *r++ = hexmap[hi];
  798. <a name="l00758"></a>00758 *r++ = hexmap[low];
  799. <a name="l00759"></a>00759 }
  800. <a name="l00760"></a>00760 *r = 0;
  801. <a name="l00761"></a>00761 }
  802. <a name="l00762"></a>00762
  803. <a name="l00763"></a>00763 <span class="keywordtype">void</span>
  804. <a name="l00764"></a><a class="code" href="stun_8h.html#ad8f7e4041f7591f4b621625c9190e80f">00764</a> <a class="code" href="stun_8cc.html#a5f442520a968eaa3f90caa8dcd40bd33">stunCreateUserName</a>(<span class="keyword">const</span> <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; source, <a class="code" href="structStunAtrString.html">StunAtrString</a>* username)
  805. <a name="l00765"></a>00765 {
  806. <a name="l00766"></a>00766 <a class="code" href="stun_8h.html#aa1d7d331b6d522d6d9840fece27108d8">UInt64</a> time = <a class="code" href="stun_8cc.html#a43c07bd7f476e16ea3e48a1033e90ba0">stunGetSystemTimeSecs</a>();
  807. <a name="l00767"></a>00767 time -= (time % 20*60);
  808. <a name="l00768"></a>00768 <span class="comment">//UInt64 hitime = time &gt;&gt; 32;</span>
  809. <a name="l00769"></a>00769 <a class="code" href="stun_8h.html#aa1d7d331b6d522d6d9840fece27108d8">UInt64</a> lotime = time &amp; 0xFFFFFFFF;
  810. <a name="l00770"></a>00770
  811. <a name="l00771"></a>00771 <span class="keywordtype">char</span> buffer[1024];
  812. <a name="l00772"></a>00772 sprintf(buffer,
  813. <a name="l00773"></a>00773 <span class="stringliteral">&quot;%08x:%08x:%08x:&quot;</span>,
  814. <a name="l00774"></a>00774 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a>(source.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>),
  815. <a name="l00775"></a>00775 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a>(<a class="code" href="stun_8cc.html#a67e399c9420f09178de194bee0bde87a">stunRand</a>()),
  816. <a name="l00776"></a>00776 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a>(lotime));
  817. <a name="l00777"></a>00777 assert( strlen(buffer) &lt; 1024 );
  818. <a name="l00778"></a>00778
  819. <a name="l00779"></a>00779 assert(strlen(buffer) + 41 &lt; <a class="code" href="stun_8h.html#ac4d86797b42c1696b7acbd715481c0b1">STUN_MAX_STRING</a>);
  820. <a name="l00780"></a>00780
  821. <a name="l00781"></a>00781 <span class="keywordtype">char</span> hmac[20];
  822. <a name="l00782"></a>00782 <span class="keywordtype">char</span> key[] = <span class="stringliteral">&quot;Jason&quot;</span>;
  823. <a name="l00783"></a>00783 <a class="code" href="stun_8cc.html#afdae90956a999518a90595e783525734">computeHmac</a>(hmac, buffer, strlen(buffer), key, strlen(key) );
  824. <a name="l00784"></a>00784 <span class="keywordtype">char</span> hmacHex[41];
  825. <a name="l00785"></a>00785 <a class="code" href="stun_8cc.html#a2c6016ed098c5abccd8fd044c02fb6dc">toHex</a>(hmac, 20, hmacHex );
  826. <a name="l00786"></a>00786 hmacHex[40] =0;
  827. <a name="l00787"></a>00787
  828. <a name="l00788"></a>00788 strcat(buffer,hmacHex);
  829. <a name="l00789"></a>00789
  830. <a name="l00790"></a>00790 <span class="keywordtype">int</span> l = strlen(buffer);
  831. <a name="l00791"></a>00791 assert( l+1 &lt; <a class="code" href="stun_8h.html#ac4d86797b42c1696b7acbd715481c0b1">STUN_MAX_STRING</a> );
  832. <a name="l00792"></a>00792 assert( l%4 == 0 );
  833. <a name="l00793"></a>00793
  834. <a name="l00794"></a>00794 username-&gt;<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = l;
  835. <a name="l00795"></a>00795 memcpy(username-&gt;<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>,buffer,l);
  836. <a name="l00796"></a>00796 username-&gt;<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>[l]=0;
  837. <a name="l00797"></a>00797
  838. <a name="l00798"></a>00798 <span class="comment">//if (verbose) clog &lt;&lt; &quot;computed username=&quot; &lt;&lt; username.value &lt;&lt; endl;</span>
  839. <a name="l00799"></a>00799 }
  840. <a name="l00800"></a>00800
  841. <a name="l00801"></a>00801 <span class="keywordtype">void</span>
  842. <a name="l00802"></a><a class="code" href="stun_8h.html#aa8926aedab40d08f5cff31d759415962">00802</a> <a class="code" href="stun_8cc.html#aa8926aedab40d08f5cff31d759415962">stunCreatePassword</a>(<span class="keyword">const</span> <a class="code" href="structStunAtrString.html">StunAtrString</a>&amp; username, <a class="code" href="structStunAtrString.html">StunAtrString</a>* password)
  843. <a name="l00803"></a>00803 {
  844. <a name="l00804"></a>00804 <span class="keywordtype">char</span> hmac[20];
  845. <a name="l00805"></a>00805 <span class="keywordtype">char</span> key[] = <span class="stringliteral">&quot;Fluffy&quot;</span>;
  846. <a name="l00806"></a>00806 <span class="comment">//char buffer[STUN_MAX_STRING];</span>
  847. <a name="l00807"></a>00807 <a class="code" href="stun_8cc.html#afdae90956a999518a90595e783525734">computeHmac</a>(hmac, username.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>, strlen(username.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>), key, strlen(key));
  848. <a name="l00808"></a>00808 <a class="code" href="stun_8cc.html#a2c6016ed098c5abccd8fd044c02fb6dc">toHex</a>(hmac, 20, password-&gt;<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>);
  849. <a name="l00809"></a>00809 password-&gt;<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = 40;
  850. <a name="l00810"></a>00810 password-&gt;<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>[40]=0;
  851. <a name="l00811"></a>00811
  852. <a name="l00812"></a>00812 <span class="comment">//clog &lt;&lt; &quot;password=&quot; &lt;&lt; password-&gt;value &lt;&lt; endl;</span>
  853. <a name="l00813"></a>00813 }
  854. <a name="l00814"></a>00814
  855. <a name="l00815"></a>00815
  856. <a name="l00816"></a>00816 <a class="code" href="stun_8h.html#aa1d7d331b6d522d6d9840fece27108d8">UInt64</a>
  857. <a name="l00817"></a><a class="code" href="stun_8h.html#a43c07bd7f476e16ea3e48a1033e90ba0">00817</a> <a class="code" href="stun_8cc.html#a43c07bd7f476e16ea3e48a1033e90ba0">stunGetSystemTimeSecs</a>()
  858. <a name="l00818"></a>00818 {
  859. <a name="l00819"></a>00819 <a class="code" href="stun_8h.html#aa1d7d331b6d522d6d9840fece27108d8">UInt64</a> time=0;
  860. <a name="l00820"></a>00820 <span class="preprocessor">#if defined(WIN32)</span>
  861. <a name="l00821"></a>00821 <span class="preprocessor"></span> SYSTEMTIME t;
  862. <a name="l00822"></a>00822 <span class="comment">// CJ TODO - this probably has bug on wrap around every 24 hours</span>
  863. <a name="l00823"></a>00823 GetSystemTime( &amp;t );
  864. <a name="l00824"></a>00824 time = (t.wHour*60+t.wMinute)*60+t.wSecond;
  865. <a name="l00825"></a>00825 #<span class="keywordflow">else</span>
  866. <a name="l00826"></a>00826 <span class="keyword">struct</span> timeval now;
  867. <a name="l00827"></a>00827 gettimeofday( &amp;now , NULL );
  868. <a name="l00828"></a>00828 <span class="comment">//assert( now );</span>
  869. <a name="l00829"></a>00829 time = now.tv_sec;
  870. <a name="l00830"></a>00830 <span class="preprocessor">#endif</span>
  871. <a name="l00831"></a>00831 <span class="preprocessor"></span> <span class="keywordflow">return</span> time;
  872. <a name="l00832"></a>00832 }
  873. <a name="l00833"></a>00833
  874. <a name="l00834"></a>00834
  875. <a name="l00835"></a><a class="code" href="stun_8cc.html#a8cca5b933ad45c62c8d5e4c82dfb19cc">00835</a> ostream&amp; <a class="code" href="MessageObserver_8cc.html#a8f32b0caa624b1003f9f7a382c634798">operator&lt;&lt; </a>( ostream&amp; strm, <span class="keyword">const</span> <a class="code" href="structUInt128.html">UInt128</a>&amp; r )
  876. <a name="l00836"></a>00836 {
  877. <a name="l00837"></a>00837 strm &lt;&lt; int(r.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[0]);
  878. <a name="l00838"></a>00838 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=1; i&lt;16; i++ )
  879. <a name="l00839"></a>00839 {
  880. <a name="l00840"></a>00840 strm &lt;&lt; <span class="charliteral">&#39;:&#39;</span> &lt;&lt; int(r.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[i]);
  881. <a name="l00841"></a>00841 }
  882. <a name="l00842"></a>00842
  883. <a name="l00843"></a>00843 <span class="keywordflow">return</span> strm;
  884. <a name="l00844"></a>00844 }
  885. <a name="l00845"></a>00845
  886. <a name="l00846"></a>00846 ostream&amp;
  887. <a name="l00847"></a><a class="code" href="stun_8cc.html#a34a70949080bffbb59fb937092c26802">00847</a> <a class="code" href="MessageObserver_8cc.html#a8f32b0caa624b1003f9f7a382c634798">operator&lt;&lt;</a>( ostream&amp; strm, <span class="keyword">const</span> <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; addr)
  888. <a name="l00848"></a>00848 {
  889. <a name="l00849"></a>00849 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a> ip = addr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  890. <a name="l00850"></a>00850 strm &lt;&lt; ((int)(ip&gt;&gt;24)&amp;0xFF) &lt;&lt; <span class="stringliteral">&quot;.&quot;</span>;
  891. <a name="l00851"></a>00851 strm &lt;&lt; ((int)(ip&gt;&gt;16)&amp;0xFF) &lt;&lt; <span class="stringliteral">&quot;.&quot;</span>;
  892. <a name="l00852"></a>00852 strm &lt;&lt; ((int)(ip&gt;&gt; 8)&amp;0xFF) &lt;&lt; <span class="stringliteral">&quot;.&quot;</span>;
  893. <a name="l00853"></a>00853 strm &lt;&lt; ((int)(ip&gt;&gt; 0)&amp;0xFF) ;
  894. <a name="l00854"></a>00854
  895. <a name="l00855"></a>00855 strm &lt;&lt; <span class="stringliteral">&quot;:&quot;</span> &lt;&lt; addr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  896. <a name="l00856"></a>00856
  897. <a name="l00857"></a>00857 <span class="keywordflow">return</span> strm;
  898. <a name="l00858"></a>00858 }
  899. <a name="l00859"></a>00859
  900. <a name="l00860"></a>00860
  901. <a name="l00861"></a>00861 <span class="comment">// returns true if it scucceeded</span>
  902. <a name="l00862"></a>00862 <span class="keywordtype">bool</span>
  903. <a name="l00863"></a><a class="code" href="stun_8h.html#af497f133b72e1dc4595d9d293d706351">00863</a> <a class="code" href="stun_8cc.html#af497f133b72e1dc4595d9d293d706351">stunParseHostName</a>( <span class="keywordtype">char</span>* peerName,
  904. <a name="l00864"></a>00864 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a>&amp; ip,
  905. <a name="l00865"></a>00865 <a class="code" href="stun_8h.html#accc14a7c843716dfed734f3a03e8d2b6">UInt16</a>&amp; portVal,
  906. <a name="l00866"></a>00866 <a class="code" href="stun_8h.html#accc14a7c843716dfed734f3a03e8d2b6">UInt16</a> defaultPort )
  907. <a name="l00867"></a>00867 {
  908. <a name="l00868"></a>00868 in_addr sin_addr;
  909. <a name="l00869"></a>00869
  910. <a name="l00870"></a>00870 <span class="keywordtype">char</span> host[512];
  911. <a name="l00871"></a>00871 strncpy(host,peerName,512);
  912. <a name="l00872"></a>00872 host[512-1]=<span class="charliteral">&#39;\0&#39;</span>;
  913. <a name="l00873"></a>00873 <span class="keywordtype">char</span>* port = NULL;
  914. <a name="l00874"></a>00874
  915. <a name="l00875"></a>00875 <span class="keywordtype">int</span> portNum = defaultPort;
  916. <a name="l00876"></a>00876
  917. <a name="l00877"></a>00877 <span class="comment">// pull out the port part if present.</span>
  918. <a name="l00878"></a>00878 <span class="keywordtype">char</span>* sep = strchr(host,<span class="charliteral">&#39;:&#39;</span>);
  919. <a name="l00879"></a>00879
  920. <a name="l00880"></a>00880 <span class="keywordflow">if</span> ( sep == NULL )
  921. <a name="l00881"></a>00881 {
  922. <a name="l00882"></a>00882 portNum = defaultPort;
  923. <a name="l00883"></a>00883 }
  924. <a name="l00884"></a>00884 <span class="keywordflow">else</span>
  925. <a name="l00885"></a>00885 {
  926. <a name="l00886"></a>00886 *sep = <span class="charliteral">&#39;\0&#39;</span>;
  927. <a name="l00887"></a>00887 port = sep + 1;
  928. <a name="l00888"></a>00888 <span class="comment">// set port part</span>
  929. <a name="l00889"></a>00889
  930. <a name="l00890"></a>00890 <span class="keywordtype">char</span>* endPtr=NULL;
  931. <a name="l00891"></a>00891
  932. <a name="l00892"></a>00892 portNum = strtol(port,&amp;endPtr,10);
  933. <a name="l00893"></a>00893
  934. <a name="l00894"></a>00894 <span class="keywordflow">if</span> ( endPtr != NULL )
  935. <a name="l00895"></a>00895 {
  936. <a name="l00896"></a>00896 <span class="keywordflow">if</span> ( *endPtr != <span class="charliteral">&#39;\0&#39;</span> )
  937. <a name="l00897"></a>00897 {
  938. <a name="l00898"></a>00898 portNum = defaultPort;
  939. <a name="l00899"></a>00899 }
  940. <a name="l00900"></a>00900 }
  941. <a name="l00901"></a>00901 }
  942. <a name="l00902"></a>00902
  943. <a name="l00903"></a>00903 <span class="keywordflow">if</span> ( portNum &lt; 1024 ) <span class="keywordflow">return</span> <span class="keyword">false</span>;
  944. <a name="l00904"></a>00904 <span class="keywordflow">if</span> ( portNum &gt;= 0xFFFF ) <span class="keywordflow">return</span> <span class="keyword">false</span>;
  945. <a name="l00905"></a>00905
  946. <a name="l00906"></a>00906 <span class="comment">// figure out the host part</span>
  947. <a name="l00907"></a>00907 <span class="keyword">struct </span>hostent* h;
  948. <a name="l00908"></a>00908
  949. <a name="l00909"></a>00909 <span class="preprocessor">#ifdef WIN32</span>
  950. <a name="l00910"></a>00910 <span class="preprocessor"></span> assert( strlen(host) &gt;= 1 );
  951. <a name="l00911"></a>00911 <span class="keywordflow">if</span> ( isdigit( host[0] ) )
  952. <a name="l00912"></a>00912 {
  953. <a name="l00913"></a>00913 <span class="comment">// assume it is a ip address</span>
  954. <a name="l00914"></a>00914 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> a = inet_addr(host);
  955. <a name="l00915"></a>00915 <span class="comment">//cerr &lt;&lt; &quot;a=0x&quot; &lt;&lt; hex &lt;&lt; a &lt;&lt; dec &lt;&lt; endl;</span>
  956. <a name="l00916"></a>00916
  957. <a name="l00917"></a>00917 ip = ntohl( a );
  958. <a name="l00918"></a>00918 }
  959. <a name="l00919"></a>00919 <span class="keywordflow">else</span>
  960. <a name="l00920"></a>00920 {
  961. <a name="l00921"></a>00921 <span class="comment">// assume it is a host name</span>
  962. <a name="l00922"></a>00922 h = gethostbyname( host );
  963. <a name="l00923"></a>00923
  964. <a name="l00924"></a>00924 <span class="keywordflow">if</span> ( h == NULL )
  965. <a name="l00925"></a>00925 {
  966. <a name="l00926"></a>00926 <span class="keywordtype">int</span> err = <a class="code" href="stun__udp_8h.html#a604d53fe9d66ce3a47ab2354b20f8e62">getErrno</a>();
  967. <a name="l00927"></a>00927 std::cerr &lt;&lt; <span class="stringliteral">&quot;error was &quot;</span> &lt;&lt; err &lt;&lt; std::endl;
  968. <a name="l00928"></a>00928 assert( err != <a class="code" href="stun__udp_8h.html#a25b517a4390903e6fa0f945e80a3394c">WSANOTINITIALISED</a> );
  969. <a name="l00929"></a>00929
  970. <a name="l00930"></a>00930 ip = ntohl( 0x7F000001L );
  971. <a name="l00931"></a>00931
  972. <a name="l00932"></a>00932 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  973. <a name="l00933"></a>00933 }
  974. <a name="l00934"></a>00934 <span class="keywordflow">else</span>
  975. <a name="l00935"></a>00935 {
  976. <a name="l00936"></a>00936 sin_addr = *(<span class="keyword">struct </span>in_addr*)h-&gt;h_addr;
  977. <a name="l00937"></a>00937 ip = ntohl( sin_addr.s_addr );
  978. <a name="l00938"></a>00938 }
  979. <a name="l00939"></a>00939 }
  980. <a name="l00940"></a>00940
  981. <a name="l00941"></a>00941 <span class="preprocessor">#else</span>
  982. <a name="l00942"></a>00942 <span class="preprocessor"></span> h = gethostbyname( host );
  983. <a name="l00943"></a>00943 <span class="keywordflow">if</span> ( h == NULL )
  984. <a name="l00944"></a>00944 {
  985. <a name="l00945"></a>00945 <span class="keywordtype">int</span> err = <a class="code" href="stun__udp_8h.html#a604d53fe9d66ce3a47ab2354b20f8e62">getErrno</a>();
  986. <a name="l00946"></a>00946 std::cerr &lt;&lt; <span class="stringliteral">&quot;error was &quot;</span> &lt;&lt; err &lt;&lt; std::endl;
  987. <a name="l00947"></a>00947 ip = ntohl( 0x7F000001L );
  988. <a name="l00948"></a>00948 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  989. <a name="l00949"></a>00949 }
  990. <a name="l00950"></a>00950 <span class="keywordflow">else</span>
  991. <a name="l00951"></a>00951 {
  992. <a name="l00952"></a>00952 sin_addr = *(<span class="keyword">struct </span>in_addr*)h-&gt;h_addr;
  993. <a name="l00953"></a>00953 ip = ntohl( sin_addr.s_addr );
  994. <a name="l00954"></a>00954 }
  995. <a name="l00955"></a>00955 <span class="preprocessor">#endif</span>
  996. <a name="l00956"></a>00956 <span class="preprocessor"></span>
  997. <a name="l00957"></a>00957 portVal = portNum;
  998. <a name="l00958"></a>00958
  999. <a name="l00959"></a>00959 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1000. <a name="l00960"></a>00960 }
  1001. <a name="l00961"></a>00961
  1002. <a name="l00962"></a>00962
  1003. <a name="l00963"></a>00963 <span class="keywordtype">bool</span>
  1004. <a name="l00964"></a><a class="code" href="stun_8h.html#ac4cabe7b17eb753dedad02cbb755563c">00964</a> <a class="code" href="stun_8cc.html#acc6ab05f9237a355919b963c6835ec9e" title="find the IP address of a the specified stun server - return false is fails parse">stunParseServerName</a>( <span class="keywordtype">char</span>* name, <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; addr)
  1005. <a name="l00965"></a>00965 {
  1006. <a name="l00966"></a>00966 assert(name);
  1007. <a name="l00967"></a>00967
  1008. <a name="l00968"></a>00968 <span class="comment">// TODO - put in DNS SRV stuff.</span>
  1009. <a name="l00969"></a>00969
  1010. <a name="l00970"></a>00970 <span class="keywordtype">bool</span> ret = <a class="code" href="stun_8cc.html#af497f133b72e1dc4595d9d293d706351">stunParseHostName</a>( name, addr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, addr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>, 3478);
  1011. <a name="l00971"></a>00971 <span class="keywordflow">if</span> ( ret != <span class="keyword">true</span> )
  1012. <a name="l00972"></a>00972 {
  1013. <a name="l00973"></a>00973 addr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>=0xFFFF;
  1014. <a name="l00974"></a>00974 }
  1015. <a name="l00975"></a>00975 <span class="keywordflow">return</span> ret;
  1016. <a name="l00976"></a>00976 }
  1017. <a name="l00977"></a>00977
  1018. <a name="l00978"></a>00978
  1019. <a name="l00979"></a>00979 <span class="keyword">static</span> <span class="keywordtype">void</span>
  1020. <a name="l00980"></a><a class="code" href="stun_8cc.html#a965ceef7c244ec6895b9670acdf57930">00980</a> <a class="code" href="stun_8cc.html#a965ceef7c244ec6895b9670acdf57930">stunCreateErrorResponse</a>(<a class="code" href="structStunMessage.html">StunMessage</a>&amp; response, <span class="keywordtype">int</span> cl, <span class="keywordtype">int</span> number, <span class="keyword">const</span> <span class="keywordtype">char</span>* msg)
  1021. <a name="l00981"></a>00981 {
  1022. <a name="l00982"></a>00982 response.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a7cc96e6cbb87582e56f298eb891c43e2">msgType</a> = <a class="code" href="stun_8h.html#ae440f0f666689240a0aa0f450d1783ab">BindErrorResponseMsg</a>;
  1023. <a name="l00983"></a>00983 response.<a class="code" href="structStunMessage.html#afe66349979726d392fa4f0c9c84ebedf">hasErrorCode</a> = <span class="keyword">true</span>;
  1024. <a name="l00984"></a>00984 response.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#aef65890aa74d342d326ea74000064be5">errorClass</a> = cl;
  1025. <a name="l00985"></a>00985 response.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#abdee224c138f23ad06aeb97ed9d6e0fe">number</a> = number;
  1026. <a name="l00986"></a>00986 strcpy(response.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#a3bb11d3802d0bfe65a153d8a6bccd472">reason</a>, msg);
  1027. <a name="l00987"></a>00987 }
  1028. <a name="l00988"></a>00988
  1029. <a name="l00989"></a>00989 <span class="preprocessor">#if 0</span>
  1030. <a name="l00990"></a>00990 <span class="preprocessor"></span><span class="keyword">static</span> <span class="keywordtype">void</span>
  1031. <a name="l00991"></a>00991 stunCreateSharedSecretErrorResponse(<a class="code" href="structStunMessage.html">StunMessage</a>&amp; response, <span class="keywordtype">int</span> cl, <span class="keywordtype">int</span> number, <span class="keyword">const</span> <span class="keywordtype">char</span>* msg)
  1032. <a name="l00992"></a>00992 {
  1033. <a name="l00993"></a>00993 response.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a7cc96e6cbb87582e56f298eb891c43e2">msgType</a> = <a class="code" href="stun_8h.html#aaaa9b37f28fbe5021e650eaccd372d4d">SharedSecretErrorResponseMsg</a>;
  1034. <a name="l00994"></a>00994 response.<a class="code" href="structStunMessage.html#afe66349979726d392fa4f0c9c84ebedf">hasErrorCode</a> = <span class="keyword">true</span>;
  1035. <a name="l00995"></a>00995 response.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#aef65890aa74d342d326ea74000064be5">errorClass</a> = cl;
  1036. <a name="l00996"></a>00996 response.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#abdee224c138f23ad06aeb97ed9d6e0fe">number</a> = number;
  1037. <a name="l00997"></a>00997 strcpy(response.<a class="code" href="structStunMessage.html#a8e6a7183381efea4e4a92e2b4e8bba6a">errorCode</a>.<a class="code" href="structStunAtrError.html#a3bb11d3802d0bfe65a153d8a6bccd472">reason</a>, msg);
  1038. <a name="l00998"></a>00998 }
  1039. <a name="l00999"></a>00999 <span class="preprocessor">#endif</span>
  1040. <a name="l01000"></a>01000 <span class="preprocessor"></span>
  1041. <a name="l01001"></a>01001 <span class="keyword">static</span> <span class="keywordtype">void</span>
  1042. <a name="l01002"></a><a class="code" href="stun_8cc.html#a1382231021dcb9f568718d9a75087f48">01002</a> <a class="code" href="stun_8cc.html#a1382231021dcb9f568718d9a75087f48">stunCreateSharedSecretResponse</a>(<span class="keyword">const</span> <a class="code" href="structStunMessage.html">StunMessage</a>&amp; request, <span class="keyword">const</span> <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; source, <a class="code" href="structStunMessage.html">StunMessage</a>&amp; response)
  1043. <a name="l01003"></a>01003 {
  1044. <a name="l01004"></a>01004 response.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a7cc96e6cbb87582e56f298eb891c43e2">msgType</a> = <a class="code" href="stun_8h.html#af179b3b160dd9d71db8bcff29500504c">SharedSecretResponseMsg</a>;
  1045. <a name="l01005"></a>01005 response.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a> = request.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>;
  1046. <a name="l01006"></a>01006
  1047. <a name="l01007"></a>01007 response.<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a> = <span class="keyword">true</span>;
  1048. <a name="l01008"></a>01008 <a class="code" href="stun_8cc.html#a5f442520a968eaa3f90caa8dcd40bd33">stunCreateUserName</a>( source, &amp;response.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>);
  1049. <a name="l01009"></a>01009
  1050. <a name="l01010"></a>01010 response.<a class="code" href="structStunMessage.html#ac09b6e5f081db9f5ec2c1a4532676366">hasPassword</a> = <span class="keyword">true</span>;
  1051. <a name="l01011"></a>01011 <a class="code" href="stun_8cc.html#aa8926aedab40d08f5cff31d759415962">stunCreatePassword</a>( response.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>, &amp;response.<a class="code" href="structStunMessage.html#aa15cab0d5b2a9a044895e81d527c1b68">password</a>);
  1052. <a name="l01012"></a>01012 }
  1053. <a name="l01013"></a>01013
  1054. <a name="l01014"></a>01014
  1055. <a name="l01015"></a>01015 <span class="comment">// This funtion takes a single message sent to a stun server, parses</span>
  1056. <a name="l01016"></a>01016 <span class="comment">// and constructs an apropriate repsonse - returns true if message is</span>
  1057. <a name="l01017"></a>01017 <span class="comment">// valid</span>
  1058. <a name="l01018"></a>01018 <span class="keywordtype">bool</span>
  1059. <a name="l01019"></a><a class="code" href="stun_8cc.html#a811b0bd40524a040d7ec6676e9635c22">01019</a> <a class="code" href="stun_8cc.html#a811b0bd40524a040d7ec6676e9635c22">stunServerProcessMsg</a>( <span class="keywordtype">char</span>* buf,
  1060. <a name="l01020"></a>01020 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufLen,
  1061. <a name="l01021"></a>01021 <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; from,
  1062. <a name="l01022"></a>01022 <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; secondary,
  1063. <a name="l01023"></a>01023 <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; myAddr,
  1064. <a name="l01024"></a>01024 <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; altAddr,
  1065. <a name="l01025"></a>01025 <a class="code" href="structStunMessage.html">StunMessage</a>* resp,
  1066. <a name="l01026"></a>01026 <a class="code" href="structStunAddress4.html">StunAddress4</a>* destination,
  1067. <a name="l01027"></a>01027 <a class="code" href="structStunAtrString.html">StunAtrString</a>* hmacPassword,
  1068. <a name="l01028"></a>01028 <span class="keywordtype">bool</span>* changePort,
  1069. <a name="l01029"></a>01029 <span class="keywordtype">bool</span>* changeIp,
  1070. <a name="l01030"></a>01030 <span class="keywordtype">bool</span> verbose)
  1071. <a name="l01031"></a>01031 {
  1072. <a name="l01032"></a>01032
  1073. <a name="l01033"></a>01033 <span class="comment">// set up information for default response</span>
  1074. <a name="l01034"></a>01034
  1075. <a name="l01035"></a>01035 memset( resp, 0 , <span class="keyword">sizeof</span>(*resp) );
  1076. <a name="l01036"></a>01036
  1077. <a name="l01037"></a>01037 *changeIp = <span class="keyword">false</span>;
  1078. <a name="l01038"></a>01038 *changePort = <span class="keyword">false</span>;
  1079. <a name="l01039"></a>01039
  1080. <a name="l01040"></a>01040 <a class="code" href="structStunMessage.html">StunMessage</a> req;
  1081. <a name="l01041"></a>01041 <span class="keywordtype">bool</span> ok = <a class="code" href="stun_8cc.html#af81c30137f26a4cb158b48aa21f4f681">stunParseMessage</a>( buf,bufLen, req, verbose);
  1082. <a name="l01042"></a>01042
  1083. <a name="l01043"></a>01043 <span class="keywordflow">if</span> (!ok) <span class="comment">// Complete garbage, drop it on the floor</span>
  1084. <a name="l01044"></a>01044 {
  1085. <a name="l01045"></a>01045 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Request did not parse&quot;</span> &lt;&lt; endl;
  1086. <a name="l01046"></a>01046 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  1087. <a name="l01047"></a>01047 }
  1088. <a name="l01048"></a>01048 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Request parsed ok&quot;</span> &lt;&lt; endl;
  1089. <a name="l01049"></a>01049
  1090. <a name="l01050"></a>01050 <a class="code" href="structStunAddress4.html">StunAddress4</a> mapped = req.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>;
  1091. <a name="l01051"></a>01051 <a class="code" href="structStunAddress4.html">StunAddress4</a> respondTo = req.<a class="code" href="structStunMessage.html#adb06537683f153438687995e8d1783c6">responseAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>;
  1092. <a name="l01052"></a>01052 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a> flags = req.<a class="code" href="structStunMessage.html#a65e5de40376764ea12d96f5b5ebe2c29">changeRequest</a>.<a class="code" href="structStunAtrChangeRequest.html#adbb006a8e7cc0637d01959c8ba985c0c">value</a>;
  1093. <a name="l01053"></a>01053
  1094. <a name="l01054"></a>01054 <span class="keywordflow">switch</span> (req.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a7cc96e6cbb87582e56f298eb891c43e2">msgType</a>)
  1095. <a name="l01055"></a>01055 {
  1096. <a name="l01056"></a>01056 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a03609184bf06df206ab09c4102428a33">SharedSecretRequestMsg</a>:
  1097. <a name="l01057"></a>01057 <span class="keywordflow">if</span>(verbose) clog &lt;&lt; <span class="stringliteral">&quot;Received SharedSecretRequestMsg on udp. send error 433.&quot;</span> &lt;&lt; endl;
  1098. <a name="l01058"></a>01058 <span class="comment">// !cj! - should fix so you know if this came over TLS or UDP</span>
  1099. <a name="l01059"></a>01059 <a class="code" href="stun_8cc.html#a1382231021dcb9f568718d9a75087f48">stunCreateSharedSecretResponse</a>(req, from, *resp);
  1100. <a name="l01060"></a>01060 <span class="comment">//stunCreateSharedSecretErrorResponse(*resp, 4, 33, &quot;this request must be over TLS&quot;);</span>
  1101. <a name="l01061"></a>01061 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1102. <a name="l01062"></a>01062
  1103. <a name="l01063"></a>01063 <span class="keywordflow">case</span> <a class="code" href="stun_8h.html#a4dfa49ebae731be7b582f0fc7b57d429">BindRequestMsg</a>:
  1104. <a name="l01064"></a>01064 <span class="keywordflow">if</span> (!req.<a class="code" href="structStunMessage.html#a2a47413181e29d0de7063d4bfa6609ff">hasMessageIntegrity</a>)
  1105. <a name="l01065"></a>01065 {
  1106. <a name="l01066"></a>01066 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;BindRequest does not contain MessageIntegrity&quot;</span> &lt;&lt; endl;
  1107. <a name="l01067"></a>01067
  1108. <a name="l01068"></a>01068 <span class="keywordflow">if</span> (0) <span class="comment">// !jf! mustAuthenticate</span>
  1109. <a name="l01069"></a>01069 {
  1110. <a name="l01070"></a>01070 <span class="keywordflow">if</span>(verbose) clog &lt;&lt; <span class="stringliteral">&quot;Received BindRequest with no MessageIntegrity. Sending 401.&quot;</span> &lt;&lt; endl;
  1111. <a name="l01071"></a>01071 <a class="code" href="stun_8cc.html#a965ceef7c244ec6895b9670acdf57930">stunCreateErrorResponse</a>(*resp, 4, 1, <span class="stringliteral">&quot;Missing MessageIntegrity&quot;</span>);
  1112. <a name="l01072"></a>01072 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1113. <a name="l01073"></a>01073 }
  1114. <a name="l01074"></a>01074 }
  1115. <a name="l01075"></a>01075 <span class="keywordflow">else</span>
  1116. <a name="l01076"></a>01076 {
  1117. <a name="l01077"></a>01077 <span class="keywordflow">if</span> (!req.<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a>)
  1118. <a name="l01078"></a>01078 {
  1119. <a name="l01079"></a>01079 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;No UserName. Send 432.&quot;</span> &lt;&lt; endl;
  1120. <a name="l01080"></a>01080 <a class="code" href="stun_8cc.html#a965ceef7c244ec6895b9670acdf57930">stunCreateErrorResponse</a>(*resp, 4, 32, <span class="stringliteral">&quot;No UserName and contains MessageIntegrity&quot;</span>);
  1121. <a name="l01081"></a>01081 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1122. <a name="l01082"></a>01082 }
  1123. <a name="l01083"></a>01083 <span class="keywordflow">else</span>
  1124. <a name="l01084"></a>01084 {
  1125. <a name="l01085"></a>01085 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Validating username: &quot;</span> &lt;&lt; req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a> &lt;&lt; endl;
  1126. <a name="l01086"></a>01086 <span class="comment">// !jf! could retrieve associated password from provisioning here</span>
  1127. <a name="l01087"></a>01087 <span class="keywordflow">if</span> (strcmp(req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>, <span class="stringliteral">&quot;test&quot;</span>) == 0)
  1128. <a name="l01088"></a>01088 {
  1129. <a name="l01089"></a>01089 <span class="keywordflow">if</span> (0)
  1130. <a name="l01090"></a>01090 {
  1131. <a name="l01091"></a>01091 <span class="comment">// !jf! if the credentials are stale</span>
  1132. <a name="l01092"></a>01092 <a class="code" href="stun_8cc.html#a965ceef7c244ec6895b9670acdf57930">stunCreateErrorResponse</a>(*resp, 4, 30, <span class="stringliteral">&quot;Stale credentials on BindRequest&quot;</span>);
  1133. <a name="l01093"></a>01093 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1134. <a name="l01094"></a>01094 }
  1135. <a name="l01095"></a>01095 <span class="keywordflow">else</span>
  1136. <a name="l01096"></a>01096 {
  1137. <a name="l01097"></a>01097 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Validating MessageIntegrity&quot;</span> &lt;&lt; endl;
  1138. <a name="l01098"></a>01098 <span class="comment">// need access to shared secret</span>
  1139. <a name="l01099"></a>01099
  1140. <a name="l01100"></a>01100 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> hmac[20];
  1141. <a name="l01101"></a>01101 <span class="preprocessor">#ifndef NOSSL</span>
  1142. <a name="l01102"></a>01102 <span class="preprocessor"></span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hmacSize=20;
  1143. <a name="l01103"></a>01103
  1144. <a name="l01104"></a>01104 HMAC(EVP_sha1(),
  1145. <a name="l01105"></a>01105 <span class="stringliteral">&quot;1234&quot;</span>, 4,
  1146. <a name="l01106"></a>01106 reinterpret_cast&lt;const unsigned char*&gt;(buf), bufLen-20-4,
  1147. <a name="l01107"></a>01107 hmac, &amp;hmacSize);
  1148. <a name="l01108"></a>01108 assert(hmacSize == 20);
  1149. <a name="l01109"></a>01109 <span class="preprocessor">#endif</span>
  1150. <a name="l01110"></a>01110 <span class="preprocessor"></span>
  1151. <a name="l01111"></a>01111 <span class="keywordflow">if</span> (memcmp(buf, hmac, 20) != 0)
  1152. <a name="l01112"></a>01112 {
  1153. <a name="l01113"></a>01113 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;MessageIntegrity is bad. Sending &quot;</span> &lt;&lt; endl;
  1154. <a name="l01114"></a>01114 <a class="code" href="stun_8cc.html#a965ceef7c244ec6895b9670acdf57930">stunCreateErrorResponse</a>(*resp, 4, 3, <span class="stringliteral">&quot;Unknown username. Try test with password 1234&quot;</span>);
  1155. <a name="l01115"></a>01115 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1156. <a name="l01116"></a>01116 }
  1157. <a name="l01117"></a>01117
  1158. <a name="l01118"></a>01118 <span class="comment">// need to compute this later after message is filled in</span>
  1159. <a name="l01119"></a>01119 resp-&gt;<a class="code" href="structStunMessage.html#a2a47413181e29d0de7063d4bfa6609ff">hasMessageIntegrity</a> = <span class="keyword">true</span>;
  1160. <a name="l01120"></a>01120 assert(req.<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a>);
  1161. <a name="l01121"></a>01121 resp-&gt;<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a> = <span class="keyword">true</span>;
  1162. <a name="l01122"></a>01122 resp-&gt;<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a> = req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>; <span class="comment">// copy username in</span>
  1163. <a name="l01123"></a>01123 }
  1164. <a name="l01124"></a>01124 }
  1165. <a name="l01125"></a>01125 <span class="keywordflow">else</span>
  1166. <a name="l01126"></a>01126 {
  1167. <a name="l01127"></a>01127 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Invalid username: &quot;</span> &lt;&lt; req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a> &lt;&lt; <span class="stringliteral">&quot;Send 430.&quot;</span> &lt;&lt; endl;
  1168. <a name="l01128"></a>01128 }
  1169. <a name="l01129"></a>01129 }
  1170. <a name="l01130"></a>01130 }
  1171. <a name="l01131"></a>01131
  1172. <a name="l01132"></a>01132 <span class="comment">// TODO !jf! should check for unknown attributes here and send 420 listing the</span>
  1173. <a name="l01133"></a>01133 <span class="comment">// unknown attributes.</span>
  1174. <a name="l01134"></a>01134
  1175. <a name="l01135"></a>01135 <span class="keywordflow">if</span> ( respondTo.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> == 0 ) respondTo = from;
  1176. <a name="l01136"></a>01136 <span class="keywordflow">if</span> ( mapped.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> == 0 ) mapped = from;
  1177. <a name="l01137"></a>01137
  1178. <a name="l01138"></a>01138 *changeIp = ( flags &amp; <a class="code" href="stun_8h.html#ad0235d173b2af6e92be9843aaa585eef">ChangeIpFlag</a> )?<span class="keyword">true</span>:<span class="keyword">false</span>;
  1179. <a name="l01139"></a>01139 *changePort = ( flags &amp; <a class="code" href="stun_8h.html#a2f247c137dd06ef8971319b9a6078ef4">ChangePortFlag</a> )?<span class="keyword">true</span>:<span class="keyword">false</span>;
  1180. <a name="l01140"></a>01140
  1181. <a name="l01141"></a>01141 <span class="keywordflow">if</span> (verbose)
  1182. <a name="l01142"></a>01142 {
  1183. <a name="l01143"></a>01143 clog &lt;&lt; <span class="stringliteral">&quot;Request is valid:&quot;</span> &lt;&lt; endl;
  1184. <a name="l01144"></a>01144 clog &lt;&lt; <span class="stringliteral">&quot;\t flags=&quot;</span> &lt;&lt; flags &lt;&lt; endl;
  1185. <a name="l01145"></a>01145 clog &lt;&lt; <span class="stringliteral">&quot;\t changeIp=&quot;</span> &lt;&lt; *changeIp &lt;&lt; endl;
  1186. <a name="l01146"></a>01146 clog &lt;&lt; <span class="stringliteral">&quot;\t changePort=&quot;</span> &lt;&lt; *changePort &lt;&lt; endl;
  1187. <a name="l01147"></a>01147 clog &lt;&lt; <span class="stringliteral">&quot;\t from = &quot;</span> &lt;&lt; from &lt;&lt; endl;
  1188. <a name="l01148"></a>01148 clog &lt;&lt; <span class="stringliteral">&quot;\t respond to = &quot;</span> &lt;&lt; respondTo &lt;&lt; endl;
  1189. <a name="l01149"></a>01149 clog &lt;&lt; <span class="stringliteral">&quot;\t mapped = &quot;</span> &lt;&lt; mapped &lt;&lt; endl;
  1190. <a name="l01150"></a>01150 }
  1191. <a name="l01151"></a>01151
  1192. <a name="l01152"></a>01152 <span class="comment">// form the outgoing message</span>
  1193. <a name="l01153"></a>01153 resp-&gt;<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a7cc96e6cbb87582e56f298eb891c43e2">msgType</a> = <a class="code" href="stun_8h.html#af0396ed8e86ae295115e3c2d42a7f1ad">BindResponseMsg</a>;
  1194. <a name="l01154"></a>01154 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i&lt;16; i++ )
  1195. <a name="l01155"></a>01155 {
  1196. <a name="l01156"></a>01156 resp-&gt;<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[i] = req.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[i];
  1197. <a name="l01157"></a>01157 }
  1198. <a name="l01158"></a>01158
  1199. <a name="l01159"></a>01159 <span class="keywordflow">if</span> ( req.<a class="code" href="structStunMessage.html#ad2d3b64955956272841f656fe354aa18">xorOnly</a> == <span class="keyword">false</span> )
  1200. <a name="l01160"></a>01160 {
  1201. <a name="l01161"></a>01161 resp-&gt;<a class="code" href="structStunMessage.html#a605889708c9696c60f1284b3c486ca18">hasMappedAddress</a> = <span class="keyword">true</span>;
  1202. <a name="l01162"></a>01162 resp-&gt;<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = mapped.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  1203. <a name="l01163"></a>01163 resp-&gt;<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = mapped.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  1204. <a name="l01164"></a>01164 }
  1205. <a name="l01165"></a>01165
  1206. <a name="l01166"></a>01166 <span class="keywordflow">if</span> (1) <span class="comment">// do xorMapped address or not</span>
  1207. <a name="l01167"></a>01167 {
  1208. <a name="l01168"></a>01168 resp-&gt;<a class="code" href="structStunMessage.html#a8e149870681eebee0a55b0f630c89471">hasXorMappedAddress</a> = <span class="keyword">true</span>;
  1209. <a name="l01169"></a>01169 <a class="code" href="stun_8h.html#accc14a7c843716dfed734f3a03e8d2b6">UInt16</a> id16 = req.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[0]&lt;&lt;8
  1210. <a name="l01170"></a>01170 | req.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[1];
  1211. <a name="l01171"></a>01171 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a> id32 = req.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[0]&lt;&lt;24
  1212. <a name="l01172"></a>01172 | req.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[1]&lt;&lt;16
  1213. <a name="l01173"></a>01173 | req.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[2]&lt;&lt;8
  1214. <a name="l01174"></a>01174 | req.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[3];
  1215. <a name="l01175"></a>01175 resp-&gt;<a class="code" href="structStunMessage.html#a2ef5db0ff5bd395800262b8b9d49b6bb">xorMappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = mapped.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>^id16;
  1216. <a name="l01176"></a>01176 resp-&gt;<a class="code" href="structStunMessage.html#a2ef5db0ff5bd395800262b8b9d49b6bb">xorMappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = mapped.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>^id32;
  1217. <a name="l01177"></a>01177 }
  1218. <a name="l01178"></a>01178
  1219. <a name="l01179"></a>01179 resp-&gt;<a class="code" href="structStunMessage.html#a5feb7431931118218d78642c4a7c83db">hasSourceAddress</a> = <span class="keyword">true</span>;
  1220. <a name="l01180"></a>01180 resp-&gt;<a class="code" href="structStunMessage.html#ab7203fc2e5a8b1248c9e14ce6e6ec313">sourceAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = (*changePort) ? altAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> : myAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  1221. <a name="l01181"></a>01181 resp-&gt;<a class="code" href="structStunMessage.html#ab7203fc2e5a8b1248c9e14ce6e6ec313">sourceAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = (*changeIp) ? altAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> : myAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  1222. <a name="l01182"></a>01182
  1223. <a name="l01183"></a>01183 resp-&gt;<a class="code" href="structStunMessage.html#a4d3036e012cce04e6be670898f4e24eb">hasChangedAddress</a> = <span class="keyword">true</span>;
  1224. <a name="l01184"></a>01184 resp-&gt;<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = altAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  1225. <a name="l01185"></a>01185 resp-&gt;<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = altAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  1226. <a name="l01186"></a>01186
  1227. <a name="l01187"></a>01187 <span class="keywordflow">if</span> ( secondary.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 )
  1228. <a name="l01188"></a>01188 {
  1229. <a name="l01189"></a>01189 resp-&gt;<a class="code" href="structStunMessage.html#ace894f36510487ec8151609513974eba">hasSecondaryAddress</a> = <span class="keyword">true</span>;
  1230. <a name="l01190"></a>01190 resp-&gt;<a class="code" href="structStunMessage.html#a8fc16bd394228aa6fa6e1f5f98c356c1">secondaryAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = secondary.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  1231. <a name="l01191"></a>01191 resp-&gt;<a class="code" href="structStunMessage.html#a8fc16bd394228aa6fa6e1f5f98c356c1">secondaryAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = secondary.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  1232. <a name="l01192"></a>01192 }
  1233. <a name="l01193"></a>01193
  1234. <a name="l01194"></a>01194 <span class="keywordflow">if</span> ( req.<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a> &amp;&amp; req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> &gt; 0 )
  1235. <a name="l01195"></a>01195 {
  1236. <a name="l01196"></a>01196 <span class="comment">// copy username in</span>
  1237. <a name="l01197"></a>01197 resp-&gt;<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a> = <span class="keyword">true</span>;
  1238. <a name="l01198"></a>01198 assert( req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> % 4 == 0 );
  1239. <a name="l01199"></a>01199 assert( req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> &lt; <a class="code" href="stun_8h.html#ac4d86797b42c1696b7acbd715481c0b1">STUN_MAX_STRING</a> );
  1240. <a name="l01200"></a>01200 memcpy( resp-&gt;<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>, req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>, req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> );
  1241. <a name="l01201"></a>01201 resp-&gt;<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a>;
  1242. <a name="l01202"></a>01202 }
  1243. <a name="l01203"></a>01203
  1244. <a name="l01204"></a>01204 <span class="keywordflow">if</span> (1) <span class="comment">// add ServerName</span>
  1245. <a name="l01205"></a>01205 {
  1246. <a name="l01206"></a>01206 resp-&gt;<a class="code" href="structStunMessage.html#afcf544b5b0663edad215525b8bcd67df">hasServerName</a> = <span class="keyword">true</span>;
  1247. <a name="l01207"></a>01207 <span class="keyword">const</span> <span class="keywordtype">char</span> serverName[] = <span class="stringliteral">&quot;Vovida.org &quot;</span> STUN_VERSION; <span class="comment">// must pad to mult of 4</span>
  1248. <a name="l01208"></a>01208
  1249. <a name="l01209"></a>01209 assert( <span class="keyword">sizeof</span>(serverName) &lt; <a class="code" href="stun_8h.html#ac4d86797b42c1696b7acbd715481c0b1">STUN_MAX_STRING</a> );
  1250. <a name="l01210"></a>01210 <span class="comment">//cerr &lt;&lt; &quot;sizeof serverName is &quot; &lt;&lt; sizeof(serverName) &lt;&lt; endl;</span>
  1251. <a name="l01211"></a>01211 assert( <span class="keyword">sizeof</span>(serverName)%4 == 0 );
  1252. <a name="l01212"></a>01212 memcpy( resp-&gt;<a class="code" href="structStunMessage.html#a1998c160a79020e146957c14ae1add11">serverName</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>, serverName, <span class="keyword">sizeof</span>(serverName));
  1253. <a name="l01213"></a>01213 resp-&gt;<a class="code" href="structStunMessage.html#a1998c160a79020e146957c14ae1add11">serverName</a>.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = <span class="keyword">sizeof</span>(serverName);
  1254. <a name="l01214"></a>01214 }
  1255. <a name="l01215"></a>01215
  1256. <a name="l01216"></a>01216 <span class="keywordflow">if</span> ( req.<a class="code" href="structStunMessage.html#a2a47413181e29d0de7063d4bfa6609ff">hasMessageIntegrity</a> &amp; req.<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a> )
  1257. <a name="l01217"></a>01217 {
  1258. <a name="l01218"></a>01218 <span class="comment">// this creates the password that will be used in the HMAC when then</span>
  1259. <a name="l01219"></a>01219 <span class="comment">// messages is sent</span>
  1260. <a name="l01220"></a>01220 <a class="code" href="stun_8cc.html#aa8926aedab40d08f5cff31d759415962">stunCreatePassword</a>( req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>, hmacPassword );
  1261. <a name="l01221"></a>01221 }
  1262. <a name="l01222"></a>01222
  1263. <a name="l01223"></a>01223 <span class="keywordflow">if</span> (req.<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a> &amp;&amp; (req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> &gt; 64 ) )
  1264. <a name="l01224"></a>01224 {
  1265. <a name="l01225"></a>01225 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a> source;
  1266. <a name="l01226"></a>01226 assert( <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>) == <span class="keyword">sizeof</span>(<a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a>) );
  1267. <a name="l01227"></a>01227
  1268. <a name="l01228"></a>01228 sscanf(req.<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a>.<a class="code" href="structStunAtrString.html#a609965b0f07f831423bda6aaa1b852dc">value</a>, <span class="stringliteral">&quot;%x&quot;</span>, &amp;source);
  1269. <a name="l01229"></a>01229 resp-&gt;<a class="code" href="structStunMessage.html#ab88124a21ca8562a44a696ed4b39fa4f">hasReflectedFrom</a> = <span class="keyword">true</span>;
  1270. <a name="l01230"></a>01230 resp-&gt;<a class="code" href="structStunMessage.html#ab80a59189b1b938698339f788417f32b">reflectedFrom</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = 0;
  1271. <a name="l01231"></a>01231 resp-&gt;<a class="code" href="structStunMessage.html#ab80a59189b1b938698339f788417f32b">reflectedFrom</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = source;
  1272. <a name="l01232"></a>01232 }
  1273. <a name="l01233"></a>01233
  1274. <a name="l01234"></a>01234 destination-&gt;<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = respondTo.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  1275. <a name="l01235"></a>01235 destination-&gt;<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = respondTo.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  1276. <a name="l01236"></a>01236
  1277. <a name="l01237"></a>01237 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1278. <a name="l01238"></a>01238
  1279. <a name="l01239"></a>01239 <span class="keywordflow">default</span>:
  1280. <a name="l01240"></a>01240 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;Unknown or unsupported request &quot;</span> &lt;&lt; endl;
  1281. <a name="l01241"></a>01241 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  1282. <a name="l01242"></a>01242 }
  1283. <a name="l01243"></a>01243
  1284. <a name="l01244"></a>01244 assert(0);
  1285. <a name="l01245"></a>01245 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  1286. <a name="l01246"></a>01246 }
  1287. <a name="l01247"></a>01247
  1288. <a name="l01248"></a>01248 <span class="keywordtype">bool</span>
  1289. <a name="l01249"></a><a class="code" href="stun_8h.html#a99639b68ee528c79e96973a2642184a4">01249</a> <a class="code" href="stun_8cc.html#a99639b68ee528c79e96973a2642184a4" title="return true if all is OK Create a media relay and do the STERN thing if startMediaPort is non-zero...">stunInitServer</a>(<a class="code" href="structStunServerInfo.html">StunServerInfo</a>&amp; info, <span class="keyword">const</span> <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; myAddr,
  1290. <a name="l01250"></a>01250 <span class="keyword">const</span> <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; altAddr, <span class="keywordtype">int</span> startMediaPort, <span class="keywordtype">bool</span> verbose )
  1291. <a name="l01251"></a>01251 {
  1292. <a name="l01252"></a>01252 assert( myAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 );
  1293. <a name="l01253"></a>01253 assert( altAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>!= 0 );
  1294. <a name="l01254"></a>01254 assert( myAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> != 0 );
  1295. <a name="l01255"></a>01255 <span class="comment">//assert( altAddr.addr != 0 );</span>
  1296. <a name="l01256"></a>01256
  1297. <a name="l01257"></a>01257 info.<a class="code" href="structStunServerInfo.html#a9abf6e6b89123dd6d7a71bba5a0b77d6">myAddr</a> = myAddr;
  1298. <a name="l01258"></a>01258 info.<a class="code" href="structStunServerInfo.html#ac6295f141e0926994747bbc198a19d55">altAddr</a> = altAddr;
  1299. <a name="l01259"></a>01259
  1300. <a name="l01260"></a>01260 info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a> = <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>;
  1301. <a name="l01261"></a>01261 info.<a class="code" href="structStunServerInfo.html#a2da8a94e9a9867e7c5d3b3e3c317120c">altPortFd</a> = <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>;
  1302. <a name="l01262"></a>01262 info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a> = <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>;
  1303. <a name="l01263"></a>01263 info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a> = <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>;
  1304. <a name="l01264"></a>01264
  1305. <a name="l01265"></a>01265 memset(info.<a class="code" href="structStunServerInfo.html#aed8568086c549986f985f02a691da173">relays</a>, 0, <span class="keyword">sizeof</span>(info.<a class="code" href="structStunServerInfo.html#aed8568086c549986f985f02a691da173">relays</a>));
  1306. <a name="l01266"></a>01266 <span class="keywordflow">if</span> (startMediaPort &gt; 0)
  1307. <a name="l01267"></a>01267 {
  1308. <a name="l01268"></a>01268 info.<a class="code" href="structStunServerInfo.html#a5dcc784d754928455caab60d6a34f73e">relay</a> = <span class="keyword">true</span>;
  1309. <a name="l01269"></a>01269
  1310. <a name="l01270"></a>01270 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;MAX_MEDIA_RELAYS; ++i)
  1311. <a name="l01271"></a>01271 {
  1312. <a name="l01272"></a>01272 <a class="code" href="structStunMediaRelay.html">StunMediaRelay</a>* relay = &amp;info.<a class="code" href="structStunServerInfo.html#aed8568086c549986f985f02a691da173">relays</a>[i];
  1313. <a name="l01273"></a>01273 relay-&gt;<a class="code" href="structStunMediaRelay.html#a18c86cb9d0058de5ab6b5adf449a275b">relayPort</a> = startMediaPort+i;
  1314. <a name="l01274"></a>01274 relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a> = 0;
  1315. <a name="l01275"></a>01275 relay-&gt;<a class="code" href="structStunMediaRelay.html#a16873193e181c40809e3f3f91bfb78ff">expireTime</a> = 0;
  1316. <a name="l01276"></a>01276 }
  1317. <a name="l01277"></a>01277 }
  1318. <a name="l01278"></a>01278 <span class="keywordflow">else</span>
  1319. <a name="l01279"></a>01279 {
  1320. <a name="l01280"></a>01280 info.<a class="code" href="structStunServerInfo.html#a5dcc784d754928455caab60d6a34f73e">relay</a> = <span class="keyword">false</span>;
  1321. <a name="l01281"></a>01281 }
  1322. <a name="l01282"></a>01282
  1323. <a name="l01283"></a>01283 <span class="keywordflow">if</span> ((info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a> = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>(myAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>, myAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>,verbose)) == <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>)
  1324. <a name="l01284"></a>01284 {
  1325. <a name="l01285"></a>01285 clog &lt;&lt; <span class="stringliteral">&quot;Can&#39;t open &quot;</span> &lt;&lt; myAddr &lt;&lt; endl;
  1326. <a name="l01286"></a>01286 <a class="code" href="stun_8cc.html#a9ed82a9b4b17f3f4d9bf1fd4d5b8bfd2">stunStopServer</a>(info);
  1327. <a name="l01287"></a>01287
  1328. <a name="l01288"></a>01288 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  1329. <a name="l01289"></a>01289 }
  1330. <a name="l01290"></a>01290 <span class="comment">//if (verbose) clog &lt;&lt; &quot;Opened &quot; &lt;&lt; myAddr.addr &lt;&lt; &quot;:&quot; &lt;&lt; myAddr.port &lt;&lt; &quot; --&gt; &quot; &lt;&lt; info.myFd &lt;&lt; endl;</span>
  1331. <a name="l01291"></a>01291
  1332. <a name="l01292"></a>01292 <span class="keywordflow">if</span> ((info.<a class="code" href="structStunServerInfo.html#a2da8a94e9a9867e7c5d3b3e3c317120c">altPortFd</a> = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>(altAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>,myAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>,verbose)) == <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>)
  1333. <a name="l01293"></a>01293 {
  1334. <a name="l01294"></a>01294 clog &lt;&lt; <span class="stringliteral">&quot;Can&#39;t open &quot;</span> &lt;&lt; myAddr &lt;&lt; endl;
  1335. <a name="l01295"></a>01295 <a class="code" href="stun_8cc.html#a9ed82a9b4b17f3f4d9bf1fd4d5b8bfd2">stunStopServer</a>(info);
  1336. <a name="l01296"></a>01296 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  1337. <a name="l01297"></a>01297 }
  1338. <a name="l01298"></a>01298 <span class="comment">//if (verbose) clog &lt;&lt; &quot;Opened &quot; &lt;&lt; myAddr.addr &lt;&lt; &quot;:&quot; &lt;&lt; altAddr.port &lt;&lt; &quot; --&gt; &quot; &lt;&lt; info.altPortFd &lt;&lt; endl;</span>
  1339. <a name="l01299"></a>01299
  1340. <a name="l01300"></a>01300
  1341. <a name="l01301"></a>01301 info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a> = <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>;
  1342. <a name="l01302"></a>01302 <span class="keywordflow">if</span> ( altAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> != 0 )
  1343. <a name="l01303"></a>01303 {
  1344. <a name="l01304"></a>01304 <span class="keywordflow">if</span> ((info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a> = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>( myAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>, altAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>,verbose)) == <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>)
  1345. <a name="l01305"></a>01305 {
  1346. <a name="l01306"></a>01306 clog &lt;&lt; <span class="stringliteral">&quot;Can&#39;t open &quot;</span> &lt;&lt; altAddr &lt;&lt; endl;
  1347. <a name="l01307"></a>01307 <a class="code" href="stun_8cc.html#a9ed82a9b4b17f3f4d9bf1fd4d5b8bfd2">stunStopServer</a>(info);
  1348. <a name="l01308"></a>01308 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  1349. <a name="l01309"></a>01309 }
  1350. <a name="l01310"></a>01310 <span class="comment">//if (verbose) clog &lt;&lt; &quot;Opened &quot; &lt;&lt; altAddr.addr &lt;&lt; &quot;:&quot; &lt;&lt; myAddr.port &lt;&lt; &quot; --&gt; &quot; &lt;&lt; info.altIpFd &lt;&lt; endl;;</span>
  1351. <a name="l01311"></a>01311 }
  1352. <a name="l01312"></a>01312
  1353. <a name="l01313"></a>01313 info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a> = <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>;
  1354. <a name="l01314"></a>01314 <span class="keywordflow">if</span> ( altAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> != 0 )
  1355. <a name="l01315"></a>01315 { <span class="keywordflow">if</span> ((info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a> = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>(altAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>, altAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>,verbose)) == <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>)
  1356. <a name="l01316"></a>01316 {
  1357. <a name="l01317"></a>01317 clog &lt;&lt; <span class="stringliteral">&quot;Can&#39;t open &quot;</span> &lt;&lt; altAddr &lt;&lt; endl;
  1358. <a name="l01318"></a>01318 <a class="code" href="stun_8cc.html#a9ed82a9b4b17f3f4d9bf1fd4d5b8bfd2">stunStopServer</a>(info);
  1359. <a name="l01319"></a>01319 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  1360. <a name="l01320"></a>01320 }
  1361. <a name="l01321"></a>01321 <span class="comment">//if (verbose) clog &lt;&lt; &quot;Opened &quot; &lt;&lt; altAddr.addr &lt;&lt; &quot;:&quot; &lt;&lt; altAddr.port &lt;&lt; &quot; --&gt; &quot; &lt;&lt; info.altIpPortFd &lt;&lt; endl;;</span>
  1362. <a name="l01322"></a>01322 }
  1363. <a name="l01323"></a>01323
  1364. <a name="l01324"></a>01324 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1365. <a name="l01325"></a>01325 }
  1366. <a name="l01326"></a>01326
  1367. <a name="l01327"></a>01327 <span class="keywordtype">void</span>
  1368. <a name="l01328"></a><a class="code" href="stun_8h.html#a9ed82a9b4b17f3f4d9bf1fd4d5b8bfd2">01328</a> <a class="code" href="stun_8cc.html#a9ed82a9b4b17f3f4d9bf1fd4d5b8bfd2">stunStopServer</a>(<a class="code" href="structStunServerInfo.html">StunServerInfo</a>&amp; info)
  1369. <a name="l01329"></a>01329 {
  1370. <a name="l01330"></a>01330 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a> &gt; 0) <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>(info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a>);
  1371. <a name="l01331"></a>01331 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#a2da8a94e9a9867e7c5d3b3e3c317120c">altPortFd</a> &gt; 0) <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>(info.<a class="code" href="structStunServerInfo.html#a2da8a94e9a9867e7c5d3b3e3c317120c">altPortFd</a>);
  1372. <a name="l01332"></a>01332 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a> &gt; 0) <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>(info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>);
  1373. <a name="l01333"></a>01333 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a> &gt; 0) <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>(info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>);
  1374. <a name="l01334"></a>01334
  1375. <a name="l01335"></a>01335 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#a5dcc784d754928455caab60d6a34f73e">relay</a>)
  1376. <a name="l01336"></a>01336 {
  1377. <a name="l01337"></a>01337 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;MAX_MEDIA_RELAYS; ++i)
  1378. <a name="l01338"></a>01338 {
  1379. <a name="l01339"></a>01339 <a class="code" href="structStunMediaRelay.html">StunMediaRelay</a>* relay = &amp;info.<a class="code" href="structStunServerInfo.html#aed8568086c549986f985f02a691da173">relays</a>[i];
  1380. <a name="l01340"></a>01340 <span class="keywordflow">if</span> (relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a>)
  1381. <a name="l01341"></a>01341 {
  1382. <a name="l01342"></a>01342 <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>(relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a>);
  1383. <a name="l01343"></a>01343 relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a> = 0;
  1384. <a name="l01344"></a>01344 }
  1385. <a name="l01345"></a>01345 }
  1386. <a name="l01346"></a>01346 }
  1387. <a name="l01347"></a>01347 }
  1388. <a name="l01348"></a>01348
  1389. <a name="l01349"></a>01349
  1390. <a name="l01350"></a>01350 <span class="keywordtype">bool</span>
  1391. <a name="l01351"></a><a class="code" href="stun_8h.html#a9c0e750bd19cdfcf4b2b36f9ddefc497">01351</a> <a class="code" href="stun_8cc.html#a9c0e750bd19cdfcf4b2b36f9ddefc497" title="return true if all is OK">stunServerProcess</a>(<a class="code" href="structStunServerInfo.html">StunServerInfo</a>&amp; info, <span class="keywordtype">bool</span> verbose)
  1392. <a name="l01352"></a>01352 {
  1393. <a name="l01353"></a>01353 <span class="keywordtype">char</span> msg[STUN_MAX_MESSAGE_SIZE];
  1394. <a name="l01354"></a>01354 <span class="keywordtype">int</span> msgLen = <span class="keyword">sizeof</span>(msg);
  1395. <a name="l01355"></a>01355
  1396. <a name="l01356"></a>01356 <span class="keywordtype">bool</span> ok = <span class="keyword">false</span>;
  1397. <a name="l01357"></a>01357 <span class="keywordtype">bool</span> recvAltIp =<span class="keyword">false</span>;
  1398. <a name="l01358"></a>01358 <span class="keywordtype">bool</span> recvAltPort = <span class="keyword">false</span>;
  1399. <a name="l01359"></a>01359
  1400. <a name="l01360"></a>01360 fd_set fdSet;
  1401. <a name="l01361"></a>01361 <a class="code" href="stun_8h.html#af9381320a2640ea84038579d8f143c01">Socket</a> maxFd=0;
  1402. <a name="l01362"></a>01362
  1403. <a name="l01363"></a>01363 FD_ZERO(&amp;fdSet);
  1404. <a name="l01364"></a>01364 FD_SET(info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a>,&amp;fdSet);
  1405. <a name="l01365"></a>01365 <span class="keywordflow">if</span> ( info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a> &gt;= maxFd ) maxFd=info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a>+1;
  1406. <a name="l01366"></a>01366 FD_SET(info.<a class="code" href="structStunServerInfo.html#a2da8a94e9a9867e7c5d3b3e3c317120c">altPortFd</a>,&amp;fdSet);
  1407. <a name="l01367"></a>01367 <span class="keywordflow">if</span> ( info.<a class="code" href="structStunServerInfo.html#a2da8a94e9a9867e7c5d3b3e3c317120c">altPortFd</a> &gt;= maxFd ) maxFd=info.<a class="code" href="structStunServerInfo.html#a2da8a94e9a9867e7c5d3b3e3c317120c">altPortFd</a>+1;
  1408. <a name="l01368"></a>01368
  1409. <a name="l01369"></a>01369 <span class="keywordflow">if</span> ( info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a> != <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a> )
  1410. <a name="l01370"></a>01370 {
  1411. <a name="l01371"></a>01371 FD_SET(info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>,&amp;fdSet);
  1412. <a name="l01372"></a>01372 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>&gt;=maxFd) maxFd=info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>+1;
  1413. <a name="l01373"></a>01373 }
  1414. <a name="l01374"></a>01374 <span class="keywordflow">if</span> ( info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a> != <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a> )
  1415. <a name="l01375"></a>01375 {
  1416. <a name="l01376"></a>01376 FD_SET(info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>,&amp;fdSet);
  1417. <a name="l01377"></a>01377 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>&gt;=maxFd) maxFd=info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>+1;
  1418. <a name="l01378"></a>01378 }
  1419. <a name="l01379"></a>01379
  1420. <a name="l01380"></a>01380 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#a5dcc784d754928455caab60d6a34f73e">relay</a>)
  1421. <a name="l01381"></a>01381 {
  1422. <a name="l01382"></a>01382 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;MAX_MEDIA_RELAYS; ++i)
  1423. <a name="l01383"></a>01383 {
  1424. <a name="l01384"></a>01384 <a class="code" href="structStunMediaRelay.html">StunMediaRelay</a>* relay = &amp;info.<a class="code" href="structStunServerInfo.html#aed8568086c549986f985f02a691da173">relays</a>[i];
  1425. <a name="l01385"></a>01385 <span class="keywordflow">if</span> (relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a>)
  1426. <a name="l01386"></a>01386 {
  1427. <a name="l01387"></a>01387 FD_SET(relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a>, &amp;fdSet);
  1428. <a name="l01388"></a>01388 <span class="keywordflow">if</span> (relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a> &gt;= maxFd)
  1429. <a name="l01389"></a>01389 {
  1430. <a name="l01390"></a>01390 maxFd=relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a>+1;
  1431. <a name="l01391"></a>01391 }
  1432. <a name="l01392"></a>01392 }
  1433. <a name="l01393"></a>01393 }
  1434. <a name="l01394"></a>01394 }
  1435. <a name="l01395"></a>01395
  1436. <a name="l01396"></a>01396 <span class="keywordflow">if</span> ( info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a> != <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a> )
  1437. <a name="l01397"></a>01397 {
  1438. <a name="l01398"></a>01398 FD_SET(info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>,&amp;fdSet);
  1439. <a name="l01399"></a>01399 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>&gt;=maxFd) maxFd=info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>+1;
  1440. <a name="l01400"></a>01400 }
  1441. <a name="l01401"></a>01401 <span class="keywordflow">if</span> ( info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a> != <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a> )
  1442. <a name="l01402"></a>01402 {
  1443. <a name="l01403"></a>01403 FD_SET(info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>,&amp;fdSet);
  1444. <a name="l01404"></a>01404 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>&gt;=maxFd) maxFd=info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>+1;
  1445. <a name="l01405"></a>01405 }
  1446. <a name="l01406"></a>01406
  1447. <a name="l01407"></a>01407 <span class="keyword">struct </span>timeval tv;
  1448. <a name="l01408"></a>01408 tv.tv_sec = 0;
  1449. <a name="l01409"></a>01409 tv.tv_usec = 1000;
  1450. <a name="l01410"></a>01410
  1451. <a name="l01411"></a>01411 <span class="keywordtype">int</span> e = select( maxFd, &amp;fdSet, NULL,NULL, &amp;tv );
  1452. <a name="l01412"></a>01412 <span class="keywordflow">if</span> (e &lt; 0)
  1453. <a name="l01413"></a>01413 {
  1454. <a name="l01414"></a>01414 <span class="keywordtype">int</span> err = <a class="code" href="stun__udp_8h.html#a604d53fe9d66ce3a47ab2354b20f8e62">getErrno</a>();
  1455. <a name="l01415"></a>01415 clog &lt;&lt; <span class="stringliteral">&quot;Error on select: &quot;</span> &lt;&lt; strerror(err) &lt;&lt; endl;
  1456. <a name="l01416"></a>01416 }
  1457. <a name="l01417"></a>01417 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (e &gt;= 0)
  1458. <a name="l01418"></a>01418 {
  1459. <a name="l01419"></a>01419 <a class="code" href="structStunAddress4.html">StunAddress4</a> from;
  1460. <a name="l01420"></a>01420
  1461. <a name="l01421"></a>01421 <span class="comment">// do the media relaying</span>
  1462. <a name="l01422"></a>01422 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#a5dcc784d754928455caab60d6a34f73e">relay</a>)
  1463. <a name="l01423"></a>01423 {
  1464. <a name="l01424"></a>01424 time_t now = time(0);
  1465. <a name="l01425"></a>01425 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;MAX_MEDIA_RELAYS; ++i)
  1466. <a name="l01426"></a>01426 {
  1467. <a name="l01427"></a>01427 <a class="code" href="structStunMediaRelay.html">StunMediaRelay</a>* relay = &amp;info.<a class="code" href="structStunServerInfo.html#aed8568086c549986f985f02a691da173">relays</a>[i];
  1468. <a name="l01428"></a>01428 <span class="keywordflow">if</span> (relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a>)
  1469. <a name="l01429"></a>01429 {
  1470. <a name="l01430"></a>01430 <span class="keywordflow">if</span> (FD_ISSET(relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a>, &amp;fdSet))
  1471. <a name="l01431"></a>01431 {
  1472. <a name="l01432"></a>01432 <span class="keywordtype">char</span> msg[MAX_RTP_MSG_SIZE];
  1473. <a name="l01433"></a>01433 <span class="keywordtype">int</span> msgLen = <span class="keyword">sizeof</span>(msg);
  1474. <a name="l01434"></a>01434
  1475. <a name="l01435"></a>01435 <a class="code" href="structStunAddress4.html">StunAddress4</a> rtpFrom;
  1476. <a name="l01436"></a>01436 ok = <a class="code" href="stun__udp_8h.html#a6eef3fdbadd5e5a9de73b7be46ce5aa2" title="recive a UDP message">getMessage</a>( relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a>, msg, &amp;msgLen, &amp;rtpFrom.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, &amp;rtpFrom.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> ,verbose);
  1477. <a name="l01437"></a>01437 <span class="keywordflow">if</span> (ok)
  1478. <a name="l01438"></a>01438 {
  1479. <a name="l01439"></a>01439 <a class="code" href="stun__udp_8h.html#a784917c5dceb731fee666978078b8bc5" title="send a UDP message">sendMessage</a>(info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a>, msg, msgLen, relay-&gt;<a class="code" href="structStunMediaRelay.html#a3adead8f4ff0c1b71bee2aecfa88f641">destination</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, relay-&gt;<a class="code" href="structStunMediaRelay.html#a3adead8f4ff0c1b71bee2aecfa88f641">destination</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>, verbose);
  1480. <a name="l01440"></a>01440 relay-&gt;<a class="code" href="structStunMediaRelay.html#a16873193e181c40809e3f3f91bfb78ff">expireTime</a> = now + MEDIA_RELAY_TIMEOUT;
  1481. <a name="l01441"></a>01441 <span class="keywordflow">if</span> ( verbose ) clog &lt;&lt; <span class="stringliteral">&quot;Relay packet on &quot;</span>
  1482. <a name="l01442"></a>01442 &lt;&lt; relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a>
  1483. <a name="l01443"></a>01443 &lt;&lt; <span class="stringliteral">&quot; from &quot;</span> &lt;&lt; rtpFrom
  1484. <a name="l01444"></a>01444 &lt;&lt; <span class="stringliteral">&quot; -&gt; &quot;</span> &lt;&lt; relay-&gt;<a class="code" href="structStunMediaRelay.html#a3adead8f4ff0c1b71bee2aecfa88f641">destination</a>
  1485. <a name="l01445"></a>01445 &lt;&lt; endl;
  1486. <a name="l01446"></a>01446 }
  1487. <a name="l01447"></a>01447 }
  1488. <a name="l01448"></a>01448 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (now &gt; relay-&gt;<a class="code" href="structStunMediaRelay.html#a16873193e181c40809e3f3f91bfb78ff">expireTime</a>)
  1489. <a name="l01449"></a>01449 {
  1490. <a name="l01450"></a>01450 <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>(relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a>);
  1491. <a name="l01451"></a>01451 relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a> = 0;
  1492. <a name="l01452"></a>01452 }
  1493. <a name="l01453"></a>01453 }
  1494. <a name="l01454"></a>01454 }
  1495. <a name="l01455"></a>01455 }
  1496. <a name="l01456"></a>01456
  1497. <a name="l01457"></a>01457
  1498. <a name="l01458"></a>01458 <span class="keywordflow">if</span> (FD_ISSET(info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a>,&amp;fdSet))
  1499. <a name="l01459"></a>01459 {
  1500. <a name="l01460"></a>01460 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;received on A1:P1&quot;</span> &lt;&lt; endl;
  1501. <a name="l01461"></a>01461 recvAltIp = <span class="keyword">false</span>;
  1502. <a name="l01462"></a>01462 recvAltPort = <span class="keyword">false</span>;
  1503. <a name="l01463"></a>01463 ok = <a class="code" href="stun__udp_8h.html#a6eef3fdbadd5e5a9de73b7be46ce5aa2" title="recive a UDP message">getMessage</a>( info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a>, msg, &amp;msgLen, &amp;from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, &amp;from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>,verbose );
  1504. <a name="l01464"></a>01464 }
  1505. <a name="l01465"></a>01465 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (FD_ISSET(info.<a class="code" href="structStunServerInfo.html#a2da8a94e9a9867e7c5d3b3e3c317120c">altPortFd</a>, &amp;fdSet))
  1506. <a name="l01466"></a>01466 {
  1507. <a name="l01467"></a>01467 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;received on A1:P2&quot;</span> &lt;&lt; endl;
  1508. <a name="l01468"></a>01468 recvAltIp = <span class="keyword">false</span>;
  1509. <a name="l01469"></a>01469 recvAltPort = <span class="keyword">true</span>;
  1510. <a name="l01470"></a>01470 ok = <a class="code" href="stun__udp_8h.html#a6eef3fdbadd5e5a9de73b7be46ce5aa2" title="recive a UDP message">getMessage</a>( info.<a class="code" href="structStunServerInfo.html#a2da8a94e9a9867e7c5d3b3e3c317120c">altPortFd</a>, msg, &amp;msgLen, &amp;from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, &amp;from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>,verbose );
  1511. <a name="l01471"></a>01471 }
  1512. <a name="l01472"></a>01472 <span class="keywordflow">else</span> <span class="keywordflow">if</span> ( (info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>!=<a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>) &amp;&amp; FD_ISSET(info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>,&amp;fdSet))
  1513. <a name="l01473"></a>01473 {
  1514. <a name="l01474"></a>01474 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;received on A2:P1&quot;</span> &lt;&lt; endl;
  1515. <a name="l01475"></a>01475 recvAltIp = <span class="keyword">true</span>;
  1516. <a name="l01476"></a>01476 recvAltPort = <span class="keyword">false</span>;
  1517. <a name="l01477"></a>01477 ok = <a class="code" href="stun__udp_8h.html#a6eef3fdbadd5e5a9de73b7be46ce5aa2" title="recive a UDP message">getMessage</a>( info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>, msg, &amp;msgLen, &amp;from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, &amp;from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> ,verbose);
  1518. <a name="l01478"></a>01478 }
  1519. <a name="l01479"></a>01479 <span class="keywordflow">else</span> <span class="keywordflow">if</span> ( (info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>!=<a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>) &amp;&amp; FD_ISSET(info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>, &amp;fdSet))
  1520. <a name="l01480"></a>01480 {
  1521. <a name="l01481"></a>01481 <span class="keywordflow">if</span> (verbose) clog &lt;&lt; <span class="stringliteral">&quot;received on A2:P2&quot;</span> &lt;&lt; endl;
  1522. <a name="l01482"></a>01482 recvAltIp = <span class="keyword">true</span>;
  1523. <a name="l01483"></a>01483 recvAltPort = <span class="keyword">true</span>;
  1524. <a name="l01484"></a>01484 ok = <a class="code" href="stun__udp_8h.html#a6eef3fdbadd5e5a9de73b7be46ce5aa2" title="recive a UDP message">getMessage</a>( info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>, msg, &amp;msgLen, &amp;from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, &amp;from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>,verbose );
  1525. <a name="l01485"></a>01485 }
  1526. <a name="l01486"></a>01486 <span class="keywordflow">else</span>
  1527. <a name="l01487"></a>01487 {
  1528. <a name="l01488"></a>01488 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1529. <a name="l01489"></a>01489 }
  1530. <a name="l01490"></a>01490
  1531. <a name="l01491"></a>01491 <span class="keywordtype">int</span> relayPort = 0;
  1532. <a name="l01492"></a>01492 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#a5dcc784d754928455caab60d6a34f73e">relay</a>)
  1533. <a name="l01493"></a>01493 {
  1534. <a name="l01494"></a>01494 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;MAX_MEDIA_RELAYS; ++i)
  1535. <a name="l01495"></a>01495 {
  1536. <a name="l01496"></a>01496 <a class="code" href="structStunMediaRelay.html">StunMediaRelay</a>* relay = &amp;info.<a class="code" href="structStunServerInfo.html#aed8568086c549986f985f02a691da173">relays</a>[i];
  1537. <a name="l01497"></a>01497 <span class="keywordflow">if</span> (relay-&gt;<a class="code" href="structStunMediaRelay.html#a3adead8f4ff0c1b71bee2aecfa88f641">destination</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> == from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> &amp;&amp;
  1538. <a name="l01498"></a>01498 relay-&gt;<a class="code" href="structStunMediaRelay.html#a3adead8f4ff0c1b71bee2aecfa88f641">destination</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> == from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>)
  1539. <a name="l01499"></a>01499 {
  1540. <a name="l01500"></a>01500 relayPort = relay-&gt;<a class="code" href="structStunMediaRelay.html#a18c86cb9d0058de5ab6b5adf449a275b">relayPort</a>;
  1541. <a name="l01501"></a>01501 relay-&gt;<a class="code" href="structStunMediaRelay.html#a16873193e181c40809e3f3f91bfb78ff">expireTime</a> = time(0) + MEDIA_RELAY_TIMEOUT;
  1542. <a name="l01502"></a>01502 <span class="keywordflow">break</span>;
  1543. <a name="l01503"></a>01503 }
  1544. <a name="l01504"></a>01504 }
  1545. <a name="l01505"></a>01505
  1546. <a name="l01506"></a>01506 <span class="keywordflow">if</span> (relayPort == 0)
  1547. <a name="l01507"></a>01507 {
  1548. <a name="l01508"></a>01508 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0; i&lt;MAX_MEDIA_RELAYS; ++i)
  1549. <a name="l01509"></a>01509 {
  1550. <a name="l01510"></a>01510 <a class="code" href="structStunMediaRelay.html">StunMediaRelay</a>* relay = &amp;info.<a class="code" href="structStunServerInfo.html#aed8568086c549986f985f02a691da173">relays</a>[i];
  1551. <a name="l01511"></a>01511 <span class="keywordflow">if</span> (relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a> == 0)
  1552. <a name="l01512"></a>01512 {
  1553. <a name="l01513"></a>01513 <span class="keywordflow">if</span> ( verbose ) clog &lt;&lt; <span class="stringliteral">&quot;Open relay port &quot;</span> &lt;&lt; relay-&gt;<a class="code" href="structStunMediaRelay.html#a18c86cb9d0058de5ab6b5adf449a275b">relayPort</a> &lt;&lt; endl;
  1554. <a name="l01514"></a>01514
  1555. <a name="l01515"></a>01515 relay-&gt;<a class="code" href="structStunMediaRelay.html#aebc07662e0b3728286c6da02d29806e8">fd</a> = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>(relay-&gt;<a class="code" href="structStunMediaRelay.html#a18c86cb9d0058de5ab6b5adf449a275b">relayPort</a>, info.<a class="code" href="structStunServerInfo.html#a9abf6e6b89123dd6d7a71bba5a0b77d6">myAddr</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, verbose);
  1556. <a name="l01516"></a>01516 relay-&gt;<a class="code" href="structStunMediaRelay.html#a3adead8f4ff0c1b71bee2aecfa88f641">destination</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  1557. <a name="l01517"></a>01517 relay-&gt;<a class="code" href="structStunMediaRelay.html#a3adead8f4ff0c1b71bee2aecfa88f641">destination</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  1558. <a name="l01518"></a>01518 relay-&gt;<a class="code" href="structStunMediaRelay.html#a16873193e181c40809e3f3f91bfb78ff">expireTime</a> = time(0) + MEDIA_RELAY_TIMEOUT;
  1559. <a name="l01519"></a>01519 relayPort = relay-&gt;<a class="code" href="structStunMediaRelay.html#a18c86cb9d0058de5ab6b5adf449a275b">relayPort</a>;
  1560. <a name="l01520"></a>01520 <span class="keywordflow">break</span>;
  1561. <a name="l01521"></a>01521 }
  1562. <a name="l01522"></a>01522 }
  1563. <a name="l01523"></a>01523 }
  1564. <a name="l01524"></a>01524 }
  1565. <a name="l01525"></a>01525
  1566. <a name="l01526"></a>01526 <span class="keywordflow">if</span> ( !ok )
  1567. <a name="l01527"></a>01527 {
  1568. <a name="l01528"></a>01528 <span class="keywordflow">if</span> ( verbose ) clog &lt;&lt; <span class="stringliteral">&quot;Get message did not return a valid message&quot;</span> &lt;&lt;endl;
  1569. <a name="l01529"></a>01529 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1570. <a name="l01530"></a>01530 }
  1571. <a name="l01531"></a>01531
  1572. <a name="l01532"></a>01532 <span class="keywordflow">if</span> ( verbose ) clog &lt;&lt; <span class="stringliteral">&quot;Got a request (len=&quot;</span> &lt;&lt; msgLen &lt;&lt; <span class="stringliteral">&quot;) from &quot;</span> &lt;&lt; from &lt;&lt; endl;
  1573. <a name="l01533"></a>01533
  1574. <a name="l01534"></a>01534 <span class="keywordflow">if</span> ( msgLen &lt;= 0 )
  1575. <a name="l01535"></a>01535 {
  1576. <a name="l01536"></a>01536 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1577. <a name="l01537"></a>01537 }
  1578. <a name="l01538"></a>01538
  1579. <a name="l01539"></a>01539 <span class="keywordtype">bool</span> changePort = <span class="keyword">false</span>;
  1580. <a name="l01540"></a>01540 <span class="keywordtype">bool</span> changeIp = <span class="keyword">false</span>;
  1581. <a name="l01541"></a>01541
  1582. <a name="l01542"></a>01542 <a class="code" href="structStunMessage.html">StunMessage</a> resp;
  1583. <a name="l01543"></a>01543 <a class="code" href="structStunAddress4.html">StunAddress4</a> dest;
  1584. <a name="l01544"></a>01544 <a class="code" href="structStunAtrString.html">StunAtrString</a> hmacPassword;
  1585. <a name="l01545"></a>01545 hmacPassword.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = 0;
  1586. <a name="l01546"></a>01546
  1587. <a name="l01547"></a>01547 <a class="code" href="structStunAddress4.html">StunAddress4</a> secondary;
  1588. <a name="l01548"></a>01548 secondary.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = 0;
  1589. <a name="l01549"></a>01549 secondary.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = 0;
  1590. <a name="l01550"></a>01550
  1591. <a name="l01551"></a>01551 <span class="keywordflow">if</span> (info.<a class="code" href="structStunServerInfo.html#a5dcc784d754928455caab60d6a34f73e">relay</a> &amp;&amp; relayPort)
  1592. <a name="l01552"></a>01552 {
  1593. <a name="l01553"></a>01553 secondary = from;
  1594. <a name="l01554"></a>01554
  1595. <a name="l01555"></a>01555 from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = info.<a class="code" href="structStunServerInfo.html#a9abf6e6b89123dd6d7a71bba5a0b77d6">myAddr</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  1596. <a name="l01556"></a>01556 from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = relayPort;
  1597. <a name="l01557"></a>01557 }
  1598. <a name="l01558"></a>01558
  1599. <a name="l01559"></a>01559 ok = <a class="code" href="stun_8cc.html#a811b0bd40524a040d7ec6676e9635c22">stunServerProcessMsg</a>( msg, msgLen, from, secondary,
  1600. <a name="l01560"></a>01560 recvAltIp ? info.<a class="code" href="structStunServerInfo.html#ac6295f141e0926994747bbc198a19d55">altAddr</a> : info.<a class="code" href="structStunServerInfo.html#a9abf6e6b89123dd6d7a71bba5a0b77d6">myAddr</a>,
  1601. <a name="l01561"></a>01561 recvAltIp ? info.<a class="code" href="structStunServerInfo.html#a9abf6e6b89123dd6d7a71bba5a0b77d6">myAddr</a> : info.<a class="code" href="structStunServerInfo.html#ac6295f141e0926994747bbc198a19d55">altAddr</a>,
  1602. <a name="l01562"></a>01562 &amp;resp,
  1603. <a name="l01563"></a>01563 &amp;dest,
  1604. <a name="l01564"></a>01564 &amp;hmacPassword,
  1605. <a name="l01565"></a>01565 &amp;changePort,
  1606. <a name="l01566"></a>01566 &amp;changeIp,
  1607. <a name="l01567"></a>01567 verbose );
  1608. <a name="l01568"></a>01568
  1609. <a name="l01569"></a>01569 <span class="keywordflow">if</span> ( !ok )
  1610. <a name="l01570"></a>01570 {
  1611. <a name="l01571"></a>01571 <span class="keywordflow">if</span> ( verbose ) clog &lt;&lt; <span class="stringliteral">&quot;Failed to parse message&quot;</span> &lt;&lt; endl;
  1612. <a name="l01572"></a>01572 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1613. <a name="l01573"></a>01573 }
  1614. <a name="l01574"></a>01574
  1615. <a name="l01575"></a>01575 <span class="keywordtype">char</span> buf[STUN_MAX_MESSAGE_SIZE];
  1616. <a name="l01576"></a>01576 <span class="keywordtype">int</span> len = <span class="keyword">sizeof</span>(buf);
  1617. <a name="l01577"></a>01577
  1618. <a name="l01578"></a>01578 len = <a class="code" href="stun_8cc.html#a4f9e869279d41bbf1785d321b4360f61">stunEncodeMessage</a>( resp, buf, len, hmacPassword,verbose );
  1619. <a name="l01579"></a>01579
  1620. <a name="l01580"></a>01580 <span class="keywordflow">if</span> ( dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> == 0 ) ok=<span class="keyword">false</span>;
  1621. <a name="l01581"></a>01581 <span class="keywordflow">if</span> ( dest.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> == 0 ) ok=<span class="keyword">false</span>;
  1622. <a name="l01582"></a>01582
  1623. <a name="l01583"></a>01583 <span class="keywordflow">if</span> ( ok )
  1624. <a name="l01584"></a>01584 {
  1625. <a name="l01585"></a>01585 assert( dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> != 0 );
  1626. <a name="l01586"></a>01586 assert( dest.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 );
  1627. <a name="l01587"></a>01587
  1628. <a name="l01588"></a>01588 <a class="code" href="stun_8h.html#af9381320a2640ea84038579d8f143c01">Socket</a> sendFd;
  1629. <a name="l01589"></a>01589
  1630. <a name="l01590"></a>01590 <span class="keywordtype">bool</span> sendAltIp = recvAltIp; <span class="comment">// send on the received IP address</span>
  1631. <a name="l01591"></a>01591 <span class="keywordtype">bool</span> sendAltPort = recvAltPort; <span class="comment">// send on the received port</span>
  1632. <a name="l01592"></a>01592
  1633. <a name="l01593"></a>01593 <span class="keywordflow">if</span> ( changeIp ) sendAltIp = !sendAltIp; <span class="comment">// if need to change IP, then flip logic</span>
  1634. <a name="l01594"></a>01594 <span class="keywordflow">if</span> ( changePort ) sendAltPort = !sendAltPort; <span class="comment">// if need to change port, then flip logic</span>
  1635. <a name="l01595"></a>01595
  1636. <a name="l01596"></a>01596 <span class="keywordflow">if</span> ( !sendAltPort )
  1637. <a name="l01597"></a>01597 {
  1638. <a name="l01598"></a>01598 <span class="keywordflow">if</span> ( !sendAltIp )
  1639. <a name="l01599"></a>01599 {
  1640. <a name="l01600"></a>01600 sendFd = info.<a class="code" href="structStunServerInfo.html#afa85d638d48bf0598161a7e9158aba3f">myFd</a>;
  1641. <a name="l01601"></a>01601 }
  1642. <a name="l01602"></a>01602 <span class="keywordflow">else</span>
  1643. <a name="l01603"></a>01603 {
  1644. <a name="l01604"></a>01604 sendFd = info.<a class="code" href="structStunServerInfo.html#ac9e3ea9e1322857b20f4d21211d8ca99">altIpFd</a>;
  1645. <a name="l01605"></a>01605 }
  1646. <a name="l01606"></a>01606 }
  1647. <a name="l01607"></a>01607 <span class="keywordflow">else</span>
  1648. <a name="l01608"></a>01608 {
  1649. <a name="l01609"></a>01609 <span class="keywordflow">if</span> ( !sendAltIp )
  1650. <a name="l01610"></a>01610 {
  1651. <a name="l01611"></a>01611 sendFd = info.<a class="code" href="structStunServerInfo.html#a2da8a94e9a9867e7c5d3b3e3c317120c">altPortFd</a>;
  1652. <a name="l01612"></a>01612 }
  1653. <a name="l01613"></a>01613 <span class="keywordflow">else</span>
  1654. <a name="l01614"></a>01614 {
  1655. <a name="l01615"></a>01615 sendFd = info.<a class="code" href="structStunServerInfo.html#af14364029dec110da68c7ca855d91b2d">altIpPortFd</a>;
  1656. <a name="l01616"></a>01616 }
  1657. <a name="l01617"></a>01617 }
  1658. <a name="l01618"></a>01618
  1659. <a name="l01619"></a>01619 <span class="keywordflow">if</span> ( sendFd != <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a> )
  1660. <a name="l01620"></a>01620 {
  1661. <a name="l01621"></a>01621 <a class="code" href="stun__udp_8h.html#a784917c5dceb731fee666978078b8bc5" title="send a UDP message">sendMessage</a>( sendFd, buf, len, dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, dest.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>, verbose );
  1662. <a name="l01622"></a>01622 }
  1663. <a name="l01623"></a>01623 }
  1664. <a name="l01624"></a>01624 }
  1665. <a name="l01625"></a>01625
  1666. <a name="l01626"></a>01626 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  1667. <a name="l01627"></a>01627 }
  1668. <a name="l01628"></a>01628
  1669. <a name="l01629"></a>01629 <span class="keywordtype">int</span>
  1670. <a name="l01630"></a><a class="code" href="stun_8h.html#a3ff67b4ada35e49cece8ffdc87b92300">01630</a> <a class="code" href="stun_8cc.html#a7c8890aa737ed2b41734471121af77a2" title="returns number of address found - take array or addres">stunFindLocalInterfaces</a>(<a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a>* addresses,<span class="keywordtype">int</span> maxRet)
  1671. <a name="l01631"></a>01631 {
  1672. <a name="l01632"></a>01632 <span class="preprocessor">#if defined(WIN32) || defined(__sparc__)</span>
  1673. <a name="l01633"></a>01633 <span class="preprocessor"></span> <span class="keywordflow">return</span> 0;
  1674. <a name="l01634"></a>01634 <span class="preprocessor">#else</span>
  1675. <a name="l01635"></a>01635 <span class="preprocessor"></span> <span class="keyword">struct </span>ifconf ifc;
  1676. <a name="l01636"></a>01636
  1677. <a name="l01637"></a>01637 <span class="keywordtype">int</span> s = socket( AF_INET, SOCK_DGRAM, 0 );
  1678. <a name="l01638"></a>01638 <span class="keywordtype">int</span> len = 100 * <span class="keyword">sizeof</span>(<span class="keyword">struct </span>ifreq);
  1679. <a name="l01639"></a>01639
  1680. <a name="l01640"></a>01640 <span class="keywordtype">char</span> buf[ len ];
  1681. <a name="l01641"></a>01641
  1682. <a name="l01642"></a>01642 ifc.ifc_len = len;
  1683. <a name="l01643"></a>01643 ifc.ifc_buf = buf;
  1684. <a name="l01644"></a>01644
  1685. <a name="l01645"></a>01645 <span class="keywordtype">int</span> e = ioctl(s,SIOCGIFCONF,&amp;ifc);
  1686. <a name="l01646"></a>01646 <span class="keywordtype">char</span> *ptr = buf;
  1687. <a name="l01647"></a>01647 <span class="keywordtype">int</span> tl = ifc.ifc_len;
  1688. <a name="l01648"></a>01648 <span class="keywordtype">int</span> count=0;
  1689. <a name="l01649"></a>01649
  1690. <a name="l01650"></a>01650 <span class="keywordflow">while</span> ( (tl &gt; 0) &amp;&amp; ( count &lt; maxRet) )
  1691. <a name="l01651"></a>01651 {
  1692. <a name="l01652"></a>01652 <span class="keyword">struct </span>ifreq* ifr = (<span class="keyword">struct </span>ifreq *)ptr;
  1693. <a name="l01653"></a>01653
  1694. <a name="l01654"></a>01654 <span class="keywordtype">int</span> si = <span class="keyword">sizeof</span>(ifr-&gt;ifr_name) + <span class="keyword">sizeof</span>(<span class="keyword">struct</span> sockaddr);
  1695. <a name="l01655"></a>01655 tl -= si;
  1696. <a name="l01656"></a>01656 ptr += si;
  1697. <a name="l01657"></a>01657 <span class="comment">//char* name = ifr-&gt;ifr_ifrn.ifrn_name;</span>
  1698. <a name="l01658"></a>01658 <span class="comment">//cerr &lt;&lt; &quot;name = &quot; &lt;&lt; name &lt;&lt; endl;</span>
  1699. <a name="l01659"></a>01659
  1700. <a name="l01660"></a>01660 <span class="keyword">struct </span>ifreq ifr2;
  1701. <a name="l01661"></a>01661 ifr2 = *ifr;
  1702. <a name="l01662"></a>01662
  1703. <a name="l01663"></a>01663 e = ioctl(s,SIOCGIFADDR,&amp;ifr2);
  1704. <a name="l01664"></a>01664 <span class="keywordflow">if</span> ( e == -1 )
  1705. <a name="l01665"></a>01665 {
  1706. <a name="l01666"></a>01666 <span class="keywordflow">break</span>;
  1707. <a name="l01667"></a>01667 }
  1708. <a name="l01668"></a>01668
  1709. <a name="l01669"></a>01669 <span class="comment">//cerr &lt;&lt; &quot;ioctl addr e = &quot; &lt;&lt; e &lt;&lt; endl;</span>
  1710. <a name="l01670"></a>01670
  1711. <a name="l01671"></a>01671 <span class="keyword">struct </span>sockaddr a = ifr2.ifr_addr;
  1712. <a name="l01672"></a>01672 <span class="keyword">struct </span>sockaddr_in* addr = (<span class="keyword">struct </span>sockaddr_in*) &amp;a;
  1713. <a name="l01673"></a>01673
  1714. <a name="l01674"></a>01674 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a> ai = ntohl( addr-&gt;sin_addr.s_addr );
  1715. <a name="l01675"></a>01675 <span class="keywordflow">if</span> (<span class="keywordtype">int</span>((ai&gt;&gt;24)&amp;0xFF) != 127)
  1716. <a name="l01676"></a>01676 {
  1717. <a name="l01677"></a>01677 addresses[count++] = ai;
  1718. <a name="l01678"></a>01678 }
  1719. <a name="l01679"></a>01679
  1720. <a name="l01680"></a>01680 <span class="preprocessor">#if 0</span>
  1721. <a name="l01681"></a>01681 <span class="preprocessor"></span> cerr &lt;&lt; <span class="stringliteral">&quot;Detected interface &quot;</span>
  1722. <a name="l01682"></a>01682 &lt;&lt; int((ai&gt;&gt;24)&amp;0xFF) &lt;&lt; <span class="stringliteral">&quot;.&quot;</span>
  1723. <a name="l01683"></a>01683 &lt;&lt; int((ai&gt;&gt;16)&amp;0xFF) &lt;&lt; <span class="stringliteral">&quot;.&quot;</span>
  1724. <a name="l01684"></a>01684 &lt;&lt; int((ai&gt;&gt; 8)&amp;0xFF) &lt;&lt; <span class="stringliteral">&quot;.&quot;</span>
  1725. <a name="l01685"></a>01685 &lt;&lt; int((ai )&amp;0xFF) &lt;&lt; endl;
  1726. <a name="l01686"></a>01686 <span class="preprocessor">#endif</span>
  1727. <a name="l01687"></a>01687 <span class="preprocessor"></span> }
  1728. <a name="l01688"></a>01688
  1729. <a name="l01689"></a>01689 <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>(s);
  1730. <a name="l01690"></a>01690
  1731. <a name="l01691"></a>01691 <span class="keywordflow">return</span> count;
  1732. <a name="l01692"></a>01692 <span class="preprocessor">#endif</span>
  1733. <a name="l01693"></a>01693 <span class="preprocessor"></span>}
  1734. <a name="l01694"></a>01694
  1735. <a name="l01695"></a>01695
  1736. <a name="l01696"></a>01696 <span class="keywordtype">void</span>
  1737. <a name="l01697"></a><a class="code" href="stun_8h.html#a663e56a2d581f365fb4918a0f9a88a2d">01697</a> <a class="code" href="stun_8cc.html#a07bb6d28e9a67cc882184b04c227e82b">stunBuildReqSimple</a>( <a class="code" href="structStunMessage.html">StunMessage</a>* msg,
  1738. <a name="l01698"></a>01698 <span class="keyword">const</span> <a class="code" href="structStunAtrString.html">StunAtrString</a>&amp; username,
  1739. <a name="l01699"></a>01699 <span class="keywordtype">bool</span> changePort, <span class="keywordtype">bool</span> changeIp, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <span class="keywordtype">id</span> )
  1740. <a name="l01700"></a>01700 {
  1741. <a name="l01701"></a>01701 assert( msg );
  1742. <a name="l01702"></a>01702 memset( msg , 0 , <span class="keyword">sizeof</span>(*msg) );
  1743. <a name="l01703"></a>01703
  1744. <a name="l01704"></a>01704 msg-&gt;<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a7cc96e6cbb87582e56f298eb891c43e2">msgType</a> = <a class="code" href="stun_8h.html#a4dfa49ebae731be7b582f0fc7b57d429">BindRequestMsg</a>;
  1745. <a name="l01705"></a>01705
  1746. <a name="l01706"></a>01706 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i&lt;16; i=i+4 )
  1747. <a name="l01707"></a>01707 {
  1748. <a name="l01708"></a>01708 assert(i+3&lt;16);
  1749. <a name="l01709"></a>01709 <span class="keywordtype">int</span> r = <a class="code" href="stun_8cc.html#a67e399c9420f09178de194bee0bde87a">stunRand</a>();
  1750. <a name="l01710"></a>01710 msg-&gt;<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[i+0]= r&gt;&gt;0;
  1751. <a name="l01711"></a>01711 msg-&gt;<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[i+1]= r&gt;&gt;8;
  1752. <a name="l01712"></a>01712 msg-&gt;<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[i+2]= r&gt;&gt;16;
  1753. <a name="l01713"></a>01713 msg-&gt;<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[i+3]= r&gt;&gt;24;
  1754. <a name="l01714"></a>01714 }
  1755. <a name="l01715"></a>01715
  1756. <a name="l01716"></a>01716 <span class="keywordflow">if</span> ( <span class="keywordtype">id</span> != 0 )
  1757. <a name="l01717"></a>01717 {
  1758. <a name="l01718"></a>01718 msg-&gt;<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[0] = id;
  1759. <a name="l01719"></a>01719 }
  1760. <a name="l01720"></a>01720
  1761. <a name="l01721"></a>01721 msg-&gt;<a class="code" href="structStunMessage.html#a0fe2488f060fe65507c9a58bbb66899f">hasChangeRequest</a> = <span class="keyword">true</span>;
  1762. <a name="l01722"></a>01722 msg-&gt;<a class="code" href="structStunMessage.html#a65e5de40376764ea12d96f5b5ebe2c29">changeRequest</a>.<a class="code" href="structStunAtrChangeRequest.html#adbb006a8e7cc0637d01959c8ba985c0c">value</a> =(changeIp?<a class="code" href="stun_8h.html#ad0235d173b2af6e92be9843aaa585eef">ChangeIpFlag</a>:0) |
  1763. <a name="l01723"></a>01723 (changePort?<a class="code" href="stun_8h.html#a2f247c137dd06ef8971319b9a6078ef4">ChangePortFlag</a>:0);
  1764. <a name="l01724"></a>01724
  1765. <a name="l01725"></a>01725 <span class="keywordflow">if</span> ( username.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> &gt; 0 )
  1766. <a name="l01726"></a>01726 {
  1767. <a name="l01727"></a>01727 msg-&gt;<a class="code" href="structStunMessage.html#a2a22a4f1560b25fcf5247647885bab3c">hasUsername</a> = <span class="keyword">true</span>;
  1768. <a name="l01728"></a>01728 msg-&gt;<a class="code" href="structStunMessage.html#ab5c364c8f9339a6d4f314ce3819ed1fd">username</a> = username;
  1769. <a name="l01729"></a>01729 }
  1770. <a name="l01730"></a>01730 }
  1771. <a name="l01731"></a>01731
  1772. <a name="l01732"></a>01732
  1773. <a name="l01733"></a>01733 <span class="keyword">static</span> <span class="keywordtype">void</span>
  1774. <a name="l01734"></a><a class="code" href="stun_8cc.html#ab17bbe96f9bef6d767fbee081ca123d7">01734</a> <a class="code" href="stun_8cc.html#ab17bbe96f9bef6d767fbee081ca123d7">stunSendTest</a>( <a class="code" href="stun_8h.html#af9381320a2640ea84038579d8f143c01">Socket</a> myFd, <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; dest,
  1775. <a name="l01735"></a>01735 <span class="keyword">const</span> <a class="code" href="structStunAtrString.html">StunAtrString</a>&amp; username, <span class="keyword">const</span> <a class="code" href="structStunAtrString.html">StunAtrString</a>&amp; password,
  1776. <a name="l01736"></a>01736 <span class="keywordtype">int</span> testNum, <span class="keywordtype">bool</span> verbose )
  1777. <a name="l01737"></a>01737 {
  1778. <a name="l01738"></a>01738 assert( dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> != 0 );
  1779. <a name="l01739"></a>01739 assert( dest.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 );
  1780. <a name="l01740"></a>01740
  1781. <a name="l01741"></a>01741 <span class="keywordtype">bool</span> changePort=<span class="keyword">false</span>;
  1782. <a name="l01742"></a>01742 <span class="keywordtype">bool</span> changeIP=<span class="keyword">false</span>;
  1783. <a name="l01743"></a>01743 <span class="keywordtype">bool</span> discard=<span class="keyword">false</span>;
  1784. <a name="l01744"></a>01744
  1785. <a name="l01745"></a>01745 <span class="keywordflow">switch</span> (testNum)
  1786. <a name="l01746"></a>01746 {
  1787. <a name="l01747"></a>01747 <span class="keywordflow">case</span> 1:
  1788. <a name="l01748"></a>01748 <span class="keywordflow">case</span> 10:
  1789. <a name="l01749"></a>01749 <span class="keywordflow">case</span> 11:
  1790. <a name="l01750"></a>01750 <span class="keywordflow">break</span>;
  1791. <a name="l01751"></a>01751 <span class="keywordflow">case</span> 2:
  1792. <a name="l01752"></a>01752 <span class="comment">//changePort=true;</span>
  1793. <a name="l01753"></a>01753 changeIP=<span class="keyword">true</span>;
  1794. <a name="l01754"></a>01754 <span class="keywordflow">break</span>;
  1795. <a name="l01755"></a>01755 <span class="keywordflow">case</span> 3:
  1796. <a name="l01756"></a>01756 changePort=<span class="keyword">true</span>;
  1797. <a name="l01757"></a>01757 <span class="keywordflow">break</span>;
  1798. <a name="l01758"></a>01758 <span class="keywordflow">case</span> 4:
  1799. <a name="l01759"></a>01759 changeIP=<span class="keyword">true</span>;
  1800. <a name="l01760"></a>01760 <span class="keywordflow">break</span>;
  1801. <a name="l01761"></a>01761 <span class="keywordflow">case</span> 5:
  1802. <a name="l01762"></a>01762 discard=<span class="keyword">true</span>;
  1803. <a name="l01763"></a>01763 <span class="keywordflow">break</span>;
  1804. <a name="l01764"></a>01764 <span class="keywordflow">default</span>:
  1805. <a name="l01765"></a>01765 cerr &lt;&lt; <span class="stringliteral">&quot;Test &quot;</span> &lt;&lt; testNum &lt;&lt;<span class="stringliteral">&quot; is unkown\n&quot;</span>;
  1806. <a name="l01766"></a>01766 assert(0);
  1807. <a name="l01767"></a>01767 }
  1808. <a name="l01768"></a>01768
  1809. <a name="l01769"></a>01769 <a class="code" href="structStunMessage.html">StunMessage</a> req;
  1810. <a name="l01770"></a>01770 memset(&amp;req, 0, <span class="keyword">sizeof</span>(<a class="code" href="structStunMessage.html">StunMessage</a>));
  1811. <a name="l01771"></a>01771
  1812. <a name="l01772"></a>01772 <a class="code" href="stun_8cc.html#a07bb6d28e9a67cc882184b04c227e82b">stunBuildReqSimple</a>( &amp;req, username,
  1813. <a name="l01773"></a>01773 changePort , changeIP ,
  1814. <a name="l01774"></a>01774 testNum );
  1815. <a name="l01775"></a>01775
  1816. <a name="l01776"></a>01776 <span class="keywordtype">char</span> buf[STUN_MAX_MESSAGE_SIZE];
  1817. <a name="l01777"></a>01777 <span class="keywordtype">int</span> len = STUN_MAX_MESSAGE_SIZE;
  1818. <a name="l01778"></a>01778
  1819. <a name="l01779"></a>01779 len = <a class="code" href="stun_8cc.html#a4f9e869279d41bbf1785d321b4360f61">stunEncodeMessage</a>( req, buf, len, password,verbose );
  1820. <a name="l01780"></a>01780
  1821. <a name="l01781"></a>01781 <span class="keywordflow">if</span> ( verbose )
  1822. <a name="l01782"></a>01782 {
  1823. <a name="l01783"></a>01783 clog &lt;&lt; <span class="stringliteral">&quot;About to send msg of len &quot;</span> &lt;&lt; len &lt;&lt; <span class="stringliteral">&quot; to &quot;</span> &lt;&lt; dest &lt;&lt; endl;
  1824. <a name="l01784"></a>01784 }
  1825. <a name="l01785"></a>01785
  1826. <a name="l01786"></a>01786 <a class="code" href="stun__udp_8h.html#a784917c5dceb731fee666978078b8bc5" title="send a UDP message">sendMessage</a>( myFd, buf, len, dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, dest.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>, verbose );
  1827. <a name="l01787"></a>01787
  1828. <a name="l01788"></a>01788 <span class="comment">// add some delay so the packets don&#39;t get sent too quickly</span>
  1829. <a name="l01789"></a>01789 <span class="preprocessor">#ifdef WIN32 // !cj! TODO - should fix this up in windows</span>
  1830. <a name="l01790"></a>01790 <span class="preprocessor"></span> clock_t now = clock();
  1831. <a name="l01791"></a>01791 assert( CLOCKS_PER_SEC == 1000 );
  1832. <a name="l01792"></a>01792 <span class="keywordflow">while</span> ( clock() &lt;= now+10 ) { };
  1833. <a name="l01793"></a>01793 <span class="preprocessor">#else</span>
  1834. <a name="l01794"></a>01794 <span class="preprocessor"></span> usleep(10*1000);
  1835. <a name="l01795"></a>01795 <span class="preprocessor">#endif</span>
  1836. <a name="l01796"></a>01796 <span class="preprocessor"></span>
  1837. <a name="l01797"></a>01797 }
  1838. <a name="l01798"></a>01798
  1839. <a name="l01799"></a>01799
  1840. <a name="l01800"></a>01800 <span class="keywordtype">void</span>
  1841. <a name="l01801"></a><a class="code" href="stun_8h.html#ae9317a5a127d1281953d961bcfbd2496">01801</a> <a class="code" href="stun_8cc.html#ae9317a5a127d1281953d961bcfbd2496">stunGetUserNameAndPassword</a>( <span class="keyword">const</span> <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; dest,
  1842. <a name="l01802"></a>01802 <a class="code" href="structStunAtrString.html">StunAtrString</a>* username,
  1843. <a name="l01803"></a>01803 <a class="code" href="structStunAtrString.html">StunAtrString</a>* password)
  1844. <a name="l01804"></a>01804 {
  1845. <a name="l01805"></a>01805 <span class="comment">// !cj! This is totally bogus - need to make TLS connection to dest and get a</span>
  1846. <a name="l01806"></a>01806 <span class="comment">// username and password to use</span>
  1847. <a name="l01807"></a>01807 <a class="code" href="stun_8cc.html#a5f442520a968eaa3f90caa8dcd40bd33">stunCreateUserName</a>(dest, username);
  1848. <a name="l01808"></a>01808 <a class="code" href="stun_8cc.html#aa8926aedab40d08f5cff31d759415962">stunCreatePassword</a>(*username, password);
  1849. <a name="l01809"></a>01809 }
  1850. <a name="l01810"></a>01810
  1851. <a name="l01811"></a>01811
  1852. <a name="l01812"></a>01812 <span class="keywordtype">void</span>
  1853. <a name="l01813"></a><a class="code" href="stun_8h.html#adb5b16792462be8778618f42d2a0d1b0">01813</a> <a class="code" href="stun_8cc.html#a3e28deadbde134f7d00a0f35e70267c6">stunTest</a>( <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; dest, <span class="keywordtype">int</span> testNum, <span class="keywordtype">bool</span> verbose, <a class="code" href="structStunAddress4.html">StunAddress4</a>* sAddr )
  1854. <a name="l01814"></a>01814 {
  1855. <a name="l01815"></a>01815 assert( dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> != 0 );
  1856. <a name="l01816"></a>01816 assert( dest.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 );
  1857. <a name="l01817"></a>01817
  1858. <a name="l01818"></a>01818 <span class="keywordtype">int</span> port = <a class="code" href="stun_8cc.html#ad4a19c401bea76c471959a32a19b777e" title="return a random number to use as a port">stunRandomPort</a>();
  1859. <a name="l01819"></a>01819 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a> interfaceIp=0;
  1860. <a name="l01820"></a>01820 <span class="keywordflow">if</span> (sAddr)
  1861. <a name="l01821"></a>01821 {
  1862. <a name="l01822"></a>01822 interfaceIp = sAddr-&gt;<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  1863. <a name="l01823"></a>01823 <span class="keywordflow">if</span> ( sAddr-&gt;<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 )
  1864. <a name="l01824"></a>01824 {
  1865. <a name="l01825"></a>01825 port = sAddr-&gt;<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  1866. <a name="l01826"></a>01826 }
  1867. <a name="l01827"></a>01827 }
  1868. <a name="l01828"></a>01828 <a class="code" href="stun_8h.html#af9381320a2640ea84038579d8f143c01">Socket</a> myFd = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>(port,interfaceIp,verbose);
  1869. <a name="l01829"></a>01829
  1870. <a name="l01830"></a>01830 <a class="code" href="structStunAtrString.html">StunAtrString</a> username;
  1871. <a name="l01831"></a>01831 <a class="code" href="structStunAtrString.html">StunAtrString</a> password;
  1872. <a name="l01832"></a>01832
  1873. <a name="l01833"></a>01833 username.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = 0;
  1874. <a name="l01834"></a>01834 password.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = 0;
  1875. <a name="l01835"></a>01835
  1876. <a name="l01836"></a>01836 <span class="preprocessor">#ifdef USE_TLS</span>
  1877. <a name="l01837"></a>01837 <span class="preprocessor"></span> <a class="code" href="stun_8cc.html#ae9317a5a127d1281953d961bcfbd2496">stunGetUserNameAndPassword</a>( dest, username, password );
  1878. <a name="l01838"></a>01838 <span class="preprocessor">#endif</span>
  1879. <a name="l01839"></a>01839 <span class="preprocessor"></span>
  1880. <a name="l01840"></a>01840 <a class="code" href="stun_8cc.html#ab17bbe96f9bef6d767fbee081ca123d7">stunSendTest</a>( myFd, dest, username, password, testNum, verbose );
  1881. <a name="l01841"></a>01841
  1882. <a name="l01842"></a>01842 <span class="keywordtype">char</span> msg[STUN_MAX_MESSAGE_SIZE];
  1883. <a name="l01843"></a>01843 <span class="keywordtype">int</span> msgLen = STUN_MAX_MESSAGE_SIZE;
  1884. <a name="l01844"></a>01844
  1885. <a name="l01845"></a>01845 <a class="code" href="structStunAddress4.html">StunAddress4</a> from;
  1886. <a name="l01846"></a>01846 <a class="code" href="stun__udp_8h.html#a6eef3fdbadd5e5a9de73b7be46ce5aa2" title="recive a UDP message">getMessage</a>( myFd,
  1887. <a name="l01847"></a>01847 msg,
  1888. <a name="l01848"></a>01848 &amp;msgLen,
  1889. <a name="l01849"></a>01849 &amp;from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>,
  1890. <a name="l01850"></a>01850 &amp;from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>,verbose );
  1891. <a name="l01851"></a>01851
  1892. <a name="l01852"></a>01852 <a class="code" href="structStunMessage.html">StunMessage</a> resp;
  1893. <a name="l01853"></a>01853 memset(&amp;resp, 0, <span class="keyword">sizeof</span>(<a class="code" href="structStunMessage.html">StunMessage</a>));
  1894. <a name="l01854"></a>01854
  1895. <a name="l01855"></a>01855 <span class="keywordflow">if</span> ( verbose ) clog &lt;&lt; <span class="stringliteral">&quot;Got a response&quot;</span> &lt;&lt; endl;
  1896. <a name="l01856"></a>01856 <span class="keywordtype">bool</span> ok = <a class="code" href="stun_8cc.html#af81c30137f26a4cb158b48aa21f4f681">stunParseMessage</a>( msg,msgLen, resp,verbose );
  1897. <a name="l01857"></a>01857
  1898. <a name="l01858"></a>01858 <span class="keywordflow">if</span> ( verbose )
  1899. <a name="l01859"></a>01859 {
  1900. <a name="l01860"></a>01860 clog &lt;&lt; <span class="stringliteral">&quot;\t ok=&quot;</span> &lt;&lt; ok &lt;&lt; endl;
  1901. <a name="l01861"></a>01861 clog &lt;&lt; <span class="stringliteral">&quot;\t id=&quot;</span> &lt;&lt; resp.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a> &lt;&lt; endl;
  1902. <a name="l01862"></a>01862 clog &lt;&lt; <span class="stringliteral">&quot;\t mappedAddr=&quot;</span> &lt;&lt; resp.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  1903. <a name="l01863"></a>01863 clog &lt;&lt; <span class="stringliteral">&quot;\t changedAddr=&quot;</span> &lt;&lt; resp.<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a> &lt;&lt; endl;
  1904. <a name="l01864"></a>01864 clog &lt;&lt; endl;
  1905. <a name="l01865"></a>01865 }
  1906. <a name="l01866"></a>01866
  1907. <a name="l01867"></a>01867 <span class="keywordflow">if</span> (sAddr)
  1908. <a name="l01868"></a>01868 {
  1909. <a name="l01869"></a>01869 sAddr-&gt;<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = resp.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  1910. <a name="l01870"></a>01870 sAddr-&gt;<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = resp.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  1911. <a name="l01871"></a>01871 }
  1912. <a name="l01872"></a>01872 }
  1913. <a name="l01873"></a>01873
  1914. <a name="l01874"></a>01874
  1915. <a name="l01875"></a>01875 <a class="code" href="stun_8h.html#a73736c0f7526e31ef6a35dcc0ebd34ce">NatType</a>
  1916. <a name="l01876"></a><a class="code" href="stun_8h.html#a69e8446b6e540c25e14538b1760d3e84">01876</a> <a class="code" href="stun_8cc.html#a1bcb91a498bf5e73ba5b150c5a698c7d">stunNatType</a>( <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; dest,
  1917. <a name="l01877"></a>01877 <span class="keywordtype">bool</span> verbose,
  1918. <a name="l01878"></a>01878 <span class="keywordtype">bool</span>* preservePort, <span class="comment">// if set, is return for if NAT preservers ports or not</span>
  1919. <a name="l01879"></a>01879 <span class="keywordtype">bool</span>* hairpin, <span class="comment">// if set, is the return for if NAT will hairpin packets</span>
  1920. <a name="l01880"></a>01880 <span class="keywordtype">int</span> port, <span class="comment">// port to use for the test, 0 to choose random port</span>
  1921. <a name="l01881"></a>01881 <a class="code" href="structStunAddress4.html">StunAddress4</a>* sAddr <span class="comment">// NIC to use</span>
  1922. <a name="l01882"></a>01882 )
  1923. <a name="l01883"></a>01883 {
  1924. <a name="l01884"></a>01884 assert( dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> != 0 );
  1925. <a name="l01885"></a>01885 assert( dest.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 );
  1926. <a name="l01886"></a>01886
  1927. <a name="l01887"></a>01887 <span class="keywordflow">if</span> ( hairpin )
  1928. <a name="l01888"></a>01888 {
  1929. <a name="l01889"></a>01889 *hairpin = <span class="keyword">false</span>;
  1930. <a name="l01890"></a>01890 }
  1931. <a name="l01891"></a>01891
  1932. <a name="l01892"></a>01892 <span class="keywordflow">if</span> ( port == 0 )
  1933. <a name="l01893"></a>01893 {
  1934. <a name="l01894"></a>01894 port = <a class="code" href="stun_8cc.html#ad4a19c401bea76c471959a32a19b777e" title="return a random number to use as a port">stunRandomPort</a>();
  1935. <a name="l01895"></a>01895 }
  1936. <a name="l01896"></a>01896 <a class="code" href="stun_8h.html#a4cd0dc7d33ac7a69204e8429ecea0f86">UInt32</a> interfaceIp=0;
  1937. <a name="l01897"></a>01897 <span class="keywordflow">if</span> (sAddr)
  1938. <a name="l01898"></a>01898 {
  1939. <a name="l01899"></a>01899 interfaceIp = sAddr-&gt;<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  1940. <a name="l01900"></a>01900 }
  1941. <a name="l01901"></a>01901 <a class="code" href="stun_8h.html#af9381320a2640ea84038579d8f143c01">Socket</a> myFd1 = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>(port,interfaceIp,verbose);
  1942. <a name="l01902"></a>01902 <a class="code" href="stun_8h.html#af9381320a2640ea84038579d8f143c01">Socket</a> myFd2 = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>(port+1,interfaceIp,verbose);
  1943. <a name="l01903"></a>01903
  1944. <a name="l01904"></a>01904 <span class="keywordflow">if</span> ( ( myFd1 == <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>) || ( myFd2 == <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>) )
  1945. <a name="l01905"></a>01905 {
  1946. <a name="l01906"></a>01906 cerr &lt;&lt; <span class="stringliteral">&quot;Some problem opening port/interface to send on&quot;</span> &lt;&lt; endl;
  1947. <a name="l01907"></a>01907 <span class="keywordflow">return</span> StunTypeFailure;
  1948. <a name="l01908"></a>01908 }
  1949. <a name="l01909"></a>01909
  1950. <a name="l01910"></a>01910 assert( myFd1 != <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a> );
  1951. <a name="l01911"></a>01911 assert( myFd2 != <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a> );
  1952. <a name="l01912"></a>01912
  1953. <a name="l01913"></a>01913 <span class="keywordtype">bool</span> respTestI=<span class="keyword">false</span>;
  1954. <a name="l01914"></a>01914 <span class="keywordtype">bool</span> isNat=<span class="keyword">true</span>;
  1955. <a name="l01915"></a>01915 <a class="code" href="structStunAddress4.html">StunAddress4</a> testIchangedAddr;
  1956. <a name="l01916"></a>01916 <a class="code" href="structStunAddress4.html">StunAddress4</a> testImappedAddr;
  1957. <a name="l01917"></a>01917 <span class="keywordtype">bool</span> respTestI2=<span class="keyword">false</span>;
  1958. <a name="l01918"></a>01918 <span class="keywordtype">bool</span> mappedIpSame = <span class="keyword">true</span>;
  1959. <a name="l01919"></a>01919 <a class="code" href="structStunAddress4.html">StunAddress4</a> testI2mappedAddr;
  1960. <a name="l01920"></a>01920 <a class="code" href="structStunAddress4.html">StunAddress4</a> testI2dest=dest;
  1961. <a name="l01921"></a>01921 <span class="keywordtype">bool</span> respTestII=<span class="keyword">false</span>;
  1962. <a name="l01922"></a>01922 <span class="keywordtype">bool</span> respTestIII=<span class="keyword">false</span>;
  1963. <a name="l01923"></a>01923
  1964. <a name="l01924"></a>01924 <span class="keywordtype">bool</span> respTestHairpin=<span class="keyword">false</span>;
  1965. <a name="l01925"></a>01925 <span class="keywordtype">bool</span> respTestPreservePort=<span class="keyword">false</span>;
  1966. <a name="l01926"></a>01926
  1967. <a name="l01927"></a>01927 memset(&amp;testImappedAddr,0,<span class="keyword">sizeof</span>(testImappedAddr));
  1968. <a name="l01928"></a>01928
  1969. <a name="l01929"></a>01929 <a class="code" href="structStunAtrString.html">StunAtrString</a> username;
  1970. <a name="l01930"></a>01930 <a class="code" href="structStunAtrString.html">StunAtrString</a> password;
  1971. <a name="l01931"></a>01931
  1972. <a name="l01932"></a>01932 username.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = 0;
  1973. <a name="l01933"></a>01933 password.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = 0;
  1974. <a name="l01934"></a>01934
  1975. <a name="l01935"></a>01935 <span class="preprocessor">#ifdef USE_TLS</span>
  1976. <a name="l01936"></a>01936 <span class="preprocessor"></span> <a class="code" href="stun_8cc.html#ae9317a5a127d1281953d961bcfbd2496">stunGetUserNameAndPassword</a>( dest, username, password );
  1977. <a name="l01937"></a>01937 <span class="preprocessor">#endif</span>
  1978. <a name="l01938"></a>01938 <span class="preprocessor"></span>
  1979. <a name="l01939"></a>01939 <span class="keywordtype">int</span> count=0;
  1980. <a name="l01940"></a>01940 <span class="keywordflow">while</span> ( count &lt; 7 )
  1981. <a name="l01941"></a>01941 {
  1982. <a name="l01942"></a>01942 <span class="keyword">struct </span>timeval tv;
  1983. <a name="l01943"></a>01943 fd_set fdSet;
  1984. <a name="l01944"></a>01944 <span class="preprocessor">#ifdef WIN32</span>
  1985. <a name="l01945"></a>01945 <span class="preprocessor"></span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> fdSetSize;
  1986. <a name="l01946"></a>01946 <span class="preprocessor">#else</span>
  1987. <a name="l01947"></a>01947 <span class="preprocessor"></span> <span class="keywordtype">int</span> fdSetSize;
  1988. <a name="l01948"></a>01948 <span class="preprocessor">#endif</span>
  1989. <a name="l01949"></a>01949 <span class="preprocessor"></span> FD_ZERO(&amp;fdSet); fdSetSize=0;
  1990. <a name="l01950"></a>01950 FD_SET(myFd1,&amp;fdSet); fdSetSize = (myFd1+1&gt;fdSetSize) ? myFd1+1 : fdSetSize;
  1991. <a name="l01951"></a>01951 FD_SET(myFd2,&amp;fdSet); fdSetSize = (myFd2+1&gt;fdSetSize) ? myFd2+1 : fdSetSize;
  1992. <a name="l01952"></a>01952 tv.tv_sec=0;
  1993. <a name="l01953"></a>01953 tv.tv_usec=150*1000; <span class="comment">// 150 ms</span>
  1994. <a name="l01954"></a>01954 <span class="keywordflow">if</span> ( count == 0 ) tv.tv_usec=0;
  1995. <a name="l01955"></a>01955
  1996. <a name="l01956"></a>01956 <span class="keywordtype">int</span> err = select(fdSetSize, &amp;fdSet, NULL, NULL, &amp;tv);
  1997. <a name="l01957"></a>01957 <span class="keywordtype">int</span> e = <a class="code" href="stun__udp_8h.html#a604d53fe9d66ce3a47ab2354b20f8e62">getErrno</a>();
  1998. <a name="l01958"></a>01958 <span class="keywordflow">if</span> ( err == <a class="code" href="stun__udp_8h.html#a5196d10051a93efa5a7e25054b48c236">STUN_SOCKET_ERROR</a> )
  1999. <a name="l01959"></a>01959 {
  2000. <a name="l01960"></a>01960 <span class="comment">// error occured</span>
  2001. <a name="l01961"></a>01961 cerr &lt;&lt; <span class="stringliteral">&quot;Error &quot;</span> &lt;&lt; e &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; strerror(e) &lt;&lt; <span class="stringliteral">&quot; in select&quot;</span> &lt;&lt; endl;
  2002. <a name="l01962"></a>01962 <span class="keywordflow">return</span> StunTypeFailure;
  2003. <a name="l01963"></a>01963 }
  2004. <a name="l01964"></a>01964 <span class="keywordflow">else</span> <span class="keywordflow">if</span> ( err == 0 )
  2005. <a name="l01965"></a>01965 {
  2006. <a name="l01966"></a>01966 <span class="comment">// timeout occured</span>
  2007. <a name="l01967"></a>01967 count++;
  2008. <a name="l01968"></a>01968
  2009. <a name="l01969"></a>01969 <span class="keywordflow">if</span> ( !respTestI )
  2010. <a name="l01970"></a>01970 {
  2011. <a name="l01971"></a>01971 <a class="code" href="stun_8cc.html#ab17bbe96f9bef6d767fbee081ca123d7">stunSendTest</a>( myFd1, dest, username, password, 1 ,verbose );
  2012. <a name="l01972"></a>01972 }
  2013. <a name="l01973"></a>01973
  2014. <a name="l01974"></a>01974 <span class="keywordflow">if</span> ( (!respTestI2) &amp;&amp; respTestI )
  2015. <a name="l01975"></a>01975 {
  2016. <a name="l01976"></a>01976 <span class="comment">// check the address to send to if valid</span>
  2017. <a name="l01977"></a>01977 <span class="keywordflow">if</span> ( ( testI2dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> != 0 ) &amp;&amp;
  2018. <a name="l01978"></a>01978 ( testI2dest.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 ) )
  2019. <a name="l01979"></a>01979 {
  2020. <a name="l01980"></a>01980 <a class="code" href="stun_8cc.html#ab17bbe96f9bef6d767fbee081ca123d7">stunSendTest</a>( myFd1, testI2dest, username, password, 10 ,verbose);
  2021. <a name="l01981"></a>01981 }
  2022. <a name="l01982"></a>01982 }
  2023. <a name="l01983"></a>01983
  2024. <a name="l01984"></a>01984 <span class="keywordflow">if</span> ( !respTestII )
  2025. <a name="l01985"></a>01985 {
  2026. <a name="l01986"></a>01986 <a class="code" href="stun_8cc.html#ab17bbe96f9bef6d767fbee081ca123d7">stunSendTest</a>( myFd2, dest, username, password, 2 ,verbose );
  2027. <a name="l01987"></a>01987 }
  2028. <a name="l01988"></a>01988
  2029. <a name="l01989"></a>01989 <span class="keywordflow">if</span> ( !respTestIII )
  2030. <a name="l01990"></a>01990 {
  2031. <a name="l01991"></a>01991 <a class="code" href="stun_8cc.html#ab17bbe96f9bef6d767fbee081ca123d7">stunSendTest</a>( myFd2, dest, username, password, 3 ,verbose );
  2032. <a name="l01992"></a>01992 }
  2033. <a name="l01993"></a>01993
  2034. <a name="l01994"></a>01994 <span class="keywordflow">if</span> ( respTestI &amp;&amp; (!respTestHairpin) )
  2035. <a name="l01995"></a>01995 {
  2036. <a name="l01996"></a>01996 <span class="keywordflow">if</span> ( ( testImappedAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> != 0 ) &amp;&amp;
  2037. <a name="l01997"></a>01997 ( testImappedAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 ) )
  2038. <a name="l01998"></a>01998 {
  2039. <a name="l01999"></a>01999 <a class="code" href="stun_8cc.html#ab17bbe96f9bef6d767fbee081ca123d7">stunSendTest</a>( myFd1, testImappedAddr, username, password, 11 ,verbose );
  2040. <a name="l02000"></a>02000 }
  2041. <a name="l02001"></a>02001 }
  2042. <a name="l02002"></a>02002 }
  2043. <a name="l02003"></a>02003 <span class="keywordflow">else</span>
  2044. <a name="l02004"></a>02004 {
  2045. <a name="l02005"></a>02005 <span class="comment">//if (verbose) clog &lt;&lt; &quot;-----------------------------------------&quot; &lt;&lt; endl;</span>
  2046. <a name="l02006"></a>02006 assert( err&gt;0 );
  2047. <a name="l02007"></a>02007 <span class="comment">// data is avialbe on some fd</span>
  2048. <a name="l02008"></a>02008
  2049. <a name="l02009"></a>02009 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i&lt;2; i++)
  2050. <a name="l02010"></a>02010 {
  2051. <a name="l02011"></a>02011 <a class="code" href="stun_8h.html#af9381320a2640ea84038579d8f143c01">Socket</a> myFd;
  2052. <a name="l02012"></a>02012 <span class="keywordflow">if</span> ( i==0 )
  2053. <a name="l02013"></a>02013 {
  2054. <a name="l02014"></a>02014 myFd=myFd1;
  2055. <a name="l02015"></a>02015 }
  2056. <a name="l02016"></a>02016 <span class="keywordflow">else</span>
  2057. <a name="l02017"></a>02017 {
  2058. <a name="l02018"></a>02018 myFd=myFd2;
  2059. <a name="l02019"></a>02019 }
  2060. <a name="l02020"></a>02020
  2061. <a name="l02021"></a>02021 <span class="keywordflow">if</span> ( myFd!=<a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a> )
  2062. <a name="l02022"></a>02022 {
  2063. <a name="l02023"></a>02023 <span class="keywordflow">if</span> ( FD_ISSET(myFd,&amp;fdSet) )
  2064. <a name="l02024"></a>02024 {
  2065. <a name="l02025"></a>02025 <span class="keywordtype">char</span> msg[STUN_MAX_MESSAGE_SIZE];
  2066. <a name="l02026"></a>02026 <span class="keywordtype">int</span> msgLen = <span class="keyword">sizeof</span>(msg);
  2067. <a name="l02027"></a>02027
  2068. <a name="l02028"></a>02028 <a class="code" href="structStunAddress4.html">StunAddress4</a> from;
  2069. <a name="l02029"></a>02029
  2070. <a name="l02030"></a>02030 <a class="code" href="stun__udp_8h.html#a6eef3fdbadd5e5a9de73b7be46ce5aa2" title="recive a UDP message">getMessage</a>( myFd,
  2071. <a name="l02031"></a>02031 msg,
  2072. <a name="l02032"></a>02032 &amp;msgLen,
  2073. <a name="l02033"></a>02033 &amp;from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>,
  2074. <a name="l02034"></a>02034 &amp;from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>,verbose );
  2075. <a name="l02035"></a>02035
  2076. <a name="l02036"></a>02036 <a class="code" href="structStunMessage.html">StunMessage</a> resp;
  2077. <a name="l02037"></a>02037 memset(&amp;resp, 0, <span class="keyword">sizeof</span>(<a class="code" href="structStunMessage.html">StunMessage</a>));
  2078. <a name="l02038"></a>02038
  2079. <a name="l02039"></a>02039 <a class="code" href="stun_8cc.html#af81c30137f26a4cb158b48aa21f4f681">stunParseMessage</a>( msg,msgLen, resp,verbose );
  2080. <a name="l02040"></a>02040
  2081. <a name="l02041"></a>02041 <span class="keywordflow">if</span> ( verbose )
  2082. <a name="l02042"></a>02042 {
  2083. <a name="l02043"></a>02043 clog &lt;&lt; <span class="stringliteral">&quot;Received message of type &quot;</span> &lt;&lt; resp.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a7cc96e6cbb87582e56f298eb891c43e2">msgType</a>
  2084. <a name="l02044"></a>02044 &lt;&lt; <span class="stringliteral">&quot; id=&quot;</span> &lt;&lt; (int)(resp.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[0]) &lt;&lt; endl;
  2085. <a name="l02045"></a>02045 }
  2086. <a name="l02046"></a>02046
  2087. <a name="l02047"></a>02047 <span class="keywordflow">switch</span>( resp.<a class="code" href="structStunMessage.html#a4928bac8dd595927e7ebab4610f316f6">msgHdr</a>.<a class="code" href="structStunMsgHdr.html#a97a1d0da450500ddc5a6fad924e50c87">id</a>.<a class="code" href="structUInt128.html#ad6062a8c4f066ea23ad6f42608eee6ea">octet</a>[0] )
  2088. <a name="l02048"></a>02048 {
  2089. <a name="l02049"></a>02049 <span class="keywordflow">case</span> 1:
  2090. <a name="l02050"></a>02050 {
  2091. <a name="l02051"></a>02051 <span class="keywordflow">if</span> ( !respTestI )
  2092. <a name="l02052"></a>02052 {
  2093. <a name="l02053"></a>02053
  2094. <a name="l02054"></a>02054 testIchangedAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = resp.<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  2095. <a name="l02055"></a>02055 testIchangedAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = resp.<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  2096. <a name="l02056"></a>02056 testImappedAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = resp.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  2097. <a name="l02057"></a>02057 testImappedAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = resp.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  2098. <a name="l02058"></a>02058
  2099. <a name="l02059"></a>02059 respTestPreservePort = ( testImappedAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> == port );
  2100. <a name="l02060"></a>02060 <span class="keywordflow">if</span> ( preservePort )
  2101. <a name="l02061"></a>02061 {
  2102. <a name="l02062"></a>02062 *preservePort = respTestPreservePort;
  2103. <a name="l02063"></a>02063 }
  2104. <a name="l02064"></a>02064
  2105. <a name="l02065"></a>02065 testI2dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = resp.<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  2106. <a name="l02066"></a>02066
  2107. <a name="l02067"></a>02067 <span class="keywordflow">if</span> (sAddr)
  2108. <a name="l02068"></a>02068 {
  2109. <a name="l02069"></a>02069 sAddr-&gt;<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = testImappedAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  2110. <a name="l02070"></a>02070 sAddr-&gt;<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = testImappedAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  2111. <a name="l02071"></a>02071 }
  2112. <a name="l02072"></a>02072
  2113. <a name="l02073"></a>02073 count = 0;
  2114. <a name="l02074"></a>02074 }
  2115. <a name="l02075"></a>02075 respTestI=<span class="keyword">true</span>;
  2116. <a name="l02076"></a>02076 }
  2117. <a name="l02077"></a>02077 <span class="keywordflow">break</span>;
  2118. <a name="l02078"></a>02078 <span class="keywordflow">case</span> 2:
  2119. <a name="l02079"></a>02079 {
  2120. <a name="l02080"></a>02080 respTestII=<span class="keyword">true</span>;
  2121. <a name="l02081"></a>02081 }
  2122. <a name="l02082"></a>02082 <span class="keywordflow">break</span>;
  2123. <a name="l02083"></a>02083 <span class="keywordflow">case</span> 3:
  2124. <a name="l02084"></a>02084 {
  2125. <a name="l02085"></a>02085 respTestIII=<span class="keyword">true</span>;
  2126. <a name="l02086"></a>02086 }
  2127. <a name="l02087"></a>02087 <span class="keywordflow">break</span>;
  2128. <a name="l02088"></a>02088 <span class="keywordflow">case</span> 10:
  2129. <a name="l02089"></a>02089 {
  2130. <a name="l02090"></a>02090 <span class="keywordflow">if</span> ( !respTestI2 )
  2131. <a name="l02091"></a>02091 {
  2132. <a name="l02092"></a>02092 testI2mappedAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> = resp.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  2133. <a name="l02093"></a>02093 testI2mappedAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> = resp.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>;
  2134. <a name="l02094"></a>02094
  2135. <a name="l02095"></a>02095 mappedIpSame = <span class="keyword">false</span>;
  2136. <a name="l02096"></a>02096 <span class="keywordflow">if</span> ( (testI2mappedAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> == testImappedAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> ) &amp;&amp;
  2137. <a name="l02097"></a>02097 (testI2mappedAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> == testImappedAddr.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> ))
  2138. <a name="l02098"></a>02098 {
  2139. <a name="l02099"></a>02099 mappedIpSame = <span class="keyword">true</span>;
  2140. <a name="l02100"></a>02100 }
  2141. <a name="l02101"></a>02101
  2142. <a name="l02102"></a>02102
  2143. <a name="l02103"></a>02103 }
  2144. <a name="l02104"></a>02104 respTestI2=<span class="keyword">true</span>;
  2145. <a name="l02105"></a>02105 }
  2146. <a name="l02106"></a>02106 <span class="keywordflow">break</span>;
  2147. <a name="l02107"></a>02107 <span class="keywordflow">case</span> 11:
  2148. <a name="l02108"></a>02108 {
  2149. <a name="l02109"></a>02109
  2150. <a name="l02110"></a>02110 <span class="keywordflow">if</span> ( hairpin )
  2151. <a name="l02111"></a>02111 {
  2152. <a name="l02112"></a>02112 *hairpin = <span class="keyword">true</span>;
  2153. <a name="l02113"></a>02113 }
  2154. <a name="l02114"></a>02114 respTestHairpin = <span class="keyword">true</span>;
  2155. <a name="l02115"></a>02115 }
  2156. <a name="l02116"></a>02116 <span class="keywordflow">break</span>;
  2157. <a name="l02117"></a>02117 }
  2158. <a name="l02118"></a>02118 }
  2159. <a name="l02119"></a>02119 }
  2160. <a name="l02120"></a>02120 }
  2161. <a name="l02121"></a>02121 }
  2162. <a name="l02122"></a>02122 }
  2163. <a name="l02123"></a>02123
  2164. <a name="l02124"></a>02124 <span class="comment">// see if we can bind to this address</span>
  2165. <a name="l02125"></a>02125 <span class="comment">//cerr &lt;&lt; &quot;try binding to &quot; &lt;&lt; testImappedAddr &lt;&lt; endl;</span>
  2166. <a name="l02126"></a>02126 <a class="code" href="stun_8h.html#af9381320a2640ea84038579d8f143c01">Socket</a> s = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>( 0<span class="comment">/*use ephemeral*/</span>, testImappedAddr.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, <span class="keyword">false</span> );
  2167. <a name="l02127"></a>02127 <span class="keywordflow">if</span> ( s != <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a> )
  2168. <a name="l02128"></a>02128 {
  2169. <a name="l02129"></a>02129 <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>(s);
  2170. <a name="l02130"></a>02130 isNat = <span class="keyword">false</span>;
  2171. <a name="l02131"></a>02131 <span class="comment">//cerr &lt;&lt; &quot;binding worked&quot; &lt;&lt; endl;</span>
  2172. <a name="l02132"></a>02132 }
  2173. <a name="l02133"></a>02133 <span class="keywordflow">else</span>
  2174. <a name="l02134"></a>02134 {
  2175. <a name="l02135"></a>02135 isNat = <span class="keyword">true</span>;
  2176. <a name="l02136"></a>02136 <span class="comment">//cerr &lt;&lt; &quot;binding failed&quot; &lt;&lt; endl;</span>
  2177. <a name="l02137"></a>02137 }
  2178. <a name="l02138"></a>02138
  2179. <a name="l02139"></a>02139 <span class="keywordflow">if</span> (verbose)
  2180. <a name="l02140"></a>02140 {
  2181. <a name="l02141"></a>02141 clog &lt;&lt; <span class="stringliteral">&quot;test I = &quot;</span> &lt;&lt; respTestI &lt;&lt; endl;
  2182. <a name="l02142"></a>02142 clog &lt;&lt; <span class="stringliteral">&quot;test II = &quot;</span> &lt;&lt; respTestII &lt;&lt; endl;
  2183. <a name="l02143"></a>02143 clog &lt;&lt; <span class="stringliteral">&quot;test III = &quot;</span> &lt;&lt; respTestIII &lt;&lt; endl;
  2184. <a name="l02144"></a>02144 clog &lt;&lt; <span class="stringliteral">&quot;test I(2) = &quot;</span> &lt;&lt; respTestI2 &lt;&lt; endl;
  2185. <a name="l02145"></a>02145 clog &lt;&lt; <span class="stringliteral">&quot;is nat = &quot;</span> &lt;&lt; isNat &lt;&lt;endl;
  2186. <a name="l02146"></a>02146 clog &lt;&lt; <span class="stringliteral">&quot;mapped IP same = &quot;</span> &lt;&lt; mappedIpSame &lt;&lt; endl;
  2187. <a name="l02147"></a>02147 clog &lt;&lt; <span class="stringliteral">&quot;hairpin = &quot;</span> &lt;&lt; respTestHairpin &lt;&lt; endl;
  2188. <a name="l02148"></a>02148 clog &lt;&lt; <span class="stringliteral">&quot;preserver port = &quot;</span> &lt;&lt; respTestPreservePort &lt;&lt; endl;
  2189. <a name="l02149"></a>02149 }
  2190. <a name="l02150"></a>02150
  2191. <a name="l02151"></a>02151 <span class="preprocessor">#if 0</span>
  2192. <a name="l02152"></a>02152 <span class="preprocessor"></span> <span class="comment">// implement logic flow chart from draft RFC</span>
  2193. <a name="l02153"></a>02153 <span class="keywordflow">if</span> ( respTestI )
  2194. <a name="l02154"></a>02154 {
  2195. <a name="l02155"></a>02155 <span class="keywordflow">if</span> ( isNat )
  2196. <a name="l02156"></a>02156 {
  2197. <a name="l02157"></a>02157 <span class="keywordflow">if</span> (respTestII)
  2198. <a name="l02158"></a>02158 {
  2199. <a name="l02159"></a>02159 <span class="keywordflow">return</span> StunTypeConeNat;
  2200. <a name="l02160"></a>02160 }
  2201. <a name="l02161"></a>02161 <span class="keywordflow">else</span>
  2202. <a name="l02162"></a>02162 {
  2203. <a name="l02163"></a>02163 <span class="keywordflow">if</span> ( mappedIpSame )
  2204. <a name="l02164"></a>02164 {
  2205. <a name="l02165"></a>02165 <span class="keywordflow">if</span> ( respTestIII )
  2206. <a name="l02166"></a>02166 {
  2207. <a name="l02167"></a>02167 <span class="keywordflow">return</span> StunTypeRestrictedNat;
  2208. <a name="l02168"></a>02168 }
  2209. <a name="l02169"></a>02169 <span class="keywordflow">else</span>
  2210. <a name="l02170"></a>02170 {
  2211. <a name="l02171"></a>02171 <span class="keywordflow">return</span> StunTypePortRestrictedNat;
  2212. <a name="l02172"></a>02172 }
  2213. <a name="l02173"></a>02173 }
  2214. <a name="l02174"></a>02174 <span class="keywordflow">else</span>
  2215. <a name="l02175"></a>02175 {
  2216. <a name="l02176"></a>02176 <span class="keywordflow">return</span> StunTypeSymNat;
  2217. <a name="l02177"></a>02177 }
  2218. <a name="l02178"></a>02178 }
  2219. <a name="l02179"></a>02179 }
  2220. <a name="l02180"></a>02180 <span class="keywordflow">else</span>
  2221. <a name="l02181"></a>02181 {
  2222. <a name="l02182"></a>02182 <span class="keywordflow">if</span> (respTestII)
  2223. <a name="l02183"></a>02183 {
  2224. <a name="l02184"></a>02184 <span class="keywordflow">return</span> StunTypeOpen;
  2225. <a name="l02185"></a>02185 }
  2226. <a name="l02186"></a>02186 <span class="keywordflow">else</span>
  2227. <a name="l02187"></a>02187 {
  2228. <a name="l02188"></a>02188 <span class="keywordflow">return</span> StunTypeSymFirewall;
  2229. <a name="l02189"></a>02189 }
  2230. <a name="l02190"></a>02190 }
  2231. <a name="l02191"></a>02191 }
  2232. <a name="l02192"></a>02192 <span class="keywordflow">else</span>
  2233. <a name="l02193"></a>02193 {
  2234. <a name="l02194"></a>02194 <span class="keywordflow">return</span> StunTypeBlocked;
  2235. <a name="l02195"></a>02195 }
  2236. <a name="l02196"></a>02196 <span class="preprocessor">#else</span>
  2237. <a name="l02197"></a>02197 <span class="preprocessor"></span> <span class="keywordflow">if</span> ( respTestI ) <span class="comment">// not blocked</span>
  2238. <a name="l02198"></a>02198 {
  2239. <a name="l02199"></a>02199 <span class="keywordflow">if</span> ( isNat )
  2240. <a name="l02200"></a>02200 {
  2241. <a name="l02201"></a>02201 <span class="keywordflow">if</span> ( mappedIpSame )
  2242. <a name="l02202"></a>02202 {
  2243. <a name="l02203"></a>02203 <span class="keywordflow">if</span> (respTestII)
  2244. <a name="l02204"></a>02204 {
  2245. <a name="l02205"></a>02205 <span class="keywordflow">return</span> StunTypeIndependentFilter;
  2246. <a name="l02206"></a>02206 }
  2247. <a name="l02207"></a>02207 <span class="keywordflow">else</span>
  2248. <a name="l02208"></a>02208 {
  2249. <a name="l02209"></a>02209 <span class="keywordflow">if</span> ( respTestIII )
  2250. <a name="l02210"></a>02210 {
  2251. <a name="l02211"></a>02211 <span class="keywordflow">return</span> StunTypeDependentFilter;
  2252. <a name="l02212"></a>02212 }
  2253. <a name="l02213"></a>02213 <span class="keywordflow">else</span>
  2254. <a name="l02214"></a>02214 {
  2255. <a name="l02215"></a>02215 <span class="keywordflow">return</span> StunTypePortDependedFilter;
  2256. <a name="l02216"></a>02216 }
  2257. <a name="l02217"></a>02217 }
  2258. <a name="l02218"></a>02218 }
  2259. <a name="l02219"></a>02219 <span class="keywordflow">else</span> <span class="comment">// mappedIp is not same</span>
  2260. <a name="l02220"></a>02220 {
  2261. <a name="l02221"></a>02221 <span class="keywordflow">return</span> StunTypeDependentMapping;
  2262. <a name="l02222"></a>02222 }
  2263. <a name="l02223"></a>02223 }
  2264. <a name="l02224"></a>02224 <span class="keywordflow">else</span> <span class="comment">// isNat is false</span>
  2265. <a name="l02225"></a>02225 {
  2266. <a name="l02226"></a>02226 <span class="keywordflow">if</span> (respTestII)
  2267. <a name="l02227"></a>02227 {
  2268. <a name="l02228"></a>02228 <span class="keywordflow">return</span> StunTypeOpen;
  2269. <a name="l02229"></a>02229 }
  2270. <a name="l02230"></a>02230 <span class="keywordflow">else</span>
  2271. <a name="l02231"></a>02231 {
  2272. <a name="l02232"></a>02232 <span class="keywordflow">return</span> StunTypeFirewall;
  2273. <a name="l02233"></a>02233 }
  2274. <a name="l02234"></a>02234 }
  2275. <a name="l02235"></a>02235 }
  2276. <a name="l02236"></a>02236 <span class="keywordflow">else</span>
  2277. <a name="l02237"></a>02237 {
  2278. <a name="l02238"></a>02238 <span class="keywordflow">return</span> StunTypeBlocked;
  2279. <a name="l02239"></a>02239 }
  2280. <a name="l02240"></a>02240 <span class="preprocessor">#endif</span>
  2281. <a name="l02241"></a>02241 <span class="preprocessor"></span>
  2282. <a name="l02242"></a>02242 <span class="keywordflow">return</span> StunTypeUnknown;
  2283. <a name="l02243"></a>02243 }
  2284. <a name="l02244"></a>02244
  2285. <a name="l02245"></a>02245
  2286. <a name="l02246"></a>02246 <span class="keywordtype">int</span>
  2287. <a name="l02247"></a><a class="code" href="stun_8h.html#a5956654577cee7e80cbd7abfb06d3c7e">02247</a> <a class="code" href="stun_8cc.html#aa31355ece8353da047608020106c0b60">stunOpenSocket</a>( <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; dest, <a class="code" href="structStunAddress4.html">StunAddress4</a>* mapAddr,
  2288. <a name="l02248"></a>02248 <span class="keywordtype">int</span> port, <a class="code" href="structStunAddress4.html">StunAddress4</a>* srcAddr,
  2289. <a name="l02249"></a>02249 <span class="keywordtype">bool</span> verbose )
  2290. <a name="l02250"></a>02250 {
  2291. <a name="l02251"></a>02251 assert( dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a> != 0 );
  2292. <a name="l02252"></a>02252 assert( dest.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 );
  2293. <a name="l02253"></a>02253 assert( mapAddr );
  2294. <a name="l02254"></a>02254
  2295. <a name="l02255"></a>02255 <span class="keywordflow">if</span> ( port == 0 )
  2296. <a name="l02256"></a>02256 {
  2297. <a name="l02257"></a>02257 port = <a class="code" href="stun_8cc.html#ad4a19c401bea76c471959a32a19b777e" title="return a random number to use as a port">stunRandomPort</a>();
  2298. <a name="l02258"></a>02258 }
  2299. <a name="l02259"></a>02259 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> interfaceIp = 0;
  2300. <a name="l02260"></a>02260 <span class="keywordflow">if</span> ( srcAddr )
  2301. <a name="l02261"></a>02261 {
  2302. <a name="l02262"></a>02262 interfaceIp = srcAddr-&gt;<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  2303. <a name="l02263"></a>02263 }
  2304. <a name="l02264"></a>02264
  2305. <a name="l02265"></a>02265 <a class="code" href="stun_8h.html#af9381320a2640ea84038579d8f143c01">Socket</a> myFd = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>(port,interfaceIp,verbose);
  2306. <a name="l02266"></a>02266 <span class="keywordflow">if</span> (myFd == <a class="code" href="stun__udp_8h.html#ab095a168e3ee3fb25930ad2f03d4242b">STUN_INVALID_SOCKET</a>)
  2307. <a name="l02267"></a>02267 {
  2308. <a name="l02268"></a>02268 <span class="keywordflow">return</span> myFd;
  2309. <a name="l02269"></a>02269 }
  2310. <a name="l02270"></a>02270
  2311. <a name="l02271"></a>02271 <span class="keywordtype">char</span> msg[STUN_MAX_MESSAGE_SIZE];
  2312. <a name="l02272"></a>02272 <span class="keywordtype">int</span> msgLen = <span class="keyword">sizeof</span>(msg);
  2313. <a name="l02273"></a>02273
  2314. <a name="l02274"></a>02274 <a class="code" href="structStunAtrString.html">StunAtrString</a> username;
  2315. <a name="l02275"></a>02275 <a class="code" href="structStunAtrString.html">StunAtrString</a> password;
  2316. <a name="l02276"></a>02276
  2317. <a name="l02277"></a>02277 username.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = 0;
  2318. <a name="l02278"></a>02278 password.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = 0;
  2319. <a name="l02279"></a>02279
  2320. <a name="l02280"></a>02280 <span class="preprocessor">#ifdef USE_TLS</span>
  2321. <a name="l02281"></a>02281 <span class="preprocessor"></span> <a class="code" href="stun_8cc.html#ae9317a5a127d1281953d961bcfbd2496">stunGetUserNameAndPassword</a>( dest, username, password );
  2322. <a name="l02282"></a>02282 <span class="preprocessor">#endif</span>
  2323. <a name="l02283"></a>02283 <span class="preprocessor"></span>
  2324. <a name="l02284"></a>02284 <a class="code" href="stun_8cc.html#ab17bbe96f9bef6d767fbee081ca123d7">stunSendTest</a>(myFd, dest, username, password, 1, 0<span class="comment">/*false*/</span> );
  2325. <a name="l02285"></a>02285
  2326. <a name="l02286"></a>02286 <a class="code" href="structStunAddress4.html">StunAddress4</a> from;
  2327. <a name="l02287"></a>02287
  2328. <a name="l02288"></a>02288 <a class="code" href="stun__udp_8h.html#a6eef3fdbadd5e5a9de73b7be46ce5aa2" title="recive a UDP message">getMessage</a>( myFd, msg, &amp;msgLen, &amp;from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>, &amp;from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a>,verbose );
  2329. <a name="l02289"></a>02289
  2330. <a name="l02290"></a>02290 <a class="code" href="structStunMessage.html">StunMessage</a> resp;
  2331. <a name="l02291"></a>02291 memset(&amp;resp, 0, <span class="keyword">sizeof</span>(<a class="code" href="structStunMessage.html">StunMessage</a>));
  2332. <a name="l02292"></a>02292
  2333. <a name="l02293"></a>02293 <span class="keywordtype">bool</span> ok = <a class="code" href="stun_8cc.html#af81c30137f26a4cb158b48aa21f4f681">stunParseMessage</a>( msg, msgLen, resp,verbose );
  2334. <a name="l02294"></a>02294 <span class="keywordflow">if</span> (!ok)
  2335. <a name="l02295"></a>02295 {
  2336. <a name="l02296"></a>02296 <span class="keywordflow">return</span> -1;
  2337. <a name="l02297"></a>02297 }
  2338. <a name="l02298"></a>02298
  2339. <a name="l02299"></a>02299 <a class="code" href="structStunAddress4.html">StunAddress4</a> mappedAddr = resp.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>;
  2340. <a name="l02300"></a>02300 <a class="code" href="structStunAddress4.html">StunAddress4</a> changedAddr = resp.<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>;
  2341. <a name="l02301"></a>02301
  2342. <a name="l02302"></a>02302 <span class="comment">//clog &lt;&lt; &quot;--- stunOpenSocket --- &quot; &lt;&lt; endl;</span>
  2343. <a name="l02303"></a>02303 <span class="comment">//clog &lt;&lt; &quot;\treq id=&quot; &lt;&lt; req.id &lt;&lt; endl;</span>
  2344. <a name="l02304"></a>02304 <span class="comment">//clog &lt;&lt; &quot;\tresp id=&quot; &lt;&lt; id &lt;&lt; endl;</span>
  2345. <a name="l02305"></a>02305 <span class="comment">//clog &lt;&lt; &quot;\tmappedAddr=&quot; &lt;&lt; mappedAddr &lt;&lt; endl;</span>
  2346. <a name="l02306"></a>02306
  2347. <a name="l02307"></a>02307 *mapAddr = mappedAddr;
  2348. <a name="l02308"></a>02308
  2349. <a name="l02309"></a>02309 <span class="keywordflow">return</span> myFd;
  2350. <a name="l02310"></a>02310 }
  2351. <a name="l02311"></a>02311
  2352. <a name="l02312"></a>02312
  2353. <a name="l02313"></a>02313 <span class="keywordtype">bool</span>
  2354. <a name="l02314"></a><a class="code" href="stun_8h.html#aa4e28e0945cd1bdbf6610c6eb87db956">02314</a> <a class="code" href="stun_8cc.html#ad45b49d1f841269cea994ca84e8d071b">stunOpenSocketPair</a>( <a class="code" href="structStunAddress4.html">StunAddress4</a>&amp; dest, <a class="code" href="structStunAddress4.html">StunAddress4</a>* mapAddr,
  2355. <a name="l02315"></a>02315 <span class="keywordtype">int</span>* fd1, <span class="keywordtype">int</span>* fd2,
  2356. <a name="l02316"></a>02316 <span class="keywordtype">int</span> port, <a class="code" href="structStunAddress4.html">StunAddress4</a>* srcAddr,
  2357. <a name="l02317"></a>02317 <span class="keywordtype">bool</span> verbose )
  2358. <a name="l02318"></a>02318 {
  2359. <a name="l02319"></a>02319 assert( dest.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>!= 0 );
  2360. <a name="l02320"></a>02320 assert( dest.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> != 0 );
  2361. <a name="l02321"></a>02321 assert( mapAddr );
  2362. <a name="l02322"></a>02322
  2363. <a name="l02323"></a>02323 <span class="keyword">const</span> <span class="keywordtype">int</span> NUM=3;
  2364. <a name="l02324"></a>02324
  2365. <a name="l02325"></a>02325 <span class="keywordflow">if</span> ( port == 0 )
  2366. <a name="l02326"></a>02326 {
  2367. <a name="l02327"></a>02327 port = <a class="code" href="stun_8cc.html#ad4a19c401bea76c471959a32a19b777e" title="return a random number to use as a port">stunRandomPort</a>();
  2368. <a name="l02328"></a>02328 }
  2369. <a name="l02329"></a>02329
  2370. <a name="l02330"></a>02330 *fd1=-1;
  2371. <a name="l02331"></a>02331 *fd2=-1;
  2372. <a name="l02332"></a>02332
  2373. <a name="l02333"></a>02333 <span class="keywordtype">char</span> msg[STUN_MAX_MESSAGE_SIZE];
  2374. <a name="l02334"></a>02334 <span class="keywordtype">int</span> msgLen =<span class="keyword">sizeof</span>(msg);
  2375. <a name="l02335"></a>02335
  2376. <a name="l02336"></a>02336 <a class="code" href="structStunAddress4.html">StunAddress4</a> from;
  2377. <a name="l02337"></a>02337 <span class="keywordtype">int</span> fd[NUM];
  2378. <a name="l02338"></a>02338 <span class="keywordtype">int</span> i;
  2379. <a name="l02339"></a>02339
  2380. <a name="l02340"></a>02340 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> interfaceIp = 0;
  2381. <a name="l02341"></a>02341 <span class="keywordflow">if</span> ( srcAddr )
  2382. <a name="l02342"></a>02342 {
  2383. <a name="l02343"></a>02343 interfaceIp = srcAddr-&gt;<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>;
  2384. <a name="l02344"></a>02344 }
  2385. <a name="l02345"></a>02345
  2386. <a name="l02346"></a>02346 <span class="keywordflow">for</span>( i=0; i&lt;NUM; i++)
  2387. <a name="l02347"></a>02347 {
  2388. <a name="l02348"></a>02348 fd[i] = <a class="code" href="stun__udp_8h.html#a44f511e14438aa519a0f367a84de4000" title="Open a UDP socket to receive on the given port - if port is 0, pick a a port, if interfaceIp!=0 then ...">openPort</a>( (port == 0) ? 0 : (port + i),
  2389. <a name="l02349"></a>02349 interfaceIp, verbose);
  2390. <a name="l02350"></a>02350 <span class="keywordflow">if</span> (fd[i] &lt; 0)
  2391. <a name="l02351"></a>02351 {
  2392. <a name="l02352"></a>02352 <span class="keywordflow">while</span> (i &gt; 0)
  2393. <a name="l02353"></a>02353 {
  2394. <a name="l02354"></a>02354 <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>(fd[--i]);
  2395. <a name="l02355"></a>02355 }
  2396. <a name="l02356"></a>02356 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  2397. <a name="l02357"></a>02357 }
  2398. <a name="l02358"></a>02358 }
  2399. <a name="l02359"></a>02359
  2400. <a name="l02360"></a>02360 <a class="code" href="structStunAtrString.html">StunAtrString</a> username;
  2401. <a name="l02361"></a>02361 <a class="code" href="structStunAtrString.html">StunAtrString</a> password;
  2402. <a name="l02362"></a>02362
  2403. <a name="l02363"></a>02363 username.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = 0;
  2404. <a name="l02364"></a>02364 password.<a class="code" href="structStunAtrString.html#aa1e288247a4ae07b170ecf956bdd48c9">sizeValue</a> = 0;
  2405. <a name="l02365"></a>02365
  2406. <a name="l02366"></a>02366 <span class="preprocessor">#ifdef USE_TLS</span>
  2407. <a name="l02367"></a>02367 <span class="preprocessor"></span> <a class="code" href="stun_8cc.html#ae9317a5a127d1281953d961bcfbd2496">stunGetUserNameAndPassword</a>( dest, username, password );
  2408. <a name="l02368"></a>02368 <span class="preprocessor">#endif</span>
  2409. <a name="l02369"></a>02369 <span class="preprocessor"></span>
  2410. <a name="l02370"></a>02370 <span class="keywordflow">for</span>( i=0; i&lt;NUM; i++)
  2411. <a name="l02371"></a>02371 {
  2412. <a name="l02372"></a>02372 <a class="code" href="stun_8cc.html#ab17bbe96f9bef6d767fbee081ca123d7">stunSendTest</a>(fd[i], dest, username, password, 1<span class="comment">/*testNum*/</span>, verbose );
  2413. <a name="l02373"></a>02373 }
  2414. <a name="l02374"></a>02374
  2415. <a name="l02375"></a>02375 <a class="code" href="structStunAddress4.html">StunAddress4</a> mappedAddr[NUM];
  2416. <a name="l02376"></a>02376 <span class="keywordflow">for</span>( i=0; i&lt;NUM; i++)
  2417. <a name="l02377"></a>02377 {
  2418. <a name="l02378"></a>02378 msgLen = <span class="keyword">sizeof</span>(msg)/<span class="keyword">sizeof</span>(*msg);
  2419. <a name="l02379"></a>02379 <a class="code" href="stun__udp_8h.html#a6eef3fdbadd5e5a9de73b7be46ce5aa2" title="recive a UDP message">getMessage</a>( fd[i],
  2420. <a name="l02380"></a>02380 msg,
  2421. <a name="l02381"></a>02381 &amp;msgLen,
  2422. <a name="l02382"></a>02382 &amp;from.<a class="code" href="structStunAddress4.html#a870846bf1b2de80c031948f0795e24c6">addr</a>,
  2423. <a name="l02383"></a>02383 &amp;from.<a class="code" href="structStunAddress4.html#a182b25091f39b6f1d9904a02bcbeb43b">port</a> ,verbose);
  2424. <a name="l02384"></a>02384
  2425. <a name="l02385"></a>02385 <a class="code" href="structStunMessage.html">StunMessage</a> resp;
  2426. <a name="l02386"></a>02386 memset(&amp;resp, 0, <span class="keyword">sizeof</span>(<a class="code" href="structStunMessage.html">StunMessage</a>));
  2427. <a name="l02387"></a>02387
  2428. <a name="l02388"></a>02388 <span class="keywordtype">bool</span> ok = <a class="code" href="stun_8cc.html#af81c30137f26a4cb158b48aa21f4f681">stunParseMessage</a>( msg, msgLen, resp, verbose );
  2429. <a name="l02389"></a>02389 <span class="keywordflow">if</span> (!ok)
  2430. <a name="l02390"></a>02390 {
  2431. <a name="l02391"></a>02391 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  2432. <a name="l02392"></a>02392 }
  2433. <a name="l02393"></a>02393
  2434. <a name="l02394"></a>02394 mappedAddr[i] = resp.<a class="code" href="structStunMessage.html#a7c354e1ca2aae9d180c98e6b12ba879c">mappedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>;
  2435. <a name="l02395"></a>02395 <a class="code" href="structStunAddress4.html">StunAddress4</a> changedAddr = resp.<a class="code" href="structStunMessage.html#ac240bd9424c010b74411679f1813a705">changedAddress</a>.<a class="code" href="structStunAtrAddress4.html#a80b81fdc81e96531c1fecd5b9b5125c4">ipv4</a>;
  2436. <a name="l02396"></a>02396 }
  2437. <a name="l02397"></a>02397
  2438. <a name="l02398"></a>02398 <span class="keywordflow">if</span> (verbose)
  2439. <a name="l02399"></a>02399 {
  2440. <a name="l02400"></a>02400 clog &lt;&lt; <span class="stringliteral">&quot;--- stunOpenSocketPair --- &quot;</span> &lt;&lt; endl;
  2441. <a name="l02401"></a>02401 <span class="keywordflow">for</span>( i=0; i&lt;NUM; i++)
  2442. <a name="l02402"></a>02402 {
  2443. <a name="l02403"></a>02403 clog &lt;&lt; <span class="stringliteral">&quot;\t mappedAddr=&quot;</span> &lt;&lt; mappedAddr[i] &lt;&lt; endl;
  2444. <a name="l02404"></a>02404 }
  2445. <a name="l02405"></a>02405 }
  2446. <a name="l02406"></a>02406
  2447. <a name="l02407"></a>02407 <span class="keywordflow">if</span> ( mappedAddr[0].port %2 == 0 )
  2448. <a name="l02408"></a>02408 {
  2449. <a name="l02409"></a>02409 <span class="keywordflow">if</span> ( mappedAddr[0].port+1 == mappedAddr[1].port )
  2450. <a name="l02410"></a>02410 {
  2451. <a name="l02411"></a>02411 *mapAddr = mappedAddr[0];
  2452. <a name="l02412"></a>02412 *fd1 = fd[0];
  2453. <a name="l02413"></a>02413 *fd2 = fd[1];
  2454. <a name="l02414"></a>02414 <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>( fd[2] );
  2455. <a name="l02415"></a>02415 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  2456. <a name="l02416"></a>02416 }
  2457. <a name="l02417"></a>02417 }
  2458. <a name="l02418"></a>02418 <span class="keywordflow">else</span>
  2459. <a name="l02419"></a>02419 {
  2460. <a name="l02420"></a>02420 <span class="keywordflow">if</span> (( mappedAddr[1].port %2 == 0 )
  2461. <a name="l02421"></a>02421 &amp;&amp; ( mappedAddr[1].port+1 == mappedAddr[2].port ))
  2462. <a name="l02422"></a>02422 {
  2463. <a name="l02423"></a>02423 *mapAddr = mappedAddr[1];
  2464. <a name="l02424"></a>02424 *fd1 = fd[1];
  2465. <a name="l02425"></a>02425 *fd2 = fd[2];
  2466. <a name="l02426"></a>02426 <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>( fd[0] );
  2467. <a name="l02427"></a>02427 <span class="keywordflow">return</span> <span class="keyword">true</span>;
  2468. <a name="l02428"></a>02428 }
  2469. <a name="l02429"></a>02429 }
  2470. <a name="l02430"></a>02430
  2471. <a name="l02431"></a>02431 <span class="comment">// something failed, close all and return error</span>
  2472. <a name="l02432"></a>02432 <span class="keywordflow">for</span>( i=0; i&lt;NUM; i++)
  2473. <a name="l02433"></a>02433 {
  2474. <a name="l02434"></a>02434 <a class="code" href="stun__udp_8h.html#a79746e37503e1405951088e144510b79">stunclosesocket</a>( fd[i] );
  2475. <a name="l02435"></a>02435 }
  2476. <a name="l02436"></a>02436
  2477. <a name="l02437"></a>02437 <span class="keywordflow">return</span> <span class="keyword">false</span>;
  2478. <a name="l02438"></a>02438 }
  2479. <a name="l02439"></a>02439
  2480. <a name="l02440"></a>02440 <span class="comment">/* ====================================================================</span>
  2481. <a name="l02441"></a>02441 <span class="comment"> * The Vovida Software License, Version 1.0</span>
  2482. <a name="l02442"></a>02442 <span class="comment"> *</span>
  2483. <a name="l02443"></a>02443 <span class="comment"> * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.</span>
  2484. <a name="l02444"></a>02444 <span class="comment"> *</span>
  2485. <a name="l02445"></a>02445 <span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
  2486. <a name="l02446"></a>02446 <span class="comment"> * modification, are permitted provided that the following conditions</span>
  2487. <a name="l02447"></a>02447 <span class="comment"> * are met:</span>
  2488. <a name="l02448"></a>02448 <span class="comment"> *</span>
  2489. <a name="l02449"></a>02449 <span class="comment"> * 1. Redistributions of source code must retain the above copyright</span>
  2490. <a name="l02450"></a>02450 <span class="comment"> * notice, this list of conditions and the following disclaimer.</span>
  2491. <a name="l02451"></a>02451 <span class="comment"> *</span>
  2492. <a name="l02452"></a>02452 <span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span>
  2493. <a name="l02453"></a>02453 <span class="comment"> * notice, this list of conditions and the following disclaimer in</span>
  2494. <a name="l02454"></a>02454 <span class="comment"> * the documentation and/or other materials provided with the</span>
  2495. <a name="l02455"></a>02455 <span class="comment"> * distribution.</span>
  2496. <a name="l02456"></a>02456 <span class="comment"> *</span>
  2497. <a name="l02457"></a>02457 <span class="comment"> * 3. The names &quot;VOCAL&quot;, &quot;Vovida Open Communication Application Library&quot;,</span>
  2498. <a name="l02458"></a>02458 <span class="comment"> * and &quot;Vovida Open Communication Application Library (VOCAL)&quot; must</span>
  2499. <a name="l02459"></a>02459 <span class="comment"> * not be used to endorse or promote products derived from this</span>
  2500. <a name="l02460"></a>02460 <span class="comment"> * software without prior written permission. For written</span>
  2501. <a name="l02461"></a>02461 <span class="comment"> * permission, please contact vocal@vovida.org.</span>
  2502. <a name="l02462"></a>02462 <span class="comment"> *</span>
  2503. <a name="l02463"></a>02463 <span class="comment"> * 4. Products derived from this software may not be called &quot;VOCAL&quot;, nor</span>
  2504. <a name="l02464"></a>02464 <span class="comment"> * may &quot;VOCAL&quot; appear in their name, without prior written</span>
  2505. <a name="l02465"></a>02465 <span class="comment"> * permission of Vovida Networks, Inc.</span>
  2506. <a name="l02466"></a>02466 <span class="comment"> *</span>
  2507. <a name="l02467"></a>02467 <span class="comment"> * THIS SOFTWARE IS PROVIDED &quot;AS IS&quot; AND ANY EXPRESSED OR IMPLIED</span>
  2508. <a name="l02468"></a>02468 <span class="comment"> * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES</span>
  2509. <a name="l02469"></a>02469 <span class="comment"> * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND</span>
  2510. <a name="l02470"></a>02470 <span class="comment"> * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA</span>
  2511. <a name="l02471"></a>02471 <span class="comment"> * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES</span>
  2512. <a name="l02472"></a>02472 <span class="comment"> * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,</span>
  2513. <a name="l02473"></a>02473 <span class="comment"> * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,</span>
  2514. <a name="l02474"></a>02474 <span class="comment"> * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR</span>
  2515. <a name="l02475"></a>02475 <span class="comment"> * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY</span>
  2516. <a name="l02476"></a>02476 <span class="comment"> * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
  2517. <a name="l02477"></a>02477 <span class="comment"> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE</span>
  2518. <a name="l02478"></a>02478 <span class="comment"> * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH</span>
  2519. <a name="l02479"></a>02479 <span class="comment"> * DAMAGE.</span>
  2520. <a name="l02480"></a>02480 <span class="comment"> *</span>
  2521. <a name="l02481"></a>02481 <span class="comment"> * ====================================================================</span>
  2522. <a name="l02482"></a>02482 <span class="comment"> *</span>
  2523. <a name="l02483"></a>02483 <span class="comment"> * This software consists of voluntary contributions made by Vovida</span>
  2524. <a name="l02484"></a>02484 <span class="comment"> * Networks, Inc. and many individuals on behalf of Vovida Networks,</span>
  2525. <a name="l02485"></a>02485 <span class="comment"> * Inc. For more information on Vovida Networks, Inc., please see</span>
  2526. <a name="l02486"></a>02486 <span class="comment"> * &lt;http://www.vovida.org/&gt;.</span>
  2527. <a name="l02487"></a>02487 <span class="comment"> *</span>
  2528. <a name="l02488"></a>02488 <span class="comment"> */</span>
  2529. <a name="l02489"></a>02489
  2530. <a name="l02490"></a>02490 <span class="comment">// Local Variables:</span>
  2531. <a name="l02491"></a>02491 <span class="comment">// mode:c++</span>
  2532. <a name="l02492"></a>02492 <span class="comment">// c-file-style:&quot;ellemtel&quot;</span>
  2533. <a name="l02493"></a>02493 <span class="comment">// c-file-offsets:((case-label . +))</span>
  2534. <a name="l02494"></a>02494 <span class="comment">// indent-tabs-mode:nil</span>
  2535. <a name="l02495"></a>02495 <span class="comment">// End:</span>
  2536. <a name="l02496"></a>02496
  2537. <a name="l02497"></a>02497
  2538. </pre></div></div>
  2539. </div>
  2540. <hr class="footer"/><address class="footer"><small>Generated on Wed Nov 3 2010 14:40:45 for OverSim by&nbsp;
  2541. <a href="http://www.doxygen.org/index.html">
  2542. <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.1 </small></address>
  2543. </body>
  2544. </html>