Vidalia  0.3.1
Stream.h
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If
4 ** you did not receive the LICENSE file with this file, you may obtain it
5 ** from the Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file Stream.h
13 ** \brief Object representing a Tor stream
14 */
15 
16 #ifndef _STREAM_H
17 #define _STREAM_H
18 
19 #include "Circuit.h"
20 
21 #include <QCoreApplication>
22 #include <QString>
23 #include <QObject>
24 #include <QList>
25 #include <QMetaType>
26 
27 /** Stream IDs contains 1-16 alphanumeric ASCII characters. */
28 typedef QString StreamId;
29 
30 
31 class Stream
32 {
33  Q_DECLARE_TR_FUNCTIONS(Stream)
34 
35 public:
36  /** Stream status values */
37  enum Status {
38  Unknown, /**< Unknown status type given */
39  New, /**< New request to connect */
40  NewResolve, /**< New request to resolve an address */
41  SentConnect, /**< Sent a connect cell */
42  SentResolve, /**< Sent a resolve cell */
43  Succeeded, /**< Stream established */
44  Failed, /**< Stream failed */
45  Closed, /**< Stream closed */
46  Detached, /**< Detached from circuit */
47  Remap /**< Address re-mapped to another */
48  };
49 
50  /** Default constructor */
51  Stream();
52  /** Constructor */
53  Stream(const StreamId &streamId, Status status, const CircuitId &circuitId,
54  const QString &target);
55  /** Constructor */
56  Stream(const StreamId &streamId, Status status, const CircuitId &circuitId,
57  const QString &address, quint16 port);
58 
59  /** Parses the given string for a stream, in Tor control protocol format. */
60  static Stream fromString(const QString &stream);
61  /** Converts a string description of a stream's status to its enum value */
62  static Status toStatus(const QString &strStatus);
63 
64  /** Returns true iff the Stream object's fields are all valid. */
65  bool isValid() const;
66 
67  /** Returns the ID for this stream. */
68  StreamId id() const { return _streamId; }
69  /** Returns the status for this stream. */
70  Status status() const { return _status; }
71  /** Returns a string representation of this stream's status. */
72  QString statusString() const;
73  /** Returns the ID of the circuit to which this stream is assigned. */
74  CircuitId circuitId() const { return _circuitId; }
75  /** Returns the target address and port for this stream. */
76  QString target() const { return (_address + ":" + QString::number(_port)); }
77  /** Returns the target address for this stream. */
78  QString targetAddress() const { return _address; }
79  /** Returns the target port for this stream. */
80  quint16 targetPort() const { return _port; }
81 
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 
86 private:
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. */
90  Status _status; /**< Stream status value. */
91  quint16 _port; /**< Stream target port. */
92 };
93 
95 
96 /** A collection of Stream objects. */
97 typedef QList<Stream> StreamList;
98 
99 #endif
100 
Stream::SentConnect
@ SentConnect
Definition: Stream.h:41
Stream::Failed
@ Failed
Definition: Stream.h:44
Stream::New
@ New
Definition: Stream.h:39
Stream::status
Status status() const
Definition: Stream.h:70
Stream::fromString
static Stream fromString(const QString &stream)
Definition: Stream.cpp:63
Stream::_port
quint16 _port
Definition: Stream.h:91
Stream::SentResolve
@ SentResolve
Definition: Stream.h:42
Stream::id
StreamId id() const
Definition: Stream.h:68
Stream::targetAddress
QString targetAddress() const
Definition: Stream.h:78
Stream::NewResolve
@ NewResolve
Definition: Stream.h:40
Stream::_status
Status _status
Definition: Stream.h:90
Stream::toStatus
static Status toStatus(const QString &strStatus)
Definition: Stream.cpp:100
Stream::target
QString target() const
Definition: Stream.h:76
Stream::circuitId
CircuitId circuitId() const
Definition: Stream.h:74
StreamId
QString StreamId
Definition: Stream.h:28
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(Stream)
StreamList
QList< Stream > StreamList
Definition: Stream.h:97
Stream::Succeeded
@ Succeeded
Definition: Stream.h:43
Stream::Remap
@ Remap
Definition: Stream.h:47
Stream
Definition: Stream.h:31
Stream::statusString
QString statusString() const
Definition: Stream.cpp:126
Stream::_circuitId
CircuitId _circuitId
Definition: Stream.h:88
Stream::_streamId
StreamId _streamId
Definition: Stream.h:87
Stream::Stream
Stream()
Definition: Stream.cpp:23
Circuit.h
Stream::_address
QString _address
Definition: Stream.h:89
Stream::Detached
@ Detached
Definition: Stream.h:46
Stream::targetPort
quint16 targetPort() const
Definition: Stream.h:80
Stream::Unknown
@ Unknown
Definition: Stream.h:38
Stream::isValid
bool isValid() const
Definition: Stream.cpp:146
Stream::Status
Status
Definition: Stream.h:37
CircuitId
QString CircuitId
Definition: Circuit.h:24
Stream::Closed
@ Closed
Definition: Stream.h:45
Stream::isValidStreamId
static bool isValidStreamId(const StreamId &streamId)
Definition: Stream.cpp:84