Changeset 2977

Show
Ignore:
Timestamp:
08/16/08 21:28:25 (3 months ago)
Author:
edmanm
Message:

Create CircuitId? and StreamId? typedefs with QString as their underlying type,
and switch all circuit and stream function calls to use these new types. Fixes
ticket #400.

Location:
vidalia/trunk/src
Files:
17 modified

Legend:

Unmodified
Added
Removed
  • vidalia/trunk/src/torcontrol/circuit.cpp

    r2511 r2977  
    1818#include <QRegExp> 
    1919 
     20#include "tcglobal.h" 
    2021#include "circuit.h" 
    2122 
     
    2425Circuit::Circuit() 
    2526{ 
    26   _circId  = 0; 
    2727  _status  = Unknown; 
    2828  _isValid = false; 
     
    3939Circuit::Circuit(const QString &circuit) 
    4040{ 
    41   _isValid = false; 
    42  
    43   QStringList parts = circuit.split(" "); 
     41  QStringList parts = circuit.split(" ", QString::SkipEmptyParts); 
    4442  if (parts.size() >= 2) { 
    4543    /* Get the circuit ID */ 
    46     _circId = (quint64)parts.at(0).toULongLong(); 
     44    _circId = parts.at(0); 
     45    if (! isValidCircuitId(_circId)) 
     46      goto err; 
     47 
    4748    /* Get the circuit status value */ 
    4849    _status = Circuit::toStatus(parts.at(1)); 
     
    5354        QStringList parts = hop.split(QRegExp("[=~]")); 
    5455        if (parts.size() != 2) 
    55           return; 
     56          goto err; 
    5657 
    5758        _ids   << parts.at(0).mid(1); 
     
    6263    _isValid = true; 
    6364  } 
     65  return; 
     66   
     67err: 
     68  tc::warn("Improperly formatted circuit: '%1'").arg(circuit); 
     69  _isValid = false; 
     70} 
     71 
     72/** Returns true iff <b>circId</b> consists of only between 1 and 16 
     73 * (inclusive) ASCII-encoded letters and numbers. */ 
     74bool 
     75Circuit::isValidCircuitId(const CircuitId &circId) 
     76{ 
     77  int length = circId.length(); 
     78  if (length < 1 || length > 16) 
     79    return false; 
     80 
     81  for (int i = 0; i < length; i++) { 
     82    char c = circId[i].toAscii(); 
     83    if (c < '0' && c > '9' && c < 'A' && c > 'Z' && c < 'a' && c > 'z') 
     84      return false; 
     85  } 
     86  return true; 
    6487} 
    6588 
  • vidalia/trunk/src/torcontrol/circuit.h

    r2511 r2977  
    2121#include <QStringList> 
    2222 
     23/** Circuit IDs contains 1-16 alphanumeric ASCII characters. */ 
     24typedef QString CircuitId; 
     25 
    2326 
    2427class Circuit 
     
    4043  Circuit(); 
    4144  /** Constructor. */   
    42   Circuit(const QString &circuit); 
     45  Circuit(const CircuitId &circuit); 
    4346   
    4447  /** Returns true if this circuit is valid. */ 
     
    4649   
    4750  /** Returns the ID for this circuit */ 
    48   quint64 id() const { return _circId; } 
     51  CircuitId id() const { return _circId; } 
    4952  /** Returns the status of this circuit */ 
    5053  Status status() const { return _status; } 
     
    6164  static Status toStatus(const QString &strStatus); 
    6265 
     66  /** Returns true iff <b>circId</b> consists of only between 1 and 16 
     67   * (inclusive) ASCII-encoded letters and numbers. */ 
     68  static bool isValidCircuitId(const CircuitId &circId); 
     69 
    6370private: 
    64   quint64 _circId; /**< Circuit ID. */ 
     71  CircuitId _circId; /**< Circuit ID. */ 
    6572  Status _status;  /**< Circuit status. */ 
    6673  QStringList _names;  /**< Nicknames of the routers in the circuit. */ 
  • vidalia/trunk/src/torcontrol/circuitevent.h

    r2511 r2977  
    1919 
    2020#include <QEvent> 
     21#include <QString> 
    2122 
    2223#include "eventtype.h" 
     
    3536  Circuit circuit() const { return _circuit; } 
    3637  /** Returns the ID for this circuit event. */ 
    37   quint64 id() const { return _circuit.id(); } 
     38  CircuitId id() const { return _circuit.id(); } 
    3839  /** Returns the status of this circuit event. */ 
    3940  Circuit::Status status() const { return _circuit.status(); } 
  • vidalia/trunk/src/torcontrol/stream.cpp

    r2362 r2977  
    1616 
    1717#include <QStringList> 
    18   
     18 
     19#include "circuit.h"  
    1920#include "stream.h" 
    2021 
     
    2324Stream::Stream() 
    2425{ 
    25   _streamId  = 0; 
    2626  _status    = Unknown; 
    27   _circuitId = 0; 
    2827  _port      = 0; 
    2928} 
    3029 
    3130/** Constructor */ 
    32 Stream::Stream(quint64 streamId, Status status, quint64 circuitId,  
    33                QString address, quint16 port) 
     31Stream::Stream(const StreamId &streamId, Status status, 
     32               const CircuitId &circuitId, const QString &address, 
     33               quint16 port) 
    3434{ 
    3535  _streamId  = streamId; 
     
    4141 
    4242/** Constructor */ 
    43 Stream::Stream(quint64 streamId, Status status, quint64 circuitId, QString target) 
     43Stream::Stream(const StreamId &streamId, Status status, 
     44               const CircuitId &circuitId, const QString &target) 
    4445{ 
    4546  _streamId  = streamId; 
     
    6162 */ 
    6263Stream 
    63 Stream::fromString(QString stream) 
     64Stream::fromString(const QString &stream) 
    6465{ 
    65   QStringList parts = stream.split(" "); 
     66  QStringList parts = stream.split(" ", QString::SkipEmptyParts); 
    6667  if (parts.size() >= 4) {  
    6768    /* Get the stream ID */ 
    68     quint64 streamId = (quint64)parts.at(0).toULongLong(); 
     69    StreamId streamId = parts.at(0); 
    6970    /* Get the stream status value */ 
    7071    Stream::Status status = Stream::toStatus(parts.at(1)); 
    7172    /* Get the ID of the circuit on which this stream travels */ 
    72     quint64 circId = (quint64)parts.at(2).toULongLong(); 
     73    CircuitId circId = parts.at(2); 
    7374    /* Get the target address for this stream */ 
    7475    QString target = parts.at(3); 
     
    7980} 
    8081 
     82/** Returns true iff <b>streamId</b> consists of only between 1 and 16 
     83 * (inclusive) ASCII-encoded letters and numbers. */ 
     84bool 
     85Stream::isValidStreamId(const StreamId &streamId) 
     86{ 
     87  int length = streamId.length(); 
     88  if (length < 1 || length > 16) 
     89    return false; 
     90 
     91  for (int i = 0; i < length; i++) { 
     92    char c = streamId[i].toAscii(); 
     93    if (c < '0' && c > '9' && c < 'A' && c > 'Z' && c < 'a' && c > 'z') 
     94      return false; 
     95  } 
     96  return true; 
     97} 
     98 
    8199/** Converts a string description of a stream's status to its enum value */ 
    82100Stream::Status 
    83 Stream::toStatus(QString strStatus) 
     101Stream::toStatus(const QString &strStatus) 
    84102{ 
    85   Status status; 
    86   strStatus = strStatus.toUpper(); 
    87   if (strStatus == "NEW") { 
    88     status = New; 
    89   } else if (strStatus == "NEWRESOLVE") { 
    90     status = NewResolve; 
    91   } else if (strStatus == "SENTCONNECT") { 
    92     status = SentConnect; 
    93   } else if (strStatus == "SENTRESOLVE") { 
    94     status = SentResolve; 
    95   } else if (strStatus == "SUCCEEDED") { 
    96     status = Succeeded; 
    97   } else if (strStatus == "FAILED") { 
    98     status = Failed; 
    99   } else if (strStatus == "CLOSED") { 
    100     status = Closed; 
    101   } else if (strStatus == "DETACHED") { 
    102     status = Detached;  
    103   } else if (strStatus == "REMAP") { 
    104     status = Remap; 
    105   } else { 
    106     status = Unknown; 
    107   } 
    108   return status; 
     103  if (!strStatus.compare("NEW", Qt::CaseInsensitive)) 
     104    return New; 
     105  if (!strStatus.compare("NEWRESOLVE", Qt::CaseInsensitive)) 
     106    return NewResolve; 
     107  if (!strStatus.compare("SENTCONNECT", Qt::CaseInsensitive)) 
     108    return SentConnect; 
     109  if (!strStatus.compare("SENTRESOLVE", Qt::CaseInsensitive)) 
     110    return SentResolve; 
     111  if (!strStatus.compare("SUCCEEDED", Qt::CaseInsensitive)) 
     112    return Succeeded; 
     113  if (!strStatus.compare("FAILED", Qt::CaseInsensitive)) 
     114    return Failed; 
     115  if (!strStatus.compare("CLOSED", Qt::CaseInsensitive)) 
     116    return Closed; 
     117  if (!strStatus.compare("DETACHED", Qt::CaseInsensitive)) 
     118    return Detached; 
     119  if (!strStatus.compare("REMAP", Qt::CaseInsensitive)) 
     120    return Remap; 
     121  return Unknown; 
    109122} 
    110123 
     
    130143} 
    131144 
    132 /** Returns true if all fields in this Stream object are empty. */ 
     145/** Returns true if all fields in this Stream object are valid. */ 
    133146bool 
    134 Stream::isEmpty() const 
     147Stream::isValid() const 
    135148{ 
    136   return (!_streamId && !_circuitId &&  
    137           (_status == Unknown) && _address.isEmpty() && !_port); 
     149  return (isValidStreamId(_streamId) 
     150            && Circuit::isValidCircuitId(_circuitId) 
     151            && (_status != Unknown)  
     152            && !_address.isEmpty()); 
    138153} 
    139154 
  • vidalia/trunk/src/torcontrol/stream.h

    r2510 r2977  
    2323#include <QList> 
    2424 
     25#include "circuit.h" 
     26 
     27/** Stream IDs contains 1-16 alphanumeric ASCII characters. */ 
     28typedef QString StreamId; 
     29 
    2530 
    2631class Stream 
     
    4651  Stream(); 
    4752  /** Constructor */ 
    48   Stream(quint64 streamId, Status status, quint64 circuitId, QString target); 
     53  Stream(const StreamId &streamId, Status status, const CircuitId &circuitId, 
     54         const QString &target); 
    4955  /** Constructor */ 
    50   Stream(quint64 streamId, Status status, quint64 circuitId, 
    51          QString address, quint16 port); 
     56  Stream(const StreamId &streamId, Status status, const CircuitId &circuitId, 
     57         const QString &address, quint16 port); 
    5258 
    5359  /** Parses the given string for a stream, in Tor control protocol format. */ 
    54   static Stream fromString(QString stream); 
     60  static Stream fromString(const QString &stream); 
    5561  /** Converts a string description of a stream's status to its enum value */ 
    56   static Status toStatus(QString strStatus); 
     62  static Status toStatus(const QString &strStatus); 
    5763 
    58   /** Returns true if the Stream object's fields are all empty. */ 
    59   bool isEmpty() const; 
     64  /** Returns true iff the Stream object's fields are all valid. */ 
     65  bool isValid() const; 
    6066   
    6167  /** Returns the ID for this stream. */ 
    62   quint64 id() const { return _streamId; } 
     68  StreamId id() const { return _streamId; } 
    6369  /** Returns the status for this stream. */ 
    6470  Status status() const { return _status; } 
     
    6672  QString statusString() const; 
    6773  /** Returns the ID of the circuit to which this stream is assigned. */ 
    68   quint64 circuitId() const { return _circuitId; } 
     74  CircuitId circuitId() const { return _circuitId; } 
    6975  /** Returns the target address and port for this stream. */ 
    7076  QString target() const { return (_address + ":" + QString::number(_port)); } 
     
    7480  quint16 targetPort() const { return _port; } 
    7581 
     82  /** Returns true iff <b>streamId</b> consists of only between 1 and 16 
     83   * (inclusive) ASCII-encoded letters and numbers. */ 
     84   static bool isValidStreamId(const StreamId &streamId); 
     85 
    7686private: 
    77   quint64 _streamId;   /**< Unique ID associated with this stream. */ 
     87  StreamId _streamId;   /**< Unique ID associated with this stream. */ 
     88  CircuitId _circuitId; /**< ID of the circuit carrying this stream. */ 
     89  QString _address;    /**< Stream target address. */ 
    7890  Status  _status;     /**< Stream status value. */ 
    79   quint64 _circuitId;  /**< ID of the circuit carrying this stream. */ 
    80   QString _address;    /**< Stream target address. */ 
    8191  quint16 _port;       /**< Stream target port. */ 
    8292}; 
  • vidalia/trunk/src/torcontrol/streamevent.h

    r2362 r2977  
    3535  Stream stream() const { return _stream; } 
    3636  /** Returns the ID for this stream event. */ 
    37   quint64 id() const { return _stream.id(); } 
     37  StreamId id() const { return _stream.id(); } 
    3838  /** Returns the status for this stream event. */ 
    3939  Stream::Status status() const { return _stream.status(); } 
    4040  /** Returns the ID of the circuit to which this stream is assigned */ 
    41   quint64 circuitId() const { return _stream.circuitId(); } 
     41  CircuitId circuitId() const { return _stream.circuitId(); } 
    4242  /** Returns the target for this stream event. */ 
    4343  QString target() const { return _stream.target(); } 
  • vidalia/trunk/src/torcontrol/torcontrol.cpp

    r2919 r2977  
    975975} 
    976976 
    977 /** Closes the circuit specified by <b>circid</b>. If <b>ifUnused</b> is 
     977/** Closes the circuit specified by <b>circId</b>. If <b>ifUnused</b> is 
    978978 * true, then the circuit will not be closed unless it is unused. */ 
    979979bool 
    980 TorControl::closeCircuit(quint64 circid, bool ifUnused, QString *errmsg) 
    981 { 
    982   ControlCommand cmd("CLOSECIRCUIT", QString::number(circid)); 
     980TorControl::closeCircuit(const CircuitId &circId, bool ifUnused, QString *errmsg) 
     981{ 
     982  ControlCommand cmd("CLOSECIRCUIT", circId); 
    983983  if (ifUnused) { 
    984984    cmd.addArgument("IfUnused"); 
     
    10001000    QString msg = reply.getMessage(); 
    10011001    s = Stream::fromString(msg.mid(msg.indexOf("=")+1)); 
    1002     if (!s.isEmpty()) { 
     1002    if (s.isValid()) 
    10031003      streams << s; 
    1004     } 
    10051004     
    1006     /* The rest of the streams jsut come as data, one per line */ 
     1005    /* The rest of the streams just come as data, one per line */ 
    10071006    foreach (QString line, reply.getData()) { 
    10081007      s = Stream::fromString(line); 
    1009       if (!s.isEmpty()) { 
     1008      if (s.isValid()) 
    10101009        streams << s; 
    1011       } 
    10121010    } 
    10131011  } 
     
    10151013} 
    10161014 
    1017 /** Closes the stream specified by <b>streamid</b>. */ 
    1018 bool 
    1019 TorControl::closeStream(quint64 streamid, QString *errmsg) 
    1020 { 
    1021   ControlCommand cmd("CLOSESTREAM", QString::number(streamid)); 
     1015/** Closes the stream specified by <b>streamId</b>. */ 
     1016bool 
     1017TorControl::closeStream(const StreamId &streamId, QString *errmsg) 
     1018{ 
     1019  ControlCommand cmd("CLOSESTREAM", streamId); 
    10221020  cmd.addArgument("1"); /* 1 == REASON_MISC (tor-spec.txt) */ 
    10231021  return send(cmd, errmsg); 
  • vidalia/trunk/src/torcontrol/torcontrol.h

    r2780 r2977  
    197197 
    198198public slots: 
    199   /** Closes the circuit specified by <b>circid</b>. If <b>ifUnused</b> is 
     199  /** Closes the circuit specified by <b>circId</b>. If <b>ifUnused</b> is 
    200200   * true, then the circuit will not be closed unless it is unused. */ 
    201   bool closeCircuit(quint64 circid, bool ifUnused = false, QString *errmsg = 0); 
    202   /** Closes the stream specified by <b>streamid</b>. */ 
    203   bool closeStream(quint64 streamid, QString *errmsg = 0); 
     201  bool closeCircuit(const CircuitId &circId, bool ifUnused = false, 
     202                    QString *errmsg = 0); 
     203  /** Closes the stream specified by <b>streamId</b>. */ 
     204  bool closeStream(const StreamId &streamId, QString *errmsg = 0); 
    204205 
    205206signals: 
  • vidalia/trunk/src/vidalia/network/circuititem.cpp

    r2511 r2977  
    6565/** Returns a list of all stream items on this circuit. */ 
    6666QList<StreamItem *> 
    67 CircuitItem::streams() 
     67CircuitItem::streams() const 
    6868{ 
    6969  QList<StreamItem *> streams; 
  • vidalia/trunk/src/vidalia/network/circuititem.h

    r2511 r2977  
    4141  void update(const Circuit &circuit); 
    4242  /** Returns the ID for this circuit. */ 
    43   quint64 id() { return _circuit.id(); } 
     43  CircuitId id() const { return _circuit.id(); } 
    4444  /** Returns the Circuit object for this item. */ 
    45   Circuit circuit() { return _circuit; } 
     45  Circuit circuit() const { return _circuit; } 
    4646  /** Returns a list of all stream items on this circuit. */ 
    47   QList<StreamItem *> streams(); 
     47  QList<StreamItem *> streams() const; 
    4848   
    4949private: 
  • vidalia/trunk/src/vidalia/network/circuitlistwidget.cpp

    r2511 r2977  
    272272 * circuit's item in the list. */ 
    273273CircuitItem* 
    274 CircuitListWidget::findCircuitItem(quint64 circid) 
     274CircuitListWidget::findCircuitItem(const CircuitId &circid) 
    275275{ 
    276276  int numCircs = topLevelItemCount(); 
     
    287287 * item in the list. */ 
    288288StreamItem* 
    289 CircuitListWidget::findStreamItem(quint64 streamid) 
     289CircuitListWidget::findStreamItem(const StreamId &streamid) 
    290290{ 
    291291  int numCircs = topLevelItemCount(); 
  • vidalia/trunk/src/vidalia/network/circuitlistwidget.h

    r2511 r2977  
    5555  void circuitSelected(Circuit circuit); 
    5656  /** Emitted when a circuit is removed from the list. */ 
    57   void circuitRemoved(quint64 circid); 
     57  void circuitRemoved(CircuitId circid); 
    5858  /** Emitted when the user selects a circuit to be closed. */ 
    59   void closeCircuit(quint64 circid); 
     59  void closeCircuit(CircuitId circid); 
    6060  /** Emitted when the user selects a stream to be closed. */ 
    61   void closeStream(quint64 streamid); 
     61  void closeStream(StreamId streamid); 
    6262  /** Emitted when the user selects a circuit to zoom to. */ 
    63   void zoomToCircuit(quint64 circid); 
     63  void zoomToCircuit(CircuitId circid); 
    6464   
    6565public slots: 
     
    8787  void removeStream(StreamItem *stream); 
    8888  /** Finds the circuit with the given ID. */ 
    89   CircuitItem* findCircuitItem(quint64 circid); 
     89  CircuitItem* findCircuitItem(const CircuitId &circid); 
    9090  /** Finds the stream with the given ID. */ 
    91   StreamItem* findStreamItem(quint64 streamid); 
     91  StreamItem* findStreamItem(const StreamId &streamid); 
    9292  /** Schedules the given circuit item to be removed after the given timeout. */ 
    9393  void scheduleCircuitRemoval(CircuitItem *circuit, int delay); 
  • vidalia/trunk/src/vidalia/network/netviewer.cpp

    r2575 r2977  
    9696  connect(ui.treeCircuitList, SIGNAL(circuitSelected(Circuit)), 
    9797          this, SLOT(circuitSelected(Circuit))); 
    98   connect(ui.treeCircuitList, SIGNAL(circuitRemoved(quint64)), 
    99           _map, SLOT(removeCircuit(quint64))); 
    100   connect(ui.treeCircuitList, SIGNAL(zoomToCircuit(quint64)), 
    101           _map, SLOT(zoomToCircuit(quint64))); 
    102   connect(ui.treeCircuitList, SIGNAL(closeCircuit(quint64)), 
    103           _torControl, SLOT(closeCircuit(quint64))); 
    104   connect(ui.treeCircuitList, SIGNAL(closeStream(quint64)), 
    105           _torControl, SLOT(closeStream(quint64))); 
     98  connect(ui.treeCircuitList, SIGNAL(circuitRemoved(CircuitId)), 
     99          _map, SLOT(removeCircuit(CircuitId))); 
     100  connect(ui.treeCircuitList, SIGNAL(zoomToCircuit(CircuitId)), 
     101          _map, SLOT(zoomToCircuit(CircuitId))); 
     102  connect(ui.treeCircuitList, SIGNAL(closeCircuit(CircuitId)), 
     103          _torControl, SLOT(closeCircuit(CircuitId))); 
     104  connect(ui.treeCircuitList, SIGNAL(closeStream(StreamId)), 
     105          _torControl, SLOT(closeStream(StreamId))); 
    106106 
    107107  /* Respond to changes in the status of the control connection */ 
     
    331331/** Adds an IP address to the resolve queue and updates the queue timers. */ 
    332332void 
    333 NetViewer::addToResolveQueue(QHostAddress ip, QString id) 
     333NetViewer::addToResolveQueue(const QHostAddress &ip, const QString &id) 
    334334{ 
    335335  QString ipstr = ip.toString(); 
     
    359359 * list. */ 
    360360void 
    361 NetViewer::circuitSelected(Circuit circuit) 
     361NetViewer::circuitSelected(const Circuit &circuit) 
    362362{ 
    363363  /* Clear any selected items. */ 
     
    383383/** Called when the user selects a router from the router list. */ 
    384384void 
    385 NetViewer::routerSelected(RouterDescriptor router) 
     385NetViewer::routerSelected(const RouterDescriptor &router) 
    386386{ 
    387387  _map->deselectAll(); 
     
    415415/** Called when a list of GeoIp information has been resolved. */ 
    416416void 
    417 NetViewer::resolved(int id, QList<GeoIp> geoips) 
     417NetViewer::resolved(int id, const QList<GeoIp> &geoips) 
    418418{ 
    419419  Q_UNUSED(id); 
  • vidalia/trunk/src/vidalia/network/netviewer.h

    r2511 r2977  
    6262  void refresh(); 
    6363  /** Called when the user selects a circuit on the circuit list */ 
    64   void circuitSelected(Circuit circuit); 
     64  void circuitSelected(const Circuit &circuit); 
    6565  /** Called when an IP has been resolved to geographic information. */ 
    66   void resolved(int id, QList<GeoIp> geoips); 
     66  void resolved(int id, const QList<GeoIp> &geoips); 
    6767  /** Called when the user selects a router in the list. */ 
    68   void routerSelected(RouterDescriptor router); 
     68  void routerSelected(const RouterDescriptor &router); 
    6969  /** Handles when we get connected to Tor network */ 
    7070  void onAuthenticated(); 
     
    7676private: 
    7777  /** Adds an IP address to the resolve queue and updates the queue timers. */ 
    78   void addToResolveQueue(QHostAddress ip, QString id); 
     78  void addToResolveQueue(const QHostAddress &ip, const QString &id); 
    7979  /** Retrieves a list of all running routers from Tor and their descriptors, 
    8080   * and adds them to the RouterListWidget. */ 
  • vidalia/trunk/src/vidalia/network/streamitem.h

    r2362 r2977  
    3131  void update(const Stream &stream); 
    3232  /** Returns the ID of the stream associated with this tree item. */ 
    33   quint64 id() const { return _id; } 
     33  StreamId id() const { return _id; } 
    3434   
    3535private: 
    36   quint64 _id; /**< ID for this stream. */ 
     36  StreamId _id; /**< ID for this stream. */ 
    3737}; 
    3838 
  • vidalia/trunk/src/vidalia/network/tormapwidget.cpp

    r2362 r2977  
    797