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

📄 textops.c

📁 性能优秀的SIP Proxy
💻 C
📖 第 1 页 / 共 3 页
字号:
	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){			LOG(L_ERR, "ERROR: replace_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_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) {		LOG(L_ERR,				"ERROR:replace_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:replace_body_f: 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){			LOG(L_ERR, "ERROR: replace_body_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_f: 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){		DBG(" %s: subst_f: 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){			LOG(L_ERR, "ERROR: %s: subst_f: 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:	DBG("subst_f: lst was %p\n", lst);	if (lst) replace_lst_free(lst);	if (nmatches<0)		LOG(L_ERR, "ERROR: %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){		DBG("%s: subst_uri_f: 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 */		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;	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)			LOG(L_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.p1_type = STRING_ST;	act.p1.string = result->s;	rval = do_action(&act, msg);	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) {		LOG(L_ERR,				"ERROR:subst_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:subst_body_f: 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){		DBG("%s:subst_body_f: 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){			LOG(L_ERR, "ERROR:%s: subst_body_f: 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:	DBG("subst_body_f: lst was %p\n", lst);	if (lst) replace_lst_free(lst);	if (nmatches<0)		LOG(L_ERR, "ERROR:%s:subst_body_f: subst_run failed\n", exports.name);	return ret;}static int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo){	struct hdr_field *hf;	struct lump* l;	int cnt;	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) {		if(((str*)str_hf)->s==NULL)		{			if (((str*)str_hf)->len!=hf->type)				continue;		} else {			if (hf->name.len!=((str*)str_hf)->len)				continue;			if (strncasecmp(hf->name.s, ((str*)str_hf)->s, hf->name.len)!=0)				continue;		}		l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);		if (l==0) {			LOG(L_ERR, "ERROR: remove_hf_f: no memory\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;	/* 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(((str*)str_hf)->s==NULL)		{			if (((str*)str_hf)->len!=hf->type)				continue;		} else {			if (hf->name.len!=((str*)str_hf)->len)				continue;			if (strncasecmp(hf->name.s,((str*)str_hf)->s,hf->name.len)!=0)				continue;		}		return 1;	}	return -1;}static int fixup_regex(void** param, int param_no){	regex_t* re;	DBG("module - fixing %s\n", (char*)(*param));	if (param_no!=1) return 0;	if ((re=pkg_malloc(sizeof(regex_t)))==0) return E_OUT_OF_MEM;	if (regcomp(re, *param, REG_EXTENDED|REG_ICASE|REG_NEWLINE) ){		pkg_free(re);		LOG(L_ERR, "ERROR: %s : bad re %s\n", exports.name, (char*)*param);		return E_BAD_RE;	}	/* free string */	pkg_free(*param);	/* replace it with the compiled re */	*param=re;	return 0;}static int fixup_substre(void** param, int param_no){	struct subst_expr* se;	str subst;	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){		LOG(L_ERR, "ERROR: %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) {		LOG(L_ERR, "ERROR: append_time: gmtime failed\n");		return -1;	}	len=strftime(time_str, MAX_TIME, TIME_FORMAT, bd_time);	if (len>MAX_TIME-2 || len==0) {		LOG(L_ERR, "ERROR: append_time: 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)	{		LOG(L_ERR, "ERROR: append_time: unable to add lump\n");		return -1;	}	return 1;}static int append_to_reply_f(struct sip_msg* msg, char* key, char* str0){	xl_elem_t *model;	str s0;	if(key==NULL)	{		LOG(L_ERR,"ERROR:append_to_reply: error - bad parameters\n");		return -1;	}

⌨️ 快捷键说明

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