circuit.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If 
00004 **  you did not receive the LICENSE file with this file, you may obtain it
00005 **  from the Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to
00008 **  the terms described in the LICENSE file.
00009 */
00010 
00011 /* 
00012 ** \file circuit.cpp
00013 ** \version $Id: circuit.cpp 2511 2008-04-12 22:02:27Z edmanm $
00014 ** \brief Object representing a Tor circuit
00015 */
00016 
00017 #include <QStringList>
00018 #include <QRegExp>
00019 
00020 #include "circuit.h"
00021 
00022 
00023 /** Default constructor. */
00024 Circuit::Circuit()
00025 {
00026   _circId  = 0;
00027   _status  = Unknown;
00028   _isValid = false;
00029 }
00030 
00031 /** Parses the string given in Tor control protocol format for a circuit. The
00032  * format is:
00033  * 
00034  *      CircuitID SP CircStatus [SP Path]
00035  *
00036  * If the status is "LAUNCHED", the Path is empty. Server names in the path
00037  * must follow Tor's VERBOSE_NAMES format.
00038  */
00039 Circuit::Circuit(const QString &circuit)
00040 {
00041   _isValid = false;
00042 
00043   QStringList parts = circuit.split(" ");
00044   if (parts.size() >= 2) {
00045     /* Get the circuit ID */
00046     _circId = (quint64)parts.at(0).toULongLong();
00047     /* Get the circuit status value */
00048     _status = Circuit::toStatus(parts.at(1));
00049 
00050     /* Get the circuit path (list of routers) */
00051     if (parts.size() > 2) {
00052       foreach (QString hop, parts.at(2).split(",")) {
00053         QStringList parts = hop.split(QRegExp("[=~]"));
00054         if (parts.size() != 2)
00055           return;
00056 
00057         _ids   << parts.at(0).mid(1);
00058         _names << parts.at(1);
00059       }
00060     }
00061 
00062     _isValid = true;
00063   }
00064 }
00065 
00066 /** Converts the circuit status string to its proper enum value */
00067 Circuit::Status
00068 Circuit::toStatus(const QString &status)
00069 {
00070   if (!status.compare("LAUNCHED", Qt::CaseInsensitive))
00071     return Launched;
00072   if (!status.compare("BUILT", Qt::CaseInsensitive))
00073     return Built;
00074   if (!status.compare("EXTENDED", Qt::CaseInsensitive))
00075     return Extended;
00076   if (!status.compare("FAILED", Qt::CaseInsensitive))
00077     return Failed;
00078   if (!status.compare("CLOSED", Qt::CaseInsensitive))
00079     return Closed;
00080   return Unknown;
00081 }
00082 
00083 /** Returns a string representation of the circuit's status. */
00084 QString
00085 Circuit::statusString() const
00086 {
00087   QString status;
00088   switch (_status) {
00089     case Launched:    status = tr("New"); break;
00090     case Built:       status = tr("Open"); break;
00091     case Extended:    status = tr("Building"); break;
00092     case Failed:      status = tr("Failed");  break;
00093     case Closed:      status = tr("Closed"); break;
00094     default:          status = tr("Unknown"); break;
00095   }
00096   return status;
00097 }
00098 

Generated on Sat Aug 16 17:38:35 2008 for Vidalia by  doxygen 1.5.6