1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953 |
- /*$Id$
- *
- * Copyright (C) 2001-2003 FhG Fokus
- *
- * This file is part of Kamailio, a free SIP server.
- *
- * Kamailio is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * Kamailio is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- *
- * History:
- * -------
- * 2003-02-28 scratchpad compatibility abandoned (jiri)
- * 2003-01-29: - rewriting actions (replace, search_append) now begin
- * at the second line -- previously, they could affect
- * first line too, which resulted in wrong calculation of
- * forwarded requests and an error consequently
- * - replace_all introduced
- * 2003-01-28 scratchpad removed (jiri)
- * 2003-01-18 append_urihf introduced (jiri)
- * 2003-03-10 module export interface updated to the new format (andrei)
- * 2003-03-16 flags export parameter added (janakj)
- * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
- * 2003-04-97 actions permitted to be used from failure/reply routes (jiri)
- * 2003-04-21 remove_hf and is_present_hf introduced (jiri)
- * 2003-08-19 subst added (support for sed like res:s/re/repl/flags) (andrei)
- * 2003-08-20 subst_uri added (like above for uris) (andrei)
- * 2003-09-11 updated to new build_lump_rpl() interface (bogdan)
- * 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
- * 2004-05-09: append_time introduced (jiri)
- * 2004-07-06 subst_user added (like subst_uri but only for user) (sobomax)
- * 2004-11-12 subst_user changes (old serdev mails) (andrei)
- * 2005-07-05 is_method("name") to check method using id (ramona)
- * 2006-03-17 applied patch from Marc Haisenko <[email protected]>
- * for adding has_body() function (bogdan)
- * 2008-07-14 Moved some function declarations to a separate file (Ardjan Zwartjes)
- *
- */
- #include "../../action.h"
- #include "../../sr_module.h"
- #include "../../dprint.h"
- #include "../../data_lump.h"
- #include "../../data_lump_rpl.h"
- #include "../../error.h"
- #include "../../mem/mem.h"
- #include "../../str.h"
- #include "../../re.h"
- #include "../../mod_fix.h"
- #include "../../parser/parse_uri.h"
- #include "../../parser/parse_hname2.h"
- #include "../../parser/parse_methods.h"
- #include "../../parser/parse_content.h"
- #include "../../parser/parse_param.h"
- #include "../../parser/sdp/sdp.h"
- #include "../../parser/sdp/sdp_helpr_funcs.h"
- #include "../../lib/kcore/parse_privacy.h"
- #include "../../msg_translator.h"
- #include "../../ut.h"
- #include "../../dset.h"
- #include "../../lib/kcore/cmpapi.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h> /* for regex */
- #include <regex.h>
- #include <time.h>
- #include <sys/time.h>
- #include "textops.h"
- #include "txt_var.h"
- #include "api.h"
- MODULE_VERSION
- /* RFC822-conforming dates format:
- %a -- abbreviated week of day name (locale), %d day of month
- as decimal number, %b abbreviated month name (locale), %Y
- year with century, %T time in 24h notation
- */
- #define TIME_FORMAT "Date: %a, %d %b %Y %H:%M:%S GMT"
- #define MAX_TIME 64
- #define AUDIO_STR "audio"
- #define AUDIO_STR_LEN 5
- static int search_body_f(struct sip_msg*, char*, char*);
- static int search_hf_f(struct sip_msg*, char*, char*, char*);
- static int replace_f(struct sip_msg*, char*, char*);
- static int replace_body_f(struct sip_msg*, char*, char*);
- static int replace_all_f(struct sip_msg*, char*, char*);
- static int replace_body_all_f(struct sip_msg*, char*, char*);
- static int replace_body_atonce_f(struct sip_msg*, char*, char*);
- static int subst_f(struct sip_msg*, char*, char*);
- static int subst_uri_f(struct sip_msg*, char*, char*);
- static int subst_user_f(struct sip_msg*, char*, char*);
- static int subst_body_f(struct sip_msg*, char*, char*);
- static int subst_hf_f(struct sip_msg*, char*, char*, char*);
- static int filter_body_f(struct sip_msg*, char*, char*);
- static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo);
- static int search_append_body_f(struct sip_msg*, char*, char*);
- static int append_to_reply_f(struct sip_msg* msg, char* key, char* str);
- static int append_hf_1(struct sip_msg* msg, char* str1, char* str2);
- static int append_hf_2(struct sip_msg* msg, char* str1, char* str2);
- static int insert_hf_1(struct sip_msg* msg, char* str1, char* str2);
- static int insert_hf_2(struct sip_msg* msg, char* str1, char* str2);
- static int append_urihf(struct sip_msg* msg, char* str1, char* str2);
- static int append_time_f(struct sip_msg* msg, char* , char *);
- static int append_time_request_f(struct sip_msg* msg, char* , char *);
- static int set_body_f(struct sip_msg* msg, char*, char *);
- static int set_rpl_body_f(struct sip_msg* msg, char*, char *);
- static int set_multibody_0(struct sip_msg* msg, char*, char *, char *);
- static int set_multibody_1(struct sip_msg* msg, char*, char *, char *);
- static int set_multibody_2(struct sip_msg* msg, char*, char *, char *);
- static int set_multibody_3(struct sip_msg* msg, char*, char *, char *);
- static int append_multibody_2(struct sip_msg* msg, char*, char *);
- static int append_multibody_3(struct sip_msg* msg, char*, char *, char *);
- static int fixup_multibody_f(void** param, int param_no);
- static int remove_multibody_f(struct sip_msg *msg, char *);
- static int is_method_f(struct sip_msg* msg, char* , char *);
- static int has_body_f(struct sip_msg *msg, char *type, char *str2 );
- static int in_list_f(struct sip_msg* _msg, char* _subject, char* _list,
- char* _sep);
- static int cmp_str_f(struct sip_msg *msg, char *str1, char *str2 );
- static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2 );
- static int starts_with_f(struct sip_msg *msg, char *str1, char *str2 );
- static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo);
- static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo);
- static int is_audio_on_hold_f(struct sip_msg *msg, char *str1, char *str2 );
- static int fixup_substre(void**, int);
- static int hname_fixup(void** param, int param_no);
- static int free_hname_fixup(void** param, int param_no);
- static int fixup_method(void** param, int param_no);
- static int add_header_fixup(void** param, int param_no);
- static int fixup_body_type(void** param, int param_no);
- static int fixup_in_list(void** param, int param_no);
- static int fixup_free_in_list(void** param, int param_no);
- int fixup_regexpNL_none(void** param, int param_no);
- static int fixup_search_hf(void** param, int param_no);
- static int fixup_subst_hf(void** param, int param_no);
- static int mod_init(void);
- static tr_export_t mod_trans[] = {
- { {"re", sizeof("re")-1}, /* regexp class */
- tr_txt_parse_re },
- { { 0, 0 }, 0 }
- };
- static cmd_export_t cmds[]={
- {"search", (cmd_function)search_f, 1,
- fixup_regexp_null, fixup_free_regexp_null,
- ANY_ROUTE},
- {"search_body", (cmd_function)search_body_f, 1,
- fixup_regexp_null, fixup_free_regexp_null,
- ANY_ROUTE},
- {"search_hf", (cmd_function)search_hf_f, 3,
- fixup_search_hf, 0,
- ANY_ROUTE},
- {"search_append", (cmd_function)search_append_f, 2,
- fixup_regexp_none,fixup_free_regexp_none,
- ANY_ROUTE},
- {"search_append_body", (cmd_function)search_append_body_f, 2,
- fixup_regexp_none, fixup_free_regexp_none,
- ANY_ROUTE},
- {"replace", (cmd_function)replace_f, 2,
- fixup_regexp_none, fixup_free_regexp_none,
- ANY_ROUTE},
- {"replace_body", (cmd_function)replace_body_f, 2,
- fixup_regexp_none, fixup_free_regexp_none,
- ANY_ROUTE},
- {"replace_all", (cmd_function)replace_all_f, 2,
- fixup_regexp_none, fixup_free_regexp_none,
- ANY_ROUTE},
- {"replace_body_all", (cmd_function)replace_body_all_f,2,
- fixup_regexp_none, fixup_free_regexp_none,
- ANY_ROUTE},
- {"replace_body_atonce", (cmd_function)replace_body_atonce_f,2,
- fixup_regexpNL_none, fixup_free_regexp_none,
- ANY_ROUTE},
- {"append_to_reply", (cmd_function)append_to_reply_f, 1,
- fixup_spve_null, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
- {"append_hf", (cmd_function)append_hf_1, 1,
- add_header_fixup, 0,
- ANY_ROUTE},
- {"append_hf", (cmd_function)append_hf_2, 2,
- add_header_fixup, 0,
- ANY_ROUTE},
- {"insert_hf", (cmd_function)insert_hf_1, 1,
- add_header_fixup, 0,
- ANY_ROUTE},
- {"insert_hf", (cmd_function)insert_hf_2, 2,
- add_header_fixup, 0,
- ANY_ROUTE},
- {"append_urihf", (cmd_function)append_urihf, 2,
- fixup_str_str, fixup_free_str_str,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
- {"remove_hf", (cmd_function)remove_hf_f, 1,
- hname_fixup, free_hname_fixup,
- ANY_ROUTE},
- {"remove_hf_re", (cmd_function)remove_hf_re_f, 1,
- fixup_regexp_null, fixup_free_regexp_null,
- ANY_ROUTE},
- {"is_present_hf", (cmd_function)is_present_hf_f, 1,
- hname_fixup, free_hname_fixup,
- ANY_ROUTE},
- {"is_present_hf_re", (cmd_function)is_present_hf_re_f,1,
- fixup_regexp_null, fixup_free_regexp_null,
- ANY_ROUTE},
- {"subst", (cmd_function)subst_f, 1,
- fixup_substre, 0,
- ANY_ROUTE},
- {"subst_uri", (cmd_function)subst_uri_f, 1,
- fixup_substre, 0,
- REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
- {"subst_user", (cmd_function)subst_user_f, 1,
- fixup_substre, 0,
- REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
- {"subst_body", (cmd_function)subst_body_f, 1,
- fixup_substre, 0,
- ANY_ROUTE},
- {"subst_hf", (cmd_function)subst_hf_f, 3,
- fixup_subst_hf, 0,
- ANY_ROUTE},
- {"filter_body", (cmd_function)filter_body_f, 1,
- fixup_str_null, 0,
- ANY_ROUTE},
- {"append_time", (cmd_function)append_time_f, 0,
- 0, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
- {"set_body", (cmd_function)set_body_f, 2,
- fixup_spve_spve, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE },
- {"set_reply_body", (cmd_function)set_rpl_body_f, 2,
- fixup_spve_spve, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
- {"is_method", (cmd_function)is_method_f, 1,
- fixup_method, 0,
- ANY_ROUTE},
- {"has_body", (cmd_function)has_body_f, 0,
- 0, 0,
- ANY_ROUTE},
- {"has_body", (cmd_function)has_body_f, 1,
- fixup_body_type, 0,
- ANY_ROUTE},
- {"is_privacy", (cmd_function)is_privacy_f, 1,
- fixup_privacy, 0,
- ANY_ROUTE},
- {"in_list", (cmd_function)in_list_f, 3, fixup_in_list,
- fixup_free_in_list,
- ANY_ROUTE},
- {"cmp_str", (cmd_function)cmp_str_f, 2,
- fixup_spve_spve, 0,
- ANY_ROUTE},
- {"cmp_istr", (cmd_function)cmp_istr_f, 2,
- fixup_spve_spve, 0,
- ANY_ROUTE},
- {"starts_with", (cmd_function)starts_with_f, 2,
- fixup_spve_spve, 0,
- ANY_ROUTE},
- {"is_audio_on_hold", (cmd_function)is_audio_on_hold_f, 0,
- 0, 0,
- ANY_ROUTE},
- {"append_time_to_request", (cmd_function)append_time_request_f, 0,
- 0, 0,
- ANY_ROUTE},
- {"bind_textops", (cmd_function)bind_textops, 0, 0, 0,
- 0},
- {"set_body_multipart", (cmd_function)set_multibody_0, 0,
- 0, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
- {"set_body_multipart", (cmd_function)set_multibody_1, 1,
- fixup_spve_null, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
- {"set_body_multipart", (cmd_function)set_multibody_2, 2,
- fixup_spve_spve, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
- {"set_body_multipart", (cmd_function)set_multibody_3, 3,
- fixup_multibody_f, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
- {"append_body_part", (cmd_function)append_multibody_2, 2,
- fixup_spve_spve, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
- {"append_body_part", (cmd_function)append_multibody_3, 3,
- fixup_multibody_f, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
- {"remove_body_part", (cmd_function)remove_multibody_f, 1,
- fixup_spve_null, 0,
- REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },
- {0,0,0,0,0,0}
- };
- struct module_exports exports= {
- "textops", /* module name*/
- DEFAULT_DLFLAGS, /* dlopen flags */
- cmds, /* exported functions */
- 0, /* module parameters */
- 0, /* exported statistics */
- 0, /* exported MI functions */
- 0, /* exported pseudo-variables */
- 0, /* extra processes */
- mod_init, /* module initialization function */
- 0, /* response function */
- 0, /* destroy function */
- 0, /* per-child init function */
- };
- static int mod_init(void)
- {
- return 0;
- }
- int mod_register(char *path, int *dlflags, void *p1, void *p2)
- {
- return register_trans_mod(path, mod_trans);
- }
- static char *get_header(struct sip_msg *msg)
- {
- return msg->buf+msg->first_line.len;
- }
- int search_f(struct sip_msg* msg, char* key, char* str2)
- {
- /*we registered only 1 param, so we ignore str2*/
- regmatch_t pmatch;
- if (regexec((regex_t*) key, msg->buf, 1, &pmatch, 0)!=0) return -1;
- return 1;
- }
- static int search_body_f(struct sip_msg* msg, char* key, char* str2)
- {
- str body;
- /*we registered only 1 param, so we ignore str2*/
- regmatch_t pmatch;
- body.s = get_body(msg);
- if (body.s==0) {
- LM_ERR("failed to get the message body\n");
- return -1;
- }
- body.len = msg->len -(int)(body.s-msg->buf);
- if (body.len==0) {
- LM_DBG("message body has zero length\n");
- return -1;
- }
- if (regexec((regex_t*) key, body.s, 1, &pmatch, 0)!=0) return -1;
- return 1;
- }
- int search_append_f(struct sip_msg* msg, char* key, char* str2)
- {
- struct lump* l;
- regmatch_t pmatch;
- char* s;
- int len;
- char *begin;
- int off;
- begin=get_header(msg); /* msg->orig/buf previously .. uri problems */
- off=begin-msg->buf;
- if (regexec((regex_t*) key, begin, 1, &pmatch, 0)!=0) return -1;
- if (pmatch.rm_so!=-1){
- if ((l=anchor_lump(msg, off+pmatch.rm_eo, 0, 0))==0)
- return -1;
- len=strlen(str2);
- s=pkg_malloc(len);
- if (s==0){
- LM_ERR("memory allocation failure\n");
- return -1;
- }
- memcpy(s, str2, len);
- if (insert_new_lump_after(l, s, len, 0)==0){
- LM_ERR("could not insert new lump\n");
- pkg_free(s);
- return -1;
- }
- return 1;
- }
- return -1;
- }
- static int search_append_body_f(struct sip_msg* msg, char* key, char* str2)
- {
- struct lump* l;
- regmatch_t pmatch;
- char* s;
- int len;
- int off;
- str body;
- body.s = get_body(msg);
- if (body.s==0) {
- LM_ERR("failed to get the message body\n");
- return -1;
- }
- body.len = msg->len -(int)(body.s-msg->buf);
- if (body.len==0) {
- LM_DBG("message body has zero length\n");
- return -1;
- }
- off=body.s-msg->buf;
- if (regexec((regex_t*) key, body.s, 1, &pmatch, 0)!=0) return -1;
- if (pmatch.rm_so!=-1){
- if ((l=anchor_lump(msg, off+pmatch.rm_eo, 0, 0))==0)
- return -1;
- len=strlen(str2);
- s=pkg_malloc(len);
- if (s==0){
- LM_ERR("memory allocation failure\n");
- return -1;
- }
- memcpy(s, str2, len);
- if (insert_new_lump_after(l, s, len, 0)==0){
- LM_ERR("could not insert new lump\n");
- pkg_free(s);
- return -1;
- }
- return 1;
- }
- return -1;
- }
- static int replace_all_f(struct sip_msg* msg, char* key, char* str2)
- {
- struct lump* l;
- regmatch_t pmatch;
- char* s;
- int len;
- char* begin;
- int off;
- int ret;
- int eflags;
- begin = get_header(msg);
- ret=-1; /* pessimist: we will not find any */
- len=strlen(str2);
- eflags=0; /* match ^ at the beginning of the string*/
- while (begin<msg->buf+msg->len
- && regexec((regex_t*) key, begin, 1, &pmatch, eflags)==0) {
- off=begin-msg->buf;
- if (pmatch.rm_so==-1){
- LM_ERR("offset unknown\n");
- return -1;
- }
- if (pmatch.rm_so==pmatch.rm_eo){
- LM_ERR("matched string is empty... invalid regexp?\n");
- return -1;
- }
- if ((l=del_lump(msg, pmatch.rm_so+off,
- pmatch.rm_eo-pmatch.rm_so, 0))==0) {
- LM_ERR("del_lump failed\n");
- return -1;
- }
- s=pkg_malloc(len);
- if (s==0){
- LM_ERR("memory allocation failure\n");
- return -1;
- }
- memcpy(s, str2, len);
- if (insert_new_lump_after(l, s, len, 0)==0){
- LM_ERR("could not insert new lump\n");
- pkg_free(s);
- return -1;
- }
- /* new cycle */
- begin=begin+pmatch.rm_eo;
- /* is it still a string start */
- if (*(begin-1)=='\n' || *(begin-1)=='\r')
- eflags&=~REG_NOTBOL;
- else
- eflags|=REG_NOTBOL;
- ret=1;
- } /* while found ... */
- return ret;
- }
- static int do_replace_body_f(struct sip_msg* msg, char* key, char* str2, int nobol)
- {
- struct lump* l;
- regmatch_t pmatch;
- char* s;
- int len;
- char* begin;
- int off;
- int ret;
- int eflags;
- str body;
- body.s = get_body(msg);
- if (body.s==0) {
- LM_ERR("failed to get the message body\n");
- return -1;
- }
- body.len = msg->len -(int)(body.s-msg->buf);
- if (body.len==0) {
- LM_DBG("message body has zero length\n");
- return -1;
- }
- begin=body.s;
- ret=-1; /* pessimist: we will not find any */
- len=strlen(str2);
- eflags=0; /* match ^ at the beginning of the string*/
- while (begin<msg->buf+msg->len
- && regexec((regex_t*) key, begin, 1, &pmatch, eflags)==0) {
- off=begin-msg->buf;
- if (pmatch.rm_so==-1){
- LM_ERR("offset unknown\n");
- return -1;
- }
- if (pmatch.rm_so==pmatch.rm_eo){
- LM_ERR("matched string is empty... invalid regexp?\n");
- return -1;
- }
- if ((l=del_lump(msg, pmatch.rm_so+off,
- pmatch.rm_eo-pmatch.rm_so, 0))==0) {
- LM_ERR("del_lump failed\n");
- return -1;
- }
- s=pkg_malloc(len);
- if (s==0){
- LM_ERR("memory allocation failure\n");
- return -1;
- }
- memcpy(s, str2, len);
- if (insert_new_lump_after(l, s, len, 0)==0){
- LM_ERR("could not insert new lump\n");
- pkg_free(s);
- return -1;
- }
- /* new cycle */
- begin=begin+pmatch.rm_eo;
- /* is it still a string start */
- if (nobol && (*(begin-1)=='\n' || *(begin-1)=='\r'))
- eflags&=~REG_NOTBOL;
- else
- eflags|=REG_NOTBOL;
- ret=1;
- } /* while found ... */
- return ret;
- }
- static int replace_body_all_f(struct sip_msg* msg, char* key, char* str2)
- {
- return do_replace_body_f(msg, key, str2, 1);
- }
- static int replace_body_atonce_f(struct sip_msg* msg, char* key, char* str2)
- {
- return do_replace_body_f(msg, key, str2, 0);
- }
- static int replace_f(struct sip_msg* msg, char* key, char* str2)
- {
- struct lump* l;
- regmatch_t pmatch;
- char* s;
- int len;
- char* begin;
- int off;
- begin=get_header(msg); /* msg->orig previously .. uri problems */
- if (regexec((regex_t*) key, begin, 1, &pmatch, 0)!=0) return -1;
- off=begin-msg->buf;
- if (pmatch.rm_so!=-1){
- if ((l=del_lump(msg, pmatch.rm_so+off,
- pmatch.rm_eo-pmatch.rm_so, 0))==0)
- return -1;
- len=strlen(str2);
- s=pkg_malloc(len);
- if (s==0){
- LM_ERR("memory allocation failure\n");
- return -1;
- }
- memcpy(s, str2, len);
- if (insert_new_lump_after(l, s, len, 0)==0){
- LM_ERR("could not insert new lump\n");
- pkg_free(s);
- return -1;
- }
-
- return 1;
- }
- return -1;
- }
- static int replace_body_f(struct sip_msg* msg, char* key, char* str2)
- {
- struct lump* l;
- regmatch_t pmatch;
- char* s;
- int len;
- char* begin;
- int off;
- str body;
- body.s = get_body(msg);
- if (body.s==0) {
- LM_ERR("failed to get the message body\n");
- return -1;
- }
- body.len = msg->len -(int)(body.s-msg->buf);
- if (body.len==0) {
- LM_DBG("message body has zero length\n");
- return -1;
- }
- begin=body.s; /* msg->orig previously .. uri problems */
- if (regexec((regex_t*) key, begin, 1, &pmatch, 0)!=0) return -1;
- off=begin-msg->buf;
- if (pmatch.rm_so!=-1){
- if ((l=del_lump(msg, pmatch.rm_so+off,
- pmatch.rm_eo-pmatch.rm_so, 0))==0)
- return -1;
- len=strlen(str2);
- s=pkg_malloc(len);
- if (s==0){
- LM_ERR("memory allocation failure\n");
- return -1;
- }
- memcpy(s, str2, len);
- if (insert_new_lump_after(l, s, len, 0)==0){
- LM_ERR("could not insert new lump\n");
- pkg_free(s);
- return -1;
- }
-
- return 1;
- }
- return -1;
- }
- /* sed-perl style re: s/regular expression/replacement/flags */
- static int subst_f(struct sip_msg* msg, char* subst, char* ignored)
- {
- struct lump* l;
- struct replace_lst* lst;
- struct replace_lst* rpl;
- char* begin;
- struct subst_expr* se;
- int off;
- int ret;
- int nmatches;
-
- se=(struct subst_expr*)subst;
- begin=get_header(msg); /* start after first line to avoid replacing
- the uri */
- off=begin-msg->buf;
- ret=-1;
- if ((lst=subst_run(se, begin, msg, &nmatches))==0)
- goto error; /* not found */
- for (rpl=lst; rpl; rpl=rpl->next){
- LM_DBG("%s: replacing at offset %d [%.*s] with [%.*s]\n",
- exports.name, rpl->offset+off,
- rpl->size, rpl->offset+off+msg->buf,
- rpl->rpl.len, rpl->rpl.s);
- if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
- goto error;
- /* hack to avoid re-copying rpl, possible because both
- * replace_lst & lumps use pkg_malloc */
- if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0){
- LM_ERR("%s: could not insert new lump\n", exports.name);
- goto error;
- }
- /* hack continued: set rpl.s to 0 so that replace_lst_free will
- * not free it */
- rpl->rpl.s=0;
- rpl->rpl.len=0;
- }
- ret=1;
- error:
- LM_DBG("lst was %p\n", lst);
- if (lst) replace_lst_free(lst);
- if (nmatches<0)
- LM_ERR("%s: subst_run failed\n", exports.name);
- return ret;
- }
- /* sed-perl style re: s/regular expression/replacement/flags, like
- * subst but works on the message uri */
- static int subst_uri_f(struct sip_msg* msg, char* subst, char* ignored)
- {
- char* tmp;
- int len;
- char c;
- struct subst_expr* se;
- str* result;
-
- se=(struct subst_expr*)subst;
- if (msg->new_uri.s){
- len=msg->new_uri.len;
- tmp=msg->new_uri.s;
- }else{
- tmp=msg->first_line.u.request.uri.s;
- len =msg->first_line.u.request.uri.len;
- };
- /* ugly hack: 0 s[len], and restore it afterward
- * (our re functions require 0 term strings), we can do this
- * because we always alloc len+1 (new_uri) and for first_line, the
- * message will always be > uri.len */
- c=tmp[len];
- tmp[len]=0;
- result=subst_str(tmp, msg, se, 0); /* pkg malloc'ed result */
- tmp[len]=c;
- if (result){
- LM_DBG("%s match - old uri= [%.*s], new uri= [%.*s]\n",
- exports.name, len, tmp,
- (result->len)?result->len:0,(result->s)?result->s:"");
- if (msg->new_uri.s) pkg_free(msg->new_uri.s);
- msg->new_uri=*result;
- msg->parsed_uri_ok=0; /* reset "use cached parsed uri" flag */
- ruri_mark_new();
- pkg_free(result); /* free str* pointer */
- return 1; /* success */
- }
- return -1; /* false, no subst. made */
- }
-
- /* sed-perl style re: s/regular expression/replacement/flags, like
- * subst but works on the user part of the uri */
- static int subst_user_f(struct sip_msg* msg, char* subst, char* ignored)
- {
- int rval;
- str* result;
- struct subst_expr* se;
- struct action act;
- struct run_act_ctx h;
- str user;
- char c;
- int nmatches;
- c=0;
- if (parse_sip_msg_uri(msg)<0){
- return -1; /* error, bad uri */
- }
- if (msg->parsed_uri.user.s==0){
- /* no user in uri */
- user.s="";
- user.len=0;
- }else{
- user=msg->parsed_uri.user;
- c=user.s[user.len];
- user.s[user.len]=0;
- }
- se=(struct subst_expr*)subst;
- result=subst_str(user.s, msg, se, &nmatches);/* pkg malloc'ed result */
- if (c) user.s[user.len]=c;
- if (result == NULL) {
- if (nmatches<0)
- LM_ERR("subst_user(): subst_str() failed\n");
- return -1;
- }
- /* result->s[result->len] = '\0'; --subst_str returns 0-term strings */
- memset(&act, 0, sizeof(act)); /* be on the safe side */
- act.type = SET_USER_T;
- act.val[0].type = STRING_ST;
- act.val[0].u.string = result->s;
- init_run_actions_ctx(&h);
- rval = do_action(&h, &act, msg);
- pkg_free(result->s);
- pkg_free(result);
- return rval;
- }
- /* sed-perl style re: s/regular expression/replacement/flags */
- static int subst_body_f(struct sip_msg* msg, char* subst, char* ignored)
- {
- struct lump* l;
- struct replace_lst* lst;
- struct replace_lst* rpl;
- char* begin;
- struct subst_expr* se;
- int off;
- int ret;
- int nmatches;
- str body;
- body.s = get_body(msg);
- if (body.s==0) {
- LM_ERR("failed to get the message body\n");
- return -1;
- }
- body.len = msg->len -(int)(body.s-msg->buf);
- if (body.len==0) {
- LM_DBG("message body has zero length\n");
- return -1;
- }
-
- se=(struct subst_expr*)subst;
- begin=body.s;
-
- off=begin-msg->buf;
- ret=-1;
- if ((lst=subst_run(se, begin, msg, &nmatches))==0)
- goto error; /* not found */
- for (rpl=lst; rpl; rpl=rpl->next){
- LM_DBG("%s replacing at offset %d [%.*s] with [%.*s]\n",
- exports.name, rpl->offset+off,
- rpl->size, rpl->offset+off+msg->buf,
- rpl->rpl.len, rpl->rpl.s);
- if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
- goto error;
- /* hack to avoid re-copying rpl, possible because both
- * replace_lst & lumps use pkg_malloc */
- if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0){
- LM_ERR("%s could not insert new lump\n",
- exports.name);
- goto error;
- }
- /* hack continued: set rpl.s to 0 so that replace_lst_free will
- * not free it */
- rpl->rpl.s=0;
- rpl->rpl.len=0;
- }
- ret=1;
- error:
- LM_DBG("lst was %p\n", lst);
- if (lst) replace_lst_free(lst);
- if (nmatches<0)
- LM_ERR("%s subst_run failed\n", exports.name);
- return ret;
- }
- static inline int find_line_start(char *text, unsigned int text_len,
- char **buf, unsigned int *buf_len)
- {
- char *ch, *start;
- unsigned int len;
- start = *buf;
- len = *buf_len;
- while (text_len <= len) {
- if (strncmp(text, start, text_len) == 0) {
- *buf = start;
- *buf_len = len;
- return 1;
- }
- if ((ch = memchr(start, 13, len - 1))) {
- if (*(ch + 1) != 10) {
- LM_ERR("No LF after CR\n");
- return 0;
- }
- len = len - (ch - start + 2);
- start = ch + 2;
- } else {
- LM_ERR("No CRLF found\n");
- return 0;
- }
- }
- return 0;
- }
- /**
- * return:
- * 1: multipart
- */
- static int check_multipart(struct sip_msg *msg)
- {
- int mime;
- /* the function search for and parses the Content-Type hdr */
- mime = parse_content_type_hdr (msg);
- if(mime<0) {
- LM_ERR("failed to extract content type hdr\n");
- return -1;
- }
- if(mime!=MIMETYPE(MULTIPART,MIXED)) return 0;
- return 1;
- }
- /* Filters multipart/mixed body by leaving out everything else except
- * first body part of given content type. */
- static int filter_body_f(struct sip_msg* msg, char* _content_type,
- char* ignored)
- {
- char *start;
- unsigned int len;
- str *content_type, body;
- str boundary = {0,0};
- body.s = get_body(msg);
- if (body.s == 0) {
- LM_ERR("failed to get the message body\n");
- return -1;
- }
- body.len = msg->len - (int)(body.s - msg->buf);
- if (body.len == 0) {
- LM_DBG("message body has zero length\n");
- return -1;
- }
- if(check_multipart(msg)!=1) {
- LM_WARN("body not multipart\n");
- return -1;
- }
- if(get_boundary(msg, &boundary)!=0) {
- return -1;
- }
- content_type = (str *)_content_type;
- start = body.s;
- len = body.len;
- while (find_line_start("Content-Type: ", 14, &start, &len))
- {
- start = start + 14;
- len = len - 14;
- LM_DBG("line: [%.*s]\n", len, start);
- if (len > content_type->len + 2) {
- if (strncasecmp(start, content_type->s, content_type->len)== 0)
- {
- LM_DBG("found content type %.*s\n",
- content_type->len, content_type->s);
- start = start + content_type->len;
- if ((*start != 13) || (*(start + 1) != 10))
- {
- LM_ERR("no CRLF found after content type\n");
- goto err;
- }
- start = start + 2;
- len = len - content_type->len - 2;
- while ((len > 0) && ((*start == 13) || (*start == 10)))
- {
- len = len - 1;
- start = start + 1;
- }
- if (del_lump(msg, body.s - msg->buf, start - body.s, 0)== 0)
- {
- LM_ERR("deleting lump <%.*s> failed\n",
- (int)(start - body.s), body.s);
- goto err;
- }
- if (find_line_start(boundary.s, boundary.len, &start,
- &len))
- {
- if (del_lump(msg, start - msg->buf, len, 0) == 0)
- {
- LM_ERR("deleting lump <%.*s> failed\n", len, start);
- goto err;
- }
- else
- {
- pkg_free(boundary.s);
- return 1;
- }
- }
- else
- {
- LM_ERR("boundary not found after content\n");
- goto err;
- }
- }
- } else {
- goto err;
- }
- }
- err:
- if(boundary.s) pkg_free(boundary.s);
- return -1;
- }
- int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
- {
- struct hdr_field *hf;
- struct lump* l;
- int cnt;
- gparam_p gp;
- gp = (gparam_p)str_hf;
- cnt=0;
- /* we need to be sure we have seen all HFs */
- parse_headers(msg, HDR_EOH_F, 0);
- for (hf=msg->headers; hf; hf=hf->next) {
- /* for well known header names str_hf->s will be set to NULL
- during parsing of kamailio.cfg and str_hf->len contains
- the header type */
- if(gp->type==GPARAM_TYPE_INT)
- {
- if (gp->v.i!=hf->type)
- continue;
- } else {
- if (hf->name.len!=gp->v.str.len)
- continue;
- if (cmp_hdrname_str(&hf->name, &gp->v.str)!=0)
- continue;
- }
- l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
- if (l==0) {
- LM_ERR("no memory\n");
- return -1;
- }
- cnt++;
- }
- return cnt==0 ? -1 : 1;
- }
- static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo)
- {
- struct hdr_field *hf;
- struct lump* l;
- int cnt;
- regex_t *re;
- char c;
- regmatch_t pmatch;
- re = (regex_t*)key;
- cnt=0;
- /* we need to be sure we have seen all HFs */
- parse_headers(msg, HDR_EOH_F, 0);
- for (hf=msg->headers; hf; hf=hf->next)
- {
- c = hf->name.s[hf->name.len];
- hf->name.s[hf->name.len] = '\0';
- if (regexec(re, hf->name.s, 1, &pmatch, 0)!=0)
- {
- hf->name.s[hf->name.len] = c;
- continue;
- }
- hf->name.s[hf->name.len] = c;
- l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
- if (l==0)
- {
- LM_ERR("cannot remove header\n");
- return -1;
- }
- cnt++;
- }
- return cnt==0 ? -1 : 1;
- }
- static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
- {
- struct hdr_field *hf;
- gparam_p gp;
- gp = (gparam_p)str_hf;
- /* we need to be sure we have seen all HFs */
- parse_headers(msg, HDR_EOH_F, 0);
- for (hf=msg->headers; hf; hf=hf->next) {
- if(gp->type==GPARAM_TYPE_INT)
- {
- if (gp->v.i!=hf->type)
- continue;
- } else {
- if (hf->name.len!=gp->v.str.len)
- continue;
- if (cmp_hdrname_str(&hf->name,&gp->v.str)!=0)
- continue;
- }
- return 1;
- }
- return -1;
- }
- static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo)
- {
- struct hdr_field *hf;
- regex_t *re;
- regmatch_t pmatch;
- char c;
- re = (regex_t*)key;
- /* we need to be sure we have seen all HFs */
- parse_headers(msg, HDR_EOH_F, 0);
- for (hf=msg->headers; hf; hf=hf->next)
- {
- c = hf->name.s[hf->name.len];
- hf->name.s[hf->name.len] = '\0';
- if (regexec(re, hf->name.s, 1, &pmatch, 0)!=0)
- {
- hf->name.s[hf->name.len] = c;
- continue;
- }
- hf->name.s[hf->name.len] = c;
- return 1;
- }
- return -1;
- }
- static int fixup_substre(void** param, int param_no)
- {
- struct subst_expr* se;
- str subst;
- LM_DBG("%s module -- fixing %s\n", exports.name, (char*)(*param));
- if (param_no!=1) return 0;
- subst.s=*param;
- subst.len=strlen(*param);
- se=subst_parser(&subst);
- if (se==0){
- LM_ERR("%s: bad subst. re %s\n", exports.name,
- (char*)*param);
- return E_BAD_RE;
- }
- /* don't free string -- needed for specifiers */
- /* pkg_free(*param); */
- /* replace it with the compiled subst. re */
- *param=se;
- return 0;
- }
- static int append_time_f(struct sip_msg* msg, char* p1, char *p2)
- {
- size_t len;
- char time_str[MAX_TIME];
- time_t now;
- struct tm *bd_time;
- now=time(0);
- bd_time=gmtime(&now);
- if (bd_time==NULL) {
- LM_ERR("gmtime failed\n");
- return -1;
- }
- len=strftime(time_str, MAX_TIME, TIME_FORMAT, bd_time);
- if (len>MAX_TIME-2 || len==0) {
- LM_ERR("unexpected time length\n");
- return -1;
- }
- time_str[len]='\r';
- time_str[len+1]='\n';
- if (add_lump_rpl(msg, time_str, len+2, LUMP_RPL_HDR)==0)
- {
- LM_ERR("unable to add lump\n");
- return -1;
- }
- return 1;
- }
- static int append_time_request_f(struct sip_msg* msg, char* p1, char *p2)
- {
- str time_str = {0, 0};
- time_t now;
- struct tm *bd_time;
- struct hdr_field *hf = msg->headers;
- struct lump *anchor = anchor_lump(msg, hf->name.s + hf->len - msg->buf, 0, 0);
- now=time(0);
- bd_time=gmtime(&now);
- if (bd_time==NULL) {
- LM_ERR("gmtime failed\n");
- goto error;
- }
- time_str.s = pkg_malloc(MAX_TIME);
- time_str.len=strftime(time_str.s, MAX_TIME, TIME_FORMAT, bd_time);
- if (time_str.len>MAX_TIME-2 || time_str.len==0) {
- LM_ERR("unexpected time length\n");
- goto error;
- }
- time_str.s[time_str.len++]='\r';
- time_str.s[time_str.len++]='\n';
- if (anchor == NULL)
- {
- LM_ERR("Problem with getting anchor");
- goto error;
- }
- if (insert_new_lump_after(anchor, time_str.s, time_str.len, 0) == 0)
- {
- LM_ERR("unable to add lump\n");
- goto error;
- }
- return 1;
- error:
- if (time_str.s != NULL)
- pkg_free(time_str.s);
- return -1;
- }
- static int set_body_f(struct sip_msg* msg, char* p1, char* p2)
- {
- struct lump *anchor;
- char* buf;
- int len;
- char* value_s;
- int value_len;
- str body = {0,0};
- str nb = {0,0};
- str nc = {0,0};
- if(p1==0 || p2==0)
- {
- LM_ERR("invalid parameters\n");
- return -1;
- }
- if(fixup_get_svalue(msg, (gparam_p)p1, &nb)!=0)
- {
- LM_ERR("unable to get p1\n");
- return -1;
- }
- if(nb.s==NULL || nb.len == 0)
- {
- LM_ERR("invalid body parameter\n");
- return -1;
- }
- if(fixup_get_svalue(msg, (gparam_p)p2, &nc)!=0)
- {
- LM_ERR("unable to get p2\n");
- return -1;
- }
- if(nc.s==NULL || nc.len == 0)
- {
- LM_ERR("invalid content-type parameter\n");
- return -1;
- }
- body.len = 0;
- body.s = get_body(msg);
- if (body.s==0)
- {
- LM_ERR("malformed sip message\n");
- return -1;
- }
- del_nonshm_lump( &(msg->body_lumps) );
- msg->body_lumps = NULL;
- if (msg->content_length)
- {
- body.len = get_content_length( msg );
- if(body.len > 0)
- {
- if(body.s+body.len>msg->buf+msg->len)
- {
- LM_ERR("invalid content length: %d\n", body.len);
- return -1;
- }
- if(del_lump(msg, body.s - msg->buf, body.len, 0) == 0)
- {
- LM_ERR("cannot delete existing body");
- return -1;
- }
- }
- }
- anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
- if (anchor == 0)
- {
- LM_ERR("failed to get anchor\n");
- return -1;
- }
- if (msg->content_length==0)
- {
- /* need to add Content-Length */
- len = nb.len;
- value_s=int2str(len, &value_len);
- LM_DBG("content-length: %d (%s)\n", value_len, value_s);
- len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
- buf=pkg_malloc(sizeof(char)*(len));
- if (buf==0)
- {
- LM_ERR("out of pkg memory\n");
- return -1;
- }
- memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
- memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
- memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
- if (insert_new_lump_after(anchor, buf, len, 0) == 0)
- {
- LM_ERR("failed to insert content-length lump\n");
- pkg_free(buf);
- return -1;
- }
- }
- /* add content-type */
- if(msg->content_type==NULL || msg->content_type->body.len!=nc.len
- || strncmp(msg->content_type->body.s, nc.s, nc.len)!=0)
- {
- if(msg->content_type!=NULL)
- if(del_lump(msg, msg->content_type->name.s-msg->buf,
- msg->content_type->len, 0) == 0)
- {
- LM_ERR("failed to delete content type\n");
- return -1;
- }
- value_len = nc.len;
- len=sizeof("Content-Type: ") - 1 + value_len + CRLF_LEN;
- buf=pkg_malloc(sizeof(char)*(len));
- if (buf==0)
- {
- LM_ERR("out of pkg memory\n");
- return -1;
- }
- memcpy(buf, "Content-Type: ", sizeof("Content-Type: ") - 1);
- memcpy(buf+sizeof("Content-Type: ") - 1, nc.s, value_len);
- memcpy(buf+sizeof("Content-Type: ") - 1 + value_len, CRLF, CRLF_LEN);
- if (insert_new_lump_after(anchor, buf, len, 0) == 0)
- {
- LM_ERR("failed to insert content-type lump\n");
- pkg_free(buf);
- return -1;
- }
- }
- anchor = anchor_lump(msg, body.s - msg->buf, 0, 0);
- if (anchor == 0)
- {
- LM_ERR("failed to get body anchor\n");
- return -1;
- }
- buf=pkg_malloc(sizeof(char)*(nb.len));
- if (buf==0)
- {
- LM_ERR("out of pkg memory\n");
- return -1;
- }
- memcpy(buf, nb.s, nb.len);
- if (insert_new_lump_after(anchor, buf, nb.len, 0) == 0)
- {
- LM_ERR("failed to insert body lump\n");
- pkg_free(buf);
- return -1;
- }
- LM_DBG("new body: [%.*s]", nb.len, nb.s);
- return 1;
- }
- static int set_rpl_body_f(struct sip_msg* msg, char* p1, char* p2)
- {
- char* buf;
- int len;
- int value_len;
- str nb = {0,0};
- str nc = {0,0};
- if(p1==0 || p2==0)
- {
- LM_ERR("invalid parameters\n");
- return -1;
- }
- if(fixup_get_svalue(msg, (gparam_p)p1, &nb)!=0)
- {
- LM_ERR("unable to get p1\n");
- return -1;
- }
- if(nb.s==NULL || nb.len == 0)
- {
- LM_ERR("invalid body parameter\n");
- return -1;
- }
- if(fixup_get_svalue(msg, (gparam_p)p2, &nc)!=0)
- {
- LM_ERR("unable to get p2\n");
- return -1;
- }
- if(nc.s==NULL || nc.len == 0)
- {
- LM_ERR("invalid content-type parameter\n");
- return -1;
- }
- /* add content-type */
- value_len = nc.len;
- len=sizeof("Content-Type: ") - 1 + value_len + CRLF_LEN;
- buf=pkg_malloc(sizeof(char)*(len));
- if (buf==0)
- {
- LM_ERR("out of pkg memory\n");
- return -1;
- }
- memcpy(buf, "Content-Type: ", sizeof("Content-Type: ") - 1);
- memcpy(buf+sizeof("Content-Type: ") - 1, nc.s, value_len);
- memcpy(buf+sizeof("Content-Type: ") - 1 + value_len, CRLF, CRLF_LEN);
- if (add_lump_rpl(msg, buf, len, LUMP_RPL_HDR) == 0)
- {
- LM_ERR("failed to insert content-type lump\n");
- pkg_free(buf);
- return -1;
- }
- pkg_free(buf);
- if (add_lump_rpl( msg, nb.s, nb.len, LUMP_RPL_BODY)==0) {
- LM_ERR("cannot add body lump\n");
- return -1;
- }
-
- return 1;
- }
- static str* generate_boundary(str *txt, str *content_type,
- str *content_disposition, str *delimiter, unsigned int initial)
- {
- unsigned int i = 0;
- str cth = {"Content-Type: ", 14};
- str cdh = {"Content-Disposition: ", 21};
- str* n;
- unsigned int flag = 0;
- if(txt==NULL||txt->len==0
- ||content_type==NULL||content_type->len==0
- ||delimiter==NULL||delimiter->len==0)
- {
- LM_ERR("invalid parameters\n");
- return NULL;
- }
- if (delimiter->s[0] == '-') {
- LM_ERR("delimiter with initial '-'. Invalid parameter.\n");
- return NULL;
- }
- n = pkg_malloc(sizeof(str));
- if(n==NULL)
- {
- LM_ERR("out of pkg memory\n");
- return NULL;
- }
- n->len = delimiter->len + 2 + CRLF_LEN;
- if(initial) n->len = 2*n->len;
- if(strncmp("\r\n\r\n", txt->s+txt->len-4,4)!=0)
- {
- n->len = n->len + CRLF_LEN;
- flag = 1;
- LM_DBG("adding final CRLF+CRLF\n");
- }
- n->len=n->len + cth.len + content_type->len + 2*CRLF_LEN;
- if(content_disposition->len>0)
- {
- n->len = n->len + cdh.len + content_disposition->len + CRLF_LEN;
- }
- n->len = n->len + txt->len;
- n->s = pkg_malloc(sizeof(char)*(n->len));
- if(n->s==0)
- {
- LM_ERR("out of pkg memory\n");
- pkg_free(n);
- return NULL;
- }
- memset(n->s, 0, sizeof(char)*n->len);
- if(initial)
- {
- memcpy(n->s, "--", 2); i=2;
- memcpy(n->s+i, delimiter->s, delimiter->len); i=i+delimiter->len;
- memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN;
- }
- memcpy(n->s+i, cth.s, cth.len); i=i+cth.len;
- memcpy(n->s+i, content_type->s, content_type->len); i=i+content_type->len;
- memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN;
- if(content_disposition->len>0)
- {
- memcpy(n->s+i, cdh.s, cdh.len); i=i+cdh.len;
- memcpy(n->s+i, content_disposition->s, content_disposition->len);
- i=i+content_disposition->len;
- memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN;
- }
- memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN;
- memcpy(n->s+i, txt->s, txt->len); i=i+txt->len;
- if(flag) { memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN; }
- memcpy(n->s+i, "--", 2); i=i+2;
- memcpy(n->s+i, delimiter->s, delimiter->len); i=i+delimiter->len;
- memcpy(n->s+i, CRLF, CRLF_LEN); i=i+CRLF_LEN;
- if(i!=n->len)
- {
- LM_ERR("out of bounds\n");
- }
- return n;
- }
- int set_multibody_helper(struct sip_msg* msg, char* p1, char* p2, char* p3)
- {
- struct lump *anchor;
- char* buf = NULL;
- int len;
- char* value_s;
- int value_len;
- str body = {0,0};
- str nb = {0,0};
- str oc = {0,0};
- str cd = {0,0};
- str delimiter = {0,0};
- str default_delimiter = {"unique-boundary-1", 17};
- str nc = {0,0};
- str cth = {"Content-Type: ", 14};
- str* nbb = NULL;
- unsigned int convert = 0;
- fparam_t header;
- header.orig = NULL;
- header.type = FPARAM_STR;
- header.v.str.s = "Mime-Version: 1.0\r\n";
- header.v.str.len = 19;
- if(p3==0)
- {
- delimiter.s = default_delimiter.s;
- delimiter.len = default_delimiter.len;
- }
- else
- {
- if(fixup_get_svalue(msg, (gparam_p)p3, &delimiter)!=0)
- {
- LM_ERR("unable to get p3\n");
- return -1;
- }
- if(delimiter.s==NULL || delimiter.len == 0)
- {
- LM_ERR("invalid boundary parameter\n");
- return -1;
- }
- }
- LM_DBG("delimiter<%d>:[%.*s]\n", delimiter.len, delimiter.len, delimiter.s);
- if(p1==0 || p2==0)
- {
- if(check_multipart(msg)==1) {
- LM_WARN("body is already multipart. Do nothing\n");
- return -1;
- }
- convert = 1;
- }
- else
- {
- if(fixup_get_svalue(msg, (gparam_p)p1, &nb)!=0)
- {
- LM_ERR("unable to get p1\n");
- return -1;
- }
- if(nb.s==NULL || nb.len == 0)
- {
- LM_ERR("invalid body parameter\n");
- return -1;
- }
- if(fixup_get_svalue(msg, (gparam_p)p2, &oc)!=0)
- {
- LM_ERR("unable to get p2\n");
- return -1;
- }
- if(oc.s==NULL || oc.len==0)
- {
- LM_ERR("invalid content-type parameter\n");
- return -1;
- }
- if(check_multipart(msg)==1) {
- convert = -1;
- }
- }
- body.len = 0;
- body.s = get_body(msg);
- if(body.s==0)
- {
- LM_ERR("malformed sip message\n");
- return -1;
- }
- body.len = msg->len -(int)(body.s-msg->buf);
- del_nonshm_lump( &(msg->body_lumps) );
- msg->body_lumps = NULL;
- if(msg->content_length)
- {
- if(body.len > 0)
- {
- if(body.s+body.len>msg->buf+msg->len)
- {
- LM_ERR("invalid content length: %d\n", body.len);
- return -1;
- }
- if(convert==1)
- {
- /* need to copy body */
- nb.s=pkg_malloc(sizeof(char)*body.len);
- if (nb.s==0)
- {
- LM_ERR("out of pkg memory\n");
- return -1;
- }
- memcpy(nb.s, body.s, body.len);
- nb.len = body.len;
- if(msg->content_type!=NULL && msg->content_type->body.s!=NULL)
- {
- oc.len = msg->content_type->body.len;
- oc.s=pkg_malloc(sizeof(char)*oc.len);
- if (oc.s==0)
- {
- LM_ERR("out of pkg memory\n");
- goto error;
- }
- memcpy(oc.s, msg->content_type->body.s, oc.len);
- }
- }
- if(del_lump(msg, body.s-msg->buf, body.len, 0) == 0)
- {
- LM_ERR("cannot delete existing body");
- goto error;
- }
- }
- }
- anchor = anchor_lump(msg, msg->unparsed-msg->buf, 0, 0);
- if(anchor==0)
- {
- LM_ERR("failed to get anchor\n");
- goto error;
- }
- /* get initial boundary */
- nbb = generate_boundary(&nb, &oc, &cd, &delimiter, 1);
- if(nbb==NULL)
- {
- LM_ERR("couldn't create initial boundary\n");
- goto error;
- }
- if(msg->content_length==0)
- {
- /* need to add Content-Length */
- len = nbb->len;
- value_s=int2str(len, &value_len);
- len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
- buf=pkg_malloc(sizeof(char)*len);
- if (buf==0)
- {
- LM_ERR("out of pkg memory\n");
- goto error;
- }
- memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
- memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
- memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
- if (insert_new_lump_after(anchor, buf, len, 0) == 0)
- {
- LM_ERR("failed to insert content-length lump\n");
- goto error;
- }
- buf = NULL;
- }
- if(convert!=-1)
- {
- /* set new content type with delimiter */
- nc.len = delimiter.len + 27;
- nc.s = pkg_malloc(sizeof(char)*nc.len);
- memcpy(nc.s, "multipart/mixed;boundary=\"", 26);
- memcpy(nc.s+26, delimiter.s, delimiter.len);
- nc.s[26+delimiter.len] = '"';
- LM_DBG("content-type<%d>:[%.*s]\n", nc.len, nc.len, nc.s);
- /* add content-type */
- if(msg->content_type==NULL || msg->content_type->body.len!=nc.len
- || strncmp(msg->content_type->body.s, nc.s, nc.len)!=0)
- {
- if(msg->content_type!=NULL)
- if(del_lump(msg, msg->content_type->name.s-msg->buf,
- msg->content_type->len, 0) == 0)
- {
- LM_ERR("failed to delete content type\n");
- goto error;
- }
- value_len = nc.len;
- len = cth.len + value_len + CRLF_LEN;
- buf = pkg_malloc(sizeof(char)*len);
- if(buf==0)
- {
- LM_ERR("out of pkg memory\n");
- goto error;
- }
- memcpy(buf, cth.s, cth.len);
- memcpy(buf + cth.len, nc.s, value_len);
- memcpy(buf + cth.len + value_len, CRLF, CRLF_LEN);
- if (insert_new_lump_after(anchor, buf, len, 0) == 0)
- {
- LM_ERR("failed to insert content-type lump\n");
- goto error;
- }
- buf = NULL;
- }
- /* add Mime-Version header */
- if(add_hf_helper(msg, 0, 0, &header, 0, 0)<0)
- {
- LM_ERR("failed to add Mime-Version header\n");
- goto error;
- }
- }
- anchor = anchor_lump(msg, body.s - msg->buf, 0, 0);
- if(anchor==0)
- {
- LM_ERR("failed to get body anchor\n");
- goto error;
- }
- if(insert_new_lump_after(anchor, nbb->s, nbb->len, 0)==0)
- {
- LM_ERR("failed to insert body lump\n");
- goto error;
- }
- pkg_free(nbb);
- if(nc.s!=NULL) pkg_free(nc.s);
- LM_DBG("set flag FL_BODY_MULTIPART\n");
- msg->msg_flags |= FL_BODY_MULTIPART;
- return 1;
- error:
- if(nbb!=NULL) { pkg_free(nbb->s); pkg_free(nbb); }
- if(nc.s!=NULL) pkg_free(nc.s);
- if(buf!=NULL) pkg_free(buf);
- if(convert && nb.s!=NULL) pkg_free(nb.s);
- if(convert && oc.s!=NULL) pkg_free(oc.s);
- return -1;
- }
- static int set_multibody_0(struct sip_msg* msg, char* p1, char* p2, char* p3)
- {
- return set_multibody_helper(msg, NULL, NULL, NULL);
- }
- static int set_multibody_1(struct sip_msg* msg, char* p1, char* p2, char* p3)
- {
- return set_multibody_helper(msg, NULL, NULL, p1);
- }
- static int set_multibody_2(struct sip_msg* msg, char* p1, char* p2, char* p3)
- {
- return set_multibody_helper(msg, p1, p2, NULL);
- }
- static int set_multibody_3(struct sip_msg* msg, char* p1, char* p2, char *p3)
- {
- return set_multibody_helper(msg, p1, p2, p3);
- }
- int append_multibody_helper(struct sip_msg* msg, char* p1, char* p2, char* p3)
- {
- struct lump *l;
- int off;
- str body = {0,0};
- str nc = {0,0};
- str cd = {0,0};
- str txt = {0,0};
- str* nbb = NULL;
- str delimiter = {0,0};
- if(p1==0 || p2==0)
- {
- LM_ERR("invalid parameters\n");
- return -1;
- }
- if(fixup_get_svalue(msg, (gparam_p)p1, &txt)!=0)
- {
- LM_ERR("unable to get p1\n");
- return -1;
- }
- if(txt.s==NULL || txt.len==0)
- {
- LM_ERR("invalid body parameter\n");
- return -1;
- }
- if(fixup_get_svalue(msg, (gparam_p)p2, &nc)!=0)
- {
- LM_ERR("unable to get p2\n");
- return -1;
- }
- if(nc.s==NULL || nc.len==0)
- {
- LM_ERR("invalid content-type parameter\n");
- return -1;
- }
- if(p3!=NULL)
- {
- if(fixup_get_svalue(msg, (gparam_p)p3, &cd)!=0)
- {
- LM_ERR("unable to get p3\n");
- return -1;
- }
- }
- body.s = get_body(msg);
- if(body.s==0) {
- LM_ERR("failed to get the message body\n");
- return -1;
- }
- body.len = msg->len -(int)(body.s-msg->buf);
- if(body.len==0) {
- LM_DBG("message body has zero length\n");
- return -1;
- }
- off=body.s-msg->buf;
- if((l=anchor_lump(msg, off+body.len, 0, 0))==0)
- {
- LM_ERR("WTF\n");
- return -1;
- }
- /* get delimiter no initial -- */
- if(get_mixed_part_delimiter(&msg->content_type->body, &delimiter) < 0) {
- LM_ERR("Cannot get boundary. Is body multipart?\n");
- return -1;
- }
- nbb = generate_boundary(&txt, &nc, &cd, &delimiter, 0);
- if(nbb==NULL)
- {
- LM_ERR("couldn't create initial boundary\n");
- return -1;
- }
- if(insert_new_lump_after(l, nbb->s, nbb->len, 0)==0){
- LM_ERR("could not insert new lump\n");
- pkg_free(nbb->s); pkg_free(nbb);
- return -1;
- }
- pkg_free(nbb);
- if(!(msg->msg_flags&FL_BODY_MULTIPART))
- {
- LM_DBG("set flag FL_BODY_MULTIPART\n");
- msg->msg_flags |= FL_BODY_MULTIPART;
- }
- return 1;
- }
- static int append_multibody_2(struct sip_msg* msg, char* p1, char* p2)
- {
- return append_multibody_helper(msg, p1, p2, NULL);
- }
- static int append_multibody_3(struct sip_msg* msg, char* p1, char* p2, char *p3)
- {
- return append_multibody_helper(msg, p1, p2, p3);
- }
- static int fixup_multibody_f(void** param, int param_no)
- {
- int ret;
- fparam_t* fp;
- if(param_no<=3){
- if((ret=fix_param_types(FPARAM_PVE, param))<0){
- ERR("Cannot convert function parameter %d to spve \n",
- param_no);
- return E_UNSPEC;
- } else {
- fp=(fparam_t*)*param;
- if((ret==0) && (fp->v.pve->spec==0
- || fp->v.pve->spec->getf==0)){
- fparam_free_restore(param);
- return fix_param_types(FPARAM_STR, param);
- } else if(ret==1)
- return fix_param_types(FPARAM_STR, param);
- return ret;
- }
- } else {
- LM_ERR("wrong number of parameters\n");
- return E_UNSPEC;
- }
- }
- static inline int get_line(char *s, int len)
- {
- char *ch;
- if ((ch = memchr(s, 13, len))) {
- if (*(ch + 1) != 10) {
- LM_ERR("No LF after CR\n");
- return 0;
- }
- return ch - s + 2;
- } else {
- LM_ERR("No CRLF found\n");
- return len;
- }
- return 0;
- }
- static int remove_multibody_f(struct sip_msg* msg, char* p1)
- {
- char *start, *end;
- unsigned int len, t;
- str content_type, body;
- str boundary = {0,0};
- if(p1==0)
- {
- LM_ERR("invalid parameters\n");
- return -1;
- }
- if(fixup_get_svalue(msg, (gparam_p)p1, &content_type)!=0)
- {
- LM_ERR("unable to get p1\n");
- return -1;
- }
- body.s = get_body(msg);
- if (body.s == 0) {
- LM_ERR("failed to get the message body\n");
- return -1;
- }
- body.len = msg->len - (int)(body.s - msg->buf);
- if (body.len == 0) {
- LM_DBG("message body has zero length\n");
- return -1;
- }
- if(get_boundary(msg, &boundary)!=0) {
- LM_ERR("Cannot get boundary. Is body multipart?\n");
- return -1;
- }
- start = body.s;
- len = body.len;
- while (find_line_start("Content-Type: ", 14, &start, &len))
- {
- end = start + 14;
- len = len - 14;
- if (len > (content_type.len + 2)) {
- if (strncasecmp(end, content_type.s, content_type.len)== 0)
- {
- LM_DBG("found content type %.*s\n",
- content_type.len, content_type.s);
- end = end + content_type.len;
- if ((*end != 13) || (*(end + 1) != 10))
- {
- LM_ERR("no CRLF found after content type\n");
- goto err;
- }
- end = end + 2;
- len = len - content_type.len - 2;
- if (find_line_start(boundary.s, boundary.len, &end,
- &len))
- {
- LM_DBG("found boundary %.*s\n", boundary.len, boundary.s);
- end = end + boundary.len;
- len = len - boundary.len;
- if (!(t = get_line(end, len))) goto err;
- end += t; len = end-start;
- if (del_lump(msg, start - msg->buf, len, 0) == 0)
- {
- LM_ERR("deleting lump <%.*s> failed\n", len, start);
- goto err;
- }
- pkg_free(boundary.s);
- if(!(msg->msg_flags&FL_BODY_MULTIPART))
- {
- LM_DBG("set flag FL_BODY_MULTIPART\n");
- msg->msg_flags |= FL_BODY_MULTIPART;
- }
- return 1;
- }
- LM_ERR("boundary not found after content\n");
- goto err;
- }
- start = end;
- }
- else goto err;
- }
- err:
- pkg_free(boundary.s);
- return -1;
- }
- static int append_to_reply_f(struct sip_msg* msg, char* key, char* str0)
- {
- str s0;
- if(key==NULL)
- {
- LM_ERR("bad parameters\n");
- return -1;
- }
- if(fixup_get_svalue(msg, (gparam_p)key, &s0)!=0)
- {
- LM_ERR("cannot print the format\n");
- return -1;
- }
-
- if ( add_lump_rpl( msg, s0.s, s0.len, LUMP_RPL_HDR)==0 )
- {
- LM_ERR("unable to add lump_rl\n");
- return -1;
- }
- return 1;
- }
- /* add str1 to end of header or str1.r-uri.str2 */
- int add_hf_helper(struct sip_msg* msg, str *str1, str *str2,
- gparam_p hfval, int mode, gparam_p hfanc)
- {
- struct lump* anchor;
- struct hdr_field *hf;
- char *s;
- int len;
- str s0;
- if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
- LM_ERR("error while parsing message\n");
- return -1;
- }
-
- hf = 0;
- if(hfanc!=NULL) {
- for (hf=msg->headers; hf; hf=hf->next) {
- if(hfanc->type==GPARAM_TYPE_INT)
- {
- if (hfanc->v.i!=hf->type)
- continue;
- } else {
- if (hf->name.len!=hfanc->v.str.len)
- continue;
- if (cmp_hdrname_str(&hf->name,&hfanc->v.str)!=0)
- continue;
- }
- break;
- }
- }
- if(mode == 0) { /* append */
- if(hf==0) { /* after last header */
- anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
- } else { /* after hf */
- anchor = anchor_lump(msg, hf->name.s + hf->len - msg->buf, 0, 0);
- }
- } else { /* insert */
- if(hf==0) { /* before first header */
- anchor = anchor_lump(msg, msg->headers->name.s - msg->buf, 0, 0);
- } else { /* before hf */
- anchor = anchor_lump(msg, hf->name.s - msg->buf, 0, 0);
- }
- }
- if(anchor == 0) {
- LM_ERR("can't get anchor\n");
- return -1;
- }
- if(str1) {
- s0 = *str1;
- } else {
- if(hfval) {
- if(fixup_get_svalue(msg, hfval, &s0)!=0)
- {
- LM_ERR("cannot print the format\n");
- return -1;
- }
- } else {
- s0.len = 0;
- s0.s = 0;
- }
- }
-
- len=s0.len;
- if (str2) len+= str2->len + REQ_LINE(msg).uri.len;
- s = (char*)pkg_malloc(len);
- if (!s) {
- LM_ERR("no pkg memory left\n");
- return -1;
- }
- if (likely(s0.len>0)){
- memcpy(s, s0.s, s0.len);
- }
- if (str2) {
- memcpy(s+s0.len, REQ_LINE(msg).uri.s, REQ_LINE(msg).uri.len);
- memcpy(s+s0.len+REQ_LINE(msg).uri.len, str2->s, str2->len );
- }
- if (insert_new_lump_before(anchor, s, len, 0) == 0) {
- LM_ERR("can't insert lump\n");
- pkg_free(s);
- return -1;
- }
- return 1;
- }
- static int append_hf_1(struct sip_msg *msg, char *str1, char *str2 )
- {
- return add_hf_helper(msg, 0, 0, (gparam_p)str1, 0, 0);
- }
- static int append_hf_2(struct sip_msg *msg, char *str1, char *str2 )
- {
- return add_hf_helper(msg, 0, 0, (gparam_p)str1, 0,
- (gparam_p)str2);
- }
- static int insert_hf_1(struct sip_msg *msg, char *str1, char *str2 )
- {
- return add_hf_helper(msg, 0, 0, (gparam_p)str1, 1, 0);
- }
- static int insert_hf_2(struct sip_msg *msg, char *str1, char *str2 )
- {
- return add_hf_helper(msg, 0, 0, (gparam_p)str1, 1,
- (gparam_p)str2);
- }
- static int append_urihf(struct sip_msg *msg, char *str1, char *str2)
- {
- return add_hf_helper(msg, (str*)str1, (str*)str2, 0, 0, 0);
- }
- static int is_method_f(struct sip_msg *msg, char *meth, char *str2 )
- {
- str *m;
- m = (str*)meth;
- if(msg->first_line.type==SIP_REQUEST)
- {
- if(m->s==0)
- return (msg->first_line.u.request.method_value&m->len)?1:-1;
- else
- return (msg->first_line.u.request.method_value==METHOD_OTHER
- && msg->first_line.u.request.method.len==m->len
- && (strncasecmp(msg->first_line.u.request.method.s, m->s,
- m->len)==0))?1:-1;
- }
- if(parse_headers(msg, HDR_CSEQ_F, 0)!=0 || msg->cseq==NULL)
- {
- LM_ERR("cannot parse cseq header\n");
- return -1; /* should it be 0 ?!?! */
- }
- if(m->s==0)
- return (get_cseq(msg)->method_id&m->len)?1:-1;
- else
- return (get_cseq(msg)->method_id==METHOD_OTHER
- && get_cseq(msg)->method.len==m->len
- && (strncasecmp(get_cseq(msg)->method.s, m->s,
- m->len)==0))?1:-1;
- }
- /*
- * Convert char* header_name to str* parameter
- */
- static int hname_fixup(void** param, int param_no)
- {
- char c;
- struct hdr_field hdr;
- gparam_p gp = NULL;
-
- gp = (gparam_p)pkg_malloc(sizeof(gparam_t));
- if(gp == NULL)
- {
- LM_ERR("no more memory\n");
- return E_UNSPEC;
- }
- memset(gp, 0, sizeof(gparam_t));
- gp->v.str.s = (char*)*param;
- gp->v.str.len = strlen(gp->v.str.s);
- if(gp->v.str.len==0)
- {
- LM_ERR("empty header name parameter\n");
- pkg_free(gp);
- return E_UNSPEC;
- }
-
- c = gp->v.str.s[gp->v.str.len];
- gp->v.str.s[gp->v.str.len] = ':';
- gp->v.str.len++;
-
- if (parse_hname2(gp->v.str.s, gp->v.str.s
- + ((gp->v.str.len<4)?4:gp->v.str.len), &hdr)==0)
- {
- LM_ERR("error parsing header name\n");
- pkg_free(gp);
- return E_UNSPEC;
- }
-
- gp->v.str.len--;
- gp->v.str.s[gp->v.str.len] = c;
- if (hdr.type!=HDR_OTHER_T && hdr.type!=HDR_ERROR_T)
- {
- LM_DBG("using hdr type (%d) instead of <%.*s>\n",
- hdr.type, gp->v.str.len, gp->v.str.s);
- pkg_free(gp->v.str.s);
- gp->v.str.s = NULL;
- gp->v.i = hdr.type;
- gp->type = GPARAM_TYPE_INT;
- } else {
- gp->type = GPARAM_TYPE_STR;
- LM_DBG("using hdr type name <%.*s>\n", gp->v.str.len, gp->v.str.s);
- }
-
- *param = (void*)gp;
- return 0;
- }
- static int free_hname_fixup(void** param, int param_no)
- {
- if(*param)
- {
- if(((gparam_p)(*param))->type==GPARAM_TYPE_STR)
- pkg_free(((gparam_p)(*param))->v.str.s);
- pkg_free(*param);
- *param = 0;
- }
- return 0;
- }
- /*
- * Convert char* method to str* parameter
- */
- static int fixup_method(void** param, int param_no)
- {
- str* s;
- char *p;
- int m;
- unsigned int method;
-
- s = (str*)pkg_malloc(sizeof(str));
- if (!s) {
- LM_ERR("no pkg memory left\n");
- return E_UNSPEC;
- }
- s->s = (char*)*param;
- s->len = strlen(s->s);
- if(s->len==0)
- {
- LM_ERR("empty method name\n");
- pkg_free(s);
- return E_UNSPEC;
- }
- m=0;
- p=s->s;
- while(*p)
- {
- if(*p=='|')
- {
- *p = ',';
- m=1;
- }
- p++;
- }
- if(parse_methods(s, &method)!=0)
- {
- LM_ERR("bad method names\n");
- pkg_free(s);
- return E_UNSPEC;
- }
- if(m==1)
- {
- if(method==METHOD_UNDEF || method&METHOD_OTHER)
- {
- LM_ERR("unknown method in list [%.*s/%d] - must be only defined methods\n",
- s->len, s->s, method);
- pkg_free(s);
- return E_UNSPEC;
- }
- LM_DBG("using id for methods [%.*s/%d]\n",
- s->len, s->s, method);
- s->s = 0;
- s->len = method;
- } else {
- if(method!=METHOD_UNDEF && method!=METHOD_OTHER)
- {
- LM_DBG("using id for method [%.*s/%d]\n",
- s->len, s->s, method);
- s->s = 0;
- s->len = method;
- } else
- LM_DBG("name for method [%.*s/%d]\n",
- s->len, s->s, method);
- }
- *param = (void*)s;
- return 0;
- }
- /*
- * Convert char* privacy value to corresponding bit value
- */
- int fixup_privacy(void** param, int param_no)
- {
- str p;
- unsigned int val;
- p.s = (char*)*param;
- p.len = strlen(p.s);
- if (p.len == 0) {
- LM_ERR("empty privacy value\n");
- return E_UNSPEC;
- }
- if (parse_priv_value(p.s, p.len, &val) != p.len) {
- LM_ERR("invalid privacy value\n");
- return E_UNSPEC;
- }
-
- *param = (void *)(long)val;
- return 0;
- }
- /*
- * Fix in_list params: subject and list (strings that may contain pvars),
- * separator (string)
- */
- static int fixup_in_list(void** param, int param_no)
- {
- if ((param_no == 1) || (param_no == 2)) return fixup_spve_null(param, 1);
-
- if (param_no == 3) {
- if ((strlen((char *)*param) != 1) || (*((char *)(*param)) == 0)) {
- LM_ERR("invalid separator parameter\n");
- return -1;
- }
- return 0;
- }
-
- LM_ERR("invalid parameter number <%d>\n", param_no);
- return -1;
- }
-
- /*
- * Free in_list params
- */
- static int fixup_free_in_list(void** param, int param_no)
- {
- if ((param_no == 1) || (param_no == 2)) {
- return fixup_free_spve_null(param, 1);
- }
-
- if (param_no == 3) return 0;
-
- LM_ERR("invalid parameter number <%d>\n", param_no);
- return -1;
- }
- static int add_header_fixup(void** param, int param_no)
- {
- if(param_no==1)
- {
- return fixup_spve_null(param, param_no);
- } else if(param_no==2) {
- return hname_fixup(param, param_no);
- } else {
- LM_ERR("wrong number of parameters\n");
- return E_UNSPEC;
- }
- }
- static int fixup_body_type(void** param, int param_no)
- {
- char *p;
- char *r;
- unsigned int type;
- if(param_no==1) {
- p = (char*)*param;
- if (p==0 || p[0]==0) {
- type = 0;
- } else {
- r = decode_mime_type( p, p+strlen(p) , &type);
- if (r==0) {
- LM_ERR("unsupported mime <%s>\n",p);
- return E_CFG;
- }
- if ( r!=p+strlen(p) ) {
- LM_ERR("multiple mimes not supported!\n");
- return E_CFG;
- }
- }
- pkg_free(*param);
- *param = (void*)(long)type;
- }
- return 0;
- }
- static int has_body_f(struct sip_msg *msg, char *type, char *str2 )
- {
- int mime;
- /* parse content len hdr */
- if ( msg->content_length==NULL &&
- (parse_headers(msg,HDR_CONTENTLENGTH_F, 0)==-1||msg->content_length==NULL))
- return -1;
- if (get_content_length (msg)==0) {
- LM_DBG("content length is zero\n");
- /* Nothing to see here, please move on. */
- return -1;
- }
- /* check type also? */
- if (type==0)
- return 1;
- /* the function search for and parses the Content-Type hdr */
- mime = parse_content_type_hdr (msg);
- if (mime<0) {
- LM_ERR("failed to extract content type hdr\n");
- return -1;
- }
- if (mime==0) {
- /* content type hdr not found -> according the RFC3261 we
- * assume APPLICATION/SDP --bogdan */
- mime = ((TYPE_APPLICATION << 16) + SUBTYPE_SDP);
- }
- LM_DBG("content type is %d\n",mime);
- if ( (unsigned int)mime!=(unsigned int)(unsigned long)type )
- return -1;
- return 1;
- }
- int is_privacy_f(struct sip_msg *msg, char *_privacy, char *str2 )
- {
- if (parse_privacy(msg) == -1)
- return -1;
- return get_privacy_values(msg) & ((unsigned int)(long)_privacy) ? 1 : -1;
- }
- /*
- * Checks if subject is found in list
- */
- int in_list_f(struct sip_msg* _m, char* _subject, char* _list, char* _sep)
- {
- str subject, list;
- int sep;
- char *at, *past, *next_sep, *s;
- if (fixup_get_svalue(_m, (gparam_p)_subject, &subject) != 0) {
- LM_ERR("cannot get subject value\n");
- return -1;
- } else {
- if (subject.len == 0) {
- LM_ERR("subject cannot be empty string\n");
- return -1;
- }
- }
- if (fixup_get_svalue(_m, (gparam_p)_list, &list) != 0) {
- LM_ERR("cannot get list value\n");
- return -1;
- } else {
- if (list.len == 0) return -1;
- }
- sep = _sep[0];
- at = list.s;
- past = list.s + list.len;
- /* Eat leading white space */
- while ((at < past) &&
- ((*at == ' ') || (*at == '\t') || (*at == '\r') || (*at == '\n') )) {
- at++;
- }
- while (at < past) {
- next_sep = index(at, sep);
- s = next_sep;
-
- if (s == NULL) {
- /* Eat trailing white space */
- while ((at < past) &&
- ((*(past-1) == ' ') || (*(past-1) == '\t') || (*(past-1) == '\r') || (*(past-1) == '\n') )) {
- past--;
- }
- if ((subject.len == (past - at)) &&
- strncmp(at, subject.s, subject.len) == 0) {
- return 1;
- } else {
- return -1;
- }
- } else {
- /* Eat trailing white space */
- while ((at < s) &&
- ((*(s-1) == ' ') || (*(s-1) == '\t') || (*(s-1) == '\r') || (*(s-1) == '\n') )) {
- s--;
- }
- if ((subject.len == (s - at)) &&
- strncmp(at, subject.s, subject.len) == 0) {
- return 1;
- } else {
- at = next_sep + 1;
- /* Eat leading white space */
- while ((at < past) &&
- ((*at == ' ') || (*at == '\t') || (*at == '\r') || (*at == '\n') )) {
- at++;
- }
- }
- }
- }
- return -1;
- }
- static int cmp_str_f(struct sip_msg *msg, char *str1, char *str2 )
- {
- str s1;
- str s2;
- int ret;
- if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0)
- {
- LM_ERR("cannot get first parameter\n");
- return -8;
- }
- if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0)
- {
- LM_ERR("cannot get second parameter\n");
- return -8;
- }
- ret = cmp_str(&s1, &s2);
- if(ret==0)
- return 1;
- if(ret>0)
- return -1;
- return -2;
- }
- static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2)
- {
- str s1;
- str s2;
- int ret;
- if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0)
- {
- LM_ERR("cannot get first parameter\n");
- return -8;
- }
- if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0)
- {
- LM_ERR("cannot get second parameter\n");
- return -8;
- }
- ret = cmpi_str(&s1, &s2);
- if(ret==0)
- return 1;
- if(ret>0)
- return -1;
- return -2;
- }
- static int starts_with_f(struct sip_msg *msg, char *str1, char *str2 )
- {
- str s1;
- str s2;
- int ret;
- if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0)
- {
- LM_ERR("cannot get first parameter\n");
- return -8;
- }
- if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0)
- {
- LM_ERR("cannot get second parameter\n");
- return -8;
- }
- if (s1.len < s2.len) return -1;
- ret = strncmp(s1.s, s2.s, s2.len);
- if(ret==0)
- return 1;
- if(ret>0)
- return -1;
- return -2;
- }
- static int is_audio_on_hold_f(struct sip_msg *msg, char *str1, char *str2 )
- {
- int sdp_session_num = 0, sdp_stream_num;
- sdp_session_cell_t* sdp_session;
- sdp_stream_cell_t* sdp_stream;
- if (0 == parse_sdp(msg)) {
- for(;;) {
- sdp_session = get_sdp_session(msg, sdp_session_num);
- if(!sdp_session) break;
- sdp_stream_num = 0;
- for(;;) {
- sdp_stream = get_sdp_stream(msg, sdp_session_num, sdp_stream_num);
- if(!sdp_stream) break;
- if(sdp_stream->media.len==AUDIO_STR_LEN &&
- strncmp(sdp_stream->media.s,AUDIO_STR,AUDIO_STR_LEN)==0 &&
- sdp_stream->is_on_hold)
- return 1;
- sdp_stream_num++;
- }
- sdp_session_num++;
- }
- }
- return -1;
- }
- int fixup_regexpNL_none(void** param, int param_no)
- {
- regex_t* re;
- if (param_no != 1 && param_no != 2 )
- {
- LM_ERR("invalid parameter number %d\n", param_no);
- return E_UNSPEC;
- }
- if (param_no == 2)
- return 0;
- /* param 1 */
- if ((re=pkg_malloc(sizeof(regex_t)))==0) {
- LM_ERR("no more pkg memory\n");
- return E_OUT_OF_MEM;
- }
- if (regcomp(re, *param, REG_EXTENDED|REG_ICASE)) {
- pkg_free(re);
- LM_ERR("bad re %s\n", (char*)*param);
- return E_BAD_RE;
- }
- /* free string */
- pkg_free(*param);
- /* replace it with the compiled re */
- *param=re;
- return 0;
- }
- /*! \brief
- * fixup for functions that get two parameters
- * - first parameter is converted to regular expression structure
- * - second parameter is not converted
- */
- int fixup_regexp_none(void** param, int param_no)
- {
- if (param_no != 1 && param_no != 2 )
- {
- LM_ERR("invalid parameter number %d\n", param_no);
- return E_UNSPEC;
- }
- if (param_no == 1)
- return fixup_regexp_null(param, 1);
- return 0;
- }
- /**
- * fixup free for functions that get two parameters
- * - first parameter was converted to regular expression
- * - second parameter was notconverted
- */
- int fixup_free_regexp_none(void** param, int param_no)
- {
- if (param_no != 1 && param_no != 2 )
- {
- LM_ERR("invalid parameter number %d\n", param_no);
- return E_UNSPEC;
- }
- if (param_no == 1)
- return fixup_free_regexp_null(param, 1);
- return 0;
- }
- /**
- *
- */
- static int search_hf_f(struct sip_msg* msg, char* str_hf, char* re, char *flags)
- {
- hdr_field_t *hf;
- hdr_field_t *hfl = NULL;
- str body;
- gparam_t *gp;
- regmatch_t pmatch;
- char c;
- int ret;
- gp = (gparam_t*)str_hf;
- /* we need to be sure we have seen all HFs */
- parse_headers(msg, HDR_EOH_F, 0);
- for (hf=msg->headers; hf; hf=hf->next) {
- if(gp->type==GPARAM_TYPE_INT)
- {
- if (gp->v.i!=hf->type)
- continue;
- } else {
- if (hf->name.len!=gp->v.str.len)
- continue;
- if (cmp_hdrname_str(&hf->name,&gp->v.str)!=0)
- continue;
- }
- if(flags==NULL || *flags!='l')
- {
- body = hf->body;
- c = body.s[body.len];
- body.s[body.len] = '\0';
- ret = regexec((regex_t*) re, body.s, 1, &pmatch, 0);
- body.s[body.len] = c;
- if(ret==0)
- {
- /* match */
- if(flags==NULL || *flags!='l')
- return 1;
- } else {
- if(flags!=NULL && *flags=='f')
- return 1;
- }
- } else {
- hfl = hf;
- }
- }
- if(hfl!=NULL)
- {
- hf = hfl;
- body = hf->body;
- c = body.s[body.len];
- body.s[body.len] = '\0';
- ret = regexec((regex_t*) re, body.s, 1, &pmatch, 0);
- body.s[body.len] = c;
- if(ret==0)
- return 1;
- }
- return -1;
- }
- /*
- * Convert header name, regexp and flags
- */
- static int fixup_search_hf(void** param, int param_no)
- {
- if(param_no==1)
- return hname_fixup(param, param_no);
- if(param_no==2)
- return fixup_regexp_null(param, 1);
- return 0;
- }
- /* sed-perl style re: s/regular expression/replacement/flags */
- static int subst_hf_f(struct sip_msg *msg, char *str_hf, char *subst, char *flags)
- {
- struct lump* l;
- struct replace_lst* lst = NULL;
- struct replace_lst* rpl = NULL;
- char* begin;
- struct subst_expr* se;
- int off;
- int nmatches=0;
- str body;
- hdr_field_t *hf;
- hdr_field_t *hfl = NULL;
- gparam_t *gp;
- char c;
- int ret;
- ret = -1;
- gp = (gparam_t*)str_hf;
- se=(struct subst_expr*)subst;
- /* we need to be sure we have seen all HFs */
- parse_headers(msg, HDR_EOH_F, 0);
- for (hf=msg->headers; hf; hf=hf->next) {
- if(gp->type==GPARAM_TYPE_INT)
- {
- if (gp->v.i!=hf->type)
- continue;
- } else {
- if (hf->name.len!=gp->v.str.len)
- continue;
- if (cmp_hdrname_str(&hf->name,&gp->v.str)!=0)
- continue;
- }
- if(flags==NULL || *flags!='l')
- {
- body = hf->body;
- c = body.s[body.len];
- body.s[body.len] = '\0';
- begin=body.s;
- off=begin-msg->buf;
- lst=subst_run(se, begin, msg, &nmatches);
- body.s[body.len] = c;
- if(lst==0 && flags!=NULL && *flags=='f')
- goto error; /* not found */
- if(lst!=0)
- ret=1;
- for (rpl=lst; rpl; rpl=rpl->next)
- {
- LM_DBG("%s replacing at offset %d [%.*s] with [%.*s]\n",
- exports.name, rpl->offset+off,
- rpl->size, rpl->offset+off+msg->buf,
- rpl->rpl.len, rpl->rpl.s);
- if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
- {
- ret=-1;
- goto error;
- }
- /* hack to avoid re-copying rpl, possible because both
- * replace_lst & lumps use pkg_malloc */
- if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0)
- {
- LM_ERR("%s could not insert new lump\n",
- exports.name);
- ret=-1;
- goto error;
- }
- /* hack continued: set rpl.s to 0 so that replace_lst_free will
- * not free it */
- rpl->rpl.s=0;
- rpl->rpl.len=0;
- }
- } else {
- hfl = hf;
- }
- }
- if(hfl!=NULL)
- {
- hf= hfl;
- body = hf->body;
- c = body.s[body.len];
- body.s[body.len] = '\0';
- begin=body.s;
- off=begin-msg->buf;
- lst=subst_run(se, begin, msg, &nmatches);
- body.s[body.len] = c;
- if(lst==0)
- goto error; /* not found */
- ret=1;
- for (rpl=lst; rpl; rpl=rpl->next)
- {
- LM_DBG("%s replacing at offset %d [%.*s] with [%.*s]\n",
- exports.name, rpl->offset+off,
- rpl->size, rpl->offset+off+msg->buf,
- rpl->rpl.len, rpl->rpl.s);
- if ((l=del_lump(msg, rpl->offset+off, rpl->size, 0))==0)
- {
- ret=-1;
- goto error;
- }
- /* hack to avoid re-copying rpl, possible because both
- * replace_lst & lumps use pkg_malloc */
- if (insert_new_lump_after(l, rpl->rpl.s, rpl->rpl.len, 0)==0)
- {
- LM_ERR("%s could not insert new lump\n",
- exports.name);
- ret=-1;
- goto error;
- }
- /* hack continued: set rpl.s to 0 so that replace_lst_free will
- * not free it */
- rpl->rpl.s=0;
- rpl->rpl.len=0;
- }
- }
- error:
- LM_DBG("lst was %p\n", lst);
- if (lst) replace_lst_free(lst);
- if (nmatches<0)
- LM_ERR("%s subst_run failed\n", exports.name);
- return ret;
- }
- /*
- * Convert header name, substexp and flags
- */
- static int fixup_subst_hf(void** param, int param_no)
- {
- if(param_no==1)
- return hname_fixup(param, param_no);
- if(param_no==2)
- return fixup_substre(param, 1);
- return 0;
- }
|