diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..06c92d07df --- /dev/null +++ b/.gitattributes @@ -0,0 +1,15 @@ +# gitattributes +*.exe -diff binary executable windows dfsg-nonfree debian-ignore +*.wav -diff binary sound +*.gif -diff binary image +*.jpg -diff binary image +*.jpeg -diff binary image +*.pbm -diff binary image +/clients/flex* dfsg-nonfree +/debian* debian-ignore +/freeswitch.xcodeproj debian-ignore +/libs/ilbc* dfsg-nonfree +/libs/libg722_1* dfsg-nonfree +/libs/win32* windows debian-ignore +/htdocs* dfsg-nonfree debian-ignore +/w32* windows debian-ignore diff --git a/.gitignore b/.gitignore index f31e630473..f153ad6f7b 100644 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,7 @@ configure.lineno config.log config.status core.* +TAGS *.2010.log *.Build.CppClean.log *.tlog @@ -201,3 +202,23 @@ src/mod/formats/mod_shout/*/*/mod_shout.log /src/mod/languages/mod_managed/x64/Release_CLR/FREESWITCH.MANAGED.DLL.metagen /src/mod/languages/mod_managed/x64/Release_CLR/RSAENH.DLL.bi /src/mod/languages/mod_managed/x64/Release_CLR/TZRES.DLL.bi +libs/apr-util/a.out.dSYM/Contents/Info.plist +libs/apr-util/a.out.dSYM/Contents/Resources/DWARF/a.out +libs/apr/a.out.dSYM/Contents/Info.plist +libs/apr/a.out.dSYM/Contents/Resources/DWARF/a.out +libs/iksemel/a.out.dSYM/Contents/Info.plist +libs/iksemel/a.out.dSYM/Contents/Resources/DWARF/a.out +libs/ilbc/a.out.dSYM/Contents/Info.plist +libs/ilbc/a.out.dSYM/Contents/Resources/DWARF/a.out +libs/libedit/a.out.dSYM/Contents/Info.plist +libs/libedit/a.out.dSYM/Contents/Resources/DWARF/a.out +libs/pcre/a.out.dSYM/Contents/Info.plist +libs/pcre/a.out.dSYM/Contents/Resources/DWARF/a.out +libs/sqlite/lemon.dSYM/Contents/Info.plist +libs/sqlite/lemon.dSYM/Contents/Resources/DWARF/lemon +libs/sqlite/mkkeywordhash.dSYM/Contents/Info.plist +libs/sqlite/mkkeywordhash.dSYM/Contents/Resources/DWARF/mkkeywordhash +libs/sqlite/sqlite3.dSYM/Contents/Info.plist +libs/sqlite/sqlite3.dSYM/Contents/Resources/DWARF/sqlite3 +libs/srtp/a.out.dSYM/Contents/Info.plist +libs/srtp/a.out.dSYM/Contents/Resources/DWARF/a.out diff --git a/Freeswitch.2010.express.sln b/Freeswitch.2010.express.sln index b0df7d9c35..baea4a0c35 100644 --- a/Freeswitch.2010.express.sln +++ b/Freeswitch.2010.express.sln @@ -320,6 +320,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ldns", "libs\win32\ldns\ldn EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libpcre Generate pcre_chartables.c", "libs\win32\pcre\pcre_chartables.c.2010.vcxproj", "{1CED5987-A529-46DC-B30F-870D85FF9C94}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libzrtp", "libs\libzrtp\projects\win\libzrtp.2010.vcxproj", "{C13CC324-0032-4492-9A30-310A6BD64FF5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution All|Win32 = All|Win32 @@ -1989,6 +1991,17 @@ Global {1CED5987-A529-46DC-B30F-870D85FF9C94}.Release|Win32.ActiveCfg = Release|Win32 {1CED5987-A529-46DC-B30F-870D85FF9C94}.Release|Win32.Build.0 = Release|Win32 {1CED5987-A529-46DC-B30F-870D85FF9C94}.Release|x64.ActiveCfg = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.All|Win32.ActiveCfg = Release|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.All|x64.ActiveCfg = Release|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.All|x64.Build.0 = Release|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Debug|Win32.ActiveCfg = Debug|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Debug|Win32.Build.0 = Debug|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Debug|x64.ActiveCfg = Debug|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Debug|x64.Build.0 = Debug|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Release|Win32.ActiveCfg = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Release|Win32.Build.0 = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Release|x64.ActiveCfg = Release|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2089,6 +2102,7 @@ Global {ABB71A76-42B0-47A4-973A-42E3D920C6FD} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {9778F1C0-09BC-4698-8EBC-BD982247209A} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {56B91D01-9150-4BBF-AFA1-5B68AB991B76} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} + {C13CC324-0032-4492-9A30-310A6BD64FF5} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {EC3E5C7F-EE09-47E2-80FE-546363D14A98} = {B8F5B47B-8568-46EB-B320-64C17D2A98BC} {1AD3F51E-BBB6-4090-BA39-9DFAB1EF1F5F} = {0C808854-54D1-4230-BFF5-77B5FD905000} {ACFFF684-4D19-4D48-AF12-88EA1D778BDF} = {0C808854-54D1-4230-BFF5-77B5FD905000} diff --git a/Freeswitch.2010.sln b/Freeswitch.2010.sln index e7cccee08c..954ce1cc1b 100644 --- a/Freeswitch.2010.sln +++ b/Freeswitch.2010.sln @@ -525,6 +525,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gsmlib", "src\mod\endpoints EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mod_gsmopen", "src\mod\endpoints\mod_gsmopen\mod_gsmopen.2010.vcxproj", "{74B120FF-6935-4DFE-A142-CDB6BEA99C90}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libzrtp", "libs\libzrtp\projects\win\libzrtp.2010.vcxproj", "{C13CC324-0032-4492-9A30-310A6BD64FF5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution All|Win32 = All|Win32 @@ -3574,6 +3576,23 @@ Global {74B120FF-6935-4DFE-A142-CDB6BEA99C90}.Release|x64.ActiveCfg = Release|x64 {74B120FF-6935-4DFE-A142-CDB6BEA99C90}.Release|x64 Setup.ActiveCfg = Release|x64 {74B120FF-6935-4DFE-A142-CDB6BEA99C90}.Release|x86 Setup.ActiveCfg = Release|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.All|Win32.ActiveCfg = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.All|Win32.Build.0 = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.All|x64.ActiveCfg = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.All|x64 Setup.ActiveCfg = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.All|x86 Setup.ActiveCfg = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Debug|Win32.ActiveCfg = Debug|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Debug|Win32.Build.0 = Debug|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Debug|x64.ActiveCfg = Debug|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Debug|x64.Build.0 = Debug|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Debug|x64 Setup.ActiveCfg = Debug|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Debug|x86 Setup.ActiveCfg = Debug|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Release|Win32.ActiveCfg = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Release|Win32.Build.0 = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Release|x64.ActiveCfg = Release|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Release|x64.Build.0 = Release|x64 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Release|x64 Setup.ActiveCfg = Release|Win32 + {C13CC324-0032-4492-9A30-310A6BD64FF5}.Release|x86 Setup.ActiveCfg = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -3687,6 +3706,7 @@ Global {70A49BC2-7500-41D0-B75D-EDCC5BE987A0} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {23B4D303-79FC-49E0-89E2-2280E7E28940} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {26C82FCE-E0CF-4D10-A00C-D8E582FFEB53} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} + {C13CC324-0032-4492-9A30-310A6BD64FF5} = {EB910B0D-F27D-4B62-B67B-DE834C99AC5B} {EC3E5C7F-EE09-47E2-80FE-546363D14A98} = {B8F5B47B-8568-46EB-B320-64C17D2A98BC} {1AD3F51E-BBB6-4090-BA39-9DFAB1EF1F5F} = {0C808854-54D1-4230-BFF5-77B5FD905000} {ACFFF684-4D19-4D48-AF12-88EA1D778BDF} = {0C808854-54D1-4230-BFF5-77B5FD905000} diff --git a/Makefile.am b/Makefile.am index c6e112c4b9..12dc3fa05a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -20,11 +20,18 @@ DEFAULT_SOUNDS=en-us-callie-8000 sounds_version=`grep $$base_sound_dir $(switch_srcdir)/build/sounds_version.txt | cut -d ' ' -f2`;\ soundfile=`echo freeswitch-sounds-$$full_sound_dir-$$moh_version.tar.gz`; \ echo $$full_sound_dir | grep music >/dev/null || soundfile=`echo freeswitch-sounds-$$full_sound_dir-$$sounds_version.tar.gz`; \ - if test "$$target" = "install"; then $(MAKE) $(AM_MAKEFLAGS) core_install; else $(MAKE) $(AM_MAKEFLAGS) core ; fi; \ if test "$$target_prefix" = "sounds"; then \ - if test "$$target" = "install"; then $(GETSOUNDS) $$soundfile $(DESTDIR)$(soundsdir)/; else $(GETSOUNDS) $$soundfile ; fi; \ + if test "$$target" = "install"; then \ + $(GETSOUNDS) $$soundfile $(DESTDIR)$(soundsdir)/;\ + else \ + $(GETSOUNDS) $$soundfile ; \ + fi; \ else \ - cd src/mod && $(MAKE) $(AM_MAKEFLAGS) $@ ;\ + if test "$$target" = "install"; then \ + $(MAKE) $(AM_MAKEFLAGS) core_install && cd src/mod && $(MAKE) $(AM_MAKEFLAGS) $@ ; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) core && cd src/mod && $(MAKE) $(AM_MAKEFLAGS) $@ ;\ + fi; \ fi sounds: sounds-en-us-callie-8000 @@ -150,8 +157,8 @@ if ENABLE_ZRTP CORE_CFLAGS += -I$(switch_srcdir)/libs/libzrtp/third_party/bgaes CORE_CFLAGS += -I$(switch_srcdir)/libs/libzrtp/third_party/bnlib CORE_CFLAGS += -isystem $(switch_srcdir)/libs/libzrtp/include -ZRTP_LDFLAGS = -Llibs/libzrtp/third_party/bnlib -ZRTP_LDFLAGS += -Llibs/libzrtp/projects/gnu/build +ZRTP_LDFLAGS = -L$(switch_srcdir)/libs/libzrtp/third_party/bnlib +ZRTP_LDFLAGS += -L$(switch_srcdir)/libs/libzrtp/projects/gnu/build ZRTP_LIBS = -lbn -lzrtp libfreeswitch_la_LDFLAGS += $(ZRTP_LDFLAGS) libfreeswitch_la_LIBADD += $(ZRTP_LIBS) @@ -411,8 +418,8 @@ $(switch_builddir)/quiet_libtool: $(switch_builddir)/libtool src/include/switch_version.h: src/include/switch_version.h.in .version $(libfreeswitch_la_SOURCES) $(library_include_HEADERS) @have_version=1 ; \ force=0 ; \ - grep "@SWITCH_VERSION_REVISION@" src/include/switch_version.h.in > /dev/null && have_version=0 ; \ - test ! -f src/include/switch_version.h || grep "@SWITCH_VERSION_REVISION@" src/include/switch_version.h > /dev/null && force=1 ; \ + grep -- "@SWITCH_VERSION_REVISION@" src/include/switch_version.h.in > /dev/null || have_version=0 ; \ + test ! -f src/include/switch_version.h || grep -- "@SWITCH_VERSION_REVISION@" src/include/switch_version.h > /dev/null && force=1 ; \ if test $$have_version = 1 ; then \ cat src/include/switch_version.h.in > src/include/switch_version.h ; \ touch .version ; \ diff --git a/bootstrap.sh b/bootstrap.sh index 0e0ac56fc4..5c9ab26f03 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -343,11 +343,7 @@ bootstrap_apr() { echo "Entering directory ${LIBDIR}/apr-util" cd ${LIBDIR}/apr-util - if ! ${BGJOB}; then - ./buildconf - else - ./buildconf & - fi + ./buildconf } bootstrap_libzrtp() { @@ -454,9 +450,10 @@ bootstrap_libs() { if ! ${BGJOB}; then libbootstrap ${i} ; bootstrap_libs_post ${i} else - ((libbootstrap ${i} ; bootstrap_libs_post ${i}) &) + (libbootstrap ${i} ; bootstrap_libs_post ${i}) & fi done + ${BGJOB} && wait } run() { @@ -471,7 +468,6 @@ run() { check_libtoolize print_autotools_vers bootstrap_libs - ${BGJOB} && wait return 0 } diff --git a/build/freeswitch.init.suse b/build/freeswitch.init.suse index 2859ddc501..29f832d86a 100644 --- a/build/freeswitch.init.suse +++ b/build/freeswitch.init.suse @@ -46,12 +46,7 @@ # Voice Platform ### END INIT INFO # -# Check for missing binaries (stale symlinks should not happen) -# Note: Special treatment of stop for LSB conformance FREESWITCH_BIN=/opt/freeswitch/bin/freeswitch -test -x $FREESWITCH_BIN || { echo "$FREESWITCH_BIN not installed"; - if [ "$1" = "stop" ]; then exit 0; - else exit 5; fi; } # Check for existence of needed config file and read it FREESWITCH_CONFIG=/etc/sysconfig/freeswitch @@ -62,6 +57,12 @@ test -r $FREESWITCH_CONFIG || { echo "$FREESWITCH_CONFIG not existing"; # Read config . $FREESWITCH_CONFIG +# Check for missing binaries (stale symlinks should not happen) +# Note: Special treatment of stop for LSB conformance +test -x $FREESWITCH_BIN || { echo "$FREESWITCH_BIN not installed"; + if [ "$1" = "stop" ]; then exit 0; + else exit 5; fi; } + # Source LSB init functions # providing start_daemon, killproc, pidofproc, # log_success_msg, log_failure_msg and log_warning_msg. @@ -202,7 +203,7 @@ case "$1" in ## argument to this init script which is required for a reload. ## Note: probe is not (yet) part of LSB (as of 1.9) - test /opt/freeswitch/conf/freeswitch.xml -nt /opt/freeswitch/run/freeswitch.pid && echo reload + test ${FREESWITCH_HOME}/conf/freeswitch.xml -nt ${FREESWITCH_HOME}/run/freeswitch.pid && echo reload ;; *) echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}" diff --git a/build/getlib.sh.in b/build/getlib.sh.in index bbfe696f79..f18ec1da2d 100755 --- a/build/getlib.sh.in +++ b/build/getlib.sh.in @@ -1,4 +1,5 @@ #!/bin/sh +##### -*- mode:shell-script; indent-tabs-mode:nil; sh-basic-offset:2 -*- TAR=@TAR@ ZCAT=@ZCAT@ @@ -8,39 +9,39 @@ WGET=@WGET@ CURL=@CURL@ if [ -f "$WGET" ]; then - DOWNLOAD_CMD=$WGET + DOWNLOAD_CMD=$WGET elif [ -f "$CURL" ]; then - DOWNLOAD_CMD="$CURL -O" + DOWNLOAD_CMD="$CURL -O" fi if [ -n "`echo $1 | grep '://'`" ]; then - base=$1/ - tarfile=$2 + base=$1/ + tarfile=$2 else - base=http://files.freeswitch.org/downloads/libs/ - tarfile=$1 + base=http://files.freeswitch.org/downloads/libs/ + tarfile=$1 fi uncompressed=`echo $tarfile | sed 's/\(\(\.tar\.gz\|\.tar\.bz2\|\.tar\.xz\)\|\(\.tgz\|\.tbz2\)\)$//'` case `echo $tarfile | sed 's/^.*\.//'` in - bz2|tbz2) UNZIPPER=$BZIP ;; - xz) UNZIPPER=$XZ ;; - gz|tgz|*) UNZIPPER=$ZCAT ;; + bz2|tbz2) UNZIPPER=$BZIP ;; + xz) UNZIPPER=$XZ ;; + gz|tgz|*) UNZIPPER=$ZCAT ;; esac if [ ! -d $tarfile ]; then + if [ ! -f $tarfile ]; then + rm -fr $uncompressed + $DOWNLOAD_CMD $base$tarfile if [ ! -f $tarfile ]; then - rm -fr $uncompressed - $DOWNLOAD_CMD $base$tarfile - if [ ! -f $tarfile ]; then - echo cannot find $tarfile - exit 1 - fi - fi - if [ ! -d $uncompressed ]; then - $UNZIPPER -c -d $tarfile | $TAR -xf - + echo cannot find $tarfile + exit 1 fi + fi + if [ ! -d $uncompressed ]; then + $UNZIPPER -c -d $tarfile | $TAR -xf - + fi fi exit 0 diff --git a/build/modules.conf.in b/build/modules.conf.in index 18babbfec9..c1de94b75f 100644 --- a/build/modules.conf.in +++ b/build/modules.conf.in @@ -1,132 +1,18 @@ -#applications/mod_abstraction -#applications/mod_avmd -#applications/mod_blacklist -#applications/mod_callcenter -#applications/mod_cidlookup -applications/mod_cluechoo -applications/mod_commands -applications/mod_conference -#applications/mod_curl -applications/mod_db -#applications/mod_directory -#applications/mod_distributor -applications/mod_dptools -#applications/mod_easyroute -applications/mod_enum -applications/mod_esf -#applications/mod_esl -applications/mod_expr -applications/mod_fifo -#applications/mod_fsk -applications/mod_fsv -applications/mod_hash -applications/mod_httapi -#applications/mod_http_cache -#applications/mod_ladspa -#applications/mod_lcr -#applications/mod_memcache -#applications/mod_mongo -#applications/mod_nibblebill -#applications/mod_osp -#applications/mod_redis -#applications/mod_rss -applications/mod_sms -#applications/mod_snapshot -#applications/mod_snipe_hunt -#applications/mod_snom -#applications/mod_soundtouch -applications/mod_spandsp -#applications/mod_spy -#applications/mod_stress -applications/mod_valet_parking -#applications/mod_vmd -applications/mod_voicemail -#applications/mod_voicemail_ivr -#asr_tts/mod_cepstral -#asr_tts/mod_flite -#asr_tts/mod_pocketsphinx -#asr_tts/mod_tts_commandline -#asr_tts/mod_unimrcp -codecs/mod_amr -#codecs/mod_amrwb -codecs/mod_bv -#codecs/mod_celt -#codecs/mod_codec2 -#codecs/mod_com_g729 -#codecs/mod_dahdi_codec -codecs/mod_g723_1 -codecs/mod_g729 -codecs/mod_h26x -codecs/mod_ilbc -#codecs/mod_isac -#codecs/mod_opus -#codecs/mod_sangoma_codec -#codecs/mod_silk -codecs/mod_siren -codecs/mod_speex -dialplans/mod_dialplan_asterisk -#dialplans/mod_dialplan_directory -dialplans/mod_dialplan_xml -#directories/mod_ldap -#endpoints/mod_alsa -#endpoints/mod_dingaling -#endpoints/mod_h323 -#endpoints/mod_khomp -endpoints/mod_loopback -#endpoints/mod_opal -#endpoints/mod_portaudio -#endpoints/mod_rtmp -#endpoints/mod_skinny -#endpoints/mod_skypopen -endpoints/mod_sofia -event_handlers/mod_cdr_csv -#event_handlers/mod_cdr_mongodb -#event_handlers/mod_cdr_pg_csv -event_handlers/mod_cdr_sqlite -#event_handlers/mod_erlang_event -#event_handlers/mod_event_multicast -event_handlers/mod_event_socket -#event_handlers/mod_event_zmq -#event_handlers/mod_radius_cdr -#event_handlers/mod_snmp -formats/mod_local_stream -formats/mod_native_file -#formats/mod_portaudio_stream -#formats/mod_shell_stream -#formats/mod_shout -formats/mod_sndfile -formats/mod_tone_stream -#languages/mod_java -languages/mod_lua -#languages/mod_managed -#languages/mod_perl -#languages/mod_python -languages/mod_spidermonkey -#languages/mod_yaml loggers/mod_console loggers/mod_logfile loggers/mod_syslog -#say/mod_say_de -say/mod_say_en -#say/mod_say_es -#say/mod_say_fr -#say/mod_say_he -#say/mod_say_hu -#say/mod_say_it -#say/mod_say_nl -#say/mod_say_pt -#say/mod_say_ru -#say/mod_say_th -#say/mod_say_zh -#timers/mod_posix_timer -#timers/mod_timerfd +applications/mod_dptools +applications/mod_commands +applications/mod_hash +applications/mod_spandsp +applications/mod_distributor +dialplans/mod_dialplan_xml +endpoints/mod_sofia +endpoints/mod_opal +endpoints/mod_media_gateway +../../libs/freetdm/mod_freetdm xml_int/mod_xml_cdr -#xml_int/mod_xml_curl -#xml_int/mod_xml_ldap -xml_int/mod_xml_rpc - -#../../libs/freetdm/mod_freetdm -#../../libs/openzap/mod_openzap - -## Experimental Modules (don't cry if they're broken) -#../../contrib/mod/xml_int/mod_xml_odbc +event_handlers/mod_event_socket +codecs/mod_sangoma_codec +event_handlers/mod_radius_cdr +applications/mod_rad_auth diff --git a/build/next-release.txt b/build/next-release.txt new file mode 100644 index 0000000000..996f71d3b9 --- /dev/null +++ b/build/next-release.txt @@ -0,0 +1 @@ +1.2-rc3 diff --git a/build/set-fs-version.sh b/build/set-fs-version.sh new file mode 100755 index 0000000000..6743b58b5f --- /dev/null +++ b/build/set-fs-version.sh @@ -0,0 +1,16 @@ +#!/bin/sh +##### -*- mode:shell-script; indent-tabs-mode:nil; sh-basic-offset:2 -*- + +sdir="." +[ -n "${0%/*}" ] && sdir="${0%/*}" +. $sdir/../scripts/ci/common.sh + +check_pwd +check_input_ver_build $@ +in_ver="$1" +if [ "$in_ver" = "auto" ]; then + in_ver="$(cat build/next-release.txt)" +fi +eval $(parse_version "$in_ver") +set_fs_ver "$gver" "$gmajor" "$gminor" "$gmicro" "$grev" + diff --git a/conf/insideout/autoload_configs/modules.conf.xml b/conf/insideout/autoload_configs/modules.conf.xml index 8793a003ba..3c864aec1c 100644 --- a/conf/insideout/autoload_configs/modules.conf.xml +++ b/conf/insideout/autoload_configs/modules.conf.xml @@ -60,7 +60,7 @@ - + diff --git a/conf/insideout/sip_profiles/external.xml b/conf/insideout/sip_profiles/external.xml index 6f6045255d..f021a4c3c6 100644 --- a/conf/insideout/sip_profiles/external.xml +++ b/conf/insideout/sip_profiles/external.xml @@ -1,4 +1,4 @@ - + @@ -29,10 +29,10 @@ - @@ -47,8 +47,10 @@ + + diff --git a/conf/insideout/sip_profiles/external/example.xml b/conf/insideout/sip_profiles/external/example.xml index 7ac8db1186..c6b79e8fe3 100644 --- a/conf/insideout/sip_profiles/external/example.xml +++ b/conf/insideout/sip_profiles/external/example.xml @@ -9,7 +9,7 @@ - + diff --git a/conf/insideout/sip_profiles/internal-ipv6.xml b/conf/insideout/sip_profiles/internal-ipv6.xml index 856ac4905d..b9907a78ad 100644 --- a/conf/insideout/sip_profiles/internal-ipv6.xml +++ b/conf/insideout/sip_profiles/internal-ipv6.xml @@ -53,36 +53,39 @@ - + - + - - - - + + + + + + + - + - + - + @@ -128,4 +131,3 @@ - diff --git a/conf/insideout/sip_profiles/internal.xml b/conf/insideout/sip_profiles/internal.xml index a6010b2945..e406cd5e72 100644 --- a/conf/insideout/sip_profiles/internal.xml +++ b/conf/insideout/sip_profiles/internal.xml @@ -1,7 +1,7 @@ @@ -16,24 +16,24 @@ - + - + - + @@ -63,7 +63,7 @@ - + @@ -88,36 +88,39 @@ - + - + - - - - + + + + + + + - + - + - + @@ -138,7 +141,7 @@ - + @@ -158,24 +161,24 @@ - + - + @@ -186,4 +189,3 @@ - diff --git a/conf/insideout/sip_profiles/internal/example.xml b/conf/insideout/sip_profiles/internal/example.xml index 7ac8db1186..c6b79e8fe3 100644 --- a/conf/insideout/sip_profiles/internal/example.xml +++ b/conf/insideout/sip_profiles/internal/example.xml @@ -9,7 +9,7 @@ - + diff --git a/conf/sbc/autoload_configs/modules.conf.xml b/conf/sbc/autoload_configs/modules.conf.xml index 31b96dd27a..fb59b386be 100644 --- a/conf/sbc/autoload_configs/modules.conf.xml +++ b/conf/sbc/autoload_configs/modules.conf.xml @@ -39,7 +39,7 @@ - + diff --git a/conf/sbc/sbc_profiles/external.xml b/conf/sbc/sbc_profiles/external.xml index d16813960f..0f15159f58 100644 --- a/conf/sbc/sbc_profiles/external.xml +++ b/conf/sbc/sbc_profiles/external.xml @@ -1,4 +1,4 @@ - + @@ -7,7 +7,7 @@ - + @@ -30,10 +30,10 @@ - @@ -48,8 +48,10 @@ + + diff --git a/conf/sbc/sbc_profiles/external/example.xml b/conf/sbc/sbc_profiles/external/example.xml index 7ac8db1186..c6b79e8fe3 100644 --- a/conf/sbc/sbc_profiles/external/example.xml +++ b/conf/sbc/sbc_profiles/external/example.xml @@ -9,7 +9,7 @@ - + diff --git a/conf/sbc/sbc_profiles/internal-ipv6.xml b/conf/sbc/sbc_profiles/internal-ipv6.xml index 856ac4905d..b9907a78ad 100644 --- a/conf/sbc/sbc_profiles/internal-ipv6.xml +++ b/conf/sbc/sbc_profiles/internal-ipv6.xml @@ -53,36 +53,39 @@ - + - + - - - - + + + + + + + - + - + - + @@ -128,4 +131,3 @@ - diff --git a/conf/sbc/sbc_profiles/internal.xml b/conf/sbc/sbc_profiles/internal.xml index d06e3a76cd..3081052c88 100644 --- a/conf/sbc/sbc_profiles/internal.xml +++ b/conf/sbc/sbc_profiles/internal.xml @@ -1,7 +1,7 @@ @@ -15,24 +15,24 @@ - + - + - + @@ -69,7 +69,7 @@ - + @@ -94,36 +94,39 @@ - + - + - - - - + + + + + + + - + - + - + @@ -154,24 +157,24 @@ - + - + @@ -182,4 +185,3 @@ - diff --git a/conf/sbc/sbc_profiles/internal/example.xml b/conf/sbc/sbc_profiles/internal/example.xml index 7ac8db1186..c6b79e8fe3 100644 --- a/conf/sbc/sbc_profiles/internal/example.xml +++ b/conf/sbc/sbc_profiles/internal/example.xml @@ -9,7 +9,7 @@ - + diff --git a/conf/softphone/freeswitch.xml b/conf/softphone/freeswitch.xml index 00a2bc31cb..66e7c2a923 100644 --- a/conf/softphone/freeswitch.xml +++ b/conf/softphone/freeswitch.xml @@ -3,7 +3,7 @@ - + @@ -86,10 +86,8 @@ - - diff --git a/conf/vanilla/autoload_configs/modules.conf.xml b/conf/vanilla/autoload_configs/modules.conf.xml index 50353ac849..f88bb7b7c0 100644 --- a/conf/vanilla/autoload_configs/modules.conf.xml +++ b/conf/vanilla/autoload_configs/modules.conf.xml @@ -16,6 +16,7 @@ + @@ -62,6 +63,7 @@ + @@ -80,10 +82,10 @@ - + - + diff --git a/conf/vanilla/autoload_configs/xml_scgi.conf.xml b/conf/vanilla/autoload_configs/xml_scgi.conf.xml new file mode 100644 index 0000000000..b9662d1638 --- /dev/null +++ b/conf/vanilla/autoload_configs/xml_scgi.conf.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/conf/vanilla/lang/en/vm/sounds.xml b/conf/vanilla/lang/en/vm/sounds.xml index ff8844ae97..fdef472a5b 100644 --- a/conf/vanilla/lang/en/vm/sounds.xml +++ b/conf/vanilla/lang/en/vm/sounds.xml @@ -29,7 +29,7 @@ - + @@ -37,7 +37,7 @@ - + diff --git a/conf/vanilla/sip_profiles/external.xml b/conf/vanilla/sip_profiles/external.xml index a709cbb94c..4f18e84be7 100644 --- a/conf/vanilla/sip_profiles/external.xml +++ b/conf/vanilla/sip_profiles/external.xml @@ -1,14 +1,14 @@ - + - @@ -18,10 +18,10 @@ - - + + - + @@ -34,15 +34,15 @@ - + - @@ -56,8 +56,10 @@ + + @@ -90,6 +92,5 @@ - diff --git a/conf/vanilla/sip_profiles/external/example.xml b/conf/vanilla/sip_profiles/external/example.xml index 70668a9ce5..2a4df31aaf 100644 --- a/conf/vanilla/sip_profiles/external/example.xml +++ b/conf/vanilla/sip_profiles/external/example.xml @@ -9,7 +9,7 @@ - + diff --git a/conf/vanilla/sip_profiles/internal-ipv6.xml b/conf/vanilla/sip_profiles/internal-ipv6.xml index ee27a241a0..18274faa4f 100644 --- a/conf/vanilla/sip_profiles/internal-ipv6.xml +++ b/conf/vanilla/sip_profiles/internal-ipv6.xml @@ -54,36 +54,39 @@ - + - + - - - - + + + + + + + - + - + - + @@ -103,8 +106,8 @@ @@ -121,10 +124,9 @@ - - - + + + - diff --git a/conf/vanilla/sip_profiles/internal.xml b/conf/vanilla/sip_profiles/internal.xml index b9605a192c..0f4cabe32d 100644 --- a/conf/vanilla/sip_profiles/internal.xml +++ b/conf/vanilla/sip_profiles/internal.xml @@ -2,30 +2,30 @@ - + - + - + - + @@ -33,31 +33,31 @@ - - + + - + - @@ -106,26 +106,26 @@ - - - + + + @@ -134,10 +134,10 @@ - + - + @@ -164,7 +164,7 @@ - + @@ -203,39 +203,42 @@ - + - + - + - - - - + + + + + + + - + - + - + @@ -252,16 +255,16 @@ - + + Used as the public IP address for SDP. + Can be an one of: + ip address - "12.34.56.78" + a stun server lookup - "stun:stun.server.com" + a DNS name - "host:host.server.com" + auto - Use guessed ip. + auto-nat - Use ip learned from NAT-PMP or UPNP + --> @@ -274,8 +277,8 @@ @@ -297,28 +300,27 @@ - - + @@ -332,59 +334,58 @@ - - - - + + + - - - - - - - - - - - - - - + - - + + + + + - - - + + + + + + + + + + + + + - diff --git a/conf/vanilla/sip_profiles/internal/example.xml b/conf/vanilla/sip_profiles/internal/example.xml index fc061f96d1..10263b05a5 100644 --- a/conf/vanilla/sip_profiles/internal/example.xml +++ b/conf/vanilla/sip_profiles/internal/example.xml @@ -9,7 +9,7 @@ - + diff --git a/conf/vanilla/vars.xml b/conf/vanilla/vars.xml index 4f13a61fbb..907c6f1015 100644 --- a/conf/vanilla/vars.xml +++ b/conf/vanilla/vars.xml @@ -123,7 +123,7 @@ 127 - BV32 --> - + switch_rtp_udptl_mode\n"); switch_rtp_udptl_mode(tech_pvt->rtp_session); diff --git a/src/mod/endpoints/mod_khomp/include/applications.h b/src/mod/endpoints/mod_khomp/include/applications.h index 21b66b2b0a..14ba1d3323 100644 --- a/src/mod/endpoints/mod_khomp/include/applications.h +++ b/src/mod/endpoints/mod_khomp/include/applications.h @@ -599,7 +599,7 @@ struct Transfer return SWITCH_STATUS_FALSE; } - switch_core_session_t *peer_session = switch_core_session_locate(switch_channel_get_variable(chan,SWITCH_SIGNAL_BOND_VARIABLE)); + switch_core_session_t *peer_session = switch_core_session_locate(switch_channel_get_partner_uuid(chan)); if(!peer_session) { @@ -662,7 +662,7 @@ struct Transfer switch_channel_t * peer = switch_core_session_get_channel(peer_session); /* put the channel in hold */ - //switch_core_session_t *session = switch_core_session_locate(switch_channel_get_variable(chan,SWITCH_SIGNAL_BOND_VARIABLE)); + //switch_core_session_t *session = switch_core_session_locate(switch_channel_get_partner_uuid(chan)); //switch_channel_t *chan_core = switch_core_session_get_channel(session); const char *stream; @@ -709,7 +709,7 @@ struct Transfer switch_core_session_rwunlock(pvt->session()); switch_core_session_rwunlock(peer_session); - //switch_ivr_unhold_uuid(switch_channel_get_variable(chan,SWITCH_SIGNAL_BOND_VARIABLE)); + //switch_ivr_unhold_uuid(switch_channel_get_partner_uuid(chan)); } catch (ScopedLockFailed & err) { diff --git a/src/mod/endpoints/mod_loopback/mod_loopback.c b/src/mod/endpoints/mod_loopback/mod_loopback.c index 9ad780f838..e2a2c3daab 100644 --- a/src/mod/endpoints/mod_loopback/mod_loopback.c +++ b/src/mod/endpoints/mod_loopback/mod_loopback.c @@ -650,6 +650,39 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch return status; } +static switch_status_t find_non_loopback_bridge(switch_core_session_t *session, switch_core_session_t **br_session, const char **br_uuid) +{ + switch_channel_t *channel = switch_core_session_get_channel(session); + const char *a_uuid; + switch_core_session_t *sp; + + *br_session = NULL; + *br_uuid = NULL; + + a_uuid = switch_channel_get_partner_uuid(channel); + + while (a_uuid && (sp = switch_core_session_locate(a_uuid))) { + if (switch_core_session_check_interface(sp, loopback_endpoint_interface)) { + private_t *tech_pvt = switch_core_session_get_private(sp); + + a_uuid = switch_channel_get_partner_uuid(tech_pvt->other_channel); + switch_core_session_rwunlock(sp); + sp = NULL; + } else { + break; + } + } + + if (sp) { + *br_session = sp; + *br_uuid = a_uuid; + return SWITCH_STATUS_SUCCESS; + } + + return SWITCH_STATUS_FALSE; + +} + static switch_status_t channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id) { switch_channel_t *channel = NULL; @@ -678,8 +711,8 @@ static switch_status_t channel_write_frame(switch_core_session_t *session, switc switch_channel_test_flag(tech_pvt->other_channel, CF_BRIDGED) && switch_channel_test_flag(tech_pvt->channel, CF_ANSWERED) && switch_channel_test_flag(tech_pvt->other_channel, CF_ANSWERED) && --tech_pvt->bowout_frame_count <= 0) { - const char *a_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); - const char *b_uuid = switch_channel_get_variable(tech_pvt->other_channel, SWITCH_SIGNAL_BOND_VARIABLE); + const char *a_uuid = NULL; + const char *b_uuid = NULL; const char *vetoa, *vetob; @@ -690,16 +723,21 @@ static switch_status_t channel_write_frame(switch_core_session_t *session, switc switch_core_session_t *br_a, *br_b; switch_channel_t *ch_a = NULL, *ch_b = NULL; int good_to_go = 0; + + find_non_loopback_bridge(session, &br_a, &a_uuid); + find_non_loopback_bridge(tech_pvt->other_session, &br_b, &b_uuid); + - if ((br_a = switch_core_session_locate(a_uuid))) { + if (br_a) { ch_a = switch_core_session_get_channel(br_a); } - if ((br_b = switch_core_session_locate(b_uuid))) { + if (br_b) { ch_b = switch_core_session_get_channel(br_b); } if (ch_a && ch_b && switch_channel_test_flag(ch_a, CF_BRIDGED) && switch_channel_test_flag(ch_b, CF_BRIDGED)) { + switch_set_flag_locked(tech_pvt, TFLAG_BOWOUT); switch_set_flag_locked(tech_pvt->other_tech_pvt, TFLAG_BOWOUT); @@ -881,7 +919,7 @@ static switch_status_t loopback_bowout_on_execute_state_handler(switch_core_sess /* Wait for b_channel to be fully bridged */ switch_channel_wait_for_flag(b_channel, CF_BRIDGED, SWITCH_TRUE, 5000, NULL); - uuid = switch_channel_get_variable(b_channel, SWITCH_SIGNAL_BOND_VARIABLE); + uuid = switch_channel_get_partner_uuid(b_channel); if (uuid && (other_session = switch_core_session_locate(uuid))) { switch_channel_t *other_channel = switch_core_session_get_channel(other_session); diff --git a/src/mod/endpoints/mod_media_gateway/Makefile b/src/mod/endpoints/mod_media_gateway/Makefile new file mode 100644 index 0000000000..30b36a1b20 --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/Makefile @@ -0,0 +1,13 @@ +ifndef ARCH + ARCH=$(shell uname -m) +endif + +ifeq ($(ARCH),x86_64) + LOCAL_CFLAGS+=-DBIT_64 -DALIGN_64BIT +endif + +BASE=../../../.. +LOCAL_OBJS=media_gateway.o media_gateway_stack.o media_gateway_xml.o media_gateway_cli.o media_gateway_stack_alarms.o media_gateway_cmd_handler.o media_gateway_utils.o media_gateway_packages.o +LOCAL_CFLAGS=-fno-strict-aliasing +LOCAL_LDFLAGS=-lsng_mg +include $(BASE)/build/modmake.rules diff --git a/src/mod/endpoints/mod_media_gateway/conf/media_gateway.conf.xml b/src/mod/endpoints/mod_media_gateway/conf/media_gateway.conf.xml new file mode 100755 index 0000000000..aa314fbe5b --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/conf/media_gateway.conf.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mod/endpoints/mod_media_gateway/conf/megaco.conf.xml b/src/mod/endpoints/mod_media_gateway/conf/megaco.conf.xml new file mode 100644 index 0000000000..91f8f0cc63 --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/conf/megaco.conf.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway.c b/src/mod/endpoints/mod_media_gateway/media_gateway.c new file mode 100644 index 0000000000..98c10a7ca3 --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/media_gateway.c @@ -0,0 +1,926 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Mathieu Rene +* All rights reserved. +* +* +*/ + +#include "mod_media_gateway.h" +#include "media_gateway_stack.h" + +megaco_profile_t *megaco_profile_locate(const char *name) +{ + megaco_profile_t *profile = switch_core_hash_find_rdlock(megaco_globals.profile_hash, name, megaco_globals.profile_rwlock); + + if (profile) { + if (switch_thread_rwlock_tryrdlock(profile->rwlock) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", name); + profile = NULL; + } + } + + return profile; +} + + + +mg_peer_profile_t *megaco_peer_profile_locate(const char *name) +{ + mg_peer_profile_t *profile = switch_core_hash_find_rdlock(megaco_globals.peer_profile_hash, name, megaco_globals.peer_profile_rwlock); + + if (profile) { + if (switch_thread_rwlock_tryrdlock(profile->rwlock) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", name); + profile = NULL; + } + } + + return profile; +} + +void megaco_profile_release(megaco_profile_t *profile) +{ + switch_thread_rwlock_unlock(profile->rwlock); +} + +void megaco_peer_profile_release(mg_peer_profile_t *profile) +{ + switch_thread_rwlock_unlock(profile->rwlock); +} + +megaco_profile_t* megaco_get_profile_by_suId(SuId suId) +{ + megaco_profile_t* profile = NULL; + void *val = NULL; + switch_hash_index_t *hi = NULL; + int found = 0x00; + const void *var; + + /*iterate through profile list to get requested suID profile */ + + switch_thread_rwlock_rdlock(megaco_globals.profile_rwlock); + for (hi = switch_hash_first(NULL, megaco_globals.profile_hash); hi; hi = switch_hash_next(hi)) { + switch_hash_this(hi, &var, NULL, &val); + profile = (megaco_profile_t *) val; + if (profile->idx == suId) { + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Got profile[%s] associated with suId[%d]\n",profile->name, suId); + found = 0x01; + break; + } + } + + if(!found){ + profile = NULL; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Not able to find profile associated with suId[%d]\n",suId); + } + + switch_thread_rwlock_unlock(megaco_globals.profile_rwlock); + + return profile; +} + + +static switch_status_t mg_on_dtmf(switch_core_session_t *session, const switch_dtmf_t *dtmf, switch_dtmf_direction_t direction) +{ + switch_channel_t *channel = switch_core_session_get_channel(session); + mg_termination_t *term = switch_channel_get_private(channel, PVT_MG_TERM); + //char digit[2] = { dtmf->digit }; + if (!term) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find termination structure for session [%s]\n", + switch_core_session_get_uuid(session)); + return SWITCH_STATUS_SUCCESS; + } + + //switch_status_t mg_send_dtmf_notify(megaco_profile_t* mg_profile, const char* term_name, char* digits, int num_of_collected_digits); + //mg_send_dtmf_notify(term->profile, term->name, digit, 1); + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MEGACO DTMF Signaling NOT Sending notify to MGC for dtmf:%c\n",dtmf->digit); + return SWITCH_STATUS_SUCCESS; +} + +/* + * Creates a freeswitch channel for the specified termination. + * The channel will be parked until future actions are taken + */ +switch_status_t megaco_activate_termination(mg_termination_t *term) +{ + switch_event_t *var_event = NULL; + switch_core_session_t *session = NULL; + switch_status_t status = SWITCH_STATUS_SUCCESS; + char dialstring[100]; + switch_call_cause_t cause; + + switch_event_create(&var_event, SWITCH_EVENT_CLONE); + + if (term->type == MG_TERM_RTP) { + switch_snprintf(dialstring, sizeof dialstring, "rtp/%s", term->name); + + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, kLOCALADDR, term->u.rtp.local_addr); + switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, kLOCALPORT, "%d", term->u.rtp.local_port); + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, kREMOTEADDR, term->u.rtp.remote_addr); + switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, kREMOTEPORT, "%d", term->u.rtp.remote_port); + + switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, kPTIME, "%d", term->u.rtp.ptime); + switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, kPT, "%d", term->u.rtp.pt); + switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, kRFC2833PT, "%d", term->u.rtp.rfc2833_pt); + switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, kRATE, "%d", term->u.rtp.rate); + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, kCODEC, term->u.rtp.codec); + + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, kMEDIATYPE, mg_media_type2str(term->u.rtp.media_type)); + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "fax_enable_t38", "true"); + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "rtp_execute_on_image", "t38_gateway self nocng"); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s local_add[%s]\n",__FUNCTION__, term->u.rtp.local_addr); + } else if (term->type == MG_TERM_TDM) { + switch_snprintf(dialstring, sizeof dialstring, "tdm/%s", term->name); + + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, kSPAN_NAME, term->u.tdm.span_name); + switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, kCHAN_ID, "%d", term->u.tdm.channel); + switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, kPREBUFFER_LEN, "%d", term->profile->tdm_pre_buffer_size); + } + + /* Set common variables on the channel */ + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, SWITCH_PARK_AFTER_BRIDGE_VARIABLE, "true"); + + if (!zstr(term->uuid)) { + /* A UUID is present, check if the channel still exists */ + switch_core_session_t *session; + if ((session = switch_core_session_locate(term->uuid))) { + switch_channel_t *channel = switch_core_session_get_channel(session); + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "command", "media_modify"); + + if (term->type == MG_TERM_RTP) { + if (term->u.rtp.t38_options) { + switch_channel_set_private(channel, "t38_options", term->u.rtp.t38_options); + } + + if (term->u.rtp.media_type == MGM_IMAGE) { + mg_term_set_pre_buffer_size(term, 0); + } + } + + switch_core_session_receive_event(session, &var_event); + + switch_core_session_rwunlock(session); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Sent refresh to channel [%s], for termination [%s]\n", term->uuid, term->name); + + return SWITCH_STATUS_SUCCESS; + } + + /* The referenced channel doesn't exist anymore, clear it */ + term->uuid = NULL; + } + + if (zstr(term->uuid)) { + switch_channel_t *channel; + if (switch_ivr_originate(NULL, &session, &cause, dialstring, 0, NULL, NULL, NULL, NULL, var_event, 0, NULL) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to instanciate termination [%s]: %s\n", term->name, switch_channel_cause2str(cause)); + status = SWITCH_STATUS_FALSE; + goto done; + } + + term->uuid = switch_core_strdup(term->pool, switch_core_session_get_uuid(session)); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Termination [%s] successfully instanciated as [%s] [%s]\n", term->name, dialstring, switch_core_session_get_uuid(session)); + channel = switch_core_session_get_channel(session); + switch_channel_set_private(channel, PVT_MG_TERM, term); + + if (term->type == MG_TERM_RTP && term->u.rtp.t38_options) { + switch_channel_set_private(channel, "t38_options", term->u.rtp.t38_options); + } + + switch_core_event_hook_add_recv_dtmf(session, mg_on_dtmf); + if (term->type == MG_TERM_TDM){ + switch_core_session_execute_application_async(session, "spandsp_start_fax_detect", "mg_notify ced 120 ced"); + switch_core_session_execute_application_async(session, "spandsp_start_fax_detect", "mg_notify cng 120 cng"); + } + + } + + switch_set_flag(term, MGT_ACTIVE); + +done: + if (session) { + switch_core_session_rwunlock(session); + } + switch_event_destroy(&var_event); + + return status; +} + +switch_status_t megaco_tdm_term_dtmf_removal(mg_termination_t *term, int enable) +{ + char buf[128]; + switch_event_t *event = NULL; + mg_termination_t* tdm_term = NULL; + + if(NULL == term) return SWITCH_STATUS_FALSE; + + if(MG_TERM_RTP == term->type){ + if(NULL == term->context) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Null Context from rtp term, not able to get tdm term \n"); + return SWITCH_STATUS_FALSE; + } + tdm_term = megaco_context_get_peer_term(term->context, term); + }else{ + tdm_term = term; + } + + if(NULL == tdm_term) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Null TDM term \n"); + return SWITCH_STATUS_FALSE; + } + + memset(&buf[0],0,sizeof(buf)); + + if (switch_event_create(&event, SWITCH_EVENT_TRAP) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to create NOTIFY event\n"); + return SWITCH_STATUS_FALSE; + } + + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "span-name", "%s", tdm_term->u.tdm.span_name); + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "chan-number", "%d", tdm_term->u.tdm.channel); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "condition", "mg-tdm-dtmfremoval"); + + sprintf(buf,"%s",(1 == enable)?"enable":"disable"); + + if(enable){ + switch_set_flag(term, MG_DTMF_REMOVAL_ENABLE); + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Sending DTMF Removal Event[%s] for MG Term[%s], TDM span[%s] channel[%d]\n", + buf,tdm_term->name, tdm_term->u.tdm.span_name, tdm_term->u.tdm.channel); + + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "command", buf); + + switch_event_fire(&event); + return SWITCH_STATUS_SUCCESS; +} + +switch_status_t megaco_prepare_tdm_termination(mg_termination_t *term) +{ + switch_event_t *event = NULL; + if (switch_event_create(&event, SWITCH_EVENT_TRAP) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to create NOTIFY event\n"); + return SWITCH_STATUS_FALSE; + } + + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "span-name", "%s", term->u.tdm.span_name); + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "chan-number", "%d", term->u.tdm.channel); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "condition", "mg-tdm-prepare"); + + switch_event_fire(&event); + return SWITCH_STATUS_SUCCESS; +} + +switch_status_t megaco_check_tdm_termination(mg_termination_t *term) +{ + switch_event_t *event = NULL; + + if(!term || !term->profile) return SWITCH_STATUS_FALSE; + + if (switch_event_create(&event, SWITCH_EVENT_TRAP) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to create NOTIFY event\n"); + return SWITCH_STATUS_FALSE; + } + + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "span-name", "%s", term->u.tdm.span_name); + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "chan-number", "%d", term->u.tdm.channel); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "condition", "mg-tdm-check"); + + switch_event_fire(&event); + return SWITCH_STATUS_SUCCESS; +} + +mg_termination_t *megaco_choose_termination(megaco_profile_t *profile, const char *prefix) +{ + mg_termination_type_t termtype; + switch_memory_pool_t *pool; + mg_termination_t *term = NULL; + char name[100]; + int term_id; + size_t prefixlen = strlen(prefix); + + /* Check the termination type by prefix */ + if (strncasecmp(prefix, profile->rtp_termination_id_prefix, strlen(profile->rtp_termination_id_prefix)) == 0) { + termtype = MG_TERM_RTP; + term_id = mg_rtp_request_id(profile); + switch_snprintf(name, sizeof name, "%s/%d", profile->rtp_termination_id_prefix, term_id); + } else { + for (term = profile->physical_terminations; term; term = term->next) { + if (!switch_test_flag(term, MGT_ALLOCATED) && !strncasecmp(prefix, term->name, prefixlen)) { + switch_set_flag(term, MGT_ALLOCATED); + return term; + } + } + + return NULL; + } + + switch_core_new_memory_pool(&pool); + term = switch_core_alloc(pool, sizeof *term); + term->pool = pool; + term->type = termtype; + term->active_events = NULL; + term->profile = profile; + switch_set_flag(term, MGT_ALLOCATED); + + if (termtype == MG_TERM_RTP) { + /* Fill in local address and reserve an rtp port */ + //term->u.rtp.local_addr = profile->my_ipaddr; + term->u.rtp.local_addr = profile->rtp_ipaddr; + term->u.rtp.local_port = switch_rtp_request_port(term->u.rtp.local_addr); + term->u.rtp.codec = megaco_codec_str(profile->default_codec); + term->u.rtp.term_id = term_id; + term->u.rtp.ptime = 20; + term->name = switch_core_strdup(term->pool, name); + } + + switch_core_hash_insert_wrlock(profile->terminations, term->name, term, profile->terminations_rwlock); + + return term; +} + +mg_termination_t *megaco_term_locate_by_span_chan_id(const char *span_name, const char *chan_number) +{ + void *val = NULL; + switch_hash_index_t *hi = NULL; + mg_termination_t *term = NULL; + megaco_profile_t *profile = NULL; + const void *var; + + switch_assert(span_name); + switch_assert(chan_number); + + /* span + chan will be unique across all the mg_profiles * + * loop through all profiles and then all terminations to * + * get the mg termination associated with input span+chan */ + + switch_thread_rwlock_rdlock(megaco_globals.profile_rwlock); + for (hi = switch_hash_first(NULL, megaco_globals.profile_hash); hi; hi = switch_hash_next(hi)) { + switch_hash_this(hi, &var, NULL, &val); + profile = (megaco_profile_t *) val; + + if(NULL != (term = megaco_find_termination_by_span_chan(profile, span_name, chan_number))) break; + } + switch_thread_rwlock_unlock(megaco_globals.profile_rwlock); + + return term; +} + +mg_termination_t* megaco_find_termination_by_span_chan(megaco_profile_t *profile, const char *span_name, const char *chan_number) +{ + void *val = NULL; + switch_hash_index_t *hi = NULL; + mg_termination_t *term = NULL; + int found = 0x00; + const void *var; + + if(!span_name || !chan_number){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Invalid span_name/chan_number \n"); + return NULL; + } + + for (hi = switch_hash_first(NULL, profile->terminations); hi; hi = switch_hash_next(hi)) { + switch_hash_this(hi, &var, NULL, &val); + term = (mg_termination_t *) val; + if(!term) continue; + if(MG_TERM_TDM != term->type) continue; + + if ((!strcasecmp(span_name, term->u.tdm.span_name))&& (atoi(chan_number) == term->u.tdm.channel)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Got term[%s] associated with span[%s], channel[%s] in MG profile[%s]\n", + term->name, span_name, chan_number, profile->name); + found = 0x01; + break; + } + } + + if(!found){ + term = NULL; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + " MG profile[%s] does not have termination associated with span[%s], channel[%s]\n", + profile->name, span_name, chan_number); + } + + return term; +} + +mg_termination_t *megaco_find_termination(megaco_profile_t *profile, const char *name) +{ + mg_termination_t *term = switch_core_hash_find_rdlock(profile->terminations, name, profile->terminations_rwlock); + return term; + +} + +void megaco_termination_destroy(mg_termination_t *term) +{ + /* Lookup the FS session and hang it up */ + switch_core_session_t *session; + switch_channel_t *channel; + + if ((session = switch_core_session_locate(term->uuid))) { + channel = switch_core_session_get_channel(session); + switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING); + switch_core_session_rwunlock(session); + term->uuid = NULL; + } + + if (term->type == MG_TERM_RTP ){ + if(term->u.rtp.local_port != 0) { + switch_rtp_release_port(term->u.rtp.local_addr, term->u.rtp.local_port); + } + if(term->u.rtp.term_id != 0) { + mg_rtp_release_id(term->profile,term->u.rtp.term_id); + } + } + + if(term->active_events){ + mgUtlDelMgMgcoReqEvtDesc(term->active_events); + MG_STACK_MEM_FREE(term->active_events, sizeof(MgMgcoReqEvtDesc)); + } + term->context = NULL; + + + switch_clear_flag(term, MGT_ALLOCATED); + switch_clear_flag(term, MGT_ACTIVE); + switch_clear_flag(term, MG_FAX_NOTIFIED); + + if(switch_test_flag(term, MG_DTMF_REMOVAL_ENABLE)){ + switch_clear_flag(term, MG_DTMF_REMOVAL_ENABLE); + //megaco_tdm_term_dtmf_removal(term,0x00); + } + + if (term->type == MG_TERM_RTP) { + switch_core_hash_delete_wrlock(term->profile->terminations, term->name, term->profile->terminations_rwlock); + switch_core_destroy_memory_pool(&term->pool); + } +} + +mg_termination_t* megaco_context_get_peer_term(mg_context_t *ctx, mg_termination_t *term) +{ + + switch_assert(ctx != NULL); + switch_assert(term != NULL); + + if (ctx->terminations[0] && (term == ctx->terminations[0])) { + return ctx->terminations[1]; + } + + if (ctx->terminations[1] && (term == ctx->terminations[1])) { + return ctx->terminations[0]; + } + + return NULL; +} + +switch_status_t megaco_context_is_term_present(mg_context_t *ctx, mg_termination_t *term) +{ + + switch_assert(ctx != NULL); + switch_assert(term != NULL); + + if (ctx->terminations[0] && (term == ctx->terminations[0])) { + return SWITCH_STATUS_SUCCESS; + } + + if (ctx->terminations[1] && (term == ctx->terminations[1])) { + return SWITCH_STATUS_SUCCESS; + } + + return SWITCH_STATUS_FALSE; +} + +switch_status_t megaco_context_add_termination(mg_context_t *ctx, mg_termination_t *term) +{ + mg_termination_t* tdm_term = NULL; + switch_status_t status = SWITCH_STATUS_SUCCESS; + + switch_assert(ctx != NULL); + switch_assert(term != NULL); + + /* Check if the current context has existing terminations */ + if (ctx->terminations[0] && ctx->terminations[1]) { + /* Context is full */ + return SWITCH_STATUS_FALSE; + } + + term->context = ctx; + if (ctx->terminations[0]) { + ctx->terminations[1] = term; + } else if (ctx->terminations[1]) { + ctx->terminations[0] = term; + } else { + ctx->terminations[0] = term; + } + + if (ctx->terminations[0] && ctx->terminations[1]) { + if (zstr(ctx->terminations[0]->uuid)) { + if(SWITCH_STATUS_SUCCESS != (status = megaco_activate_termination(ctx->terminations[0]))){ + return status; + } + } + if (zstr(ctx->terminations[1]->uuid)) { + if(SWITCH_STATUS_SUCCESS != (status = megaco_activate_termination(ctx->terminations[1]))){ + return status; + } + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Bridging: %s (%s) <> %s (%s)\n", + ctx->terminations[0]->name, ctx->terminations[0]->uuid, + ctx->terminations[1]->name, ctx->terminations[1]->uuid); + + switch_ivr_uuid_bridge(ctx->terminations[0]->uuid, ctx->terminations[1]->uuid); + + if(MG_TERM_TDM == ctx->terminations[0]->type){ + tdm_term = ctx->terminations[0]; + }else{ + tdm_term = ctx->terminations[1]; + } + if(MG_EC_ENABLE == tdm_term->ec_type){ + mg_term_set_ec(tdm_term,0x01); + }else if(MG_EC_DISABLE == tdm_term->ec_type){ + mg_term_set_ec(tdm_term,0x00); + } + mg_set_term_ec_status(tdm_term, MG_EC_UNDEFINED); + + ctx->terminations[0]->profile->mg_stats->total_num_of_call_recvd++; + } + + return SWITCH_STATUS_SUCCESS; +} + + +switch_status_t megaco_context_sub_all_termination(mg_context_t *ctx) +{ + switch_assert(ctx != NULL); + + /* Channels will automatically go to park once the bridge ends */ + if (ctx->terminations[0]) { + megaco_context_sub_termination(ctx, ctx->terminations[0]); + } + + if (ctx->terminations[1]) { + megaco_context_sub_termination(ctx, ctx->terminations[1]); + } + + return SWITCH_STATUS_SUCCESS; +} + +switch_status_t megaco_context_sub_termination(mg_context_t *ctx, mg_termination_t *term) +{ + switch_assert(ctx != NULL); + switch_assert(term != NULL); + + /* Channels will automatically go to park once the bridge ends */ + if (ctx->terminations[0] == term) { + ctx->terminations[0] = NULL; + } else if (ctx->terminations[1] == term) { + ctx->terminations[1] = NULL; + } + + megaco_termination_destroy(term); + + return SWITCH_STATUS_SUCCESS; +} + + +switch_status_t megaco_context_move_termination(mg_context_t *dst, mg_termination_t *term) +{ + + return SWITCH_STATUS_SUCCESS; +} + +mg_context_t *megaco_find_context_by_suid(SuId suId, uint32_t context_id) +{ + megaco_profile_t* profile = NULL; + + if(NULL == (profile = megaco_get_profile_by_suId(suId))){ + return NULL; + } + + return megaco_get_context(profile, context_id); +} + +mg_context_t *megaco_get_context(megaco_profile_t *profile, uint32_t context_id) +{ + mg_context_t *result = NULL; + + if (context_id > MG_MAX_CONTEXTS) { + return NULL; + } + + switch_thread_rwlock_rdlock(profile->contexts_rwlock); + + /* Context exists */ + if (profile->contexts_bitmap[context_id / 8] & (1 << (context_id % 8))) { + for (result = profile->contexts[context_id % MG_CONTEXT_MODULO]; result; result = result->next) { + if (result->context_id == context_id) { + break; + } + } + } + + switch_thread_rwlock_unlock(profile->contexts_rwlock); + + return result; +} + +/* Returns a fresh new context */ +mg_context_t *megaco_choose_context(megaco_profile_t *profile) +{ + mg_context_t *ctx=NULL; + + switch_thread_rwlock_wrlock(profile->contexts_rwlock); + /* Try the next one */ + if (profile->next_context_id >= MG_MAX_CONTEXTS) { + profile->next_context_id = 1; + } + + /* Look for an available context */ + for (; profile->next_context_id < MG_MAX_CONTEXTS; profile->next_context_id++) { + if ((profile->contexts_bitmap[profile->next_context_id / 8] & (1 << (profile->next_context_id % 8))) == 0) { + /* Found! */ + int i = profile->next_context_id % MG_CONTEXT_MODULO; + profile->contexts_bitmap[profile->next_context_id / 8] |= 1 << (profile->next_context_id % 8); + ctx = malloc(sizeof *ctx); + memset(ctx, 0, sizeof *ctx); + ctx->context_id = profile->next_context_id; + ctx->profile = profile; + + if (!profile->contexts[i]) { + profile->contexts[i] = ctx; + } else { + mg_context_t *it; + for (it = profile->contexts[i]; it && it->next; it = it->next) + ; + it->next = ctx; + } + + profile->next_context_id++; + break; + } + } + + switch_thread_rwlock_unlock(profile->contexts_rwlock); + + return ctx; +} + +void megaco_release_context(mg_context_t *ctx) +{ + uint32_t context_id = ctx->context_id; + megaco_profile_t *profile = ctx->profile; + int i = context_id % MG_CONTEXT_MODULO; + + switch_thread_rwlock_wrlock(profile->contexts_rwlock); + if (profile->contexts[i] == ctx) { + profile->contexts[i] = ctx->next; + } else { + mg_context_t *it = profile->contexts[i]->next, *prev = profile->contexts[i]; + for (; it; prev = it, it = it->next) { + if (it == ctx) { + prev->next = it->next; + break; + } + } + } + + profile->contexts_bitmap[context_id / 8] &= ~(1 << (context_id % 8)); + + memset(ctx, 0, sizeof *ctx); + free(ctx); + + switch_thread_rwlock_unlock(profile->contexts_rwlock); +} + +uint32_t mg_rtp_request_id(megaco_profile_t *profile) +{ + uint32_t rtp_id = 0x00; + + if (profile->rtpid_next >= MG_MAX_RTPID || profile->rtpid_next == 0) { + profile->rtpid_next = 1; + } + + for (; profile->rtpid_next < MG_MAX_RTPID; profile->rtpid_next++) { + if ((profile->rtpid_bitmap[profile->rtpid_next / 8] & (1 << (profile->rtpid_next % 8))) == 0) { + profile->rtpid_bitmap[profile->rtpid_next / 8] |= 1 << (profile->rtpid_next % 8); + rtp_id = profile->rtpid_next; + profile->rtpid_next++; + return rtp_id; + } + } + + return 0; +} + +void mg_rtp_release_id(megaco_profile_t *profile, uint32_t id) +{ + profile->rtpid_bitmap[id / 8] &= ~(1 << (id % 8)); +} + +switch_status_t megaco_profile_start(const char *profilename) +{ + switch_memory_pool_t *pool; + megaco_profile_t *profile; + + switch_assert(profilename); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Starting profile: %s\n", profilename); + + switch_core_new_memory_pool(&pool); + profile = switch_core_alloc(pool, sizeof(*profile)); + profile->pool = pool; + profile->physical_terminations = NULL; + profile->name = switch_core_strdup(pool, profilename); + profile->next_context_id++; + profile->inact_tmr = 0x00; + profile->total_cfg_term = 0x00; + profile->peer_active = 0x00; + profile->mg_stats = switch_core_alloc(pool, sizeof(mg_stats_t)); + profile->inact_tmr_task_id = 0x00; + + switch_thread_rwlock_create(&profile->rwlock, pool); + + switch_thread_rwlock_create(&profile->contexts_rwlock, pool); + switch_thread_rwlock_create(&profile->terminations_rwlock, pool); + + switch_core_hash_init(&profile->terminations, pool); + + if (SWITCH_STATUS_SUCCESS != config_profile(profile, SWITCH_FALSE)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error configuring profile %s\n", profile->name); + goto fail; + } + + if(SWITCH_STATUS_FALSE == sng_mgco_start(profile)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error starting MEGACO Stack for profile %s\n", profile->name); + goto fail; + } + + switch_core_hash_insert_wrlock(megaco_globals.profile_hash, profile->name, profile, megaco_globals.profile_rwlock); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Started profile: %s\n", profile->name); + + return SWITCH_STATUS_SUCCESS; + +fail: + switch_core_destroy_memory_pool(&pool); + return SWITCH_STATUS_FALSE; +} + + +switch_status_t megaco_profile_destroy(megaco_profile_t **profile) +{ + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Stopping profile: %s\n", (*profile)->name); + switch_thread_rwlock_wrlock((*profile)->rwlock); + + + if(SWITCH_STATUS_FALSE == sng_mgco_stop((*profile))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error stopping MEGACO Stack for profile %s\n", (*profile)->name); + } + + /* TODO: Cleanup contexts */ + + switch_thread_rwlock_unlock((*profile)->rwlock); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Stopped profile: %s\n", (*profile)->name); + switch_core_hash_delete_wrlock(megaco_globals.profile_hash, (*profile)->name, megaco_globals.profile_rwlock); + + mg_config_cleanup(*profile); + + switch_core_destroy_memory_pool(&(*profile)->pool); + + return SWITCH_STATUS_SUCCESS; +} + +switch_status_t mgco_process_mgc_failure(SuId suId) +{ + megaco_profile_t* profile = NULL; + + if(NULL == (profile = megaco_get_profile_by_suId(suId))){ + return SWITCH_STATUS_FALSE; + } + + if(0x01 == profile->peer_active){ + /* MGC failure during active association , release all on-going calls contexts */ + megaco_release_all_calls(profile); + } + + return SWITCH_STATUS_SUCCESS; +} + +switch_status_t mgco_init_ins_service_change(SuId suId) +{ + megaco_profile_t* profile = NULL; + void *val = NULL; + const void *key = NULL; + switch_ssize_t keylen; + switch_hash_index_t *hi = NULL; + mg_termination_t *term = NULL; + + + if(NULL == (profile = megaco_get_profile_by_suId(suId))){ + return SWITCH_STATUS_FALSE; + } + + profile->peer_active = 0x01; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "mgco_init_ins_service_change : Initiating terminations service change for profile: %s\n", profile->name); + + /* loop through all termination and post get status Event */ + for (hi = switch_hash_first(NULL, profile->terminations); hi; hi = switch_hash_next(hi)) { + switch_hash_this(hi, &key, &keylen, &val); + term = (mg_termination_t *) val; + if(!term) continue; + if(MG_TERM_RTP == term->type) continue; + megaco_check_tdm_termination(term); + } + + return SWITCH_STATUS_SUCCESS; +} + +switch_status_t megaco_peer_profile_destroy(mg_peer_profile_t **profile) +{ + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Stopping peer profile: %s\n", (*profile)->name); + + switch_core_hash_delete_wrlock(megaco_globals.peer_profile_hash, (*profile)->name, megaco_globals.peer_profile_rwlock); + + mg_peer_config_cleanup(*profile); + + switch_core_destroy_memory_pool(&(*profile)->pool); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Stopped peer profile: %s\n", (*profile)->name); + + return SWITCH_STATUS_SUCCESS; +} + +void mg_term_set_pre_buffer_size(mg_termination_t *term, int newval) +{ + switch_event_t *event = NULL, *event2 = NULL; + switch_core_session_t *session, *session2; + + if (!zstr(term->uuid) && (session = switch_core_session_locate(term->uuid))) { + switch_event_create(&event, SWITCH_EVENT_CLONE); + + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "command", kPREBUFFER_LEN); + switch_event_add_header(event, SWITCH_STACK_BOTTOM, kPREBUFFER_LEN, "%d", newval); + + /* Propagate event to bridged session if there is one */ + if (switch_core_session_get_partner(session, &session2) == SWITCH_STATUS_SUCCESS) { + switch_event_dup(&event2, event); + switch_core_session_receive_event(session2, &event2); + switch_core_session_rwunlock(session2); + } + + switch_core_session_receive_event(session, &event); + switch_core_session_rwunlock(session); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Sent prebuffer_size event to [%s] to [%d ms]\n", term->uuid, newval); + } + + switch_event_destroy(&event); +} + + +void mg_term_set_ec(mg_termination_t *term, int enable) +{ + switch_event_t *event = NULL, *event2 = NULL; + switch_core_session_t *session, *session2; + + if (!zstr(term->uuid) && (session = switch_core_session_locate(term->uuid))) { + switch_event_create(&event, SWITCH_EVENT_CLONE); + + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "command", kECHOCANCEL); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, kECHOCANCEL, enable ? "true" : "false"); + + /* Propagate event to bridged session if there is one */ + if (switch_core_session_get_partner(session, &session2) == SWITCH_STATUS_SUCCESS) { + switch_event_dup(&event2, event); + switch_core_session_receive_event(session2, &event2); + switch_core_session_rwunlock(session2); + } + + switch_core_session_receive_event(session, &event); + switch_core_session_rwunlock(session); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Sent echo_cancel event to [%s] to [%s]\n", term->uuid, enable ? "enable" : "disable"); + }else{ + mg_ec_types_t status = ((enable)?MG_EC_ENABLE:MG_EC_DISABLE); + mg_set_term_ec_status(term, status); + } + + switch_event_destroy(&event); +} + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4: + */ diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway_cli.c b/src/mod/endpoints/mod_media_gateway/media_gateway_cli.c new file mode 100644 index 0000000000..dabaf2cc94 --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/media_gateway_cli.c @@ -0,0 +1,1039 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Kapil Gupta +* All rights reserved. +* +* +*/ + +/* INCLUDES *******************************************************************/ +#include "mod_media_gateway.h" +#include "media_gateway_stack.h" +/******************************************************************************/ + +/* FUNCTION PROTOTYPES ********************************************************/ +switch_status_t megaco_profile_status(switch_stream_handle_t *stream, megaco_profile_t* mg_cfg); +switch_status_t megaco_profile_xmlstatus(switch_stream_handle_t *stream, megaco_profile_t* mg_cfg); +switch_status_t megaco_profile_peer_xmlstatus(switch_stream_handle_t *stream, megaco_profile_t* mg_cfg); +switch_status_t handle_term_status_cli_cmd(switch_stream_handle_t *stream, megaco_profile_t* mg_profile, char* term_id); +switch_status_t handle_all_term_status_cli_cmd(switch_stream_handle_t *stream, megaco_profile_t* mg_profile); +void get_peer_xml_buffer(char* prntBuf, MgPeerSta* cfm); +void megaco_cli_print_usage(switch_stream_handle_t *stream); +switch_status_t handle_show_activecalls_cli_cmd(switch_stream_handle_t *stream, megaco_profile_t* mg_profile); +switch_status_t handle_show_stats(switch_stream_handle_t *stream, megaco_profile_t* mg_profile); +switch_status_t handle_show_stack_mem(switch_stream_handle_t *stream); +switch_status_t handle_span_term_status_cli_cmd(switch_stream_handle_t *stream, megaco_profile_t* mg_profile, char* span_name); + +/******************************************************************************/ + +/* FUNCTIONS ******************************************************************/ + +switch_status_t mg_process_cli_cmd(const char *cmd, switch_stream_handle_t *stream) +{ + int argc; + char* argv[10]; + char* dup = NULL; + int wild = 0x00; + megaco_profile_t* profile = NULL; + + if (zstr(cmd)) { + goto usage; + } + + dup = strdup(cmd); + argc = switch_split(dup, ' ', argv); + + if (argc < 1 || zstr(argv[0])) { + goto usage; + } + +/**********************************************************************************/ + if (!strcmp(argv[0], "profile")) { + if (zstr(argv[1]) || zstr(argv[2])) { + goto usage; + } +/**********************************************************************************/ + profile = megaco_profile_locate(argv[1]); + +/**********************************************************************************/ + if (!strcmp(argv[2], "start")) { +/**********************************************************************************/ + if (profile) { + megaco_profile_release(profile); + stream->write_function(stream, "-ERR Profile %s is already started\n", argv[2]); + } else { + megaco_profile_start(argv[1]); + stream->write_function(stream, "+OK\n"); + } +/**********************************************************************************/ + } else if (!strcmp(argv[2], "stop")) { +/**********************************************************************************/ + if (profile) { + megaco_profile_release(profile); + megaco_profile_destroy(&profile); + stream->write_function(stream, "+OK\n"); + } else { + stream->write_function(stream, "-ERR No such profile\n"); + } + +/**********************************************************************************/ + }else if(!strcmp(argv[2], "status")) { +/**********************************************************************************/ + if (profile) { + megaco_profile_release(profile); + megaco_profile_status(stream, profile); + } else { + stream->write_function(stream, "-ERR No such profile\n"); + } +/**********************************************************************************/ + }else if(!strcmp(argv[2], "xmlstatus")) { +/**********************************************************************************/ + if (profile) { + megaco_profile_release(profile); + megaco_profile_xmlstatus(stream, profile); + } else { + stream->write_function(stream, "-ERR No such profile\n"); + } +/**********************************************************************************/ + }else if(!strcmp(argv[2], "peerxmlstatus")) { +/**********************************************************************************/ + if (profile) { + megaco_profile_release(profile); + megaco_profile_peer_xmlstatus(stream, profile); + } else { + stream->write_function(stream, "-ERR No such profile\n"); + } +/**********************************************************************************/ + }else if(!strcmp(argv[2], "send")) { +/**********************************************************************************/ + if (profile) { + switch(argc) + { + case 7: + { + /* mg profile send sc */ + if(zstr(argv[3]) || zstr(argv[4]) || zstr(argv[5]) || zstr(argv[6])){ + goto usage; + } + + if(!zstr(argv[7]) && !strcasecmp(argv[7],"wild")){ + wild = 0x01; + } + + printf("Input to Send Service Change command : " + "Profile Name[%s], term-id[%s] method[%s] reason[%s] \n", + profile->name, argv[4], argv[5], argv[6]); + + megaco_profile_release(profile); + mg_send_service_change(profile->idx, argv[4], atoi(argv[5]), atoi(argv[6]),wild); + + break; + } + case 6: + { + /* mg profile send notify */ + if(zstr(argv[3]) || zstr(argv[4]) || zstr(argv[5])){ + goto usage; + } + + if(strcasecmp(argv[3],"notify")){ + stream->write_function(stream, "-ERR wrong input \n"); + goto usage; + } + + printf("Sending DTMF digits[%s] NOTIFY for termination[%s]\n", argv[5], argv[4]); + + megaco_profile_release(profile); + mg_send_dtmf_notify(profile, argv[4], (char*)argv[5], (int)strlen(argv[5])); + + break; + } + case 5: + { + if(zstr(argv[3])){ + goto usage; + } + + /*************************************************************************/ + if(!strcasecmp(argv[3],"ito")){ + /* mg profile send ito notify */ + + printf("Sending In-Activity NOTIFY \n"); + + megaco_profile_release(profile); + mg_send_ito_notify(profile); + /*************************************************************************/ + }else if(!strcasecmp(argv[3],"cng")){ + /*************************************************************************/ + /* mg profile send cng */ + + if(zstr(argv[4])){ + goto usage; + } + megaco_profile_release(profile); + mg_send_t38_cng_notify(profile, argv[4]); + + /*************************************************************************/ + }else { + stream->write_function(stream, "-ERR wrong input \n"); + goto usage; + } + /*************************************************************************/ + + break; + } + + default: + { + goto usage; + } + } + }else{ + stream->write_function(stream, "-ERR No such profile\n"); + } + +/**********************************************************************************/ + }else if (!strcmp(argv[2], "show")) { +/**********************************************************************************/ + /* mg show activecalls*/ + + if(zstr(argv[3])) { + goto usage; + } + + if(profile){ + if(!strcasecmp(argv[3], "activecalls")){ + /* mg show activecalls */ + megaco_profile_release(profile); + handle_show_activecalls_cli_cmd(stream, profile); + /*******************************************************************/ + } else if(!strcasecmp(argv[3], "stats")){ + /*******************************************************************/ + /* mg show stats */ + megaco_profile_release(profile); + handle_show_stats(stream, profile); + /*******************************************************************/ + }else if(!strcasecmp(argv[3], "alltermstatus")){ + /*******************************************************************/ + /* mg show alltermstatus */ + megaco_profile_release(profile); + handle_all_term_status_cli_cmd(stream, profile); + /*******************************************************************/ + }else if(!strcasecmp(argv[3], "termstatus")){ + /*******************************************************************/ + /* mg show termstatus */ + if (zstr(argv[4])) { + goto usage; + } + megaco_profile_release(profile); + handle_term_status_cli_cmd(stream, profile, argv[4]); + /*******************************************************************/ + }else if(!strcasecmp(argv[3], "spantermstatus")){ + /*******************************************************************/ + /* mg show spantermstatus */ + if (zstr(argv[4])) { + goto usage; + } + megaco_profile_release(profile); + handle_span_term_status_cli_cmd(stream, profile, argv[4]); + + /*******************************************************************/ + }else if(!strcasecmp(argv[3], "stackmem")){ + /*******************************************************************/ + megaco_profile_release(profile); + handle_show_stack_mem(stream); + /*******************************************************************/ +#ifdef LEAK_TEST + }else if(!strcasecmp(argv[3], "leak-report")){ + /*******************************************************************/ + megaco_profile_release(profile); + mgPrntLeakReport(); + /*******************************************************************/ +#endif + } else { + /*******************************************************************/ + stream->write_function(stream, "-ERR No such profile\n"); + goto usage; + } + } +/**********************************************************************************/ + }else { +/**********************************************************************************/ + goto usage; + } +/**********************************************************************************/ + }else if (!strcmp(argv[0], "logging")) { +/**********************************************************************************/ + if (zstr(argv[1])) { + goto usage; + } + /******************************************************************/ + if(!strcasecmp(argv[1], "enable")){ + mg_enable_logging(); + /******************************************************************/ + }else if(!strcasecmp(argv[1], "disable")){ + /******************************************************************/ + mg_disable_logging(); + /******************************************************************/ + } else { + /******************************************************************/ + goto usage; + } +/**********************************************************************************/ + + }else { +/**********************************************************************************/ + goto usage; + } +/**********************************************************************************/ + + goto done; + +usage: + if(profile) + megaco_profile_release(profile); + + megaco_cli_print_usage(stream); + +done: + switch_safe_free(dup); + return SWITCH_STATUS_SUCCESS; +} + +/******************************************************************************/ +void megaco_cli_print_usage(switch_stream_handle_t *stream) +{ + + stream->write_function(stream, "Usage: Profile Specific\n"); + stream->write_function(stream, "mg profile start \n"); + stream->write_function(stream, "mg profile stop \n"); + //stream->write_function(stream, "mg profile status \n"); + //stream->write_function(stream, "mg profile xmlstatus \n"); + stream->write_function(stream, "mg profile peerxmlstatus \n"); + //stream->write_function(stream, "mg profile send sc \n"); + //stream->write_function(stream, "mg profile send notify \n"); + //stream->write_function(stream, "mg profile send ito notify \n"); + //stream->write_function(stream, "mg profile send cng \n"); + stream->write_function(stream, "mg profile show activecalls \n"); + stream->write_function(stream, "mg profile show spantermstatus \n"); + stream->write_function(stream, "mg profile show termstatus \n"); + stream->write_function(stream, "mg profile show alltermstatus \n"); + stream->write_function(stream, "mg profile show stackmem \n"); + stream->write_function(stream, "mg profile show stats \n"); + + stream->write_function(stream, "Usage: Logging \n"); + stream->write_function(stream, "mg logging enable \n"); + stream->write_function(stream, "mg logging disable \n"); + +} + +/******************************************************************************/ +switch_status_t megaco_profile_peer_xmlstatus(switch_stream_handle_t *stream, megaco_profile_t* mg_cfg) +{ + int idx = 0x00; + int peerIdx = 0x00; + int len = 0x00; + MgMngmt cfm; + char* xmlhdr = ""; + char prntBuf[3048]; + int i = 0x00; + char *asciiAddr; + CmInetIpAddr ip; + mg_peer_profile_t* mg_peer = NULL; + + switch_assert(mg_cfg); + + memset((U8 *)&cfm, 0, sizeof(cfm)); + memset((char *)&prntBuf, 0, sizeof(prntBuf)); + + idx = mg_cfg->idx; + + len = len + sprintf(&prntBuf[0] + len,"%s\n",xmlhdr); + + len = len + sprintf(&prntBuf[0] + len,"\n"); + + for(peerIdx =0; peerIdx < mg_cfg->total_peers; peerIdx++){ + + mg_peer = megaco_peer_profile_locate(mg_cfg->peer_list[peerIdx]); + + if(!mg_peer){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," No MG peer configuration found for peername[%s] against profilename[%s]\n",mg_cfg->peer_list[peerIdx],mg_cfg->name); + return SWITCH_STATUS_FALSE; + } + + len = len + sprintf(&prntBuf[0] + len,"\n"); + len = len + sprintf(&prntBuf[0] + len,"%s\n",mg_peer->name); + + /* send request to MEGACO Trillium stack to get peer information*/ + sng_mgco_mg_get_status(STGCPENT, &cfm, mg_cfg, mg_peer); + + ip = ntohl(cfm.t.ssta.s.mgPeerSta.peerAddrTbl.netAddr[i].u.ipv4NetAddr); + cmInetNtoa(ip, &asciiAddr); + len = len + sprintf(prntBuf+len, "%s\n",asciiAddr); + + len = len + sprintf(prntBuf+len, "%s\n",PRNT_MG_PEER_STATE(cfm.t.ssta.s.mgPeerSta.peerState)); + + len = len + sprintf(&prntBuf[0] + len,"\n"); + } + + len = len + sprintf(&prntBuf[0] + len,"\n"); + + stream->write_function(stream, "\n%s\n",&prntBuf[0]); + + return SWITCH_STATUS_SUCCESS; +} + +/******************************************************************************/ + +switch_status_t megaco_profile_xmlstatus(switch_stream_handle_t *stream, megaco_profile_t* mg_cfg) +{ + int idx = 0x00; + int peerIdx = 0x00; + int len = 0x00; + MgMngmt cfm; + char* xmlhdr = ""; + char prntBuf[3048]; + int i = 0x00; + char *asciiAddr; + CmInetIpAddr ip; + mg_peer_profile_t* mg_peer = NULL; + + switch_assert(mg_cfg); + + memset((U8 *)&cfm, 0, sizeof(cfm)); + memset((char *)&prntBuf, 0, sizeof(prntBuf)); + + + + idx = mg_cfg->idx; + + len = len + sprintf(&prntBuf[0] + len,"%s\n",xmlhdr); + + len = len + sprintf(&prntBuf[0] + len,"\n"); + len = len + sprintf(&prntBuf[0] + len,"%s\n",mg_cfg->name); +/****************************************************************************************************************/ +/* Print Peer Information ***************************************************************************************/ + + len = len + sprintf(&prntBuf[0] + len,"\n"); + + for(peerIdx =0; peerIdx < mg_cfg->total_peers; peerIdx++){ + + mg_peer = megaco_peer_profile_locate(mg_cfg->peer_list[peerIdx]); + + if(!mg_peer){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," No MG peer configuration found for peername[%s] against profilename[%s]\n",mg_cfg->peer_list[peerIdx],mg_cfg->name); + return SWITCH_STATUS_FALSE; + } + + len = len + sprintf(&prntBuf[0] + len,"\n",mg_peer->name); + + /* send request to MEGACO Trillium stack to get peer information*/ + sng_mgco_mg_get_status(STGCPENT, &cfm, mg_cfg, mg_peer); + + get_peer_xml_buffer(&prntBuf[0] + len, &cfm.t.ssta.s.mgPeerSta); + + len = len + sprintf(&prntBuf[0] + len,"\n"); + } + len = len + sprintf(&prntBuf[0] + len,"\n"); + + +/****************************************************************************************************************/ +/* Print MG SAP Information ***************************************************************************************/ + + len = len + sprintf(&prntBuf[0] + len,"\n"); + + /* MG SAP Information */ + sng_mgco_mg_get_status(STSSAP, &cfm, mg_cfg, mg_peer); + + len = len + sprintf(prntBuf+len, " %s \n", PRNT_SAP_STATE((int)(cfm.t.ssta.s.mgSSAPSta.state))); + len = len + sprintf(prntBuf+len, " %u \n", (unsigned int)(cfm.t.ssta.s.mgSSAPSta.numAssocPeer)); + len = len + sprintf(prntBuf+len, " %u \n", (unsigned int)(cfm.t.ssta.s.mgSSAPSta.numServers)); + len = len + sprintf(&prntBuf[0] + len,"\n"); + for (i = 0; i < cfm.t.ssta.s.mgSSAPSta.numAssocPeer; i++) + { + len = len + sprintf(&prntBuf[0] + len,"\n"); + if(cfm.t.ssta.s.mgSSAPSta.peerInfo[i].dname.namePres.pres == PRSNT_NODEF) + { + len = len + sprintf(prntBuf+len, " %s \n", (char *)(cfm.t.ssta.s.mgSSAPSta.peerInfo[i].dname.name)); + } + switch(cfm.t.ssta.s.mgSSAPSta.peerInfo[i].dname.netAddr.type) + { + case CM_NETADDR_IPV4: + { + ip = ntohl(cfm.t.ssta.s.mgSSAPSta.peerInfo[i].dname.netAddr.u.ipv4NetAddr); + cmInetNtoa(ip, &asciiAddr); + len = len + sprintf(prntBuf+len, "%s\n",asciiAddr); + break; + } + default: + len = len + sprintf(prntBuf+len, "invalid type \n"); + break; + } + +#ifdef GCP_MGCO + if (PRSNT_NODEF == cfm.t.ssta.s.mgSSAPSta.peerInfo[i].mid.pres) + { + len = len + sprintf(prntBuf+len, " %s \n", (char *)(cfm.t.ssta.s.mgSSAPSta.peerInfo[i].mid.val)); + } +#endif /* GCP_MGCO */ + len = len + sprintf(&prntBuf[0] + len,"\n"); + } + len = len + sprintf(&prntBuf[0] + len,"\n"); + + len = len + sprintf(&prntBuf[0] + len,"\n"); + +/****************************************************************************************************************/ +/* Print MG Transport SAP Information ***************************************************************************************/ + + len = len + sprintf(&prntBuf[0] + len,"\n"); + /* MG Transport SAP Information */ + sng_mgco_mg_get_status(STTSAP, &cfm, mg_cfg, mg_peer); + len = len + sprintf(&prntBuf[0] + len," %s \n", PRNT_SAP_STATE(cfm.t.ssta.s.mgTSAPSta.state)); + len = len + sprintf(&prntBuf[0] + len," %u \n", (unsigned int)(cfm.t.ssta.s.mgTSAPSta.numServers)); + len = len + sprintf(&prntBuf[0] + len,"\n"); + +/****************************************************************************************************************/ +/* Print MG Transport Server Information ***************************************************************************************/ + + if(sng_mgco_mg_get_status(STSERVER, &cfm, mg_cfg, mg_peer)){ + len = len + sprintf(&prntBuf[0] + len," no established server found \n"); + } + else { + len = len + sprintf(&prntBuf[0] + len,"\n"); + len = len + sprintf(&prntBuf[0] + len," %s \n", PRNT_SAP_STATE(cfm.t.ssta.s.mgTptSrvSta.state)); + len = len + sprintf(prntBuf+len, ""); + + switch (cfm.t.ssta.s.mgTptSrvSta.tptAddr.type) + { + case CM_TPTADDR_NOTPRSNT: + { + len = len + sprintf(prntBuf+len, "none"); + break; + } + case CM_TPTADDR_IPV4: + { + ip = ntohl(cfm.t.ssta.s.mgTptSrvSta.tptAddr.u.ipv4TptAddr.address); + cmInetNtoa(ip, &asciiAddr); + len = len + sprintf(prntBuf+len, "IPv4 IP address #%s, port %u",asciiAddr, + (unsigned int)(cfm.t.ssta.s.mgTptSrvSta.tptAddr.u.ipv4TptAddr.port)); + + break; + } + default: + len = len + sprintf(prntBuf+len, "unknown"); + break; + } + len = len + sprintf(prntBuf+len, "\n"); + len = len + sprintf(&prntBuf[0] + len,"\n"); + } + +/****************************************************************************************************************/ + len = len + sprintf(&prntBuf[0] + len,"\n"); + + stream->write_function(stream, "\n%s\n",&prntBuf[0]); + + return SWITCH_STATUS_SUCCESS; +} + +/****************************************************************************************************************/ +switch_status_t megaco_profile_status(switch_stream_handle_t *stream, megaco_profile_t* mg_cfg) +{ + int idx = 0x00; + int len = 0x00; + MgMngmt cfm; + char prntBuf[1024]; + mg_peer_profile_t* mg_peer = NULL; + + switch_assert(mg_cfg); + + memset((U8 *)&cfm, 0, sizeof(cfm)); + memset((char *)&prntBuf, 0, sizeof(prntBuf)); + + mg_peer = megaco_peer_profile_locate(mg_cfg->peer_list[0]); + + if(!mg_peer){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," No MG peer configuration found for peername[%s] against profilename[%s]\n",mg_cfg->peer_list[0],mg_cfg->name); + return SWITCH_STATUS_FALSE; + } + + idx = mg_cfg->idx; + + /*stream->write_function(stream, "Collecting MG Profile[%s] status... \n",profilename);*/ + + /* Fetch data from Trillium MEGACO Stack * + * SystemId - Software version information * + * SSAP - MG SAP Information * + * TSAP - MG Transport SAP Information * + * Peer - MG Peer Information * + * TPT-Server - MG Transport Server information * + */ + +#if 0 + /* get System ID */ + sng_mgco_mg_get_status(STSID, &cfm, idx); + stream->write_function(stream, "***********************************************\n"); + stream->write_function(stream, "**** TRILLIUM MEGACO Software Information *****\n"); + stream->write_function(stream, "Version = %d \n", cfm.t.ssta.s.systemId.mVer); + stream->write_function(stream, "Version Revision = %d \n", cfm.t.ssta.s.systemId.mRev); + stream->write_function(stream, "Branch Version = %d \n", cfm.t.ssta.s.systemId.bVer); + stream->write_function(stream, "Branch Revision = %d \n", cfm.t.ssta.s.systemId.bRev); + stream->write_function(stream, "Part Number = %d \n", cfm.t.ssta.s.systemId.ptNmb); + stream->write_function(stream, "***********************************************\n"); +#endif + + /* MG Peer Information */ + sng_mgco_mg_get_status(STGCPENT, &cfm, mg_cfg, mg_peer); + smmgPrntPeerSta(&cfm.t.ssta.s.mgPeerSta); + + /* MG Peer Information */ + sng_mgco_mg_get_status(STSSAP, &cfm, mg_cfg, mg_peer); + smmgPrntSsapSta(&cfm.t.ssta.s.mgSSAPSta); + + /* MG Transport SAP Information */ + sng_mgco_mg_get_status(STTSAP, &cfm, mg_cfg, mg_peer); + len = len + sprintf(prntBuf+len,"***********************************************\n"); + len = len + sprintf(prntBuf+len,"**********MG TRANSPORT SAP Information**********\n"); + len = len + sprintf(prntBuf+len,"TSAP status:\n"); + len = len + sprintf(prntBuf+len,"state = %d, number of listeners %u\n", + (int)(cfm.t.ssta.s.mgTSAPSta.state), + (unsigned int)(cfm.t.ssta.s.mgTSAPSta.numServers)); + len = len + sprintf(prntBuf+len,"***********************************************\n"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"%s\n",prntBuf); + + /* MG Transport Server Information */ + sng_mgco_mg_get_status(STSERVER, &cfm, mg_cfg, mg_peer); + smmgPrntSrvSta(&cfm.t.ssta.s.mgTptSrvSta); + + return SWITCH_STATUS_SUCCESS; +} + +/******************************************************************************/ +void get_peer_xml_buffer(char* prntBuf, MgPeerSta* cfm) +{ + int len = 0x00; + int i = 0x00; + char *asciiAddr; + CmInetIpAddr ip; + + if(PRSNT_NODEF == cfm->namePres.pres) + { + len = len + sprintf(prntBuf+len, " %s \n", (char *)(cfm->name)); + } + else + { + len = len + sprintf(prntBuf+len, " Not Present \n"); + } + + /* + * Print all IP addresses in the IP addr table + */ + for(i=0; ipeerAddrTbl.count; i++) + { + switch (cfm->peerAddrTbl.netAddr[i].type) + { + case CM_NETADDR_IPV4: + { + ip = ntohl(cfm->peerAddrTbl.netAddr[i].u.ipv4NetAddr); + cmInetNtoa(ip, &asciiAddr); + len = len + sprintf(prntBuf+len, "%s\n",asciiAddr); + break; + } + case CM_NETADDR_IPV6: + { + char ipv6_buf[128]; + int len1= 0; + int j = 0; + memset(&ipv6_buf[0], 0, sizeof(ipv6_buf)); + len1 = len1 + sprintf(ipv6_buf+len1, "IP V6 address : %2x", (unsigned int) + (cfm->peerAddrTbl.netAddr[i].u.ipv6NetAddr[0])); + + for (j = 1; j < CM_IPV6ADDR_SIZE; j++) + { + len1 = len1 + sprintf(ipv6_buf+len1, ":%2x", (unsigned int) + (cfm->peerAddrTbl.netAddr[i].u.ipv6NetAddr[j])); + } + len1 = len1 + sprintf(ipv6_buf+len1, "\n"); + len = len + sprintf(prntBuf+len, "%s\n", ipv6_buf); + break; + } + default: + { + len = len + sprintf(prntBuf+len, " Invalid address type[%d]\n", cfm->peerAddrTbl.netAddr[i].type); + break; + } + } + } /* End of for */ + + len = len + sprintf(prntBuf+len," %lu \n",(unsigned long)(cfm->numPendOgTxn)); + len = len + sprintf(prntBuf+len," %lu \n",(unsigned long)(cfm->numPendIcTxn)); + len = len + sprintf(prntBuf+len," %lu \n",(unsigned long)(cfm->rttEstimate)); + + switch(cfm->protocol) + { + case LMG_PROTOCOL_MGCP: + len = len + sprintf(prntBuf+len," MGCP \n"); + break; + + case LMG_PROTOCOL_MGCO: + len = len + sprintf(prntBuf+len," MEGACO \n"); + break; + + case LMG_PROTOCOL_NONE: + len = len + sprintf(prntBuf+len," MGCP/MEGACO \n"); + break; + + default: + len = len + sprintf(prntBuf+len," invalid \n"); + break; + } + + switch(cfm->transportType) + { + case LMG_TPT_UDP: + len = len + sprintf(prntBuf+len, "UDP\n"); + break; + + case LMG_TPT_TCP: + len = len + sprintf(prntBuf+len, "TCP\n"); + break; + + case LMG_TPT_NONE: + len = len + sprintf(prntBuf+len, "UDP/TCP\n"); + break; + + default: + len = len + sprintf(prntBuf+len, "invalid\n"); + break; + } +#ifdef GCP_MGCO + switch(cfm->encodingScheme) + { + case LMG_ENCODE_BIN: + len = len + sprintf(prntBuf+len, "BINARY\n"); + break; + + case LMG_ENCODE_TXT: + len = len + sprintf(prntBuf+len, "TEXT\n"); + break; + + case LMG_ENCODE_NONE: + len = len + sprintf(prntBuf+len, "TEXT/BINARY\n"); + break; + + default: + len = len + sprintf(prntBuf+len, "invalid\n"); + break; + } + + if(LMG_VER_PROF_MGCO_H248_1_0 == cfm->version){ + len = len + sprintf(prntBuf+len, "1.0 \n"); + } else if(LMG_VER_PROF_MGCO_H248_2_0 == cfm->version){ + len = len + sprintf(prntBuf+len, "2.0 \n"); + }else if(LMG_VER_PROF_MGCO_H248_3_0 == cfm->version){ + len = len + sprintf(prntBuf+len, "3.0 \n"); + } else{ + len = len + sprintf(prntBuf+len, "invalid \n"); + } +#endif + +} +/******************************************************************************/ +switch_status_t handle_all_term_status_cli_cmd(switch_stream_handle_t *stream, megaco_profile_t* mg_profile) +{ + void *val = NULL; + switch_hash_index_t *hi = NULL; + mg_termination_t *term = NULL; + const void *var; + + if(!mg_profile){ + stream->write_function(stream, "-ERR NULL profile\n"); + return SWITCH_STATUS_FALSE; + } + + stream->write_function(stream, " Termination Name"); + stream->write_function(stream, "\t Termination State"); + stream->write_function(stream, "\t Call State"); + stream->write_function(stream, "\t Termination Type"); + stream->write_function(stream, "\t Span-Id "); + stream->write_function(stream, "\t Channel-Id "); + + switch_thread_rwlock_rdlock(mg_profile->terminations_rwlock); + + for (hi = switch_hash_first(NULL, mg_profile->terminations); hi; hi = switch_hash_next(hi)) { + switch_hash_this(hi, &var, NULL, &val); + term = (mg_termination_t *) val; + if(!term) continue; + + stream->write_function(stream, "\n"); + + stream->write_function(stream, " %s",(NULL != term->name)?term->name:"NULL"); + if(MG_TERM_RTP == term->type){ + stream->write_function(stream, "\t\t\t IN-SERVICE"); + }else{ + stream->write_function(stream, "\t\t\t %s", + (switch_test_flag(term, MG_IN_SERVICE))?"IN-SERVICE":"OUT-OF-SERVICE"); + } + + stream->write_function(stream, "\t\t%s",(NULL != term->uuid)?"IN-CALL ":"IDLE "); + stream->write_function(stream, "\t\t %s",(MG_TERM_RTP == term->type)?"MG_TERM_RTP":"MG_TERM_TDM"); + + if(MG_TERM_TDM == term->type){ + stream->write_function(stream, "\t\t %s", + (NULL != term->u.tdm.span_name)?term->u.tdm.span_name:"NULL"); + stream->write_function(stream, "\t\t %d",term->u.tdm.channel); + }else{ + stream->write_function(stream, "\t\t -"); + stream->write_function(stream, "\t\t -"); + } + stream->write_function(stream, "\n"); + } + + switch_thread_rwlock_unlock(mg_profile->terminations_rwlock); + + return SWITCH_STATUS_SUCCESS; +} +/******************************************************************************/ + +switch_status_t handle_span_term_status_cli_cmd(switch_stream_handle_t *stream, megaco_profile_t* mg_profile, char* span_name) +{ + void *val = NULL; + switch_hash_index_t *hi = NULL; + mg_termination_t *term = NULL; + const void *var; + int found = 0x00; + int first = 0x01; + + if(!mg_profile || !span_name){ + stream->write_function(stream, "-ERR NULL profile or NULL span_name\n"); + return SWITCH_STATUS_FALSE; + } + + switch_thread_rwlock_rdlock(mg_profile->terminations_rwlock); + + for (hi = switch_hash_first(NULL, mg_profile->terminations); hi; hi = switch_hash_next(hi)) { + switch_hash_this(hi, &var, NULL, &val); + term = (mg_termination_t *) val; + if(!term) continue; + + if(MG_TERM_RTP == term->type) continue; + + if(!term->u.tdm.span_name) continue; + + if(strcasecmp(span_name,term->u.tdm.span_name)) continue; + + found = 0x01; + + if(first){ + stream->write_function(stream, " Termination Name"); + stream->write_function(stream, "\t Termination State"); + stream->write_function(stream, "\t Call State"); + stream->write_function(stream, "\t Termination Type"); + stream->write_function(stream, "\t Span-Id "); + stream->write_function(stream, "\t Channel-Id "); + first = 0x00; + } + + stream->write_function(stream, "\n"); + + stream->write_function(stream, " %s",(NULL != term->name)?term->name:"NULL"); + if(MG_TERM_RTP == term->type){ + stream->write_function(stream, "\t\t\t IN-SERVICE"); + }else{ + stream->write_function(stream, "\t\t\t %s", + (switch_test_flag(term, MG_IN_SERVICE))?"IN-SERVICE":"OUT-OF-SERVICE"); + } + + stream->write_function(stream, "\t\t%s",(NULL != term->uuid)?"IN-CALL ":"IDLE "); + stream->write_function(stream, "\t\t %s",(MG_TERM_RTP == term->type)?"MG_TERM_RTP":"MG_TERM_TDM"); + + if(MG_TERM_TDM == term->type){ + stream->write_function(stream, "\t\t %s", + (NULL != term->u.tdm.span_name)?term->u.tdm.span_name:"NULL"); + stream->write_function(stream, "\t\t %d",term->u.tdm.channel); + }else{ + stream->write_function(stream, "\t\t -"); + stream->write_function(stream, "\t\t -"); + } + stream->write_function(stream, "\n"); + } + + if(!found){ + stream->write_function(stream, "No span[%s] configured\n",span_name); + } + + switch_thread_rwlock_unlock(mg_profile->terminations_rwlock); + + return SWITCH_STATUS_SUCCESS; +} +/******************************************************************************/ + +switch_status_t handle_term_status_cli_cmd(switch_stream_handle_t *stream, megaco_profile_t* mg_profile, char* term_id) +{ + mg_termination_t* term = NULL; + + if(!mg_profile || !term_id){ + stream->write_function(stream, "-ERR NULL profile/term pointer \n"); + return SWITCH_STATUS_FALSE; + } + + term = megaco_find_termination(mg_profile, term_id); + + if(!term || !term->profile){ + stream->write_function(stream, "-ERR No such termination\n"); + return SWITCH_STATUS_FALSE; + } + + stream->write_function(stream, "Associated MG Profile Name [%s] \n",term->profile->name); + stream->write_function(stream, "MEGACO Termination Name[%s] \n",(NULL != term->name)?term->name:"NULL"); + stream->write_function(stream, "MEGACO Termination Type[%s] \n",(MG_TERM_RTP == term->type)?"MG_TERM_RTP":"MG_TERM_TDM"); + stream->write_function(stream, "Termination UUID[%s] \n",(NULL != term->uuid)?term->uuid:"Term Not Activated"); + if(term->context){ + stream->write_function(stream, "Associated Context-Id[%d] \n",term->context->context_id); + if(term->context->terminations[0] && term->context->terminations[1]){ + if(term == term->context->terminations[0]){ + stream->write_function(stream, "Associated Termination Name[%s] \n", + (NULL != term->context->terminations[1]->name)?term->context->terminations[1]->name:"NULL"); + }else { + stream->write_function(stream, "Associated Termination Name[%s] \n", + (NULL != term->context->terminations[0]->name)?term->context->terminations[0]->name:"NULL"); + } + } + } + + + if(MG_TERM_RTP == term->type){ + stream->write_function(stream, "RTP Termination ID [%d] \n",term->u.rtp.term_id); + stream->write_function(stream, "RTP MEDIA Type [%s] \n", + ( MGM_IMAGE == term->u.rtp.media_type)?"MGM_IMAGE":"MGM_AUDIO"); + stream->write_function(stream, "RTP Termination Local Address[%s] \n", + (NULL != term->u.rtp.local_addr)?term->u.rtp.local_addr:"NULL"); + stream->write_function(stream, "RTP Termination Local Port[%d] \n",term->u.rtp.local_port); + stream->write_function(stream, "RTP Termination Remote Address[%s] \n", + (NULL != term->u.rtp.remote_addr)?term->u.rtp.remote_addr:"NULL"); + stream->write_function(stream, "RTP Termination Remote Port[%d] \n",term->u.rtp.remote_port); + stream->write_function(stream, "RTP Termination PTIME [%d] \n",term->u.rtp.ptime); + stream->write_function(stream, "RTP Termination PT [%d] \n",term->u.rtp.pt); + stream->write_function(stream, "RTP Termination rfc2833_pt [%d] \n",term->u.rtp.rfc2833_pt); + stream->write_function(stream, "RTP Termination Sampling Rate [%d] \n",term->u.rtp.rate); + stream->write_function(stream, "RTP Termination Codec [%s] \n", + (NULL != term->u.rtp.codec)?term->u.rtp.codec:"NULL"); + }else{ + stream->write_function(stream, "TDM Termination Service-State [%s] \n", + (switch_test_flag(term, MG_IN_SERVICE))?"IN-SERVICE":"OUT-OF-SERVICE"); + stream->write_function(stream, "TDM Termination channel [%d] \n",term->u.tdm.channel); + stream->write_function(stream, "TDM Termination span name [%s] \n", + (NULL != term->u.tdm.span_name)?term->u.tdm.span_name:"NULL"); + } + + return SWITCH_STATUS_SUCCESS; +} +/******************************************************************************/ +switch_status_t handle_show_activecalls_cli_cmd(switch_stream_handle_t *stream, megaco_profile_t* mg_profile) +{ + void *val = NULL; + switch_hash_index_t *hi = NULL; + mg_termination_t *term = NULL; + const void *var; + int found = 0x00; + + if(!mg_profile || !mg_profile->terminations){ + stream->write_function(stream, "-ERR NULL profile/term pointer \n"); + return SWITCH_STATUS_FALSE; + } + + stream->write_function(stream, "\n ------- Active Calls Terminations ------- \n"); + + switch_thread_rwlock_rdlock(mg_profile->terminations_rwlock); + + for (hi = switch_hash_first(NULL, mg_profile->terminations); hi; hi = switch_hash_next(hi)) { + switch_hash_this(hi, &var, NULL, &val); + term = (mg_termination_t *) val; + if(!term) continue; + if(NULL == term->uuid) continue; + + found = 0x01; + stream->write_function(stream, "\n ********************************* \n"); + stream->write_function(stream, "MEGACO Termination Name[%s] \n",(NULL != term->name)?term->name:"NULL"); + stream->write_function(stream, "MEGACO Termination Type[%s] \n",(MG_TERM_RTP == term->type)?"MG_TERM_RTP":"MG_TERM_TDM"); + stream->write_function(stream, "Termination UUID[%s] \n",(NULL != term->uuid)?term->uuid:"Term Not Activated"); + if(MG_TERM_RTP == term->type){ + stream->write_function(stream, "RTP Termination ID [%d] \n",term->u.rtp.term_id); + }else{ + stream->write_function(stream, "TDM Termination channel [%d] \n",term->u.tdm.channel); + stream->write_function(stream, "TDM Termination span name [%s] \n", + (NULL != term->u.tdm.span_name)?term->u.tdm.span_name:"NULL"); + } + stream->write_function(stream, "\n ********************************* \n"); + } + + switch_thread_rwlock_unlock(mg_profile->terminations_rwlock); + + + if(!found) + stream->write_function(stream, "\n ------- NO Active Calls FOUND ------- \n"); + + + return SWITCH_STATUS_SUCCESS; +} + +/******************************************************************************/ +switch_status_t handle_show_stats(switch_stream_handle_t *stream, megaco_profile_t* mg_profile) +{ + if(!mg_profile || !mg_profile->mg_stats){ + stream->write_function(stream, "-ERR NULL profile/term pointer \n"); + return SWITCH_STATUS_FALSE; + } + + stream->write_function(stream, "Total Number of Physical ADD received = %d \n", mg_profile->mg_stats->total_num_of_phy_add_recvd); + stream->write_function(stream, "Total Number of RTP ADD received = %d \n", mg_profile->mg_stats->total_num_of_rtp_add_recvd); + stream->write_function(stream, "Total Number of SUB received = %d \n", mg_profile->mg_stats->total_num_of_sub_recvd); + stream->write_function(stream, "Total Number of CALL received = %d \n", mg_profile->mg_stats->total_num_of_call_recvd); + stream->write_function(stream, "Total Number of T38 CALL received = %d \n", mg_profile->mg_stats->total_num_of_t38_call_recvd); + stream->write_function(stream, "Total Number of IN-Service Service change sent = %d \n", + mg_profile->mg_stats->total_num_of_term_in_service_change_sent); + stream->write_function(stream, "Total Number of Out-Of-Service Service change sent = %d \n", + mg_profile->mg_stats->total_num_of_term_oos_service_change_sent); + stream->write_function(stream, "Total Number of ADD failed = %d \n", mg_profile->mg_stats->total_num_of_add_failed); + stream->write_function(stream, "Total Number of Term Already in context Error = %d \n", + mg_profile->mg_stats->total_num_of_term_already_in_ctxt_error); + stream->write_function(stream, "Total Number of choose context failed Error = %d \n", + mg_profile->mg_stats->total_num_of_choose_ctxt_failed_error); + stream->write_function(stream, "Total Number of choose term failed Error = %d \n", + mg_profile->mg_stats->total_num_of_choose_term_failed_error); + stream->write_function(stream, "Total Number of find term failed Error = %d \n", + mg_profile->mg_stats->total_num_of_find_term_failed_error); + stream->write_function(stream, "Total Number of get context failed Error = %d \n", + mg_profile->mg_stats->total_num_of_get_ctxt_failed_error); + stream->write_function(stream, "Total Number of un-supported codec error = %d \n", + mg_profile->mg_stats->total_num_of_un_supported_codec_error); + stream->write_function(stream, "Total Number of Term addition to context failed error = %d \n", + mg_profile->mg_stats->total_num_of_add_term_failed_error); + stream->write_function(stream, "Total Number of Term activation failed error = %d \n", + mg_profile->mg_stats->total_num_of_term_activation_failed_error); + stream->write_function(stream, "Total Number of Term not found in context error = %d \n", + mg_profile->mg_stats->total_num_of_no_term_ctxt_error); + stream->write_function(stream, "Total Number of Term not in service error = %d \n", + mg_profile->mg_stats->total_num_of_term_not_in_service_error); + stream->write_function(stream, "Total Number of unknown context error = %d \n", + mg_profile->mg_stats->total_num_of_unknown_ctxt_error); + + + + return SWITCH_STATUS_SUCCESS; +} +/******************************************************************************/ +switch_status_t handle_show_stack_mem(switch_stream_handle_t *stream) +{ + U32 availMem = 0; + char buffer[4098]; + + memset(buffer,0,sizeof(buffer)); + + SGetMemInfoBuffer(S_REG, &availMem, buffer); + + stream->write_function(stream, "%s",buffer); + + return SWITCH_STATUS_SUCCESS; +} +/******************************************************************************/ + diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway_cmd_handler.c b/src/mod/endpoints/mod_media_gateway/media_gateway_cmd_handler.c new file mode 100644 index 0000000000..d7c03915fd --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/media_gateway_cmd_handler.c @@ -0,0 +1,2504 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Mathieu Rene +* All rights reserved. +* +* +*/ + +#include "mod_media_gateway.h" +#include "media_gateway_stack.h" + +U32 outgoing_txn_id; + +/*****************************************************************************************************************************/ +const char *mg_service_change_reason[] = { + "\"NOT USED\"", + "\"900 ServiceRestored\"", + "\"905 Termination taken out of service\"", + 0 +}; + + +/*****************************************************************************************************************************/ +void mg_restart_inactivity_timer(megaco_profile_t* profile) +{ + /* NOTE - For Restart - we are deleting existing task and adding it again */ + if(profile->inact_tmr_task_id) + switch_scheduler_del_task_id(profile->inact_tmr_task_id); + + if(profile->inact_tmr) { + mg_activate_ito_timer(profile); + } +} + +/*****************************************************************************************************************************/ +static void mg_inactivity_timer_exp(switch_scheduler_task_t *task) +{ + megaco_profile_t* profile = (megaco_profile_t*) task->cmd_arg; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," mg_inactivity_timer_exp for profile[%s]\n", profile->name); + mg_print_time(); + + mg_send_ito_notify(profile); + + /* resetting task_id */ + profile->inact_tmr_task_id = 0x00; +} + +/*****************************************************************************************************************************/ +switch_status_t mg_activate_ito_timer(megaco_profile_t* profile) +{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Starting IT/ITO Timer \n"); + mg_print_time(); + + profile->inact_tmr_task_id = switch_scheduler_add_task(switch_epoch_time_now(NULL)+profile->inact_tmr, mg_inactivity_timer_exp,"","media_gateway",0,profile,0); + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************************/ +switch_status_t mg_is_ito_pkg_req(megaco_profile_t* mg_profile, MgMgcoCommand *cmd) +{ + int descId = 0x00; + MgMgcoAmmReq* desc = NULL; + + if(CH_CMD_TYPE_IND != cmd->cmdType.val) + return SWITCH_STATUS_FALSE; + + if(MGT_MODIFY != cmd->u.mgCmdInd[0]->cmd.type.val) + return SWITCH_STATUS_FALSE; + + desc = &cmd->u.mgCmdInd[0]->cmd.u.mod; + + if(NULL == desc){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"No Valid descriptor found \n"); + return SWITCH_STATUS_FALSE; + } + + if(NOTPRSNT == desc->dl.num.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"No descriptor found in-coming megaco request \n"); + return SWITCH_STATUS_SUCCESS; + } + + + for (descId = 0; descId < desc->dl.num.val; descId++) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"descriptors[%d] type in-coming megaco request \n", desc->dl.descs[descId]->type.val); + switch (desc->dl.descs[descId]->type.val) { + case MGT_MEDIADESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," Media descriptor on ROOT termination..Not Supporting now\n"); + break; + } + + case MGT_REQEVTDESC: + { + MgMgcoReqEvtDesc* evts = &desc->dl.descs[descId]->u.evts; + MgMgcoEvtPar *reqEvtPar; + MgMgcoReqEvt *evt; + int numEvts = 0; + int i; + + /* As of now only handling ito package */ + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Requested Event descriptor\n"); + + if (evts->el.num.pres) + numEvts = evts->el.num.val; + + for (i = 0; i < numEvts; i++) + { + evt = evts->el.revts[i]; + if (evt->pl.num.pres) + { + /* Check for the package */ + if((MGT_PKG_KNOWN == evt->pkg.valType.val) && + (MGT_PKG_INACTTIMER != evt->pkg.u.val.val)) + { + continue; + } + else + { + if((MGT_GEN_TYPE_KNOWN == evt->name.type.val) && + (MGT_PKG_ENUM_REQEVT_INACTTIMER_INACT_TIMOUT == + evt->name.u.val.val)){ + + if((evt->pl.num.pres != NOTPRSNT) && + (evt->pl.num.val != 0)) { + + reqEvtPar = evt->pl.parms[0]; + + if((NULL != reqEvtPar) && + (reqEvtPar->type.val == MGT_EVTPAR_OTHER) && + (reqEvtPar->u.other.name.type.pres == PRSNT_NODEF) && + (reqEvtPar->u.other.name.type.val == MGT_GEN_TYPE_KNOWN) && + (reqEvtPar->u.other.name.u.val.pres == PRSNT_NODEF) && + (reqEvtPar->u.other.name.u.val.val == + MGT_PKG_ENUM_REQEVTOTHER_INACTTIMER_INACT_TIMOUT_MAX_IATIME)&& + (reqEvtPar->u.other.val.type.pres == PRSNT_NODEF) && + (reqEvtPar->u.other.val.type.val == MGT_VALUE_EQUAL) && + (reqEvtPar->u.other.val.u.eq.type.pres == PRSNT_NODEF) && + (reqEvtPar->u.other.val.u.eq.type.val == MGT_VALTYPE_UINT32)) + { +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Received Inactivity timer value [%d]\n", + reqEvtPar->u.other.val.u.eq.u.decInt.val); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Received Inactivity timer value [%ld]\n", + reqEvtPar->u.other.val.u.eq.u.decInt.val); +#endif + + mg_profile->inact_tmr = reqEvtPar->u.other.val.u.eq.u.decInt.val/MG_INACTIVITY_TMR_RESOLUTION; + + if(0 == mg_profile->inact_tmr){ + /* value ZERO means MGC wantes to disable ito timer */ + switch_scheduler_del_task_id(mg_profile->inact_tmr_task_id) ; + } else { + mg_activate_ito_timer(mg_profile); + } + break; + } + } + } + } + } + } + + break; + } + case MGT_SIGNALSDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," Signal descriptor on ROOT termination..Not Supporting now\n"); + break; + } + case MGT_MODEMDESC: + case MGT_MUXDESC: + case MGT_EVBUFDESC: + case MGT_DIGMAPDESC: + case MGT_AUDITDESC: + case MGT_STATSDESC: + break; + + } + } + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************************/ +switch_status_t mg_prc_sig_desc(MgMgcoSignalsReq* req, megaco_profile_t* mg_profile, mg_termination_t* term) +{ + MgMgcoSigPar *p = NULL; + MgMgcoSigOther* o = NULL; + int sig = 0x00; + + switch_assert(req); + switch_assert(mg_profile); + switch_assert(term); + + + /* As of now only checking T.38 CED tone */ + + /* Modify message will have following signal descriptor * + * Signals{ctyp/ANS{anstype=ans}} * + */ + + /* check for T.38 CED tone package i.e. MGT_PKG_CALL_TYP_DISCR */ + if((MGT_PKG_KNOWN == req->pkg.valType.val) && + (NOTPRSNT != req->pkg.u.val.pres) && + (MGT_PKG_CALL_TYP_DISCR == req->pkg.u.val.val) && + (NOTPRSNT != req->name.type.pres) && + (MGT_GEN_TYPE_KNOWN == req->name.type.val)) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Received Signal Descriptor : T.38 ctyp package\n"); + + if((NOTPRSNT != req->pl.num.pres) && (NOTPRSNT != req->pl.num.val)){ + for(sig = 0x00; sig < req->pl.num.val; sig++){ + p = req->pl.parms[sig]; + + if(NOTPRSNT == p->type.pres) continue; + + if((MGT_SIGPAR_OTHER == p->type.val) && + (NULL != (o = &p->u.other)) && + (NOTPRSNT != o->name.type.pres) && + (MGT_GEN_TYPE_KNOWN == o->name.type.val) && + (MGT_PKG_ENUM_SIGOTHERCALLTYPDISCRANSSIGANSTYP == o->name.u.val.val) && + (MGT_VALUE_EQUAL == o->val.type.val) && + (NOTPRSNT != o->val.u.eq.type.pres) && + (MGT_VALTYPE_ENUM == o->val.u.eq.type.val) && + (MGT_PKG_ENUM_SIGOTHERCALLTYPDISCRANSSIGANSTYPANS == o->val.u.eq.u.enume.val)){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Signal Descriptor : T.38 CED/ANS TONE \n"); + /* apply CED/ANS tone to specify channel */ + } + } + } + } + + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************************/ + +/* +* +* Fun: mg_prc_descriptors +* +* Desc: this api will process the descriptors received from MG stack +* +* +*/ +switch_status_t mg_prc_descriptors(megaco_profile_t* mg_profile, MgMgcoCommand *cmd, mg_termination_t* term, CmMemListCp *memCp) +{ + CmSdpMedProtoFmts *format; + TknU8 *fmt; + CmSdpMedFmtRtpList *fmt_list; + MgMgcoTermStateDesc *tstate; + int fmtCnt; + int i; + int descId = 0x00; + int j; + MgMgcoLocalParm *lclParm; + CmSdpInfo *sdp; + MgMgcoLclCtlDesc *locCtl; + MgMgcoTermStateParm *tsp; + MgMgcoAmmReq* desc = NULL; + MgMgcoLocalDesc *local; + MgMgcoRemoteDesc* remote; + + switch (cmd->cmdType.val) + { + case CH_CMD_TYPE_IND: + switch(cmd->u.mgCmdInd[0]->cmd.type.val) + { + case MGT_ADD: + { + desc = &cmd->u.mgCmdInd[0]->cmd.u.add; + break; + } + case MGT_MOVE: + { + desc = &cmd->u.mgCmdInd[0]->cmd.u.move; + break; + } + case MGT_MODIFY: + { + desc = &cmd->u.mgCmdInd[0]->cmd.u.mod; + break; + } + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Invalid cmd.type[%d] for descriptor processing \n", + cmd->u.mgCmdInd[0]->cmd.type.val); + return SWITCH_STATUS_FALSE; + } + break; + default: + { + return SWITCH_STATUS_FALSE; + } + } + + if(NULL == desc){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"No Valid descriptor found \n"); + return SWITCH_STATUS_FALSE; + } + + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"descriptors[%d] found in-coming megaco request \n", desc->dl.num.val); + + if(NOTPRSNT == desc->dl.num.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"No descriptor found in-coming megaco request \n"); + return SWITCH_STATUS_SUCCESS; + } + + + for (descId = 0; descId < desc->dl.num.val; descId++) { + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"descriptors[%d] type in-coming megaco request \n", desc->dl.descs[descId]->type.val); + switch (desc->dl.descs[descId]->type.val) { + case MGT_MEDIADESC: + { + int mediaId; + for (mediaId = 0; mediaId < desc->dl.descs[descId]->u.media.num.val; mediaId++) { + MgMgcoMediaPar *mediaPar = desc->dl.descs[descId]->u.media.parms[mediaId]; + switch (mediaPar->type.val) { + case MGT_MEDIAPAR_LOCAL: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "MGT_MEDIAPAR_LOCAL\n"); + /* Matt - check local descriptor processing */ + local = &mediaPar->u.local; + sdp = local->sdp.info[0]; + for (i = 0; i < sdp->mediaDescSet.numComp.val; i++) { + /* sdp formats */ + for (j = 0; j < + sdp->mediaDescSet.mediaDesc[i]->field.par.numProtFmts.val; j++) + { + format = sdp->mediaDescSet.mediaDesc[i]->field.par.pflst[j]; + /* Matt - format has field for T38 also */ + if ((format->protType.pres != NOTPRSNT) && + (format->protType.val == CM_SDP_MEDIA_PROTO_RTP)) { + + /* protocol type RTP */ + fmt_list = &format->u.rtp; + + /* print format */ + for(fmtCnt = 0; fmtCnt < fmt_list->num.val; fmtCnt++){ + fmt = &fmt_list->fmts[i]->val; + if(fmt->pres == NOTPRSNT) continue; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,"Format [%d]\n", fmt->val); + } + } + } + } + + mgco_handle_incoming_sdp(&local->sdp, term, MG_SDP_LOCAL, mg_profile, memCp); + + break; + } + + case MGT_MEDIAPAR_REMOTE: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "MGT_MEDIAPAR_REMOTE\n"); + /* Matt - check remote descriptor processing */ + remote = &mediaPar->u.remote; + sdp = remote->sdp.info[0]; + /* for Matt - same like local descriptor */ + mgco_handle_incoming_sdp(&remote->sdp, term, MG_SDP_REMOTE, mg_profile, memCp); + break; + } + + case MGT_MEDIAPAR_LOCCTL: + { + /* Matt - check Local Control descriptor processing */ + locCtl = &mediaPar->u.locCtl; + for (i = 0; i < locCtl->num.val; i++){ + lclParm = locCtl->parms[i]; + if (PRSNT_NODEF == lclParm->type.pres){ + switch(lclParm->type.val) + { + case MGT_LCLCTL_MODE: + { + /* Mode Property */ + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,"MGT_LCLCTL_MODE - Mode value [%d]\n", lclParm->u.mode.val); + break; + } + case MGT_LCLCTL_RESVAL: + { + /* Reserve Value */ + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,"MGT_LCLCTL_RESVAL: Reserve Value[%d] \n", lclParm->u.resVal.val); + break; + } + case MGT_LCLCTL_RESGRP: + { + /* Reserve group */ + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,"MGT_LCLCTL_RESGRP: Reserve Group[%d]\n", lclParm->u.resGrp.val); + break; + } + case MGT_LCLCTL_PROPPARM: + { + MgMgcoPropParm* p = &lclParm->u.propParm; + /* Properties (of a termination) */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,"MGT_LCLCTL_PROPPARM: \n"); + + if((MGT_PKG_KNOWN == p->pkg.valType.val) && + (MGT_PKG_TDM_CKT == p->pkg.u.val.val) && + (MGT_GEN_TYPE_KNOWN == p->name.type.val) && + (MGT_PKG_TDMC_PROP_EC == p->name.u.val.val) && + (PRSNT_NODEF == p->val.type.pres) && + (MGT_VALUE_EQUAL == p->val.type.val) && + (NOTPRSNT != p->val.u.eq.type.pres) && + (MGT_VALTYPE_ENUM == p->val.u.eq.type.val)) { + if(MGT_PKG_ENUM_PROPPARMTDMCEC_ON == p->val.u.eq.u.enume.val){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,"TDMC EC=ON\n"); + mg_term_set_ec(term,0x01); + }else if(MGT_PKG_ENUM_PROPPARMTDMCEC_OFF == p->val.u.eq.u.enume.val){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,"TDMC EC=OFF\n"); + mg_term_set_ec(term,0x00); + } + } + break; + } + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,"Invalid local control descriptor type[%d]\n",lclParm->type.val); + break; + } + } + } + + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "MGT_MEDIAPAR_LOCCTL\n"); + break; + } + case MGT_MEDIAPAR_TERMST: + { + /* Matt - apply termination state descriptor */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "MGT_MEDIAPAR_TERMST"); + tstate = &mediaPar->u.tstate; + for (i = 0; i < tstate->numComp.val; i++) + { + /* Matt to see how to apply below descriptors to a termination */ + tsp = tstate->trmStPar[i]; + if (PRSNT_NODEF == tsp->type.pres) { + switch(tsp->type.val) + { + case MGT_TERMST_PROPLST: + { + /* Matt to see how to apply properties to a termination */ + /* Properties of a termination */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,"MGT_TERMST_PROPLST:\n"); + break; + } + case MGT_TERMST_EVTBUFCTL: + { + /* Event /buffer Control Properties */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE," MGT_TERMST_EVTBUFCTL: value[%d]\n", tsp->u.evtBufCtl.val); + break; + } + case MGT_TERMST_SVCST: + { + /* Service State Properties */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE," MGT_TERMST_SVCST: value[%d]\n", tsp->u.svcState.val); + break; + } + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,"Invalid termination state descriptor type[%d]\n",tsp->type.val); + break; + } + } + } + break; + } + case MGT_MEDIAPAR_STRPAR: + { + MgMgcoStreamDesc *mgStream = &mediaPar->u.stream; + + if (mgStream->sl.remote.pres.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Got remote stream media description:\n"); + mgco_handle_incoming_sdp(&mgStream->sl.remote.sdp, term, MG_SDP_LOCAL, mg_profile, memCp); + } + + if (mgStream->sl.local.pres.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Got local stream media description:\n"); + mgco_handle_incoming_sdp(&mgStream->sl.local.sdp, term, MG_SDP_REMOTE, mg_profile, memCp); + } + + break; + } + } + } + break; + } + case MGT_REQEVTDESC: + { + MgMgcoReqEvtDesc* evt = &desc->dl.descs[descId]->u.evts; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG," Requested Event descriptor\n"); + + /* If we receive events from MGC , means clear any ongoing events */ + /* as such we dont apply any events to term, so for us (as of now) clear events means clear active_events structure*/ + + if(NULL != term->active_events){ + mgUtlDelMgMgcoReqEvtDesc(term->active_events); + MG_STACK_MEM_FREE(term->active_events, sizeof(MgMgcoReqEvtDesc)); + } + + MG_STACK_MEM_ALLOC(&term->active_events, sizeof(MgMgcoReqEvtDesc)); + + if(NULL == term->active_events){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," term->active_events Memory Alloc failed \n"); + return SWITCH_STATUS_FALSE; + } + + /* copy requested event */ + if(RFAILED == mgUtlCpyMgMgcoReqEvtDesc(term->active_events, evt, NULLP)){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," copy new events to term->active_events failed \n"); + MG_STACK_MEM_FREE(term->active_events, sizeof(MgMgcoReqEvtDesc)); + return SWITCH_STATUS_FALSE; + } + + /* print Requested event descriptor */ + /*mgAccEvntPrntMgMgcoReqEvtDesc(term->active_events, stdout);*/ + + break; + } + case MGT_SIGNALSDESC: + { + MgMgcoSignalsDesc* sig = &desc->dl.descs[descId]->u.sig; + MgMgcoSignalsParm* param = NULL; + int i = 0x00; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG," Requested Signal descriptor\n"); + + if((NOTPRSNT != sig->pres.pres) && (NOTPRSNT != sig->num.pres) && (0 != sig->num.val)){ + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Total number of Signal descriptors[%d]\n", sig->num.val); + + for(i=0; i< sig->num.val; i++){ + param = sig->parms[i]; + + if(NOTPRSNT == param->type.pres) continue; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Signal Descriptor[%d] type[%s]\n", + i, ((MGT_SIGSPAR_LST == param->type.val)?"MGT_SIGSPAR_LST":"MGT_SIGSPAR_REQ")); + + switch(param->type.val) + { + case MGT_SIGSPAR_LST: + { + MgMgcoSignalsLst* lst = ¶m->u.lst; + int sigId = 0x00; + + if((NOTPRSNT == lst->pl.num.pres) || (0 == lst->pl.num.val)) break; + + for(sigId = 0; sigId < lst->pl.num.val; sigId++){ + mg_prc_sig_desc(lst->pl.reqs[sigId], mg_profile, term); + } + + break; + } + case MGT_SIGSPAR_REQ: + { + MgMgcoSignalsReq* req = ¶m->u.req; + mg_prc_sig_desc(req, mg_profile, term); + break; + } + default: + break; + } + } + } + + break; + } + case MGT_MODEMDESC: + case MGT_MUXDESC: + case MGT_EVBUFDESC: + case MGT_DIGMAPDESC: + case MGT_AUDITDESC: + case MGT_STATSDESC: + break; + + } + } + + return SWITCH_STATUS_SUCCESS; +} + + +/*****************************************************************************************************************************/ + +/* +* +* Fun: handle_mg_add_cmd +* +* Desc: this api will handle the ADD request received from MG stack +* +* +*/ +switch_status_t handle_mg_add_cmd(megaco_profile_t* mg_profile, MgMgcoCommand *inc_cmd, MgMgcoContextId* new_ctxtId) +{ + switch_status_t ret; + MgMgcoContextId *ctxtId; + MgStr errTxt; + MgMgcoInd *mgErr; + MgMgcoTermId *termId; + MgMgcoTermIdLst* termLst; + int err_code; + int is_rtp = 0x00; + MgMgcoAmmReq *cmd = &inc_cmd->u.mgCmdInd[0]->cmd.u.add; + U32 txn_id = inc_cmd->transId.val; + mg_termination_t* term = NULL; + MgMgcoMediaDesc* inc_med_desc; + /*MgMgcoStreamDesc* inc_strm_desc;*/ + MgMgcoAudRetParm *desc; + mg_context_t* mg_ctxt; + int mediaId; + MgMgcoLocalDesc *local = NULL; + char term_name[128]; + /*CmSdpInfoSet *psdp = NULL;*/ + + memset(&term_name,0,sizeof(term_name)); + + inc_med_desc = &cmd->dl.descs[0]->u.media; + + /********************************************************************/ + ctxtId = &inc_cmd->contextId; + termLst = mg_get_term_id_list(inc_cmd); + termId = termLst->terms[0]; + MG_MEM_COPY(&term_name, termId->name.lcl.val, sizeof(U8) * termId->name.lcl.len); + + + /********************************************************************/ + /* Validating ADD request *******************************************/ + + /*-- NULL Context & ALL Context not applicable for ADD request --*/ + if ((NOTPRSNT != ctxtId->type.pres) && + ((MGT_CXTID_ALL == ctxtId->type.val) || + (MGT_CXTID_NULL == ctxtId->type.val))) { + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," ADD Request processing failed, Context ALL/NULL not allowed\n"); + + mg_util_set_ctxt_string(&errTxt, ctxtId); + err_code = MGT_MGCO_RSP_CODE_PROT_ERROR; + goto error; + } + + /********************************************************************/ + /* Allocate context - if context type is CHOOSE */ + if ((NOTPRSNT != ctxtId->type.pres) && + (MGT_CXTID_CHOOSE == ctxtId->type.val)){ + + mg_ctxt = megaco_choose_context(mg_profile); + + if(NULL == mg_ctxt){ + /* temp fix - Calling again, just in case if this time we get context */ + mg_ctxt = megaco_choose_context(mg_profile); + if(NULL == mg_ctxt){ + mg_profile->mg_stats->total_num_of_choose_ctxt_failed_error++; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," megaco_choose_context failed \n"); + mg_util_set_err_string(&errTxt, " Resource Failure "); + err_code = MGT_MGCO_RSP_CODE_RSRC_ERROR; + goto error; + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," megaco_choose_context - Success in 2nd Attempt \n"); + } + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Allocated Context[%p] with context_id[%d]\n", (void*)mg_ctxt, mg_ctxt->context_id); + + /* fill Trillium Context structure with allocated context */ + MG_SET_VAL_PRES(new_ctxtId->type, MGT_CXTID_OTHER); + MG_SET_VAL_PRES(new_ctxtId->val, mg_ctxt->context_id); + } + else { + /* context already present */ + memcpy(new_ctxtId, &inc_cmd->contextId,sizeof(MgMgcoContextId)); + mg_ctxt = megaco_get_context(mg_profile, inc_cmd->contextId.val.val); + if(NULL == mg_ctxt){ +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + " megaco_get_context failed for context-id[%d]\n", inc_cmd->contextId.val.val); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + " megaco_get_context failed for context-id[%ld]\n", inc_cmd->contextId.val.val); +#endif + mg_profile->mg_stats->total_num_of_get_ctxt_failed_error++; + mg_util_set_err_string(&errTxt, " Resource Failure "); + err_code = MGT_MGCO_RSP_CODE_RSRC_ERROR; + goto error; + } + } + + /********************************************************************/ + /* Allocate new RTP termination - If term type is CHOOSE */ + if ((NOTPRSNT != termId->type.pres) && + (MGT_TERMID_CHOOSE == termId->type.val)){ + + term = megaco_choose_termination(mg_profile, mg_profile->rtp_termination_id_prefix); + mg_profile->mg_stats->total_num_of_rtp_add_recvd++; + + if(NULL == term){ + mg_profile->mg_stats->total_num_of_choose_term_failed_error++; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," megaco_choose_termination failed \n"); + mg_util_set_err_string(&errTxt, " Resource Failure "); + err_code = MGT_MGCO_RSP_CODE_RSRC_ERROR; + goto error; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Allocated RTP Termination[%p] with term name[%s]\n", (void*)term, term->name); + + is_rtp = 0x01; + + /********************************************************************/ + }else{ /* Physical termination */ + term = megaco_find_termination(mg_profile, term_name); + mg_profile->mg_stats->total_num_of_phy_add_recvd++; + + if(NULL == term){ + mg_profile->mg_stats->total_num_of_find_term_failed_error++; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + " megaco_find_termination failed for term-id[%s] \n", term_name); + mg_util_set_err_string(&errTxt, " Resource Failure "); + err_code = MGT_MGCO_RSP_CODE_RSRC_ERROR; + goto error; + } + + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Allocated Physical Termination[%p] with term name[%s]\n", (void*)term, term->name); + } + + /********************************************************************/ + /* check if termination already is in call */ + + if(term->context){ + mg_profile->mg_stats->total_num_of_term_already_in_ctxt_error++; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," Termination[%p : %s] " + "already in context[%p -%d]..rejecting ADD \n", + (void*)term, term->name, (void*)term->context,term->context->context_id); + mg_util_set_err_string(&errTxt, " Term already is in call "); + err_code = MGT_MGCO_RSP_CODE_DUP_TERM_CTXT; + goto error; + } + +/********************************************************************/ + + ret = mg_prc_descriptors(mg_profile, inc_cmd, term, &inc_cmd->u.mgCmdInd[0]->memCp); + + /* IF there is any error , return */ + if(term->mg_error_code && (*term->mg_error_code == MGT_MGCP_RSP_CODE_INCONSISTENT_LCL_OPT)){ + mg_profile->mg_stats->total_num_of_un_supported_codec_error++; + mg_util_set_err_string(&errTxt, " Unsupported Codec "); + err_code = MGT_MGCP_RSP_CODE_INCONSISTENT_LCL_OPT; + goto error; + } + /********************************************************************/ + /* associate physical termination to context */ + + if(SWITCH_STATUS_FALSE == megaco_context_add_termination(mg_ctxt, term)){ + mg_profile->mg_stats->total_num_of_add_term_failed_error++; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"megaco_context_add_termination failed \n"); + mg_util_set_err_string(&errTxt, " Resource Failure "); + err_code = MGT_MGCO_RSP_CODE_RSRC_ERROR; + goto error; + } + + mg_apply_tdm_dtmf_removal(term, mg_ctxt); + + + mg_apply_tdm_ec(term, mg_ctxt); + + mg_print_t38_attributes(term); + + + /********************************************************************/ + + /* resp code -- begin */ + { + MgMgcoCommand rsp; + int ret = 0x00; + MgMgcoTermId *out_termId; + + memset(&rsp,0, sizeof(rsp)); + + /*copy transaction-id*/ + memcpy(&rsp.transId, &inc_cmd->transId,sizeof(MgMgcoTransId)); + + /*copy context-id*/ + memcpy(&rsp.contextId, new_ctxtId,sizeof(MgMgcoContextId)); + + /*copy peer identifier */ + memcpy(&rsp.peerId, &inc_cmd->peerId,sizeof(TknU32)); + + /*fill response structue */ + if(SWITCH_STATUS_FALSE == (ret = mg_stack_alloc_mem((Ptr*)&rsp.u.mgCmdRsp[0],sizeof(MgMgcoCmdReply)))){ + return ret; + } + + rsp.u.mgCmdRsp[0]->pres.pres = PRSNT_NODEF; + rsp.u.mgCmdRsp[0]->type.pres = PRSNT_NODEF; + rsp.u.mgCmdRsp[0]->type.val = MGT_ADD; + rsp.u.mgCmdRsp[0]->u.add.pres.pres = PRSNT_NODEF; + + if(!is_rtp){ + /* IF ADD request is for Physical term then we can simply copy incoming + * termination */ + //mgUtlAllocMgMgcoTermIdLst(&rsp.u.mgCmdRsp[0]->u.add.termIdLst, &inc_cmd->u.mgCmdReq[0]->cmd.u.add.termIdLst); + mgUtlCpyMgMgcoTermIdLst(&rsp.u.mgCmdRsp[0]->u.add.termIdLst, &inc_cmd->u.mgCmdReq[0]->cmd.u.add.termIdLst, &rsp.u.mgCmdRsp[0]->memCp); + +#ifdef GCP_VER_2_1 + out_termId = rsp.u.mgCmdRsp[0]->u.add.termIdLst.terms[0]; +#else + out_termId = &(rsp.u.mgCmdRsp[0]->u.add.termId); +#endif + }else{ + /* ADD request is for RTP term we need to create termination */ + + + /* Grow the list of reply parameters */ + if (mgUtlGrowList((void ***)&rsp.u.mgCmdRsp[0]->u.add.termIdLst.terms, sizeof(MgMgcoTermId), + &rsp.u.mgCmdRsp[0]->u.add.termIdLst.num, &rsp.u.mgCmdRsp[0]->memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + out_termId = rsp.u.mgCmdRsp[0]->u.add.termIdLst.terms[rsp.u.mgCmdRsp[0]->u.add.termIdLst.num.val-1]; + mg_fill_mgco_termid(out_termId, (char*)term->name, strlen((char*)term->name), &rsp.u.mgCmdRsp[0]->memCp); + } + + if(is_rtp){ + /* Whatever Media descriptor we have received, we can copy that and then + * whatever we want we can modify the fields */ + /* Kapil - TODO - will see if there is any problem of coping the + * descriptor */ + + if (mgUtlGrowList((void ***)&rsp.u.mgCmdRsp[0]->u.add.audit.parms, sizeof(MgMgcoAudRetParm), + &rsp.u.mgCmdRsp[0]->u.add.audit.num, &rsp.u.mgCmdRsp[0]->memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + + /* copy media descriptor */ + desc = rsp.u.mgCmdRsp[0]->u.add.audit.parms[rsp.u.mgCmdRsp[0]->u.add.audit.num.val-1]; + desc->type.pres = PRSNT_NODEF; + desc->type.val = MGT_MEDIADESC; + mgUtlCpyMgMgcoMediaDesc(&desc->u.media, inc_med_desc, &rsp.u.mgCmdRsp[0]->memCp); + /* see if we have received local descriptor */ + if((NOTPRSNT != desc->u.media.num.pres) && + (0 != desc->u.media.num.val)) + { + for(mediaId=0; mediaIdu.media.num.val; mediaId++) { + if(MGT_MEDIAPAR_LOCAL == desc->u.media.parms[mediaId]->type.val) { + local = &desc->u.media.parms[mediaId]->u.local; + } + } + } + + + /* only for RTP */ + if(SWITCH_STATUS_FALSE == mg_build_sdp(&desc->u.media, inc_med_desc, mg_profile, term, &rsp.u.mgCmdRsp[0]->memCp)) { + if(term->mg_error_code && (*term->mg_error_code == MGT_MGCP_RSP_CODE_INCONSISTENT_LCL_OPT)){ + mg_profile->mg_stats->total_num_of_un_supported_codec_error++; + mg_util_set_err_string(&errTxt, " Unsupported Codec "); + err_code = MGT_MGCP_RSP_CODE_INCONSISTENT_LCL_OPT; + goto error; + } + } + } + + + /* We will always send one command at a time..*/ + rsp.cmdStatus.pres = PRSNT_NODEF; + rsp.cmdStatus.val = CH_CMD_STATUS_END_OF_CMD; + + rsp.cmdType.pres = PRSNT_NODEF; + rsp.cmdType.val = CH_CMD_TYPE_RSP; + + ret = sng_mgco_send_cmd( mg_profile->idx, &rsp); + + } + + /*************************************************************************************************************************/ + return ret; + +error: + mg_profile->mg_stats->total_num_of_add_failed++; + if (SWITCH_STATUS_SUCCESS == + mg_build_mgco_err_request(&mgErr, txn_id, ctxtId, err_code, &errTxt)) { + sng_mgco_send_err(mg_profile->idx, mgErr); + } + if(err_code != MGT_MGCO_RSP_CODE_DUP_TERM_CTXT){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," ADD Request failed..releasing context/termination(if allocated) \n"); + if(mg_ctxt){ + /* we can call sub all from context api to release terminations.. + as it could possible that phy term added to context but + failure happened while adding rtp term, sub_all will release phy term also */ + megaco_context_sub_all_termination(mg_ctxt); + megaco_release_context(mg_ctxt); + } + if(term){ + megaco_termination_destroy(term); + } + } + return SWITCH_STATUS_FALSE; +} + +/*****************************************************************************************************************************/ + +/* +* +* Fun: handle_mg_modify_cmd +* +* Desc: this api will handle the Modify request received from MG stack +* +* +*/ +switch_status_t handle_mg_modify_cmd(megaco_profile_t* mg_profile, MgMgcoCommand *inc_cmd) +{ + mg_context_t* mg_ctxt = NULL; + MgMgcoContextId *ctxtId; + MgStr errTxt; + MgMgcoInd *mgErr; + MgMgcoTermId *termId; + MgMgcoTermIdLst* termLst; + mg_termination_t* term = NULL; + switch_status_t ret; + MgMgcoAudRetParm *desc; + MgMgcoMediaDesc* inc_med_desc = NULL; + MgMgcoLocalDesc *local = NULL; + int err_code; + int mediaId; + /*MgMgcoAmmReq *cmd = &inc_cmd->u.mgCmdInd[0]->cmd.u.mod;*/ + U32 txn_id = inc_cmd->transId.val; + char term_name[128]; + + memset(&term_name,0,sizeof(term_name)); + + /********************************************************************/ + ctxtId = &inc_cmd->contextId; + termLst = mg_get_term_id_list(inc_cmd); + termId = termLst->terms[0]; + + MG_MEM_COPY(&term_name, termId->name.lcl.val, sizeof(U8) * termId->name.lcl.len); + + /********************************************************************/ + /* Validation *******************************************/ + /********************************************************************/ + + /*** CHOOSE Context NOT ALLOWED ***/ + if ((NOTPRSNT != ctxtId->type.pres) && + (MGT_CXTID_CHOOSE == ctxtId->type.val)) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "Modify request processing failure, CHOOSE Context should not present in Modify\n"); + + err_code = MGT_MGCO_RSP_CODE_INVLD_IDENTIFIER; + mg_util_set_ctxt_string(&errTxt, ctxtId); + goto error; + } + /*** CHOOSE Termination NOT ALLOWED **/ + else if ((NOTPRSNT != termId->type.pres) && + (MGT_TERMID_CHOOSE == termId->type.val)) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "Modify request processing failure, CHOOSE Termination should not present in Modify\n"); + + err_code = MGT_MGCO_RSP_CODE_INVLD_IDENTIFIER; + mg_util_set_term_string(&errTxt,termId); + goto error; + } + + /********************************************************************/ + /* context id presence check is already being done, we are here it means context-id requested in megaco message is present */ + /********************************************************************/ + + /* MGT_TERMID_ROOT = If ROOT term then there will not be any conext */ + + if(MGT_TERMID_ROOT == termId->type.val){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Modify request is for ROOT termination \n"); + + /* check if we have ito packg request */ + mg_is_ito_pkg_req(mg_profile, inc_cmd); + + /********************************************************************/ + } else if(MGT_TERMID_OTHER == termId->type.val){ + /********************************************************************/ +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Modify request is for termination[%s] and context: type[%d], value[%d] \n", + termId->name.lcl.val, ctxtId->type.val, ctxtId->val.val); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Modify request is for termination[%s] and context: type[%d], value[%ld] \n", + termId->name.lcl.val, ctxtId->type.val, ctxtId->val.val); +#endif + + term = megaco_find_termination(mg_profile, term_name); + + if(NULL == term){ + mg_profile->mg_stats->total_num_of_find_term_failed_error++; + mg_util_set_term_string(&errTxt,termId); + err_code = MGT_MGCO_RSP_CODE_UNKNOWN_TERM_ID; + goto error; + } + + /* termination specified...context should also be specified */ + + /* check if we have terminations in the context */ + + if (NOTPRSNT != ctxtId->type.pres) { + if(MGT_CXTID_OTHER == ctxtId->type.val) { + /*find context based on received context-id */ + mg_ctxt = megaco_get_context(mg_profile, ctxtId->val.val); + if(NULL == mg_ctxt){ + mg_profile->mg_stats->total_num_of_get_ctxt_failed_error++; +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "Modify request Failed, context[%d] not found \n",ctxtId->val.val); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "Modify request Failed, context[%ld] not found \n",ctxtId->val.val); +#endif + mg_util_set_ctxt_string(&errTxt, ctxtId); + err_code = MGT_MGCO_RSP_CODE_UNKNOWN_CTXT; + goto error; + } + + if(SWITCH_STATUS_FALSE == megaco_context_is_term_present(mg_ctxt, term)){ + mg_profile->mg_stats->total_num_of_no_term_ctxt_error++; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "Modify request Failed, requested term not associated with any context \n"); + /* ERROR - termination didnt bind with requested context */ + mg_util_set_term_string(&errTxt,termId); + err_code = MGT_MGCO_RSP_CODE_NO_TERM_CTXT; + goto error; + } + + }else if(MGT_CXTID_NULL == ctxtId->type.val) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Modify request is for NULL Context \n"); + /*TODO - NULL context...nothing to do now...jump to response to send +ve response */ + goto response; + }else if(MGT_CXTID_ALL == ctxtId->type.val) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Modify request is for ALL Context \n"); + /*TODO - ALL context...nothing to do now...jump to response to send +ve response */ + goto response; + } + } + + /* Not sure if MODIFY can come with Context ALL with specified term */ + + /********************************************************************/ + + ret = mg_prc_descriptors(mg_profile, inc_cmd, term, &inc_cmd->u.mgCmdInd[0]->memCp); + + /* IF there is any error , return */ + if(term->mg_error_code && (*term->mg_error_code == MGT_MGCP_RSP_CODE_INCONSISTENT_LCL_OPT)){ + mg_profile->mg_stats->total_num_of_un_supported_codec_error++; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "Modify request Failed, Unsupported Codec \n"); + mg_util_set_err_string(&errTxt, " Unsupported Codec "); + err_code = MGT_MGCP_RSP_CODE_INCONSISTENT_LCL_OPT; + goto error; + } + + if(MG_TERM_RTP == term->type){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"MODIFY REQUEST - Updated RTP attributes:" + " Media_Type(%s),local_addr[%s] local_port[%d] remote_addr[%s], remote_port[%d], ptime[%d] pt[%d], " + " rfc2833_pt[%d] rate[%d], codec[%s], term_id[%d]\n", + mg_media_type2str(term->u.rtp.media_type), + ((NULL != term->u.rtp.local_addr)?term->u.rtp.local_addr:NULL), + term->u.rtp.local_port, + ((NULL != term->u.rtp.remote_addr)?term->u.rtp.remote_addr:NULL), + term->u.rtp.remote_port, + term->u.rtp.ptime, + term->u.rtp.pt, + term->u.rtp.rfc2833_pt, + term->u.rtp.rate, + ((NULL != term->u.rtp.codec)?term->u.rtp.codec:NULL), + term->u.rtp.term_id); + } + + mg_apply_tdm_dtmf_removal(term, mg_ctxt); + + mg_apply_tdm_ec(term, mg_ctxt); + + if((MG_TERM_RTP == term->type) && (term->u.rtp.t38_options)) + mg_profile->mg_stats->total_num_of_t38_call_recvd++; + + mg_print_t38_attributes(term); + + /* SDP updated to termination */ + if(SWITCH_STATUS_SUCCESS != megaco_activate_termination(term)) { + mg_profile->mg_stats->total_num_of_term_activation_failed_error++; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "Modify request Failed, Activation of termination failed \n"); + mg_util_set_err_string(&errTxt, " Resource Failure "); + err_code = MGT_MGCO_RSP_CODE_RSRC_ERROR; + goto error; + } + } + + /********************************************************************/ + +response: + { /* send response */ + + MgMgcoCommand rsp; + int ret = 0x00; + + memset(&rsp,0, sizeof(rsp)); + + /*copy transaction-id*/ + memcpy(&rsp.transId, &inc_cmd->transId,sizeof(MgMgcoTransId)); + + /*copy context-id*/ /*TODO - in case of $ context should be generated by app, we should not simply copy incoming structure */ + memcpy(&rsp.contextId, &inc_cmd->contextId,sizeof(MgMgcoContextId)); + + /*copy peer identifier */ + memcpy(&rsp.peerId, &inc_cmd->peerId,sizeof(TknU32)); + + /*fill response structue */ + if(SWITCH_STATUS_FALSE == (ret = mg_stack_alloc_mem((Ptr*)&rsp.u.mgCmdRsp[0],sizeof(MgMgcoCmdReply)))){ + return ret; + } + + rsp.u.mgCmdRsp[0]->pres.pres = PRSNT_NODEF; + rsp.u.mgCmdRsp[0]->type.pres = PRSNT_NODEF; + rsp.u.mgCmdRsp[0]->type.val = MGT_MODIFY; + rsp.u.mgCmdRsp[0]->u.mod.pres.pres = PRSNT_NODEF; + rsp.u.mgCmdRsp[0]->u.mod.termIdLst.num.pres = PRSNT_NODEF; + rsp.u.mgCmdRsp[0]->u.mod.termIdLst.num.val = 1; + + mgUtlCpyMgMgcoTermIdLst(&rsp.u.mgCmdRsp[0]->u.mod.termIdLst, &inc_cmd->u.mgCmdReq[0]->cmd.u.mod.termIdLst, &rsp.u.mgCmdRsp[0]->memCp); + +#ifdef GCP_VER_2_1 + termId = rsp.u.mgCmdRsp[0]->u.mod.termIdLst.terms[0]; +#else + termId = &(rsp.u.mgCmdRsp[0]->u.mod.termId); +#endif + if((MGT_TERMID_ROOT != termId->type.val) && + (term && (MG_TERM_RTP == term->type) && + ((NOTPRSNT != inc_cmd->u.mgCmdInd[0]->cmd.u.mod.dl.num.pres) && + (0 != inc_cmd->u.mgCmdInd[0]->cmd.u.mod.dl.num.val)))) { + /* Whatever Media descriptor we have received, we can copy that and then + * whatever we want we can modify the fields */ + /* Kapil - TODO - will see if there is any problem of coping the + * descriptor */ + + inc_med_desc = &inc_cmd->u.mgCmdInd[0]->cmd.u.mod.dl.descs[0]->u.media; + + if (mgUtlGrowList((void ***)&rsp.u.mgCmdRsp[0]->u.mod.audit.parms, sizeof(MgMgcoAudRetParm), + &rsp.u.mgCmdRsp[0]->u.mod.audit.num, &rsp.u.mgCmdRsp[0]->memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + /* copy media descriptor */ + desc = rsp.u.mgCmdRsp[0]->u.mod.audit.parms[rsp.u.mgCmdRsp[0]->u.mod.audit.num.val-1]; + desc->type.pres = PRSNT_NODEF; + desc->type.val = MGT_MEDIADESC; + mgUtlCpyMgMgcoMediaDesc(&desc->u.media, inc_med_desc, &rsp.u.mgCmdRsp[0]->memCp); + /* see if we have received local descriptor */ + if((NOTPRSNT != desc->u.media.num.pres) && + (0 != desc->u.media.num.val)) + { + for(mediaId=0; mediaIdu.media.num.val; mediaId++) { + if(MGT_MEDIAPAR_LOCAL == desc->u.media.parms[mediaId]->type.val) { + local = &desc->u.media.parms[mediaId]->u.local; + } + } + } + + /* only for RTP */ + if(SWITCH_STATUS_FALSE == mg_build_sdp(&desc->u.media, inc_med_desc, mg_profile, term, &rsp.u.mgCmdRsp[0]->memCp)) { + if(term->mg_error_code && (*term->mg_error_code == MGT_MGCP_RSP_CODE_INCONSISTENT_LCL_OPT)){ + mg_util_set_err_string(&errTxt, " Unsupported Codec "); + err_code = MGT_MGCP_RSP_CODE_INCONSISTENT_LCL_OPT; + goto error; + } + } + } + + /* We will always send one command at a time..*/ + rsp.cmdStatus.pres = PRSNT_NODEF; + rsp.cmdStatus.val = CH_CMD_STATUS_END_OF_CMD; + + rsp.cmdType.pres = PRSNT_NODEF; + rsp.cmdType.val = CH_CMD_TYPE_RSP; + + + ret = sng_mgco_send_cmd(mg_profile->idx, &rsp); + + } + + + return SWITCH_STATUS_SUCCESS; +error: + if (SWITCH_STATUS_SUCCESS == + mg_build_mgco_err_request(&mgErr, txn_id, ctxtId, err_code, &errTxt)) { + sng_mgco_send_err(mg_profile->idx, mgErr); + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," Modify Request failed..releasing context/termination \n"); + if(mg_ctxt){ + megaco_release_context(mg_ctxt); + } + if(term){ + megaco_termination_destroy(term); + } + return SWITCH_STATUS_FALSE; +} + +/*****************************************************************************************************************************/ + +/* +* +* Fun: handle_mg_subtract_cmd +* +* Desc: this api will handle the Subtract request received from MG stack +* +* +*/ +switch_status_t handle_mg_subtract_cmd(megaco_profile_t* mg_profile, MgMgcoCommand *inc_cmd) +{ + MgMgcoContextId *ctxtId; + MgStr errTxt; + MgMgcoInd *mgErr; + MgMgcoTermId *termId; + MgMgcoTermIdLst* termLst; + int err_code; + /*MgMgcoAmmReq *cmd = &inc_cmd->u.mgCmdInd[0]->cmd.u.add;*/ + U32 txn_id = inc_cmd->transId.val; + mg_context_t* mg_ctxt = NULL; + mg_termination_t* term = NULL; + uint8_t wild = 0x00; + char term_name[128]; + + memset(&term_name,0,sizeof(term_name)); + wild = inc_cmd->u.mgCmdReq[0]->wild.pres; + + + /************************************************************************************************************************************************************/ + ctxtId = &inc_cmd->contextId; + termLst = mg_get_term_id_list(inc_cmd); + termId = termLst->terms[0]; + MG_MEM_COPY(&term_name, termId->name.lcl.val, sizeof(U8) * termId->name.lcl.len); + + mg_profile->mg_stats->total_num_of_sub_recvd++; + + /************************************************************************************************************************************************************/ + /* Validating Subtract request *******************************************/ + + if(NOTPRSNT == ctxtId->type.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," SUB Request processing failed, Context Not Present\n"); + mg_util_set_ctxt_string(&errTxt, ctxtId); + err_code = MGT_MGCO_RSP_CODE_PROT_ERROR; + goto error; + } + + if(NOTPRSNT == termId->type.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," SUB Request processing failed, Termination Not Present\n"); + mg_util_set_term_string(&errTxt,termId); + err_code = MGT_MGCO_RSP_CODE_PROT_ERROR; + goto error; + } + + /*-- NULL Context & CHOOSE Context not applicable for SUB request --*/ + if ((MGT_CXTID_CHOOSE == ctxtId->type.val) || + (MGT_CXTID_NULL == ctxtId->type.val)) { + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," SUB Request processing failed, Context CHOOSE/NULL not allowed\n"); + + mg_util_set_ctxt_string(&errTxt, ctxtId); + err_code = MGT_MGCO_RSP_CODE_PROT_ERROR; + goto error; + } + /* ROOT Termination & CHOOSE Termination not allowed */ + else if ((MGT_TERMID_ROOT == termId->type.val) || + (MGT_TERMID_CHOOSE == termId->type.val)) { + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," SUB Request processing failed, Termination ROOT/CHOOSE not allowed\n"); + + mg_util_set_term_string(&errTxt,termId); + err_code = MGT_MGCO_RSP_CODE_INVLD_IDENTIFIER; + goto error; + } + /************************************************************************************************************************************************************/ + + if (MGT_CXTID_OTHER == ctxtId->type.val){ + +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," SUB Request for Context[%d] \n", ctxtId->val.val); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," SUB Request for Context[%ld] \n", ctxtId->val.val); +#endif + + /*find context based on received context-id */ + mg_ctxt = megaco_get_context(mg_profile, ctxtId->val.val); + if(NULL == mg_ctxt){ + mg_profile->mg_stats->total_num_of_get_ctxt_failed_error++; + mg_util_set_ctxt_string(&errTxt, ctxtId); + err_code = MGT_MGCO_RSP_CODE_UNKNOWN_CTXT; + goto error; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Found Context[%p] for context_id[%d]\n", (void*)mg_ctxt, mg_ctxt->context_id); + if(MGT_TERMID_ALL == termId->type.val){ + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," SUB Request for context[%d] with ALL termination \n", mg_ctxt->context_id); + + /* remove terminations from context */ + megaco_context_sub_all_termination(mg_ctxt); + + }else if(MGT_TERMID_OTHER == termId->type.val){ + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," SUB Request for termination[%s] \n", term_name); + + term = megaco_find_termination(mg_profile, term_name); + + if(NULL == term){ + mg_profile->mg_stats->total_num_of_find_term_failed_error++; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "Subtract request Failed, no termination found for input term string[%s] \n", term_name); + mg_util_set_term_string(&errTxt,termId); + err_code = MGT_MGCO_RSP_CODE_UNKNOWN_TERM_ID; + goto error; + } + + if(SWITCH_STATUS_FALSE == megaco_context_is_term_present(mg_ctxt, term)){ + mg_profile->mg_stats->total_num_of_no_term_ctxt_error++; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "Subtract request Failed, termination no associated with any context \n"); + /* ERROR - termination didnt bind with requested context */ + mg_util_set_term_string(&errTxt,termId); + err_code = MGT_MGCO_RSP_CODE_NO_TERM_CTXT; + goto error; + } + + /* remove termination from context */ + megaco_context_sub_termination(mg_ctxt, term); + } + + if ((NULL == mg_ctxt->terminations[0]) && (NULL == mg_ctxt->terminations[1])) { + /* release context*/ + megaco_release_context(mg_ctxt); + } + } + + /************************************************************************************************************************************************************/ + /* CONTEXT = ALL */ + + if (MGT_CXTID_ALL == ctxtId->type.val){ + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," SUB Request for ALL context \n"); + + megaco_release_all_calls(mg_profile); + + /* As of now sending +ve response */ + goto response; + } + + + /************************************************************************************************************************************************************/ + + +response: + /************************************************************************************************************************************************************/ + /* resp code -- begin */ + { + MgMgcoCommand rsp; + int ret = 0x00; + MgMgcoTermId *out_termId; + + memset(&rsp,0, sizeof(rsp)); + + /*copy transaction-id*/ + memcpy(&rsp.transId, &inc_cmd->transId, sizeof(MgMgcoTransId)); + + /*copy context-id*/ + memcpy(&rsp.contextId, &inc_cmd->contextId, sizeof(MgMgcoContextId)); + + /*copy peer identifier */ + memcpy(&rsp.peerId, &inc_cmd->peerId,sizeof(TknU32)); + + /*fill response structue */ + if(SWITCH_STATUS_FALSE == (ret = mg_stack_alloc_mem((Ptr*)&rsp.u.mgCmdRsp[0],sizeof(MgMgcoCmdReply)))){ + return ret; + } + + rsp.u.mgCmdRsp[0]->pres.pres = PRSNT_NODEF; + rsp.u.mgCmdRsp[0]->type.pres = PRSNT_NODEF; + rsp.u.mgCmdRsp[0]->type.val = MGT_SUB; + rsp.u.mgCmdRsp[0]->u.sub.pres.pres = PRSNT_NODEF; + + if(wild){ + rsp.u.mgCmdRsp[0]->wild.pres = PRSNT_NODEF; + } + + //mgUtlAllocMgMgcoTermIdLst(&rsp.u.mgCmdRsp[0]->u.add.termIdLst, &inc_cmd->u.mgCmdReq[0]->cmd.u.add.termIdLst); + mgUtlCpyMgMgcoTermIdLst(&rsp.u.mgCmdRsp[0]->u.add.termIdLst, &inc_cmd->u.mgCmdReq[0]->cmd.u.add.termIdLst, &rsp.u.mgCmdRsp[0]->memCp); + +#ifdef GCP_VER_2_1 + out_termId = rsp.u.mgCmdRsp[0]->u.add.termIdLst.terms[0]; +#else + out_termId = &(rsp.u.mgCmdRsp[0]->u.add.termId); +#endif + + /* We will always send one command at a time..*/ + rsp.cmdStatus.pres = PRSNT_NODEF; + rsp.cmdStatus.val = CH_CMD_STATUS_END_OF_CMD; + + rsp.cmdType.pres = PRSNT_NODEF; + rsp.cmdType.val = CH_CMD_TYPE_RSP; + + ret = sng_mgco_send_cmd( mg_profile->idx, &rsp); + + return ret; + + } + /* sample resp code -- end */ + /************************************************************************************************************************************************************/ + + + return SWITCH_STATUS_SUCCESS; +error: + if (SWITCH_STATUS_SUCCESS == + mg_build_mgco_err_request(&mgErr, txn_id, ctxtId, err_code, &errTxt)) { + sng_mgco_send_err(mg_profile->idx, mgErr); + } + return SWITCH_STATUS_FALSE; +} + +/*****************************************************************************************************************************/ +/* +* +* Fun: mg_send_end_of_axn +* +* Desc: this api will send the END_OF_AXN event to MG stack to indicate that application is done with processing +* +* +*/ +switch_status_t mg_send_end_of_axn(SuId suId, MgMgcoTransId* transId, MgMgcoContextId* ctxtId, TknU32* peerId) +{ + int ret = 0x00; + MgMgcoCtxt ctxt; + + memset(&ctxt,0, sizeof(ctxt)); + memcpy(&ctxt.transId,transId,sizeof(MgMgcoTransId)); + memcpy(&ctxt.cntxtId, ctxtId,sizeof(MgMgcoContextId)); + memcpy(&ctxt.peerId, peerId,sizeof(TknU32)); + ctxt.cmdStatus.pres = PRSNT_NODEF; + ctxt.cmdStatus.val = CH_CMD_STATUS_END_OF_AXN; + +#if 0 +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "mg_send_end_of_axn: Sending END_OF_AXN for transId[%d], peerId[%d], context[type = %s, value = %d]\n", + transId->val, peerId->val, PRNT_MG_CTXT_TYPE(ctxtId->type.val), ctxtId->val.val); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "mg_send_end_of_axn: Sending END_OF_AXN for transId[%lu], peerId[%lu], context[type = %s, value = %lu]\n", + transId->val, peerId->val, PRNT_MG_CTXT_TYPE(ctxtId->type.val), ctxtId->val.val); + +#endif +#endif + + ret = sng_mgco_send_axn_req(suId, &ctxt); + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************************/ +/* +* +* Fun: mg_build_mgco_err_request +* +* Desc: this api will send the Error event to MG stack to indicate failure in application +* +* +*/ + +switch_status_t mg_build_mgco_err_request(MgMgcoInd **errcmd,U32 trans_id, MgMgcoContextId *ctxt_id, U32 err, MgStr *errTxt) +{ + MgMgcoInd *mgErr = NULL; + S16 ret; + + mgErr = NULLP; + ret = ROK; + +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "Sending Error Request with erro code[%d] for trans_id[%d]\n",trans_id, err); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "Sending Error Request with erro code[%ld] for trans_id[%ld]\n",trans_id, err); +#endif + + /* Allocate for AG error */ + mg_stack_alloc_mem((Ptr*)&mgErr, sizeof(MgMgcoInd)); + if (NULL == mgErr) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " mg_build_mgco_err_request Failed : memory alloc \n"); + return SWITCH_STATUS_FALSE; + } + + /* Set transaction Id in the error request */ + MG_SET_VAL_PRES(mgErr->transId, trans_id); + + /* Copy the context Id */ + MG_MEM_COPY(&mgErr->cntxtId, + ctxt_id, + sizeof(MgMgcoContextId)); + + /* Set the peerId */ + mgErr->peerId.pres = NOTPRSNT; + + /* Set the error code */ + MG_SET_PRES(mgErr->err.pres.pres); + MG_SET_PRES(mgErr->err.code.pres); + MG_SET_VAL_PRES(mgErr->err.code, err); + + if(errTxt->len) + { + MG_GETMEM(mgErr->err.text.val, (errTxt->len)*sizeof(U8), &mgErr->memCp, &ret); + if (ROK != ret) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " mg_build_mgco_err_request Failed : memory alloc \n"); + return SWITCH_STATUS_FALSE; + } + mgErr->err.text.pres = PRSNT_NODEF; + mgErr->err.text.len = errTxt->len; + MG_MEM_COPY(mgErr->err.text.val, errTxt->val, errTxt->len); + } + + /* Set the output value */ + *errcmd = mgErr; + + return SWITCH_STATUS_SUCCESS; +} + + +/*****************************************************************************************************************************/ +switch_status_t handle_mg_audit_cmd( megaco_profile_t* mg_profile, MgMgcoCommand *auditReq) +{ + MgMgcoContextId *ctxtId; + MgMgcoTermIdLst *term_list; + MgStr errTxt; + MgMgcoInd *mgErr; + MgMgcoTermId *termId; + MgMgcoSubAudReq *audit; + MgMgcoAuditDesc *audit_desc; + MgMgcoAuditItem *audit_item; + int i; + int err_code; + MgMgcoCommand reply; + MgMgcoAuditReply *adtRep = NULLP; + U16 numOfParms; + MgMgcoMediaDesc* media; + MgMgcoCtxt ctxt; + switch_status_t ret; + uint8_t wild = 0x00; + + memset(&reply, 0, sizeof(reply)); + memset(&ctxt, 0, sizeof(ctxt)); + + audit = &auditReq->u.mgCmdReq[0]->cmd.u.aval; + wild = auditReq->u.mgCmdReq[0]->wild.pres; + + if(NOTPRSNT == audit->pres.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Audit structure not present..rejecting \n"); + return SWITCH_STATUS_FALSE; + } + + audit_desc = &audit->audit; + + if((NOTPRSNT == audit_desc->pres.pres) || ( NOTPRSNT == audit_desc->num.pres)){ + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Audit Descriptor not present.. Could be HeartBeat message\n"); + return mg_send_heartbeat_audit_rsp(mg_profile->idx, auditReq); + } + + /* dump AUDIT message information */ + /*mgAccEvntPrntMgMgcoSubAudReq(auditReq,stdout);*/ + + /*-- Get context id --*/ + ctxtId = &auditReq->contextId; + + /*-- Get termination list --*/ + term_list = mg_get_term_id_list(auditReq); + termId = term_list->terms[0]; + + /*********************************************************************************************************************/ + /**************************** Validating Audit Request ***************************************************************/ + /*********************************************************************************************************************/ + /*-- Start with Context level checks --*/ + /*-- CHOOSE Context not allowed --*/ + if ((NOTPRSNT != ctxtId->type.pres) && + (MGT_CXTID_CHOOSE == ctxtId->type.val)) { + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "failed, Ctxt: CHOOSE not allowed in Audit Value\n"); + + /* set correct error code */ + mg_util_set_ctxt_string(&errTxt,ctxtId); + err_code = MGT_MGCO_RSP_CODE_INVLD_IDENTIFIER; + goto error; + } + /*-- CHOOSE Termination not allowed --*/ + else if ((NOTPRSNT != termId->type.pres) && + (MGT_TERMID_CHOOSE == termId->type.val)) { + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "failed, Term: CHOOSE not allowed in Audit Value\n"); + + mg_util_set_term_string(&errTxt,termId); + err_code = MGT_MGCO_RSP_CODE_INVLD_IDENTIFIER; + goto error; + } + + /*-- For Audit Values, ROOT Termination is allowed with Context = ALL but not allowed with Other --*/ + /*-- Check whether the termination is present in the given context --*/ + if (((NOTPRSNT != termId->type.pres) && + (MGT_TERMID_ROOT == termId->type.val)) && + ((NOTPRSNT != ctxtId->type.pres) && + (MGT_CXTID_OTHER == ctxtId->type.val))) { + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "failed, Term: Invalid combination, ROOT Term with OTHER CONTEXT\n"); + + mg_util_set_ctxt_string(&errTxt,ctxtId); + err_code = MGT_MGCO_RSP_CODE_INVLD_IDENTIFIER; + goto error; + } + + /*********************************************************************************************************************/ + /**************************** Preparing Response Structure ***********************************************************/ + /*********************************************************************************************************************/ + /*copy transaction-id*/ + memcpy(&reply.transId, &auditReq->transId,sizeof(MgMgcoTransId)); + /*copy context-id*/ + memcpy(&reply.contextId, &auditReq->contextId,sizeof(MgMgcoContextId)); + /*copy peer identifier */ + memcpy(&reply.peerId, &auditReq->peerId,sizeof(TknU32)); + + /*fill response structue */ + if(SWITCH_STATUS_FALSE == (ret = mg_stack_alloc_mem((Ptr*)&reply.u.mgCmdRsp[0],sizeof(MgMgcoCmdReply)))){ + return ret; + } + + reply.u.mgCmdRsp[0]->pres.pres = PRSNT_NODEF; + reply.u.mgCmdRsp[0]->type.pres = PRSNT_NODEF; + reply.u.mgCmdRsp[0]->type.val = MGT_AUDITVAL; + + + adtRep = &(reply.u.mgCmdRsp[0]->u.aval); + + adtRep->type.pres = PRSNT_NODEF; + adtRep->type.val = MGT_TERMAUDIT; + adtRep->u.other.pres.pres = PRSNT_NODEF; + + mgUtlCpyMgMgcoTermIdLst(&adtRep->u.other.termIdLst, term_list, &reply.u.mgCmdRsp[0]->memCp); + + /* NOW for each requested AUDIT descriptor we need to add entry to adtRep->u.other.audit.parms list */ + + /*********************************************************************************************************************/ + /**************************** Processing Audit Request Descriptors **************************************************/ + /*********************************************************************************************************************/ + + for (i = 0; i < audit_desc->num.val; i++) { + + audit_item = audit_desc->al[i]; + + if (!audit_item) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + "Audit Descriptor is NULL.. rejecting \n"); + return SWITCH_STATUS_FALSE; + } + + /*TODO - If we are not supporint AUDIT type then can send "MGT_MGCO_RSP_CODE_UNSUPPORTED_DESC" error to MG stack */ + if (NOTPRSNT != audit_item->auditItem.pres) { + + switch(audit_item->auditItem.val) + { + case MGT_MEDIADESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Auditing MEDIA \n"); + + /* Grow the list of reply parameters */ + if (mgUtlGrowList((void ***)&adtRep->u.other.audit.parms, sizeof(MgMgcoAudRetParm), + &adtRep->u.other.audit.num, &reply.u.mgCmdRsp[0]->memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + numOfParms = adtRep->u.other.audit.num.val; + adtRep->u.other.audit.parms[numOfParms - 1]->type.pres = PRSNT_NODEF; + adtRep->u.other.audit.parms[numOfParms - 1]->type.val = MGT_MEDIADESC; + + media = get_default_media_desc(mg_profile, termId, &reply.u.mgCmdRsp[0]->memCp); + if(!media){ + return SWITCH_STATUS_FALSE; + } + mgUtlCpyMgMgcoMediaDesc(&adtRep->u.other.audit.parms[numOfParms - 1]->u.media, media, &reply.u.mgCmdRsp[0]->memCp); + + break; + } + case MGT_MODEMDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Auditing MODEM \n"); + break; + } + case MGT_MUXDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Auditing MULTIPLEX \n"); + break; + } + case MGT_REQEVTDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Auditing Events \n"); + break; + } + case MGT_SIGNALSDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Auditing Signals \n"); + break; + } + case MGT_DIGMAPDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Auditing Digit Maps \n"); + break; + } + case MGT_OBSEVTDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Auditing Buffer Events \n"); + break; + } + case MGT_EVBUFDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Auditing Events Buffer \n"); + break; + } + case MGT_STATSDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Auditing Statistics \n"); + break; + } + case MGT_PKGSDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Auditing Packages \n"); + /* Grow the list of reply parameters */ + if (mgUtlGrowList((void ***)&adtRep->u.other.audit.parms, sizeof(MgMgcoAudRetParm), + &adtRep->u.other.audit.num, &reply.u.mgCmdRsp[0]->memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + numOfParms = adtRep->u.other.audit.num.val; + adtRep->u.other.audit.parms[numOfParms - 1]->type.pres = PRSNT_NODEF; + adtRep->u.other.audit.parms[numOfParms - 1]->type.val = MGT_PKGSDESC; + + if(SWITCH_STATUS_FALSE == mg_build_pkg_desc(&adtRep->u.other.audit.parms[numOfParms - 1]->u.pkgs, &reply.u.mgCmdRsp[0]->memCp)){ + return SWITCH_STATUS_FALSE; + } + + break; + } + case MGT_INDAUD_TERMAUDDESC: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Individual Term Audit \n"); + break; + } + default: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Invalid Audit Descriptor[%d] request\n",audit_item->auditItem.val); + err_code = MGT_MGCO_RSP_CODE_UNSUPPORTED_DESC; + goto error; + } + }/*switch(audit_item->auditItem.val)*/ + }/*if (NOTPRSNT != audit_item->auditItem.pres)*/ + }/*for loop - audit_desc->num.val */ + + /*********************************************************************************************************************/ + /**************************** Send Audit Command Reply***************************************************************/ + /*********************************************************************************************************************/ + reply.cmdStatus.pres = PRSNT_NODEF; + reply.cmdStatus.val = CH_CMD_STATUS_END_OF_CMD; + reply.cmdType.pres = PRSNT_NODEF; + reply.cmdType.val = CH_CMD_TYPE_RSP; + + if(wild){ + reply.u.mgCmdRsp[0]->wild.pres = PRSNT_NODEF; + } + + + /* send command reply */ + sng_mgco_send_cmd(mg_profile->idx, &reply); + + /* send indication to stack , so he can send response back to peer */ + memcpy(&ctxt.transId,&auditReq->transId,sizeof(MgMgcoTransId)); + memcpy(&ctxt.cntxtId, &auditReq->contextId,sizeof(MgMgcoContextId)); + memcpy(&ctxt.peerId, &auditReq->peerId,sizeof(TknU32)); + ctxt.cmdStatus.pres = PRSNT_NODEF; + ctxt.cmdStatus.val = CH_CMD_STATUS_END_OF_AXN; + sng_mgco_send_axn_req(mg_profile->idx, &ctxt); + /***********************************************************************************************************************************/ + + return SWITCH_STATUS_SUCCESS; + +error: + if (SWITCH_STATUS_SUCCESS == mg_build_mgco_err_request(&mgErr, auditReq->transId.val, ctxtId, err_code, &errTxt)) { + sng_mgco_send_err(mg_profile->idx, mgErr); + } + + return SWITCH_STATUS_FALSE; +} + +/*****************************************************************************************************************************/ +switch_status_t mg_send_heartbeat_audit_rsp( SuId suId, MgMgcoCommand *auditReq) +{ + MgMgcoCtxt ctxt; + switch_status_t ret; + MgMgcoCommand reply; + MgMgcoTermIdLst *term_list; + MgMgcoTermId *termId; + MgMgcoSubAudReq *audit; + MgMgcoAuditReply *adtRep = NULLP; + + memset(&reply, 0, sizeof(reply)); + audit = &auditReq->u.mgCmdReq[0]->cmd.u.aval; + + if(NOTPRSNT == audit->pres.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Audit structure not present..rejecting \n"); + return SWITCH_STATUS_FALSE; + } + + /*-- Get termination list --*/ + term_list = mg_get_term_id_list(auditReq); + termId = term_list->terms[0]; + + + /*copy transaction-id*/ + memcpy(&reply.transId, &auditReq->transId,sizeof(MgMgcoTransId)); + /*copy context-id*/ + memcpy(&reply.contextId, &auditReq->contextId,sizeof(MgMgcoContextId)); + /*copy peer identifier */ + memcpy(&reply.peerId, &auditReq->peerId,sizeof(TknU32)); + + /*fill response structue */ + if(SWITCH_STATUS_FALSE == (ret = mg_stack_alloc_mem((Ptr*)&reply.u.mgCmdRsp[0],sizeof(MgMgcoCmdReply)))){ + return ret; + } + + reply.u.mgCmdRsp[0]->pres.pres = PRSNT_NODEF; + reply.u.mgCmdRsp[0]->type.pres = PRSNT_NODEF; + reply.u.mgCmdRsp[0]->type.val = MGT_AUDITVAL; + + + adtRep = &(reply.u.mgCmdRsp[0]->u.aval); + + adtRep->type.pres = PRSNT_NODEF; + adtRep->type.val = MGT_TERMAUDIT; + adtRep->u.other.pres.pres = PRSNT_NODEF; + adtRep->u.other.audit.num.pres = 0x00; + + mgUtlCpyMgMgcoTermIdLst(&adtRep->u.other.termIdLst, term_list, &reply.u.mgCmdRsp[0]->memCp); + + + /* We will always send one command at a time..*/ + reply.cmdStatus.pres = PRSNT_NODEF; + reply.cmdStatus.val = CH_CMD_STATUS_END_OF_CMD; + + reply.cmdType.pres = PRSNT_NODEF; + reply.cmdType.val = CH_CMD_TYPE_RSP; + + ret = sng_mgco_send_cmd(suId, &reply); + + /*will send once all audit done*/ + memcpy(&ctxt.transId,&auditReq->transId,sizeof(MgMgcoTransId)); + memcpy(&ctxt.cntxtId, &auditReq->contextId,sizeof(MgMgcoContextId)); + memcpy(&ctxt.peerId, &auditReq->peerId,sizeof(TknU32)); + ctxt.cmdStatus.pres = PRSNT_NODEF; + ctxt.cmdStatus.val = CH_CMD_STATUS_END_OF_AXN; + ret = sng_mgco_send_axn_req(suId, &ctxt); + + return ret; +} + +/*****************************************************************************************************************************/ +U32 get_txn_id(){ + outgoing_txn_id++; + return outgoing_txn_id; +} +/*****************************************************************************************************************************/ +switch_status_t mg_is_peer_active(megaco_profile_t* profile) +{ + if((profile) && (0x01 == profile->peer_active)){ + return SWITCH_STATUS_SUCCESS; + } + return SWITCH_STATUS_FALSE; +} +/*****************************************************************************************************************************/ +switch_status_t mg_send_term_service_change(char *span_name, char *chan_number, mg_term_states_e term_state) +{ + mg_termination_t* term = NULL; + switch_status_t ret = SWITCH_STATUS_SUCCESS; + + switch_assert(span_name); + switch_assert(chan_number); + + term = megaco_term_locate_by_span_chan_id(span_name, chan_number); + + if(!term || !term->profile){ + return SWITCH_STATUS_FALSE; + } + + if(SWITCH_STATUS_FALSE == mg_is_peer_active(term->profile)) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "profile: %s peer not yet enabled..\n", term->profile->name); + return SWITCH_STATUS_FALSE; + } + + switch(term_state) + { + case MG_TERM_SERVICE_STATE_IN_SERVICE: + { + if(switch_test_flag(term, MG_OUT_OF_SERVICE)){ + /* set INS flag...clear oos flag */ + switch_clear_flag(term, MG_OUT_OF_SERVICE); + switch_set_flag(term, MG_IN_SERVICE); + ret = mg_send_ins_service_change(term->profile, term->name, 0x00 ); + } + break; + } + case MG_TERM_SERVICE_STATE_OUT_OF_SERVICE: + { + if(switch_test_flag(term, MG_IN_SERVICE)){ + /* set OOS flag...clear ins flag */ + switch_clear_flag(term, MG_IN_SERVICE); + switch_set_flag(term, MG_OUT_OF_SERVICE); + ret = mg_send_oos_service_change(term->profile, term->name, 0x00 ); + } + break; + } + default: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," Invalid term_state[%d]\n", term_state); + return SWITCH_STATUS_FALSE; + } + } + + return ret; +} +/*****************************************************************************************************************************/ +/* Note : API to send Service Change when termination is coming up(in-service) */ +/* INPUT : MG Profile structure and termination name */ +/* wild flag will tell if service change request needs to be in W-SC format as we can have W-SC=A01* or SC=A01* */ +switch_status_t mg_send_ins_service_change(megaco_profile_t* mg_profile, const char* term_name, int wild) +{ + switch_assert(term_name); + switch_assert(mg_profile); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Sending In-Service Service Change for termination[%s] configured in mg profile[%s], suId[%d]\n", + term_name, mg_profile->name, mg_profile->idx); + + mg_profile->mg_stats->total_num_of_term_in_service_change_sent++; + return mg_send_service_change(mg_profile->idx, term_name, MGT_SVCCHGMETH_RESTART, MG_SVC_REASON_900_RESTORED, wild); +} + +/*****************************************************************************************************************************/ +/* Note : API to send Service Change when termination is going down (out-of-service) */ +/* INPUT : MG Profile structure and termination name */ +/* wild flag will tell if service change request needs to be in W-SC format as we can have W-SC=A01* or SC=A01* */ +switch_status_t mg_send_oos_service_change(megaco_profile_t* mg_profile, const char* term_name, int wild) +{ + switch_assert(term_name); + switch_assert(mg_profile); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Sending Out-Of-Service Service Change for termination[%s] configured in mg profile[%s], suId[%d]\n", + term_name, mg_profile->name, mg_profile->idx); + + mg_profile->mg_stats->total_num_of_term_oos_service_change_sent++; + + return mg_send_service_change(mg_profile->idx, term_name, MGT_SVCCHGMETH_FORCED, MG_SVC_REASON_905_TERM_OOS, wild); +} +/*****************************************************************************************************************************/ +/* Note : API to send Service Change */ +/* INPUT : +* method - Service change method type (can be MGT_SVCCHGMETH_RESTART/MGT_SVCCHGMETH_FORCED (please refer to sng_ss7/cm/mgt.h for more values)) +* MgServiceChangeReason_e - Service Change reason +* SuId - Service User ID for MG SAP - it will be same like mg_profile_t->idx (refer to media_gateway_xml.c->mg_sap_id) +* term_name - String format defined termination name +*/ +switch_status_t mg_send_service_change(SuId suId, const char* term_name, uint8_t method, MgServiceChangeReason_e reason,uint8_t wild) +{ + MgMgcoTermId* termId; + switch_status_t ret; + MgMgcoCommand request; + MgMgcoSvcChgReq *svc; + + MG_ZERO(&request, sizeof(request)); + + + + if(SWITCH_STATUS_FALSE == (ret = mg_create_mgco_command(&request, CH_CMD_TYPE_REQ, MGT_SVCCHG))){ + goto err; + } + + /*fill txn id */ + request.transId.pres = PRSNT_NODEF; + request.transId.val = get_txn_id(); + + request.contextId.type.pres = PRSNT_NODEF; + request.contextId.type.val = MGT_CXTID_NULL; + +#if 0 + /* TODO - fill of below fields */ +#ifdef GCP_MGCO +#ifdef GCP_VER_2_1 + MgMgcoSegNum segNum; + MgMgcoSegCmpl segCmpl; +#endif +#endif /* GCP_MGCO */ +#endif + request.cmdStatus.pres = PRSNT_NODEF; + request.cmdStatus.val = CH_CMD_STATUS_END_OF_TXN; + + request.cmdType.pres = PRSNT_NODEF; + request.cmdType.val = CH_CMD_TYPE_REQ; + + svc = &request.u.mgCmdReq[0]->cmd.u.svc; + + if(SWITCH_STATUS_FALSE == (ret = mg_fill_svc_change(&svc->parm, method, mg_service_change_reason[reason], &request.u.mgCmdReq[0]->memCp))){ + return ret; + } + + if (mgUtlGrowList((void ***)&svc->termIdLst.terms, sizeof(MgMgcoTermIdLst), + &svc->termIdLst.num, &request.u.mgCmdReq[0]->memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + goto err; + } + +#ifdef GCP_VER_2_1 + termId = svc->termIdLst.terms[0]; +#else + termId = &(svc->termId); +#endif + + mg_fill_mgco_termid(termId, (char*)term_name ,strlen(term_name), &request.u.mgCmdReq[0]->memCp); + + if(wild){ + request.u.mgCmdReq[0]->wild.pres = PRSNT_NODEF; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Sending %s Service Change for termId[%s] with reason[%s], len[%d]\n", + ((1==wild)?"WildCard":"Non Wild Card"), term_name, svc->parm.reason.val, svc->parm.reason.len); + + sng_mgco_send_cmd(suId, &request); + + return SWITCH_STATUS_SUCCESS; + +err: + mgUtlDelMgMgcoSvcChgPar(&svc->parm); + return SWITCH_STATUS_FALSE; +} + +/*****************************************************************************************************************************/ +/* API to send In-Activity Timeout NOTIFY to MGC */ +switch_status_t mg_send_ito_notify(megaco_profile_t* mg_profile ) +{ + MgMgcoObsEvt *oevt; + + switch_assert(mg_profile); + + mg_stack_alloc_mem((Ptr*)&oevt, sizeof(MgMgcoObsEvt)); + + oevt->pres.pres = PRSNT_NODEF; + + mg_get_time_stamp(&oevt->time); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.pkgType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.valType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.u.val), MGT_PKG_INACTTIMER); + + MG_INIT_TOKEN_VALUE(&(oevt->name.type),MGT_GEN_TYPE_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->name.u.val),(U8)MGT_PKG_ENUM_REQEVT_INACTTIMER_INACT_TIMOUT); + + oevt->pl.num.pres = PRSNT_NODEF; + oevt->pl.num.val = 0x00; + + return mg_send_notify(mg_profile, "ROOT", oevt); +} + +/*****************************************************************************************************************************/ +/* API to send T.38 CNG tone Notification */ +switch_status_t mg_send_t38_fax_con_change_notify(megaco_profile_t* mg_profile, const char* term_name, char* state) +{ + MgMgcoObsEvt *oevt; + MgMgcoEvtPar* param; + + switch_assert(term_name); + switch_assert(mg_profile); + switch_assert(state); + + mg_stack_alloc_mem((Ptr*)&oevt, sizeof(MgMgcoObsEvt)); + + oevt->pres.pres = PRSNT_NODEF; + + mg_get_time_stamp(&oevt->time); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.pkgType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.valType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.u.val), MGT_PKG_FAX); + + MG_INIT_TOKEN_VALUE(&(oevt->name.type),MGT_GEN_TYPE_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->name.u.val),(U8)MGT_PKG_ENUM_REQEVTIPFAXFAXCONNSTATECHNG); + + if (mgUtlGrowList((void ***)&oevt->pl.parms, sizeof(MgMgcoEvtPar), &oevt->pl.num, NULL) != ROK) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + param = oevt->pl.parms[0]; + + MG_INIT_TOKEN_VALUE(&(param->type),(U8)MGT_EVTPAR_OTHER); + + MG_INIT_TOKEN_VALUE(&(param->u.other.name.type),MGT_GEN_TYPE_KNOWN); + MG_INIT_TOKEN_VALUE(&(param->u.other.name.u.val), MGT_PKG_ENUM_OBSEVTOTHERIPFAXFAXCONNSTATECHNGFAXCONNSTATECHNG); + + MG_INIT_TOKEN_VALUE(&(param->u.other.val.type),MGT_VALUE_EQUAL); + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.type),MGT_VALTYPE_ENUM); + if(!strcasecmp(state,"connected")){ + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERIPFAXFAXCONNSTATECHNGFAXCONNSTATECHNGCONNECTED); + } else if(!strcasecmp(state,"negotiating")){ + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERIPFAXFAXCONNSTATECHNGFAXCONNSTATECHNGNEGOTIATING); + } else if(!strcasecmp(state,"disconnect")){ + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERIPFAXFAXCONNSTATECHNGFAXCONNSTATECHNGDISCONNECT); + } else if(!strcasecmp(state,"prepare")){ + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERIPFAXFAXCONNSTATECHNGFAXCONNSTATECHNGPREPARE); + } else if(!strcasecmp(state,"TrainR")){ + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERIPFAXFAXCONNSTATECHNGFAXCONNSTATECHNGTRAINR); + } else if(!strcasecmp(state,"TrainT")){ + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERIPFAXFAXCONNSTATECHNGFAXCONNSTATECHNGTRAINT); + } else if(!strcasecmp(state,"EOP")){ + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERIPFAXFAXCONNSTATECHNGFAXCONNSTATECHNGEOP); + } else if(!strcasecmp(state,"ProcInterrupt")){ + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERIPFAXFAXCONNSTATECHNGFAXCONNSTATECHNGPROCINTR); + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Invalid input state[%s] param\n", state); + return SWITCH_STATUS_FALSE; + } + + return mg_send_notify(mg_profile, term_name, oevt); +} +switch_status_t mg_send_t38_v21flag_notify(megaco_profile_t* mg_profile, const char* term_name) +{ + MgMgcoObsEvt *oevt; + MgMgcoEvtPar* param; + + switch_assert(term_name); + switch_assert(mg_profile); + + mg_stack_alloc_mem((Ptr*)&oevt, sizeof(MgMgcoObsEvt)); + + oevt->pres.pres = PRSNT_NODEF; + + mg_get_time_stamp(&oevt->time); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.pkgType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.valType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.u.val), MGT_PKG_CALL_TYP_DISCR); + + MG_INIT_TOKEN_VALUE(&(oevt->name.type),MGT_GEN_TYPE_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->name.u.val),(U8)MGT_PKG_ENUM_OBSEVTOTHERCALLTYPDISCRDISCTONEDETDISCTONETYPCNG); + + if (mgUtlGrowList((void ***)&oevt->pl.parms, sizeof(MgMgcoEvtPar), &oevt->pl.num, NULL) != ROK) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + param = oevt->pl.parms[0]; + + MG_INIT_TOKEN_VALUE(&(param->type),(U8)MGT_EVTPAR_OTHER); + + MG_INIT_TOKEN_VALUE(&(param->u.other.name.type),MGT_GEN_TYPE_KNOWN); + MG_INIT_TOKEN_VALUE(&(param->u.other.name.u.val), MGT_PKG_ENUM_OBSEVTOTHERCALLTYPDISCRDISCTONEDETDISCTONETYP); + + MG_INIT_TOKEN_VALUE(&(param->u.other.val.type),MGT_VALUE_EQUAL); + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.type),MGT_VALTYPE_ENUM); + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERCALLTYPDISCRDISCTONEDETDISCTONETYPV21FLG); + + + return mg_send_notify(mg_profile, term_name, oevt); +} + +switch_status_t mg_send_t38_ans_notify(megaco_profile_t* mg_profile, const char* term_name) +{ + MgMgcoObsEvt *oevt; + MgMgcoEvtPar* param; + + switch_assert(term_name); + switch_assert(mg_profile); + + mg_stack_alloc_mem((Ptr*)&oevt, sizeof(MgMgcoObsEvt)); + + oevt->pres.pres = PRSNT_NODEF; + + mg_get_time_stamp(&oevt->time); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.pkgType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.valType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.u.val), MGT_PKG_CALL_TYP_DISCR); + + MG_INIT_TOKEN_VALUE(&(oevt->name.type),MGT_GEN_TYPE_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->name.u.val),(U8)MGT_PKG_ENUM_OBSEVTOTHERCALLTYPDISCRDISCTONEDETDISCTONETYPCNG); + + if (mgUtlGrowList((void ***)&oevt->pl.parms, sizeof(MgMgcoEvtPar), &oevt->pl.num, NULL) != ROK) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + param = oevt->pl.parms[0]; + + MG_INIT_TOKEN_VALUE(&(param->type),(U8)MGT_EVTPAR_OTHER); + + MG_INIT_TOKEN_VALUE(&(param->u.other.name.type),MGT_GEN_TYPE_KNOWN); + MG_INIT_TOKEN_VALUE(&(param->u.other.name.u.val), MGT_PKG_ENUM_OBSEVTOTHERCALLTYPDISCRDISCTONEDETDISCTONETYP); + + MG_INIT_TOKEN_VALUE(&(param->u.other.val.type),MGT_VALUE_EQUAL); + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.type),MGT_VALTYPE_ENUM); + //MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERCALLTYPDISCRDISCTONEDETDISCTONETYPANSBAR); + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERCALLTYPDISCRDISCTONEDETDISCTONETYPANSAM); + + + return mg_send_notify(mg_profile, term_name, oevt); +} + +switch_status_t mg_send_t38_cng_notify(megaco_profile_t* mg_profile, const char* term_name) +{ + MgMgcoObsEvt *oevt; + MgMgcoEvtPar* param; + + switch_assert(term_name); + switch_assert(mg_profile); + + + mg_stack_alloc_mem((Ptr*)&oevt, sizeof(MgMgcoObsEvt)); + + oevt->pres.pres = PRSNT_NODEF; + + mg_get_time_stamp(&oevt->time); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.pkgType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.valType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.u.val), MGT_PKG_CALL_TYP_DISCR); + + MG_INIT_TOKEN_VALUE(&(oevt->name.type),MGT_GEN_TYPE_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->name.u.val),(U8)MGT_PKG_ENUM_OBSEVTOTHERCALLTYPDISCRDISCTONEDETDISCTONETYPCNG); + + if (mgUtlGrowList((void ***)&oevt->pl.parms, sizeof(MgMgcoEvtPar), &oevt->pl.num, NULL) != ROK) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + param = oevt->pl.parms[0]; + + MG_INIT_TOKEN_VALUE(&(param->type),(U8)MGT_EVTPAR_OTHER); + + MG_INIT_TOKEN_VALUE(&(param->u.other.name.type),MGT_GEN_TYPE_KNOWN); + MG_INIT_TOKEN_VALUE(&(param->u.other.name.u.val), MGT_PKG_ENUM_OBSEVTOTHERCALLTYPDISCRDISCTONEDETDISCTONETYP); + + MG_INIT_TOKEN_VALUE(&(param->u.other.val.type),MGT_VALUE_EQUAL); + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.type),MGT_VALTYPE_ENUM); + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHERCALLTYPDISCRDISCTONEDETDISCTONETYPCNG); + + return mg_send_notify(mg_profile, term_name, oevt); +} +/*****************************************************************************************************************************/ +/* API to send DTMF Digits Notification */ + +switch_status_t mg_send_dtmf_notify(megaco_profile_t* mg_profile, const char* term_name, char* digits, int num_of_collected_digits ) +{ + MgMgcoObsEvt *oevt; + MgMgcoEvtPar* param; + int ascii = 0x00; + int cnt = 0x00; + + switch_assert(term_name); + switch_assert(mg_profile); + switch_assert(digits); + + if(0 == num_of_collected_digits ){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"num_of_collected_digits cannt be ZERO \n"); + return SWITCH_STATUS_FALSE; + } + + mg_stack_alloc_mem((Ptr*)&oevt, sizeof(MgMgcoObsEvt)); + + oevt->pres.pres = PRSNT_NODEF; + + mg_get_time_stamp(&oevt->time); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.pkgType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.valType), MGT_PKG_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->pkg.u.val), MGT_PKG_EXT_DTMF); + + MG_INIT_TOKEN_VALUE(&(oevt->name.type),MGT_GEN_TYPE_KNOWN); + + MG_INIT_TOKEN_VALUE(&(oevt->name.u.val),(U8)MGT_PKG_ENUM_REQEVT_EXT_DTMF_EXT_CE); + + if (mgUtlGrowList((void ***)&oevt->pl.parms, sizeof(MgMgcoEvtPar), &oevt->pl.num, NULL) != ROK) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + param = oevt->pl.parms[0]; + + MG_INIT_TOKEN_VALUE(&(param->type),(U8)MGT_EVTPAR_OTHER); + + MG_INIT_TOKEN_VALUE(&(param->u.other.name.type),MGT_GEN_TYPE_KNOWN); + MG_INIT_TOKEN_VALUE(&(param->u.other.name.u.val),MGT_PKG_ENUM_OBSEVTOTHER_EXT_DTMF_EXT_CE_DGT_STR); + + MG_INIT_TOKEN_VALUE(&(param->u.other.val.type),MGT_VALUE_EQUAL); + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.type),MGT_VALTYPE_OCTSTRXL); + + mg_stack_alloc_mem((Ptr*)¶m->u.other.val.u.eq.u.osxl.val, num_of_collected_digits+2); + + param->u.other.val.u.eq.u.osxl.pres = 0x01; + param->u.other.val.u.eq.u.osxl.len = num_of_collected_digits+2; + + param->u.other.val.u.eq.u.osxl.val[0] = '\"'; + + /* copy collected DTMF digits */ + if(ascii) + { + for(cnt = 1; cnt< num_of_collected_digits; cnt++){ + /* convert values to ascii */ + if(digits[cnt-1] <= 9){ + param->u.other.val.u.eq.u.osxl.val[cnt] = digits[cnt-1] + '0'; + }else{ + /* 10 for decimal equivalent of A */ + param->u.other.val.u.eq.u.osxl.val[cnt] = digits[cnt-1] + 'A' - 10; + } + } + } else { + /* If incoming digits are already in ascii format .. simply copy */ + for(cnt = 1; cnt< num_of_collected_digits; cnt++){ + param->u.other.val.u.eq.u.osxl.val[cnt] = digits[cnt-1]; + } + } + + + param->u.other.val.u.eq.u.osxl.val[num_of_collected_digits+1] = '\"'; + + if (mgUtlGrowList((void ***)&oevt->pl.parms, sizeof(MgMgcoEvtPar), &oevt->pl.num, NULL) != ROK) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + param = oevt->pl.parms[1]; + + + /* Set method */ + MG_INIT_TOKEN_VALUE(&(param->type),MGT_EVTPAR_OTHER); + MG_INIT_TOKEN_VALUE(&(param->u.other.name.type),MGT_GEN_TYPE_KNOWN); + MG_INIT_TOKEN_VALUE(&(param->u.other.name.u.val),MGT_PKG_ENUM_OBSEVTOTHER_EXT_DTMF_EXT_CE_TERM_METH); + + MG_INIT_TOKEN_VALUE(&(param->u.other.val.type),MGT_VALUE_EQUAL); + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.type),MGT_VALTYPE_ENUM); + MG_INIT_TOKEN_VALUE(&(param->u.other.val.u.eq.u.enume),MGT_PKG_ENUM_OBSEVTOTHER_EXT_DTMF_EXT_CE_TERM_METHFM); + + return mg_send_notify(mg_profile, term_name, oevt); +} + +/*****************************************************************************************************************************/ + +/* Note : API to send NOTIFY Message */ +/* INPUT : +* mg_profile - MG profile structure pointer +* term_name - Termination Name(as specified for MEGACO ) +* oevt - Observed Event structure pointer +*/ +switch_status_t mg_send_notify(megaco_profile_t* mg_profile, const char* term_name, MgMgcoObsEvt* oevt) +{ + switch_status_t ret; + MgMgcoCommand request; + MgMgcoTermId* termId; + mg_termination_t* term = NULL; + MgMgcoObsEvtDesc *obs_desc = NULL; + MgMgcoRequestId reqId; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Sending Notify Message for termination[%s] !\n", term_name); + + MG_ZERO(&request, sizeof(request)); + MG_ZERO(&reqId, sizeof(reqId)); + + if(strcmp(term_name, "ROOT")){ + /* Not ROOT term then --- */ + term = megaco_find_termination(mg_profile, (char*)term_name); + + if(!term){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No termination configured for given name[%s] !\n", term_name); + return SWITCH_STATUS_FALSE; + } + + if(NULL == term->active_events){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No Active events observed on given termination[%s] !\n", term_name); + /* return SWITCH_STATUS_FALSE; */ + /*TODO - ideally we should return ... + * as of now not returning .. if we dont have active signals then + * setting default request id and sending notification to MGC */ + MG_SET_DEF_REQID(&reqId); + }else{ + MG_MEM_COPY(&reqId, &term->active_events->reqId, sizeof(MgMgcoRequestId)); + } + }else{ + MG_SET_DEF_REQID(&reqId); + } + + + + if(SWITCH_STATUS_FALSE == (ret = mg_create_mgco_command(&request, CH_CMD_TYPE_REQ, MGT_NTFY))){ + return SWITCH_STATUS_FALSE; + } + + if (mgUtlGrowList((void ***)&request.u.mgCmdReq[0]->cmd.u.ntfy.obs.el.evts, sizeof(MgMgcoObsEvtLst), + &request.u.mgCmdReq[0]->cmd.u.ntfy.obs.el.num, &request.u.mgCmdReq[0]->memCp) != ROK) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + obs_desc = &request.u.mgCmdReq[0]->cmd.u.ntfy.obs; + + MG_INIT_TOKEN_VALUE(&(obs_desc->pres), PRSNT_NODEF); + + /* copy request id */ + MG_MEM_COPY(&obs_desc->reqId, &reqId, sizeof(MgMgcoRequestId)); + + /* copy observe event */ + /*MG_MEM_COPY(obs_desc->el.evts[0], oevt, sizeof(MgMgcoObsEvt));*/ + + obs_desc->el.evts[0] = oevt; + + + /*fill txn id */ + request.transId.pres = PRSNT_NODEF; + request.transId.val = get_txn_id(); + + request.contextId.type.pres = PRSNT_NODEF; + if(term && term->context){ + printf("Temrination is in context, adding context-id[%d]\n",term->context->context_id); + request.contextId.type.val = MGT_CXTID_OTHER; + request.contextId.val.pres = PRSNT_NODEF; + request.contextId.val.val = term->context->context_id; + } else{ + request.contextId.type.val = MGT_CXTID_NULL; + } + request.cmdStatus.pres = PRSNT_NODEF; + request.cmdStatus.val = CH_CMD_STATUS_END_OF_TXN; + + request.cmdType.pres = PRSNT_NODEF; + request.cmdType.val = CH_CMD_TYPE_REQ; + + /* fill termination */ + if (mgUtlGrowList((void ***)&request.u.mgCmdReq[0]->cmd.u.ntfy.termIdLst.terms, sizeof(MgMgcoTermIdLst), + &request.u.mgCmdReq[0]->cmd.u.ntfy.termIdLst.num, &request.u.mgCmdReq[0]->memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + +#ifdef GCP_VER_2_1 + termId = request.u.mgCmdReq[0]->cmd.u.ntfy.termIdLst.terms[0]; +#else + termId = &(request.u.mgCmdReq[0]->cmd.u.ntfy.termId); +#endif + + mg_fill_mgco_termid(termId, (char*)term_name ,strlen(term_name), &request.u.mgCmdReq[0]->memCp); + + sng_mgco_send_cmd(mg_profile->idx, &request); + + return SWITCH_STATUS_SUCCESS; +} +/*****************************************************************************************************************************/ + diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway_packages.c b/src/mod/endpoints/mod_media_gateway/media_gateway_packages.c new file mode 100644 index 0000000000..fc86096335 --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/media_gateway_packages.c @@ -0,0 +1,181 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Mathieu Rene +* All rights reserved. +* +* +*/ + +#include "mod_media_gateway.h" +#include "media_gateway_stack.h" + + +MgPackage_t mg_pkg_list [] = +{ + { /* INDEX : 0 */ + MGT_PKG_GENERIC, /* Package Id 1 : Generic package */ + 1, /* Version 1 */ + "g", /* Package name */ + }, + { /* INDEX : 1 */ + MGT_PKG_ROOT, + 2, /* Version 1 */ + "root", /* Package name */ + }, + { /* INDEX : 2 */ + MGT_PKG_TONEDET, /*4*/ + 1, /* Version 1 */ + "tonedet", /* Package name */ + }, + { /* INDEX : 3 */ + MGT_PKG_DTMFDET, /*6*/ + 1, /* Version 1 */ + "dd", /* Package name */ + }, + { /* INDEX : 4 */ + MGT_PKG_NETWORK, /*11*/ + 1, /* Version 1 */ + "nt", /* Package name */ + }, + { /* INDEX : 5 */ + MGT_PKG_RTP, /*12*/ + 1, /* Version 1 */ + "rtp", /* Package name */ + }, + { /* INDEX : 6 */ + MGT_PKG_TDM_CKT, /*13*/ + 1, /* Version 1 */ + "tdmc", /* Package name */ + }, + /* TODO - not sure IF we need this */ + { /* INDEX : 7 */ + MGT_PKG_SEGMENTATION, + 1, /* Version 1 */ + "seg", /* Package name */ + }, + { /* INDEX : 8 */ + MGT_PKG_EN_ALERT, /*59*/ + 2, /* Version 2 */ + "alert", /* Package name */ + }, + { /* INDEX : 9 */ + MGT_PKG_CONTINUITY, /*60*/ + 2, /* Version 1 */ + "ct", /* Package name */ + }, + { /* INDEX : 10 */ + MGT_PKG_INACTTIMER, /*69*/ + 1, /* Version 1 */ + "it", /* Package name */ + }, + { /* INDEX : 11 */ + MGT_PKG_STIMAL, /* 147 */ + 1, /* Version 1 */ + "stimal ", /* Package name */ + }, + { /* INDEX : 12 */ + MGT_PKG_CALLPROGGEN, /* 7 */ + 1, /* Version 1 */ + "cg", /* Package name */ + }, + { /* INDEX : 13 */ + MGT_PKG_GENERIC_ANNC, /* 29 */ + 1, /* Version 1 */ + "an", /* Package name */ + }, + { /* INDEX : 14 */ + MGT_PKG_XD_CALPG_TNGN, /* 36 */ + 1, /* Version 1 */ + "xcg", /* Package name */ + }, + { /* INDEX : 15 */ + MGT_PKG_BSC_SRV_TN, /* 37 */ + 1, /* Version 1 */ + "srvtn", /* Package name */ + }, + { /* INDEX : 16 */ + MGT_PKG_ETSI_NR, + 1, /* Version 1 */ + "etsi_nr", /* Package name */ + }, + { /* INDEX : 17 */ + MGT_PKG_TONEGEN, + 1, /* Version 1 */ + "tonegen", /* Package name */ + }, + { /* INDEX : 18 */ + MGT_PKG_DTMFGEN, + 1, /* Version 1 */ + "tonegen", /* Package name */ + }, + { /* INDEX : 19 */ + MGT_PKG_CALLPROGGEN, + 1, /* Version 1 */ + "tonegen", /* Package name */ + }, + { /* INDEX : 20 */ + MGT_PKG_CALLPROGDET, + 1, /* Version 1 */ + "tonedet", /* Package name */ + }, + { /* INDEX : 21 */ + MGT_PKG_ANALOG, + 1, /* Version 1 */ + "analog", /* Package name */ + }, + { /* INDEX : 22 */ + MGT_PKG_FAX_TONE_DET, + 1, /* Version 1 */ + "ftmd", /* Package name */ + }, + { /* INDEX : 24 */ + MGT_PKG_CALL_TYP_DISCR, + 1, /* Version 1 */ + "ctype", /* Package name */ + }, + { /* INDEX : 25 */ + MGT_PKG_IP_FAX, + 1, /* Version 1 */ + "ipfax", /* Package name */ + }, + { /* INDEX : 26 */ + MGT_PKG_FAX, + 1, /* Version 1 */ + "fax", /* Package name */ + }, + /* Add more packages */ +}; + +/***************************************************************************************/ +switch_status_t mg_build_pkg_desc(MgMgcoPkgsDesc* pkg, CmMemListCp *memCp) +{ + uint16_t i = 0x00; + uint16_t num_of_pkgs = sizeof(mg_pkg_list)/sizeof(MgPackage_t); + + printf("mg_build_pkg_desc: num_of_pkgs[%d]\n",num_of_pkgs); + + for (i = 0; i < num_of_pkgs; i++) { + + if (mgUtlGrowList((void ***)&pkg->items, + sizeof(MgMgcoPkgsItem), &pkg->num, memCp) != ROK) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Package descriptor Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + pkg->items[pkg->num.val - 1 ]->pres.pres = PRSNT_NODEF; + pkg->items[pkg->num.val - 1 ]->name.type.pres = PRSNT_NODEF; + pkg->items[pkg->num.val - 1 ]->name.type.val = MGT_GEN_TYPE_KNOWN; + + pkg->items[pkg->num.val - 1 ]->name.u.val.pres = PRSNT_NODEF; + pkg->items[pkg->num.val - 1 ]->name.u.val.val = mg_pkg_list[i].package_id; + + pkg->items[pkg->num.val - 1 ]->ver.pres = PRSNT_NODEF; + pkg->items[pkg->num.val - 1 ]->ver.val = mg_pkg_list[i].version; + + printf("mg_build_pkg_desc: Inserted pkg_id[%d] with version[%d] into pkg list index[%d]\n",mg_pkg_list[i].package_id,mg_pkg_list[i].version,i); + } + + return SWITCH_STATUS_SUCCESS; +} +/***************************************************************************************/ + diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway_stack.c b/src/mod/endpoints/mod_media_gateway/media_gateway_stack.c new file mode 100644 index 0000000000..4030104c23 --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/media_gateway_stack.c @@ -0,0 +1,1401 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Kapil Gupta +* All rights reserved. +* +* +*/ + +/* INCLUDES *******************************************************************/ +#include "mod_media_gateway.h" +#include "media_gateway_stack.h" +/******************************************************************************/ + +/* DEFINES ********************************************************************/ + +/* FUNCTION PROTOTYPES ********************************************************/ +int mgco_mg_gen_config(void); +int mgco_mu_gen_config(void); +int mgco_tucl_gen_config(void); +int mgco_mu_ssap_config(int idx); +int mgco_mg_tsap_config(megaco_profile_t* profile); +int mgco_mg_ssap_config(megaco_profile_t* profile); +int mgco_mg_peer_config(megaco_profile_t* profile); +int mgco_mg_tpt_server_config(megaco_profile_t* profile); +int mgco_tucl_sap_config(int idx); + +int mgco_mg_tsap_bind_cntrl(int idx); +int mgco_mg_tsap_enable_cntrl(int idx); +int mgco_mg_ssap_cntrl(int idx); +int mgco_mu_ssap_cntrl(int idx); +int sng_mgco_tucl_shutdown(); +int sng_mgco_mg_shutdown(); +int sng_mgco_mg_ssap_stop(int sapId); +int sng_mgco_mg_tpt_server_stop(megaco_profile_t* profile); +int sng_mgco_mg_app_ssap_stop(int idx); +int mg_tucl_debug(int action); + +switch_status_t sng_mgco_stack_gen_cfg(); + + +sng_mg_transport_types_e mg_get_tpt_type(megaco_profile_t* mg_cfg); +sng_mg_transport_types_e mg_get_tpt_type_from_str(char* tpt_type); +sng_mg_encoding_types_e mg_get_enc_type_from_str(char* enc_type); +sng_mg_protocol_types_e mg_get_proto_type_from_str(char* proto_type); + +/******************************************************************************/ + +/* FUNCTIONS ******************************************************************/ + +switch_status_t sng_mgco_init(sng_mg_event_interface_t* event) +{ + uint32_t major, minor, build; + + switch_assert(event); + + /* initalize sng_mg library */ + sng_mg_init_gen(event); + + /* print the version of the library being used */ + sng_mg_version(&major, &minor, &build); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Loaded LibSng-MEGACO %d.%d.%d\n", major, minor, build); + + /* start up the stack manager */ + if (sng_mg_init_sm()) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Failed to start Stack Manager\n"); + return SWITCH_STATUS_FALSE; + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Started Stack Manager!\n"); + } + + if (sng_mg_init_tucl()) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Failed to start TUCL\n"); + return SWITCH_STATUS_FALSE; + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Started TUCL!\n"); + } + + if (sng_mg_init_mg()) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Failed to start MG\n"); + return SWITCH_STATUS_FALSE; + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Started MG!\n"); + } + + if (sng_mg_init_mu()) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Failed to start MU\n"); + return SWITCH_STATUS_FALSE; + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Started MU!\n"); + } + + + /* gen config for all the layers of megaco */ + return sng_mgco_stack_gen_cfg(); +} + +/*****************************************************************************************************************/ +switch_status_t sng_mgco_stack_shutdown() +{ + /* disable MG logging */ + mg_disable_logging(); + + /* shutdown MG */ + sng_mgco_mg_shutdown(); + + /* shutdown TUCL */ + sng_mgco_tucl_shutdown(); + + /* free MEGACO Application */ + sng_mg_free_mu(); + + /* free MEGACO */ + sng_mg_free_mg(); + + /* free TUCL */ + sng_mg_free_tucl(); + + /* free SM */ + sng_mg_free_sm(); + + /* free gen */ + sng_mg_free_gen(); + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************/ +switch_status_t sng_mgco_stack_gen_cfg() +{ + if(mgco_mg_gen_config()) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"MG Gen Config FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"MG Gen Config SUCCESS \n"); + } + + if(mgco_mu_gen_config()) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"MU(MG-Application) Gen Config FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"MU(MG-Application) Gen Config SUCCESS \n"); + } + + if(mgco_tucl_gen_config()) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," TUCL Gen Config FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," TUCL Gen Config SUCCESS \n"); + } + + return SWITCH_STATUS_SUCCESS; +} + + +/*****************************************************************************************************************/ + +switch_status_t sng_mgco_cfg(megaco_profile_t* profile) +{ + int idx = 0x00; + + switch_assert(profile); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Starting MG configuration for idx[%d] against profilename[%s]\n", profile->idx, profile->name); + + idx = profile->idx; + + if(mgco_tucl_sap_config(idx)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," mgco_tucl_sap_config FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," mgco_tucl_sap_config SUCCESS \n"); + } + + + if(mgco_mu_ssap_config(idx)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," mgco_mu_ssap_config FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," mgco_mu_ssap_config SUCCESS \n"); + } + + if(mgco_mg_tsap_config(profile)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR," mgco_mg_tsap_config FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," mgco_mg_tsap_config SUCCESS \n"); + } + + if(mgco_mg_ssap_config(profile)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " mgco_mg_ssap_config FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " mgco_mg_ssap_config SUCCESS \n"); + } + + if(mgco_mg_peer_config(profile)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " mgco_mg_peer_config FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " mgco_mg_peer_config SUCCESS \n"); + } + + if(mgco_mg_tpt_server_config(profile)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " mgco_mg_tpt_server_config FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " mgco_mg_tpt_server_config SUCCESS \n"); + } + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************/ + +switch_status_t sng_mgco_start(megaco_profile_t* profile ) +{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Starting MG stack for idx[%d] against profilename[%s]\n", profile->idx, profile->name); + + if(mgco_mu_ssap_cntrl(profile->idx)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " mgco_mu_ssap_cntrl FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " mgco_mu_ssap_cntrl SUCCESS \n"); + } + + if(mgco_mg_tsap_bind_cntrl(profile->idx)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " mgco_mg_tsap_bind_cntrl FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " mgco_mg_tsap_bind_cntrl SUCCESS \n"); + } + + if(mgco_mg_ssap_cntrl(profile->idx)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " mgco_mg_ssap_cntrl FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " mgco_mg_ssap_cntrl SUCCESS \n"); + } + + if(mgco_mg_tsap_enable_cntrl(profile->idx)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " mgco_mg_tsap_enable_cntrl FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " mgco_mg_tsap_enable_cntrl SUCCESS \n"); + } + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************/ + +switch_status_t sng_mgco_stop(megaco_profile_t* profile ) +{ + int idx = 0x00; + + switch_assert(profile); + + idx = profile->idx; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," Stopping MG stack for idx[%d] against profilename[%s]\n", idx, profile->name); + + /* MG STOP is as good as deleting that perticular mg(virtual mg instance) data from megaco stack */ + /* currently we are not supporting enable/disable MG stack */ + + if(sng_mgco_mg_ssap_stop(idx)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " sng_mgco_mg_ssap_stop FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " sng_mgco_mg_ssap_stop SUCCESS \n"); + } + + if(sng_mgco_mg_tpt_server_stop(profile)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " sng_mgco_mg_tpt_server_stop FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " sng_mgco_mg_tpt_server_stop SUCCESS \n"); + } + + if(sng_mgco_mg_app_ssap_stop(idx)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " sng_mgco_mg_app_ssap_stop FAILED \n"); + return SWITCH_STATUS_FALSE; + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " sng_mgco_mg_app_ssap_stop SUCCESS \n"); + } + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************/ +int sng_mgco_mg_app_ssap_stop(int idx) +{ + MuMngmt mgMngmt; + Pst pst; /* Post for layer manager */ + MuCntrl *cntrl; + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + + cntrl = &(mgMngmt.t.cntrl); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMU; + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCNTRL; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STSSAP; + mgMngmt.hdr.elmId.elmntInst1 = idx; + + cntrl->action = ADEL; + cntrl->subAction = SAELMNT; + + return(sng_cntrl_mu(&pst, &mgMngmt)); +} +/*****************************************************************************************************************/ + +int sng_mgco_mg_ssap_stop(int sapId) +{ + Pst pst; + MgMngmt cntrl; + + memset((U8 *)&pst, 0, sizeof(Pst)); + memset((U8 *)&cntrl, 0, sizeof(MgCntrl)); + + smPstInit(&pst); + + pst.dstEnt = ENTMG; + + /* prepare header */ + cntrl.hdr.msgType = TCNTRL; /* message type */ + cntrl.hdr.entId.ent = ENTMG; /* entity */ + cntrl.hdr.entId.inst = 0; /* instance */ + cntrl.hdr.elmId.elmnt = STSSAP; /* SSAP */ + cntrl.hdr.elmId.elmntInst1 = sapId; /* sap id */ + + cntrl.hdr.response.selector = 0; + cntrl.hdr.response.prior = PRIOR0; + cntrl.hdr.response.route = RTESPEC; + cntrl.hdr.response.mem.region = S_REG; + cntrl.hdr.response.mem.pool = S_POOL; + + cntrl.t.cntrl.action = ADEL; + cntrl.t.cntrl.subAction = SAELMNT; + cntrl.t.cntrl.spId = sapId; + return (sng_cntrl_mg (&pst, &cntrl)); +} + +/*****************************************************************************************************************/ +int sng_mgco_mg_tpt_server_stop(megaco_profile_t* mg_cfg) +{ + MgMngmt mgMngmt; + Pst pst; /* Post for layer manager */ + MgCntrl *cntrl; + MgTptCntrl *tptCntrl = &mgMngmt.t.cntrl.s.tptCntrl; + CmInetIpAddr ipAddr = 0; + + cntrl = &(mgMngmt.t.cntrl); + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + + tptCntrl->transportType = mg_get_tpt_type(mg_cfg); + + tptCntrl->serverAddr.type = CM_INET_IPV4ADDR_TYPE; + tptCntrl->serverAddr.u.ipv4TptAddr.port = atoi(mg_cfg->port); + if(ROK == cmInetAddr((S8*)mg_cfg->my_ipaddr, &ipAddr)) + { + tptCntrl->serverAddr.u.ipv4TptAddr.address = ntohl(ipAddr); + } + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCNTRL; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STSERVER; + + cntrl->action = ADEL; + cntrl->subAction = SAELMNT; + + return(sng_cntrl_mg(&pst, &mgMngmt)); +} +/*****************************************************************************************************************/ + +int mgco_mg_tsap_bind_cntrl(int idx) +{ + MgMngmt mgMngmt; + Pst pst; /* Post for layer manager */ + MgCntrl *cntrl; + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + + cntrl = &(mgMngmt.t.cntrl); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCNTRL; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STTSAP; + + cntrl->action = ABND_ENA; + cntrl->subAction = SAELMNT; + cntrl->spId = idx; + + return(sng_cntrl_mg(&pst, &mgMngmt)); +} + +/*****************************************************************************************************************/ + +int mgco_mg_tsap_enable_cntrl(int idx) +{ + MgMngmt mgMngmt; + Pst pst; /* Post for layer manager */ + MgCntrl *cntrl; + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + + cntrl = &(mgMngmt.t.cntrl); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCNTRL; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STTSAP; + + cntrl->action = AENA; + cntrl->subAction = SAELMNT; + cntrl->spId = idx; + + return(sng_cntrl_mg(&pst, &mgMngmt)); +} + +/*****************************************************************************************************************/ + +#if 0 +int mgco_mg_tpt_server(int idx) +{ + MgMngmt mgMngmt; + Pst pst; /* Post for layer manager */ + MgCntrl *cntrl; + MgTptCntrl *tptCntrl = &mgMngmt.t.cntrl.s.tptCntrl; + CmInetIpAddr ipAddr = 0; + sng_mg_cfg_t* mg_cfg = &megaco_globals.g_mg_cfg.mg_cfg[idx]; + + cntrl = &(mgMngmt.t.cntrl); + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + + tptCntrl->transportType = GET_TPT_TYPE(idx); + + tptCntrl->serverAddr.type = CM_INET_IPV4ADDR_TYPE; + tptCntrl->serverAddr.u.ipv4TptAddr.port = mg_cfg->port; + if(ROK == cmInetAddr((S8*)mg_cfg->my_ipaddr, &ipAddr)) + { + tptCntrl->serverAddr.u.ipv4TptAddr.address = ntohl(ipAddr); + } + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCNTRL; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STSERVER; + + cntrl->action = AENA; + cntrl->subAction = SAELMNT; + cntrl->spId = (SpId)0x01; + + return(sng_cntrl_mg(&pst, &mgMngmt)); +} +#endif + +/*****************************************************************************************************************/ + +int mgco_mu_ssap_cntrl(int idx) +{ + MuMngmt mgMngmt; + Pst pst; /* Post for layer manager */ + MuCntrl *cntrl; + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + + cntrl = &(mgMngmt.t.cntrl); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMU; + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCNTRL; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STSSAP; + mgMngmt.hdr.elmId.elmntInst1 = idx; + + cntrl->action = ABND_ENA; + cntrl->subAction = SAELMNT; + + return(sng_cntrl_mu(&pst, &mgMngmt)); +} + +/*****************************************************************************************************************/ + +int mgco_mg_ssap_cntrl(int idx) +{ + MgMngmt mgMngmt; + Pst pst; /* Post for layer manager */ + MgCntrl *cntrl; + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + + cntrl = &(mgMngmt.t.cntrl); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCNTRL; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STSSAP; + + cntrl->action = AENA; + cntrl->subAction = SAELMNT; + cntrl->spId = (SpId)idx; + + return(sng_cntrl_mg(&pst, &mgMngmt)); +} +/******************************************************************************/ + +int mg_enable_logging() +{ + MgMngmt mgMngmt; + Pst pst; /* Post for layer manager */ + MgCntrl* cntrl; + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + cntrl = &mgMngmt.t.cntrl; + + mg_tucl_debug(AENA); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + mgMngmt.hdr.msgType = TCFG; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STGEN; + + cntrl->action = AENA; + cntrl->subAction = SADBG; + cntrl->s.dbg.genDbgMask = 0xfffffdff; + + return(sng_cntrl_mg(&pst, &mgMngmt)); +} + +/******************************************************************************/ +int mg_disable_logging() +{ + MgMngmt mgMngmt; + Pst pst; /* Post for layer manager */ + MgCntrl* cntrl; + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + cntrl = &mgMngmt.t.cntrl; + + mg_tucl_debug(ADISIMM); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + mgMngmt.hdr.msgType = TCFG; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STGEN; + + cntrl->action = ADISIMM; + cntrl->subAction = SADBG; + cntrl->s.dbg.genDbgMask = 0xfffffdff; + + return(sng_cntrl_mg(&pst, &mgMngmt)); +} + +/******************************************************************************/ +int mg_tucl_debug(int action) +{ + Pst pst; + HiMngmt cntrl; + + memset((U8 *)&pst, 0, sizeof(Pst)); + memset((U8 *)&cntrl, 0, sizeof(HiMngmt)); + + smPstInit(&pst); + + pst.dstEnt = ENTHI; + + /* prepare header */ + cntrl.hdr.msgType = TCNTRL; /* message type */ + cntrl.hdr.entId.ent = ENTHI; /* entity */ + cntrl.hdr.entId.inst = 0; /* instance */ + cntrl.hdr.elmId.elmnt = STGEN; /* General */ + + cntrl.hdr.response.selector = 0; + cntrl.hdr.response.prior = PRIOR0; + cntrl.hdr.response.route = RTESPEC; + cntrl.hdr.response.mem.region = S_REG; + cntrl.hdr.response.mem.pool = S_POOL; + + cntrl.t.cntrl.action = action; + cntrl.t.cntrl.subAction = SADBG; + cntrl.t.cntrl.ctlType.hiDbg.dbgMask = 0xFFFF; + + return (sng_cntrl_tucl (&pst, &cntrl)); +} + +/******************************************************************************/ +int mgco_tucl_gen_config(void) +{ + HiMngmt cfg; + Pst pst; + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTHI; + + /* clear the configuration structure */ + memset(&cfg, 0, sizeof(cfg)); + + /* fill in the post structure */ + set_dest_sm_pst(&cfg.t.cfg.s.hiGen.lmPst); + /*fill in the specific fields of the header */ + cfg.hdr.msgType = TCFG; + cfg.hdr.entId.ent = ENTHI; + cfg.hdr.entId.inst = S_INST; + cfg.hdr.elmId.elmnt = STGEN; + + cfg.t.cfg.s.hiGen.numSaps = HI_MAX_SAPS; /* number of SAPs */ + cfg.t.cfg.s.hiGen.numCons = HI_MAX_NUM_OF_CON; /* maximum num of connections */ + cfg.t.cfg.s.hiGen.numFdsPerSet = HI_MAX_NUM_OF_FD_PER_SET; /* maximum num of fds to use per set */ + cfg.t.cfg.s.hiGen.numFdBins = HI_MAX_NUM_OF_FD_HASH_BINS; /* for fd hash lists */ + cfg.t.cfg.s.hiGen.numClToAccept = HI_MAX_NUM_OF_CLIENT_TO_ACCEPT; /* clients to accept simultaneously */ + cfg.t.cfg.s.hiGen.permTsk = TRUE; /* schedule as perm task or timer */ + cfg.t.cfg.s.hiGen.schdTmrVal = HI_MAX_SCHED_TMR_VALUE; /* if !permTsk - probably ignored */ + cfg.t.cfg.s.hiGen.selTimeout = HI_MAX_SELECT_TIMEOUT_VALUE; /* select() timeout */ + + /* number of raw/UDP messages to read in one iteration */ + cfg.t.cfg.s.hiGen.numRawMsgsToRead = HI_MAX_RAW_MSG_TO_READ; + cfg.t.cfg.s.hiGen.numUdpMsgsToRead = HI_MAX_UDP_MSG_TO_READ; + + /* thresholds for congestion on the memory pool */ + cfg.t.cfg.s.hiGen.poolStrtThr = HI_MEM_POOL_START_THRESHOLD; + cfg.t.cfg.s.hiGen.poolDropThr = HI_MEM_POOL_DROP_THRESHOLD; + cfg.t.cfg.s.hiGen.poolStopThr = HI_MEM_POOL_STOP_THRESHOLD; + + cfg.t.cfg.s.hiGen.timeRes = HI_PERIOD; /* time resolution */ + +#ifdef HI_SPECIFY_GENSOCK_ADDR + cfg.t.cfg.s.hiGen.ipv4GenSockAddr.address = CM_INET_INADDR_ANY; + cfg.t.cfg.s.hiGen.ipv4GenSockAddr.port = 0; /* DAVIDY - why 0? */ +#ifdef IPV6_SUPPORTED + cfg.t.cfg.s.hiGen.ipv6GenSockAddr.address = CM_INET_INADDR6_ANY; + cfg.t.cfg.s.hiGen.ipv4GenSockAddr.port = 0; +#endif +#endif + + return(sng_cfg_tucl(&pst, &cfg)); +} + +/******************************************************************************/ + +int mgco_tucl_sap_config(int idx) +{ + HiMngmt cfg; + Pst pst; + HiSapCfg *pCfg; + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTHI; + + /* clear the configuration structure */ + memset(&cfg, 0, sizeof(cfg)); + + /* fill in the post structure */ + set_dest_sm_pst(&cfg.t.cfg.s.hiGen.lmPst); + /*fill in the specific fields of the header */ + cfg.hdr.msgType = TCFG; + cfg.hdr.entId.ent = ENTHI; + cfg.hdr.entId.inst = S_INST; + cfg.hdr.elmId.elmnt = STTSAP; + + pCfg = &cfg.t.cfg.s.hiSap; + + pCfg->spId = idx; + pCfg->uiSel = 0x00; /*loosley coupled */ + pCfg->flcEnb = TRUE; + pCfg->txqCongStrtLim = HI_SAP_TXN_QUEUE_CONG_START_LIMIT; + pCfg->txqCongDropLim = HI_SAP_TXN_QUEUE_CONG_DROP_LIMIT; + pCfg->txqCongStopLim = HI_SAP_TXN_QUEUE_CONG_STOP_LIMIT; + pCfg->numBins = 10; + + pCfg->uiMemId.region = S_REG; + pCfg->uiMemId.pool = S_POOL; + pCfg->uiPrior = PRIOR0; + pCfg->uiRoute = RTESPEC; + + return(sng_cfg_tucl(&pst, &cfg)); +} + +/******************************************************************************/ + +int mgco_mg_gen_config(void) +{ + MgMngmt mgMngmt; + MgGenCfg *cfg; + Pst pst; /* Post for layer manager */ + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + cfg = &(mgMngmt.t.cfg.c.genCfg); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + + /* fill in the post structure */ + set_dest_sm_pst(&mgMngmt.t.cfg.c.genCfg.lmPst); + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCFG; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STGEN; + + + /*----------- Fill General Configuration Parameters ---------*/ + cfg->maxSSaps = (U16)MG_MAX_SSAPS; + cfg->maxTSaps = (U32)MG_MAX_TSAPS; + cfg->maxServers = (U16)MG_MAX_SERVERS; + cfg->maxConn = (U32)10; + cfg->maxTxn = (U16)MG_MAX_OUTSTANDING_TRANSACTIONS; + cfg->maxPeer = (U32)MG_MAX_PEER; + cfg->resThUpper = (Status)7; + cfg->resThLower = (Status)3; +#if (defined(GCP_MGCP) || defined(TDS_ROLL_UPGRADE_SUPPORT)) + cfg->timeResTTL = (Ticks)10000; +#endif + + cfg->timeRes = (Ticks)10; + cfg->reCfg.rspAckEnb = MG_LMG_GET_RSPACK_MGCO; + cfg->numBlks = (U32)MG_NUM_BLK; + cfg->maxBlkSize = (Size)MG_MAXBLKSIZE; + cfg->numBinsTxnIdHl = (U16)149; + cfg->numBinsNameHl = (U16)149; + cfg->entType = LMG_ENT_GW; + cfg->numBinsTptSrvrHl = (U16)149; + cfg->indicateRetx = TRUE; /* Assume environment to be lossy */ + cfg->resOrder = LMG_RES_IPV4; /* IPV4 only */ + +#ifdef CM_ABNF_MT_LIB + cfg->firstInst = 1; + cfg->edEncTmr.enb = FALSE; + cfg->edEncTmr.val = (U16)50; + cfg->edDecTmr.enb = TRUE; + cfg->edDecTmr.val = (U16)50; + cfg->noEDInst = 1; +#endif /* CM_ABNF_MT_LIB */ + + cfg->entType = LMG_ENT_GW; + +#ifdef GCP_CH + cfg->numBinsPeerCmdHl = 20; + cfg->numBinsTransReqHl = 50; + cfg->numBinsTransIndRspCmdHl = 50; +#endif /* GCP_CH */ + +#ifdef GCP_MG + cfg->maxMgCmdTimeOut.enb =TRUE; + cfg->maxMgCmdTimeOut.val =20; +#endif /* GCP_MG */ + +#ifdef GCP_MG + cfg->maxMgCmdTimeOut.enb =TRUE; + cfg->maxMgCmdTimeOut.val =20; +#endif /* GCP_MG */ + +#ifdef GCP_MGC + cfg->maxMgcCmdTimeOut.enb =TRUE; + cfg->maxMgcCmdTimeOut.val =20; +#endif /* GCP_MG */ + +#if (defined(GCP_MGCO) && (defined GCP_VER_2_1)) + cfg->reCfg.segRspTmr.enb = TRUE; + cfg->reCfg.segRspTmr.val = (U16)50; + cfg->reCfg.segRspAckTmr.enb = TRUE; + cfg->reCfg.segRspAckTmr.val = (U16)25; +#endif + +#ifdef GCP_PKG_MGCO_ROOT + cfg->limit.pres.pres = PRSNT_NODEF; + cfg->limit.mgcOriginatedPendingLimit = 20000; + cfg->limit.mgOriginatedPendingLimit = 20000; +#endif /* GCP_PKG_MGCO_ROOT */ + + return(sng_cfg_mg(&pst, &mgMngmt)); +} + +/******************************************************************************/ + +int mgco_mu_gen_config(void) +{ + MuMngmt mgmt; + MuGenCfg *cfg; + Pst pst; /* Post for layer manager */ + + memset(&mgmt, 0, sizeof(mgmt)); + cfg = &(mgmt.t.cfg); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMU; + + /*fill in the specific fields of the header */ + mgmt.hdr.msgType = TCFG; + mgmt.hdr.entId.ent = ENTMU; + mgmt.hdr.entId.inst = S_INST; + mgmt.hdr.elmId.elmnt = STGEN; + + return(sng_cfg_mu(&pst, &mgmt)); +} + +/******************************************************************************/ + +int mgco_mu_ssap_config(int idx) +{ + MuMngmt mgmt; + MuSAP_t *cfg; + Pst pst; /* Post for layer manager */ + + memset(&mgmt, 0, sizeof(mgmt)); + cfg = &(mgmt.t.sapCfg); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMU; + + /*fill in the specific fields of the header */ + mgmt.hdr.msgType = TCFG; + mgmt.hdr.entId.ent = ENTMU; + mgmt.hdr.entId.inst = S_INST; + mgmt.hdr.elmId.elmnt = STSSAP; + + /* fill lower layer i.e. MG PST */ + cfg->ssapId = idx; + cfg->spId = idx; + + cfg->mem.region = S_REG; + cfg->mem.pool = S_POOL; + cfg->dstProcId = SFndProcId(); + cfg->dstEnt = ENTMG; + cfg->dstInst = S_INST; + cfg->dstPrior = PRIOR0; + cfg->dstRoute = RTESPEC; + cfg->selector = 0x00; /* Loosely coupled */ + + return(sng_cfg_mu(&pst, &mgmt)); +} + +/******************************************************************************/ + +int mgco_mg_ssap_config(megaco_profile_t* profile) +{ + MgMngmt mgMngmt; + MgSSAPCfg *pCfg; + Pst pst; /* Post for layer manager */ + CmInetIpAddr ipAddr; + int len = 0x00; + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + pCfg = &(mgMngmt.t.cfg.c.sSAPCfg); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCFG; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STSSAP; + + /* FILL SAP config */ + + pCfg->sSAPId = profile->idx; /* SSAP ID */ + pCfg->sel = 0x00 ; /* Loosely coupled */ + pCfg->memId.region = S_REG; + pCfg->memId.pool = S_POOL; + pCfg->prior = PRIOR0; + pCfg->route = RTESPEC; + + pCfg->protocol = mg_get_proto_type_from_str(profile->protocol_type); + + pCfg->startTxnNum = 50; + pCfg->endTxnNum = 60; + + pCfg->initReg = TRUE; + pCfg->mwdTimer = (U16)10; + + pCfg->minMgcoVersion = LMG_VER_PROF_MGCO_H248_1_0; + switch(profile->protocol_version) + { + case 1: + pCfg->maxMgcoVersion = LMG_VER_PROF_MGCO_H248_1_0; + break; + case 2: + pCfg->maxMgcoVersion = LMG_VER_PROF_MGCO_H248_2_0; + break; + case 3: + pCfg->maxMgcoVersion = LMG_VER_PROF_MGCO_H248_3_0; + break; + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Protocol version[%d] \n",profile->protocol_version); + return SWITCH_STATUS_FALSE; + } + + pCfg->chEnabled = 0x01; + + pCfg->userInfo.pres.pres = PRSNT_NODEF; + pCfg->userInfo.id.pres = NOTPRSNT; + pCfg->userInfo.mid.pres = PRSNT_NODEF; + pCfg->userInfo.dname.namePres.pres = PRSNT_NODEF; + + pCfg->userInfo.mid.len = (U8)strlen((char*)profile->mid); + strncpy((char*)pCfg->userInfo.mid.val, (char*)profile->mid, MAX_MID_LEN); + + len = (U32)strlen((char*)profile->my_domain); + memcpy( (U8*)(pCfg->userInfo.dname.name), + (CONSTANT U8*)(profile->my_domain), len ); + pCfg->userInfo.dname.name[len] = '\0'; + + pCfg->userInfo.dname.netAddr.type = CM_TPTADDR_IPV4; + memset(&ipAddr,'\0',sizeof(ipAddr)); + if(ROK == cmInetAddr((S8*)profile->my_ipaddr,&ipAddr)) + { + pCfg->userInfo.dname.netAddr.u.ipv4NetAddr = ntohl(ipAddr); + } + + pCfg->reCfg.initRetxTmr.enb = TRUE; + pCfg->reCfg.initRetxTmr.val = MG_INIT_RTT; + pCfg->reCfg.provRspTmr.enb = TRUE; + pCfg->reCfg.provRspTmr.val = (U16)50; /* In timer resolution */ + pCfg->reCfg.provRspDelay = 2; + pCfg->reCfg.atMostOnceTmr.enb = TRUE; + pCfg->reCfg.atMostOnceTmr.val = (U16)30; + + return(sng_cfg_mg(&pst, &mgMngmt)); +} + +/******************************************************************************/ + +int mgco_mg_tsap_config(megaco_profile_t* profile) +{ + MgMngmt mgMngmt; + MgTSAPCfg *cfg; + Pst pst; + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + cfg = &(mgMngmt.t.cfg.c.tSAPCfg); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCFG; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STTSAP; + + /* FILL TSAP config */ + cfg->tSAPId = profile->idx; + cfg->spId = profile->idx; + cfg->provType = LMG_PROV_TYPE_TUCL; + + /* FILL TUCL Information */ + cfg->memId.region = S_REG; + cfg->memId.pool = S_POOL; + cfg->dstProcId = SFndProcId(); + cfg->dstEnt = ENTHI; + cfg->dstInst = S_INST; + cfg->dstPrior = PRIOR0; + cfg->dstRoute = RTESPEC; + cfg->dstSel = 0x00; /* Loosely coupled */ + cfg->bndTmrCfg.enb = TRUE; + cfg->bndTmrCfg.val = 5; /* 5 seconds */ + + + /* Disable DNS as of now */ + cfg->reCfg.idleTmr.enb = FALSE; + cfg->reCfg.dnsCfg.dnsAccess = LMG_DNS_DISABLED; + cfg->reCfg.dnsCfg.dnsAddr.type = CM_TPTADDR_IPV4; + cfg->reCfg.dnsCfg.dnsAddr.u.ipv4TptAddr.port = (U16)53; + cfg->reCfg.dnsCfg.dnsAddr.u.ipv4TptAddr.address = (CmInetIpAddr)MG_DNS_IP; + + cfg->reCfg.dnsCfg.dnsRslvTmr.enb = FALSE; + cfg->reCfg.dnsCfg.dnsRslvTmr.val = 60; /* 60 sec */ + cfg->reCfg.dnsCfg.maxRetxCnt = 4; + + cfg->reCfg.tMax = 1000; + cfg->reCfg.tptParam.type = CM_TPTPARAM_SOCK; + cfg->reCfg.tptParam.u.sockParam.listenQSize = 5; + cfg->reCfg.tptParam.u.sockParam.numOpts = 0; + + + return(sng_cfg_mg(&pst, &mgMngmt)); +} + +/******************************************************************************/ + +int mgco_mg_peer_config(megaco_profile_t* mg_cfg) +{ + MgMngmt mgMngmt; + MgGcpEntCfg *cfg; + Pst pst; /* Post for layer manager */ + U32 peerIdx = 0; + CmInetIpAddr ipAddr = 0; + mg_peer_profile_t* mg_peer = NULL; + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + cfg = &(mgMngmt.t.cfg.c.mgGcpEntCfg); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCFG; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STGCPENT; + + cfg->numPeer = mg_cfg->total_peers; + for(peerIdx =0; peerIdx < mg_cfg->total_peers; peerIdx++){ + + mg_peer = megaco_peer_profile_locate(mg_cfg->peer_list[peerIdx]); + + cfg->peerCfg[peerIdx].sSAPId = mg_cfg->idx; /* SSAP ID */; + cfg->peerCfg[peerIdx].port = atoi(mg_peer->port); + cfg->peerCfg[peerIdx].tsapId = mg_cfg->idx; + + cfg->peerCfg[peerIdx].mtuSize = MG_MAX_MTU_SIZE; + + cfg->peerCfg[peerIdx].peerAddrTbl.count = 1; + cfg->peerCfg[peerIdx].peerAddrTbl.netAddr[0].type = + CM_NETADDR_IPV4; + + if(ROK == cmInetAddr((S8*)&mg_peer->ipaddr[0],&ipAddr)) + { + cfg->peerCfg[peerIdx].peerAddrTbl.netAddr[0].u.ipv4NetAddr = ntohl(ipAddr); + } + else + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "cmInetAddr failed \n"); + cfg->peerCfg[peerIdx].peerAddrTbl.count = 0; + } + +#ifdef GCP_MG + cfg->peerCfg[peerIdx].transportType = mg_get_tpt_type_from_str(mg_peer->transport_type); + cfg->peerCfg[peerIdx].encodingScheme = mg_get_enc_type_from_str(mg_peer->encoding_type); + cfg->peerCfg[peerIdx].mgcPriority = peerIdx; + cfg->peerCfg[peerIdx].useAHScheme = FALSE; + cfg->peerCfg[peerIdx].mid.pres = PRSNT_NODEF; + cfg->peerCfg[peerIdx].mid.len = strlen((char*)mg_peer->mid); + cmMemcpy((U8 *)cfg->peerCfg[peerIdx].mid.val, + (CONSTANT U8*)(char*)mg_peer->mid, + cfg->peerCfg[peerIdx].mid.len); + +#endif /* GCP_MG */ + } + + return(sng_cfg_mg(&pst, &mgMngmt)); +} + +/******************************************************************************/ + +int mgco_mg_tpt_server_config(megaco_profile_t* mg_cfg) +{ + MgMngmt mgMngmt; + MgTptSrvrCfg *cfg; + Pst pst; /* Post for layer manager */ + CmInetIpAddr ipAddr = 0; + int srvIdx = 0; + mg_peer_profile_t* mg_peer = megaco_peer_profile_locate(mg_cfg->peer_list[0]); + + memset(&mgMngmt, 0, sizeof(mgMngmt)); + cfg = &(mgMngmt.t.cfg.c.tptSrvrCfg); + + /* initalize the post structure */ + smPstInit(&pst); + + /* insert the destination Entity */ + pst.dstEnt = ENTMG; + + /*fill in the specific fields of the header */ + mgMngmt.hdr.msgType = TCFG; + mgMngmt.hdr.entId.ent = ENTMG; + mgMngmt.hdr.entId.inst = S_INST; + mgMngmt.hdr.elmId.elmnt = STSERVER; + + cfg->count = 1; + cfg->srvr[srvIdx].isDefault = TRUE; + cfg->srvr[srvIdx].sSAPId = mg_cfg->idx; + cfg->srvr[srvIdx].tSAPId = mg_cfg->idx; + cfg->srvr[srvIdx].protocol = mg_get_proto_type_from_str(mg_cfg->protocol_type); + cfg->srvr[srvIdx].transportType = mg_get_tpt_type(mg_cfg); + cfg->srvr[srvIdx].encodingScheme = mg_get_enc_type_from_str(mg_peer->encoding_type); + + cfg->srvr[srvIdx].tptParam.type = CM_TPTPARAM_SOCK; + cfg->srvr[srvIdx].tptParam.u.sockParam.listenQSize = 5; + cfg->srvr[srvIdx].tptParam.u.sockParam.numOpts = 0; + cfg->srvr[srvIdx].lclTptAddr.type = CM_TPTADDR_IPV4; + cfg->srvr[srvIdx].lclTptAddr.u.ipv4TptAddr.port = atoi(mg_cfg->port); + if(ROK == cmInetAddr((S8*)mg_cfg->my_ipaddr, &ipAddr)) + { + cfg->srvr[srvIdx].lclTptAddr.u.ipv4TptAddr.address = ntohl(ipAddr); + } + + return(sng_cfg_mg(&pst, &mgMngmt)); +} + +/******************************************************************************/ +int sng_mgco_tucl_shutdown() +{ + Pst pst; + HiMngmt cntrl; + + memset((U8 *)&pst, 0, sizeof(Pst)); + memset((U8 *)&cntrl, 0, sizeof(HiMngmt)); + + smPstInit(&pst); + + pst.dstEnt = ENTHI; + + /* prepare header */ + cntrl.hdr.msgType = TCNTRL; /* message type */ + cntrl.hdr.entId.ent = ENTHI; /* entity */ + cntrl.hdr.entId.inst = 0; /* instance */ + cntrl.hdr.elmId.elmnt = STGEN; /* General */ + + cntrl.hdr.response.selector = 0; + cntrl.hdr.response.prior = PRIOR0; + cntrl.hdr.response.route = RTESPEC; + cntrl.hdr.response.mem.region = S_REG; + cntrl.hdr.response.mem.pool = S_POOL; + + cntrl.t.cntrl.action = ASHUTDOWN; + + return (sng_cntrl_tucl (&pst, &cntrl)); +} +/******************************************************************************/ +int sng_mgco_mg_shutdown() +{ + Pst pst; + MgMngmt cntrl; + + memset((U8 *)&pst, 0, sizeof(Pst)); + memset((U8 *)&cntrl, 0, sizeof(MgCntrl)); + + smPstInit(&pst); + + pst.dstEnt = ENTMG; + + /* prepare header */ + cntrl.hdr.msgType = TCNTRL; /* message type */ + cntrl.hdr.entId.ent = ENTMG; /* entity */ + cntrl.hdr.entId.inst = 0; /* instance */ + cntrl.hdr.elmId.elmnt = STGEN; /* General */ + + cntrl.hdr.response.selector = 0; + cntrl.hdr.response.prior = PRIOR0; + cntrl.hdr.response.route = RTESPEC; + cntrl.hdr.response.mem.region = S_REG; + cntrl.hdr.response.mem.pool = S_POOL; + + cntrl.t.cntrl.action = ASHUTDOWN; + cntrl.t.cntrl.subAction = SAELMNT; + + return (sng_cntrl_mg (&pst, &cntrl)); +} +/******************************************************************************/ +int sng_mgco_mg_get_status(int elemId, MgMngmt* cfm, megaco_profile_t* mg_cfg, mg_peer_profile_t* mg_peer) +{ + Pst pst; + MgMngmt cntrl; + CmInetIpAddr ipAddr = 0; + + memset((U8 *)&pst, 0, sizeof(Pst)); + memset((U8 *)&cntrl, 0, sizeof(MgCntrl)); + + smPstInit(&pst); + + pst.dstEnt = ENTMG; + + /* prepare header */ + /*cntrl.hdr.msgType = TCNTRL; */ /* message type */ + cntrl.hdr.entId.ent = ENTMG; /* entity */ + cntrl.hdr.entId.inst = 0; /* instance */ + cntrl.hdr.elmId.elmnt = elemId; /* General */ + + cntrl.hdr.response.selector = 0; + cntrl.hdr.response.prior = PRIOR0; + cntrl.hdr.response.route = RTESPEC; + cntrl.hdr.response.mem.region = S_REG; + cntrl.hdr.response.mem.pool = S_POOL; + + switch(elemId) + { + case STGCPENT: + { + cntrl.t.ssta.s.mgPeerSta.peerId.pres = PRSNT_NODEF; + cntrl.t.ssta.s.mgPeerSta.peerId.val = mg_cfg->idx; + + cntrl.t.ssta.s.mgPeerSta.mid.pres = PRSNT_NODEF; + cntrl.t.ssta.s.mgPeerSta.mid.len = strlen((char*)mg_peer->mid); + cmMemcpy((U8 *)cntrl.t.ssta.s.mgPeerSta.mid.val, + (CONSTANT U8*)(char*)mg_peer->mid, + cntrl.t.ssta.s.mgPeerSta.mid.len); + break; + } + case STSSAP: + { + cntrl.t.ssta.s.mgSSAPSta.sapId = mg_cfg->idx; + break; + } + case STTSAP: + { + cntrl.t.ssta.s.mgTSAPSta.tSapId = mg_cfg->idx; + break; + } + case STSERVER: + { + cntrl.t.ssta.s.mgTptSrvSta.tptAddr.type = CM_INET_IPV4ADDR_TYPE; + cntrl.t.ssta.s.mgTptSrvSta.tptAddr.u.ipv4TptAddr.port = atoi(mg_cfg->port); + if(ROK == cmInetAddr((S8*)mg_cfg->my_ipaddr, &ipAddr)) + { + cntrl.t.ssta.s.mgTptSrvSta.tptAddr.u.ipv4TptAddr.address = ntohl(ipAddr); + } + + + break; + } + default: + break; + } + + return (sng_sta_mg (&pst, &cntrl, cfm)); +} + +/**********************************************************************************************************************************/ +sng_mg_transport_types_e mg_get_tpt_type(megaco_profile_t* mg_profile) +{ + mg_peer_profile_t* mg_peer_profile = NULL; + + if(NULL == mg_profile){ + return SNG_MG_TPT_NONE; + } + + if(!mg_profile->total_peers){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " No peer configured against profilename[%s]\n",mg_profile->name); + return SNG_MG_TPT_NONE; + } + + if( NULL == (mg_peer_profile = megaco_peer_profile_locate(mg_profile->peer_list[0]))){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Not able to get peer_profile node based on profilename[%s]\n",mg_profile->peer_list[0]); + return SNG_MG_TPT_NONE; + } + + return mg_get_tpt_type_from_str(mg_peer_profile->transport_type); +} +/**********************************************************************************************************************************/ + +sng_mg_transport_types_e mg_get_tpt_type_from_str(char* tpt_type) +{ + if(!tpt_type) return SNG_MG_TPT_NONE; + + if(!strcasecmp(tpt_type, "UDP")){ + return SNG_MG_TPT_UDP; + }else if(!strcasecmp(tpt_type,"TCP")){ + return SNG_MG_TPT_TCP; + }else if(!strcasecmp(tpt_type,"STCP")){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "STCP Transport for H.248 Protocol Not Yet Supported \n"); + return SNG_MG_TPT_SCTP; + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Protocol Value[%s] \n",tpt_type); + return SNG_MG_TPT_NONE; + } + + return SNG_MG_TPT_NONE; +} +/**********************************************************************************************************************************/ + +sng_mg_encoding_types_e mg_get_enc_type_from_str(char* enc_type) +{ + if(!enc_type) return SNG_MG_ENCODING_NONE; + + if(!strcasecmp(enc_type, "TEXT")){ + return SNG_MG_ENCODING_TEXT; + } else if(!strcasecmp(enc_type, "BINARY")){ + return SNG_MG_ENCODING_BINARY; + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Encoding Type[%s] \n", enc_type); + return SNG_MG_ENCODING_NONE; + } + + return SNG_MG_ENCODING_NONE; +} + +/**********************************************************************************************************************************/ + +sng_mg_protocol_types_e mg_get_proto_type_from_str(char* proto_type) +{ + if(!proto_type) return SNG_MG_NONE; + + if(!strcasecmp(proto_type,"MEGACO")) { + return SNG_MG_MEGACO; + }else if(!strcasecmp(proto_type,"MGCP")){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MGCP Protocol Not Yet Supported \n"); + return SNG_MG_MGCP; + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Protocol Value[%s] \n",proto_type); + return SNG_MG_NONE; + } + + return SNG_MG_NONE; +} + + +/**********************************************************************************************************************************/ diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway_stack.h b/src/mod/endpoints/mod_media_gateway/media_gateway_stack.h new file mode 100644 index 0000000000..b2e9a91340 --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/media_gateway_stack.h @@ -0,0 +1,265 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Kapil Gupta +* All rights reserved. +* +* +*/ + +#include "mod_media_gateway.h" + +#ifndef _MEGACO_STACK_H_ +#define _MEGACO_STACK_H_ + +#define MAX_MID_LEN 30 + +#define MG_INACTIVITY_TMR_RESOLUTION 100 /* mit in ito package is experessed in 10ms steps */ + +/* rtp/avp profiles */ +#define MG_RTP_AVP_PROFILE_A_LAW 8 +#define MG_RTP_AVP_PROFILE_U_LAW 0 + + +typedef enum{ + MG_SDP_NONE, + MG_SDP_LOCAL, + MG_SDP_REMOTE, +}mgco_sdp_types_e; + + +typedef enum{ + SNG_MG_TPT_NONE, + SNG_MG_TPT_UDP, + SNG_MG_TPT_TCP, + SNG_MG_TPT_SCTP, + SNG_MG_TPT_MTP3 +}sng_mg_transport_types_e; + +typedef enum{ + SNG_MG_NONE, + SNG_MG_MGCP, + SNG_MG_MEGACO, +}sng_mg_protocol_types_e; + +#define PRNT_PROTOCOL_TYPE(_val)\ + ((_val == SNG_MG_MGCP)?"SNG_MG_MGCP":\ + (_val == SNG_MG_MEGACO)?"SNG_MG_MEGACO":\ + "SNG_MG_NONE") + +typedef enum{ + SNG_MG_ENCODING_NONE, + SNG_MG_ENCODING_BINARY, + SNG_MG_ENCODING_TEXT, +}sng_mg_encoding_types_e; + +typedef enum{ + MG_TERM_SERVICE_STATE_UNKNOWN, + MG_TERM_SERVICE_STATE_IN_SERVICE, + MG_TERM_SERVICE_STATE_OUT_OF_SERVICE, + MG_TERM_SERVICE_STATE_INVALID, +}mg_term_states_e; + +#define PRNT_ENCODING_TYPE(_val)\ + ((_val == SNG_MG_ENCODING_TEXT)?"SNG_MG_ENCODING_TEXT":\ + (_val == SNG_MG_ENCODING_BINARY)?"SNG_MG_ENCODING_BINARY":\ + "SNG_MG_ENCODING_NONE") + +typedef struct _mgStr +{ + U8 len; + U8 val[128]; +} MgStr; + +#define MAX_PACKAGE_NAME 128 + +typedef struct _mgPackage +{ + uint8_t package_id; + uint16_t version; + uint8_t name[MAX_PACKAGE_NAME+1]; +}MgPackage_t; + +extern MgPackage_t mg_pkg_list[]; + +/* Service change Reason */ +typedef enum { + MG_SVC_REASON_NOTUSED = 0, + MG_SVC_REASON_900_RESTORED = 1, + MG_SVC_REASON_905_TERM_OOS = 2, + MG_SVC_REASON_LAST = 4 +} MgServiceChangeReason_e; + +#define MG_TXN_INVALID 0 + +#define MG_ZERO(_buf, _size) {cmMemset((U8 *)(_buf), 0, _size);} + +/* Set pres field */ +#define MG_SET_PRES(_pres) \ + (_pres) = PRSNT_NODEF; + +/* Set token value */ +#define MG_SET_VAL_PRES(tkn,_val) \ + MG_SET_PRES((tkn).pres); \ + (tkn).val = _val; + +#define MG_SET_TKN_VAL_PRES(_tkn, _val, _pres) \ +{ \ + (_tkn)->val = _val; \ + (_tkn)->pres = _pres; \ +} + + +#define MG_MEM_COPY(_dst, _src, _len) \ + cmMemcpy((U8*) (_dst), (const U8*) (_src), _len) + + +#define MG_STACK_MEM_ALLOC(_buf, _size)\ +{\ + if (SGetSBuf(S_REG, S_POOL, (Data**) _buf, (Size) _size) == ROK){ \ + cmMemset((U8 *) *(_buf), 0, _size); \ + } else { \ + *(_buf) = NULLP; \ + } \ +} + +#define MG_STACK_MEM_FREE(_buf, _size)\ +{\ + if(_buf != NULL){ \ + (Void) SPutSBuf(S_REG, S_POOL, (Data *) _buf, (Size) _size);\ + (_buf) = NULL; \ + } \ +} + + + +#define MG_INIT_TOKEN_VALUE(_tkn, _val) \ +{ \ + (_tkn)->pres = PRSNT_NODEF; \ + (_tkn)->val = (_val); \ +} + +#define MG_GETMEM(_ptr,_len,_memCp,_ret) \ +{ \ + ret = cmGetMem((_memCp), (_len), (Ptr *)&(_ptr)); \ + if( ret == ROK) \ + cmMemset((U8 *)(_ptr), (U8)0, (PTR)(_len)); \ +} + +#define MG_INIT_TKNSTR(_tkn, _val, _len) \ +{ \ + (_tkn)->pres = PRSNT_NODEF; \ + (_tkn)->len = (U8)(_len); \ + cmMemcpy((U8 *)(_tkn)->val, (CONSTANT U8 *)(_val), (_len)); \ +} + +#define MG_SET_TKNSTROSXL(_tkn, _len, _val, _mem)\ +{\ + (_tkn).pres = PRSNT_NODEF;\ + (_tkn).len = (_len);\ + cmGetMem((Ptr)_mem, (Size)((_len)*sizeof(U8)), (Ptr*)&((_tkn).val));\ + cmMemcpy((U8*)((_tkn).val), (U8*)(_val), _len);\ +} + +#define MG_SET_DEF_REQID(_reqId) \ + (_reqId)->type.pres = PRSNT_NODEF; \ + (_reqId)->type.val = MGT_REQID_OTHER; \ + (_reqId)->id.pres = PRSNT_NODEF; \ + (_reqId)->id.val = 0xFFFFFFFF; + + + +void mg_set_term_ec_status(mg_termination_t* term, mg_ec_types_t status); +switch_status_t mg_prc_descriptors(megaco_profile_t* mg_profile, MgMgcoCommand *inc_cmd, mg_termination_t* term, CmMemListCp *memCp); +void handle_sng_log(uint8_t level, char *fmt, ...); +void handle_mgco_sta_ind(Pst *pst, SuId suId, MgMgtSta* msg); +void handle_mgco_txn_sta_ind(Pst *pst, SuId suId, MgMgcoInd* msg); +void handle_mgco_cmd_ind(Pst *pst, SuId suId, MgMgcoCommand* msg); +void handle_mgco_cntrl_cfm(Pst *pst, SuId suId, MgMgtCntrl* cntrl, Reason reason); +void handle_mgco_txn_ind(Pst *pst, SuId suId, MgMgcoMsg* msg); +void handle_mgco_audit_cfm(Pst *pst, SuId suId, MgMgtAudit* audit, Reason reason); +void handle_mg_alarm(Pst *pst, MgMngmt *sta); +void handle_tucl_alarm(Pst *pst, HiMngmt *sta); +int mg_enable_logging(void); +int mg_disable_logging(void); +void mg_util_set_err_string ( MgStr *errTxt, char* str); + +switch_status_t mg_build_sdp(MgMgcoMediaDesc* out, MgMgcoMediaDesc* inc, megaco_profile_t* mg_profile, mg_termination_t* term, CmMemListCp *memCp); +switch_status_t mg_add_local_descriptor(MgMgcoMediaDesc* media, megaco_profile_t* mg_profile, mg_termination_t* term, CmMemListCp *memCp); +switch_status_t mg_send_term_service_change(char *span_name, char *chan_number, mg_term_states_e term_state); + + +switch_status_t mg_send_t38_cng_notify(megaco_profile_t* mg_profile, const char* term_name); +switch_status_t mg_send_t38_ans_notify(megaco_profile_t* mg_profile, const char* term_name); +switch_status_t mg_send_t38_v21flag_notify(megaco_profile_t* mg_profile, const char* term_name); + + +switch_status_t sng_mgco_cfg(megaco_profile_t* profile); +switch_status_t sng_mgco_init(sng_mg_event_interface_t* event); +switch_status_t sng_mgco_stack_shutdown(void); +int sng_mgco_mg_get_status(int elemId, MgMngmt* cfm, megaco_profile_t* mg_cfg, mg_peer_profile_t* mg_peer); + +switch_status_t mg_is_ito_pkg_req(megaco_profile_t* mg_profile, MgMgcoCommand *cmd); +switch_status_t mg_send_end_of_axn(SuId suId, MgMgcoTransId* transId, MgMgcoContextId* ctxtId, TknU32* peerId); +void mgco_handle_incoming_sdp(CmSdpInfoSet *sdp,mg_termination_t* term, mgco_sdp_types_e sdp_type, megaco_profile_t* mg_profile, CmMemListCp *memCp); +void mg_util_set_ctxt_string ( MgStr *errTxt, MgMgcoContextId *ctxtId); +switch_status_t handle_mg_add_cmd(megaco_profile_t* mg_profile, MgMgcoCommand *inc_cmd, MgMgcoContextId* new_ctxtId); +switch_status_t handle_mg_subtract_cmd(megaco_profile_t* mg_profile, MgMgcoCommand *inc_cmd); +switch_status_t handle_mg_modify_cmd(megaco_profile_t* mg_profile, MgMgcoCommand *cmd); +switch_status_t mg_stack_free_mem(void* msg); +switch_status_t mg_stack_alloc_mem( Ptr* _memPtr, Size _memSize ); +MgMgcoMediaDesc* get_default_media_desc(megaco_profile_t* mg_profile, MgMgcoTermId* termId, CmMemListCp *memCp); +switch_status_t handle_media_audit( SuId suId, MgMgcoCommand *auditReq); +switch_status_t mg_send_add_rsp(SuId suId, MgMgcoCommand *req); +S16 mg_fill_mgco_termid ( MgMgcoTermId *termId, char* term_str, int term_len, CmMemListCp *memCp); +void mg_util_set_txn_string(MgStr *errTxt, U32 *txnId); +switch_status_t mg_build_mgco_err_request(MgMgcoInd **errcmd,U32 trans_id, MgMgcoContextId *ctxt_id, U32 err, MgStr *errTxt); +switch_status_t mg_send_audit_rsp(SuId suId, MgMgcoCommand *req); +switch_status_t handle_mg_audit_cmd(megaco_profile_t* mg_profile, MgMgcoCommand *auditReq); +switch_status_t mg_stack_termination_is_in_service(megaco_profile_t* mg_profile, char* term_str, int len); +void mg_create_tdm_term(megaco_profile_t *profile, const char *tech, const char *channel_prefix, const char *prefix, int j, int k); +void mg_util_set_cmd_name_string (MgStr *errTxt, MgMgcoCommand *cmd); +switch_status_t mgco_init_ins_service_change(SuId suId); + +switch_status_t mg_send_modify_rsp(SuId suId, MgMgcoCommand *req); +switch_status_t mg_send_subtract_rsp(SuId suId, MgMgcoCommand *req); +void mg_util_set_term_string ( MgStr *errTxt, MgMgcoTermId *termId); +MgMgcoTermIdLst *mg_get_term_id_list(MgMgcoCommand *cmd); +switch_status_t handle_pkg_audit( SuId suId, MgMgcoCommand *auditReq); +switch_status_t mg_build_pkg_desc(MgMgcoPkgsDesc* pkg, CmMemListCp *memCp); +switch_status_t mg_send_heartbeat_audit_rsp( SuId suId, MgMgcoCommand *auditReq); +void mg_get_time_stamp(MgMgcoTimeStamp *timeStamp); +switch_status_t mg_fill_svc_change(MgMgcoSvcChgPar *srvPar, uint8_t method, const char *reason,CmMemListCp *memCp); +void mg_fill_null_context(MgMgcoContextId* ctxt); +switch_status_t mg_send_service_change(SuId suId, const char* term_name, uint8_t method, MgServiceChangeReason_e reason,uint8_t wild); +switch_status_t mg_create_mgco_command(MgMgcoCommand *cmd, uint8_t apiType, uint8_t cmdType); +switch_status_t mg_add_lcl_media(CmSdpMediaDescSet* med, megaco_profile_t* mg_profile, mg_termination_t* term, CmMemListCp *memCp); +switch_status_t mg_add_supported_media_codec(CmSdpMediaDesc* media, megaco_profile_t* mg_profile, mg_termination_t* term, CmMemListCp *memCp); +switch_status_t mg_rem_unsupported_codecs (megaco_profile_t* mg_profile, mg_termination_t* term, CmSdpMedFmtRtpList *fmtList, CmSdpAttrSet *attrSet, CmMemListCp *memCp); + +switch_status_t mg_send_oos_service_change(megaco_profile_t* mg_profile, const char* term_name, int wild); +switch_status_t mg_send_ins_service_change(megaco_profile_t* mg_profile, const char* term_name, int wild); +switch_status_t mg_send_notify(megaco_profile_t* mg_profile, const char* term_name, MgMgcoObsEvt* oevt); +switch_status_t mg_send_t38_fax_con_change_notify(megaco_profile_t* mg_profile, const char* term_name, char* state); +switch_status_t mg_send_dtmf_notify(megaco_profile_t* mg_profile, const char* term_name, char* digits, int num_of_collected_digits); +switch_status_t mg_send_ito_notify(megaco_profile_t* mg_profile); +void mg_print_t38_attributes(mg_termination_t* term); +switch_status_t mg_util_build_obs_evt_desc (MgMgcoObsEvt *obs_event, MgMgcoRequestId *request_id, MgMgcoObsEvtDesc **ptr_obs_desc); +void mg_print_time(); +switch_status_t mg_activate_ito_timer(megaco_profile_t* profile); + +void mg_restart_inactivity_timer(megaco_profile_t* profile); + +switch_status_t mgco_process_mgc_failure(SuId suId); +void mg_apply_tdm_dtmf_removal(mg_termination_t* term, mg_context_t *mg_ctxt); +void mg_apply_tdm_ec(mg_termination_t* term, mg_context_t *mg_ctxt); + + +/****************************************************************************************************************/ +/* MG Stack defines */ + +/* Free Commands inside MG CH command */ +#define mg_free_cmd(_cmd) mgFreeEventMem(_cmd) + +/****************************************************************************************************************/ + +#endif /* _MEGACO_STACK_H_ */ diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway_stack_alarms.c b/src/mod/endpoints/mod_media_gateway/media_gateway_stack_alarms.c new file mode 100644 index 0000000000..4c3e507fd1 --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/media_gateway_stack_alarms.c @@ -0,0 +1,509 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Kapil Gupta +* All rights reserved. +* +* +*/ + +/* INCLUDES *******************************************************************/ +#include "mod_media_gateway.h" +#include "media_gateway_stack.h" +/******************************************************************************/ + +/* FUNCTION PROTOTYPES ********************************************************/ + +/******************************************************************************/ + +/* FUNCTIONS ******************************************************************/ + +void handle_mg_alarm(Pst *pst, MgMngmt *usta) +{ + U16 ret; + int len = 0x00; + char prBuf[3048]; + + memset(&prBuf[0], 0, sizeof(prBuf)); + + len = len + sprintf(prBuf+len,"MG Status Indication: received for sapId[%d] with Category = %d, Event = %d, Cause = %d \n", + usta->t.usta.alarmInfo.sapId, usta->t.usta.alarm.category, usta->t.usta.alarm.event, + usta->t.usta.alarm.cause); + + len = len + sprintf(prBuf+len, "Category ( "); + + switch (usta->t.usta.alarm.category) + { + case LCM_CATEGORY_PROTOCOL: + { + len = len + sprintf(prBuf+len, "protocol related "); + break; + } + case LCM_CATEGORY_INTERFACE: + { + len = len + sprintf(prBuf+len, "interface related "); + break; + } + case LCM_CATEGORY_INTERNAL: + { + len = len + sprintf(prBuf+len, "internal "); + break; + } + case LCM_CATEGORY_RESOURCE: + { + len = len + sprintf(prBuf+len, "system resources related "); + break; + } + case LCM_CATEGORY_PSF_FTHA: + { + len = len + sprintf(prBuf+len, "fault tolerance / high availability PSF related "); + break; + } + case LCM_CATEGORY_LYR_SPECIFIC: + { + len = len + sprintf(prBuf+len, "MGCP related "); + break; + } + default: + { + len = len + sprintf(prBuf+len, "unknown: %d", (int)(usta->t.usta.alarm.category)); + break; + } + } + len = len + sprintf(prBuf+len, ") "); + + len = len + sprintf(prBuf+len, " Event ( "); + switch (usta->t.usta.alarm.event) + { + case LMG_EVENT_ALL_MGC_FAILED: + { + //mgco_process_mgc_failure(usta->t.usta.alarmInfo.sapId); + len = len + sprintf(prBuf+len, "ALL MGC Failed "); + break; + } + case LMG_EVENT_MGC_FAILED: + { + len = len + sprintf(prBuf+len, "MGC Failed "); + break; + } + case LMG_EVENT_TSAP_RECVRY_SUCCESS: + { + len = len + sprintf(prBuf+len, "TSAP recovery success"); + break; + } + case LMG_EVENT_TSAP_RECVRY_FAILED: + { + len = len + sprintf(prBuf+len, "TSAP recovery failed"); + break; + } + case LCM_EVENT_UI_INV_EVT: + { + len = len + sprintf(prBuf+len, "upper interface invalid event"); + break; + } + case LCM_EVENT_LI_INV_EVT: + { + len = len + sprintf(prBuf+len, "lower interface invalid event"); + break; + } + case LCM_EVENT_PI_INV_EVT: + { + len = len + sprintf(prBuf+len, "peer interface invalid event"); + break; + } + case LCM_EVENT_INV_EVT: + { + len = len + sprintf(prBuf+len, "general invalid event"); + break; + } + case LCM_EVENT_INV_STATE: + { + len = len + sprintf(prBuf+len, "invalid internal state"); + break; + } + case LCM_EVENT_INV_TMR_EVT: + { + len = len + sprintf(prBuf+len, "invalid timer event"); + break; + } + case LCM_EVENT_MI_INV_EVT: + { + len = len + sprintf(prBuf+len, "management interface invalid event"); + break; + } + case LCM_EVENT_BND_FAIL: + { + len = len + sprintf(prBuf+len, "bind failure"); + break; + } + case LCM_EVENT_NAK: + { + len = len + sprintf(prBuf+len, "destination nack"); + break; + } + case LCM_EVENT_TIMEOUT: + { + len = len + sprintf(prBuf+len, "timeout"); + break; + } + case LCM_EVENT_BND_OK: + { + len = len + sprintf(prBuf+len, "bind ok"); + break; + } + case LCM_EVENT_SMEM_ALLOC_FAIL: + { + len = len + sprintf(prBuf+len, "static memory allocation failed"); + break; + } + case LCM_EVENT_DMEM_ALLOC_FAIL: + { + len = len + sprintf(prBuf+len, "dynamic mmemory allocation failed"); + break; + } + case LCM_EVENT_LYR_SPECIFIC: + { + len = len + sprintf(prBuf+len, "MGCP specific"); + break; + } + default: + { + len = len + sprintf(prBuf+len, "unknown event %d", (int)(usta->t.usta.alarm.event)); + break; + } + case LMG_EVENT_HIT_BNDCFM: + { + len = len + sprintf(prBuf+len, "HIT bind confirm"); + break; + } + case LMG_EVENT_HIT_CONCFM: + { + len = len + sprintf(prBuf+len, "HIT connect confirm"); + break; + } + case LMG_EVENT_HIT_DISCIND: + { + len = len + sprintf(prBuf+len, "HIT disconnect indication"); + break; + } + case LMG_EVENT_HIT_UDATIND: + { + len = len + sprintf(prBuf+len, "HIT unit data indication"); + break; + } + case LMG_EVENT_MGT_BNDREQ: + { + len = len + sprintf(prBuf+len, "MGT bind request"); + break; + } + case LMG_EVENT_PEER_CFG_FAIL: + { + len = len + sprintf(prBuf+len, "Peer Configuration Failed"); + break; + } + case LMG_EVENT_MGT_UBNDREQ: + { + len = len + sprintf(prBuf+len, "MGT unbind request"); + break; + } + case LMG_EVENT_MGT_MGCPTXNREQ: + { + len = len + sprintf(prBuf+len, "MGT MGCP transaction request"); + break; + } + case LMG_EVENT_MGT_MGCPTXNIND: + { + len = len + sprintf(prBuf+len, "MGT MGCP transaction indication"); + break; + } + + case LMG_EVENT_PEER_ENABLED: + { + len = len + sprintf(prBuf+len, "gateway enabled"); + + /* gateway enabled now we can send termination service change for all terminations */ + mgco_init_ins_service_change( usta->t.usta.alarmInfo.sapId ); + break; + } + case LMG_EVENT_PEER_DISCOVERED: + { + len = len + sprintf(prBuf+len, "gateway discovered , notified entity"); + break; + } + case LMG_EVENT_PEER_REMOVED: + { + len = len + sprintf(prBuf+len, "gateway removed"); + break; + } + case LMG_EVENT_RES_CONG_ON: + { + len = len + sprintf(prBuf+len, "resource congestion ON"); + break; + } + case LMG_EVENT_RES_CONG_OFF: + { + len = len + sprintf(prBuf+len, "resource congestion OFF"); + break; + } + case LMG_EVENT_TPTSRV: + { + len = len + sprintf(prBuf+len, "transport service"); + break; + } + case LMG_EVENT_SSAP_ENABLED: + { + len = len + sprintf(prBuf+len, "SSAP enabled"); + break; + } + case LMG_EVENT_NS_NOT_RESPONDING: + { + len = len + sprintf(prBuf+len, "name server not responding"); + break; + } + case LMG_EVENT_TPT_FAILED: + { + len = len + sprintf(prBuf+len, "transport failure"); + break; + } + } + + len = len + sprintf(prBuf+len, " ) "); + + len = len + sprintf(prBuf+len, " cause ( "); + switch (usta->t.usta.alarm.cause) + { + case LCM_CAUSE_UNKNOWN: + { + len = len + sprintf(prBuf+len, "unknown"); + break; + } + case LCM_CAUSE_OUT_OF_RANGE: + { + len = len + sprintf(prBuf+len, "out of range"); + break; + } + case LCM_CAUSE_INV_SAP: + { + len = len + sprintf(prBuf+len, "NULL/unknown sap"); + break; + } + case LCM_CAUSE_INV_SPID: + { + len = len + sprintf(prBuf+len, "invalid service provider"); + break; + } + case LCM_CAUSE_INV_SUID: + { + len = len + sprintf(prBuf+len, "invalid service user"); + break; + } + case LCM_CAUSE_INV_NETWORK_MSG: + { + len = len + sprintf(prBuf+len, "invalid network message"); + break; + } + case LCM_CAUSE_DECODE_ERR: + { + len = len + sprintf(prBuf+len, "message decoding problem"); + break; + } + case LCM_CAUSE_USER_INITIATED: + { + len = len + sprintf(prBuf+len, "user initiated"); + break; + } + case LCM_CAUSE_MGMT_INITIATED: + { + len = len + sprintf(prBuf+len, "management initiated"); + break; + } + case LCM_CAUSE_INV_STATE: /* cause and event! */ + { + len = len + sprintf(prBuf+len, "invalid state"); + break; + } + case LCM_CAUSE_TMR_EXPIRED: /* cause and event! */ + { + len = len + sprintf(prBuf+len, "timer expired"); + break; + } + case LCM_CAUSE_INV_MSG_LENGTH: + { + len = len + sprintf(prBuf+len, "invalid message length"); + break; + } + case LCM_CAUSE_PROT_NOT_ACTIVE: + { + len = len + sprintf(prBuf+len, "protocol layer not active"); + break; + } + case LCM_CAUSE_INV_PAR_VAL: + { + len = len + sprintf(prBuf+len, "invalid parameter value"); + break; + } + case LCM_CAUSE_NEG_CFM: + { + len = len + sprintf(prBuf+len, "negative confirmation"); + break; + } + case LCM_CAUSE_MEM_ALLOC_FAIL: + { + len = len + sprintf(prBuf+len, "memory allocation failure"); + break; + } + case LCM_CAUSE_HASH_FAIL: + { + len = len + sprintf(prBuf+len, "hashing failure"); + break; + } + case LCM_CAUSE_LYR_SPECIFIC: + { + len = len + sprintf(prBuf+len, "MGCP specific"); + break; + } + default: + { + len = len + sprintf(prBuf+len, "unknown %d", (int)(usta->t.usta.alarm.cause)); + break; + } + case LMG_CAUSE_TPT_FAILURE: /* make up your mind - cause or event? */ + { + len = len + sprintf(prBuf+len, "transport failure"); + break; + } + case LMG_CAUSE_NS_NOT_RESPONDING: + { + len = len + sprintf(prBuf+len, "name server not responding"); + break; + } + } + len = len + sprintf(prBuf+len, " ) "); + + len = len + sprintf(prBuf+len, " Alarm parameters ( "); + ret = smmgGetAlarmInfoField(&usta->t.usta); + switch (ret) + { + case SMMG_UNKNOWNFIELD: + { + len = len + sprintf(prBuf+len, "invalid "); + + break; + } + case SMMG_PEERINFO: + { + /* + * Invoke the new function for printing the MgPeerInfo & + * delete all print code here + */ + smmgPrntPeerInfo(&(usta->t.usta.alarmInfo.u.peerInfo)); + break; + } + case SMMG_SAPID: + { + len = len + sprintf(prBuf+len, "SAP ID %d\n", (int)(usta->t.usta.alarmInfo.u.sapId)); + break; + } + case SMMG_MEM: + { + len = len + sprintf(prBuf+len, "memory region %d pool %d\n", + (int)(usta->t.usta.alarmInfo.u.mem.region), + (int)(usta->t.usta.alarmInfo.u.mem.pool)); + + break; + } + case SMMG_SRVSTA: + { + smmgPrntSrvSta(&usta->t.usta.alarmInfo.u.srvSta); + break; + } + case SMMG_PEERSTA: + { + smmgPrntPeerSta(&usta->t.usta.alarmInfo.u.peerSta); + break; + } + case SMMG_SSAPSTA: + { + smmgPrntSsapSta(&usta->t.usta.alarmInfo.u.ssapSta); + break; + } + case SMMG_PARID: + { + len = len + sprintf(prBuf+len, "parameter type: "); + switch (usta->t.usta.alarmInfo.u.parId.parType) + { + case LMG_PAR_TPTADDR: len = len + sprintf(prBuf+len, "transport address"); break; + case LMG_PAR_MBUF: len = len + sprintf(prBuf+len, "message buffer"); break; + case LMG_PAR_CHOICE: len = len + sprintf(prBuf+len, "choice"); break; + case LMG_PAR_SPID: len = len + sprintf(prBuf+len, "spId"); break; + default: len = len + sprintf(prBuf+len, "unknown"); break; + } + + len = len + sprintf(prBuf+len, ", value %d\n", + (int)(usta->t.usta.alarmInfo.u.parId.u.sapId)); + + break; + } + case SMMG_NOT_APPL: + { + len = len + sprintf(prBuf+len, "not applicable\n"); + break; + } + + /*TODO*/ + } + len = len + sprintf(prBuf+len, " ) "); + len = len + sprintf(prBuf+len, " \n "); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s \n", prBuf); +} + +/*****************************************************************************************************************************/ +void handle_tucl_alarm(Pst *pst, HiMngmt *sta) +{ + /* To print the general information */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Recieved a status indication from TUCL layer \n\n"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " Category = %d , event = %d , cause = %d\n", + sta->t.usta.alarm.category, + sta->t.usta.alarm.event, sta->t.usta.alarm.cause); + + switch(sta->t.usta.alarm.event) + { + case LCM_EVENT_INV_EVT: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," [HI_USTA]: LCM_EVENT_INV_EVT with type (%d)\n\n", + sta->t.usta.info.type); + break; + } + case LHI_EVENT_BNDREQ: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," [HI_USTA]: LHI_EVENT_BNDREQ with type (%d) spId (%d)\n\n", + sta->t.usta.info.type, sta->t.usta.info.spId); + break; + } + case LHI_EVENT_SERVOPENREQ: + case LHI_EVENT_DATREQ: + case LHI_EVENT_UDATREQ: + case LHI_EVENT_CONREQ: + case LHI_EVENT_DISCREQ: +#if(defined(HI_TLS) && defined(HI_TCP_TLS)) + case LHI_EVENT_TLS_ESTREQ: +#endif + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO," [HI_USTA]: partype (%d) type(%d)\n\n", + sta->t.usta.info.inf.parType, sta->t.usta.info.type); + break; + } + case LCM_EVENT_DMEM_ALLOC_FAIL: + case LCM_EVENT_SMEM_ALLOC_FAIL: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " [HI_USTA]: MEM_ALLOC_FAIL with region(%d) pool (%d) type(%d)\n\n", + sta->t.usta.info.inf.mem.region, sta->t.usta.info.inf.mem.pool, + sta->t.usta.info.type); + break; + } + default: + break; + } + +} /* handle_sng_tucl_alarm */ +/******************************************************************************/ + diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway_utils.c b/src/mod/endpoints/mod_media_gateway/media_gateway_utils.c new file mode 100644 index 0000000000..02f568a78d --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/media_gateway_utils.c @@ -0,0 +1,2407 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Mathieu Rene +* All rights reserved. +* +* +*/ + +#include "mod_media_gateway.h" +#include "media_gateway_stack.h" + + +/*****************************************************************************************************************************/ +switch_status_t mg_stack_alloc_mem( Ptr* _memPtr, Size _memSize ) +{ + Mem sMem; + + sMem.region = S_REG; + sMem.pool = S_POOL; + + if ( _memSize <= 0 ) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Failed mg_stack_alloc_mem: invalid size\n"); + return SWITCH_STATUS_FALSE; + } + + if ( ROK != cmAllocEvnt( _memSize, MG_MAXBLKSIZE, &sMem, _memPtr ) ) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Failed mg_stack_alloc_mem: cmAllocEvnt return failure for _memSize=%d\n",(int)_memSize); + return SWITCH_STATUS_FALSE; + } + + // Note: memset done inside stack api + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************************/ + +switch_status_t mg_stack_get_mem(MgMgcoMsg* msg, Ptr* _memPtr, Size _memSize ) +{ + if ( _memSize <= 0 ) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Failed mg_stack_get_mem: invalid size\n"); + return SWITCH_STATUS_FALSE; + } + + if ( !msg ) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Failed mg_stack_get_mem: invalid message\n"); + return SWITCH_STATUS_FALSE; + } + + if ( cmGetMem( (Ptr)msg, _memSize, (Ptr*)_memPtr ) != ROK ) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Failed alloc_mg_stack_mem: get memory failed _memSize=%d\n", (int)_memSize ); + return SWITCH_STATUS_FALSE; + } + + // Note: memset done inside stack api + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************************/ + +switch_status_t mg_stack_free_mem(void* msg) +{ + if ( !msg ) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Failed mg_stack_get_mem: invalid message\n"); + return SWITCH_STATUS_FALSE; + } + + cmFreeMem( (Ptr)msg ); + + return SWITCH_STATUS_SUCCESS; +} + + +/*****************************************************************************************************************************/ + +/* To see if term is in service or not */ +switch_status_t mg_stack_termination_is_in_service(megaco_profile_t* mg_profile, char* term_str,int len) +{ + mg_termination_t* term = NULL; + term = megaco_find_termination(mg_profile, term_str); + + + if(term && MG_TERM_RTP == term->type) { + return SWITCH_STATUS_SUCCESS; + }else if(term && (MG_TERM_TDM == term->type)){ + if(switch_test_flag(term, MG_IN_SERVICE)){ + return SWITCH_STATUS_SUCCESS; + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, " Term[%s] not in service\n", term_str); + return SWITCH_STATUS_FALSE; + } + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Failed to find term for term string[%s]\n", term_str); + return SWITCH_STATUS_FALSE; + } +} + +/*****************************************************************************************************************************/ + +S16 mg_fill_mgco_termid ( MgMgcoTermId *termId, char* term_str, int term_len, CmMemListCp *memCp) +{ + char prnt_buf[128]; +#ifdef GCP_ASN + Size size; +#endif + S16 ret = ROK; + Ptr* ptr = NULL; + + memset(&prnt_buf,0,sizeof(prnt_buf)); + + termId->type.pres = PRSNT_NODEF; + + if(!strcmp(term_str,"ROOT")){ + termId->type.val = MGT_TERMID_ROOT; + } else { + + termId->type.val = MGT_TERMID_OTHER; + + termId->name.pres.pres = PRSNT_NODEF; + + /* domain */ + termId->name.dom.pres = NOTPRSNT; + termId->name.dom.len = 0x00; + + ptr = (Ptr*)&termId->name.dom.val; + if( cmGetMem(memCp, 128, ptr) != ROK ){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"cmGetMem failed \n"); + RETVALUE(RFAILED); + } + + /* term name */ + termId->name.lcl.pres = PRSNT_NODEF; + termId->name.lcl.len = cmStrlen((CONSTANT U8*)term_str); + + ptr = (Ptr*)&termId->name.lcl.val; + if( cmGetMem(memCp, termId->name.lcl.len, ptr) != ROK ){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"cmGetMem failed \n"); + RETVALUE(RFAILED); + } + + cmMemset((U8 *)(termId->name.lcl.val), (U8)0, (PTR)(termId->name.lcl.len)); + + cmMemcpy((U8*)(termId->name.lcl.val), (CONSTANT U8*)term_str,termId->name.lcl.len); + + cmMemcpy((U8*)(prnt_buf), (CONSTANT U8*)termId->name.lcl.val,termId->name.lcl.len); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "mg_fill_mgco_termid: Megaco termination name[%s], len[%d], input_term_str[%s], input_term_len[%d]\n", + prnt_buf, termId->name.lcl.len, term_str,term_len); + + //MG_STACK_MEM_FREE(termId->name.lcl.val, size); + } + + +#ifdef GCP_ASN + if((termId->type.val == MGT_TERMID_ALL) || + (termId->type.val == MGT_TERMID_CHOOSE)){ + /* Remove comment to fill other term ID + termId->wildcard.num.pres = NOTPRSNT; */ + /* Remove comment to fill wilcard term ID */ + termId->wildcard.num.pres = PRSNT_NODEF; + termId->wildcard.num.val = 1; + size = ((sizeof(MgMgcoWildcardField*))); + MG_GETMEM((termId->wildcard.wildcard),size,memCp, ret); + if( ret != ROK) + RETVALUE(ret); + + MG_GETMEM( ((termId->wildcard.wildcard)[0]),sizeof(MgMgcoWildcardField), + memCp, ret); + if( ret != ROK) + RETVALUE(ret); + + termId->wildcard.wildcard[0]->pres = PRSNT_NODEF; + termId->wildcard.wildcard[0]->len = 1; + termId->wildcard.wildcard[0]->val[0] = 0x55; + + }else{ + termId->wildcard.num.pres = NOTPRSNT; + } +#endif /* GCP_ASN */ + + RETVALUE(ROK); +} + +/*****************************************************************************************************************************/ +/* +* +* Fun: mg_get_term_id_list +* +* Desc: Utility function to get MgMgcoTermIdLst structure +* from MgMgcoCommand structure. +* GCP_VER_2_1 - we will have term id list instead of single term id +* +* Ret: If success, return pointer to MgMgcoTermIdLst. +* If failure, return Null. +* +* Notes: None +* +*/ + +MgMgcoTermIdLst *mg_get_term_id_list(MgMgcoCommand *cmd) +{ + uint8_t cmd_type = MGT_NONE; + uint8_t api_type = CM_CMD_TYPE_NONE; + MgMgcoTermIdLst * term_id = NULL; + + + /*-- mgCmdInd type represents the data structure for both + * incoming and outgoing requests, hence we can get the + * command type from there itself --*/ + cmd_type = cmd->u.mgCmdInd[0]->cmd.type.val; + + /*-- Find apiType --*/ + api_type = cmd->cmdType.val; + + switch (api_type) + { + case CH_CMD_TYPE_REQ: + case CH_CMD_TYPE_IND: + /* Based on Command Type, get to the TermId structure */ + switch (cmd_type) + { + case MGT_ADD: + if (cmd->u.mgCmdInd[0]->cmd.u.add.pres.pres) + term_id = &cmd->u.mgCmdInd[0]->cmd.u.add.termIdLst; + break; + + case MGT_MOVE: + if (cmd->u.mgCmdInd[0]->cmd.u.move.pres.pres) + term_id = &cmd->u.mgCmdInd[0]->cmd.u.move.termIdLst; + break; + + case MGT_MODIFY: + if (cmd->u.mgCmdInd[0]->cmd.u.mod.pres.pres) + term_id = &cmd->u.mgCmdInd[0]->cmd.u.mod.termIdLst; + break; + + case MGT_SUB: + if (cmd->u.mgCmdInd[0]->cmd.u.sub.pres.pres) + term_id = &cmd->u.mgCmdInd[0]->cmd.u.sub.termIdLst; + break; + + case MGT_AUDITCAP: + if (cmd->u.mgCmdInd[0]->cmd.u.acap.pres.pres) + term_id = &cmd->u.mgCmdInd[0]->cmd.u.acap.termIdLst; + break; + + case MGT_AUDITVAL: + if (cmd->u.mgCmdInd[0]->cmd.u.aval.pres.pres) + term_id = &cmd->u.mgCmdInd[0]->cmd.u.aval.termIdLst; + break; + + case MGT_NTFY: + if (cmd->u.mgCmdInd[0]->cmd.u.ntfy.pres.pres) + term_id = &cmd->u.mgCmdInd[0]->cmd.u.ntfy.termIdLst; + break; + + case MGT_SVCCHG: + if (cmd->u.mgCmdInd[0]->cmd.u.svc.pres.pres) + term_id = &cmd->u.mgCmdInd[0]->cmd.u.svc.termIdLst; + break; + + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s: failed, Unsupported Command[%s]\n", __PRETTY_FUNCTION__, PRNT_MG_CMD(cmd_type)); + break; + } + break; + + case CH_CMD_TYPE_RSP: + case CH_CMD_TYPE_CFM: + + cmd_type = cmd->u.mgCmdRsp[0]->type.val; + + switch (cmd_type) + { + case MGT_ADD: + if (cmd->u.mgCmdRsp[0]->u.add.pres.pres) + term_id = &cmd->u.mgCmdRsp[0]->u.add.termIdLst; + break; + + case MGT_MOVE: + if (cmd->u.mgCmdRsp[0]->u.move.pres.pres) + term_id = &cmd->u.mgCmdRsp[0]->u.move.termIdLst; + break; + + case MGT_MODIFY: + if (cmd->u.mgCmdRsp[0]->u.mod.pres.pres) + term_id = &cmd->u.mgCmdRsp[0]->u.mod.termIdLst; + break; + + case MGT_SUB: + if (cmd->u.mgCmdRsp[0]->u.sub.pres.pres) + term_id = &cmd->u.mgCmdRsp[0]->u.sub.termIdLst; + break; + + case MGT_SVCCHG: + if (cmd->u.mgCmdRsp[0]->u.svc.pres.pres) + term_id = &cmd->u.mgCmdRsp[0]->u.svc.termIdLst; + break; + + case MGT_AUDITVAL: + if (cmd->u.mgCmdRsp[0]->u.aval.u.other.pres.pres) + term_id = &cmd->u.mgCmdRsp[0]->u.aval.u.other.termIdLst; + break; + + case MGT_AUDITCAP: + if (cmd->u.mgCmdRsp[0]->u.acap.u.other.pres.pres) + term_id = &cmd->u.mgCmdRsp[0]->u.acap.u.other.termIdLst; + break; + + case MGT_NTFY: + if (cmd->u.mgCmdRsp[0]->u.ntfy.pres.pres) + term_id = &cmd->u.mgCmdRsp[0]->u.ntfy.termIdLst; + break; + + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s: failed, Unsupported Command[%s]\n", __PRETTY_FUNCTION__, PRNT_MG_CMD(cmd_type)); + } /* switch command type for reply */ + break; + + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s: failed, Unsupported api_type[%s]!\n", __PRETTY_FUNCTION__, PRNT_MG_CMD_TYPE(api_type)); + break; + } /* switch -api_type */ + + return (term_id); +} + + +/*****************************************************************************************************************************/ + +void mg_util_set_txn_string(MgStr *errTxt, U32 *txnId) +{ + MG_ZERO(errTxt->val, sizeof(errTxt->val)); + errTxt->len = 0; + + errTxt->val[errTxt->len] = '\"'; + errTxt->len += 1; + + if (MG_TXN_INVALID == txnId ) + { + MG_MEM_COPY((&errTxt->val[errTxt->len]), "TransactionId=0", 15); + errTxt->len += 15; + } + + errTxt->val[errTxt->len] = '\"'; + errTxt->len += 1; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s:" + "info, error-text is: %s\n", __PRETTY_FUNCTION__,errTxt->val); + +} + +/*****************************************************************************************************************************/ +void mg_util_set_err_string ( MgStr *errTxt, char* str) +{ + MG_ZERO((errTxt->val), sizeof(errTxt->val)); + errTxt->len = strlen(str); + strcpy((char*)&errTxt->val, str); +} +/*****************************************************************************************************************************/ +void mg_util_set_ctxt_string ( MgStr *errTxt, MgMgcoContextId *ctxtId) +{ + MG_ZERO((errTxt->val), sizeof(errTxt->val)); + errTxt->len = 0; + if(ctxtId->type.pres != NOTPRSNT) + { + errTxt->val[errTxt->len] = '\"'; + errTxt->len += 1; + if(ctxtId->type.val == MGT_CXTID_NULL) + { + errTxt->val[errTxt->len] = '-'; + errTxt->len += 1; + } + else if(ctxtId->type.val == MGT_CXTID_ALL) + { + errTxt->val[errTxt->len] = '*'; + errTxt->len += 1; + } + else if(ctxtId->type.val == MGT_CXTID_CHOOSE) + { + errTxt->val[errTxt->len] = '$'; + errTxt->len += 1; + } + else if((ctxtId->type.val == MGT_CXTID_OTHER) && (ctxtId->val.pres != NOTPRSNT)) + { +#ifdef BIT_64 + sprintf((char*)&errTxt->val[errTxt->len], "%d", ctxtId->val.val); +#else + sprintf((char*)&errTxt->val[errTxt->len], "%lu", ctxtId->val.val); +#endif + errTxt->len += cmStrlen((U8*)(&errTxt->val[errTxt->len])); + } + + errTxt->val[errTxt->len] = '\"'; + errTxt->len += 1; + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s:" + "info, error-text is: %s\n", __PRETTY_FUNCTION__,errTxt->val); +} + +/*****************************************************************************************************************************/ + +void mg_util_set_cmd_name_string (MgStr *errTxt, MgMgcoCommand *cmd) +{ + MG_ZERO((errTxt->val), sizeof(errTxt->val)); + errTxt->len = 0; + + if ((!cmd) && (!cmd->u.mgCmdInd[0])) { + switch(cmd->u.mgCmdInd[0]->cmd.type.val) + { + case MGT_AUDITCAP: + errTxt->val[0]='\"'; + errTxt->val[1]='A'; + errTxt->val[2]='u'; + errTxt->val[3]='d'; + errTxt->val[4]='i'; + errTxt->val[5]='t'; + errTxt->val[6]='C'; + errTxt->val[7]='a'; + errTxt->val[8]='p'; + errTxt->val[9]='a'; + errTxt->val[10]='b'; + errTxt->val[11]='i'; + errTxt->val[12]='l'; + errTxt->val[13]='i'; + errTxt->val[14]='t'; + errTxt->val[15]='y'; + errTxt->val[16]='\"'; + errTxt->len = 17; + break; + + case MGT_MOVE: + errTxt->val[0]='\"'; + errTxt->val[1]='M'; + errTxt->val[2]='o'; + errTxt->val[3]='v'; + errTxt->val[4]='e'; + errTxt->val[5]='\"'; + errTxt->len = 6; + break; + + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s: Not expected command Type[%d]\n", + __PRETTY_FUNCTION__,cmd->u.mgCmdInd[0]->cmd.type.val); + + break; + } + } +} + +/*****************************************************************************************************************************/ +void mgco_handle_sdp_attr_set(CmSdpAttrSet *s, mg_termination_t* term) +{ + int i=0x00; + int rfc2833_pres = 0x00; + if (s->numComp.pres) { + for (i = 0; i < s->numComp.val; i++) { + CmSdpAttr *a = s->attr[i]; + + if(NOTPRSNT == a->type.pres) continue; + + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Attribute Type[%d]\n",a->type.val); + + switch(a->type.val) + { + case CM_SDP_ATTR_GENERIC: + { + break; + } + case CM_SDP_ATTR_CAT: + { + break; + } + + case CM_SDP_ATTR_KEYWDS: + { + break; + } + case CM_SDP_ATTR_TOOL: + { + break; + } + case CM_SDP_ATTR_PTIME: + { +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t PTIME = %d \n", + (NOTPRSNT != a->u.ptime.pres)?a->u.ptime.val:-1); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t PTIME = %ld \n", + (NOTPRSNT != a->u.ptime.pres)?a->u.ptime.val:-1); +#endif + if(MG_TERM_RTP == term->type){ + term->u.rtp.ptime = a->u.ptime.val; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,"Updating ptime to [%d]\n", term->u.rtp.ptime); + } + break; + } + case CM_SDP_ATTR_RECVONLY: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t CM_SDP_ATTR_RECVONLY: \n"); + break; + } + case CM_SDP_ATTR_SENDRECV: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t CM_SDP_ATTR_SENDRECV: \n"); + break; + } + case CM_SDP_ATTR_SENDONLY: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t CM_SDP_ATTR_SENDONLY: \n"); + break; + } + case CM_SDP_ATTR_ORIENT: + { + break; + } + case CM_SDP_ATTR_TYPE: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t CM_SDP_ATTR_TYPE: \n"); + break; + } + case CM_SDP_ATTR_CHARSET: + { + break; + } + + case CM_SDP_ATTR_SDPLANG: + { + break; + } + + case CM_SDP_ATTR_LANG: + { + break; + } + case CM_SDP_ATTR_FRAMERATE: + { + break; + } + case CM_SDP_ATTR_QUALITY: + { + break; + } + case CM_SDP_ATTR_FMTP: + { + CmSdpAttrFmtp* f = &a->u.fmtp; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t CM_SDP_ATTR_FMTP: \n"); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t Format Type = %d \n",(NOTPRSNT != f->type.pres)?f->type.val:-1); + + break; + } + case CM_SDP_ATTR_RTPMAP: + { + CmSdpAttrRtpMap* r = &a->u.rtpmap; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t CM_SDP_ATTR_RTPMAP: \n"); + + if(NOTPRSNT != r->pres.pres){ + + /* payload type */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Payload Type = %d \n", + (NOTPRSNT != r->pay.type.pres)?r->pay.type.val:-1); + + /* payload value */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Payload Value = %d \n", + (NOTPRSNT != r->pay.val.pres)?r->pay.val.val:-1); + + /* encoding name */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Encoding Name value = %d \n", + (NOTPRSNT != r->enc.val.pres)?r->enc.val.val:-1); + + if((NOTPRSNT != r->enc.val.pres ) && + (CM_SDP_ENC_TELEPHONE_EVENT == r->enc.val.val)){ + + rfc2833_pres = 0x01; + term->u.rtp.rfc2833_pt=r->pay.val.val; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " Updating rfc2833_pt to [%d] \n", term->u.rtp.rfc2833_pt); + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Encoding Name name = %s \n", + (NOTPRSNT != r->enc.name.pres)? + (char*)r->enc.name.val:"Not Present"); + +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Clock Rate = %d \n", + (NOTPRSNT != r->clk.pres)?r->clk.val:-1); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Clock Rate = %ld \n", + (NOTPRSNT != r->clk.pres)?r->clk.val:-1); +#endif + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Encoding Parameters = %s \n", + (NOTPRSNT != r->parms.pres)? + (char*)r->parms.val:"Not Present"); + } + break; + } + case CM_SDP_ATTR_INACTIVE: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t CM_SDP_ATTR_INACTIVE: \n"); + break; + } + case CM_SDP_ATTR_CONTROL: + { + break; + } + case CM_SDP_ATTR_RANGE: + { + break; + } + case CM_SDP_ATTR_ETAG: + { + break; + } + case CM_SDP_ATTR_ATMMAP: + { + break; + } + case CM_SDP_ATTR_EECID: + { + break; + } + case CM_SDP_ATTR_AALTYPE: + { + break; + } + case CM_SDP_ATTR_SILENCESUPP: + { + break; + } + case CM_SDP_ATTR_ECAN: + { + break; + } + case CM_SDP_ATTR_GC: + { + break; + } + case CM_SDP_ATTR_PROFILEDESC: + { + break; + } + case CM_SDP_ATTR_VSEL: + { + break; + } + case CM_SDP_ATTR_DSEL: + { + break; + } + case CM_SDP_ATTR_FSEL: + { + break; + } + case CM_SDP_ATTR_CAPABILITY: + { + break; + } + case CM_SDP_ATTR_QOSCLASS: + { + break; + } + case CM_SDP_ATTR_BCOB: + { + break; + } + case CM_SDP_ATTR_STC: + { + break; + } + case CM_SDP_ATTR_UPCC: + { + break; + } + case CM_SDP_ATTR_ATMQOSPARMS: + { + break; + } + case CM_SDP_ATTR_AAL2QOSFPARMS: + { + break; + } + case CM_SDP_ATTR_AAL2QOSBPARMS: + { + break; + } + case CM_SDP_ATTR_ATMTRFCDESC: + { + break; + } + case CM_SDP_ATTR_AAL2FTRFCDESC: + { + break; + } + case CM_SDP_ATTR_AAL2BTRFCDESC: + { + break; + } + case CM_SDP_ATTR_ABRPARMS: + { + break; + } + case CM_SDP_ATTR_CLKREC: + { + break; + } + case CM_SDP_ATTR_FEC: + { + break; + } + case CM_SDP_ATTR_PRTFL: + { + break; + } + case CM_SDP_ATTR_BEARERTYPE: + { + break; + } + case CM_SDP_ATTR_STRUCTURE: + { + break; + } + case CM_SDP_ATTR_SBC: + { + break; + } + case CM_SDP_ATTR_CPSSDUSIZE: + { + break; + } + case CM_SDP_ATTR_AAL2CPS: + { + break; + } + case CM_SDP_ATTR_ANYCAST: + { + break; + } + case CM_SDP_ATTR_WTP: + { + break; + } + case CM_SDP_ATTR_CACHE: + { + break; + } + case CM_SDP_ATTR_CHAIN: + { + break; + } + case CM_SDP_ATTR_PHONECONTEXT: + { + break; + } + case CM_SDP_ATTR_CLIR: + { + break; + } + case CM_SDP_ATTR_DIRECTION: + { + break; + } + case CM_SDP_ATTR_MAXPTIME: + { + break; + } + case CM_SDP_ATTR_T38_FAX: + { + CmSdpAttrT38Fax* f = &a->u.fax; + if(NOTPRSNT == f->type.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "CM_SDP_ATTR_T38_FAX: TYPE not present \n"); + break; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t CM_SDP_ATTR_T38_FAX: type=%d\n", f->type.val); + + if(NULL == term->u.rtp.t38_options){ + term->u.rtp.t38_options = + switch_core_alloc(term->pool, sizeof *term->u.rtp.t38_options); + } + switch(f->type.val) + { + case CM_SDP_ATTR_T38_FAX_VER: + { + term->u.rtp.t38_options->T38FaxVersion = f->u.num.val; + break; + } + case CM_SDP_ATTR_T38_MAX_BIT_RATE: + { + term->u.rtp.t38_options->T38MaxBitRate = f->u.num.val; + break; + } + case CM_SDP_ATTR_T38_FAX_FILL_BIT_RMVL: + { + term->u.rtp.t38_options->T38FaxFillBitRemoval = f->u.num.val; + break; + } + case CM_SDP_ATTR_T38_FAX_TRNS_MMR: + { + term->u.rtp.t38_options->T38FaxTranscodingMMR = f->u.num.val; + break; + } + case CM_SDP_ATTR_T38_FAX_TRNS_JBIG: + { + term->u.rtp.t38_options->T38FaxTranscodingJBIG = f->u.num.val; + break; + } + case CM_SDP_ATTR_T38_FAX_RATE_MNGMNT: + { + switch(f->u.val.val) + { + case CM_SDP_ATTR_T38_FAX_RATE_MNG_LOC_TCF: + { + term->u.rtp.t38_options->T38FaxRateManagement = + switch_core_strdup(term->pool,"localTCF") ; + break; + } + case CM_SDP_ATTR_T38_FAX_RATE_MNG_TRANSF_TCF: + { + term->u.rtp.t38_options->T38FaxRateManagement = + switch_core_strdup(term->pool,"transferredTCF") ; + break; + } + } + break; + } + case CM_SDP_ATTR_T38_FAX_MAX_BFR: + { + term->u.rtp.t38_options->T38FaxMaxBuffer = f->u.num.val; + break; + } + case CM_SDP_ATTR_T38_FAX_MAX_DATAGRAM: + { + term->u.rtp.t38_options->T38FaxMaxDatagram = f->u.num.val; + break; + } + case CM_SDP_ATTR_T38_FAX_UDP_EC: + { + switch(f->u.val.val) + { + case CM_SDP_ATTR_T38_FAX_UDP_EC_UDP_FEC: + { + term->u.rtp.t38_options->T38FaxUdpEC = + switch_core_strdup(term->pool,"t38UDPNoEC"); + break; + } + case CM_SDP_ATTR_T38_FAX_UDP_EC_UDP_RED: + { + term->u.rtp.t38_options->T38FaxUdpEC = + switch_core_strdup(term->pool,"t38UDPRedundancy") ; + break; + } + } + break; + } + case CM_SDP_ATTR_T38_FAX_UNKNOWN: + { + if(f->u.unknown.name.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "T38: Attribute : name[len=%d, value=%s] \n", + f->u.unknown.name.len,(char*)f->u.unknown.name.val); + } + if(f->u.unknown.val.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "T38: Attribute : value[len=%d, value=%s] \n", + f->u.unknown.val.len,(char*)f->u.unknown.val.val); + } + break; + } + + default: + break; + } + + +#if 0 + term->u.rtp.t38_options->T38FaxVersion = 0x01; + term->u.rtp.t38_options->T38MaxBitRate = 14400; + term->u.rtp.t38_options->T38FaxRateManagement = + switch_core_strdup(term->pool,"transferredTCF") ; + //term->u.rtp.t38_options->T38FaxMaxBuffer = ; + //term->u.rtp.t38_options->T38FaxMaxDatagram = ; + term->u.rtp.t38_options->T38FaxUdpEC = + switch_core_strdup(term->pool,"t38UDPRedundancy") ; + //term->u.rtp.t38_options->T38VendorInfo = +#endif + + break; + } + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Not supported Type[%d]\n",a->type.val); + break; + } + } + + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "a-line not present \n"); + } + + if(0x00 == rfc2833_pres){ + term->u.rtp.rfc2833_pt = 0x00; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " Resetting rfc2833_pt to [%d] \n", term->u.rtp.rfc2833_pt); + } +} + +void mgco_handle_sdp_c_line(CmSdpConn *s, mg_termination_t* term, mgco_sdp_types_e sdp_type) +{ + char ipadd[32]; + memset(ipadd, 0, sizeof(ipadd)); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "********** SDP connection line ****** \n"); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t Net Type = %d \n", + (NOTPRSNT != s->netType.type.pres)?s->netType.type.val:-1); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t Address Type = %d \n", + (NOTPRSNT != s->addrType.pres)?s->addrType.val:-1); + + if (s->addrType.pres && s->addrType.val == CM_SDP_ADDR_TYPE_IPV4 && + s->netType.type.val == CM_SDP_NET_TYPE_IN && + s->u.ip4.addrType.val == CM_SDP_IPV4_IP_UNI) { + + if (s->u.ip4.addrType.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Address: %d.%d.%d.%d\n", + s->u.ip4.u.uniIp.b[0].val, + s->u.ip4.u.uniIp.b[1].val, + s->u.ip4.u.uniIp.b[2].val, + s->u.ip4.u.uniIp.b[3].val); + + if(MG_SDP_REMOTE == sdp_type) { + sprintf(ipadd,"%d.%d.%d.%d", + s->u.ip4.u.uniIp.b[0].val, + s->u.ip4.u.uniIp.b[1].val, + s->u.ip4.u.uniIp.b[2].val, + s->u.ip4.u.uniIp.b[3].val); + /* update remote ip */ + if(MG_TERM_RTP == term->type){ + term->u.rtp.remote_addr = switch_core_strdup(term->pool,ipadd); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,"Update remote ip to [%s]\n", term->u.rtp.remote_addr); + } + } + } + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "**************** \n"); +} + +void mgco_print_CmSdpU8OrNil(CmSdpU8OrNil* p) +{ + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,"CmSdpU8OrNil: Type = %d \n", (NOTPRSNT != p->type.pres)?p->type.val:-1); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,"CmSdpU8OrNil: Value = %d \n", (NOTPRSNT != p->val.pres)?p->val.val:-1); +} + +const char* mg_get_codec_name(megaco_profile_t* mg_profile, int iana_code) +{ + int i = 0x00; + const switch_codec_implementation_t *codecs[16]; + char *codec_prefs[16] = { 0 }; + char *szcodec_prefs; + int codec_count; + const char *name = NULL; + + szcodec_prefs = strdup(mg_profile->codec_prefs); + codec_count = switch_split(szcodec_prefs, ',', codec_prefs); + + /* Get the list of codecs, by preference */ + switch_loadable_module_get_codecs_sorted(codecs, switch_arraylen(codecs), codec_prefs, switch_arraylen(codec_prefs)); + /* see if received codec is present in our codec supported list */ + for (i = 0; codecs[i] && i < codec_count; i++) { + if(iana_code == codecs[i]->ianacode){ + name = codecs[i]->iananame; + break; + } + } + + return name; +} + +void mgco_handle_sdp_media_param(CmSdpMedPar *s, mg_termination_t* term, mgco_sdp_types_e sdp_type, megaco_profile_t* mg_profile, CmSdpAttrSet *attrSet, CmMemListCp *memCp) +{ + int i=0x00; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "***** Media Parameter *********** \n"); + if (s->numProtFmts.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Media Formats = %d \n", s->numProtFmts.val); + for (i = 0; i < s->numProtFmts.val; i++) { + CmSdpMedProtoFmts *a = s->pflst[i]; + + /*Prot*/ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " Proto Type = %d \n", (NOTPRSNT != a->prot.type.pres)?a->prot.type.val:-1); + switch(a->prot.type.val) + { + case CM_SDP_MEDIA_PROTO_UNKNOWN: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, " Proto Type Unknown , name = %s \n", + (NOTPRSNT != a->prot.u.name.pres)?(char*)a->prot.u.name.val:"Not Present "); + break; + } + case CM_SDP_MEDIA_PROTO_RTP: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, " Proto Type RTP , subtype = %d \n", + (NOTPRSNT != a->prot.u.subtype.type.pres)?a->prot.u.subtype.type.val: -1); + break; + } + case CM_SDP_MEDIA_PROTO_UDPTL: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " Proto Type T38 -UDPTL , subtype = %d \n", + (NOTPRSNT != a->prot.u.subtype.type.pres)?a->prot.u.subtype.type.val: -1); + } + } + + /*repeated from "prot" field */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, " Proto Type = %d \n", + (NOTPRSNT != a->protType.pres)?a->protType.val: -1); + + switch(a->protType.val) + { + case CM_SDP_MEDIA_PROTO_RTP: + { + CmSdpMedFmtRtpList* r = &a->u.rtp; + int i = 0x00; + term->u.rtp.media_type = MGM_AUDIO; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, " CM_SDP_MEDIA_PROTO_RTP: \n"); + if(NOTPRSNT != r->num.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " Number of Formats[%d] \n", r->num.val); + + for(i=0;inum.val;i++){ + mgco_print_CmSdpU8OrNil(r->fmts[i]); + + if(MG_RTP_AVP_PROFILE_A_LAW == r->fmts[i]->val.val){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, " MG_RTP_AVP_PROFILE_A_LAW: \n"); + }else if(MG_RTP_AVP_PROFILE_U_LAW == r->fmts[i]->val.val){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, " MG_RTP_AVP_PROFILE_U_LAW: \n"); + } + } + } + + /* Ideally remote descriptor should have supported codec..but just in case calling remove un-supported codecs api */ + mg_rem_unsupported_codecs(mg_profile, term , r, attrSet, memCp); + + + /* now whatever we have , that will be suported one */ + if((NOTPRSNT != r->num.pres) && (0 != r->num.val) && (NULL != r->fmts[0])){ + const char* name = mg_get_codec_name(mg_profile, r->fmts[0]->val.val); + if(name){ + if(MG_TERM_RTP == term->type){ + term->u.rtp.codec = name; + term->u.rtp.pt = r->fmts[0]->val.val; + /* Set default value of sampling rate depends on codec */ + switch(megaco_codec_parse(name)) + { + case MEGACO_CODEC_PCMA: + { + term->u.rtp.rate = 8000; + break; + } + case MEGACO_CODEC_PCMU: + { + /* TODO - proper values */ + term->u.rtp.rate = 8000; + break; + } + case MEGACO_CODEC_G729: + { + /* TODO - proper values */ + term->u.rtp.rate = 8000; + break; + } + case MEGACO_CODEC_G723_1: + { + /* TODO - proper values */ + term->u.rtp.rate = 8000; + break; + } + case MEGACO_CODEC_ILBC: + { + /* TODO - proper values */ + term->u.rtp.rate = 8000; + break; + } + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + " not updating sampling rate \n"); + break; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " Updating codec to[%d], name[%s] \n", + r->fmts[0]->val.val, name); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "Updating pt to [%d], rate to[%d]\n", + term->u.rtp.pt, term->u.rtp.rate); + } + }else{ + /* ERROR */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + " NO Codec Name found against iana[%d] \n", r->fmts[0]->val.val); + } + } + + break; + } + + case CM_SDP_MEDIA_PROTO_UDPTL: + { + CmSdpMedFmtUdptlList* t = &a->u.t38; + int i = 0; + + if(NOTPRSNT == t->num.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, + " CM_SDP_MEDIA_PROTO_UDPTL: no format defines..\n"); + } + + term->u.rtp.media_type = MGM_IMAGE; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " CM_SDP_MEDIA_PROTO_UDPTL: formats[%d]\n", t->num.val); + + for(i=0; i< t->num.val;i++) + { + CmSdpT38Fmt* f = t->fmts[i]; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " f->knownFmt.pres=%d, f->knownFmt.val=%d\n", + f->knownFmt.pres,f->knownFmt.val); + if(f->unknownFmt.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "f->unknownFmt.val[%s]\n",f->unknownFmt.val); + } + } + + break; + } + default: + break; + } + } + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "**************** \n"); +} + +void mgco_handle_incoming_sdp(CmSdpInfoSet *sdp, mg_termination_t* term, mgco_sdp_types_e sdp_type, megaco_profile_t* mg_profile, CmMemListCp *memCp) +{ + int i; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " Parsing %s SDP for Term[%s] \n", + (MG_SDP_LOCAL== sdp_type)?"MG_SDP_LOCAL":"MG_SDP_REMOTE", + (MG_TERM_RTP == term->type)?"MG_TERM_RTP":"MG_TERM_TDM"); + + if (sdp->numComp.pres == NOTPRSNT) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " No %s SDP present \n", + (MG_SDP_LOCAL== sdp_type)?"MG_SDP_LOCAL":"MG_SDP_REMOTE"); + return; + } + + for (i = 0; i < sdp->numComp.val; i++) { + CmSdpInfo *s = sdp->info[i]; + int mediaId; + + /************************************************************************************************************************/ + /* info presence check */ + if(NOTPRSNT == s->pres.pres) continue; + + /************************************************************************************************************************/ + /* Version */ + if(NOTPRSNT != s->ver.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + " SDP Version = %d \n", s->ver.val); + } + + /************************************************************************************************************************/ + /* Orig */ + if(NOTPRSNT != s->orig.pres.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "********** SDP orig line ****** \n \t Type = %d \n", + s->orig.type.val); + + if(NOTPRSNT != s->orig.orig.pres.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t User Name = %s \n", + (NOTPRSNT != s->orig.orig.usrName.pres)? + (char*)s->orig.orig.usrName.val:"Not Present"); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Session Id = %s \n", + (NOTPRSNT != s->orig.orig.sessId.pres)? + (char*)s->orig.orig.sessId.val:"Not Present"); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Session Version = %s \n", + (NOTPRSNT != s->orig.orig.sessVer.pres)? + (char*)s->orig.orig.sessVer.val:"Not Present"); + + /* sdpAddr */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t Net Type = %d \n", + (NOTPRSNT != s->orig.orig.sdpAddr.netType.type.pres)? + s->orig.orig.sdpAddr.netType.type.val:-1); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Address Type = %d \n", + (NOTPRSNT != s->orig.orig.sdpAddr.addrType.pres) + ?s->orig.orig.sdpAddr.addrType.val:-1); + + /* print IPV4 address */ + if (s->orig.orig.sdpAddr.addrType.pres && + s->orig.orig.sdpAddr.addrType.val == CM_SDP_ADDR_TYPE_IPV4 && + s->orig.orig.sdpAddr.netType.type.val == CM_SDP_NET_TYPE_IN && + s->orig.orig.sdpAddr.u.ip4.addrType.val == CM_SDP_IPV4_IP_UNI) + { + + if (s->orig.orig.sdpAddr.u.ip4.addrType.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "Address: %d.%d.%d.%d\n", + s->orig.orig.sdpAddr.u.ip4.u.ip.b[0].val, + s->orig.orig.sdpAddr.u.ip4.u.ip.b[1].val, + s->orig.orig.sdpAddr.u.ip4.u.ip.b[2].val, + s->orig.orig.sdpAddr.u.ip4.u.ip.b[3].val); + } + + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t O-line not present \n"); + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "********** ****** \n"); + } + } else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t O-line not present \n"); + } +/************************************************************************************************************************/ + /* Session Name (s = line) */ + + if(NOTPRSNT != s->sessName.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Session Name = %s \n", s->sessName.val); + } else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t s-line not present \n"); + } + + /************************************************************************************************************************/ + /* Session Info(i= line) */ + + if(NOTPRSNT != s->info.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Session Info = %s \n", s->info.val); + } else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t i-line not present \n"); + } + + /************************************************************************************************************************/ + /* Session Uri */ + + if(NOTPRSNT != s->uri.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Session Uri = %s \n", s->uri.val); + } else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t uri not present \n"); + } + + /************************************************************************************************************************/ + /* E-Mail */ + /* TODO */ + + + /************************************************************************************************************************/ + /* Phone */ + /* TODO */ + + + /************************************************************************************************************************/ + /* connection line */ + + mgco_handle_sdp_c_line(&s->conn, term, sdp_type); + /************************************************************************************************************************/ + /* Bandwidth */ + /* TODO */ + + /************************************************************************************************************************/ + /* SDP Time (t= line)*/ + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "*** t-line **************** \n"); + if(NOTPRSNT != s->sdpTime.pres.pres) { + if(NOTPRSNT != s->sdpTime.sdpOpTimeSet.numComp.pres) { + int i = 0x00; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "SDP op time present with total component[%d]\n", + s->sdpTime.sdpOpTimeSet.numComp.val); + for (i = 0;isdpTime.sdpOpTimeSet.numComp.val;i++){ + CmSdpOpTime* t = s->sdpTime.sdpOpTimeSet.sdpOpTime[i]; + if(NOTPRSNT == t->pres.pres) continue; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Start Time = %s \n", + (NOTPRSNT != t->startTime.pres)? + (char*)t->startTime.val:"Not Present"); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Stop Time = %s \n", + (NOTPRSNT != t->stopTime.pres)? + (char*)t->stopTime.val:"Not Present"); + + /*repeat time repFieldSet */ + + if(NOTPRSNT != t->repFieldSet.numComp.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "SDP repeat time present with total component[%d]\n", + t->repFieldSet.numComp.val); + + /*TODO - print repeat fields */ + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "SDP repeat time not present \n"); + } + } /* sdpOpTimeSet.numComp for loop -- end */ + }else{/*sdpOpTimeSet.numComp.pres if -- end */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SDP op time not present \n"); + } + + /*TODO - zoneAdjSet */ + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "t-line not present \n"); + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "**************** \n"); + + + /************************************************************************************************************************/ + /* key type (k= line)*/ + + if(NOTPRSNT != s->keyType.pres.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t Key Type = %d \n", + (NOTPRSNT != s->keyType.keyType.pres)?s->keyType.keyType.val:-1); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t Key Data = %s \n", + (NOTPRSNT != s->keyType.key_data.pres)? + (char*)s->keyType.key_data.val:"Not Present"); + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "k-line not present \n"); + } + + /************************************************************************************************************************/ + /* Attribute Set */ + + mgco_handle_sdp_attr_set(&s->attrSet, term); + + /************************************************************************************************************************/ + /* Media Descriptor Set */ + + if (s->mediaDescSet.numComp.pres) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "****** Media Descriptor Set present with numComp[%d]\n", + s->mediaDescSet.numComp.val); + for (mediaId = 0; mediaId < s->mediaDescSet.numComp.val; mediaId++) { + CmSdpMediaDesc *desc = s->mediaDescSet.mediaDesc[mediaId]; + + if(NOTPRSNT == desc->pres.pres) continue; + + /* Media Field */ + { + CmSdpMediaField* f = &desc->field; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Media Type = %d \n", + (NOTPRSNT != f->mediaType.pres)?f->mediaType.val:-1); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Media = %s \n",(NOTPRSNT != f->media.pres)? + (char*)f->media.val:"Not Present"); + + /* Channel ID */ + if(NOTPRSNT != f->id.type.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t VcId Type = %d \n", f->id.type.val); + switch(f->id.type.val){ + case CM_SDP_VCID_PORT: + { + CmSdpPort *p = &f->id.u.port; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "CM_SDP_VCID_PORT:\n"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t SDP port type = %d \n", (NOTPRSNT != p->type.pres)?p->type.val:-1); + switch(p->type.val) + { + case CM_SDP_PORT_INT: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t CM_SDP_PORT_INT: SDP port = %d type = %d \n", + p->u.portInt.port.val.val, p->u.portInt.port.type.val); + if(MG_SDP_REMOTE == sdp_type) { + /* update remote information */ + if(MG_TERM_RTP == term->type){ + term->u.rtp.remote_port + = p->u.portInt.port.val.val; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,"Updating remote port to [%d]\n", term->u.rtp.remote_port); + } + } + break; + } + case CM_SDP_PORT_VPCID: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t CM_SDP_PORT_VPCID: \n"); + break; + } + default: + break; + } + break; + } + default: + break; + } + } + mgco_handle_sdp_media_param(&f->par, term, sdp_type, mg_profile, &desc->attrSet, memCp); + } + + /*info */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "\t Info = %s \n",(NOTPRSNT != desc->info.pres)?(char*)desc->info.val:"Not Present"); + + /*connection set */ + { + int cnt=0x00; + if(NOTPRSNT != desc->connSet.numComp.pres){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\t Number of Connection component[%d]\n",desc->connSet.numComp.val); + for(cnt=0;cntconnSet.numComp.val;cnt++){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "************************\n"); + mgco_handle_sdp_c_line(desc->connSet.connSet[cnt], term, sdp_type); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "************************\n"); + } + } + } + + /* attribute set */ + mgco_handle_sdp_attr_set(&desc->attrSet, term); + + + if (desc->field.mediaType.val == CM_SDP_MEDIA_AUDIO && + desc->field.id.type.val == CM_SDP_VCID_PORT && + desc->field.id.u.port.type.val == CM_SDP_PORT_INT && + desc->field.id.u.port.u.portInt.port.type.val == CM_SDP_SPEC) { + int port = desc->field.id.u.port.u.portInt.port.val.val; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Port: %d\n", port); + } + } + } + } +} + +/*****************************************************************************************************************************/ +void mg_util_set_term_string ( MgStr *errTxt, MgMgcoTermId *termId) +{ + MG_ZERO((errTxt->val), sizeof(errTxt->val)); + errTxt->len = 0; + + if(termId->type.pres != NOTPRSNT) + { + errTxt->val[errTxt->len] = '\"'; + errTxt->len += 1; + + if(termId->type.val == MGT_TERMID_ROOT) + { + MG_MEM_COPY((&errTxt->val[errTxt->len]), "ROOT", 4); + errTxt->len += 4; + } + else if(termId->type.val == MGT_TERMID_ALL) + { + errTxt->val[errTxt->len] = '*'; + errTxt->len += 1; + } + else if(termId->type.val == MGT_TERMID_CHOOSE) + { + errTxt->val[errTxt->len] = '$'; + errTxt->len += 1; + } + else if((termId->type.val == MGT_TERMID_OTHER) && (termId->name.pres.pres != NOTPRSNT)) + { + if(termId->name.lcl.pres != NOTPRSNT) + { + MG_MEM_COPY(&(errTxt->val[errTxt->len]), termId->name.lcl.val, sizeof(U8) * termId->name.lcl.len); + errTxt->len += termId->name.lcl.len; + } + if(termId->name.dom.pres != NOTPRSNT) + { + MG_MEM_COPY(&(errTxt->val[errTxt->len]), + termId->name.dom.val, sizeof(U8) * termId->name.dom.len); + errTxt->len += termId->name.dom.len; + } + } + errTxt->val[errTxt->len] = '\"'; + errTxt->len += 1; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s:" + "info, error-text is: %s\n", __PRETTY_FUNCTION__,errTxt->val); +} +/*****************************************************************************************************************************/ +MgMgcoMediaDesc* get_default_media_desc(megaco_profile_t* mg_profile, MgMgcoTermId* termId, CmMemListCp *memCp) +{ + MgMgcoMediaDesc *media = NULL; + MgMgcoMediaPar *mediaPar = NULL; + MgMgcoTermStateParm *trmStPar = NULL; + S16 ret = ROK; + + MG_GETMEM(media, sizeof(MgMgcoMediaDesc) , memCp, ret); + + if (!media) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed, memory alloc\n"); + return NULL; + } + media->num.pres = PRSNT_NODEF; + media->num.val = 1; + + MG_GETMEM(mediaPar, sizeof(MgMgcoMediaPar) , memCp, ret); + + if (!mediaPar) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed, memory alloc\n"); + mg_stack_free_mem(media); + return NULL; + } + MG_GETMEM(media->parms, sizeof(MgMgcoMediaPar*) , memCp, ret); + + if (!media->parms) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed, memory alloc\n"); + mg_stack_free_mem((void*)mediaPar); + mg_stack_free_mem((void*)media); + return NULL; + } + mediaPar->type.pres = PRSNT_NODEF; + mediaPar->type.val = MGT_MEDIAPAR_TERMST; + mediaPar->u.tstate.numComp.pres = PRSNT_NODEF; + mediaPar->u.tstate.numComp.val = 1; + + MG_GETMEM(trmStPar, sizeof(MgMgcoTermStateParm) , memCp, ret); + + if (!trmStPar) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed, memory alloc\n"); + mg_stack_free_mem((void*)mediaPar); + mg_stack_free_mem((void*)media->parms); + mg_stack_free_mem((void*)media); + return NULL; + } + MG_GETMEM(mediaPar->u.tstate.trmStPar, sizeof(MgMgcoTermStateParm *) , memCp, ret); + + if (!mediaPar->u.tstate.trmStPar) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed, memory alloc\n"); + mg_stack_free_mem((void*)trmStPar); + mg_stack_free_mem((void*)mediaPar); + mg_stack_free_mem((void*)media->parms); + mg_stack_free_mem((void*)media); + return NULL; + } + trmStPar->type.pres = PRSNT_NODEF; + trmStPar->type.val = MGT_TERMST_SVCST; + trmStPar->u.svcState.pres = PRSNT_NODEF; + + if ((NOTPRSNT != termId->type.pres) && + (MGT_TERMID_ROOT == termId->type.val)){ + trmStPar->u.svcState.val = MGT_SVCST_INSVC; + }else{ + /*not root termination */ + mg_termination_t* term = NULL; + term = megaco_find_termination(mg_profile, (char*)termId->name.lcl.val); + if(term && switch_test_flag(term, MG_OUT_OF_SERVICE)){ + trmStPar->u.svcState.val = MGT_SVCST_OUTOFSVC; + }else{ + trmStPar->u.svcState.val = MGT_SVCST_INSVC; + } + } + + mediaPar->u.tstate.trmStPar[0] = trmStPar; + media->parms[0] = mediaPar; + + return media; +} +/*****************************************************************************************************************************/ + +switch_status_t mg_fill_svc_change(MgMgcoSvcChgPar *srvPar, uint8_t method, const char *reason, CmMemListCp *memCp) +{ + S16 ret = ROK; + MG_SET_TKN_VAL_PRES(&srvPar->pres, 0, PRSNT_NODEF); + MG_SET_TKN_VAL_PRES(&srvPar->meth.pres, 0, PRSNT_NODEF); + MG_SET_TKN_VAL_PRES(&srvPar->meth.type, method, PRSNT_NODEF); + + /* Set the reason */ + srvPar->reason.pres = PRSNT_NODEF; + srvPar->reason.len = cmStrlen((const U8 *)reason); + + MG_GETMEM(srvPar->reason.val, srvPar->reason.len , memCp, ret); + if (NULL == srvPar->reason.val) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed, memory alloc\n"); + return SWITCH_STATUS_FALSE; + } + + strncpy((char*)srvPar->reason.val, + (const char *)reason, + srvPar->reason.len); + + srvPar->reason.val[srvPar->reason.len] = '\0'; + + mg_get_time_stamp(&srvPar->time); + + printf("reason[%s], len[%d]\n",srvPar->reason.val, srvPar->reason.len); + + + return SWITCH_STATUS_SUCCESS; +} +/*****************************************************************************************************************************/ + +void mg_get_time_stamp(MgMgcoTimeStamp *timeStamp) +{ + DateTime dt; + Txt dmBuf[16]; + U32 usec; + + usec = 0; + + /*-- Get system date and time via Trillium stack API --*/ + SGetRefDateTimeAdj(0, 0, &dt, &usec); + + /*-- Now fill the time and date in the target --*/ + MG_ZERO(&dmBuf[0], 16); + + sprintf(dmBuf, "%04d%02d%02d", + (S16)(dt.year) + 1900, (S16)(dt.month), (S16)(dt.day)); + cmMemcpy((U8*) &timeStamp->date.val[0], (U8*) &dmBuf[0], 8); + + MG_ZERO(&dmBuf[0], 16); + sprintf(dmBuf, "%02d%02d%02d%02d", + (S16)(dt.hour), (S16)(dt.min), (S16)(dt.sec), (S16)(usec/10000)); + cmMemcpy((U8*) &timeStamp->time.val[0], (U8*) &dmBuf[0], 8); + + /*-- Setup the other stuff --*/ + timeStamp->pres.pres = PRSNT_NODEF; + timeStamp->date.pres = PRSNT_NODEF; + timeStamp->date.len = 8; + timeStamp->time.pres = PRSNT_NODEF; + timeStamp->time.len = 8; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"mg_get_time_stamp: time(%s)\n", dmBuf); +} +/*****************************************************************************************************************************/ +switch_status_t mg_create_mgco_command(MgMgcoCommand *cmd, uint8_t apiType, uint8_t cmdType) +{ + MgMgcoCommandReq *cmdReq; + MgMgcoCmdReply *cmdRep; + switch_status_t ret; + + cmdReq = NULL; + cmdRep = NULL; + + cmMemset((U8 *)cmd, 0, sizeof(MgMgcoCommand)); + + MG_SET_VAL_PRES(cmd->cmdType, apiType); + + /* Allocate the event structure */ + switch(apiType) + { + /* For command Request */ + case CH_CMD_TYPE_REQ: + { + if(SWITCH_STATUS_FALSE == (ret = mg_stack_alloc_mem((Ptr*)&cmd->u.mgCmdReq[0],sizeof(MgMgcoCommandReq)))){ + return ret; + } + + if (NULL == cmd->u.mgCmdReq[0]) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"mg_create_mgco_command: failed, memory alloc\n"); + return SWITCH_STATUS_FALSE; + } + + cmdReq = cmd->u.mgCmdReq[0]; + cmdReq->pres.pres = PRSNT_NODEF; + cmdReq->cmd.type.pres = PRSNT_NODEF; + cmdReq->cmd.type.val = cmdType; + switch (cmdType) + { + case MGT_SVCCHG: + cmdReq->cmd.u.svc.pres.pres = PRSNT_NODEF; + break; + + case MGT_NTFY: + cmdReq->cmd.u.ntfy.pres.pres = PRSNT_NODEF; + break; + } /* switch cmdType */ + break; + } + + /* For command Response */ + case CH_CMD_TYPE_RSP: + { + if(SWITCH_STATUS_FALSE == (ret = mg_stack_alloc_mem((Ptr*)&cmd->u.mgCmdRsp[0],sizeof(MgMgcoCmdReply)))){ + return ret; + } + + if (NULL == cmd->u.mgCmdRsp[0]) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"mg_create_mgco_command: failed, memory alloc\n"); + return SWITCH_STATUS_FALSE; + } + cmdRep = cmd->u.mgCmdRsp[0]; + cmdRep->pres.pres = PRSNT_NODEF; + cmdRep->type.pres = PRSNT_NODEF; + cmdRep->type.val = cmdType; + switch (cmdType) + { + case MGT_ADD: + cmdRep->u.add.pres.pres = PRSNT_NODEF; + break; + + case MGT_MOVE: + cmdRep->u.move.pres.pres = PRSNT_NODEF; + break; + + case MGT_MODIFY: + cmdRep->u.mod.pres.pres = PRSNT_NODEF; + break; + + case MGT_SUB: + cmdRep->u.sub.pres.pres = PRSNT_NODEF; + break; + + case MGT_SVCCHG: + cmdRep->u.svc.pres.pres = PRSNT_NODEF; + break; + + case MGT_AUDITVAL: + cmdRep->u.aval.type.pres = PRSNT_NODEF; + break; + case MGT_AUDITCAP: + cmdRep->u.acap.type.pres = PRSNT_NODEF; + break; + + } /* switch cmdType */ + break; + } + + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"mg_create_mgco_command: failed, invalid Cmd type[%d]\n",apiType); + return SWITCH_STATUS_FALSE; + } /* switch -apiType */ + + return SWITCH_STATUS_SUCCESS; +} +/*****************************************************************************************************************************/ + +void mg_fill_null_context(MgMgcoContextId* ctxt) +{ + MG_SET_TKN_VAL_PRES(&ctxt->type, MGT_CXTID_NULL, PRSNT_NODEF); +} +/*****************************************************************************************************************************/ +switch_status_t mg_util_build_obs_evt_desc (MgMgcoObsEvt *obs_event, MgMgcoRequestId *request_id, MgMgcoObsEvtDesc **ptr_obs_desc) +{ + MgMgcoObsEvtDesc *obs_desc = NULL; + + /* Check for valid request Id, if not then fill default value */ + if (NOTPRSNT == request_id->type.pres) + { + MG_SET_DEF_REQID(request_id); + } + + mg_stack_alloc_mem((Ptr*)&obs_desc, sizeof(MgMgcoObsEvtDesc)); + if (NULL == obs_desc) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to allocate MgMgcoObsEvtDesc!\n"); + return SWITCH_STATUS_FALSE; + } + + obs_desc->pres.pres = PRSNT_NODEF; + MG_MEM_COPY(&obs_desc->reqId, request_id, sizeof(MgMgcoRequestId)); + obs_desc->el.num.pres = PRSNT_NODEF; + obs_desc->el.num.val = 1; + + mg_stack_alloc_mem((Ptr*)&obs_desc->el.evts, sizeof(MgMgcoObsEvt*)); + if (NULL == obs_desc->el.evts) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to allocate MgMgcoObsEvt!\n"); + return SWITCH_STATUS_FALSE; + } + + MG_MEM_COPY(obs_desc->el.evts[0], obs_event, sizeof(obs_event)); + + *ptr_obs_desc = obs_desc; + + return SWITCH_STATUS_SUCCESS; +} +/*****************************************************************************************************************************/ +void mg_print_time() +{ + time_t now; + time(&now); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Current Time = %s", ctime(&now)); +} +/*****************************************************************************************************************************/ +switch_status_t mg_add_local_descriptor(MgMgcoMediaDesc* media, megaco_profile_t* mg_profile, mg_termination_t* term,CmMemListCp *memCp) +{ + char* ipAddress[4]; + MgMgcoLocalDesc *local; + CmSdpInfoSet *psdp = NULL; + char * dup = NULL; + switch_status_t ret = SWITCH_STATUS_SUCCESS; + CmSdpMediaDescSet* med = NULL; + + switch_assert(media); + switch_assert(mg_profile); + switch_assert(term); + + dup = strdup((char*)term->u.rtp.local_addr); + switch_split(dup,'.',ipAddress); + + /* allocating mem for local descriptor */ + if (mgUtlGrowList((void ***)&media->parms, sizeof(MgMgcoMediaPar), &media->num, memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + media->parms[media->num.val-1]->type.pres = PRSNT_NODEF; + media->parms[media->num.val-1]->type.val = MGT_MEDIAPAR_LOCAL; + + local = &media->parms[media->num.val-1]->u.local; + + local->pres.pres = PRSNT_NODEF; + + psdp = &(local->sdp); + + if (mgUtlGrowList((void ***)&psdp->info, sizeof(CmSdpInfo), &psdp->numComp, memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + psdp->info[psdp->numComp.val-1]->pres.pres = PRSNT_NODEF; + + /* fill version */ + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->ver),1); + + /* fill orig */ + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->orig.pres), 1); + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->orig.type), CM_SDP_SPEC); + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->orig.orig.pres), 1); + + MG_SET_TKNSTROSXL(psdp->info[psdp->numComp.val-1]->orig.orig.usrName, 1, "-", + memCp); + MG_SET_TKNSTROSXL(psdp->info[psdp->numComp.val-1]->orig.orig.sessId, 1, "0", + memCp); + MG_SET_TKNSTROSXL(psdp->info[psdp->numComp.val-1]->orig.orig.sessVer, 1, "0", + memCp); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.netType.type), + CM_SDP_NET_TYPE_IN); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.addrType), + CM_SDP_ADDR_TYPE_IPV4); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.addrType), + CM_SDP_IPV4_IP_UNI); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.addrType), + CM_SDP_IPV4_IP_UNI); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.u.ip.b[0]), + atoi(ipAddress[0])); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.u.ip.b[1]), + atoi(ipAddress[1])); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.u.ip.b[2]), + atoi(ipAddress[2])); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.u.ip.b[3]), + atoi(ipAddress[3])); + + /* fill session name */ + MG_SET_TKNSTROSXL(psdp->info[psdp->numComp.val-1]->sessName, 8, "SANGOMA", memCp); + + + /* Fill the SDP Connection Info */ + /* "c=" line - ipaddress */ + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->conn.netType.type),CM_SDP_NET_TYPE_IN); + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->conn.addrType), CM_SDP_ADDR_TYPE_IPV4); + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->conn.u.ip4.addrType), CM_SDP_IPV4_IP_UNI); + + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->conn.u.ip4.u.uniIp.b[0]), atoi(ipAddress[0])); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->conn.u.ip4.u.uniIp.b[1]), atoi(ipAddress[1])); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->conn.u.ip4.u.uniIp.b[2]), atoi(ipAddress[2])); + MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->conn.u.ip4.u.uniIp.b[3]), atoi(ipAddress[3])); + + + /* t= line */ + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->sdpTime.pres),1); +#if 0 + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->sdpTime.sdpOpTimeSet.numComp),0); + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->sdpTime.zoneAdjSet.numComp),0); +#endif + + med = &psdp->info[psdp->numComp.val-1]->mediaDescSet; + ret = mg_add_lcl_media(med, mg_profile, term, memCp); + + return ret; +} +/*****************************************************************************************************************************/ +switch_status_t mg_add_supported_media_codec(CmSdpMediaDesc* media, megaco_profile_t* mg_profile, mg_termination_t* term, CmMemListCp *memCp) +{ + const switch_codec_implementation_t *codecs[16]; + char *codec_prefs[16] = { 0 }; + char *szcodec_prefs; + int codec_count; + int i; + int fmt= 0x00; + + switch_assert(media); + switch_assert(mg_profile); + switch_assert(term); + switch_assert(memCp); + + szcodec_prefs = strdup(mg_profile->codec_prefs); + codec_count = switch_split(szcodec_prefs, ',', codec_prefs); + + /* Get the list of codecs, by preference */ + switch_loadable_module_get_codecs_sorted(codecs, switch_arraylen(codecs), codec_prefs, switch_arraylen(codec_prefs)); + for (i = 0; codecs[i] && i < codec_count; i++) { + int pt = codecs[i]->ianacode; + const char *name = codecs[i]->iananame; + + printf("Preference %d is %s/%d\n", i, name, pt); + + + if (mgUtlGrowList((void ***)&media->field.par.pflst[media->field.par.numProtFmts.val-1]->u.rtp.fmts, sizeof(CmSdpU8OrNil), + &media->field.par.pflst[media->field.par.numProtFmts.val-1]->u.rtp.num, memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + fmt = media->field.par.pflst[media->field.par.numProtFmts.val-1]->u.rtp.num.val-1; + + MG_INIT_TOKEN_VALUE(&(media->field.par.pflst[media->field.par.numProtFmts.val-1]->u.rtp.fmts[fmt]->type), CM_SDP_SPEC); + + MG_INIT_TOKEN_VALUE(&(media->field.par.pflst[media->field.par.numProtFmts.val-1]->u.rtp.fmts[fmt]->val), pt); + + /* add associated attributes */ + { + if (mgUtlGrowList((void ***)&media->attrSet.attr, sizeof(CmSdpAttr), &media->attrSet.numComp, memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + MG_INIT_TOKEN_VALUE(&(media->attrSet.attr[media->attrSet.numComp.val-1]->type),CM_SDP_ATTR_RTPMAP); + MG_INIT_TOKEN_VALUE(&(media->attrSet.attr[media->attrSet.numComp.val-1]->u.rtpmap.pres), 1); + MG_INIT_TOKEN_VALUE(&(media->attrSet.attr[media->attrSet.numComp.val-1]->u.rtpmap.pay.type), CM_SDP_SPEC); + MG_INIT_TOKEN_VALUE(&(media->attrSet.attr[media->attrSet.numComp.val-1]->u.rtpmap.pay.val), pt); + MG_INIT_TOKEN_VALUE(&(media->attrSet.attr[media->attrSet.numComp.val-1]->u.rtpmap.enc.val), CM_SDP_ENC_UNKNOWN); + MG_SET_TKNSTROSXL((media->attrSet.attr[media->attrSet.numComp.val-1]->u.rtpmap.enc.name), strlen(name), name, memCp); + MG_INIT_TOKEN_VALUE(&(media->attrSet.attr[media->attrSet.numComp.val-1]->u.rtpmap.clk), codecs[i]->samples_per_second); + /* encoding parameter not required to fill */ + } + + } + free(szcodec_prefs); + return SWITCH_STATUS_SUCCESS; +} +/*****************************************************************************************************************************/ +switch_status_t mg_add_lcl_media(CmSdpMediaDescSet* med, megaco_profile_t* mg_profile, mg_termination_t* term, CmMemListCp *memCp) +{ + CmSdpMediaDesc* media; + + switch_assert(med); + switch_assert(mg_profile); + switch_assert(term); + switch_assert(memCp); + + if (mgUtlGrowList((void ***)&med->mediaDesc, sizeof(CmSdpMediaDesc), + &med->numComp, memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + media = med->mediaDesc[med->numComp.val-1]; + + MG_INIT_TOKEN_VALUE(&(media->pres),1); + + /* Fill CmSdpMediaField */ + MG_INIT_TOKEN_VALUE(&(media->field.pres),1); + MG_INIT_TOKEN_VALUE(&(media->field.mediaType),CM_SDP_MEDIA_AUDIO); + + MG_INIT_TOKEN_VALUE(&(media->field.id.type),CM_SDP_VCID_PORT); + MG_INIT_TOKEN_VALUE(&(media->field.id.u.port.type),CM_SDP_PORT_INT); + MG_INIT_TOKEN_VALUE(&(media->field.id.u.port.u.portInt.pres),1); + MG_INIT_TOKEN_VALUE(&(media->field.id.u.port.u.portInt.port.type), CM_SDP_SPEC); + MG_INIT_TOKEN_VALUE(&(media->field.id.u.port.u.portInt.port.val), term->u.rtp.local_port); + + if (mgUtlGrowList((void ***)&media->field.par.pflst, sizeof(CmSdpMedProtoFmts), + &media->field.par.numProtFmts, memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + /* CmSdpMedProtoFmts */ + MG_INIT_TOKEN_VALUE(&(media->field.par.pflst[media->field.par.numProtFmts.val-1]->prot.type), CM_SDP_MEDIA_PROTO_RTP); + MG_INIT_TOKEN_VALUE(&(media->field.par.pflst[media->field.par.numProtFmts.val-1]->prot.u.subtype.type), CM_SDP_PROTO_RTP_AVP); + MG_INIT_TOKEN_VALUE(&(media->field.par.pflst[media->field.par.numProtFmts.val-1]->protType), CM_SDP_MEDIA_PROTO_RTP); + + + /***************************************************************************************************************************************************************/ + /* Fill ptime attribute */ + { + if (mgUtlGrowList((void ***)&media->attrSet.attr, sizeof(CmSdpAttr), &media->attrSet.numComp, memCp) != ROK) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Grow List failed\n"); + return SWITCH_STATUS_FALSE; + } + + MG_INIT_TOKEN_VALUE(&(media->attrSet.attr[media->attrSet.numComp.val-1]->type),CM_SDP_ATTR_PTIME); + MG_INIT_TOKEN_VALUE(&(media->attrSet.attr[media->attrSet.numComp.val-1]->u.ptime), term->u.rtp.ptime); + } + /***************************************************************************************************************************************************************/ + /* fill codec info */ + mg_add_supported_media_codec(media, mg_profile, term, memCp); + + return SWITCH_STATUS_SUCCESS; +} +/***************************************************************************************************************************************************************/ + +switch_status_t mg_rem_unsupported_codecs (megaco_profile_t* mg_profile, mg_termination_t* term, CmSdpMedFmtRtpList *fmtList, CmSdpAttrSet *attrSet, CmMemListCp *memCp) +{ + int i = 0x00; + int id = 0x00; + int j = 0x00; + int a = 0x00; + CmSdpU8OrNil *fmt = NULL; + int foundCodec = 0x00; + const switch_codec_implementation_t *codecs[16]; + char *codec_prefs[16] = { 0 }; + char *szcodec_prefs; + int codec_count; + CmSdpAttr *attr = NULL; + CmSdpAttrRtpMap *rtp = NULL; + + /* Check if code list is present */ + if (!fmtList || (NOTPRSNT == fmtList->num.pres)) + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "codec List Not present\n"); + return SWITCH_STATUS_FALSE; + } + + szcodec_prefs = strdup(mg_profile->codec_prefs); + codec_count = switch_split(szcodec_prefs, ',', codec_prefs); + + /* Get the list of codecs, by preference */ + switch_loadable_module_get_codecs_sorted(codecs, switch_arraylen(codecs), codec_prefs, switch_arraylen(codec_prefs)); + + + /* codec type is specified one */ + + /* loop through coddec list and remove un-supported codec */ + for(i = 0; i < fmtList->num.val; i++) + { + foundCodec = 0x00; + fmt = fmtList->fmts[i]; + + if((NOTPRSNT == fmt->type.pres) || (NOTPRSNT == fmt->val.pres)) continue; + + if(CM_SDP_SPEC != fmt->type.val) continue; /* TODO - need to see for other cases like CM_SDP_NIL/CM_SDP_CHOICE etc not sure as of now */ + + + /* see if received codec is present in our codec supported list */ + for (id = 0; codecs[id] && id < codec_count; id++) { + int pt = codecs[id]->ianacode; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Matching recv codec[%d] with supported codec[%d] \n", fmt->val.val, pt); + //const char *name = codecs[id]->iananame; + /* anything > 96 is dymanic we should skip codec match */ + if(fmt->val.val >= 96) {foundCodec = 0x01; break;} + if(pt == fmt->val.val){ + foundCodec = 0x01; + break; + } + } + + /* IF codec not found in list, remove it */ + if(!foundCodec) { + + for(j = i; j < fmtList->num.val - 1; j++) + { + fmtList->fmts[j] = fmtList->fmts[j +1]; + } + mgUtlShrinkList((Void ***)&fmtList->fmts, sizeof(CmSdpU8OrNil), &fmtList->num, memCp); + i-- ; + + /* remove associated a= , if present */ + if((NOTPRSNT != attrSet->numComp.pres) && (0 != attrSet->numComp.val)){ + for(a = 0; a < attrSet->numComp.val; a++) { + attr = attrSet->attr[a]; + + if(CM_SDP_ATTR_RTPMAP != attr->type.val) continue; /* as of now only checking RTPMAP */ + + rtp = &attr->u.rtpmap; + + if((NOTPRSNT != rtp->pres.pres) && (fmt->val.val == rtp->pay.val.val)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "a line found against codec[%d]..Removing a line \n", fmt->val.val); + + /* mgUtlShrinkList API will delete last node from list, hence suffling list nodes */ + for(j = a; j < attrSet->numComp.val - 1; j++) + { + attrSet->attr[j] = attrSet->attr[j +1]; + } + mgUtlShrinkList((Void ***)&attrSet->attr, sizeof(CmSdpAttr), &attrSet->numComp, memCp); + } + } + } + } + } + + if(0 == fmtList->num.val) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No Supported codec found in offer, Rejecting request \n"); + term->mg_error_code = switch_core_alloc(term->pool, sizeof(term->mg_error_code)); + *term->mg_error_code = MGT_MGCP_RSP_CODE_INCONSISTENT_LCL_OPT; + + return SWITCH_STATUS_FALSE; + } + + return SWITCH_STATUS_SUCCESS; +} + + + +/*****************************************************************************************************************************/ + +switch_status_t mg_build_sdp(MgMgcoMediaDesc* out, MgMgcoMediaDesc* inc, megaco_profile_t* mg_profile, mg_termination_t* term, CmMemListCp *memCp) +{ + CmSdpU8OrNil *fmt = NULL; + CmSdpInfoSet *psdp = NULL; + char* ipAddress[4]; + int i = 0x00; + int j = 0x00; + int choose_codec = 0x00; + int k = 0x00; + MgMgcoLocalDesc *local = NULL; + MgMgcoLocalDesc *remote = NULL; + int fresh_sdp = 0x00; + char* dup = NULL; + CmSdpMedProtoFmts *format=NULL; + + + switch_assert(out); + switch_assert(inc); + switch_assert(mg_profile); + switch_assert(term); + + dup = strdup((char*)term->u.rtp.local_addr); + switch_split(dup,'.',ipAddress); + + + if((NOTPRSNT == inc->num.pres) || (0 == inc->num.val)){ + fresh_sdp = 0x01; + } + + /* if its fresh sdp then add only local descriptor */ + if(fresh_sdp) { + mg_add_local_descriptor(out, mg_profile, term, memCp); + } else { + /* incoming has sdp, so copy that sdp and overwrite only local sdp */ + mgUtlCpyMgMgcoMediaDesc(out, inc, memCp); + + /* now see if we have local descriptor, then pick up that and modify the fields */ + + if((NOTPRSNT != out->num.pres) && (0 != out->num.val)) + { + for(i=0; inum.val; i++) { + if(MGT_MEDIAPAR_LOCAL == out->parms[i]->type.val) { + local = &out->parms[i]->u.local; + } else if(MGT_MEDIAPAR_STRPAR == out->parms[i]->type.val){ + MgMgcoStreamDesc *stream = &out->parms[i]->u.stream; + if((NOTPRSNT != stream->sl.pres.pres) && (NOTPRSNT != stream->sl.local.pres.pres)){ + local = &stream->sl.local; + } + }else if(MGT_MEDIAPAR_REMOTE == out->parms[i]->type.val) { + remote = &out->parms[i]->u.remote; + } + } + } + } + + + if(!local || (NOTPRSNT == local->sdp.numComp.pres) || (0 == local->sdp.numComp.val)){ + /* local sdp is not part of media descriptor, then add local sdp*/ + mg_add_local_descriptor(out, mg_profile, term, memCp); + }else{ + /* local sdp is present.. now go over the local descriptor and modify fields */ + psdp = &(local->sdp); + + for(i=0; i< psdp->numComp.val; i++) { +/**********************************************************************************************************************************/ + /* version - let it be same, if present else use version 1 */ + if(NOTPRSNT == psdp->info[psdp->numComp.val-1]->ver.pres) { + MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->ver),1); + } +/**********************************************************************************************************************************/ + /* orig (o- line) fill with our info */ +MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->orig.pres), 1); +MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->orig.type), CM_SDP_SPEC); +MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->orig.orig.pres), 1); + +MG_SET_TKNSTROSXL(psdp->info[psdp->numComp.val-1]->orig.orig.usrName, 1, "-", memCp); +MG_SET_TKNSTROSXL(psdp->info[psdp->numComp.val-1]->orig.orig.sessId, 1, "0", memCp); +MG_SET_TKNSTROSXL(psdp->info[psdp->numComp.val-1]->orig.orig.sessVer, 1, "0", memCp); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.netType.type), +CM_SDP_NET_TYPE_IN); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.addrType), + CM_SDP_ADDR_TYPE_IPV4); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.addrType), + CM_SDP_IPV4_IP_UNI); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.addrType), + CM_SDP_IPV4_IP_UNI); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.u.ip.b[0]), + atoi(ipAddress[0])); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.u.ip.b[1]), + atoi(ipAddress[1])); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.u.ip.b[2]), + atoi(ipAddress[2])); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->orig.orig.sdpAddr.u.ip4.u.ip.b[3]), + atoi(ipAddress[3])); + +/**********************************************************************************************************************************/ + /* session-name , let it be like this if present, else skip it */ +/**********************************************************************************************************************************/ + /* "c=" line - ipaddress */ +MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->conn.netType.type),CM_SDP_NET_TYPE_IN); +MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->conn.addrType), CM_SDP_ADDR_TYPE_IPV4); +MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->conn.u.ip4.addrType), CM_SDP_IPV4_IP_UNI); + +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->conn.u.ip4.u.uniIp.b[0]), atoi(ipAddress[0])); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->conn.u.ip4.u.uniIp.b[1]), atoi(ipAddress[1])); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->conn.u.ip4.u.uniIp.b[2]), atoi(ipAddress[2])); +MG_SET_VAL_PRES( (psdp->info[psdp->numComp.val-1]->conn.u.ip4.u.uniIp.b[3]), atoi(ipAddress[3])); + +/**********************************************************************************************************************************/ +/* t= line */ +MG_INIT_TOKEN_VALUE(&(psdp->info[psdp->numComp.val-1]->sdpTime.pres),1); +/**********************************************************************************************************************************/ +/* fill media descriptors */ +{ +CmSdpMediaDescSet* med = &psdp->info[psdp->numComp.val-1]->mediaDescSet; +CmSdpMediaDesc* media; + +if((NOTPRSNT == med->numComp.pres) || (0 == med->numComp.val)){ + mg_add_lcl_media(med, mg_profile, term, memCp); +}else{ + for(j =0;j < med->numComp.val; j++){ + media = med->mediaDesc[j]; + /* check for choose port and fill the port */ + if(NOTPRSNT != media->field.id.type.pres){ + //if(CM_SDP_VCID_CHOOSE == media->field.id.type.val) + { + MG_INIT_TOKEN_VALUE(&(media->field.id.type),CM_SDP_VCID_PORT); + MG_INIT_TOKEN_VALUE(&(media->field.id.u.port.type),CM_SDP_PORT_INT); + MG_INIT_TOKEN_VALUE(&(media->field.id.u.port.u.portInt.pres),1); + MG_INIT_TOKEN_VALUE(&(media->field.id.u.port.u.portInt.port.type), CM_SDP_SPEC); + MG_INIT_TOKEN_VALUE(&(media->field.id.u.port.u.portInt.port.val), term->u.rtp.local_port); + + } + } + + /* check for codec */ +if((NOTPRSNT == media->field.par.numProtFmts.pres) || + (0 == media->field.par.numProtFmts.val)){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "No codec specified in incoming local descriptor \n"); + mg_add_supported_media_codec(media, mg_profile, term, memCp ); +}else{ +/* check for media format/codec */ + for(k =0;k < media->field.par.numProtFmts.val; k++){ +format = media->field.par.pflst[k]; +if ((NOTPRSNT != format->protType.pres) && + (CM_SDP_MEDIA_PROTO_RTP == format->protType.val)) +{ + if((NOTPRSNT != format->u.rtp.num.pres) + &&(0 != format->u.rtp.num.val)) + { + /* If the codec type is CHOOSE then we need to fill our list */ + for(i = 0; i < format->u.rtp.num.val; i++) { + fmt = format->u.rtp.fmts[i]; + if((NOTPRSNT == fmt->type.pres) || (NOTPRSNT == fmt->val.pres)) continue; + if(CM_SDP_CHOOSE == fmt->type.val){ + choose_codec = 0x1; + } + } + if(choose_codec){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "CHOOSE codec is requested fill out supported codecs \n"); + + /* delete existing rtp format list..TODO find better way */ + for(i = 0; i < format->u.rtp.num.val; i++) + { + mgUtlShrinkList((Void ***)&format->u.rtp.fmts, sizeof(CmSdpU8OrNil), &format->u.rtp.num, memCp); + } + /* If the codec type is CHOOSE then we need to fill our list */ + mg_add_supported_media_codec(media, mg_profile, term, memCp); + } else if (!choose_codec && + (SWITCH_STATUS_FALSE == + mg_rem_unsupported_codecs(mg_profile, term , &format->u.rtp, &media->attrSet, memCp))) + { + return SWITCH_STATUS_FALSE; + } + } +} +} +} +} +} +} +/**********************************************************************************************************************************/ +} + + + } + return SWITCH_STATUS_SUCCESS; +} +/*****************************************************************************************************************************/ +void mg_print_t38_attributes(mg_termination_t* term) +{ + switch_assert(term); + + if((MG_TERM_RTP == term->type) && (term->u.rtp.t38_options)){ + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO, + "switch_t38_options_t for termination[%s]\n", term->name); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO,"T38FaxVersion[%d]\n", + term->u.rtp.t38_options->T38FaxVersion); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO,"T38MaxBitRate[%d]\n", + term->u.rtp.t38_options->T38MaxBitRate); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO,"T38FaxFillBitRemoval[%d]\n", + term->u.rtp.t38_options->T38FaxFillBitRemoval); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO,"T38FaxTranscodingMMR[%d]\n", + term->u.rtp.t38_options->T38FaxTranscodingMMR); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO,"T38FaxTranscodingJBIG[%d]\n", + term->u.rtp.t38_options->T38FaxTranscodingJBIG); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO,"T38FaxRateManagement[%s]\n", + (NULL != term->u.rtp.t38_options->T38FaxRateManagement)? + term->u.rtp.t38_options->T38FaxRateManagement:"NULL"); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO,"T38FaxMaxBuffer[%d]\n", + term->u.rtp.t38_options->T38FaxMaxBuffer); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO,"T38FaxMaxDatagram[%d]\n", + term->u.rtp.t38_options->T38FaxMaxDatagram); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO,"T38FaxUdpEC[%s]\n", + (NULL != term->u.rtp.t38_options->T38FaxUdpEC)? + term->u.rtp.t38_options->T38FaxUdpEC:"NULL"); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_INFO,"T38VendorInfo[%s]\n", + (NULL != term->u.rtp.t38_options->T38VendorInfo)? + term->u.rtp.t38_options->T38VendorInfo:"NULL"); + } + +} + +/*****************************************************************************************************************************/ +void mg_apply_tdm_dtmf_removal(mg_termination_t* term, mg_context_t *mg_ctxt) +{ + mg_termination_t* tdm_term = NULL; + + if(NULL == term) return ; + + if((MG_TERM_RTP == term->type)){ + if(NULL != (tdm_term = megaco_context_get_peer_term(mg_ctxt, term))){ + if(term->u.rtp.rfc2833_pt){ + megaco_tdm_term_dtmf_removal(tdm_term,0x01); + }else{ + megaco_tdm_term_dtmf_removal(tdm_term,0x00); + } + } + } +} +/*****************************************************************************************************************************/ +void mg_apply_tdm_ec(mg_termination_t* term, mg_context_t *mg_ctxt) +{ + mg_termination_t* tdm_term = NULL; + + if(NULL == term) return ; + + if((MG_TERM_RTP == term->type)){ + if(NULL != (tdm_term = megaco_context_get_peer_term(mg_ctxt, term))){ + if(term->u.rtp.t38_options){ + mg_term_set_ec(tdm_term,0x00); + } + } + } +} +/*****************************************************************************************************************************/ +void mg_set_term_ec_status(mg_termination_t* term, mg_ec_types_t status) +{ + if(NULL != term) { + term->ec_type = status; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Setting Term EC[%s]\n",mg_ec_types_tostr(term->ec_type)); + } +} +/*****************************************************************************************************************************/ diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway_xml.c b/src/mod/endpoints/mod_media_gateway/media_gateway_xml.c new file mode 100644 index 0000000000..40ced6087d --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/media_gateway_xml.c @@ -0,0 +1,481 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Kapil Gupta +* All rights reserved. +* +* +*/ +#include "mod_media_gateway.h" +#include "media_gateway_stack.h" + +/****************************************************************************************************************************/ +static switch_xml_config_item_t *get_instructions(megaco_profile_t *profile) ; +static switch_xml_config_item_t *get_peer_instructions(mg_peer_profile_t *profile) ; +static int mg_sap_id; +static switch_status_t modify_mg_profile_mid(megaco_profile_t *profile, char** pmid) ; +static switch_status_t modify_mg_peer_mid(mg_peer_profile_t *peer_profile, char** pmid) ; + +/****************************************************************************************************************************/ +switch_status_t config_profile(megaco_profile_t *profile, switch_bool_t reload) +{ + switch_xml_t cfg, xml, param, mg_interfaces, mg_interface, mg_peers, mg_peer, mg_phys_terms, mg_term, peer_interfaces ; + switch_status_t status = SWITCH_STATUS_FALSE; + switch_event_t *event = NULL; + const char *file = "media_gateway.conf"; + switch_xml_config_item_t *instructions = (profile ? get_instructions(profile) : NULL); + int count; + int idx; + int ret=0; + char *var, *val; + mg_peer_profile_t* peer_profile = NULL; + switch_xml_config_item_t *instructions1 = NULL; + switch_memory_pool_t *pool; + char lic_sig_file[4096]; + + memset(&lic_sig_file[0],0,sizeof(lic_sig_file)); + + if (!(xml = switch_xml_open_cfg(file, &cfg, NULL))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not open %s\n", file); + goto done; + } + + if (!(mg_interfaces = switch_xml_child(cfg, "mg_profiles"))) { + goto done; + } + + for (mg_interface = switch_xml_child(mg_interfaces, "mg_profile"); mg_interface; mg_interface = mg_interface->next) { + const char *name = switch_xml_attr_soft(mg_interface, "name"); + if (strcmp(name, profile->name)) { + continue; + } + + count = switch_event_import_xml(switch_xml_child(mg_interface, "param"), "name", "value", &event); + status = switch_xml_config_parse_event(event, count, reload, instructions); + + /* now build peer list */ + if (!(peer_interfaces = switch_xml_child(mg_interface, "peers"))) { + goto done; + } + + for (param = switch_xml_child(peer_interfaces, "param"); param; param = param->next) { + var = (char *) switch_xml_attr_soft(param, "name"); + val = (char *) switch_xml_attr_soft(param, "value"); + profile->peer_list[profile->total_peers] = switch_core_strdup(profile->pool, val); + profile->total_peers++; + } + + /* If RTP-IP is not defined then default to local-ip */ + if((!profile->rtp_ipaddr) || + (profile->rtp_ipaddr && ('\0' == profile->rtp_ipaddr[0]))){ + profile->rtp_ipaddr = switch_mprintf("%s", profile->my_ipaddr); + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,"rtp_ipaddr[%s], local ip[%s]\n", profile->rtp_ipaddr, profile->my_ipaddr); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,"t38_fax_notify[%s]\n", + (MG_T38_FAX_NOTIFY_YES == profile->t38_fax_notify)?"ENABLE":"DISABLE"); + + if(SWITCH_STATUS_FALSE == (status = modify_mg_profile_mid(profile, &profile->mid))){ + goto done; + } + + profile->idx = ++mg_sap_id; + + if ((mg_phys_terms = switch_xml_child(mg_interface, "physical_terminations"))) { + for (mg_term = switch_xml_child(mg_phys_terms, "map"); mg_term; mg_term = mg_term->next) { + // + const char *prefix = switch_xml_attr(mg_term, "termination-id-prefix"); + const char *sztermination_id_base = switch_xml_attr(mg_term, "termination-id-base"); + const char *tech = switch_xml_attr(mg_term, "tech"); + const char *channel_prefix = switch_xml_attr(mg_term, "channel-prefix"); + const char *channel_map = switch_xml_attr(mg_term, "channel-map"); + char *p = NULL; + + if (!zstr(channel_map)) { + /* Split channel-map */ + char *channel_map_dup = strdup(channel_map); + char *chanmap[24] = {0}; + int chanmap_count = 0; + int i = 0; + int startchan, endchan, j; + int mg_term_idx = (sztermination_id_base)?atoi(sztermination_id_base):1; + + /* we can have following combinations * + * i) only one channel i.e. channel-map="1" + * ii) only one chanel range i.e channel-map="1-15" + * iii) full channel range i.e. channel-map="1-15,17-31" + */ + + + chanmap_count = switch_split(channel_map_dup, ',', chanmap); + + if(1 == chanmap_count) { + p = strchr(channel_map_dup, '-'); + if(NULL != p){ + /* case (ii) */ + i = switch_split(channel_map_dup, '-', chanmap); + if(i && chanmap[0] && chanmap[1]) { + startchan = atoi(chanmap[0]); + endchan = atoi(chanmap[1]); + for (j = startchan; j <= endchan; j++) { + mg_create_tdm_term(profile, tech, channel_prefix, prefix, mg_term_idx, j); + mg_term_idx++; + } + } + }else{ + /* case (i) */ + p = channel_map_dup; + startchan = endchan = atoi(p); + mg_create_tdm_term(profile, tech, channel_prefix, prefix, mg_term_idx, startchan); + } + }else + { + /* case (iii) */ + for (i = 0; i < chanmap_count; i++) { + p = strchr(chanmap[i], '-'); + if (p) { + *p++ = '\0'; + startchan = atoi(chanmap[i]); + endchan = atoi(p); + + for (j = startchan; j <= endchan; j++) { +if (0 == i) + mg_create_tdm_term(profile, tech, channel_prefix, prefix, mg_term_idx, j); +else + mg_create_tdm_term(profile, tech, channel_prefix, prefix, mg_term_idx, j-1); + mg_term_idx++; + } + } + } + } + + free(channel_map_dup); + } + } + } + + + /* we should break from here , profile name should be unique */ + break; + } + + if (!mg_interface) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error profile %s not found\n", profile->name); + status = SWITCH_STATUS_FALSE; + } + + + if((profile->license) && ('\0' != profile->license[0])){ + sprintf(lic_sig_file, "%s.sig", profile->license); + }else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to get License file \n"); + status = SWITCH_STATUS_FALSE; + goto done; + } + + MG_CHECK_LICENSE(profile->total_cfg_term,profile->license, &lic_sig_file[0], ret); + if(ret){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "License validation failed \n"); + status = SWITCH_STATUS_FALSE; + goto done; + } + + + /* go through the peer configuration and get the mg profile associated peers only */ + if (!(mg_peers = switch_xml_child(cfg, "mg_peers"))) { + status = SWITCH_STATUS_FALSE; + goto done; + } + + for (mg_peer = switch_xml_child(mg_peers, "mg_peer"); mg_peer; mg_peer = mg_peer->next) { + const char *name = switch_xml_attr_soft(mg_peer, "name"); + for(idx=0; idxtotal_peers; idx++){ + count = 0x00; + event = NULL; + peer_profile = NULL; + if (!strcmp(name, profile->peer_list[idx])) { + /* peer profile */ + switch_core_new_memory_pool(&pool); + peer_profile = switch_core_alloc(pool, sizeof(*peer_profile)); + peer_profile->pool = pool; + peer_profile->name = switch_core_strdup(peer_profile->pool, name); + switch_thread_rwlock_create(&peer_profile->rwlock, peer_profile->pool); + instructions1 = (peer_profile ? get_peer_instructions(peer_profile) : NULL); + + count = switch_event_import_xml(switch_xml_child(mg_peer, "param"), "name", "value", &event); + if(SWITCH_STATUS_FALSE == (status = switch_xml_config_parse_event(event, count, reload, instructions1))){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Peer XML Parsing failed \n"); + goto done; + } + + if (SWITCH_STATUS_FALSE == (status = modify_mg_peer_mid(peer_profile, &peer_profile->mid))) { + goto done; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"peer_profile name[%s], ipaddr[%s] port[%s], mid[%s] transport_type[%s], encoding_type[%s] \n", + peer_profile->name, peer_profile->ipaddr, peer_profile->port,peer_profile->mid, peer_profile->transport_type, peer_profile->encoding_type); + + switch_core_hash_insert_wrlock(megaco_globals.peer_profile_hash, peer_profile->name, peer_profile, megaco_globals.peer_profile_rwlock); + } + } + } + + /* configure the MEGACO stack */ + status = sng_mgco_cfg(profile); + +done: + if (xml) { + switch_xml_free(xml); + } + + if (event) { + switch_event_destroy(&event); + } + + switch_safe_free(instructions); + + return status; +} + +/****************************************************************************************************************************/ +void mg_create_tdm_term(megaco_profile_t *profile, const char *tech, const char *channel_prefix, const char *prefix, int chan_num, int tdm_chan_num) +{ + mg_termination_t *term; + switch_memory_pool_t *pool; + + switch_core_new_memory_pool(&pool); + term = switch_core_alloc(profile->pool, sizeof *term); + term->pool = pool; + term->type = MG_TERM_TDM; + term->profile = profile; + term->tech = switch_core_strdup(pool, tech); + term->active_events = NULL; + term->name = switch_core_sprintf(pool, "%s%d", prefix, chan_num); + term->u.tdm.channel = tdm_chan_num; + term->u.tdm.span_name = switch_core_strdup(pool, channel_prefix); + switch_set_flag(term, MG_OUT_OF_SERVICE); + switch_clear_flag(term, MG_FAX_NOTIFIED); + + switch_core_hash_insert_wrlock(profile->terminations, term->name, term, profile->terminations_rwlock); + term->next = profile->physical_terminations; + profile->physical_terminations = term; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "Mapped termination [%s] to freetdm span: %s chan: %d\n", + term->name, term->u.tdm.span_name, term->u.tdm.channel); + megaco_prepare_tdm_termination(term); + +#if 0 + /* by-default : DTMF removal disable + * by default do not modify in-band audio stream*/ + megaco_tdm_term_dtmf_removal(term,0x00); +#endif + + profile->total_cfg_term++; +} +/****************************************************************************************************************************/ +switch_status_t mg_config_cleanup(megaco_profile_t* profile) +{ + switch_xml_config_item_t *instructions = (profile ? get_instructions(profile) : NULL); + switch_xml_config_cleanup(instructions); + free(instructions); + + return SWITCH_STATUS_SUCCESS; +} + +/****************************************************************************************************************************/ +switch_status_t mg_peer_config_cleanup(mg_peer_profile_t* profile) +{ + switch_xml_config_item_t *instructions = (profile ? get_peer_instructions(profile) : NULL); + switch_xml_config_cleanup(instructions); + free(instructions); + + return SWITCH_STATUS_SUCCESS; +} + +/****************************************************************************************************************************/ +static switch_xml_config_item_t *get_peer_instructions(mg_peer_profile_t *profile) { + switch_xml_config_item_t *dup; + + + switch_xml_config_item_t instructions[] = { + /* parameter name type reloadable pointer default value options structure */ + SWITCH_CONFIG_ITEM("ip", SWITCH_CONFIG_STRING, CONFIG_RELOADABLE, &profile->ipaddr, "", &switch_config_string_strdup, "", "Peer IP"), + SWITCH_CONFIG_ITEM("port", SWITCH_CONFIG_STRING, 0, &profile->port, "", &switch_config_string_strdup, "", "peer port"), + SWITCH_CONFIG_ITEM("encoding-scheme", SWITCH_CONFIG_STRING, 0, &profile->encoding_type, "", &switch_config_string_strdup, "", "peer encoding type"), + SWITCH_CONFIG_ITEM("transport-type", SWITCH_CONFIG_STRING, 0, &profile->transport_type, "", &switch_config_string_strdup, "", "peer transport type "), + SWITCH_CONFIG_ITEM("message-identifier", SWITCH_CONFIG_STRING, 0, &profile->mid, "", &switch_config_string_strdup, "", "peer message identifier "), + SWITCH_CONFIG_ITEM_END() + }; + + dup = malloc(sizeof(instructions)); + memcpy(dup, instructions, sizeof(instructions)); + return dup; +} + +/****************************************************************************************************************************/ + +static switch_xml_config_item_t *get_instructions(megaco_profile_t *profile) { + switch_xml_config_item_t *dup; + static switch_xml_config_int_options_t opt_version = { + SWITCH_TRUE, /* enforce min */ + 1, + SWITCH_TRUE, /* enforce Max */ + 3 + }; + +#if 0 + static switch_xml_config_int_options_t opt_termination_id_len = { + SWITCH_TRUE, /* enforce min */ + 1, + SWITCH_TRUE, /* enforce Max */ + 9 + }; + + static switch_xml_config_int_options_t pre_buffer_len = { + SWITCH_TRUE, /* enforce min */ + 0, + SWITCH_TRUE, /* enforce Max */ + 10000 + }; + + static switch_xml_config_enum_item_t opt_default_codec_enum[] = { + { "PCMA", MEGACO_CODEC_PCMA}, + { "PCMU", MEGACO_CODEC_PCMU}, + { "G.729", MEGACO_CODEC_G729}, + { "G.723.1", MEGACO_CODEC_G723_1}, + { "ILBC", MEGACO_CODEC_ILBC }, + }; + + static switch_xml_config_enum_item_t opt_fax_detect_type_enum[] = { + { "CED", MG_FAX_DETECT_EVENT_TYPE_CED}, + { "CNG", MG_FAX_DETECT_EVENT_TYPE_CNG}, + { "CED_CNG", MG_FAX_DETECT_EVENT_TYPE_CNG_CED}, + { "DISABLE", MG_FAX_DETECT_EVENT_TYPE_DISABLE}, + }; +#endif + static switch_xml_config_enum_item_t opt_t38_fax_notify[] = { + { "ENABLE", MG_T38_FAX_NOTIFY_YES}, + { "DISABLE", MG_T38_FAX_NOTIFY_NO}, + }; + + + + switch_xml_config_item_t instructions[] = { + /* parameter name type reloadable pointer default value options structure */ + SWITCH_CONFIG_ITEM("protocol", SWITCH_CONFIG_STRING, CONFIG_RELOADABLE, &profile->protocol_type, "MEGACO", &switch_config_string_strdup, "", "MG Protocol type"), + SWITCH_CONFIG_ITEM("version", SWITCH_CONFIG_INT, CONFIG_RELOADABLE, &profile->protocol_version, 2, &opt_version, "", "MG Protocol version"), + SWITCH_CONFIG_ITEM("local-ip", SWITCH_CONFIG_STRING, CONFIG_RELOADABLE, &profile->my_ipaddr, "127.0.0.1", &switch_config_string_strdup, "", "local ip"), + SWITCH_CONFIG_ITEM("port", SWITCH_CONFIG_STRING, 0, &profile->port, "2944", &switch_config_string_strdup, "", "port"), + SWITCH_CONFIG_ITEM("domain-name", SWITCH_CONFIG_STRING, 0, &profile->my_domain, "", &switch_config_string_strdup, "", "domain name"), + SWITCH_CONFIG_ITEM("message-identifier", SWITCH_CONFIG_STRING, 0, &profile->mid, "", &switch_config_string_strdup, "", "message identifier "), + + //SWITCH_CONFIG_ITEM("default-codec", SWITCH_CONFIG_ENUM, CONFIG_RELOADABLE, &profile->default_codec, "PCMU", &opt_default_codec_enum, "", "default codec"), + //SWITCH_CONFIG_ITEM("rtp-port-range", SWITCH_CONFIG_STRING, CONFIG_REQUIRED, &profile->rtp_port_range, "1-65535", &switch_config_string_strdup, "", "rtp port range"), + SWITCH_CONFIG_ITEM("rtp-termination-id-prefix", SWITCH_CONFIG_STRING, CONFIG_RELOADABLE, &profile->rtp_termination_id_prefix, "", &switch_config_string_strdup, "", "rtp termination prefix"), + //SWITCH_CONFIG_ITEM("rtp-termination-id-length", SWITCH_CONFIG_INT, CONFIG_RELOADABLE, &profile->rtp_termination_id_len, "", &opt_termination_id_len, "", "rtp termination id"), + //SWITCH_CONFIG_ITEM("tdm-pre-buffer-size", SWITCH_CONFIG_INT, CONFIG_RELOADABLE, &profile->tdm_pre_buffer_size, 0, &pre_buffer_len, "", "freetdm pre buffer size"), + SWITCH_CONFIG_ITEM("codec-prefs", SWITCH_CONFIG_STRING, 0, &profile->codec_prefs, "", &switch_config_string_strdup, "", "codec preferences, coma-separated"), + SWITCH_CONFIG_ITEM("license", SWITCH_CONFIG_STRING, 0, &profile->license, "/usr/local/nsg/conf/license.txt", &switch_config_string_strdup, "", "License file"), + SWITCH_CONFIG_ITEM("rtp-ip", SWITCH_CONFIG_STRING, CONFIG_RELOADABLE, &profile->rtp_ipaddr, "" , &switch_config_string_strdup, "", "rtp ip"), + //SWITCH_CONFIG_ITEM("fax-detect-event-type", SWITCH_CONFIG_ENUM, CONFIG_RELOADABLE, &profile->fax_detect_evt_type, MG_FAX_DETECT_EVENT_TYPE_CNG_CED , &opt_fax_detect_type_enum, "", "fax-detect-event-type"), + SWITCH_CONFIG_ITEM("t38-fax-notify", SWITCH_CONFIG_ENUM, CONFIG_RELOADABLE, &profile->t38_fax_notify, MG_T38_FAX_NOTIFY_YES , &opt_t38_fax_notify, "", "t38_fax_notify"), + SWITCH_CONFIG_ITEM_END() + }; + + dup = malloc(sizeof(instructions)); + memcpy(dup, instructions, sizeof(instructions)); + return dup; +} + +/****************************************************************************************************************************/ +static switch_status_t modify_mg_peer_mid(mg_peer_profile_t *peer_profile, char** pmid) +{ + char* mid = *pmid; + switch_assert(mid); + switch_assert(peer_profile); + + if(!strcasecmp(mid,"IP-PORT")){ + *pmid = switch_mprintf("[%s]:%s", peer_profile->ipaddr,peer_profile->port); + } else if(!strcasecmp(mid,"IP")){ + *pmid = switch_mprintf("[%s]", peer_profile->ipaddr); + }else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Invalid mid-type[%s] \n",mid); + return SWITCH_STATUS_FALSE; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Updated PEER MID [%s] \n",*pmid); + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t modify_mg_profile_mid(megaco_profile_t *profile, char** pmid) +{ + char* mid = *pmid; + //char* dup; + //char* val[10]; +// int count; + //switch_status_t status = SWITCH_STATUS_SUCCESS; + switch_assert(mid); + switch_assert(profile); + + if(!strcasecmp(mid,"IP-PORT")){ + *pmid = switch_mprintf("[%s]:%s", profile->my_ipaddr,profile->port); + } else if(!strcasecmp(mid,"IP")){ + *pmid = switch_mprintf("[%s]", profile->my_ipaddr); + } else if(!strcasecmp(mid,"DOMAIN")){ + *pmid = switch_mprintf("<%s>", profile->my_domain); + }else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Invalid mid-type[%s] \n",mid); + return SWITCH_STATUS_FALSE; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Updated MG MID [%s] \n",*pmid); + return SWITCH_STATUS_SUCCESS; +#if 0 + dup = strdup(mid); + + /* If MID type is IP then add mid into [] brackets , + * If MID type is domain then add mid into <> brackets * + */ + + + + count = switch_split(dup, '.', val); + + if(!count) { + /* Input string is not separated by '.', check if its separated by '-' as format could be xxx-xx-xxx/xxx-xx-xx-xxx */ + if(0 == (count = switch_split(dup, '-', val))){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid input MID string[%s]\n",mid); + goto done; + } + } + + if(('<' == val[0][0]) || ('[' == val[0][0])){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "MID = %s is already prefixed with proper brackets \n",mid); + status = SWITCH_STATUS_SUCCESS; + goto done; + } + + /*first check could be if count is 3 means domain name as generally we have xxx-xx-xxx/xxx.xx.xxx domain */ + if(3 == count){ + /* domain-type, add value into <> */ + *pmid = switch_mprintf("<%s>", mid); + free(mid); + mid = *pmid; + }else if(4 == count){ + /* IP address in xxx.xxx.xxx.xxx format */ + *pmid = switch_mprintf("[%s]", mid); + free(mid); + mid = *pmid; + }else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid input MID string[%s]\n",mid); + goto done; + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Added proper brackets to MID = %s \n",mid); + status = SWITCH_STATUS_SUCCESS; + + +done: + return status; +#endif +} diff --git a/src/mod/endpoints/mod_media_gateway/mod_media_gateway.c b/src/mod/endpoints/mod_media_gateway/mod_media_gateway.c new file mode 100644 index 0000000000..395254031c --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/mod_media_gateway.c @@ -0,0 +1,903 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Mathieu Rene +* All rights reserved. +* +* +*/ + +#include "mod_media_gateway.h" +#include "media_gateway_stack.h" + + +/**************************************************************************************************************/ +struct megaco_globals megaco_globals; +static sng_mg_event_interface_t sng_event; + +/**************************************************************************************************************/ +SWITCH_MODULE_LOAD_FUNCTION(mod_media_gateway_load); +SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_media_gateway_shutdown); +SWITCH_MODULE_DEFINITION(mod_media_gateway, mod_media_gateway_load, mod_media_gateway_shutdown, NULL); + +/**************************************************************************************************************/ + +SWITCH_STANDARD_API(megaco_function) +{ + return mg_process_cli_cmd(cmd, stream); +} + +SWITCH_STANDARD_APP(mg_notify_function) +{ + switch_channel_t *channel = switch_core_session_get_channel(session); + mg_termination_t *term = switch_channel_get_private(channel, PVT_MG_TERM); + + if (!term) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "mg_notify called on a non-supported channel.\n"); + return; + } + + if (!strcmp(data, "cng")) { + if((term->profile) && + (term->profile->t38_fax_notify == MG_T38_FAX_NOTIFY_YES) && + !switch_test_flag(term, MG_FAX_NOTIFIED)){ + switch_set_flag(term, MG_FAX_NOTIFIED); + mg_send_t38_cng_notify(term->profile, term->name); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Sent CNG notify\n"); + }else{ + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Not reporting CNG tone " + "FAX Notify[%s] MG_FAX_NOTIFIED flag [%s]\n", + (MG_T38_FAX_NOTIFY_YES == term->profile->t38_fax_notify)?"ENABLE":"DISABLE", + (switch_test_flag(term, MG_FAX_NOTIFIED))?"TRUE":"FALSE"); + } + /* Disable echo cancellation */ + if(MG_TERM_TDM == term->type){ + mg_term_set_ec(term, 0); + } + } else if (!strcmp(data, "ced")) { + if((term->profile) && + (term->profile->t38_fax_notify == MG_T38_FAX_NOTIFY_YES) && + !switch_test_flag(term, MG_FAX_NOTIFIED)){ + switch_set_flag(term, MG_FAX_NOTIFIED); + mg_send_t38_ans_notify(term->profile, term->name); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Sent CED notify\n"); + }else{ + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Not reporting CED tone " + "FAX Notify[%s] MG_FAX_NOTIFIED flag [%s]\n", + (MG_T38_FAX_NOTIFY_YES == term->profile->t38_fax_notify)?"ENABLE":"DISABLE", + (switch_test_flag(term, MG_FAX_NOTIFIED))?"TRUE":"FALSE"); + } + + if(MG_TERM_TDM == term->type){ + mg_term_set_ec(term, 0); + } + } + +} + +static switch_status_t console_complete_hashtable(switch_hash_t *hash, const char *line, const char *cursor, switch_console_callback_match_t **matches) +{ + switch_hash_index_t *hi; + void *val; + const void *vvar; + switch_console_callback_match_t *my_matches = NULL; + switch_status_t status = SWITCH_STATUS_FALSE; + + for (hi = switch_hash_first(NULL, hash); hi; hi = switch_hash_next(hi)) { + switch_hash_this(hi, &vvar, NULL, &val); + switch_console_push_match(&my_matches, (const char *) vvar); + } + + if (my_matches) { + *matches = my_matches; + status = SWITCH_STATUS_SUCCESS; + } + + return status; +} + +static switch_status_t list_profiles(const char *line, const char *cursor, switch_console_callback_match_t **matches) +{ + switch_status_t status; + switch_thread_rwlock_rdlock(megaco_globals.profile_rwlock); + status = console_complete_hashtable(megaco_globals.profile_hash, line, cursor, matches); + switch_thread_rwlock_unlock(megaco_globals.profile_rwlock); + return status; +} + +static void mg_event_handler(switch_event_t *event) +{ + switch(event->event_id) { + case SWITCH_EVENT_TRAP: + { + const char *span_name = NULL; + const char *chan_number = NULL; + const char *cond = NULL; + + cond = switch_event_get_header(event, "condition"); + if (zstr(cond)) { + return; + } + + span_name = switch_event_get_header(event, "span-name"); + chan_number = switch_event_get_header(event, "chan-number"); + + if (!strcmp(cond, "ftdm-alarm-trap")) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "ftdm-alarm-trap for span_name[%s] chan_number[%s]\n", span_name,chan_number); + mg_send_term_service_change((char*)span_name, (char*)chan_number, MG_TERM_SERVICE_STATE_OUT_OF_SERVICE); + } else if (!strcmp(cond, "ftdm-alarm-clear")) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, + "ftdm-alarm-clear for span_name[%s] chan_number[%s] \n", span_name,chan_number); + mg_send_term_service_change( (char*)span_name, (char*)chan_number, MG_TERM_SERVICE_STATE_IN_SERVICE); + } + } + break; + default: + break; + } +} + +SWITCH_MODULE_LOAD_FUNCTION(mod_media_gateway_load) +{ + switch_api_interface_t *api_interface; + switch_application_interface_t *app_interface; + + memset(&megaco_globals, 0, sizeof(megaco_globals)); + megaco_globals.pool = pool; + + *module_interface = switch_loadable_module_create_module_interface(pool, modname); + + switch_core_hash_init(&megaco_globals.profile_hash, pool); + switch_thread_rwlock_create(&megaco_globals.profile_rwlock, pool); + + switch_core_hash_init(&megaco_globals.peer_profile_hash, pool); + switch_thread_rwlock_create(&megaco_globals.peer_profile_rwlock, pool); + + SWITCH_ADD_API(api_interface, "mg", "media_gateway", megaco_function, MEGACO_FUNCTION_SYNTAX); + SWITCH_ADD_APP(app_interface, "mg_notify", "", "sends a notify to the mgc", mg_notify_function, "", 0); + + switch_console_set_complete("add mg profile ::mg::list_profiles start"); + switch_console_set_complete("add mg profile ::mg::list_profiles stop"); + switch_console_set_complete("add mg profile ::mg::list_profiles status"); + switch_console_set_complete("add mg profile ::mg::list_profiles xmlstatus"); + switch_console_set_complete("add mg profile ::mg::list_profiles peerxmlstatus"); + switch_console_set_complete("add mg logging ::mg::list_profiles enable"); + switch_console_set_complete("add mg logging ::mg::list_profiles disable"); + switch_console_add_complete_func("::mg::list_profiles", list_profiles); + + /* Initialize MEGACO Stack */ + sng_event.mg.sng_mgco_txn_ind = handle_mgco_txn_ind; + sng_event.mg.sng_mgco_cmd_ind = handle_mgco_cmd_ind; + sng_event.mg.sng_mgco_txn_sta_ind = handle_mgco_txn_sta_ind; + sng_event.mg.sng_mgco_sta_ind = handle_mgco_sta_ind; + sng_event.mg.sng_mgco_cntrl_cfm = handle_mgco_cntrl_cfm; + sng_event.mg.sng_mgco_audit_cfm = handle_mgco_audit_cfm; + /* Alarm CB */ + sng_event.sm.sng_mg_alarm = handle_mg_alarm; + sng_event.sm.sng_tucl_alarm = handle_tucl_alarm; + /* Log */ + sng_event.sm.sng_log = handle_sng_log; + + switch_event_bind("mod_media_gateway", SWITCH_EVENT_TRAP, SWITCH_EVENT_SUBCLASS_ANY, mg_event_handler, NULL); + + /* initualize MEGACO stack */ + if(SWITCH_STATUS_FALSE == sng_mgco_init(&sng_event)){ + return SWITCH_STATUS_FALSE; + } + + if(SWITCH_STATUS_FALSE == megaco_start_all_profiles()){ + return SWITCH_STATUS_FALSE; + } + + return SWITCH_STATUS_SUCCESS; +} + +SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_media_gateway_shutdown) +{ + void *val = NULL; + const void *key = NULL; + switch_ssize_t keylen; + switch_hash_index_t *hi = NULL; + megaco_profile_t* profile = NULL; + mg_peer_profile_t* peer_profile = NULL; + + /* destroy all the mg profiles */ + while ((hi = switch_hash_first(NULL, megaco_globals.profile_hash))) { + switch_hash_this(hi, &key, &keylen, &val); + profile = (megaco_profile_t *) val; + if(profile->inact_tmr_task_id){ + switch_scheduler_del_task_id(profile->inact_tmr_task_id); + profile->inact_tmr_task_id = 0x00; + } + megaco_profile_destroy(&profile); + profile = NULL; + } + + hi = NULL; + key = NULL; + val = NULL; + /* destroy all the mg peer profiles */ + while ((hi = switch_hash_first(NULL, megaco_globals.peer_profile_hash))) { + switch_hash_this(hi, &key, &keylen, &val); + peer_profile = (mg_peer_profile_t *) val; + megaco_peer_profile_destroy(&peer_profile); + peer_profile = NULL; + } + + sng_mgco_stack_shutdown(); + + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************************/ +switch_status_t megaco_release_all_calls(megaco_profile_t* mg_profile) +{ + mg_context_t* ctx = NULL; + uint32_t context_id = 0x00; + + if(NULL == mg_profile) return SWITCH_STATUS_FALSE; + + for (context_id = 0; context_id < MG_MAX_CONTEXTS; context_id++) { + ctx = megaco_get_context(mg_profile, context_id); + if(NULL == ctx) continue; + + megaco_context_sub_all_termination(ctx); + megaco_release_context(ctx); + } + + return SWITCH_STATUS_SUCCESS; +} + +/*****************************************************************************************************************************/ +switch_status_t megaco_start_all_profiles() +{ + switch_xml_t cfg, xml, mg_interfaces, mg_interface ; + switch_status_t status = SWITCH_STATUS_FALSE; + const char *file = "media_gateway.conf"; + + if (!(xml = switch_xml_open_cfg(file, &cfg, NULL))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not open %s\n", file); + status = SWITCH_STATUS_FALSE; + goto done; + } + + if (!(mg_interfaces = switch_xml_child(cfg, "mg_profiles"))) { + status = SWITCH_STATUS_FALSE; + goto done; + } + + for (mg_interface = switch_xml_child(mg_interfaces, "mg_profile"); mg_interface; mg_interface = mg_interface->next) { + const char *name = switch_xml_attr_soft(mg_interface, "name"); + + if(SWITCH_STATUS_FALSE == megaco_profile_start(name)){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error starting MEGACO profile %s\n", name); + status = SWITCH_STATUS_FALSE; + goto done; + } + } + + status = SWITCH_STATUS_SUCCESS; + +done: + if (xml) { + switch_xml_free(xml); + } + + return status; +} + +/*****************************************************************************************************************************/ +void handle_sng_log(uint8_t level, char *fmt, ...) +{ + int log_level; + char print_buf[1024]; + va_list ptr; + + memset(&print_buf[0],0,sizeof(1024)); + + va_start(ptr, fmt); + + switch(level) + { + case SNG_LOGLEVEL_DEBUG: log_level = SWITCH_LOG_DEBUG; break; + case SNG_LOGLEVEL_INFO: log_level = SWITCH_LOG_INFO; break; + case SNG_LOGLEVEL_WARN: log_level = SWITCH_LOG_WARNING; break; + case SNG_LOGLEVEL_ERROR: log_level = SWITCH_LOG_ERROR; break; + case SNG_LOGLEVEL_CRIT: log_level = SWITCH_LOG_CRIT; break; + default: log_level = SWITCH_LOG_DEBUG; break; + }; + + vsprintf(&print_buf[0], fmt, ptr); + + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, log_level, " MOD_MEGACO: %s \n", &print_buf[0]); + + va_end(ptr); +} +/*****************************************************************************************************************************/ + + +/* KAPIL- NOTE : We are using Command mode operation of MEGACO stack, so we will always get command indication instead of transaction */ +/* Below API is not useful ... just leaving as it is...*/ + +void handle_mgco_txn_ind(Pst *pst, SuId suId, MgMgcoMsg* msg) +{ + size_t txnIter; + switch_memory_pool_t *pool; + + switch_core_new_memory_pool(&pool); + + /*TODO*/ + if(msg->body.type.val == MGT_TXN) + { + /* Loop over transaction list */ + for(txnIter=0;txnIterbody.u.tl.num.val;txnIter++) + { + + switch(msg->body.u.tl.txns[txnIter]->type.val) { + case MGT_TXNREQ: + { + MgMgcoTxnReq* txnReq; + /*MgMgcoTransId transId; *//* XXX */ + int axnIter; + txnReq = &(msg->body.u.tl.txns[txnIter]->u.req); + + /* Loop over action list */ + for (axnIter=0;axnIteral.num.val;axnIter++) { + MgMgcoActionReq *actnReq; + MgMgcoContextId ctxId; + int cmdIter; + + actnReq = txnReq->al.actns[axnIter]; + ctxId = actnReq->cxtId; /* XXX */ + + if (actnReq->pres.pres == NOTPRSNT) { + continue; + } + + /* Loop over command list */ + for (cmdIter=0; cmdIter < (actnReq->cl.num.val); cmdIter++) { + MgMgcoCommandReq *cmdReq = actnReq->cl.cmds[cmdIter]; + /*MgMgcoTermId *term_id = NULLP;*/ + /* The reply we'll send */ + MgMgcoCommand mgCmd; + memset(&mgCmd, 0, sizeof(mgCmd)); + mgCmd.peerId = msg->lcl.id; + mgCmd.u.mgCmdInd[0] = cmdReq; + + + /* XXX Handle choose context before this */ + + mgCmd.contextId = ctxId; + /*mgCmd.transId = transId;*/ + + mgCmd.cmdStatus.pres = PRSNT_NODEF; + + if(cmdIter == (actnReq->cl.num.val -1)) + { + mgCmd.cmdStatus.val = CH_CMD_STATUS_END_OF_AXN; + if(axnIter == (txnReq->al.num.val-1)) + { + mgCmd.cmdStatus.val= CH_CMD_STATUS_END_OF_TXN; + } + } + else + { + mgCmd.cmdStatus.val = CH_CMD_STATUS_PENDING; + } + + /* XXX handle props */ + mgCmd.cmdType.pres = PRSNT_NODEF; + mgCmd.cmdType.val = CH_CMD_TYPE_REQ; + mgCmd.u.mgCmdReq[0] = cmdReq; + sng_mgco_send_cmd(suId, &mgCmd); + + + switch (cmdReq->cmd.type.val) { + case MGT_ADD: + { + MgMgcoAmmReq *addReq = &cmdReq->cmd.u.add; + int descId; + for (descId = 0; descId < addReq->dl.num.val; descId++) { + switch (addReq->dl.descs[descId]->type.val) { + case MGT_MEDIADESC: + { + int mediaId; + for (mediaId = 0; mediaId < addReq->dl.descs[descId]->u.media.num.val; mediaId++) { + MgMgcoMediaPar *mediaPar = addReq->dl.descs[descId]->u.media.parms[mediaId]; + switch (mediaPar->type.val) { + case MGT_MEDIAPAR_LOCAL: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "MGT_MEDIAPAR_LOCAL"); + break; + } + case MGT_MEDIAPAR_REMOTE: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "MGT_MEDIAPAR_REMOTE"); + break; + } + + case MGT_MEDIAPAR_LOCCTL: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "MGT_MEDIAPAR_LOCCTL"); + break; + } + case MGT_MEDIAPAR_TERMST: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "MGT_MEDIAPAR_TERMST"); + break; + } + case MGT_MEDIAPAR_STRPAR: + { +// MgMgcoStreamDesc *mgStream = &mediaPar->u.stream; +// +// if (mgStream->sl.remote.pres.pres) { +// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Got remote stream media description:\n"); +// mgco_print_sdp(&mgStream->sl.remote.sdp); +// } +// +// if (mgStream->sl.local.pres.pres) { +// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Got local stream media description:\n"); +// mgco_print_sdp(&mgStream->sl.local.sdp); +// } + + + break; + } + } + } + } + case MGT_MODEMDESC: + case MGT_MUXDESC: + case MGT_REQEVTDESC: + case MGT_EVBUFDESC: + case MGT_SIGNALSDESC: + case MGT_DIGMAPDESC: + case MGT_AUDITDESC: + case MGT_STATSDESC: + break; + } + } + + + + break; + } + case MGT_MODIFY: + { + /*MgMgcoAmmReq *addReq = &cmdReq->cmd.u.mod;*/ + break; + } + case MGT_MOVE: + { + /*MgMgcoAmmReq *addReq = &cmdReq->cmd.u.move;*/ + break; + + } + case MGT_SUB: + { + /*MgMgcoSubAudReq *addReq = &cmdReq->cmd.u.sub;*/ + } + case MGT_SVCCHG: + case MGT_NTFY: + case MGT_AUDITCAP: + case MGT_AUDITVAL: + break; + } + + } + } + + break; + } + case MGT_TXNREPLY: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MGT_TXNREPLY\n"); + break; + } + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Received unknown command %d in transaction\n", msg->body.u.tl.txns[txnIter]->type.val); + break; + } + } + } + + switch_core_destroy_memory_pool(&pool); +} + +/*****************************************************************************************************************************/ +void handle_mgco_cmd_ind(Pst *pst, SuId suId, MgMgcoCommand* cmd) +{ + MgMgcoContextId out_ctxt; + U32 txn_id = 0x00; + MgMgcoInd *mgErr; + MgStr errTxt; + MgMgcoContextId ctxtId; + MgMgcoContextId *inc_context; + MgMgcoTermIdLst* termLst; + MgMgcoTermId *termId; + int count; + int err_code; + megaco_profile_t* mg_profile; + char prnt_buf[128]; + + memset(&out_ctxt,0,sizeof(out_ctxt)); + memset(&prnt_buf,0,sizeof(prnt_buf)); + + inc_context = &cmd->contextId; + memcpy(&out_ctxt, inc_context,sizeof(MgMgcoContextId)); + + /*switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "%s: Received Command Type[%s] \n", __PRETTY_FUNCTION__, PRNT_MG_CMD_TYPE(cmd->cmdType.val));*/ + + /*get mg profile associated with SuId */ + if(NULL == (mg_profile = megaco_get_profile_by_suId(suId))){ + goto done; + } + + /* first thing - restart ito timer */ + mg_restart_inactivity_timer(mg_profile); + + /* validate Transaction Id */ + if (NOTPRSNT != cmd->transId.pres){ + txn_id = cmd->transId.val; + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s: Transaction Id not present, rejecting\n", __PRETTY_FUNCTION__); + + /*-- Send Error to MG Stack --*/ + MG_ZERO(&ctxtId, sizeof(MgMgcoContextId)); + ctxtId.type.pres = NOTPRSNT; + ctxtId.val.pres = NOTPRSNT; + + mg_util_set_txn_string(&errTxt, &txn_id); + err_code = MGT_MGCO_RSP_CODE_INVLD_IDENTIFIER; + goto error; + } + + /* Get the termination Id list from the command(Note: GCP_2_1 has termination list , else it will be termination Id) */ + termLst = mg_get_term_id_list(cmd); + if ((NULL == termLst) || (NOTPRSNT == termLst->num.pres)) { + /* termination-id not present , error */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Termination-Id Not received..rejecting command \n"); + + /*-- Send Error to MG Stack --*/ + MG_ZERO(&ctxtId, sizeof(MgMgcoContextId)); + ctxtId.type.pres = NOTPRSNT; + ctxtId.val.pres = NOTPRSNT; + mg_util_set_txn_string(&errTxt, &txn_id); + err_code = MGT_MGCO_RSP_CODE_INVLD_IDENTIFIER; + goto error; + } + + termId = termLst->terms[0]; + + + /* Not sure - IF Stack fills term type properly..but adding code just to be sure ...*/ + if ((PRSNT_NODEF == termId->type.pres) && + (MGT_TERMID_OTHER == termId->type.val)) { + /* Checking the $ in the pathname */ + if ((PRSNT_NODEF == termId->name.pres.pres) && + (PRSNT_NODEF == termId->name.lcl.pres)) { + for (count = 0; count < termId->name.lcl.len; count++) { + if (termId->name.lcl.val[count] == '$') { + termId->type.val = MGT_TERMID_CHOOSE; + break; + } + + if (termId->name.lcl.val[count] == '*') { + termId->type.val = MGT_TERMID_ALL; + break; + } + } + } + } + + /*switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "Termination-Id received..value[%s] type[%d] \n", termId->name.lcl.val, termId->type.val);*/ + + /*If term type is other then check if that term is configured with us..for term type CHOOSE/ALL , no need to check */ + /* check is only if command is not AUDIT */ + if ((CH_CMD_TYPE_IND == cmd->cmdType.val) && + (MGT_TERMID_OTHER == termId->type.val) && + (MGT_AUDITVAL != cmd->u.mgCmdInd[0]->cmd.type.val)){ + MG_MEM_COPY(&prnt_buf, termId->name.lcl.val, sizeof(U8) * termId->name.lcl.len); + if(SWITCH_STATUS_FALSE == mg_stack_termination_is_in_service(mg_profile, prnt_buf, strlen(prnt_buf))){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Termination[%s] not in service \n",(strlen(prnt_buf))?prnt_buf:"NULL"); + mg_profile->mg_stats->total_num_of_term_not_in_service_error++; + mg_util_set_term_string(&errTxt, termId); + err_code = MGT_MGCO_RSP_CODE_UNKNOWN_TERM_ID; + goto error; + } + } + + + /* Validate Context - if context is specified then check if its present with us */ + MG_ZERO(&ctxtId, sizeof(MgMgcoContextId)); + memcpy(&ctxtId, inc_context, sizeof(MgMgcoContextId)); + + if(NOTPRSNT == inc_context->type.pres){ + goto ctxt_error; + + }else if(MGT_CXTID_OTHER == inc_context->type.pres){ + + if(NOTPRSNT != inc_context->val.pres){ +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Context specific request for contextId[%d]\n",inc_context->val.val); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,"Context specific request for contextId[%ld]\n",inc_context->val.val); +#endif + /* check if context present with us */ + if(NULL == megaco_find_context_by_suid(suId, inc_context->val.val)){ + goto ctxt_error; + } + }else{ + /* context id value not present - in case of type OTHER we should have context value */ + goto ctxt_error; + } + } + + + /*mgAccEvntPrntMgMgcoCommand(cmd, stdout);*/ + + switch(cmd->cmdType.val) + { + case CH_CMD_TYPE_IND: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s: Received Command indication for command[%s]\n", + __PRETTY_FUNCTION__,PRNT_MG_CMD(cmd->u.mgCmdInd[0]->cmd.type.val)); + + switch(cmd->u.mgCmdInd[0]->cmd.type.val) + { + case MGT_ADD: + { + handle_mg_add_cmd(mg_profile, cmd, &out_ctxt); + break; + } + + case MGT_MODIFY: + { + handle_mg_modify_cmd(mg_profile, cmd); + break; + } + case MGT_MOVE: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MOVE Method Not Yet Supported\n"); + err_code = MGT_MGCO_RSP_CODE_UNSUPPORTED_CMD; + mg_util_set_cmd_name_string(&errTxt, cmd); + goto error; + } + case MGT_SUB: + { + handle_mg_subtract_cmd(mg_profile, cmd); + break; + } + case MGT_SVCCHG: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Service-Change Method Not Yet Supported\n"); + break; + } + case MGT_NTFY: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "NOTIFY Method Not Yet Supported\n"); + break; + } + case MGT_AUDITCAP: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Audit-Capability Method Not Yet Supported\n"); + err_code = MGT_MGCO_RSP_CODE_UNSUPPORTED_CMD; + mg_util_set_cmd_name_string(&errTxt, cmd); + goto error; + } + case MGT_AUDITVAL: + { + handle_mg_audit_cmd(mg_profile, cmd); + break; + } + break; + } + + break; + } + case CH_CMD_TYPE_REQ: + { + break; + } + case CH_CMD_TYPE_RSP: + { + break; + } + case CH_CMD_TYPE_CFM: + { +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Received Command[%s] txn[%d] Response/Confirmation \n", + PRNT_MG_CMD(cmd->u.mgCmdCfm[0]->type.val), txn_id); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Received Command[%s] txn[%ld] Response/Confirmation \n", + PRNT_MG_CMD(cmd->u.mgCmdCfm[0]->type.val), txn_id); +#endif + switch(cmd->u.mgCmdCfm[0]->type.val) + { + case MGT_NTFY: + { + MgMgcoNtfyReply* ntfy = &cmd->u.mgCmdCfm[0]->u.ntfy; + MgMgcoTermId* term = NULL; + char term_name[32]; + memset(&term_name[0], 0x00,32); + + strcpy(&term_name[0], "Invalid"); + +#ifdef GCP_VER_2_1 + if((NOTPRSNT != ntfy->termIdLst.num.pres) && + (0 != ntfy->termIdLst.num.val)){ + term = ntfy->termIdLst.terms[0]; + } +#else + term = &ntfy->termId; + +#endif + if(NOTPRSNT != term->type.pres){ + if(MGT_TERMID_ROOT == term->type.val){ + strcpy(&term_name[0],"ROOT"); + } + else if(MGT_TERMID_OTHER == term->type.val){ + strcpy(&term_name[0], (char*)term->name.lcl.val); + }else if(MGT_TERMID_ALL == term->type.val){ + strcpy(&term_name[0],"ALL Termination"); + }else if(MGT_TERMID_CHOOSE == term->type.val){ + strcpy(&term_name[0],"CHOOSE Termination"); + } + } + + if(NOTPRSNT != ntfy->pres.pres){ + if((NOTPRSNT != ntfy->err.pres.pres) && + (NOTPRSNT != ntfy->err.code.pres)){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "Received NOTIFY command response with ErroCode[%d] for Termination[%s] \n", + ntfy->err.code.val, &term_name[0]); + } + else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "Received Successful NOTIFY command response for Termination[%s] \n", &term_name[0]); + } + } + + break; + } + case MGT_SVCCHG: + { + MgMgcoSvcChgReply* svc = &cmd->u.mgCmdCfm[0]->u.svc; + MgMgcoTermId* term = NULL; + char term_name[32]; + memset(&term_name[0], 0x00, 32); + + strcpy(&term_name[0], "Invalid"); + +#ifdef GCP_VER_2_1 + if((NOTPRSNT != svc->termIdLst.num.pres) && + (0 != svc->termIdLst.num.val)){ + term = svc->termIdLst.terms[0]; + } +#else + term = &svc->termId; + +#endif + if(NOTPRSNT != term->type.pres){ + if(MGT_TERMID_ROOT == term->type.val){ + strcpy(&term_name[0],"ROOT"); + } + else if(MGT_TERMID_OTHER == term->type.val){ + strcpy(&term_name[0], (char*)term->name.lcl.val); + }else if(MGT_TERMID_ALL == term->type.val){ + strcpy(&term_name[0],"ALL Termination"); + }else if(MGT_TERMID_CHOOSE == term->type.val){ + strcpy(&term_name[0],"CHOOSE Termination"); + } + } + + if(NOTPRSNT != svc->pres.pres){ + + if((NOTPRSNT != svc->res.type.pres) && + (MGT_ERRDESC == svc->res.type.val)){ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "Received Service-Change command response with ErroCode[%d] for Termination[%s] \n", + svc->res.u.err.code.val, &term_name[0]); + } + else{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, + "Received Successful Service-Change command response for Termination[%s] \n", &term_name[0]); + } + } + + break; + } + default: + break; + } + break; + } + default: +#ifdef BIT_64 + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Invalid command type[%d]\n",cmd->cmdType.val); +#else + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Invalid command type[%d]\n",cmd->cmdType.val); +#endif + return; + } + + /* END OF TXN received - means last command in txn to process. + * Send response to peer */ + if(CH_CMD_TYPE_IND == cmd->cmdType.val){ + /*if(CH_CMD_STATUS_END_OF_TXN == cmd->cmdStatus.val)*/ + mg_send_end_of_axn(suId, &cmd->transId, &out_ctxt, &cmd->peerId); + } + + goto done; + +ctxt_error: + mg_profile->mg_stats->total_num_of_unknown_ctxt_error++; + err_code = MGT_MGCO_RSP_CODE_UNKNOWN_CTXT; + +error: + if (SWITCH_STATUS_SUCCESS == + mg_build_mgco_err_request(&mgErr, txn_id, &ctxtId, err_code, &errTxt)) { + sng_mgco_send_err(suId, mgErr); + } + if(CH_CMD_STATUS_END_OF_TXN == cmd->cmdStatus.val){ + mg_send_end_of_axn(suId, &cmd->transId, &out_ctxt, &cmd->peerId); + } +done: + if(CH_CMD_TYPE_IND == cmd->cmdType.val){ + mg_free_cmd(cmd->u.mgCmdReq[0]); + }else if(CH_CMD_TYPE_CFM == cmd->cmdType.val){ + mg_free_cmd(cmd->u.mgCmdCfm[0]); + } + return; +} + +/*****************************************************************************************************************************/ +void handle_mgco_sta_ind(Pst *pst, SuId suId, MgMgtSta* sta) +{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s\n", __PRETTY_FUNCTION__); /*TODO*/ +} + +/*****************************************************************************************************************************/ + +void handle_mgco_txn_sta_ind(Pst *pst, SuId suId, MgMgcoInd* txn_sta_ind) +{ + /*TODO*/ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s\n", __PRETTY_FUNCTION__); + + /*dump information*/ + mgAccEvntPrntMgMgcoInd(txn_sta_ind, stdout); + + mg_free_cmd(txn_sta_ind); +} + +/*****************************************************************************************************************************/ +void handle_mgco_cntrl_cfm(Pst *pst, SuId suId, MgMgtCntrl* cntrl, Reason reason) +{ + /*TODO*/ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s\n", __PRETTY_FUNCTION__); +} + +/*****************************************************************************************************************************/ +void handle_mgco_audit_cfm(Pst *pst, SuId suId, MgMgtAudit* audit, Reason reason) +{ + /*TODO*/ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s\n", __PRETTY_FUNCTION__); +} + + +/*****************************************************************************************************************************/ + +/*****************************************************************************************************************************/ +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4: + */ diff --git a/src/mod/endpoints/mod_media_gateway/mod_media_gateway.h b/src/mod/endpoints/mod_media_gateway/mod_media_gateway.h new file mode 100644 index 0000000000..ac18dca7ee --- /dev/null +++ b/src/mod/endpoints/mod_media_gateway/mod_media_gateway.h @@ -0,0 +1,410 @@ +/* +* Copyright (c) 2012, Sangoma Technologies +* Mathieu Rene +* All rights reserved. +* +* +*/ + + +#ifndef MOD_MEGACO_H +#define MOD_MEGACO_H + +#include "sng_mg/sng_mg.h" +#include + +#define MG_MAX_PEERS 5 + + +#define MG_CONTEXT_MAX_TERMS 3 + +#define MEGACO_CLI_SYNTAX "profile|logging" +#define MEGACO_LOGGING_CLI_SYNTAX "logging [enable|disable]" +#define MEGACO_FUNCTION_SYNTAX "profile [name] [start | stop] [status] [xmlstatus] [peerxmlstatus]" + +#define PVT_MG_TERM "_mg_term_" + +struct megaco_globals { + switch_memory_pool_t *pool; + switch_hash_t *profile_hash; + switch_hash_t *peer_profile_hash; + switch_thread_rwlock_t *profile_rwlock; + switch_thread_rwlock_t *peer_profile_rwlock; +}; +extern struct megaco_globals megaco_globals; /* < defined in mod_megaco.c */ + +typedef enum { + PF_RUNNING = (1 << 0) +} megaco_profile_flags_t; + +typedef enum { + MEGACO_CODEC_PCMA, + MEGACO_CODEC_PCMU, + MEGACO_CODEC_G729, + MEGACO_CODEC_G723_1, + MEGACO_CODEC_ILBC, + + /* Nothing below this line */ + MEGACO_CODEC_INVALID = 0xFFFFFF +} megaco_codec_t; + +typedef struct mg_peer_profile_s{ + char *name; + switch_memory_pool_t *pool; + switch_thread_rwlock_t *rwlock; /* < Reference counting rwlock */ + megaco_profile_flags_t flags; + char* ipaddr; /* Peer IP */ + char* port; /*Peer Port */ + char* mid; /* Peer H.248 MID */ + char* transport_type; /* UDP/TCP */ + char* encoding_type; /* Encoding TEXT/Binary */ +} mg_peer_profile_t; + + +typedef struct mg_stats_s{ + uint32_t total_num_of_phy_add_recvd; + uint32_t total_num_of_term_in_service_change_sent; + uint32_t total_num_of_term_oos_service_change_sent; + uint32_t total_num_of_rtp_add_recvd; + uint32_t total_num_of_sub_recvd; + uint32_t total_num_of_call_recvd; + uint32_t total_num_of_t38_call_recvd; + uint32_t total_num_of_add_failed; + uint32_t total_num_of_term_already_in_ctxt_error; + uint32_t total_num_of_choose_ctxt_failed_error; + uint32_t total_num_of_choose_term_failed_error; + uint32_t total_num_of_find_term_failed_error; + uint32_t total_num_of_get_ctxt_failed_error; + uint32_t total_num_of_un_supported_codec_error; + uint32_t total_num_of_add_term_failed_error; + uint32_t total_num_of_term_activation_failed_error; + uint32_t total_num_of_no_term_ctxt_error; + uint32_t total_num_of_term_not_in_service_error; + uint32_t total_num_of_unknown_ctxt_error; +}mg_stats_t; + + +typedef enum { + MG_TERM_FREE = 0, + MG_TERM_TDM, + MG_TERM_RTP +} mg_termination_type_t; + +typedef struct megaco_profile_s megaco_profile_t; +typedef struct mg_context_s mg_context_t; + +/* RTP parameters understood by the controllable channel */ +#define kLOCALADDR "local_addr" +#define kLOCALPORT "local_port" +#define kREMOTEADDR "remote_addr" +#define kREMOTEPORT "remote_port" +#define kCODEC "codec" +#define kPTIME "ptime" +#define kPT "pt" +#define kRFC2833PT "rfc2833_pt" +#define kMODE "mode" +#define kRATE "rate" +#define kMEDIATYPE "media_type" + +/* TDM parameters understood by the controllable channel */ +#define kSPAN_ID "span" +#define kCHAN_ID "chan" +#define kSPAN_NAME "span_name" +#define kPREBUFFER_LEN "prebuffer_len" +#define kECHOCANCEL "echo_cancel" + +typedef struct mg_termination_s mg_termination_t; + +enum { + MGT_ALLOCATED = (1 << 0), + MGT_ACTIVE = (1 << 1), + MG_IN_SERVICE = (1 << 2), + MG_OUT_OF_SERVICE = (1 << 3), + MG_FAX_NOTIFIED = (1 << 4), + MG_DTMF_REMOVAL_ENABLE = (1 << 5), + +} mg_termination_flags; + + +typedef enum { + MG_T38_FAX_NOTIFY_NONE, + MG_T38_FAX_NOTIFY_YES, + MG_T38_FAX_NOTIFY_NO, + MG_T38_FAX_NOTIFY_INVALID, +} mg_t38_fax_notify_t; + +typedef enum { + MG_EC_UNDEFINED = 0, + MG_EC_ENABLE, + MG_EC_DISABLE, + MG_EC_INVALID +} mg_ec_types_t; + +static inline const char *mg_ec_types_tostr(mg_ec_types_t type) { + switch (type) { + case MG_EC_UNDEFINED: + return "UNDEFINED"; + case MG_EC_ENABLE: + return "ENABLE"; + case MG_EC_DISABLE: + return "DISABLE"; + case MG_EC_INVALID: + return "INVALID"; + default: + return "Invalid"; + } + return NULL; +} + + +typedef enum { + MGM_AUDIO = 0, + MGM_IMAGE, + MGM_INVALID +} mg_media_type_t; + +static inline const char *mg_media_type2str(mg_media_type_t type) { + switch (type) { + case MGM_AUDIO: + return "audio"; + case MGM_IMAGE: + return "image"; + case MGM_INVALID: + return NULL; + } + return NULL; +} + +static inline mg_media_type_t mg_media_type_parse(const char *str) { + if (!strcasecmp(str, "audio")) { + return MGM_AUDIO; + } else if (!strcasecmp(str, "image")) { + return MGM_IMAGE; + } + return MGM_INVALID; +} + +typedef enum { + MG_FAX_DETECT_EVENT_TYPE_NONE = 0, + MG_FAX_DETECT_EVENT_TYPE_CED, + MG_FAX_DETECT_EVENT_TYPE_CNG, + MG_FAX_DETECT_EVENT_TYPE_CNG_CED, + MG_FAX_DETECT_EVENT_TYPE_DISABLE, + MG_FAX_DETECT_EVENT_TYPE_INVALID, +} mg_fax_detect_event_type_t; + +static inline const char *mg_fax_detect_evt_type2str(mg_fax_detect_event_type_t type) { + switch (type) { + case MG_FAX_DETECT_EVENT_TYPE_CED: + return "CED"; + case MG_FAX_DETECT_EVENT_TYPE_CNG: + return "CNG"; + case MG_FAX_DETECT_EVENT_TYPE_CNG_CED: + return "CED AND CNG"; + case MG_FAX_DETECT_EVENT_TYPE_DISABLE: + return "DISABLE"; + default: + return "Invalid"; + } + return NULL; +} + +struct mg_context_s { + uint32_t context_id; + mg_termination_t *terminations[MG_CONTEXT_MAX_TERMS]; + megaco_profile_t *profile; + mg_context_t *next; + switch_memory_pool_t *pool; +}; + +struct mg_termination_s { + switch_memory_pool_t *pool; + mg_termination_type_t type; + const char *name; /*!< Megaco Name */ + const char *uuid; /*!< UUID of the associated FS channel, or NULL if it's not activated */ + mg_context_t *context; /*!< Context in which this termination is connected, or NULL */ + megaco_profile_t *profile; /*!< Parent MG profile */ + MgMgcoReqEvtDesc *active_events; /* !< active megaco events */ + mg_termination_t *next; /*!< List for physical terminations */ + int *mg_error_code; /* MEGACO error code */ + uint32_t flags; + const char *tech; /* Endpoint controlling the TDM interface - only FreeTDM tested so far */ + mg_ec_types_t ec_type; + + union { + struct { + /* The RTP termination will automatically operate as "sendonly" or "recvonly" as soon as + * one of the network addresses are NULL */ + const char *local_addr; /*!< RTP Session's Local IP address */ + switch_port_t local_port; /*!< RTP Session's Local IP address */ + + const char *remote_addr; /*!< RTP Session's Remote IP address */ + switch_port_t remote_port; /*!< RTP Session's Remote IP address */ + + int ptime; /*!< Packetization Interval, in miliseconds. The default is 20, but it has to be set */ + int pt; /*!< Payload type */ + int rfc2833_pt; /*!< If the stream is using rfc2833 for dtmf events, this has to be set to its negotiated payload type */ + int rate; /*!< Sampling rate */ + const char *codec; /*!< Codec to use, using the freeswitch nomenclature. This could be "PCMU" for G711.U, "PCMA" for G711.A, or "G729" for g729 */ + int term_id; + switch_t38_options_t *t38_options; + mg_media_type_t media_type; + } rtp; + + struct { + int channel; + const char *span_name; + } tdm; + } u; +}; + + + + +#define MG_CONTEXT_MODULO 16 +#define MG_MAX_CONTEXTS 32768 +#define MG_MAX_RTPID 32768 + + +struct megaco_profile_s { + char *name; + switch_memory_pool_t *pool; + switch_thread_rwlock_t *rwlock; /* < Reference counting rwlock */ + megaco_profile_flags_t flags; + int idx; /* Trillium MEGACO SAP identification*/ + char* mid; /* MG H.248 MID */ + char* my_domain; /* local domain name */ + char* my_ipaddr; /* local domain name */ + char* rtp_ipaddr; /* local rtp ip */ + char* port; /* port */ + char* protocol_type; /* MEGACO/MGCP */ + char* license; /* License file location/name */ + int protocol_version; /* Protocol supported version */ + int total_peers; + int total_cfg_term; + megaco_codec_t default_codec; + char* rtp_port_range; + char* rtp_termination_id_prefix; + int rtp_termination_id_len; + int tdm_pre_buffer_size; + char* peer_list[MG_MAX_PEERS]; /* MGC Peer ID LIST */ + char* codec_prefs; + int inact_tmr; /* inactivity timer value */ + int peer_active; /* inactivity timer value */ + uint32_t inact_tmr_task_id; /* FS timer scheduler task-id */ + mg_t38_fax_notify_t t38_fax_notify; + + switch_thread_rwlock_t *contexts_rwlock; + uint32_t next_context_id; + uint8_t contexts_bitmap[MG_MAX_CONTEXTS/8]; /* Availability matrix, enough bits for a 32768 bitmap */ + mg_context_t *contexts[MG_CONTEXT_MODULO]; + + uint8_t rtpid_bitmap[MG_MAX_CONTEXTS/8]; + uint32_t rtpid_next; + + mg_termination_t *physical_terminations; + mg_stats_t* mg_stats; + + switch_hash_t *terminations; + switch_thread_rwlock_t *terminations_rwlock; +}; + + + +static inline const char *megaco_codec_str(megaco_codec_t codec) +{ + switch (codec) { + case MEGACO_CODEC_PCMU: + return "PCMU"; + case MEGACO_CODEC_PCMA: + return "PCMA"; + case MEGACO_CODEC_G729: + return "G729"; + case MEGACO_CODEC_G723_1: + return "G723"; /* XXX double check this */ + case MEGACO_CODEC_ILBC: + return "ILBC"; + case MEGACO_CODEC_INVALID: + default: + return NULL; + } +} + +static inline megaco_codec_t megaco_codec_parse(const char *codec) { + if (!strcasecmp(codec, "PCMU")) { + return MEGACO_CODEC_PCMU; + } else if (!strcasecmp(codec, "PCMA")) { + return MEGACO_CODEC_PCMA; + } else if (!strcasecmp(codec, "G729")) { + return MEGACO_CODEC_G729; + } else if (!strcasecmp(codec, "G723")) { + return MEGACO_CODEC_G723_1; + } else if (!strcasecmp(codec, "ILBC")) { + return MEGACO_CODEC_ILBC; + } else { + return MEGACO_CODEC_INVALID; + } +} + + +switch_status_t megaco_tdm_term_dtmf_removal(mg_termination_t *term, int enable); +megaco_profile_t *megaco_profile_locate(const char *name); +mg_termination_t *megaco_term_locate_by_span_chan_id(const char *span_name, const char *chan_number); +mg_peer_profile_t *megaco_peer_profile_locate(const char *name); +void megaco_profile_release(megaco_profile_t *profile); +mg_termination_t* megaco_find_termination_by_span_chan(megaco_profile_t *profile, const char *span_name, const char *chan_number); + +switch_status_t megaco_start_all_profiles(void); +switch_status_t megaco_profile_start(const char *profilename); +switch_status_t megaco_profile_destroy(megaco_profile_t **profile); +switch_status_t megaco_release_all_calls(megaco_profile_t* mg_profile); + +uint32_t mg_rtp_request_id(megaco_profile_t *profile); +void mg_rtp_release_id(megaco_profile_t *profile, uint32_t id); +void mg_term_set_pre_buffer_size(mg_termination_t *term, int newval); +void mg_term_set_ec(mg_termination_t *term, int enable); + +mg_context_t *megaco_get_context(megaco_profile_t *profile, uint32_t context_id); +mg_context_t *megaco_choose_context(megaco_profile_t *profile); +void megaco_release_context(mg_context_t *ctx); +switch_status_t megaco_context_sub_termination(mg_context_t *ctx, mg_termination_t *term); +switch_status_t megaco_context_sub_all_termination(mg_context_t *ctx); +switch_status_t megaco_activate_termination(mg_termination_t *term); +switch_status_t megaco_prepare_termination(mg_termination_t *term); + +mg_termination_t *megaco_choose_termination(megaco_profile_t *profile, const char *prefix); +mg_termination_t *megaco_find_termination(megaco_profile_t *profile, const char *name); +void megaco_termination_destroy(mg_termination_t *term); + +megaco_profile_t* megaco_get_profile_by_suId(SuId suId); +mg_context_t *megaco_find_context_by_suid(SuId suId, uint32_t context_id); + +switch_status_t config_profile(megaco_profile_t *profile, switch_bool_t reload); +switch_status_t sng_mgco_start(megaco_profile_t* profile); +switch_status_t sng_mgco_stop(megaco_profile_t* profile); +switch_status_t mg_config_cleanup(megaco_profile_t* profile); +switch_status_t mg_peer_config_cleanup(mg_peer_profile_t* profile); +switch_status_t megaco_peer_profile_destroy(mg_peer_profile_t **profile); +switch_status_t mg_process_cli_cmd(const char *cmd, switch_stream_handle_t *stream); +switch_status_t megaco_context_add_termination(mg_context_t *ctx, mg_termination_t *term); +switch_status_t megaco_context_is_term_present(mg_context_t *ctx, mg_termination_t *term); + +switch_status_t megaco_prepare_tdm_termination(mg_termination_t *term); +switch_status_t megaco_check_tdm_termination(mg_termination_t *term); +mg_termination_t* megaco_context_get_peer_term(mg_context_t *ctx, mg_termination_t *term); + + + +#endif /* MOD_MEGACO_H */ + + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4: + */ diff --git a/src/mod/endpoints/mod_opal/mod_opal.cpp b/src/mod/endpoints/mod_opal/mod_opal.cpp index 0760588019..82827015a7 100644 --- a/src/mod/endpoints/mod_opal/mod_opal.cpp +++ b/src/mod/endpoints/mod_opal/mod_opal.cpp @@ -4,6 +4,7 @@ * Version: MPL 1.1 * * Copyright (c) 2007 Tuyan Ozipek (tuyanozipek@gmail.com) + * Copyright (c) 2008-2012 Vox Lucida Pty. Ltd. (robertj@voxlucida.com.au) * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with @@ -28,29 +29,34 @@ #include #include -SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_codec_string, mod_opal_globals.codec_string); -SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_context, mod_opal_globals.context); -SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, mod_opal_globals.dialplan); + +/* FreeSWITCH does not correctly handle an H.323 subtely, that is that a + MAXIMUM audio frames per packet is nototiated, and there is no + requirement for the remote to actually send that many. So, in say GSM, we + negotiate up to 3 frames or 60ms of data and the remote actually sends one + (20ms) frame per packet. Perfectly legal but blows up the media handling + in FS. + + Eventually we will get around to bundling the packets, but not yet. This + compile flag will just force one frame/packet for all audio codecs. + */ +#define IMPLEMENT_MULTI_FAME_AUDIO 0 -#define CF_NEED_FLUSH (1 << 1) - -struct mod_opal_globals mod_opal_globals = { 0 }; - - -static switch_call_cause_t create_outgoing_channel(switch_core_session_t *session, switch_event_t *var_event, - switch_caller_profile_t *outbound_profile, switch_core_session_t **new_session, - switch_memory_pool_t **pool, switch_originate_flag_t flags, switch_call_cause_t *cancel_cause); +static switch_call_cause_t create_outgoing_channel(switch_core_session_t *session, + switch_event_t *var_event, + switch_caller_profile_t *outbound_profile, + switch_core_session_t **new_session, + switch_memory_pool_t **pool, + switch_originate_flag_t flags, + switch_call_cause_t *cancel_cause); static FSProcess *opal_process = NULL; -static const char ModuleName[] = "opal"; - - -static switch_status_t on_hangup(switch_core_session_t *session); -static switch_status_t on_destroy(switch_core_session_t *session); +static PConstString const ModuleName("opal"); +static char const ConfigFile[] = "opal.conf"; static switch_io_routines_t opalfs_io_routines = { @@ -70,7 +76,7 @@ static switch_state_handler_table_t opalfs_event_handlers = { /*.on_init */ FSConnection::on_init, /*.on_routing */ FSConnection::on_routing, /*.on_execute */ FSConnection::on_execute, - /*.on_hangup */ on_hangup, + /*.on_hangup */ FSConnection::on_hangup, /*.on_exchange_media */ FSConnection::on_exchange_media, /*.on_soft_execute */ FSConnection::on_soft_execute, /*.on_consume_media*/ NULL, @@ -78,7 +84,7 @@ static switch_state_handler_table_t opalfs_event_handlers = { /*.on_reset*/ NULL, /*.on_park*/ NULL, /*.on_reporting*/ NULL, - /*.on_destroy*/ on_destroy + /*.on_destroy*/ FSConnection::on_destroy }; @@ -89,7 +95,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_opal_load); SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_opal_shutdown); SWITCH_MODULE_DEFINITION(mod_opal, mod_opal_load, mod_opal_shutdown, NULL); -SWITCH_MODULE_LOAD_FUNCTION(mod_opal_load) { +SWITCH_MODULE_LOAD_FUNCTION(mod_opal_load) +{ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Starting loading mod_opal\n"); /* Prevent the loading of OPAL codecs via "plug ins", this is a directory @@ -110,8 +117,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_opal_load) { if (opal_process->Initialise(*module_interface)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Opal manager initialized and running\n"); //unloading causes a seg in linux - return SWITCH_STATUS_NOUNLOAD; - //return SWITCH_STATUS_SUCCESS; + //return SWITCH_STATUS_UNLOAD; + return SWITCH_STATUS_SUCCESS; } delete opal_process; @@ -120,11 +127,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_opal_load) { } -SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_opal_shutdown) { - - switch_safe_free(mod_opal_globals.context); - switch_safe_free(mod_opal_globals.dialplan); - switch_safe_free(mod_opal_globals.codec_string); +SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_opal_shutdown) +{ delete opal_process; opal_process = NULL; return SWITCH_STATUS_SUCCESS; @@ -133,161 +137,101 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_opal_shutdown) { SWITCH_END_EXTERN_C /*******************************************************************************/ - - -static switch_call_cause_t create_outgoing_channel(switch_core_session_t *session, - switch_event_t *var_event, - switch_caller_profile_t *outbound_profile, - switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags, switch_call_cause_t *cancel_cause) -{ - if (opal_process == NULL) { - return SWITCH_CAUSE_CRASH; - } - - PString token; - - FSManager & manager = opal_process->GetManager(); - if (!manager.SetUpCall("local:", outbound_profile->destination_number, token, outbound_profile)) { - return SWITCH_CAUSE_INVALID_NUMBER_FORMAT; - } - - PSafePtr < OpalCall > call = manager.FindCallWithLock(token); - - if (call == NULL) { - return SWITCH_CAUSE_PROTOCOL_ERROR; - } - - PSafePtr < FSConnection > connection = call->GetConnectionAs < FSConnection > (0); - - if (connection == NULL) { - return SWITCH_CAUSE_PROTOCOL_ERROR; - } - - *new_session = connection->GetSession(); - - return SWITCH_CAUSE_SUCCESS; -} - - /////////////////////////////////////////////////////////////////////// #if PTRACING -class FSTrace : public ostream { - public: - FSTrace() - : ostream(&buffer) - { - } +class FSTrace : public std::ostream +{ +private: + class Buffer : public std::stringbuf + { + virtual int sync() + { + std::string s = str(); + if (s.empty()) + return 0; - private: - class Buffer : public streambuf { - char buffer[250]; + //Due to explicit setting of flags we know exactly what we are getting + #define THREAD_ID_INDEX 2 + #define FILE_NAME_INDEX 3 + #define FILE_LINE_INDEX 4 +#if PTLIB_CHECK_VERSION(2,11,1) + #define CONTEXT_ID_REGEX "([0-9]+|- - - - - - -)\t" + #define LOG_PRINTF_FORMAT "{%s,%s} %s" + #define FULL_TEXT_INDEX 6 +#else + #define CONTEXT_ID_REGEX + #define LOG_PRINTF_FORMAT "{%s} %s" + #define FULL_TEXT_INDEX 5 +#endif + PStringArray fields(7); + static PRegularExpression logRE("^([0-9]+)\t *(.+)\t *([^(]+)\\(([0-9]+)\\)\t"CONTEXT_ID_REGEX"(.*)", + PRegularExpression::Extended); + if (!logRE.Execute(s.c_str(), fields)) { + fields[1] = "4"; + fields[THREAD_ID_INDEX] = "unknown"; + fields[FILE_NAME_INDEX] = __FILE__; + fields[FILE_LINE_INDEX] = __LINE__; + fields[FULL_TEXT_INDEX] = s; + } - public: - Buffer() - { - setg(buffer, buffer, &buffer[sizeof(buffer)-2]); - setp(buffer, &buffer[sizeof(buffer)-2]); - } + switch_log_level_t level; + switch (fields[1].AsUnsigned()) { + case 0 : + level = SWITCH_LOG_ALERT; + break; + case 1 : + level = SWITCH_LOG_ERROR; + break; + case 2 : + level = SWITCH_LOG_WARNING; + break; + case 3 : + level = SWITCH_LOG_INFO; + break; + default : + level = SWITCH_LOG_DEBUG; + break; + } - virtual int sync() - { - return overflow(EOF); - } + fields[4].Replace("\t", " ", true); +#if PTLIB_CHECK_VERSION(2,11,1) + fields[5].Replace("- - - - - - -", "-"), +#endif + switch_log_printf(SWITCH_CHANNEL_ID_LOG, + fields[FILE_NAME_INDEX], + "PTLib-OPAL", + fields[FILE_LINE_INDEX].AsUnsigned(), + NULL, + level, + LOG_PRINTF_FORMAT, + fields[THREAD_ID_INDEX].GetPointer(), +#if PTLIB_CHECK_VERSION(2,11,1) + fields[5].GetPointer(), +#endif + fields[FULL_TEXT_INDEX].GetPointer()); - virtual int underflow() - { - return EOF; - } + // Reset string + str(std::string()); + return 0; + } + } buffer; - virtual int overflow(int c) - { - const char *fmt = "%s"; - char *func = NULL; - - int bufSize = pptr() - pbase(); - - if (c != EOF) { - *pptr() = (char)c; - bufSize++; - } - - if (bufSize != 0) { - char *bufPtr = pbase(); - char *bufEndPtr = NULL; - setp(bufPtr, epptr()); - bufPtr[bufSize] = '\0'; - int line = 0; - char *p; - - char *file = NULL; - switch_log_level_t level; - - - switch (strtoul(bufPtr, &file, 10)) { - case 1 : - level = SWITCH_LOG_INFO; - break; - default : - level = SWITCH_LOG_DEBUG; - break; - } - - if (file) { - while (isspace(*file)) file++; - - if (file && (bufPtr = strchr(file, '(')) && (bufEndPtr = strchr(bufPtr, ')'))) { - char *e; - - for(p = bufPtr; p && *p; p++) { - if (*p == '\t') { - *p = ' '; - } - } - - *bufPtr++ = '\0'; - line = atoi(bufPtr); - while (bufEndPtr && isspace(*(++bufEndPtr))); - bufPtr = bufEndPtr; - if (bufPtr && ((e = strchr(bufPtr, ' ')) || (e = strchr(bufPtr, '\t')))) { - func = bufPtr; - bufPtr = e; - *bufPtr++ = '\0'; - } - } - } - - switch_text_channel_t tchannel = SWITCH_CHANNEL_ID_LOG; - - if (!bufPtr) { - bufPtr = pbase(); - level = SWITCH_LOG_DEBUG; - } - - if (bufPtr) { - if (end_of(bufPtr) != '\n') { - fmt = "%s\n"; - } - if (!(file && func && line)) tchannel = SWITCH_CHANNEL_ID_LOG_CLEAN; - - switch_log_printf(tchannel, file, func, line, NULL, level, fmt, bufPtr); - } - - } - - return 0; - } - } buffer; +public: + FSTrace() + : ostream(&buffer) + { + } }; -#endif +#endif // PTRACING /////////////////////////////////////////////////////////////////////// FSProcess::FSProcess() - : PLibraryProcess("Vox Lucida Pty. Ltd.", "mod_opal", 1, 0, AlphaCode, 1) + : PLibraryProcess("Vox Lucida Pty. Ltd.", MODNAME, 1, 1, BetaCode, 1) , m_manager(NULL) { } @@ -296,19 +240,24 @@ FSProcess::FSProcess() FSProcess::~FSProcess() { delete m_manager; +#if PTRACING + PTrace::SetStream(NULL); // This will delete the FSTrace object +#endif } bool FSProcess::Initialise(switch_loadable_module_interface_t *iface) { - m_manager = new FSManager(); - return m_manager != NULL && m_manager->Initialise(iface); + m_manager = new FSManager(); + return m_manager != NULL && m_manager->Initialise(iface); } /////////////////////////////////////////////////////////////////////// FSManager::FSManager() + : m_context("default") + , m_dialplan("XML") { // These are deleted by the OpalManager class, no need to have destructor m_h323ep = new H323EndPoint(*this); @@ -321,12 +270,6 @@ bool FSManager::Initialise(switch_loadable_module_interface_t *iface) { ReadConfig(false); -#if PTRACING - PTrace::SetLevel(mod_opal_globals.trace_level); //just for fun and eyecandy ;) - PTrace::SetOptions(PTrace::TraceLevel); - PTrace::SetStream(new FSTrace); -#endif - m_FreeSwitch = (switch_endpoint_interface_t *) switch_loadable_module_create_interface(iface, SWITCH_ENDPOINT_INTERFACE); m_FreeSwitch->interface_name = ModuleName; m_FreeSwitch->io_routines = &opalfs_io_routines; @@ -338,8 +281,8 @@ bool FSManager::Initialise(switch_loadable_module_interface_t *iface) m_h323ep->StartListener(""); } else { for (std::list < FSListener >::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it) { - if (!m_h323ep->StartListener(it->listenAddress)) { - PTRACE(3, "mod_opal\tCannot start listener for " << it->name); + if (!m_h323ep->StartListener(OpalTransportAddress(it->m_address, it->m_port))) { + PTRACE(2, "mod_opal\tCannot start listener for " << it->m_name); } } } @@ -363,15 +306,16 @@ bool FSManager::Initialise(switch_loadable_module_interface_t *iface) GetOpalGSMAMR(); GetOpaliLBC(); - /* For compatibility with the algorithm in FSConnection::SetCodecs() we need - to set all audio media formats to be 1 frame per packet */ +#if !IMPLEMENT_MULTI_FAME_AUDIO OpalMediaFormatList allCodecs = OpalMediaFormat::GetAllRegisteredMediaFormats(); for (OpalMediaFormatList::iterator it = allCodecs.begin(); it != allCodecs.end(); ++it) { if (it->GetMediaType() == OpalMediaType::Audio()) { it->SetOptionInteger(OpalAudioFormat::RxFramesPerPacketOption(), 1); it->SetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption(), 1); + OpalMediaFormat::SetRegisteredMediaFormat(*it); } } +#endif // IMPLEMENT_MULTI_FAME_AUDIO if (!m_gkAddress.IsEmpty()) { if (m_h323ep->UseGatekeeper(m_gkAddress, m_gkIdentifer, m_gkInterface)) @@ -391,61 +335,55 @@ bool FSManager::Initialise(switch_loadable_module_interface_t *iface) switch_status_t FSManager::ReadConfig(int reload) { - const char *cf = "opal.conf"; - switch_status_t status = SWITCH_STATUS_SUCCESS; + switch_event_t *request_params = NULL; + switch_event_create(&request_params, SWITCH_EVENT_REQUEST_PARAMS); + switch_assert(request_params); + switch_event_add_header_string(request_params, SWITCH_STACK_BOTTOM, "profile", switch_str_nil("")); - switch_memory_pool_t *pool = NULL; - if ((status = switch_core_new_memory_pool(&pool)) != SWITCH_STATUS_SUCCESS) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n"); - return status; - } - - set_global_context("default"); - set_global_dialplan("XML"); - - switch_event_t *params = NULL; - switch_event_create(¶ms, SWITCH_EVENT_REQUEST_PARAMS); - switch_assert(params); - switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "profile", switch_str_nil("")); switch_xml_t cfg; - switch_xml_t xml = switch_xml_open_cfg(cf, &cfg, params); + switch_xml_t xml = switch_xml_open_cfg(ConfigFile, &cfg, request_params); if (xml == NULL) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", ConfigFile); return SWITCH_STATUS_FALSE; } switch_xml_t xmlSettings = switch_xml_child(cfg, "settings"); if (xmlSettings) { for (switch_xml_t xmlParam = switch_xml_child(xmlSettings, "param"); xmlParam != NULL; xmlParam = xmlParam->next) { - const char *var = switch_xml_attr_soft(xmlParam, "name"); - const char *val = switch_xml_attr_soft(xmlParam, "value"); + PConstCaselessString const var(switch_xml_attr_soft(xmlParam, "name")); + PConstString const val(switch_xml_attr_soft(xmlParam, "value")); - if (!strcasecmp(var, "trace-level")) { - int level = atoi(val); - if (level > 0) { - mod_opal_globals.trace_level = level; - } - } else if (!strcasecmp(var, "context")) { - set_global_context(val); - } else if (!strcasecmp(var, "dialplan")) { - set_global_dialplan(val); - } else if (!strcasecmp(var, "codec-prefs")) { - set_global_codec_string(val); - } else if (!strcasecmp(var, "jitter-size")) { - char * next; - unsigned minJitter = strtoul(val, &next, 10); - if (minJitter >= 10) { - unsigned maxJitter = minJitter; - if (*next == ',') - maxJitter = atoi(next+1); - SetAudioJitterDelay(minJitter, maxJitter); // In milliseconds - } - } else if (!strcasecmp(var, "gk-address")) { + if (var == "context") { + m_context = val; + } else if (var == "dialplan") { + m_dialplan = val; + } else if (var == "codec-prefs") { + m_codecPrefs = val; + } else if (var == "disable-transcoding") { + m_disableTranscoding = switch_true(val); + } else if (var == "jitter-size") { + SetAudioJitterDelay(val.AsUnsigned(), val.Mid(val.Find(',')+1).AsUnsigned()); // In milliseconds + } else if (var == "gk-address") { m_gkAddress = val; - } else if (!strcasecmp(var, "gk-identifer")) { + } else if (var == "gk-identifer") { m_gkIdentifer = val; - } else if (!strcasecmp(var, "gk-interface")) { + } else if (var == "gk-interface") { m_gkInterface = val; +#if PTRACING + } else if (var == "trace-level") { + unsigned level = val.AsUnsigned(); + if (level > 0) { + PTrace::SetLevel(level); + PTrace::ClearOptions(0xffffffff); // Everything off + PTrace::SetOptions( // Except these + PTrace::TraceLevel|PTrace::FileAndLine|PTrace::Thread +#if PTLIB_CHECK_VERSION(2,11,1) + |PTrace::ContextIdentifier +#endif + ); + PTrace::SetStream(new FSTrace); + } +#endif } } } @@ -457,196 +395,206 @@ switch_status_t FSManager::ReadConfig(int reload) m_listeners.push_back(FSListener()); FSListener & listener = m_listeners.back(); - listener.name = switch_xml_attr_soft(xmlListener, "name"); - if (listener.name.IsEmpty()) - listener.name = "unnamed"; - - PIPSocket::Address ip; - WORD port = 1720; + listener.m_name = switch_xml_attr_soft(xmlListener, "name"); + if (listener.m_name.IsEmpty()) + listener.m_name = "unnamed"; for (switch_xml_t xmlParam = switch_xml_child(xmlListener, "param"); xmlParam != NULL; xmlParam = xmlParam->next) { - const char *var = switch_xml_attr_soft(xmlParam, "name"); - const char *val = switch_xml_attr_soft(xmlParam, "value"); - //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Var - '%s' and Val - '%s' \n", var, val); - if (!strcasecmp(var, "h323-ip")) - ip = val; - else if (!strcasecmp(var, "h323-port")) - port = (WORD) atoi(val); + PConstCaselessString const var(switch_xml_attr_soft(xmlParam, "name")); + PConstString const val(switch_xml_attr_soft(xmlParam, "value")); + if (var == "h323-ip") + listener.m_address = val; + else if (var == "h323-port") + listener.m_port = (uint16_t)val.AsUnsigned(); } - listener.listenAddress = OpalTransportAddress(ip, port); - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created Listener '%s'\n", (const char *) listener.name); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created Listener '%s'\n", (const char *) listener.m_name); } } - switch_event_destroy(¶ms); + switch_event_destroy(&request_params); if (xml) switch_xml_free(xml); - return status; + return SWITCH_STATUS_SUCCESS; } -OpalCall * FSManager::CreateCall(void * /*userData*/) +static switch_call_cause_t create_outgoing_channel(switch_core_session_t *session, + switch_event_t *var_event, + switch_caller_profile_t *outbound_profile, + switch_core_session_t **new_session, + switch_memory_pool_t **pool, + switch_originate_flag_t flags, + switch_call_cause_t *cancel_cause) { - return new FSCall(*this); + if (opal_process == NULL) + return SWITCH_CAUSE_CRASH; + + FSConnection::outgoing_params params; + params.var_event = var_event; + params.outbound_profile = outbound_profile; + params.new_session = new_session; + params.pool = pool; + params.flags = flags; + params.cancel_cause = cancel_cause; + params.fail_cause = SWITCH_CAUSE_INVALID_NUMBER_FORMAT; + + if (opal_process->GetManager().SetUpCall("local:", outbound_profile->destination_number, ¶ms) != NULL) + return SWITCH_CAUSE_SUCCESS; + + if (*new_session != NULL) + switch_core_session_destroy(new_session); + return params.fail_cause; } /////////////////////////////////////////////////////////////////////// FSEndPoint::FSEndPoint(FSManager & manager) -: OpalLocalEndPoint(manager) + : OpalLocalEndPoint(manager) + , m_manager(manager) { - PTRACE(3, "mod_opal\t FSEndPoint Created!"); -} - - -bool FSEndPoint::OnIncomingCall(OpalLocalConnection & connection) -{ - return ((FSConnection &) connection).OnIncoming(); + PTRACE(4, "mod_opal\tFSEndPoint created."); } OpalLocalConnection *FSEndPoint::CreateConnection(OpalCall & call, void *userData, unsigned options, OpalConnection::StringOptions* stringOptions) { - FSManager & mgr = (FSManager &) GetManager(); - switch_core_session_t *fsSession = switch_core_session_request(mgr.GetSwitchInterface(), - (switch_caller_profile_t *)userData ? SWITCH_CALL_DIRECTION_OUTBOUND : SWITCH_CALL_DIRECTION_INBOUND, SOF_NONE, NULL); - if (fsSession == NULL) - return NULL; - - switch_channel_t *fsChannel = switch_core_session_get_channel(fsSession); - - if (fsChannel == NULL) { - switch_core_session_destroy(&fsSession); - return NULL; - } - - return new FSConnection(call, *this, userData, options, stringOptions, (switch_caller_profile_t *)userData, fsSession, fsChannel); + return new FSConnection(call, *this, options, stringOptions, (FSConnection::outgoing_params *)userData); } /////////////////////////////////////////////////////////////////////// -FSCall::FSCall(OpalManager & manager) - : OpalCall(manager) -{ -} - - -PBoolean FSCall::OnSetUp(OpalConnection & connection) -{ - // Transfer FS caller_id_number & caller_id_name from the FSConnection - // to the protocol connectionm (e.g. H.323) so gets sent correctly - // in outgoing packets - PSafePtr local = GetConnectionAs(); - if (local != NULL) { - PSafePtr proto = local->GetOtherPartyConnection(); - if (proto != NULL) { - proto->SetLocalPartyName(local->GetLocalPartyName()); - proto->SetDisplayName(local->GetDisplayName()); - } - } - - return OpalCall::OnSetUp(connection); -} - - -/////////////////////////////////////////////////////////////////////// - - -FSConnection::FSConnection(OpalCall & call, FSEndPoint & endpoint, void* userData, unsigned options, OpalConnection::StringOptions* stringOptions, switch_caller_profile_t *outbound_profile, switch_core_session_t *fsSession, switch_channel_t *fsChannel) - : OpalLocalConnection(call, endpoint, userData, options, stringOptions) +FSConnection::FSConnection(OpalCall & call, + FSEndPoint & endpoint, + unsigned options, + OpalConnection::StringOptions* stringOptions, + outgoing_params * params) + : OpalLocalConnection(call, endpoint, NULL, options, stringOptions) , m_endpoint(endpoint) - , m_fsSession(fsSession) - , m_fsChannel(fsChannel) + , m_fsSession(NULL) + , m_fsChannel(NULL) + , m_flushAudio(false) { - opal_private_t *tech_pvt; + memset(&m_read_timer, 0, sizeof(m_read_timer)); + memset(&m_read_codec, 0, sizeof(m_read_codec)); + memset(&m_write_codec, 0, sizeof(m_write_codec)); + memset(&m_vid_read_timer, 0, sizeof(m_vid_read_timer)); + memset(&m_vid_read_codec, 0, sizeof(m_vid_read_codec)); + memset(&m_vid_write_codec, 0, sizeof(m_vid_write_codec)); - tech_pvt = (opal_private_t *) switch_core_session_alloc(m_fsSession, sizeof(*tech_pvt)); - tech_pvt->me = this; - switch_core_session_set_private(m_fsSession, tech_pvt); + if (params != NULL) { + // If we fail, this is the cause + params->fail_cause = SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER; - if (outbound_profile != NULL) { - SetLocalPartyName(outbound_profile->caller_id_number); - SetDisplayName(outbound_profile->caller_id_name); - - switch_caller_profile_t *caller_profile = switch_caller_profile_clone(m_fsSession, outbound_profile); - switch_channel_set_caller_profile(m_fsChannel, caller_profile); - - PString name = "opal/"; - name += outbound_profile->destination_number; - switch_channel_set_name(m_fsChannel, name); - - switch_channel_set_state(m_fsChannel, CS_INIT); + if ((m_fsSession = switch_core_session_request(endpoint.GetManager().GetSwitchInterface(), + SWITCH_CALL_DIRECTION_INBOUND, params->flags, params->pool)) == NULL) { + PTRACE(1, "mod_opal\tCannot create session for outgoing call."); + return; + } } + else { + if ((m_fsSession = switch_core_session_request(endpoint.GetManager().GetSwitchInterface(), + SWITCH_CALL_DIRECTION_INBOUND, SOF_NONE, NULL)) == NULL) { + PTRACE(1, "mod_opal\tCannot create session for incoming call."); + return; + } + } + + if ((m_fsChannel = switch_core_session_get_channel(m_fsSession)) == NULL) { + switch_core_session_destroy(&m_fsSession); + return; + } + + switch_core_session_set_private(m_fsSession, this); + SafeReference(); // Make sure cannot be deleted until on_destroy() + + if (params != NULL) { + switch_caller_profile_t *caller_profile = switch_caller_profile_clone(m_fsSession, params->outbound_profile); + switch_channel_set_caller_profile(m_fsChannel, caller_profile); + SetLocalPartyName(caller_profile->caller_id_number); + SetDisplayName(caller_profile->caller_id_name); + + *params->new_session = m_fsSession; + } + + switch_channel_set_state(m_fsChannel, CS_INIT); +} + + +bool FSConnection::OnOutgoingSetUp() +{ + if (m_fsSession == NULL || m_fsChannel == NULL) { + PTRACE(1, "mod_opal\tSession request failed."); + return false; + } + + // Transfer FS caller_id_number & caller_id_name from the FSConnection + // to the protocol connection (e.g. H.323) so gets sent correctly + // in outgoing packets + PSafePtr proto = GetOtherPartyConnection(); + if (proto == NULL) { + PTRACE(1, "mod_opal\tNo protocol connection in call."); + return false; + } + + proto->SetLocalPartyName(GetLocalPartyName()); + proto->SetDisplayName(GetDisplayName()); + + switch_channel_set_name(m_fsChannel, ModuleName + '/' + GetRemotePartyURL()); + return true; } bool FSConnection::OnIncoming() { - if (m_fsSession == NULL) { + if (m_fsSession == NULL || m_fsChannel == NULL) { PTRACE(1, "mod_opal\tSession request failed."); return false; } switch_core_session_add_stream(m_fsSession, NULL); - switch_channel_t *channel = switch_core_session_get_channel(m_fsSession); - if (channel == NULL) { - PTRACE(1, "mod_opal\tSession does not have a channel"); - return false; - } - PURL url = GetRemotePartyURL(); - switch_caller_profile_t *caller_profile = switch_caller_profile_new(switch_core_session_get_pool(m_fsSession), - url.GetUserName(), - /** username */ - mod_opal_globals.dialplan, - /** dial plan */ - GetRemotePartyName(), - /** caller_id_name */ - GetRemotePartyNumber(), - /** caller_id_number */ - url.GetHostName(), - /** network addr */ - NULL, - /** ANI */ - NULL, - /** ANI II */ - NULL, - /** RDNIS */ - ModuleName, - /** source */ - mod_opal_globals.context, - /** set context */ - GetCalledPartyNumber() - /** destination_number */ - ); + switch_caller_profile_t *caller_profile = switch_caller_profile_new( + switch_core_session_get_pool(m_fsSession), + url.GetUserName(), /** username */ + m_endpoint.GetManager().GetDialPlan(), /** dial plan */ + GetRemotePartyName(), /** caller_id_name */ + GetRemotePartyNumber(), /** caller_id_number */ + url.GetHostName(), /** network addr */ + NULL, /** ANI */ + NULL, /** ANI II */ + NULL, /** RDNIS */ + ModuleName, /** source */ + m_endpoint.GetManager().GetContext(), /** set context */ + GetCalledPartyNumber() /** destination_number */ + ); if (caller_profile == NULL) { PTRACE(1, "mod_opal\tCould not create caller profile"); return false; } PTRACE(4, "mod_opal\tCreated switch caller profile:\n" - " username = " << caller_profile->username << "\n" - " dialplan = " << caller_profile->dialplan << "\n" - " caller_id_name = " << caller_profile->caller_id_name << "\n" - " caller_id_number = " << caller_profile->caller_id_number << "\n" - " network_addr = " << caller_profile->network_addr << "\n" - " source = " << caller_profile->source << "\n" - " context = " << caller_profile->context << "\n" " destination_number= " << caller_profile->destination_number); - switch_channel_set_caller_profile(channel, caller_profile); + " username = " << caller_profile->username << "\n" + " dialplan = " << caller_profile->dialplan << "\n" + " caller_id_name = " << caller_profile->caller_id_name << "\n" + " caller_id_number = " << caller_profile->caller_id_number << "\n" + " network_addr = " << caller_profile->network_addr << "\n" + " source = " << caller_profile->source << "\n" + " context = " << caller_profile->context << "\n" + " destination_number= " << caller_profile->destination_number); + switch_channel_set_caller_profile(m_fsChannel, caller_profile); - char name[256] = "opal/in:"; - switch_copy_string(name + 8, caller_profile->destination_number, sizeof(name)-8); - switch_channel_set_name(channel, name); - switch_channel_set_state(channel, CS_INIT); + switch_channel_set_name(m_fsChannel, ModuleName + '/' + url.GetScheme() + ':' + caller_profile->destination_number); if (switch_core_session_thread_launch(m_fsSession) != SWITCH_STATUS_SUCCESS) { PTRACE(1, "mod_opal\tCould not launch session thread"); + switch_core_session_destroy(&m_fsSession); + m_fsChannel = NULL; return false; } @@ -656,57 +604,44 @@ bool FSConnection::OnIncoming() void FSConnection::OnReleased() { - opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(m_fsSession); - - /* so FS on_hangup will not try to deref a landmine */ - tech_pvt->me = NULL; - m_rxAudioOpened.Signal(); // Just in case m_txAudioOpened.Signal(); - H225_ReleaseCompleteReason dummy; - switch_channel_hangup(switch_core_session_get_channel(m_fsSession), - (switch_call_cause_t)H323TranslateFromCallEndReason(GetCallEndReason(), dummy)); + + if (m_fsChannel == NULL) { + PTRACE(3, "mod_opal\tHanging up FS side"); + switch_channel_hangup(m_fsChannel, (switch_call_cause_t)callEndReason.q931); + } + OpalLocalConnection::OnReleased(); } -void FSConnection::OnAlerting() -{ - switch_channel_mark_ring_ready(m_fsChannel); - return OpalLocalConnection::OnAlerting(); -} - PBoolean FSConnection::SetAlerting(const PString & calleeName, PBoolean withMedia) { + if (PAssertNULL(m_fsChannel) == NULL) + return false; + + switch_channel_mark_ring_ready(m_fsChannel); return OpalLocalConnection::SetAlerting(calleeName, withMedia); } -void FSConnection::OnEstablished() -{ - OpalLocalConnection::OnEstablished(); -} - - PBoolean FSConnection::SendUserInputTone(char tone, unsigned duration) { + if (PAssertNULL(m_fsChannel) == NULL) + return false; + switch_dtmf_t dtmf = { tone, duration }; return switch_channel_queue_dtmf(m_fsChannel, &dtmf) == SWITCH_STATUS_SUCCESS; } -PBoolean FSConnection::SendUserInputString(const PString & value) -{ - return OpalConnection::SendUserInputString(value); -} - - OpalMediaFormatList FSConnection::GetMediaFormats() const { if (m_switchMediaFormats.IsEmpty()) { const_cast(this)->SetCodecs(); } - + return m_switchMediaFormats; } @@ -714,39 +649,52 @@ OpalMediaFormatList FSConnection::GetMediaFormats() const void FSConnection::SetCodecs() { int numCodecs = 0; - const switch_codec_implementation_t *codecs[SWITCH_MAX_CODECS]; - const char *codec_string = NULL, *abs, *ocodec; - char *tmp_codec_string = NULL; - char *codec_order[SWITCH_MAX_CODECS]; - int codec_order_last; + const switch_codec_implementation_t *codecs[SWITCH_MAX_CODECS]; - - if ((abs = switch_channel_get_variable(m_fsChannel, "absolute_codec_string"))) { - codec_string = abs; - } else { - if ((abs = switch_channel_get_variable(m_fsChannel, "codec_string"))) { - codec_string = abs; + PString codec_string = switch_channel_get_variable(m_fsChannel, "absolute_codec_string"); + if (codec_string.IsEmpty()) { + codec_string = switch_channel_get_variable(m_fsChannel, "codec_string"); + if (codec_string.IsEmpty()) { + codec_string = m_endpoint.GetManager().GetCodecPrefs(); + if (codec_string.IsEmpty()) { + numCodecs = switch_loadable_module_get_codecs(codecs, sizeof(codecs) / sizeof(codecs[0])); + for (int i = 0; i < numCodecs; i++) { + if (i > 0) + codec_string += ','; + codec_string += codecs[i]->iananame; + } + PTRACE(4, "mod_opal\tDefault to all loaded codecs=" << codec_string); + } + else { + PTRACE(4, "mod_opal\tSettings codec-prefs=" << codec_string); + } + } + else { + PTRACE(4, "mod_opal\tChannel codec_string=" << codec_string); } - if ((ocodec = switch_channel_get_variable(m_fsChannel, SWITCH_ORIGINATOR_CODEC_VARIABLE))) { - codec_string = switch_core_session_sprintf(m_fsSession, "%s,%s", ocodec, codec_string); + PString orig_codec = switch_channel_get_variable(m_fsChannel, SWITCH_ORIGINATOR_CODEC_VARIABLE); + if (!orig_codec.IsEmpty()) { + if (m_endpoint.GetManager().GetDisableTranscoding()) { + codec_string = orig_codec; + PTRACE(4, "mod_opal\tNo transcoding, forced to originator codec=" << orig_codec); + } + else { + codec_string.Splice(orig_codec+',', 0); + PTRACE(4, "mod_opal\tSetting preference to originator codec=" << orig_codec); + } } } - - if (!codec_string) { - codec_string = mod_opal_globals.codec_string; + else { + PTRACE(4, "mod_opal\tChannel absolute_codec_string=" << codec_string); } - if (codec_string) { - if ((tmp_codec_string = strdup(codec_string))) { - codec_order_last = switch_separate_string(tmp_codec_string, ',', codec_order, SWITCH_MAX_CODECS); - numCodecs = switch_loadable_module_get_codecs_sorted(codecs, SWITCH_MAX_CODECS, codec_order, codec_order_last); - - } - } else { - numCodecs = switch_loadable_module_get_codecs(codecs, sizeof(codecs) / sizeof(codecs[0])); + if (!codec_string.IsEmpty()) { + char *codec_order[SWITCH_MAX_CODECS]; + int codec_order_last = switch_separate_string((char *)codec_string.GetPointer(), ',', codec_order, SWITCH_MAX_CODECS); + numCodecs = switch_loadable_module_get_codecs_sorted(codecs, SWITCH_MAX_CODECS, codec_order, codec_order_last); } - + for (int i = 0; i < numCodecs; i++) { const switch_codec_implementation_t *codec = codecs[i]; @@ -758,16 +706,19 @@ void FSConnection::SetCodecs() // See if we have a match by name alone switchFormat = codec->iananame; if (!switchFormat.IsValid()) { - PTRACE(2, "mod_opal\tCould not match FS codec " << codec->iananame << " to OPAL media format."); + PTRACE(2, "mod_opal\tCould not match FS codec " + << codec->iananame << '@' << codec->samples_per_second + << " (pt=" << codec->ianacode << ")" + " to an OPAL media format."); continue; } } - + PTRACE(4, "mod_opal\tMatched FS codec " << codec->iananame << " to OPAL media format " << switchFormat); + +#if IMPLEMENT_MULTI_FAME_AUDIO // Did we match or create a new media format? if (switchFormat.IsValid() && codec->codec_type == SWITCH_CODEC_TYPE_AUDIO) { - PTRACE(2, "mod_opal\tMatched FS codec " << codec->iananame << " to OPAL media format " << switchFormat); - // Calculate frames per packet, do not use codec->codec_frames_per_packet as that field // has slightly different semantics when used in streamed codecs such as G.711 int fpp = codec->samples_per_packet/switchFormat.GetFrameTime(); @@ -778,7 +729,7 @@ void FSConnection::SetCodecs() could end up with 60ms and the codec cannot be created. The "holes" are unlikely in all but streamed codecs such as G.711, where it is theoretically possible for OPAL to come up with 32ms and there is only 30ms and 40ms in the FS table. We deem these - scenarios succifiently rare that we can safely ignore them ... for now. */ + scenarios sufficiently rare that we can safely ignore them ... for now. */ if (fpp > switchFormat.GetOptionInteger(OpalAudioFormat::RxFramesPerPacketOption())) { switchFormat.SetOptionInteger(OpalAudioFormat::RxFramesPerPacketOption(), fpp); @@ -788,11 +739,10 @@ void FSConnection::SetCodecs() switchFormat.SetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption(), fpp); } } +#endif // IMPLEMENT_MULTI_FAME_AUDIO m_switchMediaFormats += switchFormat; } - - switch_safe_free(tmp_codec_string); } @@ -802,110 +752,91 @@ OpalMediaStream *FSConnection::CreateMediaStream(const OpalMediaFormat & mediaFo } -PBoolean FSConnection::OnOpenMediaStream(OpalMediaStream & stream) +void FSConnection::OnPatchMediaStream(PBoolean isSource, OpalMediaPatch & patch) { - if (!OpalConnection::OnOpenMediaStream(stream)) { - return false; - } + OpalConnection::OnPatchMediaStream(isSource, patch); - if (stream.GetMediaFormat().GetMediaType() != OpalMediaType::Audio()) { - return true; - } + if (PAssertNULL(m_fsChannel) == NULL) + return; - if (stream.IsSource()) { - m_rxAudioOpened.Signal(); - } else { - m_txAudioOpened.Signal(); - } + if (patch.GetSource().GetMediaFormat().GetMediaType() != OpalMediaType::Audio()) + return; - if (GetMediaStream(stream.GetSessionID(), stream.IsSink()) != NULL) { + if (switch_channel_direction(m_fsChannel) == SWITCH_CALL_DIRECTION_INBOUND) { + if (isSource) + m_rxAudioOpened.Signal(); + else + m_txAudioOpened.Signal(); + } + else if (GetMediaStream(OpalMediaType::Audio(), !isSource) != NULL) { // Have open media in both directions. - if (GetPhase() == AlertingPhase) { - switch_channel_mark_pre_answered(m_fsChannel); - } else if (GetPhase() < ReleasingPhase) { + if (IsEstablished()) switch_channel_mark_answered(m_fsChannel); - } + else if (!IsReleased()) + switch_channel_mark_pre_answered(m_fsChannel); } - - return true; } switch_status_t FSConnection::on_init() { - switch_channel_t *channel = switch_core_session_get_channel(m_fsSession); - if (channel == NULL) { + if (PAssertNULL(m_fsChannel) == NULL) return SWITCH_STATUS_FALSE; - } - PTRACE(3, "mod_opal\tStarted routing for connection " << *this); - switch_channel_set_state(channel, CS_ROUTING); + PTRACE(4, "mod_opal\tStarted routing for connection " << *this); + switch_channel_set_state(m_fsChannel, CS_ROUTING); return SWITCH_STATUS_SUCCESS; } switch_status_t FSConnection::on_routing() { - PTRACE(3, "mod_opal\tRouting connection " << *this); + if (PAssertNULL(m_fsChannel) == NULL) + return SWITCH_STATUS_FALSE; + + PTRACE(4, "mod_opal\tRouting connection " << *this); return SWITCH_STATUS_SUCCESS; } switch_status_t FSConnection::on_execute() { - PTRACE(3, "mod_opal\tExecuting connection " << *this); + if (PAssertNULL(m_fsChannel) == NULL) + return SWITCH_STATUS_FALSE; + + PTRACE(4, "mod_opal\tExecuting connection " << *this); return SWITCH_STATUS_SUCCESS; } -static switch_status_t on_destroy(switch_core_session_t *session) + +switch_status_t FSConnection::on_destroy() { - //switch_channel_t *channel = switch_core_session_get_channel(session); - opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(session); - - if (tech_pvt) { - if (tech_pvt->read_codec.implementation) { - switch_core_codec_destroy(&tech_pvt->read_codec); - } + PTRACE(3, "mod_opal\tFS on_destroy for connection " << *this); - if (tech_pvt->write_codec.implementation) { - switch_core_codec_destroy(&tech_pvt->write_codec); - } + m_fsChannel = NULL; // Will be destoyed by FS, so don't use it any more. - if (tech_pvt->vid_read_codec.implementation) { - switch_core_codec_destroy(&tech_pvt->vid_read_codec); - } + switch_core_codec_destroy(&m_read_codec); + switch_core_codec_destroy(&m_write_codec); + switch_core_codec_destroy(&m_vid_read_codec); + switch_core_codec_destroy(&m_vid_write_codec); + switch_core_timer_destroy(&m_read_timer); + switch_core_timer_destroy(&m_vid_read_timer); - if (tech_pvt->vid_write_codec.implementation) { - switch_core_codec_destroy(&tech_pvt->vid_write_codec); - } - - if (tech_pvt->read_timer.timer_interface) { - switch_core_timer_destroy(&tech_pvt->read_timer); - } - - if (tech_pvt->vid_read_timer.timer_interface) { - switch_core_timer_destroy(&tech_pvt->vid_read_timer); - } - } + switch_core_session_set_private(m_fsSession, NULL); + SafeDereference(); return SWITCH_STATUS_SUCCESS; } -/* this function has to be called with the original session beause the FSConnection might already be destroyed and we - will can't have it be a method of a dead object - */ -static switch_status_t on_hangup(switch_core_session_t *session) + +switch_status_t FSConnection::on_hangup() { - switch_channel_t *channel = switch_core_session_get_channel(session); - opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(session); - + if (PAssertNULL(m_fsChannel) == NULL) + return SWITCH_STATUS_FALSE; + /* if this is still here it was our idea to hangup not opal's */ - if (tech_pvt->me) { - Q931::CauseValues cause = (Q931::CauseValues)switch_channel_get_cause_q850(channel); - tech_pvt->me->SetQ931Cause(cause); - tech_pvt->me->ClearCallSynchronous(NULL, H323TranslateToCallEndReason(cause, UINT_MAX)); - tech_pvt->me = NULL; - } + ClearCallSynchronous(NULL, H323TranslateToCallEndReason( + (Q931::CauseValues)switch_channel_get_cause_q850(m_fsChannel), UINT_MAX)); return SWITCH_STATUS_SUCCESS; } @@ -913,30 +844,30 @@ static switch_status_t on_hangup(switch_core_session_t *session) switch_status_t FSConnection::on_exchange_media() { - PTRACE(3, "mod_opal\tLoopback on connection " << *this); + PTRACE(4, "mod_opal\tExchanging media on connection " << *this); return SWITCH_STATUS_SUCCESS; } switch_status_t FSConnection::on_soft_execute() { - PTRACE(3, "mod_opal\tTransmit on connection " << *this); + PTRACE(4, "mod_opal\tTransmit on connection " << *this); return SWITCH_STATUS_SUCCESS; } switch_status_t FSConnection::kill_channel(int sig) { - PTRACE(3, "mod_opal\tKill " << sig << " on connection " << *this); - switch (sig) { - case SWITCH_SIG_BREAK: - break; case SWITCH_SIG_KILL: m_rxAudioOpened.Signal(); m_txAudioOpened.Signal(); + PTRACE(4, "mod_opal\tSignal channel KILL on connection " << *this); break; + case SWITCH_SIG_XFER: + case SWITCH_SIG_BREAK: default: + PTRACE(4, "mod_opal\tSignal channel " << sig << " on connection " << *this); break; } @@ -953,41 +884,16 @@ switch_status_t FSConnection::send_dtmf(const switch_dtmf_t *dtmf) switch_status_t FSConnection::receive_message(switch_core_session_message_t *msg) { - switch_channel_t *channel = switch_core_session_get_channel(m_fsSession); + if (PAssertNULL(m_fsChannel) == NULL) + return SWITCH_STATUS_FALSE; - - /* - SWITCH_MESSAGE_INDICATE_PROGRESS: establish early media now and return SWITCH_STATUS_FALSE if you can't - SWITCH_MESSAGE_INDICATE_ANSWER: answer and set up media now if it's not already and return SWITCH_STATUS_FALSE if you can't - - Neither message means anything on an outbound call.... - - It would only happen if someone called switch_channel_answer() instead of switch_channel_mark_answered() on an outbound call. - it should not do anything if someone does it by accident somewhere hense this in both cases: - - if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { - return SWITCH_STATUS_FALSE; - } - - - When we get these messages the core will trust that you have triggered FSMediaStream::Open and are ready for media if we do not - have media we MUST return SWITCH_STATUS_FALSE or it will cause a CRASH. - - - - */ switch (msg->message_id) { - case SWITCH_MESSAGE_INDICATE_BRIDGE: - case SWITCH_MESSAGE_INDICATE_UNBRIDGE: - case SWITCH_MESSAGE_INDICATE_AUDIO_SYNC: - switch_channel_set_private_flag(channel, CF_NEED_FLUSH); - break; - case SWITCH_MESSAGE_INDICATE_RINGING: case SWITCH_MESSAGE_INDICATE_PROGRESS: case SWITCH_MESSAGE_INDICATE_ANSWER: - { - switch_caller_profile_t * profile = switch_channel_get_caller_profile(channel); + case SWITCH_MESSAGE_INDICATE_DEFLECT: + if (switch_channel_direction(m_fsChannel) == SWITCH_CALL_DIRECTION_INBOUND) { + switch_caller_profile_t * profile = switch_channel_get_caller_profile(m_fsChannel); if (profile != NULL && profile->caller_extension != NULL) { PSafePtr other = GetOtherPartyConnection(); @@ -999,6 +905,9 @@ switch_status_t FSConnection::receive_message(switch_core_session_message_t *msg SetDisplayName(profile->caller_extension->extension_name); } } + else { + return SWITCH_STATUS_FALSE; + } break; default: @@ -1006,89 +915,78 @@ switch_status_t FSConnection::receive_message(switch_core_session_message_t *msg } switch (msg->message_id) { - case SWITCH_MESSAGE_INDICATE_RINGING: - SetPhase(OpalConnection::AlertingPhase); - OnAlerting(); + case SWITCH_MESSAGE_INDICATE_BRIDGE: + case SWITCH_MESSAGE_INDICATE_UNBRIDGE: + case SWITCH_MESSAGE_INDICATE_AUDIO_SYNC: + m_flushAudio = true; break; - case SWITCH_MESSAGE_INDICATE_DEFLECT: - { - PSafePtr other = GetOtherPartyConnection(); - if (other != NULL) - other->TransferConnection(msg->string_arg); + case SWITCH_MESSAGE_INDICATE_RINGING: + AlertingIncoming(); break; - } case SWITCH_MESSAGE_INDICATE_PROGRESS: - case SWITCH_MESSAGE_INDICATE_ANSWER: - { - int fixed = 0; - - if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { - return SWITCH_STATUS_FALSE; - } + AutoStartMediaStreams(); + AlertingIncoming(); - if (msg->message_id == SWITCH_MESSAGE_INDICATE_PROGRESS) { - if (fixed) { - /* this should send alerting + media and wait for it to be established and return SUCCESS or FAIL - depending on if media was able to be established. Need code to tell the other side we want early media here. - */ - GetCall().OpenSourceMediaStreams(*this, OpalMediaType::Audio()); - SetPhase(OpalConnection::AlertingPhase); - /* how do i say please establish early media ? */ - OnAlerting(); - } else { - /* hack to avoid getting stuck, pre_answer will imply answer */ - OnConnectedInternal(); - } - } else { - OnConnectedInternal(); - } - - // Wait for media - PTRACE(2, "mod_opal\tAwaiting media start on connection " << *this); - m_rxAudioOpened.Wait(); - m_txAudioOpened.Wait(); - - if (GetPhase() >= ReleasingPhase) { - // Call got aborted - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(m_fsSession), SWITCH_LOG_ERROR, "Call abandoned!\n"); - return SWITCH_STATUS_FALSE; - } - - PTRACE(4, "mod_opal\tMedia started on connection " << *this); - - if (msg->message_id == SWITCH_MESSAGE_INDICATE_PROGRESS) { - if (!switch_channel_test_flag(m_fsChannel, CF_EARLY_MEDIA)) { - switch_channel_mark_pre_answered(m_fsChannel); - } - } else { - if (!switch_channel_test_flag(m_fsChannel, CF_EARLY_MEDIA)) { - switch_channel_mark_answered(m_fsChannel); - } - } + if (!WaitForMedia()) + return SWITCH_STATUS_FALSE; + if (!switch_channel_test_flag(m_fsChannel, CF_EARLY_MEDIA)) { + switch_channel_mark_pre_answered(m_fsChannel); } break; + case SWITCH_MESSAGE_INDICATE_ANSWER: + AcceptIncoming(); + + if (!WaitForMedia()) + return SWITCH_STATUS_FALSE; + + if (!switch_channel_test_flag(m_fsChannel, CF_ANSWERED)) { + switch_channel_mark_answered(m_fsChannel); + } + break; + + case SWITCH_MESSAGE_INDICATE_DEFLECT: + ownerCall.Transfer(msg->string_arg, GetOtherPartyConnection()); + break; + default: - PTRACE(3, "mod_opal\tReceived message " << msg->message_id << " on connection " << *this); + PTRACE(3, "mod_opal\tReceived unhandled message " << msg->message_id << " on connection " << *this); } return SWITCH_STATUS_SUCCESS; } +bool FSConnection::WaitForMedia() +{ + PTRACE(4, "mod_opal\tAwaiting media start on connection " << *this); + m_rxAudioOpened.Wait(); + m_txAudioOpened.Wait(); + + if (IsReleased()) { + // Call got aborted + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(m_fsSession), SWITCH_LOG_ERROR, "Call abandoned!\n"); + return false; + } + + PTRACE(3, "mod_opal\tMedia started on connection " << *this); + return true; +} + + switch_status_t FSConnection::receive_event(switch_event_t *event) { - PTRACE(3, "mod_opal\tReceived event " << event->event_id << " on connection " << *this); + PTRACE(4, "mod_opal\tReceived event " << event->event_id << " on connection " << *this); return SWITCH_STATUS_SUCCESS; } switch_status_t FSConnection::state_change() { - PTRACE(3, "mod_opal\tState changed on connection " << *this); + PTRACE(4, "mod_opal\tState changed on connection " << *this); return SWITCH_STATUS_SUCCESS; } @@ -1119,14 +1017,14 @@ switch_status_t FSConnection::write_video_frame(switch_frame_t *frame, switch_io switch_status_t FSConnection::read_frame(const OpalMediaType & mediaType, switch_frame_t **frame, switch_io_flag_t flags) { - PSafePtr < FSMediaStream > stream = PSafePtrCast < OpalMediaStream, FSMediaStream > (GetMediaStream(mediaType, false)); + PSafePtr stream = PSafePtrCast (GetMediaStream(mediaType, false)); return stream != NULL ? stream->read_frame(frame, flags) : SWITCH_STATUS_FALSE; } switch_status_t FSConnection::write_frame(const OpalMediaType & mediaType, const switch_frame_t *frame, switch_io_flag_t flags) { - PSafePtr < FSMediaStream > stream = PSafePtrCast < OpalMediaStream, FSMediaStream > (GetMediaStream(mediaType, true)); + PSafePtr stream = PSafePtrCast(GetMediaStream(mediaType, true)); return stream != NULL ? stream->write_frame(frame, flags) : SWITCH_STATUS_FALSE; } @@ -1135,113 +1033,103 @@ switch_status_t FSConnection::write_frame(const OpalMediaType & mediaType, const FSMediaStream::FSMediaStream(FSConnection & conn, const OpalMediaFormat & mediaFormat, unsigned sessionID, bool isSource) : OpalMediaStream(conn, mediaFormat, sessionID, isSource) - , m_fsSession(conn.GetSession()) - , m_readRTP(0, 512) - , m_callOnStart(true) + , m_connection(conn) + , m_readRTP(0, SWITCH_RECOMMENDED_BUFFER_SIZE) { memset(&m_readFrame, 0, sizeof(m_readFrame)); - m_readFrame.codec = m_switchCodec; - m_readFrame.flags = SFF_RAW_RTP; } PBoolean FSMediaStream::Open() { - opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(m_fsSession); - if (IsOpen()) { return true; } + switch_core_session_t *fsSession = m_connection.GetSession(); + switch_channel_t *fsChannel = m_connection.GetChannel(); + if (PAssertNULL(fsSession) == NULL || PAssertNULL(fsChannel) == NULL) + return false; + bool isAudio; if (mediaFormat.GetMediaType() == OpalMediaType::Audio()) { isAudio = true; } else if (mediaFormat.GetMediaType() == OpalMediaType::Video()) { isAudio = false; } else { - return OpalMediaStream::Open(); + return false; } - m_fsChannel = switch_core_session_get_channel(m_fsSession); - int ptime = mediaFormat.GetOptionInteger(OpalAudioFormat::TxFramesPerPacketOption()) * mediaFormat.GetFrameTime() / mediaFormat.GetTimeUnits(); - if (IsSink()) { - m_switchCodec = isAudio ? &tech_pvt->read_codec : &tech_pvt->vid_read_codec; - m_switchTimer = isAudio ? &tech_pvt->read_timer : &tech_pvt->vid_read_timer; + m_switchCodec = isAudio ? &m_connection.m_read_codec : &m_connection.m_vid_read_codec; + m_switchTimer = isAudio ? &m_connection.m_read_timer : &m_connection.m_vid_read_timer; + m_readFrame.codec = m_switchCodec; + m_readFrame.rate = mediaFormat.GetClockRate(); } else { - m_switchCodec = isAudio ? &tech_pvt->write_codec : &tech_pvt->vid_write_codec; + m_switchCodec = isAudio ? &m_connection.m_write_codec : &m_connection.m_vid_write_codec; } // The following is performed on two different instances of this object. if (switch_core_codec_init(m_switchCodec, mediaFormat.GetEncodingName(), NULL, // FMTP mediaFormat.GetClockRate(), ptime, 1, // Channels SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, // Settings - switch_core_session_get_pool(m_fsSession)) != SWITCH_STATUS_SUCCESS) { + switch_core_session_get_pool(fsSession)) != SWITCH_STATUS_SUCCESS) { // Could not select a codecs using negotiated frames/packet, so try using default. if (switch_core_codec_init(m_switchCodec, mediaFormat.GetEncodingName(), NULL, // FMTP mediaFormat.GetClockRate(), 0, 1, // Channels SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, // Settings - switch_core_session_get_pool(m_fsSession)) != SWITCH_STATUS_SUCCESS) { - PTRACE(1, "mod_opal " << switch_channel_get_name(m_fsChannel)<< " Cannot initialise " << (IsSink()? "read" : "write") << ' ' + switch_core_session_get_pool(fsSession)) != SWITCH_STATUS_SUCCESS) { + PTRACE(1, "mod_opal\t" << switch_channel_get_name(fsChannel) + << " cannot initialise " << (IsSink()? "read" : "write") << ' ' << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this); - switch_channel_hangup(m_fsChannel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION); + switch_channel_hangup(fsChannel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION); return false; } - PTRACE(2, "mod_opal " << switch_channel_get_name(m_fsChannel)<< " Unsupported ptime of " << ptime << " on " << (IsSink()? "read" : "write") << ' ' + PTRACE(2, "mod_opal\t" << switch_channel_get_name(fsChannel) + << " unsupported ptime of " << ptime << " on " << (IsSink()? "read" : "write") << ' ' << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this); } - PTRACE(1, "mod_opal " << switch_channel_get_name(m_fsChannel)<< " initialise " << - switch_channel_get_name(m_fsChannel) << (IsSink()? "read" : "write") << ' ' - << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this); - if (IsSink()) { - m_readFrame.rate = mediaFormat.GetClockRate(); - if (isAudio) { - switch_core_session_set_read_codec(m_fsSession, m_switchCodec); + switch_core_session_set_read_codec(fsSession, m_switchCodec); if (switch_core_timer_init(m_switchTimer, "soft", m_switchCodec->implementation->microseconds_per_packet / 1000, m_switchCodec->implementation->samples_per_packet, - switch_core_session_get_pool(m_fsSession)) != SWITCH_STATUS_SUCCESS) { + switch_core_session_get_pool(fsSession)) != SWITCH_STATUS_SUCCESS) { + PTRACE(1, "mod_opal\t" << switch_channel_get_name(fsChannel) + << " timer init failed on " << (IsSink()? "read" : "write") << ' ' + << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this); switch_core_codec_destroy(m_switchCodec); m_switchCodec = NULL; return false; } } else { - switch_core_session_set_video_read_codec(m_fsSession, m_switchCodec); - switch_channel_set_flag(m_fsChannel, CF_VIDEO); + switch_core_session_set_video_read_codec(fsSession, m_switchCodec); + switch_channel_set_flag(fsChannel, CF_VIDEO); } } else { if (isAudio) { - switch_core_session_set_write_codec(m_fsSession, m_switchCodec); + switch_core_session_set_write_codec(fsSession, m_switchCodec); } else { - switch_core_session_set_video_write_codec(m_fsSession, m_switchCodec); - switch_channel_set_flag(m_fsChannel, CF_VIDEO); + switch_core_session_set_video_write_codec(fsSession, m_switchCodec); + switch_channel_set_flag(fsChannel, CF_VIDEO); } } - PTRACE(3, "mod_opal\tSet " << (IsSink()? "read" : "write") << ' ' - << mediaFormat.GetMediaType() << " codec to << " << mediaFormat << " for connection " << *this); + PTRACE(3, "mod_opal\t" << switch_channel_get_name(fsChannel) + << " initialised " << (IsSink()? "read" : "write") << ' ' + << mediaFormat.GetMediaType() << " codec " << mediaFormat << " for connection " << *this); return OpalMediaStream::Open(); } -PBoolean FSMediaStream::Close() +void FSMediaStream::InternalClose() { - if (!IsOpen()) - return false; - - /* forget these FS will properly destroy them for us */ - - m_switchTimer = NULL; - m_switchCodec = NULL; - - return OpalMediaStream::Close(); } @@ -1256,149 +1144,89 @@ PBoolean FSMediaStream::RequiresPatchThread(OpalMediaStream *) const return false; } -bool FSMediaStream::CheckPatchAndLock() -{ - if (GetConnection().GetPhase() >= GetConnection().ReleasingPhase || !IsOpen()) - return false; - if (LockReadWrite()) { - if (!GetPatch() || !IsOpen()) { - UnlockReadWrite(); - return false; - } - return true; - } else { - return false; +int FSMediaStream::StartReadWrite(PatchPtr & mediaPatch) const +{ + if (!IsOpen()) { + PTRACE(2, "mod_opal\tNot open!"); + return -1; } + + if (!m_switchCodec) { + PTRACE(2, "mod_opal\tNo codec!"); + return -1; + } + + if (!m_connection.IsChannelReady()) { + PTRACE(2, "mod_opal\tChannel not ready!"); + return -1; + } + + // We make referenced copy of pointer so can't be deleted out from under us + mediaPatch = m_mediaPatch; + if (mediaPatch == NULL) { + /*There is a race here... sometimes we make it here and m_mediaPatch is NULL + if we wait it shows up in 1ms, maybe there is a better way to wait. */ + PTRACE(3, "mod_opal\tPatch not ready!"); + return 1; + } + + return 0; } + switch_status_t FSMediaStream::read_frame(switch_frame_t **frame, switch_io_flag_t flags) { - - if (!m_switchCodec) { + PatchPtr mediaPatch; + switch (StartReadWrite(mediaPatch)) { + case -1 : return SWITCH_STATUS_FALSE; + case 1 : + return SWITCH_STATUS_SUCCESS; } - if (m_callOnStart) { - /* - There is a race here... sometimes we make it here and GetPatch() is NULL - if we wait it shows up in 1ms, maybe there is a better way to wait. - - */ - while(!GetPatch()) { - if (!m_fsChannel || !switch_channel_up(m_fsChannel)) { - return SWITCH_STATUS_FALSE; - } - switch_cond_next(); - } - if (CheckPatchAndLock()) { - GetPatch()->OnStartMediaPatch(); - m_callOnStart = false; - UnlockReadWrite(); - } else { - return SWITCH_STATUS_FALSE; - } - } - - m_readFrame.flags = 0; - - /* - while (switch_channel_ready(m_fsChannel)) { - if (CheckPatchAndLock()) { - if (!GetPatch()->GetSource().ReadPacket(m_readRTP)) { - UnlockReadWrite(); - return SWITCH_STATUS_FALSE; - } - UnlockReadWrite(); - } else { - return SWITCH_STATUS_FALSE; - } - - if ((m_readFrame.datalen = m_readRTP.GetPayloadSize()) || switch_core_timer_check(&m_switchTimer, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) { - if (m_readFrame.datalen) { - } else { - m_readFrame.flags = SFF_CNG; - } - break; - } - - switch_yield(1000); - } - */ - - if (switch_channel_test_private_flag(m_fsChannel, CF_NEED_FLUSH)) { - switch_channel_clear_private_flag(m_fsChannel, CF_NEED_FLUSH); - for(;;) { - if (CheckPatchAndLock()) { - if (!GetPatch()->GetSource().ReadPacket(m_readRTP)) { - UnlockReadWrite(); - return SWITCH_STATUS_FALSE; - } - UnlockReadWrite(); - } else { - return SWITCH_STATUS_FALSE; - } - - if (!m_readRTP.GetPayloadSize()) { - m_readFrame.flags = SFF_CNG; - break; - } - } + if (m_connection.NeedFlushAudio()) { + mediaPatch->GetSource().EnableJitterBuffer(); // This flushes data and resets jitter buffer + m_readRTP.SetPayloadSize(0); } else { + m_readRTP.SetTimestamp(m_readFrame.timestamp + m_switchCodec->implementation->samples_per_packet); - if (CheckPatchAndLock()) { - if (!m_switchTimer || !GetPatch()->GetSource().ReadPacket(m_readRTP)) { - UnlockReadWrite(); - return SWITCH_STATUS_FALSE; - } - UnlockReadWrite(); - } else { - return SWITCH_STATUS_FALSE; + if (!mediaPatch->GetSource().ReadPacket(m_readRTP)) { + return SWITCH_STATUS_FALSE; } - + } + + if (m_switchTimer != NULL) { switch_core_timer_next(m_switchTimer); - - if (!(m_readFrame.datalen = m_readRTP.GetPayloadSize())) { - m_readFrame.flags = SFF_CNG; + } + + if (m_switchCodec != NULL) { + if (!switch_core_codec_ready(m_switchCodec)) { + PTRACE(2, "mod_opal\tread_frame: codec not ready!"); + return SWITCH_STATUS_FALSE; } } - if (!switch_channel_ready(m_fsChannel)) { - return SWITCH_STATUS_FALSE; - } + m_readFrame.packet = m_readRTP.GetPointer(); + m_readFrame.packetlen = m_readRTP.GetHeaderSize() + m_readFrame.datalen; - if (!switch_core_codec_ready(m_switchCodec)) { - return SWITCH_STATUS_FALSE; - } +#if IMPLEMENT_MULTI_FAME_AUDIO + // Repackage frames in incoming packet to agree with what FS expects. + // Not implmented yet!!!!!!!!! + // Cheating and only supporting one frame per packet +#endif - //switch_core_timer_step(&m_switchTimer); - - if (m_readFrame.payload == RTP_DataFrame::CN || m_readFrame.payload == RTP_DataFrame::Cisco_CN) { - m_readFrame.flags = SFF_CNG; - } - - if (m_readFrame.flags & SFF_CNG) { - m_readFrame.buflen = sizeof(m_buf); - m_readFrame.data = m_buf; - m_readFrame.packet = NULL; - m_readFrame.packetlen = 0; - m_readFrame.timestamp = 0; - m_readFrame.m = SWITCH_FALSE; - m_readFrame.seq = 0; - m_readFrame.ssrc = 0; - m_readFrame.codec = m_switchCodec; - } else { - m_readFrame.buflen = m_readRTP.GetSize(); - m_readFrame.data = m_readRTP.GetPayloadPtr(); - m_readFrame.packet = m_readRTP.GetPointer(); - m_readFrame.packetlen = m_readRTP.GetHeaderSize() + m_readFrame.datalen; - m_readFrame.payload = (switch_payload_t) m_readRTP.GetPayloadType(); - m_readFrame.timestamp = m_readRTP.GetTimestamp(); - m_readFrame.m = (switch_bool_t) m_readRTP.GetMarker(); - m_readFrame.seq = m_readRTP.GetSequenceNumber(); - m_readFrame.ssrc = m_readRTP.GetSyncSource(); - m_readFrame.codec = m_switchCodec; - } + m_readFrame.buflen = m_readRTP.GetSize(); + m_readFrame.data = m_readRTP.GetPayloadPtr(); + m_readFrame.datalen = m_readRTP.GetPayloadSize(); + m_readFrame.timestamp = m_readRTP.GetTimestamp(); + m_readFrame.seq = m_readRTP.GetSequenceNumber(); + m_readFrame.ssrc = m_readRTP.GetSyncSource(); + m_readFrame.m = m_readRTP.GetMarker() ? SWITCH_TRUE : SWITCH_FALSE; + m_readFrame.payload = (switch_payload_t)m_readRTP.GetPayloadType(); + m_readFrame.flags = m_readFrame.datalen == 0 || + m_readFrame.payload == RTP_DataFrame::CN || + m_readFrame.payload == RTP_DataFrame::Cisco_CN ? SFF_CNG : 0; *frame = &m_readFrame; @@ -1408,69 +1236,35 @@ switch_status_t FSMediaStream::read_frame(switch_frame_t **frame, switch_io_flag switch_status_t FSMediaStream::write_frame(const switch_frame_t *frame, switch_io_flag_t flags) { - if (!switch_channel_ready(m_fsChannel)) { + PatchPtr mediaPatch; + switch (StartReadWrite(mediaPatch)) { + case -1 : return SWITCH_STATUS_FALSE; - } - - if (m_callOnStart) { - if (CheckPatchAndLock()) { - GetPatch()->OnStartMediaPatch(); - m_callOnStart = false; - UnlockReadWrite(); - } else { - return SWITCH_STATUS_FALSE; - } - } - - if ((frame->flags & SFF_CNG)) { + case 1 : return SWITCH_STATUS_SUCCESS; } if ((frame->flags & SFF_RAW_RTP) != 0) { - RTP_DataFrame rtp((const BYTE *) frame->packet, frame->packetlen, false); - - if (CheckPatchAndLock()) { - if (GetPatch()->PushFrame(rtp)) { - UnlockReadWrite(); - return SWITCH_STATUS_SUCCESS; - } - UnlockReadWrite(); - } else { - return SWITCH_STATUS_FALSE; - } - } - - /* If we reach this code it means a call to an ivr or something else that does not generate timestamps - Its possible that frame->timestamp is set but not guarenteed and is best ignored for the time being. - We are probably relying on the rtp stack to generate the timestamp and ssrc for us at this point. - As a quick hack I am going to keep a sample counter and increment it by frame->samples but it would be - better if we could engage whatever it is in opal that makes it generate the timestamp. - */ - - RTP_DataFrame rtp(frame->datalen); - rtp.SetPayloadType(mediaFormat.GetPayloadType()); - - m_timeStamp += frame->samples; - rtp.SetTimestamp(m_timeStamp); - - //rtp.SetTimestamp(frame->timestamp); - //rtp.SetSyncSource(frame->ssrc); - //rtp.SetMarker(frame->m); - - memcpy(rtp.GetPayloadPtr(), frame->data, frame->datalen); - - if (CheckPatchAndLock()) { - if (GetPatch()->PushFrame(rtp)) { - UnlockReadWrite(); - return SWITCH_STATUS_SUCCESS; - } - UnlockReadWrite(); - } else { - return SWITCH_STATUS_FALSE; + RTP_DataFrame rtp((const BYTE *)frame->packet, frame->packetlen, false); + return mediaPatch->PushFrame(rtp) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; } + RTP_DataFrame rtp(frame->datalen); + memcpy(rtp.GetPayloadPtr(), frame->data, frame->datalen); - return SWITCH_STATUS_FALSE; + rtp.SetPayloadType(mediaFormat.GetPayloadType()); + + /* Not sure what FS is going to give us! + Suspect it depends on the mod on the other side sending it. */ + if (frame->timestamp != 0) + timestamp = frame->timestamp; + else if (frame->samples != 0) + timestamp += frame->samples; + else + timestamp += m_switchCodec->implementation->samples_per_packet; + rtp.SetTimestamp(timestamp); + + return mediaPatch->PushFrame(rtp) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; } diff --git a/src/mod/endpoints/mod_opal/mod_opal.h b/src/mod/endpoints/mod_opal/mod_opal.h index 1915d54b90..6a4590f654 100644 --- a/src/mod/endpoints/mod_opal/mod_opal.h +++ b/src/mod/endpoints/mod_opal/mod_opal.h @@ -4,6 +4,7 @@ * Version: MPL 1.1 * * Copyright (c) 2007 Tuyan Ozipek (tuyanozipek@gmail.com) + * Copyright (c) 2008-2012 Vox Lucida Pty. Ltd. (robertj@voxlucida.com.au) * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with @@ -43,9 +44,32 @@ #undef strcasecmp #undef strncasecmp + +#if _MSC_VER < 1600 +/*The following insanity is because libteletone_generate.h defines int8_t in + a slightly different manner to most other cases (SDL, PCAP, Java V8, stdint.h + etc) and does not provide a mechanism to prevent it's inclusion. Then, to + cap it off, VS2008 barfs on the difference. VS2010 seems OK with it. + + Sigh. + */ +#pragma include_alias(, <../../libs/libteletone/src/libteletone.h>) +#pragma include_alias(, <../../libs/libteletone/src/libteletone_generate.h>) +#pragma include_alias(, <../../libs/libteletone/src/libteletone_detect.h>) +#define int8_t signed int8_t +#include +#undef int8_t +#endif // End of insanity + + #define HAVE_APR +#define uint32_t uint32_t // Avoid conflict in stdint definitions #include +#undef uint32_t + #include + + #define MODNAME "mod_opal" @@ -53,206 +77,242 @@ class FSEndPoint; class FSManager; -struct mod_opal_globals { - int trace_level; - char *codec_string; - char *context; - char *dialplan; +class FSProcess : public PLibraryProcess +{ + PCLASSINFO(FSProcess, PLibraryProcess); + public: + FSProcess(); + ~FSProcess(); + + bool Initialise(switch_loadable_module_interface_t *iface); + + FSManager & GetManager() const + { + return *m_manager; + } + + protected: + FSManager * m_manager; }; -extern struct mod_opal_globals mod_opal_globals; + +struct FSListener +{ + FSListener() : m_port(H323EndPoint::DefaultTcpSignalPort) { } + + PString m_name; + PIPSocket::Address m_address; + uint16_t m_port; +}; -class FSProcess:public PLibraryProcess { - PCLASSINFO(FSProcess, PLibraryProcess); +class FSManager : public OpalManager +{ + PCLASSINFO(FSManager, OpalManager); public: - FSProcess(); - ~FSProcess(); + FSManager(); - bool Initialise(switch_loadable_module_interface_t *iface); + bool Initialise(switch_loadable_module_interface_t *iface); - FSManager & GetManager() const { - return *m_manager; - } protected: - FSManager * m_manager; -}; + switch_status_t ReadConfig(int reload); - -struct FSListener { - FSListener() { - } PString name; - OpalTransportAddress listenAddress; - PString localUserName; - PString gatekeeper; -}; - - -class FSCall:public OpalCall { - PCLASSINFO(FSCall, OpalCall); - public: - FSCall(OpalManager & manager); - virtual PBoolean OnSetUp(OpalConnection & connection); -}; - - -class FSManager:public OpalManager { - PCLASSINFO(FSManager, OpalManager); - - public: - FSManager(); - - bool Initialise(switch_loadable_module_interface_t *iface); - - switch_status_t ReadConfig(int reload); - - switch_endpoint_interface_t *GetSwitchInterface() const { - return m_FreeSwitch; - } virtual OpalCall *CreateCall(void *userData); + switch_endpoint_interface_t *GetSwitchInterface() const { return m_FreeSwitch; } + const PString & GetContext() const { return m_context; } + const PString & GetDialPlan() const { return m_dialplan; } + const PString & GetCodecPrefs() const { return m_codecPrefs; } + bool GetDisableTranscoding() const { return m_disableTranscoding; } private: - switch_endpoint_interface_t *m_FreeSwitch; + switch_endpoint_interface_t *m_FreeSwitch; - H323EndPoint *m_h323ep; - IAX2EndPoint *m_iaxep; - FSEndPoint *m_fsep; + H323EndPoint *m_h323ep; + IAX2EndPoint *m_iaxep; + FSEndPoint *m_fsep; - PString m_gkAddress; - PString m_gkIdentifer; - PString m_gkInterface; + PString m_context; + PString m_dialplan; + PString m_codecPrefs; + bool m_disableTranscoding; + PString m_gkAddress; + PString m_gkIdentifer; + PString m_gkInterface; - list < FSListener > m_listeners; + list m_listeners; +}; + + +class FSEndPoint : public OpalLocalEndPoint +{ + PCLASSINFO(FSEndPoint, OpalLocalEndPoint); + public: + FSEndPoint(FSManager & manager); + + virtual OpalLocalConnection *CreateConnection(OpalCall & call, void *userData, unsigned options, OpalConnection::StringOptions * stringOptions); + + FSManager & GetManager() const { return m_manager; } + + protected: + FSManager & m_manager; }; class FSConnection; -typedef struct { - switch_timer_t read_timer; - switch_codec_t read_codec; - switch_codec_t write_codec; - - switch_timer_t vid_read_timer; - switch_codec_t vid_read_codec; - switch_codec_t vid_write_codec; - FSConnection *me; -} opal_private_t; -class FSEndPoint:public OpalLocalEndPoint { - PCLASSINFO(FSEndPoint, OpalLocalEndPoint); +class FSMediaStream : public OpalMediaStream +{ + PCLASSINFO(FSMediaStream, OpalMediaStream); public: - FSEndPoint(FSManager & manager); + FSMediaStream( + FSConnection & conn, + const OpalMediaFormat & mediaFormat, ///< Media format for stream + unsigned sessionID, ///< Session number for stream + bool isSource ///< Is a source stream + ); - virtual bool OnIncomingCall(OpalLocalConnection &); - virtual OpalLocalConnection *CreateConnection(OpalCall & call, void *userData, unsigned options, OpalConnection::StringOptions * stringOptions); + virtual PBoolean Open(); + virtual PBoolean IsSynchronous() const; + virtual PBoolean RequiresPatchThread(OpalMediaStream *) const; + + switch_status_t read_frame(switch_frame_t **frame, switch_io_flag_t flags); + switch_status_t write_frame(const switch_frame_t *frame, switch_io_flag_t flags); + + protected: + virtual void InternalClose(); + int StartReadWrite(PatchPtr & mediaPatch) const; + + private: + bool CheckPatchAndLock(); + + FSConnection &m_connection; + switch_timer_t *m_switchTimer; + switch_codec_t *m_switchCodec; + switch_frame_t m_readFrame; + RTP_DataFrame m_readRTP; }; #define DECLARE_CALLBACK0(name) \ static switch_status_t name(switch_core_session_t *session) { \ - opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(session); \ - return tech_pvt && tech_pvt->me != NULL ? tech_pvt->me->name() : SWITCH_STATUS_FALSE; } \ -switch_status_t name() + FSConnection *tech_pvt = (FSConnection *) switch_core_session_get_private(session); \ + return tech_pvt != NULL ? tech_pvt->name() : SWITCH_STATUS_FALSE; } \ + switch_status_t name() #define DECLARE_CALLBACK1(name, type1, name1) \ static switch_status_t name(switch_core_session_t *session, type1 name1) { \ - opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(session); \ - return tech_pvt && tech_pvt->me != NULL ? tech_pvt->me->name(name1) : SWITCH_STATUS_FALSE; } \ -switch_status_t name(type1 name1) + FSConnection *tech_pvt = (FSConnection *) switch_core_session_get_private(session); \ + return tech_pvt != NULL ? tech_pvt->name(name1) : SWITCH_STATUS_FALSE; } \ + switch_status_t name(type1 name1) #define DECLARE_CALLBACK3(name, type1, name1, type2, name2, type3, name3) \ static switch_status_t name(switch_core_session_t *session, type1 name1, type2 name2, type3 name3) { \ - opal_private_t *tech_pvt = (opal_private_t *) switch_core_session_get_private(session); \ - return tech_pvt && tech_pvt->me != NULL ? tech_pvt->me->name(name1, name2, name3) : SWITCH_STATUS_FALSE; } \ -switch_status_t name(type1 name1, type2 name2, type3 name3) + FSConnection *tech_pvt = (FSConnection *) switch_core_session_get_private(session); \ + return tech_pvt != NULL ? tech_pvt->name(name1, name2, name3) : SWITCH_STATUS_FALSE; } \ + switch_status_t name(type1 name1, type2 name2, type3 name3) -class FSConnection:public OpalLocalConnection { - PCLASSINFO(FSConnection, OpalLocalConnection) +class FSConnection : public OpalLocalConnection +{ + PCLASSINFO(FSConnection, OpalLocalConnection) public: - FSConnection(OpalCall & call, - FSEndPoint & endpoint, - void *userData, - unsigned options, - OpalConnection::StringOptions * stringOptions, - switch_caller_profile_t *outbound_profile, switch_core_session_t *fsSession, switch_channel_t *fsChannel); + struct outgoing_params { + switch_event_t *var_event; + switch_caller_profile_t *outbound_profile; + switch_core_session_t **new_session; + switch_memory_pool_t **pool; + switch_originate_flag_t flags; + switch_call_cause_t *cancel_cause; + switch_call_cause_t fail_cause; + }; - virtual bool OnIncoming(); - virtual void OnReleased(); - virtual PBoolean SetAlerting(const PString & calleeName, PBoolean withMedia); - virtual void OnAlerting(); - virtual void OnEstablished(); - virtual OpalMediaStream *CreateMediaStream(const OpalMediaFormat &, unsigned, PBoolean); - virtual PBoolean OnOpenMediaStream(OpalMediaStream & stream); - virtual OpalMediaFormatList GetMediaFormats() const; - virtual PBoolean SendUserInputTone(char tone, unsigned duration); - virtual PBoolean SendUserInputString(const PString & value); + FSConnection(OpalCall & call, + FSEndPoint & endpoint, + unsigned options, + OpalConnection::StringOptions * stringOptions, + outgoing_params * params); - void SetCodecs(); + virtual bool OnOutgoingSetUp(); + virtual bool OnIncoming(); + virtual void OnReleased(); + virtual PBoolean SetAlerting(const PString & calleeName, PBoolean withMedia); + virtual OpalMediaStream *CreateMediaStream(const OpalMediaFormat &, unsigned, PBoolean); + virtual void OnPatchMediaStream(PBoolean isSource, OpalMediaPatch & patch); + virtual OpalMediaFormatList GetMediaFormats() const; + virtual PBoolean SendUserInputTone(char tone, unsigned duration); - DECLARE_CALLBACK0(on_init); - DECLARE_CALLBACK0(on_routing); - DECLARE_CALLBACK0(on_execute); + DECLARE_CALLBACK0(on_init); + DECLARE_CALLBACK0(on_destroy); + DECLARE_CALLBACK0(on_routing); + DECLARE_CALLBACK0(on_execute); + DECLARE_CALLBACK0(on_hangup); - DECLARE_CALLBACK0(on_exchange_media); - DECLARE_CALLBACK0(on_soft_execute); + DECLARE_CALLBACK0(on_exchange_media); + DECLARE_CALLBACK0(on_soft_execute); - DECLARE_CALLBACK1(kill_channel, int, sig); - DECLARE_CALLBACK1(send_dtmf, const switch_dtmf_t *, dtmf); - DECLARE_CALLBACK1(receive_message, switch_core_session_message_t *, msg); - DECLARE_CALLBACK1(receive_event, switch_event_t *, event); - DECLARE_CALLBACK0(state_change); - DECLARE_CALLBACK3(read_audio_frame, switch_frame_t **, frame, switch_io_flag_t, flags, int, stream_id); - DECLARE_CALLBACK3(write_audio_frame, switch_frame_t *, frame, switch_io_flag_t, flags, int, stream_id); - DECLARE_CALLBACK3(read_video_frame, switch_frame_t **, frame, switch_io_flag_t, flag, int, stream_id); - DECLARE_CALLBACK3(write_video_frame, switch_frame_t *, frame, switch_io_flag_t, flag, int, stream_id); + DECLARE_CALLBACK1(kill_channel, int, sig); + DECLARE_CALLBACK1(send_dtmf, const switch_dtmf_t *, dtmf); + DECLARE_CALLBACK1(receive_message, switch_core_session_message_t *, msg); + DECLARE_CALLBACK1(receive_event, switch_event_t *, event); + DECLARE_CALLBACK0(state_change); + DECLARE_CALLBACK3(read_audio_frame, switch_frame_t **, frame, switch_io_flag_t, flags, int, stream_id); + DECLARE_CALLBACK3(write_audio_frame, switch_frame_t *, frame, switch_io_flag_t, flags, int, stream_id); + DECLARE_CALLBACK3(read_video_frame, switch_frame_t **, frame, switch_io_flag_t, flag, int, stream_id); + DECLARE_CALLBACK3(write_video_frame, switch_frame_t *, frame, switch_io_flag_t, flag, int, stream_id); - switch_status_t read_frame(const OpalMediaType & mediaType, switch_frame_t **frame, switch_io_flag_t flags); - switch_status_t write_frame(const OpalMediaType & mediaType, const switch_frame_t *frame, switch_io_flag_t flags); + __inline switch_core_session_t *GetSession() const + { + return m_fsSession; + } - switch_core_session_t *GetSession() const { - return m_fsSession; - } private: - FSEndPoint & m_endpoint; - switch_core_session_t *m_fsSession; - switch_channel_t *m_fsChannel; - PSyncPoint m_rxAudioOpened; - PSyncPoint m_txAudioOpened; - OpalMediaFormatList m_switchMediaFormats; -}; + __inline switch_channel_t *GetChannel() const + { + return m_fsChannel; + } + bool IsChannelReady() const + { + return m_fsChannel != NULL && switch_channel_ready(m_fsChannel); + } -class FSMediaStream:public OpalMediaStream { - PCLASSINFO(FSMediaStream, OpalMediaStream); - public: - FSMediaStream(FSConnection & conn, const OpalMediaFormat & mediaFormat, ///< Media format for stream - unsigned sessionID, ///< Session number for stream - bool isSource ///< Is a source stream - ); + bool NeedFlushAudio() + { + if (!m_flushAudio) + return false; + m_flushAudio = false; + return true; + } - virtual PBoolean Open(); - virtual PBoolean Close(); - virtual PBoolean IsSynchronous() const; - virtual PBoolean RequiresPatchThread(OpalMediaStream *) const; + protected: + void SetCodecs(); + bool WaitForMedia(); - switch_status_t read_frame(switch_frame_t **frame, switch_io_flag_t flags); - switch_status_t write_frame(const switch_frame_t *frame, switch_io_flag_t flags); + switch_status_t read_frame(const OpalMediaType & mediaType, switch_frame_t **frame, switch_io_flag_t flags); + switch_status_t write_frame(const OpalMediaType & mediaType, const switch_frame_t *frame, switch_io_flag_t flags); private: - switch_core_session_t *m_fsSession; - switch_channel_t *m_fsChannel; - switch_timer_t *m_switchTimer; - switch_codec_t *m_switchCodec; - switch_frame_t m_readFrame; - unsigned char m_buf[SWITCH_RECOMMENDED_BUFFER_SIZE]; - RTP_DataFrame m_readRTP; - bool m_callOnStart; - uint32_t m_timeStamp; + FSEndPoint &m_endpoint; + switch_core_session_t *m_fsSession; + switch_channel_t *m_fsChannel; + PSyncPoint m_rxAudioOpened; + PSyncPoint m_txAudioOpened; + OpalMediaFormatList m_switchMediaFormats; - bool CheckPatchAndLock(); + // If FS ever supports more than one audio and one video, this needs to change + switch_timer_t m_read_timer; + switch_codec_t m_read_codec; + switch_codec_t m_write_codec; + + switch_timer_t m_vid_read_timer; + switch_codec_t m_vid_read_codec; + switch_codec_t m_vid_write_codec; + + bool m_flushAudio; + + friend PBoolean FSMediaStream::Open(); }; diff --git a/src/mod/endpoints/mod_rtmp/mod_rtmp.c b/src/mod/endpoints/mod_rtmp/mod_rtmp.c index 5247074762..6b5bc862c9 100644 --- a/src/mod/endpoints/mod_rtmp/mod_rtmp.c +++ b/src/mod/endpoints/mod_rtmp/mod_rtmp.c @@ -625,7 +625,7 @@ switch_call_cause_t rtmp_outgoing_channel(switch_core_session_t *session, switch /*switch_channel_mark_pre_answered(channel);*/ switch_channel_ring_ready(channel); - rtmp_send_incoming_call(*newsession); + rtmp_send_incoming_call(*newsession, var_event); switch_channel_set_state(channel, CS_INIT); switch_set_flag_locked(tech_pvt, TFLAG_IO); diff --git a/src/mod/endpoints/mod_rtmp/mod_rtmp.h b/src/mod/endpoints/mod_rtmp/mod_rtmp.h index 388afeacb5..6109c8544e 100644 --- a/src/mod/endpoints/mod_rtmp/mod_rtmp.h +++ b/src/mod/endpoints/mod_rtmp/mod_rtmp.h @@ -616,7 +616,7 @@ switch_status_t rtmp_send_message(rtmp_session_t *rsession, uint8_t amfnumber, u void rtmp_send_event(rtmp_session_t *rsession, switch_event_t *event); void rtmp_notify_call_state(switch_core_session_t *session); void rtmp_send_display_update(switch_core_session_t *session); -void rtmp_send_incoming_call(switch_core_session_t *session); +void rtmp_send_incoming_call(switch_core_session_t *session, switch_event_t *var_event); void rtmp_send_onhangup(switch_core_session_t *session); void rtmp_add_registration(rtmp_session_t *rsession, const char *auth, const char *nickname); void rtmp_clear_registration(rtmp_session_t *rsession, const char *auth, const char *nickname); diff --git a/src/mod/endpoints/mod_rtmp/rtmp.c b/src/mod/endpoints/mod_rtmp/rtmp.c index f28f518383..a5224719e6 100644 --- a/src/mod/endpoints/mod_rtmp/rtmp.c +++ b/src/mod/endpoints/mod_rtmp/rtmp.c @@ -357,6 +357,25 @@ void rtmp_get_user_variables(switch_event_t **event, switch_core_session_t *sess } } + +void rtmp_get_user_variables_event(switch_event_t **event, switch_event_t *var_event) +{ + switch_event_header_t *he; + + if (!*event && switch_event_create(event, SWITCH_EVENT_CLONE) != SWITCH_STATUS_SUCCESS) { + return; + } + + if ((he = var_event->headers)) { + for (; he; he = he->next) { + if (!strncmp(he->name, RTMP_USER_VARIABLE_PREFIX, strlen(RTMP_USER_VARIABLE_PREFIX))) { + switch_event_add_header_string(*event, SWITCH_STACK_BOTTOM, he->name, he->value); + } + } + } +} + + void rtmp_session_send_onattach(rtmp_session_t *rsession) { const char *uuid = ""; @@ -386,7 +405,7 @@ void rtmp_send_display_update(switch_core_session_t *session) amf0_str(switch_str_nil(tech_pvt->display_callee_id_number)), NULL); } -void rtmp_send_incoming_call(switch_core_session_t *session) +void rtmp_send_incoming_call(switch_core_session_t *session, switch_event_t *var_event) { rtmp_private_t *tech_pvt = switch_core_session_get_private(session); switch_channel_t *channel = switch_core_session_get_channel(session); @@ -394,7 +413,11 @@ void rtmp_send_incoming_call(switch_core_session_t *session) switch_event_t *event = NULL; amf0_data *obj = NULL; - rtmp_get_user_variables(&event, session); + if (var_event) { + rtmp_get_user_variables_event(&event, var_event); + } else { + rtmp_get_user_variables(&event, session); + } if (event) { amf_event_to_object(&obj, event); diff --git a/src/mod/endpoints/mod_rtmp/rtmp_sig.c b/src/mod/endpoints/mod_rtmp/rtmp_sig.c index 5f57eb04c7..a29465ecc0 100644 --- a/src/mod/endpoints/mod_rtmp/rtmp_sig.c +++ b/src/mod/endpoints/mod_rtmp/rtmp_sig.c @@ -539,7 +539,7 @@ RTMP_INVOKE_FUNCTION(rtmp_i_transfer) } if ((tech_pvt = rtmp_locate_private(rsession, uuid))) { - const char *other_uuid = switch_channel_get_variable(tech_pvt->channel, SWITCH_SIGNAL_BOND_VARIABLE); + const char *other_uuid = switch_channel_get_partner_uuid(tech_pvt->channel); switch_core_session_t *session; if (!zstr(other_uuid) && (session = switch_core_session_locate(other_uuid))) { @@ -570,8 +570,8 @@ RTMP_INVOKE_FUNCTION(rtmp_i_join) return SWITCH_STATUS_FALSE; } - if ((other_uuid[0] = switch_channel_get_variable(tech_pvt[0]->channel, SWITCH_SIGNAL_BOND_VARIABLE)) && - (other_uuid[1] = switch_channel_get_variable(tech_pvt[1]->channel, SWITCH_SIGNAL_BOND_VARIABLE))) { + if ((other_uuid[0] = switch_channel_get_partner_uuid(tech_pvt[0]->channel)) && + (other_uuid[1] = switch_channel_get_partner_uuid(tech_pvt[1]->channel))) { #ifndef RTMP_DONT_HOLD if (switch_test_flag(tech_pvt[0], TFLAG_DETACHED)) { @@ -725,8 +725,8 @@ RTMP_INVOKE_FUNCTION(rtmp_i_three_way) return SWITCH_STATUS_FALSE; } - if (!(other_uuid[0] = switch_channel_get_variable(tech_pvt[0]->channel, SWITCH_SIGNAL_BOND_VARIABLE)) || - !(other_uuid[1] = switch_channel_get_variable(tech_pvt[1]->channel, SWITCH_SIGNAL_BOND_VARIABLE))) { + if (!(other_uuid[0] = switch_channel_get_partner_uuid(tech_pvt[0]->channel)) || + !(other_uuid[1] = switch_channel_get_partner_uuid(tech_pvt[1]->channel))) { return SWITCH_STATUS_FALSE; /* Both calls aren't bridged */ } diff --git a/src/mod/endpoints/mod_skinny/skinny_server.c b/src/mod/endpoints/mod_skinny/skinny_server.c index 8e47691c36..95f7e6c83e 100644 --- a/src/mod/endpoints/mod_skinny/skinny_server.c +++ b/src/mod/endpoints/mod_skinny/skinny_server.c @@ -784,12 +784,12 @@ switch_status_t skinny_session_transfer(switch_core_session_t *session, listener tech_pvt = switch_core_session_get_private(session); channel = switch_core_session_get_channel(session); - remote_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); + remote_uuid = switch_channel_get_partner_uuid(channel); if (tech_pvt->transfer_from_call_id) { if((session2 = skinny_profile_find_session(listener->profile, listener, &line_instance, tech_pvt->transfer_from_call_id))) { switch_channel_t *channel2 = switch_core_session_get_channel(session2); - const char *remote_uuid2 = switch_channel_get_variable(channel2, SWITCH_SIGNAL_BOND_VARIABLE); + const char *remote_uuid2 = switch_channel_get_partner_uuid(channel2); if (switch_ivr_uuid_bridge(remote_uuid, remote_uuid2) == SWITCH_STATUS_SUCCESS) { switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING); switch_channel_hangup(channel2, SWITCH_CAUSE_NORMAL_CLEARING); @@ -1007,6 +1007,7 @@ switch_status_t skinny_handle_register(listener_t *listener, skinny_message_t *r const char *value = switch_xml_attr_soft(xbutton, "value"); if(type == SKINNY_BUTTON_LINE) { const char *caller_name = switch_xml_attr_soft(xbutton, "caller-name"); + const char *reg_metadata = switch_xml_attr_soft(xbutton, "registration-metadata"); uint32_t ring_on_idle = atoi(switch_xml_attr_soft(xbutton, "ring-on-idle")); uint32_t ring_on_active = atoi(switch_xml_attr_soft(xbutton, "ring-on-active")); uint32_t busy_trigger = atoi(switch_xml_attr_soft(xbutton, "busy-trigger")); @@ -1030,7 +1031,7 @@ switch_status_t skinny_handle_register(listener_t *listener, skinny_message_t *r switch_safe_free(sql); token = switch_mprintf("skinny/%q/%q/%q:%d", profile->name, value, request->data.reg.device_name, request->data.reg.instance); url = switch_mprintf("skinny/%q/%q", profile->name, value); - switch_core_add_registration(value, profile->domain, token, url, 0, network_ip, network_port_c, "tcp"); + switch_core_add_registration(value, profile->domain, token, url, 0, network_ip, network_port_c, "tcp", reg_metadata); switch_safe_free(token); switch_safe_free(url); } diff --git a/src/mod/endpoints/mod_skypopen/mod_skypopen.c b/src/mod/endpoints/mod_skypopen/mod_skypopen.c index f489cc613a..ce89211b83 100644 --- a/src/mod/endpoints/mod_skypopen/mod_skypopen.c +++ b/src/mod/endpoints/mod_skypopen/mod_skypopen.c @@ -1,6 +1,6 @@ /* * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - * Copyright (C) 2005-2012, Anthony Minessale II + * Copyright (C) 2005-2011, Anthony Minessale II * * Version: MPL 1.1 * @@ -21,19 +21,17 @@ * Portions created by the Initial Developer are Copyright (C) * the Initial Developer. All Rights Reserved. * - * This module (mod_skypopen) has been contributed by: - * - * Giovanni Maruzzelli (gmaruzz@gmail.com) - * - * - * Further Contributors: + * This module (mod_gsmopen) has been contributed by: * + * Giovanni Maruzzelli * + * Maintainer: Giovanni Maruzzelli * * mod_skypopen.c -- Skype compatible Endpoint Module * */ + #include "skypopen.h" #define SKYPE_CHAT_PROTO "skype" @@ -879,7 +877,7 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch switch_sleep(1000); //XXX don't like this try++; if (try < 5) { - DEBUGA_SKYPE("skypopen_audio_read going back to read\n", SKYPOPEN_P_LOG); + //DEBUGA_SKYPE("skypopen_audio_read going back to read\n", SKYPOPEN_P_LOG); goto read; } DEBUGA_SKYPE("READ BUFFER EMPTY, skypopen_audio_read Silence\n", SKYPOPEN_P_LOG); @@ -2533,9 +2531,7 @@ SWITCH_STANDARD_API(sk_function) globals.SKYPOPEN_INTERFACES[tmp_i].message + 30); } } - } - - if (!strcasecmp(argv[0], "list")) { + } else if (!strcasecmp(argv[0], "list")) { int i; unsigned int ib = 0; unsigned int ib_failed = 0; diff --git a/src/mod/endpoints/mod_skypopen/skypopen.h b/src/mod/endpoints/mod_skypopen/skypopen.h index d2dd51bd37..864a22c3e4 100644 --- a/src/mod/endpoints/mod_skypopen/skypopen.h +++ b/src/mod/endpoints/mod_skypopen/skypopen.h @@ -1,6 +1,6 @@ /* * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - * Copyright (C) 2005/2012, Anthony Minessale II + * Copyright (C) 2005-2011, Anthony Minessale II * * Version: MPL 1.1 * @@ -21,18 +21,17 @@ * Portions created by the Initial Developer are Copyright (C) * the Initial Developer. All Rights Reserved. * - * This module (mod_skypopen) has been contributed by: + * This module (mod_gsmopen) has been contributed by: * - * Giovanni Maruzzelli (gmaruzz@gmail.com) - * - * - * Further Contributors: + * Giovanni Maruzzelli * + * Maintainer: Giovanni Maruzzelli * * mod_skypopen.c -- Skype compatible Endpoint Module * */ + #include #include diff --git a/src/mod/endpoints/mod_skypopen/skypopen_protocol.c b/src/mod/endpoints/mod_skypopen/skypopen_protocol.c index 5aa113c3bf..2126955452 100644 --- a/src/mod/endpoints/mod_skypopen/skypopen_protocol.c +++ b/src/mod/endpoints/mod_skypopen/skypopen_protocol.c @@ -1,3 +1,37 @@ +/* + * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2005-2011, Anthony Minessale II + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Anthony Minessale II + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * This module (mod_gsmopen) has been contributed by: + * + * Giovanni Maruzzelli + * + * Maintainer: Giovanni Maruzzelli + * + * skypopen_protocol.c -- Low Level Interface for mod_skypopen + * + */ + + #include "skypopen.h" #ifdef ASTERISK diff --git a/src/mod/endpoints/mod_sofia/Makefile.am b/src/mod/endpoints/mod_sofia/Makefile.am index fb0ab25e0b..efd6b2c385 100644 --- a/src/mod/endpoints/mod_sofia/Makefile.am +++ b/src/mod/endpoints/mod_sofia/Makefile.am @@ -9,7 +9,7 @@ SOFIAUA_BUILDDIR=$(SOFIA_BUILDDIR)/libsofia-sip-ua SOFIALA=$(SOFIAUA_BUILDDIR)/libsofia-sip-ua.la mod_LTLIBRARIES = mod_sofia.la -mod_sofia_la_SOURCES = mod_sofia.c sofia.c sofia_glue.c sofia_presence.c sofia_reg.c sip-dig.c mod_sofia.h +mod_sofia_la_SOURCES = mod_sofia.c sofia.c sofia_glue.c sofia_presence.c sofia_reg.c sip-dig.c rtp.c mod_sofia.h mod_sofia_la_CFLAGS = $(AM_CFLAGS) -I. $(SOFIA_CMD_LINE_CFLAGS) mod_sofia_la_CFLAGS += -I$(SOFIAUA_DIR)/bnf -I$(SOFIAUA_BUILDDIR)/bnf mod_sofia_la_CFLAGS += -I$(SOFIAUA_DIR)/http -I$(SOFIAUA_BUILDDIR)/http diff --git a/src/mod/endpoints/mod_sofia/conf/sip_profiles/external.xml b/src/mod/endpoints/mod_sofia/conf/sip_profiles/external.xml deleted file mode 100644 index a709cbb94c..0000000000 --- a/src/mod/endpoints/mod_sofia/conf/sip_profiles/external.xml +++ /dev/null @@ -1,95 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/mod/endpoints/mod_sofia/conf/sip_profiles/external/example.xml b/src/mod/endpoints/mod_sofia/conf/sip_profiles/external/example.xml deleted file mode 100644 index 70668a9ce5..0000000000 --- a/src/mod/endpoints/mod_sofia/conf/sip_profiles/external/example.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/mod/endpoints/mod_sofia/conf/sip_profiles/internal-ipv6.xml b/src/mod/endpoints/mod_sofia/conf/sip_profiles/internal-ipv6.xml deleted file mode 100644 index ee27a241a0..0000000000 --- a/src/mod/endpoints/mod_sofia/conf/sip_profiles/internal-ipv6.xml +++ /dev/null @@ -1,130 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/mod/endpoints/mod_sofia/conf/sip_profiles/internal.xml b/src/mod/endpoints/mod_sofia/conf/sip_profiles/internal.xml deleted file mode 100644 index b8390c7934..0000000000 --- a/src/mod/endpoints/mod_sofia/conf/sip_profiles/internal.xml +++ /dev/null @@ -1,385 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/mod/endpoints/mod_sofia/conf/sip_profiles/internal/example.xml b/src/mod/endpoints/mod_sofia/conf/sip_profiles/internal/example.xml deleted file mode 100644 index fc061f96d1..0000000000 --- a/src/mod/endpoints/mod_sofia/conf/sip_profiles/internal/example.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml b/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml new file mode 100644 index 0000000000..30d8063a67 --- /dev/null +++ b/src/mod/endpoints/mod_sofia/conf/sofia.conf.xml @@ -0,0 +1,475 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index ea2ba7ce4d..4f671d3467 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -91,10 +91,6 @@ static switch_status_t sofia_on_init(switch_core_session_t *session) sofia_set_flag(tech_pvt, TFLAG_RECOVERED); } - if (switch_channel_direction(tech_pvt->channel) == SWITCH_CALL_DIRECTION_INBOUND) { - nua_respond(tech_pvt->nh, 101, "Dialing", TAG_END()); - } - if (sofia_test_flag(tech_pvt, TFLAG_OUTBOUND) || sofia_test_flag(tech_pvt, TFLAG_RECOVERING)) { const char *var; @@ -176,7 +172,7 @@ static switch_status_t sofia_on_reset(switch_core_session_t *session) const char *uuid = switch_core_session_get_uuid(session); if (switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) { - const char *other_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); + const char *other_uuid = switch_channel_get_partner_uuid(channel); int x = 0; if (other_uuid) { @@ -1591,6 +1587,8 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi case SWITCH_MESSAGE_INDICATE_BRIDGE: { + + switch_channel_set_variable(channel, SOFIA_REPLACES_HEADER, NULL); sofia_glue_tech_track(tech_pvt->profile, session); sofia_set_flag(tech_pvt, TFLAG_SIMPLIFY); @@ -1653,6 +1651,28 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi } } goto end; + case SWITCH_MESSAGE_INDICATE_BLIND_TRANSFER_RESPONSE: + { + const char *event = switch_channel_get_variable(channel, "sip_blind_transfer_event"); + const char *uuid = switch_channel_get_variable(channel, "blind_transfer_uuid"); + char *xdest; + + if (event && uuid) { + nua_notify(tech_pvt->nh, NUTAG_NEWSUB(1), SIPTAG_CONTENT_TYPE_STR("message/sipfrag;version=2.0"), + NUTAG_SUBSTATE(nua_substate_terminated), + SIPTAG_SUBSCRIPTION_STATE_STR("terminated;reason=noresource"), + SIPTAG_PAYLOAD_STR(msg->numeric_arg ? "SIP/2.0 200 OK\r\n" : "SIP/2.0 403 Forbidden\r\n"), + SIPTAG_EVENT_STR(event), TAG_END()); + + + if (!msg->numeric_arg) { + xdest = switch_core_session_sprintf(session, "intercept:%s", uuid); + switch_ivr_session_transfer(session, xdest, "inline", NULL); + } + } + + } + goto end; case SWITCH_MESSAGE_INDICATE_UNBRIDGE: if (switch_rtp_ready(tech_pvt->rtp_session)) { @@ -1806,7 +1826,7 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi } sofia_glue_tech_set_local_sdp(tech_pvt, NULL, SWITCH_FALSE); - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) + if ((uuid = switch_channel_get_partner_uuid(channel)) && (other_session = switch_core_session_locate(uuid))) { other_channel = switch_core_session_get_channel(other_session); ip = switch_channel_get_variable(other_channel, SWITCH_REMOTE_MEDIA_IP_VARIABLE); @@ -2043,7 +2063,7 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi const char *uuid; const char *call_id = NULL, *to_user = NULL, *to_host = NULL, *to_tag = NULL, *from_tag = NULL, *from_user = NULL, *from_host = NULL; - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) || (uuid = switch_channel_get_variable(channel, "originate_signal_bond"))) { + if ((uuid = switch_channel_get_partner_uuid(channel))) { switch_core_session_t *rsession; if ((rsession = switch_core_session_locate(uuid))) { switch_channel_t *rchannel = switch_core_session_get_channel(rsession); @@ -2100,6 +2120,8 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi number = tech_pvt->caller_profile->destination_number; } + switch_ivr_eavesdrop_update_display(session, name, number); + if (!sofia_test_flag(tech_pvt, TFLAG_UPDATING_DISPLAY) && switch_channel_test_flag(channel, CF_ANSWERED)) { if (zstr(tech_pvt->last_sent_callee_id_name) || strcmp(tech_pvt->last_sent_callee_id_name, name) || zstr(tech_pvt->last_sent_callee_id_number) || strcmp(tech_pvt->last_sent_callee_id_number, number)) { @@ -2171,7 +2193,7 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi if (switch_event_create(&event, SWITCH_EVENT_CALL_UPDATE) == SWITCH_STATUS_SUCCESS) { - const char *uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); + const char *uuid = switch_channel_get_partner_uuid(channel); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Direction", "SEND"); @@ -2977,6 +2999,7 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t stream->write_function(stream, "NOMEDIA \t%s\n", sofia_test_flag(profile, TFLAG_INB_NOMEDIA) ? "true" : "false"); stream->write_function(stream, "LATE-NEG \t%s\n", sofia_test_flag(profile, TFLAG_LATE_NEGOTIATION) ? "true" : "false"); stream->write_function(stream, "PROXY-MEDIA \t%s\n", sofia_test_flag(profile, TFLAG_PROXY_MEDIA) ? "true" : "false"); + stream->write_function(stream, "ZRTP-PASSTHRU \t%s\n", sofia_test_flag(profile, TFLAG_ZRTP_PASSTHRU) ? "true" : "false"); stream->write_function(stream, "AGGRESSIVENAT \t%s\n", sofia_test_pflag(profile, PFLAG_AGGRESSIVE_NAT_DETECTION) ? "true" : "false"); stream->write_function(stream, "STUN-ENABLED \t%s\n", sofia_test_pflag(profile, PFLAG_STUN_ENABLED) ? "true" : "false"); @@ -3255,6 +3278,7 @@ static switch_status_t cmd_xml_status(char **argv, int argc, switch_stream_handl stream->write_function(stream, " %s\n", sofia_test_flag(profile, TFLAG_INB_NOMEDIA) ? "true" : "false"); stream->write_function(stream, " %s\n", sofia_test_flag(profile, TFLAG_LATE_NEGOTIATION) ? "true" : "false"); stream->write_function(stream, " %s\n", sofia_test_flag(profile, TFLAG_PROXY_MEDIA) ? "true" : "false"); + stream->write_function(stream, " %s\n", sofia_test_flag(profile, TFLAG_ZRTP_PASSTHRU) ? "true" : "false"); stream->write_function(stream, " %s\n", sofia_test_pflag(profile, PFLAG_AGGRESSIVE_NAT_DETECTION) ? "true" : "false"); stream->write_function(stream, " %s\n", @@ -3620,6 +3644,17 @@ static switch_status_t cmd_profile(char **argv, int argc, switch_stream_handle_t if (!strcasecmp(argv[1], "stop")) { sofia_clear_pflag_locked(profile, PFLAG_RUNNING); + if (argv[2] && !strcasecmp(argv[2], "wait")) { + int loops = 20 * 2; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Waiting for %s to finish SIP transactions.\n", profile->name); + while (!sofia_test_pflag(profile, PFLAG_SHUTDOWN)) { + switch_yield(500000); + if (!--loops) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Timeout Waiting for %s to finish SIP transactions.\n", profile->name); + break; + } + } + } stream->write_function(stream, "stopping: %s", profile->name); } else { sofia_set_pflag_locked(profile, PFLAG_RESPAWN); @@ -3934,6 +3969,7 @@ SWITCH_STANDARD_API(sofia_contact_function) char *p; sofia_profile_t *profile = NULL; const char *exclude_contact = NULL; + const char *user_replacement = NULL; char *reply = "error/facility_not_subscribed"; switch_stream_handle_t mystream = { 0 }; @@ -3945,6 +3981,7 @@ SWITCH_STANDARD_API(sofia_contact_function) if (session) { switch_channel_t *channel = switch_core_session_get_channel(session); exclude_contact = switch_channel_get_variable(channel, "sip_exclude_contact"); + user_replacement = switch_channel_get_variable(channel, "sip_contact_user_replacement"); } @@ -4018,12 +4055,107 @@ SWITCH_STANDARD_API(sofia_contact_function) profile = NULL; } } + } + switch_mutex_unlock(mod_sofia_globals.hash_mutex); } reply = (char *) mystream.data; + if (user_replacement) { + int urlcount = 0; + int copyerr = 0; + char *newreply = NULL; + char *urlstart = NULL; + char *newptr = NULL; + char *bufend = NULL; + char *str = reply; + switch_size_t copysize = 0; + switch_size_t replacesize = strlen(user_replacement); + switch_size_t allocsize = 0; + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "sofia_contact(): trying to replace %s in %s\n", + user_replacement, str); + + /* first pass to count how many URLs we have */ + while ((urlstart = strcasestr(str, "sip:")) || (urlstart = strcasestr(str, "sips:"))) { + urlcount++; + str = urlstart + 4; + } + + if (!urlcount) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): no sip URLs found to replace the user\n"); + copyerr++; + goto copydone; + } + + /* this allocates a bit more than needed but better safe than sorry doing more funky math */ + allocsize = strlen(reply) + (urlcount * replacesize); + newreply = switch_core_session_alloc(session, allocsize); + if (!newreply) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): no buffer space available for replacement\n"); + copyerr++; + goto copydone; + } + + /* get a working pointer to the new reply */ + newptr = newreply; + + /* pointer to the end of the allocated buffer for safety checks */ + bufend = newreply + allocsize; + *bufend = 0; + + /* go thru all the urls and replace the user part */ + str = reply; + while ((urlstart = strcasestr(str, "sip:")) || (urlstart = strcasestr(str, "sips:"))) { + + /* found an URL, copy up to the start of the url */ + copysize = ( urlstart - str ) + 4; + + /* double check boundaries before copying anything at all (this should not happen) */ + if ((newptr + copysize + replacesize) >= bufend) { + copyerr++; + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): wow buffer was not big enough!\n"); + break; + } + + /* copy the original contact string except for the user */ + memcpy(newptr, str, copysize); + newptr += copysize; + + /* copy the user replacement */ + memcpy(newptr, user_replacement, replacesize); + newptr += replacesize; + + /* skip the original user part */ + str = strchr(urlstart, '@'); + if (!str) { + copyerr++; + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): not host part found for contact\n"); + break; + } + /* continue searching for the next sip: URL */ + } + +copydone: + if (!copyerr) { + /* copy the remaining reply string */ + copysize = strlen(str); + if ((newptr + copysize) >= bufend) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "sofia_contact(): wow buffer was not big enough, close, but not enough!\n"); + } else { + strcpy(newptr, str); + reply = newreply; + goto end; + } + } else { + /* on error, we do nothing and just default to the original stream returned (mystream.data) */ + } + } + + reply = (char *) mystream.data; + end: if (zstr(reply)) { @@ -4109,7 +4241,7 @@ SWITCH_STANDARD_API(sofia_function) "sofia global siptrace \n" "sofia capture \n" " watchdog \n\n" - "sofia profile [start | stop | restart | rescan]\n" + "sofia profile [start | stop | restart | rescan] [wait]\n" " flush_inbound_reg [ | <[user]@domain>] [reboot]\n" " check_sync [ | <[user]@domain>]\n" " [register | unregister] [ | all]\n" @@ -4754,6 +4886,17 @@ static switch_call_cause_t sofia_outgoing_channel(switch_core_session_t *session sofia_clear_flag(ctech_pvt, TFLAG_ENABLE_SOA); } + if (switch_channel_test_flag(o_channel, CF_ZRTP_PASSTHRU_REQ)) { + const char *x = NULL; + sofia_glue_pass_zrtp_hash2(session, nsession); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "[zrtp_passthru] Setting a-leg inherit_codec=true\n"); + switch_channel_set_variable(o_channel, "inherit_codec", "true"); + if ((x = switch_channel_get_variable(o_channel, "ep_codec_string"))) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "[zrtp_passthru] Setting b-leg absolute_codec_string='%s'\n", x); + switch_channel_set_variable(nchannel, "absolute_codec_string", x); + } + } + /* SNARK: lets copy this across so we can see if we're the other leg of 3PCC + bypass_media... */ if (sofia_test_flag(ctech_pvt, TFLAG_3PCC) && (switch_channel_test_flag(o_channel, CF_PROXY_MODE) || switch_channel_test_flag(o_channel, CF_PROXY_MEDIA))) { sofia_set_flag(tech_pvt, TFLAG_3PCC_INVITE); @@ -5341,12 +5484,52 @@ static switch_status_t list_profile_gateway(const char *line, const char *cursor return status; } +SWITCH_STANDARD_APP(sofia_sla_function) +{ + private_object_t *tech_pvt; + switch_core_session_t *bargee_session; + + if (zstr(data)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Usage: \n"); + return; + } + + if ((bargee_session = switch_core_session_locate((char *)data))) { + if (bargee_session == session) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "BARGE: %s (cannot barge on myself)\n", (char *) data); + } else { + switch_channel_t *channel; + + if (switch_core_session_check_interface(bargee_session, sofia_endpoint_interface)) { + tech_pvt = switch_core_session_get_private(bargee_session); + sofia_clear_flag(tech_pvt, TFLAG_SLA_BARGING); + sofia_set_flag(tech_pvt, TFLAG_SLA_BARGE); + switch_ivr_transfer_variable(bargee_session, session, SWITCH_SIGNAL_BOND_VARIABLE); + } + + if (switch_core_session_check_interface(session, sofia_endpoint_interface)) { + tech_pvt = switch_core_session_get_private(session); + sofia_set_flag(tech_pvt, TFLAG_SLA_BARGING); + } + + channel = switch_core_session_get_channel(session); + switch_channel_set_variable(channel, "sip_barging_uuid", (char *)data); + + } + + switch_core_session_rwunlock(bargee_session); + } + + switch_ivr_eavesdrop_session(session, data, NULL, ED_MUX_READ | ED_MUX_WRITE | ED_COPY_DISPLAY); +} + SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load) { switch_chat_interface_t *chat_interface; switch_api_interface_t *api_interface; switch_management_interface_t *management_interface; + switch_application_interface_t *app_interface; struct in_addr in; memset(&mod_sofia_globals, 0, sizeof(mod_sofia_globals)); @@ -5376,6 +5559,22 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load) switch_queue_create(&mod_sofia_globals.presence_queue, SOFIA_QUEUE_SIZE, mod_sofia_globals.pool); switch_queue_create(&mod_sofia_globals.mwi_queue, SOFIA_QUEUE_SIZE, mod_sofia_globals.pool); + mod_sofia_globals.cpu_count = switch_core_cpu_count(); + mod_sofia_globals.max_msg_queues = (mod_sofia_globals.cpu_count / 2) + 1; + if (mod_sofia_globals.max_msg_queues < 2) { + mod_sofia_globals.max_msg_queues = 2; + } + + if (mod_sofia_globals.max_msg_queues > SOFIA_MAX_MSG_QUEUE) { + mod_sofia_globals.max_msg_queues = SOFIA_MAX_MSG_QUEUE; + } + + switch_queue_create(&mod_sofia_globals.msg_queue, SOFIA_MSG_QUEUE_SIZE * mod_sofia_globals.max_msg_queues, mod_sofia_globals.pool); + + /* start one message thread */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Starting initial message thread.\n"); + sofia_msg_thread_start(0); + if (config_sofia(0, NULL) != SWITCH_STATUS_SUCCESS) { mod_sofia_globals.running = 0; return SWITCH_STATUS_GENERR; @@ -5384,17 +5583,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Waiting for profiles to start\n"); switch_yield(1500000); - mod_sofia_globals.cpu_count = switch_core_cpu_count(); - mod_sofia_globals.max_msg_queues = mod_sofia_globals.cpu_count + 1; - - if (mod_sofia_globals.max_msg_queues > SOFIA_MAX_MSG_QUEUE) { - mod_sofia_globals.max_msg_queues = SOFIA_MAX_MSG_QUEUE; - } - - /* start one message thread */ - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Starting initial message thread.\n"); - sofia_msg_thread_start(0); - if (switch_event_bind_removable(modname, SWITCH_EVENT_CUSTOM, MULTICAST_EVENT, event_handler, NULL, &mod_sofia_globals.custom_node) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n"); @@ -5468,6 +5656,10 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load) management_interface->relative_oid = "1001"; management_interface->management_function = sofia_manage; + SWITCH_ADD_APP(app_interface, "sofia_sla", "private sofia sla function", + "private sofia sla function", sofia_sla_function, "", SAF_NONE); + + SWITCH_ADD_API(api_interface, "sofia", "Sofia Controls", sofia_function, " "); SWITCH_ADD_API(api_interface, "sofia_gateway_data", "Get data from a sofia gateway", sofia_gateway_data_function, " [ivar|ovar|var] "); @@ -5488,9 +5680,9 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load) switch_console_set_complete("add sofia profile"); switch_console_set_complete("add sofia profile restart all"); - + switch_console_set_complete("add sofia profile ::sofia::list_profiles start"); - switch_console_set_complete("add sofia profile ::sofia::list_profiles stop"); + switch_console_set_complete("add sofia profile ::sofia::list_profiles stop wait"); switch_console_set_complete("add sofia profile ::sofia::list_profiles rescan"); switch_console_set_complete("add sofia profile ::sofia::list_profiles restart"); @@ -5526,6 +5718,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load) SWITCH_ADD_API(api_interface, "sofia_count_reg", "Count Sofia registration", sofia_count_reg_function, "[profile/]@"); SWITCH_ADD_API(api_interface, "sofia_dig", "SIP DIG", sip_dig_function, ""); SWITCH_ADD_CHAT(chat_interface, SOFIA_CHAT_PROTO, sofia_presence_chat_send); + + crtp_init(*module_interface); /* indicate that the module should continue to be loaded */ return SWITCH_STATUS_SUCCESS; @@ -5563,11 +5757,12 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_sofia_shutdown) } - for (i = 0; i < mod_sofia_globals.msg_queue_len; i++) { - switch_queue_push(mod_sofia_globals.msg_queue[i], NULL); + for (i = 0; mod_sofia_globals.msg_queue_thread[i]; i++) { + switch_queue_push(mod_sofia_globals.msg_queue, NULL); } - for (i = 0; i < mod_sofia_globals.msg_queue_len; i++) { + + for (i = 0; mod_sofia_globals.msg_queue_thread[i]; i++) { switch_status_t st; switch_thread_join(&st, mod_sofia_globals.msg_queue_thread[i]); } diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h index 5a51f68a2d..8a8e277da4 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.h +++ b/src/mod/endpoints/mod_sofia/mod_sofia.h @@ -92,7 +92,7 @@ typedef struct private_object private_object_t; #define MULTICAST_EVENT "multicast::event" #define SOFIA_REPLACES_HEADER "_sofia_replaces_" -#define SOFIA_USER_AGENT "FreeSWITCH-mod_sofia/" SWITCH_VERSION_MAJOR "." SWITCH_VERSION_MINOR "." SWITCH_VERSION_MICRO "-" SWITCH_VERSION_REVISION +#define SOFIA_USER_AGENT "NetborderSS7/" SWITCH_VERSION_MAJOR "." SWITCH_VERSION_MINOR "." SWITCH_VERSION_MICRO "-" SWITCH_VERSION_REVISION #define SOFIA_CHAT_PROTO "sip" #define SOFIA_MULTIPART_PREFIX "sip_mp_" #define SOFIA_MULTIPART_PREFIX_T "~sip_mp_" @@ -151,6 +151,7 @@ typedef struct sofia_dispatch_event_s { sofia_profile_t *profile; int save; switch_core_session_t *session; + switch_memory_pool_t *pool; } sofia_dispatch_event_t; struct sofia_private { @@ -264,6 +265,9 @@ typedef enum { PFLAG_PRESENCE_MAP, PFLAG_OPTIONS_RESPOND_503_ON_BUSY, PFLAG_PRESENCE_DISABLE_EARLY, + PFLAG_CONFIRM_BLIND_TRANSFER, + PFLAG_THREAD_PER_REG, + PFLAG_MWI_USE_REG_CALLID, /* No new flags below this line */ PFLAG_MAX } PFLAGS; @@ -313,6 +317,7 @@ typedef enum { TFLAG_TPORT_LOG, TFLAG_SENT_UPDATE, TFLAG_PROXY_MEDIA, + TFLAG_ZRTP_PASSTHRU, TFLAG_HOLD_LOCK, TFLAG_3PCC_HAS_ACK, TFLAG_PASS_RFC2833, @@ -332,12 +337,14 @@ typedef enum { TFLAG_GOT_ACK, TFLAG_CAPTURE, TFLAG_REINVITED, + TFLAG_SLA_BARGE, + TFLAG_SLA_BARGING, /* No new flags below this line */ TFLAG_MAX } TFLAGS; #define SOFIA_MAX_MSG_QUEUE 64 -#define SOFIA_MSG_QUEUE_SIZE 250 +#define SOFIA_MSG_QUEUE_SIZE 100 struct mod_sofia_globals { switch_memory_pool_t *pool; @@ -354,7 +361,7 @@ struct mod_sofia_globals { char hostname[512]; switch_queue_t *presence_queue; switch_queue_t *mwi_queue; - switch_queue_t *msg_queue[SOFIA_MAX_MSG_QUEUE]; + switch_queue_t *msg_queue; switch_thread_t *msg_queue_thread[SOFIA_MAX_MSG_QUEUE]; int msg_queue_len; struct sofia_private destroy_private; @@ -607,6 +614,7 @@ struct sofia_profile { sofia_gateway_t *gateways; //su_home_t *home; switch_hash_t *chat_hash; + switch_hash_t *mwi_debounce_hash; //switch_core_db_t *master_db; switch_thread_rwlock_t *rwlock; switch_mutex_t *flag_mutex; @@ -818,6 +826,11 @@ struct private_object { switch_payload_t ianacodes[SWITCH_MAX_CODECS]; uint32_t session_timeout; enum nua_session_refresher session_refresher; + /** ZRTP **/ + char *local_sdp_audio_zrtp_hash; + char *local_sdp_video_zrtp_hash; + char *remote_sdp_audio_zrtp_hash; + char *remote_sdp_video_zrtp_hash; }; struct callback_t { @@ -925,6 +938,8 @@ void launch_sofia_profile_thread(sofia_profile_t *profile); switch_status_t sofia_presence_chat_send(switch_event_t *message_event); void sofia_glue_tech_absorb_sdp(private_object_t *tech_pvt); +void sofia_glue_pass_zrtp_hash2(switch_core_session_t *aleg_session, switch_core_session_t *bleg_session); +void sofia_glue_pass_zrtp_hash(switch_core_session_t *session); /* * \brief Sets the "ep_codec_string" channel variable, parsing r_sdp and taing codec_string in consideration @@ -1134,7 +1149,7 @@ void sofia_glue_get_addr(msg_t *msg, char *buf, size_t buflen, int *port); sofia_destination_t *sofia_glue_get_destination(char *data); void sofia_glue_free_destination(sofia_destination_t *dst); switch_status_t sofia_glue_send_notify(sofia_profile_t *profile, const char *user, const char *host, const char *event, const char *contenttype, - const char *body, const char *o_contact, const char *network_ip); + const char *body, const char *o_contact, const char *network_ip, const char *call_id); char *sofia_glue_get_extra_headers(switch_channel_t *channel, const char *prefix); void sofia_glue_set_extra_headers(switch_core_session_t *session, sip_t const *sip, const char *prefix); char *sofia_glue_get_extra_headers_from_event(switch_event_t *event, const char *prefix); @@ -1172,3 +1187,16 @@ void sofia_process_dispatch_event(sofia_dispatch_event_t **dep); char *sofia_glue_get_host(const char *str, switch_memory_pool_t *pool); void sofia_presence_check_subscriptions(sofia_profile_t *profile, time_t now); void sofia_msg_thread_start(int idx); +void crtp_init(switch_loadable_module_interface_t *module_interface); + + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4: + */ diff --git a/src/mod/endpoints/mod_sofia/rtp.c b/src/mod/endpoints/mod_sofia/rtp.c new file mode 100644 index 0000000000..50288a43df --- /dev/null +++ b/src/mod/endpoints/mod_sofia/rtp.c @@ -0,0 +1,703 @@ +/* + * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2005-2011, Anthony Minessale II + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Anthony Minessale II + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Mathieu Rene + * + * rtp.c -- RTP Controllable Channel Module + * + */ + +#include +#include "mod_sofia.h" + +#define kLOCALADDR "local_addr" +#define kLOCALPORT "local_port" +#define kREMOTEADDR "remote_addr" +#define kREMOTEPORT "remote_port" +#define kCODEC "codec" +#define kPTIME "ptime" +#define kPT "pt" +#define kRFC2833PT "rfc2833_pt" +#define kMEDIATYPE "media_type" +#define kRATE "rate" + +static struct { + switch_memory_pool_t *pool; + switch_endpoint_interface_t *endpoint_interface; +} crtp; + +typedef struct { + switch_core_session_t *session; + switch_channel_t *channel; + switch_codec_t read_codec, write_codec; + switch_frame_t read_frame; + + + + switch_rtp_bug_flag_t rtp_bugs; + switch_rtp_t *rtp_session; + + uint32_t timestamp_send; + + const char *local_address; + const char *remote_address; + const char *codec; + int ptime; + + const switch_codec_implementation_t *negotiated_codecs[SWITCH_MAX_CODECS]; + int num_negotiated_codecs; + + char *origin; + + switch_port_t local_port; + switch_port_t remote_port; + switch_payload_t agreed_pt; /*XXX*/ + switch_payload_t rfc2833_pt; + + sofia_dtmf_t dtmf_type; + enum { + RTP_SENDONLY, + RTP_RECVONLY, + RTP_SENDRECV + } mode; +} crtp_private_t; + +static switch_status_t channel_on_init(switch_core_session_t *session); +static switch_status_t channel_on_destroy(switch_core_session_t *session); +static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *session, switch_event_t *var_event, + switch_caller_profile_t *outbound_profile, + switch_core_session_t **new_session, + switch_memory_pool_t **pool, + switch_originate_flag_t flags, switch_call_cause_t *cancel_cause); +static switch_status_t channel_read_frame(switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id); +static switch_status_t channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id); +static switch_status_t channel_receive_message(switch_core_session_t *session, switch_core_session_message_t *msg); +static switch_status_t channel_send_dtmf(switch_core_session_t *session, const switch_dtmf_t *dtmf); +static switch_status_t channel_receive_event(switch_core_session_t *session, switch_event_t *event); + +switch_state_handler_table_t crtp_state_handlers = { + .on_init = channel_on_init, + .on_destroy = channel_on_destroy +}; + +switch_io_routines_t crtp_io_routines = { + .outgoing_channel = channel_outgoing_channel, + .read_frame = channel_read_frame, + .write_frame = channel_write_frame, + .receive_message = channel_receive_message, + .receive_event = channel_receive_event, + .send_dtmf = channel_send_dtmf +}; + + +void crtp_init(switch_loadable_module_interface_t *module_interface) +{ + switch_endpoint_interface_t *endpoint_interface; + //switch_api_interface_t *api_interface; + + crtp.pool = module_interface->pool; + endpoint_interface = switch_loadable_module_create_interface(module_interface, SWITCH_ENDPOINT_INTERFACE); + endpoint_interface->interface_name = "rtp"; + endpoint_interface->io_routines = &crtp_io_routines; + endpoint_interface->state_handler = &crtp_state_handlers; + crtp.endpoint_interface = endpoint_interface; + + //SWITCH_ADD_API(api_interface, "rtp_test", "test", test_function, ""); +} + +static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *session, switch_event_t *var_event, + switch_caller_profile_t *outbound_profile, + switch_core_session_t **new_session, + switch_memory_pool_t **pool, + switch_originate_flag_t flags, switch_call_cause_t *cancel_cause) +{ + switch_channel_t *channel; + char name[128]; + crtp_private_t *tech_pvt = NULL; + switch_caller_profile_t *caller_profile; + + const char *err; + + + const char *local_addr = switch_event_get_header_nil(var_event, kLOCALADDR), + *szlocal_port = switch_event_get_header_nil(var_event, kLOCALPORT), + *remote_addr = switch_event_get_header_nil(var_event, kREMOTEADDR), + *szremote_port = switch_event_get_header_nil(var_event, kREMOTEPORT), + *codec = switch_event_get_header_nil(var_event, kCODEC), + *szptime = switch_event_get_header_nil(var_event, kPTIME), + //*mode = switch_event_get_header_nil(var_event, kMODE), + *szrfc2833_pt = switch_event_get_header_nil(var_event, kRFC2833PT), + *szrate = switch_event_get_header_nil(var_event, kRATE), + *szpt = switch_event_get_header_nil(var_event, kPT); + + + switch_port_t local_port = !zstr(szlocal_port) ? atoi(szlocal_port) : 0, + remote_port = !zstr(szremote_port) ? atoi(szremote_port) : 0; + + int ptime = !zstr(szptime) ? atoi(szptime) : 0, + rfc2833_pt = !zstr(szrfc2833_pt) ? atoi(szrfc2833_pt) : 0, + rate = !zstr(szrate) ? atoi(szrate) : 8000, + pt = !zstr(szpt) ? atoi(szpt) : 0; + + if ( + ((zstr(remote_addr) || remote_port == 0) && (zstr(local_addr) || local_port == 0)) || + zstr(codec) || + zstr(szpt)) { + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing required arguments\n"); + goto fail; + } + + + if (!(*new_session = switch_core_session_request(crtp.endpoint_interface, SWITCH_CALL_DIRECTION_OUTBOUND, 0, pool))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't request session.\n"); + goto fail; + } + + channel = switch_core_session_get_channel(*new_session); + + tech_pvt = switch_core_session_alloc(*new_session, sizeof *tech_pvt); + tech_pvt->session = *new_session; + tech_pvt->channel = channel; + tech_pvt->local_address = switch_core_session_strdup(*new_session, local_addr); + tech_pvt->local_port = local_port; + tech_pvt->remote_address = switch_core_session_strdup(*new_session, remote_addr); + tech_pvt->remote_port = remote_port; + tech_pvt->ptime = ptime; + tech_pvt->agreed_pt = pt; + tech_pvt->rfc2833_pt = rfc2833_pt; + if (rfc2833_pt) { + tech_pvt->dtmf_type = DTMF_2833; + } else { + tech_pvt->dtmf_type = DTMF_NONE; + } + + if (zstr(local_addr) || local_port == 0) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "The local address and port must be set\n"); + goto fail; + } else if (zstr(remote_addr) || remote_port == 0) { + tech_pvt->mode = RTP_RECVONLY; + } else { + tech_pvt->mode = RTP_SENDRECV; + } + + switch_core_session_set_private(*new_session, tech_pvt); + + caller_profile = switch_caller_profile_clone(*new_session, outbound_profile); + switch_channel_set_caller_profile(channel, caller_profile); + + + snprintf(name, sizeof(name), "rtp/%s", outbound_profile->destination_number); + switch_channel_set_name(channel, name); + + switch_channel_set_state(channel, CS_INIT); + + if (switch_core_codec_init(&tech_pvt->read_codec, + codec, + NULL, + rate, + ptime, + 1, + SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, + NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't load codec?\n"); + goto fail; + } else { + if (switch_core_codec_init(&tech_pvt->write_codec, + codec, + NULL, + rate, + ptime, + 1, + SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, + NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't load codec?\n"); + goto fail; + } + } + + if (switch_core_session_set_read_codec(*new_session, &tech_pvt->read_codec) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't set read codec?\n"); + goto fail; + } + + if (switch_core_session_set_write_codec(*new_session, &tech_pvt->write_codec) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't set write codec?\n"); + goto fail; + } + + if (!(tech_pvt->rtp_session = switch_rtp_new(local_addr, local_port, remote_addr, remote_port, tech_pvt->agreed_pt, + tech_pvt->read_codec.implementation->samples_per_packet, ptime * 1000, + flags, "soft", &err, switch_core_session_get_pool(*new_session)))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't setup RTP session: [%s]\n", err); + goto fail; + } + + if (tech_pvt->dtmf_type == DTMF_2833) { + switch_rtp_set_telephony_event(tech_pvt->rtp_session, tech_pvt->rfc2833_pt); + } + + if (switch_core_session_thread_launch(*new_session) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't start session thread.\n"); + goto fail; + } + + switch_channel_mark_answered(channel); + + return SWITCH_CAUSE_SUCCESS; + +fail: + if (tech_pvt) { + if (tech_pvt->read_codec.implementation) { + switch_core_codec_destroy(&tech_pvt->read_codec); + } + + if (tech_pvt->write_codec.implementation) { + switch_core_codec_destroy(&tech_pvt->write_codec); + } + } + + if (*new_session) { + switch_core_session_destroy(new_session); + } + return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER; +} + +static switch_status_t channel_on_init(switch_core_session_t *session) +{ + + switch_channel_t *channel = switch_core_session_get_channel(session); + + switch_channel_set_state(channel, CS_CONSUME_MEDIA); + + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t channel_on_destroy(switch_core_session_t *session) +{ + crtp_private_t *tech_pvt = switch_core_session_get_private(session); + + if ((tech_pvt = switch_core_session_get_private(session))) { + + if (tech_pvt->read_codec.implementation) { + switch_core_codec_destroy(&tech_pvt->read_codec); + } + + if (tech_pvt->write_codec.implementation) { + switch_core_codec_destroy(&tech_pvt->write_codec); + } + } + + return SWITCH_STATUS_SUCCESS; +} + + +static switch_status_t channel_read_frame(switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id) +{ + crtp_private_t *tech_pvt; + switch_channel_t *channel; + switch_status_t status; + + channel = switch_core_session_get_channel(session); + assert(channel != NULL); + + tech_pvt = switch_core_session_get_private(session); + assert(tech_pvt != NULL); + + if (!tech_pvt->rtp_session || tech_pvt->mode == RTP_SENDONLY) { + switch_yield(20000); /* replace by local timer XXX */ + goto cng; + } + + if (switch_rtp_has_dtmf(tech_pvt->rtp_session)) { + switch_dtmf_t dtmf = { 0 }; + switch_rtp_dequeue_dtmf(tech_pvt->rtp_session, &dtmf); + switch_channel_queue_dtmf(channel, &dtmf); + } + + tech_pvt->read_frame.flags = SFF_NONE; + tech_pvt->read_frame.codec = &tech_pvt->read_codec; + status = switch_rtp_zerocopy_read_frame(tech_pvt->rtp_session, &tech_pvt->read_frame, flags); + + if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) { + goto cng; + } + + *frame = &tech_pvt->read_frame; + return SWITCH_STATUS_SUCCESS; + +cng: + *frame = &tech_pvt->read_frame; + tech_pvt->read_frame.codec = &tech_pvt->read_codec; + tech_pvt->read_frame.flags |= SFF_CNG; + tech_pvt->read_frame.datalen = 0; + + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id) +{ + crtp_private_t *tech_pvt; + switch_channel_t *channel; + //int frames = 0, bytes = 0, samples = 0; + + channel = switch_core_session_get_channel(session); + assert(channel != NULL); + + tech_pvt = switch_core_session_get_private(session); + assert(tech_pvt != NULL); + + +#if 0 + if (!switch_test_flag(frame, SFF_CNG) && !switch_test_flag(frame, SFF_PROXY_PACKET)) { + if (tech_pvt->read_codec.implementation->encoded_bytes_per_packet) { + bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_packet; + frames = ((int) frame->datalen / bytes); + } else + frames = 1; + + samples = frames * tech_pvt->read_codec.implementation->samples_per_packet; + } + + tech_pvt->timestamp_send += samples; +#endif + if (tech_pvt->mode == RTP_RECVONLY) { + return SWITCH_STATUS_SUCCESS; + } + + switch_rtp_write_frame(tech_pvt->rtp_session, frame); + + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t channel_send_dtmf(switch_core_session_t *session, const switch_dtmf_t *dtmf) +{ + crtp_private_t *tech_pvt = NULL; + + tech_pvt = switch_core_session_get_private(session); + assert(tech_pvt != NULL); + + switch(tech_pvt->dtmf_type) { + case DTMF_2833: + { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Enqueuing RFC2833 DTMF %c of length %d\n", dtmf->digit, dtmf->duration); + return switch_rtp_queue_rfc2833(tech_pvt->rtp_session, dtmf); + } + case DTMF_NONE: + default: + { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Discarding DTMF %c of length %d, DTMF type is NONE\n", dtmf->digit, dtmf->duration); + } + } + + return SWITCH_STATUS_SUCCESS; +} + +static switch_bool_t compare_var(switch_event_t *event, switch_channel_t *channel, const char *varname) +{ + const char *chan_val = switch_channel_get_variable_dup(channel, varname, SWITCH_FALSE, -1); + const char *event_val = switch_event_get_header(event, varname); + + if (zstr(chan_val) || zstr(event_val)) { + return 1; + } + + return strcasecmp(chan_val, event_val); +} + +static switch_status_t channel_receive_event(switch_core_session_t *session, switch_event_t *event) +{ + const char *command = switch_event_get_header(event, "command"); + switch_channel_t *channel = switch_core_session_get_channel(session); + crtp_private_t *tech_pvt = switch_core_session_get_private(session); + char *codec = switch_event_get_header_nil(event, kCODEC); + char *szptime = switch_event_get_header_nil(event, kPTIME); + char *szrate = switch_event_get_header_nil(event, kRATE); + char *szpt = switch_event_get_header_nil(event, kPT); + + int ptime = !zstr(szptime) ? atoi(szptime) : 0, + rate = !zstr(szrate) ? atoi(szrate) : 8000, + pt = !zstr(szpt) ? atoi(szpt) : 0; + + + if (!zstr(command) && !strcasecmp(command, "media_modify")) { + /* Compare parameters */ + if (compare_var(event, channel, kREMOTEADDR) || + compare_var(event, channel, kREMOTEPORT)) { + char *remote_addr = switch_event_get_header(event, kREMOTEADDR); + char *szremote_port = switch_event_get_header(event, kREMOTEPORT); + switch_port_t remote_port = !zstr(szremote_port) ? atoi(szremote_port) : 0; + const char *err; + + + switch_channel_set_variable(channel, kREMOTEADDR, remote_addr); + switch_channel_set_variable(channel, kREMOTEPORT, szremote_port); + + if (switch_rtp_set_remote_address(tech_pvt->rtp_session, remote_addr, remote_port, 0, SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error setting RTP remote address: %s\n", err); + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Set RTP remote: %s:%d\n", remote_addr, (int)remote_port); + tech_pvt->mode = RTP_SENDRECV; + } + } + + if (compare_var(event, channel, kCODEC) || + compare_var(event, channel, kPTIME) || + compare_var(event, channel, kPT) || + compare_var(event, channel, kRATE)) { + /* Reset codec */ + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Switching codec updating \n"); + + if (switch_core_codec_init(&tech_pvt->read_codec, + codec, + NULL, + rate, + ptime, + 1, + SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, + NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't load codec?\n"); + goto fail; + } else { + if (switch_core_codec_init(&tech_pvt->write_codec, + codec, + NULL, + rate, + ptime, + 1, + SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, + NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't load codec?\n"); + goto fail; + } + } + + if (switch_core_session_set_read_codec(session, &tech_pvt->read_codec) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't set read codec?\n"); + goto fail; + } + + if (switch_core_session_set_write_codec(session, &tech_pvt->write_codec) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't set write codec?\n"); + goto fail; + } + + switch_rtp_set_default_payload(tech_pvt->rtp_session, pt); + switch_rtp_set_recv_pt(tech_pvt->rtp_session, pt); + } + + if (compare_var(event, channel, kRFC2833PT)) { + const char *szpt = switch_event_get_header(event, kRFC2833PT); + int pt = !zstr(szpt) ? atoi(szpt) : 0; + + tech_pvt->rfc2833_pt = pt; + + if (pt) { + tech_pvt->dtmf_type = DTMF_2833; + } else { + tech_pvt->dtmf_type = DTMF_NONE; + } + + switch_channel_set_variable(channel, kRFC2833PT, szpt); + switch_rtp_set_telephony_event(tech_pvt->rtp_session, pt); + } + + if (compare_var(event, channel, kMEDIATYPE)) { + const char *newmode = switch_event_get_header(event, kMEDIATYPE); + + if (!strcmp(newmode, "image")) { + switch_channel_set_variable(tech_pvt->channel, "has_t38", "true"); + switch_channel_execute_on(tech_pvt->channel, "rtp_execute_on_image"); + switch_channel_set_app_flag_key("T38", tech_pvt->channel, CF_APP_T38); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Got IMAGE description\n"); + } + + switch_channel_set_variable(channel, kMEDIATYPE, newmode); + } + + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Received unknown command [%s] in event.\n", !command ? "null" : command); + } + + return SWITCH_STATUS_SUCCESS; +fail: + if (tech_pvt) { + if (tech_pvt->read_codec.implementation) { + switch_core_codec_destroy(&tech_pvt->read_codec); + } + + if (tech_pvt->write_codec.implementation) { + switch_core_codec_destroy(&tech_pvt->write_codec); + } + } + + if (session) { + switch_core_session_destroy(&session); + } + return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER; + +} + +static switch_status_t channel_receive_message(switch_core_session_t *session, switch_core_session_message_t *msg) +{ + crtp_private_t *tech_pvt = NULL; + + tech_pvt = switch_core_session_get_private(session); + assert(tech_pvt != NULL); + + switch (msg->message_id) { + case SWITCH_MESSAGE_INDICATE_UDPTL_MODE: + { + switch_t38_options_t *t38_options = switch_channel_get_private(tech_pvt->channel, "t38_options"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "SWITCH_MESSAGE_INDICATE_UDPTL_MODE\n"); + if (!t38_options) { + goto end; + } + + switch_rtp_udptl_mode(tech_pvt->rtp_session); + break; + } + case SWITCH_MESSAGE_INDICATE_T38_DESCRIPTION: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "SWITCH_MESSAGE_INDICATE_T38_DESCRIPTION\n"); + break; + } + case SWITCH_MESSAGE_INDICATE_REQUEST_IMAGE_MEDIA: + { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "SWITCH_MESSAGE_INDICATE_REQUEST_IMAGE_MEDIA\n"); + break; + } + case SWITCH_MESSAGE_INDICATE_DEBUG_AUDIO: + { + if (switch_rtp_ready(tech_pvt->rtp_session) && !zstr(msg->string_array_arg[0]) && !zstr(msg->string_array_arg[1])) { + int32_t flags = 0; + if (!strcasecmp(msg->string_array_arg[0], "read")) { + flags |= SWITCH_RTP_FLAG_DEBUG_RTP_READ; + } else if (!strcasecmp(msg->string_array_arg[0], "write")) { + flags |= SWITCH_RTP_FLAG_DEBUG_RTP_WRITE; + } else if (!strcasecmp(msg->string_array_arg[0], "both")) { + flags |= SWITCH_RTP_FLAG_DEBUG_RTP_READ | SWITCH_RTP_FLAG_DEBUG_RTP_WRITE; + } + + if (flags) { + if (switch_true(msg->string_array_arg[1])) { + switch_rtp_set_flag(tech_pvt->rtp_session, flags); + } else { + switch_rtp_clear_flag(tech_pvt->rtp_session, flags); + } + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Invalid Options\n"); + } + } + break; + } + case SWITCH_MESSAGE_INDICATE_AUDIO_SYNC: + if (switch_rtp_ready(tech_pvt->rtp_session)) { + rtp_flush_read_buffer(tech_pvt->rtp_session, SWITCH_RTP_FLUSH_ONCE); + } + break; + case SWITCH_MESSAGE_INDICATE_JITTER_BUFFER: + { + if (switch_rtp_ready(tech_pvt->rtp_session)) { + int len = 0, maxlen = 0, qlen = 0, maxqlen = 50, max_drift = 0; + + if (msg->string_arg) { + char *p, *q; + const char *s; + + if (!strcasecmp(msg->string_arg, "pause")) { + switch_rtp_pause_jitter_buffer(tech_pvt->rtp_session, SWITCH_TRUE); + goto end; + } else if (!strcasecmp(msg->string_arg, "resume")) { + switch_rtp_pause_jitter_buffer(tech_pvt->rtp_session, SWITCH_FALSE); + goto end; + } else if (!strncasecmp(msg->string_arg, "debug:", 6)) { + s = msg->string_arg + 6; + if (s && !strcmp(s, "off")) { + s = NULL; + } + switch_rtp_debug_jitter_buffer(tech_pvt->rtp_session, s); + goto end; + } + + + if ((len = atoi(msg->string_arg))) { + qlen = len / (tech_pvt->read_codec.implementation->microseconds_per_packet / 1000); + if (qlen < 1) { + qlen = 3; + } + } + + if (qlen) { + if ((p = strchr(msg->string_arg, ':'))) { + p++; + maxlen = atol(p); + if ((q = strchr(p, ':'))) { + q++; + max_drift = abs(atol(q)); + } + } + } + + + if (maxlen) { + maxqlen = maxlen / (tech_pvt->read_codec.implementation->microseconds_per_packet / 1000); + } + } + + if (qlen) { + if (maxqlen < qlen) { + maxqlen = qlen * 5; + } + if (switch_rtp_activate_jitter_buffer(tech_pvt->rtp_session, qlen, maxqlen, + tech_pvt->read_codec.implementation->samples_per_packet, + tech_pvt->read_codec.implementation->samples_per_second, max_drift) == SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), + SWITCH_LOG_DEBUG, "Setting Jitterbuffer to %dms (%d frames) (%d max frames) (%d max drift)\n", + len, qlen, maxqlen, max_drift); + switch_channel_set_flag(tech_pvt->channel, CF_JITTERBUFFER); + if (!switch_false(switch_channel_get_variable(tech_pvt->channel, "sip_jitter_buffer_plc"))) { + switch_channel_set_flag(tech_pvt->channel, CF_JITTERBUFFER_PLC); + } + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), + SWITCH_LOG_WARNING, "Error Setting Jitterbuffer to %dms (%d frames)\n", len, qlen); + } + + } else { + switch_rtp_deactivate_jitter_buffer(tech_pvt->rtp_session); + } + } + } + break; + + default: + break; + } +end: + return SWITCH_STATUS_SUCCESS; +} + diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index 7ae868ef62..281aa511da 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -599,6 +599,60 @@ void sofia_handle_sip_i_bye(switch_core_session_t *session, int status, status = 200; phrase = "OK"; + if (sofia_test_flag(tech_pvt, TFLAG_SLA_BARGING)) { + const char *bargee_uuid = switch_channel_get_variable(channel, "sip_barging_uuid"); + switch_core_session_t *bargee_session; + uint32_t ttl = 0; + + if ((bargee_session = switch_core_session_locate(bargee_uuid))) { + //switch_channel_t *bargee_channel = switch_core_session_get_channel(bargee_session); + if ((ttl = switch_core_media_bug_count(bargee_session, "eavesdrop")) == 1) { + if (switch_core_session_check_interface(bargee_session, sofia_endpoint_interface)) { + private_object_t *bargee_tech_pvt = switch_core_session_get_private(bargee_session); + sofia_clear_flag(bargee_tech_pvt, TFLAG_SLA_BARGE); + } + } + switch_core_session_rwunlock(bargee_session); + } + } + + if (sofia_test_flag(tech_pvt, TFLAG_SLA_BARGE)) { + switch_core_session_t *new_session, *other_session; + const char *other_uuid = switch_channel_get_partner_uuid(tech_pvt->channel); + char *cmd = NULL; + + if (!zstr(other_uuid) && (other_session = switch_core_session_locate(other_uuid))) { + switch_channel_t *other_channel = switch_core_session_get_channel(other_session); + + switch_mutex_lock(profile->ireg_mutex); + if (switch_ivr_eavesdrop_pop_eavesdropper(session, &new_session) == SWITCH_STATUS_SUCCESS) { + switch_channel_t *new_channel = switch_core_session_get_channel(new_session); + const char *new_uuid = switch_core_session_get_uuid(new_session); + + + switch_channel_set_variable(new_channel, SWITCH_SIGNAL_BOND_VARIABLE, NULL); + + switch_channel_set_flag(other_channel, CF_REDIRECT); + + switch_channel_set_state(new_channel, CS_RESET); + + switch_ivr_uuid_bridge(new_uuid, other_uuid); + cmd = switch_core_session_sprintf(session, "sleep:500,sofia_sla:%s inline", new_uuid); + + switch_channel_clear_flag(other_channel, CF_REDIRECT); + + switch_core_session_rwunlock(new_session); + } + switch_mutex_unlock(profile->ireg_mutex); + + switch_core_session_rwunlock(other_session); + } + + if (!zstr(cmd)) { + switch_ivr_eavesdrop_exec_all(session, "transfer", cmd); + } + } + sofia_set_flag_locked(tech_pvt, TFLAG_BYE); call_info = switch_channel_get_variable(channel, "presence_call_info_full"); @@ -729,7 +783,7 @@ void sofia_send_callee_id(switch_core_session_t *session, const char *name, cons number = caller_profile->destination_number; } - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) && (session_b = switch_core_session_locate(uuid))) { + if ((uuid = switch_channel_get_partner_uuid(channel)) && (session_b = switch_core_session_locate(uuid))) { switch_core_session_message_t *msg; //switch_channel_t *channel_b = switch_core_session_get_channel(session_b); @@ -852,7 +906,7 @@ void sofia_update_callee_id(switch_core_session_t *session, sofia_profile_t *pro caller_profile->callee_id_number = switch_sanitize_number(switch_core_strdup(caller_profile->pool, number)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s Update Callee ID to \"%s\" <%s>\n", switch_channel_get_name(channel), name, number); - if (lazy || (att && !switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE))) { + if (lazy || (att && !switch_channel_get_partner_uuid(channel))) { switch_channel_flip_cid(channel); } } @@ -860,7 +914,7 @@ void sofia_update_callee_id(switch_core_session_t *session, sofia_profile_t *pro if (send) { if (switch_event_create(&event, SWITCH_EVENT_CALL_UPDATE) == SWITCH_STATUS_SUCCESS) { - const char *uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); + const char *uuid = switch_channel_get_partner_uuid(channel); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Direction", "RECV"); if (uuid) { switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridged-To", uuid); @@ -1228,6 +1282,56 @@ static void our_sofia_event_callback(nua_event_t event, } } +void *SWITCH_THREAD_FUNC sofia_msg_thread_run_once(switch_thread_t *thread, void *obj) +{ + sofia_dispatch_event_t *de = (sofia_dispatch_event_t *) obj; + switch_memory_pool_t *pool = NULL; + + if (de) { + pool = de->pool; + de->pool = NULL; + sofia_process_dispatch_event(&de); + } + + if (pool) { + switch_core_destroy_memory_pool(&pool); + } + + return NULL; +} + +void sofia_process_dispatch_event_in_thread(sofia_dispatch_event_t **dep) +{ + sofia_dispatch_event_t *de = *dep; + switch_threadattr_t *thd_attr = NULL; + switch_memory_pool_t *pool; + switch_thread_t *thread; + sofia_profile_t *profile = (*dep)->profile; + switch_status_t status; + + switch_core_new_memory_pool(&pool); + + + *dep = NULL; + de->pool = pool; + + switch_mutex_lock(profile->ireg_mutex); + switch_threadattr_create(&thd_attr, de->pool); + switch_threadattr_detach_set(thd_attr, 1); + switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); + status = switch_thread_create(&thread, + thd_attr, + sofia_msg_thread_run_once, + de, + de->pool); + switch_mutex_unlock(profile->ireg_mutex); + + if (status != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot create threads!\n"); + sofia_process_dispatch_event(&de); + } +} + void sofia_process_dispatch_event(sofia_dispatch_event_t **dep) { sofia_dispatch_event_t *de = *dep; @@ -1249,42 +1353,56 @@ void sofia_process_dispatch_event(sofia_dispatch_event_t **dep) nua_handle_unref(nh); nua_stack_unref(nua); + switch_os_yield(); } + +static int msg_queue_threads = 0; +//static int count = 0; + void *SWITCH_THREAD_FUNC sofia_msg_thread_run(switch_thread_t *thread, void *obj) { void *pop; switch_queue_t *q = (switch_queue_t *) obj; int my_id; + for (my_id = 0; my_id < mod_sofia_globals.msg_queue_len; my_id++) { - if (mod_sofia_globals.msg_queue[my_id] == q) { + if (mod_sofia_globals.msg_queue_thread[my_id] == thread) { + break; + } + } + + switch_mutex_lock(mod_sofia_globals.mutex); + msg_queue_threads++; + switch_mutex_unlock(mod_sofia_globals.mutex); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "MSG Thread %d Started\n", my_id); + + + for(;;) { + + if (switch_queue_pop(q, &pop) != SWITCH_STATUS_SUCCESS) { + switch_cond_next(); + continue; + } + + if (pop) { + sofia_dispatch_event_t *de = (sofia_dispatch_event_t *) pop; + sofia_process_dispatch_event(&de); + switch_os_yield(); + } else { break; } } - - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "MSG Thread %d Started\n", my_id); - -#ifdef HAVE_CPU_SET_MACROS - { - cpu_set_t set; - CPU_ZERO(&set); - CPU_SET(my_id, &set); - sched_setaffinity(0, sizeof(set), &set); - } -#endif - - - while(switch_queue_pop(q, &pop) == SWITCH_STATUS_SUCCESS && pop) { - sofia_dispatch_event_t *de = (sofia_dispatch_event_t *) pop; - sofia_process_dispatch_event(&de); - switch_cond_next(); - } - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "MSG Thread Ended\n"); + switch_mutex_lock(mod_sofia_globals.mutex); + msg_queue_threads--; + switch_mutex_unlock(mod_sofia_globals.mutex); + return NULL; } @@ -1303,18 +1421,16 @@ void sofia_msg_thread_start(int idx) mod_sofia_globals.msg_queue_len = idx + 1; for (i = 0; i < mod_sofia_globals.msg_queue_len; i++) { - if (!mod_sofia_globals.msg_queue[i]) { + if (!mod_sofia_globals.msg_queue_thread[i]) { switch_threadattr_t *thd_attr = NULL; - switch_queue_create(&mod_sofia_globals.msg_queue[i], SOFIA_MSG_QUEUE_SIZE, mod_sofia_globals.pool); - switch_threadattr_create(&thd_attr, mod_sofia_globals.pool); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_threadattr_priority_increase(thd_attr); switch_thread_create(&mod_sofia_globals.msg_queue_thread[i], thd_attr, sofia_msg_thread_run, - mod_sofia_globals.msg_queue[i], + mod_sofia_globals.msg_queue, mod_sofia_globals.pool); } } @@ -1323,35 +1439,35 @@ void sofia_msg_thread_start(int idx) switch_mutex_unlock(mod_sofia_globals.mutex); } - +//static int foo = 0; static void sofia_queue_message(sofia_dispatch_event_t *de) { - int idx = 0, queued = 0; + int launch = 0; - if (mod_sofia_globals.running == 0 || !mod_sofia_globals.msg_queue[0]) { + if (mod_sofia_globals.running == 0 || !mod_sofia_globals.msg_queue) { sofia_process_dispatch_event(&de); return; } - again: - for (idx = 0; idx < mod_sofia_globals.msg_queue_len; idx++) { - if (switch_queue_trypush(mod_sofia_globals.msg_queue[idx], de) == SWITCH_STATUS_SUCCESS) { - queued++; - break; - } + if (de->profile && sofia_test_pflag(de->profile, PFLAG_THREAD_PER_REG) && de->data->e_event == nua_i_register) { + sofia_process_dispatch_event_in_thread(&de); + return; } - if (!queued) { + if ((switch_queue_size(mod_sofia_globals.msg_queue) > (SOFIA_MSG_QUEUE_SIZE * msg_queue_threads))) { + launch++; + } + + + if (launch) { if (mod_sofia_globals.msg_queue_len < mod_sofia_globals.max_msg_queues) { sofia_msg_thread_start(mod_sofia_globals.msg_queue_len + 1); - goto again; } - - switch_queue_push(mod_sofia_globals.msg_queue[0], de); } - + + switch_queue_push(mod_sofia_globals.msg_queue, de); } @@ -1362,8 +1478,14 @@ void sofia_event_callback(nua_event_t event, tagi_t tags[]) { sofia_dispatch_event_t *de; + int critical = (((SOFIA_MSG_QUEUE_SIZE * mod_sofia_globals.max_msg_queues) * 900) / 1000); + if (switch_queue_size(mod_sofia_globals.msg_queue) > critical) { + nua_respond(nh, 503, "System Busy", SIPTAG_RETRY_AFTER_STR("300"), TAG_END()); + return; + } + if (sofia_test_pflag(profile, PFLAG_STANDBY)) { if (event < nua_r_set_params || event > nua_r_authenticate) { nua_respond(nh, 503, "System Paused", TAG_END()); @@ -1371,6 +1493,7 @@ void sofia_event_callback(nua_event_t event, return; } + switch_mutex_lock(profile->flag_mutex); profile->queued_events++; @@ -1419,8 +1542,8 @@ void sofia_event_callback(nua_event_t event, } } - sofia_queue_message(de); + switch_os_yield(); } @@ -1657,11 +1780,6 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread /* While we're running, or there is a pending sql statment that we haven't appended to sqlbuf yet, because of a lack of buffer space */ while ((mod_sofia_globals.running == 1 && sofia_test_pflag(profile, PFLAG_RUNNING)) || sql) { - if (sofia_test_pflag(profile, PFLAG_STANDBY)) { - switch_yield(1000000); - continue; - } - if (sofia_test_pflag(profile, PFLAG_SQL_IN_TRANS)) { /* Do we have enough statements or is the timeout expired */ while (sql || (sofia_test_pflag(profile, PFLAG_RUNNING) && mod_sofia_globals.running == 1 && @@ -1758,18 +1876,21 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread } } - if (++ireg_loops >= IREG_SECONDS) { - time_t now = switch_epoch_time_now(NULL); - sofia_reg_check_expire(profile, now, 0); - ireg_loops = 0; - } - if (++gateway_loops >= GATEWAY_SECONDS) { - sofia_reg_check_gateway(profile, switch_epoch_time_now(NULL)); - gateway_loops = 0; - } + if (!sofia_test_pflag(profile, PFLAG_STANDBY)) { + if (++ireg_loops >= IREG_SECONDS) { + time_t now = switch_epoch_time_now(NULL); + sofia_reg_check_expire(profile, now, 0); + ireg_loops = 0; + } + + if (++gateway_loops >= GATEWAY_SECONDS) { + sofia_reg_check_gateway(profile, switch_epoch_time_now(NULL)); + gateway_loops = 0; + } - sofia_sub_check_gateway(profile, time(NULL)); + sofia_sub_check_gateway(profile, time(NULL)); + } last_check = switch_micro_time_now(); } @@ -2186,6 +2307,7 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void sofia_glue_del_profile(profile); switch_core_hash_destroy(&profile->chat_hash); + switch_core_hash_destroy(&profile->mwi_debounce_hash); switch_thread_rwlock_unlock(profile->rwlock); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Write unlock %s\n", profile->name); @@ -3068,6 +3190,18 @@ switch_status_t reconfig_sofia(sofia_profile_t *profile) } else { sofia_clear_pflag(profile, PFLAG_LOG_AUTH_FAIL); } + } else if (!strcasecmp(var, "confirm-blind-transfer")) { + if (switch_true(val)) { + sofia_set_pflag(profile, PFLAG_CONFIRM_BLIND_TRANSFER); + } else { + sofia_clear_pflag(profile, PFLAG_CONFIRM_BLIND_TRANSFER); + } + } else if (!strcasecmp(var, "mwi-use-reg-callid")) { + if (switch_true(val)) { + sofia_set_pflag(profile, PFLAG_MWI_USE_REG_CALLID); + } else { + sofia_clear_pflag(profile, PFLAG_MWI_USE_REG_CALLID); + } } else if (!strcasecmp(var, "presence-proto-lookup")) { if (switch_true(val)) { sofia_set_pflag(profile, PFLAG_PRESENCE_MAP); @@ -3187,12 +3321,24 @@ switch_status_t reconfig_sofia(sofia_profile_t *profile) } else { sofia_clear_flag(profile, TFLAG_PROXY_MEDIA); } + } else if (!strcasecmp(var, "inbound-zrtp-passthru")) { + if (switch_true(val)) { + sofia_set_flag(profile, TFLAG_ZRTP_PASSTHRU); + } else { + sofia_clear_flag(profile, TFLAG_ZRTP_PASSTHRU); + } } else if (!strcasecmp(var, "inbound-use-callid-as-uuid")) { if (switch_true(val)) { sofia_set_pflag(profile, PFLAG_CALLID_AS_UUID); } else { sofia_clear_pflag(profile, PFLAG_CALLID_AS_UUID); } + } else if (!strcasecmp(var, "inbound-reg-in-new-thread")) { + if (switch_true(val)) { + sofia_set_pflag(profile, PFLAG_THREAD_PER_REG); + } else { + sofia_clear_pflag(profile, PFLAG_THREAD_PER_REG); + } } else if (!strcasecmp(var, "rtp-autoflush-during-bridge")) { if (switch_true(val)) { sofia_set_pflag(profile, PFLAG_RTP_AUTOFLUSH_DURING_BRIDGE); @@ -3555,6 +3701,10 @@ switch_status_t reconfig_sofia(sofia_profile_t *profile) } } + if (sofia_test_flag(profile, TFLAG_ZRTP_PASSTHRU)) { + sofia_set_flag(profile, TFLAG_LATE_NEGOTIATION); + } + if ((gateways_tag = switch_xml_child(xprofile, "gateways"))) { parse_gateways(profile, gateways_tag); } @@ -3771,6 +3921,7 @@ switch_status_t config_sofia(int reload, char *profile_name) profile->dbname = switch_core_strdup(profile->pool, url); switch_core_hash_init(&profile->chat_hash, profile->pool); + switch_core_hash_init(&profile->mwi_debounce_hash, profile->pool); switch_thread_rwlock_create(&profile->rwlock, profile->pool); switch_mutex_init(&profile->flag_mutex, SWITCH_MUTEX_NESTED, profile->pool); profile->dtmf_duration = 100; @@ -3855,6 +4006,18 @@ switch_status_t config_sofia(int reload, char *profile_name) } else { sofia_clear_pflag(profile, PFLAG_LOG_AUTH_FAIL); } + } else if (!strcasecmp(var, "confirm-blind-transfer")) { + if (switch_true(val)) { + sofia_set_pflag(profile, PFLAG_CONFIRM_BLIND_TRANSFER); + } else { + sofia_clear_pflag(profile, PFLAG_CONFIRM_BLIND_TRANSFER); + } + } else if (!strcasecmp(var, "mwi-use-reg-callid")) { + if (switch_true(val)) { + sofia_set_pflag(profile, PFLAG_MWI_USE_REG_CALLID); + } else { + sofia_clear_pflag(profile, PFLAG_MWI_USE_REG_CALLID); + } } else if (!strcasecmp(var, "presence-proto-lookup")) { if (switch_true(val)) { sofia_set_pflag(profile, PFLAG_PRESENCE_MAP); @@ -4018,6 +4181,8 @@ switch_status_t config_sofia(int reload, char *profile_name) } } else if (!strcasecmp(var, "inbound-proxy-media") && switch_true(val)) { sofia_set_flag(profile, TFLAG_PROXY_MEDIA); + } else if (!strcasecmp(var, "inbound-zrtp-passthru") && switch_true(val)) { + sofia_set_flag(profile, TFLAG_ZRTP_PASSTHRU); } else if (!strcasecmp(var, "force-subscription-expires")) { int tmp = atoi(val); if (tmp > 0) { @@ -4038,6 +4203,12 @@ switch_status_t config_sofia(int reload, char *profile_name) sofia_clear_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER); sofia_clear_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER); } + } else if (!strcasecmp(var, "inbound-reg-in-new-thread")) { + if (switch_true(val)) { + sofia_set_pflag(profile, PFLAG_THREAD_PER_REG); + } else { + sofia_clear_pflag(profile, PFLAG_THREAD_PER_REG); + } } else if (!strcasecmp(var, "inbound-use-callid-as-uuid")) { if (switch_true(val)) { sofia_set_pflag(profile, PFLAG_CALLID_AS_UUID); @@ -4642,6 +4813,10 @@ switch_status_t config_sofia(int reload, char *profile_name) } } + if (sofia_test_flag(profile, TFLAG_ZRTP_PASSTHRU)) { + sofia_set_flag(profile, TFLAG_LATE_NEGOTIATION); + } + if ((!profile->cng_pt) && (!sofia_test_pflag(profile, PFLAG_SUPPRESS_CNG))) { profile->cng_pt = SWITCH_RTP_CNG_PAYLOAD; } @@ -5036,7 +5211,7 @@ static void sofia_handle_sip_r_invite(switch_core_session_t *session, int status goto end; } - if ((br = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE))) { + if ((br = switch_channel_get_partner_uuid(channel))) { switch_xml_t root = NULL, domain = NULL; switch_core_session_t *a_session; switch_channel_t *a_channel; @@ -5195,7 +5370,7 @@ static void sofia_handle_sip_r_invite(switch_core_session_t *session, int status if (sofia_test_flag(tech_pvt, TFLAG_SENT_UPDATE)) { sofia_clear_flag_locked(tech_pvt, TFLAG_SENT_UPDATE); - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) && (other_session = switch_core_session_locate(uuid))) { + if ((uuid = switch_channel_get_partner_uuid(channel)) && (other_session = switch_core_session_locate(uuid))) { const char *r_sdp = NULL; switch_core_session_message_t *msg; private_object_t *other_tech_pvt = switch_core_session_get_private(other_session); @@ -5427,7 +5602,7 @@ void *SWITCH_THREAD_FUNC media_on_hold_thread_run(switch_thread_t *thread, void switch_channel_t *channel = switch_core_session_get_channel(session); private_object_t *tech_pvt = switch_core_session_get_private(session); - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) && (other_session = switch_core_session_locate(uuid))) { + if ((uuid = switch_channel_get_partner_uuid(channel)) && (other_session = switch_core_session_locate(uuid))) { if (switch_core_session_compare(session, other_session)) { sofia_set_flag_locked(tech_pvt, TFLAG_HOLD_LOCK); switch_ivr_media(switch_core_session_get_uuid(other_session), SMF_REBRIDGE); @@ -5882,7 +6057,7 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status, } if ((b_private = nua_handle_magic(bnh))) { - const char *br_b = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); + const char *br_b = switch_channel_get_partner_uuid(channel); char *br_a = b_private->uuid; @@ -6250,31 +6425,30 @@ static void sofia_handle_sip_i_state(switch_core_session_t *session, int status, switch_core_session_rwunlock(other_session); } } else { - uint8_t match = 0; - int is_ok = 1; + uint8_t match = 0; + int is_ok = 1; + if (tech_pvt->num_codecs) { + match = sofia_glue_negotiate_sdp(session, r_sdp); + } - if (tech_pvt->num_codecs) { - match = sofia_glue_negotiate_sdp(session, r_sdp); - } - - if (match) { - sofia_set_flag_locked(tech_pvt, TFLAG_REINVITE); - if (sofia_glue_activate_rtp(tech_pvt, 0) != SWITCH_STATUS_SUCCESS) { - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "RTP Error!\n"); - switch_channel_set_variable(tech_pvt->channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "RTP ERROR"); + if (match) { + sofia_set_flag_locked(tech_pvt, TFLAG_REINVITE); + if (sofia_glue_activate_rtp(tech_pvt, 0) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "RTP Error!\n"); + switch_channel_set_variable(tech_pvt->channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "RTP ERROR"); + is_ok = 0; + } + sofia_clear_flag_locked(tech_pvt, TFLAG_REINVITE); + } else { + switch_channel_set_variable(tech_pvt->channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "CODEC NEGOTIATION ERROR"); is_ok = 0; } - sofia_clear_flag_locked(tech_pvt, TFLAG_REINVITE); - } else { - switch_channel_set_variable(tech_pvt->channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "CODEC NEGOTIATION ERROR"); - is_ok = 0; - } - if (!is_ok) { - nua_respond(nh, SIP_488_NOT_ACCEPTABLE, TAG_END()); - switch_channel_hangup(tech_pvt->channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION); - } + if (!is_ok) { + nua_respond(nh, SIP_488_NOT_ACCEPTABLE, TAG_END()); + switch_channel_hangup(tech_pvt->channel, SWITCH_CAUSE_INCOMPATIBLE_DESTINATION); + } } goto done; } @@ -6623,6 +6797,9 @@ void sofia_handle_sip_i_refer(nua_t *nua, sofia_profile_t *profile, nua_handle_t nua_respond(nh, SIP_202_ACCEPTED, NUTAG_WITH_THIS_MSG(de->data->e_msg), SIPTAG_EXPIRES_STR("60"), TAG_END()); + + switch_channel_set_variable(tech_pvt->channel, SOFIA_REPLACES_HEADER, NULL); + if (sip->sip_referred_by) { full_ref_by = sip_header_as_string(home, (void *) sip->sip_referred_by); } @@ -6685,8 +6862,8 @@ void sofia_handle_sip_i_refer(nua_t *nua, sofia_profile_t *profile, nua_handle_t b_tech_pvt = (private_object_t *) switch_core_session_get_private(b_session); channel_b = switch_core_session_get_channel(b_session); - br_a = switch_channel_get_variable(channel_a, SWITCH_SIGNAL_BOND_VARIABLE); - br_b = switch_channel_get_variable(channel_b, SWITCH_SIGNAL_BOND_VARIABLE); + br_a = switch_channel_get_partner_uuid(channel_a); + br_b = switch_channel_get_partner_uuid(channel_b); if (!switch_ivr_uuid_exists(br_a)) { br_a = NULL; @@ -6935,7 +7112,7 @@ void sofia_handle_sip_i_refer(nua_t *nua, sofia_profile_t *profile, nua_handle_t } nua_handle_unref(bnh); } else { /* the other channel is on a different box, we have to go find them */ - if (exten && (br_a = switch_channel_get_variable(channel_a, SWITCH_SIGNAL_BOND_VARIABLE))) { + if (exten && (br_a = switch_channel_get_partner_uuid(channel_a))) { switch_core_session_t *a_session; switch_channel_t *channel = switch_core_session_get_channel(session); @@ -7056,37 +7233,54 @@ void sofia_handle_sip_i_refer(nua_t *nua, sofia_profile_t *profile, nua_handle_t if (exten) { switch_channel_t *channel = switch_core_session_get_channel(session); const char *br; + switch_core_session_t *b_session; - if ((br = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE))) { - switch_core_session_t *b_session; + if ((br = switch_channel_get_partner_uuid(channel)) && (b_session = switch_core_session_locate(br))) { - if ((b_session = switch_core_session_locate(br))) { - switch_channel_t *b_channel = switch_core_session_get_channel(b_session); - switch_channel_set_variable(channel, "transfer_fallback_extension", from->a_user); - if (!zstr(full_ref_by)) { - switch_channel_set_variable(b_channel, SOFIA_SIP_HEADER_PREFIX "Referred-By", full_ref_by); - } - if (!zstr(full_ref_to)) { - switch_channel_set_variable(b_channel, SOFIA_REFER_TO_VARIABLE, full_ref_to); - } + const char *var; + switch_channel_t *b_channel = switch_core_session_get_channel(b_session); - if (switch_true(switch_channel_get_variable(channel, "recording_follow_transfer"))) { - switch_core_media_bug_transfer_recordings(session, b_session); - } + switch_channel_set_variable(channel, "transfer_fallback_extension", from->a_user); + if (!zstr(full_ref_by)) { + switch_channel_set_variable(b_channel, SOFIA_SIP_HEADER_PREFIX "Referred-By", full_ref_by); + } - switch_ivr_session_transfer(b_session, exten, NULL, NULL); - switch_core_session_rwunlock(b_session); + if (!zstr(full_ref_to)) { + switch_channel_set_variable(b_channel, SOFIA_REFER_TO_VARIABLE, full_ref_to); + } + + if (switch_true(switch_channel_get_variable(channel, "recording_follow_transfer"))) { + switch_core_media_bug_transfer_recordings(session, b_session); } switch_channel_set_variable(channel, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "BLIND_TRANSFER"); - nua_notify(tech_pvt->nh, NUTAG_NEWSUB(1), SIPTAG_CONTENT_TYPE_STR("message/sipfrag;version=2.0"), - NUTAG_SUBSTATE(nua_substate_terminated),SIPTAG_SUBSCRIPTION_STATE_STR("terminated;reason=noresource"), SIPTAG_PAYLOAD_STR("SIP/2.0 200 OK\r\n"), SIPTAG_EVENT_STR(etmp), TAG_END()); + + if (((var = switch_channel_get_variable(channel, "confirm_blind_transfer")) && switch_true(var)) || + sofia_test_pflag(profile, PFLAG_CONFIRM_BLIND_TRANSFER)) { + switch_channel_set_state_flag(b_channel, CF_CONFIRM_BLIND_TRANSFER); + switch_channel_set_variable(channel, "sip_blind_transfer_event", etmp); + switch_channel_set_variable(b_channel, "blind_transfer_uuid", switch_core_session_get_uuid(session)); + switch_channel_set_variable(channel, "blind_transfer_uuid", switch_core_session_get_uuid(b_session)); + + switch_channel_set_variable(channel, "park_timeout", "600:blind_transfer"); + switch_channel_set_state(channel, CS_PARK); + } else { + nua_notify(tech_pvt->nh, NUTAG_NEWSUB(1), SIPTAG_CONTENT_TYPE_STR("message/sipfrag;version=2.0"), + NUTAG_SUBSTATE(nua_substate_terminated), + SIPTAG_SUBSCRIPTION_STATE_STR("terminated;reason=noresource"), + SIPTAG_PAYLOAD_STR("SIP/2.0 200 OK\r\n"), SIPTAG_EVENT_STR(etmp), TAG_END()); + } + + switch_ivr_session_transfer(b_session, exten, NULL, NULL); + switch_core_session_rwunlock(b_session); } else { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot Blind Transfer 1 Legged calls\n"); switch_channel_set_variable(channel_a, SWITCH_ENDPOINT_DISPOSITION_VARIABLE, "ATTENDED_TRANSFER_ERROR"); nua_notify(tech_pvt->nh, NUTAG_NEWSUB(1), SIPTAG_CONTENT_TYPE_STR("message/sipfrag;version=2.0"), - NUTAG_SUBSTATE(nua_substate_terminated),SIPTAG_SUBSCRIPTION_STATE_STR("terminated;reason=noresource"), SIPTAG_PAYLOAD_STR("SIP/2.0 403 Forbidden\r\n"), SIPTAG_EVENT_STR(etmp), TAG_END()); + NUTAG_SUBSTATE(nua_substate_terminated), + SIPTAG_SUBSCRIPTION_STATE_STR("terminated;reason=noresource"), + SIPTAG_PAYLOAD_STR("SIP/2.0 403 Forbidden\r\n"), SIPTAG_EVENT_STR(etmp), TAG_END()); } } @@ -7363,7 +7557,7 @@ void sofia_handle_sip_i_info(nua_t *nua, sofia_profile_t *profile, nua_handle_t const char *uuid; switch_core_session_t *session_b; - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) && (session_b = switch_core_session_locate(uuid))) { + if ((uuid = switch_channel_get_partner_uuid(channel)) && (session_b = switch_core_session_locate(uuid))) { while (switch_channel_has_dtmf(channel)) { switch_dtmf_t idtmf = { 0, 0 }; if (switch_channel_dequeue_dtmf(channel, &idtmf) == SWITCH_STATUS_SUCCESS) { @@ -8089,6 +8283,10 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ switch_channel_set_flag(channel, CF_PROXY_MEDIA); } + if (sofia_test_flag(tech_pvt, TFLAG_ZRTP_PASSTHRU)) { + switch_channel_set_flag(channel, CF_ZRTP_PASSTHRU_REQ); + } + if (!tech_pvt->call_id && sip->sip_call_id && sip->sip_call_id->i_id) { tech_pvt->call_id = switch_core_session_strdup(session, sip->sip_call_id->i_id); switch_channel_set_variable(channel, "sip_call_id", tech_pvt->call_id); @@ -8353,11 +8551,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ if ((b_session = switch_core_session_locate(b_private->uuid))) { switch_channel_t *b_channel = switch_core_session_get_channel(b_session); - const char *uuid; - int one_leg = 1; - private_object_t *b_tech_pvt = NULL; - const char *app = switch_channel_get_variable(b_channel, SWITCH_CURRENT_APPLICATION_VARIABLE); - const char *data = switch_channel_get_variable(b_channel, SWITCH_CURRENT_APPLICATION_DATA_VARIABLE); + const char *bridge_uuid; switch_caller_profile_t *orig_cp; const char *sent_name, *sent_number; orig_cp = switch_channel_get_caller_profile(b_channel); @@ -8382,77 +8576,58 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Setting NAT mode based on %s\n", is_nat); } + tech_pvt->caller_profile->dialplan = "inline"; - if (app && data && !strcasecmp(app, "conference")) { - tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, "answer,conference:%s+flags{dist-dtmf}", data); - tech_pvt->caller_profile->dialplan = "inline"; - } else { - if (switch_core_session_check_interface(b_session, sofia_endpoint_interface)) { - b_tech_pvt = switch_core_session_get_private(b_session); - } + bridge_uuid = switch_channel_get_partner_uuid(b_channel); - if ((uuid = switch_channel_get_variable(b_channel, SWITCH_SIGNAL_BOND_VARIABLE))) { - one_leg = 0; + if (call_info) { + const char *olu; + switch_core_session_t *os; + switch_codec_implementation_t read_impl = { 0 }; + char *codec_str = ""; + + if (!zstr(bridge_uuid) && switch_channel_test_flag(b_channel, CF_LEG_HOLDING)) { + olu = bridge_uuid; } else { - uuid = switch_core_session_get_uuid(b_session); + olu = b_private->uuid; } + + if ((os = switch_core_session_locate(olu))) { + switch_core_session_get_real_read_impl(os, &read_impl); + switch_core_session_rwunlock(os); - if (uuid) { - switch_core_session_t *c_session = NULL; - int do_conf = 0; - const char *c_app = NULL; - const char *c_data = NULL; + codec_str = switch_core_session_sprintf(session, "set:absolute_codec_string=%s@%di,", read_impl.iananame, + read_impl.microseconds_per_packet / 1000); + } + + + + if (!zstr(bridge_uuid) && switch_channel_test_flag(b_channel, CF_LEG_HOLDING)) { + tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, + "%sanswer,intercept:%s", codec_str, bridge_uuid); + } else { + tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, + "%sanswer,sofia_sla:%s", codec_str, b_private->uuid); + } + } else { + if (!zstr(bridge_uuid)) { + switch_channel_mark_hold(b_channel, SWITCH_FALSE); + tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, "answer,intercept:%s", bridge_uuid); + } else { + const char *b_app = switch_channel_get_variable(b_channel, SWITCH_CURRENT_APPLICATION_VARIABLE); + const char *b_data = switch_channel_get_variable(b_channel, SWITCH_CURRENT_APPLICATION_DATA_VARIABLE); - uuid = switch_core_session_strdup(b_session, uuid); - - if (call_info && (c_session = switch_core_session_locate(uuid))) { - switch_channel_t *c_channel = switch_core_session_get_channel(c_session); - private_object_t *c_tech_pvt = NULL; - - c_app = switch_channel_get_variable(c_channel, SWITCH_CURRENT_APPLICATION_VARIABLE); - c_data = switch_channel_get_variable(c_channel, SWITCH_CURRENT_APPLICATION_DATA_VARIABLE); - - if (switch_core_session_check_interface(c_session, sofia_endpoint_interface)) { - c_tech_pvt = switch_core_session_get_private(c_session); - } + if (b_data && b_app) { + tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, "answer,%s:%s", b_app, b_data); + } else if (b_app) { + tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, "answer,%s", b_app); + } - if (!one_leg && - (!b_tech_pvt || !sofia_test_flag(b_tech_pvt, TFLAG_SIP_HOLD)) && - (!c_tech_pvt || !sofia_test_flag(c_tech_pvt, TFLAG_SIP_HOLD))) { - char *ext = switch_core_session_sprintf(session, "answer,conference:%s@sla+flags{mintwo|dist-dtmf}", uuid); - - switch_channel_set_flag(c_channel, CF_REDIRECT); - switch_ivr_session_transfer(b_session, ext, "inline", NULL); - switch_ivr_session_transfer(c_session, ext, "inline", NULL); - switch_channel_clear_flag(c_channel, CF_REDIRECT); - do_conf = 1; - } - switch_core_session_rwunlock(c_session); - } - - if (do_conf) { - tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, - "answer,conference:%s@sla+flags{mintwo|dist-dtmf}", uuid); - } else { - if (one_leg && c_app) { - if (c_data) { - tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, - "answer,%s:%s", c_app, c_data); - } else { - tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, - "answer,%s", c_app); - } - } else { - switch_channel_mark_hold(b_channel, SWITCH_FALSE); - tech_pvt->caller_profile->destination_number = switch_core_sprintf(tech_pvt->caller_profile->pool, - "answer,intercept:%s", uuid); - } - } - - tech_pvt->caller_profile->dialplan = "inline"; + switch_channel_hangup(b_channel, SWITCH_CAUSE_ATTENDED_TRANSFER); } } + switch_core_session_rwunlock(b_session); } } diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c index 6610895927..2cb2fdd551 100644 --- a/src/mod/endpoints/mod_sofia/sofia_glue.c +++ b/src/mod/endpoints/mod_sofia/sofia_glue.c @@ -184,7 +184,6 @@ static void generate_m(private_object_t *tech_pvt, char *buf, size_t buflen, int rate; int already_did[128] = { 0 }; int ptime = 0, noptime = 0; - const char *zrtp; switch_snprintf(buf + strlen(buf), buflen - strlen(buf), "m=audio %d RTP/%sAVP", port, secure ? "S" : ""); @@ -344,8 +343,11 @@ static void generate_m(private_object_t *tech_pvt, char *buf, size_t buflen, switch_snprintf(buf + strlen(buf), buflen - strlen(buf), "a=ptime:%d\n", cur_ptime); } - if ((zrtp = switch_channel_get_variable(tech_pvt->channel, "sdp_zrtp_hash_string"))) { - switch_snprintf(buf + strlen(buf), buflen - strlen(buf), "a=zrtp-hash:%s\n", zrtp); + if (tech_pvt->local_sdp_audio_zrtp_hash) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Adding audio a=zrtp-hash:%s\n", + tech_pvt->local_sdp_audio_zrtp_hash); + switch_snprintf(buf + strlen(buf), buflen - strlen(buf), "a=zrtp-hash:%s\n", + tech_pvt->local_sdp_audio_zrtp_hash); } if (sr) { @@ -391,7 +393,6 @@ void sofia_glue_set_local_sdp(private_object_t *tech_pvt, const char *ip, switch switch_event_t *map = NULL, *ptmap = NULL; const char *b_sdp = NULL; int verbose_sdp = 0; - const char *zrtp; sofia_glue_check_dtmf_type(tech_pvt); @@ -545,8 +546,12 @@ void sofia_glue_set_local_sdp(private_object_t *tech_pvt, const char *ip, switch switch_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "a=ptime:%d\n", ptime); } - if ((zrtp = switch_channel_get_variable(tech_pvt->channel, "sdp_zrtp_hash_string"))) { - switch_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "a=zrtp-hash:%s\n", zrtp); + + if (tech_pvt->local_sdp_audio_zrtp_hash) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Adding audio a=zrtp-hash:%s\n", + tech_pvt->local_sdp_audio_zrtp_hash); + switch_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "a=zrtp-hash:%s\n", + tech_pvt->local_sdp_audio_zrtp_hash); } if (sr) { @@ -683,7 +688,7 @@ void sofia_glue_set_local_sdp(private_object_t *tech_pvt, const char *ip, switch pass_fmtp = NULL; - if (switch_channel_get_variable(tech_pvt->channel, SWITCH_SIGNAL_BOND_VARIABLE)) { + if (switch_channel_get_partner_uuid(tech_pvt->channel)) { if ((of = switch_channel_get_variable_partner(tech_pvt->channel, "sip_video_fmtp"))) { pass_fmtp = of; } @@ -747,6 +752,13 @@ void sofia_glue_set_local_sdp(private_object_t *tech_pvt, const char *ip, switch } } + + if (tech_pvt->local_sdp_video_zrtp_hash) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Adding video a=zrtp-hash:%s\n", + tech_pvt->local_sdp_video_zrtp_hash); + switch_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "a=zrtp-hash:%s\n", + tech_pvt->local_sdp_video_zrtp_hash); + } } } @@ -1739,7 +1751,7 @@ void sofia_glue_tech_patch_sdp(private_object_t *tech_pvt) } - } else if ((!strncmp("m=audio ", p, 8) && *(p + 9) != '0') || (!strncmp("m=image ", p, 8) && *(p + 9) != '0')) { + } else if ((!strncmp("m=audio ", p, 8) && *(p + 8) != '0') || (!strncmp("m=image ", p, 8) && *(p + 8) != '0')) { strncpy(q, p, 8); p += 8; @@ -1775,7 +1787,7 @@ void sofia_glue_tech_patch_sdp(private_object_t *tech_pvt) has_audio++; - } else if (!strncmp("m=video ", p, 8) && *(p + 9) != '0') { + } else if (!strncmp("m=video ", p, 8) && *(p + 8) != '0') { if (!has_video) { sofia_glue_tech_choose_video_port(tech_pvt, 1); tech_pvt->video_rm_encoding = "PROXY-VID"; @@ -2075,7 +2087,9 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session) } - rep = switch_channel_get_variable(channel, SOFIA_REPLACES_HEADER); + if ((rep = switch_channel_get_variable(channel, SOFIA_REPLACES_HEADER))) { + switch_channel_set_variable(channel, SOFIA_REPLACES_HEADER, NULL); + } switch_assert(tech_pvt != NULL); @@ -3532,7 +3546,7 @@ switch_status_t sofia_glue_activate_rtp(private_object_t *tech_pvt, switch_rtp_f if (((val = switch_channel_get_variable(tech_pvt->channel, "rtp_digit_delay")))) { int delayi = atoi(val); if (delayi < 0) delayi = 0; - delay = (uint32_t) delay; + delay = (uint32_t) delayi; } @@ -3563,9 +3577,11 @@ switch_status_t sofia_glue_activate_rtp(private_object_t *tech_pvt, switch_rtp_f switch_channel_set_variable(tech_pvt->channel, SWITCH_REMOTE_MEDIA_PORT_VARIABLE, tmp); - if (switch_channel_test_flag(tech_pvt->channel, CF_ZRTP_PASS)) { + if (switch_channel_test_flag(tech_pvt->channel, CF_ZRTP_PASSTHRU)) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_INFO, "Activating ZRTP PROXY MODE\n"); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Disable NOTIMER_DURING_BRIDGE\n"); sofia_clear_flag(tech_pvt, TFLAG_NOTIMER_DURING_BRIDGE); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Activating audio UDPTL mode\n"); switch_rtp_udptl_mode(tech_pvt->rtp_session); } @@ -3752,7 +3768,10 @@ switch_status_t sofia_glue_activate_rtp(private_object_t *tech_pvt, switch_rtp_f } } } - + if (switch_channel_test_flag(tech_pvt->channel, CF_ZRTP_PASSTHRU)) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_DEBUG, "Activating video UDPTL mode\n"); + switch_rtp_udptl_mode(tech_pvt->video_rtp_session); + } } else { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(tech_pvt->session), SWITCH_LOG_ERROR, "VIDEO RTP REPORTS ERROR: [%s]\n", switch_str_nil(err)); @@ -3837,7 +3856,93 @@ static void add_audio_codec(sdp_rtpmap_t *map, int ptime, char *buf, switch_size } +void sofia_glue_pass_zrtp_hash2(switch_core_session_t *aleg_session, switch_core_session_t *bleg_session) +{ + switch_channel_t *aleg_channel = switch_core_session_get_channel(aleg_session); + private_object_t *aleg_tech_pvt = switch_core_session_get_private(aleg_session); + switch_channel_t *bleg_channel = switch_core_session_get_channel(bleg_session); + private_object_t *bleg_tech_pvt = switch_core_session_get_private(bleg_session); + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(aleg_channel), SWITCH_LOG_DEBUG, "Deciding whether to pass zrtp-hash between a-leg and b-leg\n"); + if (!(switch_channel_test_flag(aleg_tech_pvt->channel, CF_ZRTP_PASSTHRU_REQ))) { + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(aleg_channel), SWITCH_LOG_DEBUG, "CF_ZRTP_PASSTHRU_REQ not set on a-leg, so not propagating zrtp-hash\n"); + return; + } + if (aleg_tech_pvt->remote_sdp_audio_zrtp_hash) { + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(aleg_channel), SWITCH_LOG_DEBUG, "Passing a-leg remote zrtp-hash (audio) to b-leg\n"); + bleg_tech_pvt->local_sdp_audio_zrtp_hash = switch_core_session_strdup(bleg_tech_pvt->session, aleg_tech_pvt->remote_sdp_audio_zrtp_hash); + switch_channel_set_variable(bleg_channel, "l_sdp_audio_zrtp_hash", bleg_tech_pvt->local_sdp_audio_zrtp_hash); + } + if (aleg_tech_pvt->remote_sdp_video_zrtp_hash) { + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(aleg_channel), SWITCH_LOG_DEBUG, "Passing a-leg remote zrtp-hash (video) to b-leg\n"); + bleg_tech_pvt->local_sdp_video_zrtp_hash = switch_core_session_strdup(bleg_tech_pvt->session, aleg_tech_pvt->remote_sdp_video_zrtp_hash); + switch_channel_set_variable(bleg_channel, "l_sdp_video_zrtp_hash", bleg_tech_pvt->local_sdp_video_zrtp_hash); + } + if (bleg_tech_pvt->remote_sdp_audio_zrtp_hash) { + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(aleg_channel), SWITCH_LOG_DEBUG, "Passing b-leg remote zrtp-hash (audio) to a-leg\n"); + aleg_tech_pvt->local_sdp_audio_zrtp_hash = switch_core_session_strdup(aleg_tech_pvt->session, bleg_tech_pvt->remote_sdp_audio_zrtp_hash); + switch_channel_set_variable(aleg_channel, "l_sdp_audio_zrtp_hash", aleg_tech_pvt->local_sdp_audio_zrtp_hash); + } + if (bleg_tech_pvt->remote_sdp_video_zrtp_hash) { + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(aleg_channel), SWITCH_LOG_DEBUG, "Passing b-leg remote zrtp-hash (video) to a-leg\n"); + aleg_tech_pvt->local_sdp_video_zrtp_hash = switch_core_session_strdup(aleg_tech_pvt->session, bleg_tech_pvt->remote_sdp_video_zrtp_hash); + switch_channel_set_variable(aleg_channel, "l_sdp_video_zrtp_hash", aleg_tech_pvt->local_sdp_video_zrtp_hash); + } +} +void sofia_glue_pass_zrtp_hash(switch_core_session_t *session) +{ + switch_channel_t *channel = switch_core_session_get_channel(session); + private_object_t *tech_pvt = switch_core_session_get_private(session); + switch_core_session_t *other_session; + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "Deciding whether to pass zrtp-hash between legs\n"); + if (!(switch_channel_test_flag(tech_pvt->channel, CF_ZRTP_PASSTHRU_REQ))) { + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "CF_ZRTP_PASSTHRU_REQ not set, so not propagating zrtp-hash\n"); + return; + } else if (!(switch_core_session_get_partner(session, &other_session) == SWITCH_STATUS_SUCCESS)) { + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "No partner channel found, so not propagating zrtp-hash\n"); + return; + } else { + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "Found peer channel; propagating zrtp-hash if set\n"); + sofia_glue_pass_zrtp_hash2(session, other_session); + switch_core_session_rwunlock(other_session); + } +} + +static void find_zrtp_hash(switch_core_session_t *session, sdp_session_t *sdp) +{ + switch_channel_t *channel = switch_core_session_get_channel(session); + private_object_t *tech_pvt = switch_core_session_get_private(session); + sdp_media_t *m; + sdp_attribute_t *attr; + int got_audio = 0, got_video = 0; + + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "Looking for zrtp-hash\n"); + for (m = sdp->sdp_media; m; m = m->m_next) { + if (got_audio && got_video) break; + if (m->m_port && ((m->m_type == sdp_media_audio && !got_audio) + || (m->m_type == sdp_media_video && !got_video))) { + for (attr = m->m_attributes; attr; attr = attr->a_next) { + if (zstr(attr->a_name)) continue; + if (strcasecmp(attr->a_name, "zrtp-hash") || !(attr->a_value)) continue; + if (m->m_type == sdp_media_audio) { + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, + "Found audio zrtp-hash; setting r_sdp_audio_zrtp_hash=%s\n", attr->a_value); + switch_channel_set_variable(channel, "r_sdp_audio_zrtp_hash", attr->a_value); + tech_pvt->remote_sdp_audio_zrtp_hash = switch_core_session_strdup(tech_pvt->session, attr->a_value); + got_audio++; + } else if (m->m_type == sdp_media_video) { + switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, + "Found video zrtp-hash; setting r_sdp_video_zrtp_hash=%s\n", attr->a_value); + switch_channel_set_variable(channel, "r_sdp_video_zrtp_hash", attr->a_value); + tech_pvt->remote_sdp_video_zrtp_hash = switch_core_session_strdup(tech_pvt->session, attr->a_value); + got_video++; + } + switch_channel_set_flag(channel, CF_ZRTP_HASH); + break; + } + } + } +} void sofia_glue_set_r_sdp_codec_string(switch_core_session_t *session, const char *codec_string, sdp_session_t *sdp) { @@ -3887,18 +3992,8 @@ void sofia_glue_set_r_sdp_codec_string(switch_core_session_t *session, const cha } } - switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "Looking for zrtp-hash to set sdp_zrtp_hash_string\n"); - for (m = sdp->sdp_media; m; m = m->m_next) { - for (attr = m->m_attributes; attr; attr = attr->a_next) { - if (zstr(attr->a_name)) continue; - if (!strcasecmp(attr->a_name, "zrtp-hash") && attr->a_value) { - switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_DEBUG, "Found zrtp-hash, setting sdp_zrtp_hash_string=%s\n", attr->a_value); - switch_channel_set_variable(channel, "sdp_zrtp_hash_string", attr->a_value); - switch_channel_set_flag(channel, CF_ZRTP_HASH); - break; - } - } - } + find_zrtp_hash(session, sdp); + sofia_glue_pass_zrtp_hash(session); for (m = sdp->sdp_media; m; m = m->m_next) { ptime = dptime; @@ -4061,6 +4156,10 @@ int sofia_glue_toggle_hold(private_object_t *tech_pvt, int sendonly) { int changed = 0; + if (sofia_test_flag(tech_pvt, TFLAG_SLA_BARGE) || sofia_test_flag(tech_pvt, TFLAG_SLA_BARGING)) { + return 0; + } + if (sendonly && switch_channel_test_flag(tech_pvt->channel, CF_ANSWERED)) { if (!sofia_test_flag(tech_pvt, TFLAG_SIP_HOLD)) { const char *stream; @@ -4092,9 +4191,9 @@ int sofia_glue_toggle_hold(private_object_t *tech_pvt, int sendonly) if (!strcasecmp(stream, "indicate_hold")) { switch_channel_set_flag(tech_pvt->channel, CF_SUSPEND); switch_channel_set_flag(tech_pvt->channel, CF_HOLD); - switch_ivr_hold_uuid(switch_channel_get_variable(tech_pvt->channel, SWITCH_SIGNAL_BOND_VARIABLE), NULL, 0); + switch_ivr_hold_uuid(switch_channel_get_partner_uuid(tech_pvt->channel), NULL, 0); } else { - switch_ivr_broadcast(switch_channel_get_variable(tech_pvt->channel, SWITCH_SIGNAL_BOND_VARIABLE), stream, + switch_ivr_broadcast(switch_channel_get_partner_uuid(tech_pvt->channel), stream, SMF_ECHO_ALEG | SMF_LOOP | SMF_PRIORITY); switch_yield(250000); } @@ -4120,7 +4219,7 @@ int sofia_glue_toggle_hold(private_object_t *tech_pvt, int sendonly) switch_rtp_set_max_missed_packets(tech_pvt->rtp_session, tech_pvt->max_missed_packets); } - if ((uuid = switch_channel_get_variable(tech_pvt->channel, SWITCH_SIGNAL_BOND_VARIABLE)) && (b_session = switch_core_session_locate(uuid))) { + if ((uuid = switch_channel_get_partner_uuid(tech_pvt->channel)) && (b_session = switch_core_session_locate(uuid))) { switch_channel_t *b_channel = switch_core_session_get_channel(b_session); if (switch_channel_test_flag(tech_pvt->channel, CF_HOLD)) { @@ -4574,6 +4673,9 @@ uint8_t sofia_glue_negotiate_sdp(switch_core_session_t *session, const char *r_s switch_channel_set_variable(tech_pvt->channel, "t38_broken_boolean", "true"); } + find_zrtp_hash(session, sdp); + sofia_glue_pass_zrtp_hash(session); + for (m = sdp->sdp_media; m; m = m->m_next) { sdp_connection_t *connection; switch_core_session_t *other_session; @@ -4695,9 +4797,6 @@ uint8_t sofia_glue_negotiate_sdp(switch_core_session_t *session, const char *r_s ptime = atoi(attr->a_value); } else if (!strcasecmp(attr->a_name, "maxptime") && attr->a_value) { maxptime = atoi(attr->a_value); - } else if (!strcasecmp(attr->a_name, "zrtp-hash") && attr->a_value) { - switch_channel_set_variable(tech_pvt->channel, "sdp_zrtp_hash_string", attr->a_value); - switch_channel_set_flag(tech_pvt->channel, CF_ZRTP_HASH); } else if (!got_crypto && !strcasecmp(attr->a_name, "crypto") && !zstr(attr->a_value)) { int crypto_tag; @@ -5233,7 +5332,7 @@ void sofia_glue_pass_sdp(private_object_t *tech_pvt, char *sdp) switch_core_session_t *other_session; switch_channel_t *other_channel; - if ((val = switch_channel_get_variable(tech_pvt->channel, SWITCH_SIGNAL_BOND_VARIABLE)) + if ((val = switch_channel_get_partner_uuid(tech_pvt->channel)) && (other_session = switch_core_session_locate(val))) { other_channel = switch_core_session_get_channel(other_session); switch_channel_set_variable(other_channel, SWITCH_B_SDP_VARIABLE, sdp); @@ -5807,7 +5906,7 @@ static int recover_callback(void *pArg, int argc, char **argv, char **columnName } - if (switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) { + if (switch_channel_get_partner_uuid(channel)) { sofia_set_flag(tech_pvt, TFLAG_RECOVERING_BRIDGE); } else { switch_xml_t callflow, param, x_extension; @@ -5854,6 +5953,7 @@ int sofia_glue_recover(switch_bool_t flush) for (m = matches->head; m; m = m->next) { if ((profile = sofia_glue_find_profile(m->val))) { r += sofia_glue_profile_recover(profile, flush); + sofia_glue_release_profile(profile); } } switch_console_free_matches(&matches); @@ -6639,7 +6739,7 @@ void sofia_glue_free_destination(sofia_destination_t *dst) } switch_status_t sofia_glue_send_notify(sofia_profile_t *profile, const char *user, const char *host, const char *event, const char *contenttype, - const char *body, const char *o_contact, const char *network_ip) + const char *body, const char *o_contact, const char *network_ip, const char *call_id) { char *id = NULL; nua_handle_t *nh; @@ -6702,6 +6802,7 @@ switch_status_t sofia_glue_send_notify(sofia_profile_t *profile, const char *use TAG_IF(user_via, SIPTAG_VIA_STR(user_via)), SIPTAG_SUBSCRIPTION_STATE_STR("terminated;reason=noresource"), TAG_IF(event, SIPTAG_EVENT_STR(event)), + TAG_IF(call_id, SIPTAG_CALL_ID_STR(call_id)), TAG_IF(contenttype, SIPTAG_CONTENT_TYPE_STR(contenttype)), TAG_IF(body, SIPTAG_PAYLOAD_STR(body)), TAG_END()); switch_safe_free(contact); @@ -6727,7 +6828,7 @@ void sofia_glue_tech_simplify(private_object_t *tech_pvt) - if ((uuid = switch_channel_get_variable(tech_pvt->channel, SWITCH_SIGNAL_BOND_VARIABLE)) + if ((uuid = switch_channel_get_partner_uuid(tech_pvt->channel)) && (other_session = switch_core_session_locate(uuid))) { other_channel = switch_core_session_get_channel(other_session); diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c index 6befca3439..23ae017531 100644 --- a/src/mod/endpoints/mod_sofia/sofia_presence.c +++ b/src/mod/endpoints/mod_sofia/sofia_presence.c @@ -90,6 +90,9 @@ struct presence_helper { switch_event_t *event; switch_stream_handle_t stream; char last_uuid[512]; + int hup; + int calls_up; + }; switch_status_t sofia_presence_chat_send(switch_event_t *message_event) @@ -555,11 +558,11 @@ static void actual_sofia_presence_mwi_event_handler(switch_event_t *event) } if (for_everyone) { - sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,network_ip,'%q' " + sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,network_ip,'%q',call_id " "from sip_registrations where hostname='%q' and profile_name='%q' and mwi_user='%q' and mwi_host='%q'", stream.data, mod_sofia_globals.hostname, profile->name, user, host); } else if (call_id) { - sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,network_ip,'%q' " + sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,network_ip,'%q',call_id " "from sip_registrations where hostname='%q' and profile_name='%q' and call_id='%q'", stream.data, mod_sofia_globals.hostname, profile->name, call_id); } @@ -1112,6 +1115,7 @@ static void actual_sofia_presence_event_handler(switch_event_t *event) "sip_user='%q' and sip_host='%q'", rpid, status, mod_sofia_globals.hostname, profile->name, euser, host); sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE); + proto = SOFIA_CHAT_PROTO; } if (zstr(uuid)) { @@ -1167,11 +1171,7 @@ static void actual_sofia_presence_event_handler(switch_event_t *event) } } #endif - - if (hup && dh.hits > 0) { - goto done; - } - + if (zstr(call_id) && (dh.hits && presence_source && (!strcasecmp(presence_source, "register") || switch_stristr("register", status)))) { goto done; @@ -1255,88 +1255,90 @@ static void actual_sofia_presence_event_handler(switch_event_t *event) switch_str_nil(status), switch_str_nil(rpid), host, dh.status,dh.rpid,dh.presence_id, mod_sofia_globals.hostname, profile->name, call_id); - } + } - - helper.profile = profile; - helper.event = event; - SWITCH_STANDARD_STREAM(helper.stream); - switch_assert(helper.stream.data); + helper.hup = hup; + helper.calls_up = dh.hits; + helper.profile = profile; + helper.event = event; + SWITCH_STANDARD_STREAM(helper.stream); + switch_assert(helper.stream.data); - if (mod_sofia_globals.debug_presence > 0) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s START_PRESENCE_SQL (%s)\n", - event->event_id == SWITCH_EVENT_PRESENCE_IN ? "IN" : "OUT", profile->name); - } - - if (mod_sofia_globals.debug_presence) { - char *buf; - switch_event_serialize(event, &buf, SWITCH_FALSE); - switch_assert(buf); - if (mod_sofia_globals.debug_presence > 1) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DUMP PRESENCE SQL:\n%s\nEVENT DUMP:\n%s\n", sql, buf); - } else { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "EVENT DUMP:\n%s\n", buf); + if (mod_sofia_globals.debug_presence > 0) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s START_PRESENCE_SQL (%s)\n", + event->event_id == SWITCH_EVENT_PRESENCE_IN ? "IN" : "OUT", profile->name); } - free(buf); - } - sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_sub_callback, &helper); - switch_safe_free(sql); + if (mod_sofia_globals.debug_presence) { + char *buf; + switch_event_serialize(event, &buf, SWITCH_FALSE); + switch_assert(buf); + if (mod_sofia_globals.debug_presence > 1) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DUMP PRESENCE SQL:\n%s\nEVENT DUMP:\n%s\n", sql, buf); + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "EVENT DUMP:\n%s\n", buf); + } + free(buf); + } + + sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_sub_callback, &helper); + switch_safe_free(sql); - if (mod_sofia_globals.debug_presence > 0) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s END_PRESENCE_SQL (%s)\n", - event->event_id == SWITCH_EVENT_PRESENCE_IN ? "IN" : "OUT", profile->name); - } - - - if (hup && dh.hits < 1) { - /* so many phones get confused when whe hangup we have to reprobe to get them all to reset to absolute states so the lights stay correct */ - switch_event_t *s_event; - - if (switch_event_create(&s_event, SWITCH_EVENT_PRESENCE_PROBE) == SWITCH_STATUS_SUCCESS) { - switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO); - switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "login", profile->name); - switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "from", "%s@%s", euser, host); - switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "to", "%s@%s", euser, host); - switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "event_type", "presence"); - switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "alt_event_type", "dialog"); - switch_event_fire(&s_event); + if (mod_sofia_globals.debug_presence > 0) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s END_PRESENCE_SQL (%s)\n", + event->event_id == SWITCH_EVENT_PRESENCE_IN ? "IN" : "OUT", profile->name); + } + + + if (hup && dh.hits < 1) { + /* so many phones get confused when whe hangup we have to reprobe to get them all to reset to absolute states so the lights stay correct */ + switch_event_t *s_event; + + if (switch_event_create(&s_event, SWITCH_EVENT_PRESENCE_PROBE) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO); + switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "login", profile->name); + switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "from", "%s@%s", euser, host); + switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "to", "%s@%s", euser, host); + switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "event_type", "presence"); + switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "alt_event_type", "dialog"); + switch_event_fire(&s_event); + } } - } - if (!zstr((char *) helper.stream.data)) { - char *this_sql = (char *) helper.stream.data; - char *next = NULL; - char *last = NULL; + if (!zstr((char *) helper.stream.data)) { + char *this_sql = (char *) helper.stream.data; + char *next = NULL; + char *last = NULL; - do { - if ((next = strchr(this_sql, ';'))) { - *next++ = '\0'; - while (*next == '\n' || *next == ' ' || *next == '\r') { + do { + if ((next = strchr(this_sql, ';'))) { *next++ = '\0'; + while (*next == '\n' || *next == ' ' || *next == '\r') { + *next++ = '\0'; + } } - } - if (!zstr(this_sql) && (!last || strcmp(last, this_sql))) { - sofia_glue_execute_sql(profile, &this_sql, SWITCH_FALSE); - last = this_sql; - } - this_sql = next; - } while (this_sql); - } - switch_safe_free(helper.stream.data); - helper.stream.data = NULL; + if (!zstr(this_sql) && (!last || strcmp(last, this_sql))) { + sofia_glue_execute_sql(profile, &this_sql, SWITCH_FALSE); + last = this_sql; + } + this_sql = next; + } while (this_sql); + } + switch_safe_free(helper.stream.data); + helper.stream.data = NULL; - sofia_glue_release_profile(profile); + sofia_glue_release_profile(profile); + } } + switch_console_free_matches(&matches); } - switch_console_free_matches(&matches); -} -done: -switch_safe_free(sql); -switch_safe_free(user); + done: + + switch_safe_free(sql); + switch_safe_free(user); } static int EVENT_THREAD_RUNNING = 0; @@ -2434,6 +2436,10 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char * is_dialog = !strcmp(event, "dialog"); + if (helper->hup && helper->calls_up > 0 && (!is_dialog || !user_agent || !switch_stristr("polycom", user_agent) || !switch_stristr("snom", user_agent))) { + goto end; + } + if (helper->event) { switch_stream_handle_t stream = { 0 }; const char *direction = switch_str_nil(switch_event_get_header(helper->event, "presence-call-direction")); @@ -2453,6 +2459,7 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char * int force_status = 0; const char *call_state = switch_event_get_header(helper->event, "channel-state"); char *call_info_state = switch_event_get_header(helper->event, "presence-call-info-state"); + int term = 0; if (user_agent && switch_stristr("snom", user_agent) && uuid) { default_dialog = "full" ; @@ -2460,18 +2467,21 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char * if (call_state && !strcasecmp(call_state, "cs_hangup")) { astate = "hangup"; - } + holding = 0; + term = 1; + } else { - if (event_status && !strncasecmp(event_status, "hold", 4)) { - holding = 1; - } + if (event_status && !strncasecmp(event_status, "hold", 4)) { + holding = 1; + } - if (force_event_status && !event_status) { - event_status = force_event_status; - } + if (force_event_status && !event_status) { + event_status = force_event_status; + } - if (event_status && !strncasecmp(event_status, "hold", 4)) { - holding = 1; + if (event_status && !strncasecmp(event_status, "hold", 4)) { + holding = 1; + } } if (!strcasecmp(direction, "inbound")) { @@ -2698,8 +2708,6 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char * open = "open"; if (switch_false(resub)) { - int term; - const char *direction = switch_event_get_header(helper->event, "Caller-Direction"); const char *op, *what = "Ring"; @@ -2906,6 +2914,7 @@ static int sofia_presence_mwi_callback2(void *pArg, int argc, char **argv, char const char *body = argv[5]; const char *o_contact = argv[2]; const char *network_ip = argv[4]; + const char *call_id = argv[6]; char *profile_name = argv[3]; struct mwi_helper *h = (struct mwi_helper *) pArg; @@ -2917,7 +2926,11 @@ static int sofia_presence_mwi_callback2(void *pArg, int argc, char **argv, char } } - sofia_glue_send_notify(profile, user, host, event, contenttype, body, o_contact, network_ip); + if (!sofia_test_pflag(profile, PFLAG_MWI_USE_REG_CALLID)) { + call_id = NULL; + } + + sofia_glue_send_notify(profile, user, host, event, contenttype, body, o_contact, network_ip, call_id); if (ext_profile) { sofia_glue_release_profile(ext_profile); @@ -3417,9 +3430,8 @@ void sofia_presence_handle_sip_i_subscribe(int status, "sub del sql: %s\n", sql); } - switch_mutex_lock(profile->ireg_mutex); switch_assert(sql != NULL); - sofia_glue_actually_execute_sql(profile, sql, NULL); + sofia_glue_actually_execute_sql(profile, sql, profile->ireg_mutex); switch_safe_free(sql); sstr = switch_mprintf("terminated;reason=noresource"); @@ -3458,7 +3470,6 @@ void sofia_presence_handle_sip_i_subscribe(int status, sstr = switch_mprintf("active;expires=%ld", exp_delta); } - switch_mutex_unlock(profile->ireg_mutex); } if (status < 200) { diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c index 81b8da1696..7ec9f1f93a 100644 --- a/src/mod/endpoints/mod_sofia/sofia_reg.c +++ b/src/mod/endpoints/mod_sofia/sofia_reg.c @@ -428,6 +428,7 @@ void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now) gateway_ptr->status = SOFIA_GATEWAY_DOWN; nua_unregister(gateway_ptr->nh, NUTAG_URL(gateway_ptr->register_url), + TAG_IF(gateway_ptr->register_sticky_proxy, NUTAG_PROXY(gateway_ptr->register_sticky_proxy)), TAG_IF(user_via, SIPTAG_VIA_STR(user_via)), SIPTAG_FROM_STR(gateway_ptr->register_from), SIPTAG_TO_STR(gateway_ptr->distinct_to ? gateway_ptr->register_to : gateway_ptr->register_from), @@ -586,7 +587,7 @@ void sofia_reg_send_reboot(sofia_profile_t *profile, const char *user, const cha event = "reboot"; } - sofia_glue_send_notify(profile, user, host, event, contenttype, body, contact, network_ip); + sofia_glue_send_notify(profile, user, host, event, contenttype, body, contact, network_ip, NULL); } int sofia_sla_dialog_del_callback(void *pArg, int argc, char **argv, char **columnNames) @@ -992,6 +993,30 @@ uint32_t sofia_reg_reg_count(sofia_profile_t *profile, const char *user, const c return atoi(buf); } +static int debounce_check(sofia_profile_t *profile, const char *user, const char *host) +{ + char key[512] = ""; + int r = 0; + time_t *last, now = switch_epoch_time_now(NULL); + + snprintf(key, sizeof(key), "%s%s", user, host); + + if ((last = switch_core_hash_find(profile->mwi_debounce_hash, key))) { + if (now - *last > 30) { + *last = now; + r = 1; + } + } else { + last = switch_core_alloc(profile->pool, sizeof(*last)); + *last = now; + switch_core_hash_insert(profile->mwi_debounce_hash, key, last); + r = 1; + } + + return r; +} + + uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sip_t const *sip, sofia_dispatch_event_t *de, sofia_regtype_t regtype, char *key, uint32_t keylen, switch_event_t **v_event, const char *is_nat) @@ -1003,6 +1028,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand sip_contact_t const *contact = NULL; char *sql; switch_event_t *s_event; + const char *reg_meta = NULL; const char *to_user = NULL; const char *to_host = NULL; char *mwi_account = NULL; @@ -1460,6 +1486,10 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand to_user = var; } + if (v_event && *v_event && (var = switch_event_get_header(*v_event, "registration_metadata"))) { + reg_meta = var; + } + if (v_event && *v_event && (mwi_account = switch_event_get_header(*v_event, "mwi-account"))) { dup_mwi_account = strdup(mwi_account); switch_assert(dup_mwi_account != NULL); @@ -1534,7 +1564,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand url = switch_mprintf("sofia/%q/sip:%q", profile->name, sofia_glue_strip_proto(contact)); switch_core_add_registration(to_user, reg_host, call_id, url, (long) switch_epoch_time_now(NULL) + (long) exptime + 60, - network_ip, network_port_c, is_tls ? "tls" : is_tcp ? "tcp" : "udp"); + network_ip, network_port_c, is_tls ? "tls" : is_tcp ? "tcp" : "udp", reg_meta); switch_safe_free(url); switch_safe_free(contact); @@ -1667,11 +1697,14 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand if (contact) { if (exptime) { + int debounce_ok = debounce_check(profile, mwi_user, mwi_host); + switch_snprintf(exp_param, sizeof(exp_param), "expires=%ld", exptime); sip_contact_add_param(nua_handle_home(nh), sip->sip_contact, exp_param); - - if (sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER) || - (reg_count == 1 && sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER))) { + + if ((sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER) || + (reg_count == 1 && sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER))) && debounce_ok) { + if (switch_event_create(&s_mwi_event, SWITCH_EVENT_MESSAGE_QUERY) == SWITCH_STATUS_SUCCESS) { switch_event_add_header(s_mwi_event, SWITCH_STACK_BOTTOM, "Message-Account", "sip:%s@%s", mwi_user, mwi_host); switch_event_add_header_string(s_mwi_event, SWITCH_STACK_BOTTOM, "VM-Sofia-Profile", profile->name); @@ -1679,9 +1712,9 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand } } - if (sofia_test_pflag(profile, PFLAG_PRESENCE_ON_REGISTER) || + if ((sofia_test_pflag(profile, PFLAG_PRESENCE_ON_REGISTER) || (reg_count == 1 && sofia_test_pflag(profile, PFLAG_PRESENCE_ON_FIRST_REGISTER)) - || send_pres == 1 || (reg_count == 1 && send_pres == 2)) { + || send_pres == 1 || (reg_count == 1 && send_pres == 2)) && debounce_ok) { if (sofia_test_pflag(profile, PFLAG_PRESENCE_PROBE_ON_REGISTER)) { if (switch_event_create(&s_event, SWITCH_EVENT_PRESENCE_PROBE) == SWITCH_STATUS_SUCCESS) { diff --git a/src/mod/event_handlers/mod_cdr_mongodb/Makefile b/src/mod/event_handlers/mod_cdr_mongodb/Makefile index b9e04b983d..6a412a0f66 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/Makefile +++ b/src/mod/event_handlers/mod_cdr_mongodb/Makefile @@ -2,8 +2,8 @@ include ../../../../build/modmake.rules MONGODB_DRIVER=./driver/src LOCAL_CFLAGS=-I$(MONGODB_DRIVER) -LOCAL_OBJS=$(MONGODB_DRIVER)/md5.o \ - $(MONGODB_DRIVER)/mongo.o $(MONGODB_DRIVER)/net.o \ - $(MONGODB_DRIVER)/bson.o $(MONGODB_DRIVER)/numbers.o $(MONGODB_DRIVER)/encoding.o \ +LOCAL_OBJS=$(MONGODB_DRIVER)/encoding.o $(MONGODB_DRIVER)/env_posix.o \ + $(MONGODB_DRIVER)/bson.o $(MONGODB_DRIVER)/md5.o \ + $(MONGODB_DRIVER)/mongo.o $(MONGODB_DRIVER)/numbers.o local_depend: $(LOCAL_OBJS) diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/HISTORY.md b/src/mod/event_handlers/mod_cdr_mongodb/driver/HISTORY.md index f3f0fab21d..1659192909 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/HISTORY.md +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/HISTORY.md @@ -1,5 +1,35 @@ # MongoDB C Driver History +## 0.5.2 +2012-5-4 + +* Validate collection and database names on insert. +* Validate insert limits using max BSON size. +* Support getaddrinfo and SO_RCVTIMEO and SO_SNDTIMEO on Windows. +* Store errno/WSAGetLastError() on errors. +* Various bug fixes and refactorings. +* Update error reporting docs. + +## 0.5.1 + +* Env for POSIX, WIN32, and standard C. +* Various bug fixes. + +## 0.5 +2012-3-31 + +* Separate cursor-specific errors into their own enum: mongo_cursor_error_t. +* Catch $err return on bad queries and store the result in conn->getlasterrorcode + and conn->getlasterrstr. +* On queries that return $err, set cursor->err to MONGO_CURSOR_QUERY_FAIL. +* When passing bad BSON to a cursor object, set cursor->err to MONGO_CURSOR_BSON_ERROR, + and store the specific BSON error on the conn->err field. +* Remove bson_copy_basic(). +* bson_copy() will copy finished bson objects only. +* bson_copy() returns BSON_OK on success and BSON_ERROR on failure. +* Added a Makefile for easy compile and install on Linux and OS X. +* Replica set connect fixes. + ## 0.4 THIS RELEASE INCLUDES NUMEROUS BACKWARD-BREAKING CHANGES. diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/README.md b/src/mod/event_handlers/mod_cdr_mongodb/driver/README.md index 1afe39e69c..d8c7526683 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/README.md +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/README.md @@ -12,9 +12,9 @@ Until the 1.0 release, this driver should be considered alpha. Keep in mind that # Building First check out the version you want to build. *Always build from a particular tag, since HEAD may be -a work in progress.* For example, to build version 0.4, run: +a work in progress.* For example, to build version 0.5.2, run: - git checkout v0.4 + git checkout v0.5.2 You can then build the driver with scons: diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/SConstruct b/src/mod/event_handlers/mod_cdr_mongodb/driver/SConstruct deleted file mode 100644 index d3be2d89a6..0000000000 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/SConstruct +++ /dev/null @@ -1,178 +0,0 @@ -# -*- mode: python; -*- - -VERSION = "0.4" - -# --- options ---- -AddOption('--test-server', - dest='test_server', - default='127.0.0.1', - type='string', - nargs=1, - action='store', - help='IP address of server to use for testing') - -AddOption('--seed-start-port', - dest='seed_start_port', - default=30000, - type='int', - nargs=1, - action='store', - help='IP address of server to use for testing') - -AddOption('--c99', - dest='use_c99', - default=False, - action='store_true', - help='Compile with c99 (recommended for gcc)') - -AddOption('--d', - dest='optimize', - default=True, - action='store_false', - help='disable optimizations') - -AddOption('--use-platform', - dest='compile_platform', - default='GENERIC', - type='string', - nargs=1, - action='store', - help='Compile for a specific platform to take advantage ' - ' of particular system features. For the moment, this include timeouts only.' - ' Current options include LINUX, ' - ' GENERIC, and CUSTOM. If you specific CUSTOM, you must place a' - ' system-specific implementation of net.h and net.c in src/platform/custom/') - -import os, sys - -env = Environment( ENV=os.environ ) - -# ---- Docs ---- -def build_docs(env, target, source): - buildscript_path = os.path.join(os.path.abspath("docs")) - sys.path.insert(0, buildscript_path) - import buildscripts - from buildscripts import docs - docs.main() - -env.Alias("docs", [], [build_docs]) -env.AlwaysBuild("docs") - -# ---- Platforms ---- -PLATFORM_TEST_DIR = None -if "LINUX" == GetOption('compile_platform'): - env.Append( CPPFLAGS=" -D_MONGO_USE_LINUX_SYSTEM" ) - NET_LIB = "src/platform/linux/net.c" - PLATFORM_TEST_DIR = "test/platform/linux/" - PLATFORM_TESTS = [ "timeouts" ] -elif "CUSTOM" == GetOption('compile_platform'): - env.Append( CPPFLAGS=" -D_MONGO_USE_CUSTOM_SYSTEM" ) - NET_LIB = "src/platform/custom/net.c" -else: - NET_LIB = "src/net.c" - -# ---- Libraries ---- -if os.sys.platform in ["darwin", "linux2"]: - env.Append( CPPFLAGS=" -pedantic -Wall -ggdb -DMONGO_HAVE_STDINT" ) - env.Append( CPPPATH=["/opt/local/include/"] ) - env.Append( LIBPATH=["/opt/local/lib/"] ) - - if GetOption('use_c99'): - env.Append( CFLAGS=" -std=c99 " ) - env.Append( CXXDEFINES="MONGO_HAVE_STDINT" ) - else: - env.Append( CFLAGS=" -ansi " ) - - if GetOption('optimize'): - env.Append( CPPFLAGS=" -O3 " ) - # -O3 benchmarks *significantly* faster than -O2 when disabling networking -elif 'win32' == os.sys.platform: - env.Append( LIBS='ws2_32' ) - -#we shouldn't need these options in c99 mode -if not GetOption('use_c99'): - conf = Configure(env) - - if not conf.CheckType('int64_t'): - if conf.CheckType('int64_t', '#include \n'): - conf.env.Append( CPPDEFINES="MONGO_HAVE_STDINT" ) - elif conf.CheckType('int64_t', '#include \n'): - conf.env.Append( CPPDEFINES="MONGO_HAVE_UNISTD" ) - elif conf.CheckType('__int64'): - conf.env.Append( CPPDEFINES="MONGO_USE__INT64" ) - elif conf.CheckType('long long int'): - conf.env.Append( CPPDEFINES="MONGO_USE_LONG_LONG_INT" ) - else: - print "*** what is your 64 bit int type? ****" - Exit(1) - - env = conf.Finish() - -have_libjson = False -conf = Configure(env) -if conf.CheckLib('json'): - have_libjson = True -env = conf.Finish() - -if sys.byteorder == 'big': - env.Append( CPPDEFINES="MONGO_BIG_ENDIAN" ) - -env.Append( CPPPATH=["src/"] ) - -coreFiles = ["src/md5.c" ] -mFiles = [ "src/mongo.c", NET_LIB, "src/gridfs.c"] -bFiles = [ "src/bson.c", "src/numbers.c", "src/encoding.c"] -mLibFiles = coreFiles + mFiles + bFiles -bLibFiles = coreFiles + bFiles -m = env.Library( "mongoc" , mLibFiles ) -b = env.Library( "bson" , bLibFiles ) -env.Default( env.Alias( "lib" , [ m[0] , b[0] ] ) ) - -if os.sys.platform == "linux2": - env.Append( SHLINKFLAGS="-shared -Wl,-soname,libmongoc.so." + VERSION ) - env.Append( SHLINKFLAGS = "-shared -Wl,-soname,libbson.so." + VERSION ) - -dynm = env.SharedLibrary( "mongoc" , mLibFiles ) -dynb = env.SharedLibrary( "bson" , bLibFiles ) -env.Default( env.Alias( "sharedlib" , [ dynm[0] , dynb[0] ] ) ) - - - -# ---- Benchmarking ---- -benchmarkEnv = env.Clone() -benchmarkEnv.Append( CPPDEFINES=[('TEST_SERVER', r'\"%s\"'%GetOption('test_server')), -('SEED_START_PORT', r'%d'%GetOption('seed_start_port'))] ) -benchmarkEnv.Append( LIBS=[m, b] ) -benchmarkEnv.Prepend( LIBPATH=["."] ) -benchmarkEnv.Program( "benchmark" , [ "test/benchmark.c"] ) - -# ---- Tests ---- -testEnv = benchmarkEnv.Clone() -testCoreFiles = [ ] - -def run_tests( root, tests ): - for name in tests: - filename = "%s/%s.c" % (root, name) - exe = "test_" + name - test = testEnv.Program( exe , testCoreFiles + [filename] ) - test_alias = testEnv.Alias('test', [test], test[0].abspath + ' 2> ' + os.path.devnull) - AlwaysBuild(test_alias) - -tests = Split("sizes resize endian_swap bson bson_subobject simple update errors " -"count_delete auth gridfs validate examples helpers oid functions cursors replica_set") - -# Run standard tests -run_tests("test", tests) - -# Run platform tests -if not PLATFORM_TEST_DIR is None: - run_tests( PLATFORM_TEST_DIR, PLATFORM_TESTS ) - -if have_libjson: - tests.append('json') - testEnv.Append( LIBS=["json"] ) - -# special case for cpptest -test = testEnv.Program( 'test_cpp' , testCoreFiles + ['test/cpptest.cpp'] ) -test_alias = testEnv.Alias('test', [test], test[0].abspath + ' 2> '+ os.path.devnull) -AlwaysBuild(test_alias) diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/bson.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/bson.c index 349f42284c..26d17d8696 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/bson.c +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/bson.c @@ -33,7 +33,11 @@ static const int zero = 0; void *( *bson_malloc_func )( size_t ) = malloc; void *( *bson_realloc_func )( void *, size_t ) = realloc; void ( *bson_free )( void * ) = free; +#ifdef R_SAFETY_NET +bson_printf_func bson_printf; +#else bson_printf_func bson_printf = printf; +#endif bson_fprintf_func bson_fprintf = fprintf; bson_sprintf_func bson_sprintf = sprintf; @@ -48,33 +52,32 @@ static int ( *oid_inc_func )( void ) = NULL; READING ------------------------------ */ -bson *bson_empty( bson *obj ) { +MONGO_EXPORT bson* bson_create() { + return (bson*)bson_malloc(sizeof(bson)); +} + +MONGO_EXPORT void bson_dispose(bson* b) { + bson_free(b); +} + +MONGO_EXPORT bson *bson_empty( bson *obj ) { static char *data = "\005\0\0\0\0"; bson_init_data( obj, data ); obj->finished = 1; obj->err = 0; + obj->errstr = NULL; obj->stackPos = 0; return obj; } -void bson_copy_basic( bson *out, const bson *in ) { - if ( !out ) return; +MONGO_EXPORT int bson_copy( bson *out, const bson *in ) { + if ( !out ) return BSON_ERROR; + if ( !in->finished ) return BSON_ERROR; bson_init_size( out, bson_size( in ) ); memcpy( out->data, in->data, bson_size( in ) ); -} + out->finished = 1; -void bson_copy( bson *out, const bson *in ) { - int i; - - if ( !out ) return; - bson_copy_basic( out, in ); - out->cur = out->data + ( in->cur - in->data ); - out->dataSize = in->dataSize; - out->finished = in->finished; - out->stackPos = in->stackPos; - out->err = in->err; - for( i=0; istackPos; i++ ) - out->stack[i] = in->stack[i]; + return BSON_OK; } int bson_init_data( bson *b, char *data ) { @@ -82,6 +85,12 @@ int bson_init_data( bson *b, char *data ) { return BSON_OK; } +int bson_init_finished_data( bson *b, char *data ) { + bson_init_data( b, data ); + b->finished = 1; + return BSON_OK; +} + static void _bson_reset( bson *b ) { b->finished = 0; b->stackPos = 0; @@ -89,7 +98,7 @@ static void _bson_reset( bson *b ) { b->errstr = NULL; } -int bson_size( const bson *b ) { +MONGO_EXPORT int bson_size( const bson *b ) { int i; if ( ! b || ! b->data ) return 0; @@ -97,7 +106,12 @@ int bson_size( const bson *b ) { return i; } -const char *bson_data( bson *b ) { +MONGO_EXPORT int bson_buffer_size( const bson *b ) { + return (b->cur - b->data + 1); +} + + +MONGO_EXPORT const char *bson_data( const bson *b ) { return (const char *)b->data; } @@ -146,14 +160,14 @@ static char hexbyte( char hex ) { } } -void bson_oid_from_string( bson_oid_t *oid, const char *str ) { +MONGO_EXPORT void bson_oid_from_string( bson_oid_t *oid, const char *str ) { int i; for ( i=0; i<12; i++ ) { oid->bytes[i] = ( hexbyte( str[2*i] ) << 4 ) | hexbyte( str[2*i + 1] ); } } -void bson_oid_to_string( const bson_oid_t *oid, char *str ) { +MONGO_EXPORT void bson_oid_to_string( const bson_oid_t *oid, char *str ) { static const char hex[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; int i; for ( i=0; i<12; i++ ) { @@ -163,15 +177,15 @@ void bson_oid_to_string( const bson_oid_t *oid, char *str ) { str[24] = '\0'; } -void bson_set_oid_fuzz( int ( *func )( void ) ) { +MONGO_EXPORT void bson_set_oid_fuzz( int ( *func )( void ) ) { oid_fuzz_func = func; } -void bson_set_oid_inc( int ( *func )( void ) ) { +MONGO_EXPORT void bson_set_oid_inc( int ( *func )( void ) ) { oid_inc_func = func; } -void bson_oid_gen( bson_oid_t *oid ) { +MONGO_EXPORT void bson_oid_gen( bson_oid_t *oid ) { static int incr = 0; static int fuzz = 0; int i; @@ -196,18 +210,18 @@ void bson_oid_gen( bson_oid_t *oid ) { bson_big_endian32( &oid->ints[2], &i ); } -time_t bson_oid_generated_time( bson_oid_t *oid ) { +MONGO_EXPORT time_t bson_oid_generated_time( bson_oid_t *oid ) { time_t out; bson_big_endian32( &out, &oid->ints[0] ); return out; } -void bson_print( bson *b ) { +MONGO_EXPORT void bson_print( const bson *b ) { bson_print_raw( b->data , 0 ); } -void bson_print_raw( const char *data , int depth ) { +MONGO_EXPORT void bson_print_raw( const char *data , int depth ) { bson_iterator i; const char *key; int temp; @@ -223,69 +237,69 @@ void bson_print_raw( const char *data , int depth ) { key = bson_iterator_key( &i ); for ( temp=0; temp<=depth; temp++ ) - printf( "\t" ); + bson_printf( "\t" ); bson_printf( "%s : %d \t " , key , t ); switch ( t ) { case BSON_DOUBLE: - printf( "%f" , bson_iterator_double( &i ) ); + bson_printf( "%f" , bson_iterator_double( &i ) ); break; case BSON_STRING: - printf( "%s" , bson_iterator_string( &i ) ); + bson_printf( "%s" , bson_iterator_string( &i ) ); break; case BSON_SYMBOL: - printf( "SYMBOL: %s" , bson_iterator_string( &i ) ); + bson_printf( "SYMBOL: %s" , bson_iterator_string( &i ) ); break; case BSON_OID: bson_oid_to_string( bson_iterator_oid( &i ), oidhex ); - printf( "%s" , oidhex ); + bson_printf( "%s" , oidhex ); break; case BSON_BOOL: - printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); + bson_printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break; case BSON_DATE: - printf( "%ld" , ( long int )bson_iterator_date( &i ) ); + bson_printf( "%ld" , ( long int )bson_iterator_date( &i ) ); break; case BSON_BINDATA: - printf( "BSON_BINDATA" ); + bson_printf( "BSON_BINDATA" ); break; case BSON_UNDEFINED: - printf( "BSON_UNDEFINED" ); + bson_printf( "BSON_UNDEFINED" ); break; case BSON_NULL: - printf( "BSON_NULL" ); + bson_printf( "BSON_NULL" ); break; case BSON_REGEX: - printf( "BSON_REGEX: %s", bson_iterator_regex( &i ) ); + bson_printf( "BSON_REGEX: %s", bson_iterator_regex( &i ) ); break; case BSON_CODE: - printf( "BSON_CODE: %s", bson_iterator_code( &i ) ); + bson_printf( "BSON_CODE: %s", bson_iterator_code( &i ) ); break; case BSON_CODEWSCOPE: - printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) ); + bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) ); bson_init( &scope ); bson_iterator_code_scope( &i, &scope ); - printf( "\n\t SCOPE: " ); + bson_printf( "\n\t SCOPE: " ); bson_print( &scope ); break; case BSON_INT: - printf( "%d" , bson_iterator_int( &i ) ); + bson_printf( "%d" , bson_iterator_int( &i ) ); break; case BSON_LONG: - printf( "%lld" , ( long long int )bson_iterator_long( &i ) ); + bson_printf( "%lld" , ( uint64_t )bson_iterator_long( &i ) ); break; case BSON_TIMESTAMP: ts = bson_iterator_timestamp( &i ); - printf( "i: %d, t: %d", ts.i, ts.t ); + bson_printf( "i: %d, t: %d", ts.i, ts.t ); break; case BSON_OBJECT: case BSON_ARRAY: - printf( "\n" ); + bson_printf( "\n" ); bson_print_raw( bson_iterator_value( &i ) , depth + 1 ); break; default: bson_errprintf( "can't print type : %d\n" , t ); } - printf( "\n" ); + bson_printf( "\n" ); } } @@ -293,17 +307,25 @@ void bson_print_raw( const char *data , int depth ) { ITERATOR ------------------------------ */ -void bson_iterator_init( bson_iterator *i, const bson *b ) { +MONGO_EXPORT bson_iterator* bson_iterator_create() { + return (bson_iterator*)malloc(sizeof(bson_iterator*)); +} + +MONGO_EXPORT void bson_iterator_dispose(bson_iterator* i) { + free(i); +} + +MONGO_EXPORT void bson_iterator_init( bson_iterator *i, const bson *b ) { i->cur = b->data + 4; i->first = 1; } -void bson_iterator_from_buffer( bson_iterator *i, const char *buffer ) { +MONGO_EXPORT void bson_iterator_from_buffer( bson_iterator *i, const char *buffer ) { i->cur = buffer + 4; i->first = 1; } -bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ) { +MONGO_EXPORT bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ) { bson_iterator_init( it, (bson *)obj ); while( bson_iterator_next( it ) ) { if ( strcmp( name, bson_iterator_key( it ) ) == 0 ) @@ -312,11 +334,11 @@ bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ) { return bson_iterator_type( it ); } -bson_bool_t bson_iterator_more( const bson_iterator *i ) { +MONGO_EXPORT bson_bool_t bson_iterator_more( const bson_iterator *i ) { return *( i->cur ); } -bson_type bson_iterator_next( bson_iterator *i ) { +MONGO_EXPORT bson_type bson_iterator_next( bson_iterator *i ) { int ds; if ( i->first ) { @@ -384,15 +406,15 @@ bson_type bson_iterator_next( bson_iterator *i ) { return ( bson_type )( *i->cur ); } -bson_type bson_iterator_type( const bson_iterator *i ) { +MONGO_EXPORT bson_type bson_iterator_type( const bson_iterator *i ) { return ( bson_type )i->cur[0]; } -const char *bson_iterator_key( const bson_iterator *i ) { +MONGO_EXPORT const char *bson_iterator_key( const bson_iterator *i ) { return i->cur + 1; } -const char *bson_iterator_value( const bson_iterator *i ) { +MONGO_EXPORT const char *bson_iterator_value( const bson_iterator *i ) { const char *t = i->cur + 1; t += strlen( t ) + 1; return t; @@ -422,11 +444,11 @@ bson_bool_t bson_iterator_bool_raw( const bson_iterator *i ) { return bson_iterator_value( i )[0]; } -bson_oid_t *bson_iterator_oid( const bson_iterator *i ) { +MONGO_EXPORT bson_oid_t *bson_iterator_oid( const bson_iterator *i ) { return ( bson_oid_t * )bson_iterator_value( i ); } -int bson_iterator_int( const bson_iterator *i ) { +MONGO_EXPORT int bson_iterator_int( const bson_iterator *i ) { switch ( bson_iterator_type( i ) ) { case BSON_INT: return bson_iterator_int_raw( i ); @@ -439,7 +461,7 @@ int bson_iterator_int( const bson_iterator *i ) { } } -double bson_iterator_double( const bson_iterator *i ) { +MONGO_EXPORT double bson_iterator_double( const bson_iterator *i ) { switch ( bson_iterator_type( i ) ) { case BSON_INT: return bson_iterator_int_raw( i ); @@ -452,7 +474,7 @@ double bson_iterator_double( const bson_iterator *i ) { } } -int64_t bson_iterator_long( const bson_iterator *i ) { +MONGO_EXPORT int64_t bson_iterator_long( const bson_iterator *i ) { switch ( bson_iterator_type( i ) ) { case BSON_INT: return bson_iterator_int_raw( i ); @@ -465,14 +487,29 @@ int64_t bson_iterator_long( const bson_iterator *i ) { } } -bson_timestamp_t bson_iterator_timestamp( const bson_iterator *i ) { +MONGO_EXPORT bson_timestamp_t bson_iterator_timestamp( const bson_iterator *i ) { bson_timestamp_t ts; bson_little_endian32( &( ts.i ), bson_iterator_value( i ) ); bson_little_endian32( &( ts.t ), bson_iterator_value( i ) + 4 ); return ts; } -bson_bool_t bson_iterator_bool( const bson_iterator *i ) { + +MONGO_EXPORT int bson_iterator_timestamp_time( const bson_iterator *i ) { + int time; + bson_little_endian32( &time, bson_iterator_value( i ) + 4 ); + return time; +} + + +MONGO_EXPORT int bson_iterator_timestamp_increment( const bson_iterator *i ) { + int increment; + bson_little_endian32( &increment, bson_iterator_value( i ) ); + return increment; +} + + +MONGO_EXPORT bson_bool_t bson_iterator_bool( const bson_iterator *i ) { switch ( bson_iterator_type( i ) ) { case BSON_BOOL: return bson_iterator_bool_raw( i ); @@ -490,15 +527,21 @@ bson_bool_t bson_iterator_bool( const bson_iterator *i ) { } } -const char *bson_iterator_string( const bson_iterator *i ) { - return bson_iterator_value( i ) + 4; +MONGO_EXPORT const char *bson_iterator_string( const bson_iterator *i ) { + switch ( bson_iterator_type( i ) ) { + case BSON_STRING: + case BSON_SYMBOL: + return bson_iterator_value( i ) + 4; + default: + return ""; + } } int bson_iterator_string_len( const bson_iterator *i ) { return bson_iterator_int_raw( i ); } -const char *bson_iterator_code( const bson_iterator *i ) { +MONGO_EXPORT const char *bson_iterator_code( const bson_iterator *i ) { switch ( bson_iterator_type( i ) ) { case BSON_STRING: case BSON_CODE: @@ -510,57 +553,59 @@ const char *bson_iterator_code( const bson_iterator *i ) { } } -void bson_iterator_code_scope( const bson_iterator *i, bson *scope ) { +MONGO_EXPORT void bson_iterator_code_scope( const bson_iterator *i, bson *scope ) { if ( bson_iterator_type( i ) == BSON_CODEWSCOPE ) { int code_len; bson_little_endian32( &code_len, bson_iterator_value( i )+4 ); bson_init_data( scope, ( void * )( bson_iterator_value( i )+8+code_len ) ); + _bson_reset( scope ); + scope->finished = 1; } else { bson_empty( scope ); } } -bson_date_t bson_iterator_date( const bson_iterator *i ) { +MONGO_EXPORT bson_date_t bson_iterator_date( const bson_iterator *i ) { return bson_iterator_long_raw( i ); } -time_t bson_iterator_time_t( const bson_iterator *i ) { +MONGO_EXPORT time_t bson_iterator_time_t( const bson_iterator *i ) { return bson_iterator_date( i ) / 1000; } -int bson_iterator_bin_len( const bson_iterator *i ) { +MONGO_EXPORT int bson_iterator_bin_len( const bson_iterator *i ) { return ( bson_iterator_bin_type( i ) == BSON_BIN_BINARY_OLD ) ? bson_iterator_int_raw( i ) - 4 : bson_iterator_int_raw( i ); } -char bson_iterator_bin_type( const bson_iterator *i ) { +MONGO_EXPORT char bson_iterator_bin_type( const bson_iterator *i ) { return bson_iterator_value( i )[4]; } -const char *bson_iterator_bin_data( const bson_iterator *i ) { +MONGO_EXPORT const char *bson_iterator_bin_data( const bson_iterator *i ) { return ( bson_iterator_bin_type( i ) == BSON_BIN_BINARY_OLD ) ? bson_iterator_value( i ) + 9 : bson_iterator_value( i ) + 5; } -const char *bson_iterator_regex( const bson_iterator *i ) { +MONGO_EXPORT const char *bson_iterator_regex( const bson_iterator *i ) { return bson_iterator_value( i ); } -const char *bson_iterator_regex_opts( const bson_iterator *i ) { +MONGO_EXPORT const char *bson_iterator_regex_opts( const bson_iterator *i ) { const char *p = bson_iterator_value( i ); return p + strlen( p ) + 1; } -void bson_iterator_subobject( const bson_iterator *i, bson *sub ) { +MONGO_EXPORT void bson_iterator_subobject( const bson_iterator *i, bson *sub ) { bson_init_data( sub, ( char * )bson_iterator_value( i ) ); _bson_reset( sub ); sub->finished = 1; } -void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub ) { +MONGO_EXPORT void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub ) { bson_iterator_from_buffer( sub, bson_iterator_value( i ) ); } @@ -578,7 +623,7 @@ static void _bson_init_size( bson *b, int size ) { _bson_reset( b ); } -void bson_init( bson *b ) { +MONGO_EXPORT void bson_init( bson *b ) { _bson_init_size( b, initialBufferSize ); } @@ -635,7 +680,7 @@ int bson_ensure_space( bson *b, const int bytesNeeded ) { return BSON_OK; } -int bson_finish( bson *b ) { +MONGO_EXPORT int bson_finish( bson *b ) { int i; if( b->err & BSON_NOT_UTF8 ) @@ -652,12 +697,14 @@ int bson_finish( bson *b ) { return BSON_OK; } -void bson_destroy( bson *b ) { - bson_free( b->data ); - b->err = 0; - b->data = 0; - b->cur = 0; - b->finished = 1; +MONGO_EXPORT void bson_destroy( bson *b ) { + if (b) { + bson_free( b->data ); + b->err = 0; + b->data = 0; + b->cur = 0; + b->finished = 1; + } } static int bson_append_estart( bson *b, int type, const char *name, const int dataSize ) { @@ -686,41 +733,41 @@ static int bson_append_estart( bson *b, int type, const char *name, const int da BUILDING TYPES ------------------------------ */ -int bson_append_int( bson *b, const char *name, const int i ) { +MONGO_EXPORT int bson_append_int( bson *b, const char *name, const int i ) { if ( bson_append_estart( b, BSON_INT, name, 4 ) == BSON_ERROR ) return BSON_ERROR; bson_append32( b , &i ); return BSON_OK; } -int bson_append_long( bson *b, const char *name, const int64_t i ) { +MONGO_EXPORT int bson_append_long( bson *b, const char *name, const int64_t i ) { if ( bson_append_estart( b , BSON_LONG, name, 8 ) == BSON_ERROR ) return BSON_ERROR; bson_append64( b , &i ); return BSON_OK; } -int bson_append_double( bson *b, const char *name, const double d ) { +MONGO_EXPORT int bson_append_double( bson *b, const char *name, const double d ) { if ( bson_append_estart( b, BSON_DOUBLE, name, 8 ) == BSON_ERROR ) return BSON_ERROR; bson_append64( b , &d ); return BSON_OK; } -int bson_append_bool( bson *b, const char *name, const bson_bool_t i ) { +MONGO_EXPORT int bson_append_bool( bson *b, const char *name, const bson_bool_t i ) { if ( bson_append_estart( b, BSON_BOOL, name, 1 ) == BSON_ERROR ) return BSON_ERROR; bson_append_byte( b , i != 0 ); return BSON_OK; } -int bson_append_null( bson *b, const char *name ) { +MONGO_EXPORT int bson_append_null( bson *b, const char *name ) { if ( bson_append_estart( b , BSON_NULL, name, 0 ) == BSON_ERROR ) return BSON_ERROR; return BSON_OK; } -int bson_append_undefined( bson *b, const char *name ) { +MONGO_EXPORT int bson_append_undefined( bson *b, const char *name ) { if ( bson_append_estart( b, BSON_UNDEFINED, name, 0 ) == BSON_ERROR ) return BSON_ERROR; return BSON_OK; @@ -741,31 +788,31 @@ int bson_append_string_base( bson *b, const char *name, return BSON_OK; } -int bson_append_string( bson *b, const char *name, const char *value ) { +MONGO_EXPORT int bson_append_string( bson *b, const char *name, const char *value ) { return bson_append_string_base( b, name, value, strlen ( value ), BSON_STRING ); } -int bson_append_symbol( bson *b, const char *name, const char *value ) { +MONGO_EXPORT int bson_append_symbol( bson *b, const char *name, const char *value ) { return bson_append_string_base( b, name, value, strlen ( value ), BSON_SYMBOL ); } -int bson_append_code( bson *b, const char *name, const char *value ) { +MONGO_EXPORT int bson_append_code( bson *b, const char *name, const char *value ) { return bson_append_string_base( b, name, value, strlen ( value ), BSON_CODE ); } -int bson_append_string_n( bson *b, const char *name, const char *value, int len ) { +MONGO_EXPORT int bson_append_string_n( bson *b, const char *name, const char *value, int len ) { return bson_append_string_base( b, name, value, len, BSON_STRING ); } -int bson_append_symbol_n( bson *b, const char *name, const char *value, int len ) { +MONGO_EXPORT int bson_append_symbol_n( bson *b, const char *name, const char *value, int len ) { return bson_append_string_base( b, name, value, len, BSON_SYMBOL ); } -int bson_append_code_n( bson *b, const char *name, const char *value, int len ) { +MONGO_EXPORT int bson_append_code_n( bson *b, const char *name, const char *value, int len ) { return bson_append_string_base( b, name, value, len, BSON_CODE ); } -int bson_append_code_w_scope_n( bson *b, const char *name, +MONGO_EXPORT int bson_append_code_w_scope_n( bson *b, const char *name, const char *code, int len, const bson *scope ) { int sl = len + 1; @@ -779,11 +826,11 @@ int bson_append_code_w_scope_n( bson *b, const char *name, return BSON_OK; } -int bson_append_code_w_scope( bson *b, const char *name, const char *code, const bson *scope ) { +MONGO_EXPORT int bson_append_code_w_scope( bson *b, const char *name, const char *code, const bson *scope ) { return bson_append_code_w_scope_n( b, name, code, strlen ( code ), scope ); } -int bson_append_binary( bson *b, const char *name, char type, const char *str, int len ) { +MONGO_EXPORT int bson_append_binary( bson *b, const char *name, char type, const char *str, int len ) { if ( type == BSON_BIN_BINARY_OLD ) { int subtwolen = len + 4; if ( bson_append_estart( b, BSON_BINDATA, name, 4+1+4+len ) == BSON_ERROR ) @@ -802,20 +849,20 @@ int bson_append_binary( bson *b, const char *name, char type, const char *str, i return BSON_OK; } -int bson_append_oid( bson *b, const char *name, const bson_oid_t *oid ) { +MONGO_EXPORT int bson_append_oid( bson *b, const char *name, const bson_oid_t *oid ) { if ( bson_append_estart( b, BSON_OID, name, 12 ) == BSON_ERROR ) return BSON_ERROR; bson_append( b , oid , 12 ); return BSON_OK; } -int bson_append_new_oid( bson *b, const char *name ) { +MONGO_EXPORT int bson_append_new_oid( bson *b, const char *name ) { bson_oid_t oid; bson_oid_gen( &oid ); return bson_append_oid( b, name, &oid ); } -int bson_append_regex( bson *b, const char *name, const char *pattern, const char *opts ) { +MONGO_EXPORT int bson_append_regex( bson *b, const char *name, const char *pattern, const char *opts ) { const int plen = strlen( pattern )+1; const int olen = strlen( opts )+1; if ( bson_append_estart( b, BSON_REGEX, name, plen + olen ) == BSON_ERROR ) @@ -827,14 +874,14 @@ int bson_append_regex( bson *b, const char *name, const char *pattern, const cha return BSON_OK; } -int bson_append_bson( bson *b, const char *name, const bson *bson ) { +MONGO_EXPORT int bson_append_bson( bson *b, const char *name, const bson *bson ) { if ( bson_append_estart( b, BSON_OBJECT, name, bson_size( bson ) ) == BSON_ERROR ) return BSON_ERROR; bson_append( b , bson->data , bson_size( bson ) ); return BSON_OK; } -int bson_append_element( bson *b, const char *name_or_null, const bson_iterator *elem ) { +MONGO_EXPORT int bson_append_element( bson *b, const char *name_or_null, const bson_iterator *elem ) { bson_iterator next = *elem; int size; @@ -854,7 +901,7 @@ int bson_append_element( bson *b, const char *name_or_null, const bson_iterator return BSON_OK; } -int bson_append_timestamp( bson *b, const char *name, bson_timestamp_t *ts ) { +MONGO_EXPORT int bson_append_timestamp( bson *b, const char *name, bson_timestamp_t *ts ) { if ( bson_append_estart( b, BSON_TIMESTAMP, name, 8 ) == BSON_ERROR ) return BSON_ERROR; bson_append32( b , &( ts->i ) ); @@ -863,31 +910,39 @@ int bson_append_timestamp( bson *b, const char *name, bson_timestamp_t *ts ) { return BSON_OK; } -int bson_append_date( bson *b, const char *name, bson_date_t millis ) { +MONGO_EXPORT int bson_append_timestamp2( bson *b, const char *name, int time, int increment ) { + if ( bson_append_estart( b, BSON_TIMESTAMP, name, 8 ) == BSON_ERROR ) return BSON_ERROR; + + bson_append32( b , &increment ); + bson_append32( b , &time ); + return BSON_OK; +} + +MONGO_EXPORT int bson_append_date( bson *b, const char *name, bson_date_t millis ) { if ( bson_append_estart( b, BSON_DATE, name, 8 ) == BSON_ERROR ) return BSON_ERROR; bson_append64( b , &millis ); return BSON_OK; } -int bson_append_time_t( bson *b, const char *name, time_t secs ) { +MONGO_EXPORT int bson_append_time_t( bson *b, const char *name, time_t secs ) { return bson_append_date( b, name, ( bson_date_t )secs * 1000 ); } -int bson_append_start_object( bson *b, const char *name ) { +MONGO_EXPORT int bson_append_start_object( bson *b, const char *name ) { if ( bson_append_estart( b, BSON_OBJECT, name, 5 ) == BSON_ERROR ) return BSON_ERROR; b->stack[ b->stackPos++ ] = b->cur - b->data; bson_append32( b , &zero ); return BSON_OK; } -int bson_append_start_array( bson *b, const char *name ) { +MONGO_EXPORT int bson_append_start_array( bson *b, const char *name ) { if ( bson_append_estart( b, BSON_ARRAY, name, 5 ) == BSON_ERROR ) return BSON_ERROR; b->stack[ b->stackPos++ ] = b->cur - b->data; bson_append32( b , &zero ); return BSON_OK; } -int bson_append_finish_object( bson *b ) { +MONGO_EXPORT int bson_append_finish_object( bson *b ) { char *start; int i; if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR; @@ -900,22 +955,25 @@ int bson_append_finish_object( bson *b ) { return BSON_OK; } -int bson_append_finish_array( bson *b ) { - return bson_append_finish_object( b ); +MONGO_EXPORT double bson_int64_to_double( int64_t i64 ) { + return (double)i64; } +MONGO_EXPORT int bson_append_finish_array( bson *b ) { + return bson_append_finish_object( b ); +} /* Error handling and allocators. */ static bson_err_handler err_handler = NULL; -bson_err_handler set_bson_err_handler( bson_err_handler func ) { +MONGO_EXPORT bson_err_handler set_bson_err_handler( bson_err_handler func ) { bson_err_handler old = err_handler; err_handler = func; return old; } -void *bson_malloc( int size ) { +MONGO_EXPORT void *bson_malloc( int size ) { void *p; p = bson_malloc_func( size ); bson_fatal_msg( !!p, "malloc() failed" ); @@ -933,7 +991,9 @@ int _bson_errprintf( const char *format, ... ) { va_list ap; int ret; va_start( ap, format ); +#ifndef R_SAFETY_NET ret = vfprintf( stderr, format, ap ); +#endif va_end( ap ); return ret; @@ -961,9 +1021,10 @@ void bson_fatal_msg( int ok , const char *msg ) { if ( err_handler ) { err_handler( msg ); } - +#ifndef R_SAFETY_NET bson_errprintf( "error: %s\n" , msg ); exit( -5 ); +#endif } @@ -976,3 +1037,28 @@ void bson_numstr( char *str, int i ) { else bson_sprintf( str,"%d", i ); } + +MONGO_EXPORT void bson_swap_endian64( void *outp, const void *inp ) { + const char *in = ( const char * )inp; + char *out = ( char * )outp; + + out[0] = in[7]; + out[1] = in[6]; + out[2] = in[5]; + out[3] = in[4]; + out[4] = in[3]; + out[5] = in[2]; + out[6] = in[1]; + out[7] = in[0]; + +} + +MONGO_EXPORT void bson_swap_endian32( void *outp, const void *inp ) { + const char *in = ( const char * )inp; + char *out = ( char * )outp; + + out[0] = in[3]; + out[1] = in[2]; + out[2] = in[1]; + out[3] = in[0]; +} diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/bson.h b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/bson.h index 2e385b9160..b83d1ca9c9 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/bson.h +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/bson.h @@ -3,7 +3,7 @@ * @brief BSON Declarations */ -/* Copyright 2009-2011 10gen Inc. +/* Copyright 2009-2012 10gen Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,16 +18,63 @@ * limitations under the License. */ -#ifndef _BSON_H_ -#define _BSON_H_ +#ifndef BSON_H_ +#define BSON_H_ -#include "platform.h" #include #include #include #include #include +#ifdef __GNUC__ + #define MONGO_INLINE static __inline__ + #define MONGO_EXPORT +#else + #define MONGO_INLINE static + #ifdef MONGO_STATIC_BUILD + #define MONGO_EXPORT + #elif defined(MONGO_DLL_BUILD) + #define MONGO_EXPORT __declspec(dllexport) + #else + #define MONGO_EXPORT __declspec(dllimport) + #endif +#endif + +#ifdef __cplusplus +#define MONGO_EXTERN_C_START extern "C" { +#define MONGO_EXTERN_C_END } +#else +#define MONGO_EXTERN_C_START +#define MONGO_EXTERN_C_END +#endif + +#if defined(MONGO_HAVE_STDINT) || __STDC_VERSION__ >= 199901L +#include +#elif defined(MONGO_HAVE_UNISTD) +#include +#elif defined(MONGO_USE__INT64) +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +#elif defined(MONGO_USE_LONG_LONG_INT) +typedef long long int int64_t; +typedef unsigned long long int uint64_t; +#else +#error Must compile with c99 or define MONGO_HAVE_STDINT, MONGO_HAVE_UNISTD, MONGO_USE__INT64, or MONGO_USE_LONG_INT. +#endif + +#ifdef MONGO_BIG_ENDIAN +#define bson_little_endian64(out, in) ( bson_swap_endian64(out, in) ) +#define bson_little_endian32(out, in) ( bson_swap_endian32(out, in) ) +#define bson_big_endian64(out, in) ( memcpy(out, in, 8) ) +#define bson_big_endian32(out, in) ( memcpy(out, in, 4) ) +#else +#define bson_little_endian64(out, in) ( memcpy(out, in, 8) ) +#define bson_little_endian32(out, in) ( memcpy(out, in, 4) ) +#define bson_big_endian64(out, in) ( bson_swap_endian64(out, in) ) +#define bson_big_endian32(out, in) ( bson_swap_endian32(out, in) ) +#endif + MONGO_EXTERN_C_START #define BSON_OK 0 @@ -84,12 +131,12 @@ typedef struct { } bson_iterator; typedef struct { - char *data; - char *cur; - int dataSize; - bson_bool_t finished; - int stack[32]; - int stackPos; + char *data; /**< Pointer to a block of data in this BSON object. */ + char *cur; /**< Pointer to the current position. */ + int dataSize; /**< The number of bytes allocated to char *data. */ + bson_bool_t finished; /**< When finished, the BSON object can no longer be modified. */ + int stack[32]; /**< A stack used to keep track of nested BSON elements. */ + int stackPos; /**< Index of current stack position. */ int err; /**< Bitfield representing errors or warnings on this buffer */ char *errstr; /**< A string representation of the most recent error or warning. */ } bson; @@ -112,6 +159,9 @@ typedef struct { READING ------------------------------ */ +MONGO_EXPORT bson* bson_create(); +MONGO_EXPORT void bson_dispose(bson* b); + /** * Size of a BSON object. * @@ -119,21 +169,22 @@ typedef struct { * * @return the size. */ -int bson_size( const bson *b ); +MONGO_EXPORT int bson_size( const bson *b ); +MONGO_EXPORT int bson_buffer_size( const bson *b ); /** * Print a string representation of a BSON object. * * @param b the BSON object to print. */ -void bson_print( bson *b ); +MONGO_EXPORT void bson_print( const bson *b ); /** * Return a pointer to the raw buffer stored by this bson object. * * @param b a BSON object */ -const char *bson_data( bson *b ); +MONGO_EXPORT const char *bson_data( const bson *b ); /** * Print a string representation of a BSON object. @@ -141,7 +192,7 @@ const char *bson_data( bson *b ); * @param bson the raw data to print. * @param depth the depth to recurse the object.x */ -void bson_print_raw( const char *bson , int depth ); +MONGO_EXPORT void bson_print_raw( const char *bson , int depth ); /** * Advance a bson_iterator to the named field. @@ -152,15 +203,18 @@ void bson_print_raw( const char *bson , int depth ); * * @return the type of the found object or BSON_EOO if it is not found. */ -bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ); +MONGO_EXPORT bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ); + +MONGO_EXPORT bson_iterator* bson_iterator_create(); +MONGO_EXPORT void bson_iterator_dispose(bson_iterator*); /** * Initialize a bson_iterator. * * @param i the bson_iterator to initialize. * @param bson the BSON object to associate with the iterator. */ -void bson_iterator_init( bson_iterator *i , const bson *b ); +MONGO_EXPORT void bson_iterator_init( bson_iterator *i , const bson *b ); /** * Initialize a bson iterator from a const char* buffer. Note @@ -169,7 +223,7 @@ void bson_iterator_init( bson_iterator *i , const bson *b ); * @param i the bson_iterator to initialize. * @param buffer the buffer to point to. */ -void bson_iterator_from_buffer( bson_iterator *i, const char *buffer ); +MONGO_EXPORT void bson_iterator_from_buffer( bson_iterator *i, const char *buffer ); /* more returns true for eoo. best to loop with bson_iterator_next(&it) */ /** @@ -179,7 +233,7 @@ void bson_iterator_from_buffer( bson_iterator *i, const char *buffer ); * * @return returns true if there is more data. */ -bson_bool_t bson_iterator_more( const bson_iterator *i ); +MONGO_EXPORT bson_bool_t bson_iterator_more( const bson_iterator *i ); /** * Point the iterator at the next BSON object. @@ -188,7 +242,7 @@ bson_bool_t bson_iterator_more( const bson_iterator *i ); * * @return the type of the next BSON object. */ -bson_type bson_iterator_next( bson_iterator *i ); +MONGO_EXPORT bson_type bson_iterator_next( bson_iterator *i ); /** * Get the type of the BSON object currently pointed to by the iterator. @@ -197,7 +251,7 @@ bson_type bson_iterator_next( bson_iterator *i ); * * @return the type of the current BSON object. */ -bson_type bson_iterator_type( const bson_iterator *i ); +MONGO_EXPORT bson_type bson_iterator_type( const bson_iterator *i ); /** * Get the key of the BSON object currently pointed to by the iterator. @@ -206,7 +260,7 @@ bson_type bson_iterator_type( const bson_iterator *i ); * * @return the key of the current BSON object. */ -const char *bson_iterator_key( const bson_iterator *i ); +MONGO_EXPORT const char *bson_iterator_key( const bson_iterator *i ); /** * Get the value of the BSON object currently pointed to by the iterator. @@ -215,7 +269,7 @@ const char *bson_iterator_key( const bson_iterator *i ); * * @return the value of the current BSON object. */ -const char *bson_iterator_value( const bson_iterator *i ); +MONGO_EXPORT const char *bson_iterator_value( const bson_iterator *i ); /* these convert to the right type (return 0 if non-numeric) */ /** @@ -226,7 +280,7 @@ const char *bson_iterator_value( const bson_iterator *i ); * * @return the value of the current BSON object. */ -double bson_iterator_double( const bson_iterator *i ); +MONGO_EXPORT double bson_iterator_double( const bson_iterator *i ); /** * Get the int value of the BSON object currently pointed to by the iterator. @@ -235,7 +289,7 @@ double bson_iterator_double( const bson_iterator *i ); * * @return the value of the current BSON object. */ -int bson_iterator_int( const bson_iterator *i ); +MONGO_EXPORT int bson_iterator_int( const bson_iterator *i ); /** * Get the long value of the BSON object currently pointed to by the iterator. @@ -244,7 +298,7 @@ int bson_iterator_int( const bson_iterator *i ); * * @return the value of the current BSON object. */ -int64_t bson_iterator_long( const bson_iterator *i ); +MONGO_EXPORT int64_t bson_iterator_long( const bson_iterator *i ); /* return the bson timestamp as a whole or in parts */ /** @@ -255,7 +309,9 @@ int64_t bson_iterator_long( const bson_iterator *i ); * * @return the value of the current BSON object. */ -bson_timestamp_t bson_iterator_timestamp( const bson_iterator *i ); +MONGO_EXPORT bson_timestamp_t bson_iterator_timestamp( const bson_iterator *i ); +MONGO_EXPORT int bson_iterator_timestamp_time( const bson_iterator *i ); +MONGO_EXPORT int bson_iterator_timestamp_increment( const bson_iterator *i ); /** * Get the boolean value of the BSON object currently pointed to by @@ -267,7 +323,7 @@ bson_timestamp_t bson_iterator_timestamp( const bson_iterator *i ); */ /* false: boolean false, 0 in any type, or null */ /* true: anything else (even empty strings and objects) */ -bson_bool_t bson_iterator_bool( const bson_iterator *i ); +MONGO_EXPORT bson_bool_t bson_iterator_bool( const bson_iterator *i ); /** * Get the double value of the BSON object currently pointed to by the @@ -318,7 +374,7 @@ bson_bool_t bson_iterator_bool_raw( const bson_iterator *i ); * * @return the value of the current BSON object. */ -bson_oid_t *bson_iterator_oid( const bson_iterator *i ); +MONGO_EXPORT bson_oid_t *bson_iterator_oid( const bson_iterator *i ); /** * Get the string value of the BSON object currently pointed to by the @@ -329,7 +385,7 @@ bson_oid_t *bson_iterator_oid( const bson_iterator *i ); * @return the value of the current BSON object. */ /* these can also be used with bson_code and bson_symbol*/ -const char *bson_iterator_string( const bson_iterator *i ); +MONGO_EXPORT const char *bson_iterator_string( const bson_iterator *i ); /** * Get the string length of the BSON object currently pointed to by the @@ -352,7 +408,7 @@ int bson_iterator_string_len( const bson_iterator *i ); */ /* works with bson_code, bson_codewscope, and BSON_STRING */ /* returns NULL for everything else */ -const char *bson_iterator_code( const bson_iterator *i ); +MONGO_EXPORT const char *bson_iterator_code( const bson_iterator *i ); /** * Calls bson_empty on scope if not a bson_codewscope @@ -361,7 +417,7 @@ const char *bson_iterator_code( const bson_iterator *i ); * @param scope the bson scope. */ /* calls bson_empty on scope if not a bson_codewscope */ -void bson_iterator_code_scope( const bson_iterator *i, bson *scope ); +MONGO_EXPORT void bson_iterator_code_scope( const bson_iterator *i, bson *scope ); /** * Get the date value of the BSON object currently pointed to by the @@ -372,7 +428,7 @@ void bson_iterator_code_scope( const bson_iterator *i, bson *scope ); * @return the date value of the current BSON object. */ /* both of these only work with bson_date */ -bson_date_t bson_iterator_date( const bson_iterator *i ); +MONGO_EXPORT bson_date_t bson_iterator_date( const bson_iterator *i ); /** * Get the time value of the BSON object currently pointed to by the @@ -382,7 +438,7 @@ bson_date_t bson_iterator_date( const bson_iterator *i ); * * @return the time value of the current BSON object. */ -time_t bson_iterator_time_t( const bson_iterator *i ); +MONGO_EXPORT time_t bson_iterator_time_t( const bson_iterator *i ); /** * Get the length of the BSON binary object currently pointed to by the @@ -392,7 +448,7 @@ time_t bson_iterator_time_t( const bson_iterator *i ); * * @return the length of the current BSON binary object. */ -int bson_iterator_bin_len( const bson_iterator *i ); +MONGO_EXPORT int bson_iterator_bin_len( const bson_iterator *i ); /** * Get the type of the BSON binary object currently pointed to by the @@ -402,7 +458,7 @@ int bson_iterator_bin_len( const bson_iterator *i ); * * @return the type of the current BSON binary object. */ -char bson_iterator_bin_type( const bson_iterator *i ); +MONGO_EXPORT char bson_iterator_bin_type( const bson_iterator *i ); /** * Get the value of the BSON binary object currently pointed to by the @@ -412,7 +468,7 @@ char bson_iterator_bin_type( const bson_iterator *i ); * * @return the value of the current BSON binary object. */ -const char *bson_iterator_bin_data( const bson_iterator *i ); +MONGO_EXPORT const char *bson_iterator_bin_data( const bson_iterator *i ); /** * Get the value of the BSON regex object currently pointed to by the @@ -422,7 +478,7 @@ const char *bson_iterator_bin_data( const bson_iterator *i ); * * @return the value of the current BSON regex object. */ -const char *bson_iterator_regex( const bson_iterator *i ); +MONGO_EXPORT const char *bson_iterator_regex( const bson_iterator *i ); /** * Get the options of the BSON regex object currently pointed to by the @@ -432,7 +488,7 @@ const char *bson_iterator_regex( const bson_iterator *i ); * * @return the options of the current BSON regex object. */ -const char *bson_iterator_regex_opts( const bson_iterator *i ); +MONGO_EXPORT const char *bson_iterator_regex_opts( const bson_iterator *i ); /* these work with BSON_OBJECT and BSON_ARRAY */ /** @@ -442,7 +498,7 @@ const char *bson_iterator_regex_opts( const bson_iterator *i ); * @param i the bson_iterator. * @param sub the BSON subobject destination. */ -void bson_iterator_subobject( const bson_iterator *i, bson *sub ); +MONGO_EXPORT void bson_iterator_subobject( const bson_iterator *i, bson *sub ); /** * Get a bson_iterator that on the BSON subobject. @@ -450,7 +506,7 @@ void bson_iterator_subobject( const bson_iterator *i, bson *sub ); * @param i the bson_iterator. * @param sub the iterator to point at the BSON subobject. */ -void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub ); +MONGO_EXPORT void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub ); /* str must be at least 24 hex chars + null byte */ /** @@ -459,7 +515,7 @@ void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub ); * @param oid the bson_oid_t destination. * @param str a null terminated string comprised of at least 24 hex chars. */ -void bson_oid_from_string( bson_oid_t *oid, const char *str ); +MONGO_EXPORT void bson_oid_from_string( bson_oid_t *oid, const char *str ); /** * Create a string representation of the bson_oid_t. @@ -467,14 +523,14 @@ void bson_oid_from_string( bson_oid_t *oid, const char *str ); * @param oid the bson_oid_t source. * @param str the string representation destination. */ -void bson_oid_to_string( const bson_oid_t *oid, char *str ); +MONGO_EXPORT void bson_oid_to_string( const bson_oid_t *oid, char *str ); /** * Create a bson_oid object. * * @param oid the destination for the newly created bson_oid_t. */ -void bson_oid_gen( bson_oid_t *oid ); +MONGO_EXPORT void bson_oid_gen( bson_oid_t *oid ); /** * Set a function to be used to generate the second four bytes @@ -482,7 +538,7 @@ void bson_oid_gen( bson_oid_t *oid ); * * @param func a pointer to a function that returns an int. */ -void bson_set_oid_fuzz( int ( *func )( void ) ); +MONGO_EXPORT void bson_set_oid_fuzz( int ( *func )( void ) ); /** * Set a function to be used to generate the incrementing part @@ -491,14 +547,14 @@ void bson_set_oid_fuzz( int ( *func )( void ) ); * * @param func a pointer to a function that returns an int. */ -void bson_set_oid_inc( int ( *func )( void ) ); +MONGO_EXPORT void bson_set_oid_inc( int ( *func )( void ) ); /** * Get the time a bson_oid_t was created. * * @param oid the bson_oid_t. */ -time_t bson_oid_generated_time( bson_oid_t *oid ); /* Gives the time the OID was created */ +MONGO_EXPORT time_t bson_oid_generated_time( bson_oid_t *oid ); /* Gives the time the OID was created */ /* ---------------------------- BUILDING @@ -512,7 +568,7 @@ time_t bson_oid_generated_time( bson_oid_t *oid ); /* Gives the time the OID was * @note When finished, you must pass the bson object to * bson_destroy( ). */ -void bson_init( bson *b ); +MONGO_EXPORT void bson_init( bson *b ); /** * Initialize a BSON object, and point its data @@ -524,6 +580,7 @@ void bson_init( bson *b ); * @return BSON_OK or BSON_ERROR. */ int bson_init_data( bson *b , char *data ); +int bson_init_finished_data( bson *b, char *data ) ; /** * Initialize a BSON object, and set its @@ -555,7 +612,7 @@ int bson_ensure_space( bson *b, const int bytesNeeded ); * @return the standard error code. To deallocate memory, * call bson_destroy on the bson object. */ -int bson_finish( bson *b ); +MONGO_EXPORT int bson_finish( bson *b ); /** * Destroy a bson object. @@ -563,7 +620,7 @@ int bson_finish( bson *b ); * @param b the bson object to destroy. * */ -void bson_destroy( bson *b ); +MONGO_EXPORT void bson_destroy( bson *b ); /** * Returns a pointer to a static empty BSON object. @@ -573,23 +630,17 @@ void bson_destroy( bson *b ); * @return the empty initialized BSON object. */ /* returns pointer to static empty bson object */ -bson *bson_empty( bson *obj ); - -/** - * Copy BSON data only from one object to another. - * - * @param out the copy destination BSON object. - * @param in the copy source BSON object. - */ -void bson_copy_basic( bson *out, const bson *in ); +MONGO_EXPORT bson *bson_empty( bson *obj ); /** * Make a complete copy of the a BSON object. + * The source bson object must be in a finished + * state; otherwise, the copy will fail. * * @param out the copy destination BSON object. * @param in the copy source BSON object. */ -void bson_copy( bson *out, const bson *in ); /* puts data in new buffer. NOOP if out==NULL */ +MONGO_EXPORT int bson_copy( bson *out, const bson *in ); /* puts data in new buffer. NOOP if out==NULL */ /** * Append a previously created bson_oid_t to a bson object. @@ -600,7 +651,7 @@ void bson_copy( bson *out, const bson *in ); /* puts data in new buffer. NOOP if * * @return BSON_OK or BSON_ERROR. */ -int bson_append_oid( bson *b, const char *name, const bson_oid_t *oid ); +MONGO_EXPORT int bson_append_oid( bson *b, const char *name, const bson_oid_t *oid ); /** * Append a bson_oid_t to a bson. @@ -610,7 +661,7 @@ int bson_append_oid( bson *b, const char *name, const bson_oid_t *oid ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_new_oid( bson *b, const char *name ); +MONGO_EXPORT int bson_append_new_oid( bson *b, const char *name ); /** * Append an int to a bson. @@ -621,7 +672,7 @@ int bson_append_new_oid( bson *b, const char *name ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_int( bson *b, const char *name, const int i ); +MONGO_EXPORT int bson_append_int( bson *b, const char *name, const int i ); /** * Append an long to a bson. @@ -632,7 +683,7 @@ int bson_append_int( bson *b, const char *name, const int i ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_long( bson *b, const char *name, const int64_t i ); +MONGO_EXPORT int bson_append_long( bson *b, const char *name, const int64_t i ); /** * Append an double to a bson. @@ -643,7 +694,7 @@ int bson_append_long( bson *b, const char *name, const int64_t i ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_double( bson *b, const char *name, const double d ); +MONGO_EXPORT int bson_append_double( bson *b, const char *name, const double d ); /** * Append a string to a bson. @@ -654,7 +705,7 @@ int bson_append_double( bson *b, const char *name, const double d ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_string( bson *b, const char *name, const char *str ); +MONGO_EXPORT int bson_append_string( bson *b, const char *name, const char *str ); /** * Append len bytes of a string to a bson. @@ -666,7 +717,7 @@ int bson_append_string( bson *b, const char *name, const char *str ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_string_n( bson *b, const char *name, const char *str, int len ); +MONGO_EXPORT int bson_append_string_n( bson *b, const char *name, const char *str, int len ); /** * Append a symbol to a bson. @@ -677,7 +728,7 @@ int bson_append_string_n( bson *b, const char *name, const char *str, int len ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_symbol( bson *b, const char *name, const char *str ); +MONGO_EXPORT int bson_append_symbol( bson *b, const char *name, const char *str ); /** * Append len bytes of a symbol to a bson. @@ -689,7 +740,7 @@ int bson_append_symbol( bson *b, const char *name, const char *str ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_symbol_n( bson *b, const char *name, const char *str, int len ); +MONGO_EXPORT int bson_append_symbol_n( bson *b, const char *name, const char *str, int len ); /** * Append code to a bson. @@ -701,7 +752,7 @@ int bson_append_symbol_n( bson *b, const char *name, const char *str, int len ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_code( bson *b, const char *name, const char *str ); +MONGO_EXPORT int bson_append_code( bson *b, const char *name, const char *str ); /** * Append len bytes of code to a bson. @@ -713,7 +764,7 @@ int bson_append_code( bson *b, const char *name, const char *str ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_code_n( bson *b, const char *name, const char *str, int len ); +MONGO_EXPORT int bson_append_code_n( bson *b, const char *name, const char *str, int len ); /** * Append code to a bson with scope. @@ -725,7 +776,7 @@ int bson_append_code_n( bson *b, const char *name, const char *str, int len ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_code_w_scope( bson *b, const char *name, const char *code, const bson *scope ); +MONGO_EXPORT int bson_append_code_w_scope( bson *b, const char *name, const char *code, const bson *scope ); /** * Append len bytes of code to a bson with scope. @@ -738,7 +789,7 @@ int bson_append_code_w_scope( bson *b, const char *name, const char *code, const * * @return BSON_OK or BSON_ERROR. */ -int bson_append_code_w_scope_n( bson *b, const char *name, const char *code, int size, const bson *scope ); +MONGO_EXPORT int bson_append_code_w_scope_n( bson *b, const char *name, const char *code, int size, const bson *scope ); /** * Append binary data to a bson. @@ -751,7 +802,7 @@ int bson_append_code_w_scope_n( bson *b, const char *name, const char *code, int * * @return BSON_OK or BSON_ERROR. */ -int bson_append_binary( bson *b, const char *name, char type, const char *str, int len ); +MONGO_EXPORT int bson_append_binary( bson *b, const char *name, char type, const char *str, int len ); /** * Append a bson_bool_t to a bson. @@ -762,7 +813,7 @@ int bson_append_binary( bson *b, const char *name, char type, const char *str, i * * @return BSON_OK or BSON_ERROR. */ -int bson_append_bool( bson *b, const char *name, const bson_bool_t v ); +MONGO_EXPORT int bson_append_bool( bson *b, const char *name, const bson_bool_t v ); /** * Append a null value to a bson. @@ -772,7 +823,7 @@ int bson_append_bool( bson *b, const char *name, const bson_bool_t v ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_null( bson *b, const char *name ); +MONGO_EXPORT int bson_append_null( bson *b, const char *name ); /** * Append an undefined value to a bson. @@ -782,7 +833,7 @@ int bson_append_null( bson *b, const char *name ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_undefined( bson *b, const char *name ); +MONGO_EXPORT int bson_append_undefined( bson *b, const char *name ); /** * Append a regex value to a bson. @@ -794,7 +845,7 @@ int bson_append_undefined( bson *b, const char *name ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_regex( bson *b, const char *name, const char *pattern, const char *opts ); +MONGO_EXPORT int bson_append_regex( bson *b, const char *name, const char *pattern, const char *opts ); /** * Append bson data to a bson. @@ -805,7 +856,7 @@ int bson_append_regex( bson *b, const char *name, const char *pattern, const cha * * @return BSON_OK or BSON_ERROR. */ -int bson_append_bson( bson *b, const char *name, const bson *bson ); +MONGO_EXPORT int bson_append_bson( bson *b, const char *name, const bson *bson ); /** * Append a BSON element to a bson from the current point of an iterator. @@ -816,7 +867,7 @@ int bson_append_bson( bson *b, const char *name, const bson *bson ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_element( bson *b, const char *name_or_null, const bson_iterator *elem ); +MONGO_EXPORT int bson_append_element( bson *b, const char *name_or_null, const bson_iterator *elem ); /** * Append a bson_timestamp_t value to a bson. @@ -827,7 +878,8 @@ int bson_append_element( bson *b, const char *name_or_null, const bson_iterator * * @return BSON_OK or BSON_ERROR. */ -int bson_append_timestamp( bson *b, const char *name, bson_timestamp_t *ts ); +MONGO_EXPORT int bson_append_timestamp( bson *b, const char *name, bson_timestamp_t *ts ); +MONGO_EXPORT int bson_append_timestamp2( bson *b, const char *name, int time, int increment ); /* these both append a bson_date */ /** @@ -839,7 +891,7 @@ int bson_append_timestamp( bson *b, const char *name, bson_timestamp_t *ts ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_date( bson *b, const char *name, bson_date_t millis ); +MONGO_EXPORT int bson_append_date( bson *b, const char *name, bson_date_t millis ); /** * Append a time_t value to a bson. @@ -850,7 +902,7 @@ int bson_append_date( bson *b, const char *name, bson_date_t millis ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_time_t( bson *b, const char *name, time_t secs ); +MONGO_EXPORT int bson_append_time_t( bson *b, const char *name, time_t secs ); /** * Start appending a new object to a bson. @@ -860,7 +912,7 @@ int bson_append_time_t( bson *b, const char *name, time_t secs ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_start_object( bson *b, const char *name ); +MONGO_EXPORT int bson_append_start_object( bson *b, const char *name ); /** * Start appending a new array to a bson. @@ -870,7 +922,7 @@ int bson_append_start_object( bson *b, const char *name ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_start_array( bson *b, const char *name ); +MONGO_EXPORT int bson_append_start_array( bson *b, const char *name ); /** * Finish appending a new object or array to a bson. @@ -879,7 +931,7 @@ int bson_append_start_array( bson *b, const char *name ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_finish_object( bson *b ); +MONGO_EXPORT int bson_append_finish_object( bson *b ); /** * Finish appending a new object or array to a bson. This @@ -889,13 +941,13 @@ int bson_append_finish_object( bson *b ); * * @return BSON_OK or BSON_ERROR. */ -int bson_append_finish_array( bson *b ); +MONGO_EXPORT int bson_append_finish_array( bson *b ); void bson_numstr( char *str, int i ); void bson_incnumstr( char *str ); -/* Error handling and stadard library function over-riding. */ +/* Error handling and standard library function over-riding. */ /* -------------------------------------------------------- */ /* bson_err_handlers shouldn't return!!! */ @@ -912,7 +964,6 @@ extern void ( *bson_free )( void * ); extern bson_printf_func bson_printf; extern bson_fprintf_func bson_fprintf; extern bson_sprintf_func bson_sprintf; - extern bson_printf_func bson_errprintf; /** @@ -924,7 +975,7 @@ extern bson_printf_func bson_errprintf; * * @sa malloc(3) */ -void *bson_malloc( int size ); +MONGO_EXPORT void *bson_malloc( int size ); /** * Changes the size of allocated memory and checks return value, @@ -946,7 +997,7 @@ void *bson_realloc( void *ptr, int size ); * * @return the old error handling function, or NULL. */ -bson_err_handler set_bson_err_handler( bson_err_handler func ); +MONGO_EXPORT bson_err_handler set_bson_err_handler( bson_err_handler func ); /* does nothing if ok != 0 */ /** @@ -971,5 +1022,15 @@ void bson_fatal_msg( int ok, const char *msg ); */ void bson_builder_error( bson *b ); +/** + * Cast an int64_t to double. This is necessary for embedding in + * certain environments. + * + */ +MONGO_EXPORT double bson_int64_to_double( int64_t i64 ); + +MONGO_EXPORT void bson_swap_endian32( void *outp, const void *inp ); +MONGO_EXPORT void bson_swap_endian64( void *outp, const void *inp ); + MONGO_EXTERN_C_END #endif diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/encoding.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/encoding.c index 8d2da1502f..00ecf18c92 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/encoding.c +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/encoding.c @@ -1,5 +1,5 @@ /* - * Copyright 2009-2011 10gen, Inc. + * Copyright 2009-2012 10gen, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/encoding.h b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/encoding.h index a2a07c6d5d..f13c31ee89 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/encoding.h +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/encoding.h @@ -1,5 +1,5 @@ /* - * Copyright 2009-2011 10gen, Inc. + * Copyright 2009-2012 10gen, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef _BSON_ENCODING_H_ -#define _BSON_ENCODING_H_ +#ifndef BSON_ENCODING_H_ +#define BSON_ENCODING_H_ MONGO_EXTERN_C_START diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env.h b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env.h new file mode 100644 index 0000000000..463ee32f79 --- /dev/null +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env.h @@ -0,0 +1,39 @@ +/** @file env.h */ + +/* Copyright 2009-2012 10gen Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Header for generic net.h */ +#ifndef MONGO_ENV_H_ +#define MONGO_ENV_H_ + +#include "mongo.h" + +MONGO_EXTERN_C_START + +/* This is a no-op in the generic implementation. */ +int mongo_env_set_socket_op_timeout( mongo *conn, int millis ); +int mongo_env_read_socket( mongo *conn, void *buf, int len ); +int mongo_env_write_socket( mongo *conn, const void *buf, int len ); +int mongo_env_socket_connect( mongo *conn, const char *host, int port ); + +/* Initialize socket services */ +MONGO_EXPORT int mongo_env_sock_init( void ); + +/* Close a socket */ +MONGO_EXPORT int mongo_env_close_socket( int socket ); + +MONGO_EXTERN_C_END +#endif diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env_posix.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env_posix.c new file mode 100644 index 0000000000..f1020ca80d --- /dev/null +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env_posix.c @@ -0,0 +1,165 @@ +/* env_posix.c */ + +/* Copyright 2009-2012 10gen Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Networking and other niceties for POSIX systems. */ +#include "env.h" +#include "mongo.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef NI_MAXSERV +# define NI_MAXSERV 32 +#endif + +int mongo_env_close_socket( int socket ) { + return close( socket ); +} + +int mongo_env_sock_init( void ) { + return 0; +} + +int mongo_env_write_socket( mongo *conn, const void *buf, int len ) { + const char *cbuf = buf; +#ifdef __APPLE__ + int flags = 0; +#else + int flags = MSG_NOSIGNAL; +#endif + + while ( len ) { + int sent = send( conn->sock, cbuf, len, flags ); + if ( sent == -1 ) { + if (errno == EPIPE) + conn->connected = 0; + __mongo_set_error( conn, MONGO_IO_ERROR, strerror( errno ), errno ); + return MONGO_ERROR; + } + cbuf += sent; + len -= sent; + } + + return MONGO_OK; +} + +int mongo_env_read_socket( mongo *conn, void *buf, int len ) { + char *cbuf = buf; + while ( len ) { + int sent = recv( conn->sock, cbuf, len, 0 ); + if ( sent == 0 || sent == -1 ) { + __mongo_set_error( conn, MONGO_IO_ERROR, strerror( errno ), errno ); + return MONGO_ERROR; + } + cbuf += sent; + len -= sent; + } + + return MONGO_OK; +} + +int mongo_env_set_socket_op_timeout( mongo *conn, int millis ) { + struct timeval tv; + tv.tv_sec = millis / 1000; + tv.tv_usec = ( millis % 1000 ) * 1000; + + if ( setsockopt( conn->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof( tv ) ) == -1 ) { + conn->err = MONGO_IO_ERROR; + __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_RCVTIMEO failed.", errno ); + return MONGO_ERROR; + } + + if ( setsockopt( conn->sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof( tv ) ) == -1 ) { + __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_SNDTIMEO failed.", errno ); + return MONGO_ERROR; + } + + return MONGO_OK; +} + +int mongo_env_socket_connect( mongo *conn, const char *host, int port ) { + char port_str[NI_MAXSERV]; + int status; + + struct addrinfo ai_hints; + struct addrinfo *ai_list = NULL; + struct addrinfo *ai_ptr = NULL; + + conn->sock = 0; + conn->connected = 0; + sprintf(port_str,"%d",port); + + bson_sprintf( port_str, "%d", port ); + + memset( &ai_hints, 0, sizeof( ai_hints ) ); +#ifdef AI_ADDRCONFIG + ai_hints.ai_flags = AI_ADDRCONFIG; +#endif + ai_hints.ai_family = AF_UNSPEC; + ai_hints.ai_socktype = SOCK_STREAM; + + status = getaddrinfo( host, port_str, &ai_hints, &ai_list ); + if ( status != 0 ) { + bson_errprintf( "getaddrinfo failed: %s", gai_strerror( status ) ); + conn->err = MONGO_CONN_ADDR_FAIL; + return MONGO_ERROR; + } + + for ( ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) { + conn->sock = socket( ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol ); + if ( conn->sock < 0 ) { + conn->sock = 0; + continue; + } + + status = connect( conn->sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen ); + if ( status != 0 ) { + mongo_env_close_socket( conn->sock ); + conn->sock = 0; + continue; + } + + if ( ai_ptr->ai_protocol == IPPROTO_TCP ) { + int flag = 1; + + setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, + ( void * ) &flag, sizeof( flag ) ); + if ( conn->op_timeout_ms > 0 ) + mongo_env_set_socket_op_timeout( conn, conn->op_timeout_ms ); + } + + conn->connected = 1; + break; + } + + freeaddrinfo( ai_list ); + + if ( ! conn->connected ) { + conn->err = MONGO_CONN_FAIL; + return MONGO_ERROR; + } + + return MONGO_OK; +} diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env_standard.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env_standard.c new file mode 100644 index 0000000000..36fa9f65c4 --- /dev/null +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env_standard.c @@ -0,0 +1,168 @@ +/* env_standard.c */ + +/* Copyright 2009-2012 10gen Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Vanilla networking designed to work on all systems. */ +#include "env.h" +#include +#include + +#ifdef _WIN32 + #ifdef _MSC_VER + #include // send,recv,socklen_t etc + #include // addrinfo + #else + #include + #include + typedef int socklen_t; + #endif +#else +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#ifndef NI_MAXSERV +# define NI_MAXSERV 32 +#endif + +int mongo_env_close_socket( int socket ) { +#ifdef _WIN32 + return closesocket( socket ); +#else + return close( socket ); +#endif +} + +int mongo_env_write_socket( mongo *conn, const void *buf, int len ) { + const char *cbuf = buf; +#ifdef _WIN32 + int flags = 0; +#else +#ifdef __APPLE__ + int flags = 0; +#else + int flags = MSG_NOSIGNAL; +#endif +#endif + + while ( len ) { + int sent = send( conn->sock, cbuf, len, flags ); + if ( sent == -1 ) { + if (errno == EPIPE) + conn->connected = 0; + conn->err = MONGO_IO_ERROR; + return MONGO_ERROR; + } + cbuf += sent; + len -= sent; + } + + return MONGO_OK; +} + +int mongo_env_read_socket( mongo *conn, void *buf, int len ) { + char *cbuf = buf; + while ( len ) { + int sent = recv( conn->sock, cbuf, len, 0 ); + if ( sent == 0 || sent == -1 ) { + conn->err = MONGO_IO_ERROR; + return MONGO_ERROR; + } + cbuf += sent; + len -= sent; + } + + return MONGO_OK; +} + +/* This is a no-op in the generic implementation. */ +int mongo_env_set_socket_op_timeout( mongo *conn, int millis ) { + return MONGO_OK; +} + +int mongo_env_socket_connect( mongo *conn, const char *host, int port ) { + struct sockaddr_in sa; + socklen_t addressSize; + int flag = 1; + + if ( ( conn->sock = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) { + conn->sock = 0; + conn->err = MONGO_CONN_NO_SOCKET; + return MONGO_ERROR; + } + + memset( sa.sin_zero , 0 , sizeof( sa.sin_zero ) ); + sa.sin_family = AF_INET; + sa.sin_port = htons( port ); + sa.sin_addr.s_addr = inet_addr( host ); + addressSize = sizeof( sa ); + + if ( connect( conn->sock, ( struct sockaddr * )&sa, addressSize ) == -1 ) { + mongo_env_close_socket( conn->sock ); + conn->connected = 0; + conn->sock = 0; + conn->err = MONGO_CONN_FAIL; + return MONGO_ERROR; + } + + setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, ( char * ) &flag, sizeof( flag ) ); + + if( conn->op_timeout_ms > 0 ) + mongo_env_set_socket_op_timeout( conn, conn->op_timeout_ms ); + + conn->connected = 1; + + return MONGO_OK; +} + +MONGO_EXPORT int mongo_env_sock_init( void ) { + +#if defined(_WIN32) + WSADATA wsaData; + WORD wVers; +#elif defined(SIGPIPE) + struct sigaction act; +#endif + + static int called_once; + static int retval; + if (called_once) return retval; + called_once = 1; + +#if defined(_WIN32) + wVers = MAKEWORD(1, 1); + retval = (WSAStartup(wVers, &wsaData) == 0); +#elif defined(MACINTOSH) + GUSISetup(GUSIwithInternetSockets); + retval = 1; +#elif defined(SIGPIPE) + retval = 1; + if (sigaction(SIGPIPE, (struct sigaction *)NULL, &act) < 0) + retval = 0; + else if (act.sa_handler == SIG_DFL) { + act.sa_handler = SIG_IGN; + if (sigaction(SIGPIPE, &act, (struct sigaction *)NULL) < 0) + retval = 0; + } +#endif + return retval; +} diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env_win32.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env_win32.c new file mode 100644 index 0000000000..0b301cea61 --- /dev/null +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/env_win32.c @@ -0,0 +1,183 @@ +/* env_win32.c */ + +/* Copyright 2009-2012 10gen Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Networking and other niceties for WIN32. */ +#include "env.h" +#include "mongo.h" +#include + +#ifdef _MSC_VER +#include // send,recv,socklen_t etc +#include // addrinfo +#else +#include +#include +typedef int socklen_t; +#endif + +#ifndef NI_MAXSERV +# define NI_MAXSERV 32 +#endif + +static void mongo_clear_errors( mongo *conn ) { + conn->err = 0; + memset( conn->errstr, 0, MONGO_ERR_LEN ); +} + +int mongo_env_close_socket( int socket ) { + return closesocket( socket ); +} + +int mongo_env_write_socket( mongo *conn, const void *buf, int len ) { + const char *cbuf = buf; + int flags = 0; + + while ( len ) { + int sent = send( conn->sock, cbuf, len, flags ); + if ( sent == -1 ) { + __mongo_set_error( conn, MONGO_IO_ERROR, NULL, WSAGetLastError() ); + conn->connected = 0; + return MONGO_ERROR; + } + cbuf += sent; + len -= sent; + } + + return MONGO_OK; +} + +int mongo_env_read_socket( mongo *conn, void *buf, int len ) { + char *cbuf = buf; + + while ( len ) { + int sent = recv( conn->sock, cbuf, len, 0 ); + if ( sent == 0 || sent == -1 ) { + __mongo_set_error( conn, MONGO_IO_ERROR, NULL, WSAGetLastError() ); + return MONGO_ERROR; + } + cbuf += sent; + len -= sent; + } + + return MONGO_OK; +} + +int mongo_env_set_socket_op_timeout( mongo *conn, int millis ) { + if ( setsockopt( conn->sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&millis, + sizeof( millis ) ) == -1 ) { + __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_RCVTIMEO failed.", + WSAGetLastError() ); + return MONGO_ERROR; + } + + if ( setsockopt( conn->sock, SOL_SOCKET, SO_SNDTIMEO, (const char *)&millis, + sizeof( millis ) ) == -1 ) { + __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_SNDTIMEO failed.", + WSAGetLastError() ); + return MONGO_ERROR; + } + + return MONGO_OK; +} + +int mongo_env_socket_connect( mongo *conn, const char *host, int port ) { + char port_str[NI_MAXSERV]; + char errstr[MONGO_ERR_LEN]; + int status; + + struct addrinfo ai_hints; + struct addrinfo *ai_list = NULL; + struct addrinfo *ai_ptr = NULL; + + conn->sock = 0; + conn->connected = 0; + + bson_sprintf( port_str, "%d", port ); + + memset( &ai_hints, 0, sizeof( ai_hints ) ); + ai_hints.ai_family = AF_UNSPEC; + ai_hints.ai_socktype = SOCK_STREAM; + ai_hints.ai_protocol = IPPROTO_TCP; + + status = getaddrinfo( host, port_str, &ai_hints, &ai_list ); + if ( status != 0 ) { + bson_sprintf( errstr, "getaddrinfo failed with error %d", status ); + __mongo_set_error( conn, MONGO_CONN_ADDR_FAIL, errstr, WSAGetLastError() ); + return MONGO_ERROR; + } + + for ( ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) { + conn->sock = socket( ai_ptr->ai_family, ai_ptr->ai_socktype, + ai_ptr->ai_protocol ); + + if ( conn->sock < 0 ) { + __mongo_set_error( conn, MONGO_SOCKET_ERROR, "socket() failed", + WSAGetLastError() ); + conn->sock = 0; + continue; + } + + status = connect( conn->sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen ); + if ( status != 0 ) { + __mongo_set_error( conn, MONGO_SOCKET_ERROR, "connect() failed", + WSAGetLastError() ); + mongo_env_close_socket( conn->sock ); + conn->sock = 0; + continue; + } + + if ( ai_ptr->ai_protocol == IPPROTO_TCP ) { + int flag = 1; + + setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, + ( void * ) &flag, sizeof( flag ) ); + + if ( conn->op_timeout_ms > 0 ) + mongo_env_set_socket_op_timeout( conn, conn->op_timeout_ms ); + } + + conn->connected = 1; + break; + } + + freeaddrinfo( ai_list ); + + if ( ! conn->connected ) { + conn->err = MONGO_CONN_FAIL; + return MONGO_ERROR; + } + else { + mongo_clear_errors( conn ); + return MONGO_OK; + } +} + +MONGO_EXPORT int mongo_env_sock_init( void ) { + + WSADATA wsaData; + WORD wVers; + static int called_once; + static int retval; + + if (called_once) return retval; + + called_once = 1; + wVers = MAKEWORD(1, 1); + retval = (WSAStartup(wVers, &wsaData) == 0); + + return retval; +} diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/gridfs.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/gridfs.c index f51b397d3f..9b2d1455ef 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/gridfs.c +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/gridfs.c @@ -1,6 +1,6 @@ /* gridfs.c */ -/* Copyright 2009-2011 10gen Inc. +/* Copyright 2009-2012 10gen Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,27 @@ #include #include +MONGO_EXPORT gridfs* gridfs_create() { + return (gridfs*)bson_malloc(sizeof(gridfs)); +} + +MONGO_EXPORT void gridfs_dispose(gridfs* gfs) { + free(gfs); +} + +MONGO_EXPORT gridfile* gridfile_create() { + return (gridfile*)bson_malloc(sizeof(gridfile)); +} + +MONGO_EXPORT void gridfile_dispose(gridfile* gf) { + free(gf); +} + +MONGO_EXPORT void gridfile_get_descriptor(gridfile* gf, bson* out) { + *out = *gf->meta; +} + + static bson *chunk_new( bson_oid_t id, int chunkNumber, const char *data, int len ) { bson *b = bson_malloc( sizeof( bson ) ); @@ -104,7 +125,7 @@ int gridfs_init( mongo *client, const char *dbname, const char *prefix, return MONGO_OK; } -void gridfs_destroy( gridfs *gfs ) { +MONGO_EXPORT void gridfs_destroy( gridfs *gfs ) { if ( gfs == NULL ) return; if ( gfs->dbname ) bson_free( ( char * )gfs->dbname ); if ( gfs->prefix ) bson_free( ( char * )gfs->prefix ); @@ -120,14 +141,17 @@ static int gridfs_insert_file( gridfs *gfs, const char *name, bson res; bson_iterator it; int result; + int64_t d; /* Check run md5 */ bson_init( &command ); bson_append_oid( &command, "filemd5", &id ); bson_append_string( &command, "root", gfs->prefix ); bson_finish( &command ); - assert( mongo_run_command( gfs->client, gfs->dbname, &command, &res ) == MONGO_OK ); + result = mongo_run_command( gfs->client, gfs->dbname, &command, &res ); bson_destroy( &command ); + if (result != MONGO_OK) + return result; /* Create and insert BSON for file metadata */ bson_init( &ret ); @@ -137,7 +161,8 @@ static int gridfs_insert_file( gridfs *gfs, const char *name, } bson_append_long( &ret, "length", length ); bson_append_int( &ret, "chunkSize", DEFAULT_CHUNK_SIZE ); - bson_append_date( &ret, "uploadDate", ( bson_date_t )1000*time( NULL ) ); + d = ( bson_date_t )1000*time( NULL ); + bson_append_date( &ret, "uploadDate", d); bson_find( &it, &res, "md5" ); bson_append_string( &ret, "md5", bson_iterator_string( &it ) ); bson_destroy( &res ); @@ -151,7 +176,7 @@ static int gridfs_insert_file( gridfs *gfs, const char *name, return result; } -int gridfs_store_buffer( gridfs *gfs, const char *data, +MONGO_EXPORT int gridfs_store_buffer( gridfs *gfs, const char *data, gridfs_offset length, const char *remotename, const char *contenttype ) { @@ -163,7 +188,7 @@ int gridfs_store_buffer( gridfs *gfs, const char *data, bson *oChunk; /* Large files Assertion */ - assert( length <= 0xffffffff ); + /* assert( length <= 0xffffffff ); */ /* Generate and append an oid*/ bson_oid_gen( &id ); @@ -183,7 +208,7 @@ int gridfs_store_buffer( gridfs *gfs, const char *data, return gridfs_insert_file( gfs, remotename, id, length, contenttype ); } -void gridfile_writer_init( gridfile *gfile, gridfs *gfs, +MONGO_EXPORT void gridfile_writer_init( gridfile *gfile, gridfs *gfs, const char *remote_name, const char *content_type ) { gfile->gfs = gfs; @@ -200,7 +225,7 @@ void gridfile_writer_init( gridfile *gfile, gridfs *gfs, strcpy( ( char * )gfile->content_type, content_type ); } -void gridfile_write_buffer( gridfile *gfile, const char *data, +MONGO_EXPORT void gridfile_write_buffer( gridfile *gfile, const char *data, gridfs_offset length ) { int bytes_left = 0; @@ -221,14 +246,13 @@ void gridfile_write_buffer( gridfile *gfile, const char *data, gfile->pending_len += length; } else { /* At least one chunk of data to write */ + chunks_to_write = to_write / DEFAULT_CHUNK_SIZE; + bytes_left = to_write % DEFAULT_CHUNK_SIZE; /* If there's a pending chunk to be written, we need to combine * the buffer provided up to DEFAULT_CHUNK_SIZE. */ if ( gfile->pending_len > 0 ) { - chunks_to_write = to_write / DEFAULT_CHUNK_SIZE; - bytes_left = to_write % DEFAULT_CHUNK_SIZE; - data_partial_len = DEFAULT_CHUNK_SIZE - gfile->pending_len; buffer = ( char * )bson_malloc( DEFAULT_CHUNK_SIZE ); memcpy( buffer, gfile->pending_data, gfile->pending_len ); @@ -270,7 +294,7 @@ void gridfile_write_buffer( gridfile *gfile, const char *data, } } -int gridfile_writer_done( gridfile *gfile ) { +MONGO_EXPORT int gridfile_writer_done( gridfile *gfile ) { /* write any remaining pending chunk data. * pending data will always take up less than one chunk */ @@ -307,8 +331,11 @@ int gridfs_store_file( gridfs *gfs, const char *filename, /* Open the file and the correct stream */ if ( strcmp( filename, "-" ) == 0 ) fd = stdin; - else fd = fopen( filename, "rb" ); - assert( fd != NULL ); /* No such file */ + else { + fd = fopen( filename, "rb" ); + if (fd == NULL) + return MONGO_ERROR; + } /* Generate and append an oid*/ bson_oid_gen( &id ); @@ -339,7 +366,7 @@ int gridfs_store_file( gridfs *gfs, const char *filename, return gridfs_insert_file( gfs, remotename, id, length, contenttype ); } -void gridfs_remove_filename( gridfs *gfs, const char *filename ) { +MONGO_EXPORT void gridfs_remove_filename( gridfs *gfs, const char *filename ) { bson query; mongo_cursor *files; bson file; @@ -433,7 +460,7 @@ int gridfile_init( gridfs *gfs, bson *meta, gridfile *gfile ) return MONGO_OK; } -void gridfile_destroy( gridfile *gfile ) +MONGO_EXPORT void gridfile_destroy( gridfile *gfile ) { bson_destroy( gfile->meta ); @@ -444,21 +471,21 @@ bson_bool_t gridfile_exists( gridfile *gfile ) { return ( bson_bool_t )( gfile != NULL || gfile->meta == NULL ); } -const char *gridfile_get_filename( gridfile *gfile ) { +MONGO_EXPORT const char *gridfile_get_filename( gridfile *gfile ) { bson_iterator it; bson_find( &it, gfile->meta, "filename" ); return bson_iterator_string( &it ); } -int gridfile_get_chunksize( gridfile *gfile ) { +MONGO_EXPORT int gridfile_get_chunksize( gridfile *gfile ) { bson_iterator it; bson_find( &it, gfile->meta, "chunkSize" ); return bson_iterator_int( &it ); } -gridfs_offset gridfile_get_contentlength( gridfile *gfile ) { +MONGO_EXPORT gridfs_offset gridfile_get_contentlength( gridfile *gfile ) { bson_iterator it; bson_find( &it, gfile->meta, "length" ); @@ -469,7 +496,7 @@ gridfs_offset gridfile_get_contentlength( gridfile *gfile ) { return ( gridfs_offset )bson_iterator_long( &it ); } -const char *gridfile_get_contenttype( gridfile *gfile ) { +MONGO_EXPORT const char *gridfile_get_contenttype( gridfile *gfile ) { bson_iterator it; if ( bson_find( &it, gfile->meta, "contentType" ) ) @@ -477,14 +504,14 @@ const char *gridfile_get_contenttype( gridfile *gfile ) { else return NULL; } -bson_date_t gridfile_get_uploaddate( gridfile *gfile ) { +MONGO_EXPORT bson_date_t gridfile_get_uploaddate( gridfile *gfile ) { bson_iterator it; bson_find( &it, gfile->meta, "uploadDate" ); return bson_iterator_date( &it ); } -const char *gridfile_get_md5( gridfile *gfile ) { +MONGO_EXPORT const char *gridfile_get_md5( gridfile *gfile ) { bson_iterator it; bson_find( &it, gfile->meta, "md5" ); @@ -505,20 +532,16 @@ bson_bool_t gridfile_get_boolean( gridfile *gfile, const char *name ) { return bson_iterator_bool( &it ); } -bson gridfile_get_metadata( gridfile *gfile ) { - bson sub; +MONGO_EXPORT void gridfile_get_metadata( gridfile *gfile, bson* out ) { bson_iterator it; - if ( bson_find( &it, gfile->meta, "metadata" ) ) { - bson_iterator_subobject( &it, &sub ); - return sub; - } else { - bson_empty( &sub ); - return sub; - } + if ( bson_find( &it, gfile->meta, "metadata" ) ) + bson_iterator_subobject( &it, out ); + else + bson_empty( out ); } -int gridfile_get_numchunks( gridfile *gfile ) { +MONGO_EXPORT int gridfile_get_numchunks( gridfile *gfile ) { bson_iterator it; gridfs_offset length; gridfs_offset chunkSize; @@ -539,11 +562,12 @@ int gridfile_get_numchunks( gridfile *gfile ) { : ( int )( numchunks ); } -bson gridfile_get_chunk( gridfile *gfile, int n ) { +MONGO_EXPORT void gridfile_get_chunk( gridfile *gfile, int n, bson* out ) { bson query; - bson out; + bson_iterator it; bson_oid_t id; + int result; bson_init( &query ); bson_find( &it, gfile->meta, "_id" ); @@ -552,15 +576,18 @@ bson gridfile_get_chunk( gridfile *gfile, int n ) { bson_append_int( &query, "n", n ); bson_finish( &query ); - assert( mongo_find_one( gfile->gfs->client, - gfile->gfs->chunks_ns, - &query, NULL, &out ) == MONGO_OK ); - + result = (mongo_find_one(gfile->gfs->client, + gfile->gfs->chunks_ns, + &query, NULL, out ) == MONGO_OK ); bson_destroy( &query ); - return out; + if (!result) { + bson empty; + bson_empty(&empty); + bson_copy(out, &empty); + } } -mongo_cursor *gridfile_get_chunks( gridfile *gfile, int start, int size ) { +MONGO_EXPORT mongo_cursor *gridfile_get_chunks( gridfile *gfile, int start, int size ) { bson_iterator it; bson_oid_t id; bson gte; @@ -613,18 +640,18 @@ gridfs_offset gridfile_write_file( gridfile *gfile, FILE *stream ) { const int num = gridfile_get_numchunks( gfile ); for ( i=0; i Length of pending_data buffer */ } gridfile; +MONGO_EXPORT gridfs* gridfs_create(); +MONGO_EXPORT void gridfs_dispose(gridfs* gfs); +MONGO_EXPORT gridfile* gridfile_create(); +MONGO_EXPORT void gridfile_dispose(gridfile* gf); +MONGO_EXPORT void gridfile_get_descriptor(gridfile* gf, bson* out); + /** * Initializes a GridFS object * @param client - db connection @@ -60,7 +66,7 @@ typedef struct { * * @return - MONGO_OK or MONGO_ERROR. */ -int gridfs_init( mongo *client, const char *dbname, +MONGO_EXPORT int gridfs_init( mongo *client, const char *dbname, const char *prefix, gridfs *gfs ); /** @@ -69,7 +75,7 @@ int gridfs_init( mongo *client, const char *dbname, * * @param gfs a grid */ -void gridfs_destroy( gridfs *gfs ); +MONGO_EXPORT void gridfs_destroy( gridfs *gfs ); /** * Initializes a gridfile for writing incrementally with gridfs_write_buffer. @@ -77,7 +83,7 @@ void gridfs_destroy( gridfs *gfs ); * When done, you must call gridfs_writer_done to save the file metadata. * */ -void gridfile_writer_init( gridfile *gfile, gridfs *gfs, const char *remote_name, +MONGO_EXPORT void gridfile_writer_init( gridfile *gfile, gridfs *gfs, const char *remote_name, const char *content_type ); /** @@ -86,7 +92,7 @@ void gridfile_writer_init( gridfile *gfile, gridfs *gfs, const char *remote_name * stream to a GridFS file. When finished, be sure to call gridfs_writer_done. * */ -void gridfile_write_buffer( gridfile *gfile, const char *data, +MONGO_EXPORT void gridfile_write_buffer( gridfile *gfile, const char *data, gridfs_offset length ); /** @@ -96,7 +102,7 @@ void gridfile_write_buffer( gridfile *gfile, const char *data, * * @return - MONGO_OK or MONGO_ERROR. */ -int gridfile_writer_done( gridfile *gfile ); +MONGO_EXPORT int gridfile_writer_done( gridfile *gfile ); /** * Store a buffer as a GridFS file. @@ -108,7 +114,7 @@ int gridfile_writer_done( gridfile *gfile ); * * @return - MONGO_OK or MONGO_ERROR. */ -int gridfs_store_buffer( gridfs *gfs, const char *data, gridfs_offset length, +MONGO_EXPORT int gridfs_store_buffer( gridfs *gfs, const char *data, gridfs_offset length, const char *remotename, const char *contenttype ); @@ -121,7 +127,7 @@ int gridfs_store_buffer( gridfs *gfs, const char *data, gridfs_offset length, * * @return - MONGO_OK or MONGO_ERROR. */ -int gridfs_store_file( gridfs *gfs, const char *filename, +MONGO_EXPORT int gridfs_store_file( gridfs *gfs, const char *filename, const char *remotename, const char *contenttype ); /** @@ -129,7 +135,7 @@ int gridfs_store_file( gridfs *gfs, const char *filename, * @param gfs - the working GridFS * @param filename - the filename of the file/s to be removed */ -void gridfs_remove_filename( gridfs *gfs, const char *filename ); +MONGO_EXPORT void gridfs_remove_filename( gridfs *gfs, const char *filename ); /** * Find the first file matching the provided query within the @@ -141,7 +147,7 @@ void gridfs_remove_filename( gridfs *gfs, const char *filename ); * * @return MONGO_OK if successful, MONGO_ERROR otherwise */ -int gridfs_find_query( gridfs *gfs, bson *query, gridfile *gfile ); +MONGO_EXPORT int gridfs_find_query( gridfs *gfs, bson *query, gridfile *gfile ); /** * Find the first file referenced by filename within the GridFS @@ -152,7 +158,7 @@ int gridfs_find_query( gridfs *gfs, bson *query, gridfile *gfile ); * * @return MONGO_OK or MONGO_ERROR. */ -int gridfs_find_filename( gridfs *gfs, const char *filename, gridfile *gfile ); +MONGO_EXPORT int gridfs_find_filename( gridfs *gfs, const char *filename, gridfile *gfile ); /** * Initializes a GridFile containing the GridFS and file bson @@ -162,20 +168,20 @@ int gridfs_find_filename( gridfs *gfs, const char *filename, gridfile *gfile ); * * @return - MONGO_OK or MONGO_ERROR. */ -int gridfile_init( gridfs *gfs, bson *meta, gridfile *gfile ); +MONGO_EXPORT int gridfile_init( gridfs *gfs, bson *meta, gridfile *gfile ); /** * Destroys the GridFile * * @param oGridFIle - the GridFile being destroyed */ -void gridfile_destroy( gridfile *gfile ); +MONGO_EXPORT void gridfile_destroy( gridfile *gfile ); /** * Returns whether or not the GridFile exists * @param gfile - the GridFile being examined */ -bson_bool_t gridfile_exists( gridfile *gfile ); +MONGO_EXPORT bson_bool_t gridfile_exists( gridfile *gfile ); /** * Returns the filename of GridFile @@ -183,7 +189,7 @@ bson_bool_t gridfile_exists( gridfile *gfile ); * * @return - the filename of the Gridfile */ -const char *gridfile_get_filename( gridfile *gfile ); +MONGO_EXPORT const char *gridfile_get_filename( gridfile *gfile ); /** * Returns the size of the chunks of the GridFile @@ -191,7 +197,7 @@ const char *gridfile_get_filename( gridfile *gfile ); * * @return - the size of the chunks of the Gridfile */ -int gridfile_get_chunksize( gridfile *gfile ); +MONGO_EXPORT int gridfile_get_chunksize( gridfile *gfile ); /** * Returns the length of GridFile's data @@ -200,7 +206,7 @@ int gridfile_get_chunksize( gridfile *gfile ); * * @return - the length of the Gridfile's data */ -gridfs_offset gridfile_get_contentlength( gridfile *gfile ); +MONGO_EXPORT gridfs_offset gridfile_get_contentlength( gridfile *gfile ); /** * Returns the MIME type of the GridFile @@ -210,7 +216,7 @@ gridfs_offset gridfile_get_contentlength( gridfile *gfile ); * @return - the MIME type of the Gridfile * (NULL if no type specified) */ -const char *gridfile_get_contenttype( gridfile *gfile ); +MONGO_EXPORT const char *gridfile_get_contenttype( gridfile *gfile ); /** * Returns the upload date of GridFile @@ -219,7 +225,7 @@ const char *gridfile_get_contenttype( gridfile *gfile ); * * @return - the upload date of the Gridfile */ -bson_date_t gridfile_get_uploaddate( gridfile *gfile ); +MONGO_EXPORT bson_date_t gridfile_get_uploaddate( gridfile *gfile ); /** * Returns the MD5 of GridFile @@ -228,7 +234,7 @@ bson_date_t gridfile_get_uploaddate( gridfile *gfile ); * * @return - the MD5 of the Gridfile */ -const char *gridfile_get_md5( gridfile *gfile ); +MONGO_EXPORT const char *gridfile_get_md5( gridfile *gfile ); /** * Returns the field in GridFile specified by name @@ -260,7 +266,7 @@ bson_bool_t gridfile_get_boolean( gridfile *gfile, * @return - the metadata of the Gridfile in a bson object * (an empty bson is returned if none exists) */ -bson gridfile_get_metadata( gridfile *gfile ); +MONGO_EXPORT void gridfile_get_metadata( gridfile *gfile, bson* out ); /** * Returns the number of chunks in the GridFile @@ -268,7 +274,7 @@ bson gridfile_get_metadata( gridfile *gfile ); * * @return - the number of chunks in the Gridfile */ -int gridfile_get_numchunks( gridfile *gfile ); +MONGO_EXPORT int gridfile_get_numchunks( gridfile *gfile ); /** * Returns chunk n of GridFile @@ -276,7 +282,7 @@ int gridfile_get_numchunks( gridfile *gfile ); * * @return - the nth chunk of the Gridfile */ -bson gridfile_get_chunk( gridfile *gfile, int n ); +MONGO_EXPORT void gridfile_get_chunk( gridfile *gfile, int n, bson* out ); /** * Returns a mongo_cursor of *size* chunks starting with chunk *start* @@ -287,7 +293,7 @@ bson gridfile_get_chunk( gridfile *gfile, int n ); * * @return - mongo_cursor of the chunks (must be destroyed after use) */ -mongo_cursor *gridfile_get_chunks( gridfile *gfile, int start, int size ); +MONGO_EXPORT mongo_cursor *gridfile_get_chunks( gridfile *gfile, int start, int size ); /** * Writes the GridFile to a stream @@ -295,7 +301,7 @@ mongo_cursor *gridfile_get_chunks( gridfile *gfile, int start, int size ); * @param gfile - the working GridFile * @param stream - the file stream to write to */ -gridfs_offset gridfile_write_file( gridfile *gfile, FILE *stream ); +MONGO_EXPORT gridfs_offset gridfile_write_file( gridfile *gfile, FILE *stream ); /** * Reads length bytes from the GridFile to a buffer @@ -309,7 +315,7 @@ gridfs_offset gridfile_write_file( gridfile *gfile, FILE *stream ); * * @return - the number of bytes read */ -gridfs_offset gridfile_read( gridfile *gfile, gridfs_offset size, char *buf ); +MONGO_EXPORT gridfs_offset gridfile_read( gridfile *gfile, gridfs_offset size, char *buf ); /** * Updates the position in the file @@ -321,6 +327,6 @@ gridfs_offset gridfile_read( gridfile *gfile, gridfs_offset size, char *buf ); * * @return - resulting offset location */ -gridfs_offset gridfile_seek( gridfile *gfile, gridfs_offset offset ); +MONGO_EXPORT gridfs_offset gridfile_seek( gridfile *gfile, gridfs_offset offset ); #endif diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/md5.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/md5.c index d1c1e3ab0b..68edd29513 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/md5.c +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/md5.c @@ -309,7 +309,7 @@ mongo_md5_process(mongo_md5_state_t *pms, const mongo_md5_byte_t *data /*[64]*/) pms->abcd[3] += d; } -void +MONGO_EXPORT void mongo_md5_init(mongo_md5_state_t *pms) { pms->count[0] = pms->count[1] = 0; @@ -319,7 +319,7 @@ mongo_md5_init(mongo_md5_state_t *pms) pms->abcd[3] = 0x10325476; } -void +MONGO_EXPORT void mongo_md5_append(mongo_md5_state_t *pms, const mongo_md5_byte_t *data, int nbytes) { const mongo_md5_byte_t *p = data; @@ -357,7 +357,7 @@ mongo_md5_append(mongo_md5_state_t *pms, const mongo_md5_byte_t *data, int nbyte memcpy(pms->buf, p, left); } -void +MONGO_EXPORT void mongo_md5_finish(mongo_md5_state_t *pms, mongo_md5_byte_t digest[16]) { static const mongo_md5_byte_t pad[64] = { diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/md5.h b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/md5.h index 540da3a584..342b6a7307 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/md5.h +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/md5.h @@ -47,8 +47,8 @@ 1999-05-03 lpd Original version. */ -#ifndef md5_INCLUDED -# define md5_INCLUDED +#ifndef MONGO_MD5_H_ +#define MONGO_MD5_H_ /* * This package supports both compile-time and run-time determination of CPU @@ -59,6 +59,7 @@ * run on either big- or little-endian CPUs, but will run slightly less * efficiently on either one than if ARCH_IS_BIG_ENDIAN is defined. */ +#include "bson.h" typedef unsigned char mongo_md5_byte_t; /* 8-bit byte */ typedef unsigned int mongo_md5_word_t; /* 32-bit word */ @@ -75,17 +76,17 @@ extern "C" { #endif - /* Initialize the algorithm. */ - void mongo_md5_init(mongo_md5_state_t *pms); +/* Initialize the algorithm. */ +MONGO_EXPORT void mongo_md5_init(mongo_md5_state_t *pms); - /* Append a string to the message. */ - void mongo_md5_append(mongo_md5_state_t *pms, const mongo_md5_byte_t *data, int nbytes); +/* Append a string to the message. */ +MONGO_EXPORT void mongo_md5_append(mongo_md5_state_t *pms, const mongo_md5_byte_t *data, int nbytes); - /* Finish the message and return the digest. */ - void mongo_md5_finish(mongo_md5_state_t *pms, mongo_md5_byte_t digest[16]); +/* Finish the message and return the digest. */ +MONGO_EXPORT void mongo_md5_finish(mongo_md5_state_t *pms, mongo_md5_byte_t digest[16]); #ifdef __cplusplus } /* end extern "C" */ #endif -#endif /* md5_INCLUDED */ +#endif /* MONGO_MD5_H_ */ diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/mongo.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/mongo.c index 2090e74312..eb16392297 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/mongo.c +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/mongo.c @@ -1,6 +1,6 @@ /* mongo.c */ -/* Copyright 2009-2011 10gen Inc. +/* Copyright 2009-2012 10gen Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,19 +17,235 @@ #include "mongo.h" #include "md5.h" +#include "env.h" #include #include #include #include -#ifdef _USE_LINUX_SYSTEM -#include "platform/linux/net.h" -#elif defined _USE_CUSTOM_SYSTEM -#include "platform/custom/net.h" -#else -#include "net.h" -#endif +MONGO_EXPORT mongo* mongo_create() { + return (mongo*)bson_malloc(sizeof(mongo)); +} + + +MONGO_EXPORT void mongo_dispose(mongo* conn) { + free(conn); +} + +MONGO_EXPORT int mongo_get_err(mongo* conn) { + return conn->err; +} + + +MONGO_EXPORT int mongo_is_connected(mongo* conn) { + return conn->connected != 0; +} + + +MONGO_EXPORT int mongo_get_op_timeout(mongo* conn) { + return conn->op_timeout_ms; +} + + +const char* _get_host_port(mongo_host_port* hp) { + static char _hp[sizeof(hp->host)+12]; + bson_sprintf(_hp, "%s:%d", hp->host, hp->port); + return _hp; +} + + +MONGO_EXPORT const char* mongo_get_primary(mongo* conn) { + mongo* conn_ = (mongo*)conn; + return _get_host_port(conn_->primary); +} + + +MONGO_EXPORT int mongo_get_socket(mongo* conn) { + mongo* conn_ = (mongo*)conn; + return conn_->sock; +} + + +MONGO_EXPORT int mongo_get_host_count(mongo* conn) { + mongo_replset* r = conn->replset; + mongo_host_port* hp; + int count = 0; + if (!r) return 0; + for (hp = r->hosts; hp; hp = hp->next) + ++count; + return count; +} + + +MONGO_EXPORT const char* mongo_get_host(mongo* conn, int i) { + mongo_replset* r = conn->replset; + mongo_host_port* hp; + int count = 0; + if (!r) return 0; + for (hp = r->hosts; hp; hp = hp->next) { + if (count == i) + return _get_host_port(hp); + ++count; + } + return 0; +} + + +MONGO_EXPORT mongo_cursor* mongo_cursor_create() { + return (mongo_cursor*)bson_malloc(sizeof(mongo_cursor)); +} + + +MONGO_EXPORT void mongo_cursor_dispose(mongo_cursor* cursor) { + free(cursor); +} + + +MONGO_EXPORT int mongo_get_server_err(mongo* conn) { + return conn->lasterrcode; +} + + +MONGO_EXPORT const char* mongo_get_server_err_string(mongo* conn) { + return conn->lasterrstr; +} + +MONGO_EXPORT void __mongo_set_error( mongo *conn, mongo_error_t err, const char *str, + int errcode ) { + int errstr_size, str_size; + + conn->err = err; + conn->errcode = errcode; + + if( str ) { + str_size = strlen( str ) + 1; + errstr_size = str_size > MONGO_ERR_LEN ? MONGO_ERR_LEN : str_size; + memcpy( conn->errstr, str, errstr_size ); + conn->errstr[errstr_size] = '\0'; + } +} + +MONGO_EXPORT void mongo_clear_errors( mongo *conn ) { + conn->err = 0; + conn->errcode = 0; + conn->lasterrcode = 0; + memset( conn->errstr, 0, MONGO_ERR_LEN ); + memset( conn->lasterrstr, 0, MONGO_ERR_LEN ); +} + +MONGO_EXPORT int mongo_validate_ns( mongo *conn, const char *ns ) { + char *last = NULL; + char *current = NULL; + const char *db_name = ns; + char *collection_name = NULL; + char errmsg[64]; + int ns_len = 0; + + /* If the first character is a '.', fail. */ + if( *ns == '.' ) { + __mongo_set_error( conn, MONGO_NS_INVALID, "ns cannot start with a '.'.", 0 ); + return MONGO_ERROR; + } + + /* Find the division between database and collection names. */ + for( current = (char *)ns; *current != '\0'; current++ ) { + if( *current == '.' ) { + current++; + break; + } + } + + /* Fail because the ns doesn't contain a '.' + * or the collection part starts with a dot. */ + if( *current == '\0' || *current == '.' ) { + __mongo_set_error( conn, MONGO_NS_INVALID, "ns cannot start with a '.'.", 0 ); + return MONGO_ERROR; + } + + /* Fail if collection length is 0. */ + if( *(current + 1) == '\0' ) { + __mongo_set_error( conn, MONGO_NS_INVALID, "Collection name missing.", 0 ); + return MONGO_ERROR; + } + + + /* Point to the beginning of the collection name. */ + collection_name = current; + + /* Ensure that the database name is greater than one char.*/ + if( collection_name - 1 == db_name ) { + __mongo_set_error( conn, MONGO_NS_INVALID, "Database name missing.", 0 ); + return MONGO_ERROR; + } + + /* Go back and validate the database name. */ + for( current = (char *)db_name; *current != '.'; current++ ) { + switch( *current ) { + case ' ': + case '$': + case '/': + case '\\': + __mongo_set_error( conn, MONGO_NS_INVALID, + "Database name may not contain ' ', '$', '/', or '\\'", 0 ); + return MONGO_ERROR; + default: + break; + } + + ns_len++; + } + + /* Add one to the length for the '.' character. */ + ns_len++; + + /* Now validate the collection name. */ + for( current = collection_name; *current != '\0'; current++ ) { + + /* Cannot have two consecutive dots. */ + if( last && *last == '.' && *current == '.' ) { + __mongo_set_error( conn, MONGO_NS_INVALID, + "Collection may not contain two consecutive '.'", 0 ); + return MONGO_ERROR; + } + + /* Cannot contain a '$' */ + if( *current == '$' ) { + __mongo_set_error( conn, MONGO_NS_INVALID, + "Collection may not contain '$'", 0 ); + return MONGO_ERROR; + } + + last = current; + ns_len++; + } + + if( ns_len > 128 ) { + bson_sprintf( errmsg, "Namespace too long; has %d but must <= 128.", + ns_len ); + __mongo_set_error( conn, MONGO_NS_INVALID, errmsg, 0 ); + return MONGO_ERROR; + } + + /* Cannot end with a '.' */ + if( *(current - 1) == '.' ) { + __mongo_set_error( conn, MONGO_NS_INVALID, + "Collection may not end with '.'", 0 ); + return MONGO_ERROR; + } + + return MONGO_OK; +} + +static void mongo_set_last_error( mongo *conn, bson_iterator *it, bson *obj ) { + int result_len = bson_iterator_string_len( it ); + const char *result_string = bson_iterator_string( it ); + int len = result_len < MONGO_ERR_LEN ? result_len : MONGO_ERR_LEN; + memcpy( conn->lasterrstr, result_string, len ); + + if( bson_find( it, obj, "code" ) != BSON_NULL ) + conn->lasterrcode = bson_iterator_int( it ); +} static const int ZERO = 0; static const int ONE = 1; @@ -57,13 +273,13 @@ int mongo_message_send( mongo *conn, mongo_message *mm ) { bson_little_endian32( &head.responseTo, &mm->head.responseTo ); bson_little_endian32( &head.op, &mm->head.op ); - res = mongo_write_socket( conn, &head, sizeof( head ) ); + res = mongo_env_write_socket( conn, &head, sizeof( head ) ); if( res != MONGO_OK ) { bson_free( mm ); return res; } - res = mongo_write_socket( conn, &mm->data, mm->head.len - sizeof( head ) ); + res = mongo_env_write_socket( conn, &mm->data, mm->head.len - sizeof( head ) ); if( res != MONGO_OK ) { bson_free( mm ); return res; @@ -80,8 +296,8 @@ int mongo_read_response( mongo *conn, mongo_reply **reply ) { unsigned int len; int res; - mongo_read_socket( conn, &head, sizeof( head ) ); - mongo_read_socket( conn, &fields, sizeof( fields ) ); + mongo_env_read_socket( conn, &head, sizeof( head ) ); + mongo_env_read_socket( conn, &fields, sizeof( fields ) ); bson_little_endian32( &len, &head.len ); @@ -100,7 +316,7 @@ int mongo_read_response( mongo *conn, mongo_reply **reply ) { bson_little_endian32( &out->fields.start, &fields.start ); bson_little_endian32( &out->fields.num, &fields.num ); - res = mongo_read_socket( conn, &out->objs, len-sizeof( head )-sizeof( fields ) ); + res = mongo_env_read_socket( conn, &out->objs, len-sizeof( head )-sizeof( fields ) ); if( res != MONGO_OK ) { bson_free( out ); return res; @@ -133,12 +349,17 @@ static int mongo_check_is_master( mongo *conn ) { bson out; bson_iterator it; bson_bool_t ismaster = 0; + int max_bson_size = MONGO_DEFAULT_MAX_BSON_SIZE; out.data = NULL; if ( mongo_simple_int_command( conn, "admin", "ismaster", 1, &out ) == MONGO_OK ) { if( bson_find( &it, &out, "ismaster" ) ) ismaster = bson_iterator_bool( &it ); + if( bson_find( &it, &out, "maxBsonObjectSize" ) ) { + max_bson_size = bson_iterator_int( &it ); + } + conn->max_bson_size = max_bson_size; } else { return MONGO_ERROR; } @@ -153,25 +374,25 @@ static int mongo_check_is_master( mongo *conn ) { } } -void mongo_init( mongo *conn ) { - conn->replset = NULL; - conn->err = 0; - conn->errstr = NULL; - conn->lasterrcode = 0; - conn->lasterrstr = NULL; - - conn->conn_timeout_ms = 0; - conn->op_timeout_ms = 0; +MONGO_EXPORT void mongo_init_sockets( void ) { + mongo_env_sock_init(); } -int mongo_connect( mongo *conn , const char *host, int port ) { + +MONGO_EXPORT void mongo_init( mongo *conn ) { + memset( conn, 0, sizeof( mongo ) ); + conn->max_bson_size = MONGO_DEFAULT_MAX_BSON_SIZE; +} + +MONGO_EXPORT int mongo_connect( mongo *conn , const char *host, int port ) { + mongo_init( conn ); + conn->primary = bson_malloc( sizeof( mongo_host_port ) ); strncpy( conn->primary->host, host, strlen( host ) + 1 ); conn->primary->port = port; conn->primary->next = NULL; - mongo_init( conn ); - if( mongo_socket_connect( conn, host, port ) != MONGO_OK ) + if( mongo_env_socket_connect( conn, host, port ) != MONGO_OK ) return MONGO_ERROR; if( mongo_check_is_master( conn ) != MONGO_OK ) @@ -180,7 +401,7 @@ int mongo_connect( mongo *conn , const char *host, int port ) { return MONGO_OK; } -void mongo_replset_init( mongo *conn, const char *name ) { +MONGO_EXPORT void mongo_replset_init( mongo *conn, const char *name ) { mongo_init( conn ); conn->replset = bson_malloc( sizeof( mongo_replset ) ); @@ -222,7 +443,7 @@ static void mongo_replset_free_list( mongo_host_port **list ) { *list = NULL; } -void mongo_replset_add_seed( mongo *conn, const char *host, int port ) { +MONGO_EXPORT void mongo_replset_add_seed( mongo *conn, const char *host, int port ) { mongo_replset_add_node( &conn->replset->seeds, host, port ); } @@ -291,7 +512,7 @@ static void mongo_replset_check_seed( mongo *conn ) { bson_destroy( &out ); bson_destroy( &hosts ); - mongo_close_socket( conn->sock ); + mongo_env_close_socket( conn->sock ); conn->sock = 0; conn->connected = 0; @@ -306,6 +527,7 @@ static int mongo_replset_check_host( mongo *conn ) { bson_iterator it; bson_bool_t ismaster = 0; const char *set_name; + int max_bson_size = MONGO_DEFAULT_MAX_BSON_SIZE; out.data = NULL; @@ -313,6 +535,10 @@ static int mongo_replset_check_host( mongo *conn ) { if( bson_find( &it, &out, "ismaster" ) ) ismaster = bson_iterator_bool( &it ); + if( bson_find( &it, &out, "maxBsonObjectSize" ) ) + max_bson_size = bson_iterator_int( &it ); + conn->max_bson_size = max_bson_size; + if( bson_find( &it, &out, "setName" ) ) { set_name = bson_iterator_string( &it ); if( strcmp( set_name, conn->replset->name ) != 0 ) { @@ -328,13 +554,13 @@ static int mongo_replset_check_host( mongo *conn ) { if( ismaster ) { conn->replset->primary_connected = 1; } else { - mongo_close_socket( conn->sock ); + mongo_env_close_socket( conn->sock ); } return MONGO_OK; } -int mongo_replset_connect( mongo *conn ) { +MONGO_EXPORT int mongo_replset_connect( mongo *conn ) { int res = 0; mongo_host_port *node; @@ -347,15 +573,12 @@ int mongo_replset_connect( mongo *conn ) { */ node = conn->replset->seeds; while( node != NULL ) { - res = mongo_socket_connect( conn, ( const char * )&node->host, node->port ); - if( res != MONGO_OK ) - return MONGO_ERROR; - - mongo_replset_check_seed( conn ); - - if( conn->replset->hosts ) - break; - + res = mongo_env_socket_connect( conn, ( const char * )&node->host, node->port ); + if( res == MONGO_OK ) { + mongo_replset_check_seed( conn ); + if( conn->replset->hosts ) + break; + } node = node->next; } @@ -367,19 +590,22 @@ int mongo_replset_connect( mongo *conn ) { node = conn->replset->hosts; while( node != NULL ) { - res = mongo_socket_connect( conn, ( const char * )&node->host, node->port ); + res = mongo_env_socket_connect( conn, ( const char * )&node->host, node->port ); if( res == MONGO_OK ) { if( mongo_replset_check_host( conn ) != MONGO_OK ) return MONGO_ERROR; /* Primary found, so return. */ - else if( conn->replset->primary_connected ) + else if( conn->replset->primary_connected ) { + strncpy( conn->primary->host, node->host, strlen( node->host ) + 1 ); + conn->primary->port = node->port; return MONGO_OK; + } /* No primary, so close the connection. */ else { - mongo_close_socket( conn->sock ); + mongo_env_close_socket( conn->sock ); conn->sock = 0; conn->connected = 0; } @@ -394,15 +620,15 @@ int mongo_replset_connect( mongo *conn ) { return MONGO_ERROR; } -int mongo_set_op_timeout( mongo *conn, int millis ) { +MONGO_EXPORT int mongo_set_op_timeout( mongo *conn, int millis ) { conn->op_timeout_ms = millis; if( conn->sock && conn->connected ) - mongo_set_socket_op_timeout( conn, millis ); + mongo_env_set_socket_op_timeout( conn, millis ); return MONGO_OK; } -int mongo_reconnect( mongo *conn ) { +MONGO_EXPORT int mongo_reconnect( mongo *conn ) { int res; mongo_disconnect( conn ); @@ -413,10 +639,10 @@ int mongo_reconnect( mongo *conn ) { res = mongo_replset_connect( conn ); return res; } else - return mongo_socket_connect( conn, conn->primary->host, conn->primary->port ); + return mongo_env_socket_connect( conn, conn->primary->host, conn->primary->port ); } -int mongo_check_connection( mongo *conn ) { +MONGO_EXPORT int mongo_check_connection( mongo *conn ) { if( ! conn->connected ) return MONGO_ERROR; @@ -426,7 +652,7 @@ int mongo_check_connection( mongo *conn ) { return MONGO_ERROR; } -void mongo_disconnect( mongo *conn ) { +MONGO_EXPORT void mongo_disconnect( mongo *conn ) { if( ! conn->connected ) return; @@ -436,13 +662,13 @@ void mongo_disconnect( mongo *conn ) { conn->replset->hosts = NULL; } - mongo_close_socket( conn->sock ); + mongo_env_close_socket( conn->sock ); conn->sock = 0; conn->connected = 0; } -void mongo_destroy( mongo *conn ) { +MONGO_EXPORT void mongo_destroy( mongo *conn ) { mongo_disconnect( conn ); if( conn->replset ) { @@ -454,17 +680,20 @@ void mongo_destroy( mongo *conn ) { } bson_free( conn->primary ); - bson_free( conn->errstr ); - bson_free( conn->lasterrstr ); - conn->err = 0; - conn->errstr = NULL; - conn->lasterrcode = 0; - conn->lasterrstr = NULL; + mongo_clear_errors( conn ); } /* Determine whether this BSON object is valid for the given operation. */ -static int mongo_bson_valid( mongo *conn, bson *bson, int write ) { +static int mongo_bson_valid( mongo *conn, const bson *bson, int write ) { + int size; + + size = bson_size( bson ); + if( size > conn->max_bson_size ) { + conn->err = MONGO_BSON_TOO_LARGE; + return MONGO_ERROR; + } + if( ! bson->finished ) { conn->err = MONGO_BSON_NOT_FINISHED; return MONGO_ERROR; @@ -486,20 +715,21 @@ static int mongo_bson_valid( mongo *conn, bson *bson, int write ) { } conn->err = 0; - conn->errstr = NULL; return MONGO_OK; } /* Determine whether this BSON object is valid for the given operation. */ -static int mongo_cursor_bson_valid( mongo_cursor *cursor, bson *bson ) { +static int mongo_cursor_bson_valid( mongo_cursor *cursor, const bson *bson ) { if( ! bson->finished ) { - cursor->err = MONGO_BSON_NOT_FINISHED; + cursor->err = MONGO_CURSOR_BSON_ERROR; + cursor->conn->err = MONGO_BSON_NOT_FINISHED; return MONGO_ERROR; } if( bson->err & BSON_NOT_UTF8 ) { - cursor->err = MONGO_BSON_INVALID; + cursor->err = MONGO_CURSOR_BSON_ERROR; + cursor->conn->err = MONGO_BSON_INVALID; return MONGO_ERROR; } @@ -508,13 +738,17 @@ static int mongo_cursor_bson_valid( mongo_cursor *cursor, bson *bson ) { /* MongoDB CRUD API */ -int mongo_insert_batch( mongo *conn, const char *ns, - bson **bsons, int count ) { +MONGO_EXPORT int mongo_insert_batch( mongo *conn, const char *ns, + const bson **bsons, int count ) { - int size = 16 + 4 + strlen( ns ) + 1; - int i; mongo_message *mm; + int i; char *data; + int overhead = 16 + 4 + strlen( ns ) + 1; + int size = overhead; + + if( mongo_validate_ns( conn, ns ) != MONGO_OK ) + return MONGO_ERROR; for( i=0; i conn->max_bson_size ) { + conn->err = MONGO_BSON_TOO_LARGE; + return MONGO_ERROR; + } + mm = mongo_message_create( size , 0 , 0 , MONGO_OP_INSERT ); data = &mm->data; @@ -535,11 +774,14 @@ int mongo_insert_batch( mongo *conn, const char *ns, return mongo_message_send( conn, mm ); } -int mongo_insert( mongo *conn , const char *ns , bson *bson ) { +MONGO_EXPORT int mongo_insert( mongo *conn , const char *ns , const bson *bson ) { char *data; mongo_message *mm; + if( mongo_validate_ns( conn, ns ) != MONGO_OK ) + return MONGO_ERROR; + /* Make sure that BSON is valid for insert. */ if( mongo_bson_valid( conn, bson, 1 ) != MONGO_OK ) { return MONGO_ERROR; @@ -559,7 +801,7 @@ int mongo_insert( mongo *conn , const char *ns , bson *bson ) { return mongo_message_send( conn, mm ); } -int mongo_update( mongo *conn, const char *ns, const bson *cond, +MONGO_EXPORT int mongo_update( mongo *conn, const char *ns, const bson *cond, const bson *op, int flags ) { char *data; @@ -590,14 +832,9 @@ int mongo_update( mongo *conn, const char *ns, const bson *cond, return mongo_message_send( conn, mm ); } -int mongo_remove( mongo *conn, const char *ns, const bson *cond ) { +MONGO_EXPORT int mongo_remove( mongo *conn, const char *ns, const bson *cond ) { char *data; - mongo_message *mm = mongo_message_create( 16 /* header */ - + 4 /* ZERO */ - + strlen( ns ) + 1 - + 4 /* ZERO */ - + bson_size( cond ) - , 0 , 0 , MONGO_OP_DELETE ); + mongo_message *mm; /* Make sure that the BSON is valid UTF-8. * TODO: decide whether to check cond as well. @@ -606,6 +843,13 @@ int mongo_remove( mongo *conn, const char *ns, const bson *cond ) { return MONGO_ERROR; } + mm = mongo_message_create( 16 /* header */ + + 4 /* ZERO */ + + strlen( ns ) + 1 + + 4 /* ZERO */ + + bson_size( cond ) + , 0 , 0 , MONGO_OP_DELETE ); + data = &mm->data; data = mongo_data_append32( data, &ZERO ); data = mongo_data_append( data, ns, strlen( ns ) + 1 ); @@ -621,6 +865,11 @@ static int mongo_cursor_op_query( mongo_cursor *cursor ) { bson empty; char *data; mongo_message *mm; + bson temp; + bson_iterator it; + + /* Clear any errors. */ + mongo_clear_errors( cursor->conn ); /* Set up default values for query and fields, if necessary. */ if( ! cursor->query ) @@ -662,6 +911,15 @@ static int mongo_cursor_op_query( mongo_cursor *cursor ) { return MONGO_ERROR; } + if( cursor->reply->fields.num == 1 ) { + bson_init_data( &temp, &cursor->reply->objs ); + if( bson_find( &it, &temp, "$err" ) ) { + mongo_set_last_error( cursor->conn, &it, &temp ); + cursor->err = MONGO_CURSOR_QUERY_FAIL; + return MONGO_ERROR; + } + } + cursor->seen += cursor->reply->fields.num; cursor->flags |= MONGO_CURSOR_QUERY_SENT; return MONGO_OK; @@ -719,8 +977,8 @@ static int mongo_cursor_get_more( mongo_cursor *cursor ) { } } -mongo_cursor *mongo_find( mongo *conn, const char *ns, bson *query, - bson *fields, int limit, int skip, int options ) { +MONGO_EXPORT mongo_cursor *mongo_find( mongo *conn, const char *ns, const bson *query, + const bson *fields, int limit, int skip, int options ) { mongo_cursor *cursor = ( mongo_cursor * )bson_malloc( sizeof( mongo_cursor ) ); mongo_cursor_init( cursor, conn, ns ); @@ -740,13 +998,20 @@ mongo_cursor *mongo_find( mongo *conn, const char *ns, bson *query, } } -int mongo_find_one( mongo *conn, const char *ns, bson *query, - bson *fields, bson *out ) { +MONGO_EXPORT int mongo_find_one( mongo *conn, const char *ns, const bson *query, + const bson *fields, bson *out ) { - mongo_cursor *cursor = mongo_find( conn, ns, query, fields, 1, 0, 0 ); + mongo_cursor cursor[1]; + mongo_cursor_init( cursor, conn, ns ); + mongo_cursor_set_query( cursor, query ); + mongo_cursor_set_fields( cursor, fields ); + mongo_cursor_set_limit( cursor, 1 ); - if ( cursor && mongo_cursor_next( cursor ) == MONGO_OK ) { - bson_copy_basic( out, &cursor->current ); + if ( mongo_cursor_next( cursor ) == MONGO_OK ) { + bson_init_size( out, bson_size( (bson *)&cursor->current ) ); + memcpy( out->data, cursor->current.data, + bson_size( (bson *)&cursor->current ) ); + out->finished = 1; mongo_cursor_destroy( cursor ); return MONGO_OK; } else { @@ -755,56 +1020,49 @@ int mongo_find_one( mongo *conn, const char *ns, bson *query, } } -void mongo_cursor_init( mongo_cursor *cursor, mongo *conn, const char *ns ) { +MONGO_EXPORT void mongo_cursor_init( mongo_cursor *cursor, mongo *conn, const char *ns ) { + memset( cursor, 0, sizeof( mongo_cursor ) ); cursor->conn = conn; cursor->ns = ( const char * )bson_malloc( strlen( ns ) + 1 ); strncpy( ( char * )cursor->ns, ns, strlen( ns ) + 1 ); cursor->current.data = NULL; - cursor->reply = NULL; - cursor->flags = 0; - cursor->seen = 0; - cursor->err = 0; - cursor->options = 0; - cursor->query = NULL; - cursor->fields = NULL; - cursor->skip = 0; - cursor->limit = 0; } -void mongo_cursor_set_query( mongo_cursor *cursor, bson *query ) { +MONGO_EXPORT void mongo_cursor_set_query( mongo_cursor *cursor, const bson *query ) { cursor->query = query; } -void mongo_cursor_set_fields( mongo_cursor *cursor, bson *fields ) { +MONGO_EXPORT void mongo_cursor_set_fields( mongo_cursor *cursor, const bson *fields ) { cursor->fields = fields; } -void mongo_cursor_set_skip( mongo_cursor *cursor, int skip ) { +MONGO_EXPORT void mongo_cursor_set_skip( mongo_cursor *cursor, int skip ) { cursor->skip = skip; } -void mongo_cursor_set_limit( mongo_cursor *cursor, int limit ) { +MONGO_EXPORT void mongo_cursor_set_limit( mongo_cursor *cursor, int limit ) { cursor->limit = limit; } -void mongo_cursor_set_options( mongo_cursor *cursor, int options ) { +MONGO_EXPORT void mongo_cursor_set_options( mongo_cursor *cursor, int options ) { cursor->options = options; } -const char *mongo_cursor_data( mongo_cursor *cursor ) { +MONGO_EXPORT const char *mongo_cursor_data( mongo_cursor *cursor ) { return cursor->current.data; } -const bson *mongo_cursor_bson( mongo_cursor *cursor ) { +MONGO_EXPORT const bson *mongo_cursor_bson( mongo_cursor *cursor ) { return (const bson *)&(cursor->current); } -int mongo_cursor_next( mongo_cursor *cursor ) { +MONGO_EXPORT int mongo_cursor_next( mongo_cursor *cursor ) { char *next_object; char *message_end; if( ! ( cursor->flags & MONGO_CURSOR_QUERY_SENT ) ) - mongo_cursor_op_query( cursor ); + if( mongo_cursor_op_query( cursor ) != MONGO_OK ) + return MONGO_ERROR; if( !cursor->reply ) return MONGO_ERROR; @@ -826,7 +1084,7 @@ int mongo_cursor_next( mongo_cursor *cursor ) { /* first */ if ( cursor->current.data == NULL ) { - bson_init_data( &cursor->current, &cursor->reply->objs ); + bson_init_finished_data( &cursor->current, &cursor->reply->objs ); return MONGO_OK; } @@ -843,15 +1101,15 @@ int mongo_cursor_next( mongo_cursor *cursor ) { return MONGO_ERROR; } - bson_init_data( &cursor->current, &cursor->reply->objs ); + bson_init_finished_data( &cursor->current, &cursor->reply->objs ); } else { - bson_init_data( &cursor->current, next_object ); + bson_init_finished_data( &cursor->current, next_object ); } return MONGO_OK; } -int mongo_cursor_destroy( mongo_cursor *cursor ) { +MONGO_EXPORT int mongo_cursor_destroy( mongo_cursor *cursor ) { int result = MONGO_OK; if ( !cursor ) return result; @@ -883,7 +1141,7 @@ int mongo_cursor_destroy( mongo_cursor *cursor ) { /* MongoDB Helper Functions */ -int mongo_create_index( mongo *conn, const char *ns, bson *key, int options, bson *out ) { +MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns, const bson *key, int options, bson *out ) { bson b; bson_iterator it; char name[255] = {'_'}; @@ -933,10 +1191,10 @@ bson_bool_t mongo_create_simple_index( mongo *conn, const char *ns, const char * return success; } -int64_t mongo_count( mongo *conn, const char *db, const char *ns, bson *query ) { +MONGO_EXPORT double mongo_count( mongo *conn, const char *db, const char *ns, const bson *query ) { bson cmd; bson out = {NULL, 0}; - int64_t count = -1; + double count = -1; bson_init( &cmd ); bson_append_string( &cmd, "count", ns ); @@ -947,7 +1205,7 @@ int64_t mongo_count( mongo *conn, const char *db, const char *ns, bson *query ) if( mongo_run_command( conn, db, &cmd, &out ) == MONGO_OK ) { bson_iterator it; if( bson_find( &it, &out, "n" ) ) - count = bson_iterator_long( &it ); + count = bson_iterator_double( &it ); bson_destroy( &cmd ); bson_destroy( &out ); return count; @@ -958,38 +1216,51 @@ int64_t mongo_count( mongo *conn, const char *db, const char *ns, bson *query ) } } -int mongo_run_command( mongo *conn, const char *db, bson *command, +MONGO_EXPORT int mongo_run_command( mongo *conn, const char *db, const bson *command, bson *out ) { + bson response = {NULL, 0}; bson fields; int sl = strlen( db ); char *ns = bson_malloc( sl + 5 + 1 ); /* ".$cmd" + nul */ - int res; + int res, success = 0; strcpy( ns, db ); strcpy( ns+sl, ".$cmd" ); - res = mongo_find_one( conn, ns, command, bson_empty( &fields ), out ); + res = mongo_find_one( conn, ns, command, bson_empty( &fields ), &response ); bson_free( ns ); - return res; + + if( res != MONGO_OK ) + return MONGO_ERROR; + else { + bson_iterator it; + if( bson_find( &it, &response, "ok" ) ) + success = bson_iterator_bool( &it ); + + if( !success ) { + conn->err = MONGO_COMMAND_FAILED; + return MONGO_ERROR; + } else { + if( out ) + *out = response; + return MONGO_OK; + } + } } -int mongo_simple_int_command( mongo *conn, const char *db, +MONGO_EXPORT int mongo_simple_int_command( mongo *conn, const char *db, const char *cmdstr, int arg, bson *realout ) { bson out = {NULL, 0}; bson cmd; - bson_bool_t success = 0; + int result; bson_init( &cmd ); bson_append_int( &cmd, cmdstr, arg ); bson_finish( &cmd ); - if( mongo_run_command( conn, db, &cmd, &out ) == MONGO_OK ) { - bson_iterator it; - if( bson_find( &it, &out, "ok" ) ) - success = bson_iterator_bool( &it ); - } + result = mongo_run_command( conn, db, &cmd, &out ); bson_destroy( &cmd ); @@ -998,30 +1269,21 @@ int mongo_simple_int_command( mongo *conn, const char *db, else bson_destroy( &out ); - if( success ) - return MONGO_OK; - else { - conn->err = MONGO_COMMAND_FAILED; - return MONGO_ERROR; - } + return result; } -int mongo_simple_str_command( mongo *conn, const char *db, +MONGO_EXPORT int mongo_simple_str_command( mongo *conn, const char *db, const char *cmdstr, const char *arg, bson *realout ) { bson out = {NULL, 0}; - int success = 0; + int result; bson cmd; bson_init( &cmd ); bson_append_string( &cmd, cmdstr, arg ); bson_finish( &cmd ); - if( mongo_run_command( conn, db, &cmd, &out ) == MONGO_OK ) { - bson_iterator it; - if( bson_find( &it, &out, "ok" ) ) - success = bson_iterator_bool( &it ); - } + result = mongo_run_command( conn, db, &cmd, &out ); bson_destroy( &cmd ); @@ -1030,21 +1292,18 @@ int mongo_simple_str_command( mongo *conn, const char *db, else bson_destroy( &out ); - if( success ) - return MONGO_OK; - else - return MONGO_ERROR; + return result; } -int mongo_cmd_drop_db( mongo *conn, const char *db ) { +MONGO_EXPORT int mongo_cmd_drop_db( mongo *conn, const char *db ) { return mongo_simple_int_command( conn, db, "dropDatabase", 1, NULL ); } -int mongo_cmd_drop_collection( mongo *conn, const char *db, const char *collection, bson *out ) { +MONGO_EXPORT int mongo_cmd_drop_collection( mongo *conn, const char *db, const char *collection, bson *out ) { return mongo_simple_str_command( conn, db, "drop", collection, out ); } -void mongo_cmd_reset_error( mongo *conn, const char *db ) { +MONGO_EXPORT void mongo_cmd_reset_error( mongo *conn, const char *db ) { mongo_simple_int_command( conn, db, "reseterror", 1, NULL ); } @@ -1055,23 +1314,13 @@ static int mongo_cmd_get_error_helper( mongo *conn, const char *db, bson_bool_t haserror = 0; /* Reset last error codes. */ - conn->lasterrcode = 0; - bson_free( conn->lasterrstr ); - conn->lasterrstr = NULL; + mongo_clear_errors( conn ); /* If there's an error, store its code and string in the connection object. */ if( mongo_simple_int_command( conn, db, cmdtype, 1, &out ) == MONGO_OK ) { bson_iterator it; haserror = ( bson_find( &it, &out, "err" ) != BSON_NULL ); - if( haserror ) { - conn->lasterrstr = ( char * )bson_malloc( bson_iterator_string_len( &it ) ); - if( conn->lasterrstr ) { - strcpy( conn->lasterrstr, bson_iterator_string( &it ) ); - } - - if( bson_find( &it, &out, "code" ) != BSON_NULL ) - conn->lasterrcode = bson_iterator_int( &it ); - } + if( haserror ) mongo_set_last_error( conn, &it, &out ); } if( realout ) @@ -1085,15 +1334,15 @@ static int mongo_cmd_get_error_helper( mongo *conn, const char *db, return MONGO_OK; } -int mongo_cmd_get_prev_error( mongo *conn, const char *db, bson *out ) { +MONGO_EXPORT int mongo_cmd_get_prev_error( mongo *conn, const char *db, bson *out ) { return mongo_cmd_get_error_helper( conn, db, out, "getpreverror" ); } -int mongo_cmd_get_last_error( mongo *conn, const char *db, bson *out ) { +MONGO_EXPORT int mongo_cmd_get_last_error( mongo *conn, const char *db, bson *out ) { return mongo_cmd_get_error_helper( conn, db, out, "getlasterror" ); } -bson_bool_t mongo_cmd_ismaster( mongo *conn, bson *realout ) { +MONGO_EXPORT bson_bool_t mongo_cmd_ismaster( mongo *conn, bson *realout ) { bson out = {NULL,0}; bson_bool_t ismaster = 0; @@ -1133,7 +1382,7 @@ static void mongo_pass_digest( const char *user, const char *pass, char hex_dige digest2hex( digest, hex_digest ); } -int mongo_cmd_add_user( mongo *conn, const char *db, const char *user, const char *pass ) { +MONGO_EXPORT int mongo_cmd_add_user( mongo *conn, const char *db, const char *user, const char *pass ) { bson user_obj; bson pass_obj; char hex_digest[33]; @@ -1164,12 +1413,12 @@ int mongo_cmd_add_user( mongo *conn, const char *db, const char *user, const cha return res; } -bson_bool_t mongo_cmd_authenticate( mongo *conn, const char *db, const char *user, const char *pass ) { +MONGO_EXPORT bson_bool_t mongo_cmd_authenticate( mongo *conn, const char *db, const char *user, const char *pass ) { bson from_db; bson cmd; bson out; const char *nonce; - bson_bool_t success = 0; + int result; mongo_md5_state_t st; mongo_md5_byte_t digest[16]; @@ -1200,18 +1449,11 @@ bson_bool_t mongo_cmd_authenticate( mongo *conn, const char *db, const char *use bson_finish( &cmd ); bson_destroy( &from_db ); - /*bson_init( &from_db ); */ - if( mongo_run_command( conn, db, &cmd, &out ) == MONGO_OK ) { - bson_iterator it; - if( bson_find( &it, &out, "ok" ) ) - success = bson_iterator_bool( &it ); - } + + result = mongo_run_command( conn, db, &cmd, &out ); bson_destroy( &from_db ); bson_destroy( &cmd ); - if( success ) - return MONGO_OK; - else - return MONGO_ERROR; + return result; } diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/mongo.h b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/mongo.h index 6f86174101..dd4c4b4f40 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/mongo.h +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/mongo.h @@ -3,7 +3,7 @@ * @brief Main MongoDB Declarations */ -/* Copyright 2009-2011 10gen Inc. +/* Copyright 2009-2012 10gen Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,22 +18,26 @@ * limitations under the License. */ -#ifndef _MONGO_H_ -#define _MONGO_H_ +#ifndef MONGO_H_ +#define MONGO_H_ #include "bson.h" MONGO_EXTERN_C_START #define MONGO_MAJOR 0 -#define MONGO_MINOR 4 -#define MONGO_PATCH 0 +#define MONGO_MINOR 5 +#define MONGO_PATCH 2 #define MONGO_OK 0 #define MONGO_ERROR -1 #define MONGO_DEFAULT_PORT 27017 +#define MONGO_DEFAULT_MAX_BSON_SIZE 4 * 1024 * 1024 + +#define MONGO_ERR_LEN 128 + typedef enum mongo_error_t { MONGO_CONN_SUCCESS = 0, /**< Connection success! */ MONGO_CONN_NO_SOCKET, /**< Could not create a socket. */ @@ -43,15 +47,25 @@ typedef enum mongo_error_t { MONGO_CONN_BAD_SET_NAME, /**< Given rs name doesn't match this replica set. */ MONGO_CONN_NO_PRIMARY, /**< Can't find primary in replica set. Connection closed. */ - MONGO_IO_ERROR, /**< An error occurred while reading or writing on socket. */ + MONGO_IO_ERROR, /**< An error occurred while reading or writing on the socket. */ + MONGO_SOCKET_ERROR, /**< Other socket error. */ MONGO_READ_SIZE_ERROR, /**< The response is not the expected length. */ MONGO_COMMAND_FAILED, /**< The command returned with 'ok' value of 0. */ + MONGO_NS_INVALID, /**< The name for the ns (database or collection) is invalid. */ + MONGO_BSON_INVALID, /**< BSON not valid for the specified op. */ + MONGO_BSON_NOT_FINISHED, /**< BSON object has not been finished. */ + MONGO_BSON_TOO_LARGE /**< BSON object exceeds max BSON size. */ +} mongo_error_t; + +typedef enum mongo_cursor_error_t { MONGO_CURSOR_EXHAUSTED, /**< The cursor has no more results. */ MONGO_CURSOR_INVALID, /**< The cursor has timed out or is not recognized. */ MONGO_CURSOR_PENDING, /**< Tailable cursor still alive but no data. */ - MONGO_BSON_INVALID, /**< BSON not valid for the specified op. */ - MONGO_BSON_NOT_FINISHED /**< BSON object has not been finished. */ -} mongo_error_t; + MONGO_CURSOR_QUERY_FAIL, /**< The server returned an '$err' object, indicating query failure. + See conn->lasterrcode and conn->lasterrstr for details. */ + MONGO_CURSOR_BSON_ERROR /**< Something is wrong with the BSON provided. See conn->err + for details. */ +} mongo_cursor_error_t; enum mongo_cursor_flags { MONGO_CURSOR_MUST_FREE = 1, /**< mongo_cursor_destroy should free cursor. */ @@ -137,12 +151,14 @@ typedef struct mongo { int flags; /**< Flags on this connection object. */ int conn_timeout_ms; /**< Connection timeout in milliseconds. */ int op_timeout_ms; /**< Read and write timeout in milliseconds. */ + int max_bson_size; /**< Largest BSON object allowed on this connection. */ bson_bool_t connected; /**< Connection status. */ - mongo_error_t err; /**< Most recent driver error code. */ - char *errstr; /**< String version of most recent driver error code. */ - int lasterrcode; /**< getlasterror given by the server on calls. */ - char *lasterrstr; /**< getlasterror string generated by server. */ + mongo_error_t err; /**< Most recent driver error code. */ + int errcode; /**< Most recent errno or WSAGetLastError(). */ + char errstr[MONGO_ERR_LEN]; /**< String version of error. */ + int lasterrcode; /**< getlasterror code from the server. */ + char lasterrstr[MONGO_ERR_LEN]; /**< getlasterror string from the server. */ } mongo; typedef struct { @@ -152,9 +168,9 @@ typedef struct { int flags; /**< Flags used internally by this drivers. */ int seen; /**< Number returned so far. */ bson current; /**< This cursor's current bson object. */ - mongo_error_t err; /**< Errors on this cursor. */ - bson *query; /**< Bitfield containing cursor options. */ - bson *fields; /**< Bitfield containing cursor options. */ + mongo_cursor_error_t err; /**< Errors on this cursor. */ + const bson *query; /**< Bitfield containing cursor options. */ + const bson *fields;/**< Bitfield containing cursor options. */ int options; /**< Bitfield containing cursor options. */ int limit; /**< Bitfield containing cursor options. */ int skip; /**< Bitfield containing cursor options. */ @@ -162,9 +178,44 @@ typedef struct { /* Connection API */ -/** Initialize a new mongo connection object. If not created - * with mongo_new, you must initialize each mongo - * object using this function. +MONGO_EXPORT mongo* mongo_create(); +MONGO_EXPORT void mongo_dispose(mongo* conn); +MONGO_EXPORT int mongo_get_err(mongo* conn); +MONGO_EXPORT int mongo_is_connected(mongo* conn); +MONGO_EXPORT int mongo_get_op_timeout(mongo* conn); +MONGO_EXPORT const char* mongo_get_primary(mongo* conn); +MONGO_EXPORT int mongo_get_socket(mongo* conn) ; +MONGO_EXPORT int mongo_get_host_count(mongo* conn); +MONGO_EXPORT const char* mongo_get_host(mongo* conn, int i); +MONGO_EXPORT mongo_cursor* mongo_cursor_create(); +MONGO_EXPORT void mongo_cursor_dispose(mongo_cursor* cursor); +MONGO_EXPORT int mongo_get_server_err(mongo* conn); +MONGO_EXPORT const char* mongo_get_server_err_string(mongo* conn); + +/** + * Set an error this mongo connection object. Mostly for internal use. + * + * @param conn a mongo connection object. + * @param err a driver error code of mongo_error_t. + * @param errstr a string version of the error. + * @param errorcode Currently errno or WSAGetLastError(). + */ +MONGO_EXPORT void __mongo_set_error( mongo *conn, mongo_error_t err, + const char *errstr, int errorcode ); +/** + * Clear all errors stored on this mongo connection object. + * + * @param conn a mongo connection object. + */ +MONGO_EXPORT void mongo_clear_errors( mongo *conn ); + +/** Initialize sockets for Windows. + */ +MONGO_EXPORT void mongo_init_sockets(); + +/** + * Initialize a new mongo connection object. You must initialize each mongo + * object using this function. * * @note When finished, you must pass this object to * mongo_destroy( ). @@ -172,7 +223,7 @@ typedef struct { * @param conn a mongo connection object allocated on the stack * or heap. */ -void mongo_init( mongo *conn ); +MONGO_EXPORT void mongo_init( mongo *conn ); /** * Connect to a single MongoDB server. @@ -182,9 +233,9 @@ void mongo_init( mongo *conn ); * @param port the port to connect to. * * @return MONGO_OK or MONGO_ERROR on failure. On failure, a constant of type - * mongo_conn_return_t will be set on the conn->err field. + * mongo_error_t will be set on the conn->err field. */ -int mongo_connect( mongo *conn , const char *host, int port ); +MONGO_EXPORT int mongo_connect( mongo *conn , const char *host, int port ); /** * Set up this connection object for connecting to a replica set. @@ -193,7 +244,7 @@ int mongo_connect( mongo *conn , const char *host, int port ); * @param conn a mongo object. * @param name the name of the replica set to connect to. * */ -void mongo_replset_init( mongo *conn, const char *name ); +MONGO_EXPORT void mongo_replset_init( mongo *conn, const char *name ); /** * Add a seed node to the replica set connection object. @@ -204,7 +255,7 @@ void mongo_replset_init( mongo *conn, const char *name ); * @param host a numerical network address or a network hostname. * @param port the port to connect to. */ -void mongo_replset_add_seed( mongo *conn, const char *host, int port ); +MONGO_EXPORT void mongo_replset_add_seed( mongo *conn, const char *host, int port ); /** * Utility function for converting a host-port string to a mongo_host_port. @@ -215,6 +266,17 @@ void mongo_replset_add_seed( mongo *conn, const char *host, int port ); */ void mongo_parse_host( const char *host_string, mongo_host_port *host_port ); +/** + * Utility function for validation database and collection names. + * + * @param conn a mongo object. + * + * @return MONGO_OK or MONGO_ERROR on failure. On failure, a constant of type + * mongo_conn_return_t will be set on the conn->err field. + * + */ +MONGO_EXPORT int mongo_validate_ns( mongo *conn, const char *ns ); + /** * Connect to a replica set. * @@ -226,7 +288,7 @@ void mongo_parse_host( const char *host_string, mongo_host_port *host_port ); * @return MONGO_OK or MONGO_ERROR on failure. On failure, a constant of type * mongo_conn_return_t will be set on the conn->err field. */ -int mongo_replset_connect( mongo *conn ); +MONGO_EXPORT int mongo_replset_connect( mongo *conn ); /** Set a timeout for operations on this connection. This * is a platform-specific feature, and only work on *nix @@ -238,7 +300,7 @@ int mongo_replset_connect( mongo *conn ); * @return MONGO_OK. On error, return MONGO_ERROR and * set the conn->err field. */ -int mongo_set_op_timeout( mongo *conn, int millis ); +MONGO_EXPORT int mongo_set_op_timeout( mongo *conn, int millis ); /** * Ensure that this connection is healthy by performing @@ -248,7 +310,7 @@ int mongo_set_op_timeout( mongo *conn, int millis ); * * @return MONGO_OK if connected; otherwise, MONGO_ERROR. */ -int mongo_check_connection( mongo *conn ); +MONGO_EXPORT int mongo_check_connection( mongo *conn ); /** * Try reconnecting to the server using the existing connection settings. @@ -261,7 +323,7 @@ int mongo_check_connection( mongo *conn ); * @return MONGO_OK or MONGO_ERROR and * set the conn->err field. */ -int mongo_reconnect( mongo *conn ); +MONGO_EXPORT int mongo_reconnect( mongo *conn ); /** * Close the current connection to the server. After calling @@ -270,7 +332,7 @@ int mongo_reconnect( mongo *conn ); * * @param conn a mongo object. */ -void mongo_disconnect( mongo *conn ); +MONGO_EXPORT void mongo_disconnect( mongo *conn ); /** * Close any existing connection to the server and free all allocated @@ -280,7 +342,7 @@ void mongo_disconnect( mongo *conn ); * * @param conn a mongo object. */ -void mongo_destroy( mongo *conn ); +MONGO_EXPORT void mongo_destroy( mongo *conn ); /** * Insert a BSON document into a MongoDB server. This function @@ -295,7 +357,7 @@ void mongo_destroy( mongo *conn ); * field is MONGO_BSON_INVALID, check the err field * on the bson struct for the reason. */ -int mongo_insert( mongo *conn, const char *ns, bson *data ); +MONGO_EXPORT int mongo_insert( mongo *conn, const char *ns, const bson *data ); /** * Insert a batch of BSON documents into a MongoDB server. This function @@ -309,8 +371,8 @@ int mongo_insert( mongo *conn, const char *ns, bson *data ); * @return MONGO_OK or MONGO_ERROR. * */ -int mongo_insert_batch( mongo *conn , const char *ns , - bson **data , int num ); +MONGO_EXPORT int mongo_insert_batch( mongo *conn , const char *ns , + const bson **data , int num ); /** * Update a document in a MongoDB server. @@ -324,7 +386,7 @@ int mongo_insert_batch( mongo *conn , const char *ns , * @return MONGO_OK or MONGO_ERROR with error stored in conn object. * */ -int mongo_update( mongo *conn, const char *ns, const bson *cond, +MONGO_EXPORT int mongo_update( mongo *conn, const char *ns, const bson *cond, const bson *op, int flags ); /** @@ -336,7 +398,7 @@ int mongo_update( mongo *conn, const char *ns, const bson *cond, * * @return MONGO_OK or MONGO_ERROR with error stored in conn object. */ -int mongo_remove( mongo *conn, const char *ns, const bson *cond ); +MONGO_EXPORT int mongo_remove( mongo *conn, const char *ns, const bson *cond ); /** * Find documents in a MongoDB server. @@ -353,8 +415,8 @@ int mongo_remove( mongo *conn, const char *ns, const bson *cond ); * an error has occurred. For finer-grained error checking, * use the cursor builder API instead. */ -mongo_cursor *mongo_find( mongo *conn, const char *ns, bson *query, - bson *fields, int limit, int skip, int options ); +MONGO_EXPORT mongo_cursor *mongo_find( mongo *conn, const char *ns, const bson *query, + const bson *fields, int limit, int skip, int options ); /** * Initalize a new cursor object. @@ -363,7 +425,7 @@ mongo_cursor *mongo_find( mongo *conn, const char *ns, bson *query, * @param ns the namespace, represented as the the database * name and collection name separated by a dot. e.g., "test.users" */ -void mongo_cursor_init( mongo_cursor *cursor, mongo *conn, const char *ns ); +MONGO_EXPORT void mongo_cursor_init( mongo_cursor *cursor, mongo *conn, const char *ns ); /** * Set the bson object specifying this cursor's query spec. If @@ -376,7 +438,7 @@ void mongo_cursor_init( mongo_cursor *cursor, mongo *conn, const char *ns ); * $query, $orderby, $hint, and/or $explain. See * http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol for details. */ -void mongo_cursor_set_query( mongo_cursor *cursor, bson *query ); +MONGO_EXPORT void mongo_cursor_set_query( mongo_cursor *cursor, const bson *query ); /** * Set the fields to return for this cursor. If you want to return @@ -386,7 +448,7 @@ void mongo_cursor_set_query( mongo_cursor *cursor, bson *query ); * @param fields a bson object representing the fields to return. * See http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields. */ -void mongo_cursor_set_fields( mongo_cursor *cursor, bson *fields ); +MONGO_EXPORT void mongo_cursor_set_fields( mongo_cursor *cursor, const bson *fields ); /** * Set the number of documents to skip. @@ -394,7 +456,7 @@ void mongo_cursor_set_fields( mongo_cursor *cursor, bson *fields ); * @param cursor * @param skip */ -void mongo_cursor_set_skip( mongo_cursor *cursor, int skip ); +MONGO_EXPORT void mongo_cursor_set_skip( mongo_cursor *cursor, int skip ); /** * Set the number of documents to return. @@ -402,7 +464,7 @@ void mongo_cursor_set_skip( mongo_cursor *cursor, int skip ); * @param cursor * @param limit */ -void mongo_cursor_set_limit( mongo_cursor *cursor, int limit ); +MONGO_EXPORT void mongo_cursor_set_limit( mongo_cursor *cursor, int limit ); /** * Set any of the available query options (e.g., MONGO_TAILABLE). @@ -411,7 +473,7 @@ void mongo_cursor_set_limit( mongo_cursor *cursor, int limit ); * @param options a bitfield storing query options. See * mongo_cursor_bitfield_t for available constants. */ -void mongo_cursor_set_options( mongo_cursor *cursor, int options ); +MONGO_EXPORT void mongo_cursor_set_options( mongo_cursor *cursor, int options ); /** * Return the current BSON object data as a const char*. This is useful @@ -419,7 +481,7 @@ void mongo_cursor_set_options( mongo_cursor *cursor, int options ); * * @param cursor */ -const char *mongo_cursor_data( mongo_cursor *cursor ); +MONGO_EXPORT const char *mongo_cursor_data( mongo_cursor *cursor ); /** * Return the current BSON object data as a const char*. This is useful @@ -427,7 +489,7 @@ const char *mongo_cursor_data( mongo_cursor *cursor ); * * @param cursor */ -const bson *mongo_cursor_bson( mongo_cursor *cursor ); +MONGO_EXPORT const bson *mongo_cursor_bson( mongo_cursor *cursor ); /** * Iterate the cursor, returning the next item. When successful, @@ -438,7 +500,7 @@ const bson *mongo_cursor_bson( mongo_cursor *cursor ); * @return MONGO_OK. On error, returns MONGO_ERROR and sets * cursor->err with a value of mongo_error_t. */ -int mongo_cursor_next( mongo_cursor *cursor ); +MONGO_EXPORT int mongo_cursor_next( mongo_cursor *cursor ); /** * Destroy a cursor object. When finished with a cursor, you @@ -449,7 +511,7 @@ int mongo_cursor_next( mongo_cursor *cursor ); * @return MONGO_OK or an error code. On error, check cursor->conn->err * for errors. */ -int mongo_cursor_destroy( mongo_cursor *cursor ); +MONGO_EXPORT int mongo_cursor_destroy( mongo_cursor *cursor ); /** * Find a single document in a MongoDB server. @@ -462,8 +524,8 @@ int mongo_cursor_destroy( mongo_cursor *cursor ); * */ /* out can be NULL if you don't care about results. useful for commands */ -bson_bool_t mongo_find_one( mongo *conn, const char *ns, bson *query, - bson *fields, bson *out ); +MONGO_EXPORT int mongo_find_one( mongo *conn, const char *ns, const bson *query, + const bson *fields, bson *out ); /* MongoDB Helper Functions */ @@ -478,8 +540,8 @@ bson_bool_t mongo_find_one( mongo *conn, const char *ns, bson *query, * @return the number of matching documents. If the command fails, * MONGO_ERROR is returned. */ -int64_t mongo_count( mongo *conn, const char *db, const char *coll, - bson *query ); +MONGO_EXPORT double mongo_count( mongo *conn, const char *db, const char *coll, + const bson *query ); /** * Create a compouned index. @@ -494,7 +556,7 @@ int64_t mongo_count( mongo *conn, const char *db, const char *coll, * * @return MONGO_OK if index is created successfully; otherwise, MONGO_ERROR. */ -int mongo_create_index( mongo *conn, const char *ns, bson *key, int options, bson *out ); +MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns, const bson *key, int options, bson *out ); /** * Create an index with a single key. @@ -521,9 +583,9 @@ bson_bool_t mongo_create_simple_index( mongo *conn, const char *ns, const char * * @param command the BSON command to run. * @param out the BSON result of the command. * - * @return true if the command ran without error. + * @return MONGO_OK if the command ran without error. */ -bson_bool_t mongo_run_command( mongo *conn, const char *db, bson *command, bson *out ); +MONGO_EXPORT int mongo_run_command( mongo *conn, const char *db, const bson *command, bson *out ); /** * Run a command that accepts a simple string key and integer value. @@ -537,7 +599,7 @@ bson_bool_t mongo_run_command( mongo *conn, const char *db, bson *command, bson * @return MONGO_OK or an error code. * */ -int mongo_simple_int_command( mongo *conn, const char *db, +MONGO_EXPORT int mongo_simple_int_command( mongo *conn, const char *db, const char *cmd, int arg, bson *out ); /** @@ -552,7 +614,7 @@ int mongo_simple_int_command( mongo *conn, const char *db, * @return true if the command ran without error. * */ -bson_bool_t mongo_simple_str_command( mongo *conn, const char *db, const char *cmd, const char *arg, bson *out ); +MONGO_EXPORT int mongo_simple_str_command( mongo *conn, const char *db, const char *cmd, const char *arg, bson *out ); /** * Drop a database. @@ -562,7 +624,7 @@ bson_bool_t mongo_simple_str_command( mongo *conn, const char *db, const char *c * * @return MONGO_OK or an error code. */ -int mongo_cmd_drop_db( mongo *conn, const char *db ); +MONGO_EXPORT int mongo_cmd_drop_db( mongo *conn, const char *db ); /** * Drop a collection. @@ -574,7 +636,7 @@ int mongo_cmd_drop_db( mongo *conn, const char *db ); * * @return true if the collection drop was successful. */ -bson_bool_t mongo_cmd_drop_collection( mongo *conn, const char *db, const char *collection, bson *out ); +MONGO_EXPORT int mongo_cmd_drop_collection( mongo *conn, const char *db, const char *collection, bson *out ); /** * Add a database user. @@ -586,7 +648,7 @@ bson_bool_t mongo_cmd_drop_collection( mongo *conn, const char *db, const char * * * @return MONGO_OK or MONGO_ERROR. */ -int mongo_cmd_add_user( mongo *conn, const char *db, const char *user, const char *pass ); +MONGO_EXPORT int mongo_cmd_add_user( mongo *conn, const char *db, const char *user, const char *pass ); /** * Authenticate a user. @@ -598,7 +660,7 @@ int mongo_cmd_add_user( mongo *conn, const char *db, const char *user, const cha * * @return MONGO_OK on sucess and MONGO_ERROR on failure. */ -int mongo_cmd_authenticate( mongo *conn, const char *db, const char *user, const char *pass ); +MONGO_EXPORT int mongo_cmd_authenticate( mongo *conn, const char *db, const char *user, const char *pass ); /** * Check if the current server is a master. @@ -609,7 +671,7 @@ int mongo_cmd_authenticate( mongo *conn, const char *db, const char *user, const * @return true if the server is a master. */ /* return value is master status */ -bson_bool_t mongo_cmd_ismaster( mongo *conn, bson *out ); +MONGO_EXPORT bson_bool_t mongo_cmd_ismaster( mongo *conn, bson *out ); /** * Get the error for the last command with the current connection. @@ -621,7 +683,7 @@ bson_bool_t mongo_cmd_ismaster( mongo *conn, bson *out ); * @return MONGO_OK if no error and MONGO_ERROR on error. On error, check the values * of conn->lasterrcode and conn->lasterrstr for the error status. */ -int mongo_cmd_get_last_error( mongo *conn, const char *db, bson *out ); +MONGO_EXPORT int mongo_cmd_get_last_error( mongo *conn, const char *db, bson *out ); /** * Get the most recent error with the current connection. @@ -633,7 +695,7 @@ int mongo_cmd_get_last_error( mongo *conn, const char *db, bson *out ); * @return MONGO_OK if no error and MONGO_ERROR on error. On error, check the values * of conn->lasterrcode and conn->lasterrstr for the error status. */ -int mongo_cmd_get_prev_error( mongo *conn, const char *db, bson *out ); +MONGO_EXPORT int mongo_cmd_get_prev_error( mongo *conn, const char *db, bson *out ); /** * Reset the error state for the connection. @@ -641,7 +703,8 @@ int mongo_cmd_get_prev_error( mongo *conn, const char *db, bson *out ); * @param conn a mongo object. * @param db the name of the database. */ -void mongo_cmd_reset_error( mongo *conn, const char *db ); +MONGO_EXPORT void mongo_cmd_reset_error( mongo *conn, const char *db ); + MONGO_EXTERN_C_END diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/net.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/net.c deleted file mode 100644 index 5c8ad9ba5e..0000000000 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/net.c +++ /dev/null @@ -1,98 +0,0 @@ -/* net.c */ - -/* Copyright 2009-2011 10gen Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* Implementation for generic version of net.h */ -#include "net.h" -#include - -int mongo_write_socket( mongo *conn, const void *buf, int len ) { - const char *cbuf = buf; - while ( len ) { - int sent = send( conn->sock, cbuf, len, 0 ); - if ( sent == -1 ) { - conn->err = MONGO_IO_ERROR; - return MONGO_ERROR; - } - cbuf += sent; - len -= sent; - } - - return MONGO_OK; -} - -int mongo_read_socket( mongo *conn, void *buf, int len ) { - char *cbuf = buf; - while ( len ) { - int sent = recv( conn->sock, cbuf, len, 0 ); - if ( sent == 0 || sent == -1 ) { - conn->err = MONGO_IO_ERROR; - return MONGO_ERROR; - } - cbuf += sent; - len -= sent; - } - - return MONGO_OK; -} - -/* This is a no-op in the generic implementation. */ -int mongo_set_socket_op_timeout( mongo *conn, int millis ) { - return MONGO_OK; -} - -static int mongo_create_socket( mongo *conn ) { - int fd; - - if( ( fd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) { - conn->err = MONGO_CONN_NO_SOCKET; - return MONGO_ERROR; - } - conn->sock = fd; - - return MONGO_OK; -} - -int mongo_socket_connect( mongo *conn, const char *host, int port ) { - struct sockaddr_in sa; - socklen_t addressSize; - int flag = 1; - - if( mongo_create_socket( conn ) != MONGO_OK ) - return MONGO_ERROR; - - memset( sa.sin_zero , 0 , sizeof( sa.sin_zero ) ); - sa.sin_family = AF_INET; - sa.sin_port = htons( port ); - sa.sin_addr.s_addr = inet_addr( host ); - addressSize = sizeof( sa ); - - if ( connect( conn->sock, ( struct sockaddr * )&sa, addressSize ) == -1 ) { - mongo_close_socket( conn->sock ); - conn->connected = 0; - conn->sock = 0; - conn->err = MONGO_CONN_FAIL; - return MONGO_ERROR; - } - - setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, ( char * ) &flag, sizeof( flag ) ); - if( conn->op_timeout_ms > 0 ) - mongo_set_socket_op_timeout( conn, conn->op_timeout_ms ); - - conn->connected = 1; - - return MONGO_OK; -} diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/net.h b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/net.h deleted file mode 100644 index 49190877c8..0000000000 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/net.h +++ /dev/null @@ -1,57 +0,0 @@ -/** @file net.h */ - -/* Copyright 2009-2011 10gen Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* Header for generic net.h */ -#ifndef _MONGO_NET_H_ -#define _MONGO_NET_H_ - -#include "mongo.h" - -#ifdef _WIN32 -#include -#include -#define mongo_close_socket(sock) ( closesocket(sock) ) -typedef int socklen_t; -#else -#include -#include -#include -#include -#include -#include -#include -#define mongo_close_socket(sock) ( close(sock) ) -#endif - -#ifndef _WIN32 -#include -#endif - -#if defined(_XOPEN_SOURCE) || defined(_POSIX_SOURCE) || _POSIX_C_SOURCE >= 1 -#define _MONGO_USE_GETADDRINFO -#endif - -MONGO_EXTERN_C_START - -/* This is a no-op in the generic implementation. */ -int mongo_set_socket_op_timeout( mongo *conn, int millis ); -int mongo_read_socket( mongo *conn, void *buf, int len ); -int mongo_write_socket( mongo *conn, const void *buf, int len ); -int mongo_socket_connect( mongo *conn, const char *host, int port ); - -MONGO_EXTERN_C_END -#endif diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/numbers.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/numbers.c index a63e3d73f9..b3032d5ee3 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/numbers.c +++ b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/numbers.c @@ -1,4 +1,4 @@ -/* Copyright 2009-2011 10gen Inc. +/* Copyright 2009-2012 10gen Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/platform.h b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/platform.h deleted file mode 100644 index 4a96af77e3..0000000000 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/platform.h +++ /dev/null @@ -1,94 +0,0 @@ -/** @file platform.h */ - -/** Copyright 2009-2011 10gen Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -/* all platform-specific ifdefs should go here */ - -#ifndef _PLATFORM_HACKS_H_ -#define _PLATFORM_HACKS_H_ - -#ifdef __GNUC__ -#define MONGO_INLINE static __inline__ -#else -#define MONGO_INLINE static -#endif - -#ifdef __cplusplus -#define MONGO_EXTERN_C_START extern "C" { -#define MONGO_EXTERN_C_END } -#else -#define MONGO_EXTERN_C_START -#define MONGO_EXTERN_C_END -#endif - - -#if defined(MONGO_HAVE_STDINT) || __STDC_VERSION__ >= 199901L -#include -#elif defined(MONGO_HAVE_UNISTD) -#include -#elif defined(MONGO_USE__INT64) -typedef __int64 int64_t; -typedef unsigned __int64 uint64_t; -#elif defined(MONGO_USE_LONG_LONG_INT) -typedef long long int int64_t; -typedef unsigned long long int uint64_t; -#else -#error must have a 64bit int type -#endif - -/* big endian is only used for OID generation. little is used everywhere else */ -#ifdef MONGO_BIG_ENDIAN -#define bson_little_endian64(out, in) ( bson_swap_endian64(out, in) ) -#define bson_little_endian32(out, in) ( bson_swap_endian32(out, in) ) -#define bson_big_endian64(out, in) ( memcpy(out, in, 8) ) -#define bson_big_endian32(out, in) ( memcpy(out, in, 4) ) -#else -#define bson_little_endian64(out, in) ( memcpy(out, in, 8) ) -#define bson_little_endian32(out, in) ( memcpy(out, in, 4) ) -#define bson_big_endian64(out, in) ( bson_swap_endian64(out, in) ) -#define bson_big_endian32(out, in) ( bson_swap_endian32(out, in) ) -#endif - -MONGO_EXTERN_C_START - -MONGO_INLINE void bson_swap_endian64( void *outp, const void *inp ) { - const char *in = ( const char * )inp; - char *out = ( char * )outp; - - out[0] = in[7]; - out[1] = in[6]; - out[2] = in[5]; - out[3] = in[4]; - out[4] = in[3]; - out[5] = in[2]; - out[6] = in[1]; - out[7] = in[0]; - -} -MONGO_INLINE void bson_swap_endian32( void *outp, const void *inp ) { - const char *in = ( const char * )inp; - char *out = ( char * )outp; - - out[0] = in[3]; - out[1] = in[2]; - out[2] = in[1]; - out[3] = in[0]; -} - -MONGO_EXTERN_C_END - -#endif diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/platform/linux/net.c b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/platform/linux/net.c deleted file mode 100644 index b2f7c22898..0000000000 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/platform/linux/net.c +++ /dev/null @@ -1,183 +0,0 @@ -/* net.c */ - -/* Copyright 2009-2011 10gen Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* Implementation for Linux version of net.h */ -#include "net.h" -#include - -int mongo_write_socket( mongo *conn, const void *buf, int len ) { - const char *cbuf = buf; - while ( len ) { - int sent = send( conn->sock, cbuf, len, 0 ); - if ( sent == -1 ) { - conn->err = MONGO_IO_ERROR; - return MONGO_ERROR; - } - cbuf += sent; - len -= sent; - } - - return MONGO_OK; -} - -int mongo_read_socket( mongo *conn, void *buf, int len ) { - char *cbuf = buf; - while ( len ) { - int sent = recv( conn->sock, cbuf, len, 0 ); - if ( sent == 0 || sent == -1 ) { - conn->err = MONGO_IO_ERROR; - return MONGO_ERROR; - } - cbuf += sent; - len -= sent; - } - - return MONGO_OK; -} - -static int mongo_create_socket( mongo *conn ) { - int fd; - - if( ( fd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) { - conn->err = MONGO_CONN_NO_SOCKET; - return MONGO_ERROR; - } - conn->sock = fd; - - return MONGO_OK; -} - -static int mongo_set_blocking_status( mongo *conn ) { - int flags; - int blocking; - - blocking = ( conn->conn_timeout_ms == 0 ); - if( blocking ) - return MONGO_OK; - else { - if( ( flags = fcntl( conn->sock, F_GETFL ) ) == -1 ) { - conn->err = MONGO_IO_ERROR; - mongo_close_socket( conn->sock ); - return MONGO_ERROR; - } - - flags |= O_NONBLOCK; - - if( ( flags = fcntl( conn->sock, F_SETFL, flags ) ) == -1 ) { - conn->err = MONGO_IO_ERROR; - mongo_close_socket( conn->sock ); - return MONGO_ERROR; - } - } - - return MONGO_OK; -} - -int mongo_set_socket_op_timeout( mongo *conn, int millis ) { - struct timeval tv; - tv.tv_sec = millis / 1000; - tv.tv_usec = ( millis % 1000 ) * 1000; - - if ( setsockopt( conn->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof( tv ) ) == -1 ) { - conn->err = MONGO_IO_ERROR; - return MONGO_ERROR; - } - - if ( setsockopt( conn->sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof( tv ) ) == -1 ) { - conn->err = MONGO_IO_ERROR; - return MONGO_ERROR; - } - - return MONGO_OK; -} - -#ifdef _MONGO_USE_GETADDRINFO -int mongo_socket_connect( mongo *conn, const char *host, int port ) { - - struct addrinfo *addrs = NULL; - struct addrinfo hints; - int flag = 1; - char port_str[12]; - int ret; - - conn->sock = 0; - conn->connected = 0; - - memset( &hints, 0, sizeof( hints ) ); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - - sprintf( port_str, "%d", port ); - - if( mongo_create_socket( conn ) != MONGO_OK ) - return MONGO_ERROR; - - if( getaddrinfo( host, port_str, &hints, &addrs ) != 0 ) { - bson_errprintf( "getaddrinfo failed: %s", gai_strerror( ret ) ); - conn->err = MONGO_CONN_ADDR_FAIL; - return MONGO_ERROR; - } - - if ( connect( conn->sock, addrs->ai_addr, addrs->ai_addrlen ) == -1 ) { - mongo_close_socket( conn->sock ); - freeaddrinfo( addrs ); - conn->err = MONGO_CONN_FAIL; - return MONGO_ERROR; - } - - setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, ( char * )&flag, sizeof( flag ) ); - if( conn->op_timeout_ms > 0 ) - mongo_set_socket_op_timeout( conn, conn->op_timeout_ms ); - - conn->connected = 1; - freeaddrinfo( addrs ); - - return MONGO_OK; -} -#else -int mongo_socket_connect( mongo *conn, const char *host, int port ) { - struct sockaddr_in sa; - socklen_t addressSize; - int flag = 1; - - if( mongo_create_socket( conn ) != MONGO_OK ) - return MONGO_ERROR; - - memset( sa.sin_zero , 0 , sizeof( sa.sin_zero ) ); - sa.sin_family = AF_INET; - sa.sin_port = htons( port ); - sa.sin_addr.s_addr = inet_addr( host ); - addressSize = sizeof( sa ); - - if ( connect( conn->sock, ( struct sockaddr * )&sa, addressSize ) == -1 ) { - mongo_close_socket( conn->sock ); - conn->connected = 0; - conn->sock = 0; - conn->err = MONGO_CONN_FAIL; - return MONGO_ERROR; - } - - setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, ( char * ) &flag, sizeof( flag ) ); - - if( conn->op_timeout_ms > 0 ) - mongo_set_socket_op_timeout( conn, conn->op_timeout_ms ); - - conn->connected = 1; - - return MONGO_OK; -} -#endif diff --git a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/platform/linux/net.h b/src/mod/event_handlers/mod_cdr_mongodb/driver/src/platform/linux/net.h deleted file mode 100644 index 054247c1e8..0000000000 --- a/src/mod/event_handlers/mod_cdr_mongodb/driver/src/platform/linux/net.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - * @file net.h - * @brief Networking. - */ - -/* Copyright 2009-2011 10gen Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* Header for Linux net.h */ -#ifndef _MONGO_NET_H_ -#define _MONGO_NET_H_ - -#include "mongo.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#define mongo_close_socket(sock) ( close(sock) ) - -#if defined(_XOPEN_SOURCE) || defined(_POSIX_SOURCE) || _POSIX_C_SOURCE >= 1 -#define _MONGO_USE_GETADDRINFO -#endif - -MONGO_EXTERN_C_START - -int mongo_set_socket_op_timeout( mongo *conn, int millis ); -int mongo_read_socket( mongo *conn, void *buf, int len ); -int mongo_write_socket( mongo *conn, const void *buf, int len ); -int mongo_socket_connect( mongo *conn, const char *host, int port ); - -MONGO_EXTERN_C_END -#endif diff --git a/src/mod/event_handlers/mod_cdr_mongodb/mod_cdr_mongodb.c b/src/mod/event_handlers/mod_cdr_mongodb/mod_cdr_mongodb.c index f2ecd35a4a..4c8aded61c 100644 --- a/src/mod/event_handlers/mod_cdr_mongodb/mod_cdr_mongodb.c +++ b/src/mod/event_handlers/mod_cdr_mongodb/mod_cdr_mongodb.c @@ -124,11 +124,16 @@ static switch_status_t my_on_reporting(switch_core_session_t *session) /* Channel variables */ bson_append_start_object(&cdr, "variables"); - for (hi = switch_channel_variable_first(channel); hi; hi = hi->next) { - if (!zstr(hi->name) && !zstr(hi->value)) { - bson_append_string(&cdr, hi->name, hi->value); + + if ((hi = switch_channel_variable_first(channel))) { + for (; hi; hi = hi->next) { + if (!zstr(hi->name) && !zstr(hi->value)) { + bson_append_string(&cdr, hi->name, hi->value); + } } + switch_channel_variable_last(channel); } + bson_append_finish_object(&cdr); /* variables */ diff --git a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c index b56c094872..d71a6fa70e 100644 --- a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c +++ b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c @@ -1356,16 +1356,16 @@ static switch_status_t read_packet(listener_t *listener, switch_event_t **event, if (switch_test_flag(listener, LFLAG_LINGER)) { char disco_buf[512] = ""; - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(listener->session), SWITCH_LOG_DEBUG, "%s Socket Linger %"TIME_T_FMT"\n", - switch_channel_get_name(channel), listener->linger_timeout); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(listener->session), SWITCH_LOG_DEBUG, "%s Socket Linger %d\n", + switch_channel_get_name(channel), (int)listener->linger_timeout); switch_snprintf(disco_buf, sizeof(disco_buf), "Content-Type: text/disconnect-notice\n" "Controlled-Session-UUID: %s\n" "Content-Disposition: linger\n" "Channel-Name: %s\n" - "Linger-Time: %"TIME_T_FMT"\n" + "Linger-Time: %d\n" "Content-Length: 0\n\n", - switch_core_session_get_uuid(listener->session), switch_channel_get_name(channel), listener->linger_timeout); + switch_core_session_get_uuid(listener->session), switch_channel_get_name(channel), (int)listener->linger_timeout); if (listener->linger_timeout != (time_t) -1) { diff --git a/src/mod/event_handlers/mod_json_cdr/Makefile b/src/mod/event_handlers/mod_json_cdr/Makefile index 0620c7df63..c9dfa5399e 100644 --- a/src/mod/event_handlers/mod_json_cdr/Makefile +++ b/src/mod/event_handlers/mod_json_cdr/Makefile @@ -1,27 +1,4 @@ -json-c=json-c-0.9 BASE=../../../.. - -JSON_DIR=$(switch_srcdir)/libs/$(json-c) -JSON_BUILDDIR=$(switch_builddir)/libs/$(json-c) - -JSONLA=$(JSON_BUILDDIR)/libjson.la - -LOCAL_CFLAGS=-I$(JSON_DIR) -LOCAL_LIBADD=$(JSONLA) - include $(BASE)/build/modmake.rules -$(JSON_DIR): - $(GETLIB) $(json-c).tar.gz - -$(JSON_BUILDDIR)/Makefile: $(JSON_DIR) - mkdir -p $(JSON_BUILDDIR) - cd $(JSON_BUILDDIR) && $(DEFAULT_VARS) $(JSON_DIR)/configure $(DEFAULT_ARGS) --srcdir=$(JSON_DIR) CPPFLAGS= LDFLAGS= - $(TOUCH_TARGET) - -$(JSONLA): $(JSON_BUILDDIR)/Makefile - cd $(JSON_BUILDDIR) && $(MAKE) - $(TOUCH_TARGET) - - diff --git a/src/mod/event_handlers/mod_json_cdr/mod_json_cdr.c b/src/mod/event_handlers/mod_json_cdr/mod_json_cdr.c index 92a51c65cb..36626461a5 100644 --- a/src/mod/event_handlers/mod_json_cdr/mod_json_cdr.c +++ b/src/mod/event_handlers/mod_json_cdr/mod_json_cdr.c @@ -33,7 +33,6 @@ #include #include #include -#include #define MAX_URLS 20 #define MAX_ERR_DIRS 20 @@ -181,432 +180,11 @@ static switch_status_t set_json_cdr_log_dirs() return status; } -#define json_object_safe_new_string(str) json_object_new_string(str ? str : "") -#define JSON_ENSURE_SUCCESS(obj) if (is_error(obj)) { return; } -static void set_json_profile_data(struct json_object *json, switch_caller_profile_t *caller_profile) -{ - struct json_object *param = NULL; - - param = json_object_safe_new_string((char *)caller_profile->username); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "username", param); - - param = json_object_safe_new_string((char *)caller_profile->dialplan); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "dialplan", param); - - param = json_object_safe_new_string((char *)caller_profile->caller_id_name); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "caller_id_name", param); - - param = json_object_safe_new_string((char *)caller_profile->ani); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "ani", param); - - param = json_object_safe_new_string((char *)caller_profile->aniii); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "aniii", param); - - param = json_object_safe_new_string((char *)caller_profile->caller_id_number); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "caller_id_number", param); - - param = json_object_safe_new_string((char *)caller_profile->network_addr); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "network_addr", param); - - param = json_object_safe_new_string((char *)caller_profile->rdnis); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "rdnis", param); - - param = json_object_safe_new_string(caller_profile->destination_number); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "destination_number", param); - - param = json_object_safe_new_string(caller_profile->uuid); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "uuid", param); - - param = json_object_safe_new_string((char *)caller_profile->source); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "source", param); - - param = json_object_safe_new_string((char *)caller_profile->context); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "context", param); - - param = json_object_safe_new_string(caller_profile->chan_name); - JSON_ENSURE_SUCCESS(param); - json_object_object_add(json, "chan_name", param); - -} - -static void set_json_chan_vars(struct json_object *json, switch_channel_t *channel) -{ - struct json_object *variable = NULL; - switch_event_header_t *hi = switch_channel_variable_first(channel); - - if (!hi) - return; - - for (; hi; hi = hi->next) { - if (!zstr(hi->name) && !zstr(hi->value)) { - char *data = hi->value; - if (globals.encode_values == ENCODING_DEFAULT) { - switch_size_t dlen = strlen(hi->value) * 3; - - if ((data = malloc(dlen))) { - memset(data, 0, dlen); - switch_url_encode(hi->value, data, dlen); - } - } - - variable = json_object_safe_new_string(data); - if (!is_error(variable)) { - json_object_object_add(json, hi->name, variable); - } - - if (data != hi->value) { - switch_safe_free(data); - } - } - } - switch_channel_variable_last(channel); -} - - - -static switch_status_t generate_json_cdr(switch_core_session_t *session, struct json_object **json_cdr) -{ - - struct json_object *cdr = json_object_new_object(); - switch_channel_t *channel = switch_core_session_get_channel(session); - switch_caller_profile_t *caller_profile; - struct json_object *variables, *j_main_cp, *j_caller_profile, *j_caller_extension, *j_times, *time_tag, - *j_application, *j_callflow, *j_inner_extension, *j_apps, *j_o, *j_channel_data, *j_field; - switch_app_log_t *app_log; - char tmp[512], *f; - - if (is_error(cdr)) { - return SWITCH_STATUS_FALSE; - } - - j_channel_data = json_object_new_object(); - if (is_error(j_channel_data)) { - goto error; - } - json_object_object_add(cdr, "channel_data", j_channel_data); - - - j_field = json_object_safe_new_string((char *) switch_channel_state_name(switch_channel_get_state(channel))); - json_object_object_add(j_channel_data, "state", j_field); - - j_field = json_object_safe_new_string(switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound"); - - json_object_object_add(j_channel_data, "direction", j_field); - - - switch_snprintf(tmp, sizeof(tmp), "%d", switch_channel_get_state(channel)); - j_field = json_object_new_string((char *) tmp); - json_object_object_add(j_channel_data, "state_number", j_field); - - - if ((f = switch_channel_get_flag_string(channel))) { - j_field = json_object_safe_new_string((char *) f); - json_object_object_add(j_channel_data, "flags", j_field); - free(f); - } - - if ((f = switch_channel_get_cap_string(channel))) { - j_field = json_object_safe_new_string((char *) f); - json_object_object_add(j_channel_data, "caps", j_field); - free(f); - } - - - variables = json_object_new_object(); - json_object_object_add(cdr, "variables", variables); - - if (is_error(variables)) { - goto error; - } - - set_json_chan_vars(variables, channel); - - - if ((app_log = switch_core_session_get_app_log(session))) { - switch_app_log_t *ap; - - j_apps = json_object_new_object(); - - if (is_error(j_apps)) { - goto error; - } - - json_object_object_add(cdr, "app_log", j_apps); - - for (ap = app_log; ap; ap = ap->next) { - j_application = json_object_new_object(); - - if (is_error(j_application)) { - goto error; - } - - json_object_object_add(j_application, "app_name", json_object_safe_new_string(ap->app)); - json_object_object_add(j_application, "app_data", json_object_safe_new_string(ap->arg)); - - json_object_object_add(j_apps, "application", j_application); - } - } - - - caller_profile = switch_channel_get_caller_profile(channel); - - while (caller_profile) { - - j_callflow = json_object_new_object(); - - if (is_error(j_callflow)) { - goto error; - } - - json_object_object_add(cdr, "callflow", j_callflow); - - if (!zstr(caller_profile->dialplan)) { - json_object_object_add(j_callflow, "dialplan", json_object_safe_new_string((char *)caller_profile->dialplan)); - } - - if (!zstr(caller_profile->profile_index)) { - json_object_object_add(j_callflow, "profile_index", json_object_safe_new_string((char *)caller_profile->profile_index)); - } - - if (caller_profile->caller_extension) { - switch_caller_application_t *ap; - - j_caller_extension = json_object_new_object(); - - if (is_error(j_caller_extension)) { - goto error; - } - - json_object_object_add(j_callflow, "extension", j_caller_extension); - - json_object_object_add(j_caller_extension, "name", json_object_safe_new_string(caller_profile->caller_extension->extension_name)); - json_object_object_add(j_caller_extension, "number", json_object_safe_new_string(caller_profile->caller_extension->extension_number)); - - if (caller_profile->caller_extension->current_application) { - json_object_object_add(j_caller_extension, "current_app", json_object_safe_new_string(caller_profile->caller_extension->current_application->application_name)); - } - - for (ap = caller_profile->caller_extension->applications; ap; ap = ap->next) { - j_application = json_object_new_object(); - - if (is_error(j_application)) { - goto error; - } - - - json_object_object_add(j_caller_extension, "application", j_application); - - if (ap == caller_profile->caller_extension->current_application) { - json_object_object_add(j_application, "last_executed", json_object_new_string("true")); - } - json_object_object_add(j_application, "app_name", json_object_safe_new_string(ap->application_name)); - json_object_object_add(j_application, "app_data", json_object_safe_new_string(switch_str_nil(ap->application_data))); - } - - if (caller_profile->caller_extension->children) { - switch_caller_profile_t *cp = NULL; - for (cp = caller_profile->caller_extension->children; cp; cp = cp->next) { - - if (!cp->caller_extension) { - continue; - } - - j_inner_extension = json_object_new_object(); - if (is_error(j_inner_extension)) { - goto error; - } - - json_object_object_add(j_caller_extension, "sub_extensions", j_inner_extension); - - - j_caller_extension = json_object_new_object(); - if (is_error(j_caller_extension)) { - goto error; - } - - json_object_object_add(j_inner_extension, "extension", j_caller_extension); - - json_object_object_add(j_caller_extension, "name", json_object_safe_new_string(cp->caller_extension->extension_name)); - json_object_object_add(j_caller_extension, "number", json_object_safe_new_string(cp->caller_extension->extension_number)); - - json_object_object_add(j_caller_extension, "dialplan", json_object_safe_new_string((char *)cp->dialplan)); - - if (cp->caller_extension->current_application) { - json_object_object_add(j_caller_extension, "current_app", json_object_safe_new_string(cp->caller_extension->current_application->application_name)); - } - - for (ap = cp->caller_extension->applications; ap; ap = ap->next) { - j_application = json_object_new_object(); - - if (is_error(j_application)) { - goto error; - } - json_object_object_add(j_caller_extension, "application", j_application); - - if (ap == cp->caller_extension->current_application) { - json_object_object_add(j_application, "last_executed", json_object_new_string("true")); - } - json_object_object_add(j_application, "app_name", json_object_safe_new_string(ap->application_name)); - json_object_object_add(j_application, "app_data", json_object_safe_new_string(switch_str_nil(ap->application_data))); - } - } - } - } - - j_main_cp = json_object_new_object(); - if (is_error(j_main_cp)) { - goto error; - } - - json_object_object_add(j_callflow, "caller_profile", j_main_cp); - - set_json_profile_data(j_main_cp, caller_profile); - - if (caller_profile->originator_caller_profile) { - switch_caller_profile_t *cp = NULL; - - j_o = json_object_new_object(); - if (is_error(j_o)) { - goto error; - } - - json_object_object_add(j_main_cp, "originator", j_o); - - for (cp = caller_profile->originator_caller_profile; cp; cp = cp->next) { - j_caller_profile = json_object_new_object(); - if (is_error(j_caller_profile)) { - goto error; - } - - json_object_object_add(j_o, "originator_caller_profile", j_caller_profile); - - set_json_profile_data(j_caller_profile, cp); - } - } - - if (caller_profile->originatee_caller_profile) { - switch_caller_profile_t *cp = NULL; - - j_o = json_object_new_object(); - if (is_error(j_o)) { - goto error; - } - - json_object_object_add(j_main_cp, "originatee", j_o); - - for (cp = caller_profile->originatee_caller_profile; cp; cp = cp->next) { - - j_caller_profile = json_object_new_object(); - if (is_error(j_caller_profile)) { - goto error; - } - - json_object_object_add(j_o, "originatee_caller_profile", j_caller_profile); - set_json_profile_data(j_caller_profile, cp); - } - } - - if (caller_profile->times) { - - j_times = json_object_new_object(); - if (is_error(j_times)) { - goto error; - } - - json_object_object_add(j_callflow, "times", j_times); - - switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->created); - time_tag = json_object_new_string(tmp); - if (is_error(time_tag)) { - goto error; - } - json_object_object_add(j_times, "created_time", time_tag); - - switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->profile_created); - time_tag = json_object_new_string(tmp); - if (is_error(time_tag)) { - goto error; - } - json_object_object_add(j_times, "profile_created_time", time_tag); - - switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->progress); - time_tag = json_object_new_string(tmp); - if (is_error(time_tag)) { - goto error; - } - json_object_object_add(j_times, "progress_time", time_tag); - - switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->progress_media); - time_tag = json_object_new_string(tmp); - if (is_error(time_tag)) { - goto error; - } - json_object_object_add(j_times, "progress_media_time", time_tag); - - - switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->answered); - time_tag = json_object_new_string(tmp); - if (is_error(time_tag)) { - goto error; - } - json_object_object_add(j_times, "answered_time", time_tag); - - switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->hungup); - time_tag = json_object_new_string(tmp); - if (is_error(time_tag)) { - goto error; - } - json_object_object_add(j_times, "hangup_time", time_tag); - - switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->resurrected); - time_tag = json_object_new_string(tmp); - if (is_error(time_tag)) { - goto error; - } - json_object_object_add(j_times, "resurrect_time", time_tag); - - switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->transferred); - time_tag = json_object_new_string(tmp); - if (is_error(time_tag)) { - goto error; - } - json_object_object_add(j_times, "transfer_time", time_tag); - - } - - caller_profile = caller_profile->next; - } - - *json_cdr = cdr; - - return SWITCH_STATUS_SUCCESS; - - error: - - if (cdr) { - json_object_put(cdr); - } - - return SWITCH_STATUS_FALSE; -} - static switch_status_t my_on_reporting(switch_core_session_t *session) { - struct json_object *json_cdr = NULL; - const char *json_text = NULL; + cJSON *json_cdr = NULL; + char *json_text = NULL; char *path = NULL; char *curl_json_text = NULL; const char *logdir = NULL; @@ -637,12 +215,12 @@ static switch_status_t my_on_reporting(switch_core_session_t *session) a_prefix = "a_"; - if (generate_json_cdr(session, &json_cdr) != SWITCH_STATUS_SUCCESS) { + if (switch_ivr_generate_json_cdr(session, &json_cdr, globals.encode_values == ENCODING_DEFAULT) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Generating Data!\n"); return SWITCH_STATUS_FALSE; } - json_text = json_object_to_json_string(json_cdr); + json_text = cJSON_PrintUnformatted(json_cdr); if (!json_text) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n"); @@ -705,6 +283,7 @@ static switch_status_t my_on_reporting(switch_core_session_t *session) switch_b64_encode((unsigned char *) json_text, need_bytes / 3, (unsigned char *) json_text_escaped, need_bytes); } + switch_safe_free(json_text); json_text = json_text_escaped; if (!(curl_json_text = switch_mprintf("cdr=%s", json_text))) { @@ -861,8 +440,8 @@ static switch_status_t my_on_reporting(switch_core_session_t *session) switch_safe_free(curl_json_text); } - json_object_put(json_cdr); - switch_safe_free(json_text_escaped); + cJSON_Delete(json_cdr); + switch_safe_free(json_text); return status; } diff --git a/src/mod/event_handlers/mod_radius_cdr/mod_radius_cdr.c b/src/mod/event_handlers/mod_radius_cdr/mod_radius_cdr.c index 9f3eaf1cb6..77a0438cca 100644 --- a/src/mod/event_handlers/mod_radius_cdr/mod_radius_cdr.c +++ b/src/mod/event_handlers/mod_radius_cdr/mod_radius_cdr.c @@ -213,7 +213,7 @@ static switch_status_t my_on_routing(switch_core_session_t *session) } */ - if ((signal_bond = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) && !zstr(signal_bond)) { + if ((signal_bond = switch_channel_get_partner_uuid(channel)) && !zstr(signal_bond)) { if (rc_avpair_add(rad_config, &send, PW_FS_OTHER_LEG_ID, (void*) signal_bond, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "[mod_radius_cdr] Failed adding Freeswitch-Other-Leg-Id: %s\n", uuid_str); rc_destroy(rad_config); diff --git a/src/mod/languages/mod_managed/freeswitch_wrap.2010.cxx b/src/mod/languages/mod_managed/freeswitch_wrap.2010.cxx index 27cc9fb600..a11d584f49 100644 --- a/src/mod/languages/mod_managed/freeswitch_wrap.2010.cxx +++ b/src/mod/languages/mod_managed/freeswitch_wrap.2010.cxx @@ -1050,6 +1050,16 @@ SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_READ_RESULT_VARIABLE_get() { } +SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_ATT_XFER_RESULT_VARIABLE_get() { + char * jresult ; + char *result = 0 ; + + result = (char *)("att_xfer_result"); + jresult = SWIG_csharp_string_callback((const char *)result); + return jresult; +} + + SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_COPY_XML_CDR_VARIABLE_get() { char * jresult ; char *result = 0 ; @@ -1060,6 +1070,16 @@ SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_COPY_XML_CDR_VARIABLE_get() { } +SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_COPY_JSON_CDR_VARIABLE_get() { + char * jresult ; + char *result = 0 ; + + result = (char *)("copy_json_cdr"); + jresult = SWIG_csharp_string_callback((const char *)result); + return jresult; +} + + SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE_get() { char * jresult ; char *result = 0 ; @@ -1400,6 +1420,16 @@ SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_PROXY_MEDIA_VARIABLE_get() { } +SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_ZRTP_PASSTHRU_VARIABLE_get() { + char * jresult ; + char *result = 0 ; + + result = (char *)("zrtp_passthru"); + jresult = SWIG_csharp_string_callback((const char *)result); + return jresult; +} + + SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_ENDPOINT_DISPOSITION_VARIABLE_get() { char * jresult ; char *result = 0 ; @@ -1520,6 +1550,16 @@ SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_SIGNAL_BOND_VARIABLE_get() { } +SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE_get() { + char * jresult ; + char *result = 0 ; + + result = (char *)("originate_signal_bond"); + jresult = SWIG_csharp_string_callback((const char *)result); + return jresult; +} + + SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_ORIGINATOR_VARIABLE_get() { char * jresult ; char *result = 0 ; @@ -6425,6 +6465,54 @@ SWIGEXPORT void SWIGSTDCALL CSharp_switch_core_session_disable_heartbeat(void * } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_pop(void * jarg1, char * jarg2, void * jarg3) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; + switch_media_bug_t **arg3 = (switch_media_bug_t **) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (char *)jarg2; + arg3 = (switch_media_bug_t **)jarg3; + result = (switch_status_t)switch_core_media_bug_pop(arg1,(char const *)arg2,arg3); + jresult = result; + return jresult; +} + + +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_exec_all(void * jarg1, char * jarg2, void * jarg3, void * jarg4) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; + switch_media_bug_exec_cb_t arg3 = (switch_media_bug_exec_cb_t) 0 ; + void *arg4 = (void *) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (char *)jarg2; + arg3 = (switch_media_bug_exec_cb_t)jarg3; + arg4 = (void *)jarg4; + result = (switch_status_t)switch_core_media_bug_exec_all(arg1,(char const *)arg2,arg3,arg4); + jresult = result; + return jresult; +} + + +SWIGEXPORT unsigned long SWIGSTDCALL CSharp_switch_core_media_bug_count(void * jarg1, char * jarg2) { + unsigned long jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; + uint32_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (char *)jarg2; + result = (uint32_t)switch_core_media_bug_count(arg1,(char const *)arg2); + jresult = (unsigned long)result; + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_add(void * jarg1, char * jarg2, char * jarg3, void * jarg4, void * jarg5, void * jarg6, unsigned long jarg7, void * jarg8) { int jresult ; switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; @@ -6531,6 +6619,16 @@ SWIGEXPORT void * SWIGSTDCALL CSharp_switch_core_media_bug_get_read_replace_fram } +SWIGEXPORT void SWIGSTDCALL CSharp_switch_core_media_bug_set_read_demux_frame(void * jarg1, void * jarg2) { + switch_media_bug_t *arg1 = (switch_media_bug_t *) 0 ; + switch_frame_t *arg2 = (switch_frame_t *) 0 ; + + arg1 = (switch_media_bug_t *)jarg1; + arg2 = (switch_frame_t *)jarg2; + switch_core_media_bug_set_read_demux_frame(arg1,arg2); +} + + SWIGEXPORT void * SWIGSTDCALL CSharp_switch_core_media_bug_get_session(void * jarg1) { void * jresult ; switch_media_bug_t *arg1 = (switch_media_bug_t *) 0 ; @@ -6657,13 +6755,15 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_close(void * jarg1) { } -SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_remove_all(void * jarg1) { +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_remove_all_function(void * jarg1, char * jarg2) { int jresult ; switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; switch_status_t result; arg1 = (switch_core_session_t *)jarg1; - result = (switch_status_t)switch_core_media_bug_remove_all(arg1); + arg2 = (char *)jarg2; + result = (switch_status_t)switch_core_media_bug_remove_all_function(arg1,(char const *)arg2); jresult = result; return jresult; } @@ -8996,6 +9096,20 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_session_get_read_impl(void * jarg1 } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_session_get_real_read_impl(void * jarg1, void * jarg2) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + switch_codec_implementation_t *arg2 = (switch_codec_implementation_t *) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (switch_codec_implementation_t *)jarg2; + result = (switch_status_t)switch_core_session_get_real_read_impl(arg1,arg2); + jresult = result; + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_session_get_write_impl(void * jarg1, void * jarg2) { int jresult ; switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; @@ -11004,7 +11118,7 @@ SWIGEXPORT unsigned long SWIGSTDCALL CSharp_switch_default_ptime(char * jarg1, u } -SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_add_registration(char * jarg1, char * jarg2, char * jarg3, char * jarg4, unsigned long jarg5, char * jarg6, char * jarg7, char * jarg8) { +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_add_registration(char * jarg1, char * jarg2, char * jarg3, char * jarg4, unsigned long jarg5, char * jarg6, char * jarg7, char * jarg8, char * jarg9) { int jresult ; char *arg1 = (char *) 0 ; char *arg2 = (char *) 0 ; @@ -11014,6 +11128,7 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_add_registration(char * jarg1, cha char *arg6 = (char *) 0 ; char *arg7 = (char *) 0 ; char *arg8 = (char *) 0 ; + char *arg9 = (char *) 0 ; switch_status_t result; arg1 = (char *)jarg1; @@ -11024,7 +11139,8 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_add_registration(char * jarg1, cha arg6 = (char *)jarg6; arg7 = (char *)jarg7; arg8 = (char *)jarg8; - result = (switch_status_t)switch_core_add_registration((char const *)arg1,(char const *)arg2,(char const *)arg3,(char const *)arg4,arg5,(char const *)arg6,(char const *)arg7,(char const *)arg8); + arg9 = (char *)jarg9; + result = (switch_status_t)switch_core_add_registration((char const *)arg1,(char const *)arg2,(char const *)arg3,(char const *)arg4,arg5,(char const *)arg6,(char const *)arg7,(char const *)arg8,(char const *)arg9); jresult = result; return jresult; } @@ -11151,6 +11267,23 @@ SWIGEXPORT void SWIGSTDCALL CSharp_switch_close_extra_files(void * jarg1, int ja } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_thread_set_cpu_affinity(int jarg1) { + int jresult ; + int arg1 ; + switch_status_t result; + + arg1 = (int)jarg1; + result = (switch_status_t)switch_core_thread_set_cpu_affinity(arg1); + jresult = result; + return jresult; +} + + +SWIGEXPORT void SWIGSTDCALL CSharp_switch_os_yield() { + switch_os_yield(); +} + + SWIGEXPORT void SWIGSTDCALL CSharp_switch_loadable_module_interface_module_name_set(void * jarg1, char * jarg2) { switch_loadable_module_interface *arg1 = (switch_loadable_module_interface *) 0 ; char *arg2 = (char *) 0 ; @@ -11635,6 +11768,24 @@ SWIGEXPORT void * SWIGSTDCALL CSharp_switch_loadable_module_get_codec_interface( } +SWIGEXPORT char * SWIGSTDCALL CSharp_switch_parse_codec_buf(char * jarg1, void * jarg2, void * jarg3, void * jarg4) { + char * jresult ; + char *arg1 = (char *) 0 ; + uint32_t *arg2 = (uint32_t *) 0 ; + uint32_t *arg3 = (uint32_t *) 0 ; + uint32_t *arg4 = (uint32_t *) 0 ; + char *result = 0 ; + + arg1 = (char *)jarg1; + arg2 = (uint32_t *)jarg2; + arg3 = (uint32_t *)jarg3; + arg4 = (uint32_t *)jarg4; + result = (char *)switch_parse_codec_buf(arg1,arg2,arg3,arg4); + jresult = SWIG_csharp_string_callback((const char *)result); + return jresult; +} + + SWIGEXPORT void * SWIGSTDCALL CSharp_switch_loadable_module_get_dialplan_interface(char * jarg1) { void * jresult ; char *arg1 = (char *) 0 ; @@ -25633,6 +25784,18 @@ SWIGEXPORT void SWIGSTDCALL CSharp_switch_channel_transfer_to_extension(void * j } +SWIGEXPORT char * SWIGSTDCALL CSharp_switch_channel_get_partner_uuid(void * jarg1) { + char * jresult ; + switch_channel_t *arg1 = (switch_channel_t *) 0 ; + char *result = 0 ; + + arg1 = (switch_channel_t *)jarg1; + result = (char *)switch_channel_get_partner_uuid(arg1); + jresult = SWIG_csharp_string_callback((const char *)result); + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_switch_buffer_create(void * jarg1, void * jarg2, void * jarg3) { int jresult ; switch_memory_pool_t *arg1 = (switch_memory_pool_t *) 0 ; @@ -27013,6 +27176,18 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_event_check_permission_list(void * jarg } +SWIGEXPORT void SWIGSTDCALL CSharp_switch_event_add_presence_data_cols(void * jarg1, void * jarg2, char * jarg3) { + switch_channel_t *arg1 = (switch_channel_t *) 0 ; + switch_event_t *arg2 = (switch_event_t *) 0 ; + char *arg3 = (char *) 0 ; + + arg1 = (switch_channel_t *)jarg1; + arg2 = (switch_event_t *)jarg2; + arg3 = (char *)jarg3; + switch_event_add_presence_data_cols(arg1,arg2,(char const *)arg3); +} + + SWIGEXPORT int SWIGSTDCALL CSharp_SWITCH_RESAMPLE_QUALITY_get() { int jresult ; int result; @@ -27403,6 +27578,24 @@ SWIGEXPORT unsigned long SWIGSTDCALL CSharp_switch_merge_sln(void * jarg1, unsig } +SWIGEXPORT unsigned long SWIGSTDCALL CSharp_switch_unmerge_sln(void * jarg1, unsigned long jarg2, void * jarg3, unsigned long jarg4) { + unsigned long jresult ; + int16_t *arg1 = (int16_t *) 0 ; + uint32_t arg2 ; + int16_t *arg3 = (int16_t *) 0 ; + uint32_t arg4 ; + uint32_t result; + + arg1 = (int16_t *)jarg1; + arg2 = (uint32_t)jarg2; + arg3 = (int16_t *)jarg3; + arg4 = (uint32_t)jarg4; + result = (uint32_t)switch_unmerge_sln(arg1,arg2,arg3,arg4); + jresult = (unsigned long)result; + return jresult; +} + + SWIGEXPORT void SWIGSTDCALL CSharp_switch_mux_channels(void * jarg1, void * jarg2, unsigned long jarg3) { int16_t *arg1 = (int16_t *) 0 ; switch_size_t arg2 ; @@ -27847,6 +28040,22 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_activate_unicast(void * jarg1, char } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_generate_json_cdr(void * jarg1, void * jarg2, int jarg3) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + cJSON **arg2 = (cJSON **) 0 ; + switch_bool_t arg3 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (cJSON **)jarg2; + arg3 = (switch_bool_t)jarg3; + result = (switch_status_t)switch_ivr_generate_json_cdr(arg1,arg2,arg3); + jresult = result; + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_generate_xml_cdr(void * jarg1, void * jarg2) { int jresult ; switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; @@ -28257,6 +28466,52 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_record_session(void * jarg1, char * } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_eavesdrop_pop_eavesdropper(void * jarg1, void * jarg2) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + switch_core_session_t **arg2 = (switch_core_session_t **) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (switch_core_session_t **)jarg2; + result = (switch_status_t)switch_ivr_eavesdrop_pop_eavesdropper(arg1,arg2); + jresult = result; + return jresult; +} + + +SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_eavesdrop_exec_all(void * jarg1, char * jarg2, char * jarg3) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; + char *arg3 = (char *) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (char *)jarg2; + arg3 = (char *)jarg3; + result = (switch_status_t)switch_ivr_eavesdrop_exec_all(arg1,(char const *)arg2,(char const *)arg3); + jresult = result; + return jresult; +} + + +SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_eavesdrop_update_display(void * jarg1, char * jarg2, char * jarg3) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; + char *arg3 = (char *) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (char *)jarg2; + arg3 = (char *)jarg3; + result = (switch_status_t)switch_ivr_eavesdrop_update_display(arg1,(char const *)arg2,(char const *)arg3); + jresult = result; + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_eavesdrop_session(void * jarg1, char * jarg2, char * jarg3, unsigned long jarg4) { int jresult ; switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; @@ -29969,6 +30224,20 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_kill_uuid(char * jarg1, int jarg2) } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_blind_transfer_ack(void * jarg1, int jarg2) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + switch_bool_t arg2 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (switch_bool_t)jarg2; + result = (switch_status_t)switch_ivr_blind_transfer_ack(arg1,arg2); + jresult = result; + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_SWITCH_RTP_MAX_BUF_LEN_get() { int jresult ; int result; @@ -31026,6 +31295,16 @@ SWIGEXPORT unsigned char SWIGSTDCALL CSharp_switch_rtp_check_auto_adj(void * jar } +SWIGEXPORT void SWIGSTDCALL CSharp_switch_rtp_set_interdigit_delay(void * jarg1, unsigned long jarg2) { + switch_rtp_t *arg1 = (switch_rtp_t *) 0 ; + uint32_t arg2 ; + + arg1 = (switch_rtp_t *)jarg1; + arg2 = (uint32_t)jarg2; + switch_rtp_set_interdigit_delay(arg1,arg2); +} + + SWIGEXPORT void SWIGSTDCALL CSharp_switch_log_node_t_data_set(void * jarg1, char * jarg2) { switch_log_node_t *arg1 = (switch_log_node_t *) 0 ; char *arg2 = (char *) 0 ; diff --git a/src/mod/languages/mod_managed/freeswitch_wrap.cxx b/src/mod/languages/mod_managed/freeswitch_wrap.cxx index 557cb05f76..33feeaee11 100644 --- a/src/mod/languages/mod_managed/freeswitch_wrap.cxx +++ b/src/mod/languages/mod_managed/freeswitch_wrap.cxx @@ -1113,6 +1113,17 @@ SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_READ_RESULT_VARIABLE_get() { } +SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_ATT_XFER_RESULT_VARIABLE_get() { + char * jresult ; + char *result = 0 ; + + result = (char *) "att_xfer_result"; + + jresult = SWIG_csharp_string_callback((const char *)result); + return jresult; +} + + SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_COPY_XML_CDR_VARIABLE_get() { char * jresult ; char *result = 0 ; @@ -1124,6 +1135,17 @@ SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_COPY_XML_CDR_VARIABLE_get() { } +SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_COPY_JSON_CDR_VARIABLE_get() { + char * jresult ; + char *result = 0 ; + + result = (char *) "copy_json_cdr"; + + jresult = SWIG_csharp_string_callback((const char *)result); + return jresult; +} + + SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE_get() { char * jresult ; char *result = 0 ; @@ -1498,6 +1520,17 @@ SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_PROXY_MEDIA_VARIABLE_get() { } +SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_ZRTP_PASSTHRU_VARIABLE_get() { + char * jresult ; + char *result = 0 ; + + result = (char *) "zrtp_passthru"; + + jresult = SWIG_csharp_string_callback((const char *)result); + return jresult; +} + + SWIGEXPORT char * SWIGSTDCALL CSharp_SWITCH_ENDPOINT_DISPOSITION_VARIABLE_get() { char * jresult ; char *result = 0 ; @@ -6732,6 +6765,54 @@ SWIGEXPORT void SWIGSTDCALL CSharp_switch_core_session_disable_heartbeat(void * } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_pop(void * jarg1, char * jarg2, void * jarg3) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; + switch_media_bug_t **arg3 = (switch_media_bug_t **) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (char *)jarg2; + arg3 = (switch_media_bug_t **)jarg3; + result = (switch_status_t)switch_core_media_bug_pop(arg1,(char const *)arg2,arg3); + jresult = result; + return jresult; +} + + +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_exec_all(void * jarg1, char * jarg2, void * jarg3, void * jarg4) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; + switch_media_bug_exec_cb_t arg3 = (switch_media_bug_exec_cb_t) 0 ; + void *arg4 = (void *) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (char *)jarg2; + arg3 = (switch_media_bug_exec_cb_t)jarg3; + arg4 = (void *)jarg4; + result = (switch_status_t)switch_core_media_bug_exec_all(arg1,(char const *)arg2,arg3,arg4); + jresult = result; + return jresult; +} + + +SWIGEXPORT unsigned long SWIGSTDCALL CSharp_switch_core_media_bug_count(void * jarg1, char * jarg2) { + unsigned long jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; + uint32_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (char *)jarg2; + result = (uint32_t)switch_core_media_bug_count(arg1,(char const *)arg2); + jresult = (unsigned long)result; + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_add(void * jarg1, char * jarg2, char * jarg3, void * jarg4, void * jarg5, void * jarg6, unsigned long jarg7, void * jarg8) { int jresult ; switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; @@ -6838,6 +6919,16 @@ SWIGEXPORT void * SWIGSTDCALL CSharp_switch_core_media_bug_get_read_replace_fram } +SWIGEXPORT void SWIGSTDCALL CSharp_switch_core_media_bug_set_read_demux_frame(void * jarg1, void * jarg2) { + switch_media_bug_t *arg1 = (switch_media_bug_t *) 0 ; + switch_frame_t *arg2 = (switch_frame_t *) 0 ; + + arg1 = (switch_media_bug_t *)jarg1; + arg2 = (switch_frame_t *)jarg2; + switch_core_media_bug_set_read_demux_frame(arg1,arg2); +} + + SWIGEXPORT void * SWIGSTDCALL CSharp_switch_core_media_bug_get_session(void * jarg1) { void * jresult ; switch_media_bug_t *arg1 = (switch_media_bug_t *) 0 ; @@ -6964,13 +7055,15 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_close(void * jarg1) { } -SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_remove_all(void * jarg1) { +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_media_bug_remove_all_function(void * jarg1, char * jarg2) { int jresult ; switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; switch_status_t result; arg1 = (switch_core_session_t *)jarg1; - result = (switch_status_t)switch_core_media_bug_remove_all(arg1); + arg2 = (char *)jarg2; + result = (switch_status_t)switch_core_media_bug_remove_all_function(arg1,(char const *)arg2); jresult = result; return jresult; } @@ -9303,6 +9396,20 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_session_get_read_impl(void * jarg1 } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_session_get_real_read_impl(void * jarg1, void * jarg2) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + switch_codec_implementation_t *arg2 = (switch_codec_implementation_t *) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (switch_codec_implementation_t *)jarg2; + result = (switch_status_t)switch_core_session_get_real_read_impl(arg1,arg2); + jresult = result; + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_session_get_write_impl(void * jarg1, void * jarg2) { int jresult ; switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; @@ -11467,6 +11574,23 @@ SWIGEXPORT void SWIGSTDCALL CSharp_switch_close_extra_files(void * jarg1, int ja } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_core_thread_set_cpu_affinity(int jarg1) { + int jresult ; + int arg1 ; + switch_status_t result; + + arg1 = (int)jarg1; + result = (switch_status_t)switch_core_thread_set_cpu_affinity(arg1); + jresult = result; + return jresult; +} + + +SWIGEXPORT void SWIGSTDCALL CSharp_switch_os_yield() { + switch_os_yield(); +} + + SWIGEXPORT void SWIGSTDCALL CSharp_switch_loadable_module_interface_module_name_set(void * jarg1, char * jarg2) { switch_loadable_module_interface *arg1 = (switch_loadable_module_interface *) 0 ; char *arg2 = (char *) 0 ; @@ -11970,6 +12094,24 @@ SWIGEXPORT void * SWIGSTDCALL CSharp_switch_loadable_module_get_codec_interface( } +SWIGEXPORT char * SWIGSTDCALL CSharp_switch_parse_codec_buf(char * jarg1, void * jarg2, void * jarg3, void * jarg4) { + char * jresult ; + char *arg1 = (char *) 0 ; + uint32_t *arg2 = (uint32_t *) 0 ; + uint32_t *arg3 = (uint32_t *) 0 ; + uint32_t *arg4 = (uint32_t *) 0 ; + char *result = 0 ; + + arg1 = (char *)jarg1; + arg2 = (uint32_t *)jarg2; + arg3 = (uint32_t *)jarg3; + arg4 = (uint32_t *)jarg4; + result = (char *)switch_parse_codec_buf(arg1,arg2,arg3,arg4); + jresult = SWIG_csharp_string_callback((const char *)result); + return jresult; +} + + SWIGEXPORT void * SWIGSTDCALL CSharp_switch_loadable_module_get_dialplan_interface(char * jarg1) { void * jresult ; char *arg1 = (char *) 0 ; @@ -27723,6 +27865,18 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_event_check_permission_list(void * jarg } +SWIGEXPORT void SWIGSTDCALL CSharp_switch_event_add_presence_data_cols(void * jarg1, void * jarg2, char * jarg3) { + switch_channel_t *arg1 = (switch_channel_t *) 0 ; + switch_event_t *arg2 = (switch_event_t *) 0 ; + char *arg3 = (char *) 0 ; + + arg1 = (switch_channel_t *)jarg1; + arg2 = (switch_event_t *)jarg2; + arg3 = (char *)jarg3; + switch_event_add_presence_data_cols(arg1,arg2,(char const *)arg3); +} + + SWIGEXPORT int SWIGSTDCALL CSharp_SWITCH_RESAMPLE_QUALITY_get() { int jresult ; int result; @@ -28123,6 +28277,24 @@ SWIGEXPORT unsigned long SWIGSTDCALL CSharp_switch_merge_sln(void * jarg1, unsig } +SWIGEXPORT unsigned long SWIGSTDCALL CSharp_switch_unmerge_sln(void * jarg1, unsigned long jarg2, void * jarg3, unsigned long jarg4) { + unsigned long jresult ; + int16_t *arg1 = (int16_t *) 0 ; + uint32_t arg2 ; + int16_t *arg3 = (int16_t *) 0 ; + uint32_t arg4 ; + uint32_t result; + + arg1 = (int16_t *)jarg1; + arg2 = (uint32_t)jarg2; + arg3 = (int16_t *)jarg3; + arg4 = (uint32_t)jarg4; + result = (uint32_t)switch_unmerge_sln(arg1,arg2,arg3,arg4); + jresult = (unsigned long)result; + return jresult; +} + + SWIGEXPORT void SWIGSTDCALL CSharp_switch_mux_channels(void * jarg1, void * jarg2, unsigned long jarg3) { int16_t *arg1 = (int16_t *) 0 ; switch_size_t arg2 ; @@ -28581,6 +28753,22 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_activate_unicast(void * jarg1, char } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_generate_json_cdr(void * jarg1, void * jarg2, int jarg3) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + cJSON **arg2 = (cJSON **) 0 ; + switch_bool_t arg3 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (cJSON **)jarg2; + arg3 = (switch_bool_t)jarg3; + result = (switch_status_t)switch_ivr_generate_json_cdr(arg1,arg2,arg3); + jresult = result; + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_generate_xml_cdr(void * jarg1, void * jarg2) { int jresult ; switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; @@ -28991,6 +29179,52 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_record_session(void * jarg1, char * } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_eavesdrop_pop_eavesdropper(void * jarg1, void * jarg2) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + switch_core_session_t **arg2 = (switch_core_session_t **) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (switch_core_session_t **)jarg2; + result = (switch_status_t)switch_ivr_eavesdrop_pop_eavesdropper(arg1,arg2); + jresult = result; + return jresult; +} + + +SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_eavesdrop_exec_all(void * jarg1, char * jarg2, char * jarg3) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; + char *arg3 = (char *) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (char *)jarg2; + arg3 = (char *)jarg3; + result = (switch_status_t)switch_ivr_eavesdrop_exec_all(arg1,(char const *)arg2,(char const *)arg3); + jresult = result; + return jresult; +} + + +SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_eavesdrop_update_display(void * jarg1, char * jarg2, char * jarg3) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + char *arg2 = (char *) 0 ; + char *arg3 = (char *) 0 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (char *)jarg2; + arg3 = (char *)jarg3; + result = (switch_status_t)switch_ivr_eavesdrop_update_display(arg1,(char const *)arg2,(char const *)arg3); + jresult = result; + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_eavesdrop_session(void * jarg1, char * jarg2, char * jarg3, unsigned long jarg4) { int jresult ; switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; @@ -30703,6 +30937,20 @@ SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_kill_uuid(char * jarg1, int jarg2) } +SWIGEXPORT int SWIGSTDCALL CSharp_switch_ivr_blind_transfer_ack(void * jarg1, int jarg2) { + int jresult ; + switch_core_session_t *arg1 = (switch_core_session_t *) 0 ; + switch_bool_t arg2 ; + switch_status_t result; + + arg1 = (switch_core_session_t *)jarg1; + arg2 = (switch_bool_t)jarg2; + result = (switch_status_t)switch_ivr_blind_transfer_ack(arg1,arg2); + jresult = result; + return jresult; +} + + SWIGEXPORT int SWIGSTDCALL CSharp_SWITCH_RTP_MAX_BUF_LEN_get() { int jresult ; int result; @@ -31771,6 +32019,16 @@ SWIGEXPORT unsigned char SWIGSTDCALL CSharp_switch_rtp_check_auto_adj(void * jar } +SWIGEXPORT void SWIGSTDCALL CSharp_switch_rtp_set_interdigit_delay(void * jarg1, unsigned long jarg2) { + switch_rtp_t *arg1 = (switch_rtp_t *) 0 ; + uint32_t arg2 ; + + arg1 = (switch_rtp_t *)jarg1; + arg2 = (uint32_t)jarg2; + switch_rtp_set_interdigit_delay(arg1,arg2); +} + + SWIGEXPORT void SWIGSTDCALL CSharp_switch_log_node_t_data_set(void * jarg1, char * jarg2) { switch_log_node_t *arg1 = (switch_log_node_t *) 0 ; char *arg2 = (char *) 0 ; diff --git a/src/mod/languages/mod_managed/managed/swig.2010.cs b/src/mod/languages/mod_managed/managed/swig.2010.cs index d814ce15b9..3a1cc13d6b 100644 --- a/src/mod/languages/mod_managed/managed/swig.2010.cs +++ b/src/mod/languages/mod_managed/managed/swig.2010.cs @@ -1066,6 +1066,21 @@ public class freeswitch { freeswitchPINVOKE.switch_core_session_disable_heartbeat(SWIGTYPE_p_switch_core_session.getCPtr(session)); } + public static switch_status_t switch_core_media_bug_pop(SWIGTYPE_p_switch_core_session orig_session, string function, SWIGTYPE_p_p_switch_media_bug pop) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_media_bug_pop(SWIGTYPE_p_switch_core_session.getCPtr(orig_session), function, SWIGTYPE_p_p_switch_media_bug.getCPtr(pop)); + return ret; + } + + public static switch_status_t switch_core_media_bug_exec_all(SWIGTYPE_p_switch_core_session orig_session, string function, SWIGTYPE_p_f_p_switch_media_bug_p_void__void cb, SWIGTYPE_p_void user_data) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_media_bug_exec_all(SWIGTYPE_p_switch_core_session.getCPtr(orig_session), function, SWIGTYPE_p_f_p_switch_media_bug_p_void__void.getCPtr(cb), SWIGTYPE_p_void.getCPtr(user_data)); + return ret; + } + + public static uint switch_core_media_bug_count(SWIGTYPE_p_switch_core_session orig_session, string function) { + uint ret = freeswitchPINVOKE.switch_core_media_bug_count(SWIGTYPE_p_switch_core_session.getCPtr(orig_session), function); + return ret; + } + public static switch_status_t switch_core_media_bug_add(SWIGTYPE_p_switch_core_session session, string function, string target, SWIGTYPE_p_f_p_switch_media_bug_p_void_enum_switch_abc_type_t__switch_bool_t callback, SWIGTYPE_p_void user_data, SWIGTYPE_p_time_t stop_time, uint flags, SWIGTYPE_p_p_switch_media_bug new_bug) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_media_bug_add(SWIGTYPE_p_switch_core_session.getCPtr(session), function, target, SWIGTYPE_p_f_p_switch_media_bug_p_void_enum_switch_abc_type_t__switch_bool_t.getCPtr(callback), SWIGTYPE_p_void.getCPtr(user_data), SWIGTYPE_p_time_t.getCPtr(stop_time), flags, SWIGTYPE_p_p_switch_media_bug.getCPtr(new_bug)); if (freeswitchPINVOKE.SWIGPendingException.Pending) throw freeswitchPINVOKE.SWIGPendingException.Retrieve(); @@ -1106,6 +1121,10 @@ public class freeswitch { return ret; } + public static void switch_core_media_bug_set_read_demux_frame(SWIGTYPE_p_switch_media_bug bug, switch_frame frame) { + freeswitchPINVOKE.switch_core_media_bug_set_read_demux_frame(SWIGTYPE_p_switch_media_bug.getCPtr(bug), switch_frame.getCPtr(frame)); + } + public static SWIGTYPE_p_switch_core_session switch_core_media_bug_get_session(SWIGTYPE_p_switch_media_bug bug) { IntPtr cPtr = freeswitchPINVOKE.switch_core_media_bug_get_session(SWIGTYPE_p_switch_media_bug.getCPtr(bug)); SWIGTYPE_p_switch_core_session ret = (cPtr == IntPtr.Zero) ? null : new SWIGTYPE_p_switch_core_session(cPtr, false); @@ -1156,8 +1175,8 @@ public class freeswitch { return ret; } - public static switch_status_t switch_core_media_bug_remove_all(SWIGTYPE_p_switch_core_session session) { - switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_media_bug_remove_all(SWIGTYPE_p_switch_core_session.getCPtr(session)); + public static switch_status_t switch_core_media_bug_remove_all_function(SWIGTYPE_p_switch_core_session session, string function) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_media_bug_remove_all_function(SWIGTYPE_p_switch_core_session.getCPtr(session), function); return ret; } @@ -2000,6 +2019,11 @@ public class freeswitch { return ret; } + public static switch_status_t switch_core_session_get_real_read_impl(SWIGTYPE_p_switch_core_session session, switch_codec_implementation impp) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_session_get_real_read_impl(SWIGTYPE_p_switch_core_session.getCPtr(session), switch_codec_implementation.getCPtr(impp)); + return ret; + } + public static switch_status_t switch_core_session_get_write_impl(SWIGTYPE_p_switch_core_session session, switch_codec_implementation impp) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_session_get_write_impl(SWIGTYPE_p_switch_core_session.getCPtr(session), switch_codec_implementation.getCPtr(impp)); return ret; @@ -2647,8 +2671,8 @@ public class freeswitch { return ret; } - public static switch_status_t switch_core_add_registration(string user, string realm, string token, string url, uint expires, string network_ip, string network_port, string network_proto) { - switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_add_registration(user, realm, token, url, expires, network_ip, network_port, network_proto); + public static switch_status_t switch_core_add_registration(string user, string realm, string token, string url, uint expires, string network_ip, string network_port, string network_proto, string metadata) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_add_registration(user, realm, token, url, expires, network_ip, network_port, network_proto, metadata); return ret; } @@ -2699,6 +2723,15 @@ public class freeswitch { freeswitchPINVOKE.switch_close_extra_files(SWIGTYPE_p_int.getCPtr(keep), keep_ttl); } + public static switch_status_t switch_core_thread_set_cpu_affinity(int cpu) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_thread_set_cpu_affinity(cpu); + return ret; + } + + public static void switch_os_yield() { + freeswitchPINVOKE.switch_os_yield(); + } + public static switch_status_t switch_loadable_module_init(switch_bool_t autoload) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_loadable_module_init((int)autoload); return ret; @@ -2720,6 +2753,11 @@ public class freeswitch { return ret; } + public static string switch_parse_codec_buf(string buf, SWIGTYPE_p_unsigned_long interval, SWIGTYPE_p_unsigned_long rate, SWIGTYPE_p_unsigned_long bit) { + string ret = freeswitchPINVOKE.switch_parse_codec_buf(buf, SWIGTYPE_p_unsigned_long.getCPtr(interval), SWIGTYPE_p_unsigned_long.getCPtr(rate), SWIGTYPE_p_unsigned_long.getCPtr(bit)); + return ret; + } + public static switch_dialplan_interface switch_loadable_module_get_dialplan_interface(string name) { IntPtr cPtr = freeswitchPINVOKE.switch_loadable_module_get_dialplan_interface(name); switch_dialplan_interface ret = (cPtr == IntPtr.Zero) ? null : new switch_dialplan_interface(cPtr, false); @@ -4080,6 +4118,11 @@ public class freeswitch { freeswitchPINVOKE.switch_channel_transfer_to_extension(SWIGTYPE_p_switch_channel.getCPtr(channel), switch_caller_extension.getCPtr(caller_extension)); } + public static string switch_channel_get_partner_uuid(SWIGTYPE_p_switch_channel channel) { + string ret = freeswitchPINVOKE.switch_channel_get_partner_uuid(SWIGTYPE_p_switch_channel.getCPtr(channel)); + return ret; + } + public static switch_status_t switch_buffer_create(SWIGTYPE_p_apr_pool_t pool, SWIGTYPE_p_p_switch_buffer buffer, SWIGTYPE_p_switch_size_t max_len) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_buffer_create(SWIGTYPE_p_apr_pool_t.getCPtr(pool), SWIGTYPE_p_p_switch_buffer.getCPtr(buffer), SWIGTYPE_p_switch_size_t.getCPtr(max_len)); if (freeswitchPINVOKE.SWIGPendingException.Pending) throw freeswitchPINVOKE.SWIGPendingException.Retrieve(); @@ -4370,6 +4413,10 @@ public class freeswitch { return ret; } + public static void switch_event_add_presence_data_cols(SWIGTYPE_p_switch_channel channel, switch_event arg1, string prefix) { + freeswitchPINVOKE.switch_event_add_presence_data_cols(SWIGTYPE_p_switch_channel.getCPtr(channel), switch_event.getCPtr(arg1), prefix); + } + public static switch_status_t switch_resample_perform_create(SWIGTYPE_p_p_switch_audio_resampler_t new_resampler, uint from_rate, uint to_rate, uint to_size, int quality, uint channels, string file, string func, int line) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_resample_perform_create(SWIGTYPE_p_p_switch_audio_resampler_t.getCPtr(new_resampler), from_rate, to_rate, to_size, quality, channels, file, func, line); return ret; @@ -4426,6 +4473,11 @@ public class freeswitch { return ret; } + public static uint switch_unmerge_sln(SWIGTYPE_p_short data, uint samples, SWIGTYPE_p_short other_data, uint other_samples) { + uint ret = freeswitchPINVOKE.switch_unmerge_sln(SWIGTYPE_p_short.getCPtr(data), samples, SWIGTYPE_p_short.getCPtr(other_data), other_samples); + return ret; + } + public static void switch_mux_channels(SWIGTYPE_p_short data, SWIGTYPE_p_switch_size_t samples, uint channels) { freeswitchPINVOKE.switch_mux_channels(SWIGTYPE_p_short.getCPtr(data), SWIGTYPE_p_switch_size_t.getCPtr(samples), channels); if (freeswitchPINVOKE.SWIGPendingException.Pending) throw freeswitchPINVOKE.SWIGPendingException.Retrieve(); @@ -4441,6 +4493,11 @@ public class freeswitch { return ret; } + public static switch_status_t switch_ivr_generate_json_cdr(SWIGTYPE_p_switch_core_session session, SWIGTYPE_p_p_cJSON json_cdr, switch_bool_t urlencode) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_generate_json_cdr(SWIGTYPE_p_switch_core_session.getCPtr(session), SWIGTYPE_p_p_cJSON.getCPtr(json_cdr), (int)urlencode); + return ret; + } + public static switch_status_t switch_ivr_generate_xml_cdr(SWIGTYPE_p_switch_core_session session, SWIGTYPE_p_p_switch_xml xml_cdr) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_generate_xml_cdr(SWIGTYPE_p_switch_core_session.getCPtr(session), SWIGTYPE_p_p_switch_xml.getCPtr(xml_cdr)); return ret; @@ -4572,6 +4629,21 @@ public class freeswitch { return ret; } + public static switch_status_t switch_ivr_eavesdrop_pop_eavesdropper(SWIGTYPE_p_switch_core_session session, SWIGTYPE_p_p_switch_core_session sessionp) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_eavesdrop_pop_eavesdropper(SWIGTYPE_p_switch_core_session.getCPtr(session), SWIGTYPE_p_p_switch_core_session.getCPtr(sessionp)); + return ret; + } + + public static switch_status_t switch_ivr_eavesdrop_exec_all(SWIGTYPE_p_switch_core_session session, string app, string arg) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_eavesdrop_exec_all(SWIGTYPE_p_switch_core_session.getCPtr(session), app, arg); + return ret; + } + + public static switch_status_t switch_ivr_eavesdrop_update_display(SWIGTYPE_p_switch_core_session session, string name, string number) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_eavesdrop_update_display(SWIGTYPE_p_switch_core_session.getCPtr(session), name, number); + return ret; + } + public static switch_status_t switch_ivr_eavesdrop_session(SWIGTYPE_p_switch_core_session session, string uuid, string require_group, uint flags) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_eavesdrop_session(SWIGTYPE_p_switch_core_session.getCPtr(session), uuid, require_group, flags); return ret; @@ -5088,6 +5160,11 @@ public class freeswitch { return ret; } + public static switch_status_t switch_ivr_blind_transfer_ack(SWIGTYPE_p_switch_core_session session, switch_bool_t success) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_blind_transfer_ack(SWIGTYPE_p_switch_core_session.getCPtr(session), (int)success); + return ret; + } + public static switch_status_t switch_rtp_add_crypto_key(SWIGTYPE_p_switch_rtp rtp_session, switch_rtp_crypto_direction_t direction, uint index, switch_rtp_crypto_key_type_t type, SWIGTYPE_p_unsigned_char key, SWIGTYPE_p_switch_size_t keylen) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_rtp_add_crypto_key(SWIGTYPE_p_switch_rtp.getCPtr(rtp_session), (int)direction, index, (int)type, SWIGTYPE_p_unsigned_char.getCPtr(key), SWIGTYPE_p_switch_size_t.getCPtr(keylen)); if (freeswitchPINVOKE.SWIGPendingException.Pending) throw freeswitchPINVOKE.SWIGPendingException.Retrieve(); @@ -5387,6 +5464,10 @@ public class freeswitch { return ret; } + public static void switch_rtp_set_interdigit_delay(SWIGTYPE_p_switch_rtp rtp_session, uint delay) { + freeswitchPINVOKE.switch_rtp_set_interdigit_delay(SWIGTYPE_p_switch_rtp.getCPtr(rtp_session), delay); + } + public static switch_status_t switch_log_init(SWIGTYPE_p_apr_pool_t pool, switch_bool_t colorize) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_log_init(SWIGTYPE_p_apr_pool_t.getCPtr(pool), (int)colorize); return ret; @@ -6004,7 +6085,9 @@ public class freeswitch { public static readonly string SWITCH_ENABLE_HEARTBEAT_EVENTS_VARIABLE = freeswitchPINVOKE.SWITCH_ENABLE_HEARTBEAT_EVENTS_VARIABLE_get(); public static readonly string SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE = freeswitchPINVOKE.SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE_get(); public static readonly string SWITCH_READ_RESULT_VARIABLE = freeswitchPINVOKE.SWITCH_READ_RESULT_VARIABLE_get(); + public static readonly string SWITCH_ATT_XFER_RESULT_VARIABLE = freeswitchPINVOKE.SWITCH_ATT_XFER_RESULT_VARIABLE_get(); public static readonly string SWITCH_COPY_XML_CDR_VARIABLE = freeswitchPINVOKE.SWITCH_COPY_XML_CDR_VARIABLE_get(); + public static readonly string SWITCH_COPY_JSON_CDR_VARIABLE = freeswitchPINVOKE.SWITCH_COPY_JSON_CDR_VARIABLE_get(); public static readonly string SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE = freeswitchPINVOKE.SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE_get(); public static readonly string SWITCH_TRANSFER_HISTORY_VARIABLE = freeswitchPINVOKE.SWITCH_TRANSFER_HISTORY_VARIABLE_get(); public static readonly string SWITCH_TRANSFER_SOURCE_VARIABLE = freeswitchPINVOKE.SWITCH_TRANSFER_SOURCE_VARIABLE_get(); @@ -6039,6 +6122,7 @@ public class freeswitch { public static readonly string SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME = freeswitchPINVOKE.SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME_get(); public static readonly string SWITCH_BYPASS_MEDIA_VARIABLE = freeswitchPINVOKE.SWITCH_BYPASS_MEDIA_VARIABLE_get(); public static readonly string SWITCH_PROXY_MEDIA_VARIABLE = freeswitchPINVOKE.SWITCH_PROXY_MEDIA_VARIABLE_get(); + public static readonly string SWITCH_ZRTP_PASSTHRU_VARIABLE = freeswitchPINVOKE.SWITCH_ZRTP_PASSTHRU_VARIABLE_get(); public static readonly string SWITCH_ENDPOINT_DISPOSITION_VARIABLE = freeswitchPINVOKE.SWITCH_ENDPOINT_DISPOSITION_VARIABLE_get(); public static readonly string SWITCH_HOLD_MUSIC_VARIABLE = freeswitchPINVOKE.SWITCH_HOLD_MUSIC_VARIABLE_get(); public static readonly string SWITCH_TEMP_HOLD_MUSIC_VARIABLE = freeswitchPINVOKE.SWITCH_TEMP_HOLD_MUSIC_VARIABLE_get(); @@ -6051,6 +6135,7 @@ public class freeswitch { public static readonly string SWITCH_LAST_BRIDGE_VARIABLE = freeswitchPINVOKE.SWITCH_LAST_BRIDGE_VARIABLE_get(); public static readonly string SWITCH_SIGNAL_BRIDGE_VARIABLE = freeswitchPINVOKE.SWITCH_SIGNAL_BRIDGE_VARIABLE_get(); public static readonly string SWITCH_SIGNAL_BOND_VARIABLE = freeswitchPINVOKE.SWITCH_SIGNAL_BOND_VARIABLE_get(); + public static readonly string SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE = freeswitchPINVOKE.SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE_get(); public static readonly string SWITCH_ORIGINATOR_VARIABLE = freeswitchPINVOKE.SWITCH_ORIGINATOR_VARIABLE_get(); public static readonly string SWITCH_ORIGINATOR_CODEC_VARIABLE = freeswitchPINVOKE.SWITCH_ORIGINATOR_CODEC_VARIABLE_get(); public static readonly string SWITCH_ORIGINATOR_VIDEO_CODEC_VARIABLE = freeswitchPINVOKE.SWITCH_ORIGINATOR_VIDEO_CODEC_VARIABLE_get(); @@ -6551,9 +6636,15 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_READ_RESULT_VARIABLE_get")] public static extern string SWITCH_READ_RESULT_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_ATT_XFER_RESULT_VARIABLE_get")] + public static extern string SWITCH_ATT_XFER_RESULT_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_COPY_XML_CDR_VARIABLE_get")] public static extern string SWITCH_COPY_XML_CDR_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_COPY_JSON_CDR_VARIABLE_get")] + public static extern string SWITCH_COPY_JSON_CDR_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE_get")] public static extern string SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE_get(); @@ -6656,6 +6747,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_PROXY_MEDIA_VARIABLE_get")] public static extern string SWITCH_PROXY_MEDIA_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_ZRTP_PASSTHRU_VARIABLE_get")] + public static extern string SWITCH_ZRTP_PASSTHRU_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_ENDPOINT_DISPOSITION_VARIABLE_get")] public static extern string SWITCH_ENDPOINT_DISPOSITION_VARIABLE_get(); @@ -6692,6 +6786,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_SIGNAL_BOND_VARIABLE_get")] public static extern string SWITCH_SIGNAL_BOND_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE_get")] + public static extern string SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_ORIGINATOR_VARIABLE_get")] public static extern string SWITCH_ORIGINATOR_VARIABLE_get(); @@ -7952,6 +8049,15 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_core_session_disable_heartbeat")] public static extern void switch_core_session_disable_heartbeat(HandleRef jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_pop")] + public static extern int switch_core_media_bug_pop(HandleRef jarg1, string jarg2, HandleRef jarg3); + + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_exec_all")] + public static extern int switch_core_media_bug_exec_all(HandleRef jarg1, string jarg2, HandleRef jarg3, HandleRef jarg4); + + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_count")] + public static extern uint switch_core_media_bug_count(HandleRef jarg1, string jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_add")] public static extern int switch_core_media_bug_add(HandleRef jarg1, string jarg2, string jarg3, HandleRef jarg4, HandleRef jarg5, HandleRef jarg6, uint jarg7, HandleRef jarg8); @@ -7976,6 +8082,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_get_read_replace_frame")] public static extern IntPtr switch_core_media_bug_get_read_replace_frame(HandleRef jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_set_read_demux_frame")] + public static extern void switch_core_media_bug_set_read_demux_frame(HandleRef jarg1, HandleRef jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_get_session")] public static extern IntPtr switch_core_media_bug_get_session(HandleRef jarg1); @@ -8006,8 +8115,8 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_close")] public static extern int switch_core_media_bug_close(HandleRef jarg1); - [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_remove_all")] - public static extern int switch_core_media_bug_remove_all(HandleRef jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_remove_all_function")] + public static extern int switch_core_media_bug_remove_all_function(HandleRef jarg1, string jarg2); [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_enumerate")] public static extern int switch_core_media_bug_enumerate(HandleRef jarg1, HandleRef jarg2); @@ -8519,6 +8628,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_core_session_get_read_impl")] public static extern int switch_core_session_get_read_impl(HandleRef jarg1, HandleRef jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_session_get_real_read_impl")] + public static extern int switch_core_session_get_real_read_impl(HandleRef jarg1, HandleRef jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_session_get_write_impl")] public static extern int switch_core_session_get_write_impl(HandleRef jarg1, HandleRef jarg2); @@ -8991,7 +9103,7 @@ class freeswitchPINVOKE { public static extern uint switch_default_ptime(string jarg1, uint jarg2); [DllImport("mod_managed", EntryPoint="CSharp_switch_core_add_registration")] - public static extern int switch_core_add_registration(string jarg1, string jarg2, string jarg3, string jarg4, uint jarg5, string jarg6, string jarg7, string jarg8); + public static extern int switch_core_add_registration(string jarg1, string jarg2, string jarg3, string jarg4, uint jarg5, string jarg6, string jarg7, string jarg8, string jarg9); [DllImport("mod_managed", EntryPoint="CSharp_switch_core_del_registration")] public static extern int switch_core_del_registration(string jarg1, string jarg2, string jarg3); @@ -9023,6 +9135,12 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_close_extra_files")] public static extern void switch_close_extra_files(HandleRef jarg1, int jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_thread_set_cpu_affinity")] + public static extern int switch_core_thread_set_cpu_affinity(int jarg1); + + [DllImport("mod_managed", EntryPoint="CSharp_switch_os_yield")] + public static extern void switch_os_yield(); + [DllImport("mod_managed", EntryPoint="CSharp_switch_loadable_module_interface_module_name_set")] public static extern void switch_loadable_module_interface_module_name_set(HandleRef jarg1, string jarg2); @@ -9155,6 +9273,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_loadable_module_get_codec_interface")] public static extern IntPtr switch_loadable_module_get_codec_interface(string jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_parse_codec_buf")] + public static extern string switch_parse_codec_buf(string jarg1, HandleRef jarg2, HandleRef jarg3, HandleRef jarg4); + [DllImport("mod_managed", EntryPoint="CSharp_switch_loadable_module_get_dialplan_interface")] public static extern IntPtr switch_loadable_module_get_dialplan_interface(string jarg1); @@ -12593,6 +12714,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_channel_transfer_to_extension")] public static extern void switch_channel_transfer_to_extension(HandleRef jarg1, HandleRef jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_channel_get_partner_uuid")] + public static extern string switch_channel_get_partner_uuid(HandleRef jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_buffer_create")] public static extern int switch_buffer_create(HandleRef jarg1, HandleRef jarg2, HandleRef jarg3); @@ -12887,6 +13011,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_event_check_permission_list")] public static extern int switch_event_check_permission_list(HandleRef jarg1, string jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_event_add_presence_data_cols")] + public static extern void switch_event_add_presence_data_cols(HandleRef jarg1, HandleRef jarg2, string jarg3); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_RESAMPLE_QUALITY_get")] public static extern int SWITCH_RESAMPLE_QUALITY_get(); @@ -12980,6 +13107,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_merge_sln")] public static extern uint switch_merge_sln(HandleRef jarg1, uint jarg2, HandleRef jarg3, uint jarg4); + [DllImport("mod_managed", EntryPoint="CSharp_switch_unmerge_sln")] + public static extern uint switch_unmerge_sln(HandleRef jarg1, uint jarg2, HandleRef jarg3, uint jarg4); + [DllImport("mod_managed", EntryPoint="CSharp_switch_mux_channels")] public static extern void switch_mux_channels(HandleRef jarg1, HandleRef jarg2, uint jarg3); @@ -13091,6 +13221,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_activate_unicast")] public static extern int switch_ivr_activate_unicast(HandleRef jarg1, string jarg2, ushort jarg3, string jarg4, ushort jarg5, string jarg6, string jarg7); + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_generate_json_cdr")] + public static extern int switch_ivr_generate_json_cdr(HandleRef jarg1, HandleRef jarg2, int jarg3); + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_generate_xml_cdr")] public static extern int switch_ivr_generate_xml_cdr(HandleRef jarg1, HandleRef jarg2); @@ -13169,6 +13302,15 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_record_session")] public static extern int switch_ivr_record_session(HandleRef jarg1, string jarg2, uint jarg3, HandleRef jarg4); + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_eavesdrop_pop_eavesdropper")] + public static extern int switch_ivr_eavesdrop_pop_eavesdropper(HandleRef jarg1, HandleRef jarg2); + + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_eavesdrop_exec_all")] + public static extern int switch_ivr_eavesdrop_exec_all(HandleRef jarg1, string jarg2, string jarg3); + + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_eavesdrop_update_display")] + public static extern int switch_ivr_eavesdrop_update_display(HandleRef jarg1, string jarg2, string jarg3); + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_eavesdrop_session")] public static extern int switch_ivr_eavesdrop_session(HandleRef jarg1, string jarg2, string jarg3, uint jarg4); @@ -13481,6 +13623,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_kill_uuid")] public static extern int switch_ivr_kill_uuid(string jarg1, int jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_blind_transfer_ack")] + public static extern int switch_ivr_blind_transfer_ack(HandleRef jarg1, int jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_RTP_MAX_BUF_LEN_get")] public static extern int SWITCH_RTP_MAX_BUF_LEN_get(); @@ -13724,6 +13869,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_rtp_check_auto_adj")] public static extern byte switch_rtp_check_auto_adj(HandleRef jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_rtp_set_interdigit_delay")] + public static extern void switch_rtp_set_interdigit_delay(HandleRef jarg1, uint jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_log_node_t_data_set")] public static extern void switch_log_node_t_data_set(HandleRef jarg1, string jarg2); @@ -17420,6 +17568,36 @@ namespace FreeSWITCH.Native { using System; using System.Runtime.InteropServices; +public class SWIGTYPE_p_f_p_switch_media_bug_p_void__void { + private HandleRef swigCPtr; + + internal SWIGTYPE_p_f_p_switch_media_bug_p_void__void(IntPtr cPtr, bool futureUse) { + swigCPtr = new HandleRef(this, cPtr); + } + + protected SWIGTYPE_p_f_p_switch_media_bug_p_void__void() { + swigCPtr = new HandleRef(null, IntPtr.Zero); + } + + internal static HandleRef getCPtr(SWIGTYPE_p_f_p_switch_media_bug_p_void__void obj) { + return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr; + } +} + +} +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 2.0.1 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +namespace FreeSWITCH.Native { + +using System; +using System.Runtime.InteropServices; + public class SWIGTYPE_p_f_p_switch_rtp_p_switch_socket_t_p_void_switch_size_t_p_switch_sockaddr_t__void { private HandleRef swigCPtr; @@ -18200,6 +18378,36 @@ namespace FreeSWITCH.Native { using System; using System.Runtime.InteropServices; +public class SWIGTYPE_p_p_cJSON { + private HandleRef swigCPtr; + + internal SWIGTYPE_p_p_cJSON(IntPtr cPtr, bool futureUse) { + swigCPtr = new HandleRef(this, cPtr); + } + + protected SWIGTYPE_p_p_cJSON() { + swigCPtr = new HandleRef(null, IntPtr.Zero); + } + + internal static HandleRef getCPtr(SWIGTYPE_p_p_cJSON obj) { + return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr; + } +} + +} +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 2.0.1 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +namespace FreeSWITCH.Native { + +using System; +using System.Runtime.InteropServices; + public class SWIGTYPE_p_p_p_char { private HandleRef swigCPtr; @@ -23052,8 +23260,12 @@ public enum switch_channel_flag_t { CF_VIDEO_REFRESH_REQ, CF_SERVICE_AUDIO, CF_SERVICE_VIDEO, + CF_ZRTP_PASSTHRU_REQ, + CF_ZRTP_PASSTHRU, CF_ZRTP_HASH, - CF_ZRTP_PASS, + CF_CHANNEL_SWAP, + CF_PICKUP, + CF_CONFIRM_BLIND_TRANSFER, CF_FLAG_MAX } @@ -24971,6 +25183,7 @@ public enum switch_core_session_message_types_t { SWITCH_MESSAGE_INDICATE_SIGNAL_DATA, SWITCH_MESSAGE_INDICATE_INFO, SWITCH_MESSAGE_INDICATE_AUDIO_DATA, + SWITCH_MESSAGE_INDICATE_BLIND_TRANSFER_RESPONSE, SWITCH_MESSAGE_INVALID } @@ -25930,7 +26143,8 @@ namespace FreeSWITCH.Native { ED_NONE = 0, ED_MUX_READ = (1 << 0), ED_MUX_WRITE = (1 << 1), - ED_DTMF = (1 << 2) + ED_DTMF = (1 << 2), + ED_COPY_DISPLAY = (1 << 3) } } @@ -29926,7 +30140,8 @@ namespace FreeSWITCH.Native { SMBF_THREAD_LOCK = (1 << 7), SMBF_PRUNE = (1 << 8), SMBF_NO_PAUSE = (1 << 9), - SMBF_STEREO_SWAP = (1 << 10) + SMBF_STEREO_SWAP = (1 << 10), + SMBF_LOCK = (1 << 11) } } diff --git a/src/mod/languages/mod_managed/managed/swig.cs b/src/mod/languages/mod_managed/managed/swig.cs index 94fecfa933..62dea3d5c5 100644 --- a/src/mod/languages/mod_managed/managed/swig.cs +++ b/src/mod/languages/mod_managed/managed/swig.cs @@ -1054,6 +1054,21 @@ public class freeswitch { freeswitchPINVOKE.switch_core_session_disable_heartbeat(SWIGTYPE_p_switch_core_session.getCPtr(session)); } + public static switch_status_t switch_core_media_bug_pop(SWIGTYPE_p_switch_core_session orig_session, string function, SWIGTYPE_p_p_switch_media_bug pop) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_media_bug_pop(SWIGTYPE_p_switch_core_session.getCPtr(orig_session), function, SWIGTYPE_p_p_switch_media_bug.getCPtr(pop)); + return ret; + } + + public static switch_status_t switch_core_media_bug_exec_all(SWIGTYPE_p_switch_core_session orig_session, string function, SWIGTYPE_p_f_p_switch_media_bug_p_void__void cb, SWIGTYPE_p_void user_data) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_media_bug_exec_all(SWIGTYPE_p_switch_core_session.getCPtr(orig_session), function, SWIGTYPE_p_f_p_switch_media_bug_p_void__void.getCPtr(cb), SWIGTYPE_p_void.getCPtr(user_data)); + return ret; + } + + public static uint switch_core_media_bug_count(SWIGTYPE_p_switch_core_session orig_session, string function) { + uint ret = freeswitchPINVOKE.switch_core_media_bug_count(SWIGTYPE_p_switch_core_session.getCPtr(orig_session), function); + return ret; + } + public static switch_status_t switch_core_media_bug_add(SWIGTYPE_p_switch_core_session session, string function, string target, SWIGTYPE_p_f_p_switch_media_bug_p_void_enum_switch_abc_type_t__switch_bool_t callback, SWIGTYPE_p_void user_data, SWIGTYPE_p_time_t stop_time, uint flags, SWIGTYPE_p_p_switch_media_bug new_bug) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_media_bug_add(SWIGTYPE_p_switch_core_session.getCPtr(session), function, target, SWIGTYPE_p_f_p_switch_media_bug_p_void_enum_switch_abc_type_t__switch_bool_t.getCPtr(callback), SWIGTYPE_p_void.getCPtr(user_data), SWIGTYPE_p_time_t.getCPtr(stop_time), flags, SWIGTYPE_p_p_switch_media_bug.getCPtr(new_bug)); if (freeswitchPINVOKE.SWIGPendingException.Pending) throw freeswitchPINVOKE.SWIGPendingException.Retrieve(); @@ -1094,6 +1109,10 @@ public class freeswitch { return ret; } + public static void switch_core_media_bug_set_read_demux_frame(SWIGTYPE_p_switch_media_bug bug, switch_frame frame) { + freeswitchPINVOKE.switch_core_media_bug_set_read_demux_frame(SWIGTYPE_p_switch_media_bug.getCPtr(bug), switch_frame.getCPtr(frame)); + } + public static SWIGTYPE_p_switch_core_session switch_core_media_bug_get_session(SWIGTYPE_p_switch_media_bug bug) { IntPtr cPtr = freeswitchPINVOKE.switch_core_media_bug_get_session(SWIGTYPE_p_switch_media_bug.getCPtr(bug)); SWIGTYPE_p_switch_core_session ret = (cPtr == IntPtr.Zero) ? null : new SWIGTYPE_p_switch_core_session(cPtr, false); @@ -1144,8 +1163,8 @@ public class freeswitch { return ret; } - public static switch_status_t switch_core_media_bug_remove_all(SWIGTYPE_p_switch_core_session session) { - switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_media_bug_remove_all(SWIGTYPE_p_switch_core_session.getCPtr(session)); + public static switch_status_t switch_core_media_bug_remove_all_function(SWIGTYPE_p_switch_core_session session, string function) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_media_bug_remove_all_function(SWIGTYPE_p_switch_core_session.getCPtr(session), function); return ret; } @@ -1988,6 +2007,11 @@ public class freeswitch { return ret; } + public static switch_status_t switch_core_session_get_real_read_impl(SWIGTYPE_p_switch_core_session session, switch_codec_implementation impp) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_session_get_real_read_impl(SWIGTYPE_p_switch_core_session.getCPtr(session), switch_codec_implementation.getCPtr(impp)); + return ret; + } + public static switch_status_t switch_core_session_get_write_impl(SWIGTYPE_p_switch_core_session session, switch_codec_implementation impp) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_session_get_write_impl(SWIGTYPE_p_switch_core_session.getCPtr(session), switch_codec_implementation.getCPtr(impp)); return ret; @@ -2687,6 +2711,15 @@ public class freeswitch { freeswitchPINVOKE.switch_close_extra_files(SWIGTYPE_p_int.getCPtr(keep), keep_ttl); } + public static switch_status_t switch_core_thread_set_cpu_affinity(int cpu) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_core_thread_set_cpu_affinity(cpu); + return ret; + } + + public static void switch_os_yield() { + freeswitchPINVOKE.switch_os_yield(); + } + public static switch_status_t switch_loadable_module_init(switch_bool_t autoload) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_loadable_module_init((int)autoload); return ret; @@ -2708,6 +2741,11 @@ public class freeswitch { return ret; } + public static string switch_parse_codec_buf(string buf, SWIGTYPE_p_unsigned_long interval, SWIGTYPE_p_unsigned_long rate, SWIGTYPE_p_unsigned_long bit) { + string ret = freeswitchPINVOKE.switch_parse_codec_buf(buf, SWIGTYPE_p_unsigned_long.getCPtr(interval), SWIGTYPE_p_unsigned_long.getCPtr(rate), SWIGTYPE_p_unsigned_long.getCPtr(bit)); + return ret; + } + public static switch_dialplan_interface switch_loadable_module_get_dialplan_interface(string name) { IntPtr cPtr = freeswitchPINVOKE.switch_loadable_module_get_dialplan_interface(name); switch_dialplan_interface ret = (cPtr == IntPtr.Zero) ? null : new switch_dialplan_interface(cPtr, false); @@ -4358,6 +4396,10 @@ public class freeswitch { return ret; } + public static void switch_event_add_presence_data_cols(SWIGTYPE_p_switch_channel channel, switch_event arg1, string prefix) { + freeswitchPINVOKE.switch_event_add_presence_data_cols(SWIGTYPE_p_switch_channel.getCPtr(channel), switch_event.getCPtr(arg1), prefix); + } + public static switch_status_t switch_resample_perform_create(SWIGTYPE_p_p_switch_audio_resampler_t new_resampler, uint from_rate, uint to_rate, uint to_size, int quality, uint channels, string file, string func, int line) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_resample_perform_create(SWIGTYPE_p_p_switch_audio_resampler_t.getCPtr(new_resampler), from_rate, to_rate, to_size, quality, channels, file, func, line); return ret; @@ -4414,6 +4456,11 @@ public class freeswitch { return ret; } + public static uint switch_unmerge_sln(SWIGTYPE_p_short data, uint samples, SWIGTYPE_p_short other_data, uint other_samples) { + uint ret = freeswitchPINVOKE.switch_unmerge_sln(SWIGTYPE_p_short.getCPtr(data), samples, SWIGTYPE_p_short.getCPtr(other_data), other_samples); + return ret; + } + public static void switch_mux_channels(SWIGTYPE_p_short data, SWIGTYPE_p_switch_size_t samples, uint channels) { freeswitchPINVOKE.switch_mux_channels(SWIGTYPE_p_short.getCPtr(data), SWIGTYPE_p_switch_size_t.getCPtr(samples), channels); if (freeswitchPINVOKE.SWIGPendingException.Pending) throw freeswitchPINVOKE.SWIGPendingException.Retrieve(); @@ -4429,6 +4476,11 @@ public class freeswitch { return ret; } + public static switch_status_t switch_ivr_generate_json_cdr(SWIGTYPE_p_switch_core_session session, SWIGTYPE_p_p_cJSON json_cdr, switch_bool_t urlencode) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_generate_json_cdr(SWIGTYPE_p_switch_core_session.getCPtr(session), SWIGTYPE_p_p_cJSON.getCPtr(json_cdr), (int)urlencode); + return ret; + } + public static switch_status_t switch_ivr_generate_xml_cdr(SWIGTYPE_p_switch_core_session session, SWIGTYPE_p_p_switch_xml xml_cdr) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_generate_xml_cdr(SWIGTYPE_p_switch_core_session.getCPtr(session), SWIGTYPE_p_p_switch_xml.getCPtr(xml_cdr)); return ret; @@ -4560,6 +4612,21 @@ public class freeswitch { return ret; } + public static switch_status_t switch_ivr_eavesdrop_pop_eavesdropper(SWIGTYPE_p_switch_core_session session, SWIGTYPE_p_p_switch_core_session sessionp) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_eavesdrop_pop_eavesdropper(SWIGTYPE_p_switch_core_session.getCPtr(session), SWIGTYPE_p_p_switch_core_session.getCPtr(sessionp)); + return ret; + } + + public static switch_status_t switch_ivr_eavesdrop_exec_all(SWIGTYPE_p_switch_core_session session, string app, string arg) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_eavesdrop_exec_all(SWIGTYPE_p_switch_core_session.getCPtr(session), app, arg); + return ret; + } + + public static switch_status_t switch_ivr_eavesdrop_update_display(SWIGTYPE_p_switch_core_session session, string name, string number) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_eavesdrop_update_display(SWIGTYPE_p_switch_core_session.getCPtr(session), name, number); + return ret; + } + public static switch_status_t switch_ivr_eavesdrop_session(SWIGTYPE_p_switch_core_session session, string uuid, string require_group, uint flags) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_eavesdrop_session(SWIGTYPE_p_switch_core_session.getCPtr(session), uuid, require_group, flags); return ret; @@ -5076,6 +5143,11 @@ public class freeswitch { return ret; } + public static switch_status_t switch_ivr_blind_transfer_ack(SWIGTYPE_p_switch_core_session session, switch_bool_t success) { + switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_ivr_blind_transfer_ack(SWIGTYPE_p_switch_core_session.getCPtr(session), (int)success); + return ret; + } + public static switch_status_t switch_rtp_add_crypto_key(SWIGTYPE_p_switch_rtp rtp_session, switch_rtp_crypto_direction_t direction, uint index, switch_rtp_crypto_key_type_t type, SWIGTYPE_p_unsigned_char key, SWIGTYPE_p_switch_size_t keylen) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_rtp_add_crypto_key(SWIGTYPE_p_switch_rtp.getCPtr(rtp_session), (int)direction, index, (int)type, SWIGTYPE_p_unsigned_char.getCPtr(key), SWIGTYPE_p_switch_size_t.getCPtr(keylen)); if (freeswitchPINVOKE.SWIGPendingException.Pending) throw freeswitchPINVOKE.SWIGPendingException.Retrieve(); @@ -5375,6 +5447,10 @@ public class freeswitch { return ret; } + public static void switch_rtp_set_interdigit_delay(SWIGTYPE_p_switch_rtp rtp_session, uint delay) { + freeswitchPINVOKE.switch_rtp_set_interdigit_delay(SWIGTYPE_p_switch_rtp.getCPtr(rtp_session), delay); + } + public static switch_status_t switch_log_init(SWIGTYPE_p_apr_pool_t pool, switch_bool_t colorize) { switch_status_t ret = (switch_status_t)freeswitchPINVOKE.switch_log_init(SWIGTYPE_p_apr_pool_t.getCPtr(pool), (int)colorize); return ret; @@ -5992,7 +6068,9 @@ public class freeswitch { public static readonly string SWITCH_ENABLE_HEARTBEAT_EVENTS_VARIABLE = freeswitchPINVOKE.SWITCH_ENABLE_HEARTBEAT_EVENTS_VARIABLE_get(); public static readonly string SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE = freeswitchPINVOKE.SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE_get(); public static readonly string SWITCH_READ_RESULT_VARIABLE = freeswitchPINVOKE.SWITCH_READ_RESULT_VARIABLE_get(); + public static readonly string SWITCH_ATT_XFER_RESULT_VARIABLE = freeswitchPINVOKE.SWITCH_ATT_XFER_RESULT_VARIABLE_get(); public static readonly string SWITCH_COPY_XML_CDR_VARIABLE = freeswitchPINVOKE.SWITCH_COPY_XML_CDR_VARIABLE_get(); + public static readonly string SWITCH_COPY_JSON_CDR_VARIABLE = freeswitchPINVOKE.SWITCH_COPY_JSON_CDR_VARIABLE_get(); public static readonly string SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE = freeswitchPINVOKE.SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE_get(); public static readonly string SWITCH_TRANSFER_HISTORY_VARIABLE = freeswitchPINVOKE.SWITCH_TRANSFER_HISTORY_VARIABLE_get(); public static readonly string SWITCH_TRANSFER_SOURCE_VARIABLE = freeswitchPINVOKE.SWITCH_TRANSFER_SOURCE_VARIABLE_get(); @@ -6027,6 +6105,7 @@ public class freeswitch { public static readonly string SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME = freeswitchPINVOKE.SWITCH_CACHE_SPEECH_HANDLES_OBJ_NAME_get(); public static readonly string SWITCH_BYPASS_MEDIA_VARIABLE = freeswitchPINVOKE.SWITCH_BYPASS_MEDIA_VARIABLE_get(); public static readonly string SWITCH_PROXY_MEDIA_VARIABLE = freeswitchPINVOKE.SWITCH_PROXY_MEDIA_VARIABLE_get(); + public static readonly string SWITCH_ZRTP_PASSTHRU_VARIABLE = freeswitchPINVOKE.SWITCH_ZRTP_PASSTHRU_VARIABLE_get(); public static readonly string SWITCH_ENDPOINT_DISPOSITION_VARIABLE = freeswitchPINVOKE.SWITCH_ENDPOINT_DISPOSITION_VARIABLE_get(); public static readonly string SWITCH_HOLD_MUSIC_VARIABLE = freeswitchPINVOKE.SWITCH_HOLD_MUSIC_VARIABLE_get(); public static readonly string SWITCH_TEMP_HOLD_MUSIC_VARIABLE = freeswitchPINVOKE.SWITCH_TEMP_HOLD_MUSIC_VARIABLE_get(); @@ -6535,9 +6614,15 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_READ_RESULT_VARIABLE_get")] public static extern string SWITCH_READ_RESULT_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_ATT_XFER_RESULT_VARIABLE_get")] + public static extern string SWITCH_ATT_XFER_RESULT_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_COPY_XML_CDR_VARIABLE_get")] public static extern string SWITCH_COPY_XML_CDR_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_COPY_JSON_CDR_VARIABLE_get")] + public static extern string SWITCH_COPY_JSON_CDR_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE_get")] public static extern string SWITCH_PROTO_SPECIFIC_HANGUP_CAUSE_VARIABLE_get(); @@ -6640,6 +6725,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_PROXY_MEDIA_VARIABLE_get")] public static extern string SWITCH_PROXY_MEDIA_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_ZRTP_PASSTHRU_VARIABLE_get")] + public static extern string SWITCH_ZRTP_PASSTHRU_VARIABLE_get(); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_ENDPOINT_DISPOSITION_VARIABLE_get")] public static extern string SWITCH_ENDPOINT_DISPOSITION_VARIABLE_get(); @@ -7936,6 +8024,15 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_core_session_disable_heartbeat")] public static extern void switch_core_session_disable_heartbeat(HandleRef jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_pop")] + public static extern int switch_core_media_bug_pop(HandleRef jarg1, string jarg2, HandleRef jarg3); + + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_exec_all")] + public static extern int switch_core_media_bug_exec_all(HandleRef jarg1, string jarg2, HandleRef jarg3, HandleRef jarg4); + + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_count")] + public static extern uint switch_core_media_bug_count(HandleRef jarg1, string jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_add")] public static extern int switch_core_media_bug_add(HandleRef jarg1, string jarg2, string jarg3, HandleRef jarg4, HandleRef jarg5, HandleRef jarg6, uint jarg7, HandleRef jarg8); @@ -7960,6 +8057,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_get_read_replace_frame")] public static extern IntPtr switch_core_media_bug_get_read_replace_frame(HandleRef jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_set_read_demux_frame")] + public static extern void switch_core_media_bug_set_read_demux_frame(HandleRef jarg1, HandleRef jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_get_session")] public static extern IntPtr switch_core_media_bug_get_session(HandleRef jarg1); @@ -7990,8 +8090,8 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_close")] public static extern int switch_core_media_bug_close(HandleRef jarg1); - [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_remove_all")] - public static extern int switch_core_media_bug_remove_all(HandleRef jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_remove_all_function")] + public static extern int switch_core_media_bug_remove_all_function(HandleRef jarg1, string jarg2); [DllImport("mod_managed", EntryPoint="CSharp_switch_core_media_bug_enumerate")] public static extern int switch_core_media_bug_enumerate(HandleRef jarg1, HandleRef jarg2); @@ -8503,6 +8603,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_core_session_get_read_impl")] public static extern int switch_core_session_get_read_impl(HandleRef jarg1, HandleRef jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_session_get_real_read_impl")] + public static extern int switch_core_session_get_real_read_impl(HandleRef jarg1, HandleRef jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_session_get_write_impl")] public static extern int switch_core_session_get_write_impl(HandleRef jarg1, HandleRef jarg2); @@ -9007,6 +9110,12 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_close_extra_files")] public static extern void switch_close_extra_files(HandleRef jarg1, int jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_core_thread_set_cpu_affinity")] + public static extern int switch_core_thread_set_cpu_affinity(int jarg1); + + [DllImport("mod_managed", EntryPoint="CSharp_switch_os_yield")] + public static extern void switch_os_yield(); + [DllImport("mod_managed", EntryPoint="CSharp_switch_loadable_module_interface_module_name_set")] public static extern void switch_loadable_module_interface_module_name_set(HandleRef jarg1, string jarg2); @@ -9139,6 +9248,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_loadable_module_get_codec_interface")] public static extern IntPtr switch_loadable_module_get_codec_interface(string jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_parse_codec_buf")] + public static extern string switch_parse_codec_buf(string jarg1, HandleRef jarg2, HandleRef jarg3, HandleRef jarg4); + [DllImport("mod_managed", EntryPoint="CSharp_switch_loadable_module_get_dialplan_interface")] public static extern IntPtr switch_loadable_module_get_dialplan_interface(string jarg1); @@ -12871,6 +12983,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_event_check_permission_list")] public static extern int switch_event_check_permission_list(HandleRef jarg1, string jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_event_add_presence_data_cols")] + public static extern void switch_event_add_presence_data_cols(HandleRef jarg1, HandleRef jarg2, string jarg3); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_RESAMPLE_QUALITY_get")] public static extern int SWITCH_RESAMPLE_QUALITY_get(); @@ -12964,6 +13079,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_merge_sln")] public static extern uint switch_merge_sln(HandleRef jarg1, uint jarg2, HandleRef jarg3, uint jarg4); + [DllImport("mod_managed", EntryPoint="CSharp_switch_unmerge_sln")] + public static extern uint switch_unmerge_sln(HandleRef jarg1, uint jarg2, HandleRef jarg3, uint jarg4); + [DllImport("mod_managed", EntryPoint="CSharp_switch_mux_channels")] public static extern void switch_mux_channels(HandleRef jarg1, HandleRef jarg2, uint jarg3); @@ -13075,6 +13193,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_activate_unicast")] public static extern int switch_ivr_activate_unicast(HandleRef jarg1, string jarg2, ushort jarg3, string jarg4, ushort jarg5, string jarg6, string jarg7); + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_generate_json_cdr")] + public static extern int switch_ivr_generate_json_cdr(HandleRef jarg1, HandleRef jarg2, int jarg3); + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_generate_xml_cdr")] public static extern int switch_ivr_generate_xml_cdr(HandleRef jarg1, HandleRef jarg2); @@ -13153,6 +13274,15 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_record_session")] public static extern int switch_ivr_record_session(HandleRef jarg1, string jarg2, uint jarg3, HandleRef jarg4); + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_eavesdrop_pop_eavesdropper")] + public static extern int switch_ivr_eavesdrop_pop_eavesdropper(HandleRef jarg1, HandleRef jarg2); + + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_eavesdrop_exec_all")] + public static extern int switch_ivr_eavesdrop_exec_all(HandleRef jarg1, string jarg2, string jarg3); + + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_eavesdrop_update_display")] + public static extern int switch_ivr_eavesdrop_update_display(HandleRef jarg1, string jarg2, string jarg3); + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_eavesdrop_session")] public static extern int switch_ivr_eavesdrop_session(HandleRef jarg1, string jarg2, string jarg3, uint jarg4); @@ -13465,6 +13595,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_kill_uuid")] public static extern int switch_ivr_kill_uuid(string jarg1, int jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_ivr_blind_transfer_ack")] + public static extern int switch_ivr_blind_transfer_ack(HandleRef jarg1, int jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_SWITCH_RTP_MAX_BUF_LEN_get")] public static extern int SWITCH_RTP_MAX_BUF_LEN_get(); @@ -13708,6 +13841,9 @@ class freeswitchPINVOKE { [DllImport("mod_managed", EntryPoint="CSharp_switch_rtp_check_auto_adj")] public static extern byte switch_rtp_check_auto_adj(HandleRef jarg1); + [DllImport("mod_managed", EntryPoint="CSharp_switch_rtp_set_interdigit_delay")] + public static extern void switch_rtp_set_interdigit_delay(HandleRef jarg1, uint jarg2); + [DllImport("mod_managed", EntryPoint="CSharp_switch_log_node_t_data_set")] public static extern void switch_log_node_t_data_set(HandleRef jarg1, string jarg2); @@ -17362,6 +17498,36 @@ namespace FreeSWITCH.Native { using System; using System.Runtime.InteropServices; +public class SWIGTYPE_p_f_p_switch_media_bug_p_void__void { + private HandleRef swigCPtr; + + internal SWIGTYPE_p_f_p_switch_media_bug_p_void__void(IntPtr cPtr, bool futureUse) { + swigCPtr = new HandleRef(this, cPtr); + } + + protected SWIGTYPE_p_f_p_switch_media_bug_p_void__void() { + swigCPtr = new HandleRef(null, IntPtr.Zero); + } + + internal static HandleRef getCPtr(SWIGTYPE_p_f_p_switch_media_bug_p_void__void obj) { + return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr; + } +} + +} +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 1.3.35 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +namespace FreeSWITCH.Native { + +using System; +using System.Runtime.InteropServices; + public class SWIGTYPE_p_f_p_switch_rtp_p_switch_socket_t_p_void_switch_size_t_p_switch_sockaddr_t__void { private HandleRef swigCPtr; @@ -18142,6 +18308,36 @@ namespace FreeSWITCH.Native { using System; using System.Runtime.InteropServices; +public class SWIGTYPE_p_p_cJSON { + private HandleRef swigCPtr; + + internal SWIGTYPE_p_p_cJSON(IntPtr cPtr, bool futureUse) { + swigCPtr = new HandleRef(this, cPtr); + } + + protected SWIGTYPE_p_p_cJSON() { + swigCPtr = new HandleRef(null, IntPtr.Zero); + } + + internal static HandleRef getCPtr(SWIGTYPE_p_p_cJSON obj) { + return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr; + } +} + +} +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 1.3.35 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +namespace FreeSWITCH.Native { + +using System; +using System.Runtime.InteropServices; + public class SWIGTYPE_p_p_p_char { private HandleRef swigCPtr; @@ -22996,8 +23192,12 @@ public enum switch_channel_flag_t { CF_VIDEO_REFRESH_REQ, CF_SERVICE_AUDIO, CF_SERVICE_VIDEO, + CF_ZRTP_PASSTHRU_REQ, + CF_ZRTP_PASSTHRU, CF_ZRTP_HASH, - CF_ZRTP_PASS, + CF_CHANNEL_SWAP, + CF_PICKUP, + CF_CONFIRM_BLIND_TRANSFER, CF_FLAG_MAX } @@ -24891,6 +25091,7 @@ public enum switch_core_session_message_types_t { SWITCH_MESSAGE_INDICATE_SIGNAL_DATA, SWITCH_MESSAGE_INDICATE_INFO, SWITCH_MESSAGE_INDICATE_AUDIO_DATA, + SWITCH_MESSAGE_INDICATE_BLIND_TRANSFER_RESPONSE, SWITCH_MESSAGE_INVALID } @@ -25836,7 +26037,8 @@ namespace FreeSWITCH.Native { ED_NONE = 0, ED_MUX_READ = (1 << 0), ED_MUX_WRITE = (1 << 1), - ED_DTMF = (1 << 2) + ED_DTMF = (1 << 2), + ED_COPY_DISPLAY = (1 << 3) } } @@ -29776,7 +29978,8 @@ namespace FreeSWITCH.Native { SMBF_THREAD_LOCK = (1 << 7), SMBF_PRUNE = (1 << 8), SMBF_NO_PAUSE = (1 << 9), - SMBF_STEREO_SWAP = (1 << 10) + SMBF_STEREO_SWAP = (1 << 10), + SMBF_LOCK = (1 << 11) } } diff --git a/src/mod/loggers/mod_logfile/mod_logfile.c b/src/mod/loggers/mod_logfile/mod_logfile.c index db78909134..27f0aeba37 100644 --- a/src/mod/loggers/mod_logfile/mod_logfile.c +++ b/src/mod/loggers/mod_logfile/mod_logfile.c @@ -333,7 +333,12 @@ static switch_status_t load_profile(switch_xml_t xml) new_profile->logfile = strdup(val); } else if (!strcmp(var, "rollover")) { new_profile->roll_size = switch_atoui(val); - } else if (!strcmp(var, "maximum-rotate")) { + } else if (!strcmp(var, "maximum-rotate") || + !strcmp(var, "maxfilecount")) { + /* maxfilecount is backwards compatible parameter, we had our own log rotation code + * and eventually mainstream FreeSWITCH included the parameter maximum-rotate which + * did pretty much the same as ours, I deleted the old code and stayed with the official + * project rotation code */ new_profile->max_rot = switch_atoui(val); if (new_profile->max_rot == 0) { new_profile->max_rot = MAX_ROT; diff --git a/src/mod/applications/mod_fax/mod_fax.2008.vcproj b/src/mod/say/mod_say_fa/mod_say_fa.2008.vcproj similarity index 74% rename from src/mod/applications/mod_fax/mod_fax.2008.vcproj rename to src/mod/say/mod_say_fa/mod_say_fa.2008.vcproj index dd10ca8a93..7cd7862cd7 100644 --- a/src/mod/applications/mod_fax/mod_fax.2008.vcproj +++ b/src/mod/say/mod_say_fa/mod_say_fa.2008.vcproj @@ -2,9 +2,9 @@ @@ -23,7 +23,7 @@ Name="Debug|Win32" ConfigurationType="2" InheritedPropertySheets="..\..\..\..\w32\module_debug.vsprops" - CharacterSet="0" + CharacterSet="2" > @@ -86,7 +84,7 @@ Name="Debug|x64" ConfigurationType="2" InheritedPropertySheets="..\..\..\..\w32\module_debug.vsprops" - CharacterSet="0" + CharacterSet="2" > @@ -215,7 +209,7 @@ Name="Release|x64" ConfigurationType="2" InheritedPropertySheets="..\..\..\..\w32\module_release.vsprops" - CharacterSet="0" + CharacterSet="2" > - - - - diff --git a/src/mod/say/mod_say_fa/mod_say_fa.2010.vcxproj b/src/mod/say/mod_say_fa/mod_say_fa.2010.vcxproj new file mode 100644 index 0000000000..cefc30eeab --- /dev/null +++ b/src/mod/say/mod_say_fa/mod_say_fa.2010.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + mod_say_fa + {85a71f08-9f6c-11e1-9967-002186cfa17e} + mod_say_fa + Win32Proj + + + + DynamicLibrary + MultiByte + + + DynamicLibrary + MultiByte + + + DynamicLibrary + MultiByte + + + DynamicLibrary + MultiByte + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + + + + + + + + false + + + + + + + X64 + + + + + + + false + + + MachineX64 + + + + + + + + + false + + + + + + + X64 + + + + + + + false + + + MachineX64 + + + + + + + + {f6c55d93-b927-4483-bb69-15aef3dd2dff} + false + + + {202d7a4e-760d-4d0e-afa1-d7459ced30ff} + false + + + + + + \ No newline at end of file diff --git a/src/mod/say/mod_say_fa/mod_say_fa.c b/src/mod/say/mod_say_fa/mod_say_fa.c new file mode 100644 index 0000000000..e044fca7b6 --- /dev/null +++ b/src/mod/say/mod_say_fa/mod_say_fa.c @@ -0,0 +1,685 @@ +/* + * Copyright (c) 2007, Anthony Minessale II + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The Initial Developer of the Original Code is + * Anthony Minessale II + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Anthony Minessale II + * Mahdi Moradi + * Babak Yakhchali + * + * mod_say_fa.c -- Say for Persian + * + */ + +#include +#include +#include + +void gregorian_to_jalali(/* output */ int *j_y, int *j_m, int *j_d, + /* input */ int g_y, int g_m, int g_d); +void jalali_to_gregorian(/* output */ int *g_y, int *g_m, int *g_d, + /* input */ int j_y, int j_m, int j_d); + +SWITCH_MODULE_LOAD_FUNCTION(mod_say_fa_load); +SWITCH_MODULE_DEFINITION(mod_say_fa, mod_say_fa_load, NULL, NULL); + +#define say_num(num, meth) { \ + char tmp[80]; \ + switch_status_t tstatus; \ + switch_say_method_t smeth = say_args->method; \ + switch_say_type_t stype = say_args->type; \ + say_args->type = SST_ITEMS; say_args->method = meth; \ + switch_snprintf(tmp, sizeof(tmp), "%u", (unsigned)num); \ + if ((tstatus = \ + fa_say_general_count(session, tmp, say_args, args)) \ + != SWITCH_STATUS_SUCCESS) { \ + return tstatus; \ + } \ + say_args->method = smeth; say_args->type = stype; \ + } \ + +#define say_file(...) { \ + char tmp[80]; \ + switch_status_t tstatus; \ + switch_snprintf(tmp, sizeof(tmp), __VA_ARGS__); \ + if ((tstatus = \ + switch_ivr_play_file(session, NULL, tmp, args)) \ + != SWITCH_STATUS_SUCCESS){ \ + return tstatus; \ + } \ + if (!switch_channel_ready(switch_core_session_get_channel(session))) { \ + return SWITCH_STATUS_FALSE; \ + }} \ + +static switch_status_t play_group(switch_say_method_t method, int a, int b, int c, char *what, switch_core_session_t *session, switch_input_args_t *args) +{ + if (a) { + if ( !b && !c ) + { + if( method == SSM_COUNTED ) + { + say_file("digits/%d00om.wav", a); + } + else + say_file("digits/%d00.wav", a); + b = c = 0; + } + else + say_file("digits/%d00+.wav", a); + } + + if (b) { + if ( !c || b == 1 ) + { + if( method == SSM_COUNTED ) + { + say_file("digits/%d%dom.wav", b, c); + } + else + say_file("digits/%d%d.wav", b, c); + c = 0; + } + else + say_file("digits/%d0+.wav", b); + } + + if (c) { + if (method == SSM_COUNTED) { + say_file("digits/%dom.wav", c); + } else { + say_file("digits/%d.wav", c); + } + } + + if (what && (a || b || c)) { + say_file(what); + } + + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t fa_say_general_count(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) +{ + int in; + int x = 0; + int sum = 0; + char what_file[50] = ""; + int places[9] = { 0 }; + char sbuf[128] = ""; + switch_status_t status; + + if (say_args->method == SSM_ITERATED) { + if ((tosay = switch_strip_commas(tosay, sbuf, sizeof(sbuf)))) { + char *p; + for (p = tosay; p && *p; p++) { + say_file("digits/%c.wav", *p); + } + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse Error!\n"); + return SWITCH_STATUS_GENERR; + } + return SWITCH_STATUS_SUCCESS; + } + + if (!(tosay = switch_strip_commas(tosay, sbuf, sizeof(sbuf))) || strlen(tosay) > 9) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse Error!\n"); + return SWITCH_STATUS_GENERR; + } + + in = atoi(tosay); + + if (in != 0) { + for (x = 8; x >= 0; x--) { + int num = (int) pow(10, x); + if ((places[(uint32_t) x] = in / num)) { + in -= places[(uint32_t) x] * num; + } + } + + switch (say_args->method) { + case SSM_COUNTED: + case SSM_PRONOUNCED: + sum = places[5] + places[4] + places[3] + places[2] + places[1] + places[0]; + if( !sum ) + { + if( say_args->method == SSM_COUNTED ) + { + strcpy(what_file,"digits/1000000om.wav"); + } + else + strcpy(what_file,"digits/1000000.wav"); + } + else + strcpy(what_file,"digits/1000000+.wav"); + if ((status = play_group(SSM_PRONOUNCED, places[8], places[7], places[6], what_file, session, args)) != SWITCH_STATUS_SUCCESS) { + return status; + } + sum = places[2] + places[1] + places[0]; + if( !sum ) + { if( say_args->method == SSM_COUNTED ) + { + strcpy(what_file,"digits/1000om.wav"); + } + else + strcpy(what_file,"digits/1000.wav"); + } + else + strcpy(what_file,"digits/1000+.wav"); + if ((status = play_group(SSM_PRONOUNCED, places[5], places[4], places[3], what_file, session, args)) != SWITCH_STATUS_SUCCESS) { + return status; + } + if ((status = play_group(say_args->method, places[2], places[1], places[0], NULL, session, args)) != SWITCH_STATUS_SUCCESS) { + return status; + } + break; + default: + break; + } + } else { + say_file("digits/0.wav"); + } + + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t fa_say_time(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) +{ + int32_t t; + switch_time_t target = 0, target_now = 0; + switch_time_exp_t tm, tm_now; + uint8_t say_date = 0, say_time = 0, say_year = 0, say_month = 0, say_dow = 0, say_day = 0, say_yesterday = 0, say_today = 0; + switch_channel_t *channel = switch_core_session_get_channel(session); + const char *tz = switch_channel_get_variable(channel, "timezone"); + int jalali_year = 1, jalali_month = 0, jalali_day = 0; + + if (say_args->type == SST_TIME_MEASUREMENT) { + int64_t hours = 0; + int64_t minutes = 0; + int64_t seconds = 0; + int64_t r = 0; + + if (strchr(tosay, ':')) { + char *tme = switch_core_session_strdup(session, tosay); + char *p; + + if ((p = strrchr(tme, ':'))) { + *p++ = '\0'; + seconds = atoi(p); + if ((p = strchr(tme, ':'))) { + *p++ = '\0'; + minutes = atoi(p); + if (tme) { + hours = atoi(tme); + } + } else { + minutes = atoi(tme); + } + } + } else { + if ((seconds = atol(tosay)) <= 0) { + seconds = (int64_t) switch_epoch_time_now(NULL); + } + + if (seconds >= 60) { + minutes = seconds / 60; + r = seconds % 60; + seconds = r; + } + + if (minutes >= 60) { + hours = minutes / 60; + r = minutes % 60; + minutes = r; + } + } + + if (hours) { + say_num(hours, SSM_PRONOUNCED); + if(minutes || seconds) + { + say_file("time/hour+.wav"); + } + else + say_file("time/hour.wav"); + } + + if (minutes) { + say_num(minutes, SSM_PRONOUNCED); + if( seconds ) + { + say_file("time/minutes+.wav"); + } + else + say_file("time/minutes.wav"); + } + + if (seconds) { + say_num(seconds, SSM_PRONOUNCED); + say_file("time/seconds.wav"); + } + + return SWITCH_STATUS_SUCCESS; + } + + if ((t = atol(tosay)) > 0) { + target = switch_time_make(t, 0); + target_now = switch_micro_time_now(); + } else { + target = switch_micro_time_now(); + target_now = switch_micro_time_now(); + } + + if (tz) { + int check = atoi(tz); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Timezone is [%s]\n", tz); + if (check) { + switch_time_exp_tz(&tm, target, check); + switch_time_exp_tz(&tm_now, target_now, check); + } else { + switch_time_exp_tz_name(tz, &tm, target); + switch_time_exp_tz_name(tz, &tm_now, target_now); + } + } else { + switch_time_exp_lt(&tm, target); + switch_time_exp_lt(&tm_now, target_now); + } + + switch (say_args->type) { + case SST_CURRENT_DATE_TIME: + say_date = say_time = 1; + break; + case SST_CURRENT_DATE: + say_date = 1; + break; + case SST_CURRENT_TIME: + say_time = 1; + break; + case SST_SHORT_DATE_TIME: + say_time = 1; + if (tm.tm_year != tm_now.tm_year) { + say_date = 1; + break; + } + if (tm.tm_yday == tm_now.tm_yday) { + say_today = 1; + break; + } + if (tm.tm_yday == tm_now.tm_yday - 1) { + say_yesterday = 1; + break; + } + if (tm.tm_yday >= tm_now.tm_yday - 5) { + say_dow = 1; + break; + } + if (tm.tm_mon != tm_now.tm_mon) { + say_month = say_day = say_dow = 1; + break; + } + + say_month = say_day = say_dow = 1; + + break; + default: + break; + } + + if (say_today) { + say_file("time/today.wav"); + } + if (say_yesterday) { + say_file("time/yesterday.wav"); + } + if (say_dow) { + say_file("time/day-%d.wav", tm.tm_wday); + } + + if (say_date) { + say_year = say_month = say_day = say_dow = 1; + say_today = say_yesterday = 0; + } + + gregorian_to_jalali(&jalali_year,&jalali_month,&jalali_day,tm.tm_year + 1900,tm.tm_mon + 1,tm.tm_mday); + + if (say_day) { + if(jalali_day > 20 && jalali_day != 30) + { + say_file("digits/%d+.wav", (jalali_day - jalali_day % 10)); + say_file("digits/%de.wav", jalali_day % 10); + } + else + say_file("digits/%de.wav", jalali_day); + } + + if (say_month) { + say_file("time/mon-%d.wav", jalali_month - 1); + } + + if (say_year) { + say_num(jalali_year, SSM_PRONOUNCED); + } + + if (say_time) { + int32_t hour = tm.tm_hour, pm = 0; + + if (say_date || say_today || say_yesterday || say_dow) { + say_file("time/at.wav"); + } + + if (hour > 12) { + hour -= 12; + pm = 1; + } else if (hour == 12) { + pm = 1; + } else if (hour == 0) { + hour = 12; + pm = 0; + } + + say_file("time/hour-e.wav"); + say_file("digits/%do.wav",hour); + play_group(SSM_PRONOUNCED, 0, (tm.tm_min - tm.tm_min % 10) / 10, tm.tm_min % 10, "time/minutes-e.wav", session, args); + say_file("time/%s.wav", pm ? "p-m" : "a-m"); + } + + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t fa_say_money(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) +{ + char sbuf[16] = ""; /* enough for 999,999,999,999.99 (w/o the commas or leading $) */ + char *rials = NULL; + char *cents = NULL; + + if (strlen(tosay) > 15 || !(tosay = switch_strip_nonnumerics(tosay, sbuf, sizeof(sbuf)))) { + /* valid characters are 0 - 9, period (.), minus (-), and plus (+) - remove all others */ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse Error!\n"); + return SWITCH_STATUS_GENERR; + } + + rials = sbuf; + + if ((cents = strchr(sbuf, '.'))) { + *cents++ = '\0'; + if (strlen(cents) > 2) { + cents[2] = '\0'; + } + } + + /* If positive sign - skip over" */ + if (sbuf[0] == '+') { + say_file("currency/positive-e.wav"); + rials++; + } + + /* If negative say "negative" */ + if (sbuf[0] == '-') { + say_file("currency/negative-e.wav"); + rials++; + } + + /* Say rial amount */ + fa_say_general_count(session, rials, say_args, args); + say_file("currency/rials.wav"); + + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t fa_say_telephone(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) +{ + char *phone_number; + int tosay_length = 0; + + if( !tosay || !strlen(tosay) ) + { + return SWITCH_STATUS_SUCCESS; + } + + if ((phone_number = strchr(tosay,'-'))) + { + *phone_number++ = '\0'; + fa_say_telephone(session,tosay,say_args,args); + fa_say_telephone(session,phone_number,say_args,args); + return SWITCH_STATUS_SUCCESS; + } + + tosay_length = strlen(tosay); + if( tosay_length == 1 ) + { + say_file("digits/%d.wav",tosay[0] - 48); + } + else if ( tosay[0] == '0' ) + { + if( tosay[1] == '0' ) + { + say_file("digits/00.wav"); + fa_say_telephone(session,tosay + 2,say_args,args); + } + else + { + say_file("digits/0.wav"); + fa_say_telephone(session,tosay + 1,say_args,args); + } + } + else if ( tosay_length % 2 ) + { + play_group(SSM_PRONOUNCED,tosay[0] - 48,tosay[1] - 48,tosay[2] - 48,NULL,session,args); + fa_say_telephone(session,tosay + 3,say_args,args); + } + else + { + play_group(SSM_PRONOUNCED,0,tosay[0] - 48,tosay[1] - 48,NULL,session,args); + fa_say_telephone(session,tosay + 2,say_args,args); + } + return SWITCH_STATUS_SUCCESS; +} + +static switch_status_t fa_say(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) +{ + switch_say_callback_t say_cb = NULL; + + switch (say_args->type) { + case SST_NUMBER: + case SST_ITEMS: + case SST_PERSONS: + case SST_MESSAGES: + say_cb = fa_say_general_count; + break; + case SST_TIME_MEASUREMENT: + case SST_CURRENT_DATE: + case SST_CURRENT_TIME: + case SST_CURRENT_DATE_TIME: + case SST_SHORT_DATE_TIME: + say_cb = fa_say_time; + break; + case SST_IP_ADDRESS: + return switch_ivr_say_ip(session, tosay, fa_say_general_count, say_args, args); + break; + case SST_NAME_SPELLED: + case SST_NAME_PHONETIC: + return switch_ivr_say_spell(session, tosay, say_args, args); + break; + case SST_CURRENCY: + say_cb = fa_say_money; + break; + case SST_TELEPHONE_NUMBER: + say_cb = fa_say_telephone; + break; + default: + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown Say type=[%d]\n", say_args->type); + break; + } + + if (say_cb) { + return say_cb(session, tosay, say_args, args); + } + + return SWITCH_STATUS_FALSE; +} + +SWITCH_MODULE_LOAD_FUNCTION(mod_say_fa_load) +{ + switch_say_interface_t *say_interface; + /* connect my internal structure to the blank pointer passed to me */ + *module_interface = switch_loadable_module_create_module_interface(pool, modname); + say_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_SAY_INTERFACE); + say_interface->interface_name = "fa"; + say_interface->say_function = fa_say; + + /* indicate that the module should continue to be loaded */ + return SWITCH_STATUS_SUCCESS; +} + +/* Utility functions */ + +int g_days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; +int j_days_in_month[12] = {31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29}; + +void gregorian_to_jalali(int *j_y, int *j_m, int *j_d, + int g_y, int g_m, int g_d) +{ + int gy, gm, gd; + int jy, jm, jd; + long g_day_no, j_day_no; + int j_np; + + int i; + + gy = g_y-1600; + gm = g_m-1; + gd = g_d-1; + + g_day_no = 365*gy+(gy+3)/4-(gy+99)/100+(gy+399)/400; + for (i=0;i1 && ((gy%4==0 && gy%100!=0) || (gy%400==0))) + /* leap and after Feb */ + ++g_day_no; + g_day_no += gd; + + j_day_no = g_day_no-79; + + j_np = j_day_no / 12053; + j_day_no %= 12053; + + jy = 979+33*j_np+4*(j_day_no/1461); + j_day_no %= 1461; + + if (j_day_no >= 366) { + jy += (j_day_no-1)/365; + j_day_no = (j_day_no-1)%365; + } + + for (i = 0; i < 11 && j_day_no >= j_days_in_month[i]; ++i) { + j_day_no -= j_days_in_month[i]; + } + jm = i+1; + jd = j_day_no+1; + *j_y = jy; + *j_m = jm; + *j_d = jd; +} + +void jalali_to_gregorian(int *g_y, int *g_m, int *g_d, + int j_y, int j_m, int j_d) +{ + int gy, gm, gd; + int jy, jm, jd; + long g_day_no, j_day_no; + int leap; + + int i; + + jy = j_y-979; + jm = j_m-1; + jd = j_d-1; + + j_day_no = 365*jy + (jy/33)*8 + (jy%33+3)/4; + for (i=0; i < jm; ++i) + j_day_no += j_days_in_month[i]; + + j_day_no += jd; + + g_day_no = j_day_no+79; + + gy = 1600 + 400*(g_day_no/146097); /* 146097 = 365*400 + 400/4 - 400/100 + 400/400 */ + g_day_no = g_day_no % 146097; + + leap = 1; + if (g_day_no >= 36525) /* 36525 = 365*100 + 100/4 */ + { + g_day_no--; + gy += 100*(g_day_no/36524); /* 36524 = 365*100 + 100/4 - 100/100 */ + g_day_no = g_day_no % 36524; + + if (g_day_no >= 365) + g_day_no++; + else + leap = 0; + } + + gy += 4*(g_day_no/1461); /* 1461 = 365*4 + 4/4 */ + g_day_no %= 1461; + + if (g_day_no >= 366) { + leap = 0; + + g_day_no--; + gy += g_day_no/365; + g_day_no = g_day_no % 365; + } + + for (i = 0; g_day_no >= g_days_in_month[i] + (i == 1 && leap); i++) + g_day_no -= g_days_in_month[i] + (i == 1 && leap); + gm = i+1; + gd = g_day_no+1; + + *g_y = gy; + *g_m = gm; + *g_d = gd; +} + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4: + */ \ No newline at end of file diff --git a/src/mod/say/mod_say_fa/mod_say_fa.vcproj b/src/mod/say/mod_say_fa/mod_say_fa.vcproj new file mode 100644 index 0000000000..02de50a6fa --- /dev/null +++ b/src/mod/say/mod_say_fa/mod_say_fa.vcproj @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mod/timers/mod_posix_timer/mod_posix_timer.c b/src/mod/timers/mod_posix_timer/mod_posix_timer.c index 04d694b5f7..cfb6584701 100644 --- a/src/mod/timers/mod_posix_timer/mod_posix_timer.c +++ b/src/mod/timers/mod_posix_timer/mod_posix_timer.c @@ -22,9 +22,11 @@ * the Initial Developer. All Rights Reserved. * * Contributor(s): - * Chris Rienzo + * Christopher M. Rienzo * Timo Teräs (based on mod_timerfd.c) * + * Maintainer: Christopher M. Rienzo + * * mod_posix_timer.c -- soft timer implemented with POSIX timers (timer_create/timer_settime/timer_getoverrun) * */ diff --git a/src/mod/xml_int/mod_xml_scgi/Makefile b/src/mod/xml_int/mod_xml_scgi/Makefile new file mode 100644 index 0000000000..2c7a91d26d --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/Makefile @@ -0,0 +1,5 @@ +BASE=../../../.. +LOCAL_SOURCES = $(BASE)/libs/libscgi/src/scgi.c +LOCAL_OBJS = $(BASE)/libs/libscgi/src/scgi.o +LOCAL_CFLAGS = -I. -I$(BASE)/libs/libscgi/src/include +include $(BASE)/build/modmake.rules diff --git a/src/mod/xml_int/mod_xml_scgi/conf/autoload_configs/xml_scgi.conf.xml b/src/mod/xml_int/mod_xml_scgi/conf/autoload_configs/xml_scgi.conf.xml new file mode 100644 index 0000000000..b9662d1638 --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/conf/autoload_configs/xml_scgi.conf.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2008.vcproj b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2008.vcproj new file mode 100644 index 0000000000..7718059dd5 --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2008.vcproj @@ -0,0 +1,287 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2010.vcxproj b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2010.vcxproj new file mode 100644 index 0000000000..cde0e239e9 --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.2010.vcxproj @@ -0,0 +1,131 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + mod_xml_scgi + {11C9BC3D-45E9-46E3-BE84-B8CEE4685E39} + mod_xml_scgi + Win32Proj + + + + DynamicLibrary + MultiByte + + + DynamicLibrary + MultiByte + + + DynamicLibrary + MultiByte + + + DynamicLibrary + MultiByte + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + + + + + + + + false + + + + + + + X64 + + + + + + + false + + + MachineX64 + + + + + + + + + false + + + + + + + X64 + + + + + + + false + + + MachineX64 + + + + + + + + {202d7a4e-760d-4d0e-afa1-d7459ced30ff} + false + + + + + + diff --git a/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.c b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.c new file mode 100644 index 0000000000..1ac74ebbdf --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/mod_xml_scgi.c @@ -0,0 +1,455 @@ +/* + * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2005-2012, Anthony Minessale II + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Anthony Minessale II + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Anthony Minessale II + * + * mod_xml_scgi.c -- SCGI XML Gateway + * + */ +#include +#include + + +SWITCH_MODULE_LOAD_FUNCTION(mod_xml_scgi_load); +SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_scgi_shutdown); +SWITCH_MODULE_DEFINITION(mod_xml_scgi, mod_xml_scgi_load, mod_xml_scgi_shutdown, NULL); + + +struct xml_binding { + char *host; + switch_port_t port; + char *url; + + int timeout; + switch_hash_t *vars_map; + char *bindings; + + char *server; + switch_thread_t *thread; + struct xml_binding *next; +}; + +static int GLOBAL_DEBUG = 0; + +typedef struct xml_binding xml_binding_t; + +#define XML_SCGI_MAX_BYTES 1024 * 1024 + +typedef struct hash_node { + switch_hash_t *hash; + struct hash_node *next; +} hash_node_t; + +static struct { + switch_memory_pool_t *pool; + hash_node_t *hash_root; + hash_node_t *hash_tail; + int running; + xml_binding_t *bindings; +} globals; + +#define XML_SCGI_SYNTAX "[debug_on|debug_off]" +SWITCH_STANDARD_API(xml_scgi_function) +{ + if (session) { + return SWITCH_STATUS_FALSE; + } + + if (zstr(cmd)) { + goto usage; + } + + if (!strcasecmp(cmd, "debug_on")) { + GLOBAL_DEBUG = 1; + } else if (!strcasecmp(cmd, "debug_off")) { + GLOBAL_DEBUG = 0; + } else { + goto usage; + } + + stream->write_function(stream, "OK\n"); + return SWITCH_STATUS_SUCCESS; + + usage: + stream->write_function(stream, "USAGE: %s\n", XML_SCGI_SYNTAX); + return SWITCH_STATUS_SUCCESS; +} + +void *SWITCH_THREAD_FUNC monitor_thread_run(switch_thread_t *thread, void *obj) +{ + xml_binding_t *binding = (xml_binding_t *) obj; + time_t st; + int diff; + + while(globals.running) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Running server command: %s\n", binding->server); + st = switch_epoch_time_now(NULL); + switch_system(binding->server, SWITCH_TRUE); + diff = (int) switch_epoch_time_now(NULL) - st; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Server command complete: %s\n", binding->server); + + if (globals.running && diff < 5) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Server command had short run duration, sleeping: %s\n", binding->server); + switch_yield(10000000); + } + } + + return NULL; +} + +static void launch_monitor_thread(xml_binding_t *binding) +{ + switch_threadattr_t *thd_attr = NULL; + + switch_threadattr_create(&thd_attr, globals.pool); + switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); + switch_threadattr_priority_increase(thd_attr); + switch_thread_create(&binding->thread, thd_attr, monitor_thread_run, binding, globals.pool); +} + + +static switch_xml_t xml_url_fetch(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params, + void *user_data) +{ + switch_xml_t xml = NULL; + char *data = NULL; + xml_binding_t *binding = (xml_binding_t *) user_data; + char hostname[256] = ""; + char basic_data[512]; + unsigned char buf[16336] = ""; + ssize_t len = -1, bytes = 0; + scgi_handle_t handle = { 0 }; + switch_stream_handle_t stream = { 0 }; + char *txt = NULL; + + strncpy(hostname, switch_core_get_switchname(), sizeof(hostname)); + + if (!binding) { + return NULL; + } + + switch_snprintf(basic_data, sizeof(basic_data), "hostname=%s§ion=%s&tag_name=%s&key_name=%s&key_value=%s", + hostname, section, switch_str_nil(tag_name), switch_str_nil(key_name), switch_str_nil(key_value)); + + data = switch_event_build_param_string(params, basic_data, binding->vars_map); + switch_assert(data); + + scgi_add_param(&handle, "REQUEST_METHOD", "POST"); + scgi_add_param(&handle, "REQUEST_URI", binding->url); + scgi_add_body(&handle, data); + + if (scgi_connect(&handle, binding->host, binding->port, binding->timeout * 1000) == SCGI_SUCCESS) { + scgi_send_request(&handle); + + SWITCH_STANDARD_STREAM(stream); + txt = (char *) stream.data; + + while((len = scgi_recv(&handle, buf, sizeof(buf))) > 0) { + char *expanded = switch_event_expand_headers(params, (char *)buf); + + bytes += len; + + if (bytes > XML_SCGI_MAX_BYTES) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Data too big!\n"); + len = -1; + break; + } + + stream.write_function(&stream, "%s", expanded); + txt = (char *) stream.data; + + if (expanded != (char *)buf) { + free(expanded); + } + + memset(buf, 0, sizeof(buf)); + } + + scgi_disconnect(&handle); + + if (len < 0 && (!txt || !strlen(txt))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DEBUG:\nURL: %s Connection Read Failed: [%s]\n", binding->url, handle.err); + goto end; + } + + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DEBUG:\nURL: %s Connection Failed: [%s]\n", binding->url, handle.err); + goto end; + } + + + + if (GLOBAL_DEBUG) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DEBUG:\nURL: %s\nPOST_DATA:\n%s\n\nRESPONSE:\n-----\n%s\n-----\n", + binding->url, data, switch_str_nil(txt)); + } + + + + if (bytes && txt) { + if (!(xml = switch_xml_parse_str_dynamic(txt, FALSE))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing Result! [%s]\ndata: [%s] RESPONSE[%s]\n", + binding->url, data, switch_str_nil(txt)); + } + txt = NULL; + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Received error trying to fetch %s\ndata: [%s] RESPONSE [%s]\n", + binding->url, data, switch_str_nil(txt)); + } + + + end: + + switch_safe_free(data); + switch_safe_free(txt); + + return xml; +} + +#define ENABLE_PARAM_VALUE "enabled" +static switch_status_t do_config(void) +{ + char *cf = "xml_scgi.conf"; + switch_xml_t cfg, xml, bindings_tag, binding_tag, param; + xml_binding_t *binding = NULL; + int x = 0; + int need_vars_map = 0; + switch_hash_t *vars_map = NULL; + + if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf); + return SWITCH_STATUS_TERM; + } + + if (!(bindings_tag = switch_xml_child(cfg, "bindings"))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing tag!\n"); + goto done; + } + + for (binding_tag = switch_xml_child(bindings_tag, "binding"); binding_tag; binding_tag = binding_tag->next) { + char *bname = (char *) switch_xml_attr_soft(binding_tag, "name"); + char *host = "127.0.0.1"; + char *port = "8080"; + char *bind_mask = NULL; + int timeout = 0; + char *server = NULL; + + hash_node_t *hash_node; + need_vars_map = 0; + vars_map = NULL; + + for (param = switch_xml_child(binding_tag, "param"); param; param = param->next) { + char *var = (char *) switch_xml_attr_soft(param, "name"); + char *val = (char *) switch_xml_attr_soft(param, "value"); + + if (!strcasecmp(var, "host")) { + bind_mask = (char *) switch_xml_attr_soft(param, "bindings"); + if (val) { + host = val; + } + } else if (!strcasecmp(var, "port")) { + port = val; + } else if (!strcasecmp(var, "timeout")) { + int tmp = atoi(val); + if (tmp >= 0) { + timeout = tmp; + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't set a negative timeout!\n"); + } + } else if (!strcasecmp(var, "enable-post-var")) { + if (!vars_map && need_vars_map == 0) { + if (switch_core_hash_init(&vars_map, globals.pool) != SWITCH_STATUS_SUCCESS) { + need_vars_map = -1; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't init params hash!\n"); + continue; + } + need_vars_map = 1; + } + + if (vars_map && val) { + if (switch_core_hash_insert(vars_map, val, ENABLE_PARAM_VALUE) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't add %s to params hash!\n", val); + } + } + } else if (!strcasecmp(var, "server")) { + server = val; + } + } + + if (!host) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Binding has no host!\n"); + if (vars_map) { + switch_core_hash_destroy(&vars_map); + } + continue; + } + + if (!(binding = switch_core_alloc(globals.pool, sizeof(*binding)))) { + if (vars_map) { + switch_core_hash_destroy(&vars_map); + } + goto done; + } + memset(binding, 0, sizeof(*binding)); + + binding->timeout = timeout; + binding->host = switch_core_strdup(globals.pool, host); + binding->port = atoi(port); + binding->vars_map = vars_map; + binding->url = switch_mprintf("scgi://%s:%s/%s", host, port, bname); + + if (server) { + binding->server = switch_core_strdup(globals.pool, server); + } + + if (bind_mask) { + binding->bindings = switch_core_strdup(globals.pool, bind_mask); + } + + if (vars_map) { + switch_zmalloc(hash_node, sizeof(hash_node_t)); + hash_node->hash = vars_map; + hash_node->next = NULL; + + if (!globals.hash_root) { + globals.hash_root = hash_node; + globals.hash_tail = globals.hash_root; + } + + else { + globals.hash_tail->next = hash_node; + globals.hash_tail = globals.hash_tail->next; + } + + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Binding [%s] XML Fetch Function [%s] [%s]\n", + zstr(bname) ? "N/A" : bname, binding->url, binding->bindings ? binding->bindings : "all"); + switch_xml_bind_search_function(xml_url_fetch, switch_xml_parse_section_string(binding->bindings), binding); + + if (binding->server) { + launch_monitor_thread(binding); + } + + binding->next = globals.bindings; + globals.bindings = binding; + + + x++; + binding = NULL; + } + + done: + switch_xml_free(xml); + + return x ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; +} + +SWITCH_MODULE_LOAD_FUNCTION(mod_xml_scgi_load) +{ + switch_api_interface_t *xml_scgi_api_interface; + + /* connect my internal structure to the blank pointer passed to me */ + *module_interface = switch_loadable_module_create_module_interface(pool, modname); + + memset(&globals, 0, sizeof(globals)); + globals.running = 1; + globals.pool = pool; + globals.hash_root = NULL; + globals.hash_tail = NULL; + + if (do_config() != SWITCH_STATUS_SUCCESS) { + return SWITCH_STATUS_FALSE; + } + + SWITCH_ADD_API(xml_scgi_api_interface, "xml_scgi", "XML SCGI", xml_scgi_function, XML_SCGI_SYNTAX); + switch_console_set_complete("add xml_scgi debug_on"); + switch_console_set_complete("add xml_scgi debug_off"); + + /* indicate that the module should continue to be loaded */ + return SWITCH_STATUS_SUCCESS; +} + +SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_scgi_shutdown) +{ + hash_node_t *ptr = NULL; + xml_binding_t *bp; + + globals.running = 0; + + for(bp = globals.bindings; bp; bp = bp->next) { + if (bp->thread) { + switch_status_t st; + scgi_handle_t handle = { 0 }; + unsigned char buf[16336] = ""; + int x = 3; + + scgi_add_param(&handle, "REQUEST_METHOD", "POST"); + scgi_add_param(&handle, "REQUEST_URI", bp->url); + scgi_add_body(&handle, "SHUTDOWN"); + + while(x--) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Sending shutdown message to server for %s\n", bp->url); + + if (scgi_connect(&handle, bp->host, bp->port, bp->timeout * 1000) == SCGI_SUCCESS) { + while(0 && scgi_recv(&handle, buf, sizeof(buf)) > 0) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s\n", (char *) buf); + memset(buf, 0, sizeof(buf)); + } + break; + } + + switch_yield(5000000); + } + + scgi_disconnect(&handle); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Waiting for server to stop.\n"); + switch_thread_join(&st, bp->thread); + } + } + + + while (globals.hash_root) { + ptr = globals.hash_root; + switch_core_hash_destroy(&ptr->hash); + globals.hash_root = ptr->next; + switch_safe_free(ptr); + } + + switch_xml_unbind_search_function_ptr(xml_url_fetch); + + return SWITCH_STATUS_SUCCESS; +} + +/* For Emacs: + * Local Variables: + * mode:c + * indent-tabs-mode:t + * tab-width:4 + * c-basic-offset:4 + * End: + * For VIM: + * vim:set softtabstop=4 shiftwidth=4 tabstop=4: + */ diff --git a/src/mod/xml_int/mod_xml_scgi/xml_scgi_server.pl b/src/mod/xml_int/mod_xml_scgi/xml_scgi_server.pl new file mode 100644 index 0000000000..0f4af275bc --- /dev/null +++ b/src/mod/xml_int/mod_xml_scgi/xml_scgi_server.pl @@ -0,0 +1,103 @@ +# +# Copyright (c) 2012-2013, Anthony Minessale II +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the name of the original author; nor the names of any contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER +# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +### EXAMPLE SERVER SIDE FOR mod_xml_SCGI +### You will need the SCGI module from CPAN + +use SCGI; +use CGI; +use IO::Socket; +use Data::Dumper; + +my $socket = IO::Socket::INET->new(Listen => 5, ReuseAddr => 1, LocalPort => 8080) + or die "cannot bind to port 8080: $!"; + +my $scgi = SCGI->new($socket, blocking => 1); + + +my $xml = qq# + +
+ + + + + + + + + +
+
+#; + +$SIG{CHLD} = "IGNORE"; + +while (my $request = $scgi->accept) { + # fork every new req into its own process (optional) + my $pid = fork(); + + if ($pid) { + $request->close(); + next; + } + + my $handle = $request->connection; + + $request->read_env; + + # get the body that contains the PARAMS + read $handle, $body, $request->env->{CONTENT_LENGTH}; + + # Dump SCGI HEADERS + print Dumper $request->env; + + # Create a CGI parser on the PARAMS + my $cgi = CGI->new($body); + my %params = $cgi->Vars(); + # might be big output + print Dumper \%params; + + + ### DO something CGI-like here with %params ... I'm just going to return static xml + + # header is not necessary but optional + #print $handle "Content-Type: text/xml\n\n"; + + print $handle $xml; + + exit unless $pid; +} + + + diff --git a/src/switch.c b/src/switch.c index a3f8759d8c..95b73a7a04 100644 --- a/src/switch.c +++ b/src/switch.c @@ -48,7 +48,7 @@ #include "private/switch_core_pvt.h" /* pid filename: Stores the process id of the freeswitch process */ -#define PIDFILE "freeswitch.pid" +#define PIDFILE "nsg.pid" static char *pfile = PIDFILE; static int system_ready = 0; @@ -288,7 +288,7 @@ void daemonize(int do_wait) while(--sanity && !system_ready) { if (sanity % 2 == 0) { - printf("FreeSWITCH[%d] Waiting for background process pid:%d to be ready.....\n", getpid(), (int) pid); + printf("FreeSWITCH[%d] Waiting for background process pid:%d to be ready.....\n", (int)getpid(), (int) pid); } sleep(1); } @@ -424,6 +424,7 @@ int main(int argc, char *argv[]) "\t-ncwait -- do not output to a console and background but wait until the system is ready before exiting (implies -nc)\n" #endif "\t-c -- output to a console and stay in the foreground\n" + "\t-base [basedir] -- specify an alternate base dir\n" "\t-conf [confdir] -- specify an alternate config dir\n" "\t-log [logdir] -- specify an alternate log dir\n" "\t-run [rundir] -- specify an alternate run dir\n" @@ -542,7 +543,7 @@ int main(int argc, char *argv[]) } if (local_argv[x] && !strcmp(local_argv[x], "-version")) { - fprintf(stdout, "FreeSWITCH version: %s\n", SWITCH_VERSION_FULL); + fprintf(stdout, "Netborder SS7 Version: %s\n", SWITCH_VERSION_FULL); return 0; known_opt++; } @@ -673,6 +674,22 @@ int main(int argc, char *argv[]) known_opt++; } + if (local_argv[x] && !strcmp(local_argv[x], "-base")) { + x++; + if (local_argv[x] && strlen(local_argv[x])) { + SWITCH_GLOBAL_dirs.base_dir = (char *) malloc(strlen(local_argv[x]) + 1); + if (!SWITCH_GLOBAL_dirs.base_dir) { + fprintf(stderr, "Allocation error\n"); + return 255; + } + strcpy(SWITCH_GLOBAL_dirs.base_dir, local_argv[x]); + } else { + fprintf(stderr, "When using -base you must specify a base directory\n"); + return 255; + } + known_opt++; + } + if (local_argv[x] && !strcmp(local_argv[x], "-log")) { x++; if (local_argv[x] && strlen(local_argv[x])) { @@ -896,8 +913,17 @@ int main(int argc, char *argv[]) apr_pool_create(&pool, NULL); + switch_filepath_set(SWITCH_GLOBAL_dirs.base_dir, pool); + switch_dir_make_recursive(SWITCH_GLOBAL_dirs.run_dir, SWITCH_DEFAULT_DIR_PERMS, pool); + if (switch_core_init_and_modload(flags, nc ? SWITCH_FALSE : SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { + fprintf(stderr, "Failed to initialize modules: %s\n", err); + /* 65 is EX_DATAERR (see sysexits.h), meaning some input from the user failed, some init scripts use + * this to tell when fs fails to start due to configuration error */ + return 65; + } + if (switch_file_open(&fd, pid_path, SWITCH_FOPEN_READ, SWITCH_FPROT_UREAD | SWITCH_FPROT_UWRITE, pool) == SWITCH_STATUS_SUCCESS) { old_pid_len = sizeof(old_pid_buffer); @@ -924,11 +950,6 @@ int main(int argc, char *argv[]) switch_file_write(fd, pid_buffer, &pid_len); - if (switch_core_init_and_modload(flags, nc ? SWITCH_FALSE : SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { - fprintf(stderr, "Cannot Initialize [%s]\n", err); - return 255; - } - #ifndef WIN32 if (do_wait) { kill(getppid(), SIGUSR2); diff --git a/src/switch_apr.c b/src/switch_apr.c index 426bed59f5..795c177049 100644 --- a/src/switch_apr.c +++ b/src/switch_apr.c @@ -408,6 +408,11 @@ SWITCH_DECLARE(switch_status_t) switch_file_open(switch_file_t ** newf, const ch return apr_file_open(newf, fname, flag, perm, pool); } +SWITCH_DECLARE(switch_status_t) switch_filepath_set(const char *path, switch_memory_pool_t *pool) +{ + return apr_filepath_set(path, pool); +} + SWITCH_DECLARE(switch_status_t) switch_file_seek(switch_file_t *thefile, switch_seek_where_t where, int64_t *offset) { apr_status_t rv; @@ -422,6 +427,19 @@ SWITCH_DECLARE(switch_status_t) switch_file_copy(const char *from_path, const ch return apr_file_copy(from_path, to_path, perms, pool); } +SWITCH_DECLARE(switch_status_t) switch_file_stat(switch_finfo_t *finfo, const char *fname, int32_t wanted, switch_memory_pool_t *pool) +{ + apr_status_t status; + apr_finfo_t aprinfo; + if (sizeof(*finfo) != sizeof(aprinfo)) { + fprintf(stderr, "Error:structure file mismatch switch_finfo_t:%zd apr_finfo_t:%zd\n", sizeof(*finfo), sizeof(aprinfo)); + return SWITCH_STATUS_MEMERR; + } + status = apr_stat(&aprinfo, fname, wanted, pool); + memcpy(finfo, &aprinfo, sizeof(*finfo)); + return status; +} + SWITCH_DECLARE(switch_status_t) switch_file_close(switch_file_t *thefile) { diff --git a/src/switch_channel.c b/src/switch_channel.c index 3c4262b0a9..3b78df8aaf 100644 --- a/src/switch_channel.c +++ b/src/switch_channel.c @@ -831,7 +831,7 @@ SWITCH_DECLARE(const char *) switch_channel_get_variable_partner(switch_channel_ switch_assert(channel != NULL); if (!zstr(varname)) { - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) || (uuid = switch_channel_get_variable(channel, "originate_signal_bond"))) { + if ((uuid = switch_channel_get_partner_uuid(channel))) { switch_core_session_t *session; if ((session = switch_core_session_locate(uuid))) { switch_channel_t *tchannel = switch_core_session_get_channel(session); @@ -896,7 +896,7 @@ SWITCH_DECLARE(void *) switch_channel_get_private_partner(switch_channel_t *chan switch_assert(channel != NULL); - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) || (uuid = switch_channel_get_variable(channel, "originate_signal_bond"))) { + if ((uuid = switch_channel_get_partner_uuid(channel))) { switch_core_session_t *session; if ((session = switch_core_session_locate(uuid))) { val = switch_core_hash_find_locked(channel->private_hash, key, channel->profile_mutex); @@ -1160,6 +1160,38 @@ SWITCH_DECLARE(uint32_t) switch_channel_del_variable_prefix(switch_channel_t *ch return r; } + +SWITCH_DECLARE(void) switch_channel_set_presence_data_vals(switch_channel_t *channel, const char *presence_data_cols) +{ + if (!zstr(presence_data_cols)) { + char *cols[128] = { 0 }; + char header_name[128] = ""; + int col_count = 0, i = 0; + char *data_copy = NULL; + + if (zstr(presence_data_cols)) { + presence_data_cols = switch_channel_get_variable_dup(channel, "presence_data_cols", SWITCH_FALSE, -1); + if (zstr(presence_data_cols)) { + return; + } + } + + data_copy = strdup(presence_data_cols); + + col_count = switch_split(data_copy, ':', cols); + + for (i = 0; i < col_count; i++) { + const char *val = NULL; + switch_snprintf(header_name, sizeof(header_name), "PD-%s", cols[i]); + val = switch_channel_get_variable(channel, cols[i]); + switch_channel_set_profile_var(channel, header_name, val); + } + + switch_safe_free(data_copy); + } +} + + SWITCH_DECLARE(switch_status_t) switch_channel_set_variable_var_check(switch_channel_t *channel, const char *varname, const char *value, switch_bool_t var_check) { @@ -1292,7 +1324,7 @@ SWITCH_DECLARE(switch_status_t) switch_channel_set_variable_partner_var_check(sw switch_assert(channel != NULL); if (!zstr(varname)) { - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) || (uuid = switch_channel_get_variable(channel, "originate_signal_bond"))) { + if ((uuid = switch_channel_get_partner_uuid(channel))) { switch_core_session_t *session; if ((session = switch_core_session_locate(uuid))) { switch_channel_t *tchannel = switch_core_session_get_channel(session); @@ -1325,7 +1357,7 @@ SWITCH_DECLARE(switch_bool_t) switch_channel_set_flag_partner(switch_channel_t * switch_assert(channel != NULL); - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) || (uuid = switch_channel_get_variable(channel, "originate_signal_bond"))) { + if ((uuid = switch_channel_get_partner_uuid(channel))) { switch_core_session_t *session; if ((session = switch_core_session_locate(uuid))) { switch_channel_set_flag(switch_core_session_get_channel(session), flag); @@ -1344,7 +1376,7 @@ SWITCH_DECLARE(uint32_t) switch_channel_test_flag_partner(switch_channel_t *chan switch_assert(channel != NULL); - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) || (uuid = switch_channel_get_variable(channel, "originate_signal_bond"))) { + if ((uuid = switch_channel_get_partner_uuid(channel))) { switch_core_session_t *session; if ((session = switch_core_session_locate(uuid))) { r = switch_channel_test_flag(switch_core_session_get_channel(session), flag); @@ -1361,7 +1393,7 @@ SWITCH_DECLARE(switch_bool_t) switch_channel_clear_flag_partner(switch_channel_t switch_assert(channel != NULL); - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) || (uuid = switch_channel_get_variable(channel, "originate_signal_bond"))) { + if ((uuid = switch_channel_get_partner_uuid(channel))) { switch_core_session_t *session; if ((session = switch_core_session_locate(uuid))) { switch_channel_clear_flag(switch_core_session_get_channel(session), flag); @@ -1383,7 +1415,7 @@ SWITCH_DECLARE(void) switch_channel_wait_for_state(switch_channel_t *channel, sw (other_channel && switch_channel_down_nosig(other_channel)) || switch_channel_down(channel)) { break; } - switch_yield(20000); + switch_cond_next(); } } @@ -1481,7 +1513,7 @@ SWITCH_DECLARE(uint32_t) switch_channel_test_cap_partner(switch_channel_t *chann switch_assert(channel != NULL); - if ((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) || (uuid = switch_channel_get_variable(channel, "originate_signal_bond"))) { + if ((uuid = switch_channel_get_partner_uuid(channel))) { switch_core_session_t *session; if ((session = switch_core_session_locate(uuid))) { r = switch_channel_test_cap(switch_core_session_get_channel(session), cap); @@ -1905,7 +1937,6 @@ SWITCH_DECLARE(switch_channel_state_t) switch_channel_perform_set_running_state( channel->direction == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound"); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Presence-Call-Direction", channel->direction == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound"); - switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-HIT-Dialplan", switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND || switch_channel_test_flag(channel, CF_DIALPLAN) ? "true" : "false"); @@ -2192,6 +2223,7 @@ SWITCH_DECLARE(void) switch_channel_event_set_basic_data(switch_channel_t *chann if ((v = switch_channel_get_variable(channel, "presence_data_cols"))) { switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Presence-Data-Cols", v); + switch_event_add_presence_data_cols(channel, event, "PD-"); } if ((v = switch_channel_get_variable(channel, "call_uuid"))) { @@ -2697,7 +2729,7 @@ SWITCH_DECLARE(void) switch_channel_flip_cid(switch_channel_t *channel) if (switch_event_create(&event, SWITCH_EVENT_CALL_UPDATE) == SWITCH_STATUS_SUCCESS) { - const char *uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); + const char *uuid = switch_channel_get_partner_uuid(channel); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Direction", "RECV"); if (uuid) { @@ -2922,7 +2954,9 @@ SWITCH_DECLARE(switch_status_t) switch_channel_perform_mark_ring_ready_value(swi SWITCH_DECLARE(void) switch_channel_check_zrtp(switch_channel_t *channel) { - if (switch_channel_test_flag(channel, CF_ZRTP_HASH) && !switch_channel_test_flag(channel, CF_ZRTP_PASS)) { + if (!switch_channel_test_flag(channel, CF_ZRTP_PASSTHRU) + && switch_channel_test_flag(channel, CF_ZRTP_PASSTHRU_REQ) + && switch_channel_test_flag(channel, CF_ZRTP_HASH)) { switch_core_session_t *other_session; switch_channel_t *other_channel; int doit = 1; @@ -2930,14 +2964,16 @@ SWITCH_DECLARE(void) switch_channel_check_zrtp(switch_channel_t *channel) if (switch_core_session_get_partner(channel->session, &other_session) == SWITCH_STATUS_SUCCESS) { other_channel = switch_core_session_get_channel(other_session); - if (switch_channel_test_flag(other_channel, CF_ZRTP_HASH) && !switch_channel_test_flag(other_channel, CF_ZRTP_PASS)) { + if (switch_channel_test_flag(other_channel, CF_ZRTP_HASH) && !switch_channel_test_flag(other_channel, CF_ZRTP_PASSTHRU)) { - switch_channel_set_flag(channel, CF_ZRTP_PASS); - switch_channel_set_flag(other_channel, CF_ZRTP_PASS); + switch_channel_set_flag(channel, CF_ZRTP_PASSTHRU); + switch_channel_set_flag(other_channel, CF_ZRTP_PASSTHRU); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(channel->session), SWITCH_LOG_INFO, "%s Activating ZRTP passthru mode.\n", switch_channel_get_name(channel)); + switch_channel_set_variable(channel, "zrtp_passthru_active", "true"); + switch_channel_set_variable(other_channel, "zrtp_passthru_active", "true"); switch_channel_set_variable(channel, "zrtp_secure_media", "false"); switch_channel_set_variable(other_channel, "zrtp_secure_media", "false"); doit = 0; @@ -2947,18 +2983,20 @@ SWITCH_DECLARE(void) switch_channel_check_zrtp(switch_channel_t *channel) } if (doit) { + switch_channel_set_variable(channel, "zrtp_passthru_active", "false"); switch_channel_set_variable(channel, "zrtp_secure_media", "true"); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(channel->session), SWITCH_LOG_INFO, - "%s ZRTP not negotiated on both sides, Activating ZRTP man-in-the-middle mode.\n", switch_channel_get_name(channel)); + "%s ZRTP not negotiated on both sides; disabling ZRTP passthru mode.\n", switch_channel_get_name(channel)); - switch_channel_clear_flag(channel, CF_ZRTP_PASS); + switch_channel_clear_flag(channel, CF_ZRTP_PASSTHRU); switch_channel_clear_flag(channel, CF_ZRTP_HASH); if (switch_core_session_get_partner(channel->session, &other_session) == SWITCH_STATUS_SUCCESS) { other_channel = switch_core_session_get_channel(other_session); + switch_channel_set_variable(other_channel, "zrtp_passthru_active", "false"); switch_channel_set_variable(other_channel, "zrtp_secure_media", "true"); - switch_channel_clear_flag(other_channel, CF_ZRTP_PASS); + switch_channel_clear_flag(other_channel, CF_ZRTP_PASSTHRU); switch_channel_clear_flag(other_channel, CF_ZRTP_HASH); switch_core_session_rwunlock(other_session); @@ -3378,12 +3416,16 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c memset(data, 0, olen); c = data; for (p = indup; p && p < endof_indup && *p; p++) { + int global = 0; vtype = 0; if (*p == '\\') { if (*(p + 1) == '$') { nv = 1; p++; + if (*(p + 1) == '$') { + p++; + } } else if (*(p + 1) == '\'') { p++; continue; @@ -3395,9 +3437,15 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c } if (*p == '$' && !nv) { + + if (*(p + 1) == '$') { + p++; + global++; + } + if (*(p + 1)) { if (*(p + 1) == '{') { - vtype = 1; + vtype = global ? 3 : 1; } else { nv = 1; } @@ -3419,7 +3467,7 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c s++; - if (vtype == 1 && *s == '{') { + if ((vtype == 1 || vtype == 3) && *s == '{') { br = 1; s++; } @@ -3480,7 +3528,7 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c vtype = 2; } - if (vtype == 1) { + if (vtype == 1 || vtype == 3) { char *expanded = NULL; int offset = 0; int ooffset = 0; @@ -3507,7 +3555,7 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c idx = atoi(ptr); } - if ((sub_val = (char *) switch_channel_get_variable_dup(channel, vname, SWITCH_TRUE, idx))) { + if (vtype == 3 || (sub_val = (char *) switch_channel_get_variable_dup(channel, vname, SWITCH_TRUE, idx))) { if (var_list && !switch_event_check_permission_list(var_list, vname)) { sub_val = "INVALID"; } @@ -4101,6 +4149,17 @@ SWITCH_DECLARE(switch_status_t) switch_channel_set_timestamps(switch_channel_t * return status; } +SWITCH_DECLARE(const char *) switch_channel_get_partner_uuid(switch_channel_t *channel) +{ + const char *uuid = NULL; + + if (!(uuid = switch_channel_get_variable_dup(channel, SWITCH_SIGNAL_BOND_VARIABLE, SWITCH_FALSE, -1))) { + uuid = switch_channel_get_variable_dup(channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE, SWITCH_FALSE, -1); + } + + return uuid; +} + /* For Emacs: * Local Variables: * mode:c diff --git a/src/switch_console.c b/src/switch_console.c index 41d308e339..6da8b82cbd 100644 --- a/src/switch_console.c +++ b/src/switch_console.c @@ -346,7 +346,7 @@ SWITCH_DECLARE(switch_status_t) switch_console_execute(char *xcmd, int rec, swit int argc; char *argv[128]; int x; - char *dup = strdup(xcmd); + char *dup = NULL; char *cmd; switch_status_t status = SWITCH_STATUS_FALSE; @@ -357,6 +357,12 @@ SWITCH_DECLARE(switch_status_t) switch_console_execute(char *xcmd, int rec, swit goto end; } + while (xcmd && *xcmd == ' ') { + xcmd++; + } + + dup = strdup(xcmd); + if (!strncasecmp(xcmd, "alias", 5)) { argc = 1; argv[0] = xcmd; @@ -998,7 +1004,7 @@ static unsigned char console_f12key(EditLine * el, int ch) char *prompt(EditLine * e) { if (*prompt_str == '\0') { - switch_snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", switch_core_get_switchname()); + switch_snprintf(prompt_str, sizeof(prompt_str), "nsg@%s> ", switch_core_get_switchname()); } return prompt_str; diff --git a/src/switch_core.c b/src/switch_core.c index f957a7ae5e..17e9b2f12b 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -527,6 +527,9 @@ SWITCH_DECLARE(void) switch_core_set_globals(void) #else char base_dir[1024] = SWITCH_PREFIX_DIR; + if (SWITCH_GLOBAL_dirs.base_dir) { + snprintf(base_dir, sizeof(base_dir), "%s", SWITCH_GLOBAL_dirs.base_dir); + } #endif if (!SWITCH_GLOBAL_dirs.base_dir && (SWITCH_GLOBAL_dirs.base_dir = (char *) malloc(BUFSIZE))) { @@ -1325,6 +1328,35 @@ SWITCH_DECLARE(uint32_t) switch_core_min_dtmf_duration(uint32_t duration) return runtime.min_dtmf_duration; } +SWITCH_DECLARE(switch_status_t) switch_core_thread_set_cpu_affinity(int cpu) +{ + switch_status_t status = SWITCH_STATUS_FALSE; + + if (cpu > -1) { + +#ifdef HAVE_CPU_SET_MACROS + cpu_set_t set; + + CPU_ZERO(&set); + CPU_SET(cpu, &set); + + if (!sched_setaffinity(0, sizeof(set), &set)) { + status = SWITCH_STATUS_SUCCESS; + } + +#else +#if WIN32 + if (SetThreadAffinityMask(GetCurrentThread(), (DWORD_PTR) cpu)) { + status = SWITCH_STATUS_SUCCESS; + } +#endif +#endif + } + + return status; +} + + static void switch_core_set_serial(void) { char buf[13] = ""; @@ -1391,7 +1423,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc gethostname(runtime.hostname, sizeof(runtime.hostname)); runtime.max_db_handles = 50; - runtime.db_handle_timeout = 5000000;; + runtime.db_handle_timeout = 5000000; runtime.runlevel++; runtime.sql_buffer_len = 1024 * 32; @@ -1402,8 +1434,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc switch_set_flag((&runtime.dummy_cng_frame), SFF_CNG); switch_set_flag((&runtime), SCF_AUTO_SCHEMAS); switch_set_flag((&runtime), SCF_CLEAR_SQL); +#ifdef WIN32 switch_set_flag((&runtime), SCF_THREADED_SYSTEM_EXEC); - +#endif switch_set_flag((&runtime), SCF_NO_NEW_SESSIONS); runtime.hard_log_level = SWITCH_LOG_DEBUG; runtime.mailer_app = "sendmail"; @@ -1444,12 +1477,14 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc switch_dir_make_recursive(SWITCH_GLOBAL_dirs.log_dir, SWITCH_DEFAULT_DIR_PERMS, runtime.memory_pool); switch_dir_make_recursive(SWITCH_GLOBAL_dirs.run_dir, SWITCH_DEFAULT_DIR_PERMS, runtime.memory_pool); switch_dir_make_recursive(SWITCH_GLOBAL_dirs.db_dir, SWITCH_DEFAULT_DIR_PERMS, runtime.memory_pool); +#if 0 switch_dir_make_recursive(SWITCH_GLOBAL_dirs.script_dir, SWITCH_DEFAULT_DIR_PERMS, runtime.memory_pool); switch_dir_make_recursive(SWITCH_GLOBAL_dirs.htdocs_dir, SWITCH_DEFAULT_DIR_PERMS, runtime.memory_pool); switch_dir_make_recursive(SWITCH_GLOBAL_dirs.grammar_dir, SWITCH_DEFAULT_DIR_PERMS, runtime.memory_pool); switch_dir_make_recursive(SWITCH_GLOBAL_dirs.recordings_dir, SWITCH_DEFAULT_DIR_PERMS, runtime.memory_pool); switch_dir_make_recursive(SWITCH_GLOBAL_dirs.sounds_dir, SWITCH_DEFAULT_DIR_PERMS, runtime.memory_pool); switch_dir_make_recursive(SWITCH_GLOBAL_dirs.temp_dir, SWITCH_DEFAULT_DIR_PERMS, runtime.memory_pool); +#endif switch_mutex_init(&runtime.uuid_mutex, SWITCH_MUTEX_NESTED, runtime.memory_pool); @@ -1488,6 +1523,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc switch_find_local_ip(guess_ip, sizeof(guess_ip), NULL, AF_INET6); switch_core_set_variable("local_ip_v6", guess_ip); switch_core_set_variable("base_dir", SWITCH_GLOBAL_dirs.base_dir); + switch_core_set_variable("base_logdir", SWITCH_GLOBAL_dirs.log_dir); switch_core_set_variable("recordings_dir", SWITCH_GLOBAL_dirs.recordings_dir); switch_core_set_variable("sound_prefix", SWITCH_GLOBAL_dirs.sounds_dir); switch_core_set_variable("sounds_dir", SWITCH_GLOBAL_dirs.sounds_dir); @@ -1855,7 +1891,7 @@ static void switch_load_core_config(const char *file) SWITCH_DECLARE(const char *) switch_core_banner(void) { - +#if 0 return ("\n" " _____ ______ _____ _____ ____ _ _ \n" " | ___| __ ___ ___/ ___\\ \\ / /_ _|_ _/ ___| | | | \n" @@ -1868,6 +1904,19 @@ SWITCH_DECLARE(const char *) switch_core_banner(void) "* FreeSWITCH (http://www.freeswitch.org) *\n" "* Paypal Donations Appreciated: paypal@freeswitch.org *\n" "* Brought to you by ClueCon http://www.cluecon.com/ *\n" "************************************************************\n" "\n"); +#else + return ( + "************************************************************************\n" + "* .__ __. .______ _______ _______. _______. ______ *\n" + "* | \\ | | | _ \\ | ____| / | / ||____ |*\n" + "* | \\| | | |_) | | |__ | (----` | (----` / / *\n" + "* | . ` | | _ < | __| \\ \\ \\ \\ / / *\n" + "* | |\\ | | |_) | | |____ .----) | .----) | / / *\n" + "* |__| \\__| |______/ |_______| |_______/ |_______/ /_/ *\n" + "* *\n" + "* Powered by: FreeSWITCH (http://www.freeswitch.org) *\n" + "**************************************************************************\n" "\n"); +#endif } @@ -1913,7 +1962,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_init_and_modload(switch_core_flag_t switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, - "\nFreeSWITCH Version %s Started.\nMax Sessions[%u]\nSession Rate[%d]\nSQL [%s]\n", SWITCH_VERSION_FULL, + "\nNSG Version %s Started.\nMax Sessions[%u]\nSession Rate[%d]\nSQL [%s]\n", SWITCH_VERSION_FULL, switch_core_session_limit(0), switch_core_sessions_per_second(0), switch_test_flag((&runtime), SCF_USE_SQL) ? "Enabled" : "Disabled"); diff --git a/src/switch_core_codec.c b/src/switch_core_codec.c index 28439c1b60..e7fa3f3fe7 100644 --- a/src/switch_core_codec.c +++ b/src/switch_core_codec.c @@ -114,6 +114,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_real_read_codec(switch_c changed_read_codec = 1; if (codec->implementation) { session->read_impl = *codec->implementation; + session->real_read_impl = *codec->implementation; } else { memset(&session->read_impl, 0, sizeof(session->read_impl)); } @@ -136,7 +137,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_real_read_codec(switch_c session->read_codec = codec; changed_read_codec = 1; if (codec->implementation) { - session->read_impl = *codec->implementation; + session->read_impl = *codec->implementation; + session->real_read_impl = *codec->implementation; } else { memset(&session->read_impl, 0, sizeof(session->read_impl)); } @@ -206,6 +208,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_read_codec(switch_core_s session->read_codec = session->real_read_codec = codec; if (codec->implementation) { session->read_impl = *codec->implementation; + session->real_read_impl = *codec->implementation; } else { memset(&session->read_impl, 0, sizeof(session->read_impl)); } @@ -319,6 +322,16 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_get_read_impl(switch_core_se return SWITCH_STATUS_FALSE; } +SWITCH_DECLARE(switch_status_t) switch_core_session_get_real_read_impl(switch_core_session_t *session, switch_codec_implementation_t *impp) +{ + if (session->real_read_impl.codec_id) { + *impp = session->real_read_impl; + return SWITCH_STATUS_SUCCESS; + } + + return SWITCH_STATUS_FALSE; +} + SWITCH_DECLARE(switch_status_t) switch_core_session_get_write_impl(switch_core_session_t *session, switch_codec_implementation_t *impp) { if (session->write_impl.codec_id) { diff --git a/src/switch_core_io.c b/src/switch_core_io.c index 020556a490..0a3db343ed 100644 --- a/src/switch_core_io.c +++ b/src/switch_core_io.c @@ -288,7 +288,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi if (switch_test_flag(session, SSF_READ_TRANSCODE) && !need_codec && switch_core_codec_ready(session->read_codec)) { switch_core_session_t *other_session; - const char *uuid = switch_channel_get_variable(switch_core_session_get_channel(session), SWITCH_SIGNAL_BOND_VARIABLE); + const char *uuid = switch_channel_get_partner_uuid(switch_core_session_get_channel(session)); switch_clear_flag(session, SSF_READ_TRANSCODE); if (uuid && (other_session = switch_core_session_locate(uuid))) { @@ -498,21 +498,12 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi continue; } - if (bp->ready && switch_test_flag(bp, SMBF_READ_STREAM)) { - switch_mutex_lock(bp->read_mutex); - switch_buffer_write(bp->raw_read_buffer, read_frame->data, read_frame->datalen); - - if (bp->callback) { - ok = bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_READ); - } - switch_mutex_unlock(bp->read_mutex); - } - if (ok && switch_test_flag(bp, SMBF_READ_REPLACE)) { do_bugs = 0; if (bp->callback) { bp->read_replace_frame_in = read_frame; bp->read_replace_frame_out = read_frame; + bp->read_demux_frame = NULL; if ((ok = bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_READ_REPLACE)) == SWITCH_TRUE) { read_frame = bp->read_replace_frame_out; } @@ -532,6 +523,55 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi } } + if (session->bugs) { + switch_media_bug_t *bp; + switch_bool_t ok = SWITCH_TRUE; + int prune = 0; + switch_thread_rwlock_rdlock(session->bug_rwlock); + + for (bp = session->bugs; bp; bp = bp->next) { + if (switch_channel_test_flag(session->channel, CF_PAUSE_BUGS) && !switch_core_media_bug_test_flag(bp, SMBF_NO_PAUSE)) { + continue; + } + + if (!switch_channel_test_flag(session->channel, CF_ANSWERED) && switch_core_media_bug_test_flag(bp, SMBF_ANSWER_REQ)) { + continue; + } + if (switch_test_flag(bp, SMBF_PRUNE)) { + prune++; + continue; + } + + if (ok && bp->ready && switch_test_flag(bp, SMBF_READ_STREAM)) { + switch_mutex_lock(bp->read_mutex); + if (bp->read_demux_frame) { + uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE]; + int bytes = read_frame->datalen / 2; + + memcpy(data, read_frame->data, read_frame->datalen); + switch_unmerge_sln((int16_t *)data, bytes, bp->read_demux_frame->data, bytes); + switch_buffer_write(bp->raw_read_buffer, data, read_frame->datalen); + } else { + switch_buffer_write(bp->raw_read_buffer, read_frame->data, read_frame->datalen); + } + + if (bp->callback) { + ok = bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_READ); + } + switch_mutex_unlock(bp->read_mutex); + } + + if ((bp->stop_time && bp->stop_time <= switch_epoch_time_now(NULL)) || ok == SWITCH_FALSE) { + switch_set_flag(bp, SMBF_PRUNE); + prune++; + } + } + switch_thread_rwlock_unlock(session->bug_rwlock); + if (prune) { + switch_core_media_bug_prune(session); + } + } + if (do_bugs) { goto done; } @@ -826,7 +866,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess if (switch_test_flag(session, SSF_WRITE_TRANSCODE) && !need_codec && switch_core_codec_ready(session->write_codec)) { switch_core_session_t *other_session; - const char *uuid = switch_channel_get_variable(switch_core_session_get_channel(session), SWITCH_SIGNAL_BOND_VARIABLE); + const char *uuid = switch_channel_get_partner_uuid(switch_core_session_get_channel(session)); if (uuid && (other_session = switch_core_session_locate(uuid))) { switch_set_flag(other_session, SSF_READ_CODEC_RESET); @@ -1429,6 +1469,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_send_dtmf_string(switch_core switch_assert(session != NULL); + if (zstr(dtmf_string)) { + return SWITCH_STATUS_FALSE; + } + if (*dtmf_string == '~') { dtmf_string++; dtmf.flags = 0; @@ -1438,9 +1482,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_send_dtmf_string(switch_core return SWITCH_STATUS_FALSE; } - if (zstr(dtmf_string)) { - return SWITCH_STATUS_FALSE; - } if (strlen(dtmf_string) > 99) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Attempt to send very large dtmf string ignored!\n"); diff --git a/src/switch_core_media_bug.c b/src/switch_core_media_bug.c index f5d14137b7..8b466b111e 100644 --- a/src/switch_core_media_bug.c +++ b/src/switch_core_media_bug.c @@ -72,6 +72,9 @@ SWITCH_DECLARE(uint32_t) switch_core_media_bug_test_flag(switch_media_bug_t *bug SWITCH_DECLARE(uint32_t) switch_core_media_bug_set_flag(switch_media_bug_t *bug, uint32_t flag) { + if ((flag & SMBF_PRUNE)) { + switch_clear_flag(bug, SMBF_LOCK); + } return switch_set_flag(bug, flag); } @@ -105,6 +108,11 @@ SWITCH_DECLARE(void) switch_core_media_bug_set_read_replace_frame(switch_media_b bug->read_replace_frame_out = frame; } +SWITCH_DECLARE(void) switch_core_media_bug_set_read_demux_frame(switch_media_bug_t *bug, switch_frame_t *frame) +{ + bug->read_demux_frame = frame; +} + SWITCH_DECLARE(void *) switch_core_media_bug_get_user_data(switch_media_bug_t *bug) { return bug->user_data; @@ -175,6 +183,18 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *b bytes = read_impl.decoded_bytes_per_packet; +#ifdef TESTINGONLY + if (0 && bug->session->recur_buffer_len) { + frame->datalen = bug->session->recur_buffer_len; + frame->samples = bug->session->recur_buffer_len / sizeof(int16_t); + frame->rate = read_impl.actual_samples_per_second; + frame->codec = NULL; + memcpy(frame->data, bug->session->recur_buffer, bug->session->recur_buffer_len); + return SWITCH_STATUS_SUCCESS; + } +#endif + + if (frame->buflen < bytes) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(switch_core_media_bug_get_session(bug)), SWITCH_LOG_ERROR, "%s frame buffer too small!\n", switch_channel_get_name(bug->session->channel)); @@ -363,6 +383,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_read(switch_media_bug_t *b return SWITCH_STATUS_BREAK; } + memcpy(bug->session->recur_buffer, frame->data, frame->datalen); + bug->session->recur_buffer_len = frame->datalen; + return SWITCH_STATUS_SUCCESS; } @@ -552,6 +575,71 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_transfer_recordings(switch return x ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; } +SWITCH_DECLARE(switch_status_t) switch_core_media_bug_pop(switch_core_session_t *orig_session, const char *function, switch_media_bug_t **pop) +{ + switch_media_bug_t *bp; + + if (orig_session->bugs) { + switch_thread_rwlock_wrlock(orig_session->bug_rwlock); + for (bp = orig_session->bugs; bp; bp = bp->next) { + if (!strcmp(bp->function, function)) { + switch_set_flag(bp, SMBF_LOCK); + break; + } + } + switch_thread_rwlock_unlock(orig_session->bug_rwlock); + + if (bp) { + *pop = bp; + return SWITCH_STATUS_SUCCESS; + } else { + *pop = NULL; + } + } + + return SWITCH_STATUS_FALSE; +} + +SWITCH_DECLARE(uint32_t) switch_core_media_bug_count(switch_core_session_t *orig_session, const char *function) +{ + switch_media_bug_t *bp; + uint32_t x = 0; + + if (orig_session->bugs) { + switch_thread_rwlock_rdlock(orig_session->bug_rwlock); + for (bp = orig_session->bugs; bp; bp = bp->next) { + if (!switch_test_flag(bp, SMBF_PRUNE) && !switch_test_flag(bp, SMBF_LOCK) && !strcmp(bp->function, function)) { + x++; + } + } + switch_thread_rwlock_unlock(orig_session->bug_rwlock); + } + + return x; +} + +SWITCH_DECLARE(switch_status_t) switch_core_media_bug_exec_all(switch_core_session_t *orig_session, + const char *function, switch_media_bug_exec_cb_t cb, void *user_data) +{ + switch_media_bug_t *bp; + int x = 0; + + switch_assert(cb); + + if (orig_session->bugs) { + switch_thread_rwlock_wrlock(orig_session->bug_rwlock); + for (bp = orig_session->bugs; bp; bp = bp->next) { + if (!switch_test_flag(bp, SMBF_PRUNE) && !switch_test_flag(bp, SMBF_LOCK) && !strcmp(bp->function, function)) { + cb(bp, user_data); + x++; + } + } + switch_thread_rwlock_unlock(orig_session->bug_rwlock); + } + + return x ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; +} + SWITCH_DECLARE(switch_status_t) switch_core_media_bug_enumerate(switch_core_session_t *session, switch_stream_handle_t *stream) { switch_media_bug_t *bp; @@ -559,7 +647,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_enumerate(switch_core_sess stream->write_function(stream, "\n"); if (session->bugs) { - switch_thread_rwlock_wrlock(session->bug_rwlock); + switch_thread_rwlock_rdlock(session->bug_rwlock); for (bp = session->bugs; bp; bp = bp->next) { int thread_locked = (bp->thread_id && bp->thread_id == switch_thread_self()); stream->write_function(stream, @@ -579,7 +667,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_enumerate(switch_core_sess return SWITCH_STATUS_SUCCESS; } -SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove_all(switch_core_session_t *session) +SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove_all_function(switch_core_session_t *session, const char *function) { switch_media_bug_t *bp; switch_status_t status = SWITCH_STATUS_FALSE; @@ -587,10 +675,15 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove_all(switch_core_ses if (session->bugs) { switch_thread_rwlock_wrlock(session->bug_rwlock); for (bp = session->bugs; bp; bp = bp->next) { - if (bp->thread_id && bp->thread_id != switch_thread_self()) { + if ((bp->thread_id && bp->thread_id != switch_thread_self()) || switch_test_flag(bp, SMBF_LOCK)) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "BUG is thread locked skipping.\n"); continue; } + + if (!zstr(function) && strcmp(bp->function, function)) { + continue; + } + if (bp->callback) { bp->callback(bp, bp->user_data, SWITCH_ABC_TYPE_CLOSE); @@ -614,7 +707,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_close(switch_media_bug_t * { switch_media_bug_t *bp = *bug; if (bp) { - if (bp->thread_id && bp->thread_id != switch_thread_self()) { + if ((bp->thread_id && bp->thread_id != switch_thread_self()) || switch_test_flag(bp, SMBF_LOCK)) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(switch_core_media_bug_get_session(*bug)), SWITCH_LOG_DEBUG, "BUG is thread locked skipping.\n"); return SWITCH_STATUS_FALSE; } @@ -637,6 +730,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove(switch_core_session { switch_media_bug_t *bp = NULL, *last = NULL; switch_status_t status = SWITCH_STATUS_FALSE; + + if (switch_core_media_bug_test_flag(*bug, SMBF_LOCK)) { + return status; + } switch_thread_rwlock_wrlock(session->bug_rwlock); if (session->bugs) { @@ -699,6 +796,8 @@ SWITCH_DECLARE(uint32_t) switch_core_media_bug_prune(switch_core_session_t *sess switch_thread_rwlock_unlock(session->bug_rwlock); if (bp) { + switch_clear_flag(bp, SMBF_LOCK); + bp->thread_id = 0; switch_core_media_bug_close(&bp); ttl++; goto top; diff --git a/src/switch_core_memory.c b/src/switch_core_memory.c index 3a93632338..38a066b020 100644 --- a/src/switch_core_memory.c +++ b/src/switch_core_memory.c @@ -646,7 +646,7 @@ switch_memory_pool_t *switch_core_memory_init(void) switch_queue_create(&memory_manager.pool_recycle_queue, 50000, memory_manager.memory_pool); switch_threadattr_create(&thd_attr, memory_manager.memory_pool); - switch_threadattr_detach_set(thd_attr, 1); + switch_threadattr_detach_set(thd_attr, 0); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_thread_create(&pool_thread_p, thd_attr, pool_thread, NULL, memory_manager.memory_pool); diff --git a/src/switch_core_session.c b/src/switch_core_session.c index 6551158522..89f055858f 100644 --- a/src/switch_core_session.c +++ b/src/switch_core_session.c @@ -184,7 +184,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_get_partner(switch_core_sess { const char *uuid; - if ((uuid = switch_channel_get_variable(session->channel, SWITCH_SIGNAL_BOND_VARIABLE)) || (uuid = switch_channel_get_variable(session->channel, "originate_signal_bond"))) { + if ((uuid = switch_channel_get_partner_uuid(session->channel))) { if ((*partner = switch_core_session_locate(uuid))) { return SWITCH_STATUS_SUCCESS; } @@ -544,7 +544,7 @@ SWITCH_DECLARE(switch_call_cause_t) switch_core_session_outgoing_channel(switch_ switch_channel_set_variable(peer_channel, SWITCH_ORIGINATOR_VARIABLE, switch_core_session_get_uuid(session)); switch_channel_set_variable(peer_channel, SWITCH_SIGNAL_BOND_VARIABLE, switch_core_session_get_uuid(session)); // Needed by 3PCC proxy so that aleg can find bleg to pass SDP to, when final ACK arrives. - switch_channel_set_variable(channel, "originate_signal_bond", switch_core_session_get_uuid(*new_session)); + switch_channel_set_variable(channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE, switch_core_session_get_uuid(*new_session)); if ((val = switch_channel_get_variable(channel, SWITCH_PROCESS_CDR_VARIABLE))) { switch_channel_set_variable(peer_channel, SWITCH_PROCESS_CDR_VARIABLE, val); @@ -577,6 +577,10 @@ SWITCH_DECLARE(switch_call_cause_t) switch_core_session_outgoing_channel(switch_ } } + if (switch_channel_test_flag(channel, CF_ZRTP_PASSTHRU_REQ)) { + switch_channel_set_flag(peer_channel, CF_ZRTP_PASSTHRU_REQ); + } + if (profile) { if ((cloned_profile = switch_caller_profile_clone(*new_session, profile)) != 0) { switch_channel_set_originator_caller_profile(peer_channel, cloned_profile); @@ -640,6 +644,7 @@ static const char *message_names[] = { "SIGNAL_DATA", "INFO", "AUDIO_DATA", + "BLIND_TRANSFER_RESPONSE", "INVALID" }; @@ -722,6 +727,24 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_perform_receive_message(swit break; } } + + + if (message->message_id == SWITCH_MESSAGE_INDICATE_BRIDGE && + switch_channel_test_flag(session->channel, CF_CONFIRM_BLIND_TRANSFER)) { + switch_core_session_t *other_session; + const char *uuid = switch_channel_get_variable(session->channel, "blind_transfer_uuid"); + + switch_channel_clear_flag(session->channel, CF_CONFIRM_BLIND_TRANSFER); + + if (!zstr(uuid) && (other_session = switch_core_session_locate(uuid))) { + switch_core_session_message_t msg = { 0 }; + msg.message_id = SWITCH_MESSAGE_INDICATE_BLIND_TRANSFER_RESPONSE; + msg.from = __FILE__; + msg.numeric_arg = 1; + switch_core_session_receive_message(other_session, &msg); + switch_core_session_rwunlock(other_session); + } + } } @@ -774,7 +797,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_pass_indication(switch_core_ switch_channel_t *channel = switch_core_session_get_channel(session); switch_status_t status = SWITCH_STATUS_SUCCESS; - if (((uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) || (uuid = switch_channel_get_variable(channel, "originate_signal_bond"))) && (other_session = switch_core_session_locate(uuid))) { + if (((uuid = switch_channel_get_partner_uuid(channel))) && (other_session = switch_core_session_locate(uuid))) { msg.message_id = indication; msg.from = __FILE__; status = switch_core_session_receive_message(other_session, &msg); @@ -1442,9 +1465,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_thread_launch(switch_core_se switch_mutex_lock(session->mutex); if (switch_test_flag(session, SSF_THREAD_RUNNING)) { - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Cannot double-launch thread!\n"); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot double-launch thread!\n"); } else if (switch_test_flag(session, SSF_THREAD_STARTED)) { - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Cannot launch thread again after it has already been run!\n"); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Cannot launch thread again after it has already been run!\n"); } else { switch_set_flag(session, SSF_THREAD_RUNNING); switch_set_flag(session, SSF_THREAD_STARTED); diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c index d6c64758aa..9b40674065 100644 --- a/src/switch_core_sqldb.c +++ b/src/switch_core_sqldb.c @@ -218,7 +218,7 @@ SWITCH_DECLARE(switch_status_t) _switch_core_db_handle(switch_cache_db_handle_t } -#define SQL_CACHE_TIMEOUT 120 +#define SQL_CACHE_TIMEOUT 30 #define SQL_REG_TIMEOUT 15 @@ -1171,7 +1171,7 @@ static void *SWITCH_THREAD_FUNC switch_core_sql_thread(switch_thread_t *thread, static char *parse_presence_data_cols(switch_event_t *event) { - char *cols[25] = { 0 }; + char *cols[128] = { 0 }; int col_count = 0; char *data_copy; switch_stream_handle_t stream = { 0 }; @@ -1713,7 +1713,8 @@ static char create_registrations_sql[] = " network_ip VARCHAR(256),\n" " network_port VARCHAR(256),\n" " network_proto VARCHAR(256),\n" - " hostname VARCHAR(256)\n" + " hostname VARCHAR(256),\n" + " metadata VARCHAR(256)\n" ");\n"; @@ -1843,7 +1844,8 @@ static char basic_calls_sql[] = SWITCH_DECLARE(switch_status_t) switch_core_add_registration(const char *user, const char *realm, const char *token, const char *url, uint32_t expires, - const char *network_ip, const char *network_port, const char *network_proto) + const char *network_ip, const char *network_port, const char *network_proto, + const char *metadata) { char *sql; @@ -1860,19 +1862,35 @@ SWITCH_DECLARE(switch_status_t) switch_core_add_registration(const char *user, c } switch_queue_push(sql_manager.sql_queue[0], sql); - - sql = switch_mprintf("insert into registrations (reg_user,realm,token,url,expires,network_ip,network_port,network_proto,hostname) " - "values ('%q','%q','%q','%q',%ld,'%q','%q','%q','%q')", - switch_str_nil(user), - switch_str_nil(realm), - switch_str_nil(token), - switch_str_nil(url), - expires, - switch_str_nil(network_ip), - switch_str_nil(network_port), - switch_str_nil(network_proto), - switch_core_get_switchname() - ); + + if ( !zstr(metadata) ) { + sql = switch_mprintf("insert into registrations (reg_user,realm,token,url,expires,network_ip,network_port,network_proto,hostname,metadata) " + "values ('%q','%q','%q','%q',%ld,'%q','%q','%q','%q','%q')", + switch_str_nil(user), + switch_str_nil(realm), + switch_str_nil(token), + switch_str_nil(url), + expires, + switch_str_nil(network_ip), + switch_str_nil(network_port), + switch_str_nil(network_proto), + switch_core_get_switchname(), + metadata + ); + } else { + sql = switch_mprintf("insert into registrations (reg_user,realm,token,url,expires,network_ip,network_port,network_proto,hostname) " + "values ('%q','%q','%q','%q',%ld,'%q','%q','%q','%q')", + switch_str_nil(user), + switch_str_nil(realm), + switch_str_nil(token), + switch_str_nil(url), + expires, + switch_str_nil(network_ip), + switch_str_nil(network_port), + switch_str_nil(network_proto), + switch_core_get_switchname() + ); + } switch_queue_push(sql_manager.sql_queue[0], sql); @@ -2007,6 +2025,8 @@ switch_status_t switch_core_sqldb_start(switch_memory_pool_t *pool, switch_bool_ switch_cache_db_test_reactive(dbh, "delete from registrations where reg_user='' or network_proto='tcp' or network_proto='tls'", "DROP TABLE registrations", create_registrations_sql); + switch_cache_db_test_reactive(dbh, "select metadata from registrations", NULL, "ALTER TABLE registrations ADD COLUMN metadata VARCHAR(256)"); + switch (dbh->type) { case SCDB_TYPE_ODBC: diff --git a/src/switch_core_state_machine.c b/src/switch_core_state_machine.c index 7b2e900e7f..e567daa9fb 100644 --- a/src/switch_core_state_machine.c +++ b/src/switch_core_state_machine.c @@ -174,8 +174,11 @@ static void switch_core_standard_on_routing(switch_core_session_t *session) } if (!extension) { - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "No Route, Aborting\n"); - switch_channel_hangup(session->channel, SWITCH_CAUSE_NO_ROUTE_DESTINATION); + + if (switch_ivr_blind_transfer_ack(session, SWITCH_FALSE) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "No Route, Aborting\n"); + switch_channel_hangup(session->channel, SWITCH_CAUSE_NO_ROUTE_DESTINATION); + } } end: @@ -188,6 +191,7 @@ static void switch_core_standard_on_routing(switch_core_session_t *session) static void switch_core_standard_on_execute(switch_core_session_t *session) { switch_caller_extension_t *extension; + const char *uuid; switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%s Standard EXECUTE\n", switch_channel_get_name(session->channel)); @@ -222,6 +226,25 @@ static void switch_core_standard_on_execute(switch_core_session_t *session) } + if (switch_channel_ready(session->channel) && switch_channel_get_state(session->channel) == CS_EXECUTE && + switch_channel_test_flag(session->channel, CF_CONFIRM_BLIND_TRANSFER) && + (uuid = switch_channel_get_variable(session->channel, "blind_transfer_uuid"))) { + switch_core_session_t *other_session; + + if ((other_session = switch_core_session_locate(uuid))) { + switch_core_session_message_t msg = { 0 }; + msg.message_id = SWITCH_MESSAGE_INDICATE_BLIND_TRANSFER_RESPONSE; + msg.from = __FILE__; + msg.numeric_arg = 0; + switch_core_session_receive_message(other_session, &msg); + switch_core_session_rwunlock(other_session); + + switch_channel_set_variable(session->channel, "park_timeout", "10:blind_transfer"); + switch_channel_set_state(session->channel, CS_PARK); + switch_channel_clear_flag(session->channel, CF_CONFIRM_BLIND_TRANSFER); + } + } + if (switch_channel_ready(session->channel) && switch_channel_get_state(session->channel) == CS_EXECUTE) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "%s has executed the last dialplan instruction, hanging up.\n", switch_channel_get_name(session->channel)); diff --git a/src/switch_event.c b/src/switch_event.c index bce57b79ee..defae3586c 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -35,7 +35,7 @@ #include #include //#define SWITCH_EVENT_RECYCLE -#define DISPATCH_QUEUE_LEN 1000 +#define DISPATCH_QUEUE_LEN 100 //#define DEBUG_DISPATCH_QUEUES /*! \brief A node to store binded events */ @@ -74,23 +74,20 @@ static switch_mutex_t *BLOCK = NULL; static switch_mutex_t *POOL_LOCK = NULL; static switch_memory_pool_t *RUNTIME_POOL = NULL; static switch_memory_pool_t *THRUNTIME_POOL = NULL; -#define NUMBER_OF_QUEUES 3 -static switch_thread_t *EVENT_QUEUE_THREADS[NUMBER_OF_QUEUES] = { 0 }; -static switch_queue_t *EVENT_QUEUE[NUMBER_OF_QUEUES] = { 0 }; static switch_thread_t *EVENT_DISPATCH_QUEUE_THREADS[MAX_DISPATCH_VAL] = { 0 }; static uint8_t EVENT_DISPATCH_QUEUE_RUNNING[MAX_DISPATCH_VAL] = { 0 }; -static switch_queue_t *EVENT_DISPATCH_QUEUE[MAX_DISPATCH_VAL] = { 0 }; -static int POOL_COUNT_MAX = SWITCH_CORE_QUEUE_LEN; +static switch_queue_t *EVENT_DISPATCH_QUEUE = NULL; static switch_mutex_t *EVENT_QUEUE_MUTEX = NULL; static switch_hash_t *CUSTOM_HASH = NULL; static int THREAD_COUNT = 0; +static int DISPATCH_THREAD_COUNT = 0; static int SYSTEM_RUNNING = 0; static uint64_t EVENT_SEQUENCE_NR = 0; #ifdef SWITCH_EVENT_RECYCLE static switch_queue_t *EVENT_RECYCLE_QUEUE = NULL; static switch_queue_t *EVENT_HEADER_RECYCLE_QUEUE = NULL; #endif -static void launch_dispatch_threads(uint32_t max, int len, switch_memory_pool_t *pool); +static void launch_dispatch_threads(uint32_t max, switch_memory_pool_t *pool); static char *my_dup(const char *s) { @@ -244,25 +241,17 @@ static void *SWITCH_THREAD_FUNC switch_event_dispatch_thread(switch_thread_t *th switch_mutex_lock(EVENT_QUEUE_MUTEX); THREAD_COUNT++; + DISPATCH_THREAD_COUNT++; - for (my_id = 0; my_id < NUMBER_OF_QUEUES; my_id++) { - if (EVENT_DISPATCH_QUEUE[my_id] == queue) { + for (my_id = 0; my_id < MAX_DISPATCH_VAL; my_id++) { + if (EVENT_DISPATCH_QUEUE_THREADS[my_id] == thread) { break; } } EVENT_DISPATCH_QUEUE_RUNNING[my_id] = 1; switch_mutex_unlock(EVENT_QUEUE_MUTEX); - -#ifdef HAVE_CPU_SET_MACROS - { - cpu_set_t set; - CPU_ZERO(&set); - CPU_SET(my_id, &set); - sched_setaffinity(0, sizeof(set), &set); - } -#endif - + for (;;) { void *pop = NULL; @@ -273,7 +262,7 @@ static void *SWITCH_THREAD_FUNC switch_event_dispatch_thread(switch_thread_t *th } if (switch_queue_pop(queue, &pop) != SWITCH_STATUS_SUCCESS) { - break; + continue; } if (!pop) { @@ -288,6 +277,7 @@ static void *SWITCH_THREAD_FUNC switch_event_dispatch_thread(switch_thread_t *th switch_mutex_lock(EVENT_QUEUE_MUTEX); EVENT_DISPATCH_QUEUE_RUNNING[my_id] = 0; THREAD_COUNT--; + DISPATCH_THREAD_COUNT--; switch_mutex_unlock(EVENT_QUEUE_MUTEX); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Dispatch Thread %d Ended.\n", my_id); @@ -295,74 +285,41 @@ static void *SWITCH_THREAD_FUNC switch_event_dispatch_thread(switch_thread_t *th } - -static void *SWITCH_THREAD_FUNC switch_event_thread(switch_thread_t *thread, void *obj) +static switch_status_t switch_event_queue_dispatch_event(switch_event_t **eventp) { - switch_queue_t *queue = (switch_queue_t *) obj; - uint32_t index = 0; - int my_id = 0; - switch_mutex_lock(EVENT_QUEUE_MUTEX); - THREAD_COUNT++; - switch_mutex_unlock(EVENT_QUEUE_MUTEX); + switch_event_t *event = *eventp; - for (my_id = 0; my_id < NUMBER_OF_QUEUES; my_id++) { - if (EVENT_QUEUE[my_id] == queue) { - break; - } + if (!SYSTEM_RUNNING) { + return SWITCH_STATUS_FALSE; } + + while (event) { + int launch = 0; - for (;;) { - void *pop = NULL; - switch_event_t *event = NULL; + switch_mutex_lock(EVENT_QUEUE_MUTEX); - if (switch_queue_pop(queue, &pop) != SWITCH_STATUS_SUCCESS) { - break; + if (switch_queue_size(EVENT_DISPATCH_QUEUE) > (unsigned int)(DISPATCH_QUEUE_LEN * DISPATCH_THREAD_COUNT)) { + launch++; } - - if (!pop) { - break; - } - - if (!SYSTEM_RUNNING) { - break; - } - - event = (switch_event_t *) pop; - - while (event) { - - for (index = 0; index < SOFT_MAX_DISPATCH; index++) { - if (switch_queue_trypush(EVENT_DISPATCH_QUEUE[index], event) == SWITCH_STATUS_SUCCESS) { - event = NULL; - break; - } - } - - if (event) { - if (SOFT_MAX_DISPATCH + 1 < MAX_DISPATCH) { - switch_mutex_lock(EVENT_QUEUE_MUTEX); - launch_dispatch_threads(SOFT_MAX_DISPATCH + 1, DISPATCH_QUEUE_LEN, RUNTIME_POOL); - switch_mutex_unlock(EVENT_QUEUE_MUTEX); - } else { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Event Thread %d is blocking\n", my_id); - switch_queue_push(EVENT_DISPATCH_QUEUE[0], event); - event = NULL; - } + + if (launch) { + if (SOFT_MAX_DISPATCH + 1 < MAX_DISPATCH) { + launch_dispatch_threads(SOFT_MAX_DISPATCH + 1, RUNTIME_POOL); } } + + switch_mutex_unlock(EVENT_QUEUE_MUTEX); + + *eventp = NULL; + switch_queue_push(EVENT_DISPATCH_QUEUE, event); + event = NULL; + } - - switch_mutex_lock(EVENT_QUEUE_MUTEX); - THREAD_COUNT--; - switch_mutex_unlock(EVENT_QUEUE_MUTEX); - - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Event Thread %d Ended.\n", my_id); - return NULL; - + + return SWITCH_STATUS_SUCCESS; } - SWITCH_DECLARE(void) switch_event_deliver(switch_event_t **event) { switch_event_types_t e; @@ -507,20 +464,23 @@ SWITCH_DECLARE(switch_status_t) switch_event_shutdown(void) SYSTEM_RUNNING = 0; switch_mutex_unlock(EVENT_QUEUE_MUTEX); - for (x = 0; x < 3; x++) { - if (EVENT_QUEUE[x]) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping event queue %d\n", x); - switch_queue_trypush(EVENT_QUEUE[x], NULL); - switch_queue_interrupt_all(EVENT_QUEUE[x]); - } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping dispatch queues\n"); + + + for(x = 0; x < (uint32_t)DISPATCH_THREAD_COUNT; x++) { + switch_queue_trypush(EVENT_DISPATCH_QUEUE, NULL); } - for (x = 0; x < SOFT_MAX_DISPATCH; x++) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping dispatch queue %d\n", x); - switch_queue_trypush(EVENT_DISPATCH_QUEUE[x], NULL); - switch_queue_interrupt_all(EVENT_DISPATCH_QUEUE[x]); + switch_queue_interrupt_all(EVENT_DISPATCH_QUEUE); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping dispatch threads\n"); + + for(x = 0; x < (uint32_t)DISPATCH_THREAD_COUNT; x++) { + switch_status_t st; + switch_thread_join(&st, EVENT_DISPATCH_QUEUE_THREADS[x]); } + x = 0; while (x < 10000 && THREAD_COUNT) { switch_cond_next(); if (THREAD_COUNT == last) { @@ -529,37 +489,16 @@ SWITCH_DECLARE(switch_status_t) switch_event_shutdown(void) last = THREAD_COUNT; } - for (x = 0; x < SOFT_MAX_DISPATCH; x++) { + { void *pop = NULL; switch_event_t *event = NULL; - switch_status_t st; - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping dispatch thread %d\n", x); - switch_thread_join(&st, EVENT_DISPATCH_QUEUE_THREADS[x]); - - while (switch_queue_trypop(EVENT_DISPATCH_QUEUE[x], &pop) == SWITCH_STATUS_SUCCESS && pop) { + while (switch_queue_trypop(EVENT_DISPATCH_QUEUE, &pop) == SWITCH_STATUS_SUCCESS && pop) { event = (switch_event_t *) pop; switch_event_destroy(&event); } } - for (x = 0; x < NUMBER_OF_QUEUES; x++) { - void *pop = NULL; - switch_event_t *event = NULL; - switch_status_t st; - - if (EVENT_QUEUE_THREADS[x]) { - - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping queue thread %d\n", x); - switch_thread_join(&st, EVENT_QUEUE_THREADS[x]); - - while (switch_queue_trypop(EVENT_QUEUE[x], &pop) == SWITCH_STATUS_SUCCESS && pop) { - event = (switch_event_t *) pop; - switch_event_destroy(&event); - } - } - } - for (hi = switch_hash_first(NULL, CUSTOM_HASH); hi; hi = switch_hash_next(hi)) { switch_event_subclass_t *subclass; switch_hash_this(hi, &var, NULL, &val); @@ -576,7 +515,7 @@ SWITCH_DECLARE(switch_status_t) switch_event_shutdown(void) return SWITCH_STATUS_SUCCESS; } -static void launch_dispatch_threads(uint32_t max, int len, switch_memory_pool_t *pool) +static void launch_dispatch_threads(uint32_t max, switch_memory_pool_t *pool) { switch_threadattr_t *thd_attr; uint32_t index = 0; @@ -592,14 +531,14 @@ static void launch_dispatch_threads(uint32_t max, int len, switch_memory_pool_t } for (index = SOFT_MAX_DISPATCH; index < max && index < MAX_DISPATCH; index++) { - if (EVENT_DISPATCH_QUEUE[index]) { + if (EVENT_DISPATCH_QUEUE_THREADS[index]) { continue; } - switch_queue_create(&EVENT_DISPATCH_QUEUE[index], len, pool); + switch_threadattr_create(&thd_attr, pool); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_threadattr_priority_increase(thd_attr); - switch_thread_create(&EVENT_DISPATCH_QUEUE_THREADS[index], thd_attr, switch_event_dispatch_thread, EVENT_DISPATCH_QUEUE[index], pool); + switch_thread_create(&EVENT_DISPATCH_QUEUE_THREADS[index], thd_attr, switch_event_dispatch_thread, EVENT_DISPATCH_QUEUE, pool); while(--sanity && !EVENT_DISPATCH_QUEUE_RUNNING[index]) switch_yield(10000); if (index == 1) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Create event dispatch thread %d\n", index); @@ -624,8 +563,10 @@ SWITCH_DECLARE(switch_status_t) switch_event_init(switch_memory_pool_t *pool) */ /* don't need any more dispatch threads than we have CPU's*/ - MAX_DISPATCH = switch_core_cpu_count() + 1; - + MAX_DISPATCH = (switch_core_cpu_count() / 2) + 1; + if (MAX_DISPATCH < 2) { + MAX_DISPATCH = 2; + } switch_assert(pool != NULL); THRUNTIME_POOL = RUNTIME_POOL = pool; @@ -656,7 +597,9 @@ SWITCH_DECLARE(switch_status_t) switch_event_init(switch_memory_pool_t *pool) //switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); //switch_threadattr_priority_increase(thd_attr); - launch_dispatch_threads(1, DISPATCH_QUEUE_LEN, RUNTIME_POOL); + switch_queue_create(&EVENT_DISPATCH_QUEUE, DISPATCH_QUEUE_LEN * MAX_DISPATCH, pool); + launch_dispatch_threads(1, RUNTIME_POOL); + //switch_thread_create(&EVENT_QUEUE_THREADS[0], thd_attr, switch_event_thread, EVENT_QUEUE[0], RUNTIME_POOL); //switch_thread_create(&EVENT_QUEUE_THREADS[1], thd_attr, switch_event_thread, EVENT_QUEUE[1], RUNTIME_POOL); //switch_thread_create(&EVENT_QUEUE_THREADS[2], thd_attr, switch_event_thread, EVENT_QUEUE[2], RUNTIME_POOL); @@ -1790,30 +1733,11 @@ SWITCH_DECLARE(switch_status_t) switch_event_fire_detailed(const char *file, con (*event)->event_user_data = user_data; } - if (!EVENT_QUEUE_THREADS[(*event)->priority] && (*event)->priority < 3) { - switch_threadattr_t *thd_attr; - - switch_queue_create(&EVENT_QUEUE[(*event)->priority], POOL_COUNT_MAX + 10, THRUNTIME_POOL); - switch_threadattr_create(&thd_attr, THRUNTIME_POOL); - switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); - switch_threadattr_priority_increase(thd_attr); - switch_thread_create(&EVENT_QUEUE_THREADS[(*event)->priority], thd_attr, switch_event_thread, EVENT_QUEUE[(*event)->priority], RUNTIME_POOL); + if (switch_event_queue_dispatch_event(event) != SWITCH_STATUS_SUCCESS) { + switch_event_destroy(event); + return SWITCH_STATUS_FALSE; } - for (;;) { - if (switch_queue_trypush(EVENT_QUEUE[(*event)->priority], *event) == SWITCH_STATUS_SUCCESS) { - goto end; - } - - - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Event queue is full!\n"); - switch_yield(100000); - } - - end: - - *event = NULL; - return SWITCH_STATUS_SUCCESS; } @@ -2033,12 +1957,16 @@ SWITCH_DECLARE(char *) switch_event_expand_headers_check(switch_event_t *event, memset(data, 0, olen); c = data; for (p = indup; p && p < endof_indup && *p; p++) { + int global = 0; vtype = 0; if (*p == '\\') { if (*(p + 1) == '$') { nv = 1; p++; + if (*(p + 1) == '$') { + p++; + } } else if (*(p + 1) == '\'') { p++; continue; @@ -2050,9 +1978,14 @@ SWITCH_DECLARE(char *) switch_event_expand_headers_check(switch_event_t *event, } if (*p == '$' && !nv) { + if (*(p + 1) == '$') { + p++; + global++; + } + if (*(p + 1)) { if (*(p + 1) == '{') { - vtype = 1; + vtype = global ? 3 : 1; } else { nv = 1; } @@ -2074,7 +2007,7 @@ SWITCH_DECLARE(char *) switch_event_expand_headers_check(switch_event_t *event, s++; - if (vtype == 1 && *s == '{') { + if ((vtype == 1 || vtype == 3) && *s == '{') { br = 1; s++; } @@ -2136,7 +2069,7 @@ SWITCH_DECLARE(char *) switch_event_expand_headers_check(switch_event_t *event, vtype = 2; } - if (vtype == 1) { + if (vtype == 1 || vtype == 3) { char *expanded = NULL; int offset = 0; int ooffset = 0; @@ -2162,7 +2095,7 @@ SWITCH_DECLARE(char *) switch_event_expand_headers_check(switch_event_t *event, idx = atoi(ptr); } - if (!(sub_val = switch_event_get_header_idx(event, vname, idx))) { + if (vtype == 3 || !(sub_val = switch_event_get_header_idx(event, vname, idx))) { switch_safe_free(gvar); if ((gvar = switch_core_get_variable_dup(vname))) { sub_val = gvar; @@ -2399,7 +2332,34 @@ SWITCH_DECLARE(int) switch_event_check_permission_list(switch_event_t *list, con return r; } +SWITCH_DECLARE(void) switch_event_add_presence_data_cols(switch_channel_t *channel, switch_event_t *event, const char *prefix) +{ + const char *data; + if (!prefix) prefix = ""; + + if ((data = switch_channel_get_variable(channel, "presence_data_cols"))) { + char *cols[128] = { 0 }; + char header_name[128] = ""; + int col_count = 0, i = 0; + char *data_copy = NULL; + + data_copy = strdup(data); + + col_count = switch_split(data_copy, ':', cols); + + for (i = 0; i < col_count; i++) { + const char *val = NULL; + switch_snprintf(header_name, sizeof(header_name), "%s%s", prefix, cols[i]); + + val = switch_channel_get_variable(channel, cols[i]); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header_name, val); + } + + switch_safe_free(data_copy); + } + +} /* For Emacs: diff --git a/src/switch_ivr.c b/src/switch_ivr.c index 432bb9edc6..08d1fcd5a4 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -170,7 +170,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_sleep(switch_core_session_t *session, SWITCH_IVR_VERIFY_SILENCE_DIVISOR(sval); } - if (ms > 100 && sval) { + if (ms > 10 && sval) { switch_core_session_get_read_impl(session, &imp); if (switch_core_codec_init(&codec, @@ -559,7 +559,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_parse_event(switch_core_session_t *se switch_channel_set_flag(channel, CF_BROADCAST); } if (hold_bleg && switch_true(hold_bleg)) { - if ((b_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE))) { + if ((b_uuid = switch_channel_get_partner_uuid(channel))) { const char *stream; b_uuid = switch_core_session_strdup(session, b_uuid); @@ -1384,7 +1384,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_hold(switch_core_session_t *session, switch_core_session_receive_message(session, &msg); if (moh && (stream = switch_channel_get_hold_music(channel))) { - if ((other_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE))) { + if ((other_uuid = switch_channel_get_partner_uuid(channel))) { switch_ivr_broadcast(other_uuid, stream, SMF_ECHO_ALEG | SMF_LOOP); } } @@ -1421,7 +1421,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_unhold(switch_core_session_t *session switch_core_session_receive_message(session, &msg); - if ((other_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) && (b_session = switch_core_session_locate(other_uuid))) { + if ((other_uuid = switch_channel_get_partner_uuid(channel)) && (b_session = switch_core_session_locate(other_uuid))) { switch_channel_t *b_channel = switch_core_session_get_channel(b_session); switch_channel_stop_broadcast(b_channel); switch_channel_wait_for_flag(b_channel, CF_BROADCAST, SWITCH_FALSE, 5000, NULL); @@ -2472,6 +2472,260 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_generate_xml_cdr(switch_core_session_ return SWITCH_STATUS_FALSE; } +static void switch_ivr_set_json_profile_data(cJSON *json, switch_caller_profile_t *caller_profile) +{ + cJSON_AddItemToObject(json, "username", cJSON_CreateString((char *)caller_profile->username)); + cJSON_AddItemToObject(json, "dialplan", cJSON_CreateString((char *)caller_profile->dialplan)); + cJSON_AddItemToObject(json, "caller_id_name", cJSON_CreateString((char *)caller_profile->caller_id_name)); + cJSON_AddItemToObject(json, "ani", cJSON_CreateString((char *)caller_profile->ani)); + cJSON_AddItemToObject(json, "aniii", cJSON_CreateString((char *)caller_profile->aniii)); + cJSON_AddItemToObject(json, "caller_id_number", cJSON_CreateString((char *)caller_profile->caller_id_number)); + cJSON_AddItemToObject(json, "network_addr", cJSON_CreateString((char *)caller_profile->network_addr)); + cJSON_AddItemToObject(json, "rdnis", cJSON_CreateString((char *)caller_profile->rdnis)); + cJSON_AddItemToObject(json, "destination_number", cJSON_CreateString(caller_profile->destination_number)); + cJSON_AddItemToObject(json, "uuid", cJSON_CreateString(caller_profile->uuid)); + cJSON_AddItemToObject(json, "source", cJSON_CreateString((char *)caller_profile->source)); + cJSON_AddItemToObject(json, "context", cJSON_CreateString((char *)caller_profile->context)); + cJSON_AddItemToObject(json, "chan_name", cJSON_CreateString(caller_profile->chan_name)); +} + +static void switch_ivr_set_json_chan_vars(cJSON *json, switch_channel_t *channel, switch_bool_t urlencode) +{ + switch_event_header_t *hi = switch_channel_variable_first(channel); + + if (!hi) + return; + + for (; hi; hi = hi->next) { + if (!zstr(hi->name) && !zstr(hi->value)) { + char *data = hi->value; + if (urlencode) { + switch_size_t dlen = strlen(hi->value) * 3; + + if ((data = malloc(dlen))) { + memset(data, 0, dlen); + switch_url_encode(hi->value, data, dlen); + } + } + + cJSON_AddItemToObject(json, hi->name, cJSON_CreateString(data)); + + if (data != hi->value) { + switch_safe_free(data); + } + } + } + switch_channel_variable_last(channel); +} + + + +SWITCH_DECLARE(switch_status_t) switch_ivr_generate_json_cdr(switch_core_session_t *session, cJSON **json_cdr, switch_bool_t urlencode) +{ + cJSON *cdr = cJSON_CreateObject(); + switch_channel_t *channel = switch_core_session_get_channel(session); + switch_caller_profile_t *caller_profile; + cJSON *variables, *j_main_cp, *j_caller_profile, *j_caller_extension, *j_times, + *j_application, *j_callflow, *j_inner_extension, *j_apps, *j_o, *j_channel_data; + switch_app_log_t *app_log; + char tmp[512], *f; + + j_channel_data = cJSON_CreateObject(); + + cJSON_AddItemToObject(cdr, "channel_data", j_channel_data); + + cJSON_AddItemToObject(j_channel_data, "state", cJSON_CreateString((char *) switch_channel_state_name(switch_channel_get_state(channel)))); + cJSON_AddItemToObject(j_channel_data, "direction", cJSON_CreateString(switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND ? "outbound" : "inbound")); + + switch_snprintf(tmp, sizeof(tmp), "%d", switch_channel_get_state(channel)); + cJSON_AddItemToObject(j_channel_data, "state_number", cJSON_CreateString((char *) tmp)); + + if ((f = switch_channel_get_flag_string(channel))) { + cJSON_AddItemToObject(j_channel_data, "flags", cJSON_CreateString((char *) f)); + free(f); + } + + if ((f = switch_channel_get_cap_string(channel))) { + cJSON_AddItemToObject(j_channel_data, "caps", cJSON_CreateString((char *) f)); + free(f); + } + + variables = cJSON_CreateObject(); + cJSON_AddItemToObject(cdr, "variables", variables); + + switch_ivr_set_json_chan_vars(variables, channel, urlencode); + + + if ((app_log = switch_core_session_get_app_log(session))) { + switch_app_log_t *ap; + + j_apps = cJSON_CreateObject(); + + cJSON_AddItemToObject(cdr, "app_log", j_apps); + + for (ap = app_log; ap; ap = ap->next) { + j_application = cJSON_CreateObject(); + + cJSON_AddItemToObject(j_application, "app_name", cJSON_CreateString(ap->app)); + cJSON_AddItemToObject(j_application, "app_data", cJSON_CreateString(ap->arg)); + + cJSON_AddItemToObject(j_apps, "application", j_application); + } + } + + + caller_profile = switch_channel_get_caller_profile(channel); + + while (caller_profile) { + + j_callflow = cJSON_CreateObject(); + + cJSON_AddItemToObject(cdr, "callflow", j_callflow); + + if (!zstr(caller_profile->dialplan)) { + cJSON_AddItemToObject(j_callflow, "dialplan", cJSON_CreateString((char *)caller_profile->dialplan)); + } + + if (!zstr(caller_profile->profile_index)) { + cJSON_AddItemToObject(j_callflow, "profile_index", cJSON_CreateString((char *)caller_profile->profile_index)); + } + + if (caller_profile->caller_extension) { + switch_caller_application_t *ap; + + j_caller_extension = cJSON_CreateObject(); + + cJSON_AddItemToObject(j_callflow, "extension", j_caller_extension); + + cJSON_AddItemToObject(j_caller_extension, "name", cJSON_CreateString(caller_profile->caller_extension->extension_name)); + cJSON_AddItemToObject(j_caller_extension, "number", cJSON_CreateString(caller_profile->caller_extension->extension_number)); + + if (caller_profile->caller_extension->current_application) { + cJSON_AddItemToObject(j_caller_extension, "current_app", cJSON_CreateString(caller_profile->caller_extension->current_application->application_name)); + } + + for (ap = caller_profile->caller_extension->applications; ap; ap = ap->next) { + j_application = cJSON_CreateObject(); + + cJSON_AddItemToObject(j_caller_extension, "application", j_application); + + if (ap == caller_profile->caller_extension->current_application) { + cJSON_AddItemToObject(j_application, "last_executed", cJSON_CreateString("true")); + } + cJSON_AddItemToObject(j_application, "app_name", cJSON_CreateString(ap->application_name)); + cJSON_AddItemToObject(j_application, "app_data", cJSON_CreateString(switch_str_nil(ap->application_data))); + } + + if (caller_profile->caller_extension->children) { + switch_caller_profile_t *cp = NULL; + for (cp = caller_profile->caller_extension->children; cp; cp = cp->next) { + + if (!cp->caller_extension) { + continue; + } + + j_inner_extension = cJSON_CreateObject(); + cJSON_AddItemToObject(j_caller_extension, "sub_extensions", j_inner_extension); + + j_caller_extension = cJSON_CreateObject(); + cJSON_AddItemToObject(j_inner_extension, "extension", j_caller_extension); + + cJSON_AddItemToObject(j_caller_extension, "name", cJSON_CreateString(cp->caller_extension->extension_name)); + cJSON_AddItemToObject(j_caller_extension, "number", cJSON_CreateString(cp->caller_extension->extension_number)); + + cJSON_AddItemToObject(j_caller_extension, "dialplan", cJSON_CreateString((char *)cp->dialplan)); + + if (cp->caller_extension->current_application) { + cJSON_AddItemToObject(j_caller_extension, "current_app", cJSON_CreateString(cp->caller_extension->current_application->application_name)); + } + + for (ap = cp->caller_extension->applications; ap; ap = ap->next) { + j_application = cJSON_CreateObject(); + cJSON_AddItemToObject(j_caller_extension, "application", j_application); + + if (ap == cp->caller_extension->current_application) { + cJSON_AddItemToObject(j_application, "last_executed", cJSON_CreateString("true")); + } + cJSON_AddItemToObject(j_application, "app_name", cJSON_CreateString(ap->application_name)); + cJSON_AddItemToObject(j_application, "app_data", cJSON_CreateString(switch_str_nil(ap->application_data))); + } + } + } + } + + j_main_cp = cJSON_CreateObject(); + cJSON_AddItemToObject(j_callflow, "caller_profile", j_main_cp); + + switch_ivr_set_json_profile_data(j_main_cp, caller_profile); + + if (caller_profile->originator_caller_profile) { + switch_caller_profile_t *cp = NULL; + + j_o = cJSON_CreateObject(); + cJSON_AddItemToObject(j_main_cp, "originator", j_o); + + for (cp = caller_profile->originator_caller_profile; cp; cp = cp->next) { + j_caller_profile = cJSON_CreateObject(); + cJSON_AddItemToObject(j_o, "originator_caller_profile", j_caller_profile); + + switch_ivr_set_json_profile_data(j_caller_profile, cp); + } + } + + if (caller_profile->originatee_caller_profile) { + switch_caller_profile_t *cp = NULL; + + j_o = cJSON_CreateObject(); + cJSON_AddItemToObject(j_main_cp, "originatee", j_o); + + for (cp = caller_profile->originatee_caller_profile; cp; cp = cp->next) { + j_caller_profile = cJSON_CreateObject(); + cJSON_AddItemToObject(j_o, "originatee_caller_profile", j_caller_profile); + switch_ivr_set_json_profile_data(j_caller_profile, cp); + } + } + + if (caller_profile->times) { + + j_times = cJSON_CreateObject(); + cJSON_AddItemToObject(j_callflow, "times", j_times); + + switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->created); + cJSON_AddItemToObject(j_times, "created_time", cJSON_CreateString(tmp)); + + switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->profile_created); + cJSON_AddItemToObject(j_times, "profile_created_time", cJSON_CreateString(tmp)); + + switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->progress); + cJSON_AddItemToObject(j_times, "progress_time", cJSON_CreateString(tmp)); + + switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->progress_media); + cJSON_AddItemToObject(j_times, "progress_media_time", cJSON_CreateString(tmp)); + + switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->answered); + cJSON_AddItemToObject(j_times, "answered_time", cJSON_CreateString(tmp)); + + switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->hungup); + cJSON_AddItemToObject(j_times, "hangup_time", cJSON_CreateString(tmp)); + + switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->resurrected); + cJSON_AddItemToObject(j_times, "resurrect_time", cJSON_CreateString(tmp)); + + switch_snprintf(tmp, sizeof(tmp), "%" SWITCH_TIME_T_FMT, caller_profile->times->transferred); + cJSON_AddItemToObject(j_times, "transfer_time", cJSON_CreateString(tmp)); + + } + + caller_profile = caller_profile->next; + } + + *json_cdr = cdr; + + return SWITCH_STATUS_SUCCESS; + +} + + SWITCH_DECLARE(void) switch_ivr_park_session(switch_core_session_t *session) { switch_channel_t *channel = switch_core_session_get_channel(session); @@ -3247,6 +3501,31 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_kill_uuid(const char *uuid, switch_ca } } +SWITCH_DECLARE(switch_status_t) switch_ivr_blind_transfer_ack(switch_core_session_t *session, switch_bool_t success) +{ + switch_channel_t *channel = switch_core_session_get_channel(session); + switch_status_t status = SWITCH_STATUS_FALSE; + + if (switch_channel_test_flag(channel, CF_CONFIRM_BLIND_TRANSFER)) { + switch_core_session_t *other_session; + const char *uuid = switch_channel_get_variable(channel, "blind_transfer_uuid"); + + switch_channel_clear_flag(channel, CF_CONFIRM_BLIND_TRANSFER); + + if (!zstr(uuid) && (other_session = switch_core_session_locate(uuid))) { + switch_core_session_message_t msg = { 0 }; + msg.message_id = SWITCH_MESSAGE_INDICATE_BLIND_TRANSFER_RESPONSE; + msg.from = __FILE__; + msg.numeric_arg = success; + switch_core_session_receive_message(other_session, &msg); + switch_core_session_rwunlock(other_session); + status = SWITCH_STATUS_SUCCESS; + } + } + + return status; + +} /* For Emacs: * Local Variables: diff --git a/src/switch_ivr_async.c b/src/switch_ivr_async.c index ecddfdbbb6..37da4f0486 100644 --- a/src/switch_ivr_async.c +++ b/src/switch_ivr_async.c @@ -1148,9 +1148,14 @@ struct eavesdrop_pvt { switch_mutex_t *r_mutex; switch_buffer_t *w_buffer; switch_mutex_t *w_mutex; + switch_core_session_t *eavesdropper; uint32_t flags; + switch_frame_t demux_frame; + uint8_t data[SWITCH_RECOMMENDED_BUFFER_SIZE]; }; + + static switch_bool_t eavesdrop_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type) { struct eavesdrop_pvt *ep = (struct eavesdrop_pvt *) user_data; @@ -1190,13 +1195,18 @@ static switch_bool_t eavesdrop_callback(switch_media_bug_t *bug, void *user_data if (switch_buffer_inuse(ep->r_buffer) >= rframe->datalen) { uint32_t bytes; switch_buffer_lock(ep->r_buffer); - bytes = (uint32_t) switch_buffer_read(ep->r_buffer, data, rframe->datalen); + bytes = (uint32_t) switch_buffer_read(ep->r_buffer, ep->data, rframe->datalen); - rframe->datalen = switch_merge_sln(rframe->data, rframe->samples, (int16_t *) data, bytes / 2) * 2; + rframe->datalen = switch_merge_sln(rframe->data, rframe->samples, (int16_t *) ep->data, bytes / 2) * 2; rframe->samples = rframe->datalen / 2; + ep->demux_frame.data = ep->data; + ep->demux_frame.datalen = bytes; + ep->demux_frame.samples = bytes / 2; + switch_buffer_unlock(ep->r_buffer); switch_core_media_bug_set_read_replace_frame(bug, rframe); + switch_core_media_bug_set_read_demux_frame(bug, &ep->demux_frame); } } } @@ -1229,6 +1239,99 @@ static switch_bool_t eavesdrop_callback(switch_media_bug_t *bug, void *user_data return SWITCH_TRUE; } +SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_pop_eavesdropper(switch_core_session_t *session, switch_core_session_t **sessionp) +{ + switch_media_bug_t *bug; + switch_status_t status = SWITCH_STATUS_FALSE; + + if (switch_core_media_bug_pop(session, "eavesdrop", &bug) == SWITCH_STATUS_SUCCESS) { + struct eavesdrop_pvt *ep = (struct eavesdrop_pvt *) switch_core_media_bug_get_user_data(bug); + + if (ep && ep->eavesdropper && ep->eavesdropper != session) { + switch_core_session_read_lock(ep->eavesdropper); + *sessionp = ep->eavesdropper; + switch_core_media_bug_set_flag(bug, SMBF_PRUNE); + status = SWITCH_STATUS_SUCCESS; + } + } + + + return status; +} + +struct exec_cb_data { + switch_core_session_t *caller; + char *var; + char *val; +}; + +static void exec_cb(switch_media_bug_t *bug, void *user_data) +{ + struct exec_cb_data *data = (struct exec_cb_data *) user_data; + struct eavesdrop_pvt *ep = (struct eavesdrop_pvt *) switch_core_media_bug_get_user_data(bug); + + if (ep && ep->eavesdropper && ep->eavesdropper != data->caller) { + switch_channel_t *a = switch_core_session_get_channel(ep->eavesdropper); + switch_channel_t *b = switch_core_session_get_channel(data->caller); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s telling %s to exec %s:%s\n", + switch_channel_get_name(b), switch_channel_get_name(a), data->var, data->val); + + switch_core_session_execute_application(ep->eavesdropper, data->var, data->val); + } +} + +static void display_exec_cb(switch_media_bug_t *bug, void *user_data) +{ + struct exec_cb_data *data = (struct exec_cb_data *) user_data; + struct eavesdrop_pvt *ep = (struct eavesdrop_pvt *) switch_core_media_bug_get_user_data(bug); + + if (ep && ep->eavesdropper && ep->eavesdropper != data->caller) { + switch_core_session_message_t msg = { 0 }; + + msg.from = __FILE__; + msg.message_id = SWITCH_MESSAGE_INDICATE_DISPLAY; + msg.string_array_arg[0] = data->var; + msg.string_array_arg[1] = data->val; + + switch_core_session_receive_message(ep->eavesdropper, &msg); + } +} + +SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_exec_all(switch_core_session_t *session, const char *app, const char *arg) +{ + struct exec_cb_data *data = NULL; + + data = switch_core_session_alloc(session, sizeof(*data)); + data->var = switch_core_session_strdup(session, app); + data->val = switch_core_session_strdup(session, arg); + data->caller = session; + + return switch_core_media_bug_exec_all(session, "eavesdrop", exec_cb, data); +} + + +SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_update_display(switch_core_session_t *session, const char *name, const char *number) +{ + struct exec_cb_data *data = NULL; + switch_channel_t *channel = switch_core_session_get_channel(session); + switch_status_t status = SWITCH_STATUS_FALSE; + + data = switch_core_session_alloc(session, sizeof(*data)); + data->var = switch_core_session_strdup(session, name); + data->val = switch_core_session_strdup(session, number); + data->caller = session; + + if (!switch_channel_test_app_flag_key("EAVESDROP", channel, 1)) { + switch_channel_set_app_flag_key("EAVESDROP", channel, 1); + status = switch_core_media_bug_exec_all(session, "eavesdrop", display_exec_cb, data); + switch_channel_clear_app_flag_key("EAVESDROP", channel, 1); + } + + return status; +} + + SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_session(switch_core_session_t *session, const char *uuid, const char *require_group, switch_eavesdrop_flag_t flags) { @@ -1236,6 +1339,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_session(switch_core_session switch_status_t status = SWITCH_STATUS_FALSE; switch_channel_t *channel = switch_core_session_get_channel(session); int codec_initialized = 0; + const char *name, *num; if ((tsession = switch_core_session_locate(uuid))) { struct eavesdrop_pvt *ep = NULL; @@ -1341,6 +1445,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_session(switch_core_session write_frame.buflen = sizeof(buf); write_frame.rate = codec.implementation->actual_samples_per_second; + ep->eavesdropper = session; ep->flags = flags; switch_mutex_init(&ep->mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(tsession)); switch_buffer_create_dynamic(&ep->buffer, 2048, 2048, 8192); @@ -1370,9 +1475,28 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_eavesdrop_session(switch_core_session /* Tell the channel we are going to be in a bridge */ msg.message_id = SWITCH_MESSAGE_INDICATE_BRIDGE; switch_core_session_receive_message(session, &msg); - cp = switch_channel_get_caller_profile(tchannel); - switch_snprintf(cid_buf, sizeof(cid_buf), "%s|%s", cp->caller_id_number, cp->caller_id_name); + name = cp->caller_id_name; + num = cp->caller_id_number; + + if (flags & ED_COPY_DISPLAY) { + const char *tmp_name = NULL, *tmp_num = NULL; + name = cp->callee_id_name; + num = cp->callee_id_number; + + if (!((tmp_name = switch_channel_get_variable(tchannel, "last_sent_callee_id_name")) + && (tmp_num = switch_channel_get_variable(tchannel, "last_sent_callee_id_number")))) { + + tmp_name = switch_channel_get_variable(tchannel, "callee_id_name"); + tmp_num = switch_channel_get_variable(tchannel, "callee_id_number"); + } + + if (tmp_name) name = tmp_name; + if (tmp_num) num = tmp_num; + + } + + switch_snprintf(cid_buf, sizeof(cid_buf), "%s|%s", name, num); msg.string_arg = cid_buf; msg.message_id = SWITCH_MESSAGE_INDICATE_DISPLAY; switch_core_session_receive_message(session, &msg); @@ -3944,7 +4068,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_broadcast(const char *uuid, const cha } } - if ((flags & SMF_ECHO_BLEG) && (other_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE)) + if ((flags & SMF_ECHO_BLEG) && (other_uuid = switch_channel_get_partner_uuid(channel)) && (other_session = switch_core_session_locate(other_uuid))) { if ((flags & SMF_EXEC_INLINE)) { switch_core_session_execute_application_get_flags(other_session, app, path, &app_flags); diff --git a/src/switch_ivr_bridge.c b/src/switch_ivr_bridge.c index 8025c9d01c..bc5c24e5f0 100644 --- a/src/switch_ivr_bridge.c +++ b/src/switch_ivr_bridge.c @@ -228,6 +228,8 @@ static void *audio_bridge_thread(switch_thread_t *thread, void *obj) } } + switch_channel_clear_flag(chan_a, CF_INTERCEPT); + switch_channel_clear_flag(chan_a, CF_INTERCEPTED); switch_channel_set_flag(chan_a, CF_BRIDGED); @@ -652,9 +654,11 @@ static switch_status_t audio_bridge_on_exchange_media(switch_core_session_t *ses state = switch_channel_get_state(channel); - if (state < CS_HANGUP && switch_true(switch_channel_get_variable(channel, SWITCH_PARK_AFTER_BRIDGE_VARIABLE))) { + if (state < CS_HANGUP && !switch_channel_test_flag(channel, CF_TRANSFER) && !switch_channel_test_flag(channel, CF_REDIRECT) && + switch_true(switch_channel_get_variable(channel, SWITCH_PARK_AFTER_BRIDGE_VARIABLE))) { switch_ivr_park_session(session); - } else if (state < CS_HANGUP && (var = switch_channel_get_variable(channel, SWITCH_TRANSFER_AFTER_BRIDGE_VARIABLE))) { + } else if (state < CS_HANGUP && !switch_channel_test_flag(channel, CF_TRANSFER) && !switch_channel_test_flag(channel, CF_REDIRECT) && + (var = switch_channel_get_variable(channel, SWITCH_TRANSFER_AFTER_BRIDGE_VARIABLE))) { transfer_after_bridge(session, var); } else { if (!switch_channel_test_flag(channel, CF_TRANSFER) && !switch_channel_test_flag(channel, CF_REDIRECT) && @@ -968,9 +972,16 @@ static switch_status_t signal_bridge_on_hibernate(switch_core_session_t *session if (switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) { if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_BRIDGE) == SWITCH_STATUS_SUCCESS) { + switch_core_session_t *other_session; + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(session)); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", msg.string_arg); switch_channel_event_set_data(channel, event); + if ((other_session = switch_core_session_locate(msg.string_arg))) { + switch_channel_t *other_channel = switch_core_session_get_channel(other_session); + switch_event_add_presence_data_cols(other_channel, event, "Bridge-B-PD-"); + switch_core_session_rwunlock(other_session); + } switch_event_fire(&event); } } @@ -1052,16 +1063,27 @@ static switch_status_t signal_bridge_on_hangup(switch_core_session_t *session) } } - switch_core_session_rwunlock(other_session); - } + if (switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) { + switch_channel_clear_flag_recursive(channel, CF_BRIDGE_ORIGINATOR); + if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_UNBRIDGE) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(session)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", uuid); + switch_event_add_presence_data_cols(other_channel, event, "Bridge-B-PD-"); + switch_channel_event_set_data(channel, event); + switch_event_fire(&event); + } + } - if (switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) { - switch_channel_clear_flag_recursive(channel, CF_BRIDGE_ORIGINATOR); - if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_UNBRIDGE) == SWITCH_STATUS_SUCCESS) { - switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(session)); - switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", uuid); - switch_channel_event_set_data(channel, event); - switch_event_fire(&event); + switch_core_session_rwunlock(other_session); + } else { + if (switch_channel_test_flag(channel, CF_BRIDGE_ORIGINATOR)) { + switch_channel_clear_flag_recursive(channel, CF_BRIDGE_ORIGINATOR); + if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_UNBRIDGE) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(session)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", uuid); + switch_channel_event_set_data(channel, event); + switch_event_fire(&event); + } } } @@ -1242,6 +1264,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_multi_threaded_bridge(switch_core_ses switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(session)); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", switch_core_session_get_uuid(peer_session)); switch_channel_event_set_data(caller_channel, event); + switch_event_add_presence_data_cols(peer_channel, event, "Bridge-B-PD-"); switch_event_fire(&event); br = 1; } @@ -1366,21 +1389,41 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_multi_threaded_bridge(switch_core_ses switch_channel_set_variable(caller_channel, SWITCH_BRIDGE_HANGUP_CAUSE_VARIABLE, switch_channel_cause2str(cause)); } - if (switch_channel_down_nosig(peer_channel) && switch_true(switch_channel_get_variable(peer_channel, SWITCH_COPY_XML_CDR_VARIABLE))) { - switch_xml_t cdr = NULL; - char *xml_text; + if (switch_channel_down_nosig(peer_channel)) { + switch_bool_t copy_xml_cdr = switch_true(switch_channel_get_variable(peer_channel, SWITCH_COPY_XML_CDR_VARIABLE)); + switch_bool_t copy_json_cdr = switch_true(switch_channel_get_variable(peer_channel, SWITCH_COPY_JSON_CDR_VARIABLE)); - switch_channel_wait_for_state(peer_channel, caller_channel, CS_DESTROY); + if (copy_xml_cdr || copy_json_cdr) { + char *cdr_text = NULL; - if (switch_ivr_generate_xml_cdr(peer_session, &cdr) == SWITCH_STATUS_SUCCESS) { - if ((xml_text = switch_xml_toxml(cdr, SWITCH_FALSE))) { - switch_channel_set_variable(caller_channel, "b_leg_cdr", xml_text); - switch_safe_free(xml_text); + switch_channel_wait_for_state(peer_channel, caller_channel, CS_DESTROY); + + if (copy_xml_cdr) { + switch_xml_t cdr = NULL; + + if (switch_ivr_generate_xml_cdr(peer_session, &cdr) == SWITCH_STATUS_SUCCESS) { + cdr_text = switch_xml_toxml(cdr, SWITCH_FALSE); + switch_xml_free(cdr); + } } - switch_xml_free(cdr); - } - } + if (copy_json_cdr) { + cJSON *cdr = NULL; + if (switch_ivr_generate_json_cdr(peer_session, &cdr, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) { + cdr_text = cJSON_PrintUnformatted(cdr); + cJSON_Delete(cdr); + } + } + + if (cdr_text) { + switch_channel_set_variable(caller_channel, "b_leg_cdr", cdr_text); + switch_channel_set_variable_name_printf(caller_channel, cdr_text, "b_leg_cdr_%s", switch_core_session_get_uuid(peer_session)); + switch_safe_free(cdr_text); + } + } + + } + switch_core_session_rwunlock(peer_session); } else { @@ -1405,6 +1448,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_multi_threaded_bridge(switch_core_ses switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(session)); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", switch_core_session_get_uuid(peer_session)); switch_channel_event_set_data(caller_channel, event); + switch_event_add_presence_data_cols(peer_channel, event, "Bridge-B-PD-"); switch_event_fire(&event); } @@ -1471,21 +1515,26 @@ static void cleanup_proxy_mode_b(switch_core_session_t *session) static void cleanup_proxy_mode_a(switch_core_session_t *session) { switch_core_session_t *sbsession; - switch_channel_t *channel = switch_core_session_get_channel(session); - + int done = 0; if (switch_channel_test_flag(channel, CF_PROXY_MODE)) { - const char *sbv = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); - if (!zstr(sbv) && (sbsession = switch_core_session_locate(sbv))) { + if (switch_core_session_get_partner(session, &sbsession) == SWITCH_STATUS_SUCCESS) { switch_channel_t *sbchannel = switch_core_session_get_channel(sbsession); - /* Clear this now, otherwise will cause the one we're interested in to hang up too...*/ - switch_channel_set_variable(sbchannel, SWITCH_SIGNAL_BRIDGE_VARIABLE, NULL); - switch_channel_hangup(sbchannel, SWITCH_CAUSE_ATTENDED_TRANSFER); + + if (switch_channel_test_flag(sbchannel, CF_PROXY_MODE)) { + /* Clear this now, otherwise will cause the one we're interested in to hang up too...*/ + switch_channel_set_variable(sbchannel, SWITCH_SIGNAL_BRIDGE_VARIABLE, NULL); + switch_channel_hangup(sbchannel, SWITCH_CAUSE_ATTENDED_TRANSFER); + } else { + done = 1; + } switch_core_session_rwunlock(sbsession); } } + if (done) return; + switch_channel_set_variable(channel, SWITCH_SIGNAL_BRIDGE_VARIABLE, NULL); switch_channel_set_variable(channel, SWITCH_BRIDGE_VARIABLE, NULL); switch_channel_set_variable(channel, SWITCH_BRIDGE_UUID_VARIABLE, NULL); @@ -1657,7 +1706,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_find_bridged_uuid(const char *uuid, c const char *brto; if ((brto = switch_channel_get_variable(rchannel, "orignate_signal_bond")) || - (brto = switch_channel_get_variable(rchannel, SWITCH_SIGNAL_BOND_VARIABLE))) { + (brto = switch_channel_get_partner_uuid(rchannel))) { switch_copy_string(b_uuid, brto, blen); status = SWITCH_STATUS_SUCCESS; } @@ -1691,7 +1740,7 @@ SWITCH_DECLARE(void) switch_ivr_intercept_session(switch_core_session_t *session channel = switch_core_session_get_channel(session); rchannel = switch_core_session_get_channel(rsession); - buuid = switch_channel_get_variable(rchannel, SWITCH_SIGNAL_BOND_VARIABLE); + buuid = switch_channel_get_partner_uuid(rchannel); if ((var = switch_channel_get_variable(channel, "intercept_unbridged_only")) && switch_true(var)) { if ((switch_channel_test_flag(rchannel, CF_BRIDGED))) { diff --git a/src/switch_ivr_originate.c b/src/switch_ivr_originate.c index 87ff02ec8c..c18da450a6 100644 --- a/src/switch_ivr_originate.c +++ b/src/switch_ivr_originate.c @@ -1820,7 +1820,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *sess } if (session) { - const char *to_var, *bypass_media = NULL, *proxy_media = NULL; + const char *to_var, *bypass_media = NULL, *proxy_media = NULL, *zrtp_passthru = NULL; caller_channel = switch_core_session_get_channel(session); switch_channel_set_flag(caller_channel, CF_ORIGINATOR); oglobals.session = session; @@ -1833,6 +1833,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *sess proxy_media = switch_channel_get_variable(caller_channel, SWITCH_PROXY_MEDIA_VARIABLE); bypass_media = switch_channel_get_variable(caller_channel, SWITCH_BYPASS_MEDIA_VARIABLE); + zrtp_passthru = switch_channel_get_variable(caller_channel, SWITCH_ZRTP_PASSTHRU_VARIABLE); if (!zstr(proxy_media)) { if (switch_true(proxy_media)) { @@ -1842,6 +1843,14 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *sess } } + if (!zstr(zrtp_passthru)) { + if (switch_true(zrtp_passthru)) { + switch_channel_set_flag(caller_channel, CF_ZRTP_PASSTHRU_REQ); + } else if (switch_channel_test_flag(caller_channel, CF_ZRTP_PASSTHRU_REQ)) { + switch_channel_clear_flag(caller_channel, CF_ZRTP_PASSTHRU_REQ); + } + } + if (bypass_media && switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA) && !switch_channel_test_flag(caller_channel, CF_ANSWERED)) { switch_core_session_message_t msg = { 0 }; @@ -3583,7 +3592,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *sess switch_channel_set_variable(caller_channel, SWITCH_SIGNAL_BOND_VARIABLE, switch_core_session_get_uuid(*bleg)); // Now main SWITCH_SIGNAL_BOND_VARIABLE is populated, don't need this one anymore... - switch_channel_set_variable(caller_channel, "originate_signal_bond", NULL); + switch_channel_set_variable(caller_channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE, NULL); } diff --git a/src/switch_ivr_play_say.c b/src/switch_ivr_play_say.c index f435c87026..8738071cb4 100644 --- a/src/switch_ivr_play_say.c +++ b/src/switch_ivr_play_say.c @@ -2570,7 +2570,7 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_soft_hold(switch_core_session_t *sess channel = switch_core_session_get_channel(session); switch_assert(channel != NULL); - if ((other_uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE))) { + if ((other_uuid = switch_channel_get_partner_uuid(channel))) { if ((other_session = switch_core_session_locate(other_uuid))) { other_channel = switch_core_session_get_channel(other_session); diff --git a/src/switch_loadable_module.c b/src/switch_loadable_module.c index 51f343a70e..4bc0e93108 100644 --- a/src/switch_loadable_module.c +++ b/src/switch_loadable_module.c @@ -1675,7 +1675,7 @@ SWITCH_DECLARE(switch_status_t) switch_loadable_module_init(switch_bool_t autolo if (switch_loadable_module_load_module_ex((char *) path, (char *) val, SWITCH_FALSE, global, &err) == SWITCH_STATUS_GENERR) { if (critical && switch_true(critical)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to load critical module '%s', abort()\n", val); - abort(); + return SWITCH_STATUS_FALSE; } } count++; @@ -2234,6 +2234,10 @@ SWITCH_DECLARE(switch_status_t) switch_api_execute(const char *cmd, const char * switch_assert(stream->data != NULL); switch_assert(stream->write_function != NULL); + while (cmd && *cmd == ' ') { + cmd++; + } + if (strcasecmp(cmd, "console_complete")) { cmd_used = switch_strip_whitespace(cmd); arg_used = switch_strip_whitespace(arg); diff --git a/src/switch_resample.c b/src/switch_resample.c index 136adc9694..6722d6f448 100644 --- a/src/switch_resample.c +++ b/src/switch_resample.c @@ -252,6 +252,25 @@ SWITCH_DECLARE(uint32_t) switch_merge_sln(int16_t *data, uint32_t samples, int16 return x; } + +SWITCH_DECLARE(uint32_t) switch_unmerge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples) +{ + int i; + int32_t x; + + if (samples > other_samples) { + x = other_samples; + } else { + x = samples; + } + + for (i = 0; i < x; i++) { + data[i] -= other_data[i]; + } + + return x; +} + SWITCH_DECLARE(void) switch_mux_channels(int16_t *data, switch_size_t samples, uint32_t channels) { int16_t *buf; diff --git a/src/switch_rtp.c b/src/switch_rtp.c index 72d7a8e71c..341b005473 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -73,7 +73,11 @@ typedef srtp_hdr_t rtp_hdr_t; #ifdef ENABLE_ZRTP #include "zrtp.h" static zrtp_global_t *zrtp_global; +#ifndef WIN32 static zrtp_zid_t zid = { "FreeSWITCH01" }; +#else +static zrtp_zid_t zid = { "FreeSWITCH0" }; +#endif static int zrtp_on = 0; #define ZRTP_MITM_TRIES 100 #endif @@ -269,6 +273,46 @@ struct switch_rtp { }; +struct switch_rtcp_source { + unsigned ssrc1:32; + unsigned fraction_lost:8; + unsigned cumulative_lost:24; + unsigned hi_seq_recieved:32; + unsigned interarrival_jitter:32; + unsigned lsr:32; + unsigned lsr_delay:32; +}; + + +#if SWITCH_BYTE_ORDER == __BIG_ENDIAN +struct switch_rtcp_s_desc_head { + unsigned v:2; + unsigned padding:1; + unsigned sc:5; + unsigned pt:8; + unsigned length:16; +}; + +#else /* BIG_ENDIAN */ + +struct switch_rtcp_s_desc_head { + unsigned sc:5; + unsigned padding:1; + unsigned v:2; + unsigned pt:8; + unsigned length:16; +}; + +#endif + + +struct switch_rtcp_s_desc_trunk { + unsigned ssrc:32; + unsigned cname:8; + unsigned length:8; + char text[1]; +}; + struct switch_rtcp_senderinfo { unsigned ssrc:32; unsigned ntp_msw:32; @@ -276,6 +320,9 @@ struct switch_rtcp_senderinfo { unsigned ts:32; unsigned pc:32; unsigned oc:32; + struct switch_rtcp_source sr_source; + struct switch_rtcp_s_desc_head sr_desc_head; + struct switch_rtcp_s_desc_trunk sr_desc_ssrc; }; typedef enum { @@ -715,7 +762,7 @@ static void zrtp_event_callback(zrtp_stream_t *stream, unsigned event) switch_channel_set_variable_name_printf(channel, stream->session->sas1.buffer, "zrtp_sas1_string_%s", type); switch_channel_set_variable_name_printf(channel, stream->session->sas2.buffer, "zrtp_sas2_string", type); - zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, zrtp_session_info.sas_is_verified ^ 1); + zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, (uint8_t)(zrtp_session_info.sas_is_verified ^ 1)); } } @@ -777,7 +824,7 @@ static void zrtp_event_callback(zrtp_stream_t *stream, unsigned event) if (zrtp_status_ok == zrtp_session_get(stream->session, &zrtp_session_info)) { if (zrtp_session_info.sas_is_ready) { - zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, zrtp_session_info.sas_is_verified ^ 1); + zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, (uint8_t)(zrtp_session_info.sas_is_verified ^ 1)); } } } @@ -790,7 +837,7 @@ static void zrtp_event_callback(zrtp_stream_t *stream, unsigned event) if (zrtp_status_ok == zrtp_session_get(stream->session, &zrtp_session_info)) { if (zrtp_session_info.sas_is_ready) { - zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, zrtp_session_info.sas_is_verified ^ 1); + zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, (uint8_t)(zrtp_session_info.sas_is_verified ^ 1)); } } } @@ -803,7 +850,7 @@ static void zrtp_event_callback(zrtp_stream_t *stream, unsigned event) if (zrtp_status_ok == zrtp_session_get(stream->session, &zrtp_session_info)) { if (zrtp_session_info.sas_is_ready) { - zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, zrtp_session_info.sas_is_verified ^ 1); + zrtp_verified_set(zrtp_global, &stream->session->zid, &stream->session->peer_zid, (uint8_t)(zrtp_session_info.sas_is_verified ^ 1)); } } } @@ -858,11 +905,11 @@ SWITCH_DECLARE(void) switch_rtp_init(switch_memory_pool_t *pool) zrtp_config.lic_mode = ZRTP_LICENSE_MODE_ACTIVE; switch_snprintf(zrtp_cache_path, sizeof(zrtp_cache_path), "%s%szrtp.dat", SWITCH_GLOBAL_dirs.db_dir, SWITCH_PATH_SEPARATOR); zrtp_zstrcpyc((zrtp_stringn_t*)zrtp_config.def_cache_path.buffer, zrtp_cache_path); - zrtp_config.def_cache_path.length = strlen(zrtp_cache_path); + zrtp_config.def_cache_path.length = (uint16_t)strlen(zrtp_cache_path); zrtp_config.def_cache_path.max_length = 255; - zrtp_config.cb.event_cb.on_zrtp_protocol_event = zrtp_event_callback; + zrtp_config.cb.event_cb.on_zrtp_protocol_event = (void (*)(zrtp_stream_t*,zrtp_protocol_event_t))zrtp_event_callback; zrtp_config.cb.misc_cb.on_send_packet = zrtp_send_rtp_callback; - zrtp_config.cb.event_cb.on_zrtp_security_event = zrtp_event_callback; + zrtp_config.cb.event_cb.on_zrtp_security_event = (void (*)(zrtp_stream_t*,zrtp_security_event_t))zrtp_event_callback; zrtp_log_set_log_engine((zrtp_log_engine *) zrtp_logger); zrtp_log_set_level(4); @@ -1404,7 +1451,7 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_add_crypto_key(switch_rtp_t *rtp_sess uint32_t index, switch_rtp_crypto_key_type_t type, unsigned char *key, switch_size_t keylen) { #ifndef ENABLE_SRTP - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "SRTP NOT SUPPORTED IN THIS BUILD!\n"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SRTP NOT SUPPORTED IN THIS BUILD!\n"); return SWITCH_STATUS_FALSE; #else switch_rtp_crypto_key_t *crypto_key; @@ -1652,8 +1699,8 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_create(switch_rtp_t **new_rtp_session rtp_session->rtcp_send_msg.header.version = 2; rtp_session->rtcp_send_msg.header.p = 0; rtp_session->rtcp_send_msg.header.type = 200; - rtp_session->rtcp_send_msg.header.count = 0; - rtp_session->rtcp_send_msg.header.length = htons(6); + rtp_session->rtcp_send_msg.header.count = 1; + rtp_session->rtcp_send_msg.header.length = htons(12); switch_rtp_set_interval(rtp_session, ms_per_packet, samples_per_interval); @@ -1726,7 +1773,7 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_create(switch_rtp_t **new_rtp_session rtp_session->zrtp_profile->allowclear = 0; rtp_session->zrtp_profile->disclose_bit = 0; - rtp_session->zrtp_profile->cache_ttl = -1; + rtp_session->zrtp_profile->cache_ttl = (uint32_t) -1; if (zrtp_status_ok != zrtp_session_init(zrtp_global, rtp_session->zrtp_profile, zid, initiator, &rtp_session->zrtp_session)) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error! zRTP INIT Failed\n"); @@ -1785,6 +1832,7 @@ SWITCH_DECLARE(switch_rtp_t *) switch_rtp_new(const char *rx_host, goto end; } +#if 0 if (zstr(tx_host)) { *err = "Missing remote host"; goto end; @@ -1794,6 +1842,7 @@ SWITCH_DECLARE(switch_rtp_t *) switch_rtp_new(const char *rx_host, *err = "Missing remote port"; goto end; } +#endif if (switch_rtp_create(&rtp_session, payload, samples_per_interval, ms_per_packet, flags, timer_name, err, pool) != SWITCH_STATUS_SUCCESS) { goto end; @@ -1807,7 +1856,7 @@ SWITCH_DECLARE(switch_rtp_t *) switch_rtp_new(const char *rx_host, goto end; } - if (switch_rtp_set_remote_address(rtp_session, tx_host, tx_port, 0, SWITCH_TRUE, err) != SWITCH_STATUS_SUCCESS) { + if (!zstr(tx_host) && switch_rtp_set_remote_address(rtp_session, tx_host, tx_port, 0, SWITCH_TRUE, err) != SWITCH_STATUS_SUCCESS) { switch_mutex_unlock(rtp_session->flag_mutex); rtp_session = NULL; goto end; @@ -2114,7 +2163,7 @@ SWITCH_DECLARE(uint8_t) switch_rtp_ready(switch_rtp_t *rtp_session) } switch_mutex_lock(rtp_session->flag_mutex); - ret = (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO) && rtp_session->sock_input && rtp_session->sock_output && rtp_session->remote_addr + ret = (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO) && rtp_session->sock_input /* && rtp_session->sock_output && rtp_session->remote_addr */ && rtp_session->ready == 2) ? 1 : 0; switch_mutex_unlock(rtp_session->flag_mutex); @@ -2300,6 +2349,7 @@ static void set_dtmf_delay(switch_rtp_t *rtp_session, uint32_t ms, uint32_t max_ if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) { rtp_session->max_next_write_samplecount = rtp_session->timer.samplecount + max_upsamp; rtp_session->next_write_samplecount = rtp_session->timer.samplecount + upsamp; + rtp_session->last_write_ts += upsamp; } switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Queue digit delay of %dms\n", ms); @@ -2349,7 +2399,7 @@ static void do_2833(switch_rtp_t *rtp_session, switch_core_session_t *session) } } - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Send %s packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%d\n", + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Send %s packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n", loops == 1 ? "middle" : "end", rtp_session->dtmf_data.out_digit, rtp_session->dtmf_data.timestamp_dtmf, rtp_session->dtmf_data.out_digit_sofar, @@ -2651,7 +2701,7 @@ static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes = sbytes; break; case zrtp_status_drop: - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat); + /* switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */ *bytes = 0; return SWITCH_STATUS_SUCCESS; case zrtp_status_fail: @@ -2851,6 +2901,7 @@ static switch_status_t read_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t rtp_session->stats.rtcp.packet_count += sr->pc; rtp_session->stats.rtcp.octet_count += sr->oc; + rtp_session->stats.rtcp.peer_ssrc = ntohl(sr->ssrc); /* sender report */ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG10,"Received a SR with %d report blocks, " \ @@ -3043,7 +3094,7 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_ switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session"); switch_channel_t *channel = switch_core_session_get_channel(session); - const char *uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); + const char *uuid = switch_channel_get_partner_uuid(channel); if (uuid) { switch_core_session_t *other_session; switch_rtp_t *other_rtp_session = NULL; @@ -3621,7 +3672,7 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_zerocopy_read_frame(switch_rtp_t *rtp switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session"); switch_channel_t *channel = switch_core_session_get_channel(session); - const char *uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); + const char *uuid = switch_channel_get_partner_uuid(channel); if (uuid) { switch_core_session_t *other_session; @@ -3644,7 +3695,7 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_zerocopy_read_frame(switch_rtp_t *rtp switch_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV); switch_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND); zrtp_verified_set(zrtp_global, &rtp_session->zrtp_session->zid, - &rtp_session->zrtp_session->peer_zid, zrtp_session_info.sas_is_verified ^ 1); + &rtp_session->zrtp_session->peer_zid, (uint8_t)(zrtp_session_info.sas_is_verified ^ 1)); rtp_session->zrtp_mitm_tries++; } } @@ -3967,7 +4018,7 @@ static int rtp_common_write(switch_rtp_t *rtp_session, case zrtp_status_ok: break; case zrtp_status_drop: - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat); + /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */ ret = (int) bytes; goto end; break; @@ -4056,15 +4107,52 @@ static int rtp_common_write(switch_rtp_t *rtp_session, switch_test_flag(rtp_session, SWITCH_RTP_FLAG_ENABLE_RTCP) && !switch_test_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_PASSTHRU) && rtp_session->rtcp_interval && (rtp_session->stats.outbound.packet_count % rtp_session->rtcp_interval) == 0) { struct switch_rtcp_senderinfo* sr = (struct switch_rtcp_senderinfo*)rtp_session->rtcp_send_msg.body; + const char* str_cname=NULL; sr->ssrc = send_msg->header.ssrc; - sr->ntp_msw = htonl((u_long)rtp_session->send_time / 1000000 + 2208988800UL); + sr->ntp_msw = htonl((u_long)(rtp_session->send_time / 1000000 + 2208988800UL)); + /* sr->ntp_lsw = htonl((u_long)(rtp_session->send_time % 1000000 * ((UINT_MAX * 1.0)/ 1000000.0))); + */ + sr->ntp_lsw = htonl((u_long)(rtp_session->send_time % 1000000 * 4294.967296)); sr->ts = send_msg->header.ts; sr->pc = htonl(rtp_session->stats.outbound.packet_count); sr->oc = htonl((rtp_session->stats.outbound.raw_bytes - rtp_session->stats.outbound.packet_count * sizeof(srtp_hdr_t))); - rtcp_bytes = sizeof(switch_rtcp_hdr_t) + sizeof(struct switch_rtcp_senderinfo); + + sr->sr_source.ssrc1 = htonl(rtp_session->stats.rtcp.peer_ssrc); + sr->sr_source.fraction_lost |= 0; + sr->sr_source.cumulative_lost |= 0; + sr->sr_source.hi_seq_recieved = htonl(rtp_session->last_seq); + sr->sr_source.interarrival_jitter = htonl(0); + sr->sr_source.lsr = htonl(0); + sr->sr_source.lsr_delay = htonl(0); + + sr->sr_desc_head.v = 0x02; + sr->sr_desc_head.padding = 0; + sr->sr_desc_head.sc = 1; + sr->sr_desc_head.pt = 202; + sr->sr_desc_head.length = htons(5); + + + sr->sr_desc_ssrc.ssrc = send_msg->header.ssrc; /*htonl(rtp_session->stats.rtcp.peer_ssrc); */ + sr->sr_desc_ssrc.cname = 0x1; + { + char bufa[30]; + str_cname = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_local_addr); + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "Setting RTCP src-1 to %s\n", str_cname); + sr->sr_desc_ssrc.length = strlen(str_cname); + memcpy ((char*)sr->sr_desc_ssrc.text, str_cname, strlen(str_cname)); + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "Setting RTCP src-1 LENGTH to %d (%d, %s)\n", sr->sr_desc_ssrc.length, sr->sr_desc_head.length, str_cname); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "Setting msw = %d, lsw = %d \n", sr->ntp_msw, sr->ntp_lsw); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "now = %"SWITCH_TIME_T_FMT", now lo = %d, now hi = %d\n", (switch_time_t)now, (int32_t)(now&0xFFFFFFFF), (int32_t)((now>>32&0xFFFFFFFF))); + + + rtcp_bytes = sizeof(switch_rtcp_hdr_t) + sizeof(struct switch_rtcp_senderinfo) + sr->sr_desc_ssrc.length -1 ; + + #ifdef ENABLE_SRTP if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND)) { int sbytes = (int) rtcp_bytes; @@ -4252,7 +4340,7 @@ SWITCH_DECLARE(int) switch_rtp_write_frame(switch_rtp_t *rtp_session, switch_fra switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session"); switch_channel_t *channel = switch_core_session_get_channel(session); - const char *uuid = switch_channel_get_variable(channel, SWITCH_SIGNAL_BOND_VARIABLE); + const char *uuid = switch_channel_get_partner_uuid(channel); if (uuid) { switch_core_session_t *other_session; @@ -4274,7 +4362,7 @@ SWITCH_DECLARE(int) switch_rtp_write_frame(switch_rtp_t *rtp_session, switch_fra switch_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_RECV); switch_clear_flag(other_rtp_session, SWITCH_ZRTP_FLAG_SECURE_MITM_SEND); zrtp_verified_set(zrtp_global, &rtp_session->zrtp_session->zid, - &rtp_session->zrtp_session->peer_zid, zrtp_session_info.sas_is_verified ^ 1); + &rtp_session->zrtp_session->peer_zid, (uint8_t)(zrtp_session_info.sas_is_verified ^ 1)); rtp_session->zrtp_mitm_tries++; } rtp_session->zrtp_mitm_tries++; @@ -4378,7 +4466,7 @@ SWITCH_DECLARE(int) switch_rtp_write_manual(switch_rtp_t *rtp_session, rtp_session->write_msg.header.seq = htons(++rtp_session->seq); rtp_session->write_msg.header.ts = htonl(ts); rtp_session->write_msg.header.pt = payload; - rtp_session->write_msg.header.m = (m && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) ? 1 : 0; + rtp_session->write_msg.header.m = m; memcpy(rtp_session->write_msg.body, data, datalen); bytes = rtp_header_len + datalen; diff --git a/src/switch_time.c b/src/switch_time.c index 17a0a50675..381d2dc5ad 100644 --- a/src/switch_time.c +++ b/src/switch_time.c @@ -140,7 +140,7 @@ typedef struct timer_matrix timer_matrix_t; static timer_matrix_t TIMER_MATRIX[MAX_ELEMENTS + 1]; -static void os_yield(void) +SWITCH_DECLARE(void) switch_os_yield(void) { #if defined(WIN32) SwitchToThread(); @@ -467,7 +467,7 @@ SWITCH_DECLARE(void) switch_sleep(switch_interval_time_t t) SWITCH_DECLARE(void) switch_cond_next(void) { if (runtime.tipping_point && globals.timer_count >= runtime.tipping_point) { - os_yield(); + switch_os_yield(); return; } #ifdef DISABLE_1MS_COND @@ -633,7 +633,7 @@ static switch_status_t timer_next(switch_timer_t *timer) check_roll(); if (runtime.tipping_point && globals.timer_count >= runtime.tipping_point) { - os_yield(); + switch_os_yield(); globals.use_cond_yield = 0; } else { if (globals.use_cond_yield == 1) { @@ -795,14 +795,9 @@ SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime) runtime.profile_timer = switch_new_profile_timer(); switch_get_system_idle_time(runtime.profile_timer, &runtime.profile_time); -#ifdef HAVE_CPU_SET_MACROS - if (runtime.timer_affinity > -1) { - cpu_set_t set; - CPU_ZERO(&set); - CPU_SET(runtime.timer_affinity, &set); - sched_setaffinity(0, sizeof(set), &set); + if (runtime.timer_affinity > -1) { + switch_core_thread_set_cpu_affinity(runtime.timer_affinity); } -#endif switch_time_sync(); time_sync = runtime.time_sync; @@ -889,7 +884,7 @@ SWITCH_MODULE_RUNTIME_FUNCTION(softtimer_runtime) } if (runtime.tipping_point && globals.timer_count >= runtime.tipping_point) { - os_yield(); + switch_os_yield(); } else { if (tfd > -1 && globals.RUNNING == 1) { uint64_t exp; diff --git a/w32/Library/FreeSwitchCore.2010.vcxproj b/w32/Library/FreeSwitchCore.2010.vcxproj index 94b5dbc55a..eb1933a7f6 100644 --- a/w32/Library/FreeSwitchCore.2010.vcxproj +++ b/w32/Library/FreeSwitchCore.2010.vcxproj @@ -88,8 +88,8 @@ Disabled - ..\..\src\include;..\..\libs\include;..\..\libs\srtp\include;..\..\libs\srtp\crypto\include;..\..\libs\libteletone\src;..\..\libs\win32\sqlite;..\..\libs\pcre;..\..\libs\stfu;..\..\libs\speex\include;..\..\libs\spandsp\src\msvc;..\..\libs\spandsp\src;..\..\libs\tiff-3.8.2\libtiff;%(AdditionalIncludeDirectories) - WIN32;_DEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;PCRE_STATIC;STATICLIB;%(PreprocessorDefinitions) + ..\..\src\include;..\..\libs\include;..\..\libs\srtp\include;..\..\libs\srtp\crypto\include;..\..\libs\libteletone\src;..\..\libs\win32\sqlite;..\..\libs\pcre;..\..\libs\stfu;..\..\libs\speex\include;..\..\libs\spandsp\src\msvc;..\..\libs\spandsp\src;..\..\libs\tiff-3.8.2\libtiff;..\..\libs\libzrtp\include;..\..\libs\libzrtp\third_party\bgaes;..\..\libs\libzrtp\third_party\bnlib;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;PCRE_STATIC;STATICLIB;ENABLE_ZRTP;%(PreprocessorDefinitions) true EnableFastChecks MultiThreadedDebugDLL @@ -146,8 +146,8 @@ if not exist "$(OutDir)htdocs" xcopy "$(SolutionDir)htdocs\*.*" "$(OutDir)htdocs Disabled - ..\..\src\include;..\..\libs\include;..\..\libs\srtp\include;..\..\libs\srtp\crypto\include;..\..\libs\libteletone\src;..\..\libs\win32\sqlite;..\..\libs\pcre;..\..\libs\stfu;..\..\libs\speex\include;..\..\libs\spandsp\src\msvc;..\..\libs\spandsp\src;..\..\libs\tiff-3.8.2\libtiff;%(AdditionalIncludeDirectories) - WIN32;_DEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;PCRE_STATIC;STATICLIB;%(PreprocessorDefinitions) + ..\..\src\include;..\..\libs\include;..\..\libs\srtp\include;..\..\libs\srtp\crypto\include;..\..\libs\libteletone\src;..\..\libs\win32\sqlite;..\..\libs\pcre;..\..\libs\stfu;..\..\libs\speex\include;..\..\libs\spandsp\src\msvc;..\..\libs\spandsp\src;..\..\libs\tiff-3.8.2\libtiff;..\..\libs\libzrtp\include;..\..\libs\libzrtp\third_party\bgaes;..\..\libs\libzrtp\third_party\bnlib;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;PCRE_STATIC;STATICLIB;ENABLE_ZRTP;%(PreprocessorDefinitions) true EnableFastChecks MultiThreadedDebugDLL @@ -198,8 +198,8 @@ if not exist "$(OutDir)htdocs" xcopy "$(SolutionDir)htdocs\*.*" "$(OutDir)htdocs MaxSpeed - ..\..\src\include;..\..\libs\include;..\..\libs\srtp\include;..\..\libs\srtp\crypto\include;..\..\libs\libteletone\src;..\..\libs\win32\sqlite;..\..\libs\pcre;..\..\libs\stfu;..\..\libs\speex\include;..\..\libs\spandsp\src\msvc;..\..\libs\spandsp\src;..\..\libs\tiff-3.8.2\libtiff;%(AdditionalIncludeDirectories) - WIN32;NDEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;STATICLIB;CRASH_PROT;PCRE_STATIC;%(PreprocessorDefinitions) + ..\..\src\include;..\..\libs\include;..\..\libs\srtp\include;..\..\libs\srtp\crypto\include;..\..\libs\libteletone\src;..\..\libs\win32\sqlite;..\..\libs\pcre;..\..\libs\stfu;..\..\libs\speex\include;..\..\libs\spandsp\src\msvc;..\..\libs\spandsp\src;..\..\libs\tiff-3.8.2\libtiff;..\..\libs\libzrtp\include;..\..\libs\libzrtp\third_party\bgaes;..\..\libs\libzrtp\third_party\bnlib;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;STATICLIB;CRASH_PROT;PCRE_STATIC;ENABLE_ZRTP;%(PreprocessorDefinitions) MultiThreadedDLL Use switch.h @@ -247,8 +247,8 @@ if not exist "$(OutDir)htdocs" xcopy "$(SolutionDir)htdocs\*.*" "$(OutDir)htdocs MaxSpeed - ..\..\src\include;..\..\libs\include;..\..\libs\srtp\include;..\..\libs\srtp\crypto\include;..\..\libs\libteletone\src;..\..\libs\win32\sqlite;..\..\libs\pcre;..\..\libs\stfu;..\..\libs\speex\include;..\..\libs\spandsp\src\msvc;..\..\libs\spandsp\src;..\..\libs\tiff-3.8.2\libtiff;%(AdditionalIncludeDirectories) - WIN32;NDEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;STATICLIB;CRASH_PROT;PCRE_STATIC;%(PreprocessorDefinitions) + ..\..\src\include;..\..\libs\include;..\..\libs\srtp\include;..\..\libs\srtp\crypto\include;..\..\libs\libteletone\src;..\..\libs\win32\sqlite;..\..\libs\pcre;..\..\libs\stfu;..\..\libs\speex\include;..\..\libs\spandsp\src\msvc;..\..\libs\spandsp\src;..\..\libs\tiff-3.8.2\libtiff;..\..\libs\libzrtp\include;..\..\libs\libzrtp\third_party\bgaes;..\..\libs\libzrtp\third_party\bnlib;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_WINDOWS;_USRDLL;FREESWITCHCORE_EXPORTS;STATICLIB;CRASH_PROT;PCRE_STATIC;ENABLE_ZRTP;%(PreprocessorDefinitions) MultiThreadedDLL Use switch.h @@ -766,6 +766,9 @@ if not exist "$(OutDir)htdocs" xcopy "$(SolutionDir)htdocs\*.*" "$(OutDir)htdocs {89385c74-5860-4174-9caf-a39e7c48909c} false + + {c13cc324-0032-4492-9a30-310a6bd64ff5} + {1cbb0077-18c5-455f-801c-0a0ce7b0bbf5} diff --git a/w32/Setup/FreeSWITCHConsole.wxs b/w32/Setup/FreeSWITCHConsole.wxs new file mode 100644 index 0000000000..243e614e60 --- /dev/null +++ b/w32/Setup/FreeSWITCHConsole.wxs @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/w32/Setup/Product.wxs b/w32/Setup/Product.wxs index 2f072aa8ca..66bc63b61d 100644 --- a/w32/Setup/Product.wxs +++ b/w32/Setup/Product.wxs @@ -66,12 +66,12 @@ WorkingDirectory="INSTALLLOCATION"/> - - + - + + @@ -87,12 +87,12 @@ - - - - - - + + + + + + diff --git a/w32/Setup/Setup.wixproj b/w32/Setup/Setup.wixproj index fcba3f0ceb..f7873cac08 100644 --- a/w32/Setup/Setup.wixproj +++ b/w32/Setup/Setup.wixproj @@ -36,6 +36,7 @@ + @@ -791,6 +792,7 @@ {1af3a893-f7be-43dd-b697-8ab2397c0d67} True + True Binaries;Content;Satellites INSTALLLOCATION