<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Vidalia: ControlSocket.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_b47abd1aad201a152869566145babb61.html">src</a> » <a class="el" href="dir_67a2ce25601334632bdc5ef4a72c0f17.html">torcontrol</a> </div> </div> <div class="contents"> <h1>ControlSocket.cpp</h1><a href="_control_socket_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment">** This file is part of Vidalia, and is subject to the license terms in the</span> <a name="l00003"></a>00003 <span class="comment">** LICENSE file, found in the top level directory of this distribution. If </span> <a name="l00004"></a>00004 <span class="comment">** you did not receive the LICENSE file with this file, you may obtain it</span> <a name="l00005"></a>00005 <span class="comment">** from the Vidalia source package distributed by the Vidalia Project at</span> <a name="l00006"></a>00006 <span class="comment">** http://www.vidalia-project.net/. No part of Vidalia, including this file,</span> <a name="l00007"></a>00007 <span class="comment">** may be copied, modified, propagated, or distributed except according to</span> <a name="l00008"></a>00008 <span class="comment">** the terms described in the LICENSE file.</span> <a name="l00009"></a>00009 <span class="comment">*/</span> <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="comment">/*</span> <a name="l00012"></a>00012 <span class="comment">** \file ControlSocket.cpp</span> <a name="l00013"></a>00013 <span class="comment">** \version $Id: ControlSocket.cpp 4054 2009-08-17 02:25:08Z edmanm $</span> <a name="l00014"></a>00014 <span class="comment">** \brief Socket used to connect to Tor's control interface</span> <a name="l00015"></a>00015 <span class="comment">*/</span> <a name="l00016"></a>00016 <a name="l00017"></a>00017 <span class="preprocessor">#include "<a class="code" href="_control_socket_8h.html">ControlSocket.h</a>"</span> <a name="l00018"></a>00018 <span class="preprocessor">#include "<a class="code" href="_send_command_event_8h.html">SendCommandEvent.h</a>"</span> <a name="l00019"></a>00019 <span class="preprocessor">#include "<a class="code" href="tcglobal_8h.html">tcglobal.h</a>"</span> <a name="l00020"></a>00020 <a name="l00021"></a>00021 <span class="preprocessor">#include "<a class="code" href="stringutil_8h.html">stringutil.h</a>"</span> <a name="l00022"></a>00022 <span class="comment"></span> <a name="l00023"></a>00023 <span class="comment">/** Timeout reads in 250ms. We can set this to a short value because if there</span> <a name="l00024"></a>00024 <span class="comment">* isn't any data to read, we want to return anyway. */</span> <a name="l00025"></a><a class="code" href="_control_socket_8cpp.html#a18e13c9ac88403c42395bb0af487eb2d">00025</a> <span class="preprocessor">#define READ_TIMEOUT 250</span> <a name="l00026"></a>00026 <span class="preprocessor"></span> <a name="l00027"></a>00027 <span class="comment"></span> <a name="l00028"></a>00028 <span class="comment">/** Default constructor. */</span> <a name="l00029"></a><a class="code" href="class_control_socket.html#acd8a2d5cce14340fcc5defbedf38b59e">00029</a> <a class="code" href="class_control_socket.html#acd8a2d5cce14340fcc5defbedf38b59e">ControlSocket::ControlSocket</a>() <a name="l00030"></a>00030 { <a name="l00031"></a>00031 } <a name="l00032"></a>00032 <span class="comment"></span> <a name="l00033"></a>00033 <span class="comment">/** Returns true if the control socket is connected and ready to send or</span> <a name="l00034"></a>00034 <span class="comment"> * receive. */</span> <a name="l00035"></a>00035 <span class="keywordtype">bool</span> <a name="l00036"></a><a class="code" href="class_control_socket.html#add0d8b44456f6b98ac52a3ccb642e4f2">00036</a> <a class="code" href="class_control_socket.html#add0d8b44456f6b98ac52a3ccb642e4f2">ControlSocket::isConnected</a>() <a name="l00037"></a>00037 { <a name="l00038"></a>00038 <span class="keywordflow">return</span> (isValid() && state() == QAbstractSocket::ConnectedState); <a name="l00039"></a>00039 } <a name="l00040"></a>00040 <span class="comment"></span> <a name="l00041"></a>00041 <span class="comment">/** Processes custom events sent to this object (e.g. SendCommandEvents) from</span> <a name="l00042"></a>00042 <span class="comment"> * other threads. */</span> <a name="l00043"></a>00043 <span class="keywordtype">void</span> <a name="l00044"></a><a class="code" href="class_control_socket.html#a2ade78e22e613147c0d9dc550aeb84a5">00044</a> <a class="code" href="class_control_socket.html#a2ade78e22e613147c0d9dc550aeb84a5">ControlSocket::customEvent</a>(QEvent *event) <a name="l00045"></a>00045 { <a name="l00046"></a>00046 <span class="keywordflow">if</span> (event->type() == QEvent::User) { <a name="l00047"></a>00047 <a class="code" href="class_send_command_event.html">SendCommandEvent</a> *sce = <span class="keyword">dynamic_cast<</span><a class="code" href="class_send_command_event.html">SendCommandEvent</a> *<span class="keyword">></span>(event); <a name="l00048"></a>00048 <span class="keywordflow">if</span> (! sce) <a name="l00049"></a>00049 <span class="keywordflow">return</span>; <a name="l00050"></a>00050 <a name="l00051"></a>00051 QString errmsg; <a name="l00052"></a>00052 <span class="keywordtype">bool</span> result = <a class="code" href="class_control_socket.html#ad1a17ebeffc64f0a8478592ce0e98cfd">sendCommand</a>(sce-><a class="code" href="class_send_command_event.html#a8b1d46a93a456b83b45d8c5eda1ca9e7">command</a>(), &errmsg); <a name="l00053"></a>00053 <span class="keywordflow">if</span> (sce-><a class="code" href="class_send_command_event.html#ac80735b768437082f63fc718d20fa1cd">waiter</a>()) <a name="l00054"></a>00054 sce-><a class="code" href="class_send_command_event.html#ac80735b768437082f63fc718d20fa1cd">waiter</a>()->setResult(result, errmsg); <a name="l00055"></a>00055 sce->accept(); <a name="l00056"></a>00056 } <a name="l00057"></a>00057 } <a name="l00058"></a>00058 <span class="comment"></span> <a name="l00059"></a>00059 <span class="comment">/** Send a control command to Tor on the control socket, conforming to Tor's</span> <a name="l00060"></a>00060 <span class="comment"> * Control Protocol V1:</span> <a name="l00061"></a>00061 <span class="comment"> *</span> <a name="l00062"></a>00062 <span class="comment"> * Command = Keyword Arguments CRLF / "+" Keyword Arguments CRLF Data</span> <a name="l00063"></a>00063 <span class="comment"> * Keyword = 1*ALPHA</span> <a name="l00064"></a>00064 <span class="comment"> * Arguments = *(SP / VCHAR)</span> <a name="l00065"></a>00065 <span class="comment"> */</span> <a name="l00066"></a>00066 <span class="keywordtype">bool</span> <a name="l00067"></a><a class="code" href="class_control_socket.html#ad1a17ebeffc64f0a8478592ce0e98cfd">00067</a> <a class="code" href="class_control_socket.html#ad1a17ebeffc64f0a8478592ce0e98cfd">ControlSocket::sendCommand</a>(<a class="code" href="class_control_command.html">ControlCommand</a> cmd, QString *errmsg) <a name="l00068"></a>00068 { <a name="l00069"></a>00069 <span class="keywordflow">if</span> (!<a class="code" href="class_control_socket.html#add0d8b44456f6b98ac52a3ccb642e4f2">isConnected</a>()) { <a name="l00070"></a>00070 <span class="keywordflow">return</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>(errmsg, tr(<span class="stringliteral">"Control socket is not connected."</span>)); <a name="l00071"></a>00071 } <a name="l00072"></a>00072 <a name="l00073"></a>00073 <span class="comment">/* Format the control command */</span> <a name="l00074"></a>00074 QString strCmd = cmd.<a class="code" href="class_control_command.html#a57c94909a90bc9ecb301ff3ef3beeb45">toString</a>(); <a name="l00075"></a>00075 <a class="code" href="namespacetc.html#aee5c749751dca91b96d6d024b1c4a076">tc::debug</a>(<span class="stringliteral">"Control Command: %1"</span>).arg(strCmd.trimmed()); <a name="l00076"></a>00076 <a name="l00077"></a>00077 <span class="comment">/* Attempt to send the command to Tor */</span> <a name="l00078"></a>00078 <span class="keywordflow">if</span> (write(strCmd.toAscii()) != strCmd.length()) { <a name="l00079"></a>00079 <span class="keywordflow">return</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>(errmsg, tr(<span class="stringliteral">"Error sending control command. [%1]"</span>) <a name="l00080"></a>00080 .arg(errorString())); <a name="l00081"></a>00081 } <a name="l00082"></a>00082 flush(); <a name="l00083"></a>00083 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00084"></a>00084 } <a name="l00085"></a>00085 <span class="comment"></span> <a name="l00086"></a>00086 <span class="comment">/** Reads line data, one chunk at a time, until a newline character is</span> <a name="l00087"></a>00087 <span class="comment"> * encountered. */</span> <a name="l00088"></a>00088 <span class="keywordtype">bool</span> <a name="l00089"></a><a class="code" href="class_control_socket.html#a43db321f9e81061f319dc224b6eb96d6">00089</a> <a class="code" href="class_control_socket.html#a43db321f9e81061f319dc224b6eb96d6">ControlSocket::readLineData</a>(QString &line, QString *errmsg) <a name="l00090"></a>00090 { <a name="l00091"></a>00091 <span class="keywordtype">char</span> buffer[1024]; <span class="comment">/* Read in 1024 byte chunks at a time */</span> <a name="l00092"></a>00092 <span class="keywordtype">int</span> bytesRecv = <a class="code" href="class_control_socket.html#a0d7db4048387b2ef9c2d85e504a02825">QAbstractSocket::readLine</a>(buffer, 1024); <a name="l00093"></a>00093 <span class="keywordflow">while</span> (bytesRecv != -1) { <a name="l00094"></a>00094 line.append(buffer); <a name="l00095"></a>00095 <span class="keywordflow">if</span> (buffer[bytesRecv-1] == <span class="charliteral">'\n'</span>) { <a name="l00096"></a>00096 <span class="keywordflow">break</span>; <a name="l00097"></a>00097 } <a name="l00098"></a>00098 bytesRecv = <a class="code" href="class_control_socket.html#a0d7db4048387b2ef9c2d85e504a02825">QAbstractSocket::readLine</a>(buffer, 1024); <a name="l00099"></a>00099 } <a name="l00100"></a>00100 <span class="keywordflow">if</span> (bytesRecv == -1) { <a name="l00101"></a>00101 <span class="keywordflow">return</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>(errmsg, errorString()); <a name="l00102"></a>00102 } <a name="l00103"></a>00103 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00104"></a>00104 } <a name="l00105"></a>00105 <span class="comment"></span> <a name="l00106"></a>00106 <span class="comment">/** Reads a line of data from the socket and returns true if successful or</span> <a name="l00107"></a>00107 <span class="comment"> * false if an error occurred while waiting for a line of data to become</span> <a name="l00108"></a>00108 <span class="comment"> * available. */</span> <a name="l00109"></a>00109 <span class="keywordtype">bool</span> <a name="l00110"></a><a class="code" href="class_control_socket.html#a0d7db4048387b2ef9c2d85e504a02825">00110</a> <a class="code" href="class_control_socket.html#a0d7db4048387b2ef9c2d85e504a02825">ControlSocket::readLine</a>(QString &line, QString *errmsg) <a name="l00111"></a>00111 { <a name="l00112"></a>00112 <span class="comment">/* Make sure we have data to read before attempting anything. Note that this</span> <a name="l00113"></a>00113 <span class="comment"> * essentially makes our socket a blocking socket */</span> <a name="l00114"></a>00114 <span class="keywordflow">while</span> (!canReadLine()) { <a name="l00115"></a>00115 <span class="keywordflow">if</span> (!<a class="code" href="class_control_socket.html#add0d8b44456f6b98ac52a3ccb642e4f2">isConnected</a>()) { <a name="l00116"></a>00116 <span class="keywordflow">return</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>(errmsg, tr(<span class="stringliteral">"Socket disconnected while attempting "</span> <a name="l00117"></a>00117 <span class="stringliteral">"to read a line of data."</span>)); <a name="l00118"></a>00118 } <a name="l00119"></a>00119 waitForReadyRead(<a class="code" href="_control_socket_8cpp.html#a18e13c9ac88403c42395bb0af487eb2d">READ_TIMEOUT</a>); <a name="l00120"></a>00120 } <a name="l00121"></a>00121 line.clear(); <a name="l00122"></a>00122 <span class="keywordflow">return</span> <a class="code" href="class_control_socket.html#a43db321f9e81061f319dc224b6eb96d6">readLineData</a>(line, errmsg); <a name="l00123"></a>00123 } <a name="l00124"></a>00124 <span class="comment"></span> <a name="l00125"></a>00125 <span class="comment">/** Read a complete reply from the control socket. Replies take the following</span> <a name="l00126"></a>00126 <span class="comment"> * form, based on Tor's Control Protocol v1:</span> <a name="l00127"></a>00127 <span class="comment"> *</span> <a name="l00128"></a>00128 <span class="comment"> * Reply = *(MidReplyLine / DataReplyLine) EndReplyLine</span> <a name="l00129"></a>00129 <span class="comment"> *</span> <a name="l00130"></a>00130 <span class="comment"> * MidReplyLine = "-" ReplyLine</span> <a name="l00131"></a>00131 <span class="comment"> * DataReplyLine = "+" ReplyLine Data</span> <a name="l00132"></a>00132 <span class="comment"> * EndReplyLine = SP ReplyLine</span> <a name="l00133"></a>00133 <span class="comment"> * ReplyLine = StatusCode [ SP ReplyText ] CRLF</span> <a name="l00134"></a>00134 <span class="comment"> * ReplyText = XXXX</span> <a name="l00135"></a>00135 <span class="comment"> * StatusCode = XXiX</span> <a name="l00136"></a>00136 <span class="comment"> */</span> <a name="l00137"></a>00137 <span class="keywordtype">bool</span> <a name="l00138"></a><a class="code" href="class_control_socket.html#a6a41a10e7cd0f5e21c33e78ba4d20cc4">00138</a> <a class="code" href="class_control_socket.html#a6a41a10e7cd0f5e21c33e78ba4d20cc4">ControlSocket::readReply</a>(<a class="code" href="class_control_reply.html">ControlReply</a> &reply, QString *errmsg) <a name="l00139"></a>00139 { <a name="l00140"></a>00140 QChar c; <a name="l00141"></a>00141 QString line; <a name="l00142"></a>00142 <a name="l00143"></a>00143 <span class="keywordflow">if</span> (!<a class="code" href="class_control_socket.html#add0d8b44456f6b98ac52a3ccb642e4f2">isConnected</a>()) { <a name="l00144"></a>00144 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00145"></a>00145 } <a name="l00146"></a>00146 <a name="l00147"></a>00147 <span class="comment">/* The implementation below is (loosely) based on the Java control library</span> <a name="l00148"></a>00148 <span class="comment"> * from Tor */</span> <a name="l00149"></a>00149 <span class="keywordflow">do</span> { <a name="l00150"></a>00150 <span class="comment">/* Read a line of the response */</span> <a name="l00151"></a>00151 <span class="keywordflow">if</span> (!<a class="code" href="class_control_socket.html#a0d7db4048387b2ef9c2d85e504a02825">readLine</a>(line, errmsg)) { <a name="l00152"></a>00152 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00153"></a>00153 } <a name="l00154"></a>00154 <a name="l00155"></a>00155 <span class="keywordflow">if</span> (line.length() < 4) { <a name="l00156"></a>00156 <span class="keywordflow">return</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>(errmsg, tr(<span class="stringliteral">"Invalid control reply. [%1]"</span>).arg(line)); <a name="l00157"></a>00157 } <a name="l00158"></a>00158 <a name="l00159"></a>00159 <span class="comment">/* Parse the status and message */</span> <a name="l00160"></a>00160 <a class="code" href="class_reply_line.html">ReplyLine</a> replyLine(line.mid(0, 3), line.mid(4)); <a name="l00161"></a>00161 c = line.at(3); <a name="l00162"></a>00162 <a name="l00163"></a>00163 <span class="comment">/* If the reply line contains data, then parse out the data up until the</span> <a name="l00164"></a>00164 <span class="comment"> * trailing CRLF "." CRLF */</span> <a name="l00165"></a>00165 <span class="keywordflow">if</span> (c == QChar(<span class="charliteral">'+'</span>) && <a name="l00166"></a>00166 !line.startsWith(<span class="stringliteral">"250+PROTOCOLINFO"</span>)) { <a name="l00167"></a>00167 <span class="comment">/* XXX The second condition above is a hack to deal with Tor</span> <a name="l00168"></a>00168 <span class="comment"> * 0.2.0.5-alpha that gives a malformed PROTOCOLINFO reply. This</span> <a name="l00169"></a>00169 <span class="comment"> * should be removed once that version of Tor is sufficiently dead. */</span> <a name="l00170"></a>00170 <span class="keywordflow">while</span> (<span class="keyword">true</span>) { <a name="l00171"></a>00171 <span class="keywordflow">if</span> (!<a class="code" href="class_control_socket.html#a0d7db4048387b2ef9c2d85e504a02825">readLine</a>(line, errmsg)) { <a name="l00172"></a>00172 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00173"></a>00173 } <a name="l00174"></a>00174 <span class="keywordflow">if</span> (line.trimmed() == <span class="stringliteral">"."</span>) { <a name="l00175"></a>00175 <span class="keywordflow">break</span>; <a name="l00176"></a>00176 } <a name="l00177"></a>00177 replyLine.appendData(line); <a name="l00178"></a>00178 } <a name="l00179"></a>00179 } <a name="l00180"></a>00180 reply.<a class="code" href="class_control_reply.html#ab7f593d8a0f921c6d35fa5135676dc40">appendLine</a>(replyLine); <a name="l00181"></a>00181 } <span class="keywordflow">while</span> (c != QChar(<span class="charliteral">' '</span>)); <a name="l00182"></a>00182 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00183"></a>00183 } <a name="l00184"></a>00184 <span class="comment"></span> <a name="l00185"></a>00185 <span class="comment">/** Returns the string description of <b>error</b>. */</span> <a name="l00186"></a>00186 QString <a name="l00187"></a><a class="code" href="class_control_socket.html#a34ebf2e5e3314a619cca291c69b7f7e1">00187</a> <a class="code" href="class_control_socket.html#a34ebf2e5e3314a619cca291c69b7f7e1">ControlSocket::toString</a>(<span class="keyword">const</span> QAbstractSocket::SocketError <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">error</a>) <a name="l00188"></a>00188 { <a name="l00189"></a>00189 QString str; <a name="l00190"></a>00190 <span class="keywordflow">switch</span> (error) { <a name="l00191"></a>00191 <span class="keywordflow">case</span> ConnectionRefusedError: <a name="l00192"></a>00192 str = <span class="stringliteral">"Connection refused by peer."</span>; <span class="keywordflow">break</span>; <a name="l00193"></a>00193 <span class="keywordflow">case</span> RemoteHostClosedError: <a name="l00194"></a>00194 str = <span class="stringliteral">"Remote host closed the connection."</span>; <span class="keywordflow">break</span>; <a name="l00195"></a>00195 <span class="keywordflow">case</span> HostNotFoundError: <a name="l00196"></a>00196 str = <span class="stringliteral">"Host address not found."</span>; <span class="keywordflow">break</span>; <a name="l00197"></a>00197 <span class="keywordflow">case</span> SocketAccessError: <a name="l00198"></a>00198 str = <span class="stringliteral">"Insufficient access privileges."</span>; <span class="keywordflow">break</span>; <a name="l00199"></a>00199 <span class="keywordflow">case</span> SocketResourceError: <a name="l00200"></a>00200 str = <span class="stringliteral">"Insufficient resources."</span>; <span class="keywordflow">break</span>; <a name="l00201"></a>00201 <span class="keywordflow">case</span> SocketTimeoutError: <a name="l00202"></a>00202 str = <span class="stringliteral">"Socket operation timed out."</span>; <span class="keywordflow">break</span>; <a name="l00203"></a>00203 <span class="keywordflow">case</span> DatagramTooLargeError: <a name="l00204"></a>00204 str = <span class="stringliteral">"Datagram size exceeded the operating system limit."</span>; <span class="keywordflow">break</span>; <a name="l00205"></a>00205 <span class="keywordflow">case</span> NetworkError: <a name="l00206"></a>00206 str = <span class="stringliteral">"Network error occurred."</span>; <span class="keywordflow">break</span>; <a name="l00207"></a>00207 <span class="keywordflow">case</span> AddressInUseError: <a name="l00208"></a>00208 str = <span class="stringliteral">"Specified address already in use."</span>; <span class="keywordflow">break</span>; <a name="l00209"></a>00209 <span class="keywordflow">case</span> SocketAddressNotAvailableError: <a name="l00210"></a>00210 str = <span class="stringliteral">"Specified address does not belong to the host."</span>; <span class="keywordflow">break</span>; <a name="l00211"></a>00211 <span class="keywordflow">case</span> UnsupportedSocketOperationError: <a name="l00212"></a>00212 str = <span class="stringliteral">"The requested operation is not supported."</span>; <span class="keywordflow">break</span>; <a name="l00213"></a>00213 <span class="keywordflow">default</span>: <a name="l00214"></a>00214 str = <span class="stringliteral">"An unidentified error occurred."</span>; <span class="keywordflow">break</span>; <a name="l00215"></a>00215 } <a name="l00216"></a>00216 <span class="keywordflow">return</span> str; <a name="l00217"></a>00217 } <a name="l00218"></a>00218 </pre></div></div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Aug 30 22:58:54 2010 for Vidalia by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>