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
|
/*
* 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 "Utils.h"
#include "SipSession.h"
#include "AudioCapturePlugin.h"
#include "AudioCapturePluginCommon.h"
#include <list>
#include "VoIpConfig.h"
#include "ace/OS_NS_arpa_inet.h"
extern AudioChunkCallBackFunction g_audioChunkCallBack;
extern CaptureEventCallBackFunction g_captureEventCallBack;
extern VoIpConfigTopObjectRef g_VoIpConfigTopObjectRef;
#define DLLCONFIG g_VoIpConfigTopObjectRef.get()->m_config
SipSession::SipSession()
{
m_lastUpdated = time(NULL);
m_log = Logger::getLogger("sipsession");
m_invitorIp.s_addr = 0;
m_inviteeIp.s_addr = 0;
m_direction = CaptureEvent::DirUnkn;
}
void SipSession::Stop()
{
LOG4CXX_DEBUG(m_log, m_ipAndPort + " SIP Session stop");
CaptureEventRef stopEvent(new CaptureEvent);
stopEvent->m_type = CaptureEvent::EtStop;
stopEvent->m_timestamp = time(NULL);
g_captureEventCallBack(stopEvent, m_ipAndPort);
}
void SipSession::Start()
{
LOG4CXX_DEBUG(m_log, m_ipAndPort + " SIP Session start");
m_rtpRingBuffer.SetCapturePort(m_ipAndPort);
CaptureEventRef startEvent(new CaptureEvent);
startEvent->m_type = CaptureEvent::EtStart;
startEvent->m_timestamp = time(NULL);
g_captureEventCallBack(startEvent, m_ipAndPort);
}
void SipSession::ProcessMetadataIncoming()
{
m_remoteParty = m_invite->m_from;
m_localParty = m_invite->m_to;
m_direction = CaptureEvent::DirIn;
}
void SipSession::ProcessMetadataOutgoing()
{
m_remoteParty = m_invite->m_to;
m_localParty = m_invite->m_from;
m_direction = CaptureEvent::DirOut;
}
void SipSession::ProcessMetadata(RtpPacketInfoRef& rtpPacket)
{
bool done = false;
// work out invitee IP address
if(rtpPacket->m_sourceIp.s_addr == m_invitorIp.s_addr)
{
m_inviteeIp = rtpPacket->m_destIp;
}
else if(rtpPacket->m_destIp.s_addr == m_invitorIp.s_addr)
{
m_inviteeIp = rtpPacket->m_sourceIp;
}
else
{
LOG4CXX_DEBUG(m_log, m_ipAndPort + " alien RTP packet");
}
// work out capture port and direction
if(DLLCONFIG.IsMediaGateway(m_invitorIp))
{
if(DLLCONFIG.IsMediaGateway(m_inviteeIp))
{
// dismiss
}
if(DLLCONFIG.IsPartOfLan(m_inviteeIp))
{
ProcessMetadataIncoming();
}
}
else if (DLLCONFIG.IsPartOfLan(m_invitorIp))
{
ProcessMetadataOutgoing();
}
else
{
// SIP invitor IP address is an outside IP address
if(DLLCONFIG.IsMediaGateway(m_inviteeIp))
{
// dismiss
}
else if(DLLCONFIG.IsPartOfLan(m_inviteeIp))
{
ProcessMetadataIncoming();
}
else
{
// SIP invitee IP address is an outside IP address
ProcessMetadataOutgoing();
}
}
ReportMetadata();
}
void SipSession::ReportMetadata()
{
// report Local party
CaptureEventRef event(new CaptureEvent());
event->m_type = CaptureEvent::EtLocalParty;
event->m_value = m_localParty;
g_captureEventCallBack(event, m_ipAndPort);
// Report remote party
event.reset(new CaptureEvent());
event->m_type = CaptureEvent::EtRemoteParty;
event->m_value = m_remoteParty;
g_captureEventCallBack(event, m_ipAndPort);
// report direction
event.reset(new CaptureEvent());
event->m_type = CaptureEvent::EtDirection;
event->m_value = CaptureEvent::DirectionToString(m_direction);
g_captureEventCallBack(event, m_ipAndPort);
}
void SipSession::AddRtpPacket(RtpPacketInfoRef& rtpPacket)
{
// if first RTP packet, start session
if(m_lastRtpPacket.get() == NULL)
{
Start();
ProcessMetadata(rtpPacket);
}
m_lastRtpPacket = rtpPacket;
// send audio buffer
//AudioChunkRef chunkRef(new AudioChunk);
//chunkRef->SetBuffer(rtpPacket->m_payload, rtpPacket->m_payloadSize, AudioChunk::AlawAudio);
//g_audioChunkCallBack(chunkRef, m_ipAndPort);
m_rtpRingBuffer.AddRtpPacket(rtpPacket);
m_lastUpdated = time(NULL);
}
void SipSession::ReportSipInvite(SipInviteInfoRef& invite)
{
m_invite = invite;
m_invitorIp = invite->m_fromIp;
}
//=====================================================================
SipSessions::SipSessions()
{
m_log = Logger::getLogger("sipsessions");
}
void SipSessions::ReportSipInvite(SipInviteInfoRef& invite)
{
CStdString key = CStdString(ACE_OS::inet_ntoa(invite->m_fromIp)) + "," + invite->m_fromRtpPort;
std::map<CStdString, SipSessionRef>::iterator pair;
pair = m_byIpAndPort.find(key);
if (pair != m_byIpAndPort.end())
{
// A session exists ont the same IP+port, stop old session
SipSessionRef session = pair->second;
Stop(session);
}
// create new session and insert into both maps
SipSessionRef session(new SipSession());
session->m_ipAndPort = key;
session->ReportSipInvite(invite);
m_byCallId.insert(std::make_pair(invite->m_callId, session));
m_byIpAndPort.insert(std::make_pair(key, session));
}
void SipSessions::ReportSipBye(SipByeInfo bye)
{
std::map<CStdString, SipSessionRef>::iterator pair;
pair = m_byCallId.find(bye.m_callId);
if (pair != m_byCallId.end())
{
// Session found: stop it
SipSessionRef session = pair->second;
Stop(session);
}
}
void SipSessions::Stop(SipSessionRef& session)
{
session->Stop();
m_byIpAndPort.erase(session->m_ipAndPort);
m_byCallId.erase(session->m_invite->m_callId);
}
void SipSessions::ReportRtpPacket(RtpPacketInfoRef& rtpPacket)
{
// Does a session exist with this source Ip+Port
SipSessionRef session;
CStdString port = IntToString(rtpPacket->m_sourcePort);
CStdString ipAndPort = CStdString(ACE_OS::inet_ntoa(rtpPacket->m_sourceIp)) + "," + port;
std::map<CStdString, SipSessionRef>::iterator pair;
pair = m_byIpAndPort.find(ipAndPort);
if (pair != m_byIpAndPort.end())
{
session = pair->second;
}
else
{
// Does a session exist with this destination Ip+Port
port = IntToString(rtpPacket->m_destPort);
ipAndPort = CStdString(ACE_OS::inet_ntoa(rtpPacket->m_destIp)) + "," + port;
pair = m_byIpAndPort.find(ipAndPort);
if (pair != m_byIpAndPort.end())
{
session = pair->second;
}
}
if (!session.get() == NULL)
{
// Found a session give it the RTP packet info
session->AddRtpPacket(rtpPacket);
}
}
void SipSessions::Hoover(time_t now)
{
CStdString numSessions = IntToString(m_byIpAndPort.size());
LOG4CXX_DEBUG(m_log, "Hoover - check " + numSessions + " sessions time:" + IntToString(now));
// Go round the sessions and find inactive ones
std::map<CStdString, SipSessionRef>::iterator pair;
std::list<SipSessionRef> toDismiss;
for(pair = m_byIpAndPort.begin(); pair != m_byIpAndPort.end(); pair++)
{
SipSessionRef session = pair->second;
if((now - session->m_lastUpdated) > 10)
{
toDismiss.push_back(session);
}
}
// discard inactive sessions
for (std::list<SipSessionRef>::iterator it = toDismiss.begin(); it != toDismiss.end() ; it++)
{
SipSessionRef session = *it;
LOG4CXX_DEBUG(m_log, session->m_ipAndPort + " Expired");
Stop(session);
}
}
|