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