summaryrefslogtreecommitdiff
path: root/protocols/JabberG/src/jabber_strm_mgmt.cpp
blob: 485174e42d66037527581df940832bc110d6de3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*

Jabber Protocol Plugin for Miranda NG

Copyright (c) 2018 Miranda NG team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "stdafx.h"
#include "jabber_strm_mgmt.h"

strm_mgmt::strm_mgmt(CJabberProto *_proto) : proto(_proto), m_bStrmMgmtPendingEnable(false),
m_bStrmMgmtEnabled(false),
m_bStrmMgmtResumeSupported(false),
bSessionResumed(false)
{

}

void strm_mgmt::OnProcessEnabled(HXML node, ThreadData * /*info*/)
{
	m_bStrmMgmtEnabled = true;
	auto val = XmlGetAttrValue(node, L"max");
	if(val)
		m_nStrmMgmtResumeMaxSeconds = _wtoi(val);
	val = XmlGetAttrValue(node, L"resume");
	if (val)
	{
		if (mir_wstrcmp(val, L"true") || mir_wstrcmp(val, L"1"))
		{
			m_bStrmMgmtResumeSupported = true;
			m_sStrmMgmtResumeId = XmlGetAttrValue(node, L"id");
		}
	}
	//TODO: handle 'location'
	m_nStrmMgmtLocalHCount = 0;
	m_nStrmMgmtSrvHCount = 0;
}

void strm_mgmt::OnProcessResumed(HXML node, ThreadData * /*info*/)
{
	if (mir_wstrcmp(XmlGetAttrValue(node, L"xmlns"), L"urn:xmpp:sm:3"))
		return;
	auto var = XmlGetAttrValue(node, L"previd");
	if (!var)
		return;
	if (m_sStrmMgmtResumeId != var)
		return; //TODO: unknown session, what we should do ?
	var = XmlGetAttrValue(node, L"h");
	if (!var)
		return;
	bSessionResumed = true;
	m_nStrmMgmtSrvHCount = _wtoi(var);
	int size = m_nStrmMgmtLocalSCount - m_nStrmMgmtSrvHCount;
	if (size < 0)
	{
		//TODO: this should never happen, indicates server side bug
		//TODO: once our client side implementation good enough, abort stream in this case, noop for now
	}
	else if (size > 0 && !NodeCache.empty()) //TODO: NodeCache cannot be empty if size >0, it's a bug
		ResendNodes(size);
	else
	{
		for (auto i : NodeCache)
			xmlFree(i);
		NodeCache.clear();
	}
}

void strm_mgmt::OnProcessSMa(HXML node)
{
	if (mir_wstrcmp(XmlGetAttrValue(node, L"xmlns"), L"urn:xmpp:sm:3"))
		return;
	auto val = XmlGetAttrValue(node, L"h");
	m_nStrmMgmtSrvHCount = _wtoi(val);
	int size = m_nStrmMgmtLocalSCount - m_nStrmMgmtSrvHCount;
	if (size < 0)
	{
		//TODO: this should never happen, indicates server side bug
		//TODO: once our client side implementation good enough, abort stream in this case, noop for now
	}
	else if (size > 0 && !NodeCache.empty()) //TODO: NodeCache cannot be empty if size >0, it's a bug
		ResendNodes(size);
	else
	{
		for (auto i : NodeCache)
			xmlFree(i);
		NodeCache.clear();
	}
}

void strm_mgmt::ResendNodes(uint32_t size)
{
	if (size < NodeCache.size())
	{
		const size_t diff = NodeCache.size() - size;
		if (diff)
		{
			size_t diff_tmp = diff;
			for (auto i : NodeCache)
			{
				if (diff_tmp > 0)
				{
					xmlFree(i);
					diff_tmp--;
				}
			}
			diff_tmp = diff;
			while (diff_tmp)
			{
				NodeCache.pop_front();
				diff_tmp--;
			}
		}
	}
	std::list<HXML> tmp_list = NodeCache;
	NodeCache.clear();
	for (auto i : tmp_list)
	{
		proto->m_ThreadInfo->send_no_strm_mgmt(i); //freed by send ?
												   //xmlFree(i);
	}
}

void strm_mgmt::OnProcessSMr(HXML node)
{
	if (!mir_wstrcmp(XmlGetAttrValue(node, L"xmlns"), L"urn:xmpp:sm:3"))
		SendAck();
}

void strm_mgmt::OnProcessFailed(HXML node, ThreadData * /*info*/) //used failed instead of failure, notes: https://xmpp.org/extensions/xep-0198.html#errors
{
	if (mir_wstrcmp(XmlGetAttrValue(node, L"xmlns"), L"urn:xmpp:sm:3"))
		return;
	m_bStrmMgmtEnabled = false;
	bSessionResumed = false;
	m_sStrmMgmtResumeId.clear();
	EnableStrmMgmt(); //resume failed, try to enable strm_mgmt instead
}

void strm_mgmt::CheckStreamFeatures(HXML node)
{
	if (mir_wstrcmp(XmlGetName(node), L"sm") || !XmlGetAttrValue(node, L"xmlns") || mir_wstrcmp(XmlGetAttrValue(node, L"xmlns"), L"urn:xmpp:sm:3")) //we work only with version 3 or higher of sm
		return;
	if (!(proto->m_bJabberOnline))
		m_bStrmMgmtPendingEnable = true;
	else
		EnableStrmMgmt();
}

void strm_mgmt::CheckState()
{
	if (m_bStrmMgmtEnabled)
		return;
	if (!m_bStrmMgmtPendingEnable)
		return;
	EnableStrmMgmt();
}

void strm_mgmt::HandleOutgoingNode(HXML node)
{
	if (!m_bStrmMgmtEnabled)
		return;
	m_nStrmMgmtLocalSCount++;
	NodeCache.push_back(xmlCopyNode(node));
	if ((m_nStrmMgmtLocalSCount - m_nStrmMgmtSrvHCount) >= m_nStrmMgmtCacheSize)
		RequestAck();
}

void strm_mgmt::OnDisconnect()
{
	//TODO: following should be redone once resumption implemented
	//reset state of stream management
	m_bStrmMgmtEnabled = false;
	m_bStrmMgmtPendingEnable = false;
	//reset stream management h counters
	m_nStrmMgmtLocalHCount = m_nStrmMgmtLocalSCount = m_nStrmMgmtSrvHCount = 0;
}

void strm_mgmt::HandleIncommingNode(HXML node)
{
	if (m_bStrmMgmtEnabled && mir_wstrcmp(XmlGetName(node), L"r") && mir_wstrcmp(XmlGetName(node), L"a")) //TODO: something better
		m_nStrmMgmtLocalHCount++;
	else if (!mir_wstrcmp(XmlGetName(node), L"r"))
		OnProcessSMr(node);
	else if (!mir_wstrcmp(XmlGetName(node), L"a"))
		OnProcessSMa(node);
}

void strm_mgmt::EnableStrmMgmt()
{
	if (m_bStrmMgmtEnabled)
		return;
	if (m_sStrmMgmtResumeId.empty())
	{
		XmlNode enable_sm(L"enable");
		XmlAddAttr(enable_sm, L"xmlns", L"urn:xmpp:sm:3");
		XmlAddAttr(enable_sm, L"resume", L"true"); //enable resumption (most useful part of this xep)
		proto->m_ThreadInfo->send(enable_sm);
		m_nStrmMgmtLocalSCount = 1; //TODO: this MUST be 0, i have bug somewhere.
	}
	else //resume session
	{
		XmlNode enable_sm(L"resume");
		XmlAddAttr(enable_sm, L"xmlns", L"urn:xmpp:sm:3");
		xmlAddAttrInt(enable_sm, L"h", m_nStrmMgmtLocalHCount);
		XmlAddAttr(enable_sm, L"previd", m_sStrmMgmtResumeId.c_str());
		proto->m_ThreadInfo->send(enable_sm);
	}
}

void strm_mgmt::SendAck()
{
	if (!m_bStrmMgmtEnabled)
		return;
	XmlNode enable_sm(L"a");
	XmlAddAttr(enable_sm, L"xmlns", L"urn:xmpp:sm:3");
	xmlAddAttrInt(enable_sm, L"h", m_nStrmMgmtLocalHCount);
	proto->m_ThreadInfo->send_no_strm_mgmt(enable_sm);
}

void strm_mgmt::RequestAck()
{
	if (!m_bStrmMgmtEnabled)
		return;
	XmlNode enable_sm(L"r");
	XmlAddAttr(enable_sm, L"xmlns", L"urn:xmpp:sm:3");
	proto->m_ThreadInfo->send_no_strm_mgmt(enable_sm);
}

bool strm_mgmt::IsSessionResumed()
{
	return bSessionResumed;
}

bool strm_mgmt::IsResumeIdPresent()
{
	return !m_sStrmMgmtResumeId.empty();
}