summaryrefslogtreecommitdiff
path: root/orkaudio/audiocaptureplugins/voip/VoIp.cpp
blob: a9fdb0ebd3a3026a3e2e594396e364dbcfee422b (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

#define _WINSOCKAPI_		// prevents the inclusion of winsock.h

#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_string.h"
#include "ace/Singleton.h"
#include "ace/Min_Max.h"
#include "ace/OS_NS_arpa_inet.h"
#include "ace/OS_NS_ctype.h"
#include "AudioCapturePlugin.h"
#include "AudioCapturePluginCommon.h"
#include "Utils.h"
#include "VoIpConfig.h"
#include "pcap.h"
#include "PacketHeaderDefs.h"
#include "Rtp.h"
#include "SipSession.h"

extern AudioChunkCallBackFunction g_audioChunkCallBack;
extern CaptureEventCallBackFunction g_captureEventCallBack;
extern LogManager* g_logManager;

#include "LogManager.h"

static LoggerPtr s_log;
static LoggerPtr s_sipExtractionLog;
time_t lastHooveringTime;

VoIpConfigTopObjectRef g_VoIpConfigTopObjectRef;
#define DLLCONFIG g_VoIpConfigTopObjectRef.get()->m_config

// find the address that follows the given search string between start and stop pointers
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(memcmp(toFind, ptr, strlen(toFind)) == 0)
		{
			return (ptr+strlen(toFind));
		}
	}
	return NULL;
}

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;
}

void GrabToken(char* in, CStdString& out)
{
	for(char* c = in; *c != '\0' && *c != 0x20 && *c != 0x0D && *c != 0x0A; c = c+1)
	{
		out += *c;
	}
}

void GrabAlphaNumToken(char * in, char* limit, CStdString& out)
{
	// Look for first alphanum character
	char* start = in;
	while (!ACE_OS::ace_isalnum(*start) && start<limit)
	{
		start++;
	}

	if(start != (limit -1))
	{
		for(char* c = start; ACE_OS::ace_isalnum(*c); c = c+1)
		{
			out += *c;
		}
	}
}

void GrabString(char* start, char* stop, CStdString& out)
{
	char* c = start;
	while(c <= stop)
	{
		out += *c++;
	}
}

// Grabs a line of characters in memory from start pointer
// returns the end of line
char* GrabLine(char* start, char* limit, CStdString& out)
{
	char* c = start;
	while(c < limit && *c != 0x0D && *c != 0x0A)
	{
		out += *c++;
	}
	return c;
}

bool TryRtp(EthernetHeaderStruct* ethernetHeader, IpHeaderStruct* ipHeader, UdpHeaderStruct* udpHeader, u_char* udpPayload)
{
	bool result = false;
	RtpHeaderStruct* rtpHeader = (RtpHeaderStruct*)udpPayload;

	if (rtpHeader->version == 2)
	{
		u_short source = ntohs(udpHeader->source);
		u_short dest = ntohs(udpHeader->dest);
		if(!(ntohs(udpHeader->source)%2) && !(ntohs(udpHeader->dest)%2))	// udp ports must be even 
		{
			if(rtpHeader->pt == RTP_PT_PCMU || rtpHeader->pt == RTP_PT_PCMA)
			{
				result = true;
				u_char* payload = (u_char *)rtpHeader + sizeof(RtpHeaderStruct);
				u_char* packetEnd = (u_char *)ipHeader + ntohs(ipHeader->ip_len);
				u_int payloadLength = packetEnd - payload;

				RtpPacketInfoRef rtpInfo(new RtpPacketInfo());
				rtpInfo->m_sourceIp = ipHeader->ip_src;
				rtpInfo->m_destIp =  ipHeader->ip_dest;
				rtpInfo->m_sourcePort = ntohs(udpHeader->source);
				rtpInfo->m_destPort = ntohs(udpHeader->dest);
				rtpInfo->m_payloadSize = payloadLength;
				rtpInfo->m_payloadType = rtpHeader->pt;
				rtpInfo->m_seqNum = ntohs(rtpHeader->seq);
				rtpInfo->m_timestamp = ntohl(rtpHeader->ts);
				rtpInfo->m_payload = payload;

				CStdString debug;
				debug.Format("%s,%d seq:%u ts:%u len:%d", ACE_OS::inet_ntoa(rtpInfo->m_sourceIp), rtpInfo->m_sourcePort, ntohs(rtpHeader->seq), ntohl(rtpHeader->ts), payloadLength);
				LOG4CXX_DEBUG(s_log, debug);

				RtpSessionsSingleton::instance()->ReportRtpPacket(rtpInfo);
			}
		}
	}
	return result;
}


bool TrySipBye(EthernetHeaderStruct* ethernetHeader, IpHeaderStruct* ipHeader, UdpHeaderStruct* udpHeader, u_char* udpPayload)
{
	bool result = false;
	if (memcmp("BYE", (void*)udpPayload, 1) == 0)
	{
		result = true;
		int sipLength = ntohs(udpHeader->len);
		char* sipEnd = (char*)udpPayload + sipLength;
		SipByeInfo info;
		char* callIdField = memFindAfter("Call-ID: ", (char*)udpPayload, sipEnd);
		if(callIdField)
		{
			GrabToken(callIdField, info.m_callId);
			RtpSessionsSingleton::instance()->ReportSipBye(info);
		}
		LOG4CXX_DEBUG(s_log, "SIP BYE");
	}
	return result;
}

bool TrySipInvite(EthernetHeaderStruct* ethernetHeader, IpHeaderStruct* ipHeader, UdpHeaderStruct* udpHeader, u_char* udpPayload)
{
	bool result = false;
	if (memcmp("INVITE", (void*)udpPayload, 1) == 0)
	{
		result = true;

		int sipLength = ntohs(udpHeader->len);
		char* sipEnd = (char*)udpPayload + sipLength;

		SipInviteInfoRef info(new SipInviteInfo());

		char* fromField = memFindAfter("From: ", (char*)udpPayload, sipEnd);
		char* toField = NULL;
		char* callIdField = NULL;
		char* audioField = NULL;

		if(fromField)
		{
			if(s_sipExtractionLog->isDebugEnabled())
			{
				CStdString from;
				GrabLine(fromField, sipEnd, from);
				s_sipExtractionLog->forcedLog(Level::DEBUG, "from: " + from);
			}

			char* fromFieldEnd = memFindEOL(fromField, sipEnd);

			char* sipUser = memFindAfter("sip:", fromField, fromFieldEnd);
			if(sipUser)
			{
				GrabAlphaNumToken(sipUser, fromFieldEnd, info->m_from);
			}
			else
			{
				GrabAlphaNumToken(fromField, fromFieldEnd, info->m_from);
			}
			toField = memFindAfter("To: ", fromField, sipEnd);
		}
		if(toField)
		{
			char* toFieldEnd = NULL;
			if(s_sipExtractionLog->isDebugEnabled())
			{
				CStdString to;
				toFieldEnd = GrabLine(toField, sipEnd, to);
				s_sipExtractionLog->forcedLog(Level::DEBUG, "to: " + to);
			}

			char* sipUser = memFindAfter("sip:", toField, toFieldEnd);
			if(sipUser)
			{
				GrabAlphaNumToken(sipUser, toFieldEnd, info->m_to);
			}
			else
			{
				GrabAlphaNumToken(toField, toFieldEnd, info->m_to);
			}
			callIdField = memFindAfter("Call-ID: ", toField, sipEnd);
		}
		if(callIdField)
		{
			GrabToken(callIdField, info->m_callId);
			audioField = memFindAfter("m=audio ", callIdField, sipEnd);
		}
		if(audioField)
		{
			GrabToken(audioField, info->m_fromRtpPort);
			info->m_fromIp = ipHeader->ip_src;
			RtpSessionsSingleton::instance()->ReportSipInvite(info);
		}
		LOG4CXX_DEBUG(s_log, "SIP INVITE");
	}
	return result;
}

void HandlePacket(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
	EthernetHeaderStruct* ethernetHeader = (EthernetHeaderStruct *)pkt_data;
	IpHeaderStruct* ipHeader = (IpHeaderStruct*)((char*)ethernetHeader + sizeof(EthernetHeaderStruct));
	int ipHeaderLength = ipHeader->ip_hl*4;

	//CStdString source = ACE_OS::inet_ntoa(ipHeader->ip_src);
	//CStdString dest = ACE_OS::inet_ntoa(ipHeader->ip_dest);
	//CStdString debug;
	//debug.Format("%x, %x", ethernetHeader, ipHeader);
	//LOG4CXX_INFO(s_log, source + "-" + dest);

	//LOG4CXX_DEBUG(s_log, "*");

	if(ipHeader->ip_p == IPPROTO_UDP)
	{
		UdpHeaderStruct* udpHeader = (UdpHeaderStruct*)((char *)ipHeader + ipHeaderLength);

		if(	ntohs(udpHeader->source) > 5000 && ntohs(udpHeader->dest) > 5000 )
		{
			u_char* udpPayload = (u_char *)udpHeader + sizeof(UdpHeaderStruct);


			bool detectedUsefulPacket = TryRtp(ethernetHeader, ipHeader, udpHeader, udpPayload);
			
			if(!detectedUsefulPacket)
			{
				detectedUsefulPacket= TrySipInvite(ethernetHeader, ipHeader, udpHeader, udpPayload);
			}
			if(!detectedUsefulPacket)
			{
				detectedUsefulPacket = TrySipBye(ethernetHeader, ipHeader, udpHeader, udpPayload);
			}
		}
	}

	time_t now = time(NULL);
	if((now - lastHooveringTime) > 5)
	{
		lastHooveringTime = now;
		RtpSessionsSingleton::instance()->Hoover(now);
	}
}


class VoIp
{
public:
	VoIp();
	void Initialize();
	void Run();
	void StartCapture(CStdString& port);
	void StopCapture(CStdString& port);
private:
	pcap_t* m_pcapHandle;
};

typedef ACE_Singleton<VoIp, ACE_Thread_Mutex> VoIpSingleton;

VoIp::VoIp()
{
	m_pcapHandle = NULL;
}

void Configure(DOMNode* node)
{
	if (node)
	{
		VoIpConfigTopObjectRef VoIpConfigTopObjectRef(new VoIpConfigTopObject);
		try
		{
			VoIpConfigTopObjectRef.get()->DeSerializeDom(node);
			g_VoIpConfigTopObjectRef = VoIpConfigTopObjectRef;
		}
		catch (CStdString& e)
		{
			LOG4CXX_WARN(g_logManager->rootLog, "VoIp.dll: " + e);
		}
	}
	else
	{
		LOG4CXX_WARN(g_logManager->rootLog, "VoIp.dll: got empty DOM tree");
	}
}


void VoIp::Initialize()
{
	s_log = Logger::getLogger("voip");
	s_sipExtractionLog = Logger::getLogger("sipextraction");
	LOG4CXX_INFO(s_log, "Initializing VoIP plugin");

	// create a default config object in case it was not properly initialized by Configure
	if(!g_VoIpConfigTopObjectRef.get())
	{
		g_VoIpConfigTopObjectRef.reset(new VoIpConfigTopObject);
	}

/*
	char addr1[] = "10.1.2.3";
	char addr2[] = "192.168.5.6";
	char addr3[] = "45.168.5.6";
	struct in_addr addr;
	ACE_OS::inet_aton(addr1, &addr);
	bool result = DLLCONFIG.IsPartOfLan(addr);
	result = DLLCONFIG.IsMediaGateway(addr);
	ACE_OS::inet_aton(addr2, &addr);
	result = DLLCONFIG.IsPartOfLan(addr);
	result = DLLCONFIG.IsMediaGateway(addr);
	ACE_OS::inet_aton(addr3, &addr);
	result = DLLCONFIG.IsPartOfLan(addr);
	result = DLLCONFIG.IsMediaGateway(addr);
*/

	pcap_if_t* devices = NULL;
	pcap_if_t* lastDevice = NULL;
	pcap_if_t* deviceToSniff = NULL;
	lastHooveringTime = time(NULL);

	char * error = NULL;
	if (pcap_findalldevs(&devices, error) == -1)
	{
		LOG4CXX_ERROR(s_log, CStdString("pcap error when discovering devices: ") + error);
	}
	else
	{
		if(devices)
		{
			LOG4CXX_INFO(s_log, CStdString("Available pcap devices:"));

			for (pcap_if_t* device = devices; device != NULL; device = device->next)
			{
				LOG4CXX_INFO(s_log, CStdString("\t* ") + device->description + " " + device->name );
				if(DLLCONFIG.m_device.Equals(device->name))
				{
					deviceToSniff = device;
				}
				if(device)
				{
					lastDevice = device;
				}
			}
			if (!deviceToSniff)
			{
				if(!DLLCONFIG.m_device.IsEmpty())
				{
					LOG4CXX_ERROR(s_log, CStdString("pcap could not find wanted device: ") + DLLCONFIG.m_device + " please check your config file");
				}
				if(lastDevice)
				{
					LOG4CXX_INFO(s_log, CStdString("Defaulting to device: ") + lastDevice->name);
					deviceToSniff = lastDevice;
				}
			}
			if (deviceToSniff)
			{
				#define PROMISCUOUS 1
				if ((m_pcapHandle = pcap_open_live(deviceToSniff->name, 1500, PROMISCUOUS,
					 500, error)) == NULL)
				{
					LOG4CXX_ERROR(s_log, CStdString("pcap error when opening device: ") + deviceToSniff->name);
				}
				else
				{
					LOG4CXX_INFO(s_log, CStdString("successfully opened device: ") + deviceToSniff->name);
				}
			}
		}
		else
		{
			LOG4CXX_ERROR(s_log, CStdString("pcap could not find any device"));
		}
	}

}

void VoIp::Run()
{
	if(m_pcapHandle)
	{
		LOG4CXX_INFO(s_log, "Running VoIp.dll");
		pcap_loop(m_pcapHandle, 0, HandlePacket, NULL);
	}
	else
	{
		LOG4CXX_INFO(s_log, "No network device opened - VoIp.dll not starting");
	}
}

void VoIp::StartCapture(CStdString& port)
{
	;
}

void VoIp::StopCapture(CStdString& port)
{
	;
}

void __CDECL__ Initialize()
{
	VoIpSingleton::instance()->Initialize();
}

void __CDECL__ Run()
{
	VoIpSingleton::instance()->Run();
}

void __CDECL__ StartCapture(CStdString& capturePort)
{
	;
}

void __CDECL__ StopCapture(CStdString& capturePort)
{
	;
}