<commit_before>/*
	Flexisip, a flexible SIP proxy server with media capabilities.
    Copyright (C) 2010  Belledonne Communications SARL.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "agent.hh"
#include "mediarelay.hh"
#include "callstore.hh"
#include "sdp-modifier.hh"

#include <vector>
#include <algorithm>

using namespace::std;

class RelayedCall;

class MediaRelay : public Module, protected ModuleToolbox{
	public:
		MediaRelay(Agent *ag);
		~MediaRelay();
		virtual void onLoad(Agent *ag, const ConfigStruct * modconf);
		virtual void onRequest(SipEvent *ev);
		virtual void onResponse(SipEvent *ev);
		virtual void onIdle();
	private:
		void processNewInvite(RelayedCall *c, msg_t *msg, sip_t *sip);
		void process200OkforInvite(RelayedCall *ctx, msg_t *msg, sip_t *sip);
		CallStore mCalls;
		MediaRelayServer *mServer;
		static ModuleInfo <MediaRelay> sInfo;
};

class RelayedCall : public CallContextBase, public Masquerader{
	public:
		static const int sMaxSessions=4;
		RelayedCall(MediaRelayServer *server, sip_t *sip) : CallContextBase (sip), mServer(server){
			memset(mSessions,0,sizeof(mSessions));
		}
		/*this function is called to masquerade the SDP, for each mline*/
		virtual void onNewMedia(int mline, std::string *ip, int *port, const char *party_tag){
			if (mline>=sMaxSessions){
				LOGE("Max sessions per relayed call is reached.");
				return;
			}
			RelaySession *s=mSessions[mline];
			if (s==NULL){
				s=mServer->createSession();
				mSessions[mline]=s;
			}
			
			if (getCallerTag()==party_tag){
				s->setFrontDefaultSource(ip->c_str(),*port);
				*port=s->getFrontPort();
			}else{
				s->setBackDefaultSource(ip->c_str(),*port);
				*port=s->getBackPort();
			}
			*ip=s->getPublicIp();
		}
		virtual bool isInactive(time_t cur){
			time_t maxtime=0;
			RelaySession *r;
			for (int i=0;i<sMaxSessions;++i){
				time_t tmp;
				r=mSessions[i];
				if (r && ((tmp=r->getLastActivityTime()) > maxtime) )
					maxtime=tmp;
			}
			if (cur-maxtime>30) return true;
			return false;
		}
		virtual ~RelayedCall(){
			int i;
			for(i=0;i<sMaxSessions;++i){
				RelaySession *s=mSessions[i];
				if (s) s->unuse();<commit_msg>fix inverted back and front ports...(media relay)<commit_after>/*
	Flexisip, a flexible SIP proxy server with media capabilities.
    Copyright (C) 2010  Belledonne Communications SARL.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "agent.hh"
#include "mediarelay.hh"
#include "callstore.hh"
#include "sdp-modifier.hh"

#include <vector>
#include <algorithm>

using namespace::std;

class RelayedCall;

class MediaRelay : public Module, protected ModuleToolbox{
	public:
		MediaRelay(Agent *ag);
		~MediaRelay();
		virtual void onLoad(Agent *ag, const ConfigStruct * modconf);
		virtual void onRequest(SipEvent *ev);
		virtual void onResponse(SipEvent *ev);
		virtual void onIdle();
	private:
		void processNewInvite(RelayedCall *c, msg_t *msg, sip_t *sip);
		void process200OkforInvite(RelayedCall *ctx, msg_t *msg, sip_t *sip);
		CallStore mCalls;
		MediaRelayServer *mServer;
		static ModuleInfo <MediaRelay> sInfo;
};

class RelayedCall : public CallContextBase, public Masquerader{
	public:
		static const int sMaxSessions=4;
		RelayedCall(MediaRelayServer *server, sip_t *sip) : CallContextBase (sip), mServer(server){
			memset(mSessions,0,sizeof(mSessions));
		}
		/*this function is called to masquerade the SDP, for each mline*/
		virtual void onNewMedia(int mline, std::string *ip, int *port, const char *party_tag){
			if (mline>=sMaxSessions){
				LOGE("Max sessions per relayed call is reached.");
				return;
			}
			RelaySession *s=mSessions[mline];
			if (s==NULL){
				s=mServer->createSession();
				mSessions[mline]=s;
			}
			
			if (getCallerTag()==party_tag){
				s->setFrontDefaultSource(ip->c_str(),*port);
				*port=s->getBackPort();
			}else{
				s->setBackDefaultSource(ip->c_str(),*port);
				*port=s->getFrontPort();
			}
			*ip=s->getPublicIp();
		}
		virtual bool isInactive(time_t cur){
			time_t maxtime=0;
			RelaySession *r;
			for (int i=0;i<sMaxSessions;++i){
				time_t tmp;
				r=mSessions[i];
				if (r && ((tmp=r->getLastActivityTime()) > maxtime) )
					maxtime=tmp;
			}
			if (cur-maxtime>30) return true;
			return false;
		}
		virtual ~RelayedCall(){
			int i;
			for(i=0;i<sMaxSessions;++i){
				RelaySession *s=mSessions[i];
				if (s) s->unuse();
{'fraction_non_alphanumeric': 0.1133380631429585, 'fraction_numerical': 0.005321035828307911, 'mean_word_length': 4.0801801801801805, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '889408', 'n_tokens_mistral': 2116, 'n_tokens_neox': 1873, 'n_words': 575}