diff options
author | Henri Herscher <henri@oreka.org> | 2006-04-08 23:46:18 +0000 |
---|---|---|
committer | Henri Herscher <henri@oreka.org> | 2006-04-08 23:46:18 +0000 |
commit | 2bf46fc74157e05f696363b0b91a15bf4c1896b5 (patch) | |
tree | cde73f231dddb8e520fadfa4f2f971a48b6de72d | |
parent | 100bc1845f00147f4cbb7b88574f190a316636b5 (diff) |
* Added allowed and blocked CIDR IP address ranges
* Fixed SIP to: field detection issue
* Fixed SIP multiple invite issue
* replaced sleep by nanosleep
* Improved logging
git-svn-id: https://oreka.svn.sourceforge.net/svnroot/oreka/trunk@212 09dcff7a-b715-0410-9601-b79a96267cd0
-rw-r--r-- | orkaudio/audiocaptureplugins/voip/RtpSession.cpp | 78 | ||||
-rw-r--r-- | orkaudio/audiocaptureplugins/voip/RtpSession.h | 4 | ||||
-rw-r--r-- | orkaudio/audiocaptureplugins/voip/VoIp.cpp | 57 | ||||
-rw-r--r-- | orkaudio/audiocaptureplugins/voip/VoIpConfig.cpp | 170 | ||||
-rw-r--r-- | orkaudio/audiocaptureplugins/voip/VoIpConfig.h | 10 |
5 files changed, 270 insertions, 49 deletions
diff --git a/orkaudio/audiocaptureplugins/voip/RtpSession.cpp b/orkaudio/audiocaptureplugins/voip/RtpSession.cpp index 0c194e3..2e74786 100644 --- a/orkaudio/audiocaptureplugins/voip/RtpSession.cpp +++ b/orkaudio/audiocaptureplugins/voip/RtpSession.cpp @@ -48,9 +48,10 @@ RtpSession::RtpSession(CStdString& trackingId) void RtpSession::Stop() { + LOG4CXX_INFO(m_log, m_trackingId + ": " + m_capturePort + " Session stop"); + if(m_started) { - LOG4CXX_INFO(m_log, m_trackingId + ": " + m_capturePort + " Session stop"); CaptureEventRef stopEvent(new CaptureEvent); stopEvent->m_type = CaptureEvent::EtStop; stopEvent->m_timestamp = time(NULL); @@ -349,8 +350,7 @@ void RtpSession::AddRtpPacket(RtpPacketInfoRef& rtpPacket) details.m_encoding = AlawAudio; AudioChunkRef chunk(new AudioChunk()); chunk->SetBuffer(rtpPacket->m_payload, rtpPacket->m_payloadSize, details); - g_audioChunkCallBack(chunk, m_capturePort); // ##### after - //m_rtpRingBuffer.AddRtpPacket(rtpPacket); // ##### before + g_audioChunkCallBack(chunk, m_capturePort); m_lastUpdated = rtpPacket->m_arrivalTimestamp; } @@ -360,7 +360,7 @@ void RtpSession::AddRtpPacket(RtpPacketInfoRef& rtpPacket) void RtpSession::ReportSipInvite(SipInviteInfoRef& invite) { m_invite = invite; - m_invitorIp = invite->m_fromIp; + m_invitorIp = invite->m_fromRtpIp; } int RtpSession::ProtocolToEnum(CStdString& protocol) @@ -410,34 +410,36 @@ RtpSessions::RtpSessions() void RtpSessions::ReportSipInvite(SipInviteInfoRef& invite) { - char szFromIp[16]; - ACE_OS::inet_ntop(AF_INET, (void*)&invite->m_fromIp, szFromIp, sizeof(szFromIp)); + char szFromRtpIp[16]; + ACE_OS::inet_ntop(AF_INET, (void*)&invite->m_fromRtpIp, szFromRtpIp, sizeof(szFromRtpIp)); - CStdString ipAndPort = CStdString(szFromIp) + "," + invite->m_fromRtpPort; + CStdString ipAndPort = CStdString(szFromRtpIp) + "," + invite->m_fromRtpPort; std::map<CStdString, RtpSessionRef>::iterator pair; pair = m_byIpAndPort.find(ipAndPort); if (pair != m_byIpAndPort.end()) { - // #### old behaviour - // A session exists ont the same IP+port, stop old session - //RtpSessionRef session = pair->second; - //Stop(session); - - // #### new behaviour // The session already exists, do nothing return; } pair = m_byCallId.find(invite->m_callId); if (pair != m_byCallId.end()) { - // #### old behaviour - // A session exists ont the same CallId, stop old session - //RtpSessionRef session = pair->second; - //Stop(session); + // The session already exists + RtpSessionRef session = pair->second; + if(!session->m_ipAndPort.Equals(ipAndPort)) + { + // The session RTP connection address has changed + // Remove session from IP and Port map + m_byIpAndPort.erase(session->m_ipAndPort); + // ... update + session->m_ipAndPort = ipAndPort; + session->ReportSipInvite(invite); + // ... and reinsert + m_byIpAndPort.insert(std::make_pair(session->m_ipAndPort, session)); - // #### new behaviour - // The session already exists, do nothing + LOG4CXX_INFO(m_log, session->m_trackingId + ": updated with new INVITE data"); + } return; } @@ -450,6 +452,11 @@ void RtpSessions::ReportSipInvite(SipInviteInfoRef& invite) session->ReportSipInvite(invite); m_byIpAndPort.insert(std::make_pair(session->m_ipAndPort, session)); m_byCallId.insert(std::make_pair(session->m_callId, session)); + + CStdString numSessions = IntToString(m_byIpAndPort.size()); + LOG4CXX_DEBUG(m_log, CStdString("ByIpAndPort: ") + numSessions); + + LOG4CXX_INFO(m_log, trackingId + ": created by SIP INVITE"); } void RtpSessions::ReportSipBye(SipByeInfo bye) @@ -509,6 +516,10 @@ void RtpSessions::ReportSkinnyCallInfo(SkCallInfoStruct* callInfo, IpHeaderStruc } m_byCallId.insert(std::make_pair(session->m_callId, session)); + + CStdString numSessions = IntToString(m_byIpAndPort.size()); + LOG4CXX_DEBUG(m_log, CStdString("ByIpAndPort: ") + numSessions); + } void RtpSessions::ReportSkinnyStartMediaTransmission(SkStartMediaTransmissionStruct* startMedia, IpHeaderStruct* ipHeader) @@ -602,6 +613,9 @@ void RtpSessions::ReportSkinnyStartMediaTransmission(SkStartMediaTransmissionStr session->m_ipAndPort = ipAndPort; m_byIpAndPort.insert(std::make_pair(session->m_ipAndPort, session)); + + CStdString numSessions = IntToString(m_byIpAndPort.size()); + LOG4CXX_DEBUG(m_log, CStdString("ByIpAndPort: ") + numSessions); } else { @@ -652,6 +666,9 @@ void RtpSessions::Stop(RtpSessionRef& session) if(session->m_ipAndPort.size() > 0) { m_byIpAndPort.erase(session->m_ipAndPort); + + CStdString numSessions = IntToString(m_byIpAndPort.size()); + LOG4CXX_DEBUG(m_log, CStdString("ByIpAndPort: ") + numSessions); } if(session->m_callId.size() > 0) { @@ -681,7 +698,7 @@ void RtpSessions::ReportRtpPacket(RtpPacketInfoRef& rtpPacket) if (pair != m_byIpAndPort.end()) { session1 = pair->second; - if (!session1.get() == NULL) + if (session1.get() != NULL) { // Found a session give it the RTP packet info session1->AddRtpPacket(rtpPacket); @@ -699,7 +716,7 @@ void RtpSessions::ReportRtpPacket(RtpPacketInfoRef& rtpPacket) if (pair != m_byIpAndPort.end()) { session2 = pair->second; - if (!session2.get() == NULL) + if (session2.get() != NULL) { // Found a session give it the RTP packet info session2->AddRtpPacket(rtpPacket); @@ -758,6 +775,11 @@ void RtpSessions::ReportRtpPacket(RtpPacketInfoRef& rtpPacket) session->m_ipAndPort = ipAndPort; session->AddRtpPacket(rtpPacket); m_byIpAndPort.insert(std::make_pair(ipAndPort, session)); + + CStdString numSessions = IntToString(m_byIpAndPort.size()); + LOG4CXX_DEBUG(m_log, CStdString("ByIpAndPort: ") + numSessions); + + LOG4CXX_INFO(m_log, trackingId + ": created by RTP packet"); } } @@ -816,15 +838,21 @@ void RtpSessions::Hoover(time_t now) //========================================================== SipInviteInfo::SipInviteInfo() { - m_fromIp.s_addr = 0; + m_fromRtpIp.s_addr = 0; } void SipInviteInfo::ToString(CStdString& string) { - char fromIp[16]; - ACE_OS::inet_ntop(AF_INET, (void*)&m_fromIp, fromIp, sizeof(fromIp)); + char fromRtpIp[16]; + ACE_OS::inet_ntop(AF_INET, (void*)&m_fromRtpIp, fromRtpIp, sizeof(fromRtpIp)); + + char senderIp[16]; + ACE_OS::inet_ntop(AF_INET, (void*)&m_senderIp, senderIp, sizeof(senderIp)); + + char receiverIp[16]; + ACE_OS::inet_ntop(AF_INET, (void*)&m_receiverIp, receiverIp, sizeof(receiverIp)); - string.Format("from:%s %s,%s to:%s callid:%s", m_from, fromIp, m_fromRtpPort, m_to, m_callId); + string.Format("sender:%s from:%s RTP:%s,%s to:%s rcvr:%s callid:%s", senderIp, m_from, fromRtpIp, m_fromRtpPort, m_to, receiverIp, m_callId); } diff --git a/orkaudio/audiocaptureplugins/voip/RtpSession.h b/orkaudio/audiocaptureplugins/voip/RtpSession.h index 50e979b..6e9c739 100644 --- a/orkaudio/audiocaptureplugins/voip/RtpSession.h +++ b/orkaudio/audiocaptureplugins/voip/RtpSession.h @@ -26,7 +26,9 @@ public: SipInviteInfo(); void ToString(CStdString& string); - struct in_addr m_fromIp; + struct in_addr m_senderIp; + struct in_addr m_receiverIp; + struct in_addr m_fromRtpIp; CStdString m_fromRtpPort; CStdString m_from; CStdString m_to; diff --git a/orkaudio/audiocaptureplugins/voip/VoIp.cpp b/orkaudio/audiocaptureplugins/voip/VoIp.cpp index ed5bc5f..e8702c0 100644 --- a/orkaudio/audiocaptureplugins/voip/VoIp.cpp +++ b/orkaudio/audiocaptureplugins/voip/VoIp.cpp @@ -44,6 +44,7 @@ static LoggerPtr s_rtpPacketLog; static LoggerPtr s_sipPacketLog; static LoggerPtr s_skinnyPacketLog; static LoggerPtr s_sipExtractionLog; +static LoggerPtr s_voipPluginLog; static time_t s_lastHooveringTime; static ACE_Thread_Mutex s_mutex; static bool s_liveCapture; @@ -212,9 +213,12 @@ bool TrySipBye(EthernetHeaderStruct* ethernetHeader, IpHeaderStruct* ipHeader, U if(callIdField) { GrabToken(callIdField, info.m_callId); - RtpSessionsSingleton::instance()->ReportSipBye(info); } LOG4CXX_INFO(s_sipPacketLog, "BYE: callid:" + info.m_callId); + if(callIdField) + { + RtpSessionsSingleton::instance()->ReportSipBye(info); + } } return result; } @@ -261,13 +265,9 @@ bool TrySipInvite(EthernetHeaderStruct* ethernetHeader, IpHeaderStruct* ipHeader } if(toField) { - char* toFieldEnd = NULL; - if(s_sipExtractionLog->isDebugEnabled()) - { - CStdString to; - toFieldEnd = GrabLine(toField, sipEnd, to); - LOG4CXX_DEBUG(s_sipExtractionLog, "to: " + to); - } + CStdString to; + char* toFieldEnd = GrabLine(toField, sipEnd, to); + LOG4CXX_DEBUG(s_sipExtractionLog, "to: " + to); char* sipUser = memFindAfter("sip:", toField, toFieldEnd); if(sipUser) @@ -298,7 +298,7 @@ bool TrySipInvite(EthernetHeaderStruct* ethernetHeader, IpHeaderStruct* ipHeader { if(ACE_OS::inet_aton((PCSTR)connectionAddress, &fromIp)) { - info->m_fromIp = fromIp; + info->m_fromRtpIp = fromIp; if (DLLCONFIG.m_sipDropIndirectInvite) { @@ -311,21 +311,23 @@ bool TrySipInvite(EthernetHeaderStruct* ethernetHeader, IpHeaderStruct* ipHeader } } } - if((unsigned int)info->m_fromIp.s_addr == 0) + if((unsigned int)info->m_fromRtpIp.s_addr == 0) { // In case connection address could not be extracted, use SIP invite sender IP address - info->m_fromIp = ipHeader->ip_src; - } - - if(drop == false && info->m_fromRtpPort.size() && info->m_from.size() && info->m_to.size() && info->m_callId.size()) - { - RtpSessionsSingleton::instance()->ReportSipInvite(info); + info->m_fromRtpIp = ipHeader->ip_src; } + info->m_senderIp = ipHeader->ip_src; + info->m_receiverIp = ipHeader->ip_dest; CStdString logMsg; info->ToString(logMsg); logMsg = "INVITE: " + logMsg; LOG4CXX_INFO(s_sipPacketLog, logMsg); + + if(drop == false && info->m_fromRtpPort.size() && info->m_from.size() && info->m_to.size() && info->m_callId.size()) + { + RtpSessionsSingleton::instance()->ReportSipInvite(info); + } } return result; } @@ -388,9 +390,20 @@ void HandlePacket(u_char *param, const struct pcap_pkthdr *header, const u_char { // This is a pcap file replay, make sure Orkaudio won't be flooded by too many // packets at a time by yielding control to other threads. - ACE_Time_Value yield; - yield.set(0,1); // 1 us - ACE_OS::sleep(yield); + //ACE_Time_Value yield; + //yield.set(0,1); // 1 us + //ACE_OS::sleep(yield); + + // Use nanosleep instead + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 1; + ACE_OS::nanosleep (&ts, NULL); + } + + if(DLLCONFIG.IsPacketWanted(ipHeader) == false) + { + return; } if(ipHeader->ip_p == IPPROTO_UDP) @@ -506,6 +519,8 @@ VoIp::VoIp() void Configure(DOMNode* node) { + s_voipPluginLog = Logger::getLogger("voipplugin"); + if (node) { VoIpConfigTopObjectRef VoIpConfigTopObjectRef(new VoIpConfigTopObject); @@ -516,12 +531,12 @@ void Configure(DOMNode* node) } catch (CStdString& e) { - LOG4CXX_WARN(g_logManager->rootLog, "VoIp.dll: " + e); + LOG4CXX_ERROR(s_voipPluginLog, e); } } else { - LOG4CXX_WARN(g_logManager->rootLog, "VoIp.dll: got empty DOM tree"); + LOG4CXX_ERROR(s_voipPluginLog, "Got empty DOM tree"); } } diff --git a/orkaudio/audiocaptureplugins/voip/VoIpConfig.cpp b/orkaudio/audiocaptureplugins/voip/VoIpConfig.cpp index cdea7fb..5a12214 100644 --- a/orkaudio/audiocaptureplugins/voip/VoIpConfig.cpp +++ b/orkaudio/audiocaptureplugins/voip/VoIpConfig.cpp @@ -34,6 +34,10 @@ void VoIpConfig::Define(Serializer* s) s->CsvValue("Devices", m_devices); s->CsvValue("LanMasks", m_asciiLanMasks); s->CsvValue("MediaGateways", m_asciiMediaGateways); + + s->CsvValue("BlockedIpRanges", m_asciiBlockedIpRanges); + s->CsvValue("AllowedIpRanges", m_asciiAllowedIpRanges); + s->StringValue("PcapFile", m_pcapFile); s->StringValue("PcapDirectory", m_pcapDirectory); s->BoolValue("SipDropIndirectInvite", m_sipDropIndirectInvite); @@ -53,7 +57,7 @@ void VoIpConfig::Validate() } else { - throw (CStdString("VoIpConfig: invalid IP address in LanMasks:" + *it)); + throw (CStdString("VoIpConfig: invalid IP address in LanMasks:" + *it) + " please fix config.xml"); } } @@ -68,7 +72,100 @@ void VoIpConfig::Validate() } else { - throw (CStdString("VoIpConfig: invalid IP address in MediaGateways:" + *it)); + throw (CStdString("VoIpConfig: invalid IP address in MediaGateways:" + *it) + " please fix config.xml"); + } + } + + // Iterate over ascii allowed IP ranges and populate the bit width and prefix lists + m_allowedIpRangePrefixes.clear(); + m_allowedIpRangeBitWidths.clear(); + for(it = m_asciiAllowedIpRanges.begin(); it != m_asciiAllowedIpRanges.end(); it++) + { + CStdString cidrPrefixLengthString; + unsigned int cidrPrefixLength = 32; // by default, x.x.x.x/32 + CStdString cidrIpAddressString; + struct in_addr cidrIpAddress; + + CStdString entry = *it; + int slashPosition = entry.Find('/'); + if(slashPosition > 0) + { + cidrIpAddressString = entry.Left(slashPosition); + cidrPrefixLengthString = entry.Mid(slashPosition+1); + + bool notAnInt = false; + try + { + cidrPrefixLength = StringToInt(cidrPrefixLengthString); + } + catch (...) {notAnInt = true;} + if(cidrPrefixLength < 1 || cidrPrefixLength > 32 || notAnInt) + { + throw (CStdString("VoIpConfig: invalid CIDR prefix length in AllowedIpRanges:" + entry) + " please fix config.xml"); + } + } + else + { + cidrIpAddressString = entry; + } + + if(ACE_OS::inet_aton((PCSTR)cidrIpAddressString, &cidrIpAddress)) + { + unsigned int rangeBitWidth = 32-cidrPrefixLength; + unsigned int prefix = ntohl((unsigned int)cidrIpAddress.s_addr) >> (rangeBitWidth); + m_allowedIpRangePrefixes.push_back(prefix); + m_allowedIpRangeBitWidths.push_back(rangeBitWidth); + } + else + { + throw (CStdString("VoIpConfig: invalid IP range in AllowedIpRanges:" + entry) + " please fix config.xml"); + } + } + + + // Iterate over ascii blocked IP ranges and populate the bit width and prefix lists + m_blockedIpRangePrefixes.clear(); + m_blockedIpRangeBitWidths.clear(); + for(it = m_asciiBlockedIpRanges.begin(); it != m_asciiBlockedIpRanges.end(); it++) + { + CStdString cidrPrefixLengthString; + unsigned int cidrPrefixLength = 32; // by default, x.x.x.x/32 + CStdString cidrIpAddressString; + struct in_addr cidrIpAddress; + + CStdString entry = *it; + int slashPosition = entry.Find('/'); + if(slashPosition > 0) + { + cidrIpAddressString = entry.Left(slashPosition); + cidrPrefixLengthString = entry.Mid(slashPosition+1); + + bool notAnInt = false; + try + { + cidrPrefixLength = StringToInt(cidrPrefixLengthString); + } + catch (...) {notAnInt = true;} + if(cidrPrefixLength < 1 || cidrPrefixLength > 32 || notAnInt) + { + throw (CStdString("VoIpConfig: invalid CIDR prefix length in blockedIpRanges:" + entry) + " please fix config.xml"); + } + } + else + { + cidrIpAddressString = entry; + } + + if(ACE_OS::inet_aton((PCSTR)cidrIpAddressString, &cidrIpAddress)) + { + unsigned int rangeBitWidth = 32-cidrPrefixLength; + unsigned int prefix = ntohl((unsigned int)cidrIpAddress.s_addr) >> (rangeBitWidth); + m_blockedIpRangePrefixes.push_back(prefix); + m_blockedIpRangeBitWidths.push_back(rangeBitWidth); + } + else + { + throw (CStdString("VoIpConfig: invalid IP range in BlockedIpRanges:" + entry) + " please fix config.xml"); } } } @@ -97,6 +194,75 @@ bool VoIpConfig::IsMediaGateway(struct in_addr addr) return false; } +bool VoIpConfig::IsPacketWanted(IpHeaderStruct* ipHeader) +{ + bool wanted = true; // keep packet by default + + // If source or destination IP address does not match any existing allowing mask, drop packet + if(m_allowedIpRangePrefixes.size() > 0) + { + wanted = false; // Presence of allowing ranges -> drop packet by default + + bool sourceWanted = false; + std::list<unsigned int>::iterator bitWidthIt = m_allowedIpRangeBitWidths.begin(); + std::list<unsigned int>::iterator prefixIt = m_allowedIpRangePrefixes.begin(); + while(prefixIt != m_allowedIpRangePrefixes.end()) + { + unsigned int bitWidth = *bitWidthIt; + unsigned int prefix = *prefixIt; + unsigned int packetSourcePrefix = ntohl((unsigned int)ipHeader->ip_src.s_addr) >> bitWidth; + if(packetSourcePrefix == prefix) + { + sourceWanted = true; + break; + } + prefixIt++; + bitWidthIt++; + } + if(sourceWanted) + { + std::list<unsigned int>::iterator bitWidthIt = m_allowedIpRangeBitWidths.begin(); + std::list<unsigned int>::iterator prefixIt = m_allowedIpRangePrefixes.begin(); + while(prefixIt != m_allowedIpRangePrefixes.end()) + { + unsigned int bitWidth = *bitWidthIt; + unsigned int prefix = *prefixIt; + unsigned int packetDestPrefix = ntohl((unsigned int)ipHeader->ip_dest.s_addr) >> bitWidth; + if(packetDestPrefix == prefix) + { + wanted = true; + break; + } + prefixIt++; + bitWidthIt++; + } + } + } + // If source or destination IP address does match any existing blocking range, drop packet + std::list<unsigned int>::iterator bitWidthIt = m_blockedIpRangeBitWidths.begin(); + std::list<unsigned int>::iterator prefixIt = m_blockedIpRangePrefixes.begin(); + + while(prefixIt != m_blockedIpRangePrefixes.end() && wanted == true) + { + unsigned int bitWidth = *bitWidthIt; + unsigned int prefix = *prefixIt; + unsigned int packetSourcePrefix = ntohl((unsigned int)ipHeader->ip_src.s_addr) >> bitWidth; + unsigned int packetDestPrefix = ntohl((unsigned int)ipHeader->ip_dest.s_addr) >> bitWidth; + + if(packetSourcePrefix == prefix) + { + wanted = false; + } + if(packetDestPrefix == prefix) + { + wanted = false; + } + prefixIt++; + bitWidthIt++; + } + return wanted; +} + bool VoIpConfig::IsDeviceWanted(CStdString device) { if(device.Equals(m_device)) diff --git a/orkaudio/audiocaptureplugins/voip/VoIpConfig.h b/orkaudio/audiocaptureplugins/voip/VoIpConfig.h index 2c68eab..5313389 100644 --- a/orkaudio/audiocaptureplugins/voip/VoIpConfig.h +++ b/orkaudio/audiocaptureplugins/voip/VoIpConfig.h @@ -18,6 +18,7 @@ #include "StdString.h" #include "Object.h" #include "boost/shared_ptr.hpp" +#include "PacketHeaderDefs.h" #define DEVICE_PARAM "Device" @@ -36,6 +37,7 @@ public: bool IsPartOfLan(struct in_addr); bool IsMediaGateway(struct in_addr); bool IsDeviceWanted(CStdString device); + bool IsPacketWanted(IpHeaderStruct* ipHeader); CStdString m_device; // old style but can still be used for specifying single device std::list<CStdString> m_devices; // new style devices csv @@ -43,6 +45,14 @@ public: std::list<CStdString> m_asciiMediaGateways; std::list<unsigned int> m_lanMasks; std::list<CStdString> m_asciiLanMasks; + + std::list<CStdString> m_asciiAllowedIpRanges; // CIDR notation + std::list<unsigned int> m_allowedIpRangePrefixes; + std::list<unsigned int> m_allowedIpRangeBitWidths; + std::list<CStdString> m_asciiBlockedIpRanges; // CIDR notation + std::list<unsigned int> m_blockedIpRangePrefixes; + std::list<unsigned int> m_blockedIpRangeBitWidths; + CStdString m_pcapFile; CStdString m_pcapDirectory; bool m_sipDropIndirectInvite; |