⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textops.c

📁 性能优秀的SIP Proxy
💻 C
📖 第 1 页 / 共 3 页
字号:
/*$Id: textops.c,v 1.18 2006/04/05 08:44:25 bogdan_iancu Exp $ * * Copyright (C) 2001-2003 FhG Fokus * * This file is part of openser, a free SIP server. * * openser 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 * * openser 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 <haisenko@comdasys.com>  *              for adding has_body() function (bogdan) * */#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 "../../parser/parse_uri.h"#include "../../parser/parse_hname2.h"#include "../../parser/parse_methods.h"#include "../../parser/parse_content.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>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 64static int search_f(struct sip_msg*, char*, char*);static int search_body_f(struct sip_msg*, 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 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 remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo);static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo);static int search_append_f(struct sip_msg*, char*, char*);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 is_method_f(struct sip_msg* msg, char* , char *);static int has_body_f(struct sip_msg *msg, char *type, char *str2 );static int fixup_regex(void**, int);static int fixup_substre(void**, int);static int str_fixup(void** param, int param_no);static int 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 it_list_fixup(void** param, int param_no);static int fixup_body_type(void** param, int param_no);static int mod_init(void);static cmd_export_t cmds[]={	{"search",           search_f,          1, fixup_regex, 			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"search_body",      search_body_f,     1, fixup_regex, 			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"search_append",    search_append_f,   2, fixup_regex, 			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, 	{"search_append_body", search_append_body_f,   2, fixup_regex, 			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, 	{"replace",          replace_f,         2, fixup_regex, 			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, 	{"replace_body",     replace_body_f,    2, fixup_regex, 			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, 	{"replace_all",      replace_all_f,     2, fixup_regex, 			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, 	{"replace_body_all", replace_body_all_f,2, fixup_regex, 			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, 	{"append_to_reply",  append_to_reply_f, 1, it_list_fixup,			REQUEST_ROUTE|BRANCH_ROUTE},	{"append_hf",        append_hf_1,       1, add_header_fixup,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"append_hf",        append_hf_2,       2, add_header_fixup,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"insert_hf",        insert_hf_1,       1, add_header_fixup,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"insert_hf",        insert_hf_2,       2, add_header_fixup,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"append_urihf",     append_urihf,      2, str_fixup,   			REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"remove_hf",        remove_hf_f,       1, hname_fixup,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, 	{"is_present_hf",    is_present_hf_f,   1, hname_fixup,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, 	{"subst",            subst_f,           1, fixup_substre,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, 	{"subst_uri",        subst_uri_f,       1, fixup_substre,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, 	{"subst_user",       subst_user_f,      1, fixup_substre,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"subst_body",       subst_body_f,      1, fixup_substre,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"append_time",      append_time_f,     0, 0,			REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE },	{"is_method",        is_method_f,       1, fixup_method,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"has_body",         has_body_f,        0, 0,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{"has_body",         has_body_f,        1, fixup_body_type,			REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},	{0,0,0,0,0}};struct module_exports exports= {	"textops",  /* module name*/	cmds,       /* exported functions */	0,          /* module parameters */	0,          /* exported statistics */	mod_init,   /* module initialization function */	0,          /* response function */	0,          /* destroy function */	0,          /* per-child init function */};static int mod_init(void){	LOG(L_INFO, "TextOPS - initializing\n");	return 0;}static char *get_header(struct sip_msg *msg){	return msg->buf+msg->first_line.len;}static 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) {		LOG(L_ERR, "ERROR:search_body_f: failed to get the message body\n");		return -1;	}	body.len = msg->len -(int)(body.s-msg->buf);	if (body.len==0) {		DBG("ERROR:search_body_f: message body has zero length\n");		return -1;	}	if (regexec((regex_t*) key, body.s, 1, &pmatch, 0)!=0) return -1;	return 1;}static 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){			LOG(L_ERR, "ERROR: search_append_f: mem. allocation failure\n");			return -1;		}		memcpy(s, str2, len); 		if (insert_new_lump_after(l, s, len, 0)==0){			LOG(L_ERR, "ERROR: 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) {		LOG(L_ERR,				"ERROR:search_append_body_f: failed to get the message body\n");		return -1;	}	body.len = msg->len -(int)(body.s-msg->buf);	if (body.len==0) {		DBG("ERROR:search_append_body_f: 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){			LOG(L_ERR, "ERROR: search_append_f: mem. allocation failure\n");			return -1;		}		memcpy(s, str2, len); 		if (insert_new_lump_after(l, s, len, 0)==0){			LOG(L_ERR, "ERROR: 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); /* msg->orig previously .. uri problems */	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;		/* change eflags, not to match any more at string start */		eflags|=REG_NOTBOL;		if (pmatch.rm_so==-1){			LOG(L_ERR, "ERROR: replace_all_f: offset unknown\n");			return -1;		}		if ((l=del_lump(msg, pmatch.rm_so+off,						pmatch.rm_eo-pmatch.rm_so, 0))==0) {			LOG(L_ERR, "ERROR: replace_all_f: del_lump failed\n");			return -1;		}		s=pkg_malloc(len);		if (s==0){			LOG(L_ERR, "ERROR: replace_all_f: mem. allocation failure\n");			return -1;		}		memcpy(s, str2, len); 		if (insert_new_lump_after(l, s, len, 0)==0){			LOG(L_ERR, "ERROR: replace_all_f: could not insert new lump\n");			pkg_free(s);			return -1;		}		/* new cycle */		begin=begin+pmatch.rm_eo;		ret=1;	} /* while found ... */	return ret;}static int replace_body_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;	str body;	body.s = get_body(msg);	if (body.s==0) {		LOG(L_ERR,				"ERROR:replace_body_all_f: failed to get the message body\n");		return -1;	}	body.len = msg->len -(int)(body.s-msg->buf);	if (body.len==0) {		DBG("ERROR:replace_body_all_f: message body has zero length\n");		return -1;	}	begin=body.s; /* msg->orig previously .. uri problems */	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;		/* change eflags, not to match any more at string start */		eflags|=REG_NOTBOL;		if (pmatch.rm_so==-1){			LOG(L_ERR, "ERROR: replace_body_all_f: offset unknown\n");			return -1;		}		if ((l=del_lump(msg, pmatch.rm_so+off,						pmatch.rm_eo-pmatch.rm_so, 0))==0) {			LOG(L_ERR, "ERROR: replace_body_all_f: del_lump failed\n");			return -1;		}		s=pkg_malloc(len);		if (s==0){			LOG(L_ERR, "ERROR: replace_body_all_f: mem. allocation failure\n");			return -1;		}		memcpy(s, str2, len); 		if (insert_new_lump_after(l, s, len, 0)==0){			LOG(L_ERR,				"ERROR: replace_body_all_f: could not insert new lump\n");			pkg_free(s);			return -1;		}		/* new cycle */		begin=begin+pmatch.rm_eo;		ret=1;	} /* while found ... */	return ret;}static int replace_f(struct sip_msg* msg, char* key, char* str2){	struct lump* l;	regmatch_t pmatch;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -