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

📄 xl_lib.c

📁 用来作为linux中SIP SERVER,完成VOIP网络电话中服务器的功能
💻 C
📖 第 1 页 / 共 2 页
字号:
/** * $Id: xl_lib.c,v 1.13.2.2 2005/04/19 07:55:17 ramona Exp $ * * XLOG module * * Copyright (C) 2001-2003 FhG Fokus * * This file is part of ser, a free SIP server. * * ser 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 * * For a license to use the ser software under conditions * other than those described here, or to purchase support for this * software, please contact iptel.org by e-mail at the following addresses: *    info@iptel.org * * ser 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: * -------- * 2004-10-20 - added header name specifier (ramona) *  */#include <stdio.h>#include <string.h>#include <time.h>#include <sys/types.h>#include <unistd.h>#include "../../dprint.h"#include "../../mem/mem.h"#include "../../ut.h" #include "../../trim.h" #include "../../parser/parse_from.h"#include "../../parser/parse_uri.h"#include "../../parser/parse_hname2.h"#include "xl_lib.h"static str str_null = { "<null>", 6 };static str str_per = { "%", 1 };int msg_id = 0;time_t msg_tm = 0;int cld_pid = 0;static int xl_get_null(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;		res->s = str_null.s;	res->len = str_null.len;	return 0;}static int xl_get_percent(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;		res->s = str_per.s;	res->len = str_per.len;	return 0;}static int xl_get_pid(struct sip_msg *msg, str *res, str *hp, int hi){	int l = 0;	char *ch = NULL;	if(msg==NULL || res==NULL)		return -1;	if(cld_pid == 0)		cld_pid = (int)getpid();	ch = int2str(cld_pid, &l);	res->s = ch;	res->len = l;	return 0;}static int xl_get_times(struct sip_msg *msg, str *res, str *hp, int hi){	int l = 0;	char *ch = NULL;			if(msg==NULL || res==NULL)		return -1;	if(msg_id != msg->id || msg_tm==0)	{		msg_tm = time(NULL);		msg_id = msg->id;	}	ch = int2str(msg_tm, &l);		res->s = ch;	res->len = l;	return 0;}static int xl_get_timef(struct sip_msg *msg, str *res, str *hp, int hi){	char *ch = NULL;		if(msg==NULL || res==NULL)		return -1;	if(msg_id != msg->id || msg_tm==0)	{		msg_tm = time(NULL);		msg_id = msg->id;	}		ch = ctime(&msg_tm);		res->s = ch;	res->len = strlen(ch)-1;	return 0;}static int xl_get_msgid(struct sip_msg *msg, str *res, str *hp, int hi){	int l = 0;	char *ch = NULL;	if(msg==NULL || res==NULL)		return -1;	ch = int2str(msg->id, &l);	res->s = ch;	res->len = l;	return 0;}static int xl_get_method(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;	if(msg->first_line.type == SIP_REQUEST)	{		res->s = msg->first_line.u.request.method.s;		res->len = msg->first_line.u.request.method.len;	}	else		return xl_get_null(msg, res, hp, hi);		return 0;}static int xl_get_status(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;	if(msg->first_line.type == SIP_REPLY)	{		res->s = msg->first_line.u.reply.status.s;		res->len = msg->first_line.u.reply.status.len;			}	else		return xl_get_null(msg, res, hp, hi);		return 0;}static int xl_get_reason(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;	if(msg->first_line.type == SIP_REPLY)	{		res->s = msg->first_line.u.reply.reason.s;		res->len = msg->first_line.u.reply.reason.len;			}	else		return xl_get_null(msg, res, hp, hi);		return 0;}static int xl_get_ruri(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;	if(msg->first_line.type == SIP_REPLY)	/* REPLY doesnt have a ruri */		return xl_get_null(msg, res, hp, hi);	if(msg->parsed_uri_ok==0 /* R-URI not parsed*/ && parse_sip_msg_uri(msg)<0)	{		LOG(L_ERR, "XLOG: xl_get_ruri: ERROR while parsing the R-URI\n");		return xl_get_null(msg, res, hp, hi);	}		if (msg->new_uri.s!=NULL)	{		res->s   = msg->new_uri.s;		res->len = msg->new_uri.len;	} else {		res->s   = msg->first_line.u.request.uri.s;		res->len = msg->first_line.u.request.uri.len;	}		return 0;}static int xl_get_contact(struct sip_msg* msg, str* res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;	if(msg->contact==NULL && parse_headers(msg, HDR_CONTACT, 0)==-1) 	{		DBG("XLOG: xl_get_contact: no contact header\n");		return xl_get_null(msg, res, hp, hi);	}		if(!msg->contact || !msg->contact->body.s || msg->contact->body.len<=0)    {		DBG("XLOG: xl_get_contact: no contact header!\n");		return xl_get_null(msg, res, hp, hi);	}		res->s = msg->contact->body.s;	res->len = msg->contact->body.len;	//	res->s = ((struct to_body*)msg->contact->parsed)->uri.s;//	res->len = ((struct to_body*)msg->contact->parsed)->uri.len;	return 0;}static int xl_get_from(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;	if(parse_from_header(msg)==-1)	{		LOG(L_ERR, "XLOG: xl_get_from: ERROR cannot parse FROM header\n");		return xl_get_null(msg, res, hp, hi);	}		if(msg->from==NULL || get_from(msg)==NULL)		return xl_get_null(msg, res, hp, hi);	res->s = get_from(msg)->uri.s;	res->len = get_from(msg)->uri.len; 		return 0;}static int xl_get_from_tag(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;	if(parse_from_header(msg)==-1)	{		LOG(L_ERR, "XLOG: xl_get_from: ERROR cannot parse FROM header\n");		return xl_get_null(msg, res, hp, hi);	}			if(msg->from==NULL || get_from(msg)==NULL 			|| get_from(msg)->tag_value.s==NULL)		return xl_get_null(msg, res, hp, hi);	res->s = get_from(msg)->tag_value.s;	res->len = get_from(msg)->tag_value.len; 	return 0;}static int xl_get_to(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;	if(msg->to==NULL && parse_headers(msg, HDR_TO, 0)==-1)	{		LOG(L_ERR, "XLOG: xl_get_to: ERROR cannot parse TO header\n");		return xl_get_null(msg, res, hp, hi);	}	if(msg->to==NULL || get_to(msg)==NULL)		return xl_get_null(msg, res, hp, hi);	res->s = get_to(msg)->uri.s;	res->len = get_to(msg)->uri.len; 		return 0;}static int xl_get_to_tag(struct sip_msg* msg, str* res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;	if(msg->to==NULL && ((parse_headers(msg, HDR_TO, 0)==-1) || 				(msg->to==NULL)) )	{		LOG(L_ERR, "XLOG: xl_get_to: ERROR cannot parse TO header\n");		return xl_get_null(msg, res, hp, hi);	}		if (get_to(msg)->tag_value.len <= 0) 		return xl_get_null(msg, res, hp, hi);		res->s = get_to(msg)->tag_value.s;	res->len = get_to(msg)->tag_value.len;	return 0;}static int xl_get_cseq(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;		if(msg->cseq==NULL && ((parse_headers(msg, HDR_CSEQ, 0)==-1) || 				(msg->cseq==NULL)) )	{		LOG(L_ERR, "XLOG: xl_get_cseq: ERROR cannot parse CSEQ header\n");		return xl_get_null(msg, res, hp, hi);	}	res->s = get_cseq(msg)->number.s;	res->len = get_cseq(msg)->number.len;	return 0;}static int xl_get_msg_buf(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;		res->s = msg->buf;	res->len = msg->len;	return 0;}static int xl_get_msg_len(struct sip_msg *msg, str *res, str *hp, int hi){	int l = 0;	char *ch = NULL;	if(msg==NULL || res==NULL)		return -1;	ch = int2str(msg->len, &l);	res->s = ch;	res->len = l;	return 0;}static int xl_get_flags(struct sip_msg *msg, str *res, str *hp, int hi){	int l = 0;	char *ch = NULL;	if(msg==NULL || res==NULL)		return -1;	ch = int2str(msg->flags, &l);	res->s = ch;	res->len = l;	return 0;}static int xl_get_callid(struct sip_msg *msg, str *res, str *hp, int hi){	if(msg==NULL || res==NULL)		return -1;		if(msg->callid==NULL && ((parse_headers(msg, HDR_CALLID, 0)==-1) ||				(msg->callid==NULL)) )	{		LOG(L_ERR, "XLOG: xl_get_callid: ERROR cannot parse Call-Id header\n");		return xl_get_null(msg, res, hp, hi);	}	res->s = msg->callid->body.s;	res->len = msg->callid->body.len;	trim(res);	return 0;}static int xl_get_srcip(struct sip_msg *msg, str *res, str *hp, int hi){    if(msg==NULL || res==NULL)        return -1;    res->s = ip_addr2a(&msg->rcv.src_ip);

⌨️ 快捷键说明

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