Yate
|
A Jabber stream. More...
#include <yatejabber.h>
Public Types | |
enum | Type { c2s = 0, s2s, comp, cluster, TypeCount } |
enum | State { Idle = 0, Connecting, WaitStart, Starting, Features, WaitTlsRsp, Securing, Auth, Challenge, Compressing, Register, Running, Destroy } |
enum | Flags { NoAutoRestart = 0x00000001, TlsRequired = 0x00000002, AllowPlainAuth = 0x00000004, DialbackOnly = 0x00000008, RegisterUser = 0x00000010, Compress = 0x00000020, InError = 0x00000080, RosterRequested = 0x00000100, AvailableResource = 0x00000200, PositivePriority = 0x00000400, SetCompressed = 0x00010000, StreamSecured = 0x00020000, StreamTls = 0x00040000, StreamAuthenticated = 0x00080000, StreamRemoteVer1 = 0x00100000, StreamLocalVer1 = 0x00200000, StreamWaitBindRsp = 0x01000000, StreamWaitSessRsp = 0x02000000, StreamWaitChallenge = 0x04000000, StreamWaitChgRsp = 0x08000000, StreamRfc3920Chg = 0x10000000, StreamCompressed = 0x20000000, StreamCanCompress = 0x40000000, StreamFlags = 0x000000ff, InternalFlags = 0xffff0000 } |
Public Member Functions | |
virtual | ~JBStream () |
int | type () const |
int | xmlns () const |
State | state () const |
bool | incoming () const |
bool | outgoing () const |
JBEngine * | engine () const |
const char * | name () const |
const String & | id () const |
bool | isId (const String &str) |
const JabberID & | local () const |
void | local (JabberID &jid) |
void | setLocal (const char *jid) |
const JabberID & | remote () const |
void | remote (JabberID &jid) |
bool | remoteAddr (SocketAddr &addr) |
bool | localAddr (SocketAddr &addr) |
int | flags () const |
bool | flag (int mask) const |
void | setTlsRequired (bool set) |
bool | haveData () |
void | connectAddr (String &addr, int &port, String &localip, int &stat, ObjList &srvs) const |
const String & | serverHost () const |
void | setRosterRequested (bool ok) |
bool | setAvailableResource (bool ok, bool positive=true) |
bool | readSocket (char *buf, unsigned int len) |
virtual JBClientStream * | clientStream () |
virtual JBServerStream * | serverStream () |
virtual JBClusterStream * | clusterStream () |
JBEvent * | getEvent (u_int64_t time=Time::msecNow()) |
bool | sendStanza (XmlElement *&xml) |
bool | sendStreamXml (State newState, XmlElement *first, XmlElement *second=0, XmlElement *third=0) |
void | start (XMPPFeatureList *features=0, XmlElement *caps=0, bool useVer1=true) |
bool | authenticated (bool ok, const String &rsp=String::empty(), XMPPError::Type error=XMPPError::NotAuthorized, const char *username=0, const char *id=0, const char *resource=0) |
void | terminate (int location, bool destroy, XmlElement *xml, int error=XMPPError::NoError, const char *reason="", bool final=false) |
virtual void | connectTerminated (Socket *&sock) |
virtual bool | connecting (bool sync, int stat, ObjList &srvs) |
virtual void * | getObject (const String &name) const |
const char * | stateName () const |
const char * | typeName () const |
void | buildSha1Digest (String &buf, const String &secret) |
virtual const String & | toString () const |
Static Public Member Functions | |
static Type | lookupType (const char *text, Type defVal=TypeCount) |
Public Attributes | |
SASL * | m_sasl |
Static Public Attributes | |
static const TokenDict | s_stateName [] |
static const TokenDict | s_flagName [] |
static const TokenDict | s_typeName [] |
Protected Member Functions | |
JBStream (JBEngine *engine, Socket *socket, Type t, bool ssl=false) | |
JBStream (JBEngine *engine, Type t, const JabberID &local, const JabberID &remote, const char *name=0, const NamedList *params=0, const char *serverHost=0) | |
virtual void | destroyed () |
virtual bool | canProcess (u_int64_t time) |
virtual void | process (u_int64_t time) |
virtual bool | processRunning (XmlElement *xml, const JabberID &from, const JabberID &to) |
virtual void | checkTimeouts (u_int64_t time) |
virtual void | resetConnection (Socket *sock=0) |
virtual XmlElement * | buildStreamStart () |
virtual bool | processStart (const XmlElement *xml, const JabberID &from, const JabberID &to) |
virtual bool | processAuth (XmlElement *xml, const JabberID &from, const JabberID &to) |
virtual bool | processCompressing (XmlElement *xml, const JabberID &from, const JabberID &to) |
virtual bool | processRegister (XmlElement *xml, const JabberID &from, const JabberID &to) |
bool | processStreamStart (const XmlElement *xml) |
bool | handleCompressReq (XmlElement *xml) |
bool | streamError (XmlElement *xml) |
bool | getJids (XmlElement *xml, JabberID &from, JabberID &to) |
bool | checkStanzaRecv (XmlElement *xml, JabberID &from, JabberID &to) |
void | changeState (State newState, u_int64_t time=Time::msecNow()) |
XmlElement * | checkCompress () |
void | checkPendingEvent () |
bool | sendPending (bool streamOnly=false) |
bool | writeSocket (const void *data, unsigned int &len) |
void | updateFromRemoteDef () |
XMPPFeature * | firstRequiredFeature () |
bool | dropXml (XmlElement *&xml, const char *reason) |
bool | destroyDropXml (XmlElement *&xml, XMPPError::Type error, const char *reason) |
void | setFlags (int mask) |
void | resetFlags (int mask) |
void | setSecured () |
void | setIdleTimer (u_int64_t msecNow=Time::msecNow()) |
Protected Attributes | |
State | m_state |
String | m_id |
JabberID | m_local |
JabberID | m_remote |
String | m_serverHost |
int | m_flags |
XMPPNamespace::Type | m_xmlns |
XMPPFeatureList | m_features |
JBEvent * | m_lastEvent |
ObjList | m_events |
ObjList | m_pending |
u_int64_t | m_setupTimeout |
u_int64_t | m_startTimeout |
u_int64_t | m_pingTimeout |
u_int64_t | m_nextPing |
u_int64_t | m_idleTimeout |
u_int64_t | m_connectTimeout |
unsigned int | m_restart |
u_int64_t | m_timeToFillRestart |
String | m_pingId |
Friends | |
class | JBEngine |
class | JBEvent |
A Jabber stream.
Base class for all Jabber streams. Basic stream data processing: send/receive XML elements, keep stream state, generate events
virtual ~JBStream | ( | ) | [virtual] |
Destructor. Gracefully close the stream and the socket
bool authenticated | ( | bool | ok, |
const String & | rsp = String::empty() , |
||
XMPPError::Type | error = XMPPError::NotAuthorized , |
||
const char * | username = 0 , |
||
const char * | id = 0 , |
||
const char * | resource = 0 |
||
) |
Auth event result. This method should be called by the upper layer when processing an Auth event This method is thread safe
ok | True if the remote party was authenticated, false if authentication failed |
rsp | Optional success response content. Ignored if not authenticated |
error | Failure reason. Ignored if authenticated |
username | Authenticated user |
id | Non SASL auth response id |
resource | Client resource to set when non SASL authentication is used |
void buildSha1Digest | ( | String & | buf, |
const String & | secret | ||
) | [inline] |
Build a SHA1 digest from stream id and secret
buf | Destination buffer |
secret | The secret |
References SHA1::hexDigest(), and String::toLower().
virtual XmlElement* buildStreamStart | ( | ) | [protected, virtual] |
Build a stream start XML element
Reimplemented in JBClusterStream, and JBServerStream.
virtual bool canProcess | ( | u_int64_t | time | ) | [protected, virtual] |
Check if stream state processor can continue. This method is called from getEvent() with the stream locked
time | Current time |
void changeState | ( | State | newState, |
u_int64_t | time = Time::msecNow() |
||
) | [protected] |
Change stream state. Reset state depending data
newState | The new stream state |
time | Current time |
XmlElement* checkCompress | ( | ) | [protected] |
Check if the stream compress flag is set and compression was offered by remote party
void checkPendingEvent | ( | ) | [protected] |
Check for pending events. Set the last event
bool checkStanzaRecv | ( | XmlElement * | xml, |
JabberID & | from, | ||
JabberID & | to | ||
) | [protected] |
Check if a received element is a presence, message or iq qualified by the stream namespace and the stream is not authenticated. Validate 'from' for c2s streams Validate s2s 'to' domain and 'from' jid Fix 'from' or 'to' is needed
xml | Received xml element (will be consumed if false is returned) |
from | The sender of the stanza |
to | Stanza recipient |
virtual void checkTimeouts | ( | u_int64_t | time | ) | [protected, virtual] |
Check stream timeouts. This method is called from getEvent() with the stream locked, after the process() method returned without setting the last event
time | Current time |
virtual JBClientStream* clientStream | ( | ) | [inline, virtual] |
Get a client stream from this one
Reimplemented in JBClientStream.
virtual JBClusterStream* clusterStream | ( | ) | [inline, virtual] |
Get a cluster stream from this one
Reimplemented in JBClusterStream.
void connectAddr | ( | String & | addr, |
int & | port, | ||
String & | localip, | ||
int & | stat, | ||
ObjList & | srvs | ||
) | const |
Retrieve connection address(es), port and status This method is not thread safe
addr | The remote ip |
port | The remote port |
localip | Local ip to bind |
stat | Current connect status |
srvs | List to copy stream SRV records |
virtual bool connecting | ( | bool | sync, |
int | stat, | ||
ObjList & | srvs | ||
) | [virtual] |
Connecting notification. Start connect timer for synchronous connect This method is thread safe
sync | True if the connection is synchronous |
stat | Current status of the connect thread |
srvs | Current list of SRV records in the connect thread |
virtual void connectTerminated | ( | Socket *& | sock | ) | [virtual] |
Outgoing stream connect terminated notification. Send stream start if everithing is ok
sock | The connected socket, will be consumed and zeroed |
bool destroyDropXml | ( | XmlElement *& | xml, |
XMPPError::Type | error, | ||
const char * | reason | ||
) | [inline, protected] |
Terminate (destroy) the stream. Drop (delete) received XML element
xml | The element to delete |
error | Terminate error |
reason | Drop reason |
virtual void destroyed | ( | ) | [protected, virtual] |
Close the stream. Release memory
Reimplemented from RefObject.
Reimplemented in JBServerStream, and JBClientStream.
bool dropXml | ( | XmlElement *& | xml, |
const char * | reason | ||
) | [protected] |
Drop (delete) received XML element
xml | The element to delete |
reason | The reason |
Get the stream's owner
XMPPFeature* firstRequiredFeature | ( | ) | [protected] |
Retrieve the first required feature in the list
bool flag | ( | int | mask | ) | const [inline] |
Check if a given option (or option mask) is set
mask | The flag(s) to check |
JBEvent* getEvent | ( | u_int64_t | time = Time::msecNow() | ) |
bool getJids | ( | XmlElement * | xml, |
JabberID & | from, | ||
JabberID & | to | ||
) | [protected] |
Retrieve and check the 'from' and 'to' JIDs from a receive element
xml | Received xml element |
from | Jabber ID to set from the 'from' attribute |
to | Jabber ID to set from the 'to' attribute |
Get an object from this stream
name | The name of the object to get |
Reimplemented from RefObject.
bool handleCompressReq | ( | XmlElement * | xml | ) | [protected] |
Handle an already checked (tag and namespace) compress request Respond to it. Change stream state on success
xml | Received xml element (will be consumed) |
bool haveData | ( | ) |
Check if the stream has valid pending data (received xml elements in queue or pending events or pending xml elements that can still be sent). This method is thread safe
bool incoming | ( | ) | const [inline] |
Get the stream direction
Check if the stream id equals a given string. This method is thread safe
str | The string to check |
Get the JID of the local side of this stream
Get the JID of the local side of this stream. This method is thread safe
jid | The JID to be filled with the local side of this stream |
bool localAddr | ( | SocketAddr & | addr | ) | [inline] |
Get the local address This method is thread safe
addr | The socket address to be filled with local address |
static Type lookupType | ( | const char * | text, |
Type | defVal = TypeCount |
||
) | [inline, static] |
Get the stream type associated with a given text
text | Stream type text to find |
defVal | Value to return if not found |
References TelEngine::lookup().
const char* name | ( | ) | const [inline] |
Get the stream's name
bool outgoing | ( | ) | const [inline] |
Get the stream direction
virtual void process | ( | u_int64_t | time | ) | [protected, virtual] |
Process stream state. Get XML from parser's queue and process it This method is called from getEvent() with the stream locked
time | Current time |
virtual bool processAuth | ( | XmlElement * | xml, |
const JabberID & | from, | ||
const JabberID & | to | ||
) | [protected, virtual] |
Process elements in Auth state
xml | Received element (will be consumed) |
from | Already parsed source JID |
to | Already parsed destination JID |
Reimplemented in JBServerStream, and JBClientStream.
virtual bool processCompressing | ( | XmlElement * | xml, |
const JabberID & | from, | ||
const JabberID & | to | ||
) | [protected, virtual] |
Process elements in Compressing state
xml | Received element (will be consumed) |
from | Already parsed source JID |
to | Already parsed destination JID |
virtual bool processRegister | ( | XmlElement * | xml, |
const JabberID & | from, | ||
const JabberID & | to | ||
) | [protected, virtual] |
Process elements in Register state
xml | Received element (will be consumed) |
from | Already parsed source JID |
to | Already parsed destination JID |
Reimplemented in JBClientStream.
virtual bool processRunning | ( | XmlElement * | xml, |
const JabberID & | from, | ||
const JabberID & | to | ||
) | [protected, virtual] |
Process elements in Running state
xml | Received element (will be consumed) |
from | Already parsed source JID |
to | Already parsed destination JID |
Reimplemented in JBClusterStream, JBServerStream, and JBClientStream.
virtual bool processStart | ( | const XmlElement * | xml, |
const JabberID & | from, | ||
const JabberID & | to | ||
) | [protected, virtual] |
Process stream start elements while waiting for them
xml | Received xml element |
from | The 'from' attribute |
to | The 'to' attribute |
Reimplemented in JBClusterStream, JBServerStream, and JBClientStream.
bool processStreamStart | ( | const XmlElement * | xml | ) | [protected] |
Check if a received stream start element is correct. Check namespaces and set stream version Check and set the id for outgoing streams Generate an id for incoming streams Terminate the stream if this conditions are met
xml | Received xml element |
bool readSocket | ( | char * | buf, |
unsigned int | len | ||
) |
Read data from socket. Send it to the parser. Terminate the stream on socket or parser error
buf | Destination buffer |
len | Buffer length (must be greater then 1) |
Get the JID of the remote side of this stream
Get the JID of the remote side of this stream. This method is thread safe
jid | The JID to be filled with the remote side of this stream |
bool remoteAddr | ( | SocketAddr & | addr | ) | [inline] |
Get the remote party's address This method is thread safe
addr | The socket address to be filled with remote party's address |
virtual void resetConnection | ( | Socket * | sock = 0 | ) | [protected, virtual] |
Reset the stream's connection. Build a new XML parser if the socket is valid Release the old connection
sock | The new socket |
void resetFlags | ( | int | mask | ) | [protected] |
Reset stream flag mask
mask | The bit mask to reset |
bool sendPending | ( | bool | streamOnly = false | ) | [protected] |
Send pending stream XML or stanzas Terminate the stream on error
streamOnly | Try to send only existing stream related XML elements |
bool sendStanza | ( | XmlElement *& | xml | ) |
Send a stanza ('iq', 'message' or 'presence') or dialback elements in Running state. This method is thread safe
xml | Element to send (will be consumed and zeroed) |
bool sendStreamXml | ( | State | newState, |
XmlElement * | first, | ||
XmlElement * | second = 0 , |
||
XmlElement * | third = 0 |
||
) |
Send stream related XML when negotiating the stream or some other stanza in non Running state All elements will be consumed This method is thread safe
newState | The new stream state to set on success |
first | The first element to send |
second | Optional second element to send |
third | Optional third element to send |
const String& serverHost | ( | ) | const [inline] |
Retrieve server host when connecting. This method is not thread safe
virtual JBServerStream* serverStream | ( | ) | [inline, virtual] |
Get a server stream from this one
Reimplemented in JBServerStream.
bool setAvailableResource | ( | bool | ok, |
bool | positive = true |
||
) |
Set/reset AvailableResource/PositivePriority flags This method is thread safe
ok | True to set, false to reset it |
positive | True if an available resource has positive priority |
void setFlags | ( | int | mask | ) | [protected] |
Set stream flag mask
mask | The bit mask to set |
void setIdleTimer | ( | u_int64_t | msecNow = Time::msecNow() | ) | [protected] |
Set the idle timer in Running state
msecNow | Current time in milliseconds |
void setLocal | ( | const char * | jid | ) | [inline] |
void setRosterRequested | ( | bool | ok | ) |
Set/reset RosterRequested flag This method is thread safe
ok | True to set, false to reset it |
void setSecured | ( | ) | [inline, protected] |
Set secured flag. Remove feature from list
void setTlsRequired | ( | bool | set | ) | [inline] |
Set or reset the TLS required flag This method is not thread safe
set | True to set, false to reset the flag |
void start | ( | XMPPFeatureList * | features = 0 , |
XmlElement * | caps = 0 , |
||
bool | useVer1 = true |
||
) |
Start the stream. This method should be called by the upper layer when processing an incoming stream Start event This method is thread safe
features | Optional features to advertise to the remote party of an incoming stream. The caller is responsable of freeing it. If processed, list's elements will be moved to stream's features list |
caps | Optional entity capabilities to be added to the stream features. Ignored for outgoing streams |
useVer1 | Advertise RFC3920 version. Ignored for outgoing streams |
const char* stateName | ( | ) | const [inline] |
bool streamError | ( | XmlElement * | xml | ) | [protected] |
Check if a received element is a stream error one
xml | Received xml element |
void terminate | ( | int | location, |
bool | destroy, | ||
XmlElement * | xml, | ||
int | error = XMPPError::NoError , |
||
const char * | reason = "" , |
||
bool | final = false |
||
) |
Terminate the stream. Send stream end tag or error. Reset the stream. Deref stream if destroying. This method is thread safe
location | The terminate request location: -1: upper layer, 0: internal, 1: remote |
destroy | True to destroy. False to terminate |
xml | Received XML element. The element will be consumed |
error | Termination reason. Set it to NoError to send stream end tag |
reason | Optional text to be added to the error stanza |
final | True if called from destructor |
int type | ( | ) | const [inline] |
Get the type of this stream. See the protocol enumeration of the engine
const char* typeName | ( | ) | const [inline] |
void updateFromRemoteDef | ( | ) | [protected] |
Update stream flags and remote connection data from engine
bool writeSocket | ( | const void * | data, |
unsigned int & | len | ||
) | [protected] |
Write data to socket. Terminate the stream on socket error
data | Buffer to sent |
len | The number of bytes to send. Filled with actually sent bytes on exit |
int xmlns | ( | ) | const [inline] |
Retrieve this stream's default namespace
const TokenDict s_flagName[] [static] |
Dictionary keeping the flag names
const TokenDict s_stateName[] [static] |
Dictionary keeping the stream state names
const TokenDict s_typeName[] [static] |
Dictionary keeping the stream type names