summaryrefslogtreecommitdiff
path: root/orkaudio/audiocaptureplugins/voip/SipTcp.cpp
blob: 8b0283880454847a5547648fcd3e42c63805511a (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <list>
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_strings.h"
#include "ace/OS_NS_dirent.h"
#include "ace/Singleton.h"
#include "ace/Min_Max.h"
#include "ace/OS_NS_arpa_inet.h"
#include "ace/OS_NS_ctype.h"
#include "ace/Thread_Manager.h"
#include "ace/Thread_Mutex.h"
#include "ace/Thread_Semaphore.h"
#include "AudioCapturePlugin.h"
#include "AudioCapturePluginCommon.h"
#include "Utils.h"
#include "VoIpConfig.h"
#include "pcap.h"
#include "PacketHeaderDefs.h"
#include "Rtp.h"
#include "RtpSession.h"
#include "Iax2Session.h"
#include "SipTcp.h"
#include "boost/shared_ptr.hpp"
#include "StdString.h"
#include "SipTcp.h"


SafeBuffer::SafeBuffer()
{
	m_pBuffer = NULL;
	m_size = 0;
}

SafeBuffer::~SafeBuffer()
{
	if(m_size) {
		free(m_pBuffer);
		m_size = 0;
	}

	m_pBuffer = NULL;
}

int SafeBuffer::Size()
{
	return m_size;
}

void SafeBuffer::Store(u_char *buf, int len)
{
	if(m_size) {
		free(m_pBuffer);
		m_size = 0;
		m_pBuffer = NULL;
	}

	m_pBuffer = (u_char *)calloc(len+1, 1);
	m_size = len;

	if(!m_pBuffer) {
		char tmp[80];
		snprintf(tmp, sizeof(tmp), "%d", len);

		CStdString numBytes = CStdString(tmp);
                throw("SafeBuffer::Store could not malloc a buffer of size:" + numBytes);
	}

	memcpy(m_pBuffer, buf, len);
}

void SafeBuffer::Add(u_char *buf, int len)
{
        u_char *newBuf = NULL;

        if(!m_size) {
                Store(buf, len);
                return;
        }

        newBuf = (u_char*)realloc(m_pBuffer, m_size+len+1);
        if(!newBuf) {
                char tmp[80];
                snprintf(tmp, sizeof(tmp), "%d", len+m_size);

                CStdString numBytes = CStdString(tmp);
                throw("SafeBuffer::Add failed to realloc buffer to " + numBytes);
        }

        m_pBuffer = newBuf;
        memcpy(m_pBuffer+m_size, buf, len);
        m_size += len;
        *(m_pBuffer+m_size) = 0;
}

u_char *SafeBuffer::GetBuffer()
{
	return m_pBuffer;
}

// ============================================================

static char* memFindAfter(char* toFind, char* start, char* stop)
{
        for(char * ptr = start; (ptr<stop) && (ptr != NULL); ptr = (char *)memchr(ptr+1, toFind[0],(stop - start)))
        {
                if(ACE_OS::strncasecmp(toFind, ptr, strlen(toFind)) == 0)
                {
                        return (ptr+strlen(toFind));
                }
        }
        return NULL;
}

// Implementation of strcasestr() - works like strstr() but
// is case insensitive
char* memFindStr(char* toFind, char* start, char* stop)
{
        for(char * ptr = start; (ptr<stop) && (ptr != NULL); ptr = (char *)memchr(ptr+1, toFind[0],(stop - start)))
        {
                if(ACE_OS::strncasecmp(toFind, ptr, strlen(toFind)) == 0)
                {
                        return (ptr);
                }
        }
        return NULL;
}

static char* memFindEOL(char* start, char* limit)
{
        char* c = start;
        while(*c != '\r' && *c != '\n' && c<limit)
        {
                c++;
        }
        if(*c == '\r' || *c == '\n')
        {
                return c;
        }
        return start;
}

static void memToHex(unsigned char* input, size_t len, CStdString&output)
{
        char byteAsHex[10];
        for(int i=0; i<len; i++)
        {
                sprintf(byteAsHex, "%.2x", input[i]);
                output += byteAsHex;
        }
}

SipTcpStream::SipTcpStream()
{
	m_expectingSeqNo = 0;
	m_senderIp.s_addr = 0;
	m_receiverIp.s_addr = 0;
	m_senderPort = 0;
	m_receiverPort = 0;
	m_entryTime = time(NULL);
	m_sipRequest = SafeBufferRef(new SafeBuffer());
}

SipTcpStream::~SipTcpStream()
{
}

void SipTcpStream::ToString(CStdString& string)
{
        char senderIp[16], receiverIp[16];
	CStdString expSeq, lastSeq;

	memToHex((unsigned char *)&m_expectingSeqNo, sizeof(m_expectingSeqNo), expSeq);
	memToHex((unsigned char *)&m_lastSeqNo, sizeof(m_lastSeqNo), lastSeq);

        ACE_OS::inet_ntop(AF_INET, (void*)&m_senderIp, senderIp, sizeof(senderIp));
        ACE_OS::inet_ntop(AF_INET, (void*)&m_receiverIp, receiverIp, sizeof(receiverIp));

	//string.Format("sender:%s receiver:%s sender-port:%d receiver-port:%d entry-time:%d expecting-seq-no:%s total-bytes:%d last-seqno:%s [[[%s]]]", senderIp, receiverIp, m_senderPort, m_receiverPort, m_entryTime, expSeq, m_sipRequest->Size(), lastSeq, m_sipRequest->GetBuffer());
	string.Format("sender:%s receiver:%s sender-port:%d receiver-port:%d entry-time:%d expecting-seq-no:%s total-bytes:%d last-seqno:%s", senderIp, receiverIp, m_senderPort, m_receiverPort, m_entryTime, expSeq, m_sipRequest->Size(), lastSeq);
}

void SipTcpStream::AddTcpPacket(u_char *pBuffer, int packetLen)
{
	m_sipRequest->Add(pBuffer, packetLen);
}

/*
 * How we know the SIP request is complete:  Small excerpt from
 * RFC3261
 * 
 * ---8<---
 * ...
 * 20.14 Content-Length
 *
 * The Content-Length header field indicates the size of the message-
 * body, in decimal number of octets, sent to the recipient.
 * Applications SHOULD use this field to indicate the size of the
 * message-body to be transferred, regardless of the media type of the
 * entity.  If a stream-based protocol (such as TCP) is used as
 * transport, the header field MUST be used.
 *
 * The size of the message-body does not include the CRLF separating
 * header fields and body.  Any Content-Length greater than or equal to
 * zero is a valid value.  If no body is present in a message, then the
 * Content-Length header field value MUST be set to zero.
 * ...
 * --->8---
 *
 */
bool SipTcpStream::SipRequestIsComplete()
{
	if(!m_sipRequest->Size())
		return false;
		
	char *pBufStart = (char*)m_sipRequest->GetBuffer();
	char *pBufEnd = pBufStart+m_sipRequest->Size();
	char *contentLengthHeader = memFindStr("Content-Length: ", pBufStart, pBufEnd);
	char *contentLength = memFindAfter("Content-Length: ", pBufStart, pBufEnd);
	int cLength = 0;

	if(!contentLength || !contentLengthHeader)
		return false;

        char *eol = memFindEOL(contentLengthHeader, pBufEnd);

	if(eol == contentLengthHeader)
		return false;

	cLength = ACE_OS::atoi(contentLength);

	/* Step over headers */
	bool lnl = false, headerEndLocated = false;
	while(eol < pBufEnd) {
		if(*eol == '\r' && ((eol+1) < pBufEnd) && (*(eol+1) == '\n')) {
			if(lnl == true) {
				eol += 2;
				headerEndLocated = true;
				break;
			}

			eol += 2;
			lnl = true;
			continue;
		}

		if(*eol == '\n') {
			if(lnl == true) {
                                eol += 1;
				headerEndLocated = true;
                                break;
                        }

                        eol += 1;
                        lnl = true;
                        continue;
		}

		eol += 1;
		lnl = false;
		continue;
	}

	if(!headerEndLocated)
		return false;

	if(eol >= pBufEnd)
	{
		if(cLength == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	if(strlen(eol) == cLength)
		return true;

	return false;
}

SafeBufferRef SipTcpStream::GetCompleteSipRequest()
{
	SafeBufferRef buf(new SafeBuffer());

	buf->Store(m_sipRequest->GetBuffer(), m_sipRequest->Size());

	return buf;
}