1 /*
2  * Copyright (C) 2011 Stefan Sayer
3  *
4  * This file is part of SEMS, a free SIP media server.
5  *
6  * SEMS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * For a license to use the SEMS software under conditions
12  * other than those described here, or to purchase support for this
13  * software, please contact iptel.org by e-mail at the following addresses:
14  *    info@iptel.org
15  *
16  * SEMS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25 
26 #include "AmPlugIn.h"
27 #include "log.h"
28 #include "AmArg.h"
29 #include "SBC.h"
30 
31 #include "CCCtl.h"
32 
33 #include "SBCCallControlAPI.h"
34 
35 #include <string.h>
36 #include <algorithm>
37 
38 class CCCtlFactory : public AmDynInvokeFactory
39 {
40 public:
CCCtlFactory(const string & name)41     CCCtlFactory(const string& name)
42 	: AmDynInvokeFactory(name) {}
43 
getInstance()44     AmDynInvoke* getInstance(){
45 	return CCCtl::instance();
46     }
47 
onLoad()48     int onLoad(){
49       if (CCCtl::instance()->onLoad())
50 	return -1;
51 
52       DBG("ctl call control loaded.\n");
53 
54       return 0;
55     }
56 };
57 
58 EXPORT_PLUGIN_CLASS_FACTORY(CCCtlFactory, "ctl");
59 
60 CCCtl* CCCtl::_instance=0;
61 
instance()62 CCCtl* CCCtl::instance()
63 {
64     if(!_instance)
65 	_instance = new CCCtl();
66     return _instance;
67 }
68 
CCCtl()69 CCCtl::CCCtl()
70 {
71 }
72 
~CCCtl()73 CCCtl::~CCCtl() { }
74 
onLoad()75 int CCCtl::onLoad() {
76   AmConfigReader cfg;
77 
78   // if(cfg.loadFile(AmConfig::ModConfigPath + string(MOD_NAME ".conf"))) {
79   //   INFO(MOD_NAME "configuration  file (%s) not found, "
80   // 	 "assuming default configuration is fine\n",
81   // 	 (AmConfig::ModConfigPath + string(MOD_NAME ".conf")).c_str());
82   //   return 0;
83   // }
84 
85   // syslog_prefix = cfg.hasParameter("cdr_prefix") ?
86   //   cfg.getParameter("cdr_prefix") : syslog_prefix;
87 
88   return 0;
89 }
90 
invoke(const string & method,const AmArg & args,AmArg & ret)91 void CCCtl::invoke(const string& method, const AmArg& args, AmArg& ret)
92 {
93   DBG("CCCtl: %s(%s)\n", method.c_str(), AmArg::print(args).c_str());
94 
95   if(method == "start"){
96     SBCCallProfile* call_profile =
97       dynamic_cast<SBCCallProfile*>(args[CC_API_PARAMS_CALL_PROFILE].asObject());
98 
99     start(args[CC_API_PARAMS_CC_NAMESPACE].asCStr(),
100 	  args[CC_API_PARAMS_LTAG].asCStr(),
101 	  call_profile,
102 	  args[CC_API_PARAMS_TIMESTAMPS][CC_API_TS_START_SEC].asInt(),
103 	  args[CC_API_PARAMS_TIMESTAMPS][CC_API_TS_START_USEC].asInt(),
104 	  args[CC_API_PARAMS_CFGVALUES],
105 	  args[CC_API_PARAMS_TIMERID].asInt(),  ret);
106 
107   } else if(method == "connect"){
108     // dummy
109   } else if(method == "end"){
110     // dummy
111   } else if(method == "_list"){
112     ret.push("start");
113     ret.push("connect");
114     ret.push("end");
115   }
116   else
117     throw AmDynInvoke::NotImplemented(method);
118 }
119 
start(const string & cc_name,const string & ltag,SBCCallProfile * call_profile,int start_ts_sec,int start_ts_usec,const AmArg & values,int timer_id,AmArg & res)120 void CCCtl::start(const string& cc_name, const string& ltag,
121 		       SBCCallProfile* call_profile,
122 		       int start_ts_sec, int start_ts_usec,
123 		       const AmArg& values, int timer_id, AmArg& res) {
124   if (!call_profile) {
125     ERROR("internal - call profile not found\n");
126     return;
127   }
128 
129 #define SET_TO_CALL_PROFILE(cfgparam, member)		\
130   if (values.hasMember(cfgparam)) {			\
131     DBG("setting '%s' to '%s'\n", cfgparam, values[cfgparam].asCStr());	\
132     call_profile->member = values[cfgparam].asCStr();	\
133   }
134 
135 #define ADD_TO_CALL_PROFILE(cfgparam, member)		\
136   if (values.hasMember(cfgparam)) {			\
137     DBG("adding '%s' to '%s'\n", values[cfgparam].asCStr(), cfgparam);	\
138     call_profile->member += values[cfgparam].asCStr();	\
139   }
140 
141 #define ENABLE_IN_CALL_PROFILE(cfgparam, member)	\
142   if (values.hasMember(cfgparam)) {			\
143     call_profile->member =				\
144       string(values[cfgparam].asCStr()) == "yes";	\
145     DBG("%sabling '%s'\n", call_profile->member?"en":"dis", \
146 	values[cfgparam].asCStr());		       \
147   }
148 
149   SET_TO_CALL_PROFILE("RURI", ruri);
150   SET_TO_CALL_PROFILE("From", from);
151   SET_TO_CALL_PROFILE("To", to);
152   //TODO: SET_TO_CALL_PROFILE("Contact", contact);
153   SET_TO_CALL_PROFILE("Call-ID", callid);
154   SET_TO_CALL_PROFILE("outbound_proxy", outbound_proxy);
155   ENABLE_IN_CALL_PROFILE("force_outbound_proxy", force_outbound_proxy);
156 
157   SET_TO_CALL_PROFILE("next_hop", next_hop);
158 
159   SET_TO_CALL_PROFILE("sst_enabled", sst_enabled);
160   SET_TO_CALL_PROFILE("sst_aleg_enabled", sst_aleg_enabled);
161 
162   ADD_TO_CALL_PROFILE("append_headers", append_headers);
163 
164   if (call_profile->append_headers.size() > 2)
165       assertEndCRLF(call_profile->append_headers);
166 
167   SET_TO_CALL_PROFILE("rtprelay_enabled", rtprelay_enabled);
168   SET_TO_CALL_PROFILE("rtprelay_interface", rtprelay_interface);
169   SET_TO_CALL_PROFILE("aleg_rtprelay_interface", aleg_rtprelay_interface);
170 
171   SET_TO_CALL_PROFILE("outbound_interface", outbound_interface);
172 
173   if (values.hasMember("headerfilter")) {
174     string hf = values["headerfilter"].asCStr();
175     FilterType t = String2FilterType(hf.c_str());
176     if (Undefined != t) {
177 
178       call_profile->headerfilter.push_back(FilterEntry());
179       call_profile->headerfilter.back().filter_type = t;
180 
181       string hl;
182       if (values.hasMember("header_list"))
183 	hl = values["header_list"].asCStr();
184 
185       vector<string> elems = explode(hl, "|");
186       for (vector<string>::iterator it=elems.begin(); it != elems.end(); it++) {
187 	transform(it->begin(), it->end(), it->begin(), ::tolower);
188 	call_profile->headerfilter.back().filter_list.insert(*it);
189       }
190 
191       DBG("call control '%s': set header filter '%s', list '%s'\n",
192 	  cc_name.c_str(), FilterType2String(t), hl.c_str());
193     }
194   }
195 
196   if (values.hasMember("messagefilter")) {
197     string hf = values["messagefilter"].asCStr();
198     FilterType t = String2FilterType(hf.c_str());
199     if (Undefined != t) {
200 
201       call_profile->messagefilter.push_back(FilterEntry());
202       call_profile->messagefilter.back().filter_type = t;
203 
204       string hl;
205       if (values.hasMember("message_list"))
206 	hl = values["message_list"].asCStr();
207 
208       vector<string> elems = explode(hl, "|");
209       for (vector<string>::iterator it=elems.begin(); it != elems.end(); it++) {
210 	call_profile->messagefilter.back().filter_list.insert(*it);
211       }
212 
213       DBG("call control '%s': set message filter '%s', list '%s'\n",
214 	  cc_name.c_str(), FilterType2String(t), hl.c_str());
215     }
216   }
217 
218 }
219