Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : */
4 :
5 : #ifndef __XMPP_STANZA_H__
6 : #define __XMPP_STANZA_H__
7 :
8 : #include "xmpp/xmpp_str.h"
9 : #include "xml/xml_base.h"
10 :
11 : class XmppConnection;
12 :
13 : class XmppStanza {
14 : public:
15 : enum XmppMessageType {
16 : INVALID = 0,
17 : STREAM_HEADER = 1,
18 : MESSAGE_STANZA = 2,
19 : IQ_STANZA = 3,
20 : WHITESPACE_MESSAGE_STANZA = 4,
21 : RESERVED_STANZA = 5
22 : };
23 :
24 : enum XmppStanzaErrorType {
25 : NONE = 0,
26 : BAD_REQUEST = 1,
27 : CONFLICT = 2,
28 : FEATURE_NOT_IMPLEMENTED = 3,
29 : FORBIDDEN = 4,
30 : GONE = 5,
31 : INTERNAL_SERVER_ERROR = 6,
32 : ITEMS_NOT_FOUND = 7,
33 : JID_MALFORMED = 8,
34 : NOT_ACCEPTABLE = 9,
35 : NOT_ALLOWED = 10,
36 : NOT_AUTHORIZED = 11,
37 : POLICY_VIOLATION = 12,
38 : RECIPIENT_UNAVAILABLE = 13,
39 : REDIRECT = 14,
40 : REGISTRATION_REQUIRED = 15,
41 : REMOTE_SERVER_NOT_FOUND = 16,
42 : REMOTE_SERVER_TIMEOUT = 17,
43 : RESOURCE_CONSTRAINT = 18,
44 : SERVICE_UNAVAILABLE = 19,
45 : SUBSCRIPTION_REQUIRED = 20,
46 : UNDEFINED_CONDITION = 21,
47 : UNEXPECTED_REQUEST = 22,
48 : APPLICATION_SEPCIFIC_CONDITION = 23
49 : };
50 :
51 : class XmppMessage {
52 : public:
53 4166748 : explicit XmppMessage(XmppMessageType type) :
54 4166748 : type(type), from(""), to("") {
55 4166713 : }
56 6638163 : virtual ~XmppMessage(){ }
57 : XmppMessageType type;
58 : XmppStanzaErrorType error;
59 : std::string from;
60 : std::string to;
61 : std::string xmlns;
62 : std::unique_ptr<XmlBase> dom;
63 :
64 0 : bool IsValidType(XmppMessageType type) const {
65 0 : return (type > INVALID && type < RESERVED_STANZA);
66 : };
67 : private:
68 : XmppMessage(const XmppMessage&) = delete;
69 : XmppMessage(XmppMessage&&) = delete;
70 : const XmppMessage& operator = (const XmppMessage&) = delete;
71 : const XmppMessage& operator = (XmppMessage&&) = delete;
72 : };
73 :
74 : struct XmppStreamMessage : XmppMessage {
75 40465 : XmppStreamMessage() : XmppMessage(STREAM_HEADER) {
76 40460 : }
77 :
78 : enum XmppStreamMsgType {
79 : STREAM_NONE = 0,
80 : INIT_STREAM_HEADER = 1,
81 : INIT_STREAM_HEADER_RESP = 2,
82 : FEATURE_SASL = 3,
83 : FEATURE_TLS = 4,
84 : FEAUTRE_COMPRESS_LZW = 5,
85 : CLOSE_STREAM = 6
86 : };
87 :
88 : enum XmppStreamSASL {
89 : };
90 :
91 :
92 : enum XmppStreamTlsType {
93 : TLS_FEATURE_REQUEST = 1,
94 : TLS_START = 2,
95 : TLS_PROCEED = 3
96 : };
97 :
98 : XmppStreamMsgType strmtype;
99 : XmppStreamTlsType strmtlstype;
100 : };
101 :
102 : enum XmppMessageStateType {
103 : STATE_NONE = 0, STATE_ACTIVE = 1, STATE_COMPOSING = 2,
104 : STATE_PAUSED = 3, STATE_INACTIVE = 4, STATE_GONE = 5
105 : };
106 :
107 :
108 : struct XmppChatMessage : public XmppMessage {
109 1549872 : explicit XmppChatMessage(XmppMessageStateType stype):
110 1549872 : XmppMessage(MESSAGE_STANZA), state(stype) {
111 1549872 : };
112 : enum XmppMessageSubtype {
113 : NORMAL = 1,
114 : CHAT = 2,
115 : GROUPCHAT = 3,
116 : HEADLINE = 4,
117 : ERROR = 5
118 : };
119 :
120 : XmppMessageStateType state;
121 : XmppMessageSubtype stype;
122 : };
123 :
124 : struct XmppMessagePresence : public XmppMessage {
125 : enum XmppPresenceSubtype {
126 : SUBSCRIBED = 1,
127 : PROBE = 2,
128 : UNAVAILABLE = 3
129 : };
130 :
131 : enum XmppPresenceShowType {
132 : CHAT = 1, AWAY = 2, XA = 3, DND = 4
133 : };
134 :
135 : XmppPresenceSubtype stype;
136 : XmppPresenceShowType show;
137 : std::string timestamp;
138 : std::string statusDescr;
139 : int16_t priority;
140 : };
141 :
142 : struct XmppMessageIq : public XmppMessage {
143 98564 : XmppMessageIq() : XmppMessage(IQ_STANZA) {
144 98561 : }
145 : enum XmppIqSubtype {
146 : GET = 1,
147 : SET = 2,
148 : RESULT = 3,
149 : ERROR = 4
150 : };
151 :
152 : enum XmppIqErrorType {
153 : AUTH = 1, CANCEL = 2, CONTINUE = 3, MODIFY = 4, WAIT = 5
154 : };
155 :
156 : XmppIqSubtype stype;
157 : std::string node;
158 : std::string id;
159 : std::string iq_type;
160 : std::string action;
161 : std::string as_node;
162 : bool is_as_node;
163 : };
164 :
165 : XmppStanza();
166 :
167 4152449 : static XmlBase *AllocXmppXmlImpl(const char *doc = NULL) {
168 4152449 : XmlBase *tmp = XmppXmlImplFactory::Instance()->GetXmlImpl();
169 4152412 : BOOST_ASSERT(tmp);
170 4152412 : if (doc) tmp->LoadDoc(doc);
171 4152411 : return tmp;
172 : }
173 :
174 : private:
175 : DISALLOW_COPY_AND_ASSIGN(XmppStanza);
176 : };
177 :
178 : class XmppProto : public XmppStanza {
179 : public:
180 :
181 : static XmppStanza::XmppMessage *Decode(const XmppConnection *connection,
182 : const std::string &ts);
183 : static int EncodeStream(const XmppStreamMessage &str, std::string &to,
184 : std::string &from, const std::string &xmlns,
185 : uint8_t *data, size_t size);
186 : static int EncodeStream(const XmppMessage &str, uint8_t *data, size_t size);
187 : static int EncodeMessage(const XmppChatMessage *, uint8_t *data, size_t size);
188 : static int EncodeMessage(XmlBase *, uint8_t *data, size_t size);
189 : static int EncodePresence(uint8_t *data, size_t size);
190 : static int EncodeIq(const XmppMessageIq *iq, XmlBase *doc,
191 : uint8_t *data, size_t size);
192 :
193 : private:
194 : static int EncodeOpen(uint8_t *data, std::string &to, std::string &from,
195 : const std::string &xmlns, size_t size);
196 : static int EncodeOpenResp(uint8_t *data, std::string &to, std::string &from,
197 : size_t size);
198 : static int EncodeFeatureTlsRequest(uint8_t *data);
199 : static int EncodeFeatureTlsStart(uint8_t *data);
200 : static int EncodeFeatureTlsProceed(uint8_t *data);
201 : static int EncodeWhitespace(uint8_t *data);
202 : static int SetTo(std::string &to, XmlBase *doc);
203 : static int SetFrom(std::string &from, XmlBase *doc);
204 : static int SetXmlns(const std::string &from, XmlBase *doc);
205 :
206 : static const char *GetId(XmlBase *doc);
207 : static const char *GetType(XmlBase *doc);
208 : static const char *GetTo(XmlBase *doc);
209 : static const char *GetFrom(XmlBase *doc);
210 : static const char *GetXmlns(XmlBase *doc);
211 : static const char *GetAction(XmlBase *doc, const std::string &str);
212 : static const char *GetNode(XmlBase *doc, const std::string &str);
213 : static const char *GetAsNode(XmlBase *doc);
214 : static const char *GetDsNode(XmlBase *doc);
215 :
216 : static XmppStanza::XmppMessage *DecodeInternal(
217 : const XmppConnection *connection, const std::string &ts,
218 : XmlBase *impl);
219 :
220 : static std::unique_ptr<XmlBase> open_doc_;
221 :
222 : XmppProto();
223 : ~XmppProto();
224 :
225 : DISALLOW_COPY_AND_ASSIGN(XmppProto);
226 : };
227 :
228 : #endif // __XMPP_STANZA_H__
|